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