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