source: mainline/uspace/srv/hid/input/input.c@ d776329b

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since d776329b was 1558d85, checked in by Jakub Jermar <jakub@…>, 10 years ago

Remove duplicate includes

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