xref: /openbsd/sbin/wsconsctl/keysym.c (revision 76d0caae)
1 /*	$OpenBSD: keysym.c,v 1.8 2015/02/15 01:56:16 tedu 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 void sort_ksym_tab(void);
86 
87 static int
88 cmp_name(const void *a, const void *b)
89 {
90 	return(strcmp(((struct ksym *) a)->name, ((struct ksym *) b)->name));
91 }
92 
93 static int
94 cmp_ksym(const void *a, const void *b)
95 {
96 	int i;
97 
98 	i=((struct ksym *) b)->value - ((struct ksym *) a)->value;
99 	if (i == 0) {
100 		i=((struct ksym *) a)->enc - ((struct ksym *) b)->enc;
101 	}
102 	return(i);
103 }
104 
105 static void
106 sort_ksym_tab(void)
107 {
108 	int i;
109 
110 	for (i = 0; i < NUMKSYMS; i++)
111 		ksym_tab_by_ksym[i] = ksym_tab_by_name[i];
112 
113 	qsort(ksym_tab_by_name, NUMKSYMS, sizeof(struct ksym), cmp_name);
114 	qsort(ksym_tab_by_ksym, NUMKSYMS, sizeof(struct ksym), cmp_ksym);
115 
116 	first_time = 0;
117 }
118 
119 char *
120 ksym2name(int k)
121 {
122 	static char tmp[20];
123 	struct ksym *r;
124 	struct ksym key;
125 
126 	if (first_time)
127 		sort_ksym_tab();
128 
129 	key.value = k;
130 	key.enc = encoding;
131 	r = bsearch(&key, ksym_tab_by_ksym,
132 		    NUMKSYMS, sizeof(struct ksym), cmp_ksym);
133 
134 	if (r == NULL) {
135 		key.enc = KEYSYM_ENC_ISO;
136 		r = bsearch(&key, ksym_tab_by_ksym,
137 			    NUMKSYMS, sizeof(struct ksym), cmp_ksym);
138 	}
139 
140 	if (r != NULL)
141 		return(r->name);
142 	else {
143 		snprintf(tmp, sizeof(tmp), "unknown_%d", k);
144 		return(tmp);
145 	}
146 }
147 
148 int
149 name2ksym(char *n)
150 {
151 	int res;
152 	struct ksym *r;
153 	struct ksym key;
154 
155 	if (first_time)
156 		sort_ksym_tab();
157 
158 	key.name = n;
159 	r = bsearch(&key, ksym_tab_by_name,
160 		    NUMKSYMS, sizeof(struct ksym), cmp_name);
161 
162 	if (r != NULL)
163 		return(r->value);
164 	else if (sscanf(n, "unknown_%d", &res) == 1)
165 		return(res);
166 	else
167 		return(-1);
168 }
169 
170 void
171 ksymenc(int enc)
172 {
173 	switch(KB_ENCODING(enc)) {
174 	case KB_HU:
175 	case KB_PL:
176 	case KB_SI:
177 		encoding=KEYSYM_ENC_L2;
178 		break;
179 	case KB_TR:
180 		encoding=KEYSYM_ENC_L5;
181 		break;
182 	case KB_LT:
183 	case KB_LV:
184 		encoding=KEYSYM_ENC_L7;
185 		break;
186 	case KB_RU:
187 	case KB_UA:
188 		encoding=KEYSYM_ENC_KOI;
189 		break;
190 	default:
191 		encoding=KEYSYM_ENC_ISO;
192 		break;
193 	}
194 }
195 
196 keysym_t
197 ksym_upcase(keysym_t ksym)
198 {
199 	if (ksym >= KS_f1 && ksym <= KS_f20)
200 		return(KS_F1 - KS_f1 + ksym);
201 
202 	if (KS_GROUP(ksym) == KS_GROUP_Ascii && ksym <= 0xff &&
203 	    latin1_to_upper[ksym] != 0x00)
204 		return(latin1_to_upper[ksym]);
205 
206 	return(ksym);
207 }
208