2 * Copyright (c) 1992,1993,1994 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.
40 "@(#)kcon.c, 3.20, Last Edit-Date: [Wed Jan 25 16:33:08 1995]";
42 /*---------------------------------------------------------------------------*
44 * kcon.c Keyboard control and remapping
45 * ----------------------------------------------
47 * based on "keymap" which was written by
48 * Holger Veit (veit@du9ds3.uni-duisburg.de)
51 * -hm rewrite for pcvt 2.0 distribution
52 * -hm adding show current typematic values
53 * -hm hex/octal/esc output choices
54 * -hm remapping debugging
55 * -hm garbage output for remapped keys bugfix
56 * -hm patch from Lon Willet, adding -R
58 *---------------------------------------------------------------------------*/
62 #include <sys/types.h>
63 #include <sys/ioctl.h>
64 #include <machine/pcvt_ioctl.h>
79 /*---------------------------------------------------------------------------*
81 *---------------------------------------------------------------------------*/
98 while((c = getopt(argc, argv, "Rd:lm:opr:st:x")) != -1)
108 delay = atoi(optarg);
143 else if(*optarg == '-')
161 if((Rf == 0 && df == 0 && lf == 0 && tf == 0 && sf == 0 &&
162 rf == 0 && mf == 0 ) || errf)
167 if((kbfd = open(KEYB_DEVICE, 0)) < 0)
169 perror("kcon: keyboard open failiure");
175 showtypeamatic(kbfd);
187 if (ioctl(kbfd, KBDRESET, 0) < 0) {
188 perror ("kcon: ioctl KBDRESET failed");
202 fprintf(stderr,"Delay value (%d) out of range, possible values are 0..3!\n",delay);
207 fprintf(stderr,"Rate value (%d) out of range, possible values are 0..31!\n",rate);
210 settypeam(kbfd, delay, rate);
215 remapkeys(kbfd, map);
222 /*---------------------------------------------------------------------------*
223 * display usage info & exit
224 *---------------------------------------------------------------------------*/
227 fprintf(stderr, "\nkcon: keyboard control and remapping utility for pcvt video driver\n");
228 fprintf(stderr, "usage: [-R] [-d delay] [-l] [-m map] [-o] [-p] [-r rate] [-t +/-] [-x]\n");
229 fprintf(stderr, " -R full reset of keyboard\n");
230 fprintf(stderr, " -d delay until a key is repeated (range: 0...3 => 250...1000ms)\n");
231 fprintf(stderr, " -l produce listing of current keyboard mapping\n");
232 fprintf(stderr, " -m set keyboard remapping from a keycap entry\n");
233 fprintf(stderr, " -o set octal output for listing\n");
234 fprintf(stderr, " -p pure, don't display escape as 'ESC' for listing\n");
235 fprintf(stderr, " -r chars/second repeat value (range: 0...31 => 30...2 chars/sec)\n");
236 fprintf(stderr, " -s show, display the current keyboard typematic values\n");
237 fprintf(stderr, " -t switch repeat on(+) or off(-)\n");
238 fprintf(stderr, " -x set hexadecimal output for listing\n\n");
242 /*---------------------------------------------------------------------------*
243 * convert control char in string to printable values
244 *---------------------------------------------------------------------------*/
248 static char res_str[80];
249 static char conv_buf[80];
254 for(i = 0; s[i]; i++)
256 if(((s[i] > 0x20) && (s[i] <= 0x7e)) || ((s[i] >= 0xa0) && (s[i] <= 0xff)))
261 else if((s[i] == 0x1b) && (pf == 0))
263 strcpy(conv_buf,"ESC ");
267 sprintf(conv_buf,"\\%03.3o ", s[i]);
271 sprintf(conv_buf,"0x%02.2X ", s[i]);
273 strcat(res_str, conv_buf);
278 /*---------------------------------------------------------------------------*
279 * list the current keyboard mapping
280 *---------------------------------------------------------------------------*/
284 static char *keytypetab[] = {
302 struct kbd_ovlkey keyboardmap[KBDMAXKEYS];
303 struct kbd_ovlkey *kbmapp;
309 kbmapp = keyboardmap;
311 for (i = 0; i < KBDMAXKEYS; i++)
315 if(ioctl(kbfd, KBDGCKEY, kbmapp) < 0)
317 perror("kcon: ioctl KBDGCKEY failed");
321 if((kbmapp->type & KBD_MASK) == KBD_ALTGR)
329 printf("S Key KeyType Normal Shift Control Altgr \n");
330 printf("- --- --------- --------------- --------------- --------------- ---------------\n");
334 printf("S Key KeyType Normal Shift Control \n");
335 printf("- --- --------- --------------- --------------- ---------------\n");
338 kbmapp = &keyboardmap[1];
340 for(i = 1; i < KBDMAXKEYS; i++)
342 keytype = kbmapp->type;
346 if(keytype & KBD_OVERLOAD)
347 printf("! %3.3d %9.9s ", i, keytypetab[keytype & KBD_MASK]);
349 printf("- %3.3d %9.9s ", i, keytypetab[keytype & KBD_MASK]);
351 switch(keytype & KBD_MASK)
359 case KBD_RETURN: /* ??? */
361 if(kbmapp->subu == KBD_SUBT_STR)
362 printf("%-15s ",showcntrl(kbmapp->unshift));
364 printf("Function() ");
366 if(kbmapp->subs == KBD_SUBT_STR)
367 printf("%-15s ",showcntrl(kbmapp->shift));
369 printf("Function() ");
371 if(kbmapp->subc == KBD_SUBT_STR)
372 printf("%-15s ",showcntrl(kbmapp->ctrl));
374 printf("Function() ");
378 if(kbmapp->suba == KBD_SUBT_STR)
379 printf("%-15s ",showcntrl(kbmapp->altgr));
381 printf("Function() ");
391 /*---------------------------------------------------------------------------*
392 * show delay and rate values for keyboard
393 *---------------------------------------------------------------------------*/
397 static char *delaytab[] = {
404 static char *ratetab[] = {
442 if((ioctl(kbfd, KBDGTPMAT, &cur_typemat_val)) < 0)
444 perror("kcon: ioctl KBDGTPMAT failed");
448 delay = ((cur_typemat_val & 0x60) >> 5);
449 rate = cur_typemat_val & 0x1f;
451 printf("\nDisplaying the current keyboard typematic values:\n\n");
452 printf("The delay-until-repeat time is [ %s ] milliseconds\n",delaytab[delay]);
453 printf("The repeat-rate is [ %s ] characters per second\n\n",ratetab[rate]);
456 /*---------------------------------------------------------------------------*
457 * set repeat feature on/off
458 *---------------------------------------------------------------------------*/
466 srepsw_val = KBD_REPEATON;
468 srepsw_val = KBD_REPEATOFF;
470 if(ioctl(kbfd, KBDSREPSW, &srepsw_val) < 0)
472 perror("kcon: ioctl KBDREPSW failed");
477 /*---------------------------------------------------------------------------*
478 * set delay and rate values for keyboard
479 *---------------------------------------------------------------------------*/
480 settypeam(kbfd, delay, rate)
488 if((ioctl(kbfd, KBDGTPMAT, &cur_typemat_val)) < 0)
490 perror("kcon: ioctl KBDGTPMAT failed");
495 delay = (cur_typemat_val & 0x60);
497 delay = ((delay << 5) & 0x60);
500 rate = (cur_typemat_val & 0x1f);
504 new_typemat_val = delay | rate;
506 if((ioctl(kbfd, KBDSTPMAT, &new_typemat_val)) < 0)
508 perror("kcon: ioctl KBDSTPMAT failed");
513 /*---------------------------------------------------------------------------*
514 * remap keyboard from keycap entry
515 *---------------------------------------------------------------------------*/
520 char cap_entry[1024];
525 /* try to find the entry */
527 ret = kgetent(cap_entry, map);
531 fprintf(stderr, "kcon: keycap database not found or not accessible!\n");
536 fprintf(stderr, "kcon: keycap entry [%s] not found in database!\n", map);
540 /* set default mapping */
542 if((ioctl(kbfd, KBDDEFAULT)) < 0)
544 perror("kcon: ioctl KBDDEFAULT failed");
548 /* DE flag present? */
553 for(i = 0; i < KBDMAXKEYS; i++)
556 set_lock(keyflag, kbfd);
558 set_shift(keyflag, kbfd);
560 set_char(keyflag, kbfd);
563 /*---------------------------------------------------------------------------*
565 *---------------------------------------------------------------------------*/
566 set_lock(keyflag, kbfd)
572 struct kbd_ovlkey entry;
586 for(i = 0; i < 4; i++)
590 sprintf(cap, "%s", lock[i].ch);
598 fprintf(stderr,"kcon: duplicate key definition for key [%d]!\n",n);
604 entry.type = lock[i].typ;
606 if((ioctl(kbfd, KBDSCKEY, &entry)) < 0)
608 perror("kcon: ioctl KBDSCKEY failed");
615 /*---------------------------------------------------------------------------*
616 * care for shifting keys
617 *---------------------------------------------------------------------------*/
618 set_shift(keyflag, kbfd)
624 struct kbd_ovlkey entry;
637 for(i = 0; i < 4; i++)
639 for(j = 1; j < 10; j++)
643 sprintf(cap, "%c%d", shift[i].ch,j);
651 fprintf(stderr,"kcon: duplicate key definition for key [%d]!\n",n);
657 entry.type = shift[i].typ;
658 if((ioctl(kbfd, KBDSCKEY, &entry)) < 0)
660 perror("kcon: ioctl KBDSCKEY failed");
668 /*---------------------------------------------------------------------------*
669 * care for normal keys
670 *---------------------------------------------------------------------------*/
671 set_char(keyflag, kbfd)
680 struct kbd_ovlkey entry;
687 &entry.unshift[0], 'K',
688 &entry.shift[0], 'S',
693 for(i = 1; i < KBDMAXKEYS; i++)
699 if((ioctl(kbfd, KBDGOKEY, &entry)) < 0)
701 perror("kcon: ioctl KBDGOKEY failed");
705 entry.type = KBD_ASCII;
707 for(j = 0; j < 5; j++)
709 sprintf(cap, "%c%d", standard[j].ch,i);
711 if((j == 0) && (kgetflag(cap)))
715 entry.type = KBD_NONE;
722 addr_str = standard[j].addr;
723 if(new_str = kgetstr(cap, &addr_str))
725 if(strlen(new_str) > KBDMAXOVLKEYSIZE)
727 fprintf(stderr, "kcon: database entry string [%s] longer than max [%d]!\n",new_str,KBDMAXOVLKEYSIZE);
739 fprintf(stderr,"kcon: duplicate key definition for key [%d]!\n",i);
744 if((ioctl(kbfd, KBDSCKEY, &entry)) < 0)
746 perror("kcon: ioctl KBDSCKEY failed");
753 /*------------------- EOF ------------------------------------------------*/