Add qemu 2.4.0
[kvmfornfv.git] / qemu / roms / openhackware / src / dev / char / kbd.c
1 /*
2  * <kbd.c>
3  *
4  * Open Hack'Ware BIOS generic keyboard input translation.
5  * 
6  *  Copyright (c) 2005 Jocelyn Mayer
7  *
8  *   This program is free software; you can redistribute it and/or
9  *   modify it under the terms of the GNU General Public License V2
10  *   as published by the Free Software Foundation
11  *
12  *   This program is distributed in the hope that it will be useful,
13  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *   GNU General Public License for more details.
16  *
17  *   You should have received a copy of the GNU General Public License
18  *   along with this program; if not, write to the Free Software
19  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20  */
21
22 #include <stdlib.h>
23 #include <stdio.h>
24 #include "bios.h"
25 #include "kbd.h"
26
27 //#define DEBUG_KBD
28 #ifdef DEBUG_KBD
29 #define KBD_DPRINTF(fmt, args...) \
30 do { dprintf("KBD - %s: " fmt, __func__ , ##args); } while (0)
31 #else
32 #define KBD_DPRINTF(fmt, args...) do { } while (0)
33 #endif
34
35 void *kbd_new (int len)
36 {
37     kbd_t *kbd;
38
39     if (len < (int)sizeof(kbd_t)) {
40         kbd = NULL;
41     } else {
42         kbd = malloc(len);
43         if (kbd != NULL)
44             memset(kbd, 0, len);
45     }
46
47     return kbd;
48 }
49
50 int kbd_set_keymap (kbd_t *kbd, int nb_keys, keymap_t *keymap)
51 {
52     kbd->nb_keys = nb_keys;
53     kbd->keymap = keymap;
54
55     return 0;
56 }
57
58 int kbd_translate_key (kbd_t *kbd, int keycode, int up_down)
59 {
60     keymap_t *keyt;
61     int mod_state, key, type;
62     int ret;
63
64     ret = -1;
65     /* Get key table */
66     if (keycode < kbd->nb_keys) {
67         keyt = &kbd->keymap[keycode];
68         /* Get modifier state */
69         mod_state = (kbd->mod_state | (kbd->mod_state >> 8)) & 0xFF;
70         /* Adjust with lock */
71         if (keyt->lck_shift >= 0) {
72             if ((kbd->mod_state >> (16 + keyt->lck_shift)) & 0x01) {
73                 KBD_DPRINTF("adjust with lock %02x => %02x (%d %08x)\n",
74                             mod_state,
75                             mod_state ^ ((kbd->mod_state >>
76                                           (16 + keyt->lck_shift)) &
77                                          0x01),
78                             keyt->lck_shift, kbd->mod_state);
79             }
80             mod_state ^= (kbd->mod_state >> (16 + keyt->lck_shift)) & 0x01;
81         }
82         key = keyt->trans[mod_state];
83         type = key & 0xFF000000;
84         key &= ~0xFF000000;
85         switch (type) {
86         case KBD_TYPE_REGULAR:
87             if (!up_down) {
88                 /* We don't care about up events on "normal" keys */
89                 ret = key;
90             }
91             break;
92         case KBD_TYPE_LOCK:
93             if (!up_down) {
94                 kbd->mod_state ^= key;
95                 ret = -2;
96                 KBD_DPRINTF("Change modifier type %d key %04x %s => %08x\n",
97                             type, key, up_down ? "up" : "down",
98                             kbd->mod_state);
99             }
100             break;
101         case KBD_TYPE_LMOD:
102         case KBD_TYPE_RMOD:
103             if (up_down)
104                 kbd->mod_state &= ~key;
105             else
106                 kbd->mod_state |= key;
107             KBD_DPRINTF("Change modifier type %d key %04x %s => %08x\n",
108                         type, key, up_down ? "up" : "down", kbd->mod_state);
109             ret = -2; /* The caller may know the key was a modifier */
110             break;
111         default:
112             KBD_DPRINTF("Unknown key: keycode=%02x mod_state=%02x (%08x)\n",
113                         keycode, mod_state, kbd->mod_state);
114             break;
115         }
116     } else {
117         KBD_DPRINTF("Unmanaged key: keycode=%02x mod_state %08x\n",
118                     keycode, kbd->mod_state);
119     }
120
121     return ret;
122 }