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