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