| 1 | /*
|
|---|
| 2 | * Copyright (c) 2006 Josef Cejka
|
|---|
| 3 | * All rights reserved.
|
|---|
| 4 | *
|
|---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
|---|
| 6 | * modification, are permitted provided that the following conditions
|
|---|
| 7 | * are met:
|
|---|
| 8 | *
|
|---|
| 9 | * - Redistributions of source code must retain the above copyright
|
|---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
|---|
| 11 | * - 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.
|
|---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
|---|
| 15 | * derived from this software without specific prior written permission.
|
|---|
| 16 | *
|
|---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|---|
| 27 | */
|
|---|
| 28 |
|
|---|
| 29 | /**
|
|---|
| 30 | * @addtogroup kbdgen generic
|
|---|
| 31 | * @brief HelenOS generic uspace keyboard handler.
|
|---|
| 32 | * @ingroup kbd
|
|---|
| 33 | * @{
|
|---|
| 34 | */
|
|---|
| 35 | /** @file
|
|---|
| 36 | */
|
|---|
| 37 |
|
|---|
| 38 | #include <ipc/services.h>
|
|---|
| 39 | #include <ipc/kbd.h>
|
|---|
| 40 | #include <sysinfo.h>
|
|---|
| 41 | #include <stdio.h>
|
|---|
| 42 | #include <unistd.h>
|
|---|
| 43 | #include <stdlib.h>
|
|---|
| 44 | #include <stdio.h>
|
|---|
| 45 | #include <ipc/ns.h>
|
|---|
| 46 | #include <async.h>
|
|---|
| 47 | #include <errno.h>
|
|---|
| 48 | #include <adt/fifo.h>
|
|---|
| 49 | #include <io/console.h>
|
|---|
| 50 | #include <io/keycode.h>
|
|---|
| 51 | #include <devmap.h>
|
|---|
| 52 |
|
|---|
| 53 | #include <kbd.h>
|
|---|
| 54 | #include <kbd_port.h>
|
|---|
| 55 | #include <kbd_ctl.h>
|
|---|
| 56 | #include <layout.h>
|
|---|
| 57 |
|
|---|
| 58 | #define NAME "kbd"
|
|---|
| 59 | #define NAMESPACE "hid_in"
|
|---|
| 60 |
|
|---|
| 61 | int client_phone = -1;
|
|---|
| 62 |
|
|---|
| 63 | /** Currently active modifiers. */
|
|---|
| 64 | static unsigned mods = KM_NUM_LOCK;
|
|---|
| 65 |
|
|---|
| 66 | /** Currently pressed lock keys. We track these to tackle autorepeat. */
|
|---|
| 67 | static unsigned lock_keys;
|
|---|
| 68 |
|
|---|
| 69 | int cir_service = 0;
|
|---|
| 70 | int cir_phone = -1;
|
|---|
| 71 |
|
|---|
| 72 | #define NUM_LAYOUTS 3
|
|---|
| 73 |
|
|---|
| 74 | static layout_op_t *layout[NUM_LAYOUTS] = {
|
|---|
| 75 | &us_qwerty_op,
|
|---|
| 76 | &us_dvorak_op,
|
|---|
| 77 | &cz_op
|
|---|
| 78 | };
|
|---|
| 79 |
|
|---|
| 80 | static int active_layout = 0;
|
|---|
| 81 |
|
|---|
| 82 | void kbd_push_scancode(int scancode)
|
|---|
| 83 | {
|
|---|
| 84 | /* printf("scancode: 0x%x\n", scancode);*/
|
|---|
| 85 | kbd_ctl_parse_scancode(scancode);
|
|---|
| 86 | }
|
|---|
| 87 |
|
|---|
| 88 | void kbd_push_ev(int type, unsigned int key)
|
|---|
| 89 | {
|
|---|
| 90 | console_event_t ev;
|
|---|
| 91 | unsigned mod_mask;
|
|---|
| 92 |
|
|---|
| 93 | switch (key) {
|
|---|
| 94 | case KC_LCTRL: mod_mask = KM_LCTRL; break;
|
|---|
| 95 | case KC_RCTRL: mod_mask = KM_RCTRL; break;
|
|---|
| 96 | case KC_LSHIFT: mod_mask = KM_LSHIFT; break;
|
|---|
| 97 | case KC_RSHIFT: mod_mask = KM_RSHIFT; break;
|
|---|
| 98 | case KC_LALT: mod_mask = KM_LALT; break;
|
|---|
| 99 | case KC_RALT: mod_mask = KM_RALT; break;
|
|---|
| 100 | default: mod_mask = 0; break;
|
|---|
| 101 | }
|
|---|
| 102 |
|
|---|
| 103 | if (mod_mask != 0) {
|
|---|
| 104 | if (type == KEY_PRESS)
|
|---|
| 105 | mods = mods | mod_mask;
|
|---|
| 106 | else
|
|---|
| 107 | mods = mods & ~mod_mask;
|
|---|
| 108 | }
|
|---|
| 109 |
|
|---|
| 110 | switch (key) {
|
|---|
| 111 | case KC_CAPS_LOCK: mod_mask = KM_CAPS_LOCK; break;
|
|---|
| 112 | case KC_NUM_LOCK: mod_mask = KM_NUM_LOCK; break;
|
|---|
| 113 | case KC_SCROLL_LOCK: mod_mask = KM_SCROLL_LOCK; break;
|
|---|
| 114 | default: mod_mask = 0; break;
|
|---|
| 115 | }
|
|---|
| 116 |
|
|---|
| 117 | if (mod_mask != 0) {
|
|---|
| 118 | if (type == KEY_PRESS) {
|
|---|
| 119 | /*
|
|---|
| 120 | * Only change lock state on transition from released
|
|---|
| 121 | * to pressed. This prevents autorepeat from messing
|
|---|
| 122 | * up the lock state.
|
|---|
| 123 | */
|
|---|
| 124 | mods = mods ^ (mod_mask & ~lock_keys);
|
|---|
| 125 | lock_keys = lock_keys | mod_mask;
|
|---|
| 126 |
|
|---|
| 127 | /* Update keyboard lock indicator lights. */
|
|---|
| 128 | kbd_ctl_set_ind(mods);
|
|---|
| 129 | } else {
|
|---|
| 130 | lock_keys = lock_keys & ~mod_mask;
|
|---|
| 131 | }
|
|---|
| 132 | }
|
|---|
| 133 | /*
|
|---|
| 134 | printf("type: %d\n", type);
|
|---|
| 135 | printf("mods: 0x%x\n", mods);
|
|---|
| 136 | printf("keycode: %u\n", key);
|
|---|
| 137 | */
|
|---|
| 138 | if (type == KEY_PRESS && (mods & KM_LCTRL) &&
|
|---|
| 139 | key == KC_F1) {
|
|---|
| 140 | active_layout = 0;
|
|---|
| 141 | layout[active_layout]->reset();
|
|---|
| 142 | return;
|
|---|
| 143 | }
|
|---|
| 144 |
|
|---|
| 145 | if (type == KEY_PRESS && (mods & KM_LCTRL) &&
|
|---|
| 146 | key == KC_F2) {
|
|---|
| 147 | active_layout = 1;
|
|---|
| 148 | layout[active_layout]->reset();
|
|---|
| 149 | return;
|
|---|
| 150 | }
|
|---|
| 151 |
|
|---|
| 152 | if (type == KEY_PRESS && (mods & KM_LCTRL) &&
|
|---|
| 153 | key == KC_F3) {
|
|---|
| 154 | active_layout = 2;
|
|---|
| 155 | layout[active_layout]->reset();
|
|---|
| 156 | return;
|
|---|
| 157 | }
|
|---|
| 158 |
|
|---|
| 159 | ev.type = type;
|
|---|
| 160 | ev.key = key;
|
|---|
| 161 | ev.mods = mods;
|
|---|
| 162 |
|
|---|
| 163 | ev.c = layout[active_layout]->parse_ev(&ev);
|
|---|
| 164 |
|
|---|
| 165 | async_msg_4(client_phone, KBD_EVENT, ev.type, ev.key, ev.mods, ev.c);
|
|---|
| 166 | }
|
|---|
| 167 |
|
|---|
| 168 | static void client_connection(ipc_callid_t iid, ipc_call_t *icall)
|
|---|
| 169 | {
|
|---|
| 170 | ipc_callid_t callid;
|
|---|
| 171 | ipc_call_t call;
|
|---|
| 172 | int retval;
|
|---|
| 173 |
|
|---|
| 174 | async_answer_0(iid, EOK);
|
|---|
| 175 |
|
|---|
| 176 | while (1) {
|
|---|
| 177 | callid = async_get_call(&call);
|
|---|
| 178 | switch (IPC_GET_IMETHOD(call)) {
|
|---|
| 179 | case IPC_M_PHONE_HUNGUP:
|
|---|
| 180 | if (client_phone != -1) {
|
|---|
| 181 | async_hangup(client_phone);
|
|---|
| 182 | client_phone = -1;
|
|---|
| 183 | }
|
|---|
| 184 |
|
|---|
| 185 | async_answer_0(callid, EOK);
|
|---|
| 186 | return;
|
|---|
| 187 | case IPC_M_CONNECT_TO_ME:
|
|---|
| 188 | if (client_phone != -1) {
|
|---|
| 189 | retval = ELIMIT;
|
|---|
| 190 | break;
|
|---|
| 191 | }
|
|---|
| 192 | client_phone = IPC_GET_ARG5(call);
|
|---|
| 193 | retval = 0;
|
|---|
| 194 | break;
|
|---|
| 195 | case KBD_YIELD:
|
|---|
| 196 | kbd_port_yield();
|
|---|
| 197 | retval = 0;
|
|---|
| 198 | break;
|
|---|
| 199 | case KBD_RECLAIM:
|
|---|
| 200 | kbd_port_reclaim();
|
|---|
| 201 | retval = 0;
|
|---|
| 202 | break;
|
|---|
| 203 | default:
|
|---|
| 204 | retval = EINVAL;
|
|---|
| 205 | }
|
|---|
| 206 | async_answer_0(callid, retval);
|
|---|
| 207 | }
|
|---|
| 208 | }
|
|---|
| 209 |
|
|---|
| 210 |
|
|---|
| 211 | int main(int argc, char **argv)
|
|---|
| 212 | {
|
|---|
| 213 | printf("%s: HelenOS Keyboard service\n", NAME);
|
|---|
| 214 |
|
|---|
| 215 | sysarg_t fhc;
|
|---|
| 216 | sysarg_t obio;
|
|---|
| 217 |
|
|---|
| 218 | if ((sysinfo_get_value("kbd.cir.fhc", &fhc) == EOK) && (fhc))
|
|---|
| 219 | cir_service = SERVICE_FHC;
|
|---|
| 220 | else if ((sysinfo_get_value("kbd.cir.obio", &obio) == EOK) && (obio))
|
|---|
| 221 | cir_service = SERVICE_OBIO;
|
|---|
| 222 |
|
|---|
| 223 | if (cir_service) {
|
|---|
| 224 | while (cir_phone < 0)
|
|---|
| 225 | cir_phone = service_connect_blocking(cir_service, 0, 0);
|
|---|
| 226 | }
|
|---|
| 227 |
|
|---|
| 228 | /* Initialize port driver. */
|
|---|
| 229 | if (kbd_port_init() != 0)
|
|---|
| 230 | return -1;
|
|---|
| 231 |
|
|---|
| 232 | /* Initialize controller driver. */
|
|---|
| 233 | if (kbd_ctl_init() != 0)
|
|---|
| 234 | return -1;
|
|---|
| 235 |
|
|---|
| 236 | /* Initialize (reset) layout. */
|
|---|
| 237 | layout[active_layout]->reset();
|
|---|
| 238 |
|
|---|
| 239 | /* Register driver */
|
|---|
| 240 | int rc = devmap_driver_register(NAME, client_connection);
|
|---|
| 241 | if (rc < 0) {
|
|---|
| 242 | printf("%s: Unable to register driver (%d)\n", NAME, rc);
|
|---|
| 243 | return -1;
|
|---|
| 244 | }
|
|---|
| 245 |
|
|---|
| 246 | char kbd[DEVMAP_NAME_MAXLEN + 1];
|
|---|
| 247 | snprintf(kbd, DEVMAP_NAME_MAXLEN, "%s/%s", NAMESPACE, NAME);
|
|---|
| 248 |
|
|---|
| 249 | devmap_handle_t devmap_handle;
|
|---|
| 250 | if (devmap_device_register(kbd, &devmap_handle) != EOK) {
|
|---|
| 251 | printf("%s: Unable to register device %s\n", NAME, kbd);
|
|---|
| 252 | return -1;
|
|---|
| 253 | }
|
|---|
| 254 |
|
|---|
| 255 | printf(NAME ": Accepting connections\n");
|
|---|
| 256 | async_manager();
|
|---|
| 257 |
|
|---|
| 258 | /* Not reached. */
|
|---|
| 259 | return 0;
|
|---|
| 260 | }
|
|---|
| 261 |
|
|---|
| 262 | /**
|
|---|
| 263 | * @}
|
|---|
| 264 | */
|
|---|