[51d6f80] | 1 | /*
|
---|
[df4ed85] | 2 | * Copyright (c) 2006 Josef Cejka
|
---|
[51d6f80] | 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 |
|
---|
[231a60a] | 29 | /**
|
---|
| 30 | * @addtogroup kbdgen generic
|
---|
[ce5bcb4] | 31 | * @brief HelenOS generic uspace keyboard handler.
|
---|
| 32 | * @ingroup kbd
|
---|
| 33 | * @{
|
---|
| 34 | */
|
---|
| 35 | /** @file
|
---|
| 36 | */
|
---|
| 37 |
|
---|
[7ee6aff] | 38 | #include <ipc/ipc.h>
|
---|
| 39 | #include <ipc/services.h>
|
---|
[51d6f80] | 40 | #include <stdio.h>
|
---|
| 41 | #include <unistd.h>
|
---|
| 42 | #include <stdlib.h>
|
---|
[37458472] | 43 | #include <stdio.h>
|
---|
[7ee6aff] | 44 | #include <ipc/ns.h>
|
---|
[fa09449] | 45 | #include <async.h>
|
---|
[51d6f80] | 46 | #include <errno.h>
|
---|
[fa09449] | 47 | #include <libadt/fifo.h>
|
---|
| 48 | #include <kbd/kbd.h>
|
---|
[90e3d6a] | 49 | #include <kbd/keycode.h>
|
---|
[fa09449] | 50 |
|
---|
[51d6f80] | 51 | #include <kbd.h>
|
---|
[51c1b003] | 52 | #include <key_buffer.h>
|
---|
[f89979b] | 53 | #include <kbd_port.h>
|
---|
| 54 | #include <kbd_ctl.h>
|
---|
| 55 | #include <layout.h>
|
---|
[51d6f80] | 56 |
|
---|
[e00938c] | 57 | #define NAME "kbd"
|
---|
[854387b] | 58 |
|
---|
[085bd54] | 59 | int cons_connected = 0;
|
---|
| 60 | int phone2cons = -1;
|
---|
[d1eece6] | 61 | keybuffer_t keybuffer;
|
---|
| 62 |
|
---|
| 63 | /** Currently active modifiers. */
|
---|
[90e3d6a] | 64 | static unsigned mods = KM_NUM_LOCK;
|
---|
[085bd54] | 65 |
|
---|
[12b6796] | 66 | /** Currently pressed lock keys. We track these to tackle autorepeat. */
|
---|
| 67 | static unsigned lock_keys;
|
---|
| 68 |
|
---|
[f89979b] | 69 | void kbd_push_scancode(int scancode)
|
---|
| 70 | {
|
---|
[5ad8661] | 71 | /* printf("scancode: 0x%x\n", scancode);*/
|
---|
[f89979b] | 72 | kbd_ctl_parse_scancode(scancode);
|
---|
| 73 | }
|
---|
| 74 |
|
---|
[d1eece6] | 75 | void kbd_push_ev(int type, unsigned int key)
|
---|
[085bd54] | 76 | {
|
---|
[fa09449] | 77 | kbd_event_t ev;
|
---|
[d1eece6] | 78 | unsigned mod_mask;
|
---|
| 79 |
|
---|
| 80 | switch (key) {
|
---|
| 81 | case KC_LCTRL: mod_mask = KM_LCTRL; break;
|
---|
| 82 | case KC_RCTRL: mod_mask = KM_RCTRL; break;
|
---|
| 83 | case KC_LSHIFT: mod_mask = KM_LSHIFT; break;
|
---|
| 84 | case KC_RSHIFT: mod_mask = KM_RSHIFT; break;
|
---|
| 85 | case KC_LALT: mod_mask = KM_LALT; break;
|
---|
| 86 | case KC_RALT: mod_mask = KM_RALT; break;
|
---|
| 87 | default: mod_mask = 0; break;
|
---|
| 88 | }
|
---|
| 89 |
|
---|
| 90 | if (mod_mask != 0) {
|
---|
| 91 | if (type == KE_PRESS)
|
---|
| 92 | mods = mods | mod_mask;
|
---|
| 93 | else
|
---|
| 94 | mods = mods & ~mod_mask;
|
---|
| 95 | }
|
---|
| 96 |
|
---|
| 97 | switch (key) {
|
---|
| 98 | case KC_CAPS_LOCK: mod_mask = KM_CAPS_LOCK; break;
|
---|
| 99 | case KC_NUM_LOCK: mod_mask = KM_NUM_LOCK; break;
|
---|
| 100 | case KC_SCROLL_LOCK: mod_mask = KM_SCROLL_LOCK; break;
|
---|
| 101 | default: mod_mask = 0; break;
|
---|
| 102 | }
|
---|
| 103 |
|
---|
[12b6796] | 104 | if (mod_mask != 0) {
|
---|
| 105 | if (type == KE_PRESS) {
|
---|
| 106 | /*
|
---|
| 107 | * Only change lock state on transition from released
|
---|
| 108 | * to pressed. This prevents autorepeat from messing
|
---|
| 109 | * up the lock state.
|
---|
| 110 | */
|
---|
| 111 | mods = mods ^ (mod_mask & ~lock_keys);
|
---|
| 112 | lock_keys = lock_keys | mod_mask;
|
---|
| 113 | } else {
|
---|
| 114 | lock_keys = lock_keys & ~mod_mask;
|
---|
| 115 | }
|
---|
| 116 | }
|
---|
[5ad8661] | 117 | /*
|
---|
[f89979b] | 118 | printf("type: %d\n", type);
|
---|
| 119 | printf("mods: 0x%x\n", mods);
|
---|
| 120 | printf("keycode: %u\n", key);
|
---|
[5ad8661] | 121 | */
|
---|
[f89979b] | 122 | ev.type = type;
|
---|
| 123 | ev.key = key;
|
---|
| 124 | ev.mods = mods;
|
---|
| 125 |
|
---|
| 126 | ev.c = layout_parse_ev(&ev);
|
---|
| 127 |
|
---|
| 128 | async_msg_4(phone2cons, KBD_EVENT, ev.type, ev.key, ev.mods, ev.c);
|
---|
[085bd54] | 129 | }
|
---|
| 130 |
|
---|
| 131 | static void console_connection(ipc_callid_t iid, ipc_call_t *icall)
|
---|
| 132 | {
|
---|
| 133 | ipc_callid_t callid;
|
---|
| 134 | ipc_call_t call;
|
---|
| 135 | int retval;
|
---|
| 136 |
|
---|
| 137 | if (cons_connected) {
|
---|
[b74959bd] | 138 | ipc_answer_0(iid, ELIMIT);
|
---|
[085bd54] | 139 | return;
|
---|
| 140 | }
|
---|
| 141 | cons_connected = 1;
|
---|
[b74959bd] | 142 | ipc_answer_0(iid, EOK);
|
---|
[085bd54] | 143 |
|
---|
| 144 | while (1) {
|
---|
| 145 | callid = async_get_call(&call);
|
---|
| 146 | switch (IPC_GET_METHOD(call)) {
|
---|
| 147 | case IPC_M_PHONE_HUNGUP:
|
---|
| 148 | cons_connected = 0;
|
---|
| 149 | ipc_hangup(phone2cons);
|
---|
| 150 | phone2cons = -1;
|
---|
[b74959bd] | 151 | ipc_answer_0(callid, EOK);
|
---|
[085bd54] | 152 | return;
|
---|
| 153 | case IPC_M_CONNECT_TO_ME:
|
---|
| 154 | if (phone2cons != -1) {
|
---|
| 155 | retval = ELIMIT;
|
---|
| 156 | break;
|
---|
| 157 | }
|
---|
[38c706cc] | 158 | phone2cons = IPC_GET_ARG5(call);
|
---|
[085bd54] | 159 | retval = 0;
|
---|
| 160 | break;
|
---|
[153a209] | 161 | default:
|
---|
| 162 | retval = EINVAL;
|
---|
[085bd54] | 163 | }
|
---|
[b74959bd] | 164 | ipc_answer_0(callid, retval);
|
---|
[085bd54] | 165 | }
|
---|
| 166 | }
|
---|
| 167 |
|
---|
| 168 |
|
---|
[51d6f80] | 169 | int main(int argc, char **argv)
|
---|
| 170 | {
|
---|
[e00938c] | 171 | printf(NAME ": HelenOS Keyboard service\n");
|
---|
| 172 |
|
---|
[51d6f80] | 173 | ipcarg_t phonead;
|
---|
[37458472] | 174 |
|
---|
[f89979b] | 175 | /* Initialize port driver. */
|
---|
| 176 | if (kbd_port_init())
|
---|
[15039b67] | 177 | return -1;
|
---|
[51d6f80] | 178 |
|
---|
| 179 | /* Initialize key buffer */
|
---|
[79460ae] | 180 | keybuffer_init(&keybuffer);
|
---|
[51d6f80] | 181 |
|
---|
[d3f2cad] | 182 | async_set_client_connection(console_connection);
|
---|
[f89979b] | 183 |
|
---|
| 184 | /* Register service at nameserver. */
|
---|
[38c706cc] | 185 | if (ipc_connect_to_me(PHONE_NS, SERVICE_KEYBOARD, 0, 0, &phonead) != 0)
|
---|
[51d6f80] | 186 | return -1;
|
---|
[e00938c] | 187 |
|
---|
| 188 | printf(NAME ": Accepting connections\n");
|
---|
[085bd54] | 189 | async_manager();
|
---|
[51c1b003] | 190 |
|
---|
[f89979b] | 191 | /* Not reached. */
|
---|
[153a209] | 192 | return 0;
|
---|
[51d6f80] | 193 | }
|
---|
[ce5bcb4] | 194 |
|
---|
| 195 | /**
|
---|
| 196 | * @}
|
---|
| 197 | */
|
---|