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

ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 4c6fd56 was 4c6fd56, checked in by Jiri Svoboda <jiri@…>, 22 months ago

loc_server_register() should be callable more than once (API only)

Now loc_server_register() returns a pointer to a loc_srv_t object,
that is then passed to loc_service_register() and
loc_service_add_to_cat().

Added loc_server_unregister() that unregisters the server
and frees the loc_srv_t object.

Updated all callers. The implementation, however, is a stub.
It is not actually possible to call loc_server_register() more
than once, yet.

  • Property mode set to 100644
File size: 8.3 KB
Line 
1/*
2 * Copyright (c) 2023 Jiri Svoboda
3 * Copyright (c) 2012 Martin Sucha
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * - Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * - Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * - The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30#include <async.h>
31#include <errno.h>
32#include <fibril_synch.h>
33#include <io/serial.h>
34#include <ipc/mouseev.h>
35#include <loc.h>
36#include <stddef.h>
37#include <stdio.h>
38#include <str.h>
39#include <task.h>
40
41#include "isdv4.h"
42
43#define NAME "isdv4_tablet"
44
45static async_sess_t *client_sess = NULL;
46static fibril_mutex_t client_mutex;
47static isdv4_state_t state;
48
49static void syntax_print(void)
50{
51 fprintf(stderr, "Usage: %s [--baud=<baud>] [--print-events] [device_service]\n", NAME);
52}
53
54static errno_t read_fibril(void *unused)
55{
56 errno_t rc = isdv4_read_events(&state);
57 if (rc != EOK) {
58 fprintf(stderr, "Failed reading events");
59 return rc;
60 }
61
62 isdv4_fini(&state);
63 return EOK;
64}
65
66static void mouse_connection(ipc_call_t *icall, void *arg)
67{
68 async_accept_0(icall);
69
70 async_sess_t *sess =
71 async_callback_receive(EXCHANGE_SERIALIZE);
72
73 fibril_mutex_lock(&client_mutex);
74
75 if (client_sess == NULL)
76 client_sess = sess;
77
78 fibril_mutex_unlock(&client_mutex);
79
80 while (true) {
81 ipc_call_t call;
82 async_get_call(&call);
83
84 if (!ipc_get_imethod(&call)) {
85 async_answer_0(&call, EOK);
86 break;
87 }
88
89 async_answer_0(&call, ENOTSUP);
90 }
91}
92
93static void emit_event(const isdv4_event_t *event)
94{
95 fibril_mutex_lock(&client_mutex);
96 async_sess_t *sess = client_sess;
97 fibril_mutex_unlock(&client_mutex);
98
99 if (!sess)
100 return;
101
102 async_exch_t *exch = async_exchange_begin(sess);
103 if (exch) {
104 unsigned int max_x = state.stylus_max_x;
105 unsigned int max_y = state.stylus_max_y;
106 if (event->source == TOUCH) {
107 max_x = state.touch_max_x;
108 max_y = state.touch_max_y;
109 }
110 async_msg_4(exch, MOUSEEV_ABS_MOVE_EVENT, event->x, event->y,
111 max_x, max_y);
112 if (event->type == PRESS || event->type == RELEASE) {
113 async_msg_2(exch, MOUSEEV_BUTTON_EVENT, event->button,
114 event->type == PRESS);
115 }
116 }
117 async_exchange_end(exch);
118}
119
120static void print_and_emit_event(const isdv4_event_t *event)
121{
122 const char *type = NULL;
123 switch (event->type) {
124 case PRESS:
125 type = "PRESS";
126 break;
127 case RELEASE:
128 type = "RELEASE";
129 break;
130 case PROXIMITY_IN:
131 type = "PROXIMITY IN";
132 break;
133 case PROXIMITY_OUT:
134 type = "PROXIMITY OUT";
135 break;
136 case MOVE:
137 type = "MOVE";
138 break;
139 default:
140 type = "UNKNOWN";
141 break;
142 }
143
144 const char *source = NULL;
145 switch (event->source) {
146 case STYLUS_TIP:
147 source = "stylus tip";
148 break;
149 case STYLUS_ERASER:
150 source = "stylus eraser";
151 break;
152 case TOUCH:
153 source = "touch";
154 break;
155 }
156
157 printf("%s %s %u %u %u %u\n", type, source, event->x, event->y,
158 event->pressure, event->button);
159
160 emit_event(event);
161}
162
163static const char *touch_type(unsigned int data_id)
164{
165 switch (data_id) {
166 case 0:
167 return "resistive+stylus";
168 case 1:
169 return "capacitive+stylus";
170 case 2:
171 return "resistive";
172 case 3:
173 case 4:
174 return "capacitive";
175 case 5:
176 return "penabled";
177 }
178 return "unknown";
179}
180
181int main(int argc, char **argv)
182{
183 sysarg_t baud = 38400;
184 service_id_t svc_id;
185 serial_t *serial;
186 char *serial_port_name = NULL;
187 loc_srv_t *srv;
188
189 int arg = 1;
190 errno_t rc;
191
192 isdv4_event_fn event_fn = emit_event;
193
194 if (argc > arg && str_test_prefix(argv[arg], "--baud=")) {
195 size_t arg_offset = str_lsize(argv[arg], 7);
196 char *arg_str = argv[arg] + arg_offset;
197 if (str_length(arg_str) == 0) {
198 fprintf(stderr, "--baud requires an argument\n");
199 syntax_print();
200 return 1;
201 }
202 char *endptr;
203 baud = strtol(arg_str, &endptr, 10);
204 if (*endptr != '\0') {
205 fprintf(stderr, "Invalid value for baud\n");
206 syntax_print();
207 return 1;
208 }
209 arg++;
210 }
211
212 if (argc > arg && str_cmp(argv[arg], "--print-events") == 0) {
213 event_fn = print_and_emit_event;
214 arg++;
215 }
216
217 if (argc > arg) {
218 serial_port_name = argv[arg];
219 rc = loc_service_get_id(serial_port_name, &svc_id, 0);
220 if (rc != EOK) {
221 fprintf(stderr, "Cannot find device service %s\n",
222 argv[arg]);
223 return 1;
224 }
225 arg++;
226 } else {
227 category_id_t serial_cat_id;
228
229 rc = loc_category_get_id("serial", &serial_cat_id, 0);
230 if (rc != EOK) {
231 fprintf(stderr, "Failed getting id of category "
232 "'serial'\n");
233 return 1;
234 }
235
236 service_id_t *svc_ids;
237 size_t svc_count;
238
239 rc = loc_category_get_svcs(serial_cat_id, &svc_ids, &svc_count);
240 if (rc != EOK) {
241 fprintf(stderr, "Failed getting list of services\n");
242 return 1;
243 }
244
245 if (svc_count == 0) {
246 fprintf(stderr, "No service in category 'serial'\n");
247 free(svc_ids);
248 return 1;
249 }
250
251 svc_id = svc_ids[0];
252
253 rc = loc_service_get_name(svc_id, &serial_port_name);
254 if (rc != EOK) {
255 fprintf(stderr, "Failed getting name of serial service\n");
256 return 1;
257 }
258
259 free(svc_ids);
260 }
261
262 if (argc > arg) {
263 fprintf(stderr, "Too many arguments\n");
264 syntax_print();
265 return 1;
266 }
267
268 fibril_mutex_initialize(&client_mutex);
269
270 printf(NAME ": Using serial port %s\n", serial_port_name);
271
272 async_sess_t *sess = loc_service_connect(svc_id, INTERFACE_DDF,
273 IPC_FLAG_BLOCKING);
274 if (!sess) {
275 fprintf(stderr, "Failed connecting to service\n");
276 return 2;
277 }
278
279 rc = serial_open(sess, &serial);
280 if (rc != EOK) {
281 fprintf(stderr, "Failed opening serial port\n");
282 return 2;
283 }
284
285 rc = serial_set_comm_props(serial, baud, SERIAL_NO_PARITY, 8, 1);
286 if (rc != EOK) {
287 fprintf(stderr, "Failed setting serial properties\n");
288 return 2;
289 }
290
291 rc = isdv4_init(&state, sess, event_fn);
292 if (rc != EOK) {
293 fprintf(stderr, "Failed initializing isdv4 state");
294 return 2;
295 }
296
297 rc = isdv4_init_tablet(&state);
298 if (rc != EOK) {
299 fprintf(stderr, "Failed initializing tablet");
300 return 2;
301 }
302
303 printf("Tablet information:\n");
304 printf(" Stylus: %ux%u pressure: %u tilt: ", state.stylus_max_x,
305 state.stylus_max_y, state.stylus_max_pressure);
306 if (state.stylus_tilt_supported) {
307 printf("%ux%u\n", state.stylus_max_xtilt, state.stylus_max_ytilt);
308 } else {
309 printf("not supported\n");
310 }
311 printf(" Touch: %ux%u type: %s\n", state.touch_max_x, state.touch_max_y,
312 touch_type(state.touch_type));
313
314 fid_t fibril = fibril_create(read_fibril, NULL);
315 /* From this on, state is to be used only by read_fibril */
316 fibril_add_ready(fibril);
317
318 async_set_fallback_port_handler(mouse_connection, NULL);
319 rc = loc_server_register(NAME, &srv);
320 if (rc != EOK) {
321 printf("%s: Unable to register driver.\n", NAME);
322 return rc;
323 }
324
325 service_id_t service_id;
326 char *service_name;
327 rc = asprintf(&service_name, "mouse/isdv4-%" PRIun, svc_id);
328 if (rc < 0) {
329 printf(NAME ": Unable to create service name\n");
330 return rc;
331 }
332
333 rc = loc_service_register(srv, service_name, &service_id);
334 if (rc != EOK) {
335 loc_server_unregister(srv);
336 printf(NAME ": Unable to register service %s.\n", service_name);
337 return rc;
338 }
339
340 category_id_t mouse_category;
341 rc = loc_category_get_id("mouse", &mouse_category, IPC_FLAG_BLOCKING);
342 if (rc != EOK) {
343 printf(NAME ": Unable to get mouse category id.\n");
344 } else {
345 rc = loc_service_add_to_cat(srv, service_id, mouse_category);
346 if (rc != EOK) {
347 printf(NAME ": Unable to add device to mouse category.\n");
348 }
349 }
350
351 printf("%s: Accepting connections\n", NAME);
352 task_retval(0);
353 async_manager();
354
355 /* Not reached */
356 return 0;
357}
Note: See TracBrowser for help on using the repository browser.