source: mainline/uspace/srv/hid/adb_mouse/adb_mouse.c@ 17aca1c

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 17aca1c was ffa2c8ef, checked in by Martin Decky <martin@…>, 15 years ago

do not intermix low-level IPC methods with async framework methods

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