Transform cursor hotspot, fix wayland cursor hotspot

This commit is contained in:
emersion 2017-10-29 17:43:26 +01:00
parent 6656e25fd4
commit 95566c6bdf
No known key found for this signature in database
GPG key ID: 0FDE7BE0E88F5E48
6 changed files with 150 additions and 110 deletions

View file

@ -55,25 +55,27 @@ 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, bool update_pixels) { int32_t hotspot_x, int32_t hotspot_y, bool update_pixels) {
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;
output->cursor.hotspot_x = hotspot_x;
output->cursor.hotspot_y = hotspot_y;
if (!update_pixels) { if (!update_pixels) {
// Update hotspot without changing cursor image // Update hotspot without changing cursor image
wlr_wl_output_update_cursor(output, output->enter_serial, hotspot_x, wlr_wl_output_update_cursor(output);
hotspot_y);
return true; return true;
} }
if (!buf) { if (!buf) {
// Hide cursor // Hide cursor
if (output->cursor_surface) { if (output->cursor.surface) {
wl_surface_destroy(output->cursor_surface); wl_surface_destroy(output->cursor.surface);
munmap(output->cursor_data, output->cursor_buf_size); munmap(output->cursor.data, output->cursor.buf_size);
output->cursor_surface = NULL; output->cursor.surface = NULL;
output->cursor_buf_size = 0; output->cursor.buf_size = 0;
} }
wlr_wl_output_update_cursor(output, output->enter_serial, hotspot_x, wlr_wl_output_update_cursor(output);
hotspot_y);
return true; return true;
} }
@ -84,73 +86,77 @@ static bool wlr_wl_output_set_cursor(struct wlr_output *_output,
return false; return false;
} }
if (!output->cursor_surface) { if (!output->cursor.surface) {
output->cursor_surface = wl_compositor_create_surface(output->backend->compositor); output->cursor.surface =
wl_compositor_create_surface(output->backend->compositor);
} }
uint32_t size = stride * height; uint32_t size = stride * height;
if (output->cursor_buf_size != size) { if (output->cursor.buf_size != size) {
if (output->cursor_buffer) { if (output->cursor.buffer) {
wl_buffer_destroy(output->cursor_buffer); wl_buffer_destroy(output->cursor.buffer);
} }
if (size > output->cursor_buf_size) { if (size > output->cursor.buf_size) {
if (output->cursor_pool) { if (output->cursor.pool) {
wl_shm_pool_destroy(output->cursor_pool); wl_shm_pool_destroy(output->cursor.pool);
output->cursor_pool = NULL; output->cursor.pool = NULL;
munmap(output->cursor_data, output->cursor_buf_size); munmap(output->cursor.data, output->cursor.buf_size);
} }
} }
if (!output->cursor_pool) { if (!output->cursor.pool) {
int fd = os_create_anonymous_file(size); int fd = os_create_anonymous_file(size);
if (fd < 0) { if (fd < 0) {
wlr_log_errno(L_INFO, "creating anonymous file for cursor buffer failed"); wlr_log_errno(L_INFO,
"creating anonymous file for cursor buffer failed");
return false; return false;
} }
output->cursor_data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); output->cursor.data = mmap(NULL, size, PROT_READ | PROT_WRITE,
if (output->cursor_data == MAP_FAILED) { MAP_SHARED, fd, 0);
if (output->cursor.data == MAP_FAILED) {
close(fd); close(fd);
wlr_log_errno(L_INFO, "mmap failed"); wlr_log_errno(L_INFO, "mmap failed");
return false; return false;
} }
output->cursor_pool = wl_shm_create_pool(backend->shm, fd, size); output->cursor.pool = wl_shm_create_pool(backend->shm, fd, size);
close(fd); close(fd);
} }
output->cursor_buffer = wl_shm_pool_create_buffer(output->cursor_pool, output->cursor.buffer = wl_shm_pool_create_buffer(output->cursor.pool,
0, width, height, stride, WL_SHM_FORMAT_ARGB8888); 0, width, height, stride, WL_SHM_FORMAT_ARGB8888);
output->cursor_buf_size = size; output->cursor.buf_size = size;
} }
memcpy(output->cursor_data, buf, size); memcpy(output->cursor.data, buf, size);
wl_surface_attach(output->cursor_surface, output->cursor_buffer, 0, 0); wl_surface_attach(output->cursor.surface, output->cursor.buffer, 0, 0);
wl_surface_damage(output->cursor_surface, 0, 0, width, height); wl_surface_damage(output->cursor.surface, 0, 0, width, height);
wl_surface_commit(output->cursor_surface); wl_surface_commit(output->cursor.surface);
wlr_wl_output_update_cursor(output, output->enter_serial, wlr_wl_output_update_cursor(output);
hotspot_x, hotspot_y);
return true; return true;
} }
static void wlr_wl_output_destroy(struct wlr_output *_output) { static void wlr_wl_output_destroy(struct wlr_output *_output) {
struct wlr_wl_backend_output *output = (struct wlr_wl_backend_output *)_output; struct wlr_wl_backend_output *output =
wl_signal_emit(&output->backend->backend.events.output_remove, &output->wlr_output); (struct wlr_wl_backend_output *)_output;
wl_signal_emit(&output->backend->backend.events.output_remove,
&output->wlr_output);
if (output->cursor_buf_size != 0) { if (output->cursor.buf_size != 0) {
assert(output->cursor_data); assert(output->cursor.data);
assert(output->cursor_buffer); assert(output->cursor.buffer);
assert(output->cursor_pool); assert(output->cursor.pool);
wl_buffer_destroy(output->cursor_buffer); wl_buffer_destroy(output->cursor.buffer);
munmap(output->cursor_data, output->cursor_buf_size); munmap(output->cursor.data, output->cursor.buf_size);
wl_shm_pool_destroy(output->cursor_pool); wl_shm_pool_destroy(output->cursor.pool);
} }
if (output->cursor_surface) { if (output->cursor.surface) {
wl_surface_destroy(output->cursor_surface); wl_surface_destroy(output->cursor.surface);
} }
if (output->frame_callback) { if (output->frame_callback) {
@ -164,11 +170,11 @@ static void wlr_wl_output_destroy(struct wlr_output *_output) {
free(output); free(output);
} }
void wlr_wl_output_update_cursor(struct wlr_wl_backend_output *output, void wlr_wl_output_update_cursor(struct wlr_wl_backend_output *output) {
uint32_t serial, int32_t hotspot_x, int32_t hotspot_y) { if (output->backend->pointer && output->enter_serial) {
if (output->backend->pointer && serial) { wl_pointer_set_cursor(output->backend->pointer, output->enter_serial,
wl_pointer_set_cursor(output->backend->pointer, serial, output->cursor.surface, output->cursor.hotspot_x,
output->cursor_surface, hotspot_x, hotspot_y); output->cursor.hotspot_y);
} }
} }

View file

@ -23,8 +23,8 @@ static void pointer_handle_enter(void *data, struct wl_pointer *wl_pointer,
wlr_wl_output_for_surface(wlr_wl_dev->backend, surface); wlr_wl_output_for_surface(wlr_wl_dev->backend, surface);
assert(output); assert(output);
wlr_wl_pointer->current_output = output; wlr_wl_pointer->current_output = output;
wlr_wl_pointer->current_output->enter_serial = serial; output->enter_serial = serial;
wlr_wl_output_update_cursor(wlr_wl_pointer->current_output, serial, 0, 0); wlr_wl_output_update_cursor(output);
} }
static void pointer_handle_leave(void *data, struct wl_pointer *wl_pointer, static void pointer_handle_leave(void *data, struct wl_pointer *wl_pointer,

View file

@ -43,11 +43,15 @@ struct wlr_wl_backend_output {
struct wl_egl_window *egl_window; struct wl_egl_window *egl_window;
struct wl_callback *frame_callback; struct wl_callback *frame_callback;
struct wl_shm_pool *cursor_pool; struct {
void *cursor_buffer; // actually a (client-side) struct wl_buffer* struct wl_shm_pool *pool;
uint8_t *cursor_data; void *buffer; // actually a (client-side) struct wl_buffer*
struct wl_surface *cursor_surface; uint32_t buf_size;
uint32_t cursor_buf_size; uint8_t *data;
struct wl_surface *surface;
int32_t hotspot_x, hotspot_y;
} cursor;
uint32_t enter_serial; uint32_t enter_serial;
void *egl_surface; void *egl_surface;
@ -68,8 +72,7 @@ struct wlr_wl_pointer {
}; };
void wlr_wl_registry_poll(struct wlr_wl_backend *backend); void wlr_wl_registry_poll(struct wlr_wl_backend *backend);
void wlr_wl_output_update_cursor(struct wlr_wl_backend_output *output, void wlr_wl_output_update_cursor(struct wlr_wl_backend_output *output);
uint32_t serial, int32_t hotspot_x, int32_t hotspot_y);
struct wlr_wl_backend_output *wlr_wl_output_for_surface( struct wlr_wl_backend_output *wlr_wl_output_for_surface(
struct wlr_wl_backend *backend, struct wl_surface *surface); struct wlr_wl_backend *backend, struct wl_surface *surface);

View file

@ -15,6 +15,7 @@ struct wlr_output_mode {
struct wlr_output_cursor { struct wlr_output_cursor {
struct wlr_output *output; struct wlr_output *output;
int32_t x, y; int32_t x, y;
uint32_t width, height;
int32_t hotspot_x, hotspot_y; int32_t hotspot_x, hotspot_y;
struct wl_list link; struct wl_list link;

View file

@ -150,9 +150,8 @@ static void wlr_cursor_warp_unchecked(struct wlr_cursor *cur,
double output_y = y; double output_y = y;
wlr_output_layout_output_coords(cur->state->layout, wlr_output_layout_output_coords(cur->state->layout,
output_cursor->output_cursor->output, &output_x, &output_y); output_cursor->output_cursor->output, &output_x, &output_y);
wlr_output_cursor_move(output_cursor->output_cursor, wlr_output_cursor_move(output_cursor->output_cursor, output_x,
output_x - output_cursor->output_cursor->hotspot_x, output_y);
output_y - output_cursor->output_cursor->hotspot_y);
} }
cur->x = x; cur->x = x;

View file

@ -237,6 +237,44 @@ void wlr_output_make_current(struct wlr_output *output) {
output->impl->make_current(output); output->impl->make_current(output);
} }
static void output_cursor_get_effective_hotspot(
struct wlr_output_cursor *cursor, int32_t *hotspot_x,
int32_t *hotspot_y) {
switch (cursor->output->transform) {
case WL_OUTPUT_TRANSFORM_90:
*hotspot_x = cursor->hotspot_x;
*hotspot_y = -cursor->height + cursor->hotspot_y;
break;
case WL_OUTPUT_TRANSFORM_180:
*hotspot_x = cursor->width - cursor->hotspot_x;
*hotspot_y = cursor->height - cursor->hotspot_y;
break;
case WL_OUTPUT_TRANSFORM_270:
*hotspot_x = -cursor->height + cursor->hotspot_x;
*hotspot_y = cursor->hotspot_y;
break;
case WL_OUTPUT_TRANSFORM_FLIPPED:
*hotspot_x = cursor->width - cursor->hotspot_x;
*hotspot_y = cursor->hotspot_y;
break;
case WL_OUTPUT_TRANSFORM_FLIPPED_90:
*hotspot_x = cursor->hotspot_x;
*hotspot_y = -cursor->hotspot_y;
break;
case WL_OUTPUT_TRANSFORM_FLIPPED_180:
*hotspot_x = cursor->hotspot_x;
*hotspot_y = cursor->height - cursor->hotspot_y;
break;
case WL_OUTPUT_TRANSFORM_FLIPPED_270:
*hotspot_x = -cursor->height + cursor->hotspot_x;
*hotspot_y = cursor->width - cursor->hotspot_y;
break;
default: // WL_OUTPUT_TRANSFORM_NORMAL
*hotspot_x = cursor->hotspot_x;
*hotspot_y = cursor->hotspot_y;
}
}
static void output_cursor_render(struct wlr_output_cursor *cursor) { static void output_cursor_render(struct wlr_output_cursor *cursor) {
struct wlr_texture *texture = cursor->texture; struct wlr_texture *texture = cursor->texture;
struct wlr_renderer *renderer = cursor->renderer; struct wlr_renderer *renderer = cursor->renderer;
@ -252,9 +290,13 @@ static void output_cursor_render(struct wlr_output_cursor *cursor) {
glEnable(GL_BLEND); glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
int32_t hotspot_x, hotspot_y;
output_cursor_get_effective_hotspot(cursor, &hotspot_x, &hotspot_y);
float matrix[16]; float matrix[16];
wlr_texture_get_matrix(texture, &matrix, wlr_texture_get_matrix(texture, &matrix,
&cursor->output->transform_matrix, cursor->x, cursor->y); &cursor->output->transform_matrix, cursor->x - hotspot_x,
cursor->y - hotspot_y);
wlr_render_with_matrix(renderer, texture, &matrix); wlr_render_with_matrix(renderer, texture, &matrix);
} }
} }
@ -295,55 +337,30 @@ static void output_cursor_reset(struct wlr_output_cursor *cursor) {
} }
} }
static void output_cursor_set_hotspot(struct wlr_output_cursor *cursor,
int32_t hotspot_x, int32_t hotspot_y, uint32_t width, uint32_t height) {
switch (cursor->output->transform) {
case WL_OUTPUT_TRANSFORM_90:
cursor->hotspot_x = hotspot_x;
cursor->hotspot_y = -height + hotspot_y;
break;
case WL_OUTPUT_TRANSFORM_180:
cursor->hotspot_x = width - hotspot_x;
cursor->hotspot_y = height - hotspot_y;
break;
case WL_OUTPUT_TRANSFORM_270:
cursor->hotspot_x = -height + hotspot_x;
cursor->hotspot_y = hotspot_y;
break;
case WL_OUTPUT_TRANSFORM_FLIPPED:
cursor->hotspot_x = width - hotspot_x;
cursor->hotspot_y = hotspot_y;
break;
case WL_OUTPUT_TRANSFORM_FLIPPED_90:
cursor->hotspot_x = hotspot_x;
cursor->hotspot_y = -hotspot_y;
break;
case WL_OUTPUT_TRANSFORM_FLIPPED_180:
cursor->hotspot_x = hotspot_x;
cursor->hotspot_y = height - hotspot_y;
break;
case WL_OUTPUT_TRANSFORM_FLIPPED_270:
cursor->hotspot_x = -height + hotspot_x;
cursor->hotspot_y = width - hotspot_y;
break;
default: // WL_OUTPUT_TRANSFORM_NORMAL
cursor->hotspot_x = hotspot_x;
cursor->hotspot_y = hotspot_y;
}
}
bool wlr_output_cursor_set_image(struct wlr_output_cursor *cursor, bool wlr_output_cursor_set_image(struct wlr_output_cursor *cursor,
const uint8_t *pixels, int32_t stride, uint32_t width, uint32_t height, const uint8_t *pixels, 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) {
output_cursor_reset(cursor); output_cursor_reset(cursor);
output_cursor_set_hotspot(cursor, hotspot_x, hotspot_y, width, height);
cursor->width = width;
cursor->height = height;
cursor->hotspot_x = hotspot_x;
cursor->hotspot_y = hotspot_y;
if (cursor->output->hardware_cursor == NULL && if (cursor->output->hardware_cursor == NULL &&
cursor->output->impl->set_cursor && cursor->output->impl->set_cursor) {
cursor->output->impl->set_cursor(cursor->output, pixels, stride, int32_t hotspot_eff_x, hotspot_eff_y;
width, height, cursor->hotspot_x, cursor->hotspot_y, true)) { output_cursor_get_effective_hotspot(cursor, &hotspot_eff_x,
cursor->output->hardware_cursor = cursor; &hotspot_eff_y);
return true;
// TODO: also transform pixels with output->transform before calling
// set_cursor
int ok = cursor->output->impl->set_cursor(cursor->output, pixels,
stride, width, height, hotspot_eff_x, hotspot_eff_y, true);
if (ok) {
cursor->output->hardware_cursor = cursor;
return true;
}
} }
wlr_log(L_INFO, "Falling back to software cursor"); wlr_log(L_INFO, "Falling back to software cursor");
@ -366,6 +383,13 @@ bool wlr_output_cursor_set_image(struct wlr_output_cursor *cursor,
stride, width, height, pixels); stride, width, height, pixels);
} }
static void output_cursor_commit(struct wlr_output_cursor *cursor) {
cursor->width = cursor->surface->current->width;
cursor->height = cursor->surface->current->height;
// TODO: if hardware cursor, upload pixels
}
static inline int64_t timespec_to_msec(const struct timespec *a) { static inline int64_t timespec_to_msec(const struct timespec *a) {
return (int64_t)a->tv_sec * 1000 + a->tv_nsec / 1000000; return (int64_t)a->tv_sec * 1000 + a->tv_nsec / 1000000;
} }
@ -376,7 +400,7 @@ static void output_cursor_handle_commit(struct wl_listener *listener,
surface_commit); surface_commit);
struct wlr_surface *surface = data; struct wlr_surface *surface = data;
// TODO: if hardware cursor, upload pixels output_cursor_commit(cursor);
struct timespec now; struct timespec now;
clock_gettime(CLOCK_MONOTONIC, &now); clock_gettime(CLOCK_MONOTONIC, &now);
@ -402,16 +426,23 @@ void wlr_output_cursor_set_surface(struct wlr_output_cursor *cursor,
return; return;
} }
output_cursor_set_hotspot(cursor, hotspot_x, hotspot_y, if (surface) {
surface->current->width, surface->current->height); cursor->width = surface->current->width;
cursor->height = surface->current->height;
}
cursor->hotspot_x = hotspot_x;
cursor->hotspot_y = hotspot_y;
if (surface && surface == cursor->surface) { if (surface && surface == cursor->surface) {
if (cursor->output->hardware_cursor == cursor && if (cursor->output->hardware_cursor == cursor &&
cursor->output->impl->set_cursor) { cursor->output->impl->set_cursor) {
// If the surface hasn't changed and it's an hardware cursor, only // If the surface hasn't changed and it's an hardware cursor, only
// update the hotspot // update the hotspot
int32_t hotspot_eff_x, hotspot_eff_y;
output_cursor_get_effective_hotspot(cursor, &hotspot_eff_x,
&hotspot_eff_y);
cursor->output->impl->set_cursor(cursor->output, NULL, 0, 0, 0, cursor->output->impl->set_cursor(cursor->output, NULL, 0, 0, 0,
cursor->hotspot_x, cursor->hotspot_y, false); hotspot_eff_x, hotspot_eff_y, false);
} }
return; return;
} }
@ -432,7 +463,7 @@ void wlr_output_cursor_set_surface(struct wlr_output_cursor *cursor,
if (surface != NULL) { if (surface != NULL) {
wl_signal_add(&surface->events.commit, &cursor->surface_commit); wl_signal_add(&surface->events.commit, &cursor->surface_commit);
wl_signal_add(&surface->events.destroy, &cursor->surface_destroy); wl_signal_add(&surface->events.destroy, &cursor->surface_destroy);
// TODO: if hardware cursor, upload pixels output_cursor_commit(cursor);
} else { } else {
// TODO: if hardware cursor, disable cursor // TODO: if hardware cursor, disable cursor
} }