1 | /*
|
---|
2 | * Copyright (c) 2012 Martin Sucha
|
---|
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 | #include <async.h>
|
---|
30 | #include <errno.h>
|
---|
31 | #include <fibril_synch.h>
|
---|
32 | #include <io/serial.h>
|
---|
33 | #include <ipc/mouseev.h>
|
---|
34 | #include <loc.h>
|
---|
35 | #include <stddef.h>
|
---|
36 | #include <stdio.h>
|
---|
37 | #include <task.h>
|
---|
38 |
|
---|
39 | #include "isdv4.h"
|
---|
40 |
|
---|
41 | #define NAME "isdv4_tablet"
|
---|
42 |
|
---|
43 | static async_sess_t *client_sess = NULL;
|
---|
44 | static fibril_mutex_t client_mutex;
|
---|
45 | static isdv4_state_t state;
|
---|
46 |
|
---|
47 | static void syntax_print(void)
|
---|
48 | {
|
---|
49 | fprintf(stderr, "Usage: %s [--baud=<baud>] [--print-events] [device_service]\n", NAME);
|
---|
50 | }
|
---|
51 |
|
---|
52 | static errno_t read_fibril(void *unused)
|
---|
53 | {
|
---|
54 | errno_t rc = isdv4_read_events(&state);
|
---|
55 | if (rc != EOK) {
|
---|
56 | fprintf(stderr, "Failed reading events");
|
---|
57 | return rc;
|
---|
58 | }
|
---|
59 |
|
---|
60 | isdv4_fini(&state);
|
---|
61 | return EOK;
|
---|
62 | }
|
---|
63 |
|
---|
64 | static void mouse_connection(ipc_callid_t iid, ipc_call_t *icall, void *arg)
|
---|
65 | {
|
---|
66 | async_answer_0(iid, EOK);
|
---|
67 |
|
---|
68 | async_sess_t *sess =
|
---|
69 | async_callback_receive(EXCHANGE_SERIALIZE);
|
---|
70 |
|
---|
71 | fibril_mutex_lock(&client_mutex);
|
---|
72 |
|
---|
73 | if (client_sess == NULL)
|
---|
74 | client_sess = sess;
|
---|
75 |
|
---|
76 | fibril_mutex_unlock(&client_mutex);
|
---|
77 |
|
---|
78 | while (true) {
|
---|
79 | ipc_call_t call;
|
---|
80 | ipc_callid_t callid = async_get_call(&call);
|
---|
81 |
|
---|
82 | if (!IPC_GET_IMETHOD(call))
|
---|
83 | break;
|
---|
84 |
|
---|
85 | async_answer_0(callid, ENOTSUP);
|
---|
86 | }
|
---|
87 | }
|
---|
88 |
|
---|
89 | static void emit_event(const isdv4_event_t *event)
|
---|
90 | {
|
---|
91 | fibril_mutex_lock(&client_mutex);
|
---|
92 | async_sess_t *sess = client_sess;
|
---|
93 | fibril_mutex_unlock(&client_mutex);
|
---|
94 |
|
---|
95 | if (!sess) return;
|
---|
96 |
|
---|
97 | async_exch_t *exch = async_exchange_begin(sess);
|
---|
98 | if (exch) {
|
---|
99 | unsigned int max_x = state.stylus_max_x;
|
---|
100 | unsigned int max_y = state.stylus_max_y;
|
---|
101 | if (event->source == TOUCH) {
|
---|
102 | max_x = state.touch_max_x;
|
---|
103 | max_y = state.touch_max_y;
|
---|
104 | }
|
---|
105 | async_msg_4(exch, MOUSEEV_ABS_MOVE_EVENT, event->x, event->y,
|
---|
106 | max_x, max_y);
|
---|
107 | if (event->type == PRESS || event->type == RELEASE) {
|
---|
108 | async_msg_2(exch, MOUSEEV_BUTTON_EVENT, event->button,
|
---|
109 | event->type == PRESS);
|
---|
110 | }
|
---|
111 | }
|
---|
112 | async_exchange_end(exch);
|
---|
113 | }
|
---|
114 |
|
---|
115 | static void print_and_emit_event(const isdv4_event_t *event)
|
---|
116 | {
|
---|
117 | const char *type = NULL;
|
---|
118 | switch (event->type) {
|
---|
119 | case PRESS:
|
---|
120 | type = "PRESS";
|
---|
121 | break;
|
---|
122 | case RELEASE:
|
---|
123 | type = "RELEASE";
|
---|
124 | break;
|
---|
125 | case PROXIMITY_IN:
|
---|
126 | type = "PROXIMITY IN";
|
---|
127 | break;
|
---|
128 | case PROXIMITY_OUT:
|
---|
129 | type = "PROXIMITY OUT";
|
---|
130 | break;
|
---|
131 | case MOVE:
|
---|
132 | type = "MOVE";
|
---|
133 | break;
|
---|
134 | default:
|
---|
135 | type = "UNKNOWN";
|
---|
136 | break;
|
---|
137 | }
|
---|
138 |
|
---|
139 | const char *source = NULL;
|
---|
140 | switch (event->source) {
|
---|
141 | case STYLUS_TIP:
|
---|
142 | source = "stylus tip";
|
---|
143 | break;
|
---|
144 | case STYLUS_ERASER:
|
---|
145 | source = "stylus eraser";
|
---|
146 | break;
|
---|
147 | case TOUCH:
|
---|
148 | source = "touch";
|
---|
149 | break;
|
---|
150 | }
|
---|
151 |
|
---|
152 | printf("%s %s %u %u %u %u\n", type, source, event->x, event->y,
|
---|
153 | event->pressure, event->button);
|
---|
154 |
|
---|
155 | emit_event(event);
|
---|
156 | }
|
---|
157 |
|
---|
158 | static const char *touch_type(unsigned int data_id)
|
---|
159 | {
|
---|
160 | switch (data_id) {
|
---|
161 | case 0:
|
---|
162 | return "resistive+stylus";
|
---|
163 | case 1:
|
---|
164 | return "capacitive+stylus";
|
---|
165 | case 2:
|
---|
166 | return "resistive";
|
---|
167 | case 3:
|
---|
168 | case 4:
|
---|
169 | return "capacitive";
|
---|
170 | case 5:
|
---|
171 | return "penabled";
|
---|
172 | }
|
---|
173 | return "unknown";
|
---|
174 | }
|
---|
175 |
|
---|
176 | int main(int argc, char **argv)
|
---|
177 | {
|
---|
178 | sysarg_t baud = 38400;
|
---|
179 | service_id_t svc_id;
|
---|
180 | serial_t *serial;
|
---|
181 | char *serial_port_name = NULL;
|
---|
182 |
|
---|
183 | int arg = 1;
|
---|
184 | errno_t rc;
|
---|
185 |
|
---|
186 | isdv4_event_fn event_fn = emit_event;
|
---|
187 |
|
---|
188 | if (argc > arg && str_test_prefix(argv[arg], "--baud=")) {
|
---|
189 | size_t arg_offset = str_lsize(argv[arg], 7);
|
---|
190 | char* arg_str = argv[arg] + arg_offset;
|
---|
191 | if (str_length(arg_str) == 0) {
|
---|
192 | fprintf(stderr, "--baud requires an argument\n");
|
---|
193 | syntax_print();
|
---|
194 | return 1;
|
---|
195 | }
|
---|
196 | char *endptr;
|
---|
197 | baud = strtol(arg_str, &endptr, 10);
|
---|
198 | if (*endptr != '\0') {
|
---|
199 | fprintf(stderr, "Invalid value for baud\n");
|
---|
200 | syntax_print();
|
---|
201 | return 1;
|
---|
202 | }
|
---|
203 | arg++;
|
---|
204 | }
|
---|
205 |
|
---|
206 | if (argc > arg && str_cmp(argv[arg], "--print-events") == 0) {
|
---|
207 | event_fn = print_and_emit_event;
|
---|
208 | arg++;
|
---|
209 | }
|
---|
210 |
|
---|
211 | if (argc > arg) {
|
---|
212 | serial_port_name = argv[arg];
|
---|
213 | rc = loc_service_get_id(serial_port_name, &svc_id, 0);
|
---|
214 | if (rc != EOK) {
|
---|
215 | fprintf(stderr, "Cannot find device service %s\n",
|
---|
216 | argv[arg]);
|
---|
217 | return 1;
|
---|
218 | }
|
---|
219 | arg++;
|
---|
220 | }
|
---|
221 | else {
|
---|
222 | category_id_t serial_cat_id;
|
---|
223 |
|
---|
224 | rc = loc_category_get_id("serial", &serial_cat_id, 0);
|
---|
225 | if (rc != EOK) {
|
---|
226 | fprintf(stderr, "Failed getting id of category "
|
---|
227 | "'serial'\n");
|
---|
228 | return 1;
|
---|
229 | }
|
---|
230 |
|
---|
231 | service_id_t *svc_ids;
|
---|
232 | size_t svc_count;
|
---|
233 |
|
---|
234 | rc = loc_category_get_svcs(serial_cat_id, &svc_ids, &svc_count); if (rc != EOK) {
|
---|
235 | fprintf(stderr, "Failed getting list of services\n");
|
---|
236 | return 1;
|
---|
237 | }
|
---|
238 |
|
---|
239 | if (svc_count == 0) {
|
---|
240 | fprintf(stderr, "No service in category 'serial'\n");
|
---|
241 | free(svc_ids);
|
---|
242 | return 1;
|
---|
243 | }
|
---|
244 |
|
---|
245 | svc_id = svc_ids[0];
|
---|
246 |
|
---|
247 | rc = loc_service_get_name(svc_id, &serial_port_name);
|
---|
248 | if (rc != EOK) {
|
---|
249 | fprintf(stderr, "Failed getting name of serial service\n");
|
---|
250 | return 1;
|
---|
251 | }
|
---|
252 |
|
---|
253 | free(svc_ids);
|
---|
254 | }
|
---|
255 |
|
---|
256 | if (argc > arg) {
|
---|
257 | fprintf(stderr, "Too many arguments\n");
|
---|
258 | syntax_print();
|
---|
259 | return 1;
|
---|
260 | }
|
---|
261 |
|
---|
262 | fibril_mutex_initialize(&client_mutex);
|
---|
263 |
|
---|
264 | printf(NAME ": Using serial port %s\n", serial_port_name);
|
---|
265 |
|
---|
266 | async_sess_t *sess = loc_service_connect(svc_id, INTERFACE_DDF,
|
---|
267 | IPC_FLAG_BLOCKING);
|
---|
268 | if (!sess) {
|
---|
269 | fprintf(stderr, "Failed connecting to service\n");
|
---|
270 | return 2;
|
---|
271 | }
|
---|
272 |
|
---|
273 | rc = serial_open(sess, &serial);
|
---|
274 | if (rc != EOK) {
|
---|
275 | fprintf(stderr, "Failed opening serial port\n");
|
---|
276 | return 2;
|
---|
277 | }
|
---|
278 |
|
---|
279 | rc = serial_set_comm_props(serial, baud, SERIAL_NO_PARITY, 8, 1);
|
---|
280 | if (rc != EOK) {
|
---|
281 | fprintf(stderr, "Failed setting serial properties\n");
|
---|
282 | return 2;
|
---|
283 | }
|
---|
284 |
|
---|
285 | rc = isdv4_init(&state, sess, event_fn);
|
---|
286 | if (rc != EOK) {
|
---|
287 | fprintf(stderr, "Failed initializing isdv4 state");
|
---|
288 | return 2;
|
---|
289 | }
|
---|
290 |
|
---|
291 | rc = isdv4_init_tablet(&state);
|
---|
292 | if (rc != EOK) {
|
---|
293 | fprintf(stderr, "Failed initializing tablet");
|
---|
294 | return 2;
|
---|
295 | }
|
---|
296 |
|
---|
297 | printf("Tablet information:\n");
|
---|
298 | printf(" Stylus: %ux%u pressure: %u tilt: ", state.stylus_max_x,
|
---|
299 | state.stylus_max_y, state.stylus_max_pressure);
|
---|
300 | if (state.stylus_tilt_supported) {
|
---|
301 | printf("%ux%u\n", state.stylus_max_xtilt, state.stylus_max_ytilt);
|
---|
302 | }
|
---|
303 | else {
|
---|
304 | printf("not supported\n");
|
---|
305 | }
|
---|
306 | printf(" Touch: %ux%u type: %s\n", state.touch_max_x, state.touch_max_y,
|
---|
307 | touch_type(state.touch_type));
|
---|
308 |
|
---|
309 | fid_t fibril = fibril_create(read_fibril, NULL);
|
---|
310 | /* From this on, state is to be used only by read_fibril */
|
---|
311 | fibril_add_ready(fibril);
|
---|
312 |
|
---|
313 | async_set_fallback_port_handler(mouse_connection, NULL);
|
---|
314 | rc = loc_server_register(NAME);
|
---|
315 | if (rc != EOK) {
|
---|
316 | printf("%s: Unable to register driver.\n", NAME);
|
---|
317 | return EXIT_RC(rc);
|
---|
318 | }
|
---|
319 |
|
---|
320 | service_id_t service_id;
|
---|
321 | char *service_name;
|
---|
322 | if (asprintf(&service_name, "mouse/isdv4-%" PRIun, svc_id) < 0) {
|
---|
323 | printf(NAME ": Unable to create service name\n");
|
---|
324 | return EXIT_RC(ENOMEM);
|
---|
325 | }
|
---|
326 |
|
---|
327 | rc = loc_service_register(service_name, &service_id);
|
---|
328 | if (rc != EOK) {
|
---|
329 | printf(NAME ": Unable to register service %s.\n", service_name);
|
---|
330 | return EXIT_RC(rc);
|
---|
331 | }
|
---|
332 |
|
---|
333 | category_id_t mouse_category;
|
---|
334 | rc = loc_category_get_id("mouse", &mouse_category, IPC_FLAG_BLOCKING);
|
---|
335 | if (rc != EOK) {
|
---|
336 | printf(NAME ": Unable to get mouse category id.\n");
|
---|
337 | }
|
---|
338 | else {
|
---|
339 | rc = loc_service_add_to_cat(service_id, mouse_category);
|
---|
340 | if (rc != EOK) {
|
---|
341 | printf(NAME ": Unable to add device to mouse category.\n");
|
---|
342 | }
|
---|
343 | }
|
---|
344 |
|
---|
345 | printf("%s: Accepting connections\n", NAME);
|
---|
346 | task_retval(0);
|
---|
347 | async_manager();
|
---|
348 |
|
---|
349 | /* Not reached */
|
---|
350 | return 0;
|
---|
351 | }
|
---|