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

Last change on this file since 46577995 was 46577995, checked in by Jiří Zárevúcky <zarevucky.jiri@…>, 7 years ago

Use errno_t in all uspace and kernel code.

Change type of every variable, parameter and return value that holds an
<errno.h> constant to either errno_t (the usual case), or sys_errno_t
(some places in kernel). This is for the purpose of self-documentation,
as well as for type-checking with a bit of type definition hackery.

After this commit, HelenOS is free of code that mixes error codes with non-error
values on the assumption that error codes are negative.

  • 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>
[1c635d6]37#include <task.h>
[5bf6bc70]38
[a987832]39#include "isdv4.h"
[5bf6bc70]40
[f66ca57f]41#define NAME "isdv4_tablet"
[1499564]42
43static async_sess_t *client_sess = NULL;
44static fibril_mutex_t client_mutex;
45static isdv4_state_t state;
46
[5bf6bc70]47static void syntax_print(void)
48{
[f66ca57f]49 fprintf(stderr, "Usage: %s [--baud=<baud>] [--print-events] [device_service]\n", NAME);
[1499564]50}
51
[46577995]52static errno_t read_fibril(void *unused)
[1499564]53{
[46577995]54 errno_t rc = isdv4_read_events(&state);
[1499564]55 if (rc != EOK) {
56 fprintf(stderr, "Failed reading events");
57 return rc;
58 }
59
60 isdv4_fini(&state);
61 return EOK;
62}
63
64static 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);
[f9b2cb4c]70
[1499564]71 fibril_mutex_lock(&client_mutex);
[f9b2cb4c]72
73 if (client_sess == NULL)
74 client_sess = sess;
75
[1499564]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 }
[5bf6bc70]87}
88
[1499564]89static 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
115static void print_and_emit_event(const isdv4_event_t *event)
[6e09bee]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";
[1499564]133 break;
134 default:
[6e09bee]135 type = "UNKNOWN";
136 break;
137 }
[a987832]138
[6e09bee]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 }
[a987832]151
[1499564]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);
[6e09bee]156}
157
[5bf6bc70]158static 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
176int main(int argc, char **argv)
177{
178 sysarg_t baud = 38400;
179 service_id_t svc_id;
[c4c6025]180 serial_t *serial;
[f66ca57f]181 char *serial_port_name = NULL;
[a987832]182
[5bf6bc70]183 int arg = 1;
[46577995]184 errno_t rc;
[a987832]185
[1499564]186 isdv4_event_fn event_fn = emit_event;
187
[5bf6bc70]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 }
[a987832]205
[1499564]206 if (argc > arg && str_cmp(argv[arg], "--print-events") == 0) {
207 event_fn = print_and_emit_event;
208 arg++;
209 }
210
[5bf6bc70]211 if (argc > arg) {
[f66ca57f]212 serial_port_name = argv[arg];
213 rc = loc_service_get_id(serial_port_name, &svc_id, 0);
[5bf6bc70]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;
[a987832]223
[5bf6bc70]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 }
[a987832]230
[5bf6bc70]231 service_id_t *svc_ids;
232 size_t svc_count;
[a987832]233
[5bf6bc70]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 }
[a987832]238
[5bf6bc70]239 if (svc_count == 0) {
240 fprintf(stderr, "No service in category 'serial'\n");
241 free(svc_ids);
242 return 1;
243 }
[a987832]244
[5bf6bc70]245 svc_id = svc_ids[0];
[f66ca57f]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
[5bf6bc70]253 free(svc_ids);
254 }
[a987832]255
[5bf6bc70]256 if (argc > arg) {
257 fprintf(stderr, "Too many arguments\n");
258 syntax_print();
259 return 1;
260 }
[a987832]261
[1499564]262 fibril_mutex_initialize(&client_mutex);
263
[f66ca57f]264 printf(NAME ": Using serial port %s\n", serial_port_name);
265
[f9b2cb4c]266 async_sess_t *sess = loc_service_connect(svc_id, INTERFACE_DDF,
[5bf6bc70]267 IPC_FLAG_BLOCKING);
268 if (!sess) {
269 fprintf(stderr, "Failed connecting to service\n");
[c4c6025]270 return 2;
[5bf6bc70]271 }
[a987832]272
[c4c6025]273 rc = serial_open(sess, &serial);
274 if (rc != EOK) {
275 fprintf(stderr, "Failed opening serial port\n");
276 return 2;
277 }
[a987832]278
[c4c6025]279 rc = serial_set_comm_props(serial, baud, SERIAL_NO_PARITY, 8, 1);
[5bf6bc70]280 if (rc != EOK) {
281 fprintf(stderr, "Failed setting serial properties\n");
282 return 2;
283 }
[a987832]284
[1499564]285 rc = isdv4_init(&state, sess, event_fn);
[a987832]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));
[1499564]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);
[a987832]312
[b688fd8]313 async_set_fallback_port_handler(mouse_connection, NULL);
[1499564]314 rc = loc_server_register(NAME);
[a987832]315 if (rc != EOK) {
[1499564]316 printf("%s: Unable to register driver.\n", NAME);
[84a1a54]317 return EXIT_RC(rc);
[a987832]318 }
319
[1499564]320 service_id_t service_id;
[f66ca57f]321 char *service_name;
[84a1a54]322 if (asprintf(&service_name, "mouse/isdv4-%" PRIun, svc_id) < 0) {
[f66ca57f]323 printf(NAME ": Unable to create service name\n");
[84a1a54]324 return EXIT_RC(ENOMEM);
[f66ca57f]325 }
326
327 rc = loc_service_register(service_name, &service_id);
[1499564]328 if (rc != EOK) {
[f66ca57f]329 printf(NAME ": Unable to register service %s.\n", service_name);
[84a1a54]330 return EXIT_RC(rc);
[1499564]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();
[a987832]348
[1499564]349 /* Not reached */
[5bf6bc70]350 return 0;
351}
Note: See TracBrowser for help on using the repository browser.