xref: /openbsd-src/sbin/wsconsctl/keyboard.c (revision a28daedfc357b214be5c701aa8ba8adb29a7f1c2)
1 /*	$OpenBSD: keyboard.c,v 1.6 2008/06/26 05:42:06 ray Exp $	*/
2 /*	$NetBSD: keyboard.c 1.1 1998/12/28 14:01:17 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 <sys/ioctl.h>
34 #include <sys/time.h>
35 #include <dev/wscons/wsksymdef.h>
36 #include <dev/wscons/wsconsio.h>
37 #include <err.h>
38 #include "wsconsctl.h"
39 
40 static int kbtype;
41 static struct wskbd_bell_data bell;
42 static struct wskbd_bell_data dfbell;
43 static struct wscons_keymap mapdata[KS_NUMKEYCODES];
44 struct wskbd_map_data kbmap = { KS_NUMKEYCODES, mapdata };	/* used in map_parse.y
45 							   and in util.c */
46 static struct wskbd_keyrepeat_data repeat;
47 static struct wskbd_keyrepeat_data dfrepeat;
48 static int ledstate;
49 static int kbdencoding;
50 
51 struct field keyboard_field_tab[] = {
52     { "type",			&kbtype,	FMT_KBDTYPE,	FLG_RDONLY },
53     { "bell.pitch",		&bell.pitch,	FMT_UINT,	FLG_MODIFY },
54     { "bell.period",		&bell.period,	FMT_UINT,	FLG_MODIFY },
55     { "bell.volume",		&bell.volume,	FMT_UINT,	FLG_MODIFY },
56     { "bell.pitch.default",	&dfbell.pitch,	FMT_UINT,	FLG_MODIFY },
57     { "bell.period.default",	&dfbell.period,	FMT_UINT,	FLG_MODIFY },
58     { "bell.volume.default",	&dfbell.volume,	FMT_UINT,	FLG_MODIFY },
59     { "map",			&kbmap,		FMT_KBMAP,	FLG_MODIFY|FLG_NOAUTO },
60     { "repeat.del1",		&repeat.del1,	FMT_UINT,	FLG_MODIFY },
61     { "repeat.deln",		&repeat.delN,	FMT_UINT,	FLG_MODIFY },
62     { "repeat.del1.default",	&dfrepeat.del1,	FMT_UINT,	FLG_MODIFY },
63     { "repeat.deln.default",	&dfrepeat.delN,	FMT_UINT,	FLG_MODIFY },
64     { "ledstate",		&ledstate,	FMT_UINT,	0 },
65     { "encoding",		&kbdencoding,	FMT_KBDENC,	FLG_MODIFY },
66     { NULL }
67 };
68 
69 void
70 keyboard_get_values(const char *pre, int fd)
71 {
72 	if (field_by_value(keyboard_field_tab, &kbtype)->flags & FLG_GET)
73 		if (ioctl(fd, WSKBDIO_GTYPE, &kbtype) < 0)
74 			warn("WSKBDIO_GTYPE");
75 
76 	bell.which = 0;
77 	if (field_by_value(keyboard_field_tab, &bell.pitch)->flags & FLG_GET)
78 		bell.which |= WSKBD_BELL_DOPITCH;
79 	if (field_by_value(keyboard_field_tab, &bell.period)->flags & FLG_GET)
80 		bell.which |= WSKBD_BELL_DOPERIOD;
81 	if (field_by_value(keyboard_field_tab, &bell.volume)->flags & FLG_GET)
82 		bell.which |= WSKBD_BELL_DOVOLUME;
83 	if (bell.which != 0 && ioctl(fd, WSKBDIO_GETBELL, &bell) < 0)
84 		warn("WSKBDIO_GETBELL");
85 
86 	dfbell.which = 0;
87 	if (field_by_value(keyboard_field_tab, &dfbell.pitch)->flags & FLG_GET)
88 		dfbell.which |= WSKBD_BELL_DOPITCH;
89 	if (field_by_value(keyboard_field_tab, &dfbell.period)->flags & FLG_GET)
90 		dfbell.which |= WSKBD_BELL_DOPERIOD;
91 	if (field_by_value(keyboard_field_tab, &dfbell.volume)->flags & FLG_GET)
92 		dfbell.which |= WSKBD_BELL_DOVOLUME;
93 	if (dfbell.which != 0 &&
94 	    ioctl(fd, WSKBDIO_GETDEFAULTBELL, &dfbell) < 0)
95 		warn("WSKBDIO_GETDEFAULTBELL");
96 
97 	if (field_by_value(keyboard_field_tab, &kbmap)->flags & FLG_GET) {
98 		kbmap.maplen = KS_NUMKEYCODES;
99 		if (ioctl(fd, WSKBDIO_GETMAP, &kbmap) < 0)
100 			warn("WSKBDIO_GETMAP");
101 	}
102 
103 	repeat.which = 0;
104 	if (field_by_value(keyboard_field_tab, &repeat.del1)->flags & FLG_GET)
105 		repeat.which |= WSKBD_KEYREPEAT_DODEL1;
106 	if (field_by_value(keyboard_field_tab, &repeat.delN)->flags & FLG_GET)
107 		repeat.which |= WSKBD_KEYREPEAT_DODELN;
108 	if (repeat.which != 0 &&
109 	    ioctl(fd, WSKBDIO_GETKEYREPEAT, &repeat) < 0)
110 		warn("WSKBDIO_GETKEYREPEAT");
111 
112 	dfrepeat.which = 0;
113 	if (field_by_value(keyboard_field_tab, &dfrepeat.del1)->flags & FLG_GET)
114 		dfrepeat.which |= WSKBD_KEYREPEAT_DODEL1;
115 	if (field_by_value(keyboard_field_tab, &dfrepeat.delN)->flags & FLG_GET)
116 		dfrepeat.which |= WSKBD_KEYREPEAT_DODELN;
117 	if (dfrepeat.which != 0 &&
118 	    ioctl(fd, WSKBDIO_GETKEYREPEAT, &dfrepeat) < 0)
119 		warn("WSKBDIO_GETKEYREPEAT");
120 
121 	if (field_by_value(keyboard_field_tab, &ledstate)->flags & FLG_GET)
122 		if (ioctl(fd, WSKBDIO_GETLEDS, &ledstate) < 0)
123 			warn("WSKBDIO_GETLEDS");
124 
125 	if (field_by_value(keyboard_field_tab, &kbdencoding)->flags & FLG_GET)
126 		if (ioctl(fd, WSKBDIO_GETENCODING, &kbdencoding) < 0)
127 			warn("WSKBDIO_GETENCODING");
128 }
129 
130 void
131 keyboard_put_values(const char *pre, int fd)
132 {
133 	bell.which = 0;
134 	if (field_by_value(keyboard_field_tab, &bell.pitch)->flags & FLG_SET)
135 		bell.which |= WSKBD_BELL_DOPITCH;
136 	if (field_by_value(keyboard_field_tab, &bell.period)->flags & FLG_SET)
137 		bell.which |= WSKBD_BELL_DOPERIOD;
138 	if (field_by_value(keyboard_field_tab, &bell.volume)->flags & FLG_SET)
139 		bell.which |= WSKBD_BELL_DOVOLUME;
140 	if (bell.which != 0 && ioctl(fd, WSKBDIO_SETBELL, &bell) < 0)
141 		warn("WSKBDIO_SETBELL");
142 	else {
143 		if (bell.which & WSKBD_BELL_DOPITCH)
144 			pr_field(pre, field_by_value(keyboard_field_tab,
145 			    &bell.pitch), " -> ");
146 		if (bell.which & WSKBD_BELL_DOPERIOD)
147 			pr_field(pre, field_by_value(keyboard_field_tab,
148 			    &bell.period), " -> ");
149 		if (bell.which & WSKBD_BELL_DOVOLUME)
150 			pr_field(pre, field_by_value(keyboard_field_tab,
151 			    &bell.volume), " -> ");
152 	}
153 
154 	dfbell.which = 0;
155 	if (field_by_value(keyboard_field_tab, &dfbell.pitch)->flags & FLG_SET)
156 		dfbell.which |= WSKBD_BELL_DOPITCH;
157 	if (field_by_value(keyboard_field_tab, &dfbell.period)->flags & FLG_SET)
158 		dfbell.which |= WSKBD_BELL_DOPERIOD;
159 	if (field_by_value(keyboard_field_tab, &dfbell.volume)->flags & FLG_SET)
160 		dfbell.which |= WSKBD_BELL_DOVOLUME;
161 	if (dfbell.which != 0 &&
162 	    ioctl(fd, WSKBDIO_SETDEFAULTBELL, &dfbell) < 0)
163 		warn("WSKBDIO_SETDEFAULTBELL");
164 	else {
165 		if (dfbell.which & WSKBD_BELL_DOPITCH)
166 			pr_field(pre, field_by_value(keyboard_field_tab,
167 			    &dfbell.pitch), " -> ");
168 		if (dfbell.which & WSKBD_BELL_DOPERIOD)
169 			pr_field(pre, field_by_value(keyboard_field_tab,
170 			    &dfbell.period), " -> ");
171 		if (dfbell.which & WSKBD_BELL_DOVOLUME)
172 			pr_field(pre, field_by_value(keyboard_field_tab,
173 			    &dfbell.volume), " -> ");
174 	}
175 
176 	if (field_by_value(keyboard_field_tab, &kbmap)->flags & FLG_SET) {
177 		if (ioctl(fd, WSKBDIO_SETMAP, &kbmap) < 0)
178 			warn("WSKBDIO_SETMAP");
179 	}
180 
181 	repeat.which = 0;
182 	if (field_by_value(keyboard_field_tab, &repeat.del1)->flags & FLG_SET)
183 		repeat.which |= WSKBD_KEYREPEAT_DODEL1;
184 	if (field_by_value(keyboard_field_tab, &repeat.delN)->flags & FLG_SET)
185 		repeat.which |= WSKBD_KEYREPEAT_DODELN;
186 	if (repeat.which != 0 &&
187 	    ioctl(fd, WSKBDIO_SETKEYREPEAT, &repeat) < 0)
188 		warn("WSKBDIO_SETKEYREPEAT");
189 	else {
190 		if (repeat.which & WSKBD_KEYREPEAT_DODEL1)
191 			pr_field(pre, field_by_value(keyboard_field_tab,
192 			    &repeat.del1), " -> ");
193 		if (repeat.which & WSKBD_KEYREPEAT_DODELN)
194 			pr_field(pre, field_by_value(keyboard_field_tab,
195 			    &repeat.delN), " -> ");
196 	}
197 
198 	dfrepeat.which = 0;
199 	if (field_by_value(keyboard_field_tab, &dfrepeat.del1)->flags & FLG_SET)
200 		dfrepeat.which |= WSKBD_KEYREPEAT_DODEL1;
201 	if (field_by_value(keyboard_field_tab, &dfrepeat.delN)->flags & FLG_SET)
202 		dfrepeat.which |= WSKBD_KEYREPEAT_DODELN;
203 	if (dfrepeat.which != 0 &&
204 	    ioctl(fd, WSKBDIO_SETDEFAULTKEYREPEAT, &dfrepeat) < 0)
205 		warn("WSKBDIO_SETDEFAULTKEYREPEAT");
206 	else {
207 		if (dfrepeat.which &WSKBD_KEYREPEAT_DODEL1)
208 			pr_field(pre, field_by_value(keyboard_field_tab,
209 			    &dfrepeat.del1), " -> ");
210 		if (dfrepeat.which & WSKBD_KEYREPEAT_DODELN)
211 			pr_field(pre, field_by_value(keyboard_field_tab,
212 			    &dfrepeat.delN), " -> ");
213 	}
214 
215 	if (field_by_value(keyboard_field_tab, &ledstate)->flags & FLG_SET) {
216 		if (ioctl(fd, WSKBDIO_SETLEDS, &ledstate) < 0)
217 			warn("WSKBDIO_SETLEDS");
218 		else {
219 			pr_field(pre, field_by_value(keyboard_field_tab,
220 			    &ledstate), " -> ");
221 		}
222 	}
223 
224 	if (field_by_value(keyboard_field_tab, &kbdencoding)->flags & FLG_SET) {
225 		if (ioctl(fd, WSKBDIO_SETENCODING, &kbdencoding) < 0)
226 			warn("WSKBDIO_SETENCODING");
227 		else {
228 			pr_field(pre, field_by_value(keyboard_field_tab,
229 			    &kbdencoding), " -> ");
230 		}
231 	}
232 }
233