xref: /openbsd/sbin/wsconsctl/keyboard.c (revision 404b540a)
1 /*	$OpenBSD: keyboard.c,v 1.7 2009/07/15 20:32:28 martynas 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 int
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 		return 1;
143 	}
144 
145 	dfbell.which = 0;
146 	if (field_by_value(keyboard_field_tab, &dfbell.pitch)->flags & FLG_SET)
147 		dfbell.which |= WSKBD_BELL_DOPITCH;
148 	if (field_by_value(keyboard_field_tab, &dfbell.period)->flags & FLG_SET)
149 		dfbell.which |= WSKBD_BELL_DOPERIOD;
150 	if (field_by_value(keyboard_field_tab, &dfbell.volume)->flags & FLG_SET)
151 		dfbell.which |= WSKBD_BELL_DOVOLUME;
152 	if (dfbell.which != 0 &&
153 	    ioctl(fd, WSKBDIO_SETDEFAULTBELL, &dfbell) < 0) {
154 		warn("WSKBDIO_SETDEFAULTBELL");
155 		return 1;
156 	}
157 
158 	if (field_by_value(keyboard_field_tab, &kbmap)->flags & FLG_SET) {
159 		if (ioctl(fd, WSKBDIO_SETMAP, &kbmap) < 0) {
160 			warn("WSKBDIO_SETMAP");
161 			return 1;
162 		}
163 	}
164 
165 	repeat.which = 0;
166 	if (field_by_value(keyboard_field_tab, &repeat.del1)->flags & FLG_SET)
167 		repeat.which |= WSKBD_KEYREPEAT_DODEL1;
168 	if (field_by_value(keyboard_field_tab, &repeat.delN)->flags & FLG_SET)
169 		repeat.which |= WSKBD_KEYREPEAT_DODELN;
170 	if (repeat.which != 0 &&
171 	    ioctl(fd, WSKBDIO_SETKEYREPEAT, &repeat) < 0) {
172 		warn("WSKBDIO_SETKEYREPEAT");
173 		return 1;
174 	}
175 
176 	dfrepeat.which = 0;
177 	if (field_by_value(keyboard_field_tab, &dfrepeat.del1)->flags & FLG_SET)
178 		dfrepeat.which |= WSKBD_KEYREPEAT_DODEL1;
179 	if (field_by_value(keyboard_field_tab, &dfrepeat.delN)->flags & FLG_SET)
180 		dfrepeat.which |= WSKBD_KEYREPEAT_DODELN;
181 	if (dfrepeat.which != 0 &&
182 	    ioctl(fd, WSKBDIO_SETDEFAULTKEYREPEAT, &dfrepeat) < 0) {
183 		warn("WSKBDIO_SETDEFAULTKEYREPEAT");
184 		return 1;
185 	}
186 
187 	if (field_by_value(keyboard_field_tab, &ledstate)->flags & FLG_SET) {
188 		if (ioctl(fd, WSKBDIO_SETLEDS, &ledstate) < 0) {
189 			warn("WSKBDIO_SETLEDS");
190 			return 1;
191 		}
192 	}
193 
194 	if (field_by_value(keyboard_field_tab, &kbdencoding)->flags & FLG_SET) {
195 		if (ioctl(fd, WSKBDIO_SETENCODING, &kbdencoding) < 0) {
196 			warn("WSKBDIO_SETENCODING");
197 			return 1;
198 		}
199 	}
200 
201 	return 0;
202 }
203