2 * SPDX-License-Identifier: BSD-2-Clause
4 * Copyright (c) 1994-1995 Søren Schmidt
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer,
12 * in this position and unchanged.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. The name of the author may not be used to endorse or promote products
17 * derived from this software without specific prior written permission
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 #include <sys/cdefs.h>
40 #include <sys/consio.h>
41 #include <sys/param.h>
42 #include <sys/queue.h>
43 #include <sys/sysctl.h>
47 #define SPECIAL 0x80000000
49 #ifndef BOOTSTRAP_KBDCONTROL
50 static const char ctrl_names[32][4] = {
51 "nul", "soh", "stx", "etx", "eot", "enq", "ack", "bel",
52 "bs ", "ht ", "nl ", "vt ", "ff ", "cr ", "so ", "si ",
53 "dle", "dc1", "dc2", "dc3", "dc4", "nak", "syn", "etb",
54 "can", "em ", "sub", "esc", "fs ", "gs ", "rs ", "us "
58 static const char acc_names[15][5] = {
59 "dgra", "dacu", "dcir", "dtil", "dmac", "dbre", "ddot",
60 "duml", "dsla", "drin", "dced", "dapo", "ddac", "dogo",
64 static const char acc_names_u[15][5] = {
65 "DGRA", "DACU", "DCIR", "DTIL", "DMAC", "DBRE", "DDOT",
66 "DUML", "DSLA", "DRIN", "DCED", "DAPO", "DDAC", "DOGO",
70 #ifndef BOOTSTRAP_KBDCONTROL
71 static const char fkey_table[96][MAXFK] = {
72 /* 01-04 */ "\033[M", "\033[N", "\033[O", "\033[P",
73 /* 05-08 */ "\033[Q", "\033[R", "\033[S", "\033[T",
74 /* 09-12 */ "\033[U", "\033[V", "\033[W", "\033[X",
75 /* 13-16 */ "\033[Y", "\033[Z", "\033[a", "\033[b",
76 /* 17-20 */ "\033[c", "\033[d", "\033[e", "\033[f",
77 /* 21-24 */ "\033[g", "\033[h", "\033[i", "\033[j",
78 /* 25-28 */ "\033[k", "\033[l", "\033[m", "\033[n",
79 /* 29-32 */ "\033[o", "\033[p", "\033[q", "\033[r",
80 /* 33-36 */ "\033[s", "\033[t", "\033[u", "\033[v",
81 /* 37-40 */ "\033[w", "\033[x", "\033[y", "\033[z",
82 /* 41-44 */ "\033[@", "\033[[", "\033[\\","\033[]",
83 /* 45-48 */ "\033[^", "\033[_", "\033[`", "\033[{",
84 /* 49-52 */ "\033[H", "\033[A", "\033[I", "-" ,
85 /* 53-56 */ "\033[D", "\033[E", "\033[C", "+" ,
86 /* 57-60 */ "\033[F", "\033[B", "\033[G", "\033[L",
87 /* 61-64 */ "\177", "\033[J", "\033[~", "\033[}",
88 /* 65-68 */ "" , "" , "" , "" ,
89 /* 69-72 */ "" , "" , "" , "" ,
90 /* 73-76 */ "" , "" , "" , "" ,
91 /* 77-80 */ "" , "" , "" , "" ,
92 /* 81-84 */ "" , "" , "" , "" ,
93 /* 85-88 */ "" , "" , "" , "" ,
94 /* 89-92 */ "" , "" , "" , "" ,
95 /* 93-96 */ "" , "" , "" , "" ,
99 #ifndef BOOTSTRAP_KBDCONTROL
100 static const int ndelays = nitems(kbdelays);
101 static const int nrepeats = nitems(kbrates);
104 static int paths_configured = 0;
110 static void add_keymap_path(const char *path);
111 static void dump_accent_definition(char *name, accentmap_t *accentmap);
112 static void dump_entry(int value);
113 static void dump_key_definition(char *name, keymap_t *keymap);
114 static int get_accent_definition_line(accentmap_t *);
115 static int get_entry(void);
116 static int get_key_definition_line(keymap_t *);
117 static void load_keymap(char *opt, int dumponly);
118 #ifndef BOOTSTRAP_KBDCONTROL
119 static void load_default_functionkeys(void);
121 static char * nextarg(int ac, char **av, int *indp, int oc);
122 static char * mkfullname(const char *s1, const char *s2, const char *s3);
123 #ifndef BOOTSTRAP_KBDCONTROL
124 static void print_accent_definition_line(FILE *fp, int accent,
126 static void print_entry(FILE *fp, int value);
127 static void print_key_definition_line(FILE *fp, int scancode,
128 struct keyent_t *key);
129 static void print_keymap(void);
130 static void release_keyboard(void);
131 static void mux_keyboard(u_int op, char *kbd);
132 static void set_bell_values(char *opt);
133 static void set_functionkey(char *keynumstr, char *string);
134 static void set_keyboard(char *device);
135 static void set_keyrates(char *opt);
136 static void show_kbd_info(void);
138 static void usage(void) __dead2;
141 STAILQ_ENTRY(pathent) next;
144 static STAILQ_HEAD(, pathent) pathlist = STAILQ_HEAD_INITIALIZER(pathlist);
146 #ifndef BOOTSTRAP_KBDCONTROL
147 /* Detect presence of vt(4). */
151 char vty_name[4] = "";
152 size_t len = sizeof(vty_name);
154 if (sysctlbyname("kern.vty", vty_name, &len, NULL, 0) != 0)
156 return (strcmp(vty_name, "vt") == 0);
161 nextarg(int ac, char **av, int *indp, int oc)
164 return(av[(*indp)++]);
165 warnx("option requires two arguments -- %c", oc);
171 mkfullname(const char *s1, const char *s2, const char *s3)
173 static char *buf = NULL;
177 f = strlen(s1) + strlen(s2) + strlen(s3) + 1;
180 buf = (char *)realloc(buf, f);
182 buf = (char *)malloc(f);
200 switch ((token = yylex())) {
202 return NOP | SPECIAL;
204 return LSH | SPECIAL;
206 return RSH | SPECIAL;
208 return CLK | SPECIAL;
210 return NLK | SPECIAL;
212 return SLK | SPECIAL;
214 return BTAB | SPECIAL;
216 return LALT | SPECIAL;
218 return LCTR | SPECIAL;
220 return NEXT | SPECIAL;
222 return PREV | SPECIAL;
224 return RCTR | SPECIAL;
226 return RALT | SPECIAL;
228 return ALK | SPECIAL;
230 return ASH | SPECIAL;
232 return META | SPECIAL;
234 return RBT | SPECIAL;
236 return DBG | SPECIAL;
238 return SUSP | SPECIAL;
240 return SPSC | SPECIAL;
242 return PNC | SPECIAL;
244 return LSHA | SPECIAL;
246 return RSHA | SPECIAL;
248 return LCTRA | SPECIAL;
250 return RCTRA | SPECIAL;
252 return LALTA | SPECIAL;
254 return RALTA | SPECIAL;
256 return HALT | SPECIAL;
258 return PDWN | SPECIAL;
260 return PASTE | SPECIAL;
262 if (ACC(number) > L_ACC)
264 return ACC(number) | SPECIAL;
266 if (F(number) > L_FN)
268 return F(number) | SPECIAL;
270 if (S(number) > L_SCR)
272 return S(number) | SPECIAL;
274 return (unsigned char)letter;
276 if (number < 0x000000 || number > 0x10FFFF)
285 get_definition_line(FILE *file, keymap_t *keymap, accentmap_t *accentmap)
295 c = get_key_definition_line(keymap);
297 errx(1, "invalid key definition");
298 if (c > keymap->n_keys)
302 c = get_accent_definition_line(accentmap);
304 errx(1, "invalid accent key definition");
305 if (c > accentmap->n_accs)
306 accentmap->n_accs = c;
312 errx(1, "illegal definition line");
318 get_key_definition_line(keymap_t *map)
320 int i, def, scancode;
322 /* check scancode number */
323 if (number < 0 || number >= NUM_KEYS)
327 /* get key definitions */
328 map->key[scancode].spcl = 0;
329 for (i=0; i<NUM_STATES; i++) {
330 if ((def = get_entry()) == -1)
333 map->key[scancode].spcl |= (0x80 >> i);
334 map->key[scancode].map[i] = def & ~SPECIAL;
336 /* get lock state key def */
337 if ((token = yylex()) != TFLAG)
339 map->key[scancode].flgs = number;
341 return (scancode + 1);
345 get_accent_definition_line(accentmap_t *map)
351 if (ACC(number) < F_ACC || ACC(number) > L_ACC)
352 /* number out of range */
355 if (map->acc[accent].accchar != 0) {
356 /* this entry has already been defined before! */
357 errx(1, "duplicated accent key definition");
360 switch ((token = yylex())) {
362 map->acc[accent].accchar = letter;
365 map->acc[accent].accchar = number;
371 for (i = 0; (token = yylex()) == '(';) {
372 switch ((token = yylex())) {
382 switch ((token = yylex())) {
392 if ((token = yylex()) != ')')
394 if (i >= NUM_ACCENTCHARS) {
395 warnx("too many accented characters, ignored");
398 map->acc[accent].map[i][0] = c1;
399 map->acc[accent].map[i][1] = c2;
405 #ifndef BOOTSTRAP_KBDCONTROL
407 print_entry(FILE *fp, int value)
409 int val = value & ~SPECIAL;
413 fprintf(fp, " nop ");
416 fprintf(fp, " lshift");
419 fprintf(fp, " rshift");
422 fprintf(fp, " clock ");
425 fprintf(fp, " nlock ");
428 fprintf(fp, " slock ");
431 fprintf(fp, " btab ");
434 fprintf(fp, " lalt ");
437 fprintf(fp, " lctrl ");
440 fprintf(fp, " nscr ");
443 fprintf(fp, " pscr ");
446 fprintf(fp, " rctrl ");
449 fprintf(fp, " ralt ");
452 fprintf(fp, " alock ");
455 fprintf(fp, " ashift");
458 fprintf(fp, " meta ");
461 fprintf(fp, " boot ");
464 fprintf(fp, " debug ");
467 fprintf(fp, " susp ");
470 fprintf(fp, " saver ");
473 fprintf(fp, " panic ");
476 fprintf(fp, " lshifta");
479 fprintf(fp, " rshifta");
481 case LCTRA | SPECIAL:
482 fprintf(fp, " lctrla");
484 case RCTRA | SPECIAL:
485 fprintf(fp, " rctrla");
487 case LALTA | SPECIAL:
488 fprintf(fp, " lalta ");
490 case RALTA | SPECIAL:
491 fprintf(fp, " ralta ");
494 fprintf(fp, " halt ");
497 fprintf(fp, " pdwn ");
499 case PASTE | SPECIAL:
500 fprintf(fp, " paste ");
503 if (value & SPECIAL) {
504 if (val >= F_FN && val <= L_FN)
505 fprintf(fp, " fkey%02d", val - F_FN + 1);
506 else if (val >= F_SCR && val <= L_SCR)
507 fprintf(fp, " scr%02d ", val - F_SCR + 1);
508 else if (val >= F_ACC && val <= L_ACC)
509 fprintf(fp, " %-6s", acc_names[val - F_ACC]);
511 fprintf(fp, " 0x%02x ", val);
513 fprintf(fp, " %3d ", val);
517 fprintf(fp, " %s ", ctrl_names[val]);
519 fprintf(fp, " del ");
520 else if (isascii(val) && isprint(val))
521 fprintf(fp, " '%c' ", val);
523 fprintf(fp, " 0x%02x ", val);
525 fprintf(fp, " %3d ", val);
531 print_key_definition_line(FILE *fp, int scancode, struct keyent_t *key)
535 /* print scancode number */
537 fprintf(fp, " 0x%02x ", scancode);
539 fprintf(fp, " %03d ", scancode);
541 /* print key definitions */
542 for (i=0; i<NUM_STATES; i++) {
543 if (key->spcl & (0x80 >> i))
544 print_entry(fp, key->map[i] | SPECIAL);
546 print_entry(fp, key->map[i]);
549 /* print lock state key def */
567 print_accent_definition_line(FILE *fp, int accent, struct acc_t *key)
572 if (key->accchar == 0)
575 /* print accent number */
576 fprintf(fp, " %-6s", acc_names[accent]);
577 if (isascii(key->accchar) && isprint(key->accchar))
578 fprintf(fp, "'%c' ", key->accchar);
580 fprintf(fp, "0x%02x ", key->accchar);
582 fprintf(fp, "%03d ", key->accchar);
584 for (i = 0; i < NUM_ACCENTCHARS; ++i) {
588 if ((i > 0) && ((i % 4) == 0))
590 if (isascii(c) && isprint(c))
591 fprintf(fp, "( '%c' ", c);
593 fprintf(fp, "(0x%02x ", c);
595 fprintf(fp, "( %03d ", c);
597 if (isascii(c) && isprint(c))
598 fprintf(fp, "'%c' ) ", c);
600 fprintf(fp, "0x%02x) ", c);
602 fprintf(fp, "%03d ) ", c);
609 dump_entry(int value)
611 if (value & SPECIAL) {
705 if (value >= F_FN && value <= L_FN)
706 printf(" F(%2d),", value - F_FN + 1);
707 else if (value >= F_SCR && value <= L_SCR)
708 printf(" S(%2d),", value - F_SCR + 1);
709 else if (value >= F_ACC && value <= L_ACC)
710 printf(" %-4s, ", acc_names_u[value - F_ACC]);
712 printf(" 0x%02X, ", value);
715 } else if (value == '\'') {
717 } else if (value == '\\') {
719 } else if (isascii(value) && isprint(value)) {
720 printf(" '%c', ", value);
722 printf(" 0x%02X, ", value);
727 dump_key_definition(char *name, keymap_t *keymap)
731 printf("static keymap_t keymap_%s = { 0x%02x, {\n",
732 name, (unsigned)keymap->n_keys);
735 " * scan cntrl alt alt cntrl\n"
736 " * code base shift cntrl shift alt shift cntrl shift spcl flgs\n"
737 " * ---------------------------------------------------------------------------\n"
739 for (i = 0; i < keymap->n_keys; i++) {
740 printf("/*%02x*/{{", i);
741 for (j = 0; j < NUM_STATES; j++) {
742 if (keymap->key[i].spcl & (0x80 >> j))
743 dump_entry(keymap->key[i].map[j] | SPECIAL);
745 dump_entry(keymap->key[i].map[j]);
747 printf("}, 0x%02X,0x%02X },\n",
748 (unsigned)keymap->key[i].spcl,
749 (unsigned)keymap->key[i].flgs);
755 dump_accent_definition(char *name, accentmap_t *accentmap)
760 printf("static accentmap_t accentmap_%s = { %d",
761 name, accentmap->n_accs);
762 if (accentmap->n_accs <= 0) {
767 for (i = 0; i < NUM_DEADKEYS; i++) {
768 printf(" /* %s=%d */\n {", acc_names[i], i);
769 c = accentmap->acc[i].accchar;
773 printf(" '\\\\', {");
774 else if (isascii(c) && isprint(c))
775 printf(" '%c', {", c);
777 printf(" 0x00 }, \n");
780 printf(" 0x%02x, {", c);
781 for (j = 0; j < NUM_ACCENTCHARS; j++) {
782 c = accentmap->acc[i].map[j][0];
785 if ((j > 0) && ((j % 4) == 0))
787 if (isascii(c) && isprint(c))
788 printf(" { '%c',", c);
790 printf(" { 0x%02x,", c);
791 printf("0x%02x },", accentmap->acc[i].map[j][1]);
799 add_keymap_path(const char *path)
805 if ((pe = malloc(sizeof(*pe))) == NULL ||
806 (pe->path = malloc(len + 2)) == NULL)
808 memcpy(pe->path, path, len);
809 if (len > 0 && path[len - 1] != '/')
810 pe->path[len++] = '/';
811 pe->path[len] = '\0';
812 STAILQ_INSERT_TAIL(&pathlist, pe, next);
815 #ifdef OPIO_DEADKEYMAP
817 to_old_accentmap(accentmap_t *from, oaccentmap_t *to)
821 to->n_accs = from->n_accs;
822 for (i = 0; i < NUM_DEADKEYS; i++) {
823 for (j = 0; j < NUM_ACCENTCHARS; j++) {
824 to->acc[i].map[j][0] = from->acc[i].map[j][0];
825 to->acc[i].map[j][1] = from->acc[i].map[j][1];
826 to->acc[i].accchar = from->acc[i].accchar;
830 #endif /* OPIO_DEADKEYMAP */
833 load_keymap(char *opt, int dumponly)
836 accentmap_t accentmap;
837 #ifdef OPIO_DEADKEYMAP
838 oaccentmap_t oaccentmap;
839 #endif /* OPIO_DEADKEYMAP */
844 char blank[] = "", keymap_path[] = KEYMAP_PATH;
845 char vt_keymap_path[] = VT_KEYMAP_PATH, dotkbd[] = ".kbd";
846 char *postfix[] = {blank, dotkbd, NULL};
848 if (!paths_configured) {
849 cp = getenv("KEYMAP_PATH");
853 #ifndef BOOTSTRAP_KBDCONTROL
855 add_keymap_path(vt_keymap_path);
857 add_keymap_path(keymap_path);
859 paths_configured = 1;
863 STAILQ_FOREACH(pe, &pathlist, next) {
864 for (j=0; postfix[j] && file == NULL; j++) {
865 name = mkfullname(pe->path, opt, postfix[j]);
866 file = fopen(name, "r");
872 warn("keymap file \"%s\" not found", opt);
875 memset(&keymap, 0, sizeof(keymap));
876 memset(&accentmap, 0, sizeof(accentmap));
879 if (get_definition_line(file, &keymap, &accentmap) < 0)
883 /* fix up the filename to make it a valid C identifier */
884 for (cp = opt; *cp; cp++)
885 if (!isalpha(*cp) && !isdigit(*cp)) *cp = '_';
887 " * Automatically generated from %s.\n"
890 dump_key_definition(opt, &keymap);
891 dump_accent_definition(opt, &accentmap);
894 #ifndef BOOTSTRAP_KBDCONTROL
895 if ((keymap.n_keys > 0) && (ioctl(0, PIO_KEYMAP, &keymap) < 0)) {
896 warn("setting keymap");
900 if ((accentmap.n_accs > 0)
901 && (ioctl(0, PIO_DEADKEYMAP, &accentmap) < 0)) {
902 #ifdef OPIO_DEADKEYMAP
903 to_old_accentmap(&accentmap, &oaccentmap);
904 if (ioctl(0, OPIO_DEADKEYMAP, &oaccentmap) < 0)
905 #endif /* OGIO_DEADKEYMAP */
907 warn("setting accentmap");
915 #ifdef OPIO_DEADKEYMAP
917 to_new_accentmap(oaccentmap_t *from, accentmap_t *to)
921 to->n_accs = from->n_accs;
922 for (i = 0; i < NUM_DEADKEYS; i++) {
923 for (j = 0; j < NUM_ACCENTCHARS; j++) {
924 to->acc[i].map[j][0] = from->acc[i].map[j][0];
925 to->acc[i].map[j][1] = from->acc[i].map[j][1];
926 to->acc[i].accchar = from->acc[i].accchar;
930 #endif /* OPIO_DEADKEYMAP */
932 #ifndef BOOTSTRAP_KBDCONTROL
937 accentmap_t accentmap;
938 #ifdef OGIO_DEADKEYMAP
939 oaccentmap_t oaccentmap;
940 #endif /* OPIO_DEADKEYMAP */
943 if (ioctl(0, GIO_KEYMAP, &keymap) < 0)
944 err(1, "getting keymap");
945 if (ioctl(0, GIO_DEADKEYMAP, &accentmap) < 0) {
946 #ifdef OGIO_DEADKEYMAP
947 if (ioctl(0, OGIO_DEADKEYMAP, &oaccentmap) == 0)
948 to_new_accentmap(&oaccentmap, &accentmap);
950 #endif /* OGIO_DEADKEYMAP */
951 memset(&accentmap, 0, sizeof(accentmap));
955 "# scan cntrl alt alt cntrl lock\n"
956 "# code base shift cntrl shift alt shift cntrl shift state\n"
957 "# ------------------------------------------------------------------\n"
959 for (i=0; i<keymap.n_keys; i++)
960 print_key_definition_line(stdout, i, &keymap.key[i]);
963 for (i = 0; i < NUM_DEADKEYS; i++)
964 print_accent_definition_line(stdout, i, &accentmap.acc[i]);
969 load_default_functionkeys(void)
974 for (i=0; i<NUM_FKEYS; i++) {
976 strcpy(fkey.keydef, fkey_table[i]);
977 fkey.flen = strlen(fkey_table[i]);
978 if (ioctl(0, SETFKEY, &fkey) < 0)
979 warn("setting function key");
984 set_functionkey(char *keynumstr, char *string)
988 if (!strcmp(keynumstr, "load") && !strcmp(string, "default")) {
989 load_default_functionkeys();
992 fkey.keynum = atoi(keynumstr);
993 if (fkey.keynum < 1 || fkey.keynum > NUM_FKEYS) {
994 warnx("function key number must be between 1 and %d",
998 if ((fkey.flen = strlen(string)) > MAXFK) {
999 warnx("function key string too long (%d > %d)",
1003 strncpy(fkey.keydef, string, MAXFK);
1005 if (ioctl(0, SETFKEY, &fkey) < 0)
1006 warn("setting function key");
1010 set_bell_values(char *opt)
1012 int bell, duration, pitch;
1017 if (!strncmp(opt, "quiet.", 6)) {
1018 bell = CONS_QUIET_BELL;
1021 if (!strcmp(opt, "visual"))
1022 bell |= CONS_VISUAL_BELL;
1023 else if (!strcmp(opt, "normal"))
1024 duration = 5, pitch = 800;
1025 else if (!strcmp(opt, "off"))
1026 duration = 0, pitch = 0;
1031 duration = strtol(opt, &v1, 0);
1032 if ((duration < 0) || (*v1 != '.'))
1035 pitch = strtol(opt, &v1, 0);
1036 if ((pitch < 0) || (*opt == '\0') || (*v1 != '\0')) {
1038 warnx("argument to -b must be duration.pitch or [quiet.]visual|normal|off");
1042 pitch = 1193182 / pitch; /* in Hz */
1043 duration /= 10; /* in 10 m sec */
1046 ioctl(0, CONS_BELLTYPE, &bell);
1047 if (duration > 0 && pitch > 0)
1048 fprintf(stderr, "\e[=%d;%dB", pitch, duration);
1052 set_keyrates(char *opt)
1059 if (!strcmp(opt, "slow")) {
1064 } else if (!strcmp(opt, "normal")) {
1069 } else if (!strcmp(opt, "fast")) {
1078 delay = strtol(opt, &v1, 0);
1079 if ((delay < 0) || (*v1 != '.'))
1082 repeat = strtol(opt, &v1, 0);
1083 if ((repeat < 0) || (*opt == '\0') || (*v1 != '\0')) {
1085 warnx("argument to -r must be delay.repeat or slow|normal|fast");
1088 for (n = 0; n < ndelays - 1; n++)
1089 if (delay <= kbdelays[n])
1092 for (n = 0; n < nrepeats - 1; n++)
1093 if (repeat <= kbrates[n])
1100 if (ioctl(0, KDSETREPEAT, arg)) {
1101 warn("fallback, setting keyboard rate via legacy interface (KDSETRAD), will be removed soon");
1102 if (ioctl(0, KDSETRAD, (d << 5) | r))
1103 warn("setting keyboard rate");
1108 get_kbd_type_name(int type)
1115 { KB_101, "AT 101/102" },
1116 { KB_OTHER, "generic" },
1120 for (i = 0; i < sizeof(name_table)/sizeof(name_table[0]); ++i) {
1121 if (type == name_table[i].type)
1122 return name_table[i].name;
1130 keyboard_info_t info;
1132 if (ioctl(0, KDGKBINFO, &info) == -1) {
1133 warn("unable to obtain keyboard information");
1136 printf("kbd%d:\n", info.kb_index);
1137 printf(" %.*s%d, type:%s (%d)\n",
1138 (int)sizeof(info.kb_name), info.kb_name, info.kb_unit,
1139 get_kbd_type_name(info.kb_type), info.kb_type);
1143 set_keyboard(char *device)
1145 keyboard_info_t info;
1148 fd = open(device, O_RDONLY);
1150 warn("cannot open %s", device);
1153 if (ioctl(fd, KDGKBINFO, &info) == -1) {
1154 warn("unable to obtain keyboard information");
1159 * The keyboard device driver won't release the keyboard by
1160 * the following ioctl, but it automatically will, when the device
1161 * is closed. So, we don't check error here.
1163 ioctl(fd, CONS_RELKBD, 0);
1166 printf("kbd%d\n", info.kb_index);
1167 printf(" %.*s%d, type:%s (%d)\n",
1168 (int)sizeof(info.kb_name), info.kb_name, info.kb_unit,
1169 get_kbd_type_name(info.kb_type), info.kb_type);
1172 if (ioctl(0, CONS_SETKBD, info.kb_index) == -1)
1173 warn("unable to set keyboard");
1177 release_keyboard(void)
1179 keyboard_info_t info;
1182 * If stdin is not associated with a keyboard, the following ioctl
1185 if (ioctl(0, KDGKBINFO, &info) == -1) {
1186 warn("unable to obtain keyboard information");
1190 printf("kbd%d\n", info.kb_index);
1191 printf(" %.*s%d, type:%s (%d)\n",
1192 (int)sizeof(info.kb_name), info.kb_name, info.kb_unit,
1193 get_kbd_type_name(info.kb_type), info.kb_type);
1195 if (ioctl(0, CONS_RELKBD, 0) == -1)
1196 warn("unable to release the keyboard");
1200 mux_keyboard(u_int op, char *kbd)
1202 keyboard_info_t info;
1206 * If stdin is not associated with a keyboard, the following ioctl
1209 if (ioctl(0, KDGKBINFO, &info) == -1) {
1210 warn("unable to obtain keyboard information");
1214 printf("kbd%d\n", info.kb_index);
1215 printf(" %.*s%d, type:%s (%d)\n",
1216 (int)sizeof(info.kb_name), info.kb_name, info.kb_unit,
1217 get_kbd_type_name(info.kb_type), info.kb_type);
1220 * split kbd into name and unit. find the right most part of the
1221 * kbd string that consist of only digits.
1224 memset(&info, 0, sizeof(info));
1230 unit = strpbrk(ep + 1, "0123456789");
1232 info.kb_unit = strtol(unit, &ep, 10);
1236 } while (unit != NULL && info.kb_unit == -1);
1238 if (info.kb_unit == -1) {
1239 warnx("unable to find keyboard driver unit in '%s'", kbd);
1244 warnx("unable to find keyboard driver name in '%s'", kbd);
1247 if (unit - kbd >= (int) sizeof(info.kb_name)) {
1248 warnx("keyboard name '%s' is too long", kbd);
1252 strncpy(info.kb_name, kbd, unit - kbd);
1255 * If stdin is not associated with a kbdmux(4) keyboard, the following
1259 if (ioctl(0, op, &info) == -1)
1260 warn("unable to (un)mux the keyboard");
1267 #ifdef BOOTSTRAP_KBDCONTROL
1268 fprintf(stderr, "%s\n",
1269 "usage: kbdcontrol [-L mapfile] [-P path]");
1271 fprintf(stderr, "%s\n%s\n%s\n",
1272 "usage: kbdcontrol [-dFKix] [-A name] [-a name] [-b duration.pitch | [quiet.]belltype]",
1273 " [-r delay.repeat | speed] [-l mapfile] [-f # string]",
1274 " [-k device] [-L mapfile] [-P path]");
1281 main(int argc, char **argv)
1283 #ifdef BOOTSTRAP_KBDCONTROL
1284 const char *optstring = "L:P:";
1286 const char *optstring = "A:a:b:df:iKk:Fl:L:P:r:x";
1290 /* Collect any -P arguments, regardless of where they appear. */
1291 while ((opt = getopt(argc, argv, optstring)) != -1) {
1293 add_keymap_path(optarg);
1298 optind = optreset = 1;
1299 while ((opt = getopt(argc, argv, optstring)) != -1)
1301 #ifndef BOOTSTRAP_KBDCONTROL
1304 mux_keyboard((opt == 'A')? KBRELKBD : KBADDKBD, optarg);
1307 set_bell_values(optarg);
1313 load_keymap(optarg, 0);
1317 load_keymap(optarg, 1);
1321 #ifndef BOOTSTRAP_KBDCONTROL
1323 set_functionkey(optarg,
1324 nextarg(argc, argv, &optind, 'f'));
1327 load_default_functionkeys();
1336 set_keyboard(optarg);
1339 set_keyrates(optarg);
1348 if ((optind != argc) || (argc == 1))