aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDenys Vlasenko <vda.linux@googlemail.com>2010-08-29 05:00:40 +0200
committerDenys Vlasenko <vda.linux@googlemail.com>2010-08-29 05:00:40 +0200
commitcc131534e2bc96a5005dc98a9559de009a68068a (patch)
treefdd0936f98cd5f1e2d752fe014ca99c65cf38ee4
parent140ac91fe154c9a1bb33cba38380c3cc81e1cf15 (diff)
downloadbusybox-w32-cc131534e2bc96a5005dc98a9559de009a68068a.tar.gz
busybox-w32-cc131534e2bc96a5005dc98a9559de009a68068a.tar.bz2
busybox-w32-cc131534e2bc96a5005dc98a9559de009a68068a.zip
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>
-rw-r--r--console-tools/showkey.c32
1 files changed, 16 insertions, 16 deletions
diff --git a/console-tools/showkey.c b/console-tools/showkey.c
index ef16f6fb1..149ea6465 100644
--- a/console-tools/showkey.c
+++ b/console-tools/showkey.c
@@ -13,21 +13,19 @@
13// set raw tty mode 13// set raw tty mode
14// also used by microcom 14// also used by microcom
15// libbb candidates? 15// libbb candidates?
16static void xget1(int fd, struct termios *t, struct termios *oldt) 16static void xget1(struct termios *t, struct termios *oldt)
17{ 17{
18 tcgetattr(fd, oldt); 18 tcgetattr(STDIN_FILENO, oldt);
19 *t = *oldt; 19 *t = *oldt;
20 cfmakeraw(t); 20 cfmakeraw(t);
21} 21}
22 22
23static int xset1(int fd, struct termios *tio, const char *device) 23static void xset1(struct termios *tio)
24{ 24{
25 int ret = tcsetattr(fd, TCSAFLUSH, tio); 25 int ret = tcsetattr(STDIN_FILENO, TCSAFLUSH, tio);
26
27 if (ret) { 26 if (ret) {
28 bb_perror_msg("can't tcsetattr for %s", device); 27 bb_perror_msg("can't tcsetattr for stdin");
29 } 28 }
30 return ret;
31} 29}
32 30
33/* 31/*
@@ -49,7 +47,7 @@ struct globals {
49static void signal_handler(int signo) 47static void signal_handler(int signo)
50{ 48{
51 // restore keyboard and console settings 49 // restore keyboard and console settings
52 xset1(STDIN_FILENO, &tio0, "stdin"); 50 xset1(&tio0);
53 xioctl(STDIN_FILENO, KDSKBMODE, (void *)(ptrdiff_t)kbmode); 51 xioctl(STDIN_FILENO, KDSKBMODE, (void *)(ptrdiff_t)kbmode);
54 // alarmed? -> exit 0 52 // alarmed? -> exit 0
55 exit(SIGALRM == signo); 53 exit(SIGALRM == signo);
@@ -79,15 +77,15 @@ int showkey_main(int argc UNUSED_PARAM, char **argv)
79 , (option_mask32 & OPT_a) ? "when CTRL+D pressed" : "10s after last keypress" 77 , (option_mask32 & OPT_a) ? "when CTRL+D pressed" : "10s after last keypress"
80 ); 78 );
81 // prepare for raw mode 79 // prepare for raw mode
82 xget1(STDIN_FILENO, &tio, &tio0); 80 xget1(&tio, &tio0);
83 // put stdin in raw mode 81 // put stdin in raw mode
84 xset1(STDIN_FILENO, &tio, "stdin"); 82 xset1(&tio);
85 83
86 if (option_mask32 & OPT_a) { 84 if (option_mask32 & OPT_a) {
87 char c; 85 unsigned char c;
88 // just read stdin char by char 86 // just read stdin char by char
89 while (1 == safe_read(STDIN_FILENO, &c, 1)) { 87 while (1 == safe_read(STDIN_FILENO, &c, 1)) {
90 printf("%3d 0%03o 0x%02x\r\n", c, c, c); 88 printf("%3u 0%03o 0x%02x\r\n", c, c, c);
91 if (04 /*CTRL-D*/ == c) 89 if (04 /*CTRL-D*/ == c)
92 break; 90 break;
93 } 91 }
@@ -114,16 +112,18 @@ int showkey_main(int argc UNUSED_PARAM, char **argv)
114 // show interpreted scancodes (default) ? -> 112 // show interpreted scancodes (default) ? ->
115 } else { 113 } else {
116 int kc; 114 int kc;
117 if (i+2 < n && (c & 0x7f) == 0 115 if (i+2 < n
118 && (buf[i+1] & 0x80) != 0 116 && (c & 0x7f) == 0
119 && (buf[i+2] & 0x80) != 0) { 117 && (buf[i+1] & 0x80) != 0
118 && (buf[i+2] & 0x80) != 0
119 ) {
120 kc = ((buf[i+1] & 0x7f) << 7) | (buf[i+2] & 0x7f); 120 kc = ((buf[i+1] & 0x7f) << 7) | (buf[i+2] & 0x7f);
121 i += 3; 121 i += 3;
122 } else { 122 } else {
123 kc = (c & 0x7f); 123 kc = (c & 0x7f);
124 i++; 124 i++;
125 } 125 }
126 printf("keycode %3d %s", kc, (c & 0x80) ? "release" : "press"); 126 printf("keycode %3u %s", kc, (c & 0x80) ? "release" : "press");
127 } 127 }
128 } 128 }
129 puts("\r"); 129 puts("\r");