2 * Copyright (c) 1999 Kazutaka YOKOTA <yokota@zodiac.mech.utsunomiya-u.ac.jp>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer as
10 * the first lines of this file unmodified.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 * $FreeBSD: src/sys/dev/kbd/kbd.c,v 1.17.2.2 2001/07/30 16:46:43 yokota Exp $
27 * $DragonFly: src/sys/dev/misc/kbd/kbd.c,v 1.3 2003/06/23 17:55:31 dillon Exp $
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/kernel.h>
35 #include <sys/malloc.h>
40 #include <sys/vnode.h>
43 #include <machine/console.h>
45 #include <dev/kbd/kbdreg.h>
47 #define KBD_INDEX(dev) minor(dev)
49 typedef struct genkbd_softc {
50 int gkb_flags; /* flag/status bits */
51 #define KB_ASLEEP (1 << 0)
52 struct clist gkb_q; /* input queue */
53 struct selinfo gkb_rsel;
56 static SLIST_HEAD(, keyboard_driver) keyboard_drivers =
57 SLIST_HEAD_INITIALIZER(keyboard_drivers);
62 * We need at least one entry each in order to initialize a keyboard
63 * for the kernel console. The arrays will be increased dynamically
67 static int keyboards = 1;
68 static keyboard_t *kbd_ini;
69 static keyboard_t **keyboard = &kbd_ini;
70 static keyboard_switch_t *kbdsw_ini;
71 keyboard_switch_t **kbdsw = &kbdsw_ini;
76 kbd_realloc_array(void)
79 keyboard_switch_t **new_kbdsw;
84 newsize = ((keyboards + ARRAY_DELTA)/ARRAY_DELTA)*ARRAY_DELTA;
85 new_kbd = malloc(sizeof(*new_kbd)*newsize, M_DEVBUF, M_NOWAIT);
86 if (new_kbd == NULL) {
90 new_kbdsw = malloc(sizeof(*new_kbdsw)*newsize, M_DEVBUF, M_NOWAIT);
91 if (new_kbdsw == NULL) {
92 free(new_kbd, M_DEVBUF);
96 bzero(new_kbd, sizeof(*new_kbd)*newsize);
97 bzero(new_kbdsw, sizeof(*new_kbdsw)*newsize);
98 bcopy(keyboard, new_kbd, sizeof(*keyboard)*keyboards);
99 bcopy(kbdsw, new_kbdsw, sizeof(*kbdsw)*keyboards);
101 free(keyboard, M_DEVBUF);
102 free(kbdsw, M_DEVBUF);
110 printf("kbd: new array size %d\n", keyboards);
116 * Low-level keyboard driver functions
117 * Keyboard subdrivers, such as the AT keyboard driver and the USB keyboard
118 * driver, call these functions to initialize the keyboard_t structure
119 * and register it to the virtual keyboard driver `kbd'.
122 /* initialize the keyboard_t structure */
124 kbd_init_struct(keyboard_t *kbd, char *name, int type, int unit, int config,
125 int port, int port_size)
127 kbd->kb_flags = KB_NO_DEVICE; /* device has not been found */
131 kbd->kb_config = config & ~KB_CONF_PROBE_ONLY;
132 kbd->kb_led = 0; /* unknown */
133 kbd->kb_io_base = port;
134 kbd->kb_io_size = port_size;
136 kbd->kb_keymap = NULL;
137 kbd->kb_accentmap = NULL;
138 kbd->kb_fkeytab = NULL;
139 kbd->kb_fkeytab_size = 0;
140 kbd->kb_delay1 = KB_DELAY1; /* these values are advisory only */
141 kbd->kb_delay2 = KB_DELAY2;
143 bzero(kbd->kb_lastact, sizeof(kbd->kb_lastact));
147 kbd_set_maps(keyboard_t *kbd, keymap_t *keymap, accentmap_t *accmap,
148 fkeytab_t *fkeymap, int fkeymap_size)
150 kbd->kb_keymap = keymap;
151 kbd->kb_accentmap = accmap;
152 kbd->kb_fkeytab = fkeymap;
153 kbd->kb_fkeytab_size = fkeymap_size;
156 /* declare a new keyboard driver */
158 kbd_add_driver(keyboard_driver_t *driver)
160 if (SLIST_NEXT(driver, link))
162 SLIST_INSERT_HEAD(&keyboard_drivers, driver, link);
167 kbd_delete_driver(keyboard_driver_t *driver)
169 SLIST_REMOVE(&keyboard_drivers, driver, keyboard_driver, link);
170 SLIST_NEXT(driver, link) = NULL;
174 /* register a keyboard and associate it with a function table */
176 kbd_register(keyboard_t *kbd)
178 const keyboard_driver_t **list;
179 const keyboard_driver_t *p;
182 for (index = 0; index < keyboards; ++index) {
183 if (keyboard[index] == NULL)
186 if (index >= keyboards) {
187 if (kbd_realloc_array())
191 kbd->kb_index = index;
194 kbd->kb_active = 0; /* disabled until someone calls kbd_enable() */
195 kbd->kb_token = NULL;
196 kbd->kb_callback.kc_func = NULL;
197 kbd->kb_callback.kc_arg = NULL;
199 SLIST_FOREACH(p, &keyboard_drivers, link) {
200 if (strcmp(p->name, kbd->kb_name) == 0) {
201 keyboard[index] = kbd;
202 kbdsw[index] = p->kbdsw;
206 list = (const keyboard_driver_t **)kbddriver_set.ls_items;
207 while ((p = *list++) != NULL) {
208 if (strcmp(p->name, kbd->kb_name) == 0) {
209 keyboard[index] = kbd;
210 kbdsw[index] = p->kbdsw;
219 kbd_unregister(keyboard_t *kbd)
224 if ((kbd->kb_index < 0) || (kbd->kb_index >= keyboards))
226 if (keyboard[kbd->kb_index] != kbd)
230 if (KBD_IS_BUSY(kbd)) {
231 error = (*kbd->kb_callback.kc_func)(kbd, KBDIO_UNLOADING,
232 kbd->kb_callback.kc_arg);
237 if (KBD_IS_BUSY(kbd)) {
243 keyboard[kbd->kb_index] = NULL;
244 kbdsw[kbd->kb_index] = NULL;
250 /* find a funciton table by the driver name */
252 *kbd_get_switch(char *driver)
254 const keyboard_driver_t **list;
255 const keyboard_driver_t *p;
257 SLIST_FOREACH(p, &keyboard_drivers, link) {
258 if (strcmp(p->name, driver) == 0)
261 list = (const keyboard_driver_t **)kbddriver_set.ls_items;
262 while ((p = *list++) != NULL) {
263 if (strcmp(p->name, driver) == 0)
271 * Keyboard client functions
272 * Keyboard clients, such as the console driver `syscons' and the keyboard
273 * cdev driver, use these functions to claim and release a keyboard for
277 /* find the keyboard specified by a driver name and a unit number */
279 kbd_find_keyboard(char *driver, int unit)
283 for (i = 0; i < keyboards; ++i) {
284 if (keyboard[i] == NULL)
286 if (!KBD_IS_VALID(keyboard[i]))
288 if (strcmp("*", driver) && strcmp(keyboard[i]->kb_name, driver))
290 if ((unit != -1) && (keyboard[i]->kb_unit != unit))
297 /* allocate a keyboard */
299 kbd_allocate(char *driver, int unit, void *id, kbd_callback_func_t *func,
309 index = kbd_find_keyboard(driver, unit);
311 if (KBD_IS_BUSY(keyboard[index])) {
315 keyboard[index]->kb_token = id;
316 KBD_BUSY(keyboard[index]);
317 keyboard[index]->kb_callback.kc_func = func;
318 keyboard[index]->kb_callback.kc_arg = arg;
319 (*kbdsw[index]->clear_state)(keyboard[index]);
326 kbd_release(keyboard_t *kbd, void *id)
332 if (!KBD_IS_VALID(kbd) || !KBD_IS_BUSY(kbd)) {
334 } else if (kbd->kb_token != id) {
337 kbd->kb_token = NULL;
339 kbd->kb_callback.kc_func = NULL;
340 kbd->kb_callback.kc_arg = NULL;
341 (*kbdsw[kbd->kb_index]->clear_state)(kbd);
349 kbd_change_callback(keyboard_t *kbd, void *id, kbd_callback_func_t *func,
356 if (!KBD_IS_VALID(kbd) || !KBD_IS_BUSY(kbd)) {
358 } else if (kbd->kb_token != id) {
360 } else if (func == NULL) {
363 kbd->kb_callback.kc_func = func;
364 kbd->kb_callback.kc_arg = arg;
371 /* get a keyboard structure */
373 *kbd_get_keyboard(int index)
375 if ((index < 0) || (index >= keyboards))
377 if (keyboard[index] == NULL)
379 if (!KBD_IS_VALID(keyboard[index]))
381 return keyboard[index];
385 * The back door for the console driver; configure keyboards
386 * This function is for the kernel console to initialize keyboards
387 * at very early stage.
391 kbd_configure(int flags)
393 const keyboard_driver_t **list;
394 const keyboard_driver_t *p;
396 SLIST_FOREACH(p, &keyboard_drivers, link) {
397 if (p->configure != NULL)
398 (*p->configure)(flags);
400 list = (const keyboard_driver_t **)kbddriver_set.ls_items;
401 while ((p = *list++) != NULL) {
402 if (p->configure != NULL)
403 (*p->configure)(flags);
409 #ifdef KBD_INSTALL_CDEV
412 * Virtual keyboard cdev driver functions
413 * The virtual keyboard driver dispatches driver functions to
414 * appropriate subdrivers.
417 #define KBD_UNIT(dev) minor(dev)
419 static d_open_t genkbdopen;
420 static d_close_t genkbdclose;
421 static d_read_t genkbdread;
422 static d_write_t genkbdwrite;
423 static d_ioctl_t genkbdioctl;
424 static d_poll_t genkbdpoll;
426 #define CDEV_MAJOR 112
428 static struct cdevsw kbd_cdevsw = {
429 /* open */ genkbdopen,
430 /* close */ genkbdclose,
431 /* read */ genkbdread,
432 /* write */ genkbdwrite,
433 /* ioctl */ genkbdioctl,
434 /* poll */ genkbdpoll,
436 /* strategy */ nostrategy,
438 /* maj */ CDEV_MAJOR,
446 kbd_attach(keyboard_t *kbd)
450 if (kbd->kb_index >= keyboards)
452 if (keyboard[kbd->kb_index] != kbd)
455 dev = make_dev(&kbd_cdevsw, kbd->kb_index, UID_ROOT, GID_WHEEL, 0600,
456 "kbd%r", kbd->kb_index);
457 if (dev->si_drv1 == NULL)
458 dev->si_drv1 = malloc(sizeof(genkbd_softc_t), M_DEVBUF,
460 bzero(dev->si_drv1, sizeof(genkbd_softc_t));
462 printf("kbd%d at %s%d\n", kbd->kb_index, kbd->kb_name, kbd->kb_unit);
467 kbd_detach(keyboard_t *kbd)
471 if (kbd->kb_index >= keyboards)
473 if (keyboard[kbd->kb_index] != kbd)
476 dev = makedev(kbd_cdevsw.d_maj, kbd->kb_index);
478 free(dev->si_drv1, M_DEVBUF);
485 * Generic keyboard cdev driver functions
486 * Keyboard subdrivers may call these functions to implement common
491 #define KB_BUFSIZE 64
493 static kbd_callback_func_t genkbd_event;
496 genkbdopen(dev_t dev, int mode, int flag, d_thread_t *td)
505 kbd = kbd_get_keyboard(KBD_INDEX(dev));
506 if ((sc == NULL) || (kbd == NULL) || !KBD_IS_VALID(kbd)) {
510 i = kbd_allocate(kbd->kb_name, kbd->kb_unit, sc,
511 genkbd_event, (void *)sc);
516 /* assert(i == kbd->kb_index) */
517 /* assert(kbd == kbd_get_keyboard(i)) */
520 * NOTE: even when we have successfully claimed a keyboard,
521 * the device may still be missing (!KBD_HAS_DEVICE(kbd)).
525 bzero(&sc->gkb_q, sizeof(sc->gkb_q));
527 clist_alloc_cblocks(&sc->gkb_q, KB_QSIZE, KB_QSIZE/2); /* XXX */
528 sc->gkb_rsel.si_flags = 0;
529 sc->gkb_rsel.si_pid = 0;
536 genkbdclose(dev_t dev, int mode, int flag, d_thread_t *td)
543 * NOTE: the device may have already become invalid.
544 * kbd == NULL || !KBD_IS_VALID(kbd)
548 kbd = kbd_get_keyboard(KBD_INDEX(dev));
549 if ((sc == NULL) || (kbd == NULL) || !KBD_IS_VALID(kbd)) {
550 /* XXX: we shall be forgiving and don't report error... */
552 kbd_release(kbd, (void *)sc);
554 clist_free_cblocks(&sc->gkb_q);
562 genkbdread(dev_t dev, struct uio *uio, int flag)
566 u_char buffer[KB_BUFSIZE];
574 kbd = kbd_get_keyboard(KBD_INDEX(dev));
575 if ((sc == NULL) || (kbd == NULL) || !KBD_IS_VALID(kbd)) {
579 while (sc->gkb_q.c_cc == 0) {
580 if (flag & IO_NDELAY) {
584 sc->gkb_flags |= KB_ASLEEP;
585 error = tsleep((caddr_t)sc, PZERO | PCATCH, "kbdrea", 0);
586 kbd = kbd_get_keyboard(KBD_INDEX(dev));
587 if ((kbd == NULL) || !KBD_IS_VALID(kbd)) {
589 return ENXIO; /* our keyboard has gone... */
592 sc->gkb_flags &= ~KB_ASLEEP;
599 /* copy as much input as possible */
601 while (uio->uio_resid > 0) {
602 len = imin(uio->uio_resid, sizeof(buffer));
603 len = q_to_b(&sc->gkb_q, buffer, len);
606 error = uiomove(buffer, len, uio);
615 genkbdwrite(dev_t dev, struct uio *uio, int flag)
619 kbd = kbd_get_keyboard(KBD_INDEX(dev));
620 if ((kbd == NULL) || !KBD_IS_VALID(kbd))
626 genkbdioctl(dev_t dev, u_long cmd, caddr_t arg, int flag, d_thread_t *td)
631 kbd = kbd_get_keyboard(KBD_INDEX(dev));
632 if ((kbd == NULL) || !KBD_IS_VALID(kbd))
634 error = (*kbdsw[kbd->kb_index]->ioctl)(kbd, cmd, arg);
635 if (error == ENOIOCTL)
641 genkbdpoll(dev_t dev, int events, d_thread_t *td)
651 kbd = kbd_get_keyboard(KBD_INDEX(dev));
652 if ((sc == NULL) || (kbd == NULL) || !KBD_IS_VALID(kbd)) {
653 revents = POLLHUP; /* the keyboard has gone */
654 } else if (events & (POLLIN | POLLRDNORM)) {
655 if (sc->gkb_q.c_cc > 0)
656 revents = events & (POLLIN | POLLRDNORM);
658 selrecord(td, &sc->gkb_rsel);
665 genkbd_event(keyboard_t *kbd, int event, void *arg)
673 /* assert(KBD_IS_VALID(kbd)) */
674 sc = (genkbd_softc_t *)arg;
679 case KBDIO_UNLOADING:
680 /* the keyboard is going... */
681 kbd_release(kbd, (void *)sc);
682 if (sc->gkb_flags & KB_ASLEEP) {
683 sc->gkb_flags &= ~KB_ASLEEP;
686 selwakeup(&sc->gkb_rsel);
692 /* obtain the current key input mode */
693 if ((*kbdsw[kbd->kb_index]->ioctl)(kbd, KDGKBMODE, (caddr_t)&mode))
696 /* read all pending input */
697 while ((*kbdsw[kbd->kb_index]->check_char)(kbd)) {
698 c = (*kbdsw[kbd->kb_index]->read_char)(kbd, FALSE);
701 if (c == ERRKEY) /* XXX: ring bell? */
703 if (!KBD_IS_BUSY(kbd))
704 /* the device is not open, discard the input */
707 /* store the byte as is for K_RAW and K_CODE modes */
708 if (mode != K_XLATE) {
709 putc(KEYCHAR(c), &sc->gkb_q);
714 if (c & RELKEY) /* key release is ignored */
717 /* process special keys; most of them are just ignored... */
719 switch (KEYCHAR(c)) {
723 case BTAB: /* a backtab: ESC [ Z */
724 putc(0x1b, &sc->gkb_q);
725 putc('[', &sc->gkb_q);
726 putc('Z', &sc->gkb_q);
731 /* normal chars, normal chars with the META, function keys */
732 switch (KEYFLAGS(c)) {
733 case 0: /* a normal char */
734 putc(KEYCHAR(c), &sc->gkb_q);
736 case MKEY: /* the META flag: prepend ESC */
737 putc(0x1b, &sc->gkb_q);
738 putc(KEYCHAR(c), &sc->gkb_q);
740 case FKEY | SPCLKEY: /* a function key, return string */
741 cp = (*kbdsw[kbd->kb_index]->get_fkeystr)(kbd,
745 putc(*cp++, &sc->gkb_q);
751 /* wake up sleeping/polling processes */
752 if (sc->gkb_q.c_cc > 0) {
753 if (sc->gkb_flags & KB_ASLEEP) {
754 sc->gkb_flags &= ~KB_ASLEEP;
757 selwakeup(&sc->gkb_rsel);
763 #endif /* KBD_INSTALL_CDEV */
766 * Generic low-level keyboard functions
767 * The low-level functions in the keyboard subdriver may use these
772 genkbd_commonioctl(keyboard_t *kbd, u_long cmd, caddr_t arg)
782 case KDGKBINFO: /* get keyboard information */
783 ((keyboard_info_t *)arg)->kb_index = kbd->kb_index;
784 i = imin(strlen(kbd->kb_name) + 1,
785 sizeof(((keyboard_info_t *)arg)->kb_name));
786 bcopy(kbd->kb_name, ((keyboard_info_t *)arg)->kb_name, i);
787 ((keyboard_info_t *)arg)->kb_unit = kbd->kb_unit;
788 ((keyboard_info_t *)arg)->kb_type = kbd->kb_type;
789 ((keyboard_info_t *)arg)->kb_config = kbd->kb_config;
790 ((keyboard_info_t *)arg)->kb_flags = kbd->kb_flags;
793 case KDGKBTYPE: /* get keyboard type */
794 *(int *)arg = kbd->kb_type;
797 case KDGETREPEAT: /* get keyboard repeat rate */
798 ((int *)arg)[0] = kbd->kb_delay1;
799 ((int *)arg)[1] = kbd->kb_delay2;
802 case GIO_KEYMAP: /* get keyboard translation table */
803 bcopy(kbd->kb_keymap, arg, sizeof(*kbd->kb_keymap));
805 case PIO_KEYMAP: /* set keyboard translation table */
806 #ifndef KBD_DISABLE_KEYMAP_LOAD
807 bzero(kbd->kb_accentmap, sizeof(*kbd->kb_accentmap));
808 bcopy(arg, kbd->kb_keymap, sizeof(*kbd->kb_keymap));
815 case GIO_KEYMAPENT: /* get keyboard translation table entry */
816 keyp = (keyarg_t *)arg;
817 if (keyp->keynum >= sizeof(kbd->kb_keymap->key)
818 /sizeof(kbd->kb_keymap->key[0])) {
822 bcopy(&kbd->kb_keymap->key[keyp->keynum], &keyp->key,
825 case PIO_KEYMAPENT: /* set keyboard translation table entry */
826 #ifndef KBD_DISABLE_KEYMAP_LOAD
827 keyp = (keyarg_t *)arg;
828 if (keyp->keynum >= sizeof(kbd->kb_keymap->key)
829 /sizeof(kbd->kb_keymap->key[0])) {
833 bcopy(&keyp->key, &kbd->kb_keymap->key[keyp->keynum],
841 case GIO_DEADKEYMAP: /* get accent key translation table */
842 bcopy(kbd->kb_accentmap, arg, sizeof(*kbd->kb_accentmap));
844 case PIO_DEADKEYMAP: /* set accent key translation table */
845 #ifndef KBD_DISABLE_KEYMAP_LOAD
846 bcopy(arg, kbd->kb_accentmap, sizeof(*kbd->kb_accentmap));
853 case GETFKEY: /* get functionkey string */
854 fkeyp = (fkeyarg_t *)arg;
855 if (fkeyp->keynum >= kbd->kb_fkeytab_size) {
859 bcopy(kbd->kb_fkeytab[fkeyp->keynum].str, fkeyp->keydef,
860 kbd->kb_fkeytab[fkeyp->keynum].len);
861 fkeyp->flen = kbd->kb_fkeytab[fkeyp->keynum].len;
863 case SETFKEY: /* set functionkey string */
864 #ifndef KBD_DISABLE_KEYMAP_LOAD
865 fkeyp = (fkeyarg_t *)arg;
866 if (fkeyp->keynum >= kbd->kb_fkeytab_size) {
870 kbd->kb_fkeytab[fkeyp->keynum].len = imin(fkeyp->flen, MAXFK);
871 bcopy(fkeyp->keydef, kbd->kb_fkeytab[fkeyp->keynum].str,
872 kbd->kb_fkeytab[fkeyp->keynum].len);
888 /* get a pointer to the string associated with the given function key */
890 *genkbd_get_fkeystr(keyboard_t *kbd, int fkey, size_t *len)
895 if (fkey > kbd->kb_fkeytab_size)
897 *len = kbd->kb_fkeytab[fkey].len;
898 return kbd->kb_fkeytab[fkey].str;
901 /* diagnostic dump */
903 *get_kbd_type_name(int type)
910 { KB_101, "AT 101/102" },
911 { KB_OTHER, "generic" },
915 for (i = 0; i < sizeof(name_table)/sizeof(name_table[0]); ++i) {
916 if (type == name_table[i].type)
917 return name_table[i].name;
923 genkbd_diag(keyboard_t *kbd, int level)
926 printf("kbd%d: %s%d, %s (%d), config:0x%x, flags:0x%x",
927 kbd->kb_index, kbd->kb_name, kbd->kb_unit,
928 get_kbd_type_name(kbd->kb_type), kbd->kb_type,
929 kbd->kb_config, kbd->kb_flags);
930 if (kbd->kb_io_base > 0)
931 printf(", port:0x%x-0x%x", kbd->kb_io_base,
932 kbd->kb_io_base + kbd->kb_io_size - 1);
937 #define set_lockkey_state(k, s, l) \
938 if (!((s) & l ## DOWN)) { \
942 i = (s) & LOCK_MASK; \
943 (*kbdsw[(k)->kb_index]->ioctl)((k), KDSETLED, (caddr_t)&i); \
947 save_accent_key(keyboard_t *kbd, u_int key, int *accents)
951 /* make an index into the accent map */
953 if ((i > kbd->kb_accentmap->n_accs)
954 || (kbd->kb_accentmap->acc[i - 1].accchar == 0)) {
955 /* the index is out of range or pointing to an empty entry */
961 * If the same accent key has been hit twice, produce the accent char
965 key = kbd->kb_accentmap->acc[i - 1].accchar;
970 /* remember the index and wait for the next key */
976 make_accent_char(keyboard_t *kbd, u_int ch, int *accents)
981 acc = &kbd->kb_accentmap->acc[*accents - 1];
985 * If the accent key is followed by the space key,
986 * produce the accent char itself.
991 /* scan the accent map */
992 for (i = 0; i < NUM_ACCENTCHARS; ++i) {
993 if (acc->map[i][0] == 0) /* end of table */
995 if (acc->map[i][0] == ch)
996 return acc->map[i][1];
998 /* this char cannot be accented... */
1003 genkbd_keyaction(keyboard_t *kbd, int keycode, int up, int *shiftstate,
1006 struct keyent_t *key;
1007 int state = *shiftstate;
1013 f = state & (AGRS | ALKED);
1014 if ((f == AGRS1) || (f == AGRS2) || (f == ALKED))
1016 key = &kbd->kb_keymap->key[i];
1017 i = ((state & SHIFTS) ? 1 : 0)
1018 | ((state & CTLS) ? 2 : 0)
1019 | ((state & ALTS) ? 4 : 0);
1020 if (((key->flgs & FLAG_LOCK_C) && (state & CLKED))
1021 || ((key->flgs & FLAG_LOCK_N) && (state & NLKED)) )
1024 if (up) { /* break: key released */
1025 action = kbd->kb_lastact[keycode];
1026 kbd->kb_lastact[keycode] = NOP;
1029 if (state & SHIFTAON) {
1030 set_lockkey_state(kbd, state, ALK);
1039 if (state & SHIFTAON) {
1040 set_lockkey_state(kbd, state, ALK);
1049 if (state & SHIFTAON) {
1050 set_lockkey_state(kbd, state, ALK);
1059 if (state & SHIFTAON) {
1060 set_lockkey_state(kbd, state, ALK);
1069 if (state & SHIFTAON) {
1070 set_lockkey_state(kbd, state, ALK);
1079 if (state & SHIFTAON) {
1080 set_lockkey_state(kbd, state, ALK);
1102 i = state & LOCK_MASK;
1103 (*kbdsw[kbd->kb_index]->ioctl)(kbd, KDSETLED,
1114 /* release events of regular keys are not reported */
1115 *shiftstate &= ~SHIFTAON;
1118 *shiftstate = state & ~SHIFTAON;
1119 return (SPCLKEY | RELKEY | action);
1120 } else { /* make: key pressed */
1121 action = key->map[i];
1123 if (key->spcl & (0x80 >> i)) {
1125 if (kbd->kb_lastact[keycode] == NOP)
1126 kbd->kb_lastact[keycode] = action;
1127 if (kbd->kb_lastact[keycode] != action)
1132 set_lockkey_state(kbd, state, NLK);
1136 set_lockkey_state(kbd, state, CLK);
1139 i = state & LOCK_MASK;
1140 (*kbdsw[kbd->kb_index]->ioctl)(kbd, KDSETLED,
1145 set_lockkey_state(kbd, state, SLK);
1148 set_lockkey_state(kbd, state, ALK);
1150 /* NON-LOCKING KEYS */
1151 case SPSC: case RBT: case SUSP: case STBY:
1152 case DBG: case NEXT: case PREV: case PNC:
1153 case HALT: case PDWN:
1209 *shiftstate = state;
1212 /* is this an accent (dead) key? */
1213 *shiftstate = state;
1214 if (action >= F_ACC && action <= L_ACC) {
1215 action = save_accent_key(kbd, action,
1223 return (action | MKEY);
1229 /* other special keys */
1234 if (action >= F_FN && action <= L_FN)
1236 /* XXX: return fkey string for the FKEY? */
1237 return (SPCLKEY | action);
1239 *shiftstate = state;
1240 return (SPCLKEY | action);
1243 kbd->kb_lastact[keycode] = NOP;
1244 *shiftstate = state;
1246 /* make an accented char */
1247 action = make_accent_char(kbd, action, accents);
1248 if (action == ERRKEY)