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>
43 char ctrl_names[32][4] = {
44 "nul", "soh", "stx", "etx", "eot", "enq", "ack", "bel",
45 "bs ", "ht ", "nl ", "vt ", "ff ", "cr ", "so ", "si ",
46 "dle", "dc1", "dc2", "dc3", "dc4", "nak", "syn", "etb",
47 "can", "em ", "sub", "esc", "fs ", "gs ", "rs ", "us "
50 char acc_names[15][5] = {
51 "dgra", "dacu", "dcir", "dtil", "dmac", "dbre", "ddot",
52 "duml", "dsla", "drin", "dced", "dapo", "ddac", "dogo",
56 char acc_names_u[15][5] = {
57 "DGRA", "DACU", "DCIR", "DTIL", "DMAC", "DBRE", "DDOT",
58 "DUML", "DSLA", "DRIN", "DCED", "DAPO", "DDAC", "DOGO",
62 char fkey_table[96][MAXFK] = {
63 /* 01-04 */ "\033[M", "\033[N", "\033[O", "\033[P",
64 /* 05-08 */ "\033[Q", "\033[R", "\033[S", "\033[T",
65 /* 09-12 */ "\033[U", "\033[V", "\033[W", "\033[X",
66 /* 13-16 */ "\033[Y", "\033[Z", "\033[a", "\033[b",
67 /* 17-20 */ "\033[c", "\033[d", "\033[e", "\033[f",
68 /* 21-24 */ "\033[g", "\033[h", "\033[i", "\033[j",
69 /* 25-28 */ "\033[k", "\033[l", "\033[m", "\033[n",
70 /* 29-32 */ "\033[o", "\033[p", "\033[q", "\033[r",
71 /* 33-36 */ "\033[s", "\033[t", "\033[u", "\033[v",
72 /* 37-40 */ "\033[w", "\033[x", "\033[y", "\033[z",
73 /* 41-44 */ "\033[@", "\033[[", "\033[\\","\033[]",
74 /* 45-48 */ "\033[^", "\033[_", "\033[`", "\033[{",
75 /* 49-52 */ "\033[H", "\033[A", "\033[I", "-" ,
76 /* 53-56 */ "\033[D", "\033[E", "\033[C", "+" ,
77 /* 57-60 */ "\033[F", "\033[B", "\033[G", "\033[L",
78 /* 61-64 */ "\177", "\033[J", "\033[~", "\033[}",
79 /* 65-68 */ "" , "" , "" , "" ,
80 /* 69-72 */ "" , "" , "" , "" ,
81 /* 73-76 */ "" , "" , "" , "" ,
82 /* 77-80 */ "" , "" , "" , "" ,
83 /* 81-84 */ "" , "" , "" , "" ,
84 /* 85-88 */ "" , "" , "" , "" ,
85 /* 89-92 */ "" , "" , "" , "" ,
86 /* 93-96 */ "" , "" , "" , "" ,
89 const int delays[] = {250, 500, 750, 1000};
90 const int repeats[] = { 34, 38, 42, 46, 50, 55, 59, 63,
91 68, 76, 84, 92, 100, 110, 118, 126,
92 136, 152, 168, 184, 200, 220, 236, 252,
93 272, 304, 336, 368, 400, 440, 472, 504};
94 const int ndelays = (sizeof(delays) / sizeof(int));
95 const int nrepeats = (sizeof(repeats) / sizeof(int));
101 static int get_accent_definition_line(accentmap_t *);
102 static void mux_keyboard(u_int op, char *kbd);
103 static int get_key_definition_line(keymap_t *);
104 static void usage(void);
109 nextarg(int ac, char **av, int *indp, int oc)
112 return(av[(*indp)++]);
113 warnx("option requires two arguments -- %c", oc);
120 mkfullname(const char *s1, const char *s2, const char *s3)
122 static char *buf = NULL;
126 f = strlen(s1) + strlen(s2) + strlen(s3) + 1;
129 buf = (char *)realloc(buf, f);
131 buf = (char *)malloc(f);
149 switch ((token = yylex())) {
197 return LCTRA | 0x100;
199 return RCTRA | 0x100;
201 return LALTA | 0x100;
203 return RALTA | 0x100;
209 if (ACC(number) > L_ACC)
211 return ACC(number) | 0x100;
213 if (F(number) > L_FN)
215 return F(number) | 0x100;
217 if (S(number) > L_SCR)
219 return S(number) | 0x100;
221 return (unsigned char)letter;
223 if (number < 0 || number > 255)
232 get_definition_line(FILE *fd, keymap_t *keymap, accentmap_t *accentmap)
242 c = get_key_definition_line(keymap);
244 errx(1, "invalid key definition");
245 if (c > keymap->n_keys)
249 c = get_accent_definition_line(accentmap);
251 errx(1, "invalid accent key definition");
252 if (c > accentmap->n_accs)
253 accentmap->n_accs = c;
259 errx(1, "illegal definition line");
265 get_key_definition_line(keymap_t *map)
267 int i, def, scancode;
269 /* check scancode number */
270 if (number < 0 || number >= NUM_KEYS)
274 /* get key definitions */
275 map->key[scancode].spcl = 0;
276 for (i=0; i<NUM_STATES; i++) {
277 if ((def = get_entry()) == -1)
280 map->key[scancode].spcl |= (0x80 >> i);
281 map->key[scancode].map[i] = def & 0xFF;
283 /* get lock state key def */
284 if ((token = yylex()) != TFLAG)
286 map->key[scancode].flgs = number;
288 return (scancode + 1);
292 get_accent_definition_line(accentmap_t *map)
298 if (ACC(number) < F_ACC || ACC(number) > L_ACC)
299 /* number out of range */
302 if (map->acc[accent].accchar != 0) {
303 /* this entry has already been defined before! */
304 errx(1, "duplicated accent key definition");
307 switch ((token = yylex())) {
309 map->acc[accent].accchar = letter;
312 map->acc[accent].accchar = number;
318 for (i = 0; (token = yylex()) == '(';) {
319 switch ((token = yylex())) {
329 switch ((token = yylex())) {
339 if ((token = yylex()) != ')')
341 if (i >= NUM_ACCENTCHARS) {
342 warnx("too many accented characters, ignored");
345 map->acc[accent].map[i][0] = c1;
346 map->acc[accent].map[i][1] = c2;
353 print_entry(FILE *fp, int value)
355 int val = value & 0xFF;
359 fprintf(fp, " nop ");
362 fprintf(fp, " lshift");
365 fprintf(fp, " rshift");
368 fprintf(fp, " clock ");
371 fprintf(fp, " nlock ");
374 fprintf(fp, " slock ");
377 fprintf(fp, " btab ");
380 fprintf(fp, " lalt ");
383 fprintf(fp, " lctrl ");
386 fprintf(fp, " nscr ");
389 fprintf(fp, " pscr ");
392 fprintf(fp, " rctrl ");
395 fprintf(fp, " ralt ");
398 fprintf(fp, " alock ");
401 fprintf(fp, " ashift");
404 fprintf(fp, " meta ");
407 fprintf(fp, " boot ");
410 fprintf(fp, " debug ");
413 fprintf(fp, " susp ");
416 fprintf(fp, " saver ");
419 fprintf(fp, " panic ");
422 fprintf(fp, " lshifta");
425 fprintf(fp, " rshifta");
428 fprintf(fp, " lctrla");
431 fprintf(fp, " rctrla");
434 fprintf(fp, " lalta ");
437 fprintf(fp, " ralta ");
440 fprintf(fp, " halt ");
443 fprintf(fp, " pdwn ");
447 if (val >= F_FN && val <= L_FN)
448 fprintf(fp, " fkey%02d", val - F_FN + 1);
449 else if (val >= F_SCR && val <= L_SCR)
450 fprintf(fp, " scr%02d ", val - F_SCR + 1);
451 else if (val >= F_ACC && val <= L_ACC)
452 fprintf(fp, " %-6s", acc_names[val - F_ACC]);
454 fprintf(fp, " 0x%02x ", val);
456 fprintf(fp, " %3d ", val);
460 fprintf(fp, " %s ", ctrl_names[val]);
462 fprintf(fp, " del ");
463 else if (isascii(val) && isprint(val))
464 fprintf(fp, " '%c' ", val);
466 fprintf(fp, " 0x%02x ", val);
468 fprintf(fp, " %3d ", val);
475 print_key_definition_line(FILE *fp, int scancode, struct keyent_t *key)
479 /* print scancode number */
481 fprintf(fp, " 0x%02x ", scancode);
483 fprintf(fp, " %03d ", scancode);
485 /* print key definitions */
486 for (i=0; i<NUM_STATES; i++) {
487 if (key->spcl & (0x80 >> i))
488 print_entry(fp, key->map[i] | 0x100);
490 print_entry(fp, key->map[i]);
493 /* print lock state key def */
511 print_accent_definition_line(FILE *fp, int accent, struct acc_t *key)
516 if (key->accchar == 0)
519 /* print accent number */
520 fprintf(fp, " %-6s", acc_names[accent]);
521 if (isascii(key->accchar) && isprint(key->accchar))
522 fprintf(fp, "'%c' ", key->accchar);
524 fprintf(fp, "0x%02x ", key->accchar);
526 fprintf(fp, "%03d ", key->accchar);
528 for (i = 0; i < NUM_ACCENTCHARS; ++i) {
532 if ((i > 0) && ((i % 4) == 0))
534 if (isascii(c) && isprint(c))
535 fprintf(fp, "( '%c' ", c);
537 fprintf(fp, "(0x%02x ", c);
539 fprintf(fp, "( %03d ", c);
541 if (isascii(c) && isprint(c))
542 fprintf(fp, "'%c' ) ", c);
544 fprintf(fp, "0x%02x) ", c);
546 fprintf(fp, "%03d ) ", c);
552 dump_entry(int value)
645 if (value >= F_FN && value <= L_FN)
646 printf(" F(%2d),", value - F_FN + 1);
647 else if (value >= F_SCR && value <= L_SCR)
648 printf(" S(%2d),", value - F_SCR + 1);
649 else if (value >= F_ACC && value <= L_ACC)
650 printf(" %-4s, ", acc_names_u[value - F_ACC]);
652 printf(" 0x%02X, ", value);
655 } else if (value == '\'') {
657 } else if (value == '\\') {
659 } else if (isascii(value) && isprint(value)) {
660 printf(" '%c', ", value);
662 printf(" 0x%02X, ", value);
667 dump_key_definition(char *name, keymap_t *keymap)
671 printf("static keymap_t keymap_%s = { 0x%02x, {\n",
672 name, (unsigned)keymap->n_keys);
675 " * scan cntrl alt alt cntrl\n"
676 " * code base shift cntrl shift alt shift cntrl shift spcl flgs\n"
677 " * ---------------------------------------------------------------------------\n"
679 for (i = 0; i < keymap->n_keys; i++) {
680 printf("/*%02x*/{{", i);
681 for (j = 0; j < NUM_STATES; j++) {
682 if (keymap->key[i].spcl & (0x80 >> j))
683 dump_entry(keymap->key[i].map[j] | 0x100);
685 dump_entry(keymap->key[i].map[j]);
687 printf("}, 0x%02X,0x%02X },\n",
688 (unsigned)keymap->key[i].spcl,
689 (unsigned)keymap->key[i].flgs);
695 dump_accent_definition(char *name, accentmap_t *accentmap)
700 printf("static accentmap_t accentmap_%s = { %d",
701 name, accentmap->n_accs);
702 if (accentmap->n_accs <= 0) {
707 for (i = 0; i < NUM_DEADKEYS; i++) {
708 printf(" /* %s=%d */\n {", acc_names[i], i);
709 c = accentmap->acc[i].accchar;
713 printf(" '\\\\', {");
714 else if (isascii(c) && isprint(c))
715 printf(" '%c', {", c);
717 printf(" 0x00 }, \n");
720 printf(" 0x%02x, {", c);
721 for (j = 0; j < NUM_ACCENTCHARS; j++) {
722 c = accentmap->acc[i].map[j][0];
725 if ((j > 0) && ((j % 4) == 0))
727 if (isascii(c) && isprint(c))
728 printf(" { '%c',", c);
730 printf(" { 0x%02x,", c);
731 printf("0x%02x },", accentmap->acc[i].map[j][1]);
739 load_keymap(char *opt, int dumponly)
742 accentmap_t accentmap;
745 char *name = NULL, *cp;
746 const char *prefix[] = {"", "", KEYMAP_PATH, KEYMAP_PATH, NULL};
747 const char *postfix[] = {"", ".kbd", "", ".kbd"};
749 for (i=0; prefix[i]; i++) {
750 name = mkfullname(prefix[i], opt, postfix[i]);
751 if ((fd = fopen(name, "r")))
755 warn("keymap file not found");
758 memset(&keymap, 0, sizeof(keymap));
759 memset(&accentmap, 0, sizeof(accentmap));
762 if (get_definition_line(fd, &keymap, &accentmap) < 0)
766 /* fix up the filename to make it a valid C identifier */
767 for (cp = opt; *cp; cp++)
768 if (!isalpha(*cp) && !isdigit(*cp)) *cp = '_';
770 " * Automatically generated from %s.\n"
773 dump_key_definition(opt, &keymap);
774 dump_accent_definition(opt, &accentmap);
777 if ((keymap.n_keys > 0) && (ioctl(0, PIO_KEYMAP, &keymap) < 0)) {
778 warn("setting keymap");
782 if ((accentmap.n_accs > 0)
783 && (ioctl(0, PIO_DEADKEYMAP, &accentmap) < 0)) {
784 warn("setting accentmap");
794 accentmap_t accentmap;
797 if (ioctl(0, GIO_KEYMAP, &keymap) < 0)
798 err(1, "getting keymap");
799 if (ioctl(0, GIO_DEADKEYMAP, &accentmap) < 0)
800 memset(&accentmap, 0, sizeof(accentmap));
803 "# scan cntrl alt alt cntrl lock\n"
804 "# code base shift cntrl shift alt shift cntrl shift state\n"
805 "# ------------------------------------------------------------------\n"
807 for (i=0; i<keymap.n_keys; i++)
808 print_key_definition_line(stdout, i, &keymap.key[i]);
811 for (i = 0; i < NUM_DEADKEYS; i++)
812 print_accent_definition_line(stdout, i, &accentmap.acc[i]);
818 load_default_functionkeys(void)
823 for (i=0; i<NUM_FKEYS; i++) {
825 strcpy(fkey.keydef, fkey_table[i]);
826 fkey.flen = strlen(fkey_table[i]);
827 if (ioctl(0, SETFKEY, &fkey) < 0)
828 warn("setting function key");
833 set_functionkey(char *keynumstr, const char *string)
837 if (!strcmp(keynumstr, "load") && !strcmp(string, "default")) {
838 load_default_functionkeys();
841 fkey.keynum = atoi(keynumstr);
842 if (fkey.keynum < 1 || fkey.keynum > NUM_FKEYS) {
843 warnx("function key number must be between 1 and %d",
847 if ((fkey.flen = strlen(string)) > MAXFK) {
848 warnx("function key string too long (%d > %d)",
852 strcpy(fkey.keydef, string);
854 if (ioctl(0, SETFKEY, &fkey) < 0)
855 warn("setting function key");
860 set_bell_values(char *opt)
862 int bell, duration = 0, pitch = 0;
865 if (!strncmp(opt, "quiet.", 6)) {
869 if (!strcmp(opt, "visual"))
871 else if (!strcmp(opt, "normal"))
872 duration = 5, pitch = 800;
873 else if (!strcmp(opt, "off"))
874 duration = 0, pitch = 0;
879 duration = strtol(opt, &v1, 0);
880 if ((duration < 0) || (*v1 != '.'))
883 pitch = strtol(opt, &v1, 0);
884 if ((pitch < 0) || (*opt == '\0') || (*v1 != '\0')) {
886 warnx("argument to -b must be duration.pitch or [quiet.]visual|normal|off");
890 pitch = 1193182 / pitch; /* in Hz */
891 duration /= 10; /* in 10 m sec */
894 ioctl(0, CONS_BELLTYPE, &bell);
895 if ((bell & ~2) == 0)
896 fprintf(stderr, "
\e[=%d;%dB", pitch, duration);
901 set_keyrates(char *opt)
908 if (!strcmp(opt, "slow")) {
909 delay = 1000, repeat = 500;
911 } else if (!strcmp(opt, "normal")) {
912 delay = 500, repeat = 125;
914 } else if (!strcmp(opt, "fast")) {
921 delay = strtol(opt, &v1, 0);
922 if ((delay < 0) || (*v1 != '.'))
925 repeat = strtol(opt, &v1, 0);
926 if ((repeat < 0) || (*opt == '\0') || (*v1 != '\0')) {
928 warnx("argument to -r must be delay.repeat or slow|normal|fast");
931 for (n = 0; n < ndelays - 1; n++)
932 if (delay <= delays[n])
935 for (n = 0; n < nrepeats - 1; n++)
936 if (repeat <= repeats[n])
943 if (ioctl(0, KDSETREPEAT, arg)) {
944 if (ioctl(0, KDSETRAD, (d << 5) | r))
945 warn("setting keyboard rate");
951 set_history(char *opt)
956 if ((*opt == '\0') || size < 0) {
957 warnx("argument must be a positive number");
960 if (ioctl(0, CONS_HISTORY, &size) == -1)
961 warn("setting history buffer size");
965 get_kbd_type_name(int type)
972 { KB_101, "AT 101/102" },
973 { KB_OTHER, "generic" },
977 for (i = 0; i < sizeof(name_table)/sizeof(name_table[0]); ++i) {
978 if (type == name_table[i].type)
979 return name_table[i].name;
987 keyboard_info_t info;
989 if (ioctl(0, KDGKBINFO, &info) == -1) {
990 warn("unable to obtain keyboard information");
993 printf("kbd%d:\n", info.kb_index);
994 printf(" %.*s%d, type:%s (%d)\n",
995 (int)sizeof(info.kb_name), info.kb_name, info.kb_unit,
996 get_kbd_type_name(info.kb_type), info.kb_type);
1001 set_keyboard(char *device)
1003 keyboard_info_t info;
1006 fd = open(device, O_RDONLY);
1008 warn("cannot open %s", device);
1011 if (ioctl(fd, KDGKBINFO, &info) == -1) {
1012 warn("unable to obtain keyboard information");
1017 * The keyboard device driver won't release the keyboard by
1018 * the following ioctl, but it automatically will, when the device
1019 * is closed. So, we don't check error here.
1021 ioctl(fd, CONS_RELKBD, 0);
1024 printf("kbd%d\n", info.kb_index);
1025 printf(" %.*s%d, type:%s (%d)\n",
1026 (int)sizeof(info.kb_name), info.kb_name, info.kb_unit,
1027 get_kbd_type_name(info.kb_type), info.kb_type);
1030 if (ioctl(0, CONS_SETKBD, info.kb_index) == -1)
1031 warn("unable to set keyboard");
1036 release_keyboard(void)
1038 keyboard_info_t info;
1041 * If stdin is not associated with a keyboard, the following ioctl
1044 if (ioctl(0, KDGKBINFO, &info) == -1) {
1045 warn("unable to obtain keyboard information");
1049 printf("kbd%d\n", info.kb_index);
1050 printf(" %.*s%d, type:%s (%d)\n",
1051 (int)sizeof(info.kb_name), info.kb_name, info.kb_unit,
1052 get_kbd_type_name(info.kb_type), info.kb_type);
1054 if (ioctl(0, CONS_RELKBD, 0) == -1)
1055 warn("unable to release the keyboard");
1059 mux_keyboard(u_int op, char *kbd)
1061 keyboard_info_t info;
1065 * If stdin is not associated with a keyboard, the following ioctl
1068 if (ioctl(0, KDGKBINFO, &info) == -1) {
1069 warn("unable to obtain keyboard information");
1073 printf("kbd%d\n", info.kb_index);
1074 printf(" %.*s%d, type:%s (%d)\n",
1075 (int)sizeof(info.kb_name), info.kb_name, info.kb_unit,
1076 get_kbd_type_name(info.kb_type), info.kb_type);
1079 * split kbd into name and unit. find the right most part of the
1080 * kbd string that consist of only digits.
1083 memset(&info, 0, sizeof(info));
1089 unit = strpbrk(ep + 1, "0123456789");
1091 info.kb_unit = strtol(unit, &ep, 10);
1095 } while (unit != NULL && info.kb_unit == -1);
1097 if (info.kb_unit == -1) {
1098 warnx("unable to find keyboard driver unit in '%s'", kbd);
1103 warnx("unable to find keyboard driver name in '%s'", kbd);
1106 if (unit - kbd >= (int) sizeof(info.kb_name)) {
1107 warnx("keyboard name '%s' is too long", kbd);
1111 strncpy(info.kb_name, kbd, unit - kbd);
1114 * If stdin is not associated with a kbdmux(4) keyboard, the following
1118 if (ioctl(0, op, &info) == -1)
1119 warn("unable to (un)mux the keyboard");
1125 fprintf(stderr, "%s\n%s\n%s\n",
1126 "usage: kbdcontrol [-dFKix] [-A name] [-a name] [-b duration.pitch | [quiet.]belltype]",
1127 " [-r delay.repeat | speed] [-l mapfile] [-f # string]",
1128 " [-h size] [-k device] [-L mapfile]");
1134 main(int argc, char **argv)
1138 while((opt = getopt(argc, argv, "A:a:b:df:h:iKk:Fl:L:r:x")) != -1)
1142 mux_keyboard((opt == 'A')? KBRELKBD : KBADDKBD, optarg);
1145 set_bell_values(optarg);
1151 load_keymap(optarg, 0);
1154 load_keymap(optarg, 1);
1157 set_functionkey(optarg,
1158 nextarg(argc, argv, &optind, 'f'));
1161 load_default_functionkeys();
1164 set_history(optarg);
1173 set_keyboard(optarg);
1176 set_keyrates(optarg);
1184 if ((optind != argc) || (argc == 1))