1 /* $OpenBSD: keyboard.c,v 1.2 2001/06/30 02:12:57 mickey 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 * 3. All advertising materials mentioning features or use of this software 20 * must display the following acknowledgement: 21 * This product includes software developed by the NetBSD 22 * Foundation, Inc. and its contributors. 23 * 4. Neither the name of The NetBSD Foundation nor the names of its 24 * contributors may be used to endorse or promote products derived 25 * from this software without specific prior written permission. 26 * 27 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 28 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 29 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 30 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 31 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 32 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 33 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 34 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 35 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 36 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 37 * POSSIBILITY OF SUCH DAMAGE. 38 */ 39 40 #include <sys/ioctl.h> 41 #include <sys/time.h> 42 #include <dev/wscons/wsksymdef.h> 43 #include <dev/wscons/wsconsio.h> 44 #include <err.h> 45 #include "wsconsctl.h" 46 47 static int kbtype; 48 static struct wskbd_bell_data bell; 49 static struct wskbd_bell_data dfbell; 50 static struct wscons_keymap mapdata[KS_NUMKEYCODES]; 51 struct wskbd_map_data kbmap = { KS_NUMKEYCODES, mapdata }; /* used in map_parse.y 52 and in util.c */ 53 static struct wskbd_keyrepeat_data repeat; 54 static struct wskbd_keyrepeat_data dfrepeat; 55 static int ledstate; 56 static int kbdencoding; 57 58 struct field keyboard_field_tab[] = { 59 { "type", &kbtype, FMT_KBDTYPE, FLG_RDONLY }, 60 { "bell.pitch", &bell.pitch, FMT_UINT, FLG_MODIFY }, 61 { "bell.period", &bell.period, FMT_UINT, FLG_MODIFY }, 62 { "bell.volume", &bell.volume, FMT_UINT, FLG_MODIFY }, 63 { "bell.pitch.default", &dfbell.pitch, FMT_UINT, FLG_MODIFY }, 64 { "bell.period.default", &dfbell.period, FMT_UINT, FLG_MODIFY }, 65 { "bell.volume.default", &dfbell.volume, FMT_UINT, FLG_MODIFY }, 66 { "map", &kbmap, FMT_KBMAP, FLG_MODIFY|FLG_NOAUTO }, 67 { "repeat.del1", &repeat.del1, FMT_UINT, FLG_MODIFY }, 68 { "repeat.deln", &repeat.delN, FMT_UINT, FLG_MODIFY }, 69 { "repeat.del1.default", &dfrepeat.del1, FMT_UINT, FLG_MODIFY }, 70 { "repeat.deln.default", &dfrepeat.delN, FMT_UINT, FLG_MODIFY }, 71 { "ledstate", &ledstate, FMT_UINT, 0 }, 72 { "encoding", &kbdencoding, FMT_KBDENC, FLG_MODIFY }, 73 { NULL } 74 }; 75 76 void 77 keyboard_get_values(pre, fd) 78 const char *pre; 79 int fd; 80 { 81 if (field_by_value(keyboard_field_tab, &kbtype)->flags & FLG_GET) 82 if (ioctl(fd, WSKBDIO_GTYPE, &kbtype) < 0) 83 err(1, "WSKBDIO_GTYPE"); 84 85 bell.which = 0; 86 if (field_by_value(keyboard_field_tab, &bell.pitch)->flags & FLG_GET) 87 bell.which |= WSKBD_BELL_DOPITCH; 88 if (field_by_value(keyboard_field_tab, &bell.period)->flags & FLG_GET) 89 bell.which |= WSKBD_BELL_DOPERIOD; 90 if (field_by_value(keyboard_field_tab, &bell.volume)->flags & FLG_GET) 91 bell.which |= WSKBD_BELL_DOVOLUME; 92 if (bell.which != 0 && ioctl(fd, WSKBDIO_GETBELL, &bell) < 0) 93 err(1, "WSKBDIO_GETBELL"); 94 95 dfbell.which = 0; 96 if (field_by_value(keyboard_field_tab, &dfbell.pitch)->flags & FLG_GET) 97 dfbell.which |= WSKBD_BELL_DOPITCH; 98 if (field_by_value(keyboard_field_tab, &dfbell.period)->flags & FLG_GET) 99 dfbell.which |= WSKBD_BELL_DOPERIOD; 100 if (field_by_value(keyboard_field_tab, &dfbell.volume)->flags & FLG_GET) 101 dfbell.which |= WSKBD_BELL_DOVOLUME; 102 if (dfbell.which != 0 && 103 ioctl(fd, WSKBDIO_GETDEFAULTBELL, &dfbell) < 0) 104 err(1, "WSKBDIO_GETDEFAULTBELL"); 105 106 if (field_by_value(keyboard_field_tab, &kbmap)->flags & FLG_GET) { 107 kbmap.maplen = KS_NUMKEYCODES; 108 if (ioctl(fd, WSKBDIO_GETMAP, &kbmap) < 0) 109 err(1, "WSKBDIO_GETMAP"); 110 } 111 112 repeat.which = 0; 113 if (field_by_value(keyboard_field_tab, &repeat.del1)->flags & FLG_GET) 114 repeat.which |= WSKBD_KEYREPEAT_DODEL1; 115 if (field_by_value(keyboard_field_tab, &repeat.delN)->flags & FLG_GET) 116 repeat.which |= WSKBD_KEYREPEAT_DODELN; 117 if (repeat.which != 0 && 118 ioctl(fd, WSKBDIO_GETKEYREPEAT, &repeat) < 0) 119 err(1, "WSKBDIO_GETKEYREPEAT"); 120 121 dfrepeat.which = 0; 122 if (field_by_value(keyboard_field_tab, &dfrepeat.del1)->flags & FLG_GET) 123 dfrepeat.which |= WSKBD_KEYREPEAT_DODEL1; 124 if (field_by_value(keyboard_field_tab, &dfrepeat.delN)->flags & FLG_GET) 125 dfrepeat.which |= WSKBD_KEYREPEAT_DODELN; 126 if (dfrepeat.which != 0 && 127 ioctl(fd, WSKBDIO_GETKEYREPEAT, &dfrepeat) < 0) 128 err(1, "WSKBDIO_GETKEYREPEAT"); 129 130 if (field_by_value(keyboard_field_tab, &ledstate)->flags & FLG_GET) 131 if (ioctl(fd, WSKBDIO_GETLEDS, &ledstate) < 0) 132 err(1, "WSKBDIO_GETLEDS"); 133 134 if (field_by_value(keyboard_field_tab, &kbdencoding)->flags & FLG_GET) 135 if (ioctl(fd, WSKBDIO_GETENCODING, &kbdencoding) < 0) 136 err(1, "WSKBDIO_GETENCODING"); 137 } 138 139 void 140 keyboard_put_values(pre, fd) 141 const char *pre; 142 int fd; 143 { 144 bell.which = 0; 145 if (field_by_value(keyboard_field_tab, &bell.pitch)->flags & FLG_SET) 146 bell.which |= WSKBD_BELL_DOPITCH; 147 if (field_by_value(keyboard_field_tab, &bell.period)->flags & FLG_SET) 148 bell.which |= WSKBD_BELL_DOPERIOD; 149 if (field_by_value(keyboard_field_tab, &bell.volume)->flags & FLG_SET) 150 bell.which |= WSKBD_BELL_DOVOLUME; 151 if (bell.which != 0 && ioctl(fd, WSKBDIO_SETBELL, &bell) < 0) 152 err(1, "WSKBDIO_SETBELL"); 153 if (bell.which & WSKBD_BELL_DOPITCH) 154 pr_field(pre, field_by_value(keyboard_field_tab, &bell.pitch), " -> "); 155 if (bell.which & WSKBD_BELL_DOPERIOD) 156 pr_field(pre, field_by_value(keyboard_field_tab, &bell.period), " -> "); 157 if (bell.which & WSKBD_BELL_DOVOLUME) 158 pr_field(pre, field_by_value(keyboard_field_tab, &bell.volume), " -> "); 159 160 dfbell.which = 0; 161 if (field_by_value(keyboard_field_tab, &dfbell.pitch)->flags & FLG_SET) 162 dfbell.which |= WSKBD_BELL_DOPITCH; 163 if (field_by_value(keyboard_field_tab, &dfbell.period)->flags & FLG_SET) 164 dfbell.which |= WSKBD_BELL_DOPERIOD; 165 if (field_by_value(keyboard_field_tab, &dfbell.volume)->flags & FLG_SET) 166 dfbell.which |= WSKBD_BELL_DOVOLUME; 167 if (dfbell.which != 0 && 168 ioctl(fd, WSKBDIO_SETDEFAULTBELL, &dfbell) < 0) 169 err(1, "WSKBDIO_SETDEFAULTBELL"); 170 if (dfbell.which & WSKBD_BELL_DOPITCH) 171 pr_field(pre, field_by_value(keyboard_field_tab, &dfbell.pitch), " -> "); 172 if (dfbell.which & WSKBD_BELL_DOPERIOD) 173 pr_field(pre, field_by_value(keyboard_field_tab, &dfbell.period), " -> "); 174 if (dfbell.which & WSKBD_BELL_DOVOLUME) 175 pr_field(pre, field_by_value(keyboard_field_tab, &dfbell.volume), " -> "); 176 177 if (field_by_value(keyboard_field_tab, &kbmap)->flags & FLG_SET) { 178 if (ioctl(fd, WSKBDIO_SETMAP, &kbmap) < 0) 179 err(1, "WSKBDIO_SETMAP"); 180 pr_field(pre, field_by_value(keyboard_field_tab, &kbmap), " -> "); 181 } 182 183 repeat.which = 0; 184 if (field_by_value(keyboard_field_tab, &repeat.del1)->flags & FLG_SET) 185 repeat.which |= WSKBD_KEYREPEAT_DODEL1; 186 if (field_by_value(keyboard_field_tab, &repeat.delN)->flags & FLG_SET) 187 repeat.which |= WSKBD_KEYREPEAT_DODELN; 188 if (repeat.which != 0 && 189 ioctl(fd, WSKBDIO_SETKEYREPEAT, &repeat) < 0) 190 err(1, "WSKBDIO_SETKEYREPEAT"); 191 if (repeat.which & WSKBD_KEYREPEAT_DODEL1) 192 pr_field(pre, field_by_value(keyboard_field_tab, &repeat.del1), " -> "); 193 if (repeat.which & WSKBD_KEYREPEAT_DODELN) 194 pr_field(pre, field_by_value(keyboard_field_tab, &repeat.delN), " -> "); 195 196 dfrepeat.which = 0; 197 if (field_by_value(keyboard_field_tab, &dfrepeat.del1)->flags & FLG_SET) 198 dfrepeat.which |= WSKBD_KEYREPEAT_DODEL1; 199 if (field_by_value(keyboard_field_tab, &dfrepeat.delN)->flags & FLG_SET) 200 dfrepeat.which |= WSKBD_KEYREPEAT_DODELN; 201 if (dfrepeat.which != 0 && 202 ioctl(fd, WSKBDIO_SETDEFAULTKEYREPEAT, &dfrepeat) < 0) 203 err(1, "WSKBDIO_SETDEFAULTKEYREPEAT"); 204 if (dfrepeat.which &WSKBD_KEYREPEAT_DODEL1) 205 pr_field(pre, field_by_value(keyboard_field_tab, &dfrepeat.del1), " -> "); 206 if (dfrepeat.which & WSKBD_KEYREPEAT_DODELN) 207 pr_field(pre, field_by_value(keyboard_field_tab, &dfrepeat.delN), " -> "); 208 209 if (field_by_value(keyboard_field_tab, &ledstate)->flags & FLG_SET) { 210 if (ioctl(fd, WSKBDIO_SETLEDS, &ledstate) < 0) 211 err(1, "WSKBDIO_SETLEDS"); 212 pr_field(pre, field_by_value(keyboard_field_tab, &ledstate), " -> "); 213 } 214 215 if (field_by_value(keyboard_field_tab, &kbdencoding)->flags & FLG_SET) { 216 if (ioctl(fd, WSKBDIO_SETENCODING, &kbdencoding) < 0) 217 err(1, "WSKBDIO_SETENCODING"); 218 pr_field(pre, field_by_value(keyboard_field_tab, &kbdencoding), " -> "); 219 } 220 } 221