1 /*- 2 * Copyright (c) 1994-1995 S�ren Schmidt 3 * All rights reserved. 4 * 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 without specific prior written permission 16 * 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. 27 * 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 $ 30 */ 31 #include <sys/kbio.h> 32 33 #include <ctype.h> 34 #include <err.h> 35 #include <stdio.h> 36 #include <stdlib.h> 37 #include <string.h> 38 #include <unistd.h> 39 #include <fcntl.h> 40 #include <machine/console.h> 41 #include "path.h" 42 #include "lex.h" 43 44 char ctrl_names[32][4] = { 45 "nul", "soh", "stx", "etx", "eot", "enq", "ack", "bel", 46 "bs ", "ht ", "nl ", "vt ", "ff ", "cr ", "so ", "si ", 47 "dle", "dc1", "dc2", "dc3", "dc4", "nak", "syn", "etb", 48 "can", "em ", "sub", "esc", "fs ", "gs ", "rs ", "us " 49 }; 50 51 char acc_names[15][5] = { 52 "dgra", "dacu", "dcir", "dtil", "dmac", "dbre", "ddot", 53 "duml", "dsla", "drin", "dced", "dapo", "ddac", "dogo", 54 "dcar", 55 }; 56 57 char acc_names_u[15][5] = { 58 "DGRA", "DACU", "DCIR", "DTIL", "DMAC", "DBRE", "DDOT", 59 "DUML", "DSLA", "DRIN", "DCED", "DAPO", "DDAC", "DOGO", 60 "DCAR", 61 }; 62 63 char fkey_table[96][MAXFK] = { 64 /* 01-04 */ "\033[M", "\033[N", "\033[O", "\033[P", 65 /* 05-08 */ "\033[Q", "\033[R", "\033[S", "\033[T", 66 /* 09-12 */ "\033[U", "\033[V", "\033[W", "\033[X", 67 /* 13-16 */ "\033[Y", "\033[Z", "\033[a", "\033[b", 68 /* 17-20 */ "\033[c", "\033[d", "\033[e", "\033[f", 69 /* 21-24 */ "\033[g", "\033[h", "\033[i", "\033[j", 70 /* 25-28 */ "\033[k", "\033[l", "\033[m", "\033[n", 71 /* 29-32 */ "\033[o", "\033[p", "\033[q", "\033[r", 72 /* 33-36 */ "\033[s", "\033[t", "\033[u", "\033[v", 73 /* 37-40 */ "\033[w", "\033[x", "\033[y", "\033[z", 74 /* 41-44 */ "\033[@", "\033[[", "\033[\\","\033[]", 75 /* 45-48 */ "\033[^", "\033[_", "\033[`", "\033[{", 76 /* 49-52 */ "\033[H", "\033[A", "\033[I", "-" , 77 /* 53-56 */ "\033[D", "\033[E", "\033[C", "+" , 78 /* 57-60 */ "\033[F", "\033[B", "\033[G", "\033[L", 79 /* 61-64 */ "\177", "\033[J", "\033[~", "\033[}", 80 /* 65-68 */ "" , "" , "" , "" , 81 /* 69-72 */ "" , "" , "" , "" , 82 /* 73-76 */ "" , "" , "" , "" , 83 /* 77-80 */ "" , "" , "" , "" , 84 /* 81-84 */ "" , "" , "" , "" , 85 /* 85-88 */ "" , "" , "" , "" , 86 /* 89-92 */ "" , "" , "" , "" , 87 /* 93-96 */ "" , "" , "" , "" , 88 }; 89 90 const int delays[] = {250, 500, 750, 1000}; 91 const int repeats[] = { 34, 38, 42, 46, 50, 55, 59, 63, 92 68, 76, 84, 92, 100, 110, 118, 126, 93 136, 152, 168, 184, 200, 220, 236, 252, 94 272, 304, 336, 368, 400, 440, 472, 504}; 95 const int ndelays = (sizeof(delays) / sizeof(int)); 96 const int nrepeats = (sizeof(repeats) / sizeof(int)); 97 int hex = 0; 98 int number; 99 char letter; 100 int token; 101 102 int get_accent_definition_line(accentmap_t *); 103 void mux_keyboard(u_int op, char *kbd); 104 int get_key_definition_line(keymap_t *); 105 void usage(void); 106 107 char * 108 nextarg(int ac, char **av, int *indp, int oc) 109 { 110 if (*indp < ac) 111 return(av[(*indp)++]); 112 warnx("option requires two arguments -- %c", oc); 113 usage(); 114 return(""); 115 } 116 117 118 char * 119 mkfullname(const char *s1, const char *s2, const char *s3) 120 { 121 static char *buf = NULL; 122 static int bufl = 0; 123 int f; 124 125 f = strlen(s1) + strlen(s2) + strlen(s3) + 1; 126 if (f > bufl) { 127 if (buf) 128 buf = (char *)realloc(buf, f); 129 else 130 buf = (char *)malloc(f); 131 } 132 if (!buf) { 133 bufl = 0; 134 return(NULL); 135 } 136 137 bufl = f; 138 strcpy(buf, s1); 139 strcat(buf, s2); 140 strcat(buf, s3); 141 return(buf); 142 } 143 144 145 int 146 get_entry(void) 147 { 148 switch ((token = yylex())) { 149 case TNOP: 150 return NOP | 0x100; 151 case TLSH: 152 return LSH | 0x100; 153 case TRSH: 154 return RSH | 0x100; 155 case TCLK: 156 return CLK | 0x100; 157 case TNLK: 158 return NLK | 0x100; 159 case TSLK: 160 return SLK | 0x100; 161 case TBTAB: 162 return BTAB | 0x100; 163 case TLALT: 164 return LALT | 0x100; 165 case TLCTR: 166 return LCTR | 0x100; 167 case TNEXT: 168 return NEXT | 0x100; 169 case TPREV: 170 return PREV | 0x100; 171 case TRCTR: 172 return RCTR | 0x100; 173 case TRALT: 174 return RALT | 0x100; 175 case TALK: 176 return ALK | 0x100; 177 case TASH: 178 return ASH | 0x100; 179 case TMETA: 180 return META | 0x100; 181 case TRBT: 182 return RBT | 0x100; 183 case TDBG: 184 return DBG | 0x100; 185 case TSUSP: 186 return SUSP | 0x100; 187 case TSPSC: 188 return SPSC | 0x100; 189 case TPANIC: 190 return PNC | 0x100; 191 case TLSHA: 192 return LSHA | 0x100; 193 case TRSHA: 194 return RSHA | 0x100; 195 case TLCTRA: 196 return LCTRA | 0x100; 197 case TRCTRA: 198 return RCTRA | 0x100; 199 case TLALTA: 200 return LALTA | 0x100; 201 case TRALTA: 202 return RALTA | 0x100; 203 case THALT: 204 return HALT | 0x100; 205 case TPDWN: 206 return PDWN | 0x100; 207 case TACC: 208 if (ACC(number) > L_ACC) 209 return -1; 210 return ACC(number) | 0x100; 211 case TFUNC: 212 if (F(number) > L_FN) 213 return -1; 214 return F(number) | 0x100; 215 case TSCRN: 216 if (S(number) > L_SCR) 217 return -1; 218 return S(number) | 0x100; 219 case TLET: 220 return (unsigned char)letter; 221 case TNUM: 222 if (number < 0 || number > 255) 223 return -1; 224 return number; 225 default: 226 return -1; 227 } 228 } 229 230 static int 231 get_definition_line(FILE *fd, keymap_t *keymap, accentmap_t *accentmap) 232 { 233 int c; 234 235 yyin = fd; 236 237 if (token < 0) 238 token = yylex(); 239 switch (token) { 240 case TNUM: 241 c = get_key_definition_line(keymap); 242 if (c < 0) 243 errx(1, "invalid key definition"); 244 if (c > keymap->n_keys) 245 keymap->n_keys = c; 246 break; 247 case TACC: 248 c = get_accent_definition_line(accentmap); 249 if (c < 0) 250 errx(1, "invalid accent key definition"); 251 if (c > accentmap->n_accs) 252 accentmap->n_accs = c; 253 break; 254 case 0: 255 /* EOF */ 256 return -1; 257 default: 258 errx(1, "illegal definition line"); 259 } 260 return c; 261 } 262 263 int 264 get_key_definition_line(keymap_t *map) 265 { 266 int i, def, scancode; 267 268 /* check scancode number */ 269 if (number < 0 || number >= NUM_KEYS) 270 return -1; 271 scancode = number; 272 273 /* get key definitions */ 274 map->key[scancode].spcl = 0; 275 for (i=0; i<NUM_STATES; i++) { 276 if ((def = get_entry()) == -1) 277 return -1; 278 if (def & 0x100) 279 map->key[scancode].spcl |= (0x80 >> i); 280 map->key[scancode].map[i] = def & 0xFF; 281 } 282 /* get lock state key def */ 283 if ((token = yylex()) != TFLAG) 284 return -1; 285 map->key[scancode].flgs = number; 286 token = yylex(); 287 return (scancode + 1); 288 } 289 290 int 291 get_accent_definition_line(accentmap_t *map) 292 { 293 int accent; 294 int c1, c2; 295 int i; 296 297 if (ACC(number) < F_ACC || ACC(number) > L_ACC) 298 /* number out of range */ 299 return -1; 300 accent = number; 301 if (map->acc[accent].accchar != 0) { 302 /* this entry has already been defined before! */ 303 errx(1, "duplicated accent key definition"); 304 } 305 306 switch ((token = yylex())) { 307 case TLET: 308 map->acc[accent].accchar = letter; 309 break; 310 case TNUM: 311 map->acc[accent].accchar = number; 312 break; 313 default: 314 return -1; 315 } 316 317 for (i = 0; (token = yylex()) == '(';) { 318 switch ((token = yylex())) { 319 case TLET: 320 c1 = letter; 321 break; 322 case TNUM: 323 c1 = number; 324 break; 325 default: 326 return -1; 327 } 328 switch ((token = yylex())) { 329 case TLET: 330 c2 = letter; 331 break; 332 case TNUM: 333 c2 = number; 334 break; 335 default: 336 return -1; 337 } 338 if ((token = yylex()) != ')') 339 return -1; 340 if (i >= NUM_ACCENTCHARS) { 341 warnx("too many accented characters, ignored"); 342 continue; 343 } 344 map->acc[accent].map[i][0] = c1; 345 map->acc[accent].map[i][1] = c2; 346 ++i; 347 } 348 return (accent + 1); 349 } 350 351 void 352 print_entry(FILE *fp, int value) 353 { 354 int val = value & 0xFF; 355 356 switch (value) { 357 case NOP | 0x100: 358 fprintf(fp, " nop "); 359 break; 360 case LSH | 0x100: 361 fprintf(fp, " lshift"); 362 break; 363 case RSH | 0x100: 364 fprintf(fp, " rshift"); 365 break; 366 case CLK | 0x100: 367 fprintf(fp, " clock "); 368 break; 369 case NLK | 0x100: 370 fprintf(fp, " nlock "); 371 break; 372 case SLK | 0x100: 373 fprintf(fp, " slock "); 374 break; 375 case BTAB | 0x100: 376 fprintf(fp, " btab "); 377 break; 378 case LALT | 0x100: 379 fprintf(fp, " lalt "); 380 break; 381 case LCTR | 0x100: 382 fprintf(fp, " lctrl "); 383 break; 384 case NEXT | 0x100: 385 fprintf(fp, " nscr "); 386 break; 387 case PREV | 0x100: 388 fprintf(fp, " pscr "); 389 break; 390 case RCTR | 0x100: 391 fprintf(fp, " rctrl "); 392 break; 393 case RALT | 0x100: 394 fprintf(fp, " ralt "); 395 break; 396 case ALK | 0x100: 397 fprintf(fp, " alock "); 398 break; 399 case ASH | 0x100: 400 fprintf(fp, " ashift"); 401 break; 402 case META | 0x100: 403 fprintf(fp, " meta "); 404 break; 405 case RBT | 0x100: 406 fprintf(fp, " boot "); 407 break; 408 case DBG | 0x100: 409 fprintf(fp, " debug "); 410 break; 411 case SUSP | 0x100: 412 fprintf(fp, " susp "); 413 break; 414 case SPSC | 0x100: 415 fprintf(fp, " saver "); 416 break; 417 case PNC | 0x100: 418 fprintf(fp, " panic "); 419 break; 420 case LSHA | 0x100: 421 fprintf(fp, " lshifta"); 422 break; 423 case RSHA | 0x100: 424 fprintf(fp, " rshifta"); 425 break; 426 case LCTRA | 0x100: 427 fprintf(fp, " lctrla"); 428 break; 429 case RCTRA | 0x100: 430 fprintf(fp, " rctrla"); 431 break; 432 case LALTA | 0x100: 433 fprintf(fp, " lalta "); 434 break; 435 case RALTA | 0x100: 436 fprintf(fp, " ralta "); 437 break; 438 case HALT | 0x100: 439 fprintf(fp, " halt "); 440 break; 441 case PDWN | 0x100: 442 fprintf(fp, " pdwn "); 443 break; 444 default: 445 if (value & 0x100) { 446 if (val >= F_FN && val <= L_FN) 447 fprintf(fp, " fkey%02d", val - F_FN + 1); 448 else if (val >= F_SCR && val <= L_SCR) 449 fprintf(fp, " scr%02d ", val - F_SCR + 1); 450 else if (val >= F_ACC && val <= L_ACC) 451 fprintf(fp, " %-6s", acc_names[val - F_ACC]); 452 else if (hex) 453 fprintf(fp, " 0x%02x ", val); 454 else 455 fprintf(fp, " %3d ", val); 456 } 457 else { 458 if (val < ' ') 459 fprintf(fp, " %s ", ctrl_names[val]); 460 else if (val == 127) 461 fprintf(fp, " del "); 462 else if (isascii(val) && isprint(val)) 463 fprintf(fp, " '%c' ", val); 464 else if (hex) 465 fprintf(fp, " 0x%02x ", val); 466 else 467 fprintf(fp, " %3d ", val); 468 } 469 } 470 } 471 472 473 void 474 print_key_definition_line(FILE *fp, int scancode, struct keyent_t *key) 475 { 476 int i; 477 478 /* print scancode number */ 479 if (hex) 480 fprintf(fp, " 0x%02x ", scancode); 481 else 482 fprintf(fp, " %03d ", scancode); 483 484 /* print key definitions */ 485 for (i=0; i<NUM_STATES; i++) { 486 if (key->spcl & (0x80 >> i)) 487 print_entry(fp, key->map[i] | 0x100); 488 else 489 print_entry(fp, key->map[i]); 490 } 491 492 /* print lock state key def */ 493 switch (key->flgs) { 494 case 0: 495 fprintf(fp, " O\n"); 496 break; 497 case 1: 498 fprintf(fp, " C\n"); 499 break; 500 case 2: 501 fprintf(fp, " N\n"); 502 break; 503 case 3: 504 fprintf(fp, " B\n"); 505 break; 506 } 507 } 508 509 void 510 print_accent_definition_line(FILE *fp, int accent, struct acc_t *key) 511 { 512 int c; 513 int i; 514 515 if (key->accchar == 0) 516 return; 517 518 /* print accent number */ 519 fprintf(fp, " %-6s", acc_names[accent]); 520 if (isascii(key->accchar) && isprint(key->accchar)) 521 fprintf(fp, "'%c' ", key->accchar); 522 else if (hex) 523 fprintf(fp, "0x%02x ", key->accchar); 524 else 525 fprintf(fp, "%03d ", key->accchar); 526 527 for (i = 0; i < NUM_ACCENTCHARS; ++i) { 528 c = key->map[i][0]; 529 if (c == 0) 530 break; 531 if ((i > 0) && ((i % 4) == 0)) 532 fprintf(fp, "\n "); 533 if (isascii(c) && isprint(c)) 534 fprintf(fp, "( '%c' ", c); 535 else if (hex) 536 fprintf(fp, "(0x%02x ", c); 537 else 538 fprintf(fp, "( %03d ", c); 539 c = key->map[i][1]; 540 if (isascii(c) && isprint(c)) 541 fprintf(fp, "'%c' ) ", c); 542 else if (hex) 543 fprintf(fp, "0x%02x) ", c); 544 else 545 fprintf(fp, "%03d ) ", c); 546 } 547 fprintf(fp, "\n"); 548 } 549 550 void 551 dump_entry(int value) 552 { 553 if (value & 0x100) { 554 value &= 0x00ff; 555 switch (value) { 556 case NOP: 557 printf(" NOP, "); 558 break; 559 case LSH: 560 printf(" LSH, "); 561 break; 562 case RSH: 563 printf(" RSH, "); 564 break; 565 case CLK: 566 printf(" CLK, "); 567 break; 568 case NLK: 569 printf(" NLK, "); 570 break; 571 case SLK: 572 printf(" SLK, "); 573 break; 574 case BTAB: 575 printf(" BTAB, "); 576 break; 577 case LALT: 578 printf(" LALT, "); 579 break; 580 case LCTR: 581 printf(" LCTR, "); 582 break; 583 case NEXT: 584 printf(" NEXT, "); 585 break; 586 case PREV: 587 printf(" PREV, "); 588 break; 589 case RCTR: 590 printf(" RCTR, "); 591 break; 592 case RALT: 593 printf(" RALT, "); 594 break; 595 case ALK: 596 printf(" ALK, "); 597 break; 598 case ASH: 599 printf(" ASH, "); 600 break; 601 case META: 602 printf(" META, "); 603 break; 604 case RBT: 605 printf(" RBT, "); 606 break; 607 case DBG: 608 printf(" DBG, "); 609 break; 610 case SUSP: 611 printf(" SUSP, "); 612 break; 613 case SPSC: 614 printf(" SPSC, "); 615 break; 616 case PNC: 617 printf(" PNC, "); 618 break; 619 case LSHA: 620 printf(" LSHA, "); 621 break; 622 case RSHA: 623 printf(" RSHA, "); 624 break; 625 case LCTRA: 626 printf("LCTRA, "); 627 break; 628 case RCTRA: 629 printf("RCTRA, "); 630 break; 631 case LALTA: 632 printf("LALTA, "); 633 break; 634 case RALTA: 635 printf("RALTA, "); 636 break; 637 case HALT: 638 printf(" HALT, "); 639 break; 640 case PDWN: 641 printf(" PDWN, "); 642 break; 643 default: 644 if (value >= F_FN && value <= L_FN) 645 printf(" F(%2d),", value - F_FN + 1); 646 else if (value >= F_SCR && value <= L_SCR) 647 printf(" S(%2d),", value - F_SCR + 1); 648 else if (value >= F_ACC && value <= L_ACC) 649 printf(" %-4s, ", acc_names_u[value - F_ACC]); 650 else 651 printf(" 0x%02X, ", value); 652 break; 653 } 654 } else if (value == '\'') { 655 printf(" '\\'', "); 656 } else if (value == '\\') { 657 printf(" '\\\\', "); 658 } else if (isascii(value) && isprint(value)) { 659 printf(" '%c', ", value); 660 } else { 661 printf(" 0x%02X, ", value); 662 } 663 } 664 665 void 666 dump_key_definition(char *name, keymap_t *keymap) 667 { 668 int i, j; 669 670 printf("static keymap_t keymap_%s = { 0x%02x, {\n", 671 name, (unsigned)keymap->n_keys); 672 printf( 673 "/* alt\n" 674 " * scan cntrl alt alt cntrl\n" 675 " * code base shift cntrl shift alt shift cntrl shift spcl flgs\n" 676 " * ---------------------------------------------------------------------------\n" 677 " */\n"); 678 for (i = 0; i < keymap->n_keys; i++) { 679 printf("/*%02x*/{{", i); 680 for (j = 0; j < NUM_STATES; j++) { 681 if (keymap->key[i].spcl & (0x80 >> j)) 682 dump_entry(keymap->key[i].map[j] | 0x100); 683 else 684 dump_entry(keymap->key[i].map[j]); 685 } 686 printf("}, 0x%02X,0x%02X },\n", 687 (unsigned)keymap->key[i].spcl, 688 (unsigned)keymap->key[i].flgs); 689 } 690 printf("} };\n\n"); 691 } 692 693 void 694 dump_accent_definition(char *name, accentmap_t *accentmap) 695 { 696 int i, j; 697 int c; 698 699 printf("static accentmap_t accentmap_%s = { %d", 700 name, accentmap->n_accs); 701 if (accentmap->n_accs <= 0) { 702 printf(" };\n\n"); 703 return; 704 } 705 printf(", {\n"); 706 for (i = 0; i < NUM_DEADKEYS; i++) { 707 printf(" /* %s=%d */\n {", acc_names[i], i); 708 c = accentmap->acc[i].accchar; 709 if (c == '\'') 710 printf(" '\\'', {"); 711 else if (c == '\\') 712 printf(" '\\\\', {"); 713 else if (isascii(c) && isprint(c)) 714 printf(" '%c', {", c); 715 else if (c == 0) { 716 printf(" 0x00 }, \n"); 717 continue; 718 } else 719 printf(" 0x%02x, {", c); 720 for (j = 0; j < NUM_ACCENTCHARS; j++) { 721 c = accentmap->acc[i].map[j][0]; 722 if (c == 0) 723 break; 724 if ((j > 0) && ((j % 4) == 0)) 725 printf("\n\t "); 726 if (isascii(c) && isprint(c)) 727 printf(" { '%c',", c); 728 else 729 printf(" { 0x%02x,", c); 730 printf("0x%02x },", accentmap->acc[i].map[j][1]); 731 } 732 printf(" }, },\n"); 733 } 734 printf("} };\n\n"); 735 } 736 737 void 738 load_keymap(char *opt, int dumponly) 739 { 740 keymap_t keymap; 741 accentmap_t accentmap; 742 FILE *fd; 743 int i; 744 char *name, *cp; 745 char *prefix[] = {"", "", KEYMAP_PATH, KEYMAP_PATH, NULL}; 746 char *postfix[] = {"", ".kbd", "", ".kbd"}; 747 748 for (i=0; prefix[i]; i++) { 749 name = mkfullname(prefix[i], opt, postfix[i]); 750 if ((fd = fopen(name, "r"))) 751 break; 752 } 753 if (fd == NULL) { 754 warn("keymap file not found"); 755 return; 756 } 757 memset(&keymap, 0, sizeof(keymap)); 758 memset(&accentmap, 0, sizeof(accentmap)); 759 token = -1; 760 while (1) { 761 if (get_definition_line(fd, &keymap, &accentmap) < 0) 762 break; 763 } 764 if (dumponly) { 765 /* fix up the filename to make it a valid C identifier */ 766 for (cp = opt; *cp; cp++) 767 if (!isalpha(*cp) && !isdigit(*cp)) *cp = '_'; 768 printf("/*\n" 769 " * Automatically generated from %s.\n" 770 " * DO NOT EDIT!\n" 771 " */\n", name); 772 dump_key_definition(opt, &keymap); 773 dump_accent_definition(opt, &accentmap); 774 return; 775 } 776 if ((keymap.n_keys > 0) && (ioctl(0, PIO_KEYMAP, &keymap) < 0)) { 777 warn("setting keymap"); 778 fclose(fd); 779 return; 780 } 781 if ((accentmap.n_accs > 0) 782 && (ioctl(0, PIO_DEADKEYMAP, &accentmap) < 0)) { 783 warn("setting accentmap"); 784 fclose(fd); 785 return; 786 } 787 } 788 789 void 790 print_keymap(void) 791 { 792 keymap_t keymap; 793 accentmap_t accentmap; 794 int i; 795 796 if (ioctl(0, GIO_KEYMAP, &keymap) < 0) 797 err(1, "getting keymap"); 798 if (ioctl(0, GIO_DEADKEYMAP, &accentmap) < 0) 799 memset(&accentmap, 0, sizeof(accentmap)); 800 printf( 801 "# alt\n" 802 "# scan cntrl alt alt cntrl lock\n" 803 "# code base shift cntrl shift alt shift cntrl shift state\n" 804 "# ------------------------------------------------------------------\n" 805 ); 806 for (i=0; i<keymap.n_keys; i++) 807 print_key_definition_line(stdout, i, &keymap.key[i]); 808 809 printf("\n"); 810 for (i = 0; i < NUM_DEADKEYS; i++) 811 print_accent_definition_line(stdout, i, &accentmap.acc[i]); 812 813 } 814 815 816 void 817 load_default_functionkeys(void) 818 { 819 fkeyarg_t fkey; 820 int i; 821 822 for (i=0; i<NUM_FKEYS; i++) { 823 fkey.keynum = i; 824 strcpy(fkey.keydef, fkey_table[i]); 825 fkey.flen = strlen(fkey_table[i]); 826 if (ioctl(0, SETFKEY, &fkey) < 0) 827 warn("setting function key"); 828 } 829 } 830 831 void 832 set_functionkey(char *keynumstr, char *string) 833 { 834 fkeyarg_t fkey; 835 836 if (!strcmp(keynumstr, "load") && !strcmp(string, "default")) { 837 load_default_functionkeys(); 838 return; 839 } 840 fkey.keynum = atoi(keynumstr); 841 if (fkey.keynum < 1 || fkey.keynum > NUM_FKEYS) { 842 warnx("function key number must be between 1 and %d", 843 NUM_FKEYS); 844 return; 845 } 846 if ((fkey.flen = strlen(string)) > MAXFK) { 847 warnx("function key string too long (%d > %d)", 848 fkey.flen, MAXFK); 849 return; 850 } 851 strcpy(fkey.keydef, string); 852 fkey.keynum -= 1; 853 if (ioctl(0, SETFKEY, &fkey) < 0) 854 warn("setting function key"); 855 } 856 857 858 void 859 set_bell_values(char *opt) 860 { 861 int bell, duration, pitch; 862 863 bell = 0; 864 if (!strncmp(opt, "quiet.", 6)) { 865 bell = 2; 866 opt += 6; 867 } 868 if (!strcmp(opt, "visual")) 869 bell |= 1; 870 else if (!strcmp(opt, "normal")) 871 duration = 5, pitch = 800; 872 else if (!strcmp(opt, "off")) 873 duration = 0, pitch = 0; 874 else { 875 char *v1; 876 877 bell = 0; 878 duration = strtol(opt, &v1, 0); 879 if ((duration < 0) || (*v1 != '.')) 880 goto badopt; 881 opt = ++v1; 882 pitch = strtol(opt, &v1, 0); 883 if ((pitch < 0) || (*opt == '\0') || (*v1 != '\0')) { 884 badopt: 885 warnx("argument to -b must be duration.pitch or [quiet.]visual|normal|off"); 886 return; 887 } 888 if (pitch != 0) 889 pitch = 1193182 / pitch; /* in Hz */ 890 duration /= 10; /* in 10 m sec */ 891 } 892 893 ioctl(0, CONS_BELLTYPE, &bell); 894 if ((bell & ~2) == 0) 895 fprintf(stderr, "[=%d;%dB", pitch, duration); 896 } 897 898 899 void 900 set_keyrates(char *opt) 901 { 902 int arg[2]; 903 int repeat; 904 int delay; 905 int r, d; 906 907 if (!strcmp(opt, "slow")) { 908 delay = 1000, repeat = 500; 909 d = 3, r = 31; 910 } else if (!strcmp(opt, "normal")) { 911 delay = 500, repeat = 125; 912 d = 1, r = 15; 913 } else if (!strcmp(opt, "fast")) { 914 delay = repeat = 0; 915 d = r = 0; 916 } else { 917 int n; 918 char *v1; 919 920 delay = strtol(opt, &v1, 0); 921 if ((delay < 0) || (*v1 != '.')) 922 goto badopt; 923 opt = ++v1; 924 repeat = strtol(opt, &v1, 0); 925 if ((repeat < 0) || (*opt == '\0') || (*v1 != '\0')) { 926 badopt: 927 warnx("argument to -r must be delay.repeat or slow|normal|fast"); 928 return; 929 } 930 for (n = 0; n < ndelays - 1; n++) 931 if (delay <= delays[n]) 932 break; 933 d = n; 934 for (n = 0; n < nrepeats - 1; n++) 935 if (repeat <= repeats[n]) 936 break; 937 r = n; 938 } 939 940 arg[0] = delay; 941 arg[1] = repeat; 942 if (ioctl(0, KDSETREPEAT, arg)) { 943 if (ioctl(0, KDSETRAD, (d << 5) | r)) 944 warn("setting keyboard rate"); 945 } 946 } 947 948 949 void 950 set_history(char *opt) 951 { 952 int size; 953 954 size = atoi(opt); 955 if ((*opt == '\0') || size < 0) { 956 warnx("argument must be a positive number"); 957 return; 958 } 959 if (ioctl(0, CONS_HISTORY, &size) == -1) 960 warn("setting history buffer size"); 961 } 962 963 static char * 964 get_kbd_type_name(int type) 965 { 966 static struct { 967 int type; 968 char *name; 969 } name_table[] = { 970 { KB_84, "AT 84" }, 971 { KB_101, "AT 101/102" }, 972 { KB_OTHER, "generic" }, 973 }; 974 int i; 975 976 for (i = 0; i < sizeof(name_table)/sizeof(name_table[0]); ++i) { 977 if (type == name_table[i].type) 978 return name_table[i].name; 979 } 980 return "unknown"; 981 } 982 983 void 984 show_kbd_info(void) 985 { 986 keyboard_info_t info; 987 988 if (ioctl(0, KDGKBINFO, &info) == -1) { 989 warn("unable to obtain keyboard information"); 990 return; 991 } 992 printf("kbd%d:\n", info.kb_index); 993 printf(" %.*s%d, type:%s (%d)\n", 994 (int)sizeof(info.kb_name), info.kb_name, info.kb_unit, 995 get_kbd_type_name(info.kb_type), info.kb_type); 996 } 997 998 999 void 1000 set_keyboard(char *device) 1001 { 1002 keyboard_info_t info; 1003 int fd; 1004 1005 fd = open(device, O_RDONLY); 1006 if (fd < 0) { 1007 warn("cannot open %s", device); 1008 return; 1009 } 1010 if (ioctl(fd, KDGKBINFO, &info) == -1) { 1011 warn("unable to obtain keyboard information"); 1012 close(fd); 1013 return; 1014 } 1015 /* 1016 * The keyboard device driver won't release the keyboard by 1017 * the following ioctl, but it automatically will, when the device 1018 * is closed. So, we don't check error here. 1019 */ 1020 ioctl(fd, CONS_RELKBD, 0); 1021 close(fd); 1022 #if 1 1023 printf("kbd%d\n", info.kb_index); 1024 printf(" %.*s%d, type:%s (%d)\n", 1025 (int)sizeof(info.kb_name), info.kb_name, info.kb_unit, 1026 get_kbd_type_name(info.kb_type), info.kb_type); 1027 #endif 1028 1029 if (ioctl(0, CONS_SETKBD, info.kb_index) == -1) 1030 warn("unable to set keyboard"); 1031 } 1032 1033 1034 void 1035 release_keyboard(void) 1036 { 1037 keyboard_info_t info; 1038 1039 /* 1040 * If stdin is not associated with a keyboard, the following ioctl 1041 * will fail. 1042 */ 1043 if (ioctl(0, KDGKBINFO, &info) == -1) { 1044 warn("unable to obtain keyboard information"); 1045 return; 1046 } 1047 #if 1 1048 printf("kbd%d\n", info.kb_index); 1049 printf(" %.*s%d, type:%s (%d)\n", 1050 (int)sizeof(info.kb_name), info.kb_name, info.kb_unit, 1051 get_kbd_type_name(info.kb_type), info.kb_type); 1052 #endif 1053 if (ioctl(0, CONS_RELKBD, 0) == -1) 1054 warn("unable to release the keyboard"); 1055 } 1056 1057 void 1058 mux_keyboard(u_int op, char *kbd) 1059 { 1060 keyboard_info_t info; 1061 char *unit, *ep; 1062 1063 /* 1064 * If stdin is not associated with a keyboard, the following ioctl 1065 * will fail. 1066 */ 1067 if (ioctl(0, KDGKBINFO, &info) == -1) { 1068 warn("unable to obtain keyboard information"); 1069 return; 1070 } 1071 #if 1 1072 printf("kbd%d\n", info.kb_index); 1073 printf(" %.*s%d, type:%s (%d)\n", 1074 (int)sizeof(info.kb_name), info.kb_name, info.kb_unit, 1075 get_kbd_type_name(info.kb_type), info.kb_type); 1076 #endif 1077 /* 1078 * split kbd into name and unit. find the right most part of the 1079 * kbd string that consist of only digits. 1080 */ 1081 1082 memset(&info, 0, sizeof(info)); 1083 1084 info.kb_unit = -1; 1085 ep = kbd - 1; 1086 1087 do { 1088 unit = strpbrk(ep + 1, "0123456789"); 1089 if (unit != NULL) { 1090 info.kb_unit = strtol(unit, &ep, 10); 1091 if (*ep != '\0') 1092 info.kb_unit = -1; 1093 } 1094 } while (unit != NULL && info.kb_unit == -1); 1095 1096 if (info.kb_unit == -1) { 1097 warnx("unable to find keyboard driver unit in '%s'", kbd); 1098 return; 1099 } 1100 1101 if (unit == kbd) { 1102 warnx("unable to find keyboard driver name in '%s'", kbd); 1103 return; 1104 } 1105 if (unit - kbd >= (int) sizeof(info.kb_name)) { 1106 warnx("keyboard name '%s' is too long", kbd); 1107 return; 1108 } 1109 1110 strncpy(info.kb_name, kbd, unit - kbd); 1111 1112 /* 1113 * If stdin is not associated with a kbdmux(4) keyboard, the following 1114 * ioctl will fail. 1115 */ 1116 1117 if (ioctl(0, op, &info) == -1) 1118 warn("unable to (un)mux the keyboard"); 1119 } 1120 1121 void 1122 usage(void) 1123 { 1124 fprintf(stderr, "%s\n%s\n%s\n", 1125 "usage: kbdcontrol [-dFKix] [-A name] [-a name] [-b duration.pitch | [quiet.]belltype]", 1126 " [-r delay.repeat | speed] [-l mapfile] [-f # string]", 1127 " [-h size] [-k device] [-L mapfile]"); 1128 exit(1); 1129 } 1130 1131 1132 int 1133 main(int argc, char **argv) 1134 { 1135 int opt; 1136 1137 while((opt = getopt(argc, argv, "A:a:b:df:h:iKk:Fl:L:r:x")) != -1) 1138 switch(opt) { 1139 case 'A': 1140 case 'a': 1141 mux_keyboard((opt == 'A')? KBRELKBD : KBADDKBD, optarg); 1142 break; 1143 case 'b': 1144 set_bell_values(optarg); 1145 break; 1146 case 'd': 1147 print_keymap(); 1148 break; 1149 case 'l': 1150 load_keymap(optarg, 0); 1151 break; 1152 case 'L': 1153 load_keymap(optarg, 1); 1154 break; 1155 case 'f': 1156 set_functionkey(optarg, 1157 nextarg(argc, argv, &optind, 'f')); 1158 break; 1159 case 'F': 1160 load_default_functionkeys(); 1161 break; 1162 case 'h': 1163 set_history(optarg); 1164 break; 1165 case 'i': 1166 show_kbd_info(); 1167 break; 1168 case 'K': 1169 release_keyboard(); 1170 break; 1171 case 'k': 1172 set_keyboard(optarg); 1173 break; 1174 case 'r': 1175 set_keyrates(optarg); 1176 break; 1177 case 'x': 1178 hex = 1; 1179 break; 1180 default: 1181 usage(); 1182 } 1183 if ((optind != argc) || (argc == 1)) 1184 usage(); 1185 exit(0); 1186 } 1187