9 #include <linux/keyboard.h>
10 #include <sys/ioctl.h>
17 int tmp
; /* for debugging */
24 * version 0.81 of showkey would restore kbmode unconditially to XLATE,
25 * thus making the console unusable when it was called under X.
31 if (ioctl(fd
, KDGKBMODE
, &oldkbmode
)) {
41 m
= "MEDIUMRAW"; break;
45 m
= _("?UNKNOWN?"); break;
47 printf(_("kb mode was %s\n"), m
);
48 if (oldkbmode
!= K_XLATE
) {
49 printf(_("[ if you are trying this under X, it might not work\n"
50 "since the X server is also reading /dev/console ]\n"));
57 if (ioctl(fd
, KDSKBMODE
, oldkbmode
)) {
61 if (tcsetattr(fd
, 0, &old
) == -1)
68 printf(_("caught signal %d, cleaning up...\n"), x
);
82 "showkey version %s\n\n"
83 "usage: showkey [options...]\n"
85 "valid options are:\n"
87 " -h --help display this help text\n"
88 " -a --ascii display the decimal/octal/hex values of the keys\n"
89 " -s --scancodes display only the raw scan-codes\n"
90 " -k --keycodes display only the interpreted keycodes (default)\n"
96 main (int argc
, char *argv
[]) {
97 static char buffer
[32];
98 const char *short_opts
= "haskV";
99 const struct option long_opts
[] = {
100 { "help", no_argument
, NULL
, 'h' },
101 { "ascii", no_argument
, NULL
, 'a' },
102 { "scancodes", no_argument
, NULL
, 's' },
103 { "keycodes", no_argument
, NULL
, 'k' },
104 { "version", no_argument
, NULL
, 'V' },
109 int show_keycodes
= 1;
113 FILE *fortune
= NULL
;
119 unsigned char buf
[16];
122 set_progname(argv
[0]);
124 setlocale(LC_ALL
, "");
125 bindtextdomain(PACKAGE
, LOCALEDIR
);
128 while ((c
= getopt_long(argc
, argv
,
129 short_opts
, long_opts
, NULL
)) != -1) {
141 print_version_and_exit();
152 /* no mode and signal and timer stuff - just read stdin */
155 if (tcgetattr(fd
, &old
) == -1)
157 if (tcgetattr(fd
, &new) == -1)
160 new.c_lflag
&= ~ (ICANON
| ISIG
);
161 new.c_lflag
|= (ECHO
| ECHOCTL
);
166 if (tcsetattr(fd
, TCSAFLUSH
, &new) == -1)
168 printf(_("\nPress any keys - "
169 "Ctrl-D will terminate this program\n\n"));
172 n
= read(fd
, buf
, 1);
174 printf(" \t%3d 0%03o 0x%02x\n",
175 buf
[0], buf
[0], buf
[0]);
176 if (n
!= 1 || buf
[0] == 04)
180 if (tcsetattr(fd
, 0, &old
) == -1)
187 /* the program terminates when there is no input for 10 secs */
188 signal(SIGALRM
, watch_dog
);
191 if we receive a signal, we want to exit nicely, in
192 order not to leave the keyboard in an unusable mode
196 signal(SIGQUIT
, die
);
198 signal(SIGTRAP
, die
);
199 signal(SIGABRT
, die
);
202 signal(SIGKILL
, die
);
203 signal(SIGUSR1
, die
);
204 signal(SIGSEGV
, die
);
205 signal(SIGUSR2
, die
);
206 signal(SIGPIPE
, die
);
207 signal(SIGTERM
, die
);
209 signal(SIGSTKFLT
, die
);
211 // signal(SIGCHLD, die);
212 // signal(SIGCONT, die);
213 // signal(SIGSTOP, die);
214 // signal(SIGTSTP, die);
215 //signal(SIGTTIN, die);
216 //signal(SIGTTOU, die);
219 if (tcgetattr(fd
, &old
) == -1)
221 if (tcgetattr(fd
, &new) == -1)
224 new.c_lflag
&= ~ (ICANON
| ECHO
| ISIG
);
226 new.c_cc
[VMIN
] = sizeof(buf
);
227 new.c_cc
[VTIME
] = 1; /* 0.1 sec intercharacter timeout */
229 if (tcsetattr(fd
, TCSAFLUSH
, &new) == -1)
231 if (ioctl(fd
, KDSKBMODE
, show_keycodes
? K_MEDIUMRAW
: K_RAW
)) {
236 printf(_("Running...\n"));
242 time_t t
= time(NULL
);
243 float load1
, load2
, load3
;
244 int l
, l1
, l2
, l3
, s
;
245 FILE *f
= fopen("/proc/loadavg", "r");
247 // Display oscillating date/time
257 // Display load averages
258 fscanf(f
, "%f %f %f", &load1
, &load2
, &load3
);
266 for (l
= 0; l
< 120; l
++)
269 if (l1
>= l
) b
|= 0x07;
270 if (l2
>= l
) b
|= 0x1C;
271 if (l3
>= l
) b
|= 0x70;
275 // Display current fortune
276 for (s
= 0; s
< 2; s
++)
280 system ("/usr/games/fortune > /tmp/glop");
281 fortune
= fopen("/tmp/glop", "r");
299 memmove(fbytes
, fbytes
+1, 159);
300 fbytes
[159] = *fptr
++;
304 for (l
= 0; l
< 160; l
++)
307 // Read hex keys from console
308 n
= read(fd
, buf
, sizeof(buf
));
312 perror("showkey:read");
318 for (i
= 0; i
< n
; i
++)
320 memmove(buffer
, buffer
+1, MAX
);
321 buffer
[MAX
] = *ptr
++;
325 sprintf(buf
, "%d:", count
);
327 for (i
= 0; i
< MAX
; i
++)
329 sprintf(buf
, "%02X", (unsigned char) buffer
[i
]);