xref: /netbsd-src/sbin/wsconsctl/keysym.c (revision 8b0f9554ff8762542c4defc4f70e1eb76fb508fa)
1 /*	$NetBSD: keysym.c,v 1.7 2006/02/05 18:11:46 jmmv 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 
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <string.h>
44 #include <unistd.h>
45 
46 #include "keysym.h"
47 #include "wsconsctl.h"
48 
49 #define NUMKSYMS	(sizeof(ksym_tab_by_name)/sizeof(ksym_tab_by_name[0]))
50 
51 static int first_time = 1;
52 static struct ksym ksym_tab_by_ksym[NUMKSYMS];
53 
54 /* copied from dev/wscons/wskbdutil.c ... */
55 
56 static const u_char latin1_to_upper[256] = {
57 /*	0  8  1  9  2  a  3  b  4  c  5  d  6  e  7  f		     */
58 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,		/* 0 */
59 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,		/* 0 */
60 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,		/* 1 */
61 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,		/* 1 */
62 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,		/* 2 */
63 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,		/* 2 */
64 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,		/* 3 */
65 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,		/* 3 */
66 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,		/* 4 */
67 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,		/* 4 */
68 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,		/* 5 */
69 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,		/* 5 */
70 	0x00,  'A',  'B',  'C',  'D',  'E',  'F',  'G',		/* 6 */
71 	 'H',  'I',  'J',  'K',  'L',  'M',  'N',  'O',		/* 6 */
72 	 'P',  'Q',  'R',  'S',  'T',  'U',  'V',  'W',		/* 7 */
73 	 'X',  'Y',  'Z', 0x00, 0x00, 0x00, 0x00, 0x00,		/* 7 */
74 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,		/* 8 */
75 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,		/* 8 */
76 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,		/* 9 */
77 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,		/* 9 */
78 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,		/* a */
79 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,		/* a */
80 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,		/* b */
81 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,		/* b */
82 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,		/* c */
83 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,		/* c */
84 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,		/* d */
85 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,		/* d */
86 	0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7,		/* e */
87 	0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf,		/* e */
88 	0xd0, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0x00,		/* f */
89 	0xd8, 0xd9, 0xda, 0xdb, 0xdc, 0xdd, 0xde, 0x00,		/* f */
90 };
91 
92 static int qcmp_name(const void *, const void *);
93 static int qcmp_ksym(const void *, const void *);
94 static int bcmp_name(const void *, const void *);
95 static int bcmp_ksym(const void *, const void *);
96 
97 static void sort_ksym_tab(void);
98 
99 static int
100 qcmp_name(const void *a, const void *b)
101 {
102 
103 	return strcmp(((const struct ksym *) a)->name,
104 	    ((const struct ksym *) b)->name);
105 }
106 
107 static int
108 qcmp_ksym(const void *a, const void *b)
109 {
110 
111 	return ((const struct ksym *) b)->value -
112 	    ((const struct ksym *) a)->value;
113 }
114 
115 static int
116 bcmp_name(const void *a, const void *b)
117 {
118 
119 	return strcmp((const char *) a, ((const struct ksym *) b)->name);
120 }
121 
122 static int
123 bcmp_ksym(const void *a, const void *b)
124 {
125 
126 	return ((const struct ksym *) b)->value - *((const int *) a);
127 }
128 
129 static void
130 sort_ksym_tab(void)
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 const char *
144 ksym2name(int k)
145 {
146 	static char tmp[20];
147 	struct ksym *r;
148 
149 	if (first_time)
150 		sort_ksym_tab();
151 
152 	r = bsearch(&k, ksym_tab_by_ksym,
153 		    NUMKSYMS, sizeof(struct ksym), bcmp_ksym);
154 
155 	if (r != NULL)
156 		return r->name;
157 	else {
158 		(void)snprintf(tmp, sizeof(tmp), "unknown_%d", k);
159 		return tmp;
160 	}
161 }
162 
163 int
164 name2ksym(char *n)
165 {
166 	int res;
167 	struct ksym *r;
168 
169 	if (first_time)
170 		sort_ksym_tab();
171 
172 	r = bsearch(n, ksym_tab_by_name,
173 		    NUMKSYMS, sizeof(struct ksym), bcmp_name);
174 
175 	if (r != NULL)
176 		return r->value;
177 	else if (sscanf(n, "unknown_%d", &res) == 1)
178 		return res;
179 	else
180 		return -1;
181 }
182 
183 keysym_t
184 ksym_upcase(keysym_t ksym)
185 {
186 
187 	if (ksym >= KS_f1 && ksym <= KS_f20)
188 		return KS_F1 - KS_f1 + ksym;
189 
190 	if (KS_GROUP(ksym) == KS_GROUP_Ascii && ksym <= 0xff &&
191 	    latin1_to_upper[ksym] != 0x00)
192 		return latin1_to_upper[ksym];
193 
194 	return ksym;
195 }
196