wskbd.c revision 1.53 1 /* $NetBSD: wskbd.c,v 1.53 2001/10/27 00:35:48 augustss Exp $ */
2
3 /*
4 * Copyright (c) 1996, 1997 Christopher G. Demetriou. All rights reserved.
5 *
6 * Keysym translator:
7 * Contributed to The NetBSD Foundation by Juergen Hannken-Illjes.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. All advertising materials mentioning features or use of this software
18 * must display the following acknowledgement:
19 * This product includes software developed by Christopher G. Demetriou
20 * for the NetBSD Project.
21 * 4. The name of the author may not be used to endorse or promote products
22 * derived from this software without specific prior written permission
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
25 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
26 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
27 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
28 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
29 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
30 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
31 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
33 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 */
35
36 #include <sys/cdefs.h>
37 __KERNEL_RCSID(0, "$NetBSD: wskbd.c,v 1.53 2001/10/27 00:35:48 augustss Exp $");
38
39 /*
40 * Copyright (c) 1992, 1993
41 * The Regents of the University of California. All rights reserved.
42 *
43 * This software was developed by the Computer Systems Engineering group
44 * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
45 * contributed to Berkeley.
46 *
47 * All advertising materials mentioning features or use of this software
48 * must display the following acknowledgement:
49 * This product includes software developed by the University of
50 * California, Lawrence Berkeley Laboratory.
51 *
52 * Redistribution and use in source and binary forms, with or without
53 * modification, are permitted provided that the following conditions
54 * are met:
55 * 1. Redistributions of source code must retain the above copyright
56 * notice, this list of conditions and the following disclaimer.
57 * 2. Redistributions in binary form must reproduce the above copyright
58 * notice, this list of conditions and the following disclaimer in the
59 * documentation and/or other materials provided with the distribution.
60 * 3. All advertising materials mentioning features or use of this software
61 * must display the following acknowledgement:
62 * This product includes software developed by the University of
63 * California, Berkeley and its contributors.
64 * 4. Neither the name of the University nor the names of its contributors
65 * may be used to endorse or promote products derived from this software
66 * without specific prior written permission.
67 *
68 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
69 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
70 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
71 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
72 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
73 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
74 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
75 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
76 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
77 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
78 * SUCH DAMAGE.
79 *
80 * @(#)kbd.c 8.2 (Berkeley) 10/30/93
81 */
82
83 /*
84 * Keyboard driver (/dev/wskbd*). Translates incoming bytes to ASCII or
85 * to `wscons_events' and passes them up to the appropriate reader.
86 */
87
88 #include "opt_ddb.h"
89 #include "opt_kgdb.h"
90 #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 (sc->sc_isconsole && mux >= 0) {
362 /* printf(" (mux %d ignored for console)", mux); */
363 mux = -1;
364 }
365 if (mux >= 0)
366 printf(" mux %d", mux);
367 #else
368 if (sc->sc_base.me_dv.dv_cfdata->wskbddevcf_mux >= 0)
369 printf(" (mux ignored)");
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 memset(sc->id, 0, 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_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 sc->sc_base.me_dispdv = wsdisplay_set_console_kbd(&sc->sc_base);
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/wskbd 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 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_base.me_dv.dv_xname);
615 return;
616 }
617 ev->type = type;
618 ev->value = value;
619 microtime(&thistime);
620 TIMEVAL_TO_TIMESPEC(&thistime, &ev->time);
621 evar->put = put;
622 WSEVENT_WAKEUP(evar);
623 }
624
625 #ifdef WSDISPLAY_COMPAT_RAWKBD
626 void
627 wskbd_rawinput(struct device *dev, u_char *buf, int len)
628 {
629 #if NWSDISPLAY > 0
630 struct wskbd_softc *sc = (struct wskbd_softc *)dev;
631 int i;
632
633 if (sc->sc_base.me_dispdv != NULL)
634 for (i = 0; i < len; i++)
635 wsdisplay_kbdinput(sc->sc_base.me_dispdv, buf[i]);
636 /* this is KS_GROUP_Ascii */
637 #endif
638 }
639 #endif /* WSDISPLAY_COMPAT_RAWKBD */
640
641 #if NWSDISPLAY > 0
642 static void
643 wskbd_holdscreen(struct wskbd_softc *sc, int hold)
644 {
645 int new_state;
646
647 if (sc->sc_base.me_dispdv != NULL) {
648 wsdisplay_kbdholdscreen(sc->sc_base.me_dispdv, hold);
649 new_state = sc->sc_ledstate;
650 if (hold)
651 new_state |= WSKBD_LED_SCROLL;
652 else
653 new_state &= ~WSKBD_LED_SCROLL;
654 if (new_state != sc->sc_ledstate) {
655 (*sc->sc_accessops->set_leds)(sc->sc_accesscookie,
656 new_state);
657 sc->sc_ledstate = new_state;
658 }
659 }
660 }
661 #endif
662
663 static int
664 wskbd_enable(struct wskbd_softc *sc, int on)
665 {
666 int error;
667
668 #if 0
669 /* I don't understand the purpose of this code. And it seems to
670 * break things, so it's out. -- Lennart
671 */
672 if (!on && (!sc->sc_translating
673 #if NWSDISPLAY > 0
674 || sc->sc_base.me_dispdv
675 #endif
676 ))
677 return (EBUSY);
678 #endif
679 #if NWSDISPLAY > 0
680 if (sc->sc_base.me_dispdv != NULL)
681 return (0);
682 #endif
683
684 error = (*sc->sc_accessops->enable)(sc->sc_accesscookie, on);
685 DPRINTF(("wskbd_enable: sc=%p on=%d res=%d\n", sc, on, error));
686 return (error);
687 }
688
689 #if NWSMUX > 0
690 int
691 wskbd_mux_open(struct wsevsrc *me, struct wseventvar *evp)
692 {
693 struct wskbd_softc *sc = (struct wskbd_softc *)me;
694
695 if (sc->sc_dying)
696 return (EIO);
697
698 if (sc->sc_base.me_evp != NULL)
699 return (EBUSY);
700
701 return (wskbd_do_open(sc, evp));
702 }
703 #endif
704
705 int
706 wskbdopen(dev_t dev, int flags, int mode, struct proc *p)
707 {
708 struct wskbd_softc *sc;
709 struct wseventvar *evar;
710 int unit, error;
711
712 unit = minor(dev);
713 if (unit >= wskbd_cd.cd_ndevs || /* make sure it was attached */
714 (sc = wskbd_cd.cd_devs[unit]) == NULL)
715 return (ENXIO);
716
717 #if NWSMUX > 0
718 DPRINTF(("wskbdopen: %s mux=%p p=%p\n", sc->sc_base.me_dv.dv_xname,
719 sc->sc_base.me_parent, p));
720 #endif
721
722 if (sc->sc_dying)
723 return (EIO);
724
725 if ((flags & (FREAD | FWRITE)) == FWRITE)
726 /* Not opening for read, only ioctl is available. */
727 return (0);
728
729 #if NWSMUX > 0
730 if (sc->sc_base.me_parent != NULL)
731 /* Grab the keyboard out of the greedy hands of the mux. */
732 wsmux_detach_sc(&sc->sc_base);
733 #endif
734
735 if (sc->sc_base.me_evp != NULL)
736 return (EBUSY);
737
738 evar = &sc->sc_base.me_evar;
739 wsevent_init(evar);
740 evar->io = p;
741
742 error = wskbd_do_open(sc, evar);
743 if (error) {
744 DPRINTF(("wskbdopen: %s open failed\n",
745 sc->sc_base.me_dv.dv_xname));
746 sc->sc_base.me_evp = NULL;
747 wsevent_fini(evar);
748 }
749 return (error);
750 }
751
752 int
753 wskbd_do_open(struct wskbd_softc *sc, struct wseventvar *evp)
754 {
755 sc->sc_base.me_evp = evp;
756 sc->sc_translating = 0;
757
758 return (wskbd_enable(sc, 1));
759 }
760
761 int
762 wskbdclose(dev_t dev, int flags, int mode, struct proc *p)
763 {
764 struct wskbd_softc *sc =
765 (struct wskbd_softc *)wskbd_cd.cd_devs[minor(dev)];
766 struct wseventvar *evar = sc->sc_base.me_evp;
767
768 if (evar == NULL)
769 /* not open for read */
770 return (0);
771
772 sc->sc_base.me_evp = NULL;
773 sc->sc_translating = 1;
774 (void)wskbd_enable(sc, 0);
775 wsevent_fini(evar);
776
777 return (0);
778 }
779
780 #if NWSMUX > 0
781 int
782 wskbd_mux_close(struct wsevsrc *me)
783 {
784 struct wskbd_softc *sc = (struct wskbd_softc *)me;
785
786 sc->sc_base.me_evp = NULL;
787 sc->sc_translating = 1;
788 (void)wskbd_enable(sc, 0);
789
790 return (0);
791 }
792 #endif
793
794 int
795 wskbdread(dev_t dev, struct uio *uio, int flags)
796 {
797 struct wskbd_softc *sc = wskbd_cd.cd_devs[minor(dev)];
798 int error;
799
800 if (sc->sc_dying)
801 return (EIO);
802
803 #ifdef DIAGNOSTIC
804 if (sc->sc_base.me_evp == NULL) {
805 printf("wskbdread: evp == NULL\n");
806 return (EINVAL);
807 }
808 #endif
809
810 sc->sc_refcnt++;
811 error = wsevent_read(sc->sc_base.me_evp, uio, flags);
812 if (--sc->sc_refcnt < 0) {
813 wakeup(sc);
814 error = EIO;
815 }
816 return (error);
817 }
818
819 int
820 wskbdioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct proc *p)
821 {
822 return (wskbd_do_ioctl(wskbd_cd.cd_devs[minor(dev)], cmd, data, flag,p));
823 }
824
825 /* A wrapper around the ioctl() workhorse to make reference counting easy. */
826 int
827 wskbd_do_ioctl(struct device *dv, u_long cmd, caddr_t data, int flag,
828 struct proc *p)
829 {
830 struct wskbd_softc *sc = (struct wskbd_softc *)dv;
831 int error;
832
833 sc->sc_refcnt++;
834 error = wskbd_do_ioctl_sc(sc, cmd, data, flag, p);
835 if (--sc->sc_refcnt < 0)
836 wakeup(sc);
837 return (error);
838 }
839
840 int
841 wskbd_do_ioctl_sc(struct wskbd_softc *sc, u_long cmd, caddr_t data, int flag,
842 struct proc *p)
843 {
844 int error;
845
846 /*
847 * Try the generic ioctls that the wskbd interface supports.
848 */
849 switch (cmd) {
850 case FIONBIO: /* we will remove this someday (soon???) */
851 return (0);
852
853 case FIOASYNC:
854 if (sc->sc_base.me_evp == NULL)
855 return (EINVAL);
856 sc->sc_base.me_evp->async = *(int *)data != 0;
857 return (0);
858
859 case TIOCSPGRP:
860 if (sc->sc_base.me_evp == NULL)
861 return (EINVAL);
862 if (*(int *)data != sc->sc_base.me_evp->io->p_pgid)
863 return (EPERM);
864 return (0);
865 }
866
867 /*
868 * Try the keyboard driver for WSKBDIO ioctls. It returns -1
869 * if it didn't recognize the request.
870 */
871 error = wskbd_displayioctl(&sc->sc_base.me_dv, cmd, data, flag, p);
872 return (error != -1 ? error : ENOTTY);
873 }
874
875 /*
876 * WSKBDIO ioctls, handled in both emulation mode and in ``raw'' mode.
877 * Some of these have no real effect in raw mode, however.
878 */
879 static int
880 wskbd_displayioctl(struct device *dev, u_long cmd, caddr_t data, int flag,
881 struct proc *p)
882 {
883 struct wskbd_softc *sc = (struct wskbd_softc *)dev;
884 struct wskbd_bell_data *ubdp, *kbdp;
885 struct wskbd_keyrepeat_data *ukdp, *kkdp;
886 struct wskbd_map_data *umdp;
887 struct wskbd_mapdata md;
888 kbd_t enc;
889 void *buf;
890 int len, error;
891
892 switch (cmd) {
893 #define SETBELL(dstp, srcp, dfltp) \
894 do { \
895 (dstp)->pitch = ((srcp)->which & WSKBD_BELL_DOPITCH) ? \
896 (srcp)->pitch : (dfltp)->pitch; \
897 (dstp)->period = ((srcp)->which & WSKBD_BELL_DOPERIOD) ? \
898 (srcp)->period : (dfltp)->period; \
899 (dstp)->volume = ((srcp)->which & WSKBD_BELL_DOVOLUME) ? \
900 (srcp)->volume : (dfltp)->volume; \
901 (dstp)->which = WSKBD_BELL_DOALL; \
902 } while (0)
903
904 case WSKBDIO_BELL:
905 if ((flag & FWRITE) == 0)
906 return (EACCES);
907 return ((*sc->sc_accessops->ioctl)(sc->sc_accesscookie,
908 WSKBDIO_COMPLEXBELL, (caddr_t)&sc->sc_bell_data, flag, p));
909
910 case WSKBDIO_COMPLEXBELL:
911 if ((flag & FWRITE) == 0)
912 return (EACCES);
913 ubdp = (struct wskbd_bell_data *)data;
914 SETBELL(ubdp, ubdp, &sc->sc_bell_data);
915 return ((*sc->sc_accessops->ioctl)(sc->sc_accesscookie,
916 WSKBDIO_COMPLEXBELL, (caddr_t)ubdp, flag, p));
917
918 case WSKBDIO_SETBELL:
919 if ((flag & FWRITE) == 0)
920 return (EACCES);
921 kbdp = &sc->sc_bell_data;
922 setbell:
923 ubdp = (struct wskbd_bell_data *)data;
924 SETBELL(kbdp, ubdp, kbdp);
925 return (0);
926
927 case WSKBDIO_GETBELL:
928 kbdp = &sc->sc_bell_data;
929 getbell:
930 ubdp = (struct wskbd_bell_data *)data;
931 SETBELL(ubdp, kbdp, kbdp);
932 return (0);
933
934 case WSKBDIO_SETDEFAULTBELL:
935 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
936 return (error);
937 kbdp = &wskbd_default_bell_data;
938 goto setbell;
939
940
941 case WSKBDIO_GETDEFAULTBELL:
942 kbdp = &wskbd_default_bell_data;
943 goto getbell;
944
945 #undef SETBELL
946
947 #define SETKEYREPEAT(dstp, srcp, dfltp) \
948 do { \
949 (dstp)->del1 = ((srcp)->which & WSKBD_KEYREPEAT_DODEL1) ? \
950 (srcp)->del1 : (dfltp)->del1; \
951 (dstp)->delN = ((srcp)->which & WSKBD_KEYREPEAT_DODELN) ? \
952 (srcp)->delN : (dfltp)->delN; \
953 (dstp)->which = WSKBD_KEYREPEAT_DOALL; \
954 } while (0)
955
956 case WSKBDIO_SETKEYREPEAT:
957 if ((flag & FWRITE) == 0)
958 return (EACCES);
959 kkdp = &sc->sc_keyrepeat_data;
960 setkeyrepeat:
961 ukdp = (struct wskbd_keyrepeat_data *)data;
962 SETKEYREPEAT(kkdp, ukdp, kkdp);
963 return (0);
964
965 case WSKBDIO_GETKEYREPEAT:
966 kkdp = &sc->sc_keyrepeat_data;
967 getkeyrepeat:
968 ukdp = (struct wskbd_keyrepeat_data *)data;
969 SETKEYREPEAT(ukdp, kkdp, kkdp);
970 return (0);
971
972 case WSKBDIO_SETDEFAULTKEYREPEAT:
973 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
974 return (error);
975 kkdp = &wskbd_default_keyrepeat_data;
976 goto setkeyrepeat;
977
978
979 case WSKBDIO_GETDEFAULTKEYREPEAT:
980 kkdp = &wskbd_default_keyrepeat_data;
981 goto getkeyrepeat;
982
983 #undef SETKEYREPEAT
984
985 case WSKBDIO_SETMAP:
986 if ((flag & FWRITE) == 0)
987 return (EACCES);
988 umdp = (struct wskbd_map_data *)data;
989 if (umdp->maplen > WSKBDIO_MAXMAPLEN)
990 return (EINVAL);
991
992 len = umdp->maplen*sizeof(struct wscons_keymap);
993 buf = malloc(len, M_TEMP, M_WAITOK);
994 error = copyin(umdp->map, buf, len);
995 if (error == 0) {
996 wskbd_init_keymap(umdp->maplen,
997 &sc->sc_map, &sc->sc_maplen);
998 memcpy(sc->sc_map, buf, len);
999 /* drop the variant bits handled by the map */
1000 sc->sc_layout = KB_USER |
1001 (KB_VARIANT(sc->sc_layout) & KB_HANDLEDBYWSKBD);
1002 wskbd_update_layout(sc->id, sc->sc_layout);
1003 }
1004 free(buf, M_TEMP);
1005 return(error);
1006
1007 case WSKBDIO_GETMAP:
1008 umdp = (struct wskbd_map_data *)data;
1009 if (umdp->maplen > sc->sc_maplen)
1010 umdp->maplen = sc->sc_maplen;
1011 error = copyout(sc->sc_map, umdp->map,
1012 umdp->maplen*sizeof(struct wscons_keymap));
1013 return(error);
1014
1015 case WSKBDIO_GETENCODING:
1016 *((kbd_t *) data) = sc->sc_layout;
1017 return(0);
1018
1019 case WSKBDIO_SETENCODING:
1020 if ((flag & FWRITE) == 0)
1021 return (EACCES);
1022 enc = *((kbd_t *)data);
1023 if (KB_ENCODING(enc) == KB_USER) {
1024 /* user map must already be loaded */
1025 if (KB_ENCODING(sc->sc_layout) != KB_USER)
1026 return (EINVAL);
1027 /* map variants make no sense */
1028 if (KB_VARIANT(enc) & ~KB_HANDLEDBYWSKBD)
1029 return (EINVAL);
1030 } else {
1031 md = *(sc->id->t_keymap); /* structure assignment */
1032 md.layout = enc;
1033 error = wskbd_load_keymap(&md, &sc->sc_map,
1034 &sc->sc_maplen);
1035 if (error)
1036 return (error);
1037 }
1038 sc->sc_layout = enc;
1039 wskbd_update_layout(sc->id, enc);
1040 return (0);
1041 }
1042
1043 /*
1044 * Try the keyboard driver for WSKBDIO ioctls. It returns -1
1045 * if it didn't recognize the request, and in turn we return
1046 * -1 if we didn't recognize the request.
1047 */
1048 /* printf("kbdaccess\n"); */
1049 error = (*sc->sc_accessops->ioctl)(sc->sc_accesscookie, cmd, data,
1050 flag, p);
1051 #ifdef WSDISPLAY_COMPAT_RAWKBD
1052 if (!error && cmd == WSKBDIO_SETMODE && *(int *)data == WSKBD_RAW) {
1053 int s = spltty();
1054 sc->id->t_modifiers &= ~(MOD_SHIFT_L | MOD_SHIFT_R
1055 | MOD_CONTROL_L | MOD_CONTROL_R
1056 | MOD_META_L | MOD_META_R
1057 | MOD_COMMAND
1058 | MOD_COMMAND1 | MOD_COMMAND2);
1059 #if NWSDISPLAY > 0
1060 if (sc->sc_repeating) {
1061 sc->sc_repeating = 0;
1062 callout_stop(&sc->sc_repeat_ch);
1063 }
1064 #endif
1065 splx(s);
1066 }
1067 #endif
1068 return (error);
1069 }
1070
1071 int
1072 wskbdpoll(dev_t dev, int events, struct proc *p)
1073 {
1074 struct wskbd_softc *sc = wskbd_cd.cd_devs[minor(dev)];
1075
1076 if (sc->sc_base.me_evp == NULL)
1077 return (EINVAL);
1078 return (wsevent_poll(sc->sc_base.me_evp, events, p));
1079 }
1080
1081 #if NWSDISPLAY > 0
1082
1083 int
1084 wskbd_pickfree(void)
1085 {
1086 int i;
1087 struct wskbd_softc *sc;
1088
1089 for (i = 0; i < wskbd_cd.cd_ndevs; i++) {
1090 if ((sc = wskbd_cd.cd_devs[i]) == NULL)
1091 continue;
1092 if (sc->sc_base.me_dispdv == NULL)
1093 return (i);
1094 }
1095 return (-1);
1096 }
1097
1098 struct wsevsrc *
1099 wskbd_set_console_display(struct device *displaydv, struct wsevsrc *me)
1100 {
1101 struct wskbd_softc *sc = wskbd_console_device;
1102
1103 if (sc == NULL)
1104 return (NULL);
1105 sc->sc_base.me_dispdv = displaydv;
1106 #if NWSMUX > 0
1107 (void)wsmux_attach_sc((struct wsmux_softc *)me, &sc->sc_base);
1108 #endif
1109 return (&sc->sc_base);
1110 }
1111
1112 int
1113 wskbd_set_display(struct device *dv, struct wsevsrc *me)
1114 {
1115 struct wskbd_softc *sc = (struct wskbd_softc *)dv;
1116 struct device *displaydv = me != NULL ? me->me_dispdv : NULL;
1117 struct device *odisplaydv;
1118 int error;
1119
1120 DPRINTF(("wskbd_set_display: %s me=%p disp=%p odisp=%p cons=%d\n",
1121 dv->dv_xname, me, sc->sc_base.me_dispdv, displaydv,
1122 sc->sc_isconsole));
1123
1124 if (sc->sc_isconsole)
1125 return (EBUSY);
1126
1127 if (displaydv != NULL) {
1128 if (sc->sc_base.me_dispdv != NULL)
1129 return (EBUSY);
1130 } else {
1131 if (sc->sc_base.me_dispdv == NULL)
1132 return (ENXIO);
1133 }
1134
1135 odisplaydv = sc->sc_base.me_dispdv;
1136 sc->sc_base.me_dispdv = NULL;
1137 error = wskbd_enable(sc, displaydv != NULL);
1138 sc->sc_base.me_dispdv = displaydv;
1139 if (error) {
1140 sc->sc_base.me_dispdv = odisplaydv;
1141 return (error);
1142 }
1143
1144 if (displaydv)
1145 printf("%s: connecting to %s\n",
1146 sc->sc_base.me_dv.dv_xname, displaydv->dv_xname);
1147 else
1148 printf("%s: disconnecting from %s\n",
1149 sc->sc_base.me_dv.dv_xname, odisplaydv->dv_xname);
1150
1151 return (0);
1152 }
1153
1154 #endif /* NWSDISPLAY > 0 */
1155
1156 #if NWSMUX > 0
1157 int
1158 wskbd_add_mux(int unit, struct wsmux_softc *muxsc)
1159 {
1160 struct wskbd_softc *sc;
1161
1162 if (unit < 0 || unit >= wskbd_cd.cd_ndevs ||
1163 (sc = wskbd_cd.cd_devs[unit]) == NULL)
1164 return (ENXIO);
1165
1166 if (sc->sc_base.me_parent != NULL || sc->sc_base.me_evp != NULL)
1167 return (EBUSY);
1168
1169 return (wsmux_attach_sc(muxsc, &sc->sc_base));
1170 }
1171 #endif
1172
1173 /*
1174 * Console interface.
1175 */
1176 int
1177 wskbd_cngetc(dev_t dev)
1178 {
1179 static int num = 0;
1180 static int pos;
1181 u_int type;
1182 int data;
1183 keysym_t ks;
1184
1185 if (!wskbd_console_initted)
1186 return 0;
1187
1188 if (wskbd_console_device != NULL &&
1189 !wskbd_console_device->sc_translating)
1190 return 0;
1191
1192 for(;;) {
1193 if (num-- > 0) {
1194 ks = wskbd_console_data.t_symbols[pos++];
1195 if (KS_GROUP(ks) == KS_GROUP_Ascii)
1196 return (KS_VALUE(ks));
1197 } else {
1198 (*wskbd_console_data.t_consops->getc)
1199 (wskbd_console_data.t_consaccesscookie,
1200 &type, &data);
1201 num = wskbd_translate(&wskbd_console_data, type, data);
1202 pos = 0;
1203 }
1204 }
1205 }
1206
1207 void
1208 wskbd_cnpollc(dev_t dev, 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_t dev, u_int pitch, u_int period, u_int volume)
1224 {
1225
1226 if (!wskbd_console_initted)
1227 return;
1228
1229 if (wskbd_console_data.t_consops->bell != NULL)
1230 (*wskbd_console_data.t_consops->bell)
1231 (wskbd_console_data.t_consaccesscookie, pitch, period,
1232 volume);
1233 }
1234
1235 static inline void
1236 update_leds(struct wskbd_internal *id)
1237 {
1238 int new_state;
1239
1240 new_state = 0;
1241 if (id->t_modifiers & (MOD_SHIFTLOCK | MOD_CAPSLOCK))
1242 new_state |= WSKBD_LED_CAPS;
1243 if (id->t_modifiers & MOD_NUMLOCK)
1244 new_state |= WSKBD_LED_NUM;
1245 if (id->t_modifiers & MOD_COMPOSE)
1246 new_state |= WSKBD_LED_COMPOSE;
1247 if (id->t_modifiers & MOD_HOLDSCREEN)
1248 new_state |= WSKBD_LED_SCROLL;
1249
1250 if (id->t_sc && new_state != id->t_sc->sc_ledstate) {
1251 (*id->t_sc->sc_accessops->set_leds)
1252 (id->t_sc->sc_accesscookie, new_state);
1253 id->t_sc->sc_ledstate = new_state;
1254 }
1255 }
1256
1257 static inline void
1258 update_modifier(struct wskbd_internal *id, u_int type, int toggle, int mask)
1259 {
1260 if (toggle) {
1261 if (type == WSCONS_EVENT_KEY_DOWN)
1262 id->t_modifiers ^= mask;
1263 } else {
1264 if (type == WSCONS_EVENT_KEY_DOWN)
1265 id->t_modifiers |= mask;
1266 else
1267 id->t_modifiers &= ~mask;
1268 }
1269 }
1270
1271 #if NWSDISPLAY > 0
1272 static void
1273 change_displayparam(struct wskbd_softc *sc, int param, int updown,
1274 int wraparound)
1275 {
1276 int res;
1277 struct wsdisplay_param dp;
1278
1279 if (sc->sc_base.me_dispdv == NULL)
1280 return;
1281
1282 dp.param = param;
1283 res = wsdisplay_param(sc->sc_base.me_dispdv, WSDISPLAYIO_GETPARAM, &dp);
1284
1285 if (res == EINVAL)
1286 return; /* no such parameter */
1287
1288 dp.curval += updown;
1289 if (dp.max < dp.curval)
1290 dp.curval = wraparound ? dp.min : dp.max;
1291 else
1292 if (dp.curval < dp.min)
1293 dp.curval = wraparound ? dp.max : dp.min;
1294 wsdisplay_param(sc->sc_base.me_dispdv, WSDISPLAYIO_SETPARAM, &dp);
1295 }
1296 #endif
1297
1298 static int
1299 internal_command(struct wskbd_softc *sc, u_int *type, keysym_t ksym,
1300 keysym_t ksym2)
1301 {
1302 switch (ksym) {
1303 case KS_Cmd:
1304 update_modifier(sc->id, *type, 0, MOD_COMMAND);
1305 ksym = ksym2;
1306 break;
1307
1308 case KS_Cmd1:
1309 update_modifier(sc->id, *type, 0, MOD_COMMAND1);
1310 break;
1311
1312 case KS_Cmd2:
1313 update_modifier(sc->id, *type, 0, MOD_COMMAND2);
1314 break;
1315 }
1316
1317 if (*type != WSCONS_EVENT_KEY_DOWN ||
1318 (! MOD_ONESET(sc->id, MOD_COMMAND) &&
1319 ! MOD_ALLSET(sc->id, MOD_COMMAND1 | MOD_COMMAND2)))
1320 return (0);
1321
1322 switch (ksym) {
1323 #if defined(DDB) || defined(KGDB)
1324 case KS_Cmd_Debugger:
1325 if (sc->sc_isconsole) {
1326 #ifdef DDB
1327 console_debugger();
1328 #endif
1329 #ifdef KGDB
1330 kgdb_connect(1);
1331 #endif
1332 }
1333 /* discard this key (ddb discarded command modifiers) */
1334 *type = WSCONS_EVENT_KEY_UP;
1335 return (1);
1336 #endif
1337
1338 #if NWSDISPLAY > 0
1339 case KS_Cmd_Screen0:
1340 case KS_Cmd_Screen1:
1341 case KS_Cmd_Screen2:
1342 case KS_Cmd_Screen3:
1343 case KS_Cmd_Screen4:
1344 case KS_Cmd_Screen5:
1345 case KS_Cmd_Screen6:
1346 case KS_Cmd_Screen7:
1347 case KS_Cmd_Screen8:
1348 case KS_Cmd_Screen9:
1349 wsdisplay_switch(sc->sc_base.me_dispdv, ksym - KS_Cmd_Screen0, 0);
1350 return (1);
1351 case KS_Cmd_ResetEmul:
1352 wsdisplay_reset(sc->sc_base.me_dispdv, WSDISPLAY_RESETEMUL);
1353 return (1);
1354 case KS_Cmd_ResetClose:
1355 wsdisplay_reset(sc->sc_base.me_dispdv, WSDISPLAY_RESETCLOSE);
1356 return (1);
1357 case KS_Cmd_BacklightOn:
1358 case KS_Cmd_BacklightOff:
1359 case KS_Cmd_BacklightToggle:
1360 change_displayparam(sc, WSDISPLAYIO_PARAM_BACKLIGHT,
1361 ksym == KS_Cmd_BacklightOff ? -1 : 1,
1362 ksym == KS_Cmd_BacklightToggle ? 1 : 0);
1363 return (1);
1364 case KS_Cmd_BrightnessUp:
1365 case KS_Cmd_BrightnessDown:
1366 case KS_Cmd_BrightnessRotate:
1367 change_displayparam(sc, WSDISPLAYIO_PARAM_BRIGHTNESS,
1368 ksym == KS_Cmd_BrightnessDown ? -1 : 1,
1369 ksym == KS_Cmd_BrightnessRotate ? 1 : 0);
1370 return (1);
1371 case KS_Cmd_ContrastUp:
1372 case KS_Cmd_ContrastDown:
1373 case KS_Cmd_ContrastRotate:
1374 change_displayparam(sc, WSDISPLAYIO_PARAM_CONTRAST,
1375 ksym == KS_Cmd_ContrastDown ? -1 : 1,
1376 ksym == KS_Cmd_ContrastRotate ? 1 : 0);
1377 return (1);
1378 #endif
1379 }
1380 return (0);
1381 }
1382
1383 static int
1384 wskbd_translate(struct wskbd_internal *id, u_int type, int value)
1385 {
1386 struct wskbd_softc *sc = id->t_sc;
1387 keysym_t ksym, res, *group;
1388 struct wscons_keymap kpbuf, *kp;
1389 int iscommand = 0;
1390
1391 if (type == WSCONS_EVENT_ALL_KEYS_UP) {
1392 id->t_modifiers &= ~(MOD_SHIFT_L | MOD_SHIFT_R
1393 | MOD_CONTROL_L | MOD_CONTROL_R
1394 | MOD_META_L | MOD_META_R
1395 | MOD_MODESHIFT
1396 | MOD_COMMAND | MOD_COMMAND1 | MOD_COMMAND2);
1397 update_leds(id);
1398 return (0);
1399 }
1400
1401 if (sc != NULL) {
1402 if (value < 0 || value >= sc->sc_maplen) {
1403 #ifdef DEBUG
1404 printf("wskbd_translate: keycode %d out of range\n",
1405 value);
1406 #endif
1407 return (0);
1408 }
1409 kp = sc->sc_map + value;
1410 } else {
1411 kp = &kpbuf;
1412 wskbd_get_mapentry(id->t_keymap, value, kp);
1413 }
1414
1415 /* if this key has a command, process it first */
1416 if (sc != NULL && kp->command != KS_voidSymbol)
1417 iscommand = internal_command(sc, &type, kp->command,
1418 kp->group1[0]);
1419
1420 /* Now update modifiers */
1421 switch (kp->group1[0]) {
1422 case KS_Shift_L:
1423 update_modifier(id, type, 0, MOD_SHIFT_L);
1424 break;
1425
1426 case KS_Shift_R:
1427 update_modifier(id, type, 0, MOD_SHIFT_R);
1428 break;
1429
1430 case KS_Shift_Lock:
1431 update_modifier(id, type, 1, MOD_SHIFTLOCK);
1432 break;
1433
1434 case KS_Caps_Lock:
1435 update_modifier(id, type, 1, MOD_CAPSLOCK);
1436 break;
1437
1438 case KS_Control_L:
1439 update_modifier(id, type, 0, MOD_CONTROL_L);
1440 break;
1441
1442 case KS_Control_R:
1443 update_modifier(id, type, 0, MOD_CONTROL_R);
1444 break;
1445
1446 case KS_Alt_L:
1447 update_modifier(id, type, 0, MOD_META_L);
1448 break;
1449
1450 case KS_Alt_R:
1451 update_modifier(id, type, 0, MOD_META_R);
1452 break;
1453
1454 case KS_Mode_switch:
1455 update_modifier(id, type, 0, MOD_MODESHIFT);
1456 break;
1457
1458 case KS_Num_Lock:
1459 update_modifier(id, type, 1, MOD_NUMLOCK);
1460 break;
1461
1462 #if NWSDISPLAY > 0
1463 case KS_Hold_Screen:
1464 if (sc != NULL) {
1465 update_modifier(id, type, 1, MOD_HOLDSCREEN);
1466 wskbd_holdscreen(sc, id->t_modifiers & MOD_HOLDSCREEN);
1467 }
1468 break;
1469 #endif
1470 }
1471
1472 /* If this is a key release or we are in command mode, we are done */
1473 if (type != WSCONS_EVENT_KEY_DOWN || iscommand) {
1474 update_leds(id);
1475 return (0);
1476 }
1477
1478 /* Get the keysym */
1479 if (id->t_modifiers & MOD_MODESHIFT)
1480 group = & kp->group2[0];
1481 else
1482 group = & kp->group1[0];
1483
1484 if ((id->t_modifiers & MOD_NUMLOCK) != 0 &&
1485 KS_GROUP(group[1]) == KS_GROUP_Keypad) {
1486 if (MOD_ONESET(id, MOD_ANYSHIFT))
1487 ksym = group[0];
1488 else
1489 ksym = group[1];
1490 } else if (! MOD_ONESET(id, MOD_ANYSHIFT | MOD_CAPSLOCK)) {
1491 ksym = group[0];
1492 } else if (MOD_ONESET(id, MOD_CAPSLOCK)) {
1493 if (! MOD_ONESET(id, MOD_SHIFT_L | MOD_SHIFT_R))
1494 ksym = group[0];
1495 else
1496 ksym = group[1];
1497 if (ksym >= KS_a && ksym <= KS_z)
1498 ksym += KS_A - KS_a;
1499 else if (ksym >= KS_agrave && ksym <= KS_thorn &&
1500 ksym != KS_division)
1501 ksym += KS_Agrave - KS_agrave;
1502 } else if (MOD_ONESET(id, MOD_ANYSHIFT)) {
1503 ksym = group[1];
1504 } else {
1505 ksym = group[0];
1506 }
1507
1508 /* Process compose sequence and dead accents */
1509 res = KS_voidSymbol;
1510
1511 switch (KS_GROUP(ksym)) {
1512 case KS_GROUP_Ascii:
1513 case KS_GROUP_Keypad:
1514 case KS_GROUP_Function:
1515 res = ksym;
1516 break;
1517
1518 case KS_GROUP_Mod:
1519 if (ksym == KS_Multi_key) {
1520 update_modifier(id, 1, 0, MOD_COMPOSE);
1521 id->t_composelen = 2;
1522 }
1523 break;
1524
1525 case KS_GROUP_Dead:
1526 if (id->t_composelen == 0) {
1527 update_modifier(id, 1, 0, MOD_COMPOSE);
1528 id->t_composelen = 1;
1529 id->t_composebuf[0] = ksym;
1530 } else
1531 res = ksym;
1532 break;
1533 }
1534
1535 if (res == KS_voidSymbol) {
1536 update_leds(id);
1537 return (0);
1538 }
1539
1540 if (id->t_composelen > 0) {
1541 id->t_composebuf[2 - id->t_composelen] = res;
1542 if (--id->t_composelen == 0) {
1543 res = wskbd_compose_value(id->t_composebuf);
1544 update_modifier(id, 0, 0, MOD_COMPOSE);
1545 } else {
1546 return (0);
1547 }
1548 }
1549
1550 update_leds(id);
1551
1552 /* We are done, return the symbol */
1553 if (KS_GROUP(res) == KS_GROUP_Ascii) {
1554 if (MOD_ONESET(id, MOD_ANYCONTROL)) {
1555 if ((res >= KS_at && res <= KS_z) || res == KS_space)
1556 res = res & 0x1f;
1557 else if (res == KS_2)
1558 res = 0x00;
1559 else if (res >= KS_3 && res <= KS_7)
1560 res = KS_Escape + (res - KS_3);
1561 else if (res == KS_8)
1562 res = KS_Delete;
1563 }
1564 if (MOD_ONESET(id, MOD_ANYMETA)) {
1565 if (id->t_flags & WSKFL_METAESC) {
1566 id->t_symbols[0] = KS_Escape;
1567 id->t_symbols[1] = res;
1568 return (2);
1569 } else
1570 res |= 0x80;
1571 }
1572 }
1573
1574 id->t_symbols[0] = res;
1575 return (1);
1576 }
1577