showkey: code shrink

function                                             old     new   delta
signal_handler                                        52      45      -7
showkey_main                                         461     454      -7
xset1                                                 40      29     -11

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
This commit is contained in:
Denys Vlasenko 2010-08-29 05:00:40 +02:00
parent 140ac91fe1
commit cc131534e2

View File

@ -13,21 +13,19 @@
// set raw tty mode // set raw tty mode
// also used by microcom // also used by microcom
// libbb candidates? // libbb candidates?
static void xget1(int fd, struct termios *t, struct termios *oldt) static void xget1(struct termios *t, struct termios *oldt)
{ {
tcgetattr(fd, oldt); tcgetattr(STDIN_FILENO, oldt);
*t = *oldt; *t = *oldt;
cfmakeraw(t); cfmakeraw(t);
} }
static int xset1(int fd, struct termios *tio, const char *device) static void xset1(struct termios *tio)
{ {
int ret = tcsetattr(fd, TCSAFLUSH, tio); int ret = tcsetattr(STDIN_FILENO, TCSAFLUSH, tio);
if (ret) { if (ret) {
bb_perror_msg("can't tcsetattr for %s", device); bb_perror_msg("can't tcsetattr for stdin");
} }
return ret;
} }
/* /*
@ -49,7 +47,7 @@ struct globals {
static void signal_handler(int signo) static void signal_handler(int signo)
{ {
// restore keyboard and console settings // restore keyboard and console settings
xset1(STDIN_FILENO, &tio0, "stdin"); xset1(&tio0);
xioctl(STDIN_FILENO, KDSKBMODE, (void *)(ptrdiff_t)kbmode); xioctl(STDIN_FILENO, KDSKBMODE, (void *)(ptrdiff_t)kbmode);
// alarmed? -> exit 0 // alarmed? -> exit 0
exit(SIGALRM == signo); exit(SIGALRM == signo);
@ -79,15 +77,15 @@ int showkey_main(int argc UNUSED_PARAM, char **argv)
, (option_mask32 & OPT_a) ? "when CTRL+D pressed" : "10s after last keypress" , (option_mask32 & OPT_a) ? "when CTRL+D pressed" : "10s after last keypress"
); );
// prepare for raw mode // prepare for raw mode
xget1(STDIN_FILENO, &tio, &tio0); xget1(&tio, &tio0);
// put stdin in raw mode // put stdin in raw mode
xset1(STDIN_FILENO, &tio, "stdin"); xset1(&tio);
if (option_mask32 & OPT_a) { if (option_mask32 & OPT_a) {
char c; unsigned char c;
// just read stdin char by char // just read stdin char by char
while (1 == safe_read(STDIN_FILENO, &c, 1)) { while (1 == safe_read(STDIN_FILENO, &c, 1)) {
printf("%3d 0%03o 0x%02x\r\n", c, c, c); printf("%3u 0%03o 0x%02x\r\n", c, c, c);
if (04 /*CTRL-D*/ == c) if (04 /*CTRL-D*/ == c)
break; break;
} }
@ -114,16 +112,18 @@ int showkey_main(int argc UNUSED_PARAM, char **argv)
// show interpreted scancodes (default) ? -> // show interpreted scancodes (default) ? ->
} else { } else {
int kc; int kc;
if (i+2 < n && (c & 0x7f) == 0 if (i+2 < n
&& (buf[i+1] & 0x80) != 0 && (c & 0x7f) == 0
&& (buf[i+2] & 0x80) != 0) { && (buf[i+1] & 0x80) != 0
&& (buf[i+2] & 0x80) != 0
) {
kc = ((buf[i+1] & 0x7f) << 7) | (buf[i+2] & 0x7f); kc = ((buf[i+1] & 0x7f) << 7) | (buf[i+2] & 0x7f);
i += 3; i += 3;
} else { } else {
kc = (c & 0x7f); kc = (c & 0x7f);
i++; i++;
} }
printf("keycode %3d %s", kc, (c & 0x80) ? "release" : "press"); printf("keycode %3u %s", kc, (c & 0x80) ? "release" : "press");
} }
} }
puts("\r"); puts("\r");