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.
37 * $DragonFly: src/usr.sbin/pcvt/kcon/Attic/kcon.c,v 1.2 2004/03/24 17:46:23 cpressey Exp $
41 "@(#)kcon.c, 3.20, Last Edit-Date: [Wed Jan 25 16:33:08 1995]";
43 /*---------------------------------------------------------------------------*
45 * kcon.c Keyboard control and remapping
46 * ----------------------------------------------
48 * based on "keymap" which was written by
49 * Holger Veit (veit@du9ds3.uni-duisburg.de)
52 * -hm rewrite for pcvt 2.0 distribution
53 * -hm adding show current typematic values
54 * -hm hex/octal/esc output choices
55 * -hm remapping debugging
56 * -hm garbage output for remapped keys bugfix
57 * -hm patch from Lon Willet, adding -R
59 *---------------------------------------------------------------------------*/
61 #include <sys/types.h>
62 #include <sys/ioctl.h>
63 #include <machine/pcvt_ioctl.h>
70 static void usage(void);
71 static void listcurrent(int);
72 static void setrepeat(int, int);
73 static void settypeam(int, int, int);
74 static void remapkeys(int, char *);
75 static void set_lock(char[], int);
76 static void set_shift(char[], int);
77 static void set_char(char[], int);
90 /*---------------------------------------------------------------------------*
92 *---------------------------------------------------------------------------*/
94 main(int argc, char **argv)
108 while((c = getopt(argc, argv, "Rd:lm:opr:st:x")) != -1)
118 delay = atoi(optarg);
153 else if(*optarg == '-')
171 if((Rf == 0 && df == 0 && lf == 0 && tf == 0 && sf == 0 &&
172 rf == 0 && mf == 0 ) || errf)
177 if((kbfd = open(KEYB_DEVICE, 0)) < 0)
179 perror("kcon: keyboard open failiure");
185 showtypeamatic(kbfd);
197 if (ioctl(kbfd, KBDRESET, 0) < 0) {
198 perror ("kcon: ioctl KBDRESET failed");
212 fprintf(stderr,"Delay value (%d) out of range, possible values are 0..3!\n",delay);
217 fprintf(stderr,"Rate value (%d) out of range, possible values are 0..31!\n",rate);
220 settypeam(kbfd, delay, rate);
225 remapkeys(kbfd, map);
232 /*---------------------------------------------------------------------------*
233 * display usage info & exit
234 *---------------------------------------------------------------------------*/
238 fprintf(stderr, "\nkcon: keyboard control and remapping utility for pcvt video driver\n");
239 fprintf(stderr, "usage: [-R] [-d delay] [-l] [-m map] [-o] [-p] [-r rate] [-t +/-] [-x]\n");
240 fprintf(stderr, " -R full reset of keyboard\n");
241 fprintf(stderr, " -d delay until a key is repeated (range: 0...3 => 250...1000ms)\n");
242 fprintf(stderr, " -l produce listing of current keyboard mapping\n");
243 fprintf(stderr, " -m set keyboard remapping from a keycap entry\n");
244 fprintf(stderr, " -o set octal output for listing\n");
245 fprintf(stderr, " -p pure, don't display escape as 'ESC' for listing\n");
246 fprintf(stderr, " -r chars/second repeat value (range: 0...31 => 30...2 chars/sec)\n");
247 fprintf(stderr, " -s show, display the current keyboard typematic values\n");
248 fprintf(stderr, " -t switch repeat on(+) or off(-)\n");
249 fprintf(stderr, " -x set hexadecimal output for listing\n\n");
253 /*---------------------------------------------------------------------------*
254 * convert control char in string to printable values
255 *---------------------------------------------------------------------------*/
259 static char res_str[80];
260 static char conv_buf[80];
265 for(i = 0; s[i]; i++)
267 if(((s[i] > 0x20) && (s[i] <= 0x7e)) || ((s[i] >= 0xa0) && (s[i] <= 0xff)))
272 else if((s[i] == 0x1b) && (pf == 0))
274 strcpy(conv_buf,"ESC ");
278 sprintf(conv_buf,"\\%03.3o ", s[i]);
282 sprintf(conv_buf,"0x%02.2X ", s[i]);
284 strcat(res_str, conv_buf);
289 /*---------------------------------------------------------------------------*
290 * list the current keyboard mapping
291 *---------------------------------------------------------------------------*/
293 listcurrent(int kbfd)
295 static char *keytypetab[] = {
313 struct kbd_ovlkey keyboardmap[KBDMAXKEYS];
314 struct kbd_ovlkey *kbmapp;
320 kbmapp = keyboardmap;
322 for (i = 0; i < KBDMAXKEYS; i++)
326 if(ioctl(kbfd, KBDGCKEY, kbmapp) < 0)
328 perror("kcon: ioctl KBDGCKEY failed");
332 if((kbmapp->type & KBD_MASK) == KBD_ALTGR)
340 printf("S Key KeyType Normal Shift Control Altgr \n");
341 printf("- --- --------- --------------- --------------- --------------- ---------------\n");
345 printf("S Key KeyType Normal Shift Control \n");
346 printf("- --- --------- --------------- --------------- ---------------\n");
349 kbmapp = &keyboardmap[1];
351 for(i = 1; i < KBDMAXKEYS; i++)
353 keytype = kbmapp->type;
357 if(keytype & KBD_OVERLOAD)
358 printf("! %3.3d %9.9s ", i, keytypetab[keytype & KBD_MASK]);
360 printf("- %3.3d %9.9s ", i, keytypetab[keytype & KBD_MASK]);
362 switch(keytype & KBD_MASK)
370 case KBD_RETURN: /* ??? */
372 if(kbmapp->subu == KBD_SUBT_STR)
373 printf("%-15s ",showcntrl(kbmapp->unshift));
375 printf("Function() ");
377 if(kbmapp->subs == KBD_SUBT_STR)
378 printf("%-15s ",showcntrl(kbmapp->shift));
380 printf("Function() ");
382 if(kbmapp->subc == KBD_SUBT_STR)
383 printf("%-15s ",showcntrl(kbmapp->ctrl));
385 printf("Function() ");
389 if(kbmapp->suba == KBD_SUBT_STR)
390 printf("%-15s ",showcntrl(kbmapp->altgr));
392 printf("Function() ");
402 /*---------------------------------------------------------------------------*
403 * show delay and rate values for keyboard
404 *---------------------------------------------------------------------------*/
406 showtypeamatic(int kbfd)
408 static char *delaytab[] = {
415 static char *ratetab[] = {
453 if((ioctl(kbfd, KBDGTPMAT, &cur_typemat_val)) < 0)
455 perror("kcon: ioctl KBDGTPMAT failed");
459 delay = ((cur_typemat_val & 0x60) >> 5);
460 rate = cur_typemat_val & 0x1f;
462 printf("\nDisplaying the current keyboard typematic values:\n\n");
463 printf("The delay-until-repeat time is [ %s ] milliseconds\n",delaytab[delay]);
464 printf("The repeat-rate is [ %s ] characters per second\n\n",ratetab[rate]);
467 /*---------------------------------------------------------------------------*
468 * set repeat feature on/off
469 *---------------------------------------------------------------------------*/
471 setrepeat(int kbfd, int tf)
476 srepsw_val = KBD_REPEATON;
478 srepsw_val = KBD_REPEATOFF;
480 if(ioctl(kbfd, KBDSREPSW, &srepsw_val) < 0)
482 perror("kcon: ioctl KBDREPSW failed");
487 /*---------------------------------------------------------------------------*
488 * set delay and rate values for keyboard
489 *---------------------------------------------------------------------------*/
491 settypeam(int kbfd, int delay, int rate)
496 if((ioctl(kbfd, KBDGTPMAT, &cur_typemat_val)) < 0)
498 perror("kcon: ioctl KBDGTPMAT failed");
503 delay = (cur_typemat_val & 0x60);
505 delay = ((delay << 5) & 0x60);
508 rate = (cur_typemat_val & 0x1f);
512 new_typemat_val = delay | rate;
514 if((ioctl(kbfd, KBDSTPMAT, &new_typemat_val)) < 0)
516 perror("kcon: ioctl KBDSTPMAT failed");
521 /*---------------------------------------------------------------------------*
522 * remap keyboard from keycap entry
523 *---------------------------------------------------------------------------*/
525 remapkeys(int kbfd, char *map)
527 char cap_entry[1024];
532 /* try to find the entry */
534 ret = kgetent(cap_entry, map);
538 fprintf(stderr, "kcon: keycap database not found or not accessible!\n");
543 fprintf(stderr, "kcon: keycap entry [%s] not found in database!\n", map);
547 /* set default mapping */
549 if((ioctl(kbfd, KBDDEFAULT)) < 0)
551 perror("kcon: ioctl KBDDEFAULT failed");
555 /* DE flag present? */
560 for(i = 0; i < KBDMAXKEYS; i++)
563 set_lock(keyflag, kbfd);
565 set_shift(keyflag, kbfd);
567 set_char(keyflag, kbfd);
570 /*---------------------------------------------------------------------------*
572 *---------------------------------------------------------------------------*/
574 set_lock(char keyflag[], int kbfd)
578 struct kbd_ovlkey entry;
592 for(i = 0; i < 4; i++)
596 sprintf(cap, "%s", lock[i].ch);
604 fprintf(stderr,"kcon: duplicate key definition for key [%d]!\n",n);
610 entry.type = lock[i].typ;
612 if((ioctl(kbfd, KBDSCKEY, &entry)) < 0)
614 perror("kcon: ioctl KBDSCKEY failed");
621 /*---------------------------------------------------------------------------*
622 * care for shifting keys
623 *---------------------------------------------------------------------------*/
625 set_shift(char keyflag[], int kbfd)
629 struct kbd_ovlkey entry;
642 for(i = 0; i < 4; i++)
644 for(j = 1; j < 10; j++)
648 sprintf(cap, "%c%d", shift[i].ch,j);
656 fprintf(stderr,"kcon: duplicate key definition for key [%d]!\n",n);
662 entry.type = shift[i].typ;
663 if((ioctl(kbfd, KBDSCKEY, &entry)) < 0)
665 perror("kcon: ioctl KBDSCKEY failed");
673 /*---------------------------------------------------------------------------*
674 * care for normal keys
675 *---------------------------------------------------------------------------*/
677 set_char(char keyflag[], int kbfd)
684 struct kbd_ovlkey entry;
691 &entry.unshift[0], 'K',
692 &entry.shift[0], 'S',
697 for(i = 1; i < KBDMAXKEYS; i++)
703 if((ioctl(kbfd, KBDGOKEY, &entry)) < 0)
705 perror("kcon: ioctl KBDGOKEY failed");
709 entry.type = KBD_ASCII;
711 for(j = 0; j < 5; j++)
713 sprintf(cap, "%c%d", standard[j].ch,i);
715 if((j == 0) && (kgetflag(cap)))
719 entry.type = KBD_NONE;
726 addr_str = standard[j].addr;
727 if(new_str = kgetstr(cap, &addr_str))
729 if(strlen(new_str) > KBDMAXOVLKEYSIZE)
731 fprintf(stderr, "kcon: database entry string [%s] longer than max [%d]!\n",new_str,KBDMAXOVLKEYSIZE);
743 fprintf(stderr,"kcon: duplicate key definition for key [%d]!\n",i);
748 if((ioctl(kbfd, KBDSCKEY, &entry)) < 0)
750 perror("kcon: ioctl KBDSCKEY failed");
757 /*------------------- EOF ------------------------------------------------*/