source: mainline/uspace/srv/hid/isdv4_tablet/main.c@ 6a0b2cc

ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 6a0b2cc was fafb8e5, checked in by Jiří Zárevúcky <zarevucky.jiri@…>, 6 years ago

Mechanically lowercase IPC_SET_*/IPC_GET_*

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