2005-09-20 13:26:39 +00:00
|
|
|
/*
|
|
|
|
A small utility to print the resulting key codes from pressing a
|
|
|
|
key. Servers the same function as hitting ^V in bash, but I prefer
|
|
|
|
the way key_reader works.
|
|
|
|
|
|
|
|
Type ^C to exit the program.
|
|
|
|
*/
|
2006-05-02 16:29:50 +00:00
|
|
|
#include "config.h"
|
|
|
|
|
2005-09-20 13:26:39 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <termios.h>
|
|
|
|
#include <unistd.h>
|
2005-09-25 11:32:13 +00:00
|
|
|
#include <locale.h>
|
2005-09-20 13:26:39 +00:00
|
|
|
#include <termcap.h>
|
|
|
|
|
2006-03-10 13:43:17 +00:00
|
|
|
#include "fallback.h"
|
|
|
|
|
2005-09-22 20:16:52 +00:00
|
|
|
#include "input_common.h"
|
2005-09-20 13:26:39 +00:00
|
|
|
|
|
|
|
int writestr( char *str )
|
|
|
|
{
|
|
|
|
write( 1, str, strlen(str) );
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int main( int argc, char **argv)
|
|
|
|
{
|
2012-01-05 21:58:48 +00:00
|
|
|
set_main_thread();
|
2005-09-25 11:32:13 +00:00
|
|
|
setlocale( LC_ALL, "" );
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2005-09-25 11:32:13 +00:00
|
|
|
|
2005-09-20 13:26:39 +00:00
|
|
|
if( argc == 2 )
|
|
|
|
{
|
|
|
|
static char term_buffer[2048];
|
|
|
|
char *termtype = getenv ("TERM");
|
|
|
|
char *tbuff = malloc( sizeof(char)*9999);
|
|
|
|
char *res;
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2005-09-20 13:26:39 +00:00
|
|
|
tgetent( term_buffer, termtype );
|
2011-12-27 03:18:46 +00:00
|
|
|
res = tgetstr( argv[1], &tbuff );
|
2005-09-20 13:26:39 +00:00
|
|
|
if( res != 0 )
|
|
|
|
{
|
|
|
|
while( *res != 0 )
|
2011-12-27 03:18:46 +00:00
|
|
|
{
|
2005-09-20 13:26:39 +00:00
|
|
|
printf("%d ", *res );
|
|
|
|
|
|
|
|
|
|
|
|
res++;
|
|
|
|
}
|
2011-12-27 03:18:46 +00:00
|
|
|
printf( "\n" );
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
printf("Undefined sequence\n");
|
2011-12-27 03:18:46 +00:00
|
|
|
}
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
char scratch[1024];
|
|
|
|
unsigned int c;
|
|
|
|
|
|
|
|
struct termios modes, /* so we can change the modes */
|
|
|
|
savemodes; /* so we can reset the modes when we're done */
|
|
|
|
|
2005-09-22 20:16:52 +00:00
|
|
|
input_common_init(0);
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2005-09-22 20:16:52 +00:00
|
|
|
|
2005-09-20 13:26:39 +00:00
|
|
|
tcgetattr(0,&modes); /* get the current terminal modes */
|
|
|
|
savemodes = modes; /* save a copy so we can reset them */
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2005-09-20 13:26:39 +00:00
|
|
|
modes.c_lflag &= ~ICANON; /* turn off canonical mode */
|
|
|
|
modes.c_lflag &= ~ECHO; /* turn off echo mode */
|
|
|
|
modes.c_cc[VMIN]=1;
|
|
|
|
modes.c_cc[VTIME]=0;
|
|
|
|
tcsetattr(0,TCSANOW,&modes); /* set the new modes */
|
|
|
|
while(1)
|
|
|
|
{
|
2005-09-22 20:16:52 +00:00
|
|
|
if( (c=input_common_readch(0)) == EOF )
|
2005-09-20 13:26:39 +00:00
|
|
|
break;
|
2005-09-22 20:16:52 +00:00
|
|
|
if( (c > 31) && (c != 127) )
|
2005-09-20 13:26:39 +00:00
|
|
|
sprintf( scratch, "dec: %d hex: %x char: %c\n", c, c, c );
|
|
|
|
else
|
|
|
|
sprintf( scratch, "dec: %d hex: %x\n", c, c );
|
2011-12-27 03:18:46 +00:00
|
|
|
writestr( scratch );
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
|
|
|
/* reset the terminal to the saved mode */
|
2011-12-27 03:18:46 +00:00
|
|
|
tcsetattr(0,TCSANOW,&savemodes);
|
2005-09-22 20:16:52 +00:00
|
|
|
|
|
|
|
input_common_destroy();
|
2011-12-27 03:18:46 +00:00
|
|
|
}
|
2005-09-20 13:26:39 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|