From a7446792a1a0fd9fe3391f041d7bbfe9e2b11255 Mon Sep 17 00:00:00 2001 From: Drew DeVault Date: Sun, 22 Oct 2017 23:19:21 -0400 Subject: [PATCH 01/14] Consider scale factor when rendering views --- include/rootston/config.h | 1 + include/rootston/view.h | 1 + include/wlr/types/wlr_surface.h | 6 +++++- rootston/config.c | 4 ++++ rootston/output.c | 3 +++ types/wlr_output.c | 4 ++-- types/wlr_surface.c | 19 ++++++++++++++++--- 7 files changed, 32 insertions(+), 6 deletions(-) diff --git a/include/rootston/config.h b/include/rootston/config.h index 75c04619..e0466117 100644 --- a/include/rootston/config.h +++ b/include/rootston/config.h @@ -8,6 +8,7 @@ struct output_config { char *name; enum wl_output_transform transform; int x, y; + int scale; struct wl_list link; struct { int width, height; diff --git a/include/rootston/view.h b/include/rootston/view.h index 993ff654..4a5e8d08 100644 --- a/include/rootston/view.h +++ b/include/rootston/view.h @@ -48,6 +48,7 @@ enum roots_view_type { struct roots_view { struct roots_desktop *desktop; + struct roots_output *output; double x, y; float rotation; // TODO: Something for roots-enforced width/height diff --git a/include/wlr/types/wlr_surface.h b/include/wlr/types/wlr_surface.h index ea4184aa..e1a07566 100644 --- a/include/wlr/types/wlr_surface.h +++ b/include/wlr/types/wlr_surface.h @@ -1,10 +1,10 @@ #ifndef WLR_TYPES_WLR_SURFACE_H #define WLR_TYPES_WLR_SURFACE_H - #include #include #include #include +#include struct wlr_frame_callback { struct wl_resource *resource; @@ -135,4 +135,8 @@ struct wlr_surface *wlr_surface_get_main_surface(struct wlr_surface *surface); */ struct wlr_subsurface *wlr_surface_subsurface_at(struct wlr_surface *surface, double sx, double sy, double *sub_x, double *sub_y); + +void wlr_surface_send_enter(struct wlr_surface *surface, + struct wlr_output *output); + #endif diff --git a/rootston/config.c b/rootston/config.c index 18138ab0..983117ba 100644 --- a/rootston/config.c +++ b/rootston/config.c @@ -220,6 +220,7 @@ static int config_ini_handler(void *user, const char *section, const char *name, oc = calloc(1, sizeof(struct output_config)); oc->name = strdup(output_name); oc->transform = WL_OUTPUT_TRANSFORM_NORMAL; + oc->scale = 1; wl_list_insert(&config->outputs, &oc->link); } @@ -227,6 +228,9 @@ static int config_ini_handler(void *user, const char *section, const char *name, oc->x = strtol(value, NULL, 10); } else if (strcmp(name, "y") == 0) { oc->y = strtol(value, NULL, 10); + } else if (strcmp(name, "scale") == 0) { + oc->scale = strtol(value, NULL, 10); + assert(oc->scale >= 1); } else if (strcmp(name, "rotate") == 0) { if (strcmp(value, "90") == 0) { oc->transform = WL_OUTPUT_TRANSFORM_90; diff --git a/rootston/output.c b/rootston/output.c index baa7b6cc..faf39e74 100644 --- a/rootston/output.c +++ b/rootston/output.c @@ -27,6 +27,8 @@ static void render_surface(struct wlr_surface *surface, if (wlr_output_layout_intersects(desktop->layout, wlr_output, lx, ly, lx + width, ly + height)) { + // TODO: accomodate for mismatched scale, which can happen, for + // example, when a view is rendered over two outputs float matrix[16]; float translate_origin[16]; @@ -217,6 +219,7 @@ void output_add_notify(struct wl_listener *listener, void *data) { if (output_config->mode.width) { set_mode(wlr_output, output_config); } + wlr_output->scale = output_config->scale; wlr_output_transform(wlr_output, output_config->transform); wlr_output_layout_add(desktop->layout, wlr_output, output_config->x, output_config->y); diff --git a/types/wlr_output.c b/types/wlr_output.c index 44d24ae3..2f5e3086 100644 --- a/types/wlr_output.c +++ b/types/wlr_output.c @@ -29,8 +29,7 @@ static void wl_output_send_to_resource(struct wl_resource *resource) { if (version >= WL_OUTPUT_MODE_SINCE_VERSION) { struct wlr_output_mode *mode; wl_list_for_each(mode, &output->modes, link) { - // TODO: mode->flags should just be preferred - uint32_t flags = mode->flags; + uint32_t flags = mode->flags & ~WL_OUTPUT_MODE_PREFERRED; if (output->current_mode == mode) { flags |= WL_OUTPUT_MODE_CURRENT; } @@ -45,6 +44,7 @@ static void wl_output_send_to_resource(struct wl_resource *resource) { } } if (version >= WL_OUTPUT_SCALE_SINCE_VERSION) { + wlr_log(L_DEBUG, "Sending scale"); wl_output_send_scale(resource, output->scale); } if (version >= WL_OUTPUT_DONE_SINCE_VERSION) { diff --git a/types/wlr_surface.c b/types/wlr_surface.c index 2a8c4d04..28f3d05b 100644 --- a/types/wlr_surface.c +++ b/types/wlr_surface.c @@ -649,14 +649,14 @@ void wlr_surface_get_matrix(struct wlr_surface *surface, float (*matrix)[16], const float (*projection)[16], const float (*transform)[16]) { - int width = surface->texture->width / surface->current->scale; - int height = surface->texture->height / surface->current->scale; + int width = surface->texture->width; + int height = surface->texture->height; float scale[16]; wlr_matrix_identity(matrix); if (transform) { wlr_matrix_mul(matrix, transform, matrix); } - wlr_matrix_scale(&scale, width, height, 1); + wlr_matrix_scale(&scale, width, height, surface->current->scale); wlr_matrix_mul(matrix, &scale, matrix); wlr_matrix_mul(projection, matrix, matrix); } @@ -894,3 +894,16 @@ struct wlr_subsurface *wlr_surface_subsurface_at(struct wlr_surface *surface, return NULL; } + +void wlr_surface_send_enter(struct wlr_surface *surface, + struct wlr_output *output) { + struct wl_client *client = wl_resource_get_client(surface->resource); + struct wl_resource *resource; + wl_resource_for_each(resource, &output->wl_resources) { + if (client == wl_resource_get_client(resource)) { + wlr_log(L_DEBUG, "sending output enter"); + wl_surface_send_enter(surface->resource, resource); + break; + } + } +} From 9861add146af54836ca85dd5769ab3b966346432 Mon Sep 17 00:00:00 2001 From: Drew DeVault Date: Tue, 24 Oct 2017 08:37:30 -0400 Subject: [PATCH 02/14] Send surface enter output events to clients --- rootston/desktop.c | 32 +++- types/wlr_output.c | 442 ++++++++++++++++++++------------------------- 2 files changed, 226 insertions(+), 248 deletions(-) diff --git a/rootston/desktop.c b/rootston/desktop.c index f93d1df8..37022ca4 100644 --- a/rootston/desktop.c +++ b/rootston/desktop.c @@ -45,14 +45,35 @@ void view_get_size(struct roots_view *view, struct wlr_box *box) { box->height = view->wlr_surface->current->height; } +static void view_update_output(struct roots_view *view) { + struct roots_desktop *desktop = view->desktop; + struct roots_output *output = NULL, *_output; + struct wlr_box box; + view_get_size(view, &box); + wl_list_for_each(_output, &desktop->outputs, link) { + if (!wlr_output_layout_intersects(desktop->layout, _output->wlr_output, + view->x, view->y, view->x + box.width, view->x + box.height)) { + continue; + } + if (output == NULL + || output->wlr_output->scale < _output->wlr_output->scale) { + output = _output; + } + } + if (output && output != view->output) { + view->output = output; + wlr_surface_send_enter(view->wlr_surface, output->wlr_output); + } +} + void view_set_position(struct roots_view *view, double x, double y) { if (view->set_position) { view->set_position(view, x, y); - return; + } else { + view->x = x; + view->y = y; } - - view->x = x; - view->y = y; + view_update_output(view); } void view_activate(struct roots_view *view, bool activate) { @@ -65,6 +86,7 @@ void view_resize(struct roots_view *view, uint32_t width, uint32_t height) { if (view->resize) { view->resize(view, width, height); } + view_update_output(view); } void view_close(struct roots_view *view) { @@ -111,6 +133,8 @@ void view_setup(struct roots_view *view) { struct roots_input *input = view->desktop->server->input; set_view_focus(input, view->desktop, view); + wlr_seat_keyboard_notify_enter(input->wl_seat, view->wlr_surface); + view_update_output(view); } void view_teardown(struct roots_view *view) { diff --git a/types/wlr_output.c b/types/wlr_output.c index 2f5e3086..dd7e8d6a 100644 --- a/types/wlr_output.c +++ b/types/wlr_output.c @@ -5,7 +5,6 @@ #include #include #include -#include #include #include #include @@ -136,9 +135,7 @@ static void wlr_output_update_matrix(struct wlr_output *output) { } void wlr_output_enable(struct wlr_output *output, bool enable) { - if (output->impl->enable) { - output->impl->enable(output, enable); - } + output->impl->enable(output, enable); } bool wlr_output_set_mode(struct wlr_output *output, @@ -191,19 +188,192 @@ void wlr_output_set_position(struct wlr_output *output, int32_t lx, } } +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, true)) { + output->cursor.is_sw = false; + return true; + } + + wlr_log(L_INFO, "Falling back to software cursor"); + + output->cursor.is_sw = true; + output->cursor.width = width; + output->cursor.height = height; + + if (!output->cursor.renderer) { + output->cursor.renderer = wlr_gles2_renderer_create(output->backend); + if (!output->cursor.renderer) { + return false; + } + } + + if (!output->cursor.texture) { + 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); +} + +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) { + if (output->cursor.is_sw) { + return; + } + + 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, "wl_pointer-cursor") != 0) { + return; + } + + output->cursor.hotspot_x = hotspot_x; + output->cursor.hotspot_y = hotspot_y; + + if (surface && surface == output->cursor.surface) { + if (output->impl->set_cursor && !output->cursor.is_sw) { + // Only update the hotspot + output->impl->set_cursor(output, NULL, 0, 0, 0, hotspot_x, + hotspot_y, false); + } + 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; + } + + // Disable hardware cursor + // TODO: support hardware cursors + output->cursor.is_sw = true; + if (output->impl->set_cursor) { + output->impl->set_cursor(output, NULL, 0, 0, 0, hotspot_x, hotspot_y, + true); + } + + //output->cursor.is_sw = output->impl->set_cursor == 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); + } +} + +bool wlr_output_move_cursor(struct wlr_output *output, int x, int y) { + output->cursor.x = x; + output->cursor.y = y; + + if (output->cursor.is_sw) { + return true; + } + + if (!output->impl->move_cursor) { + return false; + } + + return output->impl->move_cursor(output, x, y); +} + void wlr_output_init(struct wlr_output *output, struct wlr_backend *backend, const struct wlr_output_impl *impl) { - assert(impl->make_current && impl->swap_buffers && impl->transform); output->backend = backend; output->impl = impl; wl_list_init(&output->modes); output->transform = WL_OUTPUT_TRANSFORM_NORMAL; output->scale = 1; - wl_list_init(&output->cursors); 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) { @@ -213,6 +383,9 @@ void wlr_output_destroy(struct wlr_output *output) { wl_signal_emit(&output->events.destroy, output); + wlr_texture_destroy(output->cursor.texture); + wlr_renderer_destroy(output->cursor.renderer); + struct wlr_output_mode *mode, *tmp_mode; wl_list_for_each_safe(mode, tmp_mode, &output->modes, link) { free(mode); @@ -227,7 +400,6 @@ void wlr_output_destroy(struct wlr_output *output) { void wlr_output_effective_resolution(struct wlr_output *output, int *width, int *height) { - // TODO: Scale factor if (output->transform % 2 == 1) { *width = output->height; *height = output->width; @@ -241,62 +413,27 @@ void wlr_output_make_current(struct wlr_output *output) { output->impl->make_current(output); } -static void output_cursor_get_box(struct wlr_output_cursor *cursor, - struct wlr_box *box) { - box->x = cursor->x - cursor->hotspot_x; - box->y = cursor->y - cursor->hotspot_y; - box->width = cursor->width; - box->height = cursor->height; -} - -static void output_cursor_render(struct wlr_output_cursor *cursor) { - struct wlr_texture *texture = cursor->texture; - struct wlr_renderer *renderer = cursor->renderer; - if (cursor->surface != NULL) { - // Some clients commit a cursor surface with a NULL buffer to hide it. - if (!wlr_surface_has_buffer(cursor->surface)) { - return; - } - texture = cursor->surface->texture; - renderer = cursor->surface->renderer; - } - - if (texture == NULL || renderer == NULL) { - return; - } - - struct wlr_box output_box; - output_box.x = output_box.y = 0; - output_box.width = cursor->output->width; - output_box.height = cursor->output->height; - - struct wlr_box cursor_box; - output_cursor_get_box(cursor, &cursor_box); - - struct wlr_box intersection; - struct wlr_box *intersection_ptr = &intersection; - if (!wlr_box_intersection(&output_box, &cursor_box, &intersection_ptr)) { - return; - } - - glViewport(0, 0, cursor->output->width, cursor->output->height); - glEnable(GL_BLEND); - glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); - - float matrix[16]; - wlr_texture_get_matrix(texture, &matrix, - &cursor->output->transform_matrix, cursor->x - cursor->hotspot_x, - cursor->y - cursor->hotspot_y); - wlr_render_with_matrix(renderer, texture, &matrix); -} - void wlr_output_swap_buffers(struct wlr_output *output) { - struct wlr_output_cursor *cursor; - wl_list_for_each(cursor, &output->cursors, link) { - if (output->hardware_cursor == cursor) { - continue; + if (output->cursor.is_sw) { + glViewport(0, 0, output->width, output->height); + glEnable(GL_BLEND); + glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); + + struct wlr_texture *texture = output->cursor.texture; + struct wlr_renderer *renderer = output->cursor.renderer; + if (output->cursor.surface) { + texture = output->cursor.surface->texture; + renderer = output->cursor.surface->renderer; + } + + // We check texture->valid because some clients set a cursor surface + // with a NULL buffer to hide it + if (renderer && texture && texture->valid) { + float matrix[16]; + wlr_texture_get_matrix(texture, &matrix, &output->transform_matrix, + output->cursor.x, output->cursor.y); + wlr_render_with_matrix(renderer, texture, &matrix); } - output_cursor_render(cursor); } wl_signal_emit(&output->events.swap_buffers, &output); @@ -317,186 +454,3 @@ uint32_t wlr_output_get_gamma_size(struct wlr_output *output) { } return output->impl->get_gamma_size(output); } - -static void output_cursor_reset(struct wlr_output_cursor *cursor) { - if (cursor->surface != NULL) { - wl_list_remove(&cursor->surface_commit.link); - wl_list_remove(&cursor->surface_destroy.link); - cursor->surface = NULL; - } -} - -bool wlr_output_cursor_set_image(struct wlr_output_cursor *cursor, - const uint8_t *pixels, int32_t stride, uint32_t width, uint32_t height, - int32_t hotspot_x, int32_t hotspot_y) { - output_cursor_reset(cursor); - - cursor->width = width; - cursor->height = height; - cursor->hotspot_x = hotspot_x; - cursor->hotspot_y = hotspot_y; - - if (cursor->output->hardware_cursor == NULL && - cursor->output->impl->set_cursor) { - int ok = cursor->output->impl->set_cursor(cursor->output, pixels, - stride, width, height, hotspot_x, hotspot_y, true); - if (ok) { - cursor->output->hardware_cursor = cursor; - return true; - } - } - - wlr_log(L_INFO, "Falling back to software cursor"); - - if (cursor->renderer == NULL) { - cursor->renderer = wlr_gles2_renderer_create(cursor->output->backend); - if (cursor->renderer == NULL) { - return false; - } - } - - if (cursor->texture == NULL) { - cursor->texture = wlr_render_texture_create(cursor->renderer); - if (cursor->texture == NULL) { - return false; - } - } - - return wlr_texture_upload_pixels(cursor->texture, WL_SHM_FORMAT_ARGB8888, - 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) { - return (int64_t)a->tv_sec * 1000 + a->tv_nsec / 1000000; -} - -static void output_cursor_handle_commit(struct wl_listener *listener, - void *data) { - struct wlr_output_cursor *cursor = wl_container_of(listener, cursor, - surface_commit); - struct wlr_surface *surface = data; - - output_cursor_commit(cursor); - - 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 output_cursor_handle_destroy(struct wl_listener *listener, - void *data) { - struct wlr_output_cursor *cursor = wl_container_of(listener, cursor, - surface_destroy); - output_cursor_reset(cursor); -} - -void wlr_output_cursor_set_surface(struct wlr_output_cursor *cursor, - struct wlr_surface *surface, int32_t hotspot_x, int32_t hotspot_y) { - if (surface && strcmp(surface->role, "wl_pointer-cursor") != 0) { - return; - } - - if (surface) { - 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 (cursor->output->hardware_cursor == cursor && - cursor->output->impl->set_cursor) { - // If the surface hasn't changed and it's an hardware cursor, only - // update the hotspot - cursor->output->impl->set_cursor(cursor->output, NULL, 0, 0, 0, - hotspot_x, hotspot_y, false); - } - return; - } - - output_cursor_reset(cursor); - - // Disable hardware cursor for surfaces - // TODO: support hardware cursors - if (cursor->output->hardware_cursor == cursor && - cursor->output->impl->set_cursor) { - cursor->output->impl->set_cursor(cursor->output, NULL, 0, 0, 0, 0, 0, - true); - cursor->output->hardware_cursor = NULL; - } - - cursor->surface = surface; - - if (surface != NULL) { - wl_signal_add(&surface->events.commit, &cursor->surface_commit); - wl_signal_add(&surface->events.destroy, &cursor->surface_destroy); - output_cursor_commit(cursor); - } else { - // TODO: if hardware cursor, disable cursor - } -} - -bool wlr_output_cursor_move(struct wlr_output_cursor *cursor, int x, int y) { - cursor->x = x; - cursor->y = y; - - if (cursor->output->hardware_cursor != cursor) { - return true; - } - - if (!cursor->output->impl->move_cursor) { - return false; - } - return cursor->output->impl->move_cursor(cursor->output, x, y); -} - -struct wlr_output_cursor *wlr_output_cursor_create(struct wlr_output *output) { - struct wlr_output_cursor *cursor = - calloc(1, sizeof(struct wlr_output_cursor)); - if (cursor == NULL) { - return NULL; - } - cursor->output = output; - wl_list_init(&cursor->surface_commit.link); - cursor->surface_commit.notify = output_cursor_handle_commit; - wl_list_init(&cursor->surface_destroy.link); - cursor->surface_destroy.notify = output_cursor_handle_destroy; - wl_list_insert(&output->cursors, &cursor->link); - return cursor; -} - -void wlr_output_cursor_destroy(struct wlr_output_cursor *cursor) { - if (cursor == NULL) { - return; - } - output_cursor_reset(cursor); - if (cursor->output->hardware_cursor == cursor) { - // If this cursor was the hardware cursor, disable it - if (cursor->output->impl->set_cursor) { - cursor->output->impl->set_cursor(cursor->output, NULL, 0, 0, 0, 0, - 0, true); - } - cursor->output->hardware_cursor = NULL; - } - if (cursor->texture != NULL) { - wlr_texture_destroy(cursor->texture); - } - if (cursor->renderer != NULL) { - wlr_renderer_destroy(cursor->renderer); - } - wl_list_remove(&cursor->link); - free(cursor); -} From a6930cd8ea2424f12aafc8d0b67426d0e5161c44 Mon Sep 17 00:00:00 2001 From: Drew DeVault Date: Wed, 25 Oct 2017 22:37:02 -0400 Subject: [PATCH 03/14] Handle output enter/leave correctly --- include/rootston/view.h | 5 ++- include/wlr/types/wlr_surface.h | 3 ++ rootston/desktop.c | 59 ++++++++++++++++++--------------- rootston/xdg_shell_v6.c | 2 +- types/wlr_output.c | 1 - types/wlr_surface.c | 13 +++++++- 6 files changed, 51 insertions(+), 32 deletions(-) diff --git a/include/rootston/view.h b/include/rootston/view.h index 4a5e8d08..79d61ba6 100644 --- a/include/rootston/view.h +++ b/include/rootston/view.h @@ -48,7 +48,6 @@ enum roots_view_type { struct roots_view { struct roots_desktop *desktop; - struct roots_output *output; double x, y; float rotation; // TODO: Something for roots-enforced width/height @@ -72,14 +71,14 @@ struct roots_view { // configure event from the xdg_shell // If not then this should follow the typical type/impl pattern we use // elsewhere - void (*get_size)(struct roots_view *view, struct wlr_box *box); + void (*get_size)(const struct roots_view *view, struct wlr_box *box); void (*activate)(struct roots_view *view, bool active); void (*resize)(struct roots_view *view, uint32_t width, uint32_t height); void (*set_position)(struct roots_view *view, double x, double y); void (*close)(struct roots_view *view); }; -void view_get_size(struct roots_view *view, struct wlr_box *box); +void view_get_size(const struct roots_view *view, struct wlr_box *box); void view_activate(struct roots_view *view, bool active); void view_resize(struct roots_view *view, uint32_t width, uint32_t height); void view_set_position(struct roots_view *view, double x, double y); diff --git a/include/wlr/types/wlr_surface.h b/include/wlr/types/wlr_surface.h index e1a07566..cea53109 100644 --- a/include/wlr/types/wlr_surface.h +++ b/include/wlr/types/wlr_surface.h @@ -139,4 +139,7 @@ struct wlr_subsurface *wlr_surface_subsurface_at(struct wlr_surface *surface, void wlr_surface_send_enter(struct wlr_surface *surface, struct wlr_output *output); +void wlr_surface_send_leave(struct wlr_surface *surface, + struct wlr_output *output); + #endif diff --git a/rootston/desktop.c b/rootston/desktop.c index 37022ca4..0cc2180d 100644 --- a/rootston/desktop.c +++ b/rootston/desktop.c @@ -35,45 +35,52 @@ void view_destroy(struct roots_view *view) { free(view); } -void view_get_size(struct roots_view *view, struct wlr_box *box) { +void view_get_size(const struct roots_view *view, struct wlr_box *box) { if (view->get_size) { view->get_size(view, box); - return; + } else { + box->width = view->wlr_surface->current->width; + box->height = view->wlr_surface->current->height; } - box->x = box->y = 0; - box->width = view->wlr_surface->current->width; - box->height = view->wlr_surface->current->height; + box->x = view->x; + box->y = view->y; } -static void view_update_output(struct roots_view *view) { +static void view_update_output(const struct roots_view *view, + const struct wlr_box *before) { struct roots_desktop *desktop = view->desktop; - struct roots_output *output = NULL, *_output; + struct roots_output *output; struct wlr_box box; view_get_size(view, &box); - wl_list_for_each(_output, &desktop->outputs, link) { - if (!wlr_output_layout_intersects(desktop->layout, _output->wlr_output, - view->x, view->y, view->x + box.width, view->x + box.height)) { - continue; + wl_list_for_each(output, &desktop->outputs, link) { + bool intersected = before->x != -1 && wlr_output_layout_intersects( + desktop->layout, output->wlr_output, + before->x, before->y, before->x + before->width, + before->y + before->height); + bool intersects = wlr_output_layout_intersects( + desktop->layout, output->wlr_output, + view->x, view->y, view->x + box.width, view->y + box.height); + if (intersected && !intersects) { + wlr_log(L_DEBUG, "Leaving output %s", output->wlr_output->name); + wlr_surface_send_leave(view->wlr_surface, output->wlr_output); } - if (output == NULL - || output->wlr_output->scale < _output->wlr_output->scale) { - output = _output; + if (!intersected && intersects) { + wlr_log(L_DEBUG, "Entering output %s", output->wlr_output->name); + wlr_surface_send_enter(view->wlr_surface, output->wlr_output); } } - if (output && output != view->output) { - view->output = output; - wlr_surface_send_enter(view->wlr_surface, output->wlr_output); - } } void view_set_position(struct roots_view *view, double x, double y) { + struct wlr_box before; + view_get_size(view, &before); if (view->set_position) { view->set_position(view, x, y); } else { view->x = x; view->y = y; } - view_update_output(view); + view_update_output(view, &before); } void view_activate(struct roots_view *view, bool activate) { @@ -83,10 +90,12 @@ void view_activate(struct roots_view *view, bool activate) { } void view_resize(struct roots_view *view, uint32_t width, uint32_t height) { + struct wlr_box before; + view_get_size(view, &before); if (view->resize) { view->resize(view, width, height); } - view_update_output(view); + view_update_output(view, &before); } void view_close(struct roots_view *view) { @@ -96,8 +105,8 @@ void view_close(struct roots_view *view) { } bool view_center(struct roots_view *view) { - struct wlr_box size; - view_get_size(view, &size); + struct wlr_box box; + view_get_size(view, &box); struct roots_desktop *desktop = view->desktop; struct wlr_cursor *cursor = desktop->server->input->cursor; @@ -122,16 +131,14 @@ bool view_center(struct roots_view *view) { double view_x = (double)(width - size.width) / 2 + l_output->x; double view_y = (double)(height - size.height) / 2 + l_output->y; - view_set_position(view, view_x, view_y); return true; } -void view_setup(struct roots_view *view) { - view_center(view); - +void view_initialize(struct roots_view *view) { struct roots_input *input = view->desktop->server->input; + view_center(view); set_view_focus(input, view->desktop, view); wlr_seat_keyboard_notify_enter(input->wl_seat, view->wlr_surface); view_update_output(view); diff --git a/rootston/xdg_shell_v6.c b/rootston/xdg_shell_v6.c index 704ccb1e..2d83019f 100644 --- a/rootston/xdg_shell_v6.c +++ b/rootston/xdg_shell_v6.c @@ -10,7 +10,7 @@ #include "rootston/server.h" #include "rootston/input.h" -static void get_size(struct roots_view *view, struct wlr_box *box) { +static void get_size(const struct roots_view *view, struct wlr_box *box) { assert(view->type == ROOTS_XDG_SHELL_V6_VIEW); struct wlr_xdg_surface_v6 *surf = view->xdg_surface_v6; // TODO: surf->geometry can be NULL diff --git a/types/wlr_output.c b/types/wlr_output.c index dd7e8d6a..d6efd9bf 100644 --- a/types/wlr_output.c +++ b/types/wlr_output.c @@ -43,7 +43,6 @@ static void wl_output_send_to_resource(struct wl_resource *resource) { } } if (version >= WL_OUTPUT_SCALE_SINCE_VERSION) { - wlr_log(L_DEBUG, "Sending scale"); wl_output_send_scale(resource, output->scale); } if (version >= WL_OUTPUT_DONE_SINCE_VERSION) { diff --git a/types/wlr_surface.c b/types/wlr_surface.c index 28f3d05b..a21be8de 100644 --- a/types/wlr_surface.c +++ b/types/wlr_surface.c @@ -901,9 +901,20 @@ void wlr_surface_send_enter(struct wlr_surface *surface, struct wl_resource *resource; wl_resource_for_each(resource, &output->wl_resources) { if (client == wl_resource_get_client(resource)) { - wlr_log(L_DEBUG, "sending output enter"); wl_surface_send_enter(surface->resource, resource); break; } } } + +void wlr_surface_send_leave(struct wlr_surface *surface, + struct wlr_output *output) { + struct wl_client *client = wl_resource_get_client(surface->resource); + struct wl_resource *resource; + wl_resource_for_each(resource, &output->wl_resources) { + if (client == wl_resource_get_client(resource)) { + wl_surface_send_leave(surface->resource, resource); + break; + } + } +} From bafb97087121dbeab83ffc7d2e571d4c53d1f000 Mon Sep 17 00:00:00 2001 From: Drew DeVault Date: Wed, 25 Oct 2017 22:46:20 -0400 Subject: [PATCH 04/14] View view_at (and pointer events) for hidpi --- rootston/desktop.c | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/rootston/desktop.c b/rootston/desktop.c index 0cc2180d..f7f12eff 100644 --- a/rootston/desktop.c +++ b/rootston/desktop.c @@ -61,11 +61,9 @@ static void view_update_output(const struct roots_view *view, desktop->layout, output->wlr_output, view->x, view->y, view->x + box.width, view->y + box.height); if (intersected && !intersects) { - wlr_log(L_DEBUG, "Leaving output %s", output->wlr_output->name); wlr_surface_send_leave(view->wlr_surface, output->wlr_output); } if (!intersected && intersects) { - wlr_log(L_DEBUG, "Entering output %s", output->wlr_output->name); wlr_surface_send_enter(view->wlr_surface, output->wlr_output); } } @@ -167,14 +165,15 @@ struct roots_view *view_at(struct roots_desktop *desktop, double lx, double ly, continue; } - double view_sx = lx - view->x; - double view_sy = ly - view->y; + int scale = view->wlr_surface->current->scale; + double view_sx = (lx - view->x) / (double)scale; + double view_sy = (ly - view->y) / (double)scale; struct wlr_box box = { .x = 0, .y = 0, - .width = view->wlr_surface->current->buffer_width, - .height = view->wlr_surface->current->buffer_height, + .width = view->wlr_surface->current->buffer_width * scale, + .height = view->wlr_surface->current->buffer_height * scale, }; if (view->rotation != 0.0) { // Coordinates relative to the center of the view From c8f97a3a2c43948147e4225e09437884a848fa9d Mon Sep 17 00:00:00 2001 From: Drew DeVault Date: Wed, 25 Oct 2017 23:03:30 -0400 Subject: [PATCH 05/14] Use surface matrix for software cursors A similar change should probably be applied to hardware cursors, though more complicated. Also, this doesn't actually fix the issue where the cursor is too small when over a scale=2 surface. Apparently they don't set their cursor scales to 2. Seems like a client bug? idk --- types/wlr_output.c | 13 +++++++++++-- types/wlr_surface.c | 2 +- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/types/wlr_output.c b/types/wlr_output.c index d6efd9bf..4bc9d6aa 100644 --- a/types/wlr_output.c +++ b/types/wlr_output.c @@ -429,8 +429,17 @@ void wlr_output_swap_buffers(struct wlr_output *output) { // with a NULL buffer to hide it if (renderer && texture && texture->valid) { float matrix[16]; - wlr_texture_get_matrix(texture, &matrix, &output->transform_matrix, - output->cursor.x, output->cursor.y); + if (output->cursor.surface) { + float translation[16]; + wlr_matrix_translate(&translation, + output->cursor.x, output->cursor.y, 0); + wlr_surface_get_matrix(output->cursor.surface, + &matrix, &output->transform_matrix, &translation); + } else { + wlr_texture_get_matrix(texture, + &matrix, &output->transform_matrix, + output->cursor.x, output->cursor.y); + } wlr_render_with_matrix(renderer, texture, &matrix); } } diff --git a/types/wlr_surface.c b/types/wlr_surface.c index a21be8de..a6f91d47 100644 --- a/types/wlr_surface.c +++ b/types/wlr_surface.c @@ -656,7 +656,7 @@ void wlr_surface_get_matrix(struct wlr_surface *surface, if (transform) { wlr_matrix_mul(matrix, transform, matrix); } - wlr_matrix_scale(&scale, width, height, surface->current->scale); + wlr_matrix_scale(&scale, width, height, 1); wlr_matrix_mul(matrix, &scale, matrix); wlr_matrix_mul(projection, matrix, matrix); } From 8c0929cfb3e97a71a00210fc299fb7c65703e053 Mon Sep 17 00:00:00 2001 From: Drew DeVault Date: Wed, 25 Oct 2017 23:27:12 -0400 Subject: [PATCH 06/14] Fix backwards bit banging --- types/wlr_output.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/types/wlr_output.c b/types/wlr_output.c index 4bc9d6aa..7a56eab6 100644 --- a/types/wlr_output.c +++ b/types/wlr_output.c @@ -28,7 +28,7 @@ static void wl_output_send_to_resource(struct wl_resource *resource) { if (version >= WL_OUTPUT_MODE_SINCE_VERSION) { struct wlr_output_mode *mode; wl_list_for_each(mode, &output->modes, link) { - uint32_t flags = mode->flags & ~WL_OUTPUT_MODE_PREFERRED; + uint32_t flags = mode->flags & WL_OUTPUT_MODE_PREFERRED; if (output->current_mode == mode) { flags |= WL_OUTPUT_MODE_CURRENT; } From 7f76f463181872f39356e5a4d6e2e34fd9a0a1bb Mon Sep 17 00:00:00 2001 From: Drew DeVault Date: Wed, 25 Oct 2017 23:48:54 -0400 Subject: [PATCH 07/14] Adjust rendering to compensate for disparate scale Something about my math is off, but I'm not certain what. Would appreciate a second opinion. --- rootston/output.c | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/rootston/output.c b/rootston/output.c index faf39e74..37af1e2d 100644 --- a/rootston/output.c +++ b/rootston/output.c @@ -20,29 +20,35 @@ static void render_surface(struct wlr_surface *surface, struct roots_desktop *desktop, struct wlr_output *wlr_output, struct timespec *when, double lx, double ly, float rotation) { if (surface->texture->valid) { - int width = surface->current->buffer_width; - int height = surface->current->buffer_height; + float scale_factor = (float)wlr_output->scale / surface->current->scale; + int width = surface->current->buffer_width * scale_factor; + int height = surface->current->buffer_height * scale_factor; double ox = lx, oy = ly; wlr_output_layout_output_coords(desktop->layout, wlr_output, &ox, &oy); if (wlr_output_layout_intersects(desktop->layout, wlr_output, lx, ly, lx + width, ly + height)) { - // TODO: accomodate for mismatched scale, which can happen, for - // example, when a view is rendered over two outputs float matrix[16]; float translate_origin[16]; wlr_matrix_translate(&translate_origin, (int)ox + width / 2, (int)oy + height / 2, 0); + float rotate[16]; wlr_matrix_rotate(&rotate, rotation); + float translate_center[16]; wlr_matrix_translate(&translate_center, -width / 2, -height / 2, 0); + + float scale[16]; + wlr_matrix_scale(&scale, width, height, 1); + float transform[16]; wlr_matrix_mul(&translate_origin, &rotate, &transform); wlr_matrix_mul(&transform, &translate_center, &transform); - wlr_surface_get_matrix(surface, &matrix, - &wlr_output->transform_matrix, &transform); + wlr_matrix_mul(&transform, &scale, &transform); + wlr_matrix_mul(&wlr_output->transform_matrix, &transform, &matrix); + wlr_render_with_matrix(desktop->server->renderer, surface->texture, &matrix); From ed74f473d60bb577aca9c7309664ae07d3ccf09b Mon Sep 17 00:00:00 2001 From: Drew DeVault Date: Wed, 1 Nov 2017 08:57:30 -0400 Subject: [PATCH 08/14] Fix various rebase-related bugs --- rootston/desktop.c | 10 +- types/wlr_output.c | 454 ++++++++++++++++++++++++--------------------- 2 files changed, 252 insertions(+), 212 deletions(-) diff --git a/rootston/desktop.c b/rootston/desktop.c index f7f12eff..a724a40c 100644 --- a/rootston/desktop.c +++ b/rootston/desktop.c @@ -127,19 +127,21 @@ bool view_center(struct roots_view *view) { int width, height; wlr_output_effective_resolution(output, &width, &height); - double view_x = (double)(width - size.width) / 2 + l_output->x; - double view_y = (double)(height - size.height) / 2 + l_output->y; + double view_x = (double)(width - box.width) / 2 + l_output->x; + double view_y = (double)(height - box.height) / 2 + l_output->y; view_set_position(view, view_x, view_y); return true; } -void view_initialize(struct roots_view *view) { +void view_setup(struct roots_view *view) { struct roots_input *input = view->desktop->server->input; view_center(view); set_view_focus(input, view->desktop, view); wlr_seat_keyboard_notify_enter(input->wl_seat, view->wlr_surface); - view_update_output(view); + struct wlr_box before; + view_get_size(view, &before); + view_update_output(view, &before); } void view_teardown(struct roots_view *view) { diff --git a/types/wlr_output.c b/types/wlr_output.c index 7a56eab6..44d24ae3 100644 --- a/types/wlr_output.c +++ b/types/wlr_output.c @@ -5,6 +5,7 @@ #include #include #include +#include #include #include #include @@ -28,7 +29,8 @@ static void wl_output_send_to_resource(struct wl_resource *resource) { if (version >= WL_OUTPUT_MODE_SINCE_VERSION) { struct wlr_output_mode *mode; wl_list_for_each(mode, &output->modes, link) { - uint32_t flags = mode->flags & WL_OUTPUT_MODE_PREFERRED; + // TODO: mode->flags should just be preferred + uint32_t flags = mode->flags; if (output->current_mode == mode) { flags |= WL_OUTPUT_MODE_CURRENT; } @@ -134,7 +136,9 @@ static void wlr_output_update_matrix(struct wlr_output *output) { } void wlr_output_enable(struct wlr_output *output, bool enable) { - output->impl->enable(output, enable); + if (output->impl->enable) { + output->impl->enable(output, enable); + } } bool wlr_output_set_mode(struct wlr_output *output, @@ -187,192 +191,19 @@ void wlr_output_set_position(struct wlr_output *output, int32_t lx, } } -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, true)) { - output->cursor.is_sw = false; - return true; - } - - wlr_log(L_INFO, "Falling back to software cursor"); - - output->cursor.is_sw = true; - output->cursor.width = width; - output->cursor.height = height; - - if (!output->cursor.renderer) { - output->cursor.renderer = wlr_gles2_renderer_create(output->backend); - if (!output->cursor.renderer) { - return false; - } - } - - if (!output->cursor.texture) { - 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); -} - -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) { - if (output->cursor.is_sw) { - return; - } - - 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, "wl_pointer-cursor") != 0) { - return; - } - - output->cursor.hotspot_x = hotspot_x; - output->cursor.hotspot_y = hotspot_y; - - if (surface && surface == output->cursor.surface) { - if (output->impl->set_cursor && !output->cursor.is_sw) { - // Only update the hotspot - output->impl->set_cursor(output, NULL, 0, 0, 0, hotspot_x, - hotspot_y, false); - } - 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; - } - - // Disable hardware cursor - // TODO: support hardware cursors - output->cursor.is_sw = true; - if (output->impl->set_cursor) { - output->impl->set_cursor(output, NULL, 0, 0, 0, hotspot_x, hotspot_y, - true); - } - - //output->cursor.is_sw = output->impl->set_cursor == 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); - } -} - -bool wlr_output_move_cursor(struct wlr_output *output, int x, int y) { - output->cursor.x = x; - output->cursor.y = y; - - if (output->cursor.is_sw) { - return true; - } - - if (!output->impl->move_cursor) { - return false; - } - - return output->impl->move_cursor(output, x, y); -} - void wlr_output_init(struct wlr_output *output, struct wlr_backend *backend, const struct wlr_output_impl *impl) { + assert(impl->make_current && impl->swap_buffers && impl->transform); output->backend = backend; output->impl = impl; wl_list_init(&output->modes); output->transform = WL_OUTPUT_TRANSFORM_NORMAL; output->scale = 1; + wl_list_init(&output->cursors); 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) { @@ -382,9 +213,6 @@ void wlr_output_destroy(struct wlr_output *output) { wl_signal_emit(&output->events.destroy, output); - wlr_texture_destroy(output->cursor.texture); - wlr_renderer_destroy(output->cursor.renderer); - struct wlr_output_mode *mode, *tmp_mode; wl_list_for_each_safe(mode, tmp_mode, &output->modes, link) { free(mode); @@ -399,6 +227,7 @@ void wlr_output_destroy(struct wlr_output *output) { void wlr_output_effective_resolution(struct wlr_output *output, int *width, int *height) { + // TODO: Scale factor if (output->transform % 2 == 1) { *width = output->height; *height = output->width; @@ -412,36 +241,62 @@ void wlr_output_make_current(struct wlr_output *output) { output->impl->make_current(output); } +static void output_cursor_get_box(struct wlr_output_cursor *cursor, + struct wlr_box *box) { + box->x = cursor->x - cursor->hotspot_x; + box->y = cursor->y - cursor->hotspot_y; + box->width = cursor->width; + box->height = cursor->height; +} + +static void output_cursor_render(struct wlr_output_cursor *cursor) { + struct wlr_texture *texture = cursor->texture; + struct wlr_renderer *renderer = cursor->renderer; + if (cursor->surface != NULL) { + // Some clients commit a cursor surface with a NULL buffer to hide it. + if (!wlr_surface_has_buffer(cursor->surface)) { + return; + } + texture = cursor->surface->texture; + renderer = cursor->surface->renderer; + } + + if (texture == NULL || renderer == NULL) { + return; + } + + struct wlr_box output_box; + output_box.x = output_box.y = 0; + output_box.width = cursor->output->width; + output_box.height = cursor->output->height; + + struct wlr_box cursor_box; + output_cursor_get_box(cursor, &cursor_box); + + struct wlr_box intersection; + struct wlr_box *intersection_ptr = &intersection; + if (!wlr_box_intersection(&output_box, &cursor_box, &intersection_ptr)) { + return; + } + + glViewport(0, 0, cursor->output->width, cursor->output->height); + glEnable(GL_BLEND); + glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); + + float matrix[16]; + wlr_texture_get_matrix(texture, &matrix, + &cursor->output->transform_matrix, cursor->x - cursor->hotspot_x, + cursor->y - cursor->hotspot_y); + wlr_render_with_matrix(renderer, texture, &matrix); +} + void wlr_output_swap_buffers(struct wlr_output *output) { - if (output->cursor.is_sw) { - glViewport(0, 0, output->width, output->height); - glEnable(GL_BLEND); - glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); - - struct wlr_texture *texture = output->cursor.texture; - struct wlr_renderer *renderer = output->cursor.renderer; - if (output->cursor.surface) { - texture = output->cursor.surface->texture; - renderer = output->cursor.surface->renderer; - } - - // We check texture->valid because some clients set a cursor surface - // with a NULL buffer to hide it - if (renderer && texture && texture->valid) { - float matrix[16]; - if (output->cursor.surface) { - float translation[16]; - wlr_matrix_translate(&translation, - output->cursor.x, output->cursor.y, 0); - wlr_surface_get_matrix(output->cursor.surface, - &matrix, &output->transform_matrix, &translation); - } else { - wlr_texture_get_matrix(texture, - &matrix, &output->transform_matrix, - output->cursor.x, output->cursor.y); - } - wlr_render_with_matrix(renderer, texture, &matrix); + struct wlr_output_cursor *cursor; + wl_list_for_each(cursor, &output->cursors, link) { + if (output->hardware_cursor == cursor) { + continue; } + output_cursor_render(cursor); } wl_signal_emit(&output->events.swap_buffers, &output); @@ -462,3 +317,186 @@ uint32_t wlr_output_get_gamma_size(struct wlr_output *output) { } return output->impl->get_gamma_size(output); } + +static void output_cursor_reset(struct wlr_output_cursor *cursor) { + if (cursor->surface != NULL) { + wl_list_remove(&cursor->surface_commit.link); + wl_list_remove(&cursor->surface_destroy.link); + cursor->surface = NULL; + } +} + +bool wlr_output_cursor_set_image(struct wlr_output_cursor *cursor, + const uint8_t *pixels, int32_t stride, uint32_t width, uint32_t height, + int32_t hotspot_x, int32_t hotspot_y) { + output_cursor_reset(cursor); + + cursor->width = width; + cursor->height = height; + cursor->hotspot_x = hotspot_x; + cursor->hotspot_y = hotspot_y; + + if (cursor->output->hardware_cursor == NULL && + cursor->output->impl->set_cursor) { + int ok = cursor->output->impl->set_cursor(cursor->output, pixels, + stride, width, height, hotspot_x, hotspot_y, true); + if (ok) { + cursor->output->hardware_cursor = cursor; + return true; + } + } + + wlr_log(L_INFO, "Falling back to software cursor"); + + if (cursor->renderer == NULL) { + cursor->renderer = wlr_gles2_renderer_create(cursor->output->backend); + if (cursor->renderer == NULL) { + return false; + } + } + + if (cursor->texture == NULL) { + cursor->texture = wlr_render_texture_create(cursor->renderer); + if (cursor->texture == NULL) { + return false; + } + } + + return wlr_texture_upload_pixels(cursor->texture, WL_SHM_FORMAT_ARGB8888, + 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) { + return (int64_t)a->tv_sec * 1000 + a->tv_nsec / 1000000; +} + +static void output_cursor_handle_commit(struct wl_listener *listener, + void *data) { + struct wlr_output_cursor *cursor = wl_container_of(listener, cursor, + surface_commit); + struct wlr_surface *surface = data; + + output_cursor_commit(cursor); + + 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 output_cursor_handle_destroy(struct wl_listener *listener, + void *data) { + struct wlr_output_cursor *cursor = wl_container_of(listener, cursor, + surface_destroy); + output_cursor_reset(cursor); +} + +void wlr_output_cursor_set_surface(struct wlr_output_cursor *cursor, + struct wlr_surface *surface, int32_t hotspot_x, int32_t hotspot_y) { + if (surface && strcmp(surface->role, "wl_pointer-cursor") != 0) { + return; + } + + if (surface) { + 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 (cursor->output->hardware_cursor == cursor && + cursor->output->impl->set_cursor) { + // If the surface hasn't changed and it's an hardware cursor, only + // update the hotspot + cursor->output->impl->set_cursor(cursor->output, NULL, 0, 0, 0, + hotspot_x, hotspot_y, false); + } + return; + } + + output_cursor_reset(cursor); + + // Disable hardware cursor for surfaces + // TODO: support hardware cursors + if (cursor->output->hardware_cursor == cursor && + cursor->output->impl->set_cursor) { + cursor->output->impl->set_cursor(cursor->output, NULL, 0, 0, 0, 0, 0, + true); + cursor->output->hardware_cursor = NULL; + } + + cursor->surface = surface; + + if (surface != NULL) { + wl_signal_add(&surface->events.commit, &cursor->surface_commit); + wl_signal_add(&surface->events.destroy, &cursor->surface_destroy); + output_cursor_commit(cursor); + } else { + // TODO: if hardware cursor, disable cursor + } +} + +bool wlr_output_cursor_move(struct wlr_output_cursor *cursor, int x, int y) { + cursor->x = x; + cursor->y = y; + + if (cursor->output->hardware_cursor != cursor) { + return true; + } + + if (!cursor->output->impl->move_cursor) { + return false; + } + return cursor->output->impl->move_cursor(cursor->output, x, y); +} + +struct wlr_output_cursor *wlr_output_cursor_create(struct wlr_output *output) { + struct wlr_output_cursor *cursor = + calloc(1, sizeof(struct wlr_output_cursor)); + if (cursor == NULL) { + return NULL; + } + cursor->output = output; + wl_list_init(&cursor->surface_commit.link); + cursor->surface_commit.notify = output_cursor_handle_commit; + wl_list_init(&cursor->surface_destroy.link); + cursor->surface_destroy.notify = output_cursor_handle_destroy; + wl_list_insert(&output->cursors, &cursor->link); + return cursor; +} + +void wlr_output_cursor_destroy(struct wlr_output_cursor *cursor) { + if (cursor == NULL) { + return; + } + output_cursor_reset(cursor); + if (cursor->output->hardware_cursor == cursor) { + // If this cursor was the hardware cursor, disable it + if (cursor->output->impl->set_cursor) { + cursor->output->impl->set_cursor(cursor->output, NULL, 0, 0, 0, 0, + 0, true); + } + cursor->output->hardware_cursor = NULL; + } + if (cursor->texture != NULL) { + wlr_texture_destroy(cursor->texture); + } + if (cursor->renderer != NULL) { + wlr_renderer_destroy(cursor->renderer); + } + wl_list_remove(&cursor->link); + free(cursor); +} From ca8cf7d48dc8de94494e23292c1687c1dad766f2 Mon Sep 17 00:00:00 2001 From: Drew DeVault Date: Thu, 2 Nov 2017 23:17:39 -0400 Subject: [PATCH 09/14] Rethink HiDPI output layouts, fixes everything Except for subsurfaces not rendering at the right scale. But that part is (somewhat) easy. --- rootston/desktop.c | 9 ++++----- rootston/output.c | 2 ++ types/wlr_output.c | 5 ++++- 3 files changed, 10 insertions(+), 6 deletions(-) diff --git a/rootston/desktop.c b/rootston/desktop.c index a724a40c..f87be11e 100644 --- a/rootston/desktop.c +++ b/rootston/desktop.c @@ -167,15 +167,14 @@ struct roots_view *view_at(struct roots_desktop *desktop, double lx, double ly, continue; } - int scale = view->wlr_surface->current->scale; - double view_sx = (lx - view->x) / (double)scale; - double view_sy = (ly - view->y) / (double)scale; + double view_sx = lx - view->x; + double view_sy = ly - view->y; struct wlr_box box = { .x = 0, .y = 0, - .width = view->wlr_surface->current->buffer_width * scale, - .height = view->wlr_surface->current->buffer_height * scale, + .width = view->wlr_surface->current->buffer_width, + .height = view->wlr_surface->current->buffer_height, }; if (view->rotation != 0.0) { // Coordinates relative to the center of the view diff --git a/rootston/output.c b/rootston/output.c index 37af1e2d..c21c6781 100644 --- a/rootston/output.c +++ b/rootston/output.c @@ -25,6 +25,8 @@ static void render_surface(struct wlr_surface *surface, int height = surface->current->buffer_height * scale_factor; double ox = lx, oy = ly; wlr_output_layout_output_coords(desktop->layout, wlr_output, &ox, &oy); + ox *= wlr_output->scale; + oy *= wlr_output->scale; if (wlr_output_layout_intersects(desktop->layout, wlr_output, lx, ly, lx + width, ly + height)) { diff --git a/types/wlr_output.c b/types/wlr_output.c index 44d24ae3..82e805b4 100644 --- a/types/wlr_output.c +++ b/types/wlr_output.c @@ -227,7 +227,6 @@ void wlr_output_destroy(struct wlr_output *output) { void wlr_output_effective_resolution(struct wlr_output *output, int *width, int *height) { - // TODO: Scale factor if (output->transform % 2 == 1) { *width = output->height; *height = output->width; @@ -235,6 +234,8 @@ void wlr_output_effective_resolution(struct wlr_output *output, *width = output->width; *height = output->height; } + *width /= output->scale; + *height /= output->scale; } void wlr_output_make_current(struct wlr_output *output) { @@ -450,6 +451,8 @@ void wlr_output_cursor_set_surface(struct wlr_output_cursor *cursor, } bool wlr_output_cursor_move(struct wlr_output_cursor *cursor, int x, int y) { + x *= cursor->output->scale; + y *= cursor->output->scale; cursor->x = x; cursor->y = y; From 975b9dc365d5a7bec531522320a1506323575525 Mon Sep 17 00:00:00 2001 From: Drew DeVault Date: Thu, 2 Nov 2017 23:33:06 -0400 Subject: [PATCH 10/14] Fix view centering on HiDPI outputs --- rootston/desktop.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/rootston/desktop.c b/rootston/desktop.c index f87be11e..fe95426f 100644 --- a/rootston/desktop.c +++ b/rootston/desktop.c @@ -126,6 +126,8 @@ bool view_center(struct roots_view *view) { int width, height; wlr_output_effective_resolution(output, &width, &height); + width /= output->scale; + height /= output->scale; double view_x = (double)(width - box.width) / 2 + l_output->x; double view_y = (double)(height - box.height) / 2 + l_output->y; From 6d8e1abfc0a266e8ff1a8c9ba1a004faeaac79d5 Mon Sep 17 00:00:00 2001 From: Drew DeVault Date: Sat, 4 Nov 2017 01:35:12 -0400 Subject: [PATCH 11/14] Improve input sensitivity We now use doubles until the last minute, which makes it so we can move the pointer more precisely. This also includes a fix for tablet tools, which move absolutely and sometimes do not update the X or Y axis. --- backend/libinput/tablet_tool.c | 2 ++ include/wlr/types/wlr_output.h | 5 +++-- rootston/cursor.c | 9 +++++++++ types/wlr_cursor.c | 4 ++-- types/wlr_output.c | 6 ++++-- 5 files changed, 20 insertions(+), 6 deletions(-) diff --git a/backend/libinput/tablet_tool.c b/backend/libinput/tablet_tool.c index 3caaf3f7..3d5fafc3 100644 --- a/backend/libinput/tablet_tool.c +++ b/backend/libinput/tablet_tool.c @@ -70,6 +70,8 @@ void handle_tablet_tool_axis(struct libinput_event *event, wlr_event.updated_axes |= WLR_TABLET_TOOL_AXIS_WHEEL; wlr_event.wheel_delta = libinput_event_tablet_tool_get_wheel_delta(tevent); } + wlr_log(L_DEBUG, "Tablet tool axis event %d @ %f,%f", + wlr_event.updated_axes, wlr_event.x_mm, wlr_event.y_mm); wl_signal_emit(&wlr_dev->tablet_tool->events.axis, &wlr_event); } diff --git a/include/wlr/types/wlr_output.h b/include/wlr/types/wlr_output.h index e6323f9c..df123639 100644 --- a/include/wlr/types/wlr_output.h +++ b/include/wlr/types/wlr_output.h @@ -14,7 +14,7 @@ struct wlr_output_mode { struct wlr_output_cursor { struct wlr_output *output; - int32_t x, y; + double x, y; bool enabled; uint32_t width, height; int32_t hotspot_x, hotspot_y; @@ -95,7 +95,8 @@ bool wlr_output_cursor_set_image(struct wlr_output_cursor *cursor, int32_t hotspot_x, int32_t hotspot_y); void wlr_output_cursor_set_surface(struct wlr_output_cursor *cursor, struct wlr_surface *surface, int32_t hotspot_x, int32_t hotspot_y); -bool wlr_output_cursor_move(struct wlr_output_cursor *cursor, int x, int y); +bool wlr_output_cursor_move(struct wlr_output_cursor *cursor, + double x, double y); void wlr_output_cursor_destroy(struct wlr_output_cursor *cursor); #endif diff --git a/rootston/cursor.c b/rootston/cursor.c index 31001a9f..aa8e5122 100644 --- a/rootston/cursor.c +++ b/rootston/cursor.c @@ -378,11 +378,20 @@ static void handle_touch_motion(struct wl_listener *listener, void *data) { static void handle_tool_axis(struct wl_listener *listener, void *data) { struct roots_input *input = wl_container_of(listener, input, cursor_tool_axis); struct wlr_event_tablet_tool_axis *event = data; + if ((event->updated_axes & WLR_TABLET_TOOL_AXIS_X) && (event->updated_axes & WLR_TABLET_TOOL_AXIS_Y)) { wlr_cursor_warp_absolute(input->cursor, event->device, event->x_mm / event->width_mm, event->y_mm / event->height_mm); cursor_update_position(input, event->time_msec); + } else if ((event->updated_axes & WLR_TABLET_TOOL_AXIS_X)) { + wlr_cursor_warp_absolute(input->cursor, event->device, + event->x_mm / event->width_mm, -1); + cursor_update_position(input, event->time_msec); + } else if ((event->updated_axes & WLR_TABLET_TOOL_AXIS_Y)) { + wlr_cursor_warp_absolute(input->cursor, event->device, + -1, event->y_mm / event->height_mm); + cursor_update_position(input, event->time_msec); } } diff --git a/types/wlr_cursor.c b/types/wlr_cursor.c index 83d36d14..dfaccb53 100644 --- a/types/wlr_cursor.c +++ b/types/wlr_cursor.c @@ -261,8 +261,8 @@ void wlr_cursor_warp_absolute(struct wlr_cursor *cur, mapping = wlr_output_layout_get_box(cur->state->layout, NULL); } - double x = mapping->width * x_mm + mapping->x; - double y = mapping->height * y_mm + mapping->y; + double x = x_mm > 0 ? mapping->width * x_mm + mapping->x : cur->x; + double y = y_mm > 0 ? mapping->height * y_mm + mapping->y : cur->y; wlr_cursor_warp_unchecked(cur, x, y); } diff --git a/types/wlr_output.c b/types/wlr_output.c index ff8b07be..3ad69ce3 100644 --- a/types/wlr_output.c +++ b/types/wlr_output.c @@ -472,9 +472,11 @@ void wlr_output_cursor_set_surface(struct wlr_output_cursor *cursor, } } -bool wlr_output_cursor_move(struct wlr_output_cursor *cursor, int x, int y) { +bool wlr_output_cursor_move(struct wlr_output_cursor *cursor, + double x, double y) { x *= cursor->output->scale; y *= cursor->output->scale; + wlr_log(L_DEBUG, "Moving cursor to %f,%f", x, y); cursor->x = x; cursor->y = y; @@ -486,7 +488,7 @@ bool wlr_output_cursor_move(struct wlr_output_cursor *cursor, int x, int y) { if (!cursor->output->impl->move_cursor) { return false; } - return cursor->output->impl->move_cursor(cursor->output, x, y); + return cursor->output->impl->move_cursor(cursor->output, (int)x, (int)y); } struct wlr_output_cursor *wlr_output_cursor_create(struct wlr_output *output) { From 2f6cfe4057fbef41977159e24fea0d19fbeeb052 Mon Sep 17 00:00:00 2001 From: Drew DeVault Date: Sat, 4 Nov 2017 11:47:34 -0400 Subject: [PATCH 12/14] Fix software cursors on scaled outputs There was an issue where it would only work within the boundaries of the unscaled resolution. --- backend/wayland/output.c | 2 +- types/wlr_output.c | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/backend/wayland/output.c b/backend/wayland/output.c index 90f8b39a..c4301617 100644 --- a/backend/wayland/output.c +++ b/backend/wayland/output.c @@ -249,7 +249,7 @@ struct wlr_output *wlr_wl_output_create(struct wlr_backend *_backend) { wlr_output_init(&output->wlr_output, &backend->backend, &output_impl); struct wlr_output *wlr_output = &output->wlr_output; - wlr_output_update_size(wlr_output, 640, 480); + wlr_output_update_size(wlr_output, 1280, 720); strncpy(wlr_output->make, "wayland", sizeof(wlr_output->make)); strncpy(wlr_output->model, "wayland", sizeof(wlr_output->model)); snprintf(wlr_output->name, sizeof(wlr_output->name), "WL-%d", diff --git a/types/wlr_output.c b/types/wlr_output.c index 3ad69ce3..df02afec 100644 --- a/types/wlr_output.c +++ b/types/wlr_output.c @@ -270,6 +270,8 @@ static void output_cursor_render(struct wlr_output_cursor *cursor) { output_box.x = output_box.y = 0; wlr_output_effective_resolution(cursor->output, &output_box.width, &output_box.height); + output_box.width *= cursor->output->scale; + output_box.height *= cursor->output->scale; struct wlr_box cursor_box; output_cursor_get_box(cursor, &cursor_box); @@ -476,7 +478,6 @@ bool wlr_output_cursor_move(struct wlr_output_cursor *cursor, double x, double y) { x *= cursor->output->scale; y *= cursor->output->scale; - wlr_log(L_DEBUG, "Moving cursor to %f,%f", x, y); cursor->x = x; cursor->y = y; From 75fd9b8426953d98c57fc0f598bf3a3ace602c0c Mon Sep 17 00:00:00 2001 From: Drew DeVault Date: Thu, 9 Nov 2017 08:40:15 -0500 Subject: [PATCH 13/14] Remove extraneous keyboard send_enter --- rootston/desktop.c | 1 - 1 file changed, 1 deletion(-) diff --git a/rootston/desktop.c b/rootston/desktop.c index df8a79f2..2448ffac 100644 --- a/rootston/desktop.c +++ b/rootston/desktop.c @@ -150,7 +150,6 @@ void view_setup(struct roots_view *view) { struct roots_input *input = view->desktop->server->input; view_center(view); set_view_focus(input, view->desktop, view); - wlr_seat_keyboard_notify_enter(input->wl_seat, view->wlr_surface); struct wlr_box before; view_get_size(view, &before); view_update_output(view, &before); From aafb00a15fd84b6d40f2efa52333eea5633b14e5 Mon Sep 17 00:00:00 2001 From: Drew DeVault Date: Fri, 10 Nov 2017 08:21:23 -0500 Subject: [PATCH 14/14] Fix centering views on scaled outputs --- rootston/desktop.c | 2 -- 1 file changed, 2 deletions(-) diff --git a/rootston/desktop.c b/rootston/desktop.c index 2448ffac..78bd271a 100644 --- a/rootston/desktop.c +++ b/rootston/desktop.c @@ -136,8 +136,6 @@ bool view_center(struct roots_view *view) { int width, height; wlr_output_effective_resolution(output, &width, &height); - width /= output->scale; - height /= output->scale; double view_x = (double)(width - box.width) / 2 + l_output->x; double view_y = (double)(height - box.height) / 2 + l_output->y;