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.
28 * $FreeBSD: src/usr.sbin/kbdcontrol/kbdcontrol.c,v 1.30.2.2 2001/06/08 18:27:32 sobomax Exp $
29 * $DragonFly: src/usr.sbin/kbdcontrol/kbdcontrol.c,v 1.5 2005/10/30 23:00:57 swildner Exp $
39 #include <machine/console.h>
43 char ctrl_names
[32][4] = {
44 "nul", "soh", "stx", "etx", "eot", "enq", "ack", "bel",
45 "bs ", "ht ", "nl ", "vt ", "ff ", "cr ", "so ", "si ",
46 "dle", "dc1", "dc2", "dc3", "dc4", "nak", "syn", "etb",
47 "can", "em ", "sub", "esc", "fs ", "gs ", "rs ", "us "
50 char acc_names
[15][5] = {
51 "dgra", "dacu", "dcir", "dtil", "dmac", "dbre", "ddot",
52 "duml", "dsla", "drin", "dced", "dapo", "ddac", "dogo",
56 char acc_names_u
[15][5] = {
57 "DGRA", "DACU", "DCIR", "DTIL", "DMAC", "DBRE", "DDOT",
58 "DUML", "DSLA", "DRIN", "DCED", "DAPO", "DDAC", "DOGO",
62 char fkey_table
[96][MAXFK
] = {
63 /* 01-04 */ "\033[M", "\033[N", "\033[O", "\033[P",
64 /* 05-08 */ "\033[Q", "\033[R", "\033[S", "\033[T",
65 /* 09-12 */ "\033[U", "\033[V", "\033[W", "\033[X",
66 /* 13-16 */ "\033[Y", "\033[Z", "\033[a", "\033[b",
67 /* 17-20 */ "\033[c", "\033[d", "\033[e", "\033[f",
68 /* 21-24 */ "\033[g", "\033[h", "\033[i", "\033[j",
69 /* 25-28 */ "\033[k", "\033[l", "\033[m", "\033[n",
70 /* 29-32 */ "\033[o", "\033[p", "\033[q", "\033[r",
71 /* 33-36 */ "\033[s", "\033[t", "\033[u", "\033[v",
72 /* 37-40 */ "\033[w", "\033[x", "\033[y", "\033[z",
73 /* 41-44 */ "\033[@", "\033[[", "\033[\\","\033[]",
74 /* 45-48 */ "\033[^", "\033[_", "\033[`", "\033[{",
75 /* 49-52 */ "\033[H", "\033[A", "\033[I", "-" ,
76 /* 53-56 */ "\033[D", "\033[E", "\033[C", "+" ,
77 /* 57-60 */ "\033[F", "\033[B", "\033[G", "\033[L",
78 /* 61-64 */ "\177", "\033[J", "\033[~", "\033[}",
79 /* 65-68 */ "" , "" , "" , "" ,
80 /* 69-72 */ "" , "" , "" , "" ,
81 /* 73-76 */ "" , "" , "" , "" ,
82 /* 77-80 */ "" , "" , "" , "" ,
83 /* 81-84 */ "" , "" , "" , "" ,
84 /* 85-88 */ "" , "" , "" , "" ,
85 /* 89-92 */ "" , "" , "" , "" ,
86 /* 93-96 */ "" , "" , "" , "" ,
89 const int delays
[] = {250, 500, 750, 1000};
90 const int repeats
[] = { 34, 38, 42, 46, 50, 55, 59, 63,
91 68, 76, 84, 92, 100, 110, 118, 126,
92 136, 152, 168, 184, 200, 220, 236, 252,
93 272, 304, 336, 368, 400, 440, 472, 504};
94 const int ndelays
= (sizeof(delays
) / sizeof(int));
95 const int nrepeats
= (sizeof(repeats
) / sizeof(int));
101 int get_accent_definition_line(accentmap_t
*);
102 int get_key_definition_line(keymap_t
*);
106 nextarg(int ac
, char **av
, int *indp
, int oc
)
109 return(av
[(*indp
)++]);
110 warnx("option requires two arguments -- %c", oc
);
117 mkfullname(const char *s1
, const char *s2
, const char *s3
)
119 static char *buf
= NULL
;
123 f
= strlen(s1
) + strlen(s2
) + strlen(s3
) + 1;
126 buf
= (char *)realloc(buf
, f
);
128 buf
= (char *)malloc(f
);
146 switch ((token
= yylex())) {
194 return LCTRA
| 0x100;
196 return RCTRA
| 0x100;
198 return LALTA
| 0x100;
200 return RALTA
| 0x100;
206 if (ACC(number
) > L_ACC
)
208 return ACC(number
) | 0x100;
210 if (F(number
) > L_FN
)
212 return F(number
) | 0x100;
214 if (S(number
) > L_SCR
)
216 return S(number
) | 0x100;
218 return (unsigned char)letter
;
220 if (number
< 0 || number
> 255)
229 get_definition_line(FILE *fd
, keymap_t
*keymap
, accentmap_t
*accentmap
)
239 c
= get_key_definition_line(keymap
);
241 errx(1, "invalid key definition");
242 if (c
> keymap
->n_keys
)
246 c
= get_accent_definition_line(accentmap
);
248 errx(1, "invalid accent key definition");
249 if (c
> accentmap
->n_accs
)
250 accentmap
->n_accs
= c
;
256 errx(1, "illegal definition line");
262 get_key_definition_line(keymap_t
*map
)
264 int i
, def
, scancode
;
266 /* check scancode number */
267 if (number
< 0 || number
>= NUM_KEYS
)
271 /* get key definitions */
272 map
->key
[scancode
].spcl
= 0;
273 for (i
=0; i
<NUM_STATES
; i
++) {
274 if ((def
= get_entry()) == -1)
277 map
->key
[scancode
].spcl
|= (0x80 >> i
);
278 map
->key
[scancode
].map
[i
] = def
& 0xFF;
280 /* get lock state key def */
281 if ((token
= yylex()) != TFLAG
)
283 map
->key
[scancode
].flgs
= number
;
285 return (scancode
+ 1);
289 get_accent_definition_line(accentmap_t
*map
)
295 if (ACC(number
) < F_ACC
|| ACC(number
) > L_ACC
)
296 /* number out of range */
299 if (map
->acc
[accent
].accchar
!= 0) {
300 /* this entry has already been defined before! */
301 errx(1, "duplicated accent key definition");
304 switch ((token
= yylex())) {
306 map
->acc
[accent
].accchar
= letter
;
309 map
->acc
[accent
].accchar
= number
;
315 for (i
= 0; (token
= yylex()) == '(';) {
316 switch ((token
= yylex())) {
326 switch ((token
= yylex())) {
336 if ((token
= yylex()) != ')')
338 if (i
>= NUM_ACCENTCHARS
) {
339 warnx("too many accented characters, ignored");
342 map
->acc
[accent
].map
[i
][0] = c1
;
343 map
->acc
[accent
].map
[i
][1] = c2
;
350 print_entry(FILE *fp
, int value
)
352 int val
= value
& 0xFF;
356 fprintf(fp
, " nop ");
359 fprintf(fp
, " lshift");
362 fprintf(fp
, " rshift");
365 fprintf(fp
, " clock ");
368 fprintf(fp
, " nlock ");
371 fprintf(fp
, " slock ");
374 fprintf(fp
, " btab ");
377 fprintf(fp
, " lalt ");
380 fprintf(fp
, " lctrl ");
383 fprintf(fp
, " nscr ");
386 fprintf(fp
, " pscr ");
389 fprintf(fp
, " rctrl ");
392 fprintf(fp
, " ralt ");
395 fprintf(fp
, " alock ");
398 fprintf(fp
, " ashift");
401 fprintf(fp
, " meta ");
404 fprintf(fp
, " boot ");
407 fprintf(fp
, " debug ");
410 fprintf(fp
, " susp ");
413 fprintf(fp
, " saver ");
416 fprintf(fp
, " panic ");
419 fprintf(fp
, " lshifta");
422 fprintf(fp
, " rshifta");
425 fprintf(fp
, " lctrla");
428 fprintf(fp
, " rctrla");
431 fprintf(fp
, " lalta ");
434 fprintf(fp
, " ralta ");
437 fprintf(fp
, " halt ");
440 fprintf(fp
, " pdwn ");
444 if (val
>= F_FN
&& val
<= L_FN
)
445 fprintf(fp
, " fkey%02d", val
- F_FN
+ 1);
446 else if (val
>= F_SCR
&& val
<= L_SCR
)
447 fprintf(fp
, " scr%02d ", val
- F_SCR
+ 1);
448 else if (val
>= F_ACC
&& val
<= L_ACC
)
449 fprintf(fp
, " %-6s", acc_names
[val
- F_ACC
]);
451 fprintf(fp
, " 0x%02x ", val
);
453 fprintf(fp
, " %3d ", val
);
457 fprintf(fp
, " %s ", ctrl_names
[val
]);
459 fprintf(fp
, " del ");
460 else if (isascii(val
) && isprint(val
))
461 fprintf(fp
, " '%c' ", val
);
463 fprintf(fp
, " 0x%02x ", val
);
465 fprintf(fp
, " %3d ", val
);
472 print_key_definition_line(FILE *fp
, int scancode
, struct keyent_t
*key
)
476 /* print scancode number */
478 fprintf(fp
, " 0x%02x ", scancode
);
480 fprintf(fp
, " %03d ", scancode
);
482 /* print key definitions */
483 for (i
=0; i
<NUM_STATES
; i
++) {
484 if (key
->spcl
& (0x80 >> i
))
485 print_entry(fp
, key
->map
[i
] | 0x100);
487 print_entry(fp
, key
->map
[i
]);
490 /* print lock state key def */
508 print_accent_definition_line(FILE *fp
, int accent
, struct acc_t
*key
)
513 if (key
->accchar
== 0)
516 /* print accent number */
517 fprintf(fp
, " %-6s", acc_names
[accent
]);
518 if (isascii(key
->accchar
) && isprint(key
->accchar
))
519 fprintf(fp
, "'%c' ", key
->accchar
);
521 fprintf(fp
, "0x%02x ", key
->accchar
);
523 fprintf(fp
, "%03d ", key
->accchar
);
525 for (i
= 0; i
< NUM_ACCENTCHARS
; ++i
) {
529 if ((i
> 0) && ((i
% 4) == 0))
531 if (isascii(c
) && isprint(c
))
532 fprintf(fp
, "( '%c' ", c
);
534 fprintf(fp
, "(0x%02x ", c
);
536 fprintf(fp
, "( %03d ", c
);
538 if (isascii(c
) && isprint(c
))
539 fprintf(fp
, "'%c' ) ", c
);
541 fprintf(fp
, "0x%02x) ", c
);
543 fprintf(fp
, "%03d ) ", c
);
549 dump_entry(int value
)
642 if (value
>= F_FN
&& value
<= L_FN
)
643 printf(" F(%2d),", value
- F_FN
+ 1);
644 else if (value
>= F_SCR
&& value
<= L_SCR
)
645 printf(" S(%2d),", value
- F_SCR
+ 1);
646 else if (value
>= F_ACC
&& value
<= L_ACC
)
647 printf(" %-4s, ", acc_names_u
[value
- F_ACC
]);
649 printf(" 0x%02X, ", value
);
652 } else if (value
== '\'') {
654 } else if (value
== '\\') {
656 } else if (isascii(value
) && isprint(value
)) {
657 printf(" '%c', ", value
);
659 printf(" 0x%02X, ", value
);
664 dump_key_definition(char *name
, keymap_t
*keymap
)
668 printf("static keymap_t keymap_%s = { 0x%02x, {\n",
669 name
, (unsigned)keymap
->n_keys
);
672 " * scan cntrl alt alt cntrl\n"
673 " * code base shift cntrl shift alt shift cntrl shift spcl flgs\n"
674 " * ---------------------------------------------------------------------------\n"
676 for (i
= 0; i
< keymap
->n_keys
; i
++) {
677 printf("/*%02x*/{{", i
);
678 for (j
= 0; j
< NUM_STATES
; j
++) {
679 if (keymap
->key
[i
].spcl
& (0x80 >> j
))
680 dump_entry(keymap
->key
[i
].map
[j
] | 0x100);
682 dump_entry(keymap
->key
[i
].map
[j
]);
684 printf("}, 0x%02X,0x%02X },\n",
685 (unsigned)keymap
->key
[i
].spcl
,
686 (unsigned)keymap
->key
[i
].flgs
);
692 dump_accent_definition(char *name
, accentmap_t
*accentmap
)
697 printf("static accentmap_t accentmap_%s = { %d",
698 name
, accentmap
->n_accs
);
699 if (accentmap
->n_accs
<= 0) {
704 for (i
= 0; i
< NUM_DEADKEYS
; i
++) {
705 printf(" /* %s=%d */\n {", acc_names
[i
], i
);
706 c
= accentmap
->acc
[i
].accchar
;
710 printf(" '\\\\', {");
711 else if (isascii(c
) && isprint(c
))
712 printf(" '%c', {", c
);
714 printf(" 0x00 }, \n");
717 printf(" 0x%02x, {", c
);
718 for (j
= 0; j
< NUM_ACCENTCHARS
; j
++) {
719 c
= accentmap
->acc
[i
].map
[j
][0];
722 if ((j
> 0) && ((j
% 4) == 0))
724 if (isascii(c
) && isprint(c
))
725 printf(" { '%c',", c
);
727 printf(" { 0x%02x,", c
);
728 printf("0x%02x },", accentmap
->acc
[i
].map
[j
][1]);
736 load_keymap(char *opt
, int dumponly
)
739 accentmap_t accentmap
;
743 char *prefix
[] = {"", "", KEYMAP_PATH
, KEYMAP_PATH
, NULL
};
744 char *postfix
[] = {"", ".kbd", "", ".kbd"};
746 for (i
=0; prefix
[i
]; i
++) {
747 name
= mkfullname(prefix
[i
], opt
, postfix
[i
]);
748 if ((fd
= fopen(name
, "r")))
752 warn("keymap file not found");
755 memset(&keymap
, 0, sizeof(keymap
));
756 memset(&accentmap
, 0, sizeof(accentmap
));
759 if (get_definition_line(fd
, &keymap
, &accentmap
) < 0)
763 /* fix up the filename to make it a valid C identifier */
764 for (cp
= opt
; *cp
; cp
++)
765 if (!isalpha(*cp
) && !isdigit(*cp
)) *cp
= '_';
767 " * Automatically generated from %s.\n"
770 dump_key_definition(opt
, &keymap
);
771 dump_accent_definition(opt
, &accentmap
);
774 if ((keymap
.n_keys
> 0) && (ioctl(0, PIO_KEYMAP
, &keymap
) < 0)) {
775 warn("setting keymap");
779 if ((accentmap
.n_accs
> 0)
780 && (ioctl(0, PIO_DEADKEYMAP
, &accentmap
) < 0)) {
781 warn("setting accentmap");
791 accentmap_t accentmap
;
794 if (ioctl(0, GIO_KEYMAP
, &keymap
) < 0)
795 err(1, "getting keymap");
796 if (ioctl(0, GIO_DEADKEYMAP
, &accentmap
) < 0)
797 memset(&accentmap
, 0, sizeof(accentmap
));
800 "# scan cntrl alt alt cntrl lock\n"
801 "# code base shift cntrl shift alt shift cntrl shift state\n"
802 "# ------------------------------------------------------------------\n"
804 for (i
=0; i
<keymap
.n_keys
; i
++)
805 print_key_definition_line(stdout
, i
, &keymap
.key
[i
]);
808 for (i
= 0; i
< NUM_DEADKEYS
; i
++)
809 print_accent_definition_line(stdout
, i
, &accentmap
.acc
[i
]);
815 load_default_functionkeys(void)
820 for (i
=0; i
<NUM_FKEYS
; i
++) {
822 strcpy(fkey
.keydef
, fkey_table
[i
]);
823 fkey
.flen
= strlen(fkey_table
[i
]);
824 if (ioctl(0, SETFKEY
, &fkey
) < 0)
825 warn("setting function key");
830 set_functionkey(char *keynumstr
, char *string
)
834 if (!strcmp(keynumstr
, "load") && !strcmp(string
, "default")) {
835 load_default_functionkeys();
838 fkey
.keynum
= atoi(keynumstr
);
839 if (fkey
.keynum
< 1 || fkey
.keynum
> NUM_FKEYS
) {
840 warnx("function key number must be between 1 and %d",
844 if ((fkey
.flen
= strlen(string
)) > MAXFK
) {
845 warnx("function key string too long (%d > %d)",
849 strcpy(fkey
.keydef
, string
);
851 if (ioctl(0, SETFKEY
, &fkey
) < 0)
852 warn("setting function key");
857 set_bell_values(char *opt
)
859 int bell
, duration
, pitch
;
862 if (!strncmp(opt
, "quiet.", 6)) {
866 if (!strcmp(opt
, "visual"))
868 else if (!strcmp(opt
, "normal"))
869 duration
= 5, pitch
= 800;
870 else if (!strcmp(opt
, "off"))
871 duration
= 0, pitch
= 0;
876 duration
= strtol(opt
, &v1
, 0);
877 if ((duration
< 0) || (*v1
!= '.'))
880 pitch
= strtol(opt
, &v1
, 0);
881 if ((pitch
< 0) || (*opt
== '\0') || (*v1
!= '\0')) {
883 warnx("argument to -b must be duration.pitch or [quiet.]visual|normal|off");
887 pitch
= 1193182 / pitch
; /* in Hz */
888 duration
/= 10; /* in 10 m sec */
891 ioctl(0, CONS_BELLTYPE
, &bell
);
892 if ((bell
& ~2) == 0)
893 fprintf(stderr
, "\e[=%d;%dB", pitch
, duration
);
898 set_keyrates(char *opt
)
905 if (!strcmp(opt
, "slow")) {
906 delay
= 1000, repeat
= 500;
908 } else if (!strcmp(opt
, "normal")) {
909 delay
= 500, repeat
= 125;
911 } else if (!strcmp(opt
, "fast")) {
918 delay
= strtol(opt
, &v1
, 0);
919 if ((delay
< 0) || (*v1
!= '.'))
922 repeat
= strtol(opt
, &v1
, 0);
923 if ((repeat
< 0) || (*opt
== '\0') || (*v1
!= '\0')) {
925 warnx("argument to -r must be delay.repeat or slow|normal|fast");
928 for (n
= 0; n
< ndelays
- 1; n
++)
929 if (delay
<= delays
[n
])
932 for (n
= 0; n
< nrepeats
- 1; n
++)
933 if (repeat
<= repeats
[n
])
940 if (ioctl(0, KDSETREPEAT
, arg
)) {
941 if (ioctl(0, KDSETRAD
, (d
<< 5) | r
))
942 warn("setting keyboard rate");
948 set_history(char *opt
)
953 if ((*opt
== '\0') || size
< 0) {
954 warnx("argument must be a positive number");
957 if (ioctl(0, CONS_HISTORY
, &size
) == -1)
958 warn("setting history buffer size");
962 get_kbd_type_name(int type
)
969 { KB_101
, "AT 101/102" },
970 { KB_OTHER
, "generic" },
974 for (i
= 0; i
< sizeof(name_table
)/sizeof(name_table
[0]); ++i
) {
975 if (type
== name_table
[i
].type
)
976 return name_table
[i
].name
;
984 keyboard_info_t info
;
986 if (ioctl(0, KDGKBINFO
, &info
) == -1) {
987 warn("unable to obtain keyboard information");
990 printf("kbd%d:\n", info
.kb_index
);
991 printf(" %.*s%d, type:%s (%d)\n",
992 (int)sizeof(info
.kb_name
), info
.kb_name
, info
.kb_unit
,
993 get_kbd_type_name(info
.kb_type
), info
.kb_type
);
998 set_keyboard(char *device
)
1000 keyboard_info_t info
;
1003 fd
= open(device
, O_RDONLY
);
1005 warn("cannot open %s", device
);
1008 if (ioctl(fd
, KDGKBINFO
, &info
) == -1) {
1009 warn("unable to obtain keyboard information");
1014 * The keyboard device driver won't release the keyboard by
1015 * the following ioctl, but it automatically will, when the device
1016 * is closed. So, we don't check error here.
1018 ioctl(fd
, CONS_RELKBD
, 0);
1021 printf("kbd%d\n", info
.kb_index
);
1022 printf(" %.*s%d, type:%s (%d)\n",
1023 (int)sizeof(info
.kb_name
), info
.kb_name
, info
.kb_unit
,
1024 get_kbd_type_name(info
.kb_type
), info
.kb_type
);
1027 if (ioctl(0, CONS_SETKBD
, info
.kb_index
) == -1)
1028 warn("unable to set keyboard");
1033 release_keyboard(void)
1035 keyboard_info_t info
;
1038 * If stdin is not associated with a keyboard, the following ioctl
1041 if (ioctl(0, KDGKBINFO
, &info
) == -1) {
1042 warn("unable to obtain keyboard information");
1046 printf("kbd%d\n", info
.kb_index
);
1047 printf(" %.*s%d, type:%s (%d)\n",
1048 (int)sizeof(info
.kb_name
), info
.kb_name
, info
.kb_unit
,
1049 get_kbd_type_name(info
.kb_type
), info
.kb_type
);
1051 if (ioctl(0, CONS_RELKBD
, 0) == -1)
1052 warn("unable to release the keyboard");
1059 fprintf(stderr
, "%s\n%s\n%s\n",
1060 "usage: kbdcontrol [-dFKix] [-b duration.pitch | [quiet.]belltype]",
1061 " [-r delay.repeat | speed] [-l mapfile] [-f # string]",
1062 " [-h size] [-k device] [-L mapfile]");
1068 main(int argc
, char **argv
)
1072 while((opt
= getopt(argc
, argv
, "b:df:h:iKk:Fl:L:r:x")) != -1)
1075 set_bell_values(optarg
);
1081 load_keymap(optarg
, 0);
1084 load_keymap(optarg
, 1);
1087 set_functionkey(optarg
,
1088 nextarg(argc
, argv
, &optind
, 'f'));
1091 load_default_functionkeys();
1094 set_history(optarg
);
1103 set_keyboard(optarg
);
1106 set_keyrates(optarg
);
1114 if ((optind
!= argc
) || (argc
== 1))