source: mainline/uspace/srv/hid/input/input.c@ 9cfbf2f

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 9cfbf2f was 2133e02, checked in by Martin Decky <martin@…>, 11 years ago

canonically reserve the first argument of IPC_M_CONNECT_ME_TO for interface type
naming service: service handle in IPC_M_CONNECT_ME_TO needs to be shifted to the second argument
service connections that use the untyped (fallback) port can only provide one additional argument now

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