source: mainline/uspace/srv/hid/input/input.c@ 3e1bc35

Last change on this file since 3e1bc35 was cb9313e, checked in by Matthieu Riolo <matthieu.riolo@…>, 6 years ago

Adding a configuration flag which allows to define which is
the default keyboard layout

This commit adds a new configuration flag which defines the
default keyboard layout. This layout will be hardcoded into
the system. Changing to this keyboard layout can be done by
pressing CTRL+F1.

  • Property mode set to 100644
File size: 18.4 KB
RevLine 
[51d6f80]1/*
[df4ed85]2 * Copyright (c) 2006 Josef Cejka
[9be360ee]3 * Copyright (c) 2011 Jiri Svoboda
[51d6f80]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
[231a60a]30/**
[5f88293]31 * @addtogroup inputgen generic
32 * @brief HelenOS input server.
33 * @ingroup input
[ce5bcb4]34 * @{
[215abc1]35 */
[ce5bcb4]36/** @file
37 */
38
[74017ce]39#include <adt/fifo.h>
[9be360ee]40#include <adt/list.h>
[fa09449]41#include <async.h>
[74017ce]42#include <config.h>
[51d6f80]43#include <errno.h>
[74017ce]44#include <fibril.h>
45#include <fibril_synch.h>
46#include <io/chardev.h>
[215abc1]47#include <io/console.h>
48#include <io/keycode.h>
[74017ce]49#include <ipc/services.h>
50#include <ipc/input.h>
[15f3c3f]51#include <loc.h>
[74017ce]52#include <ns.h>
53#include <stdbool.h>
54#include <stdio.h>
55#include <stdlib.h>
[1d6dd2a]56#include <str.h>
[593e023]57#include <str_error.h>
[74017ce]58
59#include "input.h"
[b6a088f]60#include "kbd.h"
61#include "kbd_port.h"
62#include "kbd_ctl.h"
[74017ce]63#include "layout.h"
[b6a088f]64#include "mouse.h"
65#include "mouse_proto.h"
[2a72d9f]66#include "serial.h"
[51d6f80]67
[593e023]68typedef struct {
69 /** Link into the list of clients */
70 link_t link;
[a35b458]71
[593e023]72 /** Indicate whether the client is active */
73 bool active;
[a35b458]74
[593e023]75 /** Client callback session */
76 async_sess_t *sess;
77} client_t;
[9be360ee]78
[593e023]79/** List of clients */
80static list_t clients;
81static client_t *active_client = NULL;
[d1eece6]82
[380e23a3]83/** Kernel override */
84static bool active = true;
85
[a79b42a]86/** Serial console specified by the user */
87static char *serial_console;
88
[9be360ee]89/** List of keyboard devices */
[b72efe8]90static list_t kbd_devs;
[b1bdc7a4]91
[854eddd6]92/** List of mouse devices */
[1875a0c]93static list_t mouse_devs;
[854eddd6]94
[2a72d9f]95/** List of serial devices */
96static list_t serial_devs;
97
[10a5479d]98static FIBRIL_MUTEX_INITIALIZE(discovery_lock);
99
[593e023]100static void *client_data_create(void)
101{
102 client_t *client = (client_t *) calloc(1, sizeof(client_t));
103 if (client == NULL)
104 return NULL;
[a35b458]105
[593e023]106 link_initialize(&client->link);
107 client->active = false;
108 client->sess = NULL;
[a35b458]109
[593e023]110 list_append(&client->link, &clients);
[a35b458]111
[593e023]112 return client;
113}
114
115static void client_data_destroy(void *data)
116{
117 client_t *client = (client_t *) data;
[a35b458]118
[593e023]119 list_remove(&client->link);
120 free(client);
121}
122
[1875a0c]123void kbd_push_data(kbd_dev_t *kdev, sysarg_t data)
124{
125 (*kdev->ctl_ops->parse)(data);
126}
[0175246]127
[1875a0c]128void mouse_push_data(mouse_dev_t *mdev, sysarg_t data)
[f89979b]129{
[1875a0c]130 (*mdev->proto_ops->parse)(data);
[f89979b]131}
132
[1875a0c]133void kbd_push_event(kbd_dev_t *kdev, int type, unsigned int key)
[085bd54]134{
[79ae36dd]135 kbd_event_t ev;
[1875a0c]136 unsigned int mod_mask;
[a35b458]137
[d1eece6]138 switch (key) {
[ce3efa0]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;
[d1eece6]159 }
[a35b458]160
[d1eece6]161 if (mod_mask != 0) {
[215abc1]162 if (type == KEY_PRESS)
[2f7a564]163 kdev->mods = kdev->mods | mod_mask;
[d1eece6]164 else
[2f7a564]165 kdev->mods = kdev->mods & ~mod_mask;
[d1eece6]166 }
[a35b458]167
[d1eece6]168 switch (key) {
[ce3efa0]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;
[d1eece6]180 }
[a35b458]181
[12b6796]182 if (mod_mask != 0) {
[215abc1]183 if (type == KEY_PRESS) {
[12b6796]184 /*
185 * Only change lock state on transition from released
186 * to pressed. This prevents autorepeat from messing
187 * up the lock state.
188 */
[2f7a564]189 kdev->mods = kdev->mods ^ (mod_mask & ~kdev->lock_keys);
190 kdev->lock_keys = kdev->lock_keys | mod_mask;
[a35b458]191
[c145bc2]192 /* Update keyboard lock indicator lights. */
[2f7a564]193 (*kdev->ctl_ops->set_ind)(kdev, kdev->mods);
[12b6796]194 } else {
[2f7a564]195 kdev->lock_keys = kdev->lock_keys & ~mod_mask;
[12b6796]196 }
197 }
[a35b458]198
[cb9313e]199 if ((type == KEY_PRESS) && (kdev->mods & KM_LCTRL) && (key == KC_F1)) {
[c928bb7]200 layout_destroy(kdev->active_layout);
[cb9313e]201 kdev->active_layout = layout_create(&layout_default);
[c928bb7]202 return;
203 }
[a35b458]204
[f89979b]205 ev.type = type;
206 ev.key = key;
[2f7a564]207 ev.mods = kdev->mods;
[a35b458]208
[2f7a564]209 ev.c = layout_parse_ev(kdev->active_layout, &ev);
[a35b458]210
[593e023]211 list_foreach(clients, link, client_t, client) {
212 if (client->active) {
213 async_exch_t *exch = async_exchange_begin(client->sess);
214 async_msg_4(exch, INPUT_EVENT_KEY, ev.type, ev.key, ev.mods, ev.c);
215 async_exchange_end(exch);
216 }
217 }
[854eddd6]218}
219
[593e023]220/** Mouse pointer has moved (relative mode). */
[edb3cf2]221void mouse_push_event_move(mouse_dev_t *mdev, int dx, int dy, int dz)
[854eddd6]222{
[593e023]223 list_foreach(clients, link, client_t, client) {
224 if (client->active) {
225 async_exch_t *exch = async_exchange_begin(client->sess);
[a35b458]226
[593e023]227 if ((dx) || (dy))
228 async_msg_2(exch, INPUT_EVENT_MOVE, dx, dy);
[a35b458]229
[593e023]230 if (dz) {
231 // TODO: Implement proper wheel support
232 keycode_t code = dz > 0 ? KC_UP : KC_DOWN;
[a35b458]233
[593e023]234 for (unsigned int i = 0; i < 3; i++)
235 async_msg_4(exch, INPUT_EVENT_KEY, KEY_PRESS, code, 0, 0);
[a35b458]236
[593e023]237 async_msg_4(exch, INPUT_EVENT_KEY, KEY_RELEASE, code, 0, 0);
238 }
[a35b458]239
[593e023]240 async_exchange_end(exch);
[edb3cf2]241 }
242 }
[854eddd6]243}
244
[593e023]245/** Mouse pointer has moved (absolute mode). */
[8a99c7e]246void mouse_push_event_abs_move(mouse_dev_t *mdev, unsigned int x, unsigned int y,
247 unsigned int max_x, unsigned int max_y)
248{
[593e023]249 list_foreach(clients, link, client_t, client) {
250 if (client->active) {
251 if ((max_x) && (max_y)) {
252 async_exch_t *exch = async_exchange_begin(client->sess);
253 async_msg_4(exch, INPUT_EVENT_ABS_MOVE, x, y, max_x, max_y);
254 async_exchange_end(exch);
255 }
256 }
[8a99c7e]257 }
258}
259
[854eddd6]260/** Mouse button has been pressed. */
[1875a0c]261void mouse_push_event_button(mouse_dev_t *mdev, int bnum, int press)
[854eddd6]262{
[593e023]263 list_foreach(clients, link, client_t, client) {
264 if (client->active) {
265 async_exch_t *exch = async_exchange_begin(client->sess);
266 async_msg_2(exch, INPUT_EVENT_BUTTON, bnum, press);
267 async_exchange_end(exch);
268 }
269 }
[085bd54]270}
271
[593e023]272/** Arbitrate client actiovation */
[380e23a3]273static void client_arbitration(void)
[593e023]274{
275 /* Mutual exclusion of active clients */
276 list_foreach(clients, link, client_t, client)
[774aa332]277 client->active = ((active) && (client == active_client));
[a35b458]278
[593e023]279 /* Notify clients about the arbitration */
280 list_foreach(clients, link, client_t, client) {
281 async_exch_t *exch = async_exchange_begin(client->sess);
282 async_msg_0(exch, client->active ?
283 INPUT_EVENT_ACTIVE : INPUT_EVENT_DEACTIVE);
284 async_exchange_end(exch);
285 }
286}
287
288/** New client connection */
[984a9ba]289static void client_connection(ipc_call_t *icall, void *arg)
[085bd54]290{
[593e023]291 client_t *client = (client_t *) async_get_client_data();
292 if (client == NULL) {
[984a9ba]293 async_answer_0(icall, ENOMEM);
[593e023]294 return;
295 }
[a35b458]296
[beb83c1]297 async_accept_0(icall);
[a35b458]298
[79ae36dd]299 while (true) {
[5da7199]300 ipc_call_t call;
[984a9ba]301 async_get_call(&call);
[a35b458]302
[fafb8e5]303 if (!ipc_get_imethod(&call)) {
[593e023]304 if (client->sess != NULL) {
305 async_hangup(client->sess);
306 client->sess = NULL;
[47a350f]307 }
[a35b458]308
[984a9ba]309 async_answer_0(&call, EOK);
[085bd54]310 return;
[79ae36dd]311 }
[a35b458]312
[5da7199]313 async_sess_t *sess =
314 async_callback_receive_start(EXCHANGE_SERIALIZE, &call);
315 if (sess != NULL) {
[593e023]316 if (client->sess == NULL) {
317 client->sess = sess;
[984a9ba]318 async_answer_0(&call, EOK);
[5da7199]319 } else
[984a9ba]320 async_answer_0(&call, ELIMIT);
[5da7199]321 } else {
[fafb8e5]322 switch (ipc_get_imethod(&call)) {
[593e023]323 case INPUT_ACTIVATE:
324 active_client = client;
[380e23a3]325 client_arbitration();
[984a9ba]326 async_answer_0(&call, EOK);
[085bd54]327 break;
[5da7199]328 default:
[984a9ba]329 async_answer_0(&call, EINVAL);
[085bd54]330 }
331 }
[1875a0c]332 }
[085bd54]333}
334
[01c3bb4]335static void kconsole_event_handler(ipc_call_t *call, void *arg)
[593e023]336{
[fafb8e5]337 if (ipc_get_arg1(call)) {
[593e023]338 /* Kernel console activated */
[380e23a3]339 active = false;
[593e023]340 } else {
341 /* Kernel console deactivated */
[380e23a3]342 active = true;
[593e023]343 }
[a35b458]344
[380e23a3]345 client_arbitration();
[593e023]346}
347
[2f7a564]348static kbd_dev_t *kbd_dev_new(void)
[b1bdc7a4]349{
[1875a0c]350 kbd_dev_t *kdev = calloc(1, sizeof(kbd_dev_t));
[9be360ee]351 if (kdev == NULL) {
[1875a0c]352 printf("%s: Error allocating keyboard device. "
353 "Out of memory.\n", NAME);
[2f7a564]354 return NULL;
[9be360ee]355 }
[a35b458]356
[593e023]357 link_initialize(&kdev->link);
[a35b458]358
[2f7a564]359 kdev->mods = KM_NUM_LOCK;
360 kdev->lock_keys = 0;
[cb9313e]361 kdev->active_layout = layout_create(&layout_default);
[a35b458]362
[2f7a564]363 return kdev;
364}
365
[1875a0c]366static mouse_dev_t *mouse_dev_new(void)
367{
368 mouse_dev_t *mdev = calloc(1, sizeof(mouse_dev_t));
369 if (mdev == NULL) {
[2a72d9f]370 printf("%s: Error allocating mouse device. "
[1875a0c]371 "Out of memory.\n", NAME);
372 return NULL;
373 }
[a35b458]374
[593e023]375 link_initialize(&mdev->link);
[a35b458]376
[1875a0c]377 return mdev;
378}
379
[2a72d9f]380static serial_dev_t *serial_dev_new(void)
381{
382 serial_dev_t *sdev = calloc(1, sizeof(serial_dev_t));
383 if (sdev == NULL) {
384 printf("%s: Error allocating serial device. "
385 "Out of memory.\n", NAME);
386 return NULL;
387 }
[a35b458]388
[2a72d9f]389 sdev->kdev = kbd_dev_new();
390 if (sdev->kdev == NULL) {
391 free(sdev);
392 return NULL;
393 }
394
395 link_initialize(&sdev->link);
[a35b458]396
[2a72d9f]397 return sdev;
398}
399
[2f7a564]400/** Add new legacy keyboard device. */
401static void kbd_add_dev(kbd_port_ops_t *port, kbd_ctl_ops_t *ctl)
402{
[1875a0c]403 kbd_dev_t *kdev = kbd_dev_new();
[2f7a564]404 if (kdev == NULL)
405 return;
[a35b458]406
[9be360ee]407 kdev->port_ops = port;
408 kdev->ctl_ops = ctl;
[cce8a83]409 kdev->svc_id = 0;
[a35b458]410
[9be360ee]411 /* Initialize port driver. */
[af897ff0]412 if ((*kdev->port_ops->init)(kdev) != 0)
413 goto fail;
[a35b458]414
[9be360ee]415 /* Initialize controller driver. */
416 if ((*kdev->ctl_ops->init)(kdev) != 0) {
417 /* XXX Uninit port */
418 goto fail;
419 }
[a35b458]420
[593e023]421 list_append(&kdev->link, &kbd_devs);
[9be360ee]422 return;
[a35b458]423
[9be360ee]424fail:
425 free(kdev);
426}
427
[af897ff0]428/** Add new kbdev device.
429 *
[593e023]430 * @param service_id Service ID of the keyboard device
[1875a0c]431 *
[af897ff0]432 */
[cce8a83]433static int kbd_add_kbdev(service_id_t service_id, kbd_dev_t **kdevp)
[af897ff0]434{
[1875a0c]435 kbd_dev_t *kdev = kbd_dev_new();
[2f7a564]436 if (kdev == NULL)
[af897ff0]437 return -1;
[a35b458]438
[cce8a83]439 kdev->svc_id = service_id;
[af897ff0]440 kdev->port_ops = NULL;
441 kdev->ctl_ops = &kbdev_ctl;
[a35b458]442
[b7fd2a0]443 errno_t rc = loc_service_get_name(service_id, &kdev->svc_name);
[cce8a83]444 if (rc != EOK) {
445 kdev->svc_name = NULL;
446 goto fail;
447 }
[a35b458]448
[af897ff0]449 /* Initialize controller driver. */
450 if ((*kdev->ctl_ops->init)(kdev) != 0) {
451 goto fail;
452 }
[a35b458]453
[593e023]454 list_append(&kdev->link, &kbd_devs);
[cce8a83]455 *kdevp = kdev;
[d5c1051]456 return 0;
[a35b458]457
[af897ff0]458fail:
[cce8a83]459 if (kdev->svc_name != NULL)
460 free(kdev->svc_name);
[af897ff0]461 free(kdev);
462 return -1;
463}
464
[1875a0c]465/** Add new mousedev device.
466 *
[593e023]467 * @param service_id Service ID of the mouse device
[1875a0c]468 *
469 */
[cce8a83]470static int mouse_add_mousedev(service_id_t service_id, mouse_dev_t **mdevp)
[1875a0c]471{
472 mouse_dev_t *mdev = mouse_dev_new();
473 if (mdev == NULL)
474 return -1;
[a35b458]475
[cce8a83]476 mdev->svc_id = service_id;
[1875a0c]477 mdev->port_ops = NULL;
478 mdev->proto_ops = &mousedev_proto;
[a35b458]479
[b7fd2a0]480 errno_t rc = loc_service_get_name(service_id, &mdev->svc_name);
[cce8a83]481 if (rc != EOK) {
482 mdev->svc_name = NULL;
483 goto fail;
484 }
[a35b458]485
[1875a0c]486 /* Initialize controller driver. */
487 if ((*mdev->proto_ops->init)(mdev) != 0) {
488 goto fail;
489 }
[a35b458]490
[593e023]491 list_append(&mdev->link, &mouse_devs);
[cce8a83]492 *mdevp = mdev;
[d5c1051]493 return 0;
[a35b458]494
[1875a0c]495fail:
496 free(mdev);
497 return -1;
498}
499
[b7fd2a0]500static errno_t serial_consumer(void *arg)
[2a72d9f]501{
502 serial_dev_t *sdev = (serial_dev_t *) arg;
503
504 while (true) {
505 uint8_t data;
[74017ce]506 size_t nread;
[2a72d9f]507
[f2d88f3]508 chardev_read(sdev->chardev, &data, sizeof(data), &nread,
509 chardev_f_none);
[74017ce]510 /* XXX Handle error */
[2a72d9f]511 kbd_push_data(sdev->kdev, data);
512 }
513
514 return EOK;
515}
516
517/** Add new serial console device.
518 *
519 * @param service_id Service ID of the chardev device
520 *
521 */
522static int serial_add_srldev(service_id_t service_id, serial_dev_t **sdevp)
523{
[a79b42a]524 bool match = false;
[b7fd2a0]525 errno_t rc;
[a79b42a]526
[2a72d9f]527 serial_dev_t *sdev = serial_dev_new();
528 if (sdev == NULL)
529 return -1;
[a35b458]530
[2a72d9f]531 sdev->kdev->svc_id = service_id;
[a35b458]532
[74017ce]533 rc = loc_service_get_name(service_id, &sdev->kdev->svc_name);
[a79b42a]534 if (rc != EOK)
[2a72d9f]535 goto fail;
536
537 list_append(&sdev->link, &serial_devs);
538
[a79b42a]539 /*
540 * Is this the device the user wants to use as a serial console?
541 */
542 match = (serial_console != NULL) &&
543 !str_cmp(serial_console, sdev->kdev->svc_name);
544
545 if (match) {
546 sdev->kdev->ctl_ops = &stty_ctl;
547
548 /* Initialize controller driver. */
549 if ((*sdev->kdev->ctl_ops->init)(sdev->kdev) != 0) {
550 list_remove(&sdev->link);
551 goto fail;
552 }
553
554 sdev->sess = loc_service_connect(service_id, INTERFACE_DDF,
555 IPC_FLAG_BLOCKING);
556
[74017ce]557 rc = chardev_open(sdev->sess, &sdev->chardev);
558 if (rc != EOK) {
559 async_hangup(sdev->sess);
560 sdev->sess = NULL;
561 list_remove(&sdev->link);
562 goto fail;
563 }
564
[a79b42a]565 fid_t fid = fibril_create(serial_consumer, sdev);
566 fibril_add_ready(fid);
567 }
[a35b458]568
[2a72d9f]569 *sdevp = sdev;
[d5c1051]570 return 0;
[a35b458]571
[2a72d9f]572fail:
573 if (sdev->kdev->svc_name != NULL)
574 free(sdev->kdev->svc_name);
575 free(sdev->kdev);
576 free(sdev);
577 return -1;
578}
579
[9be360ee]580/** Add legacy drivers/devices. */
581static void kbd_add_legacy_devs(void)
582{
583 /*
584 * Need to add these drivers based on config unless we can probe
585 * them automatically.
586 */
587#if defined(UARCH_arm32) && defined(MACHINE_gta02)
588 kbd_add_dev(&chardev_port, &stty_ctl);
589#endif
[6d15572]590#if defined(UARCH_ia64) && defined(MACHINE_ski)
591 kbd_add_dev(&chardev_port, &stty_ctl);
[9be360ee]592#endif
593#if defined(MACHINE_msim)
[676e833]594 kbd_add_dev(&chardev_port, &stty_ctl);
[9be360ee]595#endif
596#if defined(UARCH_sparc64) && defined(PROCESSOR_sun4v)
[7aa94304]597 kbd_add_dev(&chardev_port, &stty_ctl);
[7348c4b]598#endif
599#if defined(UARCH_arm64) && defined(MACHINE_virt)
600 kbd_add_dev(&chardev_port, &stty_ctl);
[9be360ee]601#endif
[af897ff0]602 /* Silence warning on abs32le about kbd_add_dev() being unused */
603 (void) kbd_add_dev;
[9be360ee]604}
605
[b7fd2a0]606static errno_t dev_check_new_kbdevs(void)
[af897ff0]607{
[12f9f0d0]608 category_id_t keyboard_cat;
[cc574511]609 service_id_t *svcs;
610 size_t count, i;
611 bool already_known;
[b7fd2a0]612 errno_t rc;
[a35b458]613
[cc574511]614 rc = loc_category_get_id("keyboard", &keyboard_cat, IPC_FLAG_BLOCKING);
615 if (rc != EOK) {
616 printf("%s: Failed resolving category 'keyboard'.\n", NAME);
617 return ENOENT;
618 }
[a35b458]619
[12f9f0d0]620 /*
621 * Check for new keyboard devices
622 */
623 rc = loc_category_get_svcs(keyboard_cat, &svcs, &count);
624 if (rc != EOK) {
625 printf("%s: Failed getting list of keyboard devices.\n",
626 NAME);
627 return EIO;
628 }
629
630 for (i = 0; i < count; i++) {
631 already_known = false;
[a35b458]632
[12f9f0d0]633 /* Determine whether we already know this device. */
[593e023]634 list_foreach(kbd_devs, link, kbd_dev_t, kdev) {
[12f9f0d0]635 if (kdev->svc_id == svcs[i]) {
636 already_known = true;
637 break;
638 }
639 }
[a35b458]640
[12f9f0d0]641 if (!already_known) {
642 kbd_dev_t *kdev;
[d5c1051]643 if (kbd_add_kbdev(svcs[i], &kdev) == 0) {
[12f9f0d0]644 printf("%s: Connected keyboard device '%s'\n",
645 NAME, kdev->svc_name);
646 }
647 }
648 }
[a35b458]649
[99ac5cf]650 free(svcs);
[a35b458]651
[12f9f0d0]652 /* XXX Handle device removal */
[a35b458]653
[12f9f0d0]654 return EOK;
655}
656
[b7fd2a0]657static errno_t dev_check_new_mousedevs(void)
[12f9f0d0]658{
659 category_id_t mouse_cat;
660 service_id_t *svcs;
661 size_t count, i;
662 bool already_known;
[b7fd2a0]663 errno_t rc;
[a35b458]664
[cc574511]665 rc = loc_category_get_id("mouse", &mouse_cat, IPC_FLAG_BLOCKING);
666 if (rc != EOK) {
667 printf("%s: Failed resolving category 'mouse'.\n", NAME);
668 return ENOENT;
669 }
[a35b458]670
[12f9f0d0]671 /*
672 * Check for new mouse devices
673 */
674 rc = loc_category_get_svcs(mouse_cat, &svcs, &count);
675 if (rc != EOK) {
676 printf("%s: Failed getting list of mouse devices.\n",
677 NAME);
678 return EIO;
679 }
[a35b458]680
[12f9f0d0]681 for (i = 0; i < count; i++) {
682 already_known = false;
[a35b458]683
[12f9f0d0]684 /* Determine whether we already know this device. */
[593e023]685 list_foreach(mouse_devs, link, mouse_dev_t, mdev) {
[12f9f0d0]686 if (mdev->svc_id == svcs[i]) {
687 already_known = true;
688 break;
[cc574511]689 }
[854eddd6]690 }
[a35b458]691
[12f9f0d0]692 if (!already_known) {
693 mouse_dev_t *mdev;
[d5c1051]694 if (mouse_add_mousedev(svcs[i], &mdev) == 0) {
[12f9f0d0]695 printf("%s: Connected mouse device '%s'\n",
696 NAME, mdev->svc_name);
[cc574511]697 }
[af897ff0]698 }
699 }
[a35b458]700
[99ac5cf]701 free(svcs);
[a35b458]702
[12f9f0d0]703 /* XXX Handle device removal */
[a35b458]704
[af897ff0]705 return EOK;
706}
707
[b7fd2a0]708static errno_t dev_check_new_serialdevs(void)
[2a72d9f]709{
710 category_id_t serial_cat;
711 service_id_t *svcs;
712 size_t count, i;
713 bool already_known;
[b7fd2a0]714 errno_t rc;
[a35b458]715
[2a72d9f]716 rc = loc_category_get_id("serial", &serial_cat, IPC_FLAG_BLOCKING);
717 if (rc != EOK) {
718 printf("%s: Failed resolving category 'serial'.\n", NAME);
719 return ENOENT;
720 }
[a35b458]721
[2a72d9f]722 /*
723 * Check for new serial devices
724 */
725 rc = loc_category_get_svcs(serial_cat, &svcs, &count);
726 if (rc != EOK) {
727 printf("%s: Failed getting list of serial devices.\n",
728 NAME);
729 return EIO;
730 }
731
732 for (i = 0; i < count; i++) {
733 already_known = false;
[a35b458]734
[2a72d9f]735 /* Determine whether we already know this device. */
736 list_foreach(serial_devs, link, serial_dev_t, sdev) {
737 if (sdev->kdev->svc_id == svcs[i]) {
738 already_known = true;
739 break;
740 }
741 }
[a35b458]742
[2a72d9f]743 if (!already_known) {
744 serial_dev_t *sdev;
[d5c1051]745 if (serial_add_srldev(svcs[i], &sdev) == 0) {
[2a72d9f]746 printf("%s: Connected serial device '%s'\n",
747 NAME, sdev->kdev->svc_name);
748 }
749 }
750 }
[a35b458]751
[2a72d9f]752 free(svcs);
[a35b458]753
[2a72d9f]754 /* XXX Handle device removal */
[a35b458]755
[2a72d9f]756 return EOK;
757}
758
[b7fd2a0]759static errno_t dev_check_new(void)
[af897ff0]760{
[b7fd2a0]761 errno_t rc;
[a35b458]762
[10a5479d]763 fibril_mutex_lock(&discovery_lock);
[a35b458]764
[73d8600]765 if (!serial_console) {
766 rc = dev_check_new_kbdevs();
767 if (rc != EOK) {
768 fibril_mutex_unlock(&discovery_lock);
769 return rc;
770 }
[a35b458]771
[73d8600]772 rc = dev_check_new_mousedevs();
773 if (rc != EOK) {
774 fibril_mutex_unlock(&discovery_lock);
775 return rc;
776 }
777 } else {
778 rc = dev_check_new_serialdevs();
779 if (rc != EOK) {
780 fibril_mutex_unlock(&discovery_lock);
781 return rc;
782 }
[2a72d9f]783 }
[a35b458]784
[10a5479d]785 fibril_mutex_unlock(&discovery_lock);
[a35b458]786
[12f9f0d0]787 return EOK;
788}
789
[e89a06a]790static void cat_change_cb(void *arg)
[12f9f0d0]791{
792 dev_check_new();
793}
794
795/** Start listening for new devices. */
[b7fd2a0]796static errno_t input_start_dev_discovery(void)
[12f9f0d0]797{
[e89a06a]798 errno_t rc = loc_register_cat_change_cb(cat_change_cb, NULL);
[12f9f0d0]799 if (rc != EOK) {
[dd8ab1c]800 printf("%s: Failed registering callback for device discovery: "
801 "%s\n", NAME, str_error(rc));
[12f9f0d0]802 return rc;
803 }
[a35b458]804
[12f9f0d0]805 return dev_check_new();
[af897ff0]806}
807
[b6a088f]808static void usage(char *name)
809{
810 printf("Usage: %s <service_name>\n", name);
811}
812
[51d6f80]813int main(int argc, char **argv)
814{
[b7fd2a0]815 errno_t rc;
[a79b42a]816
[b6a088f]817 if (argc < 2) {
818 usage(argv[0]);
819 return 1;
820 }
[a35b458]821
[5f88293]822 printf("%s: HelenOS input service\n", NAME);
[a35b458]823
[593e023]824 list_initialize(&clients);
[9be360ee]825 list_initialize(&kbd_devs);
[854eddd6]826 list_initialize(&mouse_devs);
[2a72d9f]827 list_initialize(&serial_devs);
[a35b458]828
[03e0beaf]829 serial_console = config_get_value("console");
[a35b458]830
[854eddd6]831 /* Add legacy keyboard devices. */
[9be360ee]832 kbd_add_legacy_devs();
[a35b458]833
[47a350f]834 /* Register driver */
[593e023]835 async_set_client_data_constructor(client_data_create);
836 async_set_client_data_destructor(client_data_destroy);
[b688fd8]837 async_set_fallback_port_handler(client_connection, NULL);
[a35b458]838
[a79b42a]839 rc = loc_server_register(NAME);
[3123d2a]840 if (rc != EOK) {
841 printf("%s: Unable to register server\n", NAME);
842 return rc;
[47a350f]843 }
[a35b458]844
[15f3c3f]845 service_id_t service_id;
[b6a088f]846 rc = loc_service_register(argv[1], &service_id);
[3123d2a]847 if (rc != EOK) {
[b6a088f]848 printf("%s: Unable to register service %s\n", NAME, argv[1]);
[3123d2a]849 return rc;
[47a350f]850 }
[a35b458]851
[593e023]852 /* Receive kernel notifications */
[8820544]853 rc = async_event_subscribe(EVENT_KCONSOLE, kconsole_event_handler, NULL);
[593e023]854 if (rc != EOK)
855 printf("%s: Failed to register kconsole notifications (%s)\n",
856 NAME, str_error(rc));
[a35b458]857
[854eddd6]858 /* Start looking for new input devices */
859 input_start_dev_discovery();
[a35b458]860
[1875a0c]861 printf("%s: Accepting connections\n", NAME);
[b6a088f]862 task_retval(0);
[085bd54]863 async_manager();
[a35b458]864
[f89979b]865 /* Not reached. */
[153a209]866 return 0;
[51d6f80]867}
[ce5bcb4]868
869/**
870 * @}
[5f88293]871 */
Note: See TracBrowser for help on using the repository browser.