2 * Copyright (c) 1992, 2000 Hellmuth Michaelis
4 * Copyright (c) 1992, 1993 Holger Veit.
8 * This code is derived from software contributed to 386BSD by
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by
22 * Hellmuth Michaelis and Holger Veit
23 * 4. The name authors may not be used to endorse or promote products
24 * derived from this software without specific prior written permission.
26 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
27 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
28 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
29 * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
30 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
31 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
35 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 /*---------------------------------------------------------------------------*
40 * kcon.c Keyboard control and remapping
41 * ----------------------------------------------
43 * based on "keymap" which was written by
44 * Holger Veit (veit@du9ds3.uni-duisburg.de)
46 * Last Edit-Date: [Mon Mar 27 17:03:50 2000]";
50 *---------------------------------------------------------------------------*/
55 #include <sys/types.h>
56 #include <sys/ioctl.h>
57 #include <machine/pcvt_ioctl.h>
72 /*---------------------------------------------------------------------------*
74 *---------------------------------------------------------------------------*/
88 while((c = getopt(argc, argv, "Rd:lm:opr:st:x")) != -1)
133 else if(*optarg == '-')
151 if((Rf == 0 && df == 0 && lf == 0 && tf == 0 && sf == 0 &&
152 rf == 0 && mf == 0 ) || errf)
157 if((kbfd = open(KEYB_DEVICE, 0)) < 0)
159 perror("kcon: keyboard open failiure");
165 showtypeamatic(kbfd);
177 if (ioctl(kbfd, KBDRESET, 0) < 0) {
178 perror ("kcon: ioctl KBDRESET failed");
192 fprintf(stderr,"Delay value (%d) out of range, possible values are 0..3!\n",delay);
197 fprintf(stderr,"Rate value (%d) out of range, possible values are 0..31!\n",rate);
200 settypeam(kbfd, delay, rate);
205 remapkeys(kbfd, map);
212 /*---------------------------------------------------------------------------*
213 * display usage info & exit
214 *---------------------------------------------------------------------------*/
217 fprintf(stderr, "\nkcon: keyboard control and remapping utility for pcvt video driver\n");
218 fprintf(stderr, "usage: [-R] [-d delay] [-l] [-m map] [-o] [-p] [-r rate] [-t +/-] [-x]\n");
219 fprintf(stderr, " -R full reset of keyboard\n");
220 fprintf(stderr, " -d delay until a key is repeated (range: 0...3 => 250...1000ms)\n");
221 fprintf(stderr, " -l produce listing of current keyboard mapping\n");
222 fprintf(stderr, " -m set keyboard remapping from a keycap entry\n");
223 fprintf(stderr, " -o set octal output for listing\n");
224 fprintf(stderr, " -p pure, don't display escape as 'ESC' for listing\n");
225 fprintf(stderr, " -r chars/second repeat value (range: 0...31 => 30...2 chars/sec)\n");
226 fprintf(stderr, " -s show, display the current keyboard typematic values\n");
227 fprintf(stderr, " -t switch repeat on(+) or off(-)\n");
228 fprintf(stderr, " -x set hexadecimal output for listing\n\n");
232 /*---------------------------------------------------------------------------*
233 * convert control char in string to printable values
234 *---------------------------------------------------------------------------*/
238 static char res_str[80];
239 static char conv_buf[80];
244 for(i = 0; s[i]; i++)
246 if(((s[i] > 0x20) && (s[i] <= 0x7e)) || ((s[i] >= 0xa0) && (s[i] <= 0xff)))
251 else if((s[i] == 0x1b) && (pf == 0))
253 strcpy(conv_buf,"ESC ");
257 sprintf(conv_buf,"\\%03.3o ", s[i]);
261 sprintf(conv_buf,"0x%02.2X ", s[i]);
263 strcat(res_str, conv_buf);
268 /*---------------------------------------------------------------------------*
269 * list the current keyboard mapping
270 *---------------------------------------------------------------------------*/
274 static char *keytypetab[] = {
292 struct kbd_ovlkey keyboardmap[KBDMAXKEYS];
293 struct kbd_ovlkey *kbmapp;
299 kbmapp = keyboardmap;
301 for (i = 0; i < KBDMAXKEYS; i++)
305 if(ioctl(kbfd, KBDGCKEY, kbmapp) < 0)
307 perror("kcon: ioctl KBDGCKEY failed");
311 if((kbmapp->type & KBD_MASK) == KBD_ALTGR)
319 printf("S Key KeyType Normal Shift Control Altgr \n");
320 printf("- --- --------- --------------- --------------- --------------- ---------------\n");
324 printf("S Key KeyType Normal Shift Control \n");
325 printf("- --- --------- --------------- --------------- ---------------\n");
328 kbmapp = &keyboardmap[1];
330 for(i = 1; i < KBDMAXKEYS; i++)
332 keytype = kbmapp->type;
336 if(keytype & KBD_OVERLOAD)
337 printf("! %3.3d %9.9s ", i, keytypetab[keytype & KBD_MASK]);
339 printf("- %3.3d %9.9s ", i, keytypetab[keytype & KBD_MASK]);
341 switch(keytype & KBD_MASK)
349 case KBD_RETURN: /* ??? */
351 if(kbmapp->subu == KBD_SUBT_STR)
352 printf("%-15s ",showcntrl(kbmapp->unshift));
354 printf("Function() ");
356 if(kbmapp->subs == KBD_SUBT_STR)
357 printf("%-15s ",showcntrl(kbmapp->shift));
359 printf("Function() ");
361 if(kbmapp->subc == KBD_SUBT_STR)
362 printf("%-15s ",showcntrl(kbmapp->ctrl));
364 printf("Function() ");
368 if(kbmapp->suba == KBD_SUBT_STR)
369 printf("%-15s ",showcntrl(kbmapp->altgr));
371 printf("Function() ");
381 /*---------------------------------------------------------------------------*
382 * show delay and rate values for keyboard
383 *---------------------------------------------------------------------------*/
387 static char *delaytab[] = {
394 static char *ratetab[] = {
432 if((ioctl(kbfd, KBDGTPMAT, &cur_typemat_val)) < 0)
434 perror("kcon: ioctl KBDGTPMAT failed");
438 delay = ((cur_typemat_val & 0x60) >> 5);
439 rate = cur_typemat_val & 0x1f;
441 printf("\nDisplaying the current keyboard typematic values:\n\n");
442 printf("The delay-until-repeat time is [ %s ] milliseconds\n",delaytab[delay]);
443 printf("The repeat-rate is [ %s ] characters per second\n\n",ratetab[rate]);
446 /*---------------------------------------------------------------------------*
447 * set repeat feature on/off
448 *---------------------------------------------------------------------------*/
456 srepsw_val = KBD_REPEATON;
458 srepsw_val = KBD_REPEATOFF;
460 if(ioctl(kbfd, KBDSREPSW, &srepsw_val) < 0)
462 perror("kcon: ioctl KBDREPSW failed");
467 /*---------------------------------------------------------------------------*
468 * set delay and rate values for keyboard
469 *---------------------------------------------------------------------------*/
470 settypeam(kbfd, delay, rate)
478 if((ioctl(kbfd, KBDGTPMAT, &cur_typemat_val)) < 0)
480 perror("kcon: ioctl KBDGTPMAT failed");
485 delay = (cur_typemat_val & 0x60);
487 delay = ((delay << 5) & 0x60);
490 rate = (cur_typemat_val & 0x1f);
494 new_typemat_val = delay | rate;
496 if((ioctl(kbfd, KBDSTPMAT, &new_typemat_val)) < 0)
498 perror("kcon: ioctl KBDSTPMAT failed");
503 /*---------------------------------------------------------------------------*
504 * remap keyboard from keycap entry
505 *---------------------------------------------------------------------------*/
510 char cap_entry[1024];
515 /* try to find the entry */
517 ret = kgetent(cap_entry, map);
521 fprintf(stderr, "kcon: keycap database not found or not accessible!\n");
526 fprintf(stderr, "kcon: keycap entry [%s] not found in database!\n", map);
530 /* set default mapping */
532 if((ioctl(kbfd, KBDDEFAULT)) < 0)
534 perror("kcon: ioctl KBDDEFAULT failed");
538 /* DE flag present? */
543 for(i = 0; i < KBDMAXKEYS; i++)
546 set_lock(keyflag, kbfd);
548 set_shift(keyflag, kbfd);
550 set_char(keyflag, kbfd);
553 /*---------------------------------------------------------------------------*
555 *---------------------------------------------------------------------------*/
556 set_lock(keyflag, kbfd)
562 struct kbd_ovlkey entry;
576 for(i = 0; i < 4; i++)
580 sprintf(cap, "%s", lock[i].ch);
588 fprintf(stderr,"kcon: duplicate key definition for key [%d]!\n",n);
594 entry.type = lock[i].typ;
596 if((ioctl(kbfd, KBDSCKEY, &entry)) < 0)
598 perror("kcon: ioctl KBDSCKEY failed");
605 /*---------------------------------------------------------------------------*
606 * care for shifting keys
607 *---------------------------------------------------------------------------*/
608 set_shift(keyflag, kbfd)
614 struct kbd_ovlkey entry;
627 for(i = 0; i < 4; i++)
629 for(j = 1; j < 10; j++)
633 sprintf(cap, "%c%d", shift[i].ch,j);
641 fprintf(stderr,"kcon: duplicate key definition for key [%d]!\n",n);
647 entry.type = shift[i].typ;
648 if((ioctl(kbfd, KBDSCKEY, &entry)) < 0)
650 perror("kcon: ioctl KBDSCKEY failed");
658 /*---------------------------------------------------------------------------*
659 * care for normal keys
660 *---------------------------------------------------------------------------*/
661 set_char(keyflag, kbfd)
670 struct kbd_ovlkey entry;
677 &entry.unshift[0], 'K',
678 &entry.shift[0], 'S',
683 for(i = 1; i < KBDMAXKEYS; i++)
689 if((ioctl(kbfd, KBDGOKEY, &entry)) < 0)
691 perror("kcon: ioctl KBDGOKEY failed");
695 entry.type = KBD_ASCII;
697 for(j = 0; j < 5; j++)
699 sprintf(cap, "%c%d", standard[j].ch,i);
701 if((j == 0) && (kgetflag(cap)))
705 entry.type = KBD_NONE;
712 addr_str = standard[j].addr;
713 if(new_str = kgetstr(cap, &addr_str))
715 if(strlen(new_str) > KBDMAXOVLKEYSIZE)
717 fprintf(stderr, "kcon: database entry string [%s] longer than max [%d]!\n",new_str,KBDMAXOVLKEYSIZE);
729 fprintf(stderr,"kcon: duplicate key definition for key [%d]!\n",i);
734 if((ioctl(kbfd, KBDSCKEY, &entry)) < 0)
736 perror("kcon: ioctl KBDSCKEY failed");
743 /*------------------- EOF ------------------------------------------------*/