Merge pull request #206 from emersion/client-cursor

Client provided cursors
This commit is contained in:
Drew DeVault 2017-10-09 17:56:05 -04:00 committed by GitHub
commit c39bfe7f84
8 changed files with 245 additions and 31 deletions

View File

@ -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, static bool wlr_wl_output_set_cursor(struct wlr_output *_output,
const uint8_t *buf, int32_t stride, uint32_t width, uint32_t height, const uint8_t *buf, int32_t stride, uint32_t width, uint32_t height,
int32_t hotspot_x, int32_t hotspot_y) { int32_t hotspot_x, int32_t hotspot_y) {
struct wlr_wl_backend_output *output = (struct wlr_wl_backend_output *)_output; struct wlr_wl_backend_output *output = (struct wlr_wl_backend_output *)_output;
struct wlr_wl_backend *backend = output->backend; 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 stride *= 4; // stride is given in pixels, we need it in bytes
if (!backend->shm || !backend->pointer) { if (!backend->shm || !backend->pointer) {

View File

@ -79,6 +79,7 @@ struct roots_input {
struct wlr_xcursor_theme *theme; struct wlr_xcursor_theme *theme;
struct wlr_xcursor *xcursor; struct wlr_xcursor *xcursor;
struct wlr_seat *wl_seat; struct wlr_seat *wl_seat;
struct roots_view *client_cursor_view;
enum roots_cursor_mode mode; enum roots_cursor_mode mode;
struct roots_view *active_view, *last_active_view; struct roots_view *active_view, *last_active_view;
@ -107,6 +108,8 @@ struct roots_input {
struct wl_listener cursor_tool_tip; struct wl_listener cursor_tool_tip;
struct wl_listener pointer_grab_end; struct wl_listener pointer_grab_end;
struct wl_listener request_set_cursor;
}; };
struct roots_input *input_create(struct roots_server *server, struct roots_input *input_create(struct roots_server *server,

View File

@ -49,11 +49,18 @@ struct wlr_output {
int32_t hotspot_x, hotspot_y; int32_t hotspot_x, hotspot_y;
struct wlr_renderer *renderer; struct wlr_renderer *renderer;
struct wlr_texture *texture; 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; } cursor;
void *data; void *data;
}; };
struct wlr_surface;
void wlr_output_enable(struct wlr_output *output, bool enable); void wlr_output_enable(struct wlr_output *output, bool enable);
bool wlr_output_set_mode(struct wlr_output *output, bool wlr_output_set_mode(struct wlr_output *output,
struct wlr_output_mode *mode); struct wlr_output_mode *mode);
@ -62,6 +69,8 @@ void wlr_output_transform(struct wlr_output *output,
bool wlr_output_set_cursor(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, const uint8_t *buf, int32_t stride, uint32_t width, uint32_t height,
int32_t hotspot_x, int32_t hotspot_y); 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); bool wlr_output_move_cursor(struct wlr_output *output, int x, int y);
void wlr_output_destroy(struct wlr_output *output); void wlr_output_destroy(struct wlr_output *output);
void wlr_output_effective_resolution(struct wlr_output *output, void wlr_output_effective_resolution(struct wlr_output *output,

View File

@ -125,11 +125,20 @@ struct wlr_seat {
struct wl_signal keyboard_grab_begin; struct wl_signal keyboard_grab_begin;
struct wl_signal keyboard_grab_end; struct wl_signal keyboard_grab_end;
struct wl_signal request_set_cursor;
} events; } events;
void *data; 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. * Allocates a new wlr_seat and adds a wl_seat global to the display.
*/ */

View File

@ -58,6 +58,19 @@ void view_begin_rotate(struct roots_input *input, struct wlr_cursor *cursor,
wlr_seat_pointer_clear_focus(input->wl_seat); 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) { void cursor_update_position(struct roots_input *input, uint32_t time) {
struct roots_desktop *desktop = input->server->desktop; struct roots_desktop *desktop = input->server->desktop;
struct roots_view *view; struct roots_view *view;
@ -67,6 +80,11 @@ void cursor_update_position(struct roots_input *input, uint32_t time) {
case ROOTS_CURSOR_PASSTHROUGH: case ROOTS_CURSOR_PASSTHROUGH:
view = view_at(desktop, input->cursor->x, input->cursor->y, &surface, view = view_at(desktop, input->cursor->x, input->cursor->y, &surface,
&sx, &sy); &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) { if (view) {
wlr_seat_pointer_notify_enter(input->wl_seat, surface, sx, sy); wlr_seat_pointer_notify_enter(input->wl_seat, surface, sx, sy);
wlr_seat_pointer_notify_motion(input->wl_seat, time, 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); 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) { void cursor_initialize(struct roots_input *input) {
struct wlr_cursor *cursor = input->cursor; 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); wl_signal_add(&input->wl_seat->events.pointer_grab_end, &input->pointer_grab_end);
input->pointer_grab_end.notify = handle_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, static void reset_device_mappings(struct roots_config *config,

View File

@ -1,9 +1,12 @@
#define _POSIX_C_SOURCE 199309L
#include <assert.h> #include <assert.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <tgmath.h> #include <tgmath.h>
#include <time.h>
#include <wayland-server.h> #include <wayland-server.h>
#include <wlr/types/wlr_output.h> #include <wlr/types/wlr_output.h>
#include <wlr/types/wlr_surface.h>
#include <wlr/interfaces/wlr_output.h> #include <wlr/interfaces/wlr_output.h>
#include <wlr/util/list.h> #include <wlr/util/list.h>
#include <wlr/util/log.h> #include <wlr/util/log.h>
@ -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); 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) { void wlr_output_enable(struct wlr_output *output, bool enable) {
output->impl->enable(output, enable); output->impl->enable(output, enable);
} }
@ -131,9 +122,9 @@ void wlr_output_transform(struct wlr_output *output,
wlr_output_update_matrix(output); wlr_output_update_matrix(output);
} }
bool wlr_output_set_cursor(struct wlr_output *output, static bool set_cursor(struct wlr_output *output, const uint8_t *buf,
const uint8_t *buf, int32_t stride, uint32_t width, uint32_t height, int32_t stride, uint32_t width, uint32_t height, int32_t hotspot_x,
int32_t hotspot_x, int32_t hotspot_y) { int32_t hotspot_y) {
if (output->impl->set_cursor if (output->impl->set_cursor
&& output->impl->set_cursor(output, buf, stride, width, height, && output->impl->set_cursor(output, buf, stride, width, height,
hotspot_x, hotspot_y)) { hotspot_x, hotspot_y)) {
@ -146,8 +137,6 @@ bool wlr_output_set_cursor(struct wlr_output *output,
output->cursor.is_sw = true; output->cursor.is_sw = true;
output->cursor.width = width; output->cursor.width = width;
output->cursor.height = height; output->cursor.height = height;
output->cursor.hotspot_x = hotspot_x;
output->cursor.hotspot_y = hotspot_y;
if (!output->cursor.renderer) { if (!output->cursor.renderer) {
/* NULL egl is okay given that we are only using pixel buffers */ /* NULL egl is okay given that we are only using pixel buffers */
@ -158,7 +147,8 @@ bool wlr_output_set_cursor(struct wlr_output *output,
} }
if (!output->cursor.texture) { 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) { if (!output->cursor.texture) {
return false; return false;
} }
@ -168,6 +158,106 @@ bool wlr_output_set_cursor(struct wlr_output *output,
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) { bool wlr_output_move_cursor(struct wlr_output *output, int x, int y) {
output->cursor.x = x; output->cursor.x = x;
output->cursor.y = y; output->cursor.y = 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); 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) { void wlr_output_destroy(struct wlr_output *output) {
if (!output) { if (!output) {
return; return;

View File

@ -20,12 +20,34 @@ static void pointer_send_frame(struct wl_resource *resource) {
} }
static void wl_pointer_set_cursor(struct wl_client *client, static void wl_pointer_set_cursor(struct wl_client *client,
struct wl_resource *resource, struct wl_resource *resource, uint32_t serial,
uint32_t serial, struct wl_resource *surface_resource,
struct wl_resource *surface, int32_t hotspot_x, int32_t hotspot_y) {
int32_t hotspot_x, struct wlr_seat_handle *handle = wl_resource_get_user_data(resource);
int32_t hotspot_y) { struct wlr_surface *surface = NULL;
wlr_log(L_DEBUG, "TODO: wl_pointer_set_cursor"); 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 = { 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_bound);
wl_signal_init(&wlr_seat->events.client_unbound); 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_begin);
wl_signal_init(&wlr_seat->events.pointer_grab_end); wl_signal_init(&wlr_seat->events.pointer_grab_end);

View File

@ -51,6 +51,8 @@ static void surface_attach(struct wl_client *client,
struct wlr_surface *surface = wl_resource_get_user_data(resource); struct wlr_surface *surface = wl_resource_get_user_data(resource);
surface->pending->invalid |= WLR_SURFACE_INVALID_BUFFER; 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_reset_buffer(surface->pending);
wlr_surface_state_set_buffer(surface->pending, buffer); wlr_surface_state_set_buffer(surface->pending, buffer);
} }