2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
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>
32 __FBSDID("$FreeBSD$");
42 #include <sys/consio.h>
43 #include <sys/queue.h>
44 #include <sys/sysctl.h>
49 * HALT, PDWN, and PASTE aren't defined in 4.x, but we need them to bridge
50 * to 5.0-current so define them here as a stop gap transition measure.
53 #define HALT 0xa1 /* halt machine */
56 #define PDWN 0xa2 /* halt machine and power down */
59 #define PASTE 0xa3 /* paste from cut-paste buffer */
62 #define SPECIAL 0x80000000
64 static const char ctrl_names[32][4] = {
65 "nul", "soh", "stx", "etx", "eot", "enq", "ack", "bel",
66 "bs ", "ht ", "nl ", "vt ", "ff ", "cr ", "so ", "si ",
67 "dle", "dc1", "dc2", "dc3", "dc4", "nak", "syn", "etb",
68 "can", "em ", "sub", "esc", "fs ", "gs ", "rs ", "us "
71 static const char acc_names[15][5] = {
72 "dgra", "dacu", "dcir", "dtil", "dmac", "dbre", "ddot",
73 "duml", "dsla", "drin", "dced", "dapo", "ddac", "dogo",
77 static const char acc_names_u[15][5] = {
78 "DGRA", "DACU", "DCIR", "DTIL", "DMAC", "DBRE", "DDOT",
79 "DUML", "DSLA", "DRIN", "DCED", "DAPO", "DDAC", "DOGO",
83 static const char fkey_table[96][MAXFK] = {
84 /* 01-04 */ "\033[M", "\033[N", "\033[O", "\033[P",
85 /* 05-08 */ "\033[Q", "\033[R", "\033[S", "\033[T",
86 /* 09-12 */ "\033[U", "\033[V", "\033[W", "\033[X",
87 /* 13-16 */ "\033[Y", "\033[Z", "\033[a", "\033[b",
88 /* 17-20 */ "\033[c", "\033[d", "\033[e", "\033[f",
89 /* 21-24 */ "\033[g", "\033[h", "\033[i", "\033[j",
90 /* 25-28 */ "\033[k", "\033[l", "\033[m", "\033[n",
91 /* 29-32 */ "\033[o", "\033[p", "\033[q", "\033[r",
92 /* 33-36 */ "\033[s", "\033[t", "\033[u", "\033[v",
93 /* 37-40 */ "\033[w", "\033[x", "\033[y", "\033[z",
94 /* 41-44 */ "\033[@", "\033[[", "\033[\\","\033[]",
95 /* 45-48 */ "\033[^", "\033[_", "\033[`", "\033[{",
96 /* 49-52 */ "\033[H", "\033[A", "\033[I", "-" ,
97 /* 53-56 */ "\033[D", "\033[E", "\033[C", "+" ,
98 /* 57-60 */ "\033[F", "\033[B", "\033[G", "\033[L",
99 /* 61-64 */ "\177", "\033[J", "\033[~", "\033[}",
100 /* 65-68 */ "" , "" , "" , "" ,
101 /* 69-72 */ "" , "" , "" , "" ,
102 /* 73-76 */ "" , "" , "" , "" ,
103 /* 77-80 */ "" , "" , "" , "" ,
104 /* 81-84 */ "" , "" , "" , "" ,
105 /* 85-88 */ "" , "" , "" , "" ,
106 /* 89-92 */ "" , "" , "" , "" ,
107 /* 93-96 */ "" , "" , "" , "" ,
110 static const int delays[] = {250, 500, 750, 1000};
111 static const int repeats[] = { 34, 38, 42, 46, 50, 55, 59, 63,
112 68, 76, 84, 92, 100, 110, 118, 126,
113 136, 152, 168, 184, 200, 220, 236, 252,
114 272, 304, 336, 368, 400, 440, 472, 504};
115 static const int ndelays = (sizeof(delays) / sizeof(int));
116 static const int nrepeats = (sizeof(repeats) / sizeof(int));
118 static int paths_configured = 0;
124 static void add_keymap_path(const char *path);
125 static void dump_accent_definition(char *name, accentmap_t *accentmap);
126 static void dump_entry(int value);
127 static void dump_key_definition(char *name, keymap_t *keymap);
128 static int get_accent_definition_line(accentmap_t *);
129 static int get_entry(void);
130 static int get_key_definition_line(keymap_t *);
131 static void load_keymap(char *opt, int dumponly);
132 static void load_default_functionkeys(void);
133 static char * nextarg(int ac, char **av, int *indp, int oc);
134 static char * mkfullname(const char *s1, const char *s2, const char *s3);
135 static void print_accent_definition_line(FILE *fp, int accent,
137 static void print_entry(FILE *fp, int value);
138 static void print_key_definition_line(FILE *fp, int scancode,
139 struct keyent_t *key);
140 static void print_keymap(void);
141 static void release_keyboard(void);
142 static void mux_keyboard(u_int op, char *kbd);
143 static void set_bell_values(char *opt);
144 static void set_functionkey(char *keynumstr, char *string);
145 static void set_keyboard(char *device);
146 static void set_keyrates(char *opt);
147 static void show_kbd_info(void);
148 static void usage(void) __dead2;
151 STAILQ_ENTRY(pathent) next;
154 static STAILQ_HEAD(, pathent) pathlist = STAILQ_HEAD_INITIALIZER(pathlist);
156 /* Detect presence of vt(4). */
160 char vty_name[4] = "";
161 size_t len = sizeof(vty_name);
163 if (sysctlbyname("kern.vty", vty_name, &len, NULL, 0) != 0)
165 return (strcmp(vty_name, "vt") == 0);
169 nextarg(int ac, char **av, int *indp, int oc)
172 return(av[(*indp)++]);
173 warnx("option requires two arguments -- %c", oc);
179 mkfullname(const char *s1, const char *s2, const char *s3)
181 static char *buf = NULL;
185 f = strlen(s1) + strlen(s2) + strlen(s3) + 1;
188 buf = (char *)realloc(buf, f);
190 buf = (char *)malloc(f);
208 switch ((token = yylex())) {
210 return NOP | SPECIAL;
212 return LSH | SPECIAL;
214 return RSH | SPECIAL;
216 return CLK | SPECIAL;
218 return NLK | SPECIAL;
220 return SLK | SPECIAL;
222 return BTAB | SPECIAL;
224 return LALT | SPECIAL;
226 return LCTR | SPECIAL;
228 return NEXT | SPECIAL;
230 return PREV | SPECIAL;
232 return RCTR | SPECIAL;
234 return RALT | SPECIAL;
236 return ALK | SPECIAL;
238 return ASH | SPECIAL;
240 return META | SPECIAL;
242 return RBT | SPECIAL;
244 return DBG | SPECIAL;
246 return SUSP | SPECIAL;
248 return SPSC | SPECIAL;
250 return PNC | SPECIAL;
252 return LSHA | SPECIAL;
254 return RSHA | SPECIAL;
256 return LCTRA | SPECIAL;
258 return RCTRA | SPECIAL;
260 return LALTA | SPECIAL;
262 return RALTA | SPECIAL;
264 return HALT | SPECIAL;
266 return PDWN | SPECIAL;
268 return PASTE | SPECIAL;
270 if (ACC(number) > L_ACC)
272 return ACC(number) | SPECIAL;
274 if (F(number) > L_FN)
276 return F(number) | SPECIAL;
278 if (S(number) > L_SCR)
280 return S(number) | SPECIAL;
282 return (unsigned char)letter;
284 if (number < 0x000000 || number > 0x10FFFF)
293 get_definition_line(FILE *file, keymap_t *keymap, accentmap_t *accentmap)
303 c = get_key_definition_line(keymap);
305 errx(1, "invalid key definition");
306 if (c > keymap->n_keys)
310 c = get_accent_definition_line(accentmap);
312 errx(1, "invalid accent key definition");
313 if (c > accentmap->n_accs)
314 accentmap->n_accs = c;
320 errx(1, "illegal definition line");
326 get_key_definition_line(keymap_t *map)
328 int i, def, scancode;
330 /* check scancode number */
331 if (number < 0 || number >= NUM_KEYS)
335 /* get key definitions */
336 map->key[scancode].spcl = 0;
337 for (i=0; i<NUM_STATES; i++) {
338 if ((def = get_entry()) == -1)
341 map->key[scancode].spcl |= (0x80 >> i);
342 map->key[scancode].map[i] = def & ~SPECIAL;
344 /* get lock state key def */
345 if ((token = yylex()) != TFLAG)
347 map->key[scancode].flgs = number;
349 return (scancode + 1);
353 get_accent_definition_line(accentmap_t *map)
359 if (ACC(number) < F_ACC || ACC(number) > L_ACC)
360 /* number out of range */
363 if (map->acc[accent].accchar != 0) {
364 /* this entry has already been defined before! */
365 errx(1, "duplicated accent key definition");
368 switch ((token = yylex())) {
370 map->acc[accent].accchar = letter;
373 map->acc[accent].accchar = number;
379 for (i = 0; (token = yylex()) == '(';) {
380 switch ((token = yylex())) {
390 switch ((token = yylex())) {
400 if ((token = yylex()) != ')')
402 if (i >= NUM_ACCENTCHARS) {
403 warnx("too many accented characters, ignored");
406 map->acc[accent].map[i][0] = c1;
407 map->acc[accent].map[i][1] = c2;
414 print_entry(FILE *fp, int value)
416 int val = value & ~SPECIAL;
420 fprintf(fp, " nop ");
423 fprintf(fp, " lshift");
426 fprintf(fp, " rshift");
429 fprintf(fp, " clock ");
432 fprintf(fp, " nlock ");
435 fprintf(fp, " slock ");
438 fprintf(fp, " btab ");
441 fprintf(fp, " lalt ");
444 fprintf(fp, " lctrl ");
447 fprintf(fp, " nscr ");
450 fprintf(fp, " pscr ");
453 fprintf(fp, " rctrl ");
456 fprintf(fp, " ralt ");
459 fprintf(fp, " alock ");
462 fprintf(fp, " ashift");
465 fprintf(fp, " meta ");
468 fprintf(fp, " boot ");
471 fprintf(fp, " debug ");
474 fprintf(fp, " susp ");
477 fprintf(fp, " saver ");
480 fprintf(fp, " panic ");
483 fprintf(fp, " lshifta");
486 fprintf(fp, " rshifta");
488 case LCTRA | SPECIAL:
489 fprintf(fp, " lctrla");
491 case RCTRA | SPECIAL:
492 fprintf(fp, " rctrla");
494 case LALTA | SPECIAL:
495 fprintf(fp, " lalta ");
497 case RALTA | SPECIAL:
498 fprintf(fp, " ralta ");
501 fprintf(fp, " halt ");
504 fprintf(fp, " pdwn ");
506 case PASTE | SPECIAL:
507 fprintf(fp, " paste ");
510 if (value & SPECIAL) {
511 if (val >= F_FN && val <= L_FN)
512 fprintf(fp, " fkey%02d", val - F_FN + 1);
513 else if (val >= F_SCR && val <= L_SCR)
514 fprintf(fp, " scr%02d ", val - F_SCR + 1);
515 else if (val >= F_ACC && val <= L_ACC)
516 fprintf(fp, " %-6s", acc_names[val - F_ACC]);
518 fprintf(fp, " 0x%02x ", val);
520 fprintf(fp, " %3d ", val);
524 fprintf(fp, " %s ", ctrl_names[val]);
526 fprintf(fp, " del ");
527 else if (isascii(val) && isprint(val))
528 fprintf(fp, " '%c' ", val);
530 fprintf(fp, " 0x%02x ", val);
532 fprintf(fp, " %3d ", val);
538 print_key_definition_line(FILE *fp, int scancode, struct keyent_t *key)
542 /* print scancode number */
544 fprintf(fp, " 0x%02x ", scancode);
546 fprintf(fp, " %03d ", scancode);
548 /* print key definitions */
549 for (i=0; i<NUM_STATES; i++) {
550 if (key->spcl & (0x80 >> i))
551 print_entry(fp, key->map[i] | SPECIAL);
553 print_entry(fp, key->map[i]);
556 /* print lock state key def */
574 print_accent_definition_line(FILE *fp, int accent, struct acc_t *key)
579 if (key->accchar == 0)
582 /* print accent number */
583 fprintf(fp, " %-6s", acc_names[accent]);
584 if (isascii(key->accchar) && isprint(key->accchar))
585 fprintf(fp, "'%c' ", key->accchar);
587 fprintf(fp, "0x%02x ", key->accchar);
589 fprintf(fp, "%03d ", key->accchar);
591 for (i = 0; i < NUM_ACCENTCHARS; ++i) {
595 if ((i > 0) && ((i % 4) == 0))
597 if (isascii(c) && isprint(c))
598 fprintf(fp, "( '%c' ", c);
600 fprintf(fp, "(0x%02x ", c);
602 fprintf(fp, "( %03d ", c);
604 if (isascii(c) && isprint(c))
605 fprintf(fp, "'%c' ) ", c);
607 fprintf(fp, "0x%02x) ", c);
609 fprintf(fp, "%03d ) ", c);
615 dump_entry(int value)
617 if (value & SPECIAL) {
711 if (value >= F_FN && value <= L_FN)
712 printf(" F(%2d),", value - F_FN + 1);
713 else if (value >= F_SCR && value <= L_SCR)
714 printf(" S(%2d),", value - F_SCR + 1);
715 else if (value >= F_ACC && value <= L_ACC)
716 printf(" %-4s, ", acc_names_u[value - F_ACC]);
718 printf(" 0x%02X, ", value);
721 } else if (value == '\'') {
723 } else if (value == '\\') {
725 } else if (isascii(value) && isprint(value)) {
726 printf(" '%c', ", value);
728 printf(" 0x%02X, ", value);
733 dump_key_definition(char *name, keymap_t *keymap)
737 printf("static keymap_t keymap_%s = { 0x%02x, {\n",
738 name, (unsigned)keymap->n_keys);
741 " * scan cntrl alt alt cntrl\n"
742 " * code base shift cntrl shift alt shift cntrl shift spcl flgs\n"
743 " * ---------------------------------------------------------------------------\n"
745 for (i = 0; i < keymap->n_keys; i++) {
746 printf("/*%02x*/{{", i);
747 for (j = 0; j < NUM_STATES; j++) {
748 if (keymap->key[i].spcl & (0x80 >> j))
749 dump_entry(keymap->key[i].map[j] | SPECIAL);
751 dump_entry(keymap->key[i].map[j]);
753 printf("}, 0x%02X,0x%02X },\n",
754 (unsigned)keymap->key[i].spcl,
755 (unsigned)keymap->key[i].flgs);
761 dump_accent_definition(char *name, accentmap_t *accentmap)
766 printf("static accentmap_t accentmap_%s = { %d",
767 name, accentmap->n_accs);
768 if (accentmap->n_accs <= 0) {
773 for (i = 0; i < NUM_DEADKEYS; i++) {
774 printf(" /* %s=%d */\n {", acc_names[i], i);
775 c = accentmap->acc[i].accchar;
779 printf(" '\\\\', {");
780 else if (isascii(c) && isprint(c))
781 printf(" '%c', {", c);
783 printf(" 0x00 }, \n");
786 printf(" 0x%02x, {", c);
787 for (j = 0; j < NUM_ACCENTCHARS; j++) {
788 c = accentmap->acc[i].map[j][0];
791 if ((j > 0) && ((j % 4) == 0))
793 if (isascii(c) && isprint(c))
794 printf(" { '%c',", c);
796 printf(" { 0x%02x,", c);
797 printf("0x%02x },", accentmap->acc[i].map[j][1]);
805 add_keymap_path(const char *path)
811 if ((pe = malloc(sizeof(*pe))) == NULL ||
812 (pe->path = malloc(len + 2)) == NULL)
814 memcpy(pe->path, path, len);
815 if (len > 0 && path[len - 1] != '/')
816 pe->path[len++] = '/';
817 pe->path[len] = '\0';
818 STAILQ_INSERT_TAIL(&pathlist, pe, next);
822 load_keymap(char *opt, int dumponly)
825 accentmap_t accentmap;
830 char blank[] = "", keymap_path[] = KEYMAP_PATH;
831 char vt_keymap_path[] = VT_KEYMAP_PATH, dotkbd[] = ".kbd";
832 char *postfix[] = {blank, dotkbd, NULL};
834 if (!paths_configured) {
835 cp = getenv("KEYMAP_PATH");
840 add_keymap_path(vt_keymap_path);
842 add_keymap_path(keymap_path);
843 paths_configured = 1;
847 STAILQ_FOREACH(pe, &pathlist, next) {
848 for (j=0; postfix[j] && file == NULL; j++) {
849 name = mkfullname(pe->path, opt, postfix[j]);
850 file = fopen(name, "r");
856 warn("keymap file \"%s\" not found", opt);
859 memset(&keymap, 0, sizeof(keymap));
860 memset(&accentmap, 0, sizeof(accentmap));
863 if (get_definition_line(file, &keymap, &accentmap) < 0)
867 /* fix up the filename to make it a valid C identifier */
868 for (cp = opt; *cp; cp++)
869 if (!isalpha(*cp) && !isdigit(*cp)) *cp = '_';
871 " * Automatically generated from %s.\n"
874 dump_key_definition(opt, &keymap);
875 dump_accent_definition(opt, &accentmap);
878 if ((keymap.n_keys > 0) && (ioctl(0, PIO_KEYMAP, &keymap) < 0)) {
879 warn("setting keymap");
883 if ((accentmap.n_accs > 0)
884 && (ioctl(0, PIO_DEADKEYMAP, &accentmap) < 0)) {
885 warn("setting accentmap");
895 accentmap_t accentmap;
898 if (ioctl(0, GIO_KEYMAP, &keymap) < 0)
899 err(1, "getting keymap");
900 if (ioctl(0, GIO_DEADKEYMAP, &accentmap) < 0)
901 memset(&accentmap, 0, sizeof(accentmap));
904 "# scan cntrl alt alt cntrl lock\n"
905 "# code base shift cntrl shift alt shift cntrl shift state\n"
906 "# ------------------------------------------------------------------\n"
908 for (i=0; i<keymap.n_keys; i++)
909 print_key_definition_line(stdout, i, &keymap.key[i]);
912 for (i = 0; i < NUM_DEADKEYS; i++)
913 print_accent_definition_line(stdout, i, &accentmap.acc[i]);
918 load_default_functionkeys(void)
923 for (i=0; i<NUM_FKEYS; i++) {
925 strcpy(fkey.keydef, fkey_table[i]);
926 fkey.flen = strlen(fkey_table[i]);
927 if (ioctl(0, SETFKEY, &fkey) < 0)
928 warn("setting function key");
933 set_functionkey(char *keynumstr, char *string)
937 if (!strcmp(keynumstr, "load") && !strcmp(string, "default")) {
938 load_default_functionkeys();
941 fkey.keynum = atoi(keynumstr);
942 if (fkey.keynum < 1 || fkey.keynum > NUM_FKEYS) {
943 warnx("function key number must be between 1 and %d",
947 if ((fkey.flen = strlen(string)) > MAXFK) {
948 warnx("function key string too long (%d > %d)",
952 strncpy(fkey.keydef, string, MAXFK);
954 if (ioctl(0, SETFKEY, &fkey) < 0)
955 warn("setting function key");
959 set_bell_values(char *opt)
961 int bell, duration, pitch;
964 if (!strncmp(opt, "quiet.", 6)) {
965 bell = CONS_QUIET_BELL;
968 if (!strcmp(opt, "visual"))
969 bell |= CONS_VISUAL_BELL;
970 else if (!strcmp(opt, "normal"))
971 duration = 5, pitch = 800;
972 else if (!strcmp(opt, "off"))
973 duration = 0, pitch = 0;
978 duration = strtol(opt, &v1, 0);
979 if ((duration < 0) || (*v1 != '.'))
982 pitch = strtol(opt, &v1, 0);
983 if ((pitch < 0) || (*opt == '\0') || (*v1 != '\0')) {
985 warnx("argument to -b must be duration.pitch or [quiet.]visual|normal|off");
989 pitch = 1193182 / pitch; /* in Hz */
990 duration /= 10; /* in 10 m sec */
993 ioctl(0, CONS_BELLTYPE, &bell);
994 if (!(bell & CONS_VISUAL_BELL))
995 fprintf(stderr, "
\e[=%d;%dB", pitch, duration);
999 set_keyrates(char *opt)
1006 if (!strcmp(opt, "slow")) {
1007 delay = 1000, repeat = 500;
1009 } else if (!strcmp(opt, "normal")) {
1010 delay = 500, repeat = 125;
1012 } else if (!strcmp(opt, "fast")) {
1019 delay = strtol(opt, &v1, 0);
1020 if ((delay < 0) || (*v1 != '.'))
1023 repeat = strtol(opt, &v1, 0);
1024 if ((repeat < 0) || (*opt == '\0') || (*v1 != '\0')) {
1026 warnx("argument to -r must be delay.repeat or slow|normal|fast");
1029 for (n = 0; n < ndelays - 1; n++)
1030 if (delay <= delays[n])
1033 for (n = 0; n < nrepeats - 1; n++)
1034 if (repeat <= repeats[n])
1041 if (ioctl(0, KDSETREPEAT, arg)) {
1042 if (ioctl(0, KDSETRAD, (d << 5) | r))
1043 warn("setting keyboard rate");
1048 get_kbd_type_name(int type)
1055 { KB_101, "AT 101/102" },
1056 { KB_OTHER, "generic" },
1060 for (i = 0; i < sizeof(name_table)/sizeof(name_table[0]); ++i) {
1061 if (type == name_table[i].type)
1062 return name_table[i].name;
1070 keyboard_info_t info;
1072 if (ioctl(0, KDGKBINFO, &info) == -1) {
1073 warn("unable to obtain keyboard information");
1076 printf("kbd%d:\n", info.kb_index);
1077 printf(" %.*s%d, type:%s (%d)\n",
1078 (int)sizeof(info.kb_name), info.kb_name, info.kb_unit,
1079 get_kbd_type_name(info.kb_type), info.kb_type);
1083 set_keyboard(char *device)
1085 keyboard_info_t info;
1088 fd = open(device, O_RDONLY);
1090 warn("cannot open %s", device);
1093 if (ioctl(fd, KDGKBINFO, &info) == -1) {
1094 warn("unable to obtain keyboard information");
1099 * The keyboard device driver won't release the keyboard by
1100 * the following ioctl, but it automatically will, when the device
1101 * is closed. So, we don't check error here.
1103 ioctl(fd, CONS_RELKBD, 0);
1106 printf("kbd%d\n", info.kb_index);
1107 printf(" %.*s%d, type:%s (%d)\n",
1108 (int)sizeof(info.kb_name), info.kb_name, info.kb_unit,
1109 get_kbd_type_name(info.kb_type), info.kb_type);
1112 if (ioctl(0, CONS_SETKBD, info.kb_index) == -1)
1113 warn("unable to set keyboard");
1117 release_keyboard(void)
1119 keyboard_info_t info;
1122 * If stdin is not associated with a keyboard, the following ioctl
1125 if (ioctl(0, KDGKBINFO, &info) == -1) {
1126 warn("unable to obtain keyboard information");
1130 printf("kbd%d\n", info.kb_index);
1131 printf(" %.*s%d, type:%s (%d)\n",
1132 (int)sizeof(info.kb_name), info.kb_name, info.kb_unit,
1133 get_kbd_type_name(info.kb_type), info.kb_type);
1135 if (ioctl(0, CONS_RELKBD, 0) == -1)
1136 warn("unable to release the keyboard");
1140 mux_keyboard(u_int op, char *kbd)
1142 keyboard_info_t info;
1146 * If stdin is not associated with a keyboard, the following ioctl
1149 if (ioctl(0, KDGKBINFO, &info) == -1) {
1150 warn("unable to obtain keyboard information");
1154 printf("kbd%d\n", info.kb_index);
1155 printf(" %.*s%d, type:%s (%d)\n",
1156 (int)sizeof(info.kb_name), info.kb_name, info.kb_unit,
1157 get_kbd_type_name(info.kb_type), info.kb_type);
1160 * split kbd into name and unit. find the right most part of the
1161 * kbd string that consist of only digits.
1164 memset(&info, 0, sizeof(info));
1170 unit = strpbrk(ep + 1, "0123456789");
1172 info.kb_unit = strtol(unit, &ep, 10);
1176 } while (unit != NULL && info.kb_unit == -1);
1178 if (info.kb_unit == -1) {
1179 warnx("unable to find keyboard driver unit in '%s'", kbd);
1184 warnx("unable to find keyboard driver name in '%s'", kbd);
1187 if (unit - kbd >= (int) sizeof(info.kb_name)) {
1188 warnx("keyboard name '%s' is too long", kbd);
1192 strncpy(info.kb_name, kbd, unit - kbd);
1195 * If stdin is not associated with a kbdmux(4) keyboard, the following
1199 if (ioctl(0, op, &info) == -1)
1200 warn("unable to (un)mux the keyboard");
1206 fprintf(stderr, "%s\n%s\n%s\n",
1207 "usage: kbdcontrol [-dFKix] [-A name] [-a name] [-b duration.pitch | [quiet.]belltype]",
1208 " [-r delay.repeat | speed] [-l mapfile] [-f # string]",
1209 " [-k device] [-L mapfile] [-P path]");
1215 main(int argc, char **argv)
1217 const char *optstring = "A:a:b:df:iKk:Fl:L:P:r:x";
1220 /* Collect any -P arguments, regardless of where they appear. */
1221 while ((opt = getopt(argc, argv, optstring)) != -1)
1223 add_keymap_path(optarg);
1225 optind = optreset = 1;
1226 while ((opt = getopt(argc, argv, optstring)) != -1)
1230 mux_keyboard((opt == 'A')? KBRELKBD : KBADDKBD, optarg);
1233 set_bell_values(optarg);
1239 load_keymap(optarg, 0);
1242 load_keymap(optarg, 1);
1247 set_functionkey(optarg,
1248 nextarg(argc, argv, &optind, 'f'));
1251 load_default_functionkeys();
1260 set_keyboard(optarg);
1263 set_keyrates(optarg);
1271 if ((optind != argc) || (argc == 1))