[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>
|
---|
[1d6dd2a] | 37 | #include <str.h>
|
---|
[1c635d6] | 38 | #include <task.h>
|
---|
[5bf6bc70] | 39 |
|
---|
[a987832] | 40 | #include "isdv4.h"
|
---|
[5bf6bc70] | 41 |
|
---|
[f66ca57f] | 42 | #define NAME "isdv4_tablet"
|
---|
[1499564] | 43 |
|
---|
| 44 | static async_sess_t *client_sess = NULL;
|
---|
| 45 | static fibril_mutex_t client_mutex;
|
---|
| 46 | static isdv4_state_t state;
|
---|
| 47 |
|
---|
[5bf6bc70] | 48 | static void syntax_print(void)
|
---|
| 49 | {
|
---|
[f66ca57f] | 50 | fprintf(stderr, "Usage: %s [--baud=<baud>] [--print-events] [device_service]\n", NAME);
|
---|
[1499564] | 51 | }
|
---|
| 52 |
|
---|
[b7fd2a0] | 53 | static errno_t read_fibril(void *unused)
|
---|
[1499564] | 54 | {
|
---|
[b7fd2a0] | 55 | errno_t rc = isdv4_read_events(&state);
|
---|
[1499564] | 56 | if (rc != EOK) {
|
---|
| 57 | fprintf(stderr, "Failed reading events");
|
---|
| 58 | return rc;
|
---|
| 59 | }
|
---|
| 60 |
|
---|
| 61 | isdv4_fini(&state);
|
---|
| 62 | return EOK;
|
---|
| 63 | }
|
---|
| 64 |
|
---|
[984a9ba] | 65 | static void mouse_connection(ipc_call_t *icall, void *arg)
|
---|
[1499564] | 66 | {
|
---|
[beb83c1] | 67 | async_accept_0(icall);
|
---|
[a35b458] | 68 |
|
---|
[1499564] | 69 | async_sess_t *sess =
|
---|
| 70 | async_callback_receive(EXCHANGE_SERIALIZE);
|
---|
[a35b458] | 71 |
|
---|
[1499564] | 72 | fibril_mutex_lock(&client_mutex);
|
---|
[a35b458] | 73 |
|
---|
[f9b2cb4c] | 74 | if (client_sess == NULL)
|
---|
| 75 | client_sess = sess;
|
---|
[a35b458] | 76 |
|
---|
[1499564] | 77 | fibril_mutex_unlock(&client_mutex);
|
---|
| 78 |
|
---|
| 79 | while (true) {
|
---|
| 80 | ipc_call_t call;
|
---|
[984a9ba] | 81 | async_get_call(&call);
|
---|
[a35b458] | 82 |
|
---|
[fafb8e5] | 83 | if (!ipc_get_imethod(&call)) {
|
---|
[889cdb1] | 84 | async_answer_0(&call, EOK);
|
---|
[1499564] | 85 | break;
|
---|
[889cdb1] | 86 | }
|
---|
[a35b458] | 87 |
|
---|
[984a9ba] | 88 | async_answer_0(&call, ENOTSUP);
|
---|
[1499564] | 89 | }
|
---|
[5bf6bc70] | 90 | }
|
---|
| 91 |
|
---|
[1499564] | 92 | static void emit_event(const isdv4_event_t *event)
|
---|
| 93 | {
|
---|
| 94 | fibril_mutex_lock(&client_mutex);
|
---|
| 95 | async_sess_t *sess = client_sess;
|
---|
| 96 | fibril_mutex_unlock(&client_mutex);
|
---|
[a35b458] | 97 |
|
---|
[1433ecda] | 98 | if (!sess)
|
---|
| 99 | return;
|
---|
[a35b458] | 100 |
|
---|
[1499564] | 101 | async_exch_t *exch = async_exchange_begin(sess);
|
---|
| 102 | if (exch) {
|
---|
| 103 | unsigned int max_x = state.stylus_max_x;
|
---|
| 104 | unsigned int max_y = state.stylus_max_y;
|
---|
| 105 | if (event->source == TOUCH) {
|
---|
| 106 | max_x = state.touch_max_x;
|
---|
| 107 | max_y = state.touch_max_y;
|
---|
| 108 | }
|
---|
| 109 | async_msg_4(exch, MOUSEEV_ABS_MOVE_EVENT, event->x, event->y,
|
---|
[1433ecda] | 110 | max_x, max_y);
|
---|
[1499564] | 111 | if (event->type == PRESS || event->type == RELEASE) {
|
---|
| 112 | async_msg_2(exch, MOUSEEV_BUTTON_EVENT, event->button,
|
---|
[1433ecda] | 113 | event->type == PRESS);
|
---|
[1499564] | 114 | }
|
---|
| 115 | }
|
---|
| 116 | async_exchange_end(exch);
|
---|
| 117 | }
|
---|
| 118 |
|
---|
| 119 | static void print_and_emit_event(const isdv4_event_t *event)
|
---|
[6e09bee] | 120 | {
|
---|
| 121 | const char *type = NULL;
|
---|
| 122 | switch (event->type) {
|
---|
[1433ecda] | 123 | case PRESS:
|
---|
| 124 | type = "PRESS";
|
---|
| 125 | break;
|
---|
| 126 | case RELEASE:
|
---|
| 127 | type = "RELEASE";
|
---|
| 128 | break;
|
---|
| 129 | case PROXIMITY_IN:
|
---|
| 130 | type = "PROXIMITY IN";
|
---|
| 131 | break;
|
---|
| 132 | case PROXIMITY_OUT:
|
---|
| 133 | type = "PROXIMITY OUT";
|
---|
| 134 | break;
|
---|
| 135 | case MOVE:
|
---|
| 136 | type = "MOVE";
|
---|
| 137 | break;
|
---|
| 138 | default:
|
---|
| 139 | type = "UNKNOWN";
|
---|
| 140 | break;
|
---|
[6e09bee] | 141 | }
|
---|
[a987832] | 142 |
|
---|
[6e09bee] | 143 | const char *source = NULL;
|
---|
| 144 | switch (event->source) {
|
---|
[1433ecda] | 145 | case STYLUS_TIP:
|
---|
| 146 | source = "stylus tip";
|
---|
| 147 | break;
|
---|
| 148 | case STYLUS_ERASER:
|
---|
| 149 | source = "stylus eraser";
|
---|
| 150 | break;
|
---|
| 151 | case TOUCH:
|
---|
| 152 | source = "touch";
|
---|
| 153 | break;
|
---|
[6e09bee] | 154 | }
|
---|
[a987832] | 155 |
|
---|
[1499564] | 156 | printf("%s %s %u %u %u %u\n", type, source, event->x, event->y,
|
---|
| 157 | event->pressure, event->button);
|
---|
[a35b458] | 158 |
|
---|
[1499564] | 159 | emit_event(event);
|
---|
[6e09bee] | 160 | }
|
---|
| 161 |
|
---|
[5bf6bc70] | 162 | static const char *touch_type(unsigned int data_id)
|
---|
| 163 | {
|
---|
| 164 | switch (data_id) {
|
---|
[1433ecda] | 165 | case 0:
|
---|
| 166 | return "resistive+stylus";
|
---|
| 167 | case 1:
|
---|
| 168 | return "capacitive+stylus";
|
---|
| 169 | case 2:
|
---|
| 170 | return "resistive";
|
---|
| 171 | case 3:
|
---|
| 172 | case 4:
|
---|
| 173 | return "capacitive";
|
---|
| 174 | case 5:
|
---|
| 175 | return "penabled";
|
---|
[5bf6bc70] | 176 | }
|
---|
| 177 | return "unknown";
|
---|
| 178 | }
|
---|
| 179 |
|
---|
| 180 | int main(int argc, char **argv)
|
---|
| 181 | {
|
---|
| 182 | sysarg_t baud = 38400;
|
---|
| 183 | service_id_t svc_id;
|
---|
[c4c6025] | 184 | serial_t *serial;
|
---|
[f66ca57f] | 185 | char *serial_port_name = NULL;
|
---|
[a987832] | 186 |
|
---|
[5bf6bc70] | 187 | int arg = 1;
|
---|
[b7fd2a0] | 188 | errno_t rc;
|
---|
[a987832] | 189 |
|
---|
[1499564] | 190 | isdv4_event_fn event_fn = emit_event;
|
---|
| 191 |
|
---|
[5bf6bc70] | 192 | if (argc > arg && str_test_prefix(argv[arg], "--baud=")) {
|
---|
| 193 | size_t arg_offset = str_lsize(argv[arg], 7);
|
---|
[1433ecda] | 194 | char *arg_str = argv[arg] + arg_offset;
|
---|
[5bf6bc70] | 195 | if (str_length(arg_str) == 0) {
|
---|
| 196 | fprintf(stderr, "--baud requires an argument\n");
|
---|
| 197 | syntax_print();
|
---|
| 198 | return 1;
|
---|
| 199 | }
|
---|
| 200 | char *endptr;
|
---|
| 201 | baud = strtol(arg_str, &endptr, 10);
|
---|
| 202 | if (*endptr != '\0') {
|
---|
| 203 | fprintf(stderr, "Invalid value for baud\n");
|
---|
| 204 | syntax_print();
|
---|
| 205 | return 1;
|
---|
| 206 | }
|
---|
| 207 | arg++;
|
---|
| 208 | }
|
---|
[a987832] | 209 |
|
---|
[1499564] | 210 | if (argc > arg && str_cmp(argv[arg], "--print-events") == 0) {
|
---|
| 211 | event_fn = print_and_emit_event;
|
---|
| 212 | arg++;
|
---|
| 213 | }
|
---|
| 214 |
|
---|
[5bf6bc70] | 215 | if (argc > arg) {
|
---|
[f66ca57f] | 216 | serial_port_name = argv[arg];
|
---|
| 217 | rc = loc_service_get_id(serial_port_name, &svc_id, 0);
|
---|
[5bf6bc70] | 218 | if (rc != EOK) {
|
---|
| 219 | fprintf(stderr, "Cannot find device service %s\n",
|
---|
| 220 | argv[arg]);
|
---|
| 221 | return 1;
|
---|
| 222 | }
|
---|
| 223 | arg++;
|
---|
[1433ecda] | 224 | } else {
|
---|
[5bf6bc70] | 225 | category_id_t serial_cat_id;
|
---|
[a987832] | 226 |
|
---|
[5bf6bc70] | 227 | rc = loc_category_get_id("serial", &serial_cat_id, 0);
|
---|
| 228 | if (rc != EOK) {
|
---|
| 229 | fprintf(stderr, "Failed getting id of category "
|
---|
| 230 | "'serial'\n");
|
---|
| 231 | return 1;
|
---|
| 232 | }
|
---|
[a987832] | 233 |
|
---|
[5bf6bc70] | 234 | service_id_t *svc_ids;
|
---|
| 235 | size_t svc_count;
|
---|
[a987832] | 236 |
|
---|
[1433ecda] | 237 | rc = loc_category_get_svcs(serial_cat_id, &svc_ids, &svc_count);
|
---|
| 238 | if (rc != EOK) {
|
---|
[5bf6bc70] | 239 | fprintf(stderr, "Failed getting list of services\n");
|
---|
| 240 | return 1;
|
---|
| 241 | }
|
---|
[a987832] | 242 |
|
---|
[5bf6bc70] | 243 | if (svc_count == 0) {
|
---|
| 244 | fprintf(stderr, "No service in category 'serial'\n");
|
---|
| 245 | free(svc_ids);
|
---|
| 246 | return 1;
|
---|
| 247 | }
|
---|
[a987832] | 248 |
|
---|
[5bf6bc70] | 249 | svc_id = svc_ids[0];
|
---|
[f66ca57f] | 250 |
|
---|
| 251 | rc = loc_service_get_name(svc_id, &serial_port_name);
|
---|
| 252 | if (rc != EOK) {
|
---|
| 253 | fprintf(stderr, "Failed getting name of serial service\n");
|
---|
| 254 | return 1;
|
---|
| 255 | }
|
---|
| 256 |
|
---|
[5bf6bc70] | 257 | free(svc_ids);
|
---|
| 258 | }
|
---|
[a987832] | 259 |
|
---|
[5bf6bc70] | 260 | if (argc > arg) {
|
---|
| 261 | fprintf(stderr, "Too many arguments\n");
|
---|
| 262 | syntax_print();
|
---|
| 263 | return 1;
|
---|
| 264 | }
|
---|
[a987832] | 265 |
|
---|
[1499564] | 266 | fibril_mutex_initialize(&client_mutex);
|
---|
| 267 |
|
---|
[f66ca57f] | 268 | printf(NAME ": Using serial port %s\n", serial_port_name);
|
---|
| 269 |
|
---|
[f9b2cb4c] | 270 | async_sess_t *sess = loc_service_connect(svc_id, INTERFACE_DDF,
|
---|
[5bf6bc70] | 271 | IPC_FLAG_BLOCKING);
|
---|
| 272 | if (!sess) {
|
---|
| 273 | fprintf(stderr, "Failed connecting to service\n");
|
---|
[c4c6025] | 274 | return 2;
|
---|
[5bf6bc70] | 275 | }
|
---|
[a987832] | 276 |
|
---|
[c4c6025] | 277 | rc = serial_open(sess, &serial);
|
---|
| 278 | if (rc != EOK) {
|
---|
| 279 | fprintf(stderr, "Failed opening serial port\n");
|
---|
| 280 | return 2;
|
---|
| 281 | }
|
---|
[a987832] | 282 |
|
---|
[c4c6025] | 283 | rc = serial_set_comm_props(serial, baud, SERIAL_NO_PARITY, 8, 1);
|
---|
[5bf6bc70] | 284 | if (rc != EOK) {
|
---|
| 285 | fprintf(stderr, "Failed setting serial properties\n");
|
---|
| 286 | return 2;
|
---|
| 287 | }
|
---|
[a987832] | 288 |
|
---|
[1499564] | 289 | rc = isdv4_init(&state, sess, event_fn);
|
---|
[a987832] | 290 | if (rc != EOK) {
|
---|
| 291 | fprintf(stderr, "Failed initializing isdv4 state");
|
---|
| 292 | return 2;
|
---|
| 293 | }
|
---|
| 294 |
|
---|
| 295 | rc = isdv4_init_tablet(&state);
|
---|
| 296 | if (rc != EOK) {
|
---|
| 297 | fprintf(stderr, "Failed initializing tablet");
|
---|
| 298 | return 2;
|
---|
| 299 | }
|
---|
| 300 |
|
---|
| 301 | printf("Tablet information:\n");
|
---|
| 302 | printf(" Stylus: %ux%u pressure: %u tilt: ", state.stylus_max_x,
|
---|
| 303 | state.stylus_max_y, state.stylus_max_pressure);
|
---|
| 304 | if (state.stylus_tilt_supported) {
|
---|
| 305 | printf("%ux%u\n", state.stylus_max_xtilt, state.stylus_max_ytilt);
|
---|
[1433ecda] | 306 | } else {
|
---|
[a987832] | 307 | printf("not supported\n");
|
---|
| 308 | }
|
---|
| 309 | printf(" Touch: %ux%u type: %s\n", state.touch_max_x, state.touch_max_y,
|
---|
[1433ecda] | 310 | touch_type(state.touch_type));
|
---|
[a35b458] | 311 |
|
---|
[1499564] | 312 | fid_t fibril = fibril_create(read_fibril, NULL);
|
---|
| 313 | /* From this on, state is to be used only by read_fibril */
|
---|
| 314 | fibril_add_ready(fibril);
|
---|
[a987832] | 315 |
|
---|
[b688fd8] | 316 | async_set_fallback_port_handler(mouse_connection, NULL);
|
---|
[1499564] | 317 | rc = loc_server_register(NAME);
|
---|
[a987832] | 318 | if (rc != EOK) {
|
---|
[1499564] | 319 | printf("%s: Unable to register driver.\n", NAME);
|
---|
| 320 | return rc;
|
---|
[a987832] | 321 | }
|
---|
| 322 |
|
---|
[1499564] | 323 | service_id_t service_id;
|
---|
[f66ca57f] | 324 | char *service_name;
|
---|
| 325 | rc = asprintf(&service_name, "mouse/isdv4-%" PRIun, svc_id);
|
---|
| 326 | if (rc < 0) {
|
---|
| 327 | printf(NAME ": Unable to create service name\n");
|
---|
| 328 | return rc;
|
---|
| 329 | }
|
---|
| 330 |
|
---|
| 331 | rc = loc_service_register(service_name, &service_id);
|
---|
[1499564] | 332 | if (rc != EOK) {
|
---|
[f66ca57f] | 333 | printf(NAME ": Unable to register service %s.\n", service_name);
|
---|
[1499564] | 334 | return rc;
|
---|
| 335 | }
|
---|
| 336 |
|
---|
| 337 | category_id_t mouse_category;
|
---|
| 338 | rc = loc_category_get_id("mouse", &mouse_category, IPC_FLAG_BLOCKING);
|
---|
| 339 | if (rc != EOK) {
|
---|
| 340 | printf(NAME ": Unable to get mouse category id.\n");
|
---|
[1433ecda] | 341 | } else {
|
---|
[1499564] | 342 | rc = loc_service_add_to_cat(service_id, mouse_category);
|
---|
| 343 | if (rc != EOK) {
|
---|
| 344 | printf(NAME ": Unable to add device to mouse category.\n");
|
---|
| 345 | }
|
---|
| 346 | }
|
---|
| 347 |
|
---|
| 348 | printf("%s: Accepting connections\n", NAME);
|
---|
| 349 | task_retval(0);
|
---|
| 350 | async_manager();
|
---|
[a987832] | 351 |
|
---|
[1499564] | 352 | /* Not reached */
|
---|
[5bf6bc70] | 353 | return 0;
|
---|
| 354 | }
|
---|