source: mainline/uspace/srv/hid/isdv4_tablet/main.c@ 3e242d2

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 3e242d2 was 3be9d10, checked in by Jakub Jermar <jakub@…>, 7 years ago

Get rid of ipc_callid_t

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