touch.patch (16627B)
1 seat: touchscreens 2 3 wl_touch for the clients that bind it, a finger staying with the surface it 4 went down on, focused and raised. elsewhere, decorations and clients 5 without touch included, the first finger is the pointer: a tap a left 6 click, a drag with the button held, a hold of half a second a right click. 7 8 --- a/libswc/compositor.c 9 +++ b/libswc/compositor.c 10 @@ -1397,6 +1397,12 @@ 11 return NULL; 12 } 13 14 +struct compositor_view * 15 +compositor_content_view_at(int32_t x, int32_t y) 16 +{ 17 + return compositor_chrome_at(x, y) ? NULL : compositor_view_at(x, y); 18 +} 19 + 20 static struct compositor_view * 21 window_view(struct compositor_view *view) 22 { 23 --- a/libswc/compositor.h 24 +++ b/libswc/compositor.h 25 @@ -161,6 +161,11 @@ 26 compositor_view_restack(struct compositor_view *view, 27 struct compositor_view *sibling, bool above); 28 29 +/* the view whose content is under x, y, NULL on a window's decoration or 30 + * border, which are the compositor's and not the client's */ 31 +struct compositor_view * 32 +compositor_content_view_at(int32_t x, int32_t y); 33 + 34 /* topmost view taking input at x, y, or NULL */ 35 struct compositor_view * 36 compositor_view_at(int32_t x, int32_t y); 37 --- a/libswc/meson.build 38 +++ b/libswc/meson.build 39 @@ -40,7 +40,7 @@ 40 'xdg_output.c', 41 'xdg_shell.c', 42 43 - input_backend == 'libinput' ? 'seat.c' : [], 44 + input_backend == 'libinput' ? ['seat.c', 'touch.c'] : [], 45 input_backend == 'wscons' ? 'seat_ws.c' : [], 46 input_backend == 'wscons' and os == 'openbsd' ? 'seat_ws_openbsd.c' : [], 47 48 --- a/libswc/seat.c 49 +++ b/libswc/seat.c 50 @@ -32,6 +32,7 @@ 51 #include "pointer.h" 52 #include "screen.h" 53 #include "surface.h" 54 +#include "touch.h" 55 #include "util.h" 56 57 #include <dirent.h> 58 @@ -78,6 +79,7 @@ 59 60 struct wl_listener keyboard_focus_listener; 61 struct pointer pointer; 62 + struct touch *touch; 63 struct wl_listener data_device_listener; 64 65 struct wl_global *global; 66 @@ -169,7 +171,12 @@ 67 static void 68 get_touch(struct wl_client *client, struct wl_resource *resource, uint32_t id) 69 { 70 - /* XXX: Implement */ 71 + struct seat *seat = wl_resource_get_user_data(resource); 72 + 73 + if (!touch_bind(seat->touch, client, wl_resource_get_version(resource), 74 + id)) { 75 + wl_resource_post_no_memory(resource); 76 + } 77 } 78 79 static const struct wl_seat_interface seat_impl = { 80 @@ -245,10 +252,11 @@ 81 libinput_device_has_capability(device, LIBINPUT_DEVICE_CAP_TABLET_TOOL)) { 82 capabilities |= WL_SEAT_CAPABILITY_POINTER; 83 } 84 - /* TODO: Add touch device support 85 - * if (libinput_device_has_capability(device, LIBINPUT_DEVICE_CAP_TOUCH)) 86 - * capabilities |= WL_SEAT_CAPABILITY_TOUCH; 87 - */ 88 + /* a finger drives the pointer where no client takes it (touch.c), so a 89 + * touchscreen is a pointer too */ 90 + if (libinput_device_has_capability(device, LIBINPUT_DEVICE_CAP_TOUCH)) { 91 + capabilities |= WL_SEAT_CAPABILITY_TOUCH | WL_SEAT_CAPABILITY_POINTER; 92 + } 93 94 return capabilities; 95 } 96 @@ -265,6 +273,7 @@ 97 struct libinput_event_keyboard *k; 98 struct libinput_event_pointer *p; 99 struct libinput_event_tablet_tool *t; 100 + struct libinput_event_touch *touch; 101 } event; 102 wl_fixed_t x, y, value; 103 uint32_t time, key, state; 104 @@ -285,6 +294,36 @@ 105 swc.manager->new_device(device); 106 } 107 break; 108 + case LIBINPUT_EVENT_TOUCH_DOWN: 109 + case LIBINPUT_EVENT_TOUCH_MOTION: 110 + /* the panel's own coordinates are the first screen's */ 111 + screen = wl_container_of(swc.screens.next, screen, link); 112 + rect = &screen->base.geometry; 113 + event.touch = libinput_event_get_touch_event(generic_event); 114 + time = libinput_event_touch_get_time(event.touch); 115 + x = wl_fixed_from_double(rect->x + 116 + libinput_event_touch_get_x_transformed(event.touch, rect->width)); 117 + y = wl_fixed_from_double(rect->y + 118 + libinput_event_touch_get_y_transformed(event.touch, rect->height)); 119 + if (libinput_event_get_type(generic_event) == LIBINPUT_EVENT_TOUCH_DOWN) { 120 + touch_handle_down(seat->touch, time, 121 + libinput_event_touch_get_seat_slot(event.touch), x, y); 122 + } else { 123 + touch_handle_motion(seat->touch, time, 124 + libinput_event_touch_get_seat_slot(event.touch), x, y); 125 + } 126 + break; 127 + case LIBINPUT_EVENT_TOUCH_UP: 128 + event.touch = libinput_event_get_touch_event(generic_event); 129 + touch_handle_up(seat->touch, libinput_event_touch_get_time(event.touch), 130 + libinput_event_touch_get_seat_slot(event.touch)); 131 + break; 132 + case LIBINPUT_EVENT_TOUCH_FRAME: 133 + touch_handle_frame(seat->touch); 134 + break; 135 + case LIBINPUT_EVENT_TOUCH_CANCEL: 136 + touch_handle_cancel(seat->touch); 137 + break; 138 case LIBINPUT_EVENT_KEYBOARD_KEY: 139 event.k = libinput_event_get_keyboard_event(generic_event); 140 time = libinput_event_keyboard_get_time(event.k); 141 @@ -532,17 +571,24 @@ 142 } 143 seat->base.pointer = &seat->pointer; 144 145 + if (!(seat->touch = touch_create(&seat->pointer))) { 146 + ERROR("Could not initialize touch\n"); 147 + goto error6; 148 + } 149 + 150 seat->libinput = NULL; 151 seat->libinput_source = NULL; 152 if (swc.backend->virtual_input) { 153 update_capabilities(seat, WL_SEAT_CAPABILITY_KEYBOARD | 154 WL_SEAT_CAPABILITY_POINTER); 155 } else if (!initialize_libinput(seat)) { 156 - goto error6; 157 + goto error7; 158 } 159 160 return &seat->base; 161 162 +error7: 163 + touch_destroy(seat->touch); 164 error6: 165 pointer_finalize(&seat->pointer); 166 error5: 167 @@ -572,6 +618,7 @@ 168 #endif 169 } 170 171 + touch_destroy(seat->touch); 172 pointer_finalize(&seat->pointer); 173 keyboard_destroy(seat->base.keyboard); 174 data_device_destroy(seat->base.data_device); 175 --- /dev/null 176 +++ b/libswc/touch.c 177 @@ -0,0 +1,389 @@ 178 +/* swc: libswc/touch.c 179 + * 180 + * a finger on a surface whose client has bound wl_touch is that client's, 181 + * from down to up, wherever it moves. a finger anywhere else (a client with 182 + * no touch, a window's decoration, the empty screen) works the pointer 183 + * instead, one finger at a time: a tap is a click of the left button, a 184 + * finger that moves drags with it, and one held still is a click of the 185 + * right button 186 + */ 187 + 188 +#include "touch.h" 189 +#include "compositor.h" 190 +#include "internal.h" 191 +#include "layer_shell.h" 192 +#include "pointer.h" 193 +#include "surface.h" 194 +#include "util.h" 195 +#include "window.h" 196 + 197 +#include <stdlib.h> 198 + 199 +#if defined(__linux__) 200 +#include <linux/input.h> 201 +#elif defined(__FreeBSD__) 202 +#include <dev/evdev/input.h> /* mirrors linux/input.h functionality */ 203 +#endif 204 + 205 +/* fingers followed at once: more are ignored */ 206 +#define POINTS 16 207 +/* how far a finger moves before it drags, and how long it is held still 208 + * before it is a right click, as touch systems commonly have them */ 209 +#define SLOP_PX 8 210 +#define HOLD_MS 500 211 + 212 +struct point { 213 + int32_t slot; 214 + /* NULL when the slot is free or the surface went away */ 215 + struct compositor_view *view; 216 + struct wl_client *client; 217 + struct wl_listener view_destroy; 218 + bool used; 219 +}; 220 + 221 +enum emulation { 222 + NOT_EMULATING, 223 + WAITING, /* down, nothing pressed yet */ 224 + DRAGGING, /* the left button held */ 225 + SPENT, /* the hold has clicked: only the lift is left */ 226 +}; 227 + 228 +struct touch { 229 + struct pointer *pointer; 230 + struct wl_list resources; 231 + struct point points[POINTS]; 232 + 233 + enum emulation emulation; 234 + int32_t slot; 235 + wl_fixed_t x, y; 236 + uint32_t time; 237 + struct wl_event_source *hold; 238 +}; 239 + 240 +static void 241 +click(struct touch *touch, uint32_t time, uint32_t button) 242 +{ 243 + pointer_handle_button(touch->pointer, time, button, 244 + WL_POINTER_BUTTON_STATE_PRESSED); 245 + pointer_handle_button(touch->pointer, time, button, 246 + WL_POINTER_BUTTON_STATE_RELEASED); 247 +} 248 + 249 +static int 250 +handle_hold(void *data) 251 +{ 252 + struct touch *touch = data; 253 + 254 + if (touch->emulation == WAITING) { 255 + click(touch, touch->time + HOLD_MS, BTN_RIGHT); 256 + pointer_handle_frame(touch->pointer); 257 + touch->emulation = SPENT; 258 + } 259 + return 0; 260 +} 261 + 262 +static void 263 +point_release(struct point *point) 264 +{ 265 + if (point->view) { 266 + wl_list_remove(&point->view_destroy.link); 267 + point->view = NULL; 268 + } 269 + point->used = false; 270 +} 271 + 272 +static void 273 +handle_view_destroy(struct wl_listener *listener, void *data) 274 +{ 275 + struct point *point = wl_container_of(listener, point, view_destroy); 276 + 277 + (void)data; 278 + wl_list_remove(&point->view_destroy.link); 279 + point->view = NULL; 280 +} 281 + 282 +static struct point * 283 +find_point(struct touch *touch, int32_t slot) 284 +{ 285 + size_t i; 286 + 287 + for (i = 0; i < POINTS; ++i) { 288 + if (touch->points[i].used && touch->points[i].slot == slot) { 289 + return &touch->points[i]; 290 + } 291 + } 292 + return NULL; 293 +} 294 + 295 +static bool 296 +takes_touch(struct touch *touch, struct wl_client *client) 297 +{ 298 + struct wl_resource *resource; 299 + 300 + wl_resource_for_each(resource, &touch->resources) 301 + { 302 + if (wl_resource_get_client(resource) == client) { 303 + return true; 304 + } 305 + } 306 + return false; 307 +} 308 + 309 +/* the position on the surface of a point of the global space */ 310 +static void 311 +on_surface(struct compositor_view *view, wl_fixed_t x, wl_fixed_t y, 312 + wl_fixed_t *sx, wl_fixed_t *sy) 313 +{ 314 + *sx = x - wl_fixed_from_int(view->base.geometry.x - view->buffer_offset_x); 315 + *sy = y - wl_fixed_from_int(view->base.geometry.y - view->buffer_offset_y); 316 +} 317 + 318 +static void 319 +emulate_down(struct touch *touch, uint32_t time, int32_t slot, wl_fixed_t x, 320 + wl_fixed_t y) 321 +{ 322 + if (touch->emulation != NOT_EMULATING) { 323 + return; 324 + } 325 + touch->emulation = WAITING; 326 + touch->slot = slot; 327 + touch->x = x; 328 + touch->y = y; 329 + touch->time = time; 330 + pointer_handle_absolute_motion(touch->pointer, time, x, y); 331 + pointer_handle_frame(touch->pointer); 332 + wl_event_source_timer_update(touch->hold, HOLD_MS); 333 +} 334 + 335 +void 336 +touch_handle_down(struct touch *touch, uint32_t time, int32_t slot, 337 + wl_fixed_t x, wl_fixed_t y) 338 +{ 339 + struct compositor_view *view; 340 + struct wl_client *client; 341 + struct wl_resource *resource; 342 + struct point *point = NULL; 343 + wl_fixed_t sx, sy; 344 + uint32_t serial; 345 + size_t i; 346 + 347 + view = compositor_content_view_at(wl_fixed_to_int(x), wl_fixed_to_int(y)); 348 + client = view ? wl_resource_get_client(view->surface->resource) : NULL; 349 + if (!client || !takes_touch(touch, client)) { 350 + emulate_down(touch, time, slot, x, y); 351 + return; 352 + } 353 + 354 + for (i = 0; !point && i < POINTS; ++i) { 355 + if (!touch->points[i].used) { 356 + point = &touch->points[i]; 357 + } 358 + } 359 + if (!point) { 360 + return; 361 + } 362 + point->used = true; 363 + point->slot = slot; 364 + point->view = view; 365 + point->client = client; 366 + point->view_destroy.notify = handle_view_destroy; 367 + wl_signal_add(&view->destroy_signal, &point->view_destroy); 368 + 369 + /* a touch focuses and raises as a press does */ 370 + layer_shell_pressed(view); 371 + raise_window(view); 372 + if (view->window) { 373 + window_pointer_entered(view->window); 374 + } 375 + 376 + on_surface(view, x, y, &sx, &sy); 377 + serial = wl_display_next_serial(swc.display); 378 + wl_resource_for_each(resource, &touch->resources) 379 + { 380 + if (wl_resource_get_client(resource) == client) { 381 + wl_touch_send_down(resource, serial, time, view->surface->resource, 382 + slot, sx, sy); 383 + } 384 + } 385 +} 386 + 387 +void 388 +touch_handle_motion(struct touch *touch, uint32_t time, int32_t slot, 389 + wl_fixed_t x, wl_fixed_t y) 390 +{ 391 + struct point *point = find_point(touch, slot); 392 + struct wl_resource *resource; 393 + wl_fixed_t sx, sy, dx, dy; 394 + 395 + if (point) { 396 + if (!point->view) { 397 + return; 398 + } 399 + on_surface(point->view, x, y, &sx, &sy); 400 + wl_resource_for_each(resource, &touch->resources) 401 + { 402 + if (wl_resource_get_client(resource) == point->client) { 403 + wl_touch_send_motion(resource, time, slot, sx, sy); 404 + } 405 + } 406 + return; 407 + } 408 + 409 + if (touch->emulation == NOT_EMULATING || touch->slot != slot) { 410 + return; 411 + } 412 + if (touch->emulation == WAITING) { 413 + dx = x - touch->x; 414 + dy = y - touch->y; 415 + if (abs(wl_fixed_to_int(dx)) < SLOP_PX && 416 + abs(wl_fixed_to_int(dy)) < SLOP_PX) { 417 + return; 418 + } 419 + /* pressed where the finger went down, then dragged */ 420 + wl_event_source_timer_update(touch->hold, 0); 421 + pointer_handle_button(touch->pointer, time, BTN_LEFT, 422 + WL_POINTER_BUTTON_STATE_PRESSED); 423 + touch->emulation = DRAGGING; 424 + } 425 + if (touch->emulation == DRAGGING) { 426 + pointer_handle_absolute_motion(touch->pointer, time, x, y); 427 + } 428 +} 429 + 430 +void 431 +touch_handle_up(struct touch *touch, uint32_t time, int32_t slot) 432 +{ 433 + struct point *point = find_point(touch, slot); 434 + struct wl_resource *resource; 435 + uint32_t serial; 436 + 437 + if (point) { 438 + serial = wl_display_next_serial(swc.display); 439 + wl_resource_for_each(resource, &touch->resources) 440 + { 441 + if (wl_resource_get_client(resource) == point->client) { 442 + wl_touch_send_up(resource, serial, time, slot); 443 + /* an up may come with no frame after it */ 444 + wl_touch_send_frame(resource); 445 + } 446 + } 447 + point_release(point); 448 + return; 449 + } 450 + 451 + if (touch->emulation == NOT_EMULATING || touch->slot != slot) { 452 + return; 453 + } 454 + wl_event_source_timer_update(touch->hold, 0); 455 + if (touch->emulation == WAITING) { 456 + click(touch, time, BTN_LEFT); 457 + } else if (touch->emulation == DRAGGING) { 458 + pointer_handle_button(touch->pointer, time, BTN_LEFT, 459 + WL_POINTER_BUTTON_STATE_RELEASED); 460 + } 461 + touch->emulation = NOT_EMULATING; 462 + pointer_handle_frame(touch->pointer); 463 +} 464 + 465 +void 466 +touch_handle_frame(struct touch *touch) 467 +{ 468 + struct wl_resource *resource; 469 + size_t i; 470 + 471 + wl_resource_for_each(resource, &touch->resources) 472 + { 473 + for (i = 0; i < POINTS; ++i) { 474 + if (touch->points[i].used && 475 + touch->points[i].client == wl_resource_get_client(resource)) { 476 + wl_touch_send_frame(resource); 477 + break; 478 + } 479 + } 480 + } 481 + if (touch->emulation != NOT_EMULATING) { 482 + pointer_handle_frame(touch->pointer); 483 + } 484 +} 485 + 486 +void 487 +touch_handle_cancel(struct touch *touch) 488 +{ 489 + struct wl_resource *resource; 490 + size_t i; 491 + 492 + wl_resource_for_each(resource, &touch->resources) 493 + { 494 + for (i = 0; i < POINTS; ++i) { 495 + if (touch->points[i].used && 496 + touch->points[i].client == wl_resource_get_client(resource)) { 497 + wl_touch_send_cancel(resource); 498 + break; 499 + } 500 + } 501 + } 502 + for (i = 0; i < POINTS; ++i) { 503 + if (touch->points[i].used) { 504 + point_release(&touch->points[i]); 505 + } 506 + } 507 + if (touch->emulation == DRAGGING) { 508 + pointer_handle_button(touch->pointer, touch->time, BTN_LEFT, 509 + WL_POINTER_BUTTON_STATE_RELEASED); 510 + pointer_handle_frame(touch->pointer); 511 + } 512 + wl_event_source_timer_update(touch->hold, 0); 513 + touch->emulation = NOT_EMULATING; 514 +} 515 + 516 +static const struct wl_touch_interface touch_impl = { 517 + .release = destroy_resource, 518 +}; 519 + 520 +bool 521 +touch_bind(struct touch *touch, struct wl_client *client, uint32_t version, 522 + uint32_t id) 523 +{ 524 + struct wl_resource *resource; 525 + 526 + resource = wl_resource_create(client, &wl_touch_interface, version, id); 527 + if (!resource) { 528 + return false; 529 + } 530 + wl_resource_set_implementation(resource, &touch_impl, touch, 531 + &remove_resource); 532 + wl_list_insert(&touch->resources, wl_resource_get_link(resource)); 533 + return true; 534 +} 535 + 536 +struct touch * 537 +touch_create(struct pointer *pointer) 538 +{ 539 + struct touch *touch = calloc(1, sizeof(*touch)); 540 + 541 + if (!touch) { 542 + return NULL; 543 + } 544 + touch->pointer = pointer; 545 + wl_list_init(&touch->resources); 546 + touch->hold = wl_event_loop_add_timer(swc.event_loop, &handle_hold, touch); 547 + if (!touch->hold) { 548 + free(touch); 549 + return NULL; 550 + } 551 + return touch; 552 +} 553 + 554 +void 555 +touch_destroy(struct touch *touch) 556 +{ 557 + size_t i; 558 + 559 + for (i = 0; i < POINTS; ++i) { 560 + if (touch->points[i].used) { 561 + point_release(&touch->points[i]); 562 + } 563 + } 564 + wl_event_source_remove(touch->hold); 565 + free(touch); 566 +} 567 --- /dev/null 568 +++ b/libswc/touch.h 569 @@ -0,0 +1,41 @@ 570 +/* swc: libswc/touch.h 571 + * 572 + * touchscreens: wl_touch for the clients that bind it, and the first finger 573 + * as the pointer for everything else, the window decorations included 574 + */ 575 + 576 +#ifndef SWC_TOUCH_H 577 +#define SWC_TOUCH_H 578 + 579 +#include <stdbool.h> 580 +#include <stdint.h> 581 +#include <wayland-server.h> 582 + 583 +struct pointer; 584 +struct touch; 585 + 586 +struct touch * 587 +touch_create(struct pointer *pointer); 588 +void 589 +touch_destroy(struct touch *touch); 590 + 591 +bool 592 +touch_bind(struct touch *touch, struct wl_client *client, uint32_t version, 593 + uint32_t id); 594 + 595 +/* slot tells fingers apart while they are down, x and y are in the global 596 + * space. a frame ends what happened at one time */ 597 +void 598 +touch_handle_down(struct touch *touch, uint32_t time, int32_t slot, 599 + wl_fixed_t x, wl_fixed_t y); 600 +void 601 +touch_handle_motion(struct touch *touch, uint32_t time, int32_t slot, 602 + wl_fixed_t x, wl_fixed_t y); 603 +void 604 +touch_handle_up(struct touch *touch, uint32_t time, int32_t slot); 605 +void 606 +touch_handle_frame(struct touch *touch); 607 +void 608 +touch_handle_cancel(struct touch *touch); 609 + 610 +#endif