Hi!

The appended patch (based on current snapshot) allow the usage of
busybox showkey for display of key sequences on none local tty's like
serial lines and pty's (in ASCII mode, option -a). Currently showkey
fails on those tty types due to the lack of the keyboard functionality.
So the appropriate ioctl to read the keyboard mode fails before the
option is checked and only key sequences are displayed. Keyboard mode
mangling is not required for that purpose, so we just have to change the
order of things a bit.

In addition I did some very small improvements on the text output and
source code readability.

[To Denys question ... after some long delay: Yes I can (and do) provide
patches, if I have an environment that allows me to do that. As last
time I wasn't able to produce a patch, I decided to send the modified
source file (in my assumption, you have all busybox sources and are able
to do just a diff on them). Sorry, for this, if I was wrong ... let to
this, that I lost the changes and had to do the modifications again ...
not that friendly]

--
Harald
diff -bdur busybox-20110224/console-tools/showkey.c 
busybox-20110224-hb/console-tools/showkey.c
--- busybox-20110224/console-tools/showkey.c    2011-02-23 01:20:44.000000000 
+0100
+++ busybox-20110224-hb/console-tools/showkey.c 2011-02-24 15:09:36.000000000 
+0100
@@ -51,42 +51,56 @@
                OPT_s = (1<<2), // display only the raw scan-codes
        };
 
+       static const char mode_was[] = "Keyboard mode was %s.\r\n\n";
+       static const char press_keys[] = "Press any keys, program terminates 
%s:\r\n\n";
+       static const char on_eof[] = "on EOF (ctrl-D)";
+       static const char on_inactivity[] = "10s after last keypress";
+
        INIT_G();
 
        // FIXME: aks are all mutually exclusive
        getopt32(argv, "aks");
 
-       // get keyboard settings
-       xioctl(STDIN_FILENO, KDGKBMODE, &kbmode);
-       printf("kb mode was %s\n\nPress any keys. Program terminates %s\n\n",
-               kbmode == K_RAW ? "RAW" :
-                       (kbmode == K_XLATE ? "XLATE" :
-                               (kbmode == K_MEDIUMRAW ? "MEDIUMRAW" :
-                                       (kbmode == K_UNICODE ? "UNICODE" : 
"UNKNOWN")))
-               , (option_mask32 & OPT_a) ? "on EOF (ctrl-D)" : "10s after last 
keypress"
-       );
-
        // prepare for raw mode
        xget1(&tio, &tio0);
        // put stdin in raw mode
        xset1(&tio);
 
        if (option_mask32 & OPT_a) {
+
+               // just read stdin char by char, don't assume a local keyboard
                unsigned char c;
 
-               // just read stdin char by char
+               // inform user that program ends on eof (ctrl-d)
+               printf(press_keys, on_eof);
+
+               // read and show byte values
                while (1 == read(STDIN_FILENO, &c, 1)) {
                        printf("%3u 0%03o 0x%02x\r\n", c, c, c);
                        if (04 /*CTRL-D*/ == c)
                                break;
                }
+
        } else {
+
+               // else we assume a local keyboard and have to mangle with it's 
mode
+               xioctl(STDIN_FILENO, KDGKBMODE, &kbmode);
+               printf(mode_was, 
+                       kbmode == K_RAW ? "RAW" :
+                               (kbmode == K_XLATE ? "XLATE" :
+                                       (kbmode == K_MEDIUMRAW ? "MEDIUMRAW" :
+                                               (kbmode == K_UNICODE ? 
"UNICODE" : "UNKNOWN")))
+               );
+
                // set raw keyboard mode
                xioctl(STDIN_FILENO, KDSKBMODE, (void 
*)(ptrdiff_t)((option_mask32 & OPT_k) ? K_MEDIUMRAW : K_RAW));
 
                // we should exit on any signal; signals should interrupt read
                bb_signals_recursive_norestart(BB_FATAL_SIGS, record_signo);
 
+               // inform user that program ends after time of inactivity
+               printf(press_keys, on_inactivity);
+
                // read and show scancodes
                while (!bb_got_signal) {
                        char buf[18];
@@ -94,6 +108,7 @@
 
                        // setup 10s watchdog
                        alarm(10);
+
                        // read scancodes
                        n = read(STDIN_FILENO, buf, sizeof(buf));
                        i = 0;
@@ -121,11 +136,13 @@
                        }
                        puts("\r");
                }
+
+               // restore keyboard mode
+               xioctl(STDIN_FILENO, KDSKBMODE, (void *)(ptrdiff_t)kbmode);
        }
 
-       // restore keyboard and console settings
+       // restore console settings
        xset1(&tio0);
-       xioctl(STDIN_FILENO, KDSKBMODE, (void *)(ptrdiff_t)kbmode);
 
        return EXIT_SUCCESS;
 }
_______________________________________________
busybox mailing list
busybox@busybox.net
http://lists.busybox.net/mailman/listinfo/busybox

Reply via email to