[BUILD] klibc port
[platform/upstream/kbd.git] / src / getkeycodes.c
1 /*
2  * call: getkeycodes
3  *
4  * aeb, 941108
5  */
6 #include <errno.h>
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <fcntl.h>
10 #include <sys/ioctl.h>
11 #include <linux/kd.h>
12 #include "getfd.h"
13 #include "nls.h"
14 #include "version.h"
15
16 static void attr_noreturn
17 usage(void) {
18     fprintf(stderr, _("usage: getkeycodes\n"));
19     exit(1);
20 }
21
22 int
23 main(int argc, char **argv) {
24         int fd;
25         unsigned int sc, sc0;
26         struct kbkeycode a;
27         int old_kernel = 0;
28
29         set_progname(argv[0]);
30 #ifndef __klibc__
31         setlocale(LC_ALL, "");
32         bindtextdomain(PACKAGE_NAME, LOCALEDIR);
33         textdomain(PACKAGE_NAME);
34 #endif
35
36         if (argc == 2 && !strcmp(argv[1], "-V"))
37                 print_version_and_exit();
38
39         if (argc != 1)
40                 usage();
41         fd = getfd(NULL);
42
43         /* Old kernels don't support changing scancodes below SC_LIM. */
44         a.scancode = 0;
45         a.keycode = 0;
46         if (ioctl(fd, KDGETKEYCODE, &a)) {
47                 old_kernel = 1;
48                 sc0 = 89;
49         } else
50         for (sc0 = 1; sc0 <= 88; sc0++) {
51                 a.scancode = sc0;
52                 a.keycode = 0;
53                 if (ioctl(fd, KDGETKEYCODE, &a) || a.keycode != sc0)
54                         break;
55         }
56
57         printf(_("Plain scancodes xx (hex) versus keycodes (dec)\n"));
58
59         if (sc0 == 89) {
60                 printf(_("0 is an error; "
61                          "for 1-88 (0x01-0x58) scancode equals keycode\n"));
62         } else if (sc0 > 1) {
63                 printf(_("for 1-%d (0x01-0x%02x) scancode equals keycode\n"),
64                        sc0 - 1, sc0 - 1);
65         }
66
67         for (sc = (sc0 & ~7); sc < 256; sc++) {
68                 if (sc == 128)
69                         printf(_("\n\nEscaped scancodes e0 xx (hex)\n"));
70                 if (sc % 8 == 0) {
71                         if (sc < 128)
72                                 printf("\n 0x%02x: ", sc);
73                         else
74                                 printf("\ne0 %02x: ", sc-128);
75                 }
76
77                 if (sc < sc0) {
78                         printf(" %3d", sc);
79                         continue;
80                 }
81
82                 a.scancode = sc;
83                 a.keycode = 0;
84                 if (ioctl(fd, KDGETKEYCODE, &a) == 0) {
85                         printf(" %3d", a.keycode);
86                         continue;
87                 }
88                 if (errno == EINVAL) {
89                         printf("   -");
90                         continue;
91                 }
92                 perror("KDGETKEYCODE");
93                 fprintf(stderr,
94                         _("failed to get keycode for scancode 0x%x\n"), sc);
95                 exit(1);
96         }
97         printf("\n");
98         return 0;
99 }