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>
32 __FBSDID("$FreeBSD$");
42 #include <sys/consio.h>
43 #include <sys/param.h>
44 #include <sys/queue.h>
45 #include <sys/sysctl.h>
49 #define SPECIAL 0x80000000
51 static const char ctrl_names[32][4] = {
52 "nul", "soh", "stx", "etx", "eot", "enq", "ack", "bel",
53 "bs ", "ht ", "nl ", "vt ", "ff ", "cr ", "so ", "si ",
54 "dle", "dc1", "dc2", "dc3", "dc4", "nak", "syn", "etb",
55 "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 static const char fkey_table[96][MAXFK] = {
71 /* 01-04 */ "\033[M", "\033[N", "\033[O", "\033[P",
72 /* 05-08 */ "\033[Q", "\033[R", "\033[S", "\033[T",
73 /* 09-12 */ "\033[U", "\033[V", "\033[W", "\033[X",
74 /* 13-16 */ "\033[Y", "\033[Z", "\033[a", "\033[b",
75 /* 17-20 */ "\033[c", "\033[d", "\033[e", "\033[f",
76 /* 21-24 */ "\033[g", "\033[h", "\033[i", "\033[j",
77 /* 25-28 */ "\033[k", "\033[l", "\033[m", "\033[n",
78 /* 29-32 */ "\033[o", "\033[p", "\033[q", "\033[r",
79 /* 33-36 */ "\033[s", "\033[t", "\033[u", "\033[v",
80 /* 37-40 */ "\033[w", "\033[x", "\033[y", "\033[z",
81 /* 41-44 */ "\033[@", "\033[[", "\033[\\","\033[]",
82 /* 45-48 */ "\033[^", "\033[_", "\033[`", "\033[{",
83 /* 49-52 */ "\033[H", "\033[A", "\033[I", "-" ,
84 /* 53-56 */ "\033[D", "\033[E", "\033[C", "+" ,
85 /* 57-60 */ "\033[F", "\033[B", "\033[G", "\033[L",
86 /* 61-64 */ "\177", "\033[J", "\033[~", "\033[}",
87 /* 65-68 */ "" , "" , "" , "" ,
88 /* 69-72 */ "" , "" , "" , "" ,
89 /* 73-76 */ "" , "" , "" , "" ,
90 /* 77-80 */ "" , "" , "" , "" ,
91 /* 81-84 */ "" , "" , "" , "" ,
92 /* 85-88 */ "" , "" , "" , "" ,
93 /* 89-92 */ "" , "" , "" , "" ,
94 /* 93-96 */ "" , "" , "" , "" ,
97 static const int ndelays = nitems(kbdelays);
98 static const int nrepeats = nitems(kbrates);
100 static int paths_configured = 0;
106 static void add_keymap_path(const char *path);
107 static void dump_accent_definition(char *name, accentmap_t *accentmap);
108 static void dump_entry(int value);
109 static void dump_key_definition(char *name, keymap_t *keymap);
110 static int get_accent_definition_line(accentmap_t *);
111 static int get_entry(void);
112 static int get_key_definition_line(keymap_t *);
113 static void load_keymap(char *opt, int dumponly);
114 static void load_default_functionkeys(void);
115 static char * nextarg(int ac, char **av, int *indp, int oc);
116 static char * mkfullname(const char *s1, const char *s2, const char *s3);
117 static void print_accent_definition_line(FILE *fp, int accent,
119 static void print_entry(FILE *fp, int value);
120 static void print_key_definition_line(FILE *fp, int scancode,
121 struct keyent_t *key);
122 static void print_keymap(void);
123 static void release_keyboard(void);
124 static void mux_keyboard(u_int op, char *kbd);
125 static void set_bell_values(char *opt);
126 static void set_functionkey(char *keynumstr, char *string);
127 static void set_keyboard(char *device);
128 static void set_keyrates(char *opt);
129 static void show_kbd_info(void);
130 static void usage(void) __dead2;
133 STAILQ_ENTRY(pathent) next;
136 static STAILQ_HEAD(, pathent) pathlist = STAILQ_HEAD_INITIALIZER(pathlist);
138 /* Detect presence of vt(4). */
142 char vty_name[4] = "";
143 size_t len = sizeof(vty_name);
145 if (sysctlbyname("kern.vty", vty_name, &len, NULL, 0) != 0)
147 return (strcmp(vty_name, "vt") == 0);
151 nextarg(int ac, char **av, int *indp, int oc)
154 return(av[(*indp)++]);
155 warnx("option requires two arguments -- %c", oc);
161 mkfullname(const char *s1, const char *s2, const char *s3)
163 static char *buf = NULL;
167 f = strlen(s1) + strlen(s2) + strlen(s3) + 1;
170 buf = (char *)realloc(buf, f);
172 buf = (char *)malloc(f);
190 switch ((token = yylex())) {
192 return NOP | SPECIAL;
194 return LSH | SPECIAL;
196 return RSH | SPECIAL;
198 return CLK | SPECIAL;
200 return NLK | SPECIAL;
202 return SLK | SPECIAL;
204 return BTAB | SPECIAL;
206 return LALT | SPECIAL;
208 return LCTR | SPECIAL;
210 return NEXT | SPECIAL;
212 return PREV | SPECIAL;
214 return RCTR | SPECIAL;
216 return RALT | SPECIAL;
218 return ALK | SPECIAL;
220 return ASH | SPECIAL;
222 return META | SPECIAL;
224 return RBT | SPECIAL;
226 return DBG | SPECIAL;
228 return SUSP | SPECIAL;
230 return SPSC | SPECIAL;
232 return PNC | SPECIAL;
234 return LSHA | SPECIAL;
236 return RSHA | SPECIAL;
238 return LCTRA | SPECIAL;
240 return RCTRA | SPECIAL;
242 return LALTA | SPECIAL;
244 return RALTA | SPECIAL;
246 return HALT | SPECIAL;
248 return PDWN | SPECIAL;
250 return PASTE | SPECIAL;
252 if (ACC(number) > L_ACC)
254 return ACC(number) | SPECIAL;
256 if (F(number) > L_FN)
258 return F(number) | SPECIAL;
260 if (S(number) > L_SCR)
262 return S(number) | SPECIAL;
264 return (unsigned char)letter;
266 if (number < 0x000000 || number > 0x10FFFF)
275 get_definition_line(FILE *file, keymap_t *keymap, accentmap_t *accentmap)
285 c = get_key_definition_line(keymap);
287 errx(1, "invalid key definition");
288 if (c > keymap->n_keys)
292 c = get_accent_definition_line(accentmap);
294 errx(1, "invalid accent key definition");
295 if (c > accentmap->n_accs)
296 accentmap->n_accs = c;
302 errx(1, "illegal definition line");
308 get_key_definition_line(keymap_t *map)
310 int i, def, scancode;
312 /* check scancode number */
313 if (number < 0 || number >= NUM_KEYS)
317 /* get key definitions */
318 map->key[scancode].spcl = 0;
319 for (i=0; i<NUM_STATES; i++) {
320 if ((def = get_entry()) == -1)
323 map->key[scancode].spcl |= (0x80 >> i);
324 map->key[scancode].map[i] = def & ~SPECIAL;
326 /* get lock state key def */
327 if ((token = yylex()) != TFLAG)
329 map->key[scancode].flgs = number;
331 return (scancode + 1);
335 get_accent_definition_line(accentmap_t *map)
341 if (ACC(number) < F_ACC || ACC(number) > L_ACC)
342 /* number out of range */
345 if (map->acc[accent].accchar != 0) {
346 /* this entry has already been defined before! */
347 errx(1, "duplicated accent key definition");
350 switch ((token = yylex())) {
352 map->acc[accent].accchar = letter;
355 map->acc[accent].accchar = number;
361 for (i = 0; (token = yylex()) == '(';) {
362 switch ((token = yylex())) {
372 switch ((token = yylex())) {
382 if ((token = yylex()) != ')')
384 if (i >= NUM_ACCENTCHARS) {
385 warnx("too many accented characters, ignored");
388 map->acc[accent].map[i][0] = c1;
389 map->acc[accent].map[i][1] = c2;
396 print_entry(FILE *fp, int value)
398 int val = value & ~SPECIAL;
402 fprintf(fp, " nop ");
405 fprintf(fp, " lshift");
408 fprintf(fp, " rshift");
411 fprintf(fp, " clock ");
414 fprintf(fp, " nlock ");
417 fprintf(fp, " slock ");
420 fprintf(fp, " btab ");
423 fprintf(fp, " lalt ");
426 fprintf(fp, " lctrl ");
429 fprintf(fp, " nscr ");
432 fprintf(fp, " pscr ");
435 fprintf(fp, " rctrl ");
438 fprintf(fp, " ralt ");
441 fprintf(fp, " alock ");
444 fprintf(fp, " ashift");
447 fprintf(fp, " meta ");
450 fprintf(fp, " boot ");
453 fprintf(fp, " debug ");
456 fprintf(fp, " susp ");
459 fprintf(fp, " saver ");
462 fprintf(fp, " panic ");
465 fprintf(fp, " lshifta");
468 fprintf(fp, " rshifta");
470 case LCTRA | SPECIAL:
471 fprintf(fp, " lctrla");
473 case RCTRA | SPECIAL:
474 fprintf(fp, " rctrla");
476 case LALTA | SPECIAL:
477 fprintf(fp, " lalta ");
479 case RALTA | SPECIAL:
480 fprintf(fp, " ralta ");
483 fprintf(fp, " halt ");
486 fprintf(fp, " pdwn ");
488 case PASTE | SPECIAL:
489 fprintf(fp, " paste ");
492 if (value & SPECIAL) {
493 if (val >= F_FN && val <= L_FN)
494 fprintf(fp, " fkey%02d", val - F_FN + 1);
495 else if (val >= F_SCR && val <= L_SCR)
496 fprintf(fp, " scr%02d ", val - F_SCR + 1);
497 else if (val >= F_ACC && val <= L_ACC)
498 fprintf(fp, " %-6s", acc_names[val - F_ACC]);
500 fprintf(fp, " 0x%02x ", val);
502 fprintf(fp, " %3d ", val);
506 fprintf(fp, " %s ", ctrl_names[val]);
508 fprintf(fp, " del ");
509 else if (isascii(val) && isprint(val))
510 fprintf(fp, " '%c' ", val);
512 fprintf(fp, " 0x%02x ", val);
514 fprintf(fp, " %3d ", val);
520 print_key_definition_line(FILE *fp, int scancode, struct keyent_t *key)
524 /* print scancode number */
526 fprintf(fp, " 0x%02x ", scancode);
528 fprintf(fp, " %03d ", scancode);
530 /* print key definitions */
531 for (i=0; i<NUM_STATES; i++) {
532 if (key->spcl & (0x80 >> i))
533 print_entry(fp, key->map[i] | SPECIAL);
535 print_entry(fp, key->map[i]);
538 /* print lock state key def */
556 print_accent_definition_line(FILE *fp, int accent, struct acc_t *key)
561 if (key->accchar == 0)
564 /* print accent number */
565 fprintf(fp, " %-6s", acc_names[accent]);
566 if (isascii(key->accchar) && isprint(key->accchar))
567 fprintf(fp, "'%c' ", key->accchar);
569 fprintf(fp, "0x%02x ", key->accchar);
571 fprintf(fp, "%03d ", key->accchar);
573 for (i = 0; i < NUM_ACCENTCHARS; ++i) {
577 if ((i > 0) && ((i % 4) == 0))
579 if (isascii(c) && isprint(c))
580 fprintf(fp, "( '%c' ", c);
582 fprintf(fp, "(0x%02x ", c);
584 fprintf(fp, "( %03d ", c);
586 if (isascii(c) && isprint(c))
587 fprintf(fp, "'%c' ) ", c);
589 fprintf(fp, "0x%02x) ", c);
591 fprintf(fp, "%03d ) ", c);
597 dump_entry(int value)
599 if (value & SPECIAL) {
693 if (value >= F_FN && value <= L_FN)
694 printf(" F(%2d),", value - F_FN + 1);
695 else if (value >= F_SCR && value <= L_SCR)
696 printf(" S(%2d),", value - F_SCR + 1);
697 else if (value >= F_ACC && value <= L_ACC)
698 printf(" %-4s, ", acc_names_u[value - F_ACC]);
700 printf(" 0x%02X, ", value);
703 } else if (value == '\'') {
705 } else if (value == '\\') {
707 } else if (isascii(value) && isprint(value)) {
708 printf(" '%c', ", value);
710 printf(" 0x%02X, ", value);
715 dump_key_definition(char *name, keymap_t *keymap)
719 printf("static keymap_t keymap_%s = { 0x%02x, {\n",
720 name, (unsigned)keymap->n_keys);
723 " * scan cntrl alt alt cntrl\n"
724 " * code base shift cntrl shift alt shift cntrl shift spcl flgs\n"
725 " * ---------------------------------------------------------------------------\n"
727 for (i = 0; i < keymap->n_keys; i++) {
728 printf("/*%02x*/{{", i);
729 for (j = 0; j < NUM_STATES; j++) {
730 if (keymap->key[i].spcl & (0x80 >> j))
731 dump_entry(keymap->key[i].map[j] | SPECIAL);
733 dump_entry(keymap->key[i].map[j]);
735 printf("}, 0x%02X,0x%02X },\n",
736 (unsigned)keymap->key[i].spcl,
737 (unsigned)keymap->key[i].flgs);
743 dump_accent_definition(char *name, accentmap_t *accentmap)
748 printf("static accentmap_t accentmap_%s = { %d",
749 name, accentmap->n_accs);
750 if (accentmap->n_accs <= 0) {
755 for (i = 0; i < NUM_DEADKEYS; i++) {
756 printf(" /* %s=%d */\n {", acc_names[i], i);
757 c = accentmap->acc[i].accchar;
761 printf(" '\\\\', {");
762 else if (isascii(c) && isprint(c))
763 printf(" '%c', {", c);
765 printf(" 0x00 }, \n");
768 printf(" 0x%02x, {", c);
769 for (j = 0; j < NUM_ACCENTCHARS; j++) {
770 c = accentmap->acc[i].map[j][0];
773 if ((j > 0) && ((j % 4) == 0))
775 if (isascii(c) && isprint(c))
776 printf(" { '%c',", c);
778 printf(" { 0x%02x,", c);
779 printf("0x%02x },", accentmap->acc[i].map[j][1]);
787 add_keymap_path(const char *path)
793 if ((pe = malloc(sizeof(*pe))) == NULL ||
794 (pe->path = malloc(len + 2)) == NULL)
796 memcpy(pe->path, path, len);
797 if (len > 0 && path[len - 1] != '/')
798 pe->path[len++] = '/';
799 pe->path[len] = '\0';
800 STAILQ_INSERT_TAIL(&pathlist, pe, next);
803 #ifdef OPIO_DEADKEYMAP
805 to_old_accentmap(accentmap_t *from, oaccentmap_t *to)
809 to->n_accs = from->n_accs;
810 for (i = 0; i < NUM_DEADKEYS; i++) {
811 for (j = 0; j < NUM_ACCENTCHARS; j++) {
812 to->acc[i].map[j][0] = from->acc[i].map[j][0];
813 to->acc[i].map[j][1] = from->acc[i].map[j][1];
814 to->acc[i].accchar = from->acc[i].accchar;
818 #endif /* OPIO_DEADKEYMAP */
821 load_keymap(char *opt, int dumponly)
824 accentmap_t accentmap;
825 #ifdef OPIO_DEADKEYMAP
826 oaccentmap_t oaccentmap;
827 #endif /* OPIO_DEADKEYMAP */
832 char blank[] = "", keymap_path[] = KEYMAP_PATH;
833 char vt_keymap_path[] = VT_KEYMAP_PATH, dotkbd[] = ".kbd";
834 char *postfix[] = {blank, dotkbd, NULL};
836 if (!paths_configured) {
837 cp = getenv("KEYMAP_PATH");
842 add_keymap_path(vt_keymap_path);
844 add_keymap_path(keymap_path);
845 paths_configured = 1;
849 STAILQ_FOREACH(pe, &pathlist, next) {
850 for (j=0; postfix[j] && file == NULL; j++) {
851 name = mkfullname(pe->path, opt, postfix[j]);
852 file = fopen(name, "r");
858 warn("keymap file \"%s\" not found", opt);
861 memset(&keymap, 0, sizeof(keymap));
862 memset(&accentmap, 0, sizeof(accentmap));
865 if (get_definition_line(file, &keymap, &accentmap) < 0)
869 /* fix up the filename to make it a valid C identifier */
870 for (cp = opt; *cp; cp++)
871 if (!isalpha(*cp) && !isdigit(*cp)) *cp = '_';
873 " * Automatically generated from %s.\n"
876 dump_key_definition(opt, &keymap);
877 dump_accent_definition(opt, &accentmap);
880 if ((keymap.n_keys > 0) && (ioctl(0, PIO_KEYMAP, &keymap) < 0)) {
881 warn("setting keymap");
885 if ((accentmap.n_accs > 0)
886 && (ioctl(0, PIO_DEADKEYMAP, &accentmap) < 0)) {
887 #ifdef OPIO_DEADKEYMAP
888 to_old_accentmap(&accentmap, &oaccentmap);
889 if (ioctl(0, OPIO_DEADKEYMAP, &oaccentmap) < 0)
890 #endif /* OGIO_DEADKEYMAP */
892 warn("setting accentmap");
899 #ifdef OPIO_DEADKEYMAP
901 to_new_accentmap(oaccentmap_t *from, accentmap_t *to)
905 to->n_accs = from->n_accs;
906 for (i = 0; i < NUM_DEADKEYS; i++) {
907 for (j = 0; j < NUM_ACCENTCHARS; j++) {
908 to->acc[i].map[j][0] = from->acc[i].map[j][0];
909 to->acc[i].map[j][1] = from->acc[i].map[j][1];
910 to->acc[i].accchar = from->acc[i].accchar;
914 #endif /* OPIO_DEADKEYMAP */
920 accentmap_t accentmap;
921 #ifdef OGIO_DEADKEYMAP
922 oaccentmap_t oaccentmap;
923 #endif /* OPIO_DEADKEYMAP */
926 if (ioctl(0, GIO_KEYMAP, &keymap) < 0)
927 err(1, "getting keymap");
928 if (ioctl(0, GIO_DEADKEYMAP, &accentmap) < 0) {
929 #ifdef OGIO_DEADKEYMAP
930 if (ioctl(0, OGIO_DEADKEYMAP, &oaccentmap) == 0)
931 to_new_accentmap(&oaccentmap, &accentmap);
933 #endif /* OGIO_DEADKEYMAP */
934 memset(&accentmap, 0, sizeof(accentmap));
938 "# scan cntrl alt alt cntrl lock\n"
939 "# code base shift cntrl shift alt shift cntrl shift state\n"
940 "# ------------------------------------------------------------------\n"
942 for (i=0; i<keymap.n_keys; i++)
943 print_key_definition_line(stdout, i, &keymap.key[i]);
946 for (i = 0; i < NUM_DEADKEYS; i++)
947 print_accent_definition_line(stdout, i, &accentmap.acc[i]);
952 load_default_functionkeys(void)
957 for (i=0; i<NUM_FKEYS; i++) {
959 strcpy(fkey.keydef, fkey_table[i]);
960 fkey.flen = strlen(fkey_table[i]);
961 if (ioctl(0, SETFKEY, &fkey) < 0)
962 warn("setting function key");
967 set_functionkey(char *keynumstr, char *string)
971 if (!strcmp(keynumstr, "load") && !strcmp(string, "default")) {
972 load_default_functionkeys();
975 fkey.keynum = atoi(keynumstr);
976 if (fkey.keynum < 1 || fkey.keynum > NUM_FKEYS) {
977 warnx("function key number must be between 1 and %d",
981 if ((fkey.flen = strlen(string)) > MAXFK) {
982 warnx("function key string too long (%d > %d)",
986 strncpy(fkey.keydef, string, MAXFK);
988 if (ioctl(0, SETFKEY, &fkey) < 0)
989 warn("setting function key");
993 set_bell_values(char *opt)
995 int bell, duration, pitch;
1000 if (!strncmp(opt, "quiet.", 6)) {
1001 bell = CONS_QUIET_BELL;
1004 if (!strcmp(opt, "visual"))
1005 bell |= CONS_VISUAL_BELL;
1006 else if (!strcmp(opt, "normal"))
1007 duration = 5, pitch = 800;
1008 else if (!strcmp(opt, "off"))
1009 duration = 0, pitch = 0;
1014 duration = strtol(opt, &v1, 0);
1015 if ((duration < 0) || (*v1 != '.'))
1018 pitch = strtol(opt, &v1, 0);
1019 if ((pitch < 0) || (*opt == '\0') || (*v1 != '\0')) {
1021 warnx("argument to -b must be duration.pitch or [quiet.]visual|normal|off");
1025 pitch = 1193182 / pitch; /* in Hz */
1026 duration /= 10; /* in 10 m sec */
1029 ioctl(0, CONS_BELLTYPE, &bell);
1030 if (duration > 0 && pitch > 0)
1031 fprintf(stderr, "\e[=%d;%dB", pitch, duration);
1035 set_keyrates(char *opt)
1042 if (!strcmp(opt, "slow")) {
1047 } else if (!strcmp(opt, "normal")) {
1052 } else if (!strcmp(opt, "fast")) {
1061 delay = strtol(opt, &v1, 0);
1062 if ((delay < 0) || (*v1 != '.'))
1065 repeat = strtol(opt, &v1, 0);
1066 if ((repeat < 0) || (*opt == '\0') || (*v1 != '\0')) {
1068 warnx("argument to -r must be delay.repeat or slow|normal|fast");
1071 for (n = 0; n < ndelays - 1; n++)
1072 if (delay <= kbdelays[n])
1075 for (n = 0; n < nrepeats - 1; n++)
1076 if (repeat <= kbrates[n])
1083 if (ioctl(0, KDSETREPEAT, arg)) {
1084 warn("fallback, setting keyboard rate via legacy interface (KDSETRAD), will be removed soon");
1085 if (ioctl(0, KDSETRAD, (d << 5) | r))
1086 warn("setting keyboard rate");
1091 get_kbd_type_name(int type)
1098 { KB_101, "AT 101/102" },
1099 { KB_OTHER, "generic" },
1103 for (i = 0; i < sizeof(name_table)/sizeof(name_table[0]); ++i) {
1104 if (type == name_table[i].type)
1105 return name_table[i].name;
1113 keyboard_info_t info;
1115 if (ioctl(0, KDGKBINFO, &info) == -1) {
1116 warn("unable to obtain keyboard information");
1119 printf("kbd%d:\n", info.kb_index);
1120 printf(" %.*s%d, type:%s (%d)\n",
1121 (int)sizeof(info.kb_name), info.kb_name, info.kb_unit,
1122 get_kbd_type_name(info.kb_type), info.kb_type);
1126 set_keyboard(char *device)
1128 keyboard_info_t info;
1131 fd = open(device, O_RDONLY);
1133 warn("cannot open %s", device);
1136 if (ioctl(fd, KDGKBINFO, &info) == -1) {
1137 warn("unable to obtain keyboard information");
1142 * The keyboard device driver won't release the keyboard by
1143 * the following ioctl, but it automatically will, when the device
1144 * is closed. So, we don't check error here.
1146 ioctl(fd, CONS_RELKBD, 0);
1149 printf("kbd%d\n", info.kb_index);
1150 printf(" %.*s%d, type:%s (%d)\n",
1151 (int)sizeof(info.kb_name), info.kb_name, info.kb_unit,
1152 get_kbd_type_name(info.kb_type), info.kb_type);
1155 if (ioctl(0, CONS_SETKBD, info.kb_index) == -1)
1156 warn("unable to set keyboard");
1160 release_keyboard(void)
1162 keyboard_info_t info;
1165 * If stdin is not associated with a keyboard, the following ioctl
1168 if (ioctl(0, KDGKBINFO, &info) == -1) {
1169 warn("unable to obtain keyboard information");
1173 printf("kbd%d\n", info.kb_index);
1174 printf(" %.*s%d, type:%s (%d)\n",
1175 (int)sizeof(info.kb_name), info.kb_name, info.kb_unit,
1176 get_kbd_type_name(info.kb_type), info.kb_type);
1178 if (ioctl(0, CONS_RELKBD, 0) == -1)
1179 warn("unable to release the keyboard");
1183 mux_keyboard(u_int op, char *kbd)
1185 keyboard_info_t info;
1189 * If stdin is not associated with a keyboard, the following ioctl
1192 if (ioctl(0, KDGKBINFO, &info) == -1) {
1193 warn("unable to obtain keyboard information");
1197 printf("kbd%d\n", info.kb_index);
1198 printf(" %.*s%d, type:%s (%d)\n",
1199 (int)sizeof(info.kb_name), info.kb_name, info.kb_unit,
1200 get_kbd_type_name(info.kb_type), info.kb_type);
1203 * split kbd into name and unit. find the right most part of the
1204 * kbd string that consist of only digits.
1207 memset(&info, 0, sizeof(info));
1213 unit = strpbrk(ep + 1, "0123456789");
1215 info.kb_unit = strtol(unit, &ep, 10);
1219 } while (unit != NULL && info.kb_unit == -1);
1221 if (info.kb_unit == -1) {
1222 warnx("unable to find keyboard driver unit in '%s'", kbd);
1227 warnx("unable to find keyboard driver name in '%s'", kbd);
1230 if (unit - kbd >= (int) sizeof(info.kb_name)) {
1231 warnx("keyboard name '%s' is too long", kbd);
1235 strncpy(info.kb_name, kbd, unit - kbd);
1238 * If stdin is not associated with a kbdmux(4) keyboard, the following
1242 if (ioctl(0, op, &info) == -1)
1243 warn("unable to (un)mux the keyboard");
1249 fprintf(stderr, "%s\n%s\n%s\n",
1250 "usage: kbdcontrol [-dFKix] [-A name] [-a name] [-b duration.pitch | [quiet.]belltype]",
1251 " [-r delay.repeat | speed] [-l mapfile] [-f # string]",
1252 " [-k device] [-L mapfile] [-P path]");
1258 main(int argc, char **argv)
1260 const char *optstring = "A:a:b:df:iKk:Fl:L:P:r:x";
1263 /* Collect any -P arguments, regardless of where they appear. */
1264 while ((opt = getopt(argc, argv, optstring)) != -1) {
1266 add_keymap_path(optarg);
1271 optind = optreset = 1;
1272 while ((opt = getopt(argc, argv, optstring)) != -1)
1276 mux_keyboard((opt == 'A')? KBRELKBD : KBADDKBD, optarg);
1279 set_bell_values(optarg);
1285 load_keymap(optarg, 0);
1288 load_keymap(optarg, 1);
1293 set_functionkey(optarg,
1294 nextarg(argc, argv, &optind, 'f'));
1297 load_default_functionkeys();
1306 set_keyboard(optarg);
1309 set_keyrates(optarg);
1317 if ((optind != argc) || (argc == 1))