[1499564] | 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>
|
---|
[fec7ba0] | 30 | #include <fibril.h>
|
---|
[74017ce] | 31 | #include <io/chardev.h>
|
---|
| 32 | #include <mem.h>
|
---|
[c4c6025] | 33 | #include <stdbool.h>
|
---|
| 34 | #include <stdint.h>
|
---|
[1499564] | 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 |
|
---|
[e8975278] | 56 | /** Packet consumer function
|
---|
| 57 | *
|
---|
| 58 | * @return true if reading of packets should continue
|
---|
| 59 | */
|
---|
| 60 | typedef bool (*packet_consumer_fn)(uint8_t *packet, size_t size,
|
---|
| 61 | isdv4_state_t *state);
|
---|
[1499564] | 62 |
|
---|
| 63 | static 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 | */
|
---|
| 72 | static 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);
|
---|
[1433ecda] | 102 | } else if (finger1 && !state->finger1_pressed) {
|
---|
[1499564] | 103 | state->finger1_pressed = true;
|
---|
| 104 |
|
---|
| 105 | event.type = PRESS;
|
---|
| 106 | event.button = 1;
|
---|
| 107 | state->emit_event_fn(&event);
|
---|
[1433ecda] | 108 | } else {
|
---|
[1499564] | 109 | event.type = MOVE;
|
---|
| 110 | event.button = 1;
|
---|
| 111 | state->emit_event_fn(&event);
|
---|
| 112 | }
|
---|
| 113 | }
|
---|
[1433ecda] | 114 | } else {
|
---|
[1499564] | 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);
|
---|
[1433ecda] | 134 | } else if (!proximity && state->stylus_in_proximity) {
|
---|
[1499564] | 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);
|
---|
[1433ecda] | 140 | } else {
|
---|
[1499564] | 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;
|
---|
[1433ecda] | 150 | } else if (!state->stylus_is_eraser && !tip && button2) {
|
---|
[1499564] | 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);
|
---|
[1433ecda] | 168 | } else if (!tip && state->tip_pressed) {
|
---|
[1499564] | 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);
|
---|
[1433ecda] | 181 | } else if (!button1 && state->button1_pressed) {
|
---|
[1499564] | 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);
|
---|
[1433ecda] | 194 | } else if (!button2 && state->button2_pressed) {
|
---|
[1499564] | 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);
|
---|
[1433ecda] | 205 | } else {
|
---|
[1499564] | 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);
|
---|
[1433ecda] | 212 | } else if (!tip && state->tip_pressed) {
|
---|
[1499564] | 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 |
|
---|
| 229 | static 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 |
|
---|
| 255 | static 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);
|
---|
[a35b458] | 275 |
|
---|
[1499564] | 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 |
|
---|
[b7fd2a0] | 287 | static errno_t read_packets(isdv4_state_t *state, packet_consumer_fn consumer)
|
---|
[1499564] | 288 | {
|
---|
| 289 | bool reading = true;
|
---|
| 290 | while (reading) {
|
---|
[74017ce] | 291 | size_t nread;
|
---|
[b7fd2a0] | 292 | errno_t rc;
|
---|
[74017ce] | 293 |
|
---|
| 294 | rc = chardev_read(state->chardev, state->buf + state->buf_end,
|
---|
[f2d88f3] | 295 | state->buf_size - state->buf_end, &nread, chardev_f_none);
|
---|
[74017ce] | 296 | if (rc != EOK && nread == 0)
|
---|
[1499564] | 297 | return EIO;
|
---|
[74017ce] | 298 | state->buf_end += nread;
|
---|
[1499564] | 299 |
|
---|
| 300 | size_t i = 0;
|
---|
| 301 |
|
---|
| 302 | /* Skip data until a start of packet is found */
|
---|
[1433ecda] | 303 | while (i < state->buf_end && (state->buf[i] & START_OF_PACKET) == 0)
|
---|
| 304 | i++;
|
---|
[1499564] | 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;
|
---|
[1433ecda] | 316 | } else if (state->buf[i] & TOUCH_EVENT) {
|
---|
[1499564] | 317 | packet_remaining = 5;
|
---|
[1433ecda] | 318 | } else {
|
---|
[1499564] | 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 */
|
---|
[1b20da0] | 324 | packet_remaining--;
|
---|
[1499564] | 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 | }
|
---|
[74017ce] | 352 |
|
---|
| 353 | static bool write_command(chardev_t *chardev, uint8_t command)
|
---|
[1499564] | 354 | {
|
---|
[b7fd2a0] | 355 | errno_t rc;
|
---|
[74017ce] | 356 | size_t nwr;
|
---|
| 357 |
|
---|
| 358 | rc = chardev_write(chardev, &command, 1, &nwr);
|
---|
| 359 | return rc == EOK;
|
---|
[1499564] | 360 | }
|
---|
| 361 |
|
---|
[b7fd2a0] | 362 | errno_t isdv4_init(isdv4_state_t *state, async_sess_t *sess,
|
---|
[1499564] | 363 | isdv4_event_fn event_fn)
|
---|
| 364 | {
|
---|
[74017ce] | 365 | chardev_t *chardev;
|
---|
[b7fd2a0] | 366 | errno_t rc;
|
---|
[74017ce] | 367 |
|
---|
| 368 | rc = chardev_open(sess, &chardev);
|
---|
| 369 | if (rc != EOK)
|
---|
| 370 | return rc;
|
---|
| 371 |
|
---|
[1499564] | 372 | memset(state, 0, sizeof(isdv4_state_t));
|
---|
[74017ce] | 373 |
|
---|
[1499564] | 374 | state->sess = sess;
|
---|
[74017ce] | 375 | state->chardev = chardev;
|
---|
| 376 |
|
---|
[1499564] | 377 | state->buf = malloc(BUF_SIZE);
|
---|
[74017ce] | 378 | if (state->buf == NULL) {
|
---|
| 379 | chardev_close(chardev);
|
---|
[1499564] | 380 | return ENOMEM;
|
---|
[74017ce] | 381 | }
|
---|
| 382 |
|
---|
[1499564] | 383 | state->buf_size = BUF_SIZE;
|
---|
| 384 | state->emit_event_fn = event_fn;
|
---|
| 385 | return EOK;
|
---|
| 386 | }
|
---|
| 387 |
|
---|
[b7fd2a0] | 388 | errno_t isdv4_init_tablet(isdv4_state_t *state)
|
---|
[1499564] | 389 | {
|
---|
[74017ce] | 390 | if (!write_command(state->chardev, CMD_STOP))
|
---|
[1499564] | 391 | return EIO;
|
---|
| 392 |
|
---|
[5f97ef44] | 393 | fibril_usleep(250000); /* 250 ms */
|
---|
[1499564] | 394 |
|
---|
| 395 | // FIXME: Read all possible garbage before sending commands
|
---|
[74017ce] | 396 | if (!write_command(state->chardev, CMD_QUERY_STYLUS))
|
---|
[1499564] | 397 | return EIO;
|
---|
| 398 |
|
---|
[b7fd2a0] | 399 | errno_t rc = read_packets(state, parse_response_stylus);
|
---|
[1499564] | 400 | if (rc != EOK)
|
---|
| 401 | return rc;
|
---|
| 402 |
|
---|
[74017ce] | 403 | if (!write_command(state->chardev, CMD_QUERY_TOUCH))
|
---|
[1499564] | 404 | return EIO;
|
---|
| 405 |
|
---|
| 406 | rc = read_packets(state, parse_response_touch);
|
---|
| 407 | if (rc != EOK)
|
---|
| 408 | return rc;
|
---|
| 409 |
|
---|
[74017ce] | 410 | if (!write_command(state->chardev, CMD_START))
|
---|
[1499564] | 411 | return EIO;
|
---|
| 412 |
|
---|
| 413 | return EOK;
|
---|
| 414 | }
|
---|
| 415 |
|
---|
[b7fd2a0] | 416 | errno_t isdv4_read_events(isdv4_state_t *state)
|
---|
[1499564] | 417 | {
|
---|
| 418 | return read_packets(state, parse_event);
|
---|
| 419 | }
|
---|
| 420 |
|
---|
| 421 | void isdv4_fini(isdv4_state_t *state)
|
---|
| 422 | {
|
---|
| 423 | free(state->buf);
|
---|
| 424 | }
|
---|