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 withough 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 $
29 * $DragonFly: src/usr.sbin/kbdcontrol/kbdcontrol.c,v 1.5 2005/10/30 23:00:57 swildner Exp $
40 #include <machine/console.h>
44 char ctrl_names[32][4] = {
45 "nul", "soh", "stx", "etx", "eot", "enq", "ack", "bel",
46 "bs ", "ht ", "nl ", "vt ", "ff ", "cr ", "so ", "si ",
47 "dle", "dc1", "dc2", "dc3", "dc4", "nak", "syn", "etb",
48 "can", "em ", "sub", "esc", "fs ", "gs ", "rs ", "us "
51 char acc_names[15][5] = {
52 "dgra", "dacu", "dcir", "dtil", "dmac", "dbre", "ddot",
53 "duml", "dsla", "drin", "dced", "dapo", "ddac", "dogo",
57 char acc_names_u[15][5] = {
58 "DGRA", "DACU", "DCIR", "DTIL", "DMAC", "DBRE", "DDOT",
59 "DUML", "DSLA", "DRIN", "DCED", "DAPO", "DDAC", "DOGO",
63 char fkey_table[96][MAXFK] = {
64 /* 01-04 */ "\033[M", "\033[N", "\033[O", "\033[P",
65 /* 05-08 */ "\033[Q", "\033[R", "\033[S", "\033[T",
66 /* 09-12 */ "\033[U", "\033[V", "\033[W", "\033[X",
67 /* 13-16 */ "\033[Y", "\033[Z", "\033[a", "\033[b",
68 /* 17-20 */ "\033[c", "\033[d", "\033[e", "\033[f",
69 /* 21-24 */ "\033[g", "\033[h", "\033[i", "\033[j",
70 /* 25-28 */ "\033[k", "\033[l", "\033[m", "\033[n",
71 /* 29-32 */ "\033[o", "\033[p", "\033[q", "\033[r",
72 /* 33-36 */ "\033[s", "\033[t", "\033[u", "\033[v",
73 /* 37-40 */ "\033[w", "\033[x", "\033[y", "\033[z",
74 /* 41-44 */ "\033[@", "\033[[", "\033[\\","\033[]",
75 /* 45-48 */ "\033[^", "\033[_", "\033[`", "\033[{",
76 /* 49-52 */ "\033[H", "\033[A", "\033[I", "-" ,
77 /* 53-56 */ "\033[D", "\033[E", "\033[C", "+" ,
78 /* 57-60 */ "\033[F", "\033[B", "\033[G", "\033[L",
79 /* 61-64 */ "\177", "\033[J", "\033[~", "\033[}",
80 /* 65-68 */ "" , "" , "" , "" ,
81 /* 69-72 */ "" , "" , "" , "" ,
82 /* 73-76 */ "" , "" , "" , "" ,
83 /* 77-80 */ "" , "" , "" , "" ,
84 /* 81-84 */ "" , "" , "" , "" ,
85 /* 85-88 */ "" , "" , "" , "" ,
86 /* 89-92 */ "" , "" , "" , "" ,
87 /* 93-96 */ "" , "" , "" , "" ,
90 const int delays[] = {250, 500, 750, 1000};
91 const int repeats[] = { 34, 38, 42, 46, 50, 55, 59, 63,
92 68, 76, 84, 92, 100, 110, 118, 126,
93 136, 152, 168, 184, 200, 220, 236, 252,
94 272, 304, 336, 368, 400, 440, 472, 504};
95 const int ndelays = (sizeof(delays) / sizeof(int));
96 const int nrepeats = (sizeof(repeats) / sizeof(int));
102 int get_accent_definition_line(accentmap_t *);
103 void mux_keyboard(u_int op, char *kbd);
104 int get_key_definition_line(keymap_t *);
108 nextarg(int ac, char **av, int *indp, int oc)
111 return(av[(*indp)++]);
112 warnx("option requires two arguments -- %c", oc);
119 mkfullname(const char *s1, const char *s2, const char *s3)
121 static char *buf = NULL;
125 f = strlen(s1) + strlen(s2) + strlen(s3) + 1;
128 buf = (char *)realloc(buf, f);
130 buf = (char *)malloc(f);
148 switch ((token = yylex())) {
196 return LCTRA | 0x100;
198 return RCTRA | 0x100;
200 return LALTA | 0x100;
202 return RALTA | 0x100;
208 if (ACC(number) > L_ACC)
210 return ACC(number) | 0x100;
212 if (F(number) > L_FN)
214 return F(number) | 0x100;
216 if (S(number) > L_SCR)
218 return S(number) | 0x100;
220 return (unsigned char)letter;
222 if (number < 0 || number > 255)
231 get_definition_line(FILE *fd, keymap_t *keymap, accentmap_t *accentmap)
241 c = get_key_definition_line(keymap);
243 errx(1, "invalid key definition");
244 if (c > keymap->n_keys)
248 c = get_accent_definition_line(accentmap);
250 errx(1, "invalid accent key definition");
251 if (c > accentmap->n_accs)
252 accentmap->n_accs = c;
258 errx(1, "illegal definition line");
264 get_key_definition_line(keymap_t *map)
266 int i, def, scancode;
268 /* check scancode number */
269 if (number < 0 || number >= NUM_KEYS)
273 /* get key definitions */
274 map->key[scancode].spcl = 0;
275 for (i=0; i<NUM_STATES; i++) {
276 if ((def = get_entry()) == -1)
279 map->key[scancode].spcl |= (0x80 >> i);
280 map->key[scancode].map[i] = def & 0xFF;
282 /* get lock state key def */
283 if ((token = yylex()) != TFLAG)
285 map->key[scancode].flgs = number;
287 return (scancode + 1);
291 get_accent_definition_line(accentmap_t *map)
297 if (ACC(number) < F_ACC || ACC(number) > L_ACC)
298 /* number out of range */
301 if (map->acc[accent].accchar != 0) {
302 /* this entry has already been defined before! */
303 errx(1, "duplicated accent key definition");
306 switch ((token = yylex())) {
308 map->acc[accent].accchar = letter;
311 map->acc[accent].accchar = number;
317 for (i = 0; (token = yylex()) == '(';) {
318 switch ((token = yylex())) {
328 switch ((token = yylex())) {
338 if ((token = yylex()) != ')')
340 if (i >= NUM_ACCENTCHARS) {
341 warnx("too many accented characters, ignored");
344 map->acc[accent].map[i][0] = c1;
345 map->acc[accent].map[i][1] = c2;
352 print_entry(FILE *fp, int value)
354 int val = value & 0xFF;
358 fprintf(fp, " nop ");
361 fprintf(fp, " lshift");
364 fprintf(fp, " rshift");
367 fprintf(fp, " clock ");
370 fprintf(fp, " nlock ");
373 fprintf(fp, " slock ");
376 fprintf(fp, " btab ");
379 fprintf(fp, " lalt ");
382 fprintf(fp, " lctrl ");
385 fprintf(fp, " nscr ");
388 fprintf(fp, " pscr ");
391 fprintf(fp, " rctrl ");
394 fprintf(fp, " ralt ");
397 fprintf(fp, " alock ");
400 fprintf(fp, " ashift");
403 fprintf(fp, " meta ");
406 fprintf(fp, " boot ");
409 fprintf(fp, " debug ");
412 fprintf(fp, " susp ");
415 fprintf(fp, " saver ");
418 fprintf(fp, " panic ");
421 fprintf(fp, " lshifta");
424 fprintf(fp, " rshifta");
427 fprintf(fp, " lctrla");
430 fprintf(fp, " rctrla");
433 fprintf(fp, " lalta ");
436 fprintf(fp, " ralta ");
439 fprintf(fp, " halt ");
442 fprintf(fp, " pdwn ");
446 if (val >= F_FN && val <= L_FN)
447 fprintf(fp, " fkey%02d", val - F_FN + 1);
448 else if (val >= F_SCR && val <= L_SCR)
449 fprintf(fp, " scr%02d ", val - F_SCR + 1);
450 else if (val >= F_ACC && val <= L_ACC)
451 fprintf(fp, " %-6s", acc_names[val - F_ACC]);
453 fprintf(fp, " 0x%02x ", val);
455 fprintf(fp, " %3d ", val);
459 fprintf(fp, " %s ", ctrl_names[val]);
461 fprintf(fp, " del ");
462 else if (isascii(val) && isprint(val))
463 fprintf(fp, " '%c' ", val);
465 fprintf(fp, " 0x%02x ", val);
467 fprintf(fp, " %3d ", val);
474 print_key_definition_line(FILE *fp, int scancode, struct keyent_t *key)
478 /* print scancode number */
480 fprintf(fp, " 0x%02x ", scancode);
482 fprintf(fp, " %03d ", scancode);
484 /* print key definitions */
485 for (i=0; i<NUM_STATES; i++) {
486 if (key->spcl & (0x80 >> i))
487 print_entry(fp, key->map[i] | 0x100);
489 print_entry(fp, key->map[i]);
492 /* print lock state key def */
510 print_accent_definition_line(FILE *fp, int accent, struct acc_t *key)
515 if (key->accchar == 0)
518 /* print accent number */
519 fprintf(fp, " %-6s", acc_names[accent]);
520 if (isascii(key->accchar) && isprint(key->accchar))
521 fprintf(fp, "'%c' ", key->accchar);
523 fprintf(fp, "0x%02x ", key->accchar);
525 fprintf(fp, "%03d ", key->accchar);
527 for (i = 0; i < NUM_ACCENTCHARS; ++i) {
531 if ((i > 0) && ((i % 4) == 0))
533 if (isascii(c) && isprint(c))
534 fprintf(fp, "( '%c' ", c);
536 fprintf(fp, "(0x%02x ", c);
538 fprintf(fp, "( %03d ", c);
540 if (isascii(c) && isprint(c))
541 fprintf(fp, "'%c' ) ", c);
543 fprintf(fp, "0x%02x) ", c);
545 fprintf(fp, "%03d ) ", c);
551 dump_entry(int value)
644 if (value >= F_FN && value <= L_FN)
645 printf(" F(%2d),", value - F_FN + 1);
646 else if (value >= F_SCR && value <= L_SCR)
647 printf(" S(%2d),", value - F_SCR + 1);
648 else if (value >= F_ACC && value <= L_ACC)
649 printf(" %-4s, ", acc_names_u[value - F_ACC]);
651 printf(" 0x%02X, ", value);
654 } else if (value == '\'') {
656 } else if (value == '\\') {
658 } else if (isascii(value) && isprint(value)) {
659 printf(" '%c', ", value);
661 printf(" 0x%02X, ", value);
666 dump_key_definition(char *name, keymap_t *keymap)
670 printf("static keymap_t keymap_%s = { 0x%02x, {\n",
671 name, (unsigned)keymap->n_keys);
674 " * scan cntrl alt alt cntrl\n"
675 " * code base shift cntrl shift alt shift cntrl shift spcl flgs\n"
676 " * ---------------------------------------------------------------------------\n"
678 for (i = 0; i < keymap->n_keys; i++) {
679 printf("/*%02x*/{{", i);
680 for (j = 0; j < NUM_STATES; j++) {
681 if (keymap->key[i].spcl & (0x80 >> j))
682 dump_entry(keymap->key[i].map[j] | 0x100);
684 dump_entry(keymap->key[i].map[j]);
686 printf("}, 0x%02X,0x%02X },\n",
687 (unsigned)keymap->key[i].spcl,
688 (unsigned)keymap->key[i].flgs);
694 dump_accent_definition(char *name, accentmap_t *accentmap)
699 printf("static accentmap_t accentmap_%s = { %d",
700 name, accentmap->n_accs);
701 if (accentmap->n_accs <= 0) {
706 for (i = 0; i < NUM_DEADKEYS; i++) {
707 printf(" /* %s=%d */\n {", acc_names[i], i);
708 c = accentmap->acc[i].accchar;
712 printf(" '\\\\', {");
713 else if (isascii(c) && isprint(c))
714 printf(" '%c', {", c);
716 printf(" 0x00 }, \n");
719 printf(" 0x%02x, {", c);
720 for (j = 0; j < NUM_ACCENTCHARS; j++) {
721 c = accentmap->acc[i].map[j][0];
724 if ((j > 0) && ((j % 4) == 0))
726 if (isascii(c) && isprint(c))
727 printf(" { '%c',", c);
729 printf(" { 0x%02x,", c);
730 printf("0x%02x },", accentmap->acc[i].map[j][1]);
738 load_keymap(char *opt, int dumponly)
741 accentmap_t accentmap;
745 char *prefix[] = {"", "", KEYMAP_PATH, KEYMAP_PATH, NULL};
746 char *postfix[] = {"", ".kbd", "", ".kbd"};
748 for (i=0; prefix[i]; i++) {
749 name = mkfullname(prefix[i], opt, postfix[i]);
750 if ((fd = fopen(name, "r")))
754 warn("keymap file not found");
757 memset(&keymap, 0, sizeof(keymap));
758 memset(&accentmap, 0, sizeof(accentmap));
761 if (get_definition_line(fd, &keymap, &accentmap) < 0)
765 /* fix up the filename to make it a valid C identifier */
766 for (cp = opt; *cp; cp++)
767 if (!isalpha(*cp) && !isdigit(*cp)) *cp = '_';
769 " * Automatically generated from %s.\n"
772 dump_key_definition(opt, &keymap);
773 dump_accent_definition(opt, &accentmap);
776 if ((keymap.n_keys > 0) && (ioctl(0, PIO_KEYMAP, &keymap) < 0)) {
777 warn("setting keymap");
781 if ((accentmap.n_accs > 0)
782 && (ioctl(0, PIO_DEADKEYMAP, &accentmap) < 0)) {
783 warn("setting accentmap");
793 accentmap_t accentmap;
796 if (ioctl(0, GIO_KEYMAP, &keymap) < 0)
797 err(1, "getting keymap");
798 if (ioctl(0, GIO_DEADKEYMAP, &accentmap) < 0)
799 memset(&accentmap, 0, sizeof(accentmap));
802 "# scan cntrl alt alt cntrl lock\n"
803 "# code base shift cntrl shift alt shift cntrl shift state\n"
804 "# ------------------------------------------------------------------\n"
806 for (i=0; i<keymap.n_keys; i++)
807 print_key_definition_line(stdout, i, &keymap.key[i]);
810 for (i = 0; i < NUM_DEADKEYS; i++)
811 print_accent_definition_line(stdout, i, &accentmap.acc[i]);
817 load_default_functionkeys(void)
822 for (i=0; i<NUM_FKEYS; i++) {
824 strcpy(fkey.keydef, fkey_table[i]);
825 fkey.flen = strlen(fkey_table[i]);
826 if (ioctl(0, SETFKEY, &fkey) < 0)
827 warn("setting function key");
832 set_functionkey(char *keynumstr, char *string)
836 if (!strcmp(keynumstr, "load") && !strcmp(string, "default")) {
837 load_default_functionkeys();
840 fkey.keynum = atoi(keynumstr);
841 if (fkey.keynum < 1 || fkey.keynum > NUM_FKEYS) {
842 warnx("function key number must be between 1 and %d",
846 if ((fkey.flen = strlen(string)) > MAXFK) {
847 warnx("function key string too long (%d > %d)",
851 strcpy(fkey.keydef, string);
853 if (ioctl(0, SETFKEY, &fkey) < 0)
854 warn("setting function key");
859 set_bell_values(char *opt)
861 int bell, duration, pitch;
864 if (!strncmp(opt, "quiet.", 6)) {
868 if (!strcmp(opt, "visual"))
870 else if (!strcmp(opt, "normal"))
871 duration = 5, pitch = 800;
872 else if (!strcmp(opt, "off"))
873 duration = 0, pitch = 0;
878 duration = strtol(opt, &v1, 0);
879 if ((duration < 0) || (*v1 != '.'))
882 pitch = strtol(opt, &v1, 0);
883 if ((pitch < 0) || (*opt == '\0') || (*v1 != '\0')) {
885 warnx("argument to -b must be duration.pitch or [quiet.]visual|normal|off");
889 pitch = 1193182 / pitch; /* in Hz */
890 duration /= 10; /* in 10 m sec */
893 ioctl(0, CONS_BELLTYPE, &bell);
894 if ((bell & ~2) == 0)
895 fprintf(stderr, "
\e[=%d;%dB", pitch, duration);
900 set_keyrates(char *opt)
907 if (!strcmp(opt, "slow")) {
908 delay = 1000, repeat = 500;
910 } else if (!strcmp(opt, "normal")) {
911 delay = 500, repeat = 125;
913 } else if (!strcmp(opt, "fast")) {
920 delay = strtol(opt, &v1, 0);
921 if ((delay < 0) || (*v1 != '.'))
924 repeat = strtol(opt, &v1, 0);
925 if ((repeat < 0) || (*opt == '\0') || (*v1 != '\0')) {
927 warnx("argument to -r must be delay.repeat or slow|normal|fast");
930 for (n = 0; n < ndelays - 1; n++)
931 if (delay <= delays[n])
934 for (n = 0; n < nrepeats - 1; n++)
935 if (repeat <= repeats[n])
942 if (ioctl(0, KDSETREPEAT, arg)) {
943 if (ioctl(0, KDSETRAD, (d << 5) | r))
944 warn("setting keyboard rate");
950 set_history(char *opt)
955 if ((*opt == '\0') || size < 0) {
956 warnx("argument must be a positive number");
959 if (ioctl(0, CONS_HISTORY, &size) == -1)
960 warn("setting history buffer size");
964 get_kbd_type_name(int type)
971 { KB_101, "AT 101/102" },
972 { KB_OTHER, "generic" },
976 for (i = 0; i < sizeof(name_table)/sizeof(name_table[0]); ++i) {
977 if (type == name_table[i].type)
978 return name_table[i].name;
986 keyboard_info_t info;
988 if (ioctl(0, KDGKBINFO, &info) == -1) {
989 warn("unable to obtain keyboard information");
992 printf("kbd%d:\n", info.kb_index);
993 printf(" %.*s%d, type:%s (%d)\n",
994 (int)sizeof(info.kb_name), info.kb_name, info.kb_unit,
995 get_kbd_type_name(info.kb_type), info.kb_type);
1000 set_keyboard(char *device)
1002 keyboard_info_t info;
1005 fd = open(device, O_RDONLY);
1007 warn("cannot open %s", device);
1010 if (ioctl(fd, KDGKBINFO, &info) == -1) {
1011 warn("unable to obtain keyboard information");
1016 * The keyboard device driver won't release the keyboard by
1017 * the following ioctl, but it automatically will, when the device
1018 * is closed. So, we don't check error here.
1020 ioctl(fd, CONS_RELKBD, 0);
1023 printf("kbd%d\n", info.kb_index);
1024 printf(" %.*s%d, type:%s (%d)\n",
1025 (int)sizeof(info.kb_name), info.kb_name, info.kb_unit,
1026 get_kbd_type_name(info.kb_type), info.kb_type);
1029 if (ioctl(0, CONS_SETKBD, info.kb_index) == -1)
1030 warn("unable to set keyboard");
1035 release_keyboard(void)
1037 keyboard_info_t info;
1040 * If stdin is not associated with a keyboard, the following ioctl
1043 if (ioctl(0, KDGKBINFO, &info) == -1) {
1044 warn("unable to obtain keyboard information");
1048 printf("kbd%d\n", info.kb_index);
1049 printf(" %.*s%d, type:%s (%d)\n",
1050 (int)sizeof(info.kb_name), info.kb_name, info.kb_unit,
1051 get_kbd_type_name(info.kb_type), info.kb_type);
1053 if (ioctl(0, CONS_RELKBD, 0) == -1)
1054 warn("unable to release the keyboard");
1058 mux_keyboard(u_int op, char *kbd)
1060 keyboard_info_t info;
1064 * If stdin is not associated with a keyboard, the following ioctl
1067 if (ioctl(0, KDGKBINFO, &info) == -1) {
1068 warn("unable to obtain keyboard information");
1072 printf("kbd%d\n", info.kb_index);
1073 printf(" %.*s%d, type:%s (%d)\n",
1074 (int)sizeof(info.kb_name), info.kb_name, info.kb_unit,
1075 get_kbd_type_name(info.kb_type), info.kb_type);
1078 * split kbd into name and unit. find the right most part of the
1079 * kbd string that consist of only digits.
1082 memset(&info, 0, sizeof(info));
1088 unit = strpbrk(ep + 1, "0123456789");
1090 info.kb_unit = strtol(unit, &ep, 10);
1094 } while (unit != NULL && info.kb_unit == -1);
1096 if (info.kb_unit == -1) {
1097 warnx("unable to find keyboard driver unit in '%s'", kbd);
1102 warnx("unable to find keyboard driver name in '%s'", kbd);
1105 if (unit - kbd >= (int) sizeof(info.kb_name)) {
1106 warnx("keyboard name '%s' is too long", kbd);
1110 strncpy(info.kb_name, kbd, unit - kbd);
1113 * If stdin is not associated with a kbdmux(4) keyboard, the following
1117 if (ioctl(0, op, &info) == -1)
1118 warn("unable to (un)mux the keyboard");
1124 fprintf(stderr, "%s\n%s\n%s\n",
1125 "usage: kbdcontrol [-dFKix] [-A name] [-a name] [-b duration.pitch | [quiet.]belltype]",
1126 " [-r delay.repeat | speed] [-l mapfile] [-f # string]",
1127 " [-h size] [-k device] [-L mapfile]");
1133 main(int argc, char **argv)
1137 while((opt = getopt(argc, argv, "A:a:b:df:h:iKk:Fl:L:r:x")) != -1)
1141 mux_keyboard((opt == 'A')? KBRELKBD : KBADDKBD, optarg);
1144 set_bell_values(optarg);
1150 load_keymap(optarg, 0);
1153 load_keymap(optarg, 1);
1156 set_functionkey(optarg,
1157 nextarg(argc, argv, &optind, 'f'));
1160 load_default_functionkeys();
1163 set_history(optarg);
1172 set_keyboard(optarg);
1175 set_keyrates(optarg);
1183 if ((optind != argc) || (argc == 1))