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.
30 static const char rcsid[] =
42 #include <sys/consio.h>
47 * PASTE isn't defined in 4.x, but we need it to bridge to 5.0-current
48 * so define it here as a stop gap transition measure.
51 #define PASTE 0xa3 /* paste from cut-paste buffer */
54 char ctrl_names[32][4] = {
55 "nul", "soh", "stx", "etx", "eot", "enq", "ack", "bel",
56 "bs ", "ht ", "nl ", "vt ", "ff ", "cr ", "so ", "si ",
57 "dle", "dc1", "dc2", "dc3", "dc4", "nak", "syn", "etb",
58 "can", "em ", "sub", "esc", "fs ", "gs ", "rs ", "us "
61 char acc_names[15][5] = {
62 "dgra", "dacu", "dcir", "dtil", "dmac", "dbre", "ddot",
63 "duml", "dsla", "drin", "dced", "dapo", "ddac", "dogo",
67 char acc_names_u[15][5] = {
68 "DGRA", "DACU", "DCIR", "DTIL", "DMAC", "DBRE", "DDOT",
69 "DUML", "DSLA", "DRIN", "DCED", "DAPO", "DDAC", "DOGO",
73 char fkey_table[96][MAXFK] = {
74 /* 01-04 */ "\033[M", "\033[N", "\033[O", "\033[P",
75 /* 05-08 */ "\033[Q", "\033[R", "\033[S", "\033[T",
76 /* 09-12 */ "\033[U", "\033[V", "\033[W", "\033[X",
77 /* 13-16 */ "\033[Y", "\033[Z", "\033[a", "\033[b",
78 /* 17-20 */ "\033[c", "\033[d", "\033[e", "\033[f",
79 /* 21-24 */ "\033[g", "\033[h", "\033[i", "\033[j",
80 /* 25-28 */ "\033[k", "\033[l", "\033[m", "\033[n",
81 /* 29-32 */ "\033[o", "\033[p", "\033[q", "\033[r",
82 /* 33-36 */ "\033[s", "\033[t", "\033[u", "\033[v",
83 /* 37-40 */ "\033[w", "\033[x", "\033[y", "\033[z",
84 /* 41-44 */ "\033[@", "\033[[", "\033[\\","\033[]",
85 /* 45-48 */ "\033[^", "\033[_", "\033[`", "\033[{",
86 /* 49-52 */ "\033[H", "\033[A", "\033[I", "-" ,
87 /* 53-56 */ "\033[D", "\033[E", "\033[C", "+" ,
88 /* 57-60 */ "\033[F", "\033[B", "\033[G", "\033[L",
89 /* 61-64 */ "\177", "\033[J", "\033[~", "\033[}",
90 /* 65-68 */ "" , "" , "" , "" ,
91 /* 69-72 */ "" , "" , "" , "" ,
92 /* 73-76 */ "" , "" , "" , "" ,
93 /* 77-80 */ "" , "" , "" , "" ,
94 /* 81-84 */ "" , "" , "" , "" ,
95 /* 85-88 */ "" , "" , "" , "" ,
96 /* 89-92 */ "" , "" , "" , "" ,
97 /* 93-96 */ "" , "" , "" , "" ,
100 const int delays[] = {250, 500, 750, 1000};
101 const int repeats[] = { 34, 38, 42, 46, 50, 55, 59, 63,
102 68, 76, 84, 92, 100, 110, 118, 126,
103 136, 152, 168, 184, 200, 220, 236, 252,
104 272, 304, 336, 368, 400, 440, 472, 504};
105 const int ndelays = (sizeof(delays) / sizeof(int));
106 const int nrepeats = (sizeof(repeats) / sizeof(int));
112 int get_accent_definition_line __P((accentmap_t *));
113 int get_key_definition_line __P((keymap_t *));
114 void usage __P((void));
117 nextarg(int ac, char **av, int *indp, int oc)
120 return(av[(*indp)++]);
121 warnx("option requires two arguments -- %c", oc);
128 mkfullname(const char *s1, const char *s2, const char *s3)
130 static char *buf = NULL;
134 f = strlen(s1) + strlen(s2) + strlen(s3) + 1;
137 buf = (char *)realloc(buf, f);
139 buf = (char *)malloc(f);
157 switch ((token = yylex())) {
205 return LCTRA | 0x100;
207 return RCTRA | 0x100;
209 return LALTA | 0x100;
211 return RALTA | 0x100;
217 return PASTE | 0x100;
219 if (ACC(number) > L_ACC)
221 return ACC(number) | 0x100;
223 if (F(number) > L_FN)
225 return F(number) | 0x100;
227 if (S(number) > L_SCR)
229 return S(number) | 0x100;
231 return (unsigned char)letter;
233 if (number < 0 || number > 255)
242 get_definition_line(FILE *fd, keymap_t *keymap, accentmap_t *accentmap)
252 c = get_key_definition_line(keymap);
254 errx(1, "invalid key definition");
255 if (c > keymap->n_keys)
259 c = get_accent_definition_line(accentmap);
261 errx(1, "invalid accent key definition");
262 if (c > accentmap->n_accs)
263 accentmap->n_accs = c;
269 errx(1, "illegal definition line");
275 get_key_definition_line(keymap_t *map)
277 int i, def, scancode;
279 /* check scancode number */
280 if (number < 0 || number >= NUM_KEYS)
284 /* get key definitions */
285 map->key[scancode].spcl = 0;
286 for (i=0; i<NUM_STATES; i++) {
287 if ((def = get_entry()) == -1)
290 map->key[scancode].spcl |= (0x80 >> i);
291 map->key[scancode].map[i] = def & 0xFF;
293 /* get lock state key def */
294 if ((token = yylex()) != TFLAG)
296 map->key[scancode].flgs = number;
298 return (scancode + 1);
302 get_accent_definition_line(accentmap_t *map)
308 if (ACC(number) < F_ACC || ACC(number) > L_ACC)
309 /* number out of range */
312 if (map->acc[accent].accchar != 0) {
313 /* this entry has already been defined before! */
314 errx(1, "duplicated accent key definition");
317 switch ((token = yylex())) {
319 map->acc[accent].accchar = letter;
322 map->acc[accent].accchar = number;
328 for (i = 0; (token = yylex()) == '(';) {
329 switch ((token = yylex())) {
339 switch ((token = yylex())) {
349 if ((token = yylex()) != ')')
351 if (i >= NUM_ACCENTCHARS) {
352 warnx("too many accented characters, ignored");
355 map->acc[accent].map[i][0] = c1;
356 map->acc[accent].map[i][1] = c2;
363 print_entry(FILE *fp, int value)
365 int val = value & 0xFF;
369 fprintf(fp, " nop ");
372 fprintf(fp, " lshift");
375 fprintf(fp, " rshift");
378 fprintf(fp, " clock ");
381 fprintf(fp, " nlock ");
384 fprintf(fp, " slock ");
387 fprintf(fp, " btab ");
390 fprintf(fp, " lalt ");
393 fprintf(fp, " lctrl ");
396 fprintf(fp, " nscr ");
399 fprintf(fp, " pscr ");
402 fprintf(fp, " rctrl ");
405 fprintf(fp, " ralt ");
408 fprintf(fp, " alock ");
411 fprintf(fp, " ashift");
414 fprintf(fp, " meta ");
417 fprintf(fp, " boot ");
420 fprintf(fp, " debug ");
423 fprintf(fp, " susp ");
426 fprintf(fp, " saver ");
429 fprintf(fp, " panic ");
432 fprintf(fp, " lshifta");
435 fprintf(fp, " rshifta");
438 fprintf(fp, " lctrla");
441 fprintf(fp, " rctrla");
444 fprintf(fp, " lalta ");
447 fprintf(fp, " ralta ");
450 fprintf(fp, " halt ");
453 fprintf(fp, " pdwn ");
456 fprintf(fp, " paste ");
460 if (val >= F_FN && val <= L_FN)
461 fprintf(fp, " fkey%02d", val - F_FN + 1);
462 else if (val >= F_SCR && val <= L_SCR)
463 fprintf(fp, " scr%02d ", val - F_SCR + 1);
464 else if (val >= F_ACC && val <= L_ACC)
465 fprintf(fp, " %-6s", acc_names[val - F_ACC]);
467 fprintf(fp, " 0x%02x ", val);
469 fprintf(fp, " %3d ", val);
473 fprintf(fp, " %s ", ctrl_names[val]);
475 fprintf(fp, " del ");
476 else if (isascii(val) && isprint(val))
477 fprintf(fp, " '%c' ", val);
479 fprintf(fp, " 0x%02x ", val);
481 fprintf(fp, " %3d ", val);
488 print_key_definition_line(FILE *fp, int scancode, struct keyent_t *key)
492 /* print scancode number */
494 fprintf(fp, " 0x%02x ", scancode);
496 fprintf(fp, " %03d ", scancode);
498 /* print key definitions */
499 for (i=0; i<NUM_STATES; i++) {
500 if (key->spcl & (0x80 >> i))
501 print_entry(fp, key->map[i] | 0x100);
503 print_entry(fp, key->map[i]);
506 /* print lock state key def */
524 print_accent_definition_line(FILE *fp, int accent, struct acc_t *key)
529 if (key->accchar == 0)
532 /* print accent number */
533 fprintf(fp, " %-6s", acc_names[accent]);
534 if (isascii(key->accchar) && isprint(key->accchar))
535 fprintf(fp, "'%c' ", key->accchar);
537 fprintf(fp, "0x%02x ", key->accchar);
539 fprintf(fp, "%03d ", key->accchar);
541 for (i = 0; i < NUM_ACCENTCHARS; ++i) {
545 if ((i > 0) && ((i % 4) == 0))
547 if (isascii(c) && isprint(c))
548 fprintf(fp, "( '%c' ", c);
550 fprintf(fp, "(0x%02x ", c);
552 fprintf(fp, "( %03d ", c);
554 if (isascii(c) && isprint(c))
555 fprintf(fp, "'%c' ) ", c);
557 fprintf(fp, "0x%02x) ", c);
559 fprintf(fp, "%03d ) ", c);
565 dump_entry(int value)
661 if (value >= F_FN && value <= L_FN)
662 printf(" F(%2d),", value - F_FN + 1);
663 else if (value >= F_SCR && value <= L_SCR)
664 printf(" S(%2d),", value - F_SCR + 1);
665 else if (value >= F_ACC && value <= L_ACC)
666 printf(" %-4s, ", acc_names_u[value - F_ACC]);
668 printf(" 0x%02X, ", value);
671 } else if (value == '\'') {
673 } else if (value == '\\') {
675 } else if (isascii(value) && isprint(value)) {
676 printf(" '%c', ", value);
678 printf(" 0x%02X, ", value);
683 dump_key_definition(char *name, keymap_t *keymap)
687 printf("static keymap_t keymap_%s = { 0x%02x, {\n",
688 name, (unsigned)keymap->n_keys);
691 " * scan cntrl alt alt cntrl\n"
692 " * code base shift cntrl shift alt shift cntrl shift spcl flgs\n"
693 " * ---------------------------------------------------------------------------\n"
695 for (i = 0; i < keymap->n_keys; i++) {
696 printf("/*%02x*/{{", i);
697 for (j = 0; j < NUM_STATES; j++) {
698 if (keymap->key[i].spcl & (0x80 >> j))
699 dump_entry(keymap->key[i].map[j] | 0x100);
701 dump_entry(keymap->key[i].map[j]);
703 printf("}, 0x%02X,0x%02X },\n",
704 (unsigned)keymap->key[i].spcl,
705 (unsigned)keymap->key[i].flgs);
711 dump_accent_definition(char *name, accentmap_t *accentmap)
716 printf("static accentmap_t accentmap_%s = { %d",
717 name, accentmap->n_accs);
718 if (accentmap->n_accs <= 0) {
723 for (i = 0; i < NUM_DEADKEYS; i++) {
724 printf(" /* %s=%d */\n {", acc_names[i], i);
725 c = accentmap->acc[i].accchar;
729 printf(" '\\\\', {");
730 else if (isascii(c) && isprint(c))
731 printf(" '%c', {", c);
733 printf(" 0x00 }, \n");
736 printf(" 0x%02x, {", c);
737 for (j = 0; j < NUM_ACCENTCHARS; j++) {
738 c = accentmap->acc[i].map[j][0];
741 if ((j > 0) && ((j % 4) == 0))
743 if (isascii(c) && isprint(c))
744 printf(" { '%c',", c);
746 printf(" { 0x%02x,", c);
747 printf("0x%02x },", accentmap->acc[i].map[j][1]);
755 load_keymap(char *opt, int dumponly)
758 accentmap_t accentmap;
762 char *prefix[] = {"", "", KEYMAP_PATH, NULL};
763 char *postfix[] = {"", ".kbd", NULL};
765 cp = getenv("KEYMAP_PATH");
767 asprintf(&(prefix[0]), "%s/", cp);
770 for (i=0; prefix[i] && fd == NULL; i++) {
771 for (j=0; postfix[j] && fd == NULL; j++) {
772 name = mkfullname(prefix[i], opt, postfix[j]);
773 fd = fopen(name, "r");
777 warn("keymap file \"%s\" not found", opt);
780 memset(&keymap, 0, sizeof(keymap));
781 memset(&accentmap, 0, sizeof(accentmap));
784 if (get_definition_line(fd, &keymap, &accentmap) < 0)
788 /* fix up the filename to make it a valid C identifier */
789 for (cp = opt; *cp; cp++)
790 if (!isalpha(*cp) && !isdigit(*cp)) *cp = '_';
792 " * Automatically generated from %s.\n"
795 dump_key_definition(opt, &keymap);
796 dump_accent_definition(opt, &accentmap);
799 if ((keymap.n_keys > 0) && (ioctl(0, PIO_KEYMAP, &keymap) < 0)) {
800 warn("setting keymap");
804 if ((accentmap.n_accs > 0)
805 && (ioctl(0, PIO_DEADKEYMAP, &accentmap) < 0)) {
806 warn("setting accentmap");
816 accentmap_t accentmap;
819 if (ioctl(0, GIO_KEYMAP, &keymap) < 0)
820 err(1, "getting keymap");
821 if (ioctl(0, GIO_DEADKEYMAP, &accentmap) < 0)
822 memset(&accentmap, 0, sizeof(accentmap));
825 "# scan cntrl alt alt cntrl lock\n"
826 "# code base shift cntrl shift alt shift cntrl shift state\n"
827 "# ------------------------------------------------------------------\n"
829 for (i=0; i<keymap.n_keys; i++)
830 print_key_definition_line(stdout, i, &keymap.key[i]);
833 for (i = 0; i < NUM_DEADKEYS; i++)
834 print_accent_definition_line(stdout, i, &accentmap.acc[i]);
840 load_default_functionkeys()
845 for (i=0; i<NUM_FKEYS; i++) {
847 strcpy(fkey.keydef, fkey_table[i]);
848 fkey.flen = strlen(fkey_table[i]);
849 if (ioctl(0, SETFKEY, &fkey) < 0)
850 warn("setting function key");
855 set_functionkey(char *keynumstr, char *string)
859 if (!strcmp(keynumstr, "load") && !strcmp(string, "default")) {
860 load_default_functionkeys();
863 fkey.keynum = atoi(keynumstr);
864 if (fkey.keynum < 1 || fkey.keynum > NUM_FKEYS) {
865 warnx("function key number must be between 1 and %d",
869 if ((fkey.flen = strlen(string)) > MAXFK) {
870 warnx("function key string too long (%d > %d)",
874 strcpy(fkey.keydef, string);
876 if (ioctl(0, SETFKEY, &fkey) < 0)
877 warn("setting function key");
882 set_bell_values(char *opt)
884 int bell, duration, pitch;
887 if (!strncmp(opt, "quiet.", 6)) {
891 if (!strcmp(opt, "visual"))
893 else if (!strcmp(opt, "normal"))
894 duration = 5, pitch = 800;
895 else if (!strcmp(opt, "off"))
896 duration = 0, pitch = 0;
901 duration = strtol(opt, &v1, 0);
902 if ((duration < 0) || (*v1 != '.'))
905 pitch = strtol(opt, &v1, 0);
906 if ((pitch < 0) || (*opt == '\0') || (*v1 != '\0')) {
908 warnx("argument to -b must be duration.pitch or [quiet.]visual|normal|off");
912 pitch = 1193182 / pitch; /* in Hz */
913 duration /= 10; /* in 10 m sec */
916 ioctl(0, CONS_BELLTYPE, &bell);
917 if ((bell & ~2) == 0)
918 fprintf(stderr, "
\e[=%d;%dB", pitch, duration);
923 set_keyrates(char *opt)
930 if (!strcmp(opt, "slow")) {
931 delay = 1000, repeat = 500;
933 } else if (!strcmp(opt, "normal")) {
934 delay = 500, repeat = 125;
936 } else if (!strcmp(opt, "fast")) {
943 delay = strtol(opt, &v1, 0);
944 if ((delay < 0) || (*v1 != '.'))
947 repeat = strtol(opt, &v1, 0);
948 if ((repeat < 0) || (*opt == '\0') || (*v1 != '\0')) {
950 warnx("argument to -r must be delay.repeat or slow|normal|fast");
953 for (n = 0; n < ndelays - 1; n++)
954 if (delay <= delays[n])
957 for (n = 0; n < nrepeats - 1; n++)
958 if (repeat <= repeats[n])
965 if (ioctl(0, KDSETREPEAT, arg)) {
966 if (ioctl(0, KDSETRAD, (d << 5) | r))
967 warn("setting keyboard rate");
972 *get_kbd_type_name(int type)
979 { KB_101, "AT 101/102" },
980 { KB_OTHER, "generic" },
984 for (i = 0; i < sizeof(name_table)/sizeof(name_table[0]); ++i) {
985 if (type == name_table[i].type)
986 return name_table[i].name;
994 keyboard_info_t info;
996 if (ioctl(0, KDGKBINFO, &info) == -1) {
997 warn("unable to obtain keyboard information");
1000 printf("kbd%d:\n", info.kb_index);
1001 printf(" %.*s%d, type:%s (%d)\n",
1002 (int)sizeof(info.kb_name), info.kb_name, info.kb_unit,
1003 get_kbd_type_name(info.kb_type), info.kb_type);
1008 set_keyboard(char *device)
1010 keyboard_info_t info;
1013 fd = open(device, O_RDONLY);
1015 warn("cannot open %s", device);
1018 if (ioctl(fd, KDGKBINFO, &info) == -1) {
1019 warn("unable to obtain keyboard information");
1024 * The keyboard device driver won't release the keyboard by
1025 * the following ioctl, but it automatically will, when the device
1026 * is closed. So, we don't check error here.
1028 ioctl(fd, CONS_RELKBD, 0);
1031 printf("kbd%d\n", info.kb_index);
1032 printf(" %.*s%d, type:%s (%d)\n",
1033 (int)sizeof(info.kb_name), info.kb_name, info.kb_unit,
1034 get_kbd_type_name(info.kb_type), info.kb_type);
1037 if (ioctl(0, CONS_SETKBD, info.kb_index) == -1)
1038 warn("unable to set keyboard");
1043 release_keyboard(void)
1045 keyboard_info_t info;
1048 * If stdin is not associated with a keyboard, the following ioctl
1051 if (ioctl(0, KDGKBINFO, &info) == -1) {
1052 warn("unable to obtain keyboard information");
1056 printf("kbd%d\n", info.kb_index);
1057 printf(" %.*s%d, type:%s (%d)\n",
1058 (int)sizeof(info.kb_name), info.kb_name, info.kb_unit,
1059 get_kbd_type_name(info.kb_type), info.kb_type);
1061 if (ioctl(0, CONS_RELKBD, 0) == -1)
1062 warn("unable to release the keyboard");
1069 fprintf(stderr, "%s\n%s\n%s\n",
1070 "usage: kbdcontrol [-dFKix] [-b duration.pitch | [quiet.]belltype]",
1071 " [-r delay.repeat | speed] [-l mapfile] [-f # string]",
1072 " [-k device] [-L mapfile]");
1078 main(int argc, char **argv)
1082 while((opt = getopt(argc, argv, "b:df:iKk:Fl:L:r:x")) != -1)
1085 set_bell_values(optarg);
1091 load_keymap(optarg, 0);
1094 load_keymap(optarg, 1);
1097 set_functionkey(optarg,
1098 nextarg(argc, argv, &optind, 'f'));
1101 load_default_functionkeys();
1110 set_keyboard(optarg);
1113 set_keyrates(optarg);
1121 if ((optind != argc) || (argc == 1))