touch.c (9497B)
1 /* swc: libswc/touch.c 2 * 3 * a finger on a surface whose client has bound wl_touch is that client's, 4 * from down to up, wherever it moves. a finger anywhere else (a client with 5 * no touch, a window's decoration, the empty screen) works the pointer 6 * instead, one finger at a time: a tap is a click of the left button, a 7 * finger that moves drags with it, and one held still is a click of the 8 * right button 9 */ 10 11 #include "touch.h" 12 #include "compositor.h" 13 #include "internal.h" 14 #include "layer_shell.h" 15 #include "pointer.h" 16 #include "surface.h" 17 #include "util.h" 18 #include "window.h" 19 20 #include <stdlib.h> 21 22 #if defined(__linux__) 23 #include <linux/input.h> 24 #elif defined(__FreeBSD__) 25 #include <dev/evdev/input.h> /* mirrors linux/input.h functionality */ 26 #endif 27 28 /* fingers followed at once: more are ignored */ 29 #define POINTS 16 30 /* how far a finger moves before it drags, and how long it is held still 31 * before it is a right click, as touch systems commonly have them */ 32 #define SLOP_PX 8 33 #define HOLD_MS 500 34 35 struct point { 36 int32_t slot; 37 /* NULL when the slot is free or the surface went away */ 38 struct compositor_view *view; 39 struct wl_client *client; 40 struct wl_listener view_destroy; 41 bool used; 42 }; 43 44 enum emulation { 45 NOT_EMULATING, 46 WAITING, /* down, nothing pressed yet */ 47 DRAGGING, /* the left button held */ 48 SPENT, /* the hold has clicked: only the lift is left */ 49 }; 50 51 struct touch { 52 struct pointer *pointer; 53 struct wl_list resources; 54 struct point points[POINTS]; 55 56 enum emulation emulation; 57 int32_t slot; 58 wl_fixed_t x, y; 59 uint32_t time; 60 struct wl_event_source *hold; 61 }; 62 63 static void 64 click(struct touch *touch, uint32_t time, uint32_t button) 65 { 66 pointer_handle_button(touch->pointer, time, button, 67 WL_POINTER_BUTTON_STATE_PRESSED); 68 pointer_handle_button(touch->pointer, time, button, 69 WL_POINTER_BUTTON_STATE_RELEASED); 70 } 71 72 static int 73 handle_hold(void *data) 74 { 75 struct touch *touch = data; 76 77 if (touch->emulation == WAITING) { 78 click(touch, touch->time + HOLD_MS, BTN_RIGHT); 79 pointer_handle_frame(touch->pointer); 80 touch->emulation = SPENT; 81 } 82 return 0; 83 } 84 85 static void 86 point_release(struct point *point) 87 { 88 if (point->view) { 89 wl_list_remove(&point->view_destroy.link); 90 point->view = NULL; 91 } 92 point->used = false; 93 } 94 95 static void 96 handle_view_destroy(struct wl_listener *listener, void *data) 97 { 98 struct point *point = wl_container_of(listener, point, view_destroy); 99 100 (void)data; 101 wl_list_remove(&point->view_destroy.link); 102 point->view = NULL; 103 } 104 105 static struct point * 106 find_point(struct touch *touch, int32_t slot) 107 { 108 size_t i; 109 110 for (i = 0; i < POINTS; ++i) { 111 if (touch->points[i].used && touch->points[i].slot == slot) { 112 return &touch->points[i]; 113 } 114 } 115 return NULL; 116 } 117 118 static bool 119 takes_touch(struct touch *touch, struct wl_client *client) 120 { 121 struct wl_resource *resource; 122 123 wl_resource_for_each(resource, &touch->resources) 124 { 125 if (wl_resource_get_client(resource) == client) { 126 return true; 127 } 128 } 129 return false; 130 } 131 132 /* the position on the surface of a point of the global space */ 133 static void 134 on_surface(struct compositor_view *view, wl_fixed_t x, wl_fixed_t y, 135 wl_fixed_t *sx, wl_fixed_t *sy) 136 { 137 *sx = x - wl_fixed_from_int(view->base.geometry.x - view->buffer_offset_x); 138 *sy = y - wl_fixed_from_int(view->base.geometry.y - view->buffer_offset_y); 139 } 140 141 static void 142 emulate_down(struct touch *touch, uint32_t time, int32_t slot, wl_fixed_t x, 143 wl_fixed_t y) 144 { 145 if (touch->emulation != NOT_EMULATING) { 146 return; 147 } 148 touch->emulation = WAITING; 149 touch->slot = slot; 150 touch->x = x; 151 touch->y = y; 152 touch->time = time; 153 pointer_handle_absolute_motion(touch->pointer, time, x, y); 154 pointer_handle_frame(touch->pointer); 155 wl_event_source_timer_update(touch->hold, HOLD_MS); 156 } 157 158 void 159 touch_handle_down(struct touch *touch, uint32_t time, int32_t slot, 160 wl_fixed_t x, wl_fixed_t y) 161 { 162 struct compositor_view *view; 163 struct wl_client *client; 164 struct wl_resource *resource; 165 struct point *point = NULL; 166 wl_fixed_t sx, sy; 167 uint32_t serial; 168 size_t i; 169 170 view = compositor_content_view_at(wl_fixed_to_int(x), wl_fixed_to_int(y)); 171 client = view ? wl_resource_get_client(view->surface->resource) : NULL; 172 if (!client || !takes_touch(touch, client)) { 173 emulate_down(touch, time, slot, x, y); 174 return; 175 } 176 177 for (i = 0; !point && i < POINTS; ++i) { 178 if (!touch->points[i].used) { 179 point = &touch->points[i]; 180 } 181 } 182 if (!point) { 183 return; 184 } 185 point->used = true; 186 point->slot = slot; 187 point->view = view; 188 point->client = client; 189 point->view_destroy.notify = handle_view_destroy; 190 wl_signal_add(&view->destroy_signal, &point->view_destroy); 191 192 /* a touch focuses and raises as a press does */ 193 layer_shell_pressed(view); 194 raise_window(view); 195 if (view->window) { 196 window_pointer_entered(view->window); 197 } 198 199 on_surface(view, x, y, &sx, &sy); 200 serial = wl_display_next_serial(swc.display); 201 wl_resource_for_each(resource, &touch->resources) 202 { 203 if (wl_resource_get_client(resource) == client) { 204 wl_touch_send_down(resource, serial, time, view->surface->resource, 205 slot, sx, sy); 206 } 207 } 208 } 209 210 void 211 touch_handle_motion(struct touch *touch, uint32_t time, int32_t slot, 212 wl_fixed_t x, wl_fixed_t y) 213 { 214 struct point *point = find_point(touch, slot); 215 struct wl_resource *resource; 216 wl_fixed_t sx, sy, dx, dy; 217 218 if (point) { 219 if (!point->view) { 220 return; 221 } 222 on_surface(point->view, x, y, &sx, &sy); 223 wl_resource_for_each(resource, &touch->resources) 224 { 225 if (wl_resource_get_client(resource) == point->client) { 226 wl_touch_send_motion(resource, time, slot, sx, sy); 227 } 228 } 229 return; 230 } 231 232 if (touch->emulation == NOT_EMULATING || touch->slot != slot) { 233 return; 234 } 235 if (touch->emulation == WAITING) { 236 dx = x - touch->x; 237 dy = y - touch->y; 238 if (abs(wl_fixed_to_int(dx)) < SLOP_PX && 239 abs(wl_fixed_to_int(dy)) < SLOP_PX) { 240 return; 241 } 242 /* pressed where the finger went down, then dragged */ 243 wl_event_source_timer_update(touch->hold, 0); 244 pointer_handle_button(touch->pointer, time, BTN_LEFT, 245 WL_POINTER_BUTTON_STATE_PRESSED); 246 touch->emulation = DRAGGING; 247 } 248 if (touch->emulation == DRAGGING) { 249 pointer_handle_absolute_motion(touch->pointer, time, x, y); 250 } 251 } 252 253 void 254 touch_handle_up(struct touch *touch, uint32_t time, int32_t slot) 255 { 256 struct point *point = find_point(touch, slot); 257 struct wl_resource *resource; 258 uint32_t serial; 259 260 if (point) { 261 serial = wl_display_next_serial(swc.display); 262 wl_resource_for_each(resource, &touch->resources) 263 { 264 if (wl_resource_get_client(resource) == point->client) { 265 wl_touch_send_up(resource, serial, time, slot); 266 /* an up may come with no frame after it */ 267 wl_touch_send_frame(resource); 268 } 269 } 270 point_release(point); 271 return; 272 } 273 274 if (touch->emulation == NOT_EMULATING || touch->slot != slot) { 275 return; 276 } 277 wl_event_source_timer_update(touch->hold, 0); 278 if (touch->emulation == WAITING) { 279 click(touch, time, BTN_LEFT); 280 } else if (touch->emulation == DRAGGING) { 281 pointer_handle_button(touch->pointer, time, BTN_LEFT, 282 WL_POINTER_BUTTON_STATE_RELEASED); 283 } 284 touch->emulation = NOT_EMULATING; 285 pointer_handle_frame(touch->pointer); 286 } 287 288 void 289 touch_handle_frame(struct touch *touch) 290 { 291 struct wl_resource *resource; 292 size_t i; 293 294 wl_resource_for_each(resource, &touch->resources) 295 { 296 for (i = 0; i < POINTS; ++i) { 297 if (touch->points[i].used && 298 touch->points[i].client == wl_resource_get_client(resource)) { 299 wl_touch_send_frame(resource); 300 break; 301 } 302 } 303 } 304 if (touch->emulation != NOT_EMULATING) { 305 pointer_handle_frame(touch->pointer); 306 } 307 } 308 309 void 310 touch_handle_cancel(struct touch *touch) 311 { 312 struct wl_resource *resource; 313 size_t i; 314 315 wl_resource_for_each(resource, &touch->resources) 316 { 317 for (i = 0; i < POINTS; ++i) { 318 if (touch->points[i].used && 319 touch->points[i].client == wl_resource_get_client(resource)) { 320 wl_touch_send_cancel(resource); 321 break; 322 } 323 } 324 } 325 for (i = 0; i < POINTS; ++i) { 326 if (touch->points[i].used) { 327 point_release(&touch->points[i]); 328 } 329 } 330 if (touch->emulation == DRAGGING) { 331 pointer_handle_button(touch->pointer, touch->time, BTN_LEFT, 332 WL_POINTER_BUTTON_STATE_RELEASED); 333 pointer_handle_frame(touch->pointer); 334 } 335 wl_event_source_timer_update(touch->hold, 0); 336 touch->emulation = NOT_EMULATING; 337 } 338 339 static const struct wl_touch_interface touch_impl = { 340 .release = destroy_resource, 341 }; 342 343 bool 344 touch_bind(struct touch *touch, struct wl_client *client, uint32_t version, 345 uint32_t id) 346 { 347 struct wl_resource *resource; 348 349 resource = wl_resource_create(client, &wl_touch_interface, version, id); 350 if (!resource) { 351 return false; 352 } 353 wl_resource_set_implementation(resource, &touch_impl, touch, 354 &remove_resource); 355 wl_list_insert(&touch->resources, wl_resource_get_link(resource)); 356 return true; 357 } 358 359 struct touch * 360 touch_create(struct pointer *pointer) 361 { 362 struct touch *touch = calloc(1, sizeof(*touch)); 363 364 if (!touch) { 365 return NULL; 366 } 367 touch->pointer = pointer; 368 wl_list_init(&touch->resources); 369 touch->hold = wl_event_loop_add_timer(swc.event_loop, &handle_hold, touch); 370 if (!touch->hold) { 371 free(touch); 372 return NULL; 373 } 374 return touch; 375 } 376 377 void 378 touch_destroy(struct touch *touch) 379 { 380 size_t i; 381 382 for (i = 0; i < POINTS; ++i) { 383 if (touch->points[i].used) { 384 point_release(&touch->points[i]); 385 } 386 } 387 wl_event_source_remove(touch->hold); 388 free(touch); 389 }