2 * Copyright (c) 1994-1995 Søren Schmidt
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer,
10 * in this position and unchanged.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 * $FreeBSD: src/usr.sbin/kbdcontrol/kbdcontrol.c,v 1.30.2.2 2001/06/08 18:27:32 sobomax Exp $
39 #include <machine/console.h>
45 char ctrl_names[32][4] = {
46 "nul", "soh", "stx", "etx", "eot", "enq", "ack", "bel",
47 "bs ", "ht ", "nl ", "vt ", "ff ", "cr ", "so ", "si ",
48 "dle", "dc1", "dc2", "dc3", "dc4", "nak", "syn", "etb",
49 "can", "em ", "sub", "esc", "fs ", "gs ", "rs ", "us "
52 char acc_names[15][5] = {
53 "dgra", "dacu", "dcir", "dtil", "dmac", "dbre", "ddot",
54 "duml", "dsla", "drin", "dced", "dapo", "ddac", "dogo",
58 char acc_names_u[15][5] = {
59 "DGRA", "DACU", "DCIR", "DTIL", "DMAC", "DBRE", "DDOT",
60 "DUML", "DSLA", "DRIN", "DCED", "DAPO", "DDAC", "DOGO",
64 char fkey_table[96][MAXFK] = {
65 /* 01-04 */ "\033[M", "\033[N", "\033[O", "\033[P",
66 /* 05-08 */ "\033[Q", "\033[R", "\033[S", "\033[T",
67 /* 09-12 */ "\033[U", "\033[V", "\033[W", "\033[X",
68 /* 13-16 */ "\033[Y", "\033[Z", "\033[a", "\033[b",
69 /* 17-20 */ "\033[c", "\033[d", "\033[e", "\033[f",
70 /* 21-24 */ "\033[g", "\033[h", "\033[i", "\033[j",
71 /* 25-28 */ "\033[k", "\033[l", "\033[m", "\033[n",
72 /* 29-32 */ "\033[o", "\033[p", "\033[q", "\033[r",
73 /* 33-36 */ "\033[s", "\033[t", "\033[u", "\033[v",
74 /* 37-40 */ "\033[w", "\033[x", "\033[y", "\033[z",
75 /* 41-44 */ "\033[@", "\033[[", "\033[\\","\033[]",
76 /* 45-48 */ "\033[^", "\033[_", "\033[`", "\033[{",
77 /* 49-52 */ "\033[H", "\033[A", "\033[I", "-" ,
78 /* 53-56 */ "\033[D", "\033[E", "\033[C", "+" ,
79 /* 57-60 */ "\033[F", "\033[B", "\033[G", "\033[L",
80 /* 61-64 */ "\177", "\033[J", "\033[~", "\033[}",
81 /* 65-68 */ "" , "" , "" , "" ,
82 /* 69-72 */ "" , "" , "" , "" ,
83 /* 73-76 */ "" , "" , "" , "" ,
84 /* 77-80 */ "" , "" , "" , "" ,
85 /* 81-84 */ "" , "" , "" , "" ,
86 /* 85-88 */ "" , "" , "" , "" ,
87 /* 89-92 */ "" , "" , "" , "" ,
88 /* 93-96 */ "" , "" , "" , "" ,
91 const int delays[] = {250, 500, 750, 1000};
92 const int repeats[] = { 34, 38, 42, 46, 50, 55, 59, 63,
93 68, 76, 84, 92, 100, 110, 118, 126,
94 136, 152, 168, 184, 200, 220, 236, 252,
95 272, 304, 336, 368, 400, 440, 472, 504};
96 const int ndelays = (sizeof(delays) / sizeof(int));
97 const int nrepeats = (sizeof(repeats) / sizeof(int));
103 static int get_accent_definition_line(accentmap_t *);
104 static void mux_keyboard(u_int op, char *kbd);
105 static int get_key_definition_line(keymap_t *);
106 static void usage(void);
111 nextarg(int ac, char **av, int *indp, int oc)
114 return(av[(*indp)++]);
115 warnx("option requires two arguments -- %c", oc);
122 mkfullname(const char *s1, const char *s2, const char *s3)
124 static char *buf = NULL;
128 f = strlen(s1) + strlen(s2) + strlen(s3) + 1;
131 buf = (char *)realloc(buf, f);
133 buf = (char *)malloc(f);
151 switch ((token = yylex())) {
199 return LCTRA | 0x100;
201 return RCTRA | 0x100;
203 return LALTA | 0x100;
205 return RALTA | 0x100;
211 if (ACC(number) > L_ACC)
213 return ACC(number) | 0x100;
215 if (F(number) > L_FN)
217 return F(number) | 0x100;
219 if (S(number) > L_SCR)
221 return S(number) | 0x100;
223 return (unsigned char)letter;
225 if (number < 0 || number > 255)
234 get_definition_line(FILE *fd, keymap_t *keymap, accentmap_t *accentmap)
244 c = get_key_definition_line(keymap);
246 errx(1, "invalid key definition");
247 if (c > keymap->n_keys)
251 c = get_accent_definition_line(accentmap);
253 errx(1, "invalid accent key definition");
254 if (c > accentmap->n_accs)
255 accentmap->n_accs = c;
261 errx(1, "illegal definition line");
267 get_key_definition_line(keymap_t *map)
269 int i, def, scancode;
271 /* check scancode number */
272 if (number < 0 || number >= NUM_KEYS)
276 /* get key definitions */
277 map->key[scancode].spcl = 0;
278 for (i=0; i<NUM_STATES; i++) {
279 if ((def = get_entry()) == -1)
282 map->key[scancode].spcl |= (0x80 >> i);
283 map->key[scancode].map[i] = def & 0xFF;
285 /* get lock state key def */
286 if ((token = yylex()) != TFLAG)
288 map->key[scancode].flgs = number;
290 return (scancode + 1);
294 get_accent_definition_line(accentmap_t *map)
300 if (ACC(number) < F_ACC || ACC(number) > L_ACC)
301 /* number out of range */
304 if (map->acc[accent].accchar != 0) {
305 /* this entry has already been defined before! */
306 errx(1, "duplicated accent key definition");
309 switch ((token = yylex())) {
311 map->acc[accent].accchar = letter;
314 map->acc[accent].accchar = number;
320 for (i = 0; (token = yylex()) == '(';) {
321 switch ((token = yylex())) {
331 switch ((token = yylex())) {
341 if ((token = yylex()) != ')')
343 if (i >= NUM_ACCENTCHARS) {
344 warnx("too many accented characters, ignored");
347 map->acc[accent].map[i][0] = c1;
348 map->acc[accent].map[i][1] = c2;
355 print_entry(FILE *fp, int value)
357 int val = value & 0xFF;
361 fprintf(fp, " nop ");
364 fprintf(fp, " lshift");
367 fprintf(fp, " rshift");
370 fprintf(fp, " clock ");
373 fprintf(fp, " nlock ");
376 fprintf(fp, " slock ");
379 fprintf(fp, " btab ");
382 fprintf(fp, " lalt ");
385 fprintf(fp, " lctrl ");
388 fprintf(fp, " nscr ");
391 fprintf(fp, " pscr ");
394 fprintf(fp, " rctrl ");
397 fprintf(fp, " ralt ");
400 fprintf(fp, " alock ");
403 fprintf(fp, " ashift");
406 fprintf(fp, " meta ");
409 fprintf(fp, " boot ");
412 fprintf(fp, " debug ");
415 fprintf(fp, " susp ");
418 fprintf(fp, " saver ");
421 fprintf(fp, " panic ");
424 fprintf(fp, " lshifta");
427 fprintf(fp, " rshifta");
430 fprintf(fp, " lctrla");
433 fprintf(fp, " rctrla");
436 fprintf(fp, " lalta ");
439 fprintf(fp, " ralta ");
442 fprintf(fp, " halt ");
445 fprintf(fp, " pdwn ");
449 if (val >= F_FN && val <= L_FN)
450 fprintf(fp, " fkey%02d", val - F_FN + 1);
451 else if (val >= F_SCR && val <= L_SCR)
452 fprintf(fp, " scr%02d ", val - F_SCR + 1);
453 else if (val >= F_ACC && val <= L_ACC)
454 fprintf(fp, " %-6s", acc_names[val - F_ACC]);
456 fprintf(fp, " 0x%02x ", val);
458 fprintf(fp, " %3d ", val);
462 fprintf(fp, " %s ", ctrl_names[val]);
464 fprintf(fp, " del ");
465 else if (isascii(val) && isprint(val))
466 fprintf(fp, " '%c' ", val);
468 fprintf(fp, " 0x%02x ", val);
470 fprintf(fp, " %3d ", val);
477 print_key_definition_line(FILE *fp, int scancode, struct keyent_t *key)
481 /* print scancode number */
483 fprintf(fp, " 0x%02x ", scancode);
485 fprintf(fp, " %03d ", scancode);
487 /* print key definitions */
488 for (i=0; i<NUM_STATES; i++) {
489 if (key->spcl & (0x80 >> i))
490 print_entry(fp, key->map[i] | 0x100);
492 print_entry(fp, key->map[i]);
495 /* print lock state key def */
513 print_accent_definition_line(FILE *fp, int accent, struct acc_t *key)
518 if (key->accchar == 0)
521 /* print accent number */
522 fprintf(fp, " %-6s", acc_names[accent]);
523 if (isascii(key->accchar) && isprint(key->accchar))
524 fprintf(fp, "'%c' ", key->accchar);
526 fprintf(fp, "0x%02x ", key->accchar);
528 fprintf(fp, "%03d ", key->accchar);
530 for (i = 0; i < NUM_ACCENTCHARS; ++i) {
534 if ((i > 0) && ((i % 4) == 0))
536 if (isascii(c) && isprint(c))
537 fprintf(fp, "( '%c' ", c);
539 fprintf(fp, "(0x%02x ", c);
541 fprintf(fp, "( %03d ", c);
543 if (isascii(c) && isprint(c))
544 fprintf(fp, "'%c' ) ", c);
546 fprintf(fp, "0x%02x) ", c);
548 fprintf(fp, "%03d ) ", c);
554 dump_entry(int value)
647 if (value >= F_FN && value <= L_FN)
648 printf(" F(%2d),", value - F_FN + 1);
649 else if (value >= F_SCR && value <= L_SCR)
650 printf(" S(%2d),", value - F_SCR + 1);
651 else if (value >= F_ACC && value <= L_ACC)
652 printf(" %-4s, ", acc_names_u[value - F_ACC]);
654 printf(" 0x%02X, ", value);
657 } else if (value == '\'') {
659 } else if (value == '\\') {
661 } else if (isascii(value) && isprint(value)) {
662 printf(" '%c', ", value);
664 printf(" 0x%02X, ", value);
669 dump_key_definition(char *name, keymap_t *keymap)
673 printf("static keymap_t keymap_%s = { 0x%02x, {\n",
674 name, (unsigned)keymap->n_keys);
677 " * scan cntrl alt alt cntrl\n"
678 " * code base shift cntrl shift alt shift cntrl shift spcl flgs\n"
679 " * ---------------------------------------------------------------------------\n"
681 for (i = 0; i < keymap->n_keys; i++) {
682 printf("/*%02x*/{{", i);
683 for (j = 0; j < NUM_STATES; j++) {
684 if (keymap->key[i].spcl & (0x80 >> j))
685 dump_entry(keymap->key[i].map[j] | 0x100);
687 dump_entry(keymap->key[i].map[j]);
689 printf("}, 0x%02X,0x%02X },\n",
690 (unsigned)keymap->key[i].spcl,
691 (unsigned)keymap->key[i].flgs);
697 dump_accent_definition(char *name, accentmap_t *accentmap)
702 printf("static accentmap_t accentmap_%s = { %d",
703 name, accentmap->n_accs);
704 if (accentmap->n_accs <= 0) {
709 for (i = 0; i < NUM_DEADKEYS; i++) {
710 printf(" /* %s=%d */\n {", acc_names[i], i);
711 c = accentmap->acc[i].accchar;
715 printf(" '\\\\', {");
716 else if (isascii(c) && isprint(c))
717 printf(" '%c', {", c);
719 printf(" 0x00 }, \n");
722 printf(" 0x%02x, {", c);
723 for (j = 0; j < NUM_ACCENTCHARS; j++) {
724 c = accentmap->acc[i].map[j][0];
727 if ((j > 0) && ((j % 4) == 0))
729 if (isascii(c) && isprint(c))
730 printf(" { '%c',", c);
732 printf(" { 0x%02x,", c);
733 printf("0x%02x },", accentmap->acc[i].map[j][1]);
741 load_keymap(char *opt, int dumponly)
744 accentmap_t accentmap;
747 char *name = NULL, *cp;
748 const char *prefix[] = {"", "", KEYMAP_PATH, KEYMAP_PATH, NULL};
749 const char *postfix[] = {"", ".kbd", "", ".kbd"};
751 for (i=0; prefix[i]; i++) {
752 name = mkfullname(prefix[i], opt, postfix[i]);
753 if ((fd = fopen(name, "r")))
757 warn("keymap file not found");
760 memset(&keymap, 0, sizeof(keymap));
761 memset(&accentmap, 0, sizeof(accentmap));
764 if (get_definition_line(fd, &keymap, &accentmap) < 0)
768 /* fix up the filename to make it a valid C identifier */
769 for (cp = opt; *cp; cp++)
770 if (!isalpha(*cp) && !isdigit(*cp)) *cp = '_';
772 " * Automatically generated from %s.\n"
775 dump_key_definition(opt, &keymap);
776 dump_accent_definition(opt, &accentmap);
779 if ((keymap.n_keys > 0) && (ioctl(0, PIO_KEYMAP, &keymap) < 0)) {
780 warn("setting keymap");
784 if ((accentmap.n_accs > 0)
785 && (ioctl(0, PIO_DEADKEYMAP, &accentmap) < 0)) {
786 warn("setting accentmap");
796 accentmap_t accentmap;
799 if (ioctl(0, GIO_KEYMAP, &keymap) < 0)
800 err(1, "getting keymap");
801 if (ioctl(0, GIO_DEADKEYMAP, &accentmap) < 0)
802 memset(&accentmap, 0, sizeof(accentmap));
805 "# scan cntrl alt alt cntrl lock\n"
806 "# code base shift cntrl shift alt shift cntrl shift state\n"
807 "# ------------------------------------------------------------------\n"
809 for (i=0; i<keymap.n_keys; i++)
810 print_key_definition_line(stdout, i, &keymap.key[i]);
813 for (i = 0; i < NUM_DEADKEYS; i++)
814 print_accent_definition_line(stdout, i, &accentmap.acc[i]);
820 load_default_functionkeys(void)
825 for (i=0; i<NUM_FKEYS; i++) {
827 strcpy(fkey.keydef, fkey_table[i]);
828 fkey.flen = strlen(fkey_table[i]);
829 if (ioctl(0, SETFKEY, &fkey) < 0)
830 warn("setting function key");
835 set_functionkey(char *keynumstr, const char *string)
839 if (!strcmp(keynumstr, "load") && !strcmp(string, "default")) {
840 load_default_functionkeys();
843 fkey.keynum = atoi(keynumstr);
844 if (fkey.keynum < 1 || fkey.keynum > NUM_FKEYS) {
845 warnx("function key number must be between 1 and %d",
849 if ((fkey.flen = strlen(string)) > MAXFK) {
850 warnx("function key string too long (%d > %d)",
854 strcpy(fkey.keydef, string);
856 if (ioctl(0, SETFKEY, &fkey) < 0)
857 warn("setting function key");
862 set_bell_values(char *opt)
864 int bell, duration = 0, pitch = 0;
867 if (!strncmp(opt, "quiet.", 6)) {
871 if (!strcmp(opt, "visual"))
873 else if (!strcmp(opt, "normal"))
874 duration = 5, pitch = 800;
875 else if (!strcmp(opt, "off"))
876 duration = 0, pitch = 0;
881 duration = strtol(opt, &v1, 0);
882 if ((duration < 0) || (*v1 != '.'))
885 pitch = strtol(opt, &v1, 0);
886 if ((pitch < 0) || (*opt == '\0') || (*v1 != '\0')) {
888 warnx("argument to -b must be duration.pitch or [quiet.]visual|normal|off");
892 pitch = 1193182 / pitch; /* in Hz */
893 duration /= 10; /* in 10 m sec */
896 ioctl(0, CONS_BELLTYPE, &bell);
897 if ((bell & ~2) == 0)
898 fprintf(stderr, "
\e[=%d;%dB", pitch, duration);
903 set_keyrates(char *opt)
909 if (!strcmp(opt, "slow")) {
910 delay = 1000, repeat = 500;
911 } else if (!strcmp(opt, "normal")) {
912 delay = 500, repeat = 125;
913 } else if (!strcmp(opt, "fast")) {
919 delay = strtol(opt, &v1, 0);
920 if ((delay < 0) || (*v1 != '.'))
923 repeat = strtol(opt, &v1, 0);
924 if ((repeat < 0) || (*opt == '\0') || (*v1 != '\0')) {
926 warnx("argument to -r must be delay.repeat or slow|normal|fast");
929 for (n = 0; n < ndelays - 1; n++)
930 if (delay <= delays[n])
932 for (n = 0; n < nrepeats - 1; n++)
933 if (repeat <= repeats[n])
939 if (ioctl(0, KDSETREPEAT, arg) == -1)
940 warn("setting keyboard rate");
945 set_history(char *opt)
950 if ((*opt == '\0') || size < 0) {
951 warnx("argument must be a positive number");
954 if (ioctl(0, CONS_HISTORY, &size) == -1)
955 warn("setting history buffer size");
959 get_kbd_type_name(int type)
966 { KB_101, "AT 101/102" },
967 { KB_OTHER, "generic" },
971 for (i = 0; i < sizeof(name_table)/sizeof(name_table[0]); ++i) {
972 if (type == name_table[i].type)
973 return name_table[i].name;
981 keyboard_info_t info;
983 if (ioctl(0, KDGKBINFO, &info) == -1) {
984 warn("unable to obtain keyboard information");
987 printf("kbd%d:\n", info.kb_index);
988 printf(" %.*s%d, type:%s (%d)\n",
989 (int)sizeof(info.kb_name), info.kb_name, info.kb_unit,
990 get_kbd_type_name(info.kb_type), info.kb_type);
995 set_keyboard(char *device)
997 keyboard_info_t info;
1000 fd = open(device, O_RDONLY);
1002 warn("cannot open %s", device);
1005 if (ioctl(fd, KDGKBINFO, &info) == -1) {
1006 warn("unable to obtain keyboard information");
1011 * The keyboard device driver won't release the keyboard by
1012 * the following ioctl, but it automatically will, when the device
1013 * is closed. So, we don't check error here.
1015 ioctl(fd, CONS_RELKBD, 0);
1018 printf("kbd%d\n", info.kb_index);
1019 printf(" %.*s%d, type:%s (%d)\n",
1020 (int)sizeof(info.kb_name), info.kb_name, info.kb_unit,
1021 get_kbd_type_name(info.kb_type), info.kb_type);
1024 if (ioctl(0, CONS_SETKBD, info.kb_index) == -1)
1025 warn("unable to set keyboard");
1030 release_keyboard(void)
1032 keyboard_info_t info;
1035 * If stdin is not associated with a keyboard, the following ioctl
1038 if (ioctl(0, KDGKBINFO, &info) == -1) {
1039 warn("unable to obtain keyboard information");
1043 printf("kbd%d\n", info.kb_index);
1044 printf(" %.*s%d, type:%s (%d)\n",
1045 (int)sizeof(info.kb_name), info.kb_name, info.kb_unit,
1046 get_kbd_type_name(info.kb_type), info.kb_type);
1048 if (ioctl(0, CONS_RELKBD, 0) == -1)
1049 warn("unable to release the keyboard");
1053 mux_keyboard(u_int op, char *kbd)
1055 keyboard_info_t info;
1059 * If stdin is not associated with a keyboard, the following ioctl
1062 if (ioctl(0, KDGKBINFO, &info) == -1) {
1063 warn("unable to obtain keyboard information");
1067 printf("kbd%d\n", info.kb_index);
1068 printf(" %.*s%d, type:%s (%d)\n",
1069 (int)sizeof(info.kb_name), info.kb_name, info.kb_unit,
1070 get_kbd_type_name(info.kb_type), info.kb_type);
1073 * split kbd into name and unit. find the right most part of the
1074 * kbd string that consist of only digits.
1077 memset(&info, 0, sizeof(info));
1083 unit = strpbrk(ep + 1, "0123456789");
1085 info.kb_unit = strtol(unit, &ep, 10);
1089 } while (unit != NULL && info.kb_unit == -1);
1091 if (info.kb_unit == -1) {
1092 warnx("unable to find keyboard driver unit in '%s'", kbd);
1097 warnx("unable to find keyboard driver name in '%s'", kbd);
1100 if (unit - kbd >= (int) sizeof(info.kb_name)) {
1101 warnx("keyboard name '%s' is too long", kbd);
1105 strncpy(info.kb_name, kbd, unit - kbd);
1108 * If stdin is not associated with a kbdmux(4) keyboard, the following
1112 if (ioctl(0, op, &info) == -1)
1113 warn("unable to (un)mux the keyboard");
1119 fprintf(stderr, "%s\n%s\n%s\n",
1120 "usage: kbdcontrol [-dFKix] [-A name] [-a name] [-b duration.pitch | [quiet.]belltype]",
1121 " [-r delay.repeat | speed] [-l mapfile] [-f # string]",
1122 " [-h size] [-k device] [-L mapfile]");
1128 main(int argc, char **argv)
1132 while((opt = getopt(argc, argv, "A:a:b:df:h:iKk:Fl:L:r:x")) != -1)
1136 mux_keyboard((opt == 'A')? KBRELKBD : KBADDKBD, optarg);
1139 set_bell_values(optarg);
1145 load_keymap(optarg, 0);
1148 load_keymap(optarg, 1);
1151 set_functionkey(optarg,
1152 nextarg(argc, argv, &optind, 'f'));
1155 load_default_functionkeys();
1158 set_history(optarg);
1167 set_keyboard(optarg);
1170 set_keyrates(optarg);
1178 if ((optind != argc) || (argc == 1))