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
RevLine 
[51d6f80]1/*
[8edec53]2 * Copyright (c) 2021 Jiri Svoboda
[df4ed85]3 * Copyright (c) 2006 Josef Cejka
[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
[453c5ce]68#define NUM_LAYOUTS 5
[1875a0c]69
70static layout_ops_t *layout[NUM_LAYOUTS] = {
71 &us_qwerty_ops,
72 &us_dvorak_ops,
[c928bb7]73 &cz_ops,
[453c5ce]74 &ar_ops,
75 &fr_azerty_ops
[1875a0c]76};
[854387b]77
[593e023]78typedef struct {
79 /** Link into the list of clients */
80 link_t link;
[a35b458]81
[593e023]82 /** Indicate whether the client is active */
83 bool active;
[a35b458]84
[593e023]85 /** Client callback session */
86 async_sess_t *sess;
87} client_t;
[9be360ee]88
[593e023]89/** List of clients */
90static list_t clients;
91static client_t *active_client = NULL;
[d1eece6]92
[380e23a3]93/** Kernel override */
94static bool active = true;
95
[a79b42a]96/** Serial console specified by the user */
97static char *serial_console;
98
[9be360ee]99/** List of keyboard devices */
[b72efe8]100static list_t kbd_devs;
[b1bdc7a4]101
[854eddd6]102/** List of mouse devices */
[1875a0c]103static list_t mouse_devs;
[854eddd6]104
[2a72d9f]105/** List of serial devices */
106static list_t serial_devs;
107
[10a5479d]108static FIBRIL_MUTEX_INITIALIZE(discovery_lock);
109
[593e023]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;
[a35b458]115
[593e023]116 link_initialize(&client->link);
117 client->active = false;
118 client->sess = NULL;
[a35b458]119
[593e023]120 list_append(&client->link, &clients);
[a35b458]121
[593e023]122 return client;
123}
124
125static void client_data_destroy(void *data)
126{
127 client_t *client = (client_t *) data;
[a35b458]128
[593e023]129 list_remove(&client->link);
130 free(client);
131}
132
[1875a0c]133void kbd_push_data(kbd_dev_t *kdev, sysarg_t data)
134{
135 (*kdev->ctl_ops->parse)(data);
136}
[0175246]137
[1875a0c]138void mouse_push_data(mouse_dev_t *mdev, sysarg_t data)
[f89979b]139{
[1875a0c]140 (*mdev->proto_ops->parse)(data);
[f89979b]141}
142
[1875a0c]143void kbd_push_event(kbd_dev_t *kdev, int type, unsigned int key)
[085bd54]144{
[79ae36dd]145 kbd_event_t ev;
[1875a0c]146 unsigned int mod_mask;
[a35b458]147
[d1eece6]148 switch (key) {
[ce3efa0]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;
[d1eece6]169 }
[a35b458]170
[d1eece6]171 if (mod_mask != 0) {
[215abc1]172 if (type == KEY_PRESS)
[2f7a564]173 kdev->mods = kdev->mods | mod_mask;
[d1eece6]174 else
[2f7a564]175 kdev->mods = kdev->mods & ~mod_mask;
[d1eece6]176 }
[a35b458]177
[d1eece6]178 switch (key) {
[ce3efa0]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;
[d1eece6]190 }
[a35b458]191
[12b6796]192 if (mod_mask != 0) {
[215abc1]193 if (type == KEY_PRESS) {
[12b6796]194 /*
195 * Only change lock state on transition from released
196 * to pressed. This prevents autorepeat from messing
197 * up the lock state.
198 */
[2f7a564]199 kdev->mods = kdev->mods ^ (mod_mask & ~kdev->lock_keys);
200 kdev->lock_keys = kdev->lock_keys | mod_mask;
[a35b458]201
[c145bc2]202 /* Update keyboard lock indicator lights. */
[2f7a564]203 (*kdev->ctl_ops->set_ind)(kdev, kdev->mods);
[12b6796]204 } else {
[2f7a564]205 kdev->lock_keys = kdev->lock_keys & ~mod_mask;
[12b6796]206 }
207 }
[a35b458]208
[ce3efa0]209 // TODO: More elegant layout switching
[a35b458]210
[453c5ce]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 }
[c928bb7]236 }
[a35b458]237
[df1a019]238 if (type == KEY_PRESS) {
239 switch (key) {
240 case KC_F12:
241 console_kcon();
242 break;
243 }
244 }
245
[f89979b]246 ev.type = type;
247 ev.key = key;
[2f7a564]248 ev.mods = kdev->mods;
[a35b458]249
[2f7a564]250 ev.c = layout_parse_ev(kdev->active_layout, &ev);
[a35b458]251
[593e023]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 }
[854eddd6]259}
260
[593e023]261/** Mouse pointer has moved (relative mode). */
[edb3cf2]262void mouse_push_event_move(mouse_dev_t *mdev, int dx, int dy, int dz)
[854eddd6]263{
[593e023]264 list_foreach(clients, link, client_t, client) {
265 if (client->active) {
266 async_exch_t *exch = async_exchange_begin(client->sess);
[a35b458]267
[593e023]268 if ((dx) || (dy))
269 async_msg_2(exch, INPUT_EVENT_MOVE, dx, dy);
[a35b458]270
[593e023]271 if (dz) {
272 // TODO: Implement proper wheel support
273 keycode_t code = dz > 0 ? KC_UP : KC_DOWN;
[a35b458]274
[593e023]275 for (unsigned int i = 0; i < 3; i++)
276 async_msg_4(exch, INPUT_EVENT_KEY, KEY_PRESS, code, 0, 0);
[a35b458]277
[593e023]278 async_msg_4(exch, INPUT_EVENT_KEY, KEY_RELEASE, code, 0, 0);
279 }
[a35b458]280
[593e023]281 async_exchange_end(exch);
[edb3cf2]282 }
283 }
[854eddd6]284}
285
[593e023]286/** Mouse pointer has moved (absolute mode). */
[8a99c7e]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{
[593e023]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 }
[8a99c7e]298 }
299}
300
[854eddd6]301/** Mouse button has been pressed. */
[1875a0c]302void mouse_push_event_button(mouse_dev_t *mdev, int bnum, int press)
[854eddd6]303{
[593e023]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 }
[085bd54]311}
312
[8edec53]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 */
[380e23a3]326static void client_arbitration(void)
[593e023]327{
328 /* Mutual exclusion of active clients */
329 list_foreach(clients, link, client_t, client)
[774aa332]330 client->active = ((active) && (client == active_client));
[a35b458]331
[593e023]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 */
[984a9ba]342static void client_connection(ipc_call_t *icall, void *arg)
[085bd54]343{
[593e023]344 client_t *client = (client_t *) async_get_client_data();
345 if (client == NULL) {
[984a9ba]346 async_answer_0(icall, ENOMEM);
[593e023]347 return;
348 }
[a35b458]349
[beb83c1]350 async_accept_0(icall);
[a35b458]351
[79ae36dd]352 while (true) {
[5da7199]353 ipc_call_t call;
[984a9ba]354 async_get_call(&call);
[a35b458]355
[fafb8e5]356 if (!ipc_get_imethod(&call)) {
[593e023]357 if (client->sess != NULL) {
358 async_hangup(client->sess);
359 client->sess = NULL;
[47a350f]360 }
[a35b458]361
[984a9ba]362 async_answer_0(&call, EOK);
[085bd54]363 return;
[79ae36dd]364 }
[a35b458]365
[5da7199]366 async_sess_t *sess =
367 async_callback_receive_start(EXCHANGE_SERIALIZE, &call);
368 if (sess != NULL) {
[593e023]369 if (client->sess == NULL) {
370 client->sess = sess;
[984a9ba]371 async_answer_0(&call, EOK);
[5da7199]372 } else
[984a9ba]373 async_answer_0(&call, ELIMIT);
[5da7199]374 } else {
[fafb8e5]375 switch (ipc_get_imethod(&call)) {
[593e023]376 case INPUT_ACTIVATE:
377 active_client = client;
[380e23a3]378 client_arbitration();
[984a9ba]379 async_answer_0(&call, EOK);
[085bd54]380 break;
[5da7199]381 default:
[984a9ba]382 async_answer_0(&call, EINVAL);
[085bd54]383 }
384 }
[1875a0c]385 }
[085bd54]386}
387
[01c3bb4]388static void kconsole_event_handler(ipc_call_t *call, void *arg)
[593e023]389{
[fafb8e5]390 if (ipc_get_arg1(call)) {
[593e023]391 /* Kernel console activated */
[380e23a3]392 active = false;
[593e023]393 } else {
394 /* Kernel console deactivated */
[380e23a3]395 active = true;
[593e023]396 }
[a35b458]397
[380e23a3]398 client_arbitration();
[593e023]399}
400
[2f7a564]401static kbd_dev_t *kbd_dev_new(void)
[b1bdc7a4]402{
[1875a0c]403 kbd_dev_t *kdev = calloc(1, sizeof(kbd_dev_t));
[9be360ee]404 if (kdev == NULL) {
[1875a0c]405 printf("%s: Error allocating keyboard device. "
406 "Out of memory.\n", NAME);
[2f7a564]407 return NULL;
[9be360ee]408 }
[a35b458]409
[593e023]410 link_initialize(&kdev->link);
[a35b458]411
[2f7a564]412 kdev->mods = KM_NUM_LOCK;
413 kdev->lock_keys = 0;
414 kdev->active_layout = layout_create(layout[0]);
[a35b458]415
[2f7a564]416 return kdev;
417}
418
[1875a0c]419static mouse_dev_t *mouse_dev_new(void)
420{
421 mouse_dev_t *mdev = calloc(1, sizeof(mouse_dev_t));
422 if (mdev == NULL) {
[2a72d9f]423 printf("%s: Error allocating mouse device. "
[1875a0c]424 "Out of memory.\n", NAME);
425 return NULL;
426 }
[a35b458]427
[593e023]428 link_initialize(&mdev->link);
[a35b458]429
[1875a0c]430 return mdev;
431}
432
[2a72d9f]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 }
[a35b458]441
[2a72d9f]442 sdev->kdev = kbd_dev_new();
443 if (sdev->kdev == NULL) {
444 free(sdev);
445 return NULL;
446 }
447
448 link_initialize(&sdev->link);
[a35b458]449
[2a72d9f]450 return sdev;
451}
452
[2f7a564]453/** Add new legacy keyboard device. */
454static void kbd_add_dev(kbd_port_ops_t *port, kbd_ctl_ops_t *ctl)
455{
[1875a0c]456 kbd_dev_t *kdev = kbd_dev_new();
[2f7a564]457 if (kdev == NULL)
458 return;
[a35b458]459
[9be360ee]460 kdev->port_ops = port;
461 kdev->ctl_ops = ctl;
[cce8a83]462 kdev->svc_id = 0;
[a35b458]463
[9be360ee]464 /* Initialize port driver. */
[af897ff0]465 if ((*kdev->port_ops->init)(kdev) != 0)
466 goto fail;
[a35b458]467
[9be360ee]468 /* Initialize controller driver. */
469 if ((*kdev->ctl_ops->init)(kdev) != 0) {
470 /* XXX Uninit port */
471 goto fail;
472 }
[a35b458]473
[593e023]474 list_append(&kdev->link, &kbd_devs);
[9be360ee]475 return;
[a35b458]476
[9be360ee]477fail:
478 free(kdev);
479}
480
[af897ff0]481/** Add new kbdev device.
482 *
[593e023]483 * @param service_id Service ID of the keyboard device
[1875a0c]484 *
[af897ff0]485 */
[cce8a83]486static int kbd_add_kbdev(service_id_t service_id, kbd_dev_t **kdevp)
[af897ff0]487{
[1875a0c]488 kbd_dev_t *kdev = kbd_dev_new();
[2f7a564]489 if (kdev == NULL)
[af897ff0]490 return -1;
[a35b458]491
[cce8a83]492 kdev->svc_id = service_id;
[af897ff0]493 kdev->port_ops = NULL;
494 kdev->ctl_ops = &kbdev_ctl;
[a35b458]495
[b7fd2a0]496 errno_t rc = loc_service_get_name(service_id, &kdev->svc_name);
[cce8a83]497 if (rc != EOK) {
498 kdev->svc_name = NULL;
499 goto fail;
500 }
[a35b458]501
[af897ff0]502 /* Initialize controller driver. */
503 if ((*kdev->ctl_ops->init)(kdev) != 0) {
504 goto fail;
505 }
[a35b458]506
[593e023]507 list_append(&kdev->link, &kbd_devs);
[cce8a83]508 *kdevp = kdev;
[d5c1051]509 return 0;
[a35b458]510
[af897ff0]511fail:
[cce8a83]512 if (kdev->svc_name != NULL)
513 free(kdev->svc_name);
[af897ff0]514 free(kdev);
515 return -1;
516}
517
[1875a0c]518/** Add new mousedev device.
519 *
[593e023]520 * @param service_id Service ID of the mouse device
[1875a0c]521 *
522 */
[cce8a83]523static int mouse_add_mousedev(service_id_t service_id, mouse_dev_t **mdevp)
[1875a0c]524{
525 mouse_dev_t *mdev = mouse_dev_new();
526 if (mdev == NULL)
527 return -1;
[a35b458]528
[cce8a83]529 mdev->svc_id = service_id;
[1875a0c]530 mdev->port_ops = NULL;
531 mdev->proto_ops = &mousedev_proto;
[a35b458]532
[b7fd2a0]533 errno_t rc = loc_service_get_name(service_id, &mdev->svc_name);
[cce8a83]534 if (rc != EOK) {
535 mdev->svc_name = NULL;
536 goto fail;
537 }
[a35b458]538
[1875a0c]539 /* Initialize controller driver. */
540 if ((*mdev->proto_ops->init)(mdev) != 0) {
541 goto fail;
542 }
[a35b458]543
[593e023]544 list_append(&mdev->link, &mouse_devs);
[cce8a83]545 *mdevp = mdev;
[d5c1051]546 return 0;
[a35b458]547
[1875a0c]548fail:
549 free(mdev);
550 return -1;
551}
552
[b7fd2a0]553static errno_t serial_consumer(void *arg)
[2a72d9f]554{
555 serial_dev_t *sdev = (serial_dev_t *) arg;
556
557 while (true) {
558 uint8_t data;
[74017ce]559 size_t nread;
[2a72d9f]560
[f2d88f3]561 chardev_read(sdev->chardev, &data, sizeof(data), &nread,
562 chardev_f_none);
[74017ce]563 /* XXX Handle error */
[2a72d9f]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{
[a79b42a]577 bool match = false;
[b7fd2a0]578 errno_t rc;
[a79b42a]579
[2a72d9f]580 serial_dev_t *sdev = serial_dev_new();
581 if (sdev == NULL)
582 return -1;
[a35b458]583
[2a72d9f]584 sdev->kdev->svc_id = service_id;
[a35b458]585
[74017ce]586 rc = loc_service_get_name(service_id, &sdev->kdev->svc_name);
[a79b42a]587 if (rc != EOK)
[2a72d9f]588 goto fail;
589
590 list_append(&sdev->link, &serial_devs);
591
[a79b42a]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
[74017ce]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
[a79b42a]618 fid_t fid = fibril_create(serial_consumer, sdev);
619 fibril_add_ready(fid);
620 }
[a35b458]621
[2a72d9f]622 *sdevp = sdev;
[d5c1051]623 return 0;
[a35b458]624
[2a72d9f]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
[9be360ee]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
[6d15572]643#if defined(UARCH_ia64) && defined(MACHINE_ski)
644 kbd_add_dev(&chardev_port, &stty_ctl);
[9be360ee]645#endif
646#if defined(MACHINE_msim)
[676e833]647 kbd_add_dev(&chardev_port, &stty_ctl);
[9be360ee]648#endif
649#if defined(UARCH_sparc64) && defined(PROCESSOR_sun4v)
[7aa94304]650 kbd_add_dev(&chardev_port, &stty_ctl);
[7348c4b]651#endif
652#if defined(UARCH_arm64) && defined(MACHINE_virt)
653 kbd_add_dev(&chardev_port, &stty_ctl);
[06f10ac]654#endif
655#if defined(UARCH_arm64) && defined(MACHINE_hikey960)
656 kbd_add_dev(&chardev_port, &stty_ctl);
[9be360ee]657#endif
[af897ff0]658 /* Silence warning on abs32le about kbd_add_dev() being unused */
659 (void) kbd_add_dev;
[9be360ee]660}
661
[b7fd2a0]662static errno_t dev_check_new_kbdevs(void)
[af897ff0]663{
[12f9f0d0]664 category_id_t keyboard_cat;
[cc574511]665 service_id_t *svcs;
666 size_t count, i;
667 bool already_known;
[b7fd2a0]668 errno_t rc;
[a35b458]669
[cc574511]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 }
[a35b458]675
[12f9f0d0]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;
[a35b458]688
[12f9f0d0]689 /* Determine whether we already know this device. */
[593e023]690 list_foreach(kbd_devs, link, kbd_dev_t, kdev) {
[12f9f0d0]691 if (kdev->svc_id == svcs[i]) {
692 already_known = true;
693 break;
694 }
695 }
[a35b458]696
[12f9f0d0]697 if (!already_known) {
698 kbd_dev_t *kdev;
[d5c1051]699 if (kbd_add_kbdev(svcs[i], &kdev) == 0) {
[12f9f0d0]700 printf("%s: Connected keyboard device '%s'\n",
701 NAME, kdev->svc_name);
702 }
703 }
704 }
[a35b458]705
[99ac5cf]706 free(svcs);
[a35b458]707
[12f9f0d0]708 /* XXX Handle device removal */
[a35b458]709
[12f9f0d0]710 return EOK;
711}
712
[b7fd2a0]713static errno_t dev_check_new_mousedevs(void)
[12f9f0d0]714{
715 category_id_t mouse_cat;
716 service_id_t *svcs;
717 size_t count, i;
718 bool already_known;
[b7fd2a0]719 errno_t rc;
[a35b458]720
[cc574511]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 }
[a35b458]726
[12f9f0d0]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 }
[a35b458]736
[12f9f0d0]737 for (i = 0; i < count; i++) {
738 already_known = false;
[a35b458]739
[12f9f0d0]740 /* Determine whether we already know this device. */
[593e023]741 list_foreach(mouse_devs, link, mouse_dev_t, mdev) {
[12f9f0d0]742 if (mdev->svc_id == svcs[i]) {
743 already_known = true;
744 break;
[cc574511]745 }
[854eddd6]746 }
[a35b458]747
[12f9f0d0]748 if (!already_known) {
749 mouse_dev_t *mdev;
[d5c1051]750 if (mouse_add_mousedev(svcs[i], &mdev) == 0) {
[12f9f0d0]751 printf("%s: Connected mouse device '%s'\n",
752 NAME, mdev->svc_name);
[cc574511]753 }
[af897ff0]754 }
755 }
[a35b458]756
[99ac5cf]757 free(svcs);
[a35b458]758
[12f9f0d0]759 /* XXX Handle device removal */
[a35b458]760
[af897ff0]761 return EOK;
762}
763
[b7fd2a0]764static errno_t dev_check_new_serialdevs(void)
[2a72d9f]765{
766 category_id_t serial_cat;
767 service_id_t *svcs;
768 size_t count, i;
769 bool already_known;
[b7fd2a0]770 errno_t rc;
[a35b458]771
[2a72d9f]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 }
[a35b458]777
[2a72d9f]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;
[a35b458]790
[2a72d9f]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 }
[a35b458]798
[2a72d9f]799 if (!already_known) {
800 serial_dev_t *sdev;
[d5c1051]801 if (serial_add_srldev(svcs[i], &sdev) == 0) {
[2a72d9f]802 printf("%s: Connected serial device '%s'\n",
803 NAME, sdev->kdev->svc_name);
804 }
805 }
806 }
[a35b458]807
[2a72d9f]808 free(svcs);
[a35b458]809
[2a72d9f]810 /* XXX Handle device removal */
[a35b458]811
[2a72d9f]812 return EOK;
813}
814
[b7fd2a0]815static errno_t dev_check_new(void)
[af897ff0]816{
[b7fd2a0]817 errno_t rc;
[a35b458]818
[10a5479d]819 fibril_mutex_lock(&discovery_lock);
[a35b458]820
[73d8600]821 if (!serial_console) {
822 rc = dev_check_new_kbdevs();
823 if (rc != EOK) {
824 fibril_mutex_unlock(&discovery_lock);
825 return rc;
826 }
[a35b458]827
[73d8600]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 }
[2a72d9f]839 }
[a35b458]840
[10a5479d]841 fibril_mutex_unlock(&discovery_lock);
[a35b458]842
[12f9f0d0]843 return EOK;
844}
845
[e89a06a]846static void cat_change_cb(void *arg)
[12f9f0d0]847{
848 dev_check_new();
849}
850
851/** Start listening for new devices. */
[b7fd2a0]852static errno_t input_start_dev_discovery(void)
[12f9f0d0]853{
[e89a06a]854 errno_t rc = loc_register_cat_change_cb(cat_change_cb, NULL);
[12f9f0d0]855 if (rc != EOK) {
[dd8ab1c]856 printf("%s: Failed registering callback for device discovery: "
857 "%s\n", NAME, str_error(rc));
[12f9f0d0]858 return rc;
859 }
[a35b458]860
[12f9f0d0]861 return dev_check_new();
[af897ff0]862}
863
[b6a088f]864static void usage(char *name)
865{
866 printf("Usage: %s <service_name>\n", name);
867}
868
[51d6f80]869int main(int argc, char **argv)
870{
[b7fd2a0]871 errno_t rc;
[a79b42a]872
[b6a088f]873 if (argc < 2) {
874 usage(argv[0]);
875 return 1;
876 }
[a35b458]877
[5f88293]878 printf("%s: HelenOS input service\n", NAME);
[a35b458]879
[593e023]880 list_initialize(&clients);
[9be360ee]881 list_initialize(&kbd_devs);
[854eddd6]882 list_initialize(&mouse_devs);
[2a72d9f]883 list_initialize(&serial_devs);
[a35b458]884
[03e0beaf]885 serial_console = config_get_value("console");
[a35b458]886
[854eddd6]887 /* Add legacy keyboard devices. */
[9be360ee]888 kbd_add_legacy_devs();
[a35b458]889
[47a350f]890 /* Register driver */
[593e023]891 async_set_client_data_constructor(client_data_create);
892 async_set_client_data_destructor(client_data_destroy);
[b688fd8]893 async_set_fallback_port_handler(client_connection, NULL);
[a35b458]894
[a79b42a]895 rc = loc_server_register(NAME);
[3123d2a]896 if (rc != EOK) {
897 printf("%s: Unable to register server\n", NAME);
898 return rc;
[47a350f]899 }
[a35b458]900
[15f3c3f]901 service_id_t service_id;
[b6a088f]902 rc = loc_service_register(argv[1], &service_id);
[3123d2a]903 if (rc != EOK) {
[b6a088f]904 printf("%s: Unable to register service %s\n", NAME, argv[1]);
[3123d2a]905 return rc;
[47a350f]906 }
[a35b458]907
[593e023]908 /* Receive kernel notifications */
[8820544]909 rc = async_event_subscribe(EVENT_KCONSOLE, kconsole_event_handler, NULL);
[593e023]910 if (rc != EOK)
911 printf("%s: Failed to register kconsole notifications (%s)\n",
912 NAME, str_error(rc));
[a35b458]913
[854eddd6]914 /* Start looking for new input devices */
915 input_start_dev_discovery();
[a35b458]916
[1875a0c]917 printf("%s: Accepting connections\n", NAME);
[b6a088f]918 task_retval(0);
[085bd54]919 async_manager();
[a35b458]920
[f89979b]921 /* Not reached. */
[153a209]922 return 0;
[51d6f80]923}
[ce5bcb4]924
925/**
926 * @}
[5f88293]927 */
Note: See TracBrowser for help on using the repository browser.