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