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.6 2003/08/07 21:16:56 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>
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 = {
430 /* maj */ CDEV_MAJOR,
435 /* open */ genkbdopen,
436 /* close */ genkbdclose,
437 /* read */ genkbdread,
438 /* write */ genkbdwrite,
439 /* ioctl */ genkbdioctl,
440 /* poll */ genkbdpoll,
442 /* strategy */ nostrategy,
448 kbd_attach(keyboard_t *kbd)
452 if (kbd->kb_index >= keyboards)
454 if (keyboard[kbd->kb_index] != kbd)
457 dev = make_dev(&kbd_cdevsw, kbd->kb_index, UID_ROOT, GID_WHEEL, 0600,
458 "kbd%r", kbd->kb_index);
459 if (dev->si_drv1 == NULL)
460 dev->si_drv1 = malloc(sizeof(genkbd_softc_t), M_DEVBUF,
462 bzero(dev->si_drv1, sizeof(genkbd_softc_t));
464 printf("kbd%d at %s%d\n", kbd->kb_index, kbd->kb_name, kbd->kb_unit);
469 kbd_detach(keyboard_t *kbd)
473 if (kbd->kb_index >= keyboards)
475 if (keyboard[kbd->kb_index] != kbd)
478 dev = makedev(kbd_cdevsw.d_maj, kbd->kb_index);
480 free(dev->si_drv1, M_DEVBUF);
487 * Generic keyboard cdev driver functions
488 * Keyboard subdrivers may call these functions to implement common
493 #define KB_BUFSIZE 64
495 static kbd_callback_func_t genkbd_event;
498 genkbdopen(dev_t dev, int mode, int flag, d_thread_t *td)
507 kbd = kbd_get_keyboard(KBD_INDEX(dev));
508 if ((sc == NULL) || (kbd == NULL) || !KBD_IS_VALID(kbd)) {
512 i = kbd_allocate(kbd->kb_name, kbd->kb_unit, sc,
513 genkbd_event, (void *)sc);
518 /* assert(i == kbd->kb_index) */
519 /* assert(kbd == kbd_get_keyboard(i)) */
522 * NOTE: even when we have successfully claimed a keyboard,
523 * the device may still be missing (!KBD_HAS_DEVICE(kbd)).
527 bzero(&sc->gkb_q, sizeof(sc->gkb_q));
529 clist_alloc_cblocks(&sc->gkb_q, KB_QSIZE, KB_QSIZE/2); /* XXX */
530 sc->gkb_rsel.si_flags = 0;
531 sc->gkb_rsel.si_pid = 0;
538 genkbdclose(dev_t dev, int mode, int flag, d_thread_t *td)
545 * NOTE: the device may have already become invalid.
546 * kbd == NULL || !KBD_IS_VALID(kbd)
550 kbd = kbd_get_keyboard(KBD_INDEX(dev));
551 if ((sc == NULL) || (kbd == NULL) || !KBD_IS_VALID(kbd)) {
552 /* XXX: we shall be forgiving and don't report error... */
554 kbd_release(kbd, (void *)sc);
556 clist_free_cblocks(&sc->gkb_q);
564 genkbdread(dev_t dev, struct uio *uio, int flag)
568 u_char buffer[KB_BUFSIZE];
576 kbd = kbd_get_keyboard(KBD_INDEX(dev));
577 if ((sc == NULL) || (kbd == NULL) || !KBD_IS_VALID(kbd)) {
581 while (sc->gkb_q.c_cc == 0) {
582 if (flag & IO_NDELAY) {
586 sc->gkb_flags |= KB_ASLEEP;
587 error = tsleep((caddr_t)sc, PCATCH, "kbdrea", 0);
588 kbd = kbd_get_keyboard(KBD_INDEX(dev));
589 if ((kbd == NULL) || !KBD_IS_VALID(kbd)) {
591 return ENXIO; /* our keyboard has gone... */
594 sc->gkb_flags &= ~KB_ASLEEP;
601 /* copy as much input as possible */
603 while (uio->uio_resid > 0) {
604 len = imin(uio->uio_resid, sizeof(buffer));
605 len = q_to_b(&sc->gkb_q, buffer, len);
608 error = uiomove(buffer, len, uio);
617 genkbdwrite(dev_t dev, struct uio *uio, int flag)
621 kbd = kbd_get_keyboard(KBD_INDEX(dev));
622 if ((kbd == NULL) || !KBD_IS_VALID(kbd))
628 genkbdioctl(dev_t dev, u_long cmd, caddr_t arg, int flag, d_thread_t *td)
633 kbd = kbd_get_keyboard(KBD_INDEX(dev));
634 if ((kbd == NULL) || !KBD_IS_VALID(kbd))
636 error = (*kbdsw[kbd->kb_index]->ioctl)(kbd, cmd, arg);
637 if (error == ENOIOCTL)
643 genkbdpoll(dev_t dev, int events, d_thread_t *td)
653 kbd = kbd_get_keyboard(KBD_INDEX(dev));
654 if ((sc == NULL) || (kbd == NULL) || !KBD_IS_VALID(kbd)) {
655 revents = POLLHUP; /* the keyboard has gone */
656 } else if (events & (POLLIN | POLLRDNORM)) {
657 if (sc->gkb_q.c_cc > 0)
658 revents = events & (POLLIN | POLLRDNORM);
660 selrecord(td, &sc->gkb_rsel);
667 genkbd_event(keyboard_t *kbd, int event, void *arg)
675 /* assert(KBD_IS_VALID(kbd)) */
676 sc = (genkbd_softc_t *)arg;
681 case KBDIO_UNLOADING:
682 /* the keyboard is going... */
683 kbd_release(kbd, (void *)sc);
684 if (sc->gkb_flags & KB_ASLEEP) {
685 sc->gkb_flags &= ~KB_ASLEEP;
688 selwakeup(&sc->gkb_rsel);
694 /* obtain the current key input mode */
695 if ((*kbdsw[kbd->kb_index]->ioctl)(kbd, KDGKBMODE, (caddr_t)&mode))
698 /* read all pending input */
699 while ((*kbdsw[kbd->kb_index]->check_char)(kbd)) {
700 c = (*kbdsw[kbd->kb_index]->read_char)(kbd, FALSE);
703 if (c == ERRKEY) /* XXX: ring bell? */
705 if (!KBD_IS_BUSY(kbd))
706 /* the device is not open, discard the input */
709 /* store the byte as is for K_RAW and K_CODE modes */
710 if (mode != K_XLATE) {
711 putc(KEYCHAR(c), &sc->gkb_q);
716 if (c & RELKEY) /* key release is ignored */
719 /* process special keys; most of them are just ignored... */
721 switch (KEYCHAR(c)) {
725 case BTAB: /* a backtab: ESC [ Z */
726 putc(0x1b, &sc->gkb_q);
727 putc('[', &sc->gkb_q);
728 putc('Z', &sc->gkb_q);
733 /* normal chars, normal chars with the META, function keys */
734 switch (KEYFLAGS(c)) {
735 case 0: /* a normal char */
736 putc(KEYCHAR(c), &sc->gkb_q);
738 case MKEY: /* the META flag: prepend ESC */
739 putc(0x1b, &sc->gkb_q);
740 putc(KEYCHAR(c), &sc->gkb_q);
742 case FKEY | SPCLKEY: /* a function key, return string */
743 cp = (*kbdsw[kbd->kb_index]->get_fkeystr)(kbd,
747 putc(*cp++, &sc->gkb_q);
753 /* wake up sleeping/polling processes */
754 if (sc->gkb_q.c_cc > 0) {
755 if (sc->gkb_flags & KB_ASLEEP) {
756 sc->gkb_flags &= ~KB_ASLEEP;
759 selwakeup(&sc->gkb_rsel);
765 #endif /* KBD_INSTALL_CDEV */
768 * Generic low-level keyboard functions
769 * The low-level functions in the keyboard subdriver may use these
774 genkbd_commonioctl(keyboard_t *kbd, u_long cmd, caddr_t arg)
784 case KDGKBINFO: /* get keyboard information */
785 ((keyboard_info_t *)arg)->kb_index = kbd->kb_index;
786 i = imin(strlen(kbd->kb_name) + 1,
787 sizeof(((keyboard_info_t *)arg)->kb_name));
788 bcopy(kbd->kb_name, ((keyboard_info_t *)arg)->kb_name, i);
789 ((keyboard_info_t *)arg)->kb_unit = kbd->kb_unit;
790 ((keyboard_info_t *)arg)->kb_type = kbd->kb_type;
791 ((keyboard_info_t *)arg)->kb_config = kbd->kb_config;
792 ((keyboard_info_t *)arg)->kb_flags = kbd->kb_flags;
795 case KDGKBTYPE: /* get keyboard type */
796 *(int *)arg = kbd->kb_type;
799 case KDGETREPEAT: /* get keyboard repeat rate */
800 ((int *)arg)[0] = kbd->kb_delay1;
801 ((int *)arg)[1] = kbd->kb_delay2;
804 case GIO_KEYMAP: /* get keyboard translation table */
805 bcopy(kbd->kb_keymap, arg, sizeof(*kbd->kb_keymap));
807 case PIO_KEYMAP: /* set keyboard translation table */
808 #ifndef KBD_DISABLE_KEYMAP_LOAD
809 bzero(kbd->kb_accentmap, sizeof(*kbd->kb_accentmap));
810 bcopy(arg, kbd->kb_keymap, sizeof(*kbd->kb_keymap));
817 case GIO_KEYMAPENT: /* get keyboard translation table entry */
818 keyp = (keyarg_t *)arg;
819 if (keyp->keynum >= sizeof(kbd->kb_keymap->key)
820 /sizeof(kbd->kb_keymap->key[0])) {
824 bcopy(&kbd->kb_keymap->key[keyp->keynum], &keyp->key,
827 case PIO_KEYMAPENT: /* set keyboard translation table entry */
828 #ifndef KBD_DISABLE_KEYMAP_LOAD
829 keyp = (keyarg_t *)arg;
830 if (keyp->keynum >= sizeof(kbd->kb_keymap->key)
831 /sizeof(kbd->kb_keymap->key[0])) {
835 bcopy(&keyp->key, &kbd->kb_keymap->key[keyp->keynum],
843 case GIO_DEADKEYMAP: /* get accent key translation table */
844 bcopy(kbd->kb_accentmap, arg, sizeof(*kbd->kb_accentmap));
846 case PIO_DEADKEYMAP: /* set accent key translation table */
847 #ifndef KBD_DISABLE_KEYMAP_LOAD
848 bcopy(arg, kbd->kb_accentmap, sizeof(*kbd->kb_accentmap));
855 case GETFKEY: /* get functionkey string */
856 fkeyp = (fkeyarg_t *)arg;
857 if (fkeyp->keynum >= kbd->kb_fkeytab_size) {
861 bcopy(kbd->kb_fkeytab[fkeyp->keynum].str, fkeyp->keydef,
862 kbd->kb_fkeytab[fkeyp->keynum].len);
863 fkeyp->flen = kbd->kb_fkeytab[fkeyp->keynum].len;
865 case SETFKEY: /* set functionkey string */
866 #ifndef KBD_DISABLE_KEYMAP_LOAD
867 fkeyp = (fkeyarg_t *)arg;
868 if (fkeyp->keynum >= kbd->kb_fkeytab_size) {
872 kbd->kb_fkeytab[fkeyp->keynum].len = imin(fkeyp->flen, MAXFK);
873 bcopy(fkeyp->keydef, kbd->kb_fkeytab[fkeyp->keynum].str,
874 kbd->kb_fkeytab[fkeyp->keynum].len);
890 /* get a pointer to the string associated with the given function key */
892 *genkbd_get_fkeystr(keyboard_t *kbd, int fkey, size_t *len)
897 if (fkey > kbd->kb_fkeytab_size)
899 *len = kbd->kb_fkeytab[fkey].len;
900 return kbd->kb_fkeytab[fkey].str;
903 /* diagnostic dump */
905 *get_kbd_type_name(int type)
912 { KB_101, "AT 101/102" },
913 { KB_OTHER, "generic" },
917 for (i = 0; i < sizeof(name_table)/sizeof(name_table[0]); ++i) {
918 if (type == name_table[i].type)
919 return name_table[i].name;
925 genkbd_diag(keyboard_t *kbd, int level)
928 printf("kbd%d: %s%d, %s (%d), config:0x%x, flags:0x%x",
929 kbd->kb_index, kbd->kb_name, kbd->kb_unit,
930 get_kbd_type_name(kbd->kb_type), kbd->kb_type,
931 kbd->kb_config, kbd->kb_flags);
932 if (kbd->kb_io_base > 0)
933 printf(", port:0x%x-0x%x", kbd->kb_io_base,
934 kbd->kb_io_base + kbd->kb_io_size - 1);
939 #define set_lockkey_state(k, s, l) \
940 if (!((s) & l ## DOWN)) { \
944 i = (s) & LOCK_MASK; \
945 (*kbdsw[(k)->kb_index]->ioctl)((k), KDSETLED, (caddr_t)&i); \
949 save_accent_key(keyboard_t *kbd, u_int key, int *accents)
953 /* make an index into the accent map */
955 if ((i > kbd->kb_accentmap->n_accs)
956 || (kbd->kb_accentmap->acc[i - 1].accchar == 0)) {
957 /* the index is out of range or pointing to an empty entry */
963 * If the same accent key has been hit twice, produce the accent char
967 key = kbd->kb_accentmap->acc[i - 1].accchar;
972 /* remember the index and wait for the next key */
978 make_accent_char(keyboard_t *kbd, u_int ch, int *accents)
983 acc = &kbd->kb_accentmap->acc[*accents - 1];
987 * If the accent key is followed by the space key,
988 * produce the accent char itself.
993 /* scan the accent map */
994 for (i = 0; i < NUM_ACCENTCHARS; ++i) {
995 if (acc->map[i][0] == 0) /* end of table */
997 if (acc->map[i][0] == ch)
998 return acc->map[i][1];
1000 /* this char cannot be accented... */
1005 genkbd_keyaction(keyboard_t *kbd, int keycode, int up, int *shiftstate,
1008 struct keyent_t *key;
1009 int state = *shiftstate;
1015 f = state & (AGRS | ALKED);
1016 if ((f == AGRS1) || (f == AGRS2) || (f == ALKED))
1018 key = &kbd->kb_keymap->key[i];
1019 i = ((state & SHIFTS) ? 1 : 0)
1020 | ((state & CTLS) ? 2 : 0)
1021 | ((state & ALTS) ? 4 : 0);
1022 if (((key->flgs & FLAG_LOCK_C) && (state & CLKED))
1023 || ((key->flgs & FLAG_LOCK_N) && (state & NLKED)) )
1026 if (up) { /* break: key released */
1027 action = kbd->kb_lastact[keycode];
1028 kbd->kb_lastact[keycode] = NOP;
1031 if (state & SHIFTAON) {
1032 set_lockkey_state(kbd, state, ALK);
1041 if (state & SHIFTAON) {
1042 set_lockkey_state(kbd, state, ALK);
1051 if (state & SHIFTAON) {
1052 set_lockkey_state(kbd, state, ALK);
1061 if (state & SHIFTAON) {
1062 set_lockkey_state(kbd, state, ALK);
1071 if (state & SHIFTAON) {
1072 set_lockkey_state(kbd, state, ALK);
1081 if (state & SHIFTAON) {
1082 set_lockkey_state(kbd, state, ALK);
1104 i = state & LOCK_MASK;
1105 (*kbdsw[kbd->kb_index]->ioctl)(kbd, KDSETLED,
1116 /* release events of regular keys are not reported */
1117 *shiftstate &= ~SHIFTAON;
1120 *shiftstate = state & ~SHIFTAON;
1121 return (SPCLKEY | RELKEY | action);
1122 } else { /* make: key pressed */
1123 action = key->map[i];
1125 if (key->spcl & (0x80 >> i)) {
1127 if (kbd->kb_lastact[keycode] == NOP)
1128 kbd->kb_lastact[keycode] = action;
1129 if (kbd->kb_lastact[keycode] != action)
1134 set_lockkey_state(kbd, state, NLK);
1138 set_lockkey_state(kbd, state, CLK);
1141 i = state & LOCK_MASK;
1142 (*kbdsw[kbd->kb_index]->ioctl)(kbd, KDSETLED,
1147 set_lockkey_state(kbd, state, SLK);
1150 set_lockkey_state(kbd, state, ALK);
1152 /* NON-LOCKING KEYS */
1153 case SPSC: case RBT: case SUSP: case STBY:
1154 case DBG: case NEXT: case PREV: case PNC:
1155 case HALT: case PDWN:
1211 *shiftstate = state;
1214 /* is this an accent (dead) key? */
1215 *shiftstate = state;
1216 if (action >= F_ACC && action <= L_ACC) {
1217 action = save_accent_key(kbd, action,
1225 return (action | MKEY);
1231 /* other special keys */
1236 if (action >= F_FN && action <= L_FN)
1238 /* XXX: return fkey string for the FKEY? */
1239 return (SPCLKEY | action);
1241 *shiftstate = state;
1242 return (SPCLKEY | action);
1245 kbd->kb_lastact[keycode] = NOP;
1246 *shiftstate = state;
1248 /* make an accented char */
1249 action = make_accent_char(kbd, action, accents);
1250 if (action == ERRKEY)