keysym.c revision 1.6 1 1.6 christos /* $NetBSD: keysym.c,v 1.6 2005/06/26 22:45:50 christos 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.4 matt #include <string.h>
43 1.1 hannken #include <unistd.h>
44 1.1 hannken #include "keysym.h"
45 1.1 hannken #include "wsconsctl.h"
46 1.1 hannken
47 1.1 hannken #define NUMKSYMS (sizeof(ksym_tab_by_name)/sizeof(ksym_tab_by_name[0]))
48 1.1 hannken
49 1.1 hannken static int first_time = 1;
50 1.1 hannken static struct ksym ksym_tab_by_ksym[NUMKSYMS];
51 1.1 hannken
52 1.1 hannken /* copied from dev/wscons/wskbdutil.c ... */
53 1.1 hannken
54 1.1 hannken static const u_char latin1_to_upper[256] = {
55 1.1 hannken /* 0 8 1 9 2 a 3 b 4 c 5 d 6 e 7 f */
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, /* 0 */
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, /* 1 */
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, /* 2 */
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, /* 3 */
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, /* 4 */
66 1.1 hannken 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 5 */
67 1.1 hannken 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 5 */
68 1.1 hannken 0x00, 'A', 'B', 'C', 'D', 'E', 'F', 'G', /* 6 */
69 1.1 hannken 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', /* 6 */
70 1.1 hannken 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', /* 7 */
71 1.1 hannken 'X', 'Y', 'Z', 0x00, 0x00, 0x00, 0x00, 0x00, /* 7 */
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, /* 8 */
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, /* 9 */
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, /* a */
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, /* b */
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, /* c */
82 1.1 hannken 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* d */
83 1.1 hannken 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* d */
84 1.1 hannken 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7, /* e */
85 1.1 hannken 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf, /* e */
86 1.1 hannken 0xd0, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0x00, /* f */
87 1.1 hannken 0xd8, 0xd9, 0xda, 0xdb, 0xdc, 0xdd, 0xde, 0x00, /* f */
88 1.1 hannken };
89 1.1 hannken
90 1.5 xtraeme static int qcmp_name(const void *, const void *);
91 1.5 xtraeme static int qcmp_ksym(const void *, const void *);
92 1.5 xtraeme static int bcmp_name(const void *, const void *);
93 1.5 xtraeme static int bcmp_ksym(const void *, const void *);
94 1.1 hannken
95 1.5 xtraeme static void sort_ksym_tab(void);
96 1.1 hannken
97 1.1 hannken static int
98 1.5 xtraeme qcmp_name(const void *a, const void *b)
99 1.1 hannken {
100 1.6 christos return(strcmp(((const struct ksym *) a)->name,
101 1.6 christos ((const struct ksym *) b)->name));
102 1.1 hannken }
103 1.1 hannken
104 1.1 hannken static int
105 1.5 xtraeme qcmp_ksym(const void *a, const void *b)
106 1.1 hannken {
107 1.6 christos return(((const struct ksym *) b)->value -
108 1.6 christos ((const struct ksym *) a)->value);
109 1.1 hannken }
110 1.1 hannken
111 1.1 hannken static int
112 1.5 xtraeme bcmp_name(const void *a, const void *b)
113 1.1 hannken {
114 1.6 christos return(strcmp((const char *) a, ((const struct ksym *) b)->name));
115 1.1 hannken }
116 1.1 hannken
117 1.1 hannken static int
118 1.5 xtraeme bcmp_ksym(const void *a, const void *b)
119 1.1 hannken {
120 1.6 christos return(((const struct ksym *) b)->value - *((const int *) a));
121 1.1 hannken }
122 1.1 hannken
123 1.1 hannken static void
124 1.5 xtraeme sort_ksym_tab(void)
125 1.1 hannken {
126 1.1 hannken int i;
127 1.1 hannken
128 1.1 hannken for (i = 0; i < NUMKSYMS; i++)
129 1.1 hannken ksym_tab_by_ksym[i] = ksym_tab_by_name[i];
130 1.1 hannken
131 1.1 hannken qsort(ksym_tab_by_name, NUMKSYMS, sizeof(struct ksym), qcmp_name);
132 1.1 hannken qsort(ksym_tab_by_ksym, NUMKSYMS, sizeof(struct ksym), qcmp_ksym);
133 1.1 hannken
134 1.1 hannken first_time = 0;
135 1.1 hannken }
136 1.1 hannken
137 1.6 christos const char *
138 1.5 xtraeme ksym2name(int k)
139 1.1 hannken {
140 1.1 hannken static char tmp[20];
141 1.1 hannken struct ksym *r;
142 1.1 hannken
143 1.1 hannken if (first_time)
144 1.1 hannken sort_ksym_tab();
145 1.1 hannken
146 1.2 nathanw r = bsearch(&k, ksym_tab_by_ksym,
147 1.1 hannken NUMKSYMS, sizeof(struct ksym), bcmp_ksym);
148 1.1 hannken
149 1.1 hannken if (r != NULL)
150 1.1 hannken return(r->name);
151 1.1 hannken else {
152 1.1 hannken snprintf(tmp, sizeof(tmp), "unknown_%d", k);
153 1.1 hannken return(tmp);
154 1.1 hannken }
155 1.1 hannken }
156 1.1 hannken
157 1.1 hannken int
158 1.5 xtraeme name2ksym(char *n)
159 1.1 hannken {
160 1.3 hannken int res;
161 1.1 hannken struct ksym *r;
162 1.1 hannken
163 1.1 hannken if (first_time)
164 1.1 hannken sort_ksym_tab();
165 1.1 hannken
166 1.1 hannken r = bsearch(n, ksym_tab_by_name,
167 1.1 hannken NUMKSYMS, sizeof(struct ksym), bcmp_name);
168 1.1 hannken
169 1.1 hannken if (r != NULL)
170 1.1 hannken return(r->value);
171 1.3 hannken else if (sscanf(n, "unknown_%d", &res) == 1)
172 1.3 hannken return(res);
173 1.1 hannken else
174 1.1 hannken return(-1);
175 1.1 hannken }
176 1.1 hannken
177 1.1 hannken keysym_t
178 1.5 xtraeme ksym_upcase(keysym_t ksym)
179 1.1 hannken {
180 1.1 hannken if (ksym >= KS_f1 && ksym <= KS_f20)
181 1.1 hannken return(KS_F1 - KS_f1 + ksym);
182 1.1 hannken
183 1.1 hannken if (KS_GROUP(ksym) == KS_GROUP_Ascii && ksym <= 0xff &&
184 1.1 hannken latin1_to_upper[ksym] != 0x00)
185 1.1 hannken return(latin1_to_upper[ksym]);
186 1.1 hannken
187 1.1 hannken return(ksym);
188 1.1 hannken }
189