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.
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
40 #include <sys/consio.h>
45 * HALT, PDWN, and PASTE aren't defined in 4.x, but we need them to bridge
46 * to 5.0-current so define them here as a stop gap transition measure.
49 #define HALT 0xa1 /* halt machine */
52 #define PDWN 0xa2 /* halt machine and power down */
55 #define PASTE 0xa3 /* paste from cut-paste buffer */
58 char ctrl_names[32][4] = {
59 "nul", "soh", "stx", "etx", "eot", "enq", "ack", "bel",
60 "bs ", "ht ", "nl ", "vt ", "ff ", "cr ", "so ", "si ",
61 "dle", "dc1", "dc2", "dc3", "dc4", "nak", "syn", "etb",
62 "can", "em ", "sub", "esc", "fs ", "gs ", "rs ", "us "
65 char acc_names[15][5] = {
66 "dgra", "dacu", "dcir", "dtil", "dmac", "dbre", "ddot",
67 "duml", "dsla", "drin", "dced", "dapo", "ddac", "dogo",
71 char acc_names_u[15][5] = {
72 "DGRA", "DACU", "DCIR", "DTIL", "DMAC", "DBRE", "DDOT",
73 "DUML", "DSLA", "DRIN", "DCED", "DAPO", "DDAC", "DOGO",
77 char fkey_table[96][MAXFK] = {
78 /* 01-04 */ "\033[M", "\033[N", "\033[O", "\033[P",
79 /* 05-08 */ "\033[Q", "\033[R", "\033[S", "\033[T",
80 /* 09-12 */ "\033[U", "\033[V", "\033[W", "\033[X",
81 /* 13-16 */ "\033[Y", "\033[Z", "\033[a", "\033[b",
82 /* 17-20 */ "\033[c", "\033[d", "\033[e", "\033[f",
83 /* 21-24 */ "\033[g", "\033[h", "\033[i", "\033[j",
84 /* 25-28 */ "\033[k", "\033[l", "\033[m", "\033[n",
85 /* 29-32 */ "\033[o", "\033[p", "\033[q", "\033[r",
86 /* 33-36 */ "\033[s", "\033[t", "\033[u", "\033[v",
87 /* 37-40 */ "\033[w", "\033[x", "\033[y", "\033[z",
88 /* 41-44 */ "\033[@", "\033[[", "\033[\\","\033[]",
89 /* 45-48 */ "\033[^", "\033[_", "\033[`", "\033[{",
90 /* 49-52 */ "\033[H", "\033[A", "\033[I", "-" ,
91 /* 53-56 */ "\033[D", "\033[E", "\033[C", "+" ,
92 /* 57-60 */ "\033[F", "\033[B", "\033[G", "\033[L",
93 /* 61-64 */ "\177", "\033[J", "\033[~", "\033[}",
94 /* 65-68 */ "" , "" , "" , "" ,
95 /* 69-72 */ "" , "" , "" , "" ,
96 /* 73-76 */ "" , "" , "" , "" ,
97 /* 77-80 */ "" , "" , "" , "" ,
98 /* 81-84 */ "" , "" , "" , "" ,
99 /* 85-88 */ "" , "" , "" , "" ,
100 /* 89-92 */ "" , "" , "" , "" ,
101 /* 93-96 */ "" , "" , "" , "" ,
104 const int delays[] = {250, 500, 750, 1000};
105 const int repeats[] = { 34, 38, 42, 46, 50, 55, 59, 63,
106 68, 76, 84, 92, 100, 110, 118, 126,
107 136, 152, 168, 184, 200, 220, 236, 252,
108 272, 304, 336, 368, 400, 440, 472, 504};
109 const int ndelays = (sizeof(delays) / sizeof(int));
110 const int nrepeats = (sizeof(repeats) / sizeof(int));
116 void dump_accent_definition(char *name, accentmap_t *accentmap);
117 void dump_entry(int value);
118 void dump_key_definition(char *name, keymap_t *keymap);
119 int get_accent_definition_line(accentmap_t *);
121 int get_key_definition_line(keymap_t *);
122 void load_keymap(char *opt, int dumponly);
123 void load_default_functionkeys(void);
124 char * nextarg(int ac, char **av, int *indp, int oc);
125 char * mkfullname(const char *s1, const char *s2, const char *s3);
126 void print_accent_definition_line(FILE *fp, int accent,
128 void print_entry(FILE *fp, int value);
129 void print_key_definition_line(FILE *fp, int scancode,
130 struct keyent_t *key);
131 void print_keymap(void);
132 void release_keyboard(void);
133 void set_bell_values(char *opt);
134 void set_functionkey(char *keynumstr, char *string);
135 void set_keyboard(char *device);
136 void set_keyrates(char *opt);
137 void show_kbd_info(void);
138 void usage(void) __dead2;
141 nextarg(int ac, char **av, int *indp, int oc)
144 return(av[(*indp)++]);
145 warnx("option requires two arguments -- %c", oc);
151 mkfullname(const char *s1, const char *s2, const char *s3)
153 static char *buf = NULL;
157 f = strlen(s1) + strlen(s2) + strlen(s3) + 1;
160 buf = (char *)realloc(buf, f);
162 buf = (char *)malloc(f);
180 switch ((token = yylex())) {
228 return LCTRA | 0x100;
230 return RCTRA | 0x100;
232 return LALTA | 0x100;
234 return RALTA | 0x100;
240 return PASTE | 0x100;
242 if (ACC(number) > L_ACC)
244 return ACC(number) | 0x100;
246 if (F(number) > L_FN)
248 return F(number) | 0x100;
250 if (S(number) > L_SCR)
252 return S(number) | 0x100;
254 return (unsigned char)letter;
256 if (number < 0 || number > 255)
265 get_definition_line(FILE *fd, keymap_t *keymap, accentmap_t *accentmap)
275 c = get_key_definition_line(keymap);
277 errx(1, "invalid key definition");
278 if (c > keymap->n_keys)
282 c = get_accent_definition_line(accentmap);
284 errx(1, "invalid accent key definition");
285 if (c > accentmap->n_accs)
286 accentmap->n_accs = c;
292 errx(1, "illegal definition line");
298 get_key_definition_line(keymap_t *map)
300 int i, def, scancode;
302 /* check scancode number */
303 if (number < 0 || number >= NUM_KEYS)
307 /* get key definitions */
308 map->key[scancode].spcl = 0;
309 for (i=0; i<NUM_STATES; i++) {
310 if ((def = get_entry()) == -1)
313 map->key[scancode].spcl |= (0x80 >> i);
314 map->key[scancode].map[i] = def & 0xFF;
316 /* get lock state key def */
317 if ((token = yylex()) != TFLAG)
319 map->key[scancode].flgs = number;
321 return (scancode + 1);
325 get_accent_definition_line(accentmap_t *map)
331 if (ACC(number) < F_ACC || ACC(number) > L_ACC)
332 /* number out of range */
335 if (map->acc[accent].accchar != 0) {
336 /* this entry has already been defined before! */
337 errx(1, "duplicated accent key definition");
340 switch ((token = yylex())) {
342 map->acc[accent].accchar = letter;
345 map->acc[accent].accchar = number;
351 for (i = 0; (token = yylex()) == '(';) {
352 switch ((token = yylex())) {
362 switch ((token = yylex())) {
372 if ((token = yylex()) != ')')
374 if (i >= NUM_ACCENTCHARS) {
375 warnx("too many accented characters, ignored");
378 map->acc[accent].map[i][0] = c1;
379 map->acc[accent].map[i][1] = c2;
386 print_entry(FILE *fp, int value)
388 int val = value & 0xFF;
392 fprintf(fp, " nop ");
395 fprintf(fp, " lshift");
398 fprintf(fp, " rshift");
401 fprintf(fp, " clock ");
404 fprintf(fp, " nlock ");
407 fprintf(fp, " slock ");
410 fprintf(fp, " btab ");
413 fprintf(fp, " lalt ");
416 fprintf(fp, " lctrl ");
419 fprintf(fp, " nscr ");
422 fprintf(fp, " pscr ");
425 fprintf(fp, " rctrl ");
428 fprintf(fp, " ralt ");
431 fprintf(fp, " alock ");
434 fprintf(fp, " ashift");
437 fprintf(fp, " meta ");
440 fprintf(fp, " boot ");
443 fprintf(fp, " debug ");
446 fprintf(fp, " susp ");
449 fprintf(fp, " saver ");
452 fprintf(fp, " panic ");
455 fprintf(fp, " lshifta");
458 fprintf(fp, " rshifta");
461 fprintf(fp, " lctrla");
464 fprintf(fp, " rctrla");
467 fprintf(fp, " lalta ");
470 fprintf(fp, " ralta ");
473 fprintf(fp, " halt ");
476 fprintf(fp, " pdwn ");
479 fprintf(fp, " paste ");
483 if (val >= F_FN && val <= L_FN)
484 fprintf(fp, " fkey%02d", val - F_FN + 1);
485 else if (val >= F_SCR && val <= L_SCR)
486 fprintf(fp, " scr%02d ", val - F_SCR + 1);
487 else if (val >= F_ACC && val <= L_ACC)
488 fprintf(fp, " %-6s", acc_names[val - F_ACC]);
490 fprintf(fp, " 0x%02x ", val);
492 fprintf(fp, " %3d ", val);
496 fprintf(fp, " %s ", ctrl_names[val]);
498 fprintf(fp, " del ");
499 else if (isascii(val) && isprint(val))
500 fprintf(fp, " '%c' ", val);
502 fprintf(fp, " 0x%02x ", val);
504 fprintf(fp, " %3d ", val);
510 print_key_definition_line(FILE *fp, int scancode, struct keyent_t *key)
514 /* print scancode number */
516 fprintf(fp, " 0x%02x ", scancode);
518 fprintf(fp, " %03d ", scancode);
520 /* print key definitions */
521 for (i=0; i<NUM_STATES; i++) {
522 if (key->spcl & (0x80 >> i))
523 print_entry(fp, key->map[i] | 0x100);
525 print_entry(fp, key->map[i]);
528 /* print lock state key def */
546 print_accent_definition_line(FILE *fp, int accent, struct acc_t *key)
551 if (key->accchar == 0)
554 /* print accent number */
555 fprintf(fp, " %-6s", acc_names[accent]);
556 if (isascii(key->accchar) && isprint(key->accchar))
557 fprintf(fp, "'%c' ", key->accchar);
559 fprintf(fp, "0x%02x ", key->accchar);
561 fprintf(fp, "%03d ", key->accchar);
563 for (i = 0; i < NUM_ACCENTCHARS; ++i) {
567 if ((i > 0) && ((i % 4) == 0))
569 if (isascii(c) && isprint(c))
570 fprintf(fp, "( '%c' ", c);
572 fprintf(fp, "(0x%02x ", c);
574 fprintf(fp, "( %03d ", c);
576 if (isascii(c) && isprint(c))
577 fprintf(fp, "'%c' ) ", c);
579 fprintf(fp, "0x%02x) ", c);
581 fprintf(fp, "%03d ) ", c);
587 dump_entry(int value)
683 if (value >= F_FN && value <= L_FN)
684 printf(" F(%2d),", value - F_FN + 1);
685 else if (value >= F_SCR && value <= L_SCR)
686 printf(" S(%2d),", value - F_SCR + 1);
687 else if (value >= F_ACC && value <= L_ACC)
688 printf(" %-4s, ", acc_names_u[value - F_ACC]);
690 printf(" 0x%02X, ", value);
693 } else if (value == '\'') {
695 } else if (value == '\\') {
697 } else if (isascii(value) && isprint(value)) {
698 printf(" '%c', ", value);
700 printf(" 0x%02X, ", value);
705 dump_key_definition(char *name, keymap_t *keymap)
709 printf("static keymap_t keymap_%s = { 0x%02x, {\n",
710 name, (unsigned)keymap->n_keys);
713 " * scan cntrl alt alt cntrl\n"
714 " * code base shift cntrl shift alt shift cntrl shift spcl flgs\n"
715 " * ---------------------------------------------------------------------------\n"
717 for (i = 0; i < keymap->n_keys; i++) {
718 printf("/*%02x*/{{", i);
719 for (j = 0; j < NUM_STATES; j++) {
720 if (keymap->key[i].spcl & (0x80 >> j))
721 dump_entry(keymap->key[i].map[j] | 0x100);
723 dump_entry(keymap->key[i].map[j]);
725 printf("}, 0x%02X,0x%02X },\n",
726 (unsigned)keymap->key[i].spcl,
727 (unsigned)keymap->key[i].flgs);
733 dump_accent_definition(char *name, accentmap_t *accentmap)
738 printf("static accentmap_t accentmap_%s = { %d",
739 name, accentmap->n_accs);
740 if (accentmap->n_accs <= 0) {
745 for (i = 0; i < NUM_DEADKEYS; i++) {
746 printf(" /* %s=%d */\n {", acc_names[i], i);
747 c = accentmap->acc[i].accchar;
751 printf(" '\\\\', {");
752 else if (isascii(c) && isprint(c))
753 printf(" '%c', {", c);
755 printf(" 0x00 }, \n");
758 printf(" 0x%02x, {", c);
759 for (j = 0; j < NUM_ACCENTCHARS; j++) {
760 c = accentmap->acc[i].map[j][0];
763 if ((j > 0) && ((j % 4) == 0))
765 if (isascii(c) && isprint(c))
766 printf(" { '%c',", c);
768 printf(" { 0x%02x,", c);
769 printf("0x%02x },", accentmap->acc[i].map[j][1]);
777 load_keymap(char *opt, int dumponly)
780 accentmap_t accentmap;
784 char blank[] = "", keymap_path[] = KEYMAP_PATH, dotkbd[] = ".kbd";
785 char *prefix[] = {blank, blank, keymap_path, NULL};
786 char *postfix[] = {blank, dotkbd, NULL};
788 cp = getenv("KEYMAP_PATH");
790 asprintf(&(prefix[0]), "%s/", cp);
793 for (i=0; prefix[i] && fd == NULL; i++) {
794 for (j=0; postfix[j] && fd == NULL; j++) {
795 name = mkfullname(prefix[i], opt, postfix[j]);
796 fd = fopen(name, "r");
800 warn("keymap file \"%s\" not found", opt);
803 memset(&keymap, 0, sizeof(keymap));
804 memset(&accentmap, 0, sizeof(accentmap));
807 if (get_definition_line(fd, &keymap, &accentmap) < 0)
811 /* fix up the filename to make it a valid C identifier */
812 for (cp = opt; *cp; cp++)
813 if (!isalpha(*cp) && !isdigit(*cp)) *cp = '_';
815 " * Automatically generated from %s.\n"
818 dump_key_definition(opt, &keymap);
819 dump_accent_definition(opt, &accentmap);
822 if ((keymap.n_keys > 0) && (ioctl(0, PIO_KEYMAP, &keymap) < 0)) {
823 warn("setting keymap");
827 if ((accentmap.n_accs > 0)
828 && (ioctl(0, PIO_DEADKEYMAP, &accentmap) < 0)) {
829 warn("setting accentmap");
839 accentmap_t accentmap;
842 if (ioctl(0, GIO_KEYMAP, &keymap) < 0)
843 err(1, "getting keymap");
844 if (ioctl(0, GIO_DEADKEYMAP, &accentmap) < 0)
845 memset(&accentmap, 0, sizeof(accentmap));
848 "# scan cntrl alt alt cntrl lock\n"
849 "# code base shift cntrl shift alt shift cntrl shift state\n"
850 "# ------------------------------------------------------------------\n"
852 for (i=0; i<keymap.n_keys; i++)
853 print_key_definition_line(stdout, i, &keymap.key[i]);
856 for (i = 0; i < NUM_DEADKEYS; i++)
857 print_accent_definition_line(stdout, i, &accentmap.acc[i]);
862 load_default_functionkeys(void)
867 for (i=0; i<NUM_FKEYS; i++) {
869 strcpy(fkey.keydef, fkey_table[i]);
870 fkey.flen = strlen(fkey_table[i]);
871 if (ioctl(0, SETFKEY, &fkey) < 0)
872 warn("setting function key");
877 set_functionkey(char *keynumstr, char *string)
881 if (!strcmp(keynumstr, "load") && !strcmp(string, "default")) {
882 load_default_functionkeys();
885 fkey.keynum = atoi(keynumstr);
886 if (fkey.keynum < 1 || fkey.keynum > NUM_FKEYS) {
887 warnx("function key number must be between 1 and %d",
891 if ((fkey.flen = strlen(string)) > MAXFK) {
892 warnx("function key string too long (%d > %d)",
896 strncpy(fkey.keydef, string, MAXFK);
898 if (ioctl(0, SETFKEY, &fkey) < 0)
899 warn("setting function key");
903 set_bell_values(char *opt)
905 int bell, duration, pitch;
908 if (!strncmp(opt, "quiet.", 6)) {
912 if (!strcmp(opt, "visual"))
914 else if (!strcmp(opt, "normal"))
915 duration = 5, pitch = 800;
916 else if (!strcmp(opt, "off"))
917 duration = 0, pitch = 0;
922 duration = strtol(opt, &v1, 0);
923 if ((duration < 0) || (*v1 != '.'))
926 pitch = strtol(opt, &v1, 0);
927 if ((pitch < 0) || (*opt == '\0') || (*v1 != '\0')) {
929 warnx("argument to -b must be duration.pitch or [quiet.]visual|normal|off");
933 pitch = 1193182 / pitch; /* in Hz */
934 duration /= 10; /* in 10 m sec */
937 ioctl(0, CONS_BELLTYPE, &bell);
938 if ((bell & ~2) == 0)
939 fprintf(stderr, "
\e[=%d;%dB", pitch, duration);
943 set_keyrates(char *opt)
950 if (!strcmp(opt, "slow")) {
951 delay = 1000, repeat = 500;
953 } else if (!strcmp(opt, "normal")) {
954 delay = 500, repeat = 125;
956 } else if (!strcmp(opt, "fast")) {
963 delay = strtol(opt, &v1, 0);
964 if ((delay < 0) || (*v1 != '.'))
967 repeat = strtol(opt, &v1, 0);
968 if ((repeat < 0) || (*opt == '\0') || (*v1 != '\0')) {
970 warnx("argument to -r must be delay.repeat or slow|normal|fast");
973 for (n = 0; n < ndelays - 1; n++)
974 if (delay <= delays[n])
977 for (n = 0; n < nrepeats - 1; n++)
978 if (repeat <= repeats[n])
985 if (ioctl(0, KDSETREPEAT, arg)) {
986 if (ioctl(0, KDSETRAD, (d << 5) | r))
987 warn("setting keyboard rate");
992 get_kbd_type_name(int type)
999 { KB_101, "AT 101/102" },
1000 { KB_OTHER, "generic" },
1004 for (i = 0; i < sizeof(name_table)/sizeof(name_table[0]); ++i) {
1005 if (type == name_table[i].type)
1006 return name_table[i].name;
1014 keyboard_info_t info;
1016 if (ioctl(0, KDGKBINFO, &info) == -1) {
1017 warn("unable to obtain keyboard information");
1020 printf("kbd%d:\n", info.kb_index);
1021 printf(" %.*s%d, type:%s (%d)\n",
1022 (int)sizeof(info.kb_name), info.kb_name, info.kb_unit,
1023 get_kbd_type_name(info.kb_type), info.kb_type);
1027 set_keyboard(char *device)
1029 keyboard_info_t info;
1032 fd = open(device, O_RDONLY);
1034 warn("cannot open %s", device);
1037 if (ioctl(fd, KDGKBINFO, &info) == -1) {
1038 warn("unable to obtain keyboard information");
1043 * The keyboard device driver won't release the keyboard by
1044 * the following ioctl, but it automatically will, when the device
1045 * is closed. So, we don't check error here.
1047 ioctl(fd, CONS_RELKBD, 0);
1050 printf("kbd%d\n", info.kb_index);
1051 printf(" %.*s%d, type:%s (%d)\n",
1052 (int)sizeof(info.kb_name), info.kb_name, info.kb_unit,
1053 get_kbd_type_name(info.kb_type), info.kb_type);
1056 if (ioctl(0, CONS_SETKBD, info.kb_index) == -1)
1057 warn("unable to set keyboard");
1061 release_keyboard(void)
1063 keyboard_info_t info;
1066 * If stdin is not associated with a keyboard, the following ioctl
1069 if (ioctl(0, KDGKBINFO, &info) == -1) {
1070 warn("unable to obtain keyboard information");
1074 printf("kbd%d\n", info.kb_index);
1075 printf(" %.*s%d, type:%s (%d)\n",
1076 (int)sizeof(info.kb_name), info.kb_name, info.kb_unit,
1077 get_kbd_type_name(info.kb_type), info.kb_type);
1079 if (ioctl(0, CONS_RELKBD, 0) == -1)
1080 warn("unable to release the keyboard");
1087 fprintf(stderr, "%s\n%s\n%s\n",
1088 "usage: kbdcontrol [-dFKix] [-b duration.pitch | [quiet.]belltype]",
1089 " [-r delay.repeat | speed] [-l mapfile] [-f # string]",
1090 " [-k device] [-L mapfile]");
1096 main(int argc, char **argv)
1100 while((opt = getopt(argc, argv, "b:df:iKk:Fl:L:r:x")) != -1)
1103 set_bell_values(optarg);
1109 load_keymap(optarg, 0);
1112 load_keymap(optarg, 1);
1115 set_functionkey(optarg,
1116 nextarg(argc, argv, &optind, 'f'));
1119 load_default_functionkeys();
1128 set_keyboard(optarg);
1131 set_keyrates(optarg);
1139 if ((optind != argc) || (argc == 1))