neuswc

patched neuswc library (tracked)
git clone https://git.daat.foo/neuswc.git
Log | Files | Refs | README | LICENSE

pointer.c (26574B)


      1 /* swc: libswc/pointer.c
      2  *
      3  * Copyright (c) 2013-2020 Michael Forney
      4  *
      5  * Permission is hereby granted, free of charge, to any person obtaining a copy
      6  * of this software and associated documentation files (the "Software"), to deal
      7  * in the Software without restriction, including without limitation the rights
      8  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
      9  * copies of the Software, and to permit persons to whom the Software is
     10  * furnished to do so, subject to the following conditions:
     11  *
     12  * The above copyright notice and this permission notice shall be included in
     13  * all copies or substantial portions of the Software.
     14  *
     15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
     16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
     18  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
     19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
     20  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
     21  * SOFTWARE.
     22  */
     23 
     24 #include "pointer.h"
     25 #include "backend.h"
     26 #include "compositor.h"
     27 #include "cursor/cursor_data.h"
     28 #include "event.h"
     29 #include "layer_shell.h"
     30 #include "internal.h"
     31 #ifdef ENABLE_DRM
     32 #include "plane.h"
     33 #endif
     34 #include "screen.h"
     35 #include "seat.h"
     36 #include "shm.h"
     37 #include "surface.h"
     38 #include "util.h"
     39 
     40 #include <assert.h>
     41 #include <stdio.h>
     42 #include <string.h>
     43 #include <wld/wld.h>
     44 
     45 /* the pictures of cursors by name, and the one the window manager shows */
     46 #define SHAPES 64
     47 struct shape {
     48 	char name[32];
     49 	const uint32_t *data;
     50 	uint32_t width, height;
     51 	int32_t hotspot_x, hotspot_y;
     52 };
     53 static struct shape shapes[SHAPES];
     54 static char shape_override[32];
     55 static void (*motion_handler)(void *data, int32_t x, int32_t y);
     56 static void *motion_data;
     57 
     58 static enum swc_cursor_kind cursor_override = SWC_CURSOR_DEFAULT;
     59 static enum swc_cursor_mode cursor_mode = SWC_CURSOR_MODE_CLIENT;
     60 
     61 static struct {
     62 	const uint32_t *data;
     63 	uint32_t width, height;
     64 	int32_t hotspot_x, hotspot_y;
     65 	bool active;
     66 } cursor_images[6];
     67 
     68 EXPORT void
     69 swc_pointer_send_button(uint32_t time, uint32_t button, uint32_t state)
     70 {
     71 	struct pointer *pointer = swc.seat ? swc.seat->pointer : NULL;
     72 	struct wl_resource *resource;
     73 	uint32_t serial;
     74 
     75 	if (!pointer || wl_list_empty(&pointer->focus.active)) {
     76 		return;
     77 	}
     78 
     79 	if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
     80 		layer_shell_pressed(pointer->focus.view);
     81 	}
     82 	serial = wl_display_next_serial(swc.display);
     83 	wl_resource_for_each(resource, &pointer->focus.active)
     84 	    wl_pointer_send_button(resource, serial, time, button, state);
     85 	wl_resource_for_each(resource, &pointer->focus.active)
     86 	{
     87 		if (wl_resource_get_version(resource) >=
     88 		    WL_POINTER_FRAME_SINCE_VERSION) {
     89 			wl_pointer_send_frame(resource);
     90 		}
     91 	}
     92 	pointer->client_axis_source = -1;
     93 }
     94 
     95 EXPORT void
     96 swc_pointer_send_axis(uint32_t time, uint32_t axis, int32_t value120)
     97 {
     98 	struct pointer *pointer = swc.seat ? swc.seat->pointer : NULL;
     99 	struct wl_resource *resource;
    100 	wl_fixed_t value;
    101 
    102 	if (!pointer || wl_list_empty(&pointer->focus.active)) {
    103 		return;
    104 	}
    105 
    106 	value = wl_fixed_from_double((double)value120 / 120.0);
    107 
    108 	wl_resource_for_each(resource, &pointer->focus.active)
    109 	{
    110 		int ver = wl_resource_get_version(resource);
    111 
    112 		if (ver >= WL_POINTER_AXIS_SOURCE_SINCE_VERSION) {
    113 			wl_pointer_send_axis_source(resource, WL_POINTER_AXIS_SOURCE_WHEEL);
    114 		}
    115 		if (value120) {
    116 			if (ver >= WL_POINTER_AXIS_VALUE120_SINCE_VERSION) {
    117 				wl_pointer_send_axis_value120(resource, axis, value120);
    118 			} else if (ver >= WL_POINTER_AXIS_DISCRETE_SINCE_VERSION) {
    119 				wl_pointer_send_axis_discrete(resource, axis, value120 / 120);
    120 			}
    121 		}
    122 
    123 		if (value) {
    124 			wl_pointer_send_axis(resource, time, axis, value);
    125 		} else if (ver >= WL_POINTER_AXIS_STOP_SINCE_VERSION) {
    126 			wl_pointer_send_axis_stop(resource, time, axis);
    127 		}
    128 	}
    129 
    130 	wl_resource_for_each(resource, &pointer->focus.active)
    131 	{
    132 		if (wl_resource_get_version(resource) >=
    133 		    WL_POINTER_FRAME_SINCE_VERSION) {
    134 			wl_pointer_send_frame(resource);
    135 		}
    136 	}
    137 	pointer->client_axis_source = -1;
    138 }
    139 
    140 static void
    141 enter(struct input_focus_handler *handler, struct wl_list *resources,
    142       struct compositor_view *view)
    143 {
    144 	struct pointer *pointer = wl_container_of(handler, pointer, focus_handler);
    145 	struct wl_resource *resource;
    146 	uint32_t serial;
    147 	wl_fixed_t surface_x, surface_y;
    148 	int32_t origin_x, origin_y;
    149 
    150 	/* the default until the client sets its own: a client that sets none
    151 	 * would otherwise show the cursor the last one left */
    152 	pointer_set_cursor(pointer, cursor_left_ptr);
    153 	if (wl_list_empty(resources)) {
    154 		return;
    155 	}
    156 	serial = wl_display_next_serial(swc.display);
    157 	/* do based on buffer origin, holy fuck */
    158 	origin_x = view->base.geometry.x - view->buffer_offset_x;
    159 	origin_y = view->base.geometry.y - view->buffer_offset_y;
    160 	surface_x = pointer->x - wl_fixed_from_int(origin_x);
    161 	surface_y = pointer->y - wl_fixed_from_int(origin_y);
    162 	wl_resource_for_each(resource, resources) wl_pointer_send_enter(
    163 	    resource, serial, view->surface->resource, surface_x, surface_y);
    164 }
    165 
    166 static void
    167 leave(struct input_focus_handler *handler, struct wl_list *resources,
    168       struct compositor_view *view)
    169 {
    170 	struct wl_resource *resource;
    171 	uint32_t serial;
    172 
    173 	serial = wl_display_next_serial(swc.display);
    174 	wl_resource_for_each(resource, resources)
    175 	    wl_pointer_send_leave(resource, serial, view->surface->resource);
    176 }
    177 
    178 static void
    179 handle_cursor_surface_destroy(struct wl_listener *listener, void *data)
    180 {
    181 	struct pointer *pointer =
    182 	    wl_container_of(listener, pointer, cursor.destroy_listener);
    183 
    184 	view_attach(&pointer->cursor.view, NULL);
    185 	pointer->cursor.surface = NULL;
    186 }
    187 
    188 static bool
    189 update(struct view *view)
    190 {
    191 	view_frame(view, get_time());
    192 	return true;
    193 }
    194 
    195 static int
    196 attach(struct view *view, struct wld_buffer *buffer)
    197 {
    198 	struct pointer *pointer = wl_container_of(view, pointer, cursor.view);
    199 	struct surface *surface = pointer->cursor.surface;
    200 #ifdef ENABLE_DRM
    201 	struct screen *screen;
    202 #endif
    203 
    204 	if (surface && !pixman_region32_not_empty(&surface->state.damage)) {
    205 		return 0;
    206 	}
    207 
    208 	wld_set_target_buffer(swc.shm->renderer, pointer->cursor.buffer);
    209 	wld_fill_rectangle(swc.shm->renderer, 0x00000000, 0, 0,
    210 	                   pointer->cursor.buffer->width,
    211 	                   pointer->cursor.buffer->height);
    212 
    213 	if (buffer) {
    214 		wld_copy_rectangle(swc.shm->renderer, buffer, 0, 0, 0, 0, buffer->width,
    215 		                   buffer->height);
    216 	}
    217 
    218 	wld_flush(swc.shm->renderer);
    219 
    220 	if (surface) {
    221 		pixman_region32_clear(&surface->state.damage);
    222 	}
    223 
    224 	/* TODO: Send an early release to the buffer */
    225 
    226 	if (view_set_size_from_buffer(view, buffer)) {
    227 		view_update_screens(view);
    228 	}
    229 
    230 	if (swc.backend->set_cursor) {
    231 		swc.backend->set_cursor(buffer ? pointer->cursor.buffer : NULL,
    232 		                        pointer->cursor.hotspot.x,
    233 		                        pointer->cursor.hotspot.y);
    234 		return 0;
    235 	}
    236 
    237 #ifdef ENABLE_DRM
    238 	if (swc.video == SWC_VIDEO_DRM) {
    239 		wl_list_for_each(screen, &swc.screens, link) {
    240 			view_attach(&screen->planes.cursor->view,
    241 			            buffer ? pointer->cursor.buffer : NULL);
    242 			view_update(&screen->planes.cursor->view);
    243 		}
    244 		return 0;
    245 	}
    246 #endif
    247 	compositor_damage_all();
    248 
    249 	return 0;
    250 }
    251 
    252 static bool
    253 move(struct view *view, int32_t x, int32_t y)
    254 {
    255 #ifdef ENABLE_DRM
    256 	struct screen *screen;
    257 #endif
    258 	struct swc_rectangle old = view->geometry;
    259 
    260 	if (view_set_position(view, x, y)) {
    261 		view_update_screens(view);
    262 	}
    263 
    264 	/* the backend shows the cursor itself and already knows where it is */
    265 	if (swc.backend->set_cursor) {
    266 		return true;
    267 	}
    268 
    269 #ifdef ENABLE_DRM
    270 	if (swc.video == SWC_VIDEO_DRM) {
    271 		wl_list_for_each(screen, &swc.screens, link) {
    272 			view_move(&screen->planes.cursor->view, view->geometry.x,
    273 			          view->geometry.y);
    274 			view_update(&screen->planes.cursor->view);
    275 		}
    276 		return true;
    277 	}
    278 #endif
    279 	/* a backend that draws the cursor into the frame repaints only where it
    280 	 * was and where it is */
    281 	compositor_damage_rect(old.x, old.y, old.width, old.height);
    282 	compositor_damage_rect(view->geometry.x, view->geometry.y,
    283 	                       view->geometry.width, view->geometry.height);
    284 
    285 	return true;
    286 }
    287 
    288 static const struct view_impl view_impl = {
    289     .update = update,
    290     .attach = attach,
    291     .move = move,
    292 };
    293 
    294 static inline void
    295 update_cursor(struct pointer *pointer)
    296 {
    297 	int32_t x = wl_fixed_to_int(pointer->x) - pointer->cursor.hotspot.x,
    298 	        y = wl_fixed_to_int(pointer->y) - pointer->cursor.hotspot.y;
    299 
    300 	view_move(&pointer->cursor.view, x, y);
    301 }
    302 
    303 static void
    304 drop_client_cursor_surface(struct pointer *pointer)
    305 {
    306 	if (!pointer || !pointer->cursor.surface) {
    307 		return;
    308 	}
    309 	surface_set_view(pointer->cursor.surface, NULL);
    310 	wl_list_remove(&pointer->cursor.destroy_listener.link);
    311 	pointer->cursor.surface = NULL;
    312 }
    313 
    314 static void
    315 apply_cursor_override(struct pointer *pointer)
    316 {
    317 	if (!pointer || pointer->cursor.surface) {
    318 		return;
    319 	}
    320 
    321 	pointer_set_cursor(pointer, cursor_left_ptr);
    322 }
    323 
    324 EXPORT void
    325 swc_set_cursor(enum swc_cursor_kind kind)
    326 {
    327 	struct pointer *pointer = swc.seat ? swc.seat->pointer : NULL;
    328 
    329 	cursor_override = kind;
    330 
    331 	drop_client_cursor_surface(pointer);
    332 
    333 	apply_cursor_override(pointer);
    334 }
    335 
    336 EXPORT void
    337 swc_set_cursor_mode(enum swc_cursor_mode mode)
    338 {
    339 	struct pointer *pointer = swc.seat ? swc.seat->pointer : NULL;
    340 
    341 	cursor_mode = mode;
    342 	if (cursor_mode == SWC_CURSOR_MODE_COMPOSITOR) {
    343 		drop_client_cursor_surface(pointer);
    344 	}
    345 	apply_cursor_override(pointer);
    346 }
    347 
    348 EXPORT void
    349 swc_set_cursor_image(enum swc_cursor_kind kind, const uint32_t *argb8888,
    350                      uint32_t width, uint32_t height, int32_t hotspot_x,
    351                      int32_t hotspot_y)
    352 {
    353 	struct pointer *pointer = swc.seat ? swc.seat->pointer : NULL;
    354 
    355 	if (kind < 0 || kind >= (int)ARRAY_LENGTH(cursor_images)) {
    356 		return;
    357 	}
    358 	if (!argb8888 || width == 0 || height == 0) {
    359 		return;
    360 	}
    361 
    362 	cursor_images[kind].data = argb8888;
    363 	cursor_images[kind].width = width;
    364 	cursor_images[kind].height = height;
    365 	cursor_images[kind].hotspot_x = hotspot_x;
    366 	cursor_images[kind].hotspot_y = hotspot_y;
    367 	cursor_images[kind].active = true;
    368 
    369 	if (cursor_mode == SWC_CURSOR_MODE_COMPOSITOR) {
    370 		drop_client_cursor_surface(pointer);
    371 	}
    372 	apply_cursor_override(pointer);
    373 }
    374 
    375 EXPORT void
    376 swc_clear_cursor_image(enum swc_cursor_kind kind)
    377 {
    378 	struct pointer *pointer = swc.seat ? swc.seat->pointer : NULL;
    379 
    380 	if (kind < 0 || kind >= (int)ARRAY_LENGTH(cursor_images)) {
    381 		return;
    382 	}
    383 
    384 	cursor_images[kind].active = false;
    385 	cursor_images[kind].data = NULL;
    386 
    387 	apply_cursor_override(pointer);
    388 }
    389 
    390 static const struct shape *
    391 shape_named(const char *name)
    392 {
    393 	if (!name || !*name) {
    394 		return NULL;
    395 	}
    396 	for (int i = 0; i < SHAPES; i++) {
    397 		if (shapes[i].data && !strcmp(shapes[i].name, name)) {
    398 			return &shapes[i];
    399 		}
    400 	}
    401 	return NULL;
    402 }
    403 
    404 EXPORT void
    405 swc_set_cursor_shape_image(const char *name, const uint32_t *argb8888,
    406                            uint32_t width, uint32_t height, int32_t hotspot_x,
    407                            int32_t hotspot_y)
    408 {
    409 	int free_slot = -1;
    410 
    411 	if (!name || !*name || strlen(name) >= sizeof shapes[0].name || !argb8888 ||
    412 	    !width || !height) {
    413 		return;
    414 	}
    415 	for (int i = 0; i < SHAPES; i++) {
    416 		if (shapes[i].data && !strcmp(shapes[i].name, name)) {
    417 			free_slot = i;
    418 			break;
    419 		}
    420 		if (!shapes[i].data && free_slot < 0) {
    421 			free_slot = i;
    422 		}
    423 	}
    424 	if (free_slot < 0) {
    425 		return;
    426 	}
    427 	strcpy(shapes[free_slot].name, name);
    428 	shapes[free_slot].data = argb8888;
    429 	shapes[free_slot].width = width;
    430 	shapes[free_slot].height = height;
    431 	shapes[free_slot].hotspot_x = hotspot_x;
    432 	shapes[free_slot].hotspot_y = hotspot_y;
    433 }
    434 
    435 EXPORT void
    436 swc_set_cursor_shape(const char *name)
    437 {
    438 	struct pointer *pointer = swc.seat ? swc.seat->pointer : NULL;
    439 
    440 	if (!name) {
    441 		name = "";
    442 	}
    443 	if (!strcmp(shape_override, name) || strlen(name) >= sizeof shape_override) {
    444 		return;
    445 	}
    446 	strcpy(shape_override, name);
    447 	apply_cursor_override(pointer);
    448 }
    449 
    450 EXPORT void
    451 swc_set_pointer_motion_handler(void (*handler)(void *data, int32_t x, int32_t y),
    452                                void *data)
    453 {
    454 	motion_handler = handler;
    455 	motion_data = data;
    456 }
    457 
    458 void
    459 pointer_motion_notify(int32_t x, int32_t y)
    460 {
    461 	if (motion_handler) {
    462 		motion_handler(motion_data, x, y);
    463 	}
    464 }
    465 
    466 /* the cursor a client asked for by name: its picture, drawn by the compositor, where the client's
    467  * own cursor surface would be */
    468 void
    469 pointer_set_shape(struct pointer *pointer, const char *name)
    470 {
    471 	const struct shape *shape = shape_named(name);
    472 	struct cursor shape_cursor;
    473 	union wld_object object;
    474 	struct wld_buffer *buffer;
    475 
    476 	if (!shape && !(shape = shape_named("default"))) {
    477 		return;
    478 	}
    479 	if (pointer->cursor.internal_buffer) {
    480 		wld_buffer_unreference(pointer->cursor.internal_buffer);
    481 		pointer->cursor.internal_buffer = NULL;
    482 	}
    483 	drop_client_cursor_surface(pointer);
    484 	shape_cursor.width = (int)shape->width;
    485 	shape_cursor.height = (int)shape->height;
    486 	object.ptr = (void *)shape->data;
    487 	buffer = wld_import_buffer(swc.shm->context, WLD_OBJECT_DATA, object,
    488 	                           shape_cursor.width, shape_cursor.height,
    489 	                           WLD_FORMAT_ARGB8888, shape_cursor.width * 4);
    490 	if (!buffer) {
    491 		WARNING("Failed to create cursor buffer\n");
    492 		return;
    493 	}
    494 	pointer->cursor.internal_buffer = buffer;
    495 	pointer->cursor.hotspot.x = shape->hotspot_x;
    496 	pointer->cursor.hotspot.y = shape->hotspot_y;
    497 	update_cursor(pointer);
    498 	view_attach(&pointer->cursor.view, buffer);
    499 }
    500 
    501 void
    502 pointer_set_cursor(struct pointer *pointer, uint32_t id)
    503 {
    504 	struct cursor *cursor = &cursor_metadata[id];
    505 	const uint32_t *data = cursor_data;
    506 	union wld_object object = {.ptr = &cursor_data[cursor->offset]};
    507 	struct wld_buffer *buffer;
    508 
    509 	if (id == cursor_left_ptr && shape_named(shape_override)) {
    510 		/* the window manager's, where no client has the pointer */
    511 		static struct cursor shape_cursor;
    512 		const struct shape *shape = shape_named(shape_override);
    513 
    514 		shape_cursor.width = (int)shape->width;
    515 		shape_cursor.height = (int)shape->height;
    516 		shape_cursor.hotspot_x = shape->hotspot_x;
    517 		shape_cursor.hotspot_y = shape->hotspot_y;
    518 		shape_cursor.offset = 0;
    519 		cursor = &shape_cursor;
    520 		data = shape->data;
    521 		object.ptr = (void *)data;
    522 	} else if (id == cursor_left_ptr) {
    523 		enum swc_cursor_kind kind = cursor_override;
    524 		if (kind < 0 || kind >= (int)ARRAY_LENGTH(cursor_images)) {
    525 			kind = SWC_CURSOR_DEFAULT;
    526 		}
    527 
    528 		if (cursor_images[kind].active) {
    529 			static struct cursor custom_cursor;
    530 			custom_cursor.width = (int)cursor_images[kind].width;
    531 			custom_cursor.height = (int)cursor_images[kind].height;
    532 			custom_cursor.hotspot_x = (int)cursor_images[kind].hotspot_x;
    533 			custom_cursor.hotspot_y = (int)cursor_images[kind].hotspot_y;
    534 			custom_cursor.offset = 0;
    535 
    536 			cursor = &custom_cursor;
    537 			data = cursor_images[kind].data;
    538 			object.ptr = (void *)data;
    539 		}
    540 	}
    541 
    542 	if (pointer->cursor.internal_buffer) {
    543 		wld_buffer_unreference(pointer->cursor.internal_buffer);
    544 	}
    545 	if (pointer->cursor.surface) {
    546 		surface_set_view(pointer->cursor.surface, NULL);
    547 		wl_list_remove(&pointer->cursor.destroy_listener.link);
    548 		pointer->cursor.surface = NULL;
    549 	}
    550 
    551 	buffer = wld_import_buffer(swc.shm->context, WLD_OBJECT_DATA, object,
    552 	                           cursor->width, cursor->height,
    553 	                           WLD_FORMAT_ARGB8888, cursor->width * 4);
    554 	if (!buffer) {
    555 		WARNING("Failed to create cursor buffer\n");
    556 	}
    557 	pointer->cursor.internal_buffer = buffer;
    558 	pointer->cursor.hotspot.x = cursor->hotspot_x;
    559 	pointer->cursor.hotspot.y = cursor->hotspot_y;
    560 	update_cursor(pointer);
    561 	view_attach(&pointer->cursor.view, buffer);
    562 }
    563 
    564 static bool
    565 client_handle_motion(struct pointer_handler *handler, uint32_t time,
    566                      wl_fixed_t x, wl_fixed_t y)
    567 {
    568 	struct pointer *pointer = wl_container_of(handler, pointer, client_handler);
    569 	struct wl_resource *resource;
    570 	wl_fixed_t sx, sy;
    571 	int32_t origin_x, origin_y;
    572 
    573 	if (wl_list_empty(&pointer->focus.active)) {
    574 		return false;
    575 	}
    576 
    577 	origin_x = pointer->focus.view->base.geometry.x -
    578 	           pointer->focus.view->buffer_offset_x;
    579 	origin_y = pointer->focus.view->base.geometry.y -
    580 	           pointer->focus.view->buffer_offset_y;
    581 	sx = x - wl_fixed_from_int(origin_x);
    582 	sy = y - wl_fixed_from_int(origin_y);
    583 	wl_resource_for_each(resource, &pointer->focus.active)
    584 	    wl_pointer_send_motion(resource, time, sx, sy);
    585 	return true;
    586 }
    587 
    588 static bool
    589 client_handle_button(struct pointer_handler *handler, uint32_t time,
    590                      struct button *button, uint32_t state)
    591 {
    592 	struct pointer *pointer = wl_container_of(handler, pointer, client_handler);
    593 	struct wl_resource *resource;
    594 
    595 	if (wl_list_empty(&pointer->focus.active)) {
    596 		return false;
    597 	}
    598 	if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
    599 		layer_shell_pressed(pointer->focus.view);
    600 	}
    601 
    602 	wl_resource_for_each(resource, &pointer->focus.active)
    603 	    wl_pointer_send_button(resource, button->press.serial, time,
    604 	                           button->press.value, state);
    605 	return true;
    606 }
    607 
    608 static bool
    609 client_handle_axis(struct pointer_handler *handler, uint32_t time,
    610                    enum wl_pointer_axis axis,
    611                    enum wl_pointer_axis_source source, wl_fixed_t value,
    612                    int value120)
    613 {
    614 	struct pointer *pointer = wl_container_of(handler, pointer, client_handler);
    615 	struct wl_resource *resource;
    616 	int ver;
    617 
    618 	if (wl_list_empty(&pointer->focus.active)) {
    619 		return false;
    620 	}
    621 
    622 	if (pointer->client_axis_source != -1) {
    623 		assert(pointer->client_axis_source == source);
    624 		source = -1;
    625 	} else {
    626 		pointer->client_axis_source = source;
    627 	}
    628 
    629 	wl_resource_for_each(resource, &pointer->focus.active)
    630 	{
    631 		ver = wl_resource_get_version(resource);
    632 		if (source != -1 && ver >= WL_POINTER_AXIS_SOURCE_SINCE_VERSION) {
    633 			wl_pointer_send_axis_source(resource, source);
    634 		}
    635 		if (value120) {
    636 			if (ver >= WL_POINTER_AXIS_VALUE120_SINCE_VERSION) {
    637 				wl_pointer_send_axis_value120(resource, axis, value120);
    638 			} else if (ver >= WL_POINTER_AXIS_DISCRETE_SINCE_VERSION) {
    639 				wl_pointer_send_axis_discrete(resource, axis, value120 / 120);
    640 			}
    641 		}
    642 		if (value) {
    643 			wl_pointer_send_axis(resource, time, axis, value);
    644 		} else if (ver >= WL_POINTER_AXIS_STOP_SINCE_VERSION) {
    645 			wl_pointer_send_axis_stop(resource, time, axis);
    646 		}
    647 	}
    648 	return true;
    649 }
    650 
    651 static void
    652 client_handle_frame(struct pointer_handler *handler)
    653 {
    654 	struct pointer *pointer = wl_container_of(handler, pointer, client_handler);
    655 	struct wl_resource *resource;
    656 
    657 	wl_resource_for_each(resource, &pointer->focus.active)
    658 	{
    659 		if (wl_resource_get_version(resource) >=
    660 		    WL_POINTER_FRAME_SINCE_VERSION) {
    661 			wl_pointer_send_frame(resource);
    662 		}
    663 	}
    664 	pointer->client_axis_source = -1;
    665 }
    666 
    667 bool
    668 pointer_initialize(struct pointer *pointer)
    669 {
    670 	struct screen *screen = wl_container_of(swc.screens.next, screen, link);
    671 	struct swc_rectangle *geom = &screen->base.geometry;
    672 
    673 	/* Center cursor in the geometry of the first screen. */
    674 	pointer->x = wl_fixed_from_int(geom->x + geom->width / 2);
    675 	pointer->y = wl_fixed_from_int(geom->y + geom->height / 2);
    676 	pointer->focus_handler.enter = enter;
    677 	pointer->focus_handler.leave = leave;
    678 	pointer->client_handler.motion = client_handle_motion;
    679 	pointer->client_handler.button = client_handle_button;
    680 	pointer->client_handler.axis = client_handle_axis;
    681 	pointer->client_handler.frame = client_handle_frame;
    682 	pointer->client_handler.pending = false;
    683 	pointer->client_axis_source = -1;
    684 	wl_list_init(&pointer->handlers);
    685 	wl_list_insert(&pointer->handlers, &pointer->client_handler.link);
    686 	wl_array_init(&pointer->buttons);
    687 
    688 	view_initialize(&pointer->cursor.view, &view_impl);
    689 	pointer->cursor.surface = NULL;
    690 	pointer->cursor.destroy_listener.notify = &handle_cursor_surface_destroy;
    691 	pointer->cursor.buffer = wld_create_buffer(
    692 	    swc.backend->context, swc.backend->cursor_width,
    693 	    swc.backend->cursor_height,
    694 	    WLD_FORMAT_ARGB8888, WLD_FLAG_MAP | WLD_FLAG_CURSOR);
    695 	pointer->cursor.internal_buffer = NULL;
    696 
    697 	if (!pointer->cursor.buffer) {
    698 		return false;
    699 	}
    700 
    701 	pointer_set_cursor(pointer, cursor_left_ptr);
    702 
    703 #ifdef ENABLE_DRM
    704 	wl_list_for_each(screen, &swc.screens, link)
    705 		if (screen->planes.cursor)
    706 			view_attach(&screen->planes.cursor->view, pointer->cursor.buffer);
    707 #endif
    708 
    709 	input_focus_initialize(&pointer->focus, &pointer->focus_handler);
    710 	pixman_region32_init(&pointer->region);
    711 
    712 	return true;
    713 }
    714 
    715 void
    716 pointer_finalize(struct pointer *pointer)
    717 {
    718 	if (pointer->cursor.surface) {
    719 		surface_set_view(pointer->cursor.surface, NULL);
    720 		wl_list_remove(&pointer->cursor.destroy_listener.link);
    721 	}
    722 	if (swc.backend->set_cursor)
    723 		swc.backend->set_cursor(NULL, 0, 0);
    724 	view_finalize(&pointer->cursor.view);
    725 	if (pointer->cursor.internal_buffer)
    726 		wld_buffer_unreference(pointer->cursor.internal_buffer);
    727 	wld_buffer_unreference(pointer->cursor.buffer);
    728 	wl_array_release(&pointer->buttons);
    729 	input_focus_finalize(&pointer->focus);
    730 	pixman_region32_fini(&pointer->region);
    731 }
    732 
    733 void
    734 pointer_set_focus(struct pointer *pointer, struct compositor_view *view)
    735 {
    736 	input_focus_set(&pointer->focus, view);
    737 }
    738 
    739 static void
    740 clip_position(struct pointer *pointer, wl_fixed_t fx, wl_fixed_t fy)
    741 {
    742 	int32_t x, y, last_x, last_y;
    743 	pixman_box32_t box;
    744 
    745 	x = wl_fixed_to_int(fx);
    746 	y = wl_fixed_to_int(fy);
    747 	last_x = wl_fixed_to_int(pointer->x);
    748 	last_y = wl_fixed_to_int(pointer->y);
    749 
    750 	if (!pixman_region32_contains_point(&pointer->region, x, y, NULL)) {
    751 		if (!pixman_region32_contains_point(&pointer->region, last_x, last_y,
    752 		                                    &box)) {
    753 			WARNING("cursor is not in the visible screen area\n");
    754 			pointer->x = 0;
    755 			pointer->y = 0;
    756 			return;
    757 		}
    758 
    759 		/* Do some clipping. */
    760 		fx = wl_fixed_from_int(MAX(MIN(x, box.x2 - 1), box.x1));
    761 		fy = wl_fixed_from_int(MAX(MIN(y, box.y2 - 1), box.y1));
    762 	}
    763 
    764 	pointer->x = fx;
    765 	pointer->y = fy;
    766 }
    767 
    768 void
    769 pointer_set_region(struct pointer *pointer, pixman_region32_t *region)
    770 {
    771 	pixman_region32_copy(&pointer->region, region);
    772 	clip_position(pointer, pointer->x, pointer->y);
    773 }
    774 
    775 static void
    776 set_cursor(struct wl_client *client, struct wl_resource *resource,
    777            uint32_t serial, struct wl_resource *surface_resource,
    778            int32_t hotspot_x, int32_t hotspot_y)
    779 {
    780 	struct pointer *pointer = wl_resource_get_user_data(resource);
    781 	struct surface *surface;
    782 
    783 	(void)serial;
    784 
    785 	if (client != pointer->focus.client) {
    786 		return;
    787 	}
    788 
    789 	/* If forcing compositor cursor, ignore client cursor surfaces. */
    790 	if (cursor_mode == SWC_CURSOR_MODE_COMPOSITOR ||
    791 	    cursor_override != SWC_CURSOR_DEFAULT) {
    792 		return;
    793 	}
    794 
    795 	if (pointer->cursor.surface) {
    796 		surface_set_view(pointer->cursor.surface, NULL);
    797 		wl_list_remove(&pointer->cursor.destroy_listener.link);
    798 	}
    799 
    800 	surface =
    801 	    surface_resource ? wl_resource_get_user_data(surface_resource) : NULL;
    802 	pointer->cursor.surface = surface;
    803 	pointer->cursor.hotspot.x = hotspot_x;
    804 	pointer->cursor.hotspot.y = hotspot_y;
    805 
    806 	if (surface) {
    807 		surface_set_view(surface, &pointer->cursor.view);
    808 		wl_resource_add_destroy_listener(surface->resource,
    809 		                                 &pointer->cursor.destroy_listener);
    810 		update_cursor(pointer);
    811 	}
    812 }
    813 
    814 static const struct wl_pointer_interface pointer_impl = {
    815     .set_cursor = set_cursor,
    816     .release = destroy_resource,
    817 };
    818 
    819 static void
    820 unbind(struct wl_resource *resource)
    821 {
    822 	struct pointer *pointer = wl_resource_get_user_data(resource);
    823 	input_focus_remove_resource(&pointer->focus, resource);
    824 }
    825 
    826 struct wl_resource *
    827 pointer_bind(struct pointer *pointer, struct wl_client *client,
    828              uint32_t version, uint32_t id)
    829 {
    830 	struct wl_resource *client_resource;
    831 
    832 	client_resource =
    833 	    wl_resource_create(client, &wl_pointer_interface, version, id);
    834 	if (!client_resource) {
    835 		return NULL;
    836 	}
    837 	wl_resource_set_implementation(client_resource, &pointer_impl, pointer,
    838 	                               &unbind);
    839 	input_focus_add_resource(&pointer->focus, client_resource);
    840 
    841 	return client_resource;
    842 }
    843 
    844 struct button *
    845 pointer_get_button(struct pointer *pointer, uint32_t serial)
    846 {
    847 	struct button *button;
    848 
    849 	wl_array_for_each(button, &pointer->buttons)
    850 	{
    851 		if (button->press.serial == serial) {
    852 			return button;
    853 		}
    854 	}
    855 
    856 	return NULL;
    857 }
    858 
    859 void
    860 pointer_handle_button(struct pointer *pointer, uint32_t time, uint32_t value,
    861                       uint32_t state)
    862 {
    863 	struct pointer_handler *handler;
    864 	struct button *button;
    865 	uint32_t serial;
    866 
    867 	serial = wl_display_next_serial(swc.display);
    868 
    869 	if (state == WL_POINTER_BUTTON_STATE_RELEASED) {
    870 		wl_array_for_each(button, &pointer->buttons)
    871 		{
    872 			if (button->press.value == value) {
    873 				if (button->handler) {
    874 					button->press.serial = serial;
    875 					button->handler->button(button->handler, time, button,
    876 					                        state);
    877 					button->handler->pending = true;
    878 				}
    879 
    880 				array_remove(&pointer->buttons, button, sizeof(*button));
    881 				break;
    882 			}
    883 		}
    884 	} else {
    885 		button = wl_array_add(&pointer->buttons, sizeof(*button));
    886 
    887 		if (!button) {
    888 			return;
    889 		}
    890 
    891 		button->press.value = value;
    892 		button->press.serial = serial;
    893 		button->handler = NULL;
    894 
    895 		wl_list_for_each(handler, &pointer->handlers, link)
    896 		{
    897 			if (handler->button &&
    898 			    handler->button(handler, time, button, state)) {
    899 				button->handler = handler;
    900 				handler->pending = true;
    901 				break;
    902 			}
    903 		}
    904 	}
    905 }
    906 
    907 void
    908 pointer_handle_axis(struct pointer *pointer, uint32_t time,
    909                     enum wl_pointer_axis axis,
    910                     enum wl_pointer_axis_source source, wl_fixed_t value,
    911                     int value120)
    912 {
    913 	struct pointer_handler *handler;
    914 
    915 	wl_list_for_each(handler, &pointer->handlers, link)
    916 	{
    917 		if (handler->axis &&
    918 		    handler->axis(handler, time, axis, source, value, value120)) {
    919 			handler->pending = true;
    920 			break;
    921 		}
    922 	}
    923 }
    924 
    925 void
    926 pointer_handle_relative_motion(struct pointer *pointer, uint32_t time,
    927                                wl_fixed_t dx, wl_fixed_t dy)
    928 {
    929 	pointer_handle_absolute_motion(pointer, time, pointer->x + dx,
    930 	                               pointer->y + dy);
    931 }
    932 
    933 void
    934 pointer_handle_absolute_motion(struct pointer *pointer, uint32_t time,
    935                                wl_fixed_t x, wl_fixed_t y)
    936 {
    937 	struct pointer_handler *handler;
    938 
    939 	clip_position(pointer, x, y);
    940 
    941 	wl_list_for_each(handler, &pointer->handlers, link)
    942 	{
    943 		if (handler->motion &&
    944 		    handler->motion(handler, time, pointer->x, pointer->y)) {
    945 			handler->pending = true;
    946 			break;
    947 		}
    948 	}
    949 }
    950 
    951 void
    952 pointer_handle_frame(struct pointer *pointer)
    953 {
    954 	struct pointer_handler *handler;
    955 
    956 	wl_list_for_each(handler, &pointer->handlers, link)
    957 	{
    958 		if (handler->pending && handler->frame) {
    959 			handler->frame(handler);
    960 			handler->pending = false;
    961 		}
    962 	}
    963 
    964 	update_cursor(pointer);
    965 }