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