[9f51afc] | 1 | /*
|
---|
[b73c26d] | 2 | * Copyright (c) 2010 Jiri Svoboda
|
---|
[9f51afc] | 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
|
---|
[b73c26d] | 31 | * @brief ADB Apple classic mouse driver.
|
---|
[9f51afc] | 32 | *
|
---|
[b73c26d] | 33 | * This driver handles a mouse connected to Apple Desktop Bus speaking
|
---|
| 34 | * the Apple classic protocol. It connects to an ADB driver.
|
---|
[9f51afc] | 35 | *
|
---|
| 36 | * @{
|
---|
| 37 | */
|
---|
| 38 | /** @file
|
---|
| 39 | */
|
---|
| 40 |
|
---|
| 41 | #include <ipc/ipc.h>
|
---|
| 42 | #include <ipc/mouse.h>
|
---|
| 43 | #include <stdio.h>
|
---|
| 44 | #include <stdlib.h>
|
---|
| 45 | #include <async.h>
|
---|
| 46 | #include <errno.h>
|
---|
| 47 | #include <devmap.h>
|
---|
| 48 |
|
---|
[b73c26d] | 49 | #include "adb_mouse.h"
|
---|
| 50 | #include "adb_dev.h"
|
---|
[9f51afc] | 51 |
|
---|
[b73c26d] | 52 | static void client_connection(ipc_callid_t iid, ipc_call_t *icall);
|
---|
| 53 | static void mouse_ev_btn(int button, int press);
|
---|
| 54 | static void mouse_ev_move(int dx, int dy);
|
---|
[9f51afc] | 55 |
|
---|
[b73c26d] | 56 | static int client_phone = -1;
|
---|
| 57 | static bool b1_pressed, b2_pressed;
|
---|
[9f51afc] | 58 |
|
---|
[b73c26d] | 59 | int main(int argc, char **argv)
|
---|
[9f51afc] | 60 | {
|
---|
[b73c26d] | 61 | printf(NAME ": Chardev mouse driver\n");
|
---|
[9f51afc] | 62 |
|
---|
[b73c26d] | 63 | /* Initialize device. */
|
---|
| 64 | if (adb_dev_init() != 0)
|
---|
| 65 | return -1;
|
---|
| 66 |
|
---|
| 67 | b1_pressed = false;
|
---|
| 68 | b2_pressed = false;
|
---|
| 69 |
|
---|
| 70 | /* Register driver */
|
---|
| 71 | int rc = devmap_driver_register(NAME, client_connection);
|
---|
| 72 | if (rc < 0) {
|
---|
| 73 | printf(NAME ": Unable to register driver (%d)\n", rc);
|
---|
| 74 | return -1;
|
---|
[9f51afc] | 75 | }
|
---|
| 76 |
|
---|
[b73c26d] | 77 | char dev_path[DEVMAP_NAME_MAXLEN + 1];
|
---|
| 78 | snprintf(dev_path, DEVMAP_NAME_MAXLEN, "%s/mouse", NAMESPACE);
|
---|
| 79 |
|
---|
[991f645] | 80 | devmap_handle_t devmap_handle;
|
---|
| 81 | if (devmap_device_register(dev_path, &devmap_handle) != EOK) {
|
---|
[b73c26d] | 82 | printf(NAME ": Unable to register device %s\n", dev_path);
|
---|
| 83 | return -1;
|
---|
| 84 | }
|
---|
| 85 |
|
---|
| 86 | printf(NAME ": Accepting connections\n");
|
---|
| 87 | task_retval(0);
|
---|
| 88 | async_manager();
|
---|
| 89 |
|
---|
| 90 | /* Not reached. */
|
---|
| 91 | return 0;
|
---|
[9f51afc] | 92 | }
|
---|
| 93 |
|
---|
| 94 | static void client_connection(ipc_callid_t iid, ipc_call_t *icall)
|
---|
| 95 | {
|
---|
| 96 | ipc_callid_t callid;
|
---|
| 97 | ipc_call_t call;
|
---|
| 98 | int retval;
|
---|
| 99 |
|
---|
| 100 | ipc_answer_0(iid, EOK);
|
---|
| 101 |
|
---|
| 102 | while (1) {
|
---|
| 103 | callid = async_get_call(&call);
|
---|
| 104 | switch (IPC_GET_METHOD(call)) {
|
---|
| 105 | case IPC_M_PHONE_HUNGUP:
|
---|
| 106 | if (client_phone != -1) {
|
---|
| 107 | ipc_hangup(client_phone);
|
---|
| 108 | client_phone = -1;
|
---|
| 109 | }
|
---|
| 110 |
|
---|
| 111 | ipc_answer_0(callid, EOK);
|
---|
| 112 | return;
|
---|
| 113 | case IPC_M_CONNECT_TO_ME:
|
---|
| 114 | if (client_phone != -1) {
|
---|
| 115 | retval = ELIMIT;
|
---|
| 116 | break;
|
---|
| 117 | }
|
---|
| 118 | client_phone = IPC_GET_ARG5(call);
|
---|
| 119 | retval = 0;
|
---|
| 120 | break;
|
---|
| 121 | default:
|
---|
| 122 | retval = EINVAL;
|
---|
| 123 | }
|
---|
| 124 | ipc_answer_0(callid, retval);
|
---|
| 125 | }
|
---|
| 126 | }
|
---|
| 127 |
|
---|
[b73c26d] | 128 | void mouse_handle_data(uint16_t data)
|
---|
[9f51afc] | 129 | {
|
---|
[b73c26d] | 130 | bool b1, b2;
|
---|
| 131 | uint16_t udx, udy;
|
---|
| 132 | int dx, dy;
|
---|
| 133 |
|
---|
| 134 | /* Extract fields. */
|
---|
| 135 | b1 = ((data >> 15) & 1) == 0;
|
---|
| 136 | udy = (data >> 8) & 0x7f;
|
---|
| 137 | b2 = ((data >> 7) & 1) == 0;
|
---|
| 138 | udx = data & 0x7f;
|
---|
| 139 |
|
---|
| 140 | /* Decode 7-bit two's complement signed values. */
|
---|
| 141 | dx = (udx & 0x40) ? (udx - 0x80) : udx;
|
---|
| 142 | dy = (udy & 0x40) ? (udy - 0x80) : udy;
|
---|
| 143 |
|
---|
| 144 | if (b1 != b1_pressed) {
|
---|
| 145 | mouse_ev_btn(1, b1);
|
---|
| 146 | b1_pressed = b1;
|
---|
| 147 | }
|
---|
[9f51afc] | 148 |
|
---|
[b73c26d] | 149 | if (b2 != b2_pressed) {
|
---|
| 150 | mouse_ev_btn(2, b2);
|
---|
| 151 | b1_pressed = b1;
|
---|
[9f51afc] | 152 | }
|
---|
| 153 |
|
---|
[b73c26d] | 154 | if (dx != 0 || dy != 0)
|
---|
| 155 | mouse_ev_move(dx, dy);
|
---|
| 156 | }
|
---|
[9f51afc] | 157 |
|
---|
[b73c26d] | 158 | static void mouse_ev_btn(int button, int press)
|
---|
| 159 | {
|
---|
| 160 | if (client_phone != -1) {
|
---|
| 161 | async_msg_2(client_phone, MEVENT_BUTTON, button, press);
|
---|
[9f51afc] | 162 | }
|
---|
[b73c26d] | 163 | }
|
---|
[9f51afc] | 164 |
|
---|
[b73c26d] | 165 | static void mouse_ev_move(int dx, int dy)
|
---|
| 166 | {
|
---|
| 167 | if (client_phone != -1)
|
---|
| 168 | async_msg_2(client_phone, MEVENT_MOVE, dx, dy);
|
---|
[9f51afc] | 169 | }
|
---|
| 170 |
|
---|
| 171 | /**
|
---|
| 172 | * @}
|
---|
| 173 | */
|
---|