1 | /*
|
---|
2 | * Copyright (c) 2006 Josef Cejka
|
---|
3 | * Copyright (c) 2011 Jiri Svoboda
|
---|
4 | * All rights reserved.
|
---|
5 | *
|
---|
6 | * Redistribution and use in source and binary forms, with or without
|
---|
7 | * modification, are permitted provided that the following conditions
|
---|
8 | * are met:
|
---|
9 | *
|
---|
10 | * - Redistributions of source code must retain the above copyright
|
---|
11 | * notice, this list of conditions and the following disclaimer.
|
---|
12 | * - Redistributions in binary form must reproduce the above copyright
|
---|
13 | * notice, this list of conditions and the following disclaimer in the
|
---|
14 | * documentation and/or other materials provided with the distribution.
|
---|
15 | * - The name of the author may not be used to endorse or promote products
|
---|
16 | * derived from this software without specific prior written permission.
|
---|
17 | *
|
---|
18 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
19 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
20 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
21 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
22 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
23 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
24 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
25 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
27 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
28 | */
|
---|
29 |
|
---|
30 | /**
|
---|
31 | * @addtogroup inputgen generic
|
---|
32 | * @brief HelenOS input server.
|
---|
33 | * @ingroup input
|
---|
34 | * @{
|
---|
35 | */
|
---|
36 | /** @file
|
---|
37 | */
|
---|
38 |
|
---|
39 | #include <adt/list.h>
|
---|
40 | #include <stdbool.h>
|
---|
41 | #include <fibril_synch.h>
|
---|
42 | #include <ipc/services.h>
|
---|
43 | #include <ipc/input.h>
|
---|
44 | #include <sysinfo.h>
|
---|
45 | #include <stdio.h>
|
---|
46 | #include <unistd.h>
|
---|
47 | #include <stdlib.h>
|
---|
48 | #include <stdio.h>
|
---|
49 | #include <ns.h>
|
---|
50 | #include <async.h>
|
---|
51 | #include <errno.h>
|
---|
52 | #include <adt/fifo.h>
|
---|
53 | #include <io/console.h>
|
---|
54 | #include <io/keycode.h>
|
---|
55 | #include <loc.h>
|
---|
56 | #include <str_error.h>
|
---|
57 | #include "layout.h"
|
---|
58 | #include "kbd.h"
|
---|
59 | #include "kbd_port.h"
|
---|
60 | #include "kbd_ctl.h"
|
---|
61 | #include "mouse.h"
|
---|
62 | #include "mouse_proto.h"
|
---|
63 | #include "input.h"
|
---|
64 |
|
---|
65 | bool irc_service = false;
|
---|
66 | async_sess_t *irc_sess = NULL;
|
---|
67 |
|
---|
68 | #define NUM_LAYOUTS 4
|
---|
69 |
|
---|
70 | static layout_ops_t *layout[NUM_LAYOUTS] = {
|
---|
71 | &us_qwerty_ops,
|
---|
72 | &us_dvorak_ops,
|
---|
73 | &cz_ops,
|
---|
74 | &ar_ops
|
---|
75 | };
|
---|
76 |
|
---|
77 | typedef struct {
|
---|
78 | /** Link into the list of clients */
|
---|
79 | link_t link;
|
---|
80 |
|
---|
81 | /** Indicate whether the client is active */
|
---|
82 | bool active;
|
---|
83 |
|
---|
84 | /** Client callback session */
|
---|
85 | async_sess_t *sess;
|
---|
86 | } client_t;
|
---|
87 |
|
---|
88 | /** List of clients */
|
---|
89 | static list_t clients;
|
---|
90 | static client_t *active_client = NULL;
|
---|
91 |
|
---|
92 | /** List of keyboard devices */
|
---|
93 | static list_t kbd_devs;
|
---|
94 |
|
---|
95 | /** List of mouse devices */
|
---|
96 | static list_t mouse_devs;
|
---|
97 |
|
---|
98 | static FIBRIL_MUTEX_INITIALIZE(discovery_lock);
|
---|
99 |
|
---|
100 | static void *client_data_create(void)
|
---|
101 | {
|
---|
102 | client_t *client = (client_t *) calloc(1, sizeof(client_t));
|
---|
103 | if (client == NULL)
|
---|
104 | return NULL;
|
---|
105 |
|
---|
106 | link_initialize(&client->link);
|
---|
107 | client->active = false;
|
---|
108 | client->sess = NULL;
|
---|
109 |
|
---|
110 | list_append(&client->link, &clients);
|
---|
111 |
|
---|
112 | return client;
|
---|
113 | }
|
---|
114 |
|
---|
115 | static void client_data_destroy(void *data)
|
---|
116 | {
|
---|
117 | client_t *client = (client_t *) data;
|
---|
118 |
|
---|
119 | list_remove(&client->link);
|
---|
120 | free(client);
|
---|
121 | }
|
---|
122 |
|
---|
123 | void kbd_push_data(kbd_dev_t *kdev, sysarg_t data)
|
---|
124 | {
|
---|
125 | (*kdev->ctl_ops->parse)(data);
|
---|
126 | }
|
---|
127 |
|
---|
128 | void mouse_push_data(mouse_dev_t *mdev, sysarg_t data)
|
---|
129 | {
|
---|
130 | (*mdev->proto_ops->parse)(data);
|
---|
131 | }
|
---|
132 |
|
---|
133 | void kbd_push_event(kbd_dev_t *kdev, int type, unsigned int key)
|
---|
134 | {
|
---|
135 | kbd_event_t ev;
|
---|
136 | unsigned int mod_mask;
|
---|
137 |
|
---|
138 | switch (key) {
|
---|
139 | case KC_LCTRL:
|
---|
140 | mod_mask = KM_LCTRL;
|
---|
141 | break;
|
---|
142 | case KC_RCTRL:
|
---|
143 | mod_mask = KM_RCTRL;
|
---|
144 | break;
|
---|
145 | case KC_LSHIFT:
|
---|
146 | mod_mask = KM_LSHIFT;
|
---|
147 | break;
|
---|
148 | case KC_RSHIFT:
|
---|
149 | mod_mask = KM_RSHIFT;
|
---|
150 | break;
|
---|
151 | case KC_LALT:
|
---|
152 | mod_mask = KM_LALT;
|
---|
153 | break;
|
---|
154 | case KC_RALT:
|
---|
155 | mod_mask = KM_RALT;
|
---|
156 | break;
|
---|
157 | default:
|
---|
158 | mod_mask = 0;
|
---|
159 | }
|
---|
160 |
|
---|
161 | if (mod_mask != 0) {
|
---|
162 | if (type == KEY_PRESS)
|
---|
163 | kdev->mods = kdev->mods | mod_mask;
|
---|
164 | else
|
---|
165 | kdev->mods = kdev->mods & ~mod_mask;
|
---|
166 | }
|
---|
167 |
|
---|
168 | switch (key) {
|
---|
169 | case KC_CAPS_LOCK:
|
---|
170 | mod_mask = KM_CAPS_LOCK;
|
---|
171 | break;
|
---|
172 | case KC_NUM_LOCK:
|
---|
173 | mod_mask = KM_NUM_LOCK;
|
---|
174 | break;
|
---|
175 | case KC_SCROLL_LOCK:
|
---|
176 | mod_mask = KM_SCROLL_LOCK;
|
---|
177 | break;
|
---|
178 | default:
|
---|
179 | mod_mask = 0;
|
---|
180 | }
|
---|
181 |
|
---|
182 | if (mod_mask != 0) {
|
---|
183 | if (type == KEY_PRESS) {
|
---|
184 | /*
|
---|
185 | * Only change lock state on transition from released
|
---|
186 | * to pressed. This prevents autorepeat from messing
|
---|
187 | * up the lock state.
|
---|
188 | */
|
---|
189 | kdev->mods = kdev->mods ^ (mod_mask & ~kdev->lock_keys);
|
---|
190 | kdev->lock_keys = kdev->lock_keys | mod_mask;
|
---|
191 |
|
---|
192 | /* Update keyboard lock indicator lights. */
|
---|
193 | (*kdev->ctl_ops->set_ind)(kdev, kdev->mods);
|
---|
194 | } else {
|
---|
195 | kdev->lock_keys = kdev->lock_keys & ~mod_mask;
|
---|
196 | }
|
---|
197 | }
|
---|
198 |
|
---|
199 | // TODO: More elegant layout switching
|
---|
200 |
|
---|
201 | if ((type == KEY_PRESS) && (kdev->mods & KM_LCTRL) &&
|
---|
202 | (key == KC_F1)) {
|
---|
203 | layout_destroy(kdev->active_layout);
|
---|
204 | kdev->active_layout = layout_create(layout[0]);
|
---|
205 | return;
|
---|
206 | }
|
---|
207 |
|
---|
208 | if ((type == KEY_PRESS) && (kdev->mods & KM_LCTRL) &&
|
---|
209 | (key == KC_F2)) {
|
---|
210 | layout_destroy(kdev->active_layout);
|
---|
211 | kdev->active_layout = layout_create(layout[1]);
|
---|
212 | return;
|
---|
213 | }
|
---|
214 |
|
---|
215 | if ((type == KEY_PRESS) && (kdev->mods & KM_LCTRL) &&
|
---|
216 | (key == KC_F3)) {
|
---|
217 | layout_destroy(kdev->active_layout);
|
---|
218 | kdev->active_layout = layout_create(layout[2]);
|
---|
219 | return;
|
---|
220 | }
|
---|
221 |
|
---|
222 | if ((type == KEY_PRESS) && (kdev->mods & KM_LCTRL) &&
|
---|
223 | (key == KC_F4)) {
|
---|
224 | layout_destroy(kdev->active_layout);
|
---|
225 | kdev->active_layout = layout_create(layout[3]);
|
---|
226 | return;
|
---|
227 | }
|
---|
228 |
|
---|
229 | ev.type = type;
|
---|
230 | ev.key = key;
|
---|
231 | ev.mods = kdev->mods;
|
---|
232 |
|
---|
233 | ev.c = layout_parse_ev(kdev->active_layout, &ev);
|
---|
234 |
|
---|
235 | list_foreach(clients, link, client_t, client) {
|
---|
236 | if (client->active) {
|
---|
237 | async_exch_t *exch = async_exchange_begin(client->sess);
|
---|
238 | async_msg_4(exch, INPUT_EVENT_KEY, ev.type, ev.key, ev.mods, ev.c);
|
---|
239 | async_exchange_end(exch);
|
---|
240 | }
|
---|
241 | }
|
---|
242 | }
|
---|
243 |
|
---|
244 | /** Mouse pointer has moved (relative mode). */
|
---|
245 | void mouse_push_event_move(mouse_dev_t *mdev, int dx, int dy, int dz)
|
---|
246 | {
|
---|
247 | list_foreach(clients, link, client_t, client) {
|
---|
248 | if (client->active) {
|
---|
249 | async_exch_t *exch = async_exchange_begin(client->sess);
|
---|
250 |
|
---|
251 | if ((dx) || (dy))
|
---|
252 | async_msg_2(exch, INPUT_EVENT_MOVE, dx, dy);
|
---|
253 |
|
---|
254 | if (dz) {
|
---|
255 | // TODO: Implement proper wheel support
|
---|
256 | keycode_t code = dz > 0 ? KC_UP : KC_DOWN;
|
---|
257 |
|
---|
258 | for (unsigned int i = 0; i < 3; i++)
|
---|
259 | async_msg_4(exch, INPUT_EVENT_KEY, KEY_PRESS, code, 0, 0);
|
---|
260 |
|
---|
261 | async_msg_4(exch, INPUT_EVENT_KEY, KEY_RELEASE, code, 0, 0);
|
---|
262 | }
|
---|
263 |
|
---|
264 | async_exchange_end(exch);
|
---|
265 | }
|
---|
266 | }
|
---|
267 | }
|
---|
268 |
|
---|
269 | /** Mouse pointer has moved (absolute mode). */
|
---|
270 | void mouse_push_event_abs_move(mouse_dev_t *mdev, unsigned int x, unsigned int y,
|
---|
271 | unsigned int max_x, unsigned int max_y)
|
---|
272 | {
|
---|
273 | list_foreach(clients, link, client_t, client) {
|
---|
274 | if (client->active) {
|
---|
275 | if ((max_x) && (max_y)) {
|
---|
276 | async_exch_t *exch = async_exchange_begin(client->sess);
|
---|
277 | async_msg_4(exch, INPUT_EVENT_ABS_MOVE, x, y, max_x, max_y);
|
---|
278 | async_exchange_end(exch);
|
---|
279 | }
|
---|
280 | }
|
---|
281 | }
|
---|
282 | }
|
---|
283 |
|
---|
284 | /** Mouse button has been pressed. */
|
---|
285 | void mouse_push_event_button(mouse_dev_t *mdev, int bnum, int press)
|
---|
286 | {
|
---|
287 | list_foreach(clients, link, client_t, client) {
|
---|
288 | if (client->active) {
|
---|
289 | async_exch_t *exch = async_exchange_begin(client->sess);
|
---|
290 | async_msg_2(exch, INPUT_EVENT_BUTTON, bnum, press);
|
---|
291 | async_exchange_end(exch);
|
---|
292 | }
|
---|
293 | }
|
---|
294 | }
|
---|
295 |
|
---|
296 | /** Arbitrate client actiovation */
|
---|
297 | static void client_arbitration(client_t *req)
|
---|
298 | {
|
---|
299 | /* Mutual exclusion of active clients */
|
---|
300 | list_foreach(clients, link, client_t, client)
|
---|
301 | client->active = (client == req);
|
---|
302 |
|
---|
303 | /* Notify clients about the arbitration */
|
---|
304 | list_foreach(clients, link, client_t, client) {
|
---|
305 | async_exch_t *exch = async_exchange_begin(client->sess);
|
---|
306 | async_msg_0(exch, client->active ?
|
---|
307 | INPUT_EVENT_ACTIVE : INPUT_EVENT_DEACTIVE);
|
---|
308 | async_exchange_end(exch);
|
---|
309 | }
|
---|
310 | }
|
---|
311 |
|
---|
312 | /** New client connection */
|
---|
313 | static void client_connection(ipc_callid_t iid, ipc_call_t *icall, void *arg)
|
---|
314 | {
|
---|
315 | client_t *client = (client_t *) async_get_client_data();
|
---|
316 | if (client == NULL) {
|
---|
317 | async_answer_0(iid, ENOMEM);
|
---|
318 | return;
|
---|
319 | }
|
---|
320 |
|
---|
321 | async_answer_0(iid, EOK);
|
---|
322 |
|
---|
323 | while (true) {
|
---|
324 | ipc_call_t call;
|
---|
325 | ipc_callid_t callid = async_get_call(&call);
|
---|
326 |
|
---|
327 | if (!IPC_GET_IMETHOD(call)) {
|
---|
328 | if (client->sess != NULL) {
|
---|
329 | async_hangup(client->sess);
|
---|
330 | client->sess = NULL;
|
---|
331 | }
|
---|
332 |
|
---|
333 | async_answer_0(callid, EOK);
|
---|
334 | return;
|
---|
335 | }
|
---|
336 |
|
---|
337 | async_sess_t *sess =
|
---|
338 | async_callback_receive_start(EXCHANGE_SERIALIZE, &call);
|
---|
339 | if (sess != NULL) {
|
---|
340 | if (client->sess == NULL) {
|
---|
341 | client->sess = sess;
|
---|
342 | async_answer_0(callid, EOK);
|
---|
343 | } else
|
---|
344 | async_answer_0(callid, ELIMIT);
|
---|
345 | } else {
|
---|
346 | switch (IPC_GET_IMETHOD(call)) {
|
---|
347 | case INPUT_ACTIVATE:
|
---|
348 | active_client = client;
|
---|
349 | client_arbitration(client);
|
---|
350 | async_answer_0(callid, EOK);
|
---|
351 | break;
|
---|
352 | default:
|
---|
353 | async_answer_0(callid, EINVAL);
|
---|
354 | }
|
---|
355 | }
|
---|
356 | }
|
---|
357 | }
|
---|
358 |
|
---|
359 | static void kconsole_event_handler(ipc_callid_t callid, ipc_call_t *call,
|
---|
360 | void *arg)
|
---|
361 | {
|
---|
362 | if (IPC_GET_ARG1(*call)) {
|
---|
363 | /* Kernel console activated */
|
---|
364 | client_arbitration(NULL);
|
---|
365 | } else {
|
---|
366 | /* Kernel console deactivated */
|
---|
367 | client_arbitration(active_client);
|
---|
368 | }
|
---|
369 | }
|
---|
370 |
|
---|
371 | static kbd_dev_t *kbd_dev_new(void)
|
---|
372 | {
|
---|
373 | kbd_dev_t *kdev = calloc(1, sizeof(kbd_dev_t));
|
---|
374 | if (kdev == NULL) {
|
---|
375 | printf("%s: Error allocating keyboard device. "
|
---|
376 | "Out of memory.\n", NAME);
|
---|
377 | return NULL;
|
---|
378 | }
|
---|
379 |
|
---|
380 | link_initialize(&kdev->link);
|
---|
381 |
|
---|
382 | kdev->mods = KM_NUM_LOCK;
|
---|
383 | kdev->lock_keys = 0;
|
---|
384 | kdev->active_layout = layout_create(layout[0]);
|
---|
385 |
|
---|
386 | return kdev;
|
---|
387 | }
|
---|
388 |
|
---|
389 | static mouse_dev_t *mouse_dev_new(void)
|
---|
390 | {
|
---|
391 | mouse_dev_t *mdev = calloc(1, sizeof(mouse_dev_t));
|
---|
392 | if (mdev == NULL) {
|
---|
393 | printf("%s: Error allocating keyboard device. "
|
---|
394 | "Out of memory.\n", NAME);
|
---|
395 | return NULL;
|
---|
396 | }
|
---|
397 |
|
---|
398 | link_initialize(&mdev->link);
|
---|
399 |
|
---|
400 | return mdev;
|
---|
401 | }
|
---|
402 |
|
---|
403 | /** Add new legacy keyboard device. */
|
---|
404 | static void kbd_add_dev(kbd_port_ops_t *port, kbd_ctl_ops_t *ctl)
|
---|
405 | {
|
---|
406 | kbd_dev_t *kdev = kbd_dev_new();
|
---|
407 | if (kdev == NULL)
|
---|
408 | return;
|
---|
409 |
|
---|
410 | kdev->port_ops = port;
|
---|
411 | kdev->ctl_ops = ctl;
|
---|
412 | kdev->svc_id = 0;
|
---|
413 |
|
---|
414 | /* Initialize port driver. */
|
---|
415 | if ((*kdev->port_ops->init)(kdev) != 0)
|
---|
416 | goto fail;
|
---|
417 |
|
---|
418 | /* Initialize controller driver. */
|
---|
419 | if ((*kdev->ctl_ops->init)(kdev) != 0) {
|
---|
420 | /* XXX Uninit port */
|
---|
421 | goto fail;
|
---|
422 | }
|
---|
423 |
|
---|
424 | list_append(&kdev->link, &kbd_devs);
|
---|
425 | return;
|
---|
426 |
|
---|
427 | fail:
|
---|
428 | free(kdev);
|
---|
429 | }
|
---|
430 |
|
---|
431 | /** Add new legacy mouse device. */
|
---|
432 | static void mouse_add_dev(mouse_port_ops_t *port, mouse_proto_ops_t *proto)
|
---|
433 | {
|
---|
434 | mouse_dev_t *mdev = mouse_dev_new();
|
---|
435 | if (mdev == NULL)
|
---|
436 | return;
|
---|
437 |
|
---|
438 | mdev->port_ops = port;
|
---|
439 | mdev->proto_ops = proto;
|
---|
440 | mdev->svc_id = 0;
|
---|
441 |
|
---|
442 | /* Initialize port driver. */
|
---|
443 | if ((*mdev->port_ops->init)(mdev) != 0)
|
---|
444 | goto fail;
|
---|
445 |
|
---|
446 | /* Initialize protocol driver. */
|
---|
447 | if ((*mdev->proto_ops->init)(mdev) != 0) {
|
---|
448 | /* XXX Uninit port */
|
---|
449 | goto fail;
|
---|
450 | }
|
---|
451 |
|
---|
452 | list_append(&mdev->link, &mouse_devs);
|
---|
453 | return;
|
---|
454 |
|
---|
455 | fail:
|
---|
456 | free(mdev);
|
---|
457 | }
|
---|
458 |
|
---|
459 | /** Add new kbdev device.
|
---|
460 | *
|
---|
461 | * @param service_id Service ID of the keyboard device
|
---|
462 | *
|
---|
463 | */
|
---|
464 | static int kbd_add_kbdev(service_id_t service_id, kbd_dev_t **kdevp)
|
---|
465 | {
|
---|
466 | kbd_dev_t *kdev = kbd_dev_new();
|
---|
467 | if (kdev == NULL)
|
---|
468 | return -1;
|
---|
469 |
|
---|
470 | kdev->svc_id = service_id;
|
---|
471 | kdev->port_ops = NULL;
|
---|
472 | kdev->ctl_ops = &kbdev_ctl;
|
---|
473 |
|
---|
474 | int rc = loc_service_get_name(service_id, &kdev->svc_name);
|
---|
475 | if (rc != EOK) {
|
---|
476 | kdev->svc_name = NULL;
|
---|
477 | goto fail;
|
---|
478 | }
|
---|
479 |
|
---|
480 | /* Initialize controller driver. */
|
---|
481 | if ((*kdev->ctl_ops->init)(kdev) != 0) {
|
---|
482 | goto fail;
|
---|
483 | }
|
---|
484 |
|
---|
485 | list_append(&kdev->link, &kbd_devs);
|
---|
486 | *kdevp = kdev;
|
---|
487 | return EOK;
|
---|
488 |
|
---|
489 | fail:
|
---|
490 | if (kdev->svc_name != NULL)
|
---|
491 | free(kdev->svc_name);
|
---|
492 | free(kdev);
|
---|
493 | return -1;
|
---|
494 | }
|
---|
495 |
|
---|
496 | /** Add new mousedev device.
|
---|
497 | *
|
---|
498 | * @param service_id Service ID of the mouse device
|
---|
499 | *
|
---|
500 | */
|
---|
501 | static int mouse_add_mousedev(service_id_t service_id, mouse_dev_t **mdevp)
|
---|
502 | {
|
---|
503 | mouse_dev_t *mdev = mouse_dev_new();
|
---|
504 | if (mdev == NULL)
|
---|
505 | return -1;
|
---|
506 |
|
---|
507 | mdev->svc_id = service_id;
|
---|
508 | mdev->port_ops = NULL;
|
---|
509 | mdev->proto_ops = &mousedev_proto;
|
---|
510 |
|
---|
511 | int rc = loc_service_get_name(service_id, &mdev->svc_name);
|
---|
512 | if (rc != EOK) {
|
---|
513 | mdev->svc_name = NULL;
|
---|
514 | goto fail;
|
---|
515 | }
|
---|
516 |
|
---|
517 | /* Initialize controller driver. */
|
---|
518 | if ((*mdev->proto_ops->init)(mdev) != 0) {
|
---|
519 | goto fail;
|
---|
520 | }
|
---|
521 |
|
---|
522 | list_append(&mdev->link, &mouse_devs);
|
---|
523 | *mdevp = mdev;
|
---|
524 | return EOK;
|
---|
525 |
|
---|
526 | fail:
|
---|
527 | free(mdev);
|
---|
528 | return -1;
|
---|
529 | }
|
---|
530 |
|
---|
531 | /** Add legacy drivers/devices. */
|
---|
532 | static void kbd_add_legacy_devs(void)
|
---|
533 | {
|
---|
534 | /*
|
---|
535 | * Need to add these drivers based on config unless we can probe
|
---|
536 | * them automatically.
|
---|
537 | */
|
---|
538 | #if defined(UARCH_arm32) && defined(MACHINE_gta02)
|
---|
539 | kbd_add_dev(&chardev_port, &stty_ctl);
|
---|
540 | #endif
|
---|
541 | #if defined(MACHINE_ski)
|
---|
542 | kbd_add_dev(&ski_port, &stty_ctl);
|
---|
543 | #endif
|
---|
544 | #if defined(MACHINE_msim)
|
---|
545 | kbd_add_dev(&msim_port, &stty_ctl);
|
---|
546 | #endif
|
---|
547 | #if defined(UARCH_ppc32)
|
---|
548 | kbd_add_dev(&adb_port, &apple_ctl);
|
---|
549 | #endif
|
---|
550 | #if defined(UARCH_sparc64) && defined(PROCESSOR_sun4v)
|
---|
551 | kbd_add_dev(&niagara_port, &stty_ctl);
|
---|
552 | #endif
|
---|
553 | #if defined(UARCH_sparc64) && defined(MACHINE_generic)
|
---|
554 | kbd_add_dev(&ns16550_port, &sun_ctl);
|
---|
555 | #endif
|
---|
556 | /* Silence warning on abs32le about kbd_add_dev() being unused */
|
---|
557 | (void) kbd_add_dev;
|
---|
558 | }
|
---|
559 |
|
---|
560 | /** Add legacy drivers/devices. */
|
---|
561 | static void mouse_add_legacy_devs(void)
|
---|
562 | {
|
---|
563 | /*
|
---|
564 | * Need to add these drivers based on config unless we can probe
|
---|
565 | * them automatically.
|
---|
566 | */
|
---|
567 | #if defined(UARCH_ppc32)
|
---|
568 | mouse_add_dev(&adb_mouse_port, &adb_proto);
|
---|
569 | #endif
|
---|
570 | /* Silence warning on abs32le about mouse_add_dev() being unused */
|
---|
571 | (void) mouse_add_dev;
|
---|
572 | }
|
---|
573 |
|
---|
574 | static int dev_check_new_kbdevs(void)
|
---|
575 | {
|
---|
576 | category_id_t keyboard_cat;
|
---|
577 | service_id_t *svcs;
|
---|
578 | size_t count, i;
|
---|
579 | bool already_known;
|
---|
580 | int rc;
|
---|
581 |
|
---|
582 | rc = loc_category_get_id("keyboard", &keyboard_cat, IPC_FLAG_BLOCKING);
|
---|
583 | if (rc != EOK) {
|
---|
584 | printf("%s: Failed resolving category 'keyboard'.\n", NAME);
|
---|
585 | return ENOENT;
|
---|
586 | }
|
---|
587 |
|
---|
588 | /*
|
---|
589 | * Check for new keyboard devices
|
---|
590 | */
|
---|
591 | rc = loc_category_get_svcs(keyboard_cat, &svcs, &count);
|
---|
592 | if (rc != EOK) {
|
---|
593 | printf("%s: Failed getting list of keyboard devices.\n",
|
---|
594 | NAME);
|
---|
595 | return EIO;
|
---|
596 | }
|
---|
597 |
|
---|
598 | for (i = 0; i < count; i++) {
|
---|
599 | already_known = false;
|
---|
600 |
|
---|
601 | /* Determine whether we already know this device. */
|
---|
602 | list_foreach(kbd_devs, link, kbd_dev_t, kdev) {
|
---|
603 | if (kdev->svc_id == svcs[i]) {
|
---|
604 | already_known = true;
|
---|
605 | break;
|
---|
606 | }
|
---|
607 | }
|
---|
608 |
|
---|
609 | if (!already_known) {
|
---|
610 | kbd_dev_t *kdev;
|
---|
611 | if (kbd_add_kbdev(svcs[i], &kdev) == EOK) {
|
---|
612 | printf("%s: Connected keyboard device '%s'\n",
|
---|
613 | NAME, kdev->svc_name);
|
---|
614 | }
|
---|
615 | }
|
---|
616 | }
|
---|
617 |
|
---|
618 | free(svcs);
|
---|
619 |
|
---|
620 | /* XXX Handle device removal */
|
---|
621 |
|
---|
622 | return EOK;
|
---|
623 | }
|
---|
624 |
|
---|
625 | static int dev_check_new_mousedevs(void)
|
---|
626 | {
|
---|
627 | category_id_t mouse_cat;
|
---|
628 | service_id_t *svcs;
|
---|
629 | size_t count, i;
|
---|
630 | bool already_known;
|
---|
631 | int rc;
|
---|
632 |
|
---|
633 | rc = loc_category_get_id("mouse", &mouse_cat, IPC_FLAG_BLOCKING);
|
---|
634 | if (rc != EOK) {
|
---|
635 | printf("%s: Failed resolving category 'mouse'.\n", NAME);
|
---|
636 | return ENOENT;
|
---|
637 | }
|
---|
638 |
|
---|
639 | /*
|
---|
640 | * Check for new mouse devices
|
---|
641 | */
|
---|
642 | rc = loc_category_get_svcs(mouse_cat, &svcs, &count);
|
---|
643 | if (rc != EOK) {
|
---|
644 | printf("%s: Failed getting list of mouse devices.\n",
|
---|
645 | NAME);
|
---|
646 | return EIO;
|
---|
647 | }
|
---|
648 |
|
---|
649 | for (i = 0; i < count; i++) {
|
---|
650 | already_known = false;
|
---|
651 |
|
---|
652 | /* Determine whether we already know this device. */
|
---|
653 | list_foreach(mouse_devs, link, mouse_dev_t, mdev) {
|
---|
654 | if (mdev->svc_id == svcs[i]) {
|
---|
655 | already_known = true;
|
---|
656 | break;
|
---|
657 | }
|
---|
658 | }
|
---|
659 |
|
---|
660 | if (!already_known) {
|
---|
661 | mouse_dev_t *mdev;
|
---|
662 | if (mouse_add_mousedev(svcs[i], &mdev) == EOK) {
|
---|
663 | printf("%s: Connected mouse device '%s'\n",
|
---|
664 | NAME, mdev->svc_name);
|
---|
665 | }
|
---|
666 | }
|
---|
667 | }
|
---|
668 |
|
---|
669 | free(svcs);
|
---|
670 |
|
---|
671 | /* XXX Handle device removal */
|
---|
672 |
|
---|
673 | return EOK;
|
---|
674 | }
|
---|
675 |
|
---|
676 | static int dev_check_new(void)
|
---|
677 | {
|
---|
678 | int rc;
|
---|
679 |
|
---|
680 | fibril_mutex_lock(&discovery_lock);
|
---|
681 |
|
---|
682 | rc = dev_check_new_kbdevs();
|
---|
683 | if (rc != EOK) {
|
---|
684 | fibril_mutex_unlock(&discovery_lock);
|
---|
685 | return rc;
|
---|
686 | }
|
---|
687 |
|
---|
688 | rc = dev_check_new_mousedevs();
|
---|
689 | if (rc != EOK) {
|
---|
690 | fibril_mutex_unlock(&discovery_lock);
|
---|
691 | return rc;
|
---|
692 | }
|
---|
693 |
|
---|
694 | fibril_mutex_unlock(&discovery_lock);
|
---|
695 |
|
---|
696 | return EOK;
|
---|
697 | }
|
---|
698 |
|
---|
699 | static void cat_change_cb(void)
|
---|
700 | {
|
---|
701 | dev_check_new();
|
---|
702 | }
|
---|
703 |
|
---|
704 | /** Start listening for new devices. */
|
---|
705 | static int input_start_dev_discovery(void)
|
---|
706 | {
|
---|
707 | int rc = loc_register_cat_change_cb(cat_change_cb);
|
---|
708 | if (rc != EOK) {
|
---|
709 | printf("%s: Failed registering callback for device discovery. "
|
---|
710 | "(%d)\n", NAME, rc);
|
---|
711 | return rc;
|
---|
712 | }
|
---|
713 |
|
---|
714 | return dev_check_new();
|
---|
715 | }
|
---|
716 |
|
---|
717 | static void usage(char *name)
|
---|
718 | {
|
---|
719 | printf("Usage: %s <service_name>\n", name);
|
---|
720 | }
|
---|
721 |
|
---|
722 | int main(int argc, char **argv)
|
---|
723 | {
|
---|
724 | if (argc < 2) {
|
---|
725 | usage(argv[0]);
|
---|
726 | return 1;
|
---|
727 | }
|
---|
728 |
|
---|
729 | printf("%s: HelenOS input service\n", NAME);
|
---|
730 |
|
---|
731 | sysarg_t obio;
|
---|
732 |
|
---|
733 | list_initialize(&clients);
|
---|
734 | list_initialize(&kbd_devs);
|
---|
735 | list_initialize(&mouse_devs);
|
---|
736 |
|
---|
737 | if ((sysinfo_get_value("kbd.cir.obio", &obio) == EOK) && (obio))
|
---|
738 | irc_service = true;
|
---|
739 |
|
---|
740 | if (irc_service) {
|
---|
741 | while (irc_sess == NULL)
|
---|
742 | irc_sess = service_connect_blocking(EXCHANGE_SERIALIZE,
|
---|
743 | SERVICE_IRC, 0, 0);
|
---|
744 | }
|
---|
745 |
|
---|
746 | /* Add legacy keyboard devices. */
|
---|
747 | kbd_add_legacy_devs();
|
---|
748 |
|
---|
749 | /* Add legacy mouse devices. */
|
---|
750 | mouse_add_legacy_devs();
|
---|
751 |
|
---|
752 | /* Register driver */
|
---|
753 | async_set_client_data_constructor(client_data_create);
|
---|
754 | async_set_client_data_destructor(client_data_destroy);
|
---|
755 | async_set_client_connection(client_connection);
|
---|
756 |
|
---|
757 | int rc = loc_server_register(NAME);
|
---|
758 | if (rc != EOK) {
|
---|
759 | printf("%s: Unable to register server\n", NAME);
|
---|
760 | return rc;
|
---|
761 | }
|
---|
762 |
|
---|
763 | service_id_t service_id;
|
---|
764 | rc = loc_service_register(argv[1], &service_id);
|
---|
765 | if (rc != EOK) {
|
---|
766 | printf("%s: Unable to register service %s\n", NAME, argv[1]);
|
---|
767 | return rc;
|
---|
768 | }
|
---|
769 |
|
---|
770 | /* Receive kernel notifications */
|
---|
771 | rc = async_event_subscribe(EVENT_KCONSOLE, kconsole_event_handler, NULL);
|
---|
772 | if (rc != EOK)
|
---|
773 | printf("%s: Failed to register kconsole notifications (%s)\n",
|
---|
774 | NAME, str_error(rc));
|
---|
775 |
|
---|
776 | /* Start looking for new input devices */
|
---|
777 | input_start_dev_discovery();
|
---|
778 |
|
---|
779 | printf("%s: Accepting connections\n", NAME);
|
---|
780 | task_retval(0);
|
---|
781 | async_manager();
|
---|
782 |
|
---|
783 | /* Not reached. */
|
---|
784 | return 0;
|
---|
785 | }
|
---|
786 |
|
---|
787 | /**
|
---|
788 | * @}
|
---|
789 | */
|
---|