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