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

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

Prefer handle over ID in naming handle variables

  • Property mode set to 100644
File size: 8.3 KB
RevLine 
[5bf6bc70]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
[c4c6025]29#include <async.h>
[5bf6bc70]30#include <errno.h>
[1499564]31#include <fibril_synch.h>
[c4c6025]32#include <io/serial.h>
[1499564]33#include <ipc/mouseev.h>
[c4c6025]34#include <loc.h>
35#include <stddef.h>
36#include <stdio.h>
[1d6dd2a]37#include <str.h>
[1c635d6]38#include <task.h>
[5bf6bc70]39
[a987832]40#include "isdv4.h"
[5bf6bc70]41
[f66ca57f]42#define NAME "isdv4_tablet"
[1499564]43
44static async_sess_t *client_sess = NULL;
45static fibril_mutex_t client_mutex;
46static isdv4_state_t state;
47
[5bf6bc70]48static void syntax_print(void)
49{
[f66ca57f]50 fprintf(stderr, "Usage: %s [--baud=<baud>] [--print-events] [device_service]\n", NAME);
[1499564]51}
52
[b7fd2a0]53static errno_t read_fibril(void *unused)
[1499564]54{
[b7fd2a0]55 errno_t rc = isdv4_read_events(&state);
[1499564]56 if (rc != EOK) {
57 fprintf(stderr, "Failed reading events");
58 return rc;
59 }
60
61 isdv4_fini(&state);
62 return EOK;
63}
64
[a46e56b]65static void mouse_connection(cap_call_handle_t icall_handle, ipc_call_t *icall, void *arg)
[1499564]66{
[a46e56b]67 async_answer_0(icall_handle, EOK);
[a35b458]68
[1499564]69 async_sess_t *sess =
70 async_callback_receive(EXCHANGE_SERIALIZE);
[a35b458]71
[1499564]72 fibril_mutex_lock(&client_mutex);
[a35b458]73
[f9b2cb4c]74 if (client_sess == NULL)
75 client_sess = sess;
[a35b458]76
[1499564]77 fibril_mutex_unlock(&client_mutex);
78
79 while (true) {
80 ipc_call_t call;
[a46e56b]81 cap_call_handle_t chandle = async_get_call(&call);
[a35b458]82
[1499564]83 if (!IPC_GET_IMETHOD(call))
84 break;
[a35b458]85
[a46e56b]86 async_answer_0(chandle, ENOTSUP);
[1499564]87 }
[5bf6bc70]88}
89
[1499564]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);
[a35b458]95
[1499564]96 if (!sess) return;
[a35b458]97
[1499564]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)
[6e09bee]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";
[1499564]134 break;
135 default:
[6e09bee]136 type = "UNKNOWN";
137 break;
138 }
[a987832]139
[6e09bee]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 }
[a987832]152
[1499564]153 printf("%s %s %u %u %u %u\n", type, source, event->x, event->y,
154 event->pressure, event->button);
[a35b458]155
[1499564]156 emit_event(event);
[6e09bee]157}
158
[5bf6bc70]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;
[c4c6025]181 serial_t *serial;
[f66ca57f]182 char *serial_port_name = NULL;
[a987832]183
[5bf6bc70]184 int arg = 1;
[b7fd2a0]185 errno_t rc;
[a987832]186
[1499564]187 isdv4_event_fn event_fn = emit_event;
188
[5bf6bc70]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 }
[a987832]206
[1499564]207 if (argc > arg && str_cmp(argv[arg], "--print-events") == 0) {
208 event_fn = print_and_emit_event;
209 arg++;
210 }
211
[5bf6bc70]212 if (argc > arg) {
[f66ca57f]213 serial_port_name = argv[arg];
214 rc = loc_service_get_id(serial_port_name, &svc_id, 0);
[5bf6bc70]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;
[a987832]224
[5bf6bc70]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 }
[a987832]231
[5bf6bc70]232 service_id_t *svc_ids;
233 size_t svc_count;
[a987832]234
[5bf6bc70]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 }
[a987832]239
[5bf6bc70]240 if (svc_count == 0) {
241 fprintf(stderr, "No service in category 'serial'\n");
242 free(svc_ids);
243 return 1;
244 }
[a987832]245
[5bf6bc70]246 svc_id = svc_ids[0];
[f66ca57f]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
[5bf6bc70]254 free(svc_ids);
255 }
[a987832]256
[5bf6bc70]257 if (argc > arg) {
258 fprintf(stderr, "Too many arguments\n");
259 syntax_print();
260 return 1;
261 }
[a987832]262
[1499564]263 fibril_mutex_initialize(&client_mutex);
264
[f66ca57f]265 printf(NAME ": Using serial port %s\n", serial_port_name);
266
[f9b2cb4c]267 async_sess_t *sess = loc_service_connect(svc_id, INTERFACE_DDF,
[5bf6bc70]268 IPC_FLAG_BLOCKING);
269 if (!sess) {
270 fprintf(stderr, "Failed connecting to service\n");
[c4c6025]271 return 2;
[5bf6bc70]272 }
[a987832]273
[c4c6025]274 rc = serial_open(sess, &serial);
275 if (rc != EOK) {
276 fprintf(stderr, "Failed opening serial port\n");
277 return 2;
278 }
[a987832]279
[c4c6025]280 rc = serial_set_comm_props(serial, baud, SERIAL_NO_PARITY, 8, 1);
[5bf6bc70]281 if (rc != EOK) {
282 fprintf(stderr, "Failed setting serial properties\n");
283 return 2;
284 }
[a987832]285
[1499564]286 rc = isdv4_init(&state, sess, event_fn);
[a987832]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));
[a35b458]309
[1499564]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);
[a987832]313
[b688fd8]314 async_set_fallback_port_handler(mouse_connection, NULL);
[1499564]315 rc = loc_server_register(NAME);
[a987832]316 if (rc != EOK) {
[1499564]317 printf("%s: Unable to register driver.\n", NAME);
318 return rc;
[a987832]319 }
320
[1499564]321 service_id_t service_id;
[f66ca57f]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);
[1499564]330 if (rc != EOK) {
[f66ca57f]331 printf(NAME ": Unable to register service %s.\n", service_name);
[1499564]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();
[a987832]350
[1499564]351 /* Not reached */
[5bf6bc70]352 return 0;
353}
Note: See TracBrowser for help on using the repository browser.