source: mainline/uspace/srv/hid/adb_mouse/adb_mouse.c@ a40dea3

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since a40dea3 was 9934f7d, checked in by Jiri Svoboda <jiri@…>, 14 years ago

Add extra argument to async connection handlers that can be used for passing
information from async_connect_to_me() to the handler.

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