xref: /netbsd-src/sbin/wsconsctl/keysym.c (revision 821d4396bdd8fe08a9bf98f7b244f0f6a074e838)
1 /*	$NetBSD: keysym.c,v 1.10 2010/01/29 09:49:34 drochner Exp $ */
2 
3 /*-
4  * Copyright (c) 1998 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Juergen Hannken-Illjes.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #include <dev/wscons/wsksymdef.h>
33 
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #include <unistd.h>
38 
39 #include "keysym.h"
40 #include "wsconsctl.h"
41 
42 #define NUMKSYMS	(sizeof(ksym_tab_by_name)/sizeof(ksym_tab_by_name[0]))
43 
44 static int first_time = 1;
45 static struct ksym ksym_tab_by_ksym[NUMKSYMS];
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 
90 static void sort_ksym_tab(void);
91 
92 static int
qcmp_name(const void * a,const void * b)93 qcmp_name(const void *a, const void *b)
94 {
95 
96 	return strcmp(((const struct ksym *) a)->name,
97 	    ((const struct ksym *) b)->name);
98 }
99 
100 static int
qcmp_ksym(const void * a,const void * b)101 qcmp_ksym(const void *a, const void *b)
102 {
103 
104 	return ((const struct ksym *) b)->value -
105 	    ((const struct ksym *) a)->value;
106 }
107 
108 static int
bcmp_name(const void * a,const void * b)109 bcmp_name(const void *a, const void *b)
110 {
111 
112 	return strcmp((const char *) a, ((const struct ksym *) b)->name);
113 }
114 
115 static int
bcmp_ksym(const void * a,const void * b)116 bcmp_ksym(const void *a, const void *b)
117 {
118 
119 	return ((const struct ksym *) b)->value - *((const int *) a);
120 }
121 
122 static void
sort_ksym_tab(void)123 sort_ksym_tab(void)
124 {
125 	size_t 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 const 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 		(void)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 
180 	if (ksym >= KS_f1 && ksym <= KS_f20)
181 		return KS_F1 - KS_f1 + ksym;
182 
183 	if (KS_GROUP(ksym) == KS_GROUP_Plain && ksym <= 0xff &&
184 	    latin1_to_upper[ksym] != 0x00)
185 		return latin1_to_upper[ksym];
186 
187 	return ksym;
188 }
189