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