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