| 1 | /*
|
|---|
| 2 | * Copyright (c) 2009 Jiri Svoboda
|
|---|
| 3 | * All rights reserved.
|
|---|
| 4 | *
|
|---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
|---|
| 6 | * modification, are permitted provided that the following conditions
|
|---|
| 7 | * are met:
|
|---|
| 8 | *
|
|---|
| 9 | * - Redistributions of source code must retain the above copyright
|
|---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
|---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
|---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
|---|
| 13 | * documentation and/or other materials provided with the distribution.
|
|---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
|---|
| 15 | * derived from this software without specific prior written permission.
|
|---|
| 16 | *
|
|---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|---|
| 27 | */
|
|---|
| 28 |
|
|---|
| 29 | /**
|
|---|
| 30 | * @addtogroup mouse
|
|---|
| 31 | * @brief Chardev mouse driver.
|
|---|
| 32 | *
|
|---|
| 33 | * This is a common driver for mice attached to simple character devices
|
|---|
| 34 | * (PS/2 mice, serial mice).
|
|---|
| 35 | *
|
|---|
| 36 | * @{
|
|---|
| 37 | */
|
|---|
| 38 | /** @file
|
|---|
| 39 | */
|
|---|
| 40 |
|
|---|
| 41 | #include <ipc/mouse.h>
|
|---|
| 42 | #include <stdio.h>
|
|---|
| 43 | #include <stdlib.h>
|
|---|
| 44 | #include <async.h>
|
|---|
| 45 | #include <errno.h>
|
|---|
| 46 | #include <devmap.h>
|
|---|
| 47 |
|
|---|
| 48 | #include <char_mouse.h>
|
|---|
| 49 | #include <mouse_port.h>
|
|---|
| 50 | #include <mouse_proto.h>
|
|---|
| 51 |
|
|---|
| 52 | #define NAME "mouse"
|
|---|
| 53 | #define NAMESPACE "hid_in"
|
|---|
| 54 |
|
|---|
| 55 | int client_phone = -1;
|
|---|
| 56 |
|
|---|
| 57 | void mouse_handle_byte(int byte)
|
|---|
| 58 | {
|
|---|
| 59 | /* printf("mouse byte: 0x%x\n", byte);*/
|
|---|
| 60 | mouse_proto_parse_byte(byte);
|
|---|
| 61 | }
|
|---|
| 62 |
|
|---|
| 63 | void mouse_ev_btn(int button, int press)
|
|---|
| 64 | {
|
|---|
| 65 | /* printf("ev_btn: button %d, press %d\n", button, press);*/
|
|---|
| 66 | if (client_phone != -1) {
|
|---|
| 67 | async_msg_2(client_phone, MEVENT_BUTTON, button, press);
|
|---|
| 68 | }
|
|---|
| 69 | }
|
|---|
| 70 |
|
|---|
| 71 | void mouse_ev_move(int dx, int dy)
|
|---|
| 72 | {
|
|---|
| 73 | /* printf("ev_move: dx %d, dy %d\n", dx, dy);*/
|
|---|
| 74 | if (client_phone != -1)
|
|---|
| 75 | async_msg_2(client_phone, MEVENT_MOVE, dx, dy);
|
|---|
| 76 | }
|
|---|
| 77 |
|
|---|
| 78 | static void client_connection(ipc_callid_t iid, ipc_call_t *icall)
|
|---|
| 79 | {
|
|---|
| 80 | ipc_callid_t callid;
|
|---|
| 81 | ipc_call_t call;
|
|---|
| 82 | int retval;
|
|---|
| 83 |
|
|---|
| 84 | async_answer_0(iid, EOK);
|
|---|
| 85 |
|
|---|
| 86 | while (1) {
|
|---|
| 87 | callid = async_get_call(&call);
|
|---|
| 88 | switch (IPC_GET_IMETHOD(call)) {
|
|---|
| 89 | case IPC_M_PHONE_HUNGUP:
|
|---|
| 90 | if (client_phone != -1) {
|
|---|
| 91 | async_hangup(client_phone);
|
|---|
| 92 | client_phone = -1;
|
|---|
| 93 | }
|
|---|
| 94 |
|
|---|
| 95 | async_answer_0(callid, EOK);
|
|---|
| 96 | return;
|
|---|
| 97 | case IPC_M_CONNECT_TO_ME:
|
|---|
| 98 | if (client_phone != -1) {
|
|---|
| 99 | retval = ELIMIT;
|
|---|
| 100 | break;
|
|---|
| 101 | }
|
|---|
| 102 | client_phone = IPC_GET_ARG5(call);
|
|---|
| 103 | retval = 0;
|
|---|
| 104 | break;
|
|---|
| 105 | default:
|
|---|
| 106 | retval = EINVAL;
|
|---|
| 107 | }
|
|---|
| 108 | async_answer_0(callid, retval);
|
|---|
| 109 | }
|
|---|
| 110 | }
|
|---|
| 111 |
|
|---|
| 112 |
|
|---|
| 113 | int main(int argc, char **argv)
|
|---|
| 114 | {
|
|---|
| 115 | printf(NAME ": Chardev mouse driver\n");
|
|---|
| 116 |
|
|---|
| 117 | /* Initialize port. */
|
|---|
| 118 | if (mouse_port_init() != 0)
|
|---|
| 119 | return -1;
|
|---|
| 120 |
|
|---|
| 121 | /* Initialize protocol driver. */
|
|---|
| 122 | if (mouse_proto_init() != 0)
|
|---|
| 123 | return -1;
|
|---|
| 124 |
|
|---|
| 125 | /* Register driver */
|
|---|
| 126 | int rc = devmap_driver_register(NAME, client_connection);
|
|---|
| 127 | if (rc < 0) {
|
|---|
| 128 | printf(NAME ": Unable to register driver (%d)\n", rc);
|
|---|
| 129 | return -1;
|
|---|
| 130 | }
|
|---|
| 131 |
|
|---|
| 132 | char dev_path[DEVMAP_NAME_MAXLEN + 1];
|
|---|
| 133 | snprintf(dev_path, DEVMAP_NAME_MAXLEN, "%s/%s", NAMESPACE, NAME);
|
|---|
| 134 |
|
|---|
| 135 | devmap_handle_t devmap_handle;
|
|---|
| 136 | if (devmap_device_register(dev_path, &devmap_handle) != EOK) {
|
|---|
| 137 | printf(NAME ": Unable to register device %s\n", dev_path);
|
|---|
| 138 | return -1;
|
|---|
| 139 | }
|
|---|
| 140 |
|
|---|
| 141 | printf(NAME ": Accepting connections\n");
|
|---|
| 142 | task_retval(0);
|
|---|
| 143 | async_manager();
|
|---|
| 144 |
|
|---|
| 145 | /* Not reached. */
|
|---|
| 146 | return 0;
|
|---|
| 147 | }
|
|---|
| 148 |
|
|---|
| 149 | /**
|
|---|
| 150 | * @}
|
|---|
| 151 | */
|
|---|