wskbd.c revision 1.41.2.4 1 /* $NetBSD: wskbd.c,v 1.41.2.4 2002/02/11 20:10:21 jdolecek 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.4 2002/02/11 20:10:21 jdolecek 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|M_ZERO);
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_repeating = 0;
389 sc->sc_translating = 1;
390 sc->sc_ledstate = -1; /* force update */
391
392 if (wskbd_load_keymap(sc->id->t_keymap,
393 &sc->sc_map, &sc->sc_maplen) != 0)
394 panic("cannot load keymap");
395
396 sc->sc_layout = sc->id->t_keymap->layout;
397
398 /* set default bell and key repeat data */
399 sc->sc_bell_data = wskbd_default_bell_data;
400 sc->sc_keyrepeat_data = wskbd_default_keyrepeat_data;
401
402 if (ap->console) {
403 KASSERT(wskbd_console_initted);
404 KASSERT(wskbd_console_device == NULL);
405
406 wskbd_console_device = sc;
407
408 printf(": console keyboard");
409
410 #if NWSDISPLAY > 0
411 wsdisplay_set_console_kbd(&sc->sc_base); /* sets me_dispv */
412 if (sc->sc_base.me_dispdv != NULL)
413 printf(", using %s", sc->sc_base.me_dispdv->dv_xname);
414 #endif
415 }
416 printf("\n");
417
418 #if NWSMUX > 0
419 if (mux >= 0) {
420 error = wsmux_attach_sc(wsmux_getmux(mux), &sc->sc_base);
421 if (error)
422 printf("%s: attach error=%d\n",
423 sc->sc_base.me_dv.dv_xname, error);
424 }
425 #endif
426 }
427
428 void
429 wskbd_cnattach(const struct wskbd_consops *consops, 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(void)
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(void *v)
467 {
468 struct wskbd_softc *sc = (struct wskbd_softc *)v;
469 int s = spltty();
470
471 if (!sc->sc_repeating) {
472 /*
473 * race condition: a "key up" event came in when wskbd_repeat()
474 * was already called but not yet spltty()'d
475 */
476 splx(s);
477 return;
478 }
479 if (sc->sc_base.me_dispdv != NULL) {
480 int i;
481 for (i = 0; i < sc->sc_repeating; i++)
482 wsdisplay_kbdinput(sc->sc_base.me_dispdv,
483 sc->id->t_symbols[i]);
484 }
485 callout_reset(&sc->sc_repeat_ch,
486 (hz * sc->sc_keyrepeat_data.delN) / 1000, wskbd_repeat, sc);
487 splx(s);
488 }
489 #endif
490
491 int
492 wskbd_activate(struct device *self, enum devact act)
493 {
494 struct wskbd_softc *sc = (struct wskbd_softc *)self;
495
496 if (act == DVACT_DEACTIVATE)
497 sc->sc_dying = 1;
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(struct device *self, int flags)
511 {
512 struct wskbd_softc *sc = (struct wskbd_softc *)self;
513 struct wseventvar *evar;
514 int maj, mn;
515 int s;
516
517 #if NWSMUX > 0
518 /* Tell parent mux we're leaving. */
519 if (sc->sc_base.me_parent != NULL)
520 wsmux_detach_sc(&sc->sc_base);
521 #endif
522
523 if (sc->sc_isconsole) {
524 KASSERT(wskbd_console_device == sc);
525 wskbd_console_device = NULL;
526 }
527
528 evar = sc->sc_base.me_evp;
529 if (evar != NULL && evar->io != NULL) {
530 s = spltty();
531 if (--sc->sc_refcnt >= 0) {
532 /* Wake everyone by generating a dummy event. */
533 if (++evar->put >= WSEVENT_QSIZE)
534 evar->put = 0;
535 WSEVENT_WAKEUP(evar);
536 /* Wait for processes to go away. */
537 if (tsleep(sc, PZERO, "wskdet", hz * 60))
538 printf("wskbd_detach: %s didn't detach\n",
539 sc->sc_base.me_dv.dv_xname);
540 }
541 splx(s);
542 }
543
544 /* locate the major number */
545 for (maj = 0; maj < nchrdev; maj++)
546 if (cdevsw[maj].d_open == wskbdopen)
547 break;
548
549 /* Nuke the vnodes for any open instances. */
550 mn = self->dv_unit;
551 vdevgone(maj, mn, mn, VCHR);
552
553 return (0);
554 }
555
556 void
557 wskbd_input(struct device *dev, u_int type, int value)
558 {
559 struct wskbd_softc *sc = (struct wskbd_softc *)dev;
560 struct wscons_event *ev;
561 struct wseventvar *evar;
562 struct timeval thistime;
563 #if NWSDISPLAY > 0
564 int num, i;
565 #endif
566 int put;
567
568 #if NWSDISPLAY > 0
569 if (sc->sc_repeating) {
570 sc->sc_repeating = 0;
571 callout_stop(&sc->sc_repeat_ch);
572 }
573
574 /*
575 * If /dev/wskbdN is not connected in event mode translate and
576 * send upstream.
577 */
578 if (sc->sc_translating) {
579 num = wskbd_translate(sc->id, type, value);
580 if (num > 0) {
581 if (sc->sc_base.me_dispdv != NULL) {
582 for (i = 0; i < num; i++)
583 wsdisplay_kbdinput(
584 sc->sc_base.me_dispdv,
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 evar = sc->sc_base.me_evp;
604 if (evar == NULL) {
605 DPRINTF(("wskbd_input: not open\n"));
606 return;
607 }
608
609 #ifdef DIAGNOSTIC
610 if (evar->q == NULL) {
611 printf("wskbd_input: evar->q=NULL\n");
612 return;
613 }
614 #endif
615
616 put = evar->put;
617 ev = &evar->q[put];
618 put = (put + 1) % WSEVENT_QSIZE;
619 if (put == evar->get) {
620 log(LOG_WARNING, "%s: event queue overflow\n",
621 sc->sc_base.me_dv.dv_xname);
622 return;
623 }
624 ev->type = type;
625 ev->value = value;
626 microtime(&thistime);
627 TIMEVAL_TO_TIMESPEC(&thistime, &ev->time);
628 evar->put = put;
629 WSEVENT_WAKEUP(evar);
630 }
631
632 #ifdef WSDISPLAY_COMPAT_RAWKBD
633 void
634 wskbd_rawinput(struct device *dev, u_char *buf, int len)
635 {
636 #if NWSDISPLAY > 0
637 struct wskbd_softc *sc = (struct wskbd_softc *)dev;
638 int i;
639
640 if (sc->sc_base.me_dispdv != NULL)
641 for (i = 0; i < len; i++)
642 wsdisplay_kbdinput(sc->sc_base.me_dispdv, 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(struct wskbd_softc *sc, int hold)
651 {
652 int new_state;
653
654 if (sc->sc_base.me_dispdv != NULL) {
655 wsdisplay_kbdholdscreen(sc->sc_base.me_dispdv, hold);
656 new_state = sc->sc_ledstate;
657 if (hold)
658 new_state |= WSKBD_LED_SCROLL;
659 else
660 new_state &= ~WSKBD_LED_SCROLL;
661 if (new_state != sc->sc_ledstate) {
662 (*sc->sc_accessops->set_leds)(sc->sc_accesscookie,
663 new_state);
664 sc->sc_ledstate = new_state;
665 }
666 }
667 }
668 #endif
669
670 static int
671 wskbd_enable(struct wskbd_softc *sc, int on)
672 {
673 int error;
674
675 #if 0
676 /* I don't understand the purpose of this code. And it seems to
677 * break things, so it's out. -- Lennart
678 */
679 if (!on && (!sc->sc_translating
680 #if NWSDISPLAY > 0
681 || sc->sc_base.me_dispdv
682 #endif
683 ))
684 return (EBUSY);
685 #endif
686 #if NWSDISPLAY > 0
687 if (sc->sc_base.me_dispdv != NULL)
688 return (0);
689 #endif
690
691 error = (*sc->sc_accessops->enable)(sc->sc_accesscookie, on);
692 DPRINTF(("wskbd_enable: sc=%p on=%d res=%d\n", sc, on, error));
693 return (error);
694 }
695
696 #if NWSMUX > 0
697 int
698 wskbd_mux_open(struct wsevsrc *me, struct wseventvar *evp)
699 {
700 struct wskbd_softc *sc = (struct wskbd_softc *)me;
701
702 if (sc->sc_dying)
703 return (EIO);
704
705 if (sc->sc_base.me_evp != NULL)
706 return (EBUSY);
707
708 return (wskbd_do_open(sc, evp));
709 }
710 #endif
711
712 int
713 wskbdopen(dev_t dev, int flags, int mode, struct proc *p)
714 {
715 struct wskbd_softc *sc;
716 struct wseventvar *evar;
717 int unit, error;
718
719 unit = minor(dev);
720 if (unit >= wskbd_cd.cd_ndevs || /* make sure it was attached */
721 (sc = wskbd_cd.cd_devs[unit]) == NULL)
722 return (ENXIO);
723
724 #if NWSMUX > 0
725 DPRINTF(("wskbdopen: %s mux=%p p=%p\n", sc->sc_base.me_dv.dv_xname,
726 sc->sc_base.me_parent, p));
727 #endif
728
729 if (sc->sc_dying)
730 return (EIO);
731
732 if ((flags & (FREAD | FWRITE)) == FWRITE)
733 /* Not opening for read, only ioctl is available. */
734 return (0);
735
736 #if NWSMUX > 0
737 if (sc->sc_base.me_parent != NULL) {
738 /* Grab the keyboard out of the greedy hands of the mux. */
739 DPRINTF(("wskbdopen: detach\n"));
740 wsmux_detach_sc(&sc->sc_base);
741 }
742 #endif
743
744 if (sc->sc_base.me_evp != NULL)
745 return (EBUSY);
746
747 evar = &sc->sc_base.me_evar;
748 wsevent_init(evar);
749 evar->io = p;
750
751 error = wskbd_do_open(sc, evar);
752 if (error) {
753 DPRINTF(("wskbdopen: %s open failed\n",
754 sc->sc_base.me_dv.dv_xname));
755 sc->sc_base.me_evp = NULL;
756 wsevent_fini(evar);
757 }
758 return (error);
759 }
760
761 int
762 wskbd_do_open(struct wskbd_softc *sc, struct wseventvar *evp)
763 {
764 sc->sc_base.me_evp = evp;
765 sc->sc_translating = 0;
766
767 return (wskbd_enable(sc, 1));
768 }
769
770 int
771 wskbdclose(dev_t dev, int flags, int mode, struct proc *p)
772 {
773 struct wskbd_softc *sc =
774 (struct wskbd_softc *)wskbd_cd.cd_devs[minor(dev)];
775 struct wseventvar *evar = sc->sc_base.me_evp;
776
777 if (evar == NULL)
778 /* not open for read */
779 return (0);
780
781 sc->sc_base.me_evp = NULL;
782 sc->sc_translating = 1;
783 (void)wskbd_enable(sc, 0);
784 wsevent_fini(evar);
785
786 return (0);
787 }
788
789 #if NWSMUX > 0
790 int
791 wskbd_mux_close(struct wsevsrc *me)
792 {
793 struct wskbd_softc *sc = (struct wskbd_softc *)me;
794
795 sc->sc_base.me_evp = NULL;
796 sc->sc_translating = 1;
797 (void)wskbd_enable(sc, 0);
798
799 return (0);
800 }
801 #endif
802
803 int
804 wskbdread(dev_t dev, struct uio *uio, int flags)
805 {
806 struct wskbd_softc *sc = wskbd_cd.cd_devs[minor(dev)];
807 int error;
808
809 if (sc->sc_dying)
810 return (EIO);
811
812 #ifdef DIAGNOSTIC
813 if (sc->sc_base.me_evp == NULL) {
814 printf("wskbdread: evp == NULL\n");
815 return (EINVAL);
816 }
817 #endif
818
819 sc->sc_refcnt++;
820 error = wsevent_read(sc->sc_base.me_evp, uio, flags);
821 if (--sc->sc_refcnt < 0) {
822 wakeup(sc);
823 error = EIO;
824 }
825 return (error);
826 }
827
828 int
829 wskbdioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct proc *p)
830 {
831 return (wskbd_do_ioctl(wskbd_cd.cd_devs[minor(dev)], cmd, data, flag,p));
832 }
833
834 /* A wrapper around the ioctl() workhorse to make reference counting easy. */
835 int
836 wskbd_do_ioctl(struct device *dv, u_long cmd, caddr_t data, int flag,
837 struct proc *p)
838 {
839 struct wskbd_softc *sc = (struct wskbd_softc *)dv;
840 int error;
841
842 sc->sc_refcnt++;
843 error = wskbd_do_ioctl_sc(sc, cmd, data, flag, p);
844 if (--sc->sc_refcnt < 0)
845 wakeup(sc);
846 return (error);
847 }
848
849 int
850 wskbd_do_ioctl_sc(struct wskbd_softc *sc, u_long cmd, caddr_t data, int flag,
851 struct proc *p)
852 {
853 int error;
854
855 /*
856 * Try the generic ioctls that the wskbd interface supports.
857 */
858 switch (cmd) {
859 case FIONBIO: /* we will remove this someday (soon???) */
860 return (0);
861
862 case FIOASYNC:
863 if (sc->sc_base.me_evp == NULL)
864 return (EINVAL);
865 sc->sc_base.me_evp->async = *(int *)data != 0;
866 return (0);
867
868 case TIOCSPGRP:
869 if (sc->sc_base.me_evp == NULL)
870 return (EINVAL);
871 if (*(int *)data != sc->sc_base.me_evp->io->p_pgid)
872 return (EPERM);
873 return (0);
874 }
875
876 /*
877 * Try the keyboard driver for WSKBDIO ioctls. It returns -1
878 * if it didn't recognize the request.
879 */
880 error = wskbd_displayioctl(&sc->sc_base.me_dv, cmd, data, flag, p);
881 return (error != -1 ? error : ENOTTY);
882 }
883
884 /*
885 * WSKBDIO ioctls, handled in both emulation mode and in ``raw'' mode.
886 * Some of these have no real effect in raw mode, however.
887 */
888 static int
889 wskbd_displayioctl(struct device *dev, u_long cmd, caddr_t data, int flag,
890 struct proc *p)
891 {
892 struct wskbd_softc *sc = (struct wskbd_softc *)dev;
893 struct wskbd_bell_data *ubdp, *kbdp;
894 struct wskbd_keyrepeat_data *ukdp, *kkdp;
895 struct wskbd_map_data *umdp;
896 struct wskbd_mapdata md;
897 kbd_t enc;
898 void *buf;
899 int len, error;
900
901 switch (cmd) {
902 #define SETBELL(dstp, srcp, dfltp) \
903 do { \
904 (dstp)->pitch = ((srcp)->which & WSKBD_BELL_DOPITCH) ? \
905 (srcp)->pitch : (dfltp)->pitch; \
906 (dstp)->period = ((srcp)->which & WSKBD_BELL_DOPERIOD) ? \
907 (srcp)->period : (dfltp)->period; \
908 (dstp)->volume = ((srcp)->which & WSKBD_BELL_DOVOLUME) ? \
909 (srcp)->volume : (dfltp)->volume; \
910 (dstp)->which = WSKBD_BELL_DOALL; \
911 } while (0)
912
913 case WSKBDIO_BELL:
914 if ((flag & FWRITE) == 0)
915 return (EACCES);
916 return ((*sc->sc_accessops->ioctl)(sc->sc_accesscookie,
917 WSKBDIO_COMPLEXBELL, (caddr_t)&sc->sc_bell_data, flag, p));
918
919 case WSKBDIO_COMPLEXBELL:
920 if ((flag & FWRITE) == 0)
921 return (EACCES);
922 ubdp = (struct wskbd_bell_data *)data;
923 SETBELL(ubdp, ubdp, &sc->sc_bell_data);
924 return ((*sc->sc_accessops->ioctl)(sc->sc_accesscookie,
925 WSKBDIO_COMPLEXBELL, (caddr_t)ubdp, flag, p));
926
927 case WSKBDIO_SETBELL:
928 if ((flag & FWRITE) == 0)
929 return (EACCES);
930 kbdp = &sc->sc_bell_data;
931 setbell:
932 ubdp = (struct wskbd_bell_data *)data;
933 SETBELL(kbdp, ubdp, kbdp);
934 return (0);
935
936 case WSKBDIO_GETBELL:
937 kbdp = &sc->sc_bell_data;
938 getbell:
939 ubdp = (struct wskbd_bell_data *)data;
940 SETBELL(ubdp, kbdp, kbdp);
941 return (0);
942
943 case WSKBDIO_SETDEFAULTBELL:
944 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
945 return (error);
946 kbdp = &wskbd_default_bell_data;
947 goto setbell;
948
949
950 case WSKBDIO_GETDEFAULTBELL:
951 kbdp = &wskbd_default_bell_data;
952 goto getbell;
953
954 #undef SETBELL
955
956 #define SETKEYREPEAT(dstp, srcp, dfltp) \
957 do { \
958 (dstp)->del1 = ((srcp)->which & WSKBD_KEYREPEAT_DODEL1) ? \
959 (srcp)->del1 : (dfltp)->del1; \
960 (dstp)->delN = ((srcp)->which & WSKBD_KEYREPEAT_DODELN) ? \
961 (srcp)->delN : (dfltp)->delN; \
962 (dstp)->which = WSKBD_KEYREPEAT_DOALL; \
963 } while (0)
964
965 case WSKBDIO_SETKEYREPEAT:
966 if ((flag & FWRITE) == 0)
967 return (EACCES);
968 kkdp = &sc->sc_keyrepeat_data;
969 setkeyrepeat:
970 ukdp = (struct wskbd_keyrepeat_data *)data;
971 SETKEYREPEAT(kkdp, ukdp, kkdp);
972 return (0);
973
974 case WSKBDIO_GETKEYREPEAT:
975 kkdp = &sc->sc_keyrepeat_data;
976 getkeyrepeat:
977 ukdp = (struct wskbd_keyrepeat_data *)data;
978 SETKEYREPEAT(ukdp, kkdp, kkdp);
979 return (0);
980
981 case WSKBDIO_SETDEFAULTKEYREPEAT:
982 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
983 return (error);
984 kkdp = &wskbd_default_keyrepeat_data;
985 goto setkeyrepeat;
986
987
988 case WSKBDIO_GETDEFAULTKEYREPEAT:
989 kkdp = &wskbd_default_keyrepeat_data;
990 goto getkeyrepeat;
991
992 #undef SETKEYREPEAT
993
994 case WSKBDIO_SETMAP:
995 if ((flag & FWRITE) == 0)
996 return (EACCES);
997 umdp = (struct wskbd_map_data *)data;
998 if (umdp->maplen > WSKBDIO_MAXMAPLEN)
999 return (EINVAL);
1000
1001 len = umdp->maplen*sizeof(struct wscons_keymap);
1002 buf = malloc(len, M_TEMP, M_WAITOK);
1003 error = copyin(umdp->map, buf, len);
1004 if (error == 0) {
1005 wskbd_init_keymap(umdp->maplen,
1006 &sc->sc_map, &sc->sc_maplen);
1007 memcpy(sc->sc_map, buf, len);
1008 /* drop the variant bits handled by the map */
1009 sc->sc_layout = KB_USER |
1010 (KB_VARIANT(sc->sc_layout) & KB_HANDLEDBYWSKBD);
1011 wskbd_update_layout(sc->id, sc->sc_layout);
1012 }
1013 free(buf, M_TEMP);
1014 return(error);
1015
1016 case WSKBDIO_GETMAP:
1017 umdp = (struct wskbd_map_data *)data;
1018 if (umdp->maplen > sc->sc_maplen)
1019 umdp->maplen = sc->sc_maplen;
1020 error = copyout(sc->sc_map, umdp->map,
1021 umdp->maplen*sizeof(struct wscons_keymap));
1022 return(error);
1023
1024 case WSKBDIO_GETENCODING:
1025 *((kbd_t *) data) = sc->sc_layout;
1026 return(0);
1027
1028 case WSKBDIO_SETENCODING:
1029 if ((flag & FWRITE) == 0)
1030 return (EACCES);
1031 enc = *((kbd_t *)data);
1032 if (KB_ENCODING(enc) == KB_USER) {
1033 /* user map must already be loaded */
1034 if (KB_ENCODING(sc->sc_layout) != KB_USER)
1035 return (EINVAL);
1036 /* map variants make no sense */
1037 if (KB_VARIANT(enc) & ~KB_HANDLEDBYWSKBD)
1038 return (EINVAL);
1039 } else {
1040 md = *(sc->id->t_keymap); /* structure assignment */
1041 md.layout = enc;
1042 error = wskbd_load_keymap(&md, &sc->sc_map,
1043 &sc->sc_maplen);
1044 if (error)
1045 return (error);
1046 }
1047 sc->sc_layout = enc;
1048 wskbd_update_layout(sc->id, enc);
1049 return (0);
1050 }
1051
1052 /*
1053 * Try the keyboard driver for WSKBDIO ioctls. It returns -1
1054 * if it didn't recognize the request, and in turn we return
1055 * -1 if we didn't recognize the request.
1056 */
1057 /* printf("kbdaccess\n"); */
1058 error = (*sc->sc_accessops->ioctl)(sc->sc_accesscookie, cmd, data,
1059 flag, p);
1060 #ifdef WSDISPLAY_COMPAT_RAWKBD
1061 if (!error && cmd == WSKBDIO_SETMODE && *(int *)data == WSKBD_RAW) {
1062 int s = spltty();
1063 sc->id->t_modifiers &= ~(MOD_SHIFT_L | MOD_SHIFT_R
1064 | MOD_CONTROL_L | MOD_CONTROL_R
1065 | MOD_META_L | MOD_META_R
1066 | MOD_COMMAND
1067 | MOD_COMMAND1 | MOD_COMMAND2);
1068 #if NWSDISPLAY > 0
1069 if (sc->sc_repeating) {
1070 sc->sc_repeating = 0;
1071 callout_stop(&sc->sc_repeat_ch);
1072 }
1073 #endif
1074 splx(s);
1075 }
1076 #endif
1077 return (error);
1078 }
1079
1080 int
1081 wskbdpoll(dev_t dev, int events, struct proc *p)
1082 {
1083 struct wskbd_softc *sc = wskbd_cd.cd_devs[minor(dev)];
1084
1085 if (sc->sc_base.me_evp == NULL)
1086 return (EINVAL);
1087 return (wsevent_poll(sc->sc_base.me_evp, events, p));
1088 }
1089
1090 int
1091 wskbdkqfilter(dev_t dev, struct knote *kn)
1092 {
1093 struct wskbd_softc *sc = wskbd_cd.cd_devs[minor(dev)];
1094
1095 if (sc->sc_base.me_evp == NULL)
1096 return (1);
1097 return (wsevent_kqfilter(sc->sc_base.me_evp, kn));
1098 }
1099
1100 #if NWSDISPLAY > 0
1101
1102 int
1103 wskbd_pickfree(void)
1104 {
1105 int i;
1106 struct wskbd_softc *sc;
1107
1108 for (i = 0; i < wskbd_cd.cd_ndevs; i++) {
1109 if ((sc = wskbd_cd.cd_devs[i]) == NULL)
1110 continue;
1111 if (sc->sc_base.me_dispdv == NULL)
1112 return (i);
1113 }
1114 return (-1);
1115 }
1116
1117 struct wsevsrc *
1118 wskbd_set_console_display(struct device *displaydv, struct wsevsrc *me)
1119 {
1120 struct wskbd_softc *sc = wskbd_console_device;
1121
1122 if (sc == NULL)
1123 return (NULL);
1124 sc->sc_base.me_dispdv = displaydv;
1125 #if NWSMUX > 0
1126 (void)wsmux_attach_sc((struct wsmux_softc *)me, &sc->sc_base);
1127 #endif
1128 return (&sc->sc_base);
1129 }
1130
1131 int
1132 wskbd_set_display(struct device *dv, struct wsevsrc *me)
1133 {
1134 struct wskbd_softc *sc = (struct wskbd_softc *)dv;
1135 struct device *displaydv = me != NULL ? me->me_dispdv : NULL;
1136 struct device *odisplaydv;
1137 int error;
1138
1139 DPRINTF(("wskbd_set_display: %s me=%p odisp=%p disp=%p cons=%d\n",
1140 dv->dv_xname, me, sc->sc_base.me_dispdv, displaydv,
1141 sc->sc_isconsole));
1142
1143 if (sc->sc_isconsole)
1144 return (EBUSY);
1145
1146 if (displaydv != NULL) {
1147 if (sc->sc_base.me_dispdv != NULL)
1148 return (EBUSY);
1149 } else {
1150 if (sc->sc_base.me_dispdv == NULL)
1151 return (ENXIO);
1152 }
1153
1154 odisplaydv = sc->sc_base.me_dispdv;
1155 sc->sc_base.me_dispdv = NULL;
1156 error = wskbd_enable(sc, displaydv != NULL);
1157 sc->sc_base.me_dispdv = displaydv;
1158 if (error) {
1159 sc->sc_base.me_dispdv = odisplaydv;
1160 return (error);
1161 }
1162
1163 if (displaydv)
1164 printf("%s: connecting to %s\n",
1165 sc->sc_base.me_dv.dv_xname, displaydv->dv_xname);
1166 else
1167 printf("%s: disconnecting from %s\n",
1168 sc->sc_base.me_dv.dv_xname, odisplaydv->dv_xname);
1169
1170 return (0);
1171 }
1172
1173 #endif /* NWSDISPLAY > 0 */
1174
1175 #if NWSMUX > 0
1176 int
1177 wskbd_add_mux(int unit, struct wsmux_softc *muxsc)
1178 {
1179 struct wskbd_softc *sc;
1180
1181 if (unit < 0 || unit >= wskbd_cd.cd_ndevs ||
1182 (sc = wskbd_cd.cd_devs[unit]) == NULL)
1183 return (ENXIO);
1184
1185 if (sc->sc_base.me_parent != NULL || sc->sc_base.me_evp != NULL)
1186 return (EBUSY);
1187
1188 return (wsmux_attach_sc(muxsc, &sc->sc_base));
1189 }
1190 #endif
1191
1192 /*
1193 * Console interface.
1194 */
1195 int
1196 wskbd_cngetc(dev_t dev)
1197 {
1198 static int num = 0;
1199 static int pos;
1200 u_int type;
1201 int data;
1202 keysym_t ks;
1203
1204 if (!wskbd_console_initted)
1205 return 0;
1206
1207 if (wskbd_console_device != NULL &&
1208 !wskbd_console_device->sc_translating)
1209 return 0;
1210
1211 for(;;) {
1212 if (num-- > 0) {
1213 ks = wskbd_console_data.t_symbols[pos++];
1214 if (KS_GROUP(ks) == KS_GROUP_Ascii)
1215 return (KS_VALUE(ks));
1216 } else {
1217 (*wskbd_console_data.t_consops->getc)
1218 (wskbd_console_data.t_consaccesscookie,
1219 &type, &data);
1220 num = wskbd_translate(&wskbd_console_data, type, data);
1221 pos = 0;
1222 }
1223 }
1224 }
1225
1226 void
1227 wskbd_cnpollc(dev_t dev, int poll)
1228 {
1229
1230 if (!wskbd_console_initted)
1231 return;
1232
1233 if (wskbd_console_device != NULL &&
1234 !wskbd_console_device->sc_translating)
1235 return;
1236
1237 (*wskbd_console_data.t_consops->pollc)
1238 (wskbd_console_data.t_consaccesscookie, poll);
1239 }
1240
1241 void
1242 wskbd_cnbell(dev_t dev, u_int pitch, u_int period, u_int volume)
1243 {
1244
1245 if (!wskbd_console_initted)
1246 return;
1247
1248 if (wskbd_console_data.t_consops->bell != NULL)
1249 (*wskbd_console_data.t_consops->bell)
1250 (wskbd_console_data.t_consaccesscookie, pitch, period,
1251 volume);
1252 }
1253
1254 static inline void
1255 update_leds(struct wskbd_internal *id)
1256 {
1257 int new_state;
1258
1259 new_state = 0;
1260 if (id->t_modifiers & (MOD_SHIFTLOCK | MOD_CAPSLOCK))
1261 new_state |= WSKBD_LED_CAPS;
1262 if (id->t_modifiers & MOD_NUMLOCK)
1263 new_state |= WSKBD_LED_NUM;
1264 if (id->t_modifiers & MOD_COMPOSE)
1265 new_state |= WSKBD_LED_COMPOSE;
1266 if (id->t_modifiers & MOD_HOLDSCREEN)
1267 new_state |= WSKBD_LED_SCROLL;
1268
1269 if (id->t_sc && new_state != id->t_sc->sc_ledstate) {
1270 (*id->t_sc->sc_accessops->set_leds)
1271 (id->t_sc->sc_accesscookie, new_state);
1272 id->t_sc->sc_ledstate = new_state;
1273 }
1274 }
1275
1276 static inline void
1277 update_modifier(struct wskbd_internal *id, u_int type, int toggle, int mask)
1278 {
1279 if (toggle) {
1280 if (type == WSCONS_EVENT_KEY_DOWN)
1281 id->t_modifiers ^= mask;
1282 } else {
1283 if (type == WSCONS_EVENT_KEY_DOWN)
1284 id->t_modifiers |= mask;
1285 else
1286 id->t_modifiers &= ~mask;
1287 }
1288 }
1289
1290 #if NWSDISPLAY > 0
1291 static void
1292 change_displayparam(struct wskbd_softc *sc, int param, int updown,
1293 int wraparound)
1294 {
1295 int res;
1296 struct wsdisplay_param dp;
1297
1298 if (sc->sc_base.me_dispdv == NULL)
1299 return;
1300
1301 dp.param = param;
1302 res = wsdisplay_param(sc->sc_base.me_dispdv, WSDISPLAYIO_GETPARAM, &dp);
1303
1304 if (res == EINVAL)
1305 return; /* no such parameter */
1306
1307 dp.curval += updown;
1308 if (dp.max < dp.curval)
1309 dp.curval = wraparound ? dp.min : dp.max;
1310 else
1311 if (dp.curval < dp.min)
1312 dp.curval = wraparound ? dp.max : dp.min;
1313 wsdisplay_param(sc->sc_base.me_dispdv, WSDISPLAYIO_SETPARAM, &dp);
1314 }
1315 #endif
1316
1317 static int
1318 internal_command(struct wskbd_softc *sc, u_int *type, keysym_t ksym,
1319 keysym_t ksym2)
1320 {
1321 switch (ksym) {
1322 case KS_Cmd:
1323 update_modifier(sc->id, *type, 0, MOD_COMMAND);
1324 ksym = ksym2;
1325 break;
1326
1327 case KS_Cmd1:
1328 update_modifier(sc->id, *type, 0, MOD_COMMAND1);
1329 break;
1330
1331 case KS_Cmd2:
1332 update_modifier(sc->id, *type, 0, MOD_COMMAND2);
1333 break;
1334 }
1335
1336 if (*type != WSCONS_EVENT_KEY_DOWN ||
1337 (! MOD_ONESET(sc->id, MOD_COMMAND) &&
1338 ! MOD_ALLSET(sc->id, MOD_COMMAND1 | MOD_COMMAND2)))
1339 return (0);
1340
1341 switch (ksym) {
1342 #if defined(DDB) || defined(KGDB)
1343 case KS_Cmd_Debugger:
1344 if (sc->sc_isconsole) {
1345 #ifdef DDB
1346 console_debugger();
1347 #endif
1348 #ifdef KGDB
1349 kgdb_connect(1);
1350 #endif
1351 }
1352 /* discard this key (ddb discarded command modifiers) */
1353 *type = WSCONS_EVENT_KEY_UP;
1354 return (1);
1355 #endif
1356
1357 #if NWSDISPLAY > 0
1358 case KS_Cmd_Screen0:
1359 case KS_Cmd_Screen1:
1360 case KS_Cmd_Screen2:
1361 case KS_Cmd_Screen3:
1362 case KS_Cmd_Screen4:
1363 case KS_Cmd_Screen5:
1364 case KS_Cmd_Screen6:
1365 case KS_Cmd_Screen7:
1366 case KS_Cmd_Screen8:
1367 case KS_Cmd_Screen9:
1368 wsdisplay_switch(sc->sc_base.me_dispdv, ksym - KS_Cmd_Screen0, 0);
1369 return (1);
1370 case KS_Cmd_ResetEmul:
1371 wsdisplay_reset(sc->sc_base.me_dispdv, WSDISPLAY_RESETEMUL);
1372 return (1);
1373 case KS_Cmd_ResetClose:
1374 wsdisplay_reset(sc->sc_base.me_dispdv, WSDISPLAY_RESETCLOSE);
1375 return (1);
1376 case KS_Cmd_BacklightOn:
1377 case KS_Cmd_BacklightOff:
1378 case KS_Cmd_BacklightToggle:
1379 change_displayparam(sc, WSDISPLAYIO_PARAM_BACKLIGHT,
1380 ksym == KS_Cmd_BacklightOff ? -1 : 1,
1381 ksym == KS_Cmd_BacklightToggle ? 1 : 0);
1382 return (1);
1383 case KS_Cmd_BrightnessUp:
1384 case KS_Cmd_BrightnessDown:
1385 case KS_Cmd_BrightnessRotate:
1386 change_displayparam(sc, WSDISPLAYIO_PARAM_BRIGHTNESS,
1387 ksym == KS_Cmd_BrightnessDown ? -1 : 1,
1388 ksym == KS_Cmd_BrightnessRotate ? 1 : 0);
1389 return (1);
1390 case KS_Cmd_ContrastUp:
1391 case KS_Cmd_ContrastDown:
1392 case KS_Cmd_ContrastRotate:
1393 change_displayparam(sc, WSDISPLAYIO_PARAM_CONTRAST,
1394 ksym == KS_Cmd_ContrastDown ? -1 : 1,
1395 ksym == KS_Cmd_ContrastRotate ? 1 : 0);
1396 return (1);
1397 #endif
1398 }
1399 return (0);
1400 }
1401
1402 static int
1403 wskbd_translate(struct wskbd_internal *id, u_int type, int value)
1404 {
1405 struct wskbd_softc *sc = id->t_sc;
1406 keysym_t ksym, res, *group;
1407 struct wscons_keymap kpbuf, *kp;
1408 int iscommand = 0;
1409
1410 if (type == WSCONS_EVENT_ALL_KEYS_UP) {
1411 id->t_modifiers &= ~(MOD_SHIFT_L | MOD_SHIFT_R
1412 | MOD_CONTROL_L | MOD_CONTROL_R
1413 | MOD_META_L | MOD_META_R
1414 | MOD_MODESHIFT
1415 | MOD_COMMAND | MOD_COMMAND1 | MOD_COMMAND2);
1416 update_leds(id);
1417 return (0);
1418 }
1419
1420 if (sc != NULL) {
1421 if (value < 0 || value >= sc->sc_maplen) {
1422 #ifdef DEBUG
1423 printf("wskbd_translate: keycode %d out of range\n",
1424 value);
1425 #endif
1426 return (0);
1427 }
1428 kp = sc->sc_map + value;
1429 } else {
1430 kp = &kpbuf;
1431 wskbd_get_mapentry(id->t_keymap, value, kp);
1432 }
1433
1434 /* if this key has a command, process it first */
1435 if (sc != NULL && kp->command != KS_voidSymbol)
1436 iscommand = internal_command(sc, &type, kp->command,
1437 kp->group1[0]);
1438
1439 /* Now update modifiers */
1440 switch (kp->group1[0]) {
1441 case KS_Shift_L:
1442 update_modifier(id, type, 0, MOD_SHIFT_L);
1443 break;
1444
1445 case KS_Shift_R:
1446 update_modifier(id, type, 0, MOD_SHIFT_R);
1447 break;
1448
1449 case KS_Shift_Lock:
1450 update_modifier(id, type, 1, MOD_SHIFTLOCK);
1451 break;
1452
1453 case KS_Caps_Lock:
1454 update_modifier(id, type, 1, MOD_CAPSLOCK);
1455 break;
1456
1457 case KS_Control_L:
1458 update_modifier(id, type, 0, MOD_CONTROL_L);
1459 break;
1460
1461 case KS_Control_R:
1462 update_modifier(id, type, 0, MOD_CONTROL_R);
1463 break;
1464
1465 case KS_Alt_L:
1466 update_modifier(id, type, 0, MOD_META_L);
1467 break;
1468
1469 case KS_Alt_R:
1470 update_modifier(id, type, 0, MOD_META_R);
1471 break;
1472
1473 case KS_Mode_switch:
1474 update_modifier(id, type, 0, MOD_MODESHIFT);
1475 break;
1476
1477 case KS_Num_Lock:
1478 update_modifier(id, type, 1, MOD_NUMLOCK);
1479 break;
1480
1481 #if NWSDISPLAY > 0
1482 case KS_Hold_Screen:
1483 if (sc != NULL) {
1484 update_modifier(id, type, 1, MOD_HOLDSCREEN);
1485 wskbd_holdscreen(sc, id->t_modifiers & MOD_HOLDSCREEN);
1486 }
1487 break;
1488 #endif
1489 }
1490
1491 /* If this is a key release or we are in command mode, we are done */
1492 if (type != WSCONS_EVENT_KEY_DOWN || iscommand) {
1493 update_leds(id);
1494 return (0);
1495 }
1496
1497 /* Get the keysym */
1498 if (id->t_modifiers & MOD_MODESHIFT)
1499 group = & kp->group2[0];
1500 else
1501 group = & kp->group1[0];
1502
1503 if ((id->t_modifiers & MOD_NUMLOCK) != 0 &&
1504 KS_GROUP(group[1]) == KS_GROUP_Keypad) {
1505 if (MOD_ONESET(id, MOD_ANYSHIFT))
1506 ksym = group[0];
1507 else
1508 ksym = group[1];
1509 } else if (! MOD_ONESET(id, MOD_ANYSHIFT | MOD_CAPSLOCK)) {
1510 ksym = group[0];
1511 } else if (MOD_ONESET(id, MOD_CAPSLOCK)) {
1512 if (! MOD_ONESET(id, MOD_SHIFT_L | MOD_SHIFT_R))
1513 ksym = group[0];
1514 else
1515 ksym = group[1];
1516 if (ksym >= KS_a && ksym <= KS_z)
1517 ksym += KS_A - KS_a;
1518 else if (ksym >= KS_agrave && ksym <= KS_thorn &&
1519 ksym != KS_division)
1520 ksym += KS_Agrave - KS_agrave;
1521 } else if (MOD_ONESET(id, MOD_ANYSHIFT)) {
1522 ksym = group[1];
1523 } else {
1524 ksym = group[0];
1525 }
1526
1527 /* Process compose sequence and dead accents */
1528 res = KS_voidSymbol;
1529
1530 switch (KS_GROUP(ksym)) {
1531 case KS_GROUP_Ascii:
1532 case KS_GROUP_Keypad:
1533 case KS_GROUP_Function:
1534 res = ksym;
1535 break;
1536
1537 case KS_GROUP_Mod:
1538 if (ksym == KS_Multi_key) {
1539 update_modifier(id, 1, 0, MOD_COMPOSE);
1540 id->t_composelen = 2;
1541 }
1542 break;
1543
1544 case KS_GROUP_Dead:
1545 if (id->t_composelen == 0) {
1546 update_modifier(id, 1, 0, MOD_COMPOSE);
1547 id->t_composelen = 1;
1548 id->t_composebuf[0] = ksym;
1549 } else
1550 res = ksym;
1551 break;
1552 }
1553
1554 if (res == KS_voidSymbol) {
1555 update_leds(id);
1556 return (0);
1557 }
1558
1559 if (id->t_composelen > 0) {
1560 id->t_composebuf[2 - id->t_composelen] = res;
1561 if (--id->t_composelen == 0) {
1562 res = wskbd_compose_value(id->t_composebuf);
1563 update_modifier(id, 0, 0, MOD_COMPOSE);
1564 } else {
1565 return (0);
1566 }
1567 }
1568
1569 update_leds(id);
1570
1571 /* We are done, return the symbol */
1572 if (KS_GROUP(res) == KS_GROUP_Ascii) {
1573 if (MOD_ONESET(id, MOD_ANYCONTROL)) {
1574 if ((res >= KS_at && res <= KS_z) || res == KS_space)
1575 res = res & 0x1f;
1576 else if (res == KS_2)
1577 res = 0x00;
1578 else if (res >= KS_3 && res <= KS_7)
1579 res = KS_Escape + (res - KS_3);
1580 else if (res == KS_8)
1581 res = KS_Delete;
1582 }
1583 if (MOD_ONESET(id, MOD_ANYMETA)) {
1584 if (id->t_flags & WSKFL_METAESC) {
1585 id->t_symbols[0] = KS_Escape;
1586 id->t_symbols[1] = res;
1587 return (2);
1588 } else
1589 res |= 0x80;
1590 }
1591 }
1592
1593 id->t_symbols[0] = res;
1594 return (1);
1595 }
1596