Home | History | Annotate | Line # | Download | only in wsconsctl
keysym.c revision 1.4
      1 /*	$NetBSD: keysym.c,v 1.4 2000/07/03 03:38:02 matt Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 1998 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Juergen Hannken-Illjes.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *	This product includes software developed by the NetBSD
     21  *	Foundation, Inc. and its contributors.
     22  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  *    contributors may be used to endorse or promote products derived
     24  *    from this software without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  * POSSIBILITY OF SUCH DAMAGE.
     37  */
     38 
     39 #include <dev/wscons/wsksymdef.h>
     40 #include <stdio.h>
     41 #include <stdlib.h>
     42 #include <string.h>
     43 #include <unistd.h>
     44 #include "keysym.h"
     45 #include "wsconsctl.h"
     46 
     47 #define NUMKSYMS	(sizeof(ksym_tab_by_name)/sizeof(ksym_tab_by_name[0]))
     48 
     49 static int first_time = 1;
     50 static struct ksym ksym_tab_by_ksym[NUMKSYMS];
     51 
     52 /* copied from dev/wscons/wskbdutil.c ... */
     53 
     54 static const u_char latin1_to_upper[256] = {
     55 /*      0  8  1  9  2  a  3  b  4  c  5  d  6  e  7  f               */
     56 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,		/* 0 */
     57 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,		/* 0 */
     58 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,		/* 1 */
     59 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,		/* 1 */
     60 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,		/* 2 */
     61 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,		/* 2 */
     62 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,		/* 3 */
     63 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,		/* 3 */
     64 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,		/* 4 */
     65 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,		/* 4 */
     66 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,		/* 5 */
     67 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,		/* 5 */
     68 	0x00,  'A',  'B',  'C',  'D',  'E',  'F',  'G',		/* 6 */
     69 	 'H',  'I',  'J',  'K',  'L',  'M',  'N',  'O',		/* 6 */
     70 	 'P',  'Q',  'R',  'S',  'T',  'U',  'V',  'W',		/* 7 */
     71 	 'X',  'Y',  'Z', 0x00, 0x00, 0x00, 0x00, 0x00,		/* 7 */
     72 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,		/* 8 */
     73 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,		/* 8 */
     74 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,		/* 9 */
     75 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,		/* 9 */
     76 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,		/* a */
     77 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,		/* a */
     78 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,		/* b */
     79 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,		/* b */
     80 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,		/* c */
     81 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,		/* c */
     82 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,		/* d */
     83 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,		/* d */
     84 	0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7,		/* e */
     85 	0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf,		/* e */
     86 	0xd0, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0x00,		/* f */
     87 	0xd8, 0xd9, 0xda, 0xdb, 0xdc, 0xdd, 0xde, 0x00,		/* f */
     88 };
     89 
     90 static int qcmp_name __P((const void *, const void *));
     91 static int qcmp_ksym __P((const void *, const void *));
     92 static int bcmp_name __P((const void *, const void *));
     93 static int bcmp_ksym __P((const void *, const void *));
     94 
     95 static void sort_ksym_tab __P((void));
     96 
     97 static int
     98 qcmp_name(a, b)
     99 	const void *a;
    100 	const void *b;
    101 {
    102 	return(strcmp(((struct ksym *) a)->name, ((struct ksym *) b)->name));
    103 }
    104 
    105 static int
    106 qcmp_ksym(a, b)
    107 	const void *a;
    108 	const void *b;
    109 {
    110 	return(((struct ksym *) b)->value - ((struct ksym *) a)->value);
    111 }
    112 
    113 static int
    114 bcmp_name(a, b)
    115 	const void *a;
    116 	const void *b;
    117 {
    118 	return(strcmp((char *) a, ((struct ksym *) b)->name));
    119 }
    120 
    121 static int
    122 bcmp_ksym(a, b)
    123 	const void *a;
    124 	const void *b;
    125 {
    126 	return(((struct ksym *) b)->value - *((int *) a));
    127 }
    128 
    129 static void
    130 sort_ksym_tab()
    131 {
    132 	int i;
    133 
    134 	for (i = 0; i < NUMKSYMS; i++)
    135 		ksym_tab_by_ksym[i] = ksym_tab_by_name[i];
    136 
    137 	qsort(ksym_tab_by_name, NUMKSYMS, sizeof(struct ksym), qcmp_name);
    138 	qsort(ksym_tab_by_ksym, NUMKSYMS, sizeof(struct ksym), qcmp_ksym);
    139 
    140 	first_time = 0;
    141 }
    142 
    143 char *
    144 ksym2name(k)
    145 	int k;
    146 {
    147 	static char tmp[20];
    148 	struct ksym *r;
    149 
    150 	if (first_time)
    151 		sort_ksym_tab();
    152 
    153 	r = bsearch(&k, ksym_tab_by_ksym,
    154 		    NUMKSYMS, sizeof(struct ksym), bcmp_ksym);
    155 
    156 	if (r != NULL)
    157 		return(r->name);
    158 	else {
    159 		snprintf(tmp, sizeof(tmp), "unknown_%d", k);
    160 		return(tmp);
    161 	}
    162 }
    163 
    164 int
    165 name2ksym(n)
    166 	char *n;
    167 {
    168 	int res;
    169 	struct ksym *r;
    170 
    171 	if (first_time)
    172 		sort_ksym_tab();
    173 
    174 	r = bsearch(n, ksym_tab_by_name,
    175 		    NUMKSYMS, sizeof(struct ksym), bcmp_name);
    176 
    177 	if (r != NULL)
    178 		return(r->value);
    179 	else if (sscanf(n, "unknown_%d", &res) == 1)
    180 		return(res);
    181 	else
    182 		return(-1);
    183 }
    184 
    185 keysym_t
    186 ksym_upcase(ksym)
    187 	keysym_t ksym;
    188 {
    189 	if (ksym >= KS_f1 && ksym <= KS_f20)
    190 		return(KS_F1 - KS_f1 + ksym);
    191 
    192 	if (KS_GROUP(ksym) == KS_GROUP_Ascii && ksym <= 0xff &&
    193 	    latin1_to_upper[ksym] != 0x00)
    194 		return(latin1_to_upper[ksym]);
    195 
    196 	return(ksym);
    197 }
    198