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