synchronize history patches with busybox git
[busybox-power] / debian / patches / obselete / showkey-any-stdin.patch
1 Pull patch from BusyBox' git repo to make "showkey -a" work on any stdin
2 Source: http://git.busybox.net/busybox/commit/?id=9beb68e3e2dc1c6f457acfb307cfed73cce65cd9
3 ---
4
5 --- a/console-tools/showkey.c
6 +++ b/console-tools/showkey.c
7 @@ -56,37 +56,45 @@ int showkey_main(int argc UNUSED_PARAM, 
8         // FIXME: aks are all mutually exclusive
9         getopt32(argv, "aks");
10  
11 -       // get keyboard settings
12 -       xioctl(STDIN_FILENO, KDGKBMODE, &kbmode);
13 -       printf("kb mode was %s\n\nPress any keys. Program terminates %s\n\n",
14 -               kbmode == K_RAW ? "RAW" :
15 -                       (kbmode == K_XLATE ? "XLATE" :
16 -                               (kbmode == K_MEDIUMRAW ? "MEDIUMRAW" :
17 -                                       (kbmode == K_UNICODE ? "UNICODE" : "UNKNOWN")))
18 -               , (option_mask32 & OPT_a) ? "on EOF (ctrl-D)" : "10s after last keypress"
19 -       );
20 -
21         // prepare for raw mode
22         xget1(&tio, &tio0);
23         // put stdin in raw mode
24         xset1(&tio);
25  
26 +#define press_keys "Press any keys, program terminates %s:\r\n\n"
27 +
28         if (option_mask32 & OPT_a) {
29 +               // just read stdin char by char
30                 unsigned char c;
31  
32 -               // just read stdin char by char
33 +               printf(press_keys, "on EOF (ctrl-D)");
34 +
35 +               // read and show byte values
36                 while (1 == read(STDIN_FILENO, &c, 1)) {
37                         printf("%3u 0%03o 0x%02x\r\n", c, c, c);
38                         if (04 /*CTRL-D*/ == c)
39                                 break;
40                 }
41 +
42         } else {
43 +                       // we assume a PC keyboard
44 +                       xioctl(STDIN_FILENO, KDGKBMODE, &kbmode);
45 +                       printf("Keyboard mode was %s.\r\n\n",
46 +                               kbmode == K_RAW ? "RAW" :
47 +                                       (kbmode == K_XLATE ? "XLATE" :
48 +                                               (kbmode == K_MEDIUMRAW ? "MEDIUMRAW" :
49 +                                                       (kbmode == K_UNICODE ? "UNICODE" : "UNKNOWN")))
50 +                       );
51 +
52                 // set raw keyboard mode
53                 xioctl(STDIN_FILENO, KDSKBMODE, (void *)(ptrdiff_t)((option_mask32 & OPT_k) ? K_MEDIUMRAW : K_RAW));
54  
55                 // we should exit on any signal; signals should interrupt read
56                 bb_signals_recursive_norestart(BB_FATAL_SIGS, record_signo);
57  
58 +               // inform user that program ends after time of inactivity
59 +               printf(press_keys, "10s after last keypress");
60 +
61                 // read and show scancodes
62                 while (!bb_got_signal) {
63                         char buf[18];
64 @@ -94,6 +102,7 @@ int showkey_main(int argc UNUSED_PARAM, 
65  
66                         // setup 10s watchdog
67                         alarm(10);
68 +
69                         // read scancodes
70                         n = read(STDIN_FILENO, buf, sizeof(buf));
71                         i = 0;
72 @@ -121,11 +130,13 @@ int showkey_main(int argc UNUSED_PARAM, 
73                         }
74                         puts("\r");
75                 }
76 +
77 +               // restore keyboard mode
78 +               xioctl(STDIN_FILENO, KDSKBMODE, (void *)(ptrdiff_t)kbmode);
79         }
80  
81 -       // restore keyboard and console settings
82 +       // restore console settings
83         xset1(&tio0);
84 -       xioctl(STDIN_FILENO, KDSKBMODE, (void *)(ptrdiff_t)kbmode);
85  
86         return EXIT_SUCCESS;
87  }