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