HAMMER 58B/Many: Revamp ioctls, add non-monotonic timestamps, mirroring
[dragonfly.git] / usr.sbin / kbdcontrol / kbdcontrol.c
blob299f56ca1cf634deb30fa32189487f5b81767e11
1 /*-
2 * Copyright (c) 1994-1995 Søren Schmidt
3 * All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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 $
32 #include <ctype.h>
33 #include <err.h>
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #include <unistd.h>
38 #include <fcntl.h>
39 #include <machine/console.h>
40 #include "path.h"
41 #include "lex.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",
53 "dcar",
56 char acc_names_u[15][5] = {
57 "DGRA", "DACU", "DCIR", "DTIL", "DMAC", "DBRE", "DDOT",
58 "DUML", "DSLA", "DRIN", "DCED", "DAPO", "DDAC", "DOGO",
59 "DCAR",
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));
96 int hex = 0;
97 int number;
98 char letter;
99 int token;
101 int get_accent_definition_line(accentmap_t *);
102 int get_key_definition_line(keymap_t *);
103 void usage(void);
105 char *
106 nextarg(int ac, char **av, int *indp, int oc)
108 if (*indp < ac)
109 return(av[(*indp)++]);
110 warnx("option requires two arguments -- %c", oc);
111 usage();
112 return("");
116 char *
117 mkfullname(const char *s1, const char *s2, const char *s3)
119 static char *buf = NULL;
120 static int bufl = 0;
121 int f;
123 f = strlen(s1) + strlen(s2) + strlen(s3) + 1;
124 if (f > bufl) {
125 if (buf)
126 buf = (char *)realloc(buf, f);
127 else
128 buf = (char *)malloc(f);
130 if (!buf) {
131 bufl = 0;
132 return(NULL);
135 bufl = f;
136 strcpy(buf, s1);
137 strcat(buf, s2);
138 strcat(buf, s3);
139 return(buf);
144 get_entry(void)
146 switch ((token = yylex())) {
147 case TNOP:
148 return NOP | 0x100;
149 case TLSH:
150 return LSH | 0x100;
151 case TRSH:
152 return RSH | 0x100;
153 case TCLK:
154 return CLK | 0x100;
155 case TNLK:
156 return NLK | 0x100;
157 case TSLK:
158 return SLK | 0x100;
159 case TBTAB:
160 return BTAB | 0x100;
161 case TLALT:
162 return LALT | 0x100;
163 case TLCTR:
164 return LCTR | 0x100;
165 case TNEXT:
166 return NEXT | 0x100;
167 case TPREV:
168 return PREV | 0x100;
169 case TRCTR:
170 return RCTR | 0x100;
171 case TRALT:
172 return RALT | 0x100;
173 case TALK:
174 return ALK | 0x100;
175 case TASH:
176 return ASH | 0x100;
177 case TMETA:
178 return META | 0x100;
179 case TRBT:
180 return RBT | 0x100;
181 case TDBG:
182 return DBG | 0x100;
183 case TSUSP:
184 return SUSP | 0x100;
185 case TSPSC:
186 return SPSC | 0x100;
187 case TPANIC:
188 return PNC | 0x100;
189 case TLSHA:
190 return LSHA | 0x100;
191 case TRSHA:
192 return RSHA | 0x100;
193 case TLCTRA:
194 return LCTRA | 0x100;
195 case TRCTRA:
196 return RCTRA | 0x100;
197 case TLALTA:
198 return LALTA | 0x100;
199 case TRALTA:
200 return RALTA | 0x100;
201 case THALT:
202 return HALT | 0x100;
203 case TPDWN:
204 return PDWN | 0x100;
205 case TACC:
206 if (ACC(number) > L_ACC)
207 return -1;
208 return ACC(number) | 0x100;
209 case TFUNC:
210 if (F(number) > L_FN)
211 return -1;
212 return F(number) | 0x100;
213 case TSCRN:
214 if (S(number) > L_SCR)
215 return -1;
216 return S(number) | 0x100;
217 case TLET:
218 return (unsigned char)letter;
219 case TNUM:
220 if (number < 0 || number > 255)
221 return -1;
222 return number;
223 default:
224 return -1;
228 static int
229 get_definition_line(FILE *fd, keymap_t *keymap, accentmap_t *accentmap)
231 int c;
233 yyin = fd;
235 if (token < 0)
236 token = yylex();
237 switch (token) {
238 case TNUM:
239 c = get_key_definition_line(keymap);
240 if (c < 0)
241 errx(1, "invalid key definition");
242 if (c > keymap->n_keys)
243 keymap->n_keys = c;
244 break;
245 case TACC:
246 c = get_accent_definition_line(accentmap);
247 if (c < 0)
248 errx(1, "invalid accent key definition");
249 if (c > accentmap->n_accs)
250 accentmap->n_accs = c;
251 break;
252 case 0:
253 /* EOF */
254 return -1;
255 default:
256 errx(1, "illegal definition line");
258 return c;
262 get_key_definition_line(keymap_t *map)
264 int i, def, scancode;
266 /* check scancode number */
267 if (number < 0 || number >= NUM_KEYS)
268 return -1;
269 scancode = number;
271 /* get key definitions */
272 map->key[scancode].spcl = 0;
273 for (i=0; i<NUM_STATES; i++) {
274 if ((def = get_entry()) == -1)
275 return -1;
276 if (def & 0x100)
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)
282 return -1;
283 map->key[scancode].flgs = number;
284 token = yylex();
285 return (scancode + 1);
289 get_accent_definition_line(accentmap_t *map)
291 int accent;
292 int c1, c2;
293 int i;
295 if (ACC(number) < F_ACC || ACC(number) > L_ACC)
296 /* number out of range */
297 return -1;
298 accent = number;
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())) {
305 case TLET:
306 map->acc[accent].accchar = letter;
307 break;
308 case TNUM:
309 map->acc[accent].accchar = number;
310 break;
311 default:
312 return -1;
315 for (i = 0; (token = yylex()) == '(';) {
316 switch ((token = yylex())) {
317 case TLET:
318 c1 = letter;
319 break;
320 case TNUM:
321 c1 = number;
322 break;
323 default:
324 return -1;
326 switch ((token = yylex())) {
327 case TLET:
328 c2 = letter;
329 break;
330 case TNUM:
331 c2 = number;
332 break;
333 default:
334 return -1;
336 if ((token = yylex()) != ')')
337 return -1;
338 if (i >= NUM_ACCENTCHARS) {
339 warnx("too many accented characters, ignored");
340 continue;
342 map->acc[accent].map[i][0] = c1;
343 map->acc[accent].map[i][1] = c2;
344 ++i;
346 return (accent + 1);
349 void
350 print_entry(FILE *fp, int value)
352 int val = value & 0xFF;
354 switch (value) {
355 case NOP | 0x100:
356 fprintf(fp, " nop ");
357 break;
358 case LSH | 0x100:
359 fprintf(fp, " lshift");
360 break;
361 case RSH | 0x100:
362 fprintf(fp, " rshift");
363 break;
364 case CLK | 0x100:
365 fprintf(fp, " clock ");
366 break;
367 case NLK | 0x100:
368 fprintf(fp, " nlock ");
369 break;
370 case SLK | 0x100:
371 fprintf(fp, " slock ");
372 break;
373 case BTAB | 0x100:
374 fprintf(fp, " btab ");
375 break;
376 case LALT | 0x100:
377 fprintf(fp, " lalt ");
378 break;
379 case LCTR | 0x100:
380 fprintf(fp, " lctrl ");
381 break;
382 case NEXT | 0x100:
383 fprintf(fp, " nscr ");
384 break;
385 case PREV | 0x100:
386 fprintf(fp, " pscr ");
387 break;
388 case RCTR | 0x100:
389 fprintf(fp, " rctrl ");
390 break;
391 case RALT | 0x100:
392 fprintf(fp, " ralt ");
393 break;
394 case ALK | 0x100:
395 fprintf(fp, " alock ");
396 break;
397 case ASH | 0x100:
398 fprintf(fp, " ashift");
399 break;
400 case META | 0x100:
401 fprintf(fp, " meta ");
402 break;
403 case RBT | 0x100:
404 fprintf(fp, " boot ");
405 break;
406 case DBG | 0x100:
407 fprintf(fp, " debug ");
408 break;
409 case SUSP | 0x100:
410 fprintf(fp, " susp ");
411 break;
412 case SPSC | 0x100:
413 fprintf(fp, " saver ");
414 break;
415 case PNC | 0x100:
416 fprintf(fp, " panic ");
417 break;
418 case LSHA | 0x100:
419 fprintf(fp, " lshifta");
420 break;
421 case RSHA | 0x100:
422 fprintf(fp, " rshifta");
423 break;
424 case LCTRA | 0x100:
425 fprintf(fp, " lctrla");
426 break;
427 case RCTRA | 0x100:
428 fprintf(fp, " rctrla");
429 break;
430 case LALTA | 0x100:
431 fprintf(fp, " lalta ");
432 break;
433 case RALTA | 0x100:
434 fprintf(fp, " ralta ");
435 break;
436 case HALT | 0x100:
437 fprintf(fp, " halt ");
438 break;
439 case PDWN | 0x100:
440 fprintf(fp, " pdwn ");
441 break;
442 default:
443 if (value & 0x100) {
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]);
450 else if (hex)
451 fprintf(fp, " 0x%02x ", val);
452 else
453 fprintf(fp, " %3d ", val);
455 else {
456 if (val < ' ')
457 fprintf(fp, " %s ", ctrl_names[val]);
458 else if (val == 127)
459 fprintf(fp, " del ");
460 else if (isascii(val) && isprint(val))
461 fprintf(fp, " '%c' ", val);
462 else if (hex)
463 fprintf(fp, " 0x%02x ", val);
464 else
465 fprintf(fp, " %3d ", val);
471 void
472 print_key_definition_line(FILE *fp, int scancode, struct keyent_t *key)
474 int i;
476 /* print scancode number */
477 if (hex)
478 fprintf(fp, " 0x%02x ", scancode);
479 else
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);
486 else
487 print_entry(fp, key->map[i]);
490 /* print lock state key def */
491 switch (key->flgs) {
492 case 0:
493 fprintf(fp, " O\n");
494 break;
495 case 1:
496 fprintf(fp, " C\n");
497 break;
498 case 2:
499 fprintf(fp, " N\n");
500 break;
501 case 3:
502 fprintf(fp, " B\n");
503 break;
507 void
508 print_accent_definition_line(FILE *fp, int accent, struct acc_t *key)
510 int c;
511 int i;
513 if (key->accchar == 0)
514 return;
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);
520 else if (hex)
521 fprintf(fp, "0x%02x ", key->accchar);
522 else
523 fprintf(fp, "%03d ", key->accchar);
525 for (i = 0; i < NUM_ACCENTCHARS; ++i) {
526 c = key->map[i][0];
527 if (c == 0)
528 break;
529 if ((i > 0) && ((i % 4) == 0))
530 fprintf(fp, "\n ");
531 if (isascii(c) && isprint(c))
532 fprintf(fp, "( '%c' ", c);
533 else if (hex)
534 fprintf(fp, "(0x%02x ", c);
535 else
536 fprintf(fp, "( %03d ", c);
537 c = key->map[i][1];
538 if (isascii(c) && isprint(c))
539 fprintf(fp, "'%c' ) ", c);
540 else if (hex)
541 fprintf(fp, "0x%02x) ", c);
542 else
543 fprintf(fp, "%03d ) ", c);
545 fprintf(fp, "\n");
548 void
549 dump_entry(int value)
551 if (value & 0x100) {
552 value &= 0x00ff;
553 switch (value) {
554 case NOP:
555 printf(" NOP, ");
556 break;
557 case LSH:
558 printf(" LSH, ");
559 break;
560 case RSH:
561 printf(" RSH, ");
562 break;
563 case CLK:
564 printf(" CLK, ");
565 break;
566 case NLK:
567 printf(" NLK, ");
568 break;
569 case SLK:
570 printf(" SLK, ");
571 break;
572 case BTAB:
573 printf(" BTAB, ");
574 break;
575 case LALT:
576 printf(" LALT, ");
577 break;
578 case LCTR:
579 printf(" LCTR, ");
580 break;
581 case NEXT:
582 printf(" NEXT, ");
583 break;
584 case PREV:
585 printf(" PREV, ");
586 break;
587 case RCTR:
588 printf(" RCTR, ");
589 break;
590 case RALT:
591 printf(" RALT, ");
592 break;
593 case ALK:
594 printf(" ALK, ");
595 break;
596 case ASH:
597 printf(" ASH, ");
598 break;
599 case META:
600 printf(" META, ");
601 break;
602 case RBT:
603 printf(" RBT, ");
604 break;
605 case DBG:
606 printf(" DBG, ");
607 break;
608 case SUSP:
609 printf(" SUSP, ");
610 break;
611 case SPSC:
612 printf(" SPSC, ");
613 break;
614 case PNC:
615 printf(" PNC, ");
616 break;
617 case LSHA:
618 printf(" LSHA, ");
619 break;
620 case RSHA:
621 printf(" RSHA, ");
622 break;
623 case LCTRA:
624 printf("LCTRA, ");
625 break;
626 case RCTRA:
627 printf("RCTRA, ");
628 break;
629 case LALTA:
630 printf("LALTA, ");
631 break;
632 case RALTA:
633 printf("RALTA, ");
634 break;
635 case HALT:
636 printf(" HALT, ");
637 break;
638 case PDWN:
639 printf(" PDWN, ");
640 break;
641 default:
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]);
648 else
649 printf(" 0x%02X, ", value);
650 break;
652 } else if (value == '\'') {
653 printf(" '\\'', ");
654 } else if (value == '\\') {
655 printf(" '\\\\', ");
656 } else if (isascii(value) && isprint(value)) {
657 printf(" '%c', ", value);
658 } else {
659 printf(" 0x%02X, ", value);
663 void
664 dump_key_definition(char *name, keymap_t *keymap)
666 int i, j;
668 printf("static keymap_t keymap_%s = { 0x%02x, {\n",
669 name, (unsigned)keymap->n_keys);
670 printf(
671 "/* alt\n"
672 " * scan cntrl alt alt cntrl\n"
673 " * code base shift cntrl shift alt shift cntrl shift spcl flgs\n"
674 " * ---------------------------------------------------------------------------\n"
675 " */\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);
681 else
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);
688 printf("} };\n\n");
691 void
692 dump_accent_definition(char *name, accentmap_t *accentmap)
694 int i, j;
695 int c;
697 printf("static accentmap_t accentmap_%s = { %d",
698 name, accentmap->n_accs);
699 if (accentmap->n_accs <= 0) {
700 printf(" };\n\n");
701 return;
703 printf(", {\n");
704 for (i = 0; i < NUM_DEADKEYS; i++) {
705 printf(" /* %s=%d */\n {", acc_names[i], i);
706 c = accentmap->acc[i].accchar;
707 if (c == '\'')
708 printf(" '\\'', {");
709 else if (c == '\\')
710 printf(" '\\\\', {");
711 else if (isascii(c) && isprint(c))
712 printf(" '%c', {", c);
713 else if (c == 0) {
714 printf(" 0x00 }, \n");
715 continue;
716 } else
717 printf(" 0x%02x, {", c);
718 for (j = 0; j < NUM_ACCENTCHARS; j++) {
719 c = accentmap->acc[i].map[j][0];
720 if (c == 0)
721 break;
722 if ((j > 0) && ((j % 4) == 0))
723 printf("\n\t ");
724 if (isascii(c) && isprint(c))
725 printf(" { '%c',", c);
726 else
727 printf(" { 0x%02x,", c);
728 printf("0x%02x },", accentmap->acc[i].map[j][1]);
730 printf(" }, },\n");
732 printf("} };\n\n");
735 void
736 load_keymap(char *opt, int dumponly)
738 keymap_t keymap;
739 accentmap_t accentmap;
740 FILE *fd;
741 int i;
742 char *name, *cp;
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")))
749 break;
751 if (fd == NULL) {
752 warn("keymap file not found");
753 return;
755 memset(&keymap, 0, sizeof(keymap));
756 memset(&accentmap, 0, sizeof(accentmap));
757 token = -1;
758 while (1) {
759 if (get_definition_line(fd, &keymap, &accentmap) < 0)
760 break;
762 if (dumponly) {
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 = '_';
766 printf("/*\n"
767 " * Automatically generated from %s.\n"
768 " * DO NOT EDIT!\n"
769 " */\n", name);
770 dump_key_definition(opt, &keymap);
771 dump_accent_definition(opt, &accentmap);
772 return;
774 if ((keymap.n_keys > 0) && (ioctl(0, PIO_KEYMAP, &keymap) < 0)) {
775 warn("setting keymap");
776 fclose(fd);
777 return;
779 if ((accentmap.n_accs > 0)
780 && (ioctl(0, PIO_DEADKEYMAP, &accentmap) < 0)) {
781 warn("setting accentmap");
782 fclose(fd);
783 return;
787 void
788 print_keymap(void)
790 keymap_t keymap;
791 accentmap_t accentmap;
792 int i;
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));
798 printf(
799 "# alt\n"
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]);
807 printf("\n");
808 for (i = 0; i < NUM_DEADKEYS; i++)
809 print_accent_definition_line(stdout, i, &accentmap.acc[i]);
814 void
815 load_default_functionkeys(void)
817 fkeyarg_t fkey;
818 int i;
820 for (i=0; i<NUM_FKEYS; i++) {
821 fkey.keynum = 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");
829 void
830 set_functionkey(char *keynumstr, char *string)
832 fkeyarg_t fkey;
834 if (!strcmp(keynumstr, "load") && !strcmp(string, "default")) {
835 load_default_functionkeys();
836 return;
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",
841 NUM_FKEYS);
842 return;
844 if ((fkey.flen = strlen(string)) > MAXFK) {
845 warnx("function key string too long (%d > %d)",
846 fkey.flen, MAXFK);
847 return;
849 strcpy(fkey.keydef, string);
850 fkey.keynum -= 1;
851 if (ioctl(0, SETFKEY, &fkey) < 0)
852 warn("setting function key");
856 void
857 set_bell_values(char *opt)
859 int bell, duration, pitch;
861 bell = 0;
862 if (!strncmp(opt, "quiet.", 6)) {
863 bell = 2;
864 opt += 6;
866 if (!strcmp(opt, "visual"))
867 bell |= 1;
868 else if (!strcmp(opt, "normal"))
869 duration = 5, pitch = 800;
870 else if (!strcmp(opt, "off"))
871 duration = 0, pitch = 0;
872 else {
873 char *v1;
875 bell = 0;
876 duration = strtol(opt, &v1, 0);
877 if ((duration < 0) || (*v1 != '.'))
878 goto badopt;
879 opt = ++v1;
880 pitch = strtol(opt, &v1, 0);
881 if ((pitch < 0) || (*opt == '\0') || (*v1 != '\0')) {
882 badopt:
883 warnx("argument to -b must be duration.pitch or [quiet.]visual|normal|off");
884 return;
886 if (pitch != 0)
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);
897 void
898 set_keyrates(char *opt)
900 int arg[2];
901 int repeat;
902 int delay;
903 int r, d;
905 if (!strcmp(opt, "slow")) {
906 delay = 1000, repeat = 500;
907 d = 3, r = 31;
908 } else if (!strcmp(opt, "normal")) {
909 delay = 500, repeat = 125;
910 d = 1, r = 15;
911 } else if (!strcmp(opt, "fast")) {
912 delay = repeat = 0;
913 d = r = 0;
914 } else {
915 int n;
916 char *v1;
918 delay = strtol(opt, &v1, 0);
919 if ((delay < 0) || (*v1 != '.'))
920 goto badopt;
921 opt = ++v1;
922 repeat = strtol(opt, &v1, 0);
923 if ((repeat < 0) || (*opt == '\0') || (*v1 != '\0')) {
924 badopt:
925 warnx("argument to -r must be delay.repeat or slow|normal|fast");
926 return;
928 for (n = 0; n < ndelays - 1; n++)
929 if (delay <= delays[n])
930 break;
931 d = n;
932 for (n = 0; n < nrepeats - 1; n++)
933 if (repeat <= repeats[n])
934 break;
935 r = n;
938 arg[0] = delay;
939 arg[1] = repeat;
940 if (ioctl(0, KDSETREPEAT, arg)) {
941 if (ioctl(0, KDSETRAD, (d << 5) | r))
942 warn("setting keyboard rate");
947 void
948 set_history(char *opt)
950 int size;
952 size = atoi(opt);
953 if ((*opt == '\0') || size < 0) {
954 warnx("argument must be a positive number");
955 return;
957 if (ioctl(0, CONS_HISTORY, &size) == -1)
958 warn("setting history buffer size");
961 static char *
962 get_kbd_type_name(int type)
964 static struct {
965 int type;
966 char *name;
967 } name_table[] = {
968 { KB_84, "AT 84" },
969 { KB_101, "AT 101/102" },
970 { KB_OTHER, "generic" },
972 int i;
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;
978 return "unknown";
981 void
982 show_kbd_info(void)
984 keyboard_info_t info;
986 if (ioctl(0, KDGKBINFO, &info) == -1) {
987 warn("unable to obtain keyboard information");
988 return;
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);
997 void
998 set_keyboard(char *device)
1000 keyboard_info_t info;
1001 int fd;
1003 fd = open(device, O_RDONLY);
1004 if (fd < 0) {
1005 warn("cannot open %s", device);
1006 return;
1008 if (ioctl(fd, KDGKBINFO, &info) == -1) {
1009 warn("unable to obtain keyboard information");
1010 close(fd);
1011 return;
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);
1019 close(fd);
1020 #if 1
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);
1025 #endif
1027 if (ioctl(0, CONS_SETKBD, info.kb_index) == -1)
1028 warn("unable to set keyboard");
1032 void
1033 release_keyboard(void)
1035 keyboard_info_t info;
1038 * If stdin is not associated with a keyboard, the following ioctl
1039 * will fail.
1041 if (ioctl(0, KDGKBINFO, &info) == -1) {
1042 warn("unable to obtain keyboard information");
1043 return;
1045 #if 1
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);
1050 #endif
1051 if (ioctl(0, CONS_RELKBD, 0) == -1)
1052 warn("unable to release the keyboard");
1056 void
1057 usage(void)
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]");
1063 exit(1);
1068 main(int argc, char **argv)
1070 int opt;
1072 while((opt = getopt(argc, argv, "b:df:h:iKk:Fl:L:r:x")) != -1)
1073 switch(opt) {
1074 case 'b':
1075 set_bell_values(optarg);
1076 break;
1077 case 'd':
1078 print_keymap();
1079 break;
1080 case 'l':
1081 load_keymap(optarg, 0);
1082 break;
1083 case 'L':
1084 load_keymap(optarg, 1);
1085 break;
1086 case 'f':
1087 set_functionkey(optarg,
1088 nextarg(argc, argv, &optind, 'f'));
1089 break;
1090 case 'F':
1091 load_default_functionkeys();
1092 break;
1093 case 'h':
1094 set_history(optarg);
1095 break;
1096 case 'i':
1097 show_kbd_info();
1098 break;
1099 case 'K':
1100 release_keyboard();
1101 break;
1102 case 'k':
1103 set_keyboard(optarg);
1104 break;
1105 case 'r':
1106 set_keyrates(optarg);
1107 break;
1108 case 'x':
1109 hex = 1;
1110 break;
1111 default:
1112 usage();
1114 if ((optind != argc) || (argc == 1))
1115 usage();
1116 exit(0);