[5bf6bc70] | 1 | /*
|
---|
[4c6fd56] | 2 | * Copyright (c) 2023 Jiri Svoboda
|
---|
[5bf6bc70] | 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 |
|
---|
[c4c6025] | 30 | #include <async.h>
|
---|
[5bf6bc70] | 31 | #include <errno.h>
|
---|
[1499564] | 32 | #include <fibril_synch.h>
|
---|
[c4c6025] | 33 | #include <io/serial.h>
|
---|
[1499564] | 34 | #include <ipc/mouseev.h>
|
---|
[c4c6025] | 35 | #include <loc.h>
|
---|
| 36 | #include <stddef.h>
|
---|
| 37 | #include <stdio.h>
|
---|
[1d6dd2a] | 38 | #include <str.h>
|
---|
[1c635d6] | 39 | #include <task.h>
|
---|
[5bf6bc70] | 40 |
|
---|
[a987832] | 41 | #include "isdv4.h"
|
---|
[5bf6bc70] | 42 |
|
---|
[f66ca57f] | 43 | #define NAME "isdv4_tablet"
|
---|
[1499564] | 44 |
|
---|
| 45 | static async_sess_t *client_sess = NULL;
|
---|
| 46 | static fibril_mutex_t client_mutex;
|
---|
| 47 | static isdv4_state_t state;
|
---|
| 48 |
|
---|
[5bf6bc70] | 49 | static void syntax_print(void)
|
---|
| 50 | {
|
---|
[f66ca57f] | 51 | fprintf(stderr, "Usage: %s [--baud=<baud>] [--print-events] [device_service]\n", NAME);
|
---|
[1499564] | 52 | }
|
---|
| 53 |
|
---|
[b7fd2a0] | 54 | static errno_t read_fibril(void *unused)
|
---|
[1499564] | 55 | {
|
---|
[b7fd2a0] | 56 | errno_t rc = isdv4_read_events(&state);
|
---|
[1499564] | 57 | if (rc != EOK) {
|
---|
| 58 | fprintf(stderr, "Failed reading events");
|
---|
| 59 | return rc;
|
---|
| 60 | }
|
---|
| 61 |
|
---|
| 62 | isdv4_fini(&state);
|
---|
| 63 | return EOK;
|
---|
| 64 | }
|
---|
| 65 |
|
---|
[984a9ba] | 66 | static void mouse_connection(ipc_call_t *icall, void *arg)
|
---|
[1499564] | 67 | {
|
---|
[beb83c1] | 68 | async_accept_0(icall);
|
---|
[a35b458] | 69 |
|
---|
[1499564] | 70 | async_sess_t *sess =
|
---|
| 71 | async_callback_receive(EXCHANGE_SERIALIZE);
|
---|
[a35b458] | 72 |
|
---|
[1499564] | 73 | fibril_mutex_lock(&client_mutex);
|
---|
[a35b458] | 74 |
|
---|
[f9b2cb4c] | 75 | if (client_sess == NULL)
|
---|
| 76 | client_sess = sess;
|
---|
[a35b458] | 77 |
|
---|
[1499564] | 78 | fibril_mutex_unlock(&client_mutex);
|
---|
| 79 |
|
---|
| 80 | while (true) {
|
---|
| 81 | ipc_call_t call;
|
---|
[984a9ba] | 82 | async_get_call(&call);
|
---|
[a35b458] | 83 |
|
---|
[fafb8e5] | 84 | if (!ipc_get_imethod(&call)) {
|
---|
[889cdb1] | 85 | async_answer_0(&call, EOK);
|
---|
[1499564] | 86 | break;
|
---|
[889cdb1] | 87 | }
|
---|
[a35b458] | 88 |
|
---|
[984a9ba] | 89 | async_answer_0(&call, ENOTSUP);
|
---|
[1499564] | 90 | }
|
---|
[5bf6bc70] | 91 | }
|
---|
| 92 |
|
---|
[1499564] | 93 | static 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);
|
---|
[a35b458] | 98 |
|
---|
[1433ecda] | 99 | if (!sess)
|
---|
| 100 | return;
|
---|
[a35b458] | 101 |
|
---|
[1499564] | 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,
|
---|
[1433ecda] | 111 | max_x, max_y);
|
---|
[1499564] | 112 | if (event->type == PRESS || event->type == RELEASE) {
|
---|
| 113 | async_msg_2(exch, MOUSEEV_BUTTON_EVENT, event->button,
|
---|
[1433ecda] | 114 | event->type == PRESS);
|
---|
[1499564] | 115 | }
|
---|
| 116 | }
|
---|
| 117 | async_exchange_end(exch);
|
---|
| 118 | }
|
---|
| 119 |
|
---|
| 120 | static void print_and_emit_event(const isdv4_event_t *event)
|
---|
[6e09bee] | 121 | {
|
---|
| 122 | const char *type = NULL;
|
---|
| 123 | switch (event->type) {
|
---|
[1433ecda] | 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;
|
---|
[6e09bee] | 142 | }
|
---|
[a987832] | 143 |
|
---|
[6e09bee] | 144 | const char *source = NULL;
|
---|
| 145 | switch (event->source) {
|
---|
[1433ecda] | 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;
|
---|
[6e09bee] | 155 | }
|
---|
[a987832] | 156 |
|
---|
[1499564] | 157 | printf("%s %s %u %u %u %u\n", type, source, event->x, event->y,
|
---|
| 158 | event->pressure, event->button);
|
---|
[a35b458] | 159 |
|
---|
[1499564] | 160 | emit_event(event);
|
---|
[6e09bee] | 161 | }
|
---|
| 162 |
|
---|
[5bf6bc70] | 163 | static const char *touch_type(unsigned int data_id)
|
---|
| 164 | {
|
---|
| 165 | switch (data_id) {
|
---|
[1433ecda] | 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";
|
---|
[5bf6bc70] | 177 | }
|
---|
| 178 | return "unknown";
|
---|
| 179 | }
|
---|
| 180 |
|
---|
| 181 | int main(int argc, char **argv)
|
---|
| 182 | {
|
---|
| 183 | sysarg_t baud = 38400;
|
---|
| 184 | service_id_t svc_id;
|
---|
[c4c6025] | 185 | serial_t *serial;
|
---|
[f66ca57f] | 186 | char *serial_port_name = NULL;
|
---|
[4c6fd56] | 187 | loc_srv_t *srv;
|
---|
[a987832] | 188 |
|
---|
[5bf6bc70] | 189 | int arg = 1;
|
---|
[b7fd2a0] | 190 | errno_t rc;
|
---|
[a987832] | 191 |
|
---|
[1499564] | 192 | isdv4_event_fn event_fn = emit_event;
|
---|
| 193 |
|
---|
[5bf6bc70] | 194 | if (argc > arg && str_test_prefix(argv[arg], "--baud=")) {
|
---|
| 195 | size_t arg_offset = str_lsize(argv[arg], 7);
|
---|
[1433ecda] | 196 | char *arg_str = argv[arg] + arg_offset;
|
---|
[5bf6bc70] | 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 | }
|
---|
[a987832] | 211 |
|
---|
[1499564] | 212 | if (argc > arg && str_cmp(argv[arg], "--print-events") == 0) {
|
---|
| 213 | event_fn = print_and_emit_event;
|
---|
| 214 | arg++;
|
---|
| 215 | }
|
---|
| 216 |
|
---|
[5bf6bc70] | 217 | if (argc > arg) {
|
---|
[f66ca57f] | 218 | serial_port_name = argv[arg];
|
---|
| 219 | rc = loc_service_get_id(serial_port_name, &svc_id, 0);
|
---|
[5bf6bc70] | 220 | if (rc != EOK) {
|
---|
| 221 | fprintf(stderr, "Cannot find device service %s\n",
|
---|
| 222 | argv[arg]);
|
---|
| 223 | return 1;
|
---|
| 224 | }
|
---|
| 225 | arg++;
|
---|
[1433ecda] | 226 | } else {
|
---|
[5bf6bc70] | 227 | category_id_t serial_cat_id;
|
---|
[a987832] | 228 |
|
---|
[5bf6bc70] | 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 | }
|
---|
[a987832] | 235 |
|
---|
[5bf6bc70] | 236 | service_id_t *svc_ids;
|
---|
| 237 | size_t svc_count;
|
---|
[a987832] | 238 |
|
---|
[1433ecda] | 239 | rc = loc_category_get_svcs(serial_cat_id, &svc_ids, &svc_count);
|
---|
| 240 | if (rc != EOK) {
|
---|
[5bf6bc70] | 241 | fprintf(stderr, "Failed getting list of services\n");
|
---|
| 242 | return 1;
|
---|
| 243 | }
|
---|
[a987832] | 244 |
|
---|
[5bf6bc70] | 245 | if (svc_count == 0) {
|
---|
| 246 | fprintf(stderr, "No service in category 'serial'\n");
|
---|
| 247 | free(svc_ids);
|
---|
| 248 | return 1;
|
---|
| 249 | }
|
---|
[a987832] | 250 |
|
---|
[5bf6bc70] | 251 | svc_id = svc_ids[0];
|
---|
[f66ca57f] | 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 |
|
---|
[5bf6bc70] | 259 | free(svc_ids);
|
---|
| 260 | }
|
---|
[a987832] | 261 |
|
---|
[5bf6bc70] | 262 | if (argc > arg) {
|
---|
| 263 | fprintf(stderr, "Too many arguments\n");
|
---|
| 264 | syntax_print();
|
---|
| 265 | return 1;
|
---|
| 266 | }
|
---|
[a987832] | 267 |
|
---|
[1499564] | 268 | fibril_mutex_initialize(&client_mutex);
|
---|
| 269 |
|
---|
[f66ca57f] | 270 | printf(NAME ": Using serial port %s\n", serial_port_name);
|
---|
| 271 |
|
---|
[f9b2cb4c] | 272 | async_sess_t *sess = loc_service_connect(svc_id, INTERFACE_DDF,
|
---|
[5bf6bc70] | 273 | IPC_FLAG_BLOCKING);
|
---|
| 274 | if (!sess) {
|
---|
| 275 | fprintf(stderr, "Failed connecting to service\n");
|
---|
[c4c6025] | 276 | return 2;
|
---|
[5bf6bc70] | 277 | }
|
---|
[a987832] | 278 |
|
---|
[c4c6025] | 279 | rc = serial_open(sess, &serial);
|
---|
| 280 | if (rc != EOK) {
|
---|
| 281 | fprintf(stderr, "Failed opening serial port\n");
|
---|
| 282 | return 2;
|
---|
| 283 | }
|
---|
[a987832] | 284 |
|
---|
[c4c6025] | 285 | rc = serial_set_comm_props(serial, baud, SERIAL_NO_PARITY, 8, 1);
|
---|
[5bf6bc70] | 286 | if (rc != EOK) {
|
---|
| 287 | fprintf(stderr, "Failed setting serial properties\n");
|
---|
| 288 | return 2;
|
---|
| 289 | }
|
---|
[a987832] | 290 |
|
---|
[1499564] | 291 | rc = isdv4_init(&state, sess, event_fn);
|
---|
[a987832] | 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);
|
---|
[1433ecda] | 308 | } else {
|
---|
[a987832] | 309 | printf("not supported\n");
|
---|
| 310 | }
|
---|
| 311 | printf(" Touch: %ux%u type: %s\n", state.touch_max_x, state.touch_max_y,
|
---|
[1433ecda] | 312 | touch_type(state.touch_type));
|
---|
[a35b458] | 313 |
|
---|
[1499564] | 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);
|
---|
[a987832] | 317 |
|
---|
[b688fd8] | 318 | async_set_fallback_port_handler(mouse_connection, NULL);
|
---|
[4c6fd56] | 319 | rc = loc_server_register(NAME, &srv);
|
---|
[a987832] | 320 | if (rc != EOK) {
|
---|
[1499564] | 321 | printf("%s: Unable to register driver.\n", NAME);
|
---|
| 322 | return rc;
|
---|
[a987832] | 323 | }
|
---|
| 324 |
|
---|
[1499564] | 325 | service_id_t service_id;
|
---|
[f66ca57f] | 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 |
|
---|
[4c6fd56] | 333 | rc = loc_service_register(srv, service_name, &service_id);
|
---|
[1499564] | 334 | if (rc != EOK) {
|
---|
[4c6fd56] | 335 | loc_server_unregister(srv);
|
---|
[f66ca57f] | 336 | printf(NAME ": Unable to register service %s.\n", service_name);
|
---|
[1499564] | 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");
|
---|
[1433ecda] | 344 | } else {
|
---|
[4c6fd56] | 345 | rc = loc_service_add_to_cat(srv, service_id, mouse_category);
|
---|
[1499564] | 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();
|
---|
[a987832] | 354 |
|
---|
[1499564] | 355 | /* Not reached */
|
---|
[5bf6bc70] | 356 | return 0;
|
---|
| 357 | }
|
---|