source: mainline/uspace/srv/hid/isdv4_tablet/isdv4.c

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

Avoid including <fibril.h> from <async.h>

  • Property mode set to 100644
File size: 11.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 <errno.h>
30#include <fibril.h>
31#include <io/chardev.h>
32#include <mem.h>
33#include <stdbool.h>
34#include <stdint.h>
35#include <stdlib.h>
36
37#include "isdv4.h"
38
39#define BUF_SIZE 64
40
41#define START_OF_PACKET 128
42#define CONTROL_PACKET 64
43#define TOUCH_EVENT 16
44#define FINGER1 1
45#define FINGER2 2
46#define TIP 1
47#define BUTTON1 2
48#define BUTTON2 4
49#define PROXIMITY 32
50
51#define CMD_START '1'
52#define CMD_STOP '0'
53#define CMD_QUERY_STYLUS '*'
54#define CMD_QUERY_TOUCH '%'
55
56/** Packet consumer function
57 *
58 * @return true if reading of packets should continue
59 */
60typedef bool (*packet_consumer_fn)(uint8_t *packet, size_t size,
61 isdv4_state_t *state);
62
63static void isdv4_event_init(isdv4_event_t *event)
64{
65 memset(event, 0, sizeof(isdv4_event_t));
66}
67
68/**
69 * Parse event packet and emit events
70 * @return true if reading of packets should continue
71 */
72static bool parse_event(uint8_t *packet, size_t size, isdv4_state_t *state)
73{
74 if (size < 1)
75 return false;
76
77 bool control_packet = ((packet[0] & CONTROL_PACKET) > 0);
78 if (control_packet)
79 return true;
80
81 /* This is an event initiated by the device */
82 isdv4_event_t event;
83 isdv4_event_init(&event);
84
85 if (packet[0] & TOUCH_EVENT) {
86 if (size != 5)
87 return true;
88
89 /* This is a touch event */
90 bool finger1 = (packet[0] & FINGER1) > 0;
91 event.x = ((packet[1] & 127) << 7) | (packet[2] & 127);
92 event.y = ((packet[3] & 127) << 7) | (packet[4] & 127);
93 event.source = TOUCH;
94
95 if (!state->stylus_in_proximity) {
96 if (!finger1 && state->finger1_pressed) {
97 state->finger1_pressed = false;
98
99 event.type = RELEASE;
100 event.button = 1;
101 state->emit_event_fn(&event);
102 } else if (finger1 && !state->finger1_pressed) {
103 state->finger1_pressed = true;
104
105 event.type = PRESS;
106 event.button = 1;
107 state->emit_event_fn(&event);
108 } else {
109 event.type = MOVE;
110 event.button = 1;
111 state->emit_event_fn(&event);
112 }
113 }
114 } else {
115 if (size != 9)
116 return true;
117
118 /* This is a stylus event */
119 bool tip = packet[0] & TIP;
120 bool button1 = packet[0] & BUTTON1;
121 bool button2 = packet[0] & BUTTON2;
122 bool proximity = packet[0] & PROXIMITY;
123 event.x = ((packet[1] & 127) << 7) | (packet[2] & 124) | ((packet[6] >> 5) & 3);
124 event.y = ((packet[3] & 127) << 7) | (packet[4] & 124) | ((packet[6] >> 3) & 3);
125 event.pressure = (packet[5] & 127) | ((packet[6] & 7) << 7);
126
127 if (proximity && !state->stylus_in_proximity) {
128 /* Stylus came into proximity */
129 state->stylus_in_proximity = true;
130 state->stylus_is_eraser = !tip && button2;
131 event.source = (state->stylus_is_eraser ? STYLUS_ERASER : STYLUS_TIP);
132 event.type = PROXIMITY_IN;
133 state->emit_event_fn(&event);
134 } else if (!proximity && state->stylus_in_proximity) {
135 /* Stylus came out of proximity */
136 state->stylus_in_proximity = false;
137 event.source = (state->stylus_is_eraser ? STYLUS_ERASER : STYLUS_TIP);
138 event.type = PROXIMITY_OUT;
139 state->emit_event_fn(&event);
140 } else {
141 /* Proximity state didn't change, but we need to check if it is still eraser */
142 if (state->stylus_is_eraser && !button2) {
143 event.type = PROXIMITY_OUT;
144 event.source = STYLUS_ERASER;
145 state->emit_event_fn(&event);
146 event.type = PROXIMITY_IN;
147 event.source = STYLUS_TIP;
148 state->emit_event_fn(&event);
149 state->stylus_is_eraser = false;
150 } else if (!state->stylus_is_eraser && !tip && button2) {
151 event.type = PROXIMITY_OUT;
152 event.source = STYLUS_TIP;
153 state->emit_event_fn(&event);
154 event.type = PROXIMITY_IN;
155 event.source = STYLUS_ERASER;
156 state->emit_event_fn(&event);
157 state->stylus_is_eraser = true;
158 }
159 }
160
161 if (!state->stylus_is_eraser) {
162 if (tip && !state->tip_pressed) {
163 state->tip_pressed = true;
164 event.type = PRESS;
165 event.source = STYLUS_TIP;
166 event.button = 1;
167 state->emit_event_fn(&event);
168 } else if (!tip && state->tip_pressed) {
169 state->tip_pressed = false;
170 event.type = RELEASE;
171 event.source = STYLUS_TIP;
172 event.button = 1;
173 state->emit_event_fn(&event);
174 }
175 if (button1 && !state->button1_pressed) {
176 state->button1_pressed = true;
177 event.type = PRESS;
178 event.source = STYLUS_TIP;
179 event.button = 2;
180 state->emit_event_fn(&event);
181 } else if (!button1 && state->button1_pressed) {
182 state->button1_pressed = false;
183 event.type = RELEASE;
184 event.source = STYLUS_TIP;
185 event.button = 2;
186 state->emit_event_fn(&event);
187 }
188 if (button2 && !state->button2_pressed) {
189 state->button2_pressed = true;
190 event.type = PRESS;
191 event.source = STYLUS_TIP;
192 event.button = 3;
193 state->emit_event_fn(&event);
194 } else if (!button2 && state->button2_pressed) {
195 state->button2_pressed = false;
196 event.type = RELEASE;
197 event.source = STYLUS_TIP;
198 event.button = 3;
199 state->emit_event_fn(&event);
200 }
201 event.type = MOVE;
202 event.source = STYLUS_TIP;
203 event.button = 0;
204 state->emit_event_fn(&event);
205 } else {
206 if (tip && !state->tip_pressed) {
207 state->tip_pressed = true;
208 event.type = PRESS;
209 event.source = STYLUS_ERASER;
210 event.button = 1;
211 state->emit_event_fn(&event);
212 } else if (!tip && state->tip_pressed) {
213 state->tip_pressed = false;
214 event.type = RELEASE;
215 event.source = STYLUS_ERASER;
216 event.button = 1;
217 state->emit_event_fn(&event);
218 }
219 event.type = MOVE;
220 event.source = STYLUS_ERASER;
221 event.button = 0;
222 state->emit_event_fn(&event);
223 }
224 }
225
226 return true;
227}
228
229static bool parse_response_stylus(uint8_t *packet, size_t size,
230 isdv4_state_t *state)
231{
232 if (size < 1)
233 return false;
234
235 bool control_packet = ((packet[0] & CONTROL_PACKET) > 0);
236 if (!control_packet)
237 return true;
238
239 if (size != 11)
240 return false;
241
242 state->stylus_max_x = ((packet[1] & 127) << 7) | (packet[2] & 124) |
243 ((packet[6] >> 5) & 3);
244 state->stylus_max_y = ((packet[3] & 127) << 7) | (packet[4] & 124) |
245 ((packet[6] >> 3) & 3);
246 state->stylus_max_pressure = (packet[5] & 63) | ((packet[6] & 7) << 7);
247 state->stylus_max_xtilt = packet[8] & 127;
248 state->stylus_max_ytilt = packet[7] & 127;
249 state->stylus_tilt_supported = (state->stylus_max_xtilt &&
250 state->stylus_max_ytilt);
251
252 return false;
253}
254
255static bool parse_response_touch(uint8_t *packet, size_t size,
256 isdv4_state_t *state)
257{
258 if (size < 1)
259 return false;
260
261 bool control_packet = ((packet[0] & CONTROL_PACKET) > 0);
262 if (!control_packet)
263 return true;
264
265 if (size != 11)
266 return false;
267
268 state->touch_type = (packet[0] & 63);
269
270 unsigned int touch_resolution = packet[1] & 127;
271 state->touch_max_x = ((packet[2] >> 5) & 3) | ((packet[3] & 127) << 7) |
272 (packet[4] & 124);
273 state->touch_max_y = ((packet[2] >> 3) & 3) | ((packet[5] & 127) << 7) |
274 (packet[6] & 124);
275
276 if (touch_resolution == 0)
277 touch_resolution = 10;
278
279 if (state->touch_max_x == 0 || state->touch_max_y == 0) {
280 state->touch_max_x = (1 << touch_resolution);
281 state->touch_max_y = (1 << touch_resolution);
282 }
283
284 return false;
285}
286
287static errno_t read_packets(isdv4_state_t *state, packet_consumer_fn consumer)
288{
289 bool reading = true;
290 while (reading) {
291 size_t nread;
292 errno_t rc;
293
294 rc = chardev_read(state->chardev, state->buf + state->buf_end,
295 state->buf_size - state->buf_end, &nread, chardev_f_none);
296 if (rc != EOK && nread == 0)
297 return EIO;
298 state->buf_end += nread;
299
300 size_t i = 0;
301
302 /* Skip data until a start of packet is found */
303 while (i < state->buf_end && (state->buf[i] & START_OF_PACKET) == 0)
304 i++;
305
306 size_t start = i;
307 size_t end = i;
308 size_t processed_end = i;
309
310 /* Process packets one by one */
311 while (reading && i < state->buf_end) {
312 /* Determine the packet length */
313 size_t packet_remaining;
314 if (state->buf[i] & CONTROL_PACKET) {
315 packet_remaining = 11;
316 } else if (state->buf[i] & TOUCH_EVENT) {
317 packet_remaining = 5;
318 } else {
319 packet_remaining = 9;
320 }
321
322 /* Find the end of the packet */
323 i++; /* We need to skip the first byte with START_OF_PACKET set */
324 packet_remaining--;
325 while (packet_remaining > 0 && i < state->buf_end &&
326 (state->buf[i] & START_OF_PACKET) == 0) {
327 i++;
328 packet_remaining--;
329 }
330 end = i;
331
332 /* If we have whole packet, process it */
333 if (end > start && packet_remaining == 0) {
334 reading = consumer(state->buf + start, end - start, state);
335 start = end;
336 processed_end = end;
337 }
338 }
339
340 if (processed_end == 0 && state->buf_end == state->buf_size) {
341 /* Packet too large, throw it away */
342 state->buf_end = 0;
343 }
344
345 /* Shift the buffer contents to the left */
346 size_t unprocessed_len = state->buf_end - processed_end;
347 memcpy(state->buf, state->buf + processed_end, unprocessed_len);
348 state->buf_end = unprocessed_len;
349 }
350 return EOK;
351}
352
353static bool write_command(chardev_t *chardev, uint8_t command)
354{
355 errno_t rc;
356 size_t nwr;
357
358 rc = chardev_write(chardev, &command, 1, &nwr);
359 return rc == EOK;
360}
361
362errno_t isdv4_init(isdv4_state_t *state, async_sess_t *sess,
363 isdv4_event_fn event_fn)
364{
365 chardev_t *chardev;
366 errno_t rc;
367
368 rc = chardev_open(sess, &chardev);
369 if (rc != EOK)
370 return rc;
371
372 memset(state, 0, sizeof(isdv4_state_t));
373
374 state->sess = sess;
375 state->chardev = chardev;
376
377 state->buf = malloc(BUF_SIZE);
378 if (state->buf == NULL) {
379 chardev_close(chardev);
380 return ENOMEM;
381 }
382
383 state->buf_size = BUF_SIZE;
384 state->emit_event_fn = event_fn;
385 return EOK;
386}
387
388errno_t isdv4_init_tablet(isdv4_state_t *state)
389{
390 if (!write_command(state->chardev, CMD_STOP))
391 return EIO;
392
393 fibril_usleep(250000); /* 250 ms */
394
395 // FIXME: Read all possible garbage before sending commands
396 if (!write_command(state->chardev, CMD_QUERY_STYLUS))
397 return EIO;
398
399 errno_t rc = read_packets(state, parse_response_stylus);
400 if (rc != EOK)
401 return rc;
402
403 if (!write_command(state->chardev, CMD_QUERY_TOUCH))
404 return EIO;
405
406 rc = read_packets(state, parse_response_touch);
407 if (rc != EOK)
408 return rc;
409
410 if (!write_command(state->chardev, CMD_START))
411 return EIO;
412
413 return EOK;
414}
415
416errno_t isdv4_read_events(isdv4_state_t *state)
417{
418 return read_packets(state, parse_event);
419}
420
421void isdv4_fini(isdv4_state_t *state)
422{
423 free(state->buf);
424}
Note: See TracBrowser for help on using the repository browser.