source: mainline/uspace/srv/hid/input/input.c@ 8edec53

serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 8edec53 was 8edec53, checked in by Jiri Svoboda <jiri@…>, 4 years ago

Support double-click

Needed to open Navigator entries using the mouse.

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