wskbd.c revision 1.42.2.2 1 /* $NetBSD: wskbd.c,v 1.42.2.2 2001/09/26 15:28:20 fvdl Exp $ */
2
3 /*
4 * Copyright (c) 1996, 1997 Christopher G. Demetriou. All rights reserved.
5 *
6 * Keysym translator:
7 * Contributed to The NetBSD Foundation by Juergen Hannken-Illjes.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. All advertising materials mentioning features or use of this software
18 * must display the following acknowledgement:
19 * This product includes software developed by Christopher G. Demetriou
20 * for the NetBSD Project.
21 * 4. The name of the author may not be used to endorse or promote products
22 * derived from this software without specific prior written permission
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
25 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
26 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
27 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
28 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
29 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
30 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
31 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
33 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 */
35
36 #include <sys/cdefs.h>
37 __KERNEL_RCSID(0, "$NetBSD: wskbd.c,v 1.42.2.2 2001/09/26 15:28:20 fvdl Exp $");
38
39 /*
40 * Copyright (c) 1992, 1993
41 * The Regents of the University of California. All rights reserved.
42 *
43 * This software was developed by the Computer Systems Engineering group
44 * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
45 * contributed to Berkeley.
46 *
47 * All advertising materials mentioning features or use of this software
48 * must display the following acknowledgement:
49 * This product includes software developed by the University of
50 * California, Lawrence Berkeley Laboratory.
51 *
52 * Redistribution and use in source and binary forms, with or without
53 * modification, are permitted provided that the following conditions
54 * are met:
55 * 1. Redistributions of source code must retain the above copyright
56 * notice, this list of conditions and the following disclaimer.
57 * 2. Redistributions in binary form must reproduce the above copyright
58 * notice, this list of conditions and the following disclaimer in the
59 * documentation and/or other materials provided with the distribution.
60 * 3. All advertising materials mentioning features or use of this software
61 * must display the following acknowledgement:
62 * This product includes software developed by the University of
63 * California, Berkeley and its contributors.
64 * 4. Neither the name of the University nor the names of its contributors
65 * may be used to endorse or promote products derived from this software
66 * without specific prior written permission.
67 *
68 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
69 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
70 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
71 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
72 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
73 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
74 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
75 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
76 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
77 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
78 * SUCH DAMAGE.
79 *
80 * @(#)kbd.c 8.2 (Berkeley) 10/30/93
81 */
82
83 /*
84 * Keyboard driver (/dev/wskbd*). Translates incoming bytes to ASCII or
85 * to `wscons_events' and passes them up to the appropriate reader.
86 */
87
88 #include "opt_ddb.h"
89 #include "opt_kgdb.h"
90
91 #include <sys/param.h>
92 #include <sys/conf.h>
93 #include <sys/device.h>
94 #include <sys/ioctl.h>
95 #include <sys/kernel.h>
96 #include <sys/proc.h>
97 #include <sys/syslog.h>
98 #include <sys/systm.h>
99 #include <sys/callout.h>
100 #include <sys/malloc.h>
101 #include <sys/tty.h>
102 #include <sys/signalvar.h>
103 #include <sys/errno.h>
104 #include <sys/fcntl.h>
105 #include <sys/vnode.h>
106
107 #include <miscfs/specfs/specdev.h>
108
109 #include <dev/wscons/wsconsio.h>
110 #include <dev/wscons/wskbdvar.h>
111 #include <dev/wscons/wsksymdef.h>
112 #include <dev/wscons/wsksymvar.h>
113 #include <dev/wscons/wseventvar.h>
114 #include <dev/wscons/wscons_callbacks.h>
115
116 #include "opt_wsdisplay_compat.h"
117
118 #include "wsdisplay.h"
119 #include "wsmux.h"
120
121 #ifdef KGDB
122 #include <sys/kgdb.h>
123 #endif
124
125 #ifdef WSKBD_DEBUG
126 #define DPRINTF(x) if (wskbddebug) printf x
127 int wskbddebug = 0;
128 #else
129 #define DPRINTF(x)
130 #endif
131
132 #if NWSMUX > 0 || NWSDISPLAY > 0
133 #include <dev/wscons/wsmuxvar.h>
134 #endif
135
136 struct wskbd_internal {
137 const struct wskbd_mapdata *t_keymap;
138
139 const struct wskbd_consops *t_consops;
140 void *t_consaccesscookie;
141
142 int t_modifiers;
143 int t_composelen; /* remaining entries in t_composebuf */
144 keysym_t t_composebuf[2];
145
146 int t_flags;
147 #define WSKFL_METAESC 1
148
149 #define MAXKEYSYMSPERKEY 2 /* ESC <key> at max */
150 keysym_t t_symbols[MAXKEYSYMSPERKEY];
151
152 struct wskbd_softc *t_sc; /* back pointer */
153 };
154
155 struct wskbd_softc {
156 struct device sc_dv;
157
158 struct wskbd_internal *id;
159
160 const struct wskbd_accessops *sc_accessops;
161 void *sc_accesscookie;
162
163 int sc_ledstate;
164
165 int sc_ready; /* accepting events */
166 struct wseventvar sc_events; /* event queue state */
167
168 int sc_isconsole;
169 #if NWSDISPLAY > 0
170 struct device *sc_displaydv;
171 #endif
172
173 struct wskbd_bell_data sc_bell_data;
174 struct wskbd_keyrepeat_data sc_keyrepeat_data;
175
176 int sc_repeating; /* we've called timeout() */
177 struct callout sc_repeat_ch;
178
179 int sc_translating; /* xlate to chars for emulation */
180
181 int sc_maplen; /* number of entries in sc_map */
182 struct wscons_keymap *sc_map; /* current translation map */
183 kbd_t sc_layout; /* current layout */
184
185 int sc_refcnt;
186 u_char sc_dying; /* device is being detached */
187
188 #if NWSMUX > 0 || NWSDISPLAY > 0
189 struct wsmux_softc *sc_mux;
190 #endif
191 };
192
193 #define MOD_SHIFT_L (1 << 0)
194 #define MOD_SHIFT_R (1 << 1)
195 #define MOD_SHIFTLOCK (1 << 2)
196 #define MOD_CAPSLOCK (1 << 3)
197 #define MOD_CONTROL_L (1 << 4)
198 #define MOD_CONTROL_R (1 << 5)
199 #define MOD_META_L (1 << 6)
200 #define MOD_META_R (1 << 7)
201 #define MOD_MODESHIFT (1 << 8)
202 #define MOD_NUMLOCK (1 << 9)
203 #define MOD_COMPOSE (1 << 10)
204 #define MOD_HOLDSCREEN (1 << 11)
205 #define MOD_COMMAND (1 << 12)
206 #define MOD_COMMAND1 (1 << 13)
207 #define MOD_COMMAND2 (1 << 14)
208
209 #define MOD_ANYSHIFT (MOD_SHIFT_L | MOD_SHIFT_R | MOD_SHIFTLOCK)
210 #define MOD_ANYCONTROL (MOD_CONTROL_L | MOD_CONTROL_R)
211 #define MOD_ANYMETA (MOD_META_L | MOD_META_R)
212
213 #define MOD_ONESET(id, mask) (((id)->t_modifiers & (mask)) != 0)
214 #define MOD_ALLSET(id, mask) (((id)->t_modifiers & (mask)) == (mask))
215
216 int wskbd_match __P((struct device *, struct cfdata *, void *));
217 void wskbd_attach __P((struct device *, struct device *, void *));
218 int wskbd_detach __P((struct device *, int));
219 int wskbd_activate __P((struct device *, enum devact));
220
221 static int wskbd_displayioctl
222 __P((struct device *, u_long, caddr_t, int, struct proc *p));
223 int wskbd_set_display __P((struct device *, struct wsmux_softc *));
224
225 static inline void update_leds __P((struct wskbd_internal *));
226 static inline void update_modifier __P((struct wskbd_internal *, u_int, int, int));
227 static int internal_command __P((struct wskbd_softc *, u_int *, keysym_t, keysym_t));
228 static int wskbd_translate __P((struct wskbd_internal *, u_int, int));
229 static int wskbd_enable __P((struct wskbd_softc *, int));
230 #if NWSDISPLAY > 0
231 static void change_displayparam __P((struct wskbd_softc *, int, int, int));
232 static void wskbd_holdscreen __P((struct wskbd_softc *, int));
233 #endif
234
235 int wskbd_do_ioctl __P((struct wskbd_softc *, u_long, caddr_t,
236 int, struct proc *));
237
238 int wskbddoclose __P((struct device *, int, int, struct proc *));
239 int wskbddoioctl __P((struct device *, u_long, caddr_t, int,
240 struct proc *));
241
242 struct cfattach wskbd_ca = {
243 sizeof (struct wskbd_softc), wskbd_match, wskbd_attach,
244 wskbd_detach, wskbd_activate
245 };
246
247 extern struct cfdriver wskbd_cd;
248
249 #ifndef WSKBD_DEFAULT_BELL_PITCH
250 #define WSKBD_DEFAULT_BELL_PITCH 1500 /* 1500Hz */
251 #endif
252 #ifndef WSKBD_DEFAULT_BELL_PERIOD
253 #define WSKBD_DEFAULT_BELL_PERIOD 100 /* 100ms */
254 #endif
255 #ifndef WSKBD_DEFAULT_BELL_VOLUME
256 #define WSKBD_DEFAULT_BELL_VOLUME 50 /* 50% volume */
257 #endif
258
259 struct wskbd_bell_data wskbd_default_bell_data = {
260 WSKBD_BELL_DOALL,
261 WSKBD_DEFAULT_BELL_PITCH,
262 WSKBD_DEFAULT_BELL_PERIOD,
263 WSKBD_DEFAULT_BELL_VOLUME,
264 };
265
266 #ifndef WSKBD_DEFAULT_KEYREPEAT_DEL1
267 #define WSKBD_DEFAULT_KEYREPEAT_DEL1 400 /* 400ms to start repeating */
268 #endif
269 #ifndef WSKBD_DEFAULT_KEYREPEAT_DELN
270 #define WSKBD_DEFAULT_KEYREPEAT_DELN 100 /* 100ms to between repeats */
271 #endif
272
273 struct wskbd_keyrepeat_data wskbd_default_keyrepeat_data = {
274 WSKBD_KEYREPEAT_DOALL,
275 WSKBD_DEFAULT_KEYREPEAT_DEL1,
276 WSKBD_DEFAULT_KEYREPEAT_DELN,
277 };
278
279 cdev_decl(wskbd);
280
281 #if NWSMUX > 0 || NWSDISPLAY > 0
282 struct wsmuxops wskbd_muxops = {
283 wskbdopen, wskbddoclose, wskbddoioctl, wskbd_displayioctl,
284 wskbd_set_display
285 };
286 #endif
287
288 int wskbd_major = -1;
289
290 #if NWSDISPLAY > 0
291 static void wskbd_repeat __P((void *v));
292 #endif
293
294 static int wskbd_console_initted;
295 static struct wskbd_softc *wskbd_console_device;
296 static struct wskbd_internal wskbd_console_data;
297
298 static void wskbd_update_layout __P((struct wskbd_internal *, kbd_t));
299
300 static void
301 wskbd_update_layout(id, enc)
302 struct wskbd_internal *id;
303 kbd_t enc;
304 {
305
306 if (enc & KB_METAESC)
307 id->t_flags |= WSKFL_METAESC;
308 else
309 id->t_flags &= ~WSKFL_METAESC;
310 }
311
312 /*
313 * Print function (for parent devices).
314 */
315 int
316 wskbddevprint(aux, pnp)
317 void *aux;
318 const char *pnp;
319 {
320 #if 0
321 struct wskbddev_attach_args *ap = aux;
322 #endif
323
324 if (pnp)
325 printf("wskbd at %s", pnp);
326 #if 0
327 printf(" console %d", ap->console);
328 #endif
329
330 return (UNCONF);
331 }
332
333 int
334 wskbd_match(parent, match, aux)
335 struct device *parent;
336 struct cfdata *match;
337 void *aux;
338 {
339 struct wskbddev_attach_args *ap = aux;
340
341 if (match->wskbddevcf_console != WSKBDDEVCF_CONSOLE_UNK) {
342 /*
343 * If console-ness of device specified, either match
344 * exactly (at high priority), or fail.
345 */
346 if (match->wskbddevcf_console != 0 && ap->console != 0)
347 return (10);
348 else
349 return (0);
350 }
351
352 /* If console-ness unspecified, it wins. */
353 return (1);
354 }
355
356 void
357 wskbd_attach(parent, self, aux)
358 struct device *parent, *self;
359 void *aux;
360 {
361 struct wskbd_softc *sc = (struct wskbd_softc *)self;
362 struct wskbddev_attach_args *ap = aux;
363 #if NWSMUX > 0 || NWSDISPLAY > 0
364 int mux;
365 #endif
366
367 #if NWSDISPLAY > 0
368 sc->sc_displaydv = NULL;
369 #endif
370 sc->sc_isconsole = ap->console;
371
372 if (wskbd_major == -1) {
373 int maj;
374
375 for (maj = 0; maj < nchrdev; maj++)
376 if (cdevsw[maj].d_open == wskbdopen)
377 break;
378 wskbd_major = maj;
379 }
380
381 #if NWSMUX > 0 || NWSDISPLAY > 0
382 mux = sc->sc_dv.dv_cfdata->wskbddevcf_mux;
383 if (sc->sc_isconsole && mux != WSKBDDEVCF_MUX_DEFAULT) {
384 printf(" (mux %d ignored for console)", mux);
385 mux = WSKBDDEVCF_MUX_DEFAULT;
386 }
387 if (mux != WSKBDDEVCF_MUX_DEFAULT)
388 printf(" mux %d", mux);
389 #endif
390
391 if (ap->console) {
392 sc->id = &wskbd_console_data;
393 } else {
394 sc->id = malloc(sizeof(struct wskbd_internal),
395 M_DEVBUF, M_WAITOK);
396 bzero(sc->id, sizeof(struct wskbd_internal));
397 sc->id->t_keymap = ap->keymap;
398 wskbd_update_layout(sc->id, ap->keymap->layout);
399 }
400
401 callout_init(&sc->sc_repeat_ch);
402
403 sc->id->t_sc = sc;
404
405 sc->sc_accessops = ap->accessops;
406 sc->sc_accesscookie = ap->accesscookie;
407 sc->sc_ready = 0; /* sanity */
408 sc->sc_repeating = 0;
409 sc->sc_translating = 1;
410 sc->sc_ledstate = -1; /* force update */
411
412 if (wskbd_load_keymap(sc->id->t_keymap,
413 &sc->sc_map, &sc->sc_maplen) != 0)
414 panic("cannot load keymap");
415
416 sc->sc_layout = sc->id->t_keymap->layout;
417
418 /* set default bell and key repeat data */
419 sc->sc_bell_data = wskbd_default_bell_data;
420 sc->sc_keyrepeat_data = wskbd_default_keyrepeat_data;
421
422 if (ap->console) {
423 KASSERT(wskbd_console_initted);
424 KASSERT(wskbd_console_device == NULL);
425
426 wskbd_console_device = sc;
427
428 printf(": console keyboard");
429
430 #if NWSDISPLAY > 0
431 if ((sc->sc_displaydv = wsdisplay_set_console_kbd(self)))
432 printf(", using %s", sc->sc_displaydv->dv_xname);
433 #endif
434 }
435 printf("\n");
436
437 #if NWSMUX > 0
438 if (mux != WSKBDDEVCF_MUX_DEFAULT)
439 wsmux_attach(mux, WSMUX_KBD, &sc->sc_dv, &sc->sc_events,
440 &sc->sc_mux, &wskbd_muxops, wskbd_major);
441 #endif
442
443 }
444
445 void
446 wskbd_cnattach(consops, conscookie, mapdata)
447 const struct wskbd_consops *consops;
448 void *conscookie;
449 const struct wskbd_mapdata *mapdata;
450 {
451 KASSERT(!wskbd_console_initted);
452
453 wskbd_console_data.t_keymap = mapdata;
454 wskbd_update_layout(&wskbd_console_data, mapdata->layout);
455
456 wskbd_console_data.t_consops = consops;
457 wskbd_console_data.t_consaccesscookie = conscookie;
458
459 #if NWSDISPLAY > 0
460 wsdisplay_set_cons_kbd(wskbd_cngetc, wskbd_cnpollc, wskbd_cnbell);
461 #endif
462
463 wskbd_console_initted = 1;
464 }
465
466 void
467 wskbd_cndetach()
468 {
469 KASSERT(wskbd_console_initted);
470
471 wskbd_console_data.t_keymap = 0;
472
473 wskbd_console_data.t_consops = 0;
474 wskbd_console_data.t_consaccesscookie = 0;
475
476 #if NWSDISPLAY > 0
477 wsdisplay_unset_cons_kbd();
478 #endif
479
480 wskbd_console_initted = 0;
481 }
482
483 #if NWSDISPLAY > 0
484 static void
485 wskbd_repeat(v)
486 void *v;
487 {
488 struct wskbd_softc *sc = (struct wskbd_softc *)v;
489 int s = spltty();
490
491 if (!sc->sc_repeating) {
492 /*
493 * race condition: a "key up" event came in when wskbd_repeat()
494 * was already called but not yet spltty()'d
495 */
496 splx(s);
497 return;
498 }
499 if (sc->sc_displaydv != NULL) {
500 int i;
501 for (i = 0; i < sc->sc_repeating; i++)
502 wsdisplay_kbdinput(sc->sc_displaydv,
503 sc->id->t_symbols[i]);
504 }
505 callout_reset(&sc->sc_repeat_ch,
506 (hz * sc->sc_keyrepeat_data.delN) / 1000, wskbd_repeat, sc);
507 splx(s);
508 }
509 #endif
510
511 int
512 wskbd_activate(self, act)
513 struct device *self;
514 enum devact act;
515 {
516 /* XXX should we do something more? */
517 return (0);
518 }
519
520 /*
521 * Detach a keyboard. To keep track of users of the softc we keep
522 * a reference count that's incremented while inside, e.g., read.
523 * If the keyboard is active and the reference count is > 0 (0 is the
524 * normal state) we post an event and then wait for the process
525 * that had the reference to wake us up again. Then we blow away the
526 * vnode and return (which will deallocate the softc).
527 */
528 int
529 wskbd_detach(self, flags)
530 struct device *self;
531 int flags;
532 {
533 struct wskbd_softc *sc = (struct wskbd_softc *)self;
534 struct wseventvar *evar;
535 int s, mn;
536 #if NWSMUX > 0
537 int mux;
538
539 mux = sc->sc_dv.dv_cfdata->wskbddevcf_mux;
540 if (mux != WSMOUSEDEVCF_MUX_DEFAULT)
541 wsmux_detach(mux, &sc->sc_dv);
542 #endif
543
544 evar = &sc->sc_events;
545 if (evar->io) {
546 s = spltty();
547 if (--sc->sc_refcnt >= 0) {
548 /* Wake everyone by generating a dummy event. */
549 if (++evar->put >= WSEVENT_QSIZE)
550 evar->put = 0;
551 WSEVENT_WAKEUP(evar);
552 /* Wait for processes to go away. */
553 if (tsleep(sc, PZERO, "wskdet", hz * 60))
554 printf("wskbd_detach: %s didn't detach\n",
555 sc->sc_dv.dv_xname);
556 }
557 splx(s);
558 }
559
560 /* Nuke the vnodes for any open instances. */
561 mn = self->dv_unit;
562 vdevgone(wskbd_major, mn, mn, VCHR);
563
564 return (0);
565 }
566
567 void
568 wskbd_input(dev, type, value)
569 struct device *dev;
570 u_int type;
571 int value;
572 {
573 struct wskbd_softc *sc = (struct wskbd_softc *)dev;
574 struct wscons_event *ev;
575 struct wseventvar *evar;
576 struct timeval xxxtime;
577 #if NWSDISPLAY > 0
578 int num, i;
579 #endif
580 int put;
581
582 #if NWSDISPLAY > 0
583 if (sc->sc_repeating) {
584 sc->sc_repeating = 0;
585 callout_stop(&sc->sc_repeat_ch);
586 }
587
588 /*
589 * If /dev/wskbd is not connected in event mode translate and
590 * send upstream.
591 */
592 if (sc->sc_translating) {
593 num = wskbd_translate(sc->id, type, value);
594 if (num > 0) {
595 if (sc->sc_displaydv != NULL) {
596 for (i = 0; i < num; i++)
597 wsdisplay_kbdinput(sc->sc_displaydv,
598 sc->id->t_symbols[i]);
599 }
600
601 sc->sc_repeating = num;
602 callout_reset(&sc->sc_repeat_ch,
603 (hz * sc->sc_keyrepeat_data.del1) / 1000,
604 wskbd_repeat, sc);
605 }
606 return;
607 }
608 #endif
609
610 /*
611 * Keyboard is generating events. Turn this keystroke into an
612 * event and put it in the queue. If the queue is full, the
613 * keystroke is lost (sorry!).
614 */
615
616 /* no one to receive; punt!*/
617 if (!sc->sc_ready)
618 return;
619
620 #if NWSMUX > 0
621 if (sc->sc_mux)
622 evar = &sc->sc_mux->sc_events;
623 else
624 #endif
625 evar = &sc->sc_events;
626
627 put = evar->put;
628 ev = &evar->q[put];
629 put = (put + 1) % WSEVENT_QSIZE;
630 if (put == evar->get) {
631 log(LOG_WARNING, "%s: event queue overflow\n",
632 sc->sc_dv.dv_xname);
633 return;
634 }
635 ev->type = type;
636 ev->value = value;
637 microtime(&xxxtime);
638 TIMEVAL_TO_TIMESPEC(&xxxtime, &ev->time);
639 evar->put = put;
640 WSEVENT_WAKEUP(evar);
641 }
642
643 #ifdef WSDISPLAY_COMPAT_RAWKBD
644 void
645 wskbd_rawinput(dev, buf, len)
646 struct device *dev;
647 u_char *buf;
648 int len;
649 {
650 #if NWSDISPLAY > 0
651 struct wskbd_softc *sc = (struct wskbd_softc *)dev;
652 int i;
653
654 for (i = 0; i < len; i++)
655 wsdisplay_kbdinput(sc->sc_displaydv, buf[i]);
656 /* this is KS_GROUP_Ascii */
657 #endif
658 }
659 #endif /* WSDISPLAY_COMPAT_RAWKBD */
660
661 #if NWSDISPLAY > 0
662 static void
663 wskbd_holdscreen(sc, hold)
664 struct wskbd_softc *sc;
665 int hold;
666 {
667 int new_state;
668
669 if (sc->sc_displaydv != NULL) {
670 wsdisplay_kbdholdscreen(sc->sc_displaydv, hold);
671 new_state = sc->sc_ledstate;
672 if (hold)
673 new_state |= WSKBD_LED_SCROLL;
674 else
675 new_state &= ~WSKBD_LED_SCROLL;
676 if (new_state != sc->sc_ledstate) {
677 (*sc->sc_accessops->set_leds)(sc->sc_accesscookie,
678 new_state);
679 sc->sc_ledstate = new_state;
680 }
681 }
682 }
683 #endif
684
685 static int
686 wskbd_enable(sc, on)
687 struct wskbd_softc *sc;
688 int on;
689 {
690 int res;
691
692 /* XXX reference count? */
693 if (!on && (!sc->sc_translating
694 #if NWSDISPLAY > 0
695 || sc->sc_displaydv
696 #endif
697 ))
698 return (EBUSY);
699
700 res = (*sc->sc_accessops->enable)(sc->sc_accesscookie, on);
701 return (res);
702 }
703
704 int
705 wskbdopen(devvp, flags, mode, p)
706 struct vnode *devvp;
707 int flags, mode;
708 struct proc *p;
709 {
710 struct wskbd_softc *sc;
711 int unit;
712
713 unit = minor(vdev_rdev(devvp));
714 if (unit >= wskbd_cd.cd_ndevs || /* make sure it was attached */
715 (sc = wskbd_cd.cd_devs[unit]) == NULL)
716 return (ENXIO);
717
718 if (sc->sc_dying)
719 return (EIO);
720
721 if (!(flags & FREAD)) {
722 /* Not opening for read, only ioctl is available. */
723 return (0);
724 }
725
726 #if NWSMUX > 0
727 if (sc->sc_mux)
728 return (EBUSY);
729 #endif
730
731 if (sc->sc_events.io) /* and that it's not in use */
732 return (EBUSY);
733
734 vdev_setprivdata(devvp, sc);
735
736 sc->sc_events.io = p;
737 wsevent_init(&sc->sc_events); /* may cause sleep */
738
739 sc->sc_translating = 0;
740 sc->sc_ready = 1; /* start accepting events */
741
742 wskbd_enable(sc, 1);
743 return (0);
744 }
745
746 int
747 wskbdclose(devvp, flags, mode, p)
748 struct vnode *devvp;
749 int flags, mode;
750 struct proc *p;
751 {
752 return (wskbddoclose(vdev_privdata(devvp), flags, mode, p));
753 }
754
755 int
756 wskbddoclose(dv, flags, mode, p)
757 struct device *dv;
758 int flags, mode;
759 struct proc *p;
760 {
761 struct wskbd_softc *sc = (struct wskbd_softc *)dv;
762
763 if (!(flags & FREAD)) {
764 /* Nothing to do, because open didn't do anything. */
765 return (0);
766 }
767
768 sc->sc_ready = 0; /* stop accepting events */
769 sc->sc_translating = 1;
770
771 wsevent_fini(&sc->sc_events);
772 sc->sc_events.io = NULL;
773
774 wskbd_enable(sc, 0);
775 return (0);
776 }
777
778 int
779 wskbdread(devvp, uio, flags)
780 struct vnode *devvp;
781 struct uio *uio;
782 int flags;
783 {
784 struct wskbd_softc *sc;
785 int error;
786
787 sc = vdev_privdata(devvp);
788
789 if (sc->sc_dying)
790 return (EIO);
791
792 sc->sc_refcnt++;
793 error = wsevent_read(&sc->sc_events, uio, flags);
794 if (--sc->sc_refcnt < 0) {
795 wakeup(sc);
796 error = EIO;
797 }
798 return (error);
799 }
800
801 int
802 wskbdioctl(devvp, cmd, data, flag, p)
803 struct vnode *devvp;
804 u_long cmd;
805 caddr_t data;
806 int flag;
807 struct proc *p;
808 {
809 return (wskbddoioctl(vdev_privdata(devvp), cmd, data, flag,p));
810 }
811
812 /* A wrapper around the ioctl() workhorse to make reference counting easy. */
813 int
814 wskbddoioctl(dv, cmd, data, flag, p)
815 struct device *dv;
816 u_long cmd;
817 caddr_t data;
818 int flag;
819 struct proc *p;
820 {
821 struct wskbd_softc *sc = (struct wskbd_softc *)dv;
822 int error;
823
824 sc->sc_refcnt++;
825 error = wskbd_do_ioctl(sc, cmd, data, flag, p);
826 if (--sc->sc_refcnt < 0)
827 wakeup(sc);
828 return (error);
829 }
830
831 int
832 wskbd_do_ioctl(sc, cmd, data, flag, p)
833 struct wskbd_softc *sc;
834 u_long cmd;
835 caddr_t data;
836 int flag;
837 struct proc *p;
838 {
839 int error;
840
841 /*
842 * Try the generic ioctls that the wskbd interface supports.
843 */
844 switch (cmd) {
845 case FIONBIO: /* we will remove this someday (soon???) */
846 return (0);
847
848 case FIOASYNC:
849 sc->sc_events.async = *(int *)data != 0;
850 return (0);
851
852 case TIOCSPGRP:
853 if (*(int *)data != sc->sc_events.io->p_pgid)
854 return (EPERM);
855 return (0);
856 }
857
858 /*
859 * Try the keyboard driver for WSKBDIO ioctls. It returns -1
860 * if it didn't recognize the request.
861 */
862 error = wskbd_displayioctl((struct device *)sc, cmd, data, flag, p);
863 return (error != -1 ? error : ENOTTY);
864 }
865
866 /*
867 * WSKBDIO ioctls, handled in both emulation mode and in ``raw'' mode.
868 * Some of these have no real effect in raw mode, however.
869 */
870 static int
871 wskbd_displayioctl(dev, cmd, data, flag, p)
872 struct device *dev;
873 u_long cmd;
874 caddr_t data;
875 int flag;
876 struct proc *p;
877 {
878 struct wskbd_softc *sc = (struct wskbd_softc *)dev;
879 struct wskbd_bell_data *ubdp, *kbdp;
880 struct wskbd_keyrepeat_data *ukdp, *kkdp;
881 struct wskbd_map_data *umdp;
882 struct wskbd_mapdata md;
883 kbd_t enc;
884 void *buf;
885 int len, error;
886
887 switch (cmd) {
888 #define SETBELL(dstp, srcp, dfltp) \
889 do { \
890 (dstp)->pitch = ((srcp)->which & WSKBD_BELL_DOPITCH) ? \
891 (srcp)->pitch : (dfltp)->pitch; \
892 (dstp)->period = ((srcp)->which & WSKBD_BELL_DOPERIOD) ? \
893 (srcp)->period : (dfltp)->period; \
894 (dstp)->volume = ((srcp)->which & WSKBD_BELL_DOVOLUME) ? \
895 (srcp)->volume : (dfltp)->volume; \
896 (dstp)->which = WSKBD_BELL_DOALL; \
897 } while (0)
898
899 case WSKBDIO_BELL:
900 if ((flag & FWRITE) == 0)
901 return (EACCES);
902 return ((*sc->sc_accessops->ioctl)(sc->sc_accesscookie,
903 WSKBDIO_COMPLEXBELL, (caddr_t)&sc->sc_bell_data, flag, p));
904
905 case WSKBDIO_COMPLEXBELL:
906 if ((flag & FWRITE) == 0)
907 return (EACCES);
908 ubdp = (struct wskbd_bell_data *)data;
909 SETBELL(ubdp, ubdp, &sc->sc_bell_data);
910 return ((*sc->sc_accessops->ioctl)(sc->sc_accesscookie,
911 WSKBDIO_COMPLEXBELL, (caddr_t)ubdp, flag, p));
912
913 case WSKBDIO_SETBELL:
914 if ((flag & FWRITE) == 0)
915 return (EACCES);
916 kbdp = &sc->sc_bell_data;
917 setbell:
918 ubdp = (struct wskbd_bell_data *)data;
919 SETBELL(kbdp, ubdp, kbdp);
920 return (0);
921
922 case WSKBDIO_GETBELL:
923 kbdp = &sc->sc_bell_data;
924 getbell:
925 ubdp = (struct wskbd_bell_data *)data;
926 SETBELL(ubdp, kbdp, kbdp);
927 return (0);
928
929 case WSKBDIO_SETDEFAULTBELL:
930 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
931 return (error);
932 kbdp = &wskbd_default_bell_data;
933 goto setbell;
934
935
936 case WSKBDIO_GETDEFAULTBELL:
937 kbdp = &wskbd_default_bell_data;
938 goto getbell;
939
940 #undef SETBELL
941
942 #define SETKEYREPEAT(dstp, srcp, dfltp) \
943 do { \
944 (dstp)->del1 = ((srcp)->which & WSKBD_KEYREPEAT_DODEL1) ? \
945 (srcp)->del1 : (dfltp)->del1; \
946 (dstp)->delN = ((srcp)->which & WSKBD_KEYREPEAT_DODELN) ? \
947 (srcp)->delN : (dfltp)->delN; \
948 (dstp)->which = WSKBD_KEYREPEAT_DOALL; \
949 } while (0)
950
951 case WSKBDIO_SETKEYREPEAT:
952 if ((flag & FWRITE) == 0)
953 return (EACCES);
954 kkdp = &sc->sc_keyrepeat_data;
955 setkeyrepeat:
956 ukdp = (struct wskbd_keyrepeat_data *)data;
957 SETKEYREPEAT(kkdp, ukdp, kkdp);
958 return (0);
959
960 case WSKBDIO_GETKEYREPEAT:
961 kkdp = &sc->sc_keyrepeat_data;
962 getkeyrepeat:
963 ukdp = (struct wskbd_keyrepeat_data *)data;
964 SETKEYREPEAT(ukdp, kkdp, kkdp);
965 return (0);
966
967 case WSKBDIO_SETDEFAULTKEYREPEAT:
968 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
969 return (error);
970 kkdp = &wskbd_default_keyrepeat_data;
971 goto setkeyrepeat;
972
973
974 case WSKBDIO_GETDEFAULTKEYREPEAT:
975 kkdp = &wskbd_default_keyrepeat_data;
976 goto getkeyrepeat;
977
978 #undef SETKEYREPEAT
979
980 case WSKBDIO_SETMAP:
981 if ((flag & FWRITE) == 0)
982 return (EACCES);
983 umdp = (struct wskbd_map_data *)data;
984 if (umdp->maplen > WSKBDIO_MAXMAPLEN)
985 return (EINVAL);
986
987 len = umdp->maplen*sizeof(struct wscons_keymap);
988 buf = malloc(len, M_TEMP, M_WAITOK);
989 error = copyin(umdp->map, buf, len);
990 if (error == 0) {
991 wskbd_init_keymap(umdp->maplen,
992 &sc->sc_map, &sc->sc_maplen);
993 memcpy(sc->sc_map, buf, len);
994 /* drop the variant bits handled by the map */
995 sc->sc_layout = KB_USER |
996 (KB_VARIANT(sc->sc_layout) & KB_HANDLEDBYWSKBD);
997 wskbd_update_layout(sc->id, sc->sc_layout);
998 }
999 free(buf, M_TEMP);
1000 return(error);
1001
1002 case WSKBDIO_GETMAP:
1003 umdp = (struct wskbd_map_data *)data;
1004 if (umdp->maplen > sc->sc_maplen)
1005 umdp->maplen = sc->sc_maplen;
1006 error = copyout(sc->sc_map, umdp->map,
1007 umdp->maplen*sizeof(struct wscons_keymap));
1008 return(error);
1009
1010 case WSKBDIO_GETENCODING:
1011 *((kbd_t *) data) = sc->sc_layout;
1012 return(0);
1013
1014 case WSKBDIO_SETENCODING:
1015 if ((flag & FWRITE) == 0)
1016 return (EACCES);
1017 enc = *((kbd_t *)data);
1018 if (KB_ENCODING(enc) == KB_USER) {
1019 /* user map must already be loaded */
1020 if (KB_ENCODING(sc->sc_layout) != KB_USER)
1021 return (EINVAL);
1022 /* map variants make no sense */
1023 if (KB_VARIANT(enc) & ~KB_HANDLEDBYWSKBD)
1024 return (EINVAL);
1025 } else {
1026 md = *(sc->id->t_keymap); /* structure assignment */
1027 md.layout = enc;
1028 error = wskbd_load_keymap(&md, &sc->sc_map,
1029 &sc->sc_maplen);
1030 if (error)
1031 return (error);
1032 }
1033 sc->sc_layout = enc;
1034 wskbd_update_layout(sc->id, enc);
1035 return (0);
1036 }
1037
1038 /*
1039 * Try the keyboard driver for WSKBDIO ioctls. It returns -1
1040 * if it didn't recognize the request, and in turn we return
1041 * -1 if we didn't recognize the request.
1042 */
1043 /* printf("kbdaccess\n"); */
1044 error = (*sc->sc_accessops->ioctl)(sc->sc_accesscookie, cmd, data,
1045 flag, p);
1046 #ifdef WSDISPLAY_COMPAT_RAWKBD
1047 if (!error && cmd == WSKBDIO_SETMODE && *(int *)data == WSKBD_RAW) {
1048 int s = spltty();
1049 sc->id->t_modifiers &= ~(MOD_SHIFT_L | MOD_SHIFT_R
1050 | MOD_CONTROL_L | MOD_CONTROL_R
1051 | MOD_META_L | MOD_META_R
1052 | MOD_COMMAND
1053 | MOD_COMMAND1 | MOD_COMMAND2);
1054 #if NWSDISPLAY > 0
1055 if (sc->sc_repeating) {
1056 sc->sc_repeating = 0;
1057 callout_stop(&sc->sc_repeat_ch);
1058 }
1059 #endif
1060 splx(s);
1061 }
1062 #endif
1063 return (error);
1064 }
1065
1066 int
1067 wskbdpoll(devvp, events, p)
1068 struct vnode *devvp;
1069 int events;
1070 struct proc *p;
1071 {
1072 struct wskbd_softc *sc;
1073
1074 sc = vdev_privdata(devvp);
1075
1076 return (wsevent_poll(&sc->sc_events, events, p));
1077 }
1078
1079 #if NWSDISPLAY > 0
1080
1081 int
1082 wskbd_pickfree()
1083 {
1084 int i;
1085 struct wskbd_softc *sc;
1086
1087 for (i = 0; i < wskbd_cd.cd_ndevs; i++) {
1088 if ((sc = wskbd_cd.cd_devs[i]) == NULL)
1089 continue;
1090 if (sc->sc_displaydv == NULL)
1091 return (i);
1092 }
1093 return (-1);
1094 }
1095
1096 struct device *
1097 wskbd_set_console_display(displaydv, muxsc)
1098 struct device *displaydv;
1099 struct wsmux_softc *muxsc;
1100 {
1101 struct wskbd_softc *sc = wskbd_console_device;
1102
1103 if (!sc)
1104 return (0);
1105 sc->sc_displaydv = displaydv;
1106 (void)wsmux_attach_sc(muxsc, WSMUX_KBD, &sc->sc_dv, &sc->sc_events,
1107 &sc->sc_mux, &wskbd_muxops, wskbd_major);
1108 return (&sc->sc_dv);
1109 }
1110
1111 int
1112 wskbd_set_display(dv, muxsc)
1113 struct device *dv;
1114 struct wsmux_softc *muxsc;
1115 {
1116 struct wskbd_softc *sc = (struct wskbd_softc *)dv;
1117 struct device *displaydv = muxsc ? muxsc->sc_displaydv : 0;
1118 struct device *odisplaydv;
1119 int error;
1120
1121 DPRINTF(("wskbd_set_display: %s mux=%p disp=%p odisp=%p cons=%d\n",
1122 dv->dv_xname, muxsc, sc->sc_displaydv, displaydv,
1123 sc->sc_isconsole));
1124
1125 if (sc->sc_isconsole)
1126 return (EBUSY);
1127
1128 if (displaydv) {
1129 if (sc->sc_displaydv)
1130 return (EBUSY);
1131 } else {
1132 if (sc->sc_displaydv == NULL)
1133 return (ENXIO);
1134 }
1135
1136 odisplaydv = sc->sc_displaydv;
1137 sc->sc_displaydv = displaydv;
1138
1139 error = wskbd_enable(sc, displaydv != NULL);
1140 if (error) {
1141 sc->sc_displaydv = odisplaydv;
1142 return (error);
1143 }
1144
1145 if (displaydv)
1146 printf("%s: connecting to %s\n",
1147 sc->sc_dv.dv_xname, displaydv->dv_xname);
1148 else
1149 printf("%s: disconnecting from %s\n",
1150 sc->sc_dv.dv_xname, odisplaydv->dv_xname);
1151
1152 return (0);
1153 }
1154
1155 int
1156 wskbd_add_mux(unit, muxsc)
1157 int unit;
1158 struct wsmux_softc *muxsc;
1159 {
1160 struct wskbd_softc *sc;
1161
1162 DPRINTF(("wskbd_add_mux: %d %s %p\n", unit, muxsc->sc_dv.dv_xname,
1163 muxsc->sc_displaydv));
1164 if (unit < 0 || unit >= wskbd_cd.cd_ndevs ||
1165 (sc = wskbd_cd.cd_devs[unit]) == NULL)
1166 return (ENXIO);
1167
1168 if (sc->sc_mux || sc->sc_events.io)
1169 return (EBUSY);
1170
1171 return (wsmux_attach_sc(muxsc, WSMUX_KBD, &sc->sc_dv, &sc->sc_events,
1172 &sc->sc_mux, &wskbd_muxops, wskbd_major));
1173 }
1174
1175 int
1176 wskbd_rem_mux(unit, muxsc)
1177 int unit;
1178 struct wsmux_softc *muxsc;
1179 {
1180 struct wskbd_softc *sc;
1181
1182 DPRINTF(("wskbd_rem_mux: %d %s\n", unit, muxsc->sc_dv.dv_xname));
1183 if (unit < 0 || unit >= wskbd_cd.cd_ndevs ||
1184 (sc = wskbd_cd.cd_devs[unit]) == NULL)
1185 return (ENXIO);
1186
1187 return (wsmux_detach_sc(muxsc, &sc->sc_dv));
1188 }
1189
1190 #endif /* NWSDISPLAY > 0 */
1191
1192 /*
1193 * Console interface.
1194 */
1195 int
1196 wskbd_cngetc(dev)
1197 dev_t dev;
1198 {
1199 static int num = 0;
1200 static int pos;
1201 u_int type;
1202 int data;
1203 keysym_t ks;
1204
1205 if (!wskbd_console_initted)
1206 return 0;
1207
1208 if (wskbd_console_device != NULL &&
1209 !wskbd_console_device->sc_translating)
1210 return 0;
1211
1212 for(;;) {
1213 if (num-- > 0) {
1214 ks = wskbd_console_data.t_symbols[pos++];
1215 if (KS_GROUP(ks) == KS_GROUP_Ascii)
1216 return (KS_VALUE(ks));
1217 } else {
1218 (*wskbd_console_data.t_consops->getc)
1219 (wskbd_console_data.t_consaccesscookie,
1220 &type, &data);
1221 num = wskbd_translate(&wskbd_console_data, type, data);
1222 pos = 0;
1223 }
1224 }
1225 }
1226
1227 void
1228 wskbd_cnpollc(dev, poll)
1229 dev_t dev;
1230 int poll;
1231 {
1232
1233 if (!wskbd_console_initted)
1234 return;
1235
1236 if (wskbd_console_device != NULL &&
1237 !wskbd_console_device->sc_translating)
1238 return;
1239
1240 (*wskbd_console_data.t_consops->pollc)
1241 (wskbd_console_data.t_consaccesscookie, poll);
1242 }
1243
1244 void
1245 wskbd_cnbell(dev, pitch, period, volume)
1246 dev_t dev;
1247 u_int pitch, period, volume;
1248 {
1249
1250 if (!wskbd_console_initted)
1251 return;
1252
1253 if (wskbd_console_data.t_consops->bell != NULL)
1254 (*wskbd_console_data.t_consops->bell)
1255 (wskbd_console_data.t_consaccesscookie, pitch, period,
1256 volume);
1257 }
1258
1259 static inline void
1260 update_leds(id)
1261 struct wskbd_internal *id;
1262 {
1263 int new_state;
1264
1265 new_state = 0;
1266 if (id->t_modifiers & (MOD_SHIFTLOCK | MOD_CAPSLOCK))
1267 new_state |= WSKBD_LED_CAPS;
1268 if (id->t_modifiers & MOD_NUMLOCK)
1269 new_state |= WSKBD_LED_NUM;
1270 if (id->t_modifiers & MOD_COMPOSE)
1271 new_state |= WSKBD_LED_COMPOSE;
1272 if (id->t_modifiers & MOD_HOLDSCREEN)
1273 new_state |= WSKBD_LED_SCROLL;
1274
1275 if (id->t_sc && new_state != id->t_sc->sc_ledstate) {
1276 (*id->t_sc->sc_accessops->set_leds)
1277 (id->t_sc->sc_accesscookie, new_state);
1278 id->t_sc->sc_ledstate = new_state;
1279 }
1280 }
1281
1282 static inline void
1283 update_modifier(id, type, toggle, mask)
1284 struct wskbd_internal *id;
1285 u_int type;
1286 int toggle;
1287 int mask;
1288 {
1289 if (toggle) {
1290 if (type == WSCONS_EVENT_KEY_DOWN)
1291 id->t_modifiers ^= mask;
1292 } else {
1293 if (type == WSCONS_EVENT_KEY_DOWN)
1294 id->t_modifiers |= mask;
1295 else
1296 id->t_modifiers &= ~mask;
1297 }
1298 }
1299
1300 #if NWSDISPLAY > 0
1301 static void
1302 change_displayparam(sc, param, updown, wraparound)
1303 struct wskbd_softc *sc;
1304 int param, updown, wraparound;
1305 {
1306 int res;
1307 struct wsdisplay_param dp;
1308
1309 if (sc->sc_displaydv == NULL)
1310 return;
1311
1312 dp.param = param;
1313 res = wsdisplay_param(sc->sc_displaydv, WSDISPLAYIO_GETPARAM, &dp);
1314
1315 if (res == EINVAL)
1316 return; /* no such parameter */
1317
1318 dp.curval += updown;
1319 if (dp.max < dp.curval)
1320 dp.curval = wraparound ? dp.min : dp.max;
1321 else
1322 if (dp.curval < dp.min)
1323 dp.curval = wraparound ? dp.max : dp.min;
1324 wsdisplay_param(sc->sc_displaydv, WSDISPLAYIO_SETPARAM, &dp);
1325 }
1326 #endif
1327
1328 static int
1329 internal_command(sc, type, ksym, ksym2)
1330 struct wskbd_softc *sc;
1331 u_int *type;
1332 keysym_t ksym, ksym2;
1333 {
1334 switch (ksym) {
1335 case KS_Cmd:
1336 update_modifier(sc->id, *type, 0, MOD_COMMAND);
1337 ksym = ksym2;
1338 break;
1339
1340 case KS_Cmd1:
1341 update_modifier(sc->id, *type, 0, MOD_COMMAND1);
1342 break;
1343
1344 case KS_Cmd2:
1345 update_modifier(sc->id, *type, 0, MOD_COMMAND2);
1346 break;
1347 }
1348
1349 if (*type != WSCONS_EVENT_KEY_DOWN ||
1350 (! MOD_ONESET(sc->id, MOD_COMMAND) &&
1351 ! MOD_ALLSET(sc->id, MOD_COMMAND1 | MOD_COMMAND2)))
1352 return (0);
1353
1354 switch (ksym) {
1355 #if defined(DDB) || defined(KGDB)
1356 case KS_Cmd_Debugger:
1357 if (sc->sc_isconsole) {
1358 #ifdef DDB
1359 console_debugger();
1360 #endif
1361 #ifdef KGDB
1362 kgdb_connect(1);
1363 #endif
1364 }
1365 /* discard this key (ddb discarded command modifiers) */
1366 *type = WSCONS_EVENT_KEY_UP;
1367 return (1);
1368 #endif
1369
1370 #if NWSDISPLAY > 0
1371 case KS_Cmd_Screen0:
1372 case KS_Cmd_Screen1:
1373 case KS_Cmd_Screen2:
1374 case KS_Cmd_Screen3:
1375 case KS_Cmd_Screen4:
1376 case KS_Cmd_Screen5:
1377 case KS_Cmd_Screen6:
1378 case KS_Cmd_Screen7:
1379 case KS_Cmd_Screen8:
1380 case KS_Cmd_Screen9:
1381 wsdisplay_switch(sc->sc_displaydv, ksym - KS_Cmd_Screen0, 0);
1382 return (1);
1383 case KS_Cmd_ResetEmul:
1384 wsdisplay_reset(sc->sc_displaydv, WSDISPLAY_RESETEMUL);
1385 return (1);
1386 case KS_Cmd_ResetClose:
1387 wsdisplay_reset(sc->sc_displaydv, WSDISPLAY_RESETCLOSE);
1388 return (1);
1389 case KS_Cmd_BacklightOn:
1390 case KS_Cmd_BacklightOff:
1391 case KS_Cmd_BacklightToggle:
1392 change_displayparam(sc, WSDISPLAYIO_PARAM_BACKLIGHT,
1393 ksym == KS_Cmd_BacklightOff ? -1 : 1,
1394 ksym == KS_Cmd_BacklightToggle ? 1 : 0);
1395 return (1);
1396 case KS_Cmd_BrightnessUp:
1397 case KS_Cmd_BrightnessDown:
1398 case KS_Cmd_BrightnessRotate:
1399 change_displayparam(sc, WSDISPLAYIO_PARAM_BRIGHTNESS,
1400 ksym == KS_Cmd_BrightnessDown ? -1 : 1,
1401 ksym == KS_Cmd_BrightnessRotate ? 1 : 0);
1402 return (1);
1403 case KS_Cmd_ContrastUp:
1404 case KS_Cmd_ContrastDown:
1405 case KS_Cmd_ContrastRotate:
1406 change_displayparam(sc, WSDISPLAYIO_PARAM_CONTRAST,
1407 ksym == KS_Cmd_ContrastDown ? -1 : 1,
1408 ksym == KS_Cmd_ContrastRotate ? 1 : 0);
1409 return (1);
1410 #endif
1411 }
1412 return (0);
1413 }
1414
1415 static int
1416 wskbd_translate(id, type, value)
1417 struct wskbd_internal *id;
1418 u_int type;
1419 int value;
1420 {
1421 struct wskbd_softc *sc = id->t_sc;
1422 keysym_t ksym, res, *group;
1423 struct wscons_keymap kpbuf, *kp;
1424 int iscommand = 0;
1425
1426 if (type == WSCONS_EVENT_ALL_KEYS_UP) {
1427 id->t_modifiers &= ~(MOD_SHIFT_L | MOD_SHIFT_R
1428 | MOD_CONTROL_L | MOD_CONTROL_R
1429 | MOD_META_L | MOD_META_R
1430 | MOD_MODESHIFT
1431 | MOD_COMMAND | MOD_COMMAND1 | MOD_COMMAND2);
1432 update_leds(id);
1433 return (0);
1434 }
1435
1436 if (sc != NULL) {
1437 if (value < 0 || value >= sc->sc_maplen) {
1438 #ifdef DEBUG
1439 printf("wskbd_translate: keycode %d out of range\n",
1440 value);
1441 #endif
1442 return (0);
1443 }
1444 kp = sc->sc_map + value;
1445 } else {
1446 kp = &kpbuf;
1447 wskbd_get_mapentry(id->t_keymap, value, kp);
1448 }
1449
1450 /* if this key has a command, process it first */
1451 if (sc != NULL && kp->command != KS_voidSymbol)
1452 iscommand = internal_command(sc, &type, kp->command,
1453 kp->group1[0]);
1454
1455 /* Now update modifiers */
1456 switch (kp->group1[0]) {
1457 case KS_Shift_L:
1458 update_modifier(id, type, 0, MOD_SHIFT_L);
1459 break;
1460
1461 case KS_Shift_R:
1462 update_modifier(id, type, 0, MOD_SHIFT_R);
1463 break;
1464
1465 case KS_Shift_Lock:
1466 update_modifier(id, type, 1, MOD_SHIFTLOCK);
1467 break;
1468
1469 case KS_Caps_Lock:
1470 update_modifier(id, type, 1, MOD_CAPSLOCK);
1471 break;
1472
1473 case KS_Control_L:
1474 update_modifier(id, type, 0, MOD_CONTROL_L);
1475 break;
1476
1477 case KS_Control_R:
1478 update_modifier(id, type, 0, MOD_CONTROL_R);
1479 break;
1480
1481 case KS_Alt_L:
1482 update_modifier(id, type, 0, MOD_META_L);
1483 break;
1484
1485 case KS_Alt_R:
1486 update_modifier(id, type, 0, MOD_META_R);
1487 break;
1488
1489 case KS_Mode_switch:
1490 update_modifier(id, type, 0, MOD_MODESHIFT);
1491 break;
1492
1493 case KS_Num_Lock:
1494 update_modifier(id, type, 1, MOD_NUMLOCK);
1495 break;
1496
1497 #if NWSDISPLAY > 0
1498 case KS_Hold_Screen:
1499 if (sc != NULL) {
1500 update_modifier(id, type, 1, MOD_HOLDSCREEN);
1501 wskbd_holdscreen(sc, id->t_modifiers & MOD_HOLDSCREEN);
1502 }
1503 break;
1504 #endif
1505 }
1506
1507 /* If this is a key release or we are in command mode, we are done */
1508 if (type != WSCONS_EVENT_KEY_DOWN || iscommand) {
1509 update_leds(id);
1510 return (0);
1511 }
1512
1513 /* Get the keysym */
1514 if (id->t_modifiers & MOD_MODESHIFT)
1515 group = & kp->group2[0];
1516 else
1517 group = & kp->group1[0];
1518
1519 if ((id->t_modifiers & MOD_NUMLOCK) != 0 &&
1520 KS_GROUP(group[1]) == KS_GROUP_Keypad) {
1521 if (MOD_ONESET(id, MOD_ANYSHIFT))
1522 ksym = group[0];
1523 else
1524 ksym = group[1];
1525 } else if (! MOD_ONESET(id, MOD_ANYSHIFT | MOD_CAPSLOCK)) {
1526 ksym = group[0];
1527 } else if (MOD_ONESET(id, MOD_CAPSLOCK)) {
1528 if (! MOD_ONESET(id, MOD_SHIFT_L | MOD_SHIFT_R))
1529 ksym = group[0];
1530 else
1531 ksym = group[1];
1532 if (ksym >= KS_a && ksym <= KS_z)
1533 ksym += KS_A - KS_a;
1534 else if (ksym >= KS_agrave && ksym <= KS_thorn &&
1535 ksym != KS_division)
1536 ksym += KS_Agrave - KS_agrave;
1537 } else if (MOD_ONESET(id, MOD_ANYSHIFT)) {
1538 ksym = group[1];
1539 } else {
1540 ksym = group[0];
1541 }
1542
1543 /* Process compose sequence and dead accents */
1544 res = KS_voidSymbol;
1545
1546 switch (KS_GROUP(ksym)) {
1547 case KS_GROUP_Ascii:
1548 case KS_GROUP_Keypad:
1549 case KS_GROUP_Function:
1550 res = ksym;
1551 break;
1552
1553 case KS_GROUP_Mod:
1554 if (ksym == KS_Multi_key) {
1555 update_modifier(id, 1, 0, MOD_COMPOSE);
1556 id->t_composelen = 2;
1557 }
1558 break;
1559
1560 case KS_GROUP_Dead:
1561 if (id->t_composelen == 0) {
1562 update_modifier(id, 1, 0, MOD_COMPOSE);
1563 id->t_composelen = 1;
1564 id->t_composebuf[0] = ksym;
1565 } else
1566 res = ksym;
1567 break;
1568 }
1569
1570 if (res == KS_voidSymbol) {
1571 update_leds(id);
1572 return (0);
1573 }
1574
1575 if (id->t_composelen > 0) {
1576 id->t_composebuf[2 - id->t_composelen] = res;
1577 if (--id->t_composelen == 0) {
1578 res = wskbd_compose_value(id->t_composebuf);
1579 update_modifier(id, 0, 0, MOD_COMPOSE);
1580 } else {
1581 return (0);
1582 }
1583 }
1584
1585 update_leds(id);
1586
1587 /* We are done, return the symbol */
1588 if (KS_GROUP(res) == KS_GROUP_Ascii) {
1589 if (MOD_ONESET(id, MOD_ANYCONTROL)) {
1590 if ((res >= KS_at && res <= KS_z) || res == KS_space)
1591 res = res & 0x1f;
1592 else if (res == KS_2)
1593 res = 0x00;
1594 else if (res >= KS_3 && res <= KS_7)
1595 res = KS_Escape + (res - KS_3);
1596 else if (res == KS_8)
1597 res = KS_Delete;
1598 }
1599 if (MOD_ONESET(id, MOD_ANYMETA)) {
1600 if (id->t_flags & WSKFL_METAESC) {
1601 id->t_symbols[0] = KS_Escape;
1602 id->t_symbols[1] = res;
1603 return (2);
1604 } else
1605 res |= 0x80;
1606 }
1607 }
1608
1609 id->t_symbols[0] = res;
1610 return (1);
1611 }
1612