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/rootston/input.h b/include/rootston/input.h index 9caf66c0..fbabbdb6 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; + struct roots_view *client_cursor_view; enum roots_cursor_mode mode; struct roots_view *active_view, *last_active_view; @@ -107,6 +108,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/include/wlr/types/wlr_output.h b/include/wlr/types/wlr_output.h index 52d377e3..dc637e26 100644 --- a/include/wlr/types/wlr_output.h +++ b/include/wlr/types/wlr_output.h @@ -49,23 +49,32 @@ 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; }; +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); +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); 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/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/rootston/cursor.c b/rootston/cursor.c index 605920cc..eeb657ad 100644 --- a/rootston/cursor.c +++ b/rootston/cursor.c @@ -58,6 +58,19 @@ 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; + } + } +} + void cursor_update_position(struct roots_input *input, uint32_t time) { struct roots_desktop *desktop = input->server->desktop; struct roots_view *view; @@ -67,6 +80,11 @@ 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); @@ -274,6 +292,38 @@ 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_surface *focused_surface = NULL; + double 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 = + 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; + 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_view = focused_view; +} + void cursor_initialize(struct roots_input *input) { struct wlr_cursor *cursor = input->cursor; @@ -304,6 +354,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_output.c b/types/wlr_output.c index 64f67f2d..13a6adbd 100644 --- a/types/wlr_output.c +++ b/types/wlr_output.c @@ -1,9 +1,12 @@ +#define _POSIX_C_SOURCE 199309L #include #include #include #include +#include #include #include +#include #include #include #include @@ -98,18 +101,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); } @@ -131,9 +122,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)) { @@ -146,8 +137,6 @@ bool wlr_output_set_cursor(struct wlr_output *output, 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 */ @@ -158,14 +147,115 @@ 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; } } 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(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; + } + + output->cursor.hotspot_x = hotspot_x; + output->cursor.hotspot_y = hotspot_y; + + return set_cursor(output, buf, stride, width, height, hotspot_x, hotspot_y); +} + +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; + } + + uint32_t format = wl_shm_buffer_get_format(buffer); + if (format != WL_SHM_FORMAT_ARGB8888) { + return; + } + + 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, stride/4, width, height, + output->cursor.hotspot_x - surface->current->sx, + output->cursor.hotspot_y - surface->current->sy); + 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, + cursor.surface_destroy); + + 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 (surface && strcmp(surface->role, "cursor") != 0) { + return; + } + + output->cursor.hotspot_x = hotspot_x; + output->cursor.hotspot_y = hotspot_y; + + if (surface && output->cursor.surface == surface) { + return; + } + + if (output->cursor.surface) { + wl_list_remove(&output->cursor.surface_commit.link); + wl_list_remove(&output->cursor.surface_destroy.link); + 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); + } else { + set_cursor(output, NULL, 0, 0, 0, hotspot_x, hotspot_y); + } } bool wlr_output_move_cursor(struct wlr_output *output, int x, int y) { @@ -183,6 +273,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; diff --git a/types/wlr_seat.c b/types/wlr_seat.c index 4566053d..d58b2129 100644 --- a/types/wlr_seat.c +++ b/types/wlr_seat.c @@ -20,12 +20,34 @@ 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 = 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 = + 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 +307,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); diff --git a/types/wlr_surface.c b/types/wlr_surface.c index 71a09bb5..ca7a9af5 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); }