2014-02-28 10:15:24 +00:00
|
|
|
/*
|
2005-09-20 13:26:39 +00:00
|
|
|
A small utility to print the resulting key codes from pressing a
|
2012-11-18 10:23:22 +00:00
|
|
|
key. Servers the same function as hitting ^V in bash, but I prefer
|
|
|
|
the way key_reader works.
|
2005-09-20 13:26:39 +00:00
|
|
|
|
2012-11-18 10:23:22 +00:00
|
|
|
Type ^C to exit the program.
|
2005-09-20 13:26:39 +00:00
|
|
|
*/
|
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>
|
|
|
|
|
2012-05-09 10:30:09 +00:00
|
|
|
#include "common.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
|
|
|
|
2012-11-19 00:30:30 +00:00
|
|
|
int writestr(char *str)
|
2005-09-20 13:26:39 +00:00
|
|
|
{
|
2012-11-19 00:30:30 +00:00
|
|
|
write(1, str, strlen(str));
|
|
|
|
return 0;
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
|
|
|
|
2012-11-19 00:30:30 +00:00
|
|
|
int main(int argc, char **argv)
|
2005-09-20 13:26:39 +00:00
|
|
|
{
|
2012-11-19 00:30:30 +00:00
|
|
|
set_main_thread();
|
2012-03-07 19:35:22 +00:00
|
|
|
setup_fork_guards();
|
2012-11-19 00:30:30 +00:00
|
|
|
setlocale(LC_ALL, "");
|
2012-11-18 10:23:22 +00:00
|
|
|
|
|
|
|
|
2012-11-19 00:30:30 +00:00
|
|
|
if (argc == 2)
|
2012-11-18 10:23:22 +00:00
|
|
|
{
|
2012-11-19 00:30:30 +00:00
|
|
|
static char term_buffer[2048];
|
|
|
|
char *termtype = getenv("TERM");
|
|
|
|
char *tbuff = new char[9999];
|
|
|
|
char *res;
|
|
|
|
|
|
|
|
tgetent(term_buffer, termtype);
|
|
|
|
res = tgetstr(argv[1], &tbuff);
|
|
|
|
if (res != 0)
|
|
|
|
{
|
|
|
|
while (*res != 0)
|
|
|
|
{
|
|
|
|
printf("%d ", *res);
|
|
|
|
|
|
|
|
|
|
|
|
res++;
|
|
|
|
}
|
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
printf("Undefined sequence\n");
|
|
|
|
}
|
2012-11-18 10:23:22 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-11-19 00:30:30 +00:00
|
|
|
char scratch[1024];
|
|
|
|
unsigned int c;
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2012-11-19 00:30:30 +00:00
|
|
|
struct termios modes, /* so we can change the modes */
|
|
|
|
savemodes; /* so we can reset the modes when we're done */
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2012-11-19 00:30:30 +00:00
|
|
|
input_common_init(0);
|
2012-11-18 10:23:22 +00:00
|
|
|
|
|
|
|
|
2012-11-19 00:30:30 +00:00
|
|
|
tcgetattr(0,&modes); /* get the current terminal modes */
|
|
|
|
savemodes = modes; /* save a copy so we can reset them */
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2012-11-19 00:30:30 +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)
|
2012-11-18 10:23:22 +00:00
|
|
|
{
|
2012-11-19 00:30:30 +00:00
|
|
|
if ((c=input_common_readch(0)) == EOF)
|
|
|
|
break;
|
|
|
|
if ((c > 31) && (c != 127))
|
|
|
|
sprintf(scratch, "dec: %d hex: %x char: %c\n", c, c, c);
|
|
|
|
else
|
|
|
|
sprintf(scratch, "dec: %d hex: %x\n", c, c);
|
|
|
|
writestr(scratch);
|
|
|
|
}
|
|
|
|
/* reset the terminal to the saved mode */
|
|
|
|
tcsetattr(0,TCSANOW,&savemodes);
|
|
|
|
|
|
|
|
input_common_destroy();
|
2012-11-18 10:23:22 +00:00
|
|
|
}
|
|
|
|
|
2012-11-19 00:30:30 +00:00
|
|
|
return 0;
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|