xref: /openbsd/sbin/wsconsctl/keysym.c (revision 891d7ab6)
1 /*	$OpenBSD: keysym.c,v 1.6 2010/06/28 20:40:39 maja Exp $	*/
2 /*	$NetBSD: keysym.c,v 1.3 1999/02/08 11:08:23 hannken Exp $ */
3 
4 /*-
5  * Copyright (c) 1998 The NetBSD Foundation, Inc.
6  * All rights reserved.
7  *
8  * This code is derived from software contributed to The NetBSD Foundation
9  * by Juergen Hannken-Illjes.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
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  *
20  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 #include <dev/wscons/wsksymdef.h>
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #include <unistd.h>
38 #include "keysym.h"
39 #include "wsconsctl.h"
40 
41 #define NUMKSYMS	(sizeof(ksym_tab_by_name)/sizeof(ksym_tab_by_name[0]))
42 
43 static int first_time = 1;
44 static struct ksym ksym_tab_by_ksym[NUMKSYMS];
45 static int encoding = KEYSYM_ENC_ISO;
46 
47 /* copied from dev/wscons/wskbdutil.c ... */
48 
49 static const u_char latin1_to_upper[256] = {
50 /*      0  8  1  9  2  a  3  b  4  c  5  d  6  e  7  f               */
51 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,		/* 0 */
52 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,		/* 0 */
53 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,		/* 1 */
54 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,		/* 1 */
55 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,		/* 2 */
56 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,		/* 2 */
57 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,		/* 3 */
58 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,		/* 3 */
59 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,		/* 4 */
60 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,		/* 4 */
61 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,		/* 5 */
62 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,		/* 5 */
63 	0x00,  'A',  'B',  'C',  'D',  'E',  'F',  'G',		/* 6 */
64 	 'H',  'I',  'J',  'K',  'L',  'M',  'N',  'O',		/* 6 */
65 	 'P',  'Q',  'R',  'S',  'T',  'U',  'V',  'W',		/* 7 */
66 	 'X',  'Y',  'Z', 0x00, 0x00, 0x00, 0x00, 0x00,		/* 7 */
67 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,		/* 8 */
68 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,		/* 8 */
69 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,		/* 9 */
70 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,		/* 9 */
71 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,		/* a */
72 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,		/* a */
73 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,		/* b */
74 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,		/* b */
75 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,		/* c */
76 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,		/* c */
77 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,		/* d */
78 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,		/* d */
79 	0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7,		/* e */
80 	0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf,		/* e */
81 	0xd0, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0x00,		/* f */
82 	0xd8, 0xd9, 0xda, 0xdb, 0xdc, 0xdd, 0xde, 0x00,		/* f */
83 };
84 
85 static int qcmp_name(const void *, const void *);
86 static int qcmp_ksym(const void *, const void *);
87 static int bcmp_name(const void *, const void *);
88 static int bcmp_ksym(const void *, const void *);
89 static int bcmp_ksym_enc(const void *, const void *);
90 
91 static void sort_ksym_tab(void);
92 
93 static int
94 qcmp_name(const void *a, const void *b)
95 {
96 	return(strcmp(((struct ksym *) a)->name, ((struct ksym *) b)->name));
97 }
98 
99 static int
100 qcmp_ksym(const void *a, const void *b)
101 {
102 	int i;
103 
104 	i=((struct ksym *) b)->value - ((struct ksym *) a)->value;
105 	if (i == 0) {
106 		i=((struct ksym *) a)->enc - ((struct ksym *) b)->enc;
107 	}
108 	return(i);
109 }
110 
111 static int
112 bcmp_name(const void *a, const void *b)
113 {
114 	return(strcmp((char *) a, ((struct ksym *) b)->name));
115 }
116 
117 static int
118 bcmp_ksym(const void *a, const void *b)
119 {
120 	int i;
121 
122 	i=((struct ksym *) b)->value - *((int *) a);
123 	if (i == 0) {
124 		i=KEYSYM_ENC_ISO - ((struct ksym *) b)->enc;
125 	}
126 	return(i);
127 }
128 
129 static int
130 bcmp_ksym_enc(const void *a, const void *b)
131 {
132 	int i;
133 
134 	i=((struct ksym *) b)->value - *((int *) a);
135 	if (i == 0) {
136 		i=encoding - ((struct ksym *) b)->enc;
137 	}
138 	return(i);
139 }
140 
141 static void
142 sort_ksym_tab(void)
143 {
144 	int i;
145 
146 	for (i = 0; i < NUMKSYMS; i++)
147 		ksym_tab_by_ksym[i] = ksym_tab_by_name[i];
148 
149 	qsort(ksym_tab_by_name, NUMKSYMS, sizeof(struct ksym), qcmp_name);
150 	qsort(ksym_tab_by_ksym, NUMKSYMS, sizeof(struct ksym), qcmp_ksym);
151 
152 	first_time = 0;
153 }
154 
155 char *
156 ksym2name(int k)
157 {
158 	static char tmp[20];
159 	struct ksym *r;
160 
161 	if (first_time)
162 		sort_ksym_tab();
163 
164 	r = bsearch(&k, ksym_tab_by_ksym,
165 		    NUMKSYMS, sizeof(struct ksym), bcmp_ksym_enc);
166 
167 	if (r == NULL) {
168 		r = bsearch(&k, ksym_tab_by_ksym,
169 			    NUMKSYMS, sizeof(struct ksym), bcmp_ksym);
170 	}
171 
172 	if (r != NULL)
173 		return(r->name);
174 	else {
175 		snprintf(tmp, sizeof(tmp), "unknown_%d", k);
176 		return(tmp);
177 	}
178 }
179 
180 int
181 name2ksym(char *n)
182 {
183 	int res;
184 	struct ksym *r;
185 
186 	if (first_time)
187 		sort_ksym_tab();
188 
189 	r = bsearch(n, ksym_tab_by_name,
190 		    NUMKSYMS, sizeof(struct ksym), bcmp_name);
191 
192 	if (r != NULL)
193 		return(r->value);
194 	else if (sscanf(n, "unknown_%d", &res) == 1)
195 		return(res);
196 	else
197 		return(-1);
198 }
199 
200 void
201 ksymenc(int enc)
202 {
203 	switch(KB_ENCODING(enc)) {
204 	case KB_HU:
205 	case KB_PL:
206 	case KB_SI:
207 		encoding=KEYSYM_ENC_L2;
208 		break;
209 	case KB_TR:
210 		encoding=KEYSYM_ENC_L5;
211 		break;
212 	case KB_LT:
213 	case KB_LV:
214 		encoding=KEYSYM_ENC_L7;
215 		break;
216 	case KB_RU:
217 	case KB_UA:
218 		encoding=KEYSYM_ENC_KOI;
219 		break;
220 	default:
221 		encoding=KEYSYM_ENC_ISO;
222 		break;
223 	}
224 }
225 
226 keysym_t
227 ksym_upcase(keysym_t ksym)
228 {
229 	if (ksym >= KS_f1 && ksym <= KS_f20)
230 		return(KS_F1 - KS_f1 + ksym);
231 
232 	if (KS_GROUP(ksym) == KS_GROUP_Ascii && ksym <= 0xff &&
233 	    latin1_to_upper[ksym] != 0x00)
234 		return(latin1_to_upper[ksym]);
235 
236 	return(ksym);
237 }
238