From 78d3582b70dcd823c3675806bcbda6776282eb6f Mon Sep 17 00:00:00 2001 From: emersion Date: Thu, 5 Oct 2017 13:11:51 +0200 Subject: [PATCH 01/16] Add request_set_cursor event --- include/wlr/types/wlr_seat.h | 9 +++++++++ types/wlr_seat.c | 27 +++++++++++++++++++++------ 2 files changed, 30 insertions(+), 6 deletions(-) diff --git a/include/wlr/types/wlr_seat.h b/include/wlr/types/wlr_seat.h index d3d3e00d..d267924c 100644 --- a/include/wlr/types/wlr_seat.h +++ b/include/wlr/types/wlr_seat.h @@ -125,11 +125,20 @@ struct wlr_seat { struct wl_signal keyboard_grab_begin; struct wl_signal keyboard_grab_end; + + struct wl_signal request_set_cursor; } events; void *data; }; +struct wlr_seat_pointer_request_set_cursor_event { + struct wl_client *client; + struct wlr_seat_handle *seat_handle; + struct wlr_surface *surface; + int32_t hotspot_x, hotspot_y; +}; + /** * Allocates a new wlr_seat and adds a wl_seat global to the display. */ diff --git a/types/wlr_seat.c b/types/wlr_seat.c index 4566053d..25c39235 100644 --- a/types/wlr_seat.c +++ b/types/wlr_seat.c @@ -20,12 +20,26 @@ static void pointer_send_frame(struct wl_resource *resource) { } static void wl_pointer_set_cursor(struct wl_client *client, - struct wl_resource *resource, - uint32_t serial, - struct wl_resource *surface, - int32_t hotspot_x, - int32_t hotspot_y) { - wlr_log(L_DEBUG, "TODO: wl_pointer_set_cursor"); + struct wl_resource *resource, uint32_t serial, + struct wl_resource *surface_resource, + int32_t hotspot_x, int32_t hotspot_y) { + struct wlr_seat_handle *handle = wl_resource_get_user_data(resource); + struct wlr_surface *surface = wl_resource_get_user_data(surface_resource); + + struct wlr_seat_pointer_request_set_cursor_event *event = + calloc(1, sizeof(struct wlr_seat_pointer_request_set_cursor_event)); + if (event == NULL) { + return; + } + event->client = client; + event->seat_handle = handle; + event->surface = surface; + event->hotspot_x = hotspot_x; + event->hotspot_y = hotspot_y; + + wl_signal_emit(&handle->wlr_seat->events.request_set_cursor, event); + + free(event); } static const struct wl_pointer_interface wl_pointer_impl = { @@ -285,6 +299,7 @@ struct wlr_seat *wlr_seat_create(struct wl_display *display, const char *name) { wl_signal_init(&wlr_seat->events.client_bound); wl_signal_init(&wlr_seat->events.client_unbound); + wl_signal_init(&wlr_seat->events.request_set_cursor); wl_signal_init(&wlr_seat->events.pointer_grab_begin); wl_signal_init(&wlr_seat->events.pointer_grab_end); From 65d57920e534acb231bb04964b257b2186c7ce73 Mon Sep 17 00:00:00 2001 From: emersion Date: Sat, 7 Oct 2017 10:55:17 +0200 Subject: [PATCH 02/16] rootston: handle request_set_cursor --- include/rootston/input.h | 2 ++ rootston/cursor.c | 23 +++++++++++++++++++++++ types/wlr_seat.c | 5 ++++- 3 files changed, 29 insertions(+), 1 deletion(-) diff --git a/include/rootston/input.h b/include/rootston/input.h index 9caf66c0..bdd2a104 100644 --- a/include/rootston/input.h +++ b/include/rootston/input.h @@ -107,6 +107,8 @@ struct roots_input { struct wl_listener cursor_tool_tip; struct wl_listener pointer_grab_end; + + struct wl_listener request_set_cursor; }; struct roots_input *input_create(struct roots_server *server, diff --git a/rootston/cursor.c b/rootston/cursor.c index 605920cc..3423d33b 100644 --- a/rootston/cursor.c +++ b/rootston/cursor.c @@ -274,6 +274,24 @@ static void handle_pointer_grab_end(struct wl_listener *listener, void *data) { cursor_update_position(input, 0); } +static void handle_request_set_cursor(struct wl_listener *listener, + void *data) { + struct roots_input *input = wl_container_of(listener, input, + request_set_cursor); + //struct wlr_seat_pointer_request_set_cursor_event *event = data; + + struct wlr_xcursor_image *image = input->xcursor->images[0]; + struct roots_output *output; + wl_list_for_each(output, &input->server->desktop->outputs, link) { + if (!wlr_output_set_cursor(output->wlr_output, image->buffer, + image->width, image->width, image->height, + image->hotspot_x, image->hotspot_y)) { + wlr_log(L_DEBUG, "Failed to set hardware cursor"); + return; + } + } +} + void cursor_initialize(struct roots_input *input) { struct wlr_cursor *cursor = input->cursor; @@ -304,6 +322,11 @@ void cursor_initialize(struct roots_input *input) { wl_signal_add(&input->wl_seat->events.pointer_grab_end, &input->pointer_grab_end); input->pointer_grab_end.notify = handle_pointer_grab_end; + + wl_list_init(&input->request_set_cursor.link); + wl_signal_add(&input->wl_seat->events.request_set_cursor, + &input->request_set_cursor); + input->request_set_cursor.notify = handle_request_set_cursor; } static void reset_device_mappings(struct roots_config *config, diff --git a/types/wlr_seat.c b/types/wlr_seat.c index 25c39235..a543936d 100644 --- a/types/wlr_seat.c +++ b/types/wlr_seat.c @@ -24,7 +24,10 @@ static void wl_pointer_set_cursor(struct wl_client *client, struct wl_resource *surface_resource, int32_t hotspot_x, int32_t hotspot_y) { struct wlr_seat_handle *handle = wl_resource_get_user_data(resource); - struct wlr_surface *surface = wl_resource_get_user_data(surface_resource); + struct wlr_surface *surface = NULL; + if (surface_resource != NULL) { + surface = wl_resource_get_user_data(surface_resource); + } struct wlr_seat_pointer_request_set_cursor_event *event = calloc(1, sizeof(struct wlr_seat_pointer_request_set_cursor_event)); From 9b83caa658de2b51e36266beac048c96a556037c Mon Sep 17 00:00:00 2001 From: emersion Date: Sun, 8 Oct 2017 21:21:06 +0200 Subject: [PATCH 03/16] Add wlr_output_set_cursor_surface --- backend/wayland/output.c | 8 +++++++- include/wlr/types/wlr_output.h | 14 +++++++++----- rootston/cursor.c | 14 +++++++++----- types/wlr_output.c | 26 +++++++++++++++++++++++++- 4 files changed, 50 insertions(+), 12 deletions(-) diff --git a/backend/wayland/output.c b/backend/wayland/output.c index 062a91a1..03b43b35 100644 --- a/backend/wayland/output.c +++ b/backend/wayland/output.c @@ -55,9 +55,15 @@ static void wlr_wl_output_transform(struct wlr_output *_output, static bool wlr_wl_output_set_cursor(struct wlr_output *_output, const uint8_t *buf, int32_t stride, uint32_t width, uint32_t height, int32_t hotspot_x, int32_t hotspot_y) { - struct wlr_wl_backend_output *output = (struct wlr_wl_backend_output *)_output; struct wlr_wl_backend *backend = output->backend; + + if (!buf) { + wl_pointer_set_cursor(output->backend->pointer, output->enter_serial, + NULL, 0, 0); + return true; + } + stride *= 4; // stride is given in pixels, we need it in bytes if (!backend->shm || !backend->pointer) { diff --git a/include/wlr/types/wlr_output.h b/include/wlr/types/wlr_output.h index 52d377e3..c25e2fb8 100644 --- a/include/wlr/types/wlr_output.h +++ b/include/wlr/types/wlr_output.h @@ -54,18 +54,22 @@ struct wlr_output { void *data; }; +struct wlr_surface; + void wlr_output_enable(struct wlr_output *output, bool enable); bool wlr_output_set_mode(struct wlr_output *output, - struct wlr_output_mode *mode); + struct wlr_output_mode *mode); void wlr_output_transform(struct wlr_output *output, - enum wl_output_transform transform); + enum wl_output_transform transform); bool wlr_output_set_cursor(struct wlr_output *output, - const uint8_t *buf, int32_t stride, uint32_t width, uint32_t height, - int32_t hotspot_x, int32_t hotspot_y); + const uint8_t *buf, int32_t stride, uint32_t width, uint32_t height, + int32_t hotspot_x, int32_t hotspot_y); +bool wlr_output_set_cursor_surface(struct wlr_output *output, + struct wlr_surface *surface, int32_t hotspot_x, int32_t hotspot_y); bool wlr_output_move_cursor(struct wlr_output *output, int x, int y); void wlr_output_destroy(struct wlr_output *output); void wlr_output_effective_resolution(struct wlr_output *output, - int *width, int *height); + int *width, int *height); void wlr_output_make_current(struct wlr_output *output); void wlr_output_swap_buffers(struct wlr_output *output); void wlr_output_set_gamma(struct wlr_output *output, diff --git a/rootston/cursor.c b/rootston/cursor.c index 3423d33b..f9372f7f 100644 --- a/rootston/cursor.c +++ b/rootston/cursor.c @@ -278,14 +278,18 @@ static void handle_request_set_cursor(struct wl_listener *listener, void *data) { struct roots_input *input = wl_container_of(listener, input, request_set_cursor); - //struct wlr_seat_pointer_request_set_cursor_event *event = data; + struct wlr_seat_pointer_request_set_cursor_event *event = data; + if (event->surface == NULL) { + wlr_log(L_DEBUG, "handle_request_set_cursor with NULL surface"); + return; + } + + wlr_log(L_DEBUG, "handle_request_set_cursor"); - struct wlr_xcursor_image *image = input->xcursor->images[0]; struct roots_output *output; wl_list_for_each(output, &input->server->desktop->outputs, link) { - if (!wlr_output_set_cursor(output->wlr_output, image->buffer, - image->width, image->width, image->height, - image->hotspot_x, image->hotspot_y)) { + if (!wlr_output_set_cursor_surface(output->wlr_output, + event->surface, event->hotspot_x, event->hotspot_y)) { wlr_log(L_DEBUG, "Failed to set hardware cursor"); return; } diff --git a/types/wlr_output.c b/types/wlr_output.c index 64f67f2d..67bd826f 100644 --- a/types/wlr_output.c +++ b/types/wlr_output.c @@ -4,6 +4,7 @@ #include #include #include +#include #include #include #include @@ -158,7 +159,8 @@ bool wlr_output_set_cursor(struct wlr_output *output, } if (!output->cursor.texture) { - output->cursor.texture = wlr_render_texture_create(output->cursor.renderer); + output->cursor.texture = + wlr_render_texture_create(output->cursor.renderer); if (!output->cursor.texture) { return false; } @@ -168,6 +170,28 @@ bool wlr_output_set_cursor(struct wlr_output *output, WL_SHM_FORMAT_ARGB8888, stride, width, height, buf); } +bool wlr_output_set_cursor_surface(struct wlr_output *output, + struct wlr_surface *surface, int32_t hotspot_x, int32_t hotspot_y) { + if (output->impl->set_cursor) { + output->impl->set_cursor(output, NULL, 0, 0, 0, 0, 0); + wlr_log(L_INFO, "TODO: wlr_output_set_cursor_surface for hw cursors"); + } + + output->cursor.is_sw = true; + output->cursor.width = surface->current->width; + output->cursor.height = surface->current->height; + output->cursor.hotspot_x = hotspot_x; + output->cursor.hotspot_y = hotspot_y; + + wlr_texture_destroy(output->cursor.texture); + output->cursor.texture = surface->texture; + + wlr_renderer_destroy(output->cursor.renderer); + output->cursor.renderer = surface->renderer; + + return true; +} + bool wlr_output_move_cursor(struct wlr_output *output, int x, int y) { output->cursor.x = x; output->cursor.y = y; From 17354b630a17f32a15b4b3f3710a8dc644e099d7 Mon Sep 17 00:00:00 2001 From: emersion Date: Sun, 8 Oct 2017 22:30:31 +0200 Subject: [PATCH 04/16] Read surface buffer to set output cursor --- types/wlr_output.c | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/types/wlr_output.c b/types/wlr_output.c index 67bd826f..ef80e143 100644 --- a/types/wlr_output.c +++ b/types/wlr_output.c @@ -172,24 +172,24 @@ bool wlr_output_set_cursor(struct wlr_output *output, bool wlr_output_set_cursor_surface(struct wlr_output *output, struct wlr_surface *surface, int32_t hotspot_x, int32_t hotspot_y) { - if (output->impl->set_cursor) { - output->impl->set_cursor(output, NULL, 0, 0, 0, 0, 0); - wlr_log(L_INFO, "TODO: wlr_output_set_cursor_surface for hw cursors"); + struct wl_shm_buffer *buffer = wl_shm_buffer_get(surface->current->buffer); + if (buffer == NULL) { + return false; } - output->cursor.is_sw = true; - output->cursor.width = surface->current->width; - output->cursor.height = surface->current->height; - output->cursor.hotspot_x = hotspot_x; - output->cursor.hotspot_y = hotspot_y; - - wlr_texture_destroy(output->cursor.texture); - output->cursor.texture = surface->texture; - - wlr_renderer_destroy(output->cursor.renderer); - output->cursor.renderer = surface->renderer; + uint32_t format = wl_shm_buffer_get_format(buffer); + if (format != WL_SHM_FORMAT_ARGB8888) { + return false; + } - return true; + void *data = wl_shm_buffer_get_data(buffer); + int32_t width = wl_shm_buffer_get_width(buffer); + int32_t height = wl_shm_buffer_get_height(buffer); + wl_shm_buffer_begin_access(buffer); + bool ok = wlr_output_set_cursor(output, data, width, width, height, + hotspot_x, hotspot_y); + wl_shm_buffer_end_access(buffer); + return ok; } bool wlr_output_move_cursor(struct wlr_output *output, int x, int y) { From 8a77d1b6a2b28b90c8bb5ef079077aec1a44407c Mon Sep 17 00:00:00 2001 From: emersion Date: Sun, 8 Oct 2017 23:11:59 +0200 Subject: [PATCH 05/16] Listen to cursor surface commit & destroy events --- include/wlr/types/wlr_output.h | 7 ++- rootston/cursor.c | 7 +-- types/wlr_output.c | 85 +++++++++++++++++++++++++--------- 3 files changed, 72 insertions(+), 27 deletions(-) diff --git a/include/wlr/types/wlr_output.h b/include/wlr/types/wlr_output.h index c25e2fb8..dc637e26 100644 --- a/include/wlr/types/wlr_output.h +++ b/include/wlr/types/wlr_output.h @@ -49,6 +49,11 @@ struct wlr_output { int32_t hotspot_x, hotspot_y; struct wlr_renderer *renderer; struct wlr_texture *texture; + + // only when using a cursor surface + struct wlr_surface *surface; + struct wl_listener surface_commit; + struct wl_listener surface_destroy; } cursor; void *data; @@ -64,7 +69,7 @@ void wlr_output_transform(struct wlr_output *output, bool wlr_output_set_cursor(struct wlr_output *output, const uint8_t *buf, int32_t stride, uint32_t width, uint32_t height, int32_t hotspot_x, int32_t hotspot_y); -bool wlr_output_set_cursor_surface(struct wlr_output *output, +void wlr_output_set_cursor_surface(struct wlr_output *output, struct wlr_surface *surface, int32_t hotspot_x, int32_t hotspot_y); bool wlr_output_move_cursor(struct wlr_output *output, int x, int y); void wlr_output_destroy(struct wlr_output *output); diff --git a/rootston/cursor.c b/rootston/cursor.c index f9372f7f..97f9ca83 100644 --- a/rootston/cursor.c +++ b/rootston/cursor.c @@ -288,11 +288,8 @@ static void handle_request_set_cursor(struct wl_listener *listener, struct roots_output *output; wl_list_for_each(output, &input->server->desktop->outputs, link) { - if (!wlr_output_set_cursor_surface(output->wlr_output, - event->surface, event->hotspot_x, event->hotspot_y)) { - wlr_log(L_DEBUG, "Failed to set hardware cursor"); - return; - } + wlr_output_set_cursor_surface(output->wlr_output, event->surface, + event->hotspot_x, event->hotspot_y); } } diff --git a/types/wlr_output.c b/types/wlr_output.c index ef80e143..06593514 100644 --- a/types/wlr_output.c +++ b/types/wlr_output.c @@ -99,18 +99,6 @@ void wlr_output_update_matrix(struct wlr_output *output) { wlr_matrix_texture(output->transform_matrix, output->width, output->height, output->transform); } -void wlr_output_init(struct wlr_output *output, - const struct wlr_output_impl *impl) { - output->impl = impl; - output->modes = list_create(); - output->transform = WL_OUTPUT_TRANSFORM_NORMAL; - output->scale = 1; - wl_signal_init(&output->events.frame); - wl_signal_init(&output->events.swap_buffers); - wl_signal_init(&output->events.resolution); - wl_signal_init(&output->events.destroy); -} - void wlr_output_enable(struct wlr_output *output, bool enable) { output->impl->enable(output, enable); } @@ -167,29 +155,67 @@ bool wlr_output_set_cursor(struct wlr_output *output, } return wlr_texture_upload_pixels(output->cursor.texture, - WL_SHM_FORMAT_ARGB8888, stride, width, height, buf); + WL_SHM_FORMAT_ARGB8888, stride, width, height, buf); } -bool wlr_output_set_cursor_surface(struct wlr_output *output, - struct wlr_surface *surface, int32_t hotspot_x, int32_t hotspot_y) { +static void handle_cursor_surface_commit(struct wl_listener *listener, + void *data) { + struct wlr_output *output = wl_container_of(listener, output, + cursor.surface_commit); + struct wlr_surface *surface = data; + struct wl_shm_buffer *buffer = wl_shm_buffer_get(surface->current->buffer); if (buffer == NULL) { - return false; + return; } uint32_t format = wl_shm_buffer_get_format(buffer); if (format != WL_SHM_FORMAT_ARGB8888) { - return false; + return; } - void *data = wl_shm_buffer_get_data(buffer); + void *buffer_data = wl_shm_buffer_get_data(buffer); int32_t width = wl_shm_buffer_get_width(buffer); int32_t height = wl_shm_buffer_get_height(buffer); wl_shm_buffer_begin_access(buffer); - bool ok = wlr_output_set_cursor(output, data, width, width, height, - hotspot_x, hotspot_y); + wlr_output_set_cursor(output, buffer_data, width, width, height, + output->cursor.hotspot_x, output->cursor.hotspot_y); wl_shm_buffer_end_access(buffer); - return ok; +} + +static void handle_cursor_surface_destroy(struct wl_listener *listener, + void *data) { + struct wlr_output *output = wl_container_of(listener, output, + cursor.surface_destroy); + struct wlr_surface *surface = data; + + if (output->cursor.surface == surface) { + wl_list_remove(&output->cursor.surface_commit.link); + wl_list_remove(&output->cursor.surface_destroy.link); + output->cursor.surface = NULL; + } +} + +void wlr_output_set_cursor_surface(struct wlr_output *output, + struct wlr_surface *surface, int32_t hotspot_x, int32_t hotspot_y) { + output->cursor.hotspot_x = hotspot_x; + output->cursor.hotspot_y = hotspot_y; + + if (output->cursor.surface == surface) { + return; + } + output->cursor.surface = surface; + + if (output->cursor.surface) { + wl_list_remove(&output->cursor.surface_commit.link); + wl_list_remove(&output->cursor.surface_destroy.link); + output->cursor.surface = NULL; + } + + if (surface != NULL) { + wl_signal_add(&surface->events.commit, &output->cursor.surface_commit); + wl_signal_add(&surface->events.destroy, &output->cursor.surface_destroy); + } } bool wlr_output_move_cursor(struct wlr_output *output, int x, int y) { @@ -207,6 +233,23 @@ bool wlr_output_move_cursor(struct wlr_output *output, int x, int y) { return output->impl->move_cursor(output, x, y); } +void wlr_output_init(struct wlr_output *output, + const struct wlr_output_impl *impl) { + output->impl = impl; + output->modes = list_create(); + output->transform = WL_OUTPUT_TRANSFORM_NORMAL; + output->scale = 1; + wl_signal_init(&output->events.frame); + wl_signal_init(&output->events.swap_buffers); + wl_signal_init(&output->events.resolution); + wl_signal_init(&output->events.destroy); + + wl_list_init(&output->cursor.surface_commit.link); + output->cursor.surface_commit.notify = handle_cursor_surface_commit; + wl_list_init(&output->cursor.surface_destroy.link); + output->cursor.surface_destroy.notify = handle_cursor_surface_destroy; +} + void wlr_output_destroy(struct wlr_output *output) { if (!output) { return; From cebb202f7d61e86b957512f3f5fb65507a48152d Mon Sep 17 00:00:00 2001 From: emersion Date: Sun, 8 Oct 2017 23:24:57 +0200 Subject: [PATCH 06/16] Set and check cursor surface role --- types/wlr_output.c | 4 ++++ types/wlr_seat.c | 5 +++++ 2 files changed, 9 insertions(+) diff --git a/types/wlr_output.c b/types/wlr_output.c index 06593514..09117441 100644 --- a/types/wlr_output.c +++ b/types/wlr_output.c @@ -198,6 +198,10 @@ static void handle_cursor_surface_destroy(struct wl_listener *listener, void wlr_output_set_cursor_surface(struct wlr_output *output, struct wlr_surface *surface, int32_t hotspot_x, int32_t hotspot_y) { + if (strcmp(surface->role, "cursor") != 0) { + return; + } + output->cursor.hotspot_x = hotspot_x; output->cursor.hotspot_y = hotspot_y; diff --git a/types/wlr_seat.c b/types/wlr_seat.c index a543936d..d58b2129 100644 --- a/types/wlr_seat.c +++ b/types/wlr_seat.c @@ -27,6 +27,11 @@ static void wl_pointer_set_cursor(struct wl_client *client, struct wlr_surface *surface = NULL; if (surface_resource != NULL) { surface = wl_resource_get_user_data(surface_resource); + + if (wlr_surface_set_role(surface, "cursor", resource, + WL_POINTER_ERROR_ROLE) < 0) { + return; + } } struct wlr_seat_pointer_request_set_cursor_event *event = From bceaee6eb78f9c1f5693703adc94b863940c3aa4 Mon Sep 17 00:00:00 2001 From: emersion Date: Sun, 8 Oct 2017 23:29:04 +0200 Subject: [PATCH 07/16] Stop listening to cursor surface when manually setting cursor data --- types/wlr_output.c | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/types/wlr_output.c b/types/wlr_output.c index 09117441..2b78f728 100644 --- a/types/wlr_output.c +++ b/types/wlr_output.c @@ -120,9 +120,9 @@ void wlr_output_transform(struct wlr_output *output, wlr_output_update_matrix(output); } -bool wlr_output_set_cursor(struct wlr_output *output, - const uint8_t *buf, int32_t stride, uint32_t width, uint32_t height, - int32_t hotspot_x, int32_t hotspot_y) { +static bool set_cursor(struct wlr_output *output, const uint8_t *buf, + int32_t stride, uint32_t width, uint32_t height, int32_t hotspot_x, + int32_t hotspot_y) { if (output->impl->set_cursor && output->impl->set_cursor(output, buf, stride, width, height, hotspot_x, hotspot_y)) { @@ -158,6 +158,18 @@ bool wlr_output_set_cursor(struct wlr_output *output, WL_SHM_FORMAT_ARGB8888, stride, width, height, buf); } +bool wlr_output_set_cursor(struct wlr_output *output, + const uint8_t *buf, int32_t stride, uint32_t width, uint32_t height, + int32_t hotspot_x, int32_t hotspot_y) { + if (output->cursor.surface) { + wl_list_remove(&output->cursor.surface_commit.link); + wl_list_remove(&output->cursor.surface_destroy.link); + output->cursor.surface = NULL; + } + + return set_cursor(output, buf, stride, width, height, hotspot_x, hotspot_y); +} + static void handle_cursor_surface_commit(struct wl_listener *listener, void *data) { struct wlr_output *output = wl_container_of(listener, output, From c7a4346bfc5db9e0af070271ff12dad25f6bddd3 Mon Sep 17 00:00:00 2001 From: emersion Date: Sun, 8 Oct 2017 23:39:38 +0200 Subject: [PATCH 08/16] Hide cursor when surface == NULL --- types/wlr_output.c | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/types/wlr_output.c b/types/wlr_output.c index 2b78f728..fbecbb2e 100644 --- a/types/wlr_output.c +++ b/types/wlr_output.c @@ -135,8 +135,6 @@ static bool set_cursor(struct wlr_output *output, const uint8_t *buf, output->cursor.is_sw = true; output->cursor.width = width; output->cursor.height = height; - output->cursor.hotspot_x = hotspot_x; - output->cursor.hotspot_y = hotspot_y; if (!output->cursor.renderer) { /* NULL egl is okay given that we are only using pixel buffers */ @@ -167,6 +165,9 @@ bool wlr_output_set_cursor(struct wlr_output *output, output->cursor.surface = NULL; } + output->cursor.hotspot_x = hotspot_x; + output->cursor.hotspot_y = hotspot_y; + return set_cursor(output, buf, stride, width, height, hotspot_x, hotspot_y); } @@ -199,25 +200,22 @@ static void handle_cursor_surface_destroy(struct wl_listener *listener, void *data) { struct wlr_output *output = wl_container_of(listener, output, cursor.surface_destroy); - struct wlr_surface *surface = data; - if (output->cursor.surface == surface) { - wl_list_remove(&output->cursor.surface_commit.link); - wl_list_remove(&output->cursor.surface_destroy.link); - output->cursor.surface = NULL; - } + wl_list_remove(&output->cursor.surface_commit.link); + wl_list_remove(&output->cursor.surface_destroy.link); + output->cursor.surface = NULL; } void wlr_output_set_cursor_surface(struct wlr_output *output, struct wlr_surface *surface, int32_t hotspot_x, int32_t hotspot_y) { - if (strcmp(surface->role, "cursor") != 0) { + if (surface && strcmp(surface->role, "cursor") != 0) { return; } output->cursor.hotspot_x = hotspot_x; output->cursor.hotspot_y = hotspot_y; - if (output->cursor.surface == surface) { + if (surface && output->cursor.surface == surface) { return; } output->cursor.surface = surface; @@ -231,6 +229,8 @@ void wlr_output_set_cursor_surface(struct wlr_output *output, if (surface != NULL) { wl_signal_add(&surface->events.commit, &output->cursor.surface_commit); wl_signal_add(&surface->events.destroy, &output->cursor.surface_destroy); + } else { + set_cursor(output, NULL, 0, 0, 0, hotspot_x, hotspot_y); } } From 008e1d596d00614ab4ead873a96aa2220b88bcd7 Mon Sep 17 00:00:00 2001 From: emersion Date: Mon, 9 Oct 2017 00:02:21 +0200 Subject: [PATCH 09/16] Fix cursor stride --- types/wlr_output.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/types/wlr_output.c b/types/wlr_output.c index fbecbb2e..d772ea06 100644 --- a/types/wlr_output.c +++ b/types/wlr_output.c @@ -190,8 +190,9 @@ static void handle_cursor_surface_commit(struct wl_listener *listener, void *buffer_data = wl_shm_buffer_get_data(buffer); int32_t width = wl_shm_buffer_get_width(buffer); int32_t height = wl_shm_buffer_get_height(buffer); + int32_t stride = wl_shm_buffer_get_stride(buffer); wl_shm_buffer_begin_access(buffer); - wlr_output_set_cursor(output, buffer_data, width, width, height, + wlr_output_set_cursor(output, buffer_data, stride/4, width, height, output->cursor.hotspot_x, output->cursor.hotspot_y); wl_shm_buffer_end_access(buffer); } From c4c948c5d987ca03449635979e431abc19e46be0 Mon Sep 17 00:00:00 2001 From: emersion Date: Mon, 9 Oct 2017 00:08:54 +0200 Subject: [PATCH 10/16] Use attach x,y when computing hotspot, set sx,sy in surface_attach --- types/wlr_output.c | 3 ++- types/wlr_surface.c | 2 ++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/types/wlr_output.c b/types/wlr_output.c index d772ea06..83d6713f 100644 --- a/types/wlr_output.c +++ b/types/wlr_output.c @@ -193,7 +193,8 @@ static void handle_cursor_surface_commit(struct wl_listener *listener, int32_t stride = wl_shm_buffer_get_stride(buffer); wl_shm_buffer_begin_access(buffer); wlr_output_set_cursor(output, buffer_data, stride/4, width, height, - output->cursor.hotspot_x, output->cursor.hotspot_y); + output->cursor.hotspot_x - surface->current->sx, + output->cursor.hotspot_y - surface->current->sy); wl_shm_buffer_end_access(buffer); } diff --git a/types/wlr_surface.c b/types/wlr_surface.c index e675ac69..7b620c6b 100644 --- a/types/wlr_surface.c +++ b/types/wlr_surface.c @@ -51,6 +51,8 @@ static void surface_attach(struct wl_client *client, struct wlr_surface *surface = wl_resource_get_user_data(resource); surface->pending->invalid |= WLR_SURFACE_INVALID_BUFFER; + surface->pending->sx = sx; + surface->pending->sy = sy; wlr_surface_state_reset_buffer(surface->pending); wlr_surface_state_set_buffer(surface->pending, buffer); } From 4d157fe5de78d632548df8f523f04a53b5688417 Mon Sep 17 00:00:00 2001 From: emersion Date: Mon, 9 Oct 2017 12:51:28 +0200 Subject: [PATCH 11/16] Switch back to compositor cursor when leaving view --- include/rootston/input.h | 1 + rootston/cursor.c | 27 ++++++++++++++++++++++----- 2 files changed, 23 insertions(+), 5 deletions(-) diff --git a/include/rootston/input.h b/include/rootston/input.h index bdd2a104..43408516 100644 --- a/include/rootston/input.h +++ b/include/rootston/input.h @@ -79,6 +79,7 @@ struct roots_input { struct wlr_xcursor_theme *theme; struct wlr_xcursor *xcursor; struct wlr_seat *wl_seat; + bool client_cursor; enum roots_cursor_mode mode; struct roots_view *active_view, *last_active_view; diff --git a/rootston/cursor.c b/rootston/cursor.c index 97f9ca83..106e054f 100644 --- a/rootston/cursor.c +++ b/rootston/cursor.c @@ -58,6 +58,21 @@ void view_begin_rotate(struct roots_input *input, struct wlr_cursor *cursor, wlr_seat_pointer_clear_focus(input->wl_seat); } +static void cursor_set_xcursor_image(struct roots_input *input, + struct wlr_xcursor_image *image) { + struct roots_output *output; + wl_list_for_each(output, &input->server->desktop->outputs, link) { + if (!wlr_output_set_cursor(output->wlr_output, image->buffer, + image->width, image->width, image->height, + image->hotspot_x, image->hotspot_y)) { + wlr_log(L_DEBUG, "Failed to set hardware cursor"); + return; + } + } + + input->client_cursor = false; +} + void cursor_update_position(struct roots_input *input, uint32_t time) { struct roots_desktop *desktop = input->server->desktop; struct roots_view *view; @@ -72,6 +87,10 @@ void cursor_update_position(struct roots_input *input, uint32_t time) { wlr_seat_pointer_notify_motion(input->wl_seat, time, sx, sy); } else { wlr_seat_pointer_clear_focus(input->wl_seat); + if (input->client_cursor) { + wlr_log(L_DEBUG, "Switching to compositor cursor"); + cursor_set_xcursor_image(input, input->xcursor->images[0]); + } } break; case ROOTS_CURSOR_MOVE: @@ -279,18 +298,16 @@ static void handle_request_set_cursor(struct wl_listener *listener, struct roots_input *input = wl_container_of(listener, input, request_set_cursor); struct wlr_seat_pointer_request_set_cursor_event *event = data; - if (event->surface == NULL) { - wlr_log(L_DEBUG, "handle_request_set_cursor with NULL surface"); - return; - } - wlr_log(L_DEBUG, "handle_request_set_cursor"); + wlr_log(L_DEBUG, "Setting client cursor"); struct roots_output *output; wl_list_for_each(output, &input->server->desktop->outputs, link) { wlr_output_set_cursor_surface(output->wlr_output, event->surface, event->hotspot_x, event->hotspot_y); } + + input->client_cursor = true; } void cursor_initialize(struct roots_input *input) { From f44c44502df83a6774947bce161784c51c79e645 Mon Sep 17 00:00:00 2001 From: emersion Date: Mon, 9 Oct 2017 13:06:53 +0200 Subject: [PATCH 12/16] Check client is focused before setting client cursor --- rootston/cursor.c | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/rootston/cursor.c b/rootston/cursor.c index 106e054f..2df9bcb5 100644 --- a/rootston/cursor.c +++ b/rootston/cursor.c @@ -299,6 +299,21 @@ static void handle_request_set_cursor(struct wl_listener *listener, request_set_cursor); struct wlr_seat_pointer_request_set_cursor_event *event = data; + struct wlr_surface *focused_surface = NULL; + double sx, sy; + view_at(input->server->desktop, input->cursor->x, input->cursor->y, + &focused_surface, &sx, &sy); + bool ok = focused_surface != NULL; + if (focused_surface != NULL) { + struct wl_client *focused_client = + wl_resource_get_client(focused_surface->resource); + ok = event->client == focused_client; + } + if (!ok) { + wlr_log(L_DEBUG, "Denying request to set cursor outside view"); + return; + } + wlr_log(L_DEBUG, "Setting client cursor"); struct roots_output *output; From 84921740881f638622c6a0c1b4ab319218f1f7ac Mon Sep 17 00:00:00 2001 From: emersion Date: Mon, 9 Oct 2017 13:12:38 +0200 Subject: [PATCH 13/16] Switch back to compositor cursor when switching from one view to another --- include/rootston/input.h | 2 +- rootston/cursor.c | 17 ++++++++--------- 2 files changed, 9 insertions(+), 10 deletions(-) diff --git a/include/rootston/input.h b/include/rootston/input.h index 43408516..fbabbdb6 100644 --- a/include/rootston/input.h +++ b/include/rootston/input.h @@ -79,7 +79,7 @@ struct roots_input { struct wlr_xcursor_theme *theme; struct wlr_xcursor *xcursor; struct wlr_seat *wl_seat; - bool client_cursor; + struct roots_view *client_cursor_view; enum roots_cursor_mode mode; struct roots_view *active_view, *last_active_view; diff --git a/rootston/cursor.c b/rootston/cursor.c index 2df9bcb5..eeb657ad 100644 --- a/rootston/cursor.c +++ b/rootston/cursor.c @@ -69,8 +69,6 @@ static void cursor_set_xcursor_image(struct roots_input *input, return; } } - - input->client_cursor = false; } void cursor_update_position(struct roots_input *input, uint32_t time) { @@ -82,15 +80,16 @@ void cursor_update_position(struct roots_input *input, uint32_t time) { case ROOTS_CURSOR_PASSTHROUGH: view = view_at(desktop, input->cursor->x, input->cursor->y, &surface, &sx, &sy); + if (view != input->client_cursor_view) { + wlr_log(L_DEBUG, "Switching to compositor cursor"); + cursor_set_xcursor_image(input, input->xcursor->images[0]); + input->client_cursor_view = NULL; + } if (view) { wlr_seat_pointer_notify_enter(input->wl_seat, surface, sx, sy); wlr_seat_pointer_notify_motion(input->wl_seat, time, sx, sy); } else { wlr_seat_pointer_clear_focus(input->wl_seat); - if (input->client_cursor) { - wlr_log(L_DEBUG, "Switching to compositor cursor"); - cursor_set_xcursor_image(input, input->xcursor->images[0]); - } } break; case ROOTS_CURSOR_MOVE: @@ -301,8 +300,8 @@ static void handle_request_set_cursor(struct wl_listener *listener, struct wlr_surface *focused_surface = NULL; double sx, sy; - view_at(input->server->desktop, input->cursor->x, input->cursor->y, - &focused_surface, &sx, &sy); + struct roots_view *focused_view = view_at(input->server->desktop, + input->cursor->x, input->cursor->y, &focused_surface, &sx, &sy); bool ok = focused_surface != NULL; if (focused_surface != NULL) { struct wl_client *focused_client = @@ -322,7 +321,7 @@ static void handle_request_set_cursor(struct wl_listener *listener, event->hotspot_x, event->hotspot_y); } - input->client_cursor = true; + input->client_cursor_view = focused_view; } void cursor_initialize(struct roots_input *input) { From b76e64e7bf5c24488cf6a37174d1858e2256578a Mon Sep 17 00:00:00 2001 From: emersion Date: Mon, 9 Oct 2017 15:23:58 +0200 Subject: [PATCH 14/16] Fix cleanup old cursor surface in wlr_output_set_cursor_surface --- types/wlr_output.c | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/types/wlr_output.c b/types/wlr_output.c index 83d6713f..011451be 100644 --- a/types/wlr_output.c +++ b/types/wlr_output.c @@ -171,12 +171,8 @@ bool wlr_output_set_cursor(struct wlr_output *output, return set_cursor(output, buf, stride, width, height, hotspot_x, hotspot_y); } -static void handle_cursor_surface_commit(struct wl_listener *listener, - void *data) { - struct wlr_output *output = wl_container_of(listener, output, - cursor.surface_commit); - struct wlr_surface *surface = data; - +static void commit_cursor_surface(struct wlr_output *output, + struct wlr_surface *surface) { struct wl_shm_buffer *buffer = wl_shm_buffer_get(surface->current->buffer); if (buffer == NULL) { return; @@ -198,6 +194,15 @@ static void handle_cursor_surface_commit(struct wl_listener *listener, wl_shm_buffer_end_access(buffer); } +static void handle_cursor_surface_commit(struct wl_listener *listener, + void *data) { + struct wlr_output *output = wl_container_of(listener, output, + cursor.surface_commit); + struct wlr_surface *surface = data; + + commit_cursor_surface(output, surface); +} + static void handle_cursor_surface_destroy(struct wl_listener *listener, void *data) { struct wlr_output *output = wl_container_of(listener, output, @@ -218,9 +223,9 @@ void wlr_output_set_cursor_surface(struct wlr_output *output, output->cursor.hotspot_y = hotspot_y; if (surface && output->cursor.surface == surface) { + commit_cursor_surface(output, surface); return; } - output->cursor.surface = surface; if (output->cursor.surface) { wl_list_remove(&output->cursor.surface_commit.link); @@ -228,9 +233,12 @@ void wlr_output_set_cursor_surface(struct wlr_output *output, output->cursor.surface = NULL; } + output->cursor.surface = surface; + if (surface != NULL) { wl_signal_add(&surface->events.commit, &output->cursor.surface_commit); wl_signal_add(&surface->events.destroy, &output->cursor.surface_destroy); + commit_cursor_surface(output, surface); } else { set_cursor(output, NULL, 0, 0, 0, hotspot_x, hotspot_y); } From 8f7962ed44a1f2b9cc9792cfa506ed76ec31dcc0 Mon Sep 17 00:00:00 2001 From: emersion Date: Mon, 9 Oct 2017 15:45:46 +0200 Subject: [PATCH 15/16] Do not commit surface on set_cursor, breaks GTK+ apps --- types/wlr_output.c | 19 ++++++------------- 1 file changed, 6 insertions(+), 13 deletions(-) diff --git a/types/wlr_output.c b/types/wlr_output.c index 011451be..cc4022eb 100644 --- a/types/wlr_output.c +++ b/types/wlr_output.c @@ -171,8 +171,12 @@ bool wlr_output_set_cursor(struct wlr_output *output, return set_cursor(output, buf, stride, width, height, hotspot_x, hotspot_y); } -static void commit_cursor_surface(struct wlr_output *output, - struct wlr_surface *surface) { +static void handle_cursor_surface_commit(struct wl_listener *listener, + void *data) { + struct wlr_output *output = wl_container_of(listener, output, + cursor.surface_commit); + struct wlr_surface *surface = data; + struct wl_shm_buffer *buffer = wl_shm_buffer_get(surface->current->buffer); if (buffer == NULL) { return; @@ -194,15 +198,6 @@ static void commit_cursor_surface(struct wlr_output *output, wl_shm_buffer_end_access(buffer); } -static void handle_cursor_surface_commit(struct wl_listener *listener, - void *data) { - struct wlr_output *output = wl_container_of(listener, output, - cursor.surface_commit); - struct wlr_surface *surface = data; - - commit_cursor_surface(output, surface); -} - static void handle_cursor_surface_destroy(struct wl_listener *listener, void *data) { struct wlr_output *output = wl_container_of(listener, output, @@ -223,7 +218,6 @@ void wlr_output_set_cursor_surface(struct wlr_output *output, output->cursor.hotspot_y = hotspot_y; if (surface && output->cursor.surface == surface) { - commit_cursor_surface(output, surface); return; } @@ -238,7 +232,6 @@ void wlr_output_set_cursor_surface(struct wlr_output *output, if (surface != NULL) { wl_signal_add(&surface->events.commit, &output->cursor.surface_commit); wl_signal_add(&surface->events.destroy, &output->cursor.surface_destroy); - commit_cursor_surface(output, surface); } else { set_cursor(output, NULL, 0, 0, 0, hotspot_x, hotspot_y); } From b1cba36aebb6629fc2019360197f34394abaf2ac Mon Sep 17 00:00:00 2001 From: emersion Date: Mon, 9 Oct 2017 19:34:56 +0200 Subject: [PATCH 16/16] Call frame_callback_list after rendering the cursor --- types/wlr_output.c | 31 ++++++++++++++++++++++++++----- 1 file changed, 26 insertions(+), 5 deletions(-) diff --git a/types/wlr_output.c b/types/wlr_output.c index cc4022eb..13a6adbd 100644 --- a/types/wlr_output.c +++ b/types/wlr_output.c @@ -1,7 +1,9 @@ +#define _POSIX_C_SOURCE 199309L #include #include #include #include +#include #include #include #include @@ -171,12 +173,12 @@ bool wlr_output_set_cursor(struct wlr_output *output, return set_cursor(output, buf, stride, width, height, hotspot_x, hotspot_y); } -static void handle_cursor_surface_commit(struct wl_listener *listener, - void *data) { - struct wlr_output *output = wl_container_of(listener, output, - cursor.surface_commit); - struct wlr_surface *surface = data; +static inline int64_t timespec_to_msec(const struct timespec *a) { + return (int64_t)a->tv_sec * 1000 + a->tv_nsec / 1000000; +} +static void commit_cursor_surface(struct wlr_output *output, + struct wlr_surface *surface) { struct wl_shm_buffer *buffer = wl_shm_buffer_get(surface->current->buffer); if (buffer == NULL) { return; @@ -198,6 +200,25 @@ static void handle_cursor_surface_commit(struct wl_listener *listener, wl_shm_buffer_end_access(buffer); } +static void handle_cursor_surface_commit(struct wl_listener *listener, + void *data) { + struct wlr_output *output = wl_container_of(listener, output, + cursor.surface_commit); + struct wlr_surface *surface = data; + + commit_cursor_surface(output, surface); + + struct timespec now; + clock_gettime(CLOCK_MONOTONIC, &now); + + struct wlr_frame_callback *cb, *cnext; + wl_list_for_each_safe(cb, cnext, &surface->current->frame_callback_list, + link) { + wl_callback_send_done(cb->resource, timespec_to_msec(&now)); + wl_resource_destroy(cb->resource); + } +} + static void handle_cursor_surface_destroy(struct wl_listener *listener, void *data) { struct wlr_output *output = wl_container_of(listener, output,