From f4754ad1a21eaede20f6e9c5d4e0c78294119cc1 Mon Sep 17 00:00:00 2001 From: emersion Date: Thu, 30 Nov 2017 23:58:12 +0100 Subject: [PATCH 01/21] Fix surface transforms --- include/wlr/render/matrix.h | 2 ++ include/wlr/types/wlr_output.h | 2 ++ render/matrix.c | 17 +++++++++++++++++ rootston/output.c | 15 +++++++++++++++ 4 files changed, 36 insertions(+) diff --git a/include/wlr/render/matrix.h b/include/wlr/render/matrix.h index 177af4b3..0b35aad3 100644 --- a/include/wlr/render/matrix.h +++ b/include/wlr/render/matrix.h @@ -10,6 +10,8 @@ void wlr_matrix_rotate(float (*output)[16], float radians); void wlr_matrix_mul(const float (*x)[16], const float (*y)[16], float (*product)[16]); enum wl_output_transform; +void wlr_matrix_transform(float mat[static 16], + enum wl_output_transform transform); void wlr_matrix_texture(float mat[static 16], int32_t width, int32_t height, enum wl_output_transform transform); diff --git a/include/wlr/types/wlr_output.h b/include/wlr/types/wlr_output.h index d382b593..b1b83044 100644 --- a/include/wlr/types/wlr_output.h +++ b/include/wlr/types/wlr_output.h @@ -106,4 +106,6 @@ bool wlr_output_cursor_move(struct wlr_output_cursor *cursor, double x, double y); void wlr_output_cursor_destroy(struct wlr_output_cursor *cursor); +enum wl_output_transform wlr_output_transform_invert(enum wl_output_transform); + #endif diff --git a/render/matrix.c b/render/matrix.c index e49d365e..8a3352e0 100644 --- a/render/matrix.c +++ b/render/matrix.c @@ -118,6 +118,23 @@ static const float transforms[][4] = { }, }; +void wlr_matrix_transform(float mat[static 16], + enum wl_output_transform transform) { + memset(mat, 0, sizeof(*mat) * 16); + + const float *t = transforms[transform]; + + // Rotation + relection + mat[0] = t[0]; + mat[1] = t[1]; + mat[4] = -t[2]; + mat[5] = -t[3]; + + // Identity + mat[10] = 1.0f; + mat[15] = 1.0f; +} + // Equivilent to glOrtho(0, width, 0, height, 1, -1) with the transform applied void wlr_matrix_texture(float mat[static 16], int32_t width, int32_t height, enum wl_output_transform transform) { diff --git a/rootston/output.c b/rootston/output.c index bf684f2f..c1d2e9f3 100644 --- a/rootston/output.c +++ b/rootston/output.c @@ -63,10 +63,25 @@ static void render_surface(struct wlr_surface *surface, float scale[16]; wlr_matrix_scale(&scale, render_width, render_height, 1); + float translate_mdr[16]; + wlr_matrix_translate(&translate_mdr, 0.5, 0.5, 0); + + float surface_transform[16]; + wlr_matrix_transform(surface_transform, + wlr_output_transform_invert(surface->current->transform)); // TODO + + float translate_mdr2[16]; + wlr_matrix_translate(&translate_mdr2, -0.5, -0.5, 0); + float transform[16]; wlr_matrix_mul(&translate_origin, &rotate, &transform); wlr_matrix_mul(&transform, &translate_center, &transform); wlr_matrix_mul(&transform, &scale, &transform); + + wlr_matrix_mul(&transform, &translate_mdr, &transform); + wlr_matrix_mul(&transform, &surface_transform, &transform); + wlr_matrix_mul(&transform, &translate_mdr2, &transform); + wlr_matrix_mul(&wlr_output->transform_matrix, &transform, &matrix); wlr_render_with_matrix(desktop->server->renderer, surface->texture, From 6a69b4419fa25f4bbcbe3253ed000276cb6ace42 Mon Sep 17 00:00:00 2001 From: emersion Date: Fri, 1 Dec 2017 09:15:33 +0100 Subject: [PATCH 02/21] Make wlr_output_transform_* functions public, refactoring --- backend/drm/drm.c | 5 ++- backend/wayland/wl_seat.c | 5 ++- include/wlr/interfaces/wlr_output.h | 4 --- include/wlr/types/wlr_box.h | 4 +++ render/matrix.c | 28 ++++++++--------- types/wlr_box.c | 47 +++++++++++++++++++++++++++++ types/wlr_output.c | 46 ---------------------------- 7 files changed, 69 insertions(+), 70 deletions(-) diff --git a/backend/drm/drm.c b/backend/drm/drm.c index dd247998..cf6ad550 100644 --- a/backend/drm/drm.c +++ b/backend/drm/drm.c @@ -569,8 +569,7 @@ static bool wlr_drm_connector_set_cursor(struct wlr_output *output, enum wl_output_transform transform = wlr_output_transform_invert(output->transform); struct wlr_box transformed_hotspot; - wlr_output_transform_apply_to_box(transform, &hotspot, - &transformed_hotspot); + wlr_box_transform(&hotspot, transform, &transformed_hotspot); plane->cursor_hotspot_x = transformed_hotspot.x; plane->cursor_hotspot_y = transformed_hotspot.y; @@ -632,7 +631,7 @@ static bool wlr_drm_connector_move_cursor(struct wlr_output *output, enum wl_output_transform transform = wlr_output_transform_invert(output->transform); struct wlr_box transformed_box; - wlr_output_transform_apply_to_box(transform, &box, &transformed_box); + wlr_box_transform(&box, transform, &transformed_box); transformed_box.x -= plane->cursor_hotspot_x; transformed_box.y -= plane->cursor_hotspot_y; diff --git a/backend/wayland/wl_seat.c b/backend/wayland/wl_seat.c index a2da8df5..77c46164 100644 --- a/backend/wayland/wl_seat.c +++ b/backend/wayland/wl_seat.c @@ -55,9 +55,8 @@ static void pointer_handle_motion(void *data, struct wl_pointer *wl_pointer, box.x = wl_fixed_to_int(surface_x); box.y = wl_fixed_to_int(surface_y); struct wlr_box transformed; - wlr_output_transform_apply_to_box( - wlr_wl_pointer->current_output->wlr_output.transform, &box, - &transformed); + wlr_box_transform(&box, + wlr_wl_pointer->current_output->wlr_output.transform, &transformed); struct wlr_event_pointer_motion_absolute wlr_event; wlr_event.device = dev; diff --git a/include/wlr/interfaces/wlr_output.h b/include/wlr/interfaces/wlr_output.h index b4f39d35..8fbb650a 100644 --- a/include/wlr/interfaces/wlr_output.h +++ b/include/wlr/interfaces/wlr_output.h @@ -32,8 +32,4 @@ struct wl_global *wlr_output_create_global(struct wlr_output *wlr_output, struct wl_display *display); void wlr_output_destroy_global(struct wlr_output *wlr_output); -void wlr_output_transform_apply_to_box(enum wl_output_transform transform, - struct wlr_box *box, struct wlr_box *dest); -enum wl_output_transform wlr_output_transform_invert(enum wl_output_transform); - #endif diff --git a/include/wlr/types/wlr_box.h b/include/wlr/types/wlr_box.h index 5b8b00c9..5290ada5 100644 --- a/include/wlr/types/wlr_box.h +++ b/include/wlr/types/wlr_box.h @@ -18,4 +18,8 @@ bool wlr_box_contains_point(struct wlr_box *box, double x, double y); bool wlr_box_empty(struct wlr_box *box); +enum wl_output_transform; +void wlr_box_transform(struct wlr_box *box, enum wl_output_transform transform, + struct wlr_box *dest); + #endif diff --git a/render/matrix.c b/render/matrix.c index 8a3352e0..6118e6a5 100644 --- a/render/matrix.c +++ b/render/matrix.c @@ -86,35 +86,35 @@ void wlr_matrix_mul(const float (*x)[16], const float (*y)[16], float (*product) static const float transforms[][4] = { [WL_OUTPUT_TRANSFORM_NORMAL] = { 1.0f, 0.0f, - 0.0f, -1.0f, + 0.0f, 1.0f, }, [WL_OUTPUT_TRANSFORM_90] = { 0.0f, -1.0f, - -1.0f, 0.0f, + 1.0f, 0.0f, }, [WL_OUTPUT_TRANSFORM_180] = { -1.0f, 0.0f, - 0.0f, 1.0f, + 0.0f, -1.0f, }, [WL_OUTPUT_TRANSFORM_270] = { 0.0f, 1.0f, - 1.0f, 0.0f, + -1.0f, 0.0f, }, [WL_OUTPUT_TRANSFORM_FLIPPED] = { -1.0f, 0.0f, - 0.0f, -1.0f, + 0.0f, 1.0f, }, [WL_OUTPUT_TRANSFORM_FLIPPED_90] = { 0.0f, 1.0f, - -1.0f, 0.0f, + 1.0f, 0.0f, }, [WL_OUTPUT_TRANSFORM_FLIPPED_180] = { 1.0f, 0.0f, - 0.0f, 1.0f, + 0.0f, -1.0f, }, [WL_OUTPUT_TRANSFORM_FLIPPED_270] = { 0.0f, -1.0f, - 1.0f, 0.0f, + -1.0f, 0.0f, }, }; @@ -124,11 +124,11 @@ void wlr_matrix_transform(float mat[static 16], const float *t = transforms[transform]; - // Rotation + relection + // Rotation + reflection mat[0] = t[0]; mat[1] = t[1]; - mat[4] = -t[2]; - mat[5] = -t[3]; + mat[4] = t[2]; + mat[5] = t[3]; // Identity mat[10] = 1.0f; @@ -144,11 +144,11 @@ void wlr_matrix_texture(float mat[static 16], int32_t width, int32_t height, float x = 2.0f / width; float y = 2.0f / height; - // Rotation + relection + // Rotation + reflection mat[0] = x * t[0]; mat[1] = x * t[1]; - mat[4] = y * t[2]; - mat[5] = y * t[3]; + mat[4] = y * -t[2]; + mat[5] = y * -t[3]; // Translation mat[3] = -copysign(1.0f, mat[0] + mat[1]); diff --git a/types/wlr_box.c b/types/wlr_box.c index 7e981833..88f4b8ab 100644 --- a/types/wlr_box.c +++ b/types/wlr_box.c @@ -2,6 +2,7 @@ #include #include #include +#include #include #include @@ -65,3 +66,49 @@ bool wlr_box_contains_point(struct wlr_box *box, double x, double y) { y >= box->y && y <= box->y + box->height; } } + +void wlr_box_transform(struct wlr_box *box, + enum wl_output_transform transform, struct wlr_box *dest) { + if (transform % 2 == 0) { + dest->width = box->width; + dest->height = box->height; + } else { + dest->width = box->height; + dest->height = box->width; + } + + switch (transform) { + case WL_OUTPUT_TRANSFORM_NORMAL: + dest->x = box->x; + dest->y = box->y; + break; + case WL_OUTPUT_TRANSFORM_90: + dest->x = box->y; + dest->y = box->width - box->x; + break; + case WL_OUTPUT_TRANSFORM_180: + dest->x = box->width - box->x; + dest->y = box->height - box->y; + break; + case WL_OUTPUT_TRANSFORM_270: + dest->x = box->height - box->y; + dest->y = box->x; + break; + case WL_OUTPUT_TRANSFORM_FLIPPED: + dest->x = box->width - box->x; + dest->y = box->y; + break; + case WL_OUTPUT_TRANSFORM_FLIPPED_90: + dest->x = box->y; + dest->y = box->x; + break; + case WL_OUTPUT_TRANSFORM_FLIPPED_180: + dest->x = box->x; + dest->y = box->height - box->y; + break; + case WL_OUTPUT_TRANSFORM_FLIPPED_270: + dest->x = box->height - box->y; + dest->y = box->width - box->x; + break; + } +} diff --git a/types/wlr_output.c b/types/wlr_output.c index 21d94cfb..aad7dfec 100644 --- a/types/wlr_output.c +++ b/types/wlr_output.c @@ -626,52 +626,6 @@ void wlr_output_cursor_destroy(struct wlr_output_cursor *cursor) { free(cursor); } -void wlr_output_transform_apply_to_box(enum wl_output_transform transform, - struct wlr_box *box, struct wlr_box *dest) { - if (transform % 2 == 0) { - dest->width = box->width; - dest->height = box->height; - } else { - dest->width = box->height; - dest->height = box->width; - } - - switch (transform) { - case WL_OUTPUT_TRANSFORM_NORMAL: - dest->x = box->x; - dest->y = box->y; - break; - case WL_OUTPUT_TRANSFORM_90: - dest->x = box->y; - dest->y = box->width - box->x; - break; - case WL_OUTPUT_TRANSFORM_180: - dest->x = box->width - box->x; - dest->y = box->height - box->y; - break; - case WL_OUTPUT_TRANSFORM_270: - dest->x = box->height - box->y; - dest->y = box->x; - break; - case WL_OUTPUT_TRANSFORM_FLIPPED: - dest->x = box->width - box->x; - dest->y = box->y; - break; - case WL_OUTPUT_TRANSFORM_FLIPPED_90: - dest->x = box->y; - dest->y = box->x; - break; - case WL_OUTPUT_TRANSFORM_FLIPPED_180: - dest->x = box->x; - dest->y = box->height - box->y; - break; - case WL_OUTPUT_TRANSFORM_FLIPPED_270: - dest->x = box->height - box->y; - dest->y = box->width - box->x; - break; - } -} - enum wl_output_transform wlr_output_transform_invert( enum wl_output_transform transform) { if ((transform & WL_OUTPUT_TRANSFORM_90) && From 4a56957a37669b38038910b41eb2b128f3a5042e Mon Sep 17 00:00:00 2001 From: emersion Date: Fri, 1 Dec 2017 09:49:32 +0100 Subject: [PATCH 03/21] Fix inverted flipped-90 and flipped-270 --- render/matrix.c | 8 ++++---- types/wlr_box.c | 8 ++++---- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/render/matrix.c b/render/matrix.c index 6118e6a5..54dba4cc 100644 --- a/render/matrix.c +++ b/render/matrix.c @@ -105,16 +105,16 @@ static const float transforms[][4] = { 0.0f, 1.0f, }, [WL_OUTPUT_TRANSFORM_FLIPPED_90] = { - 0.0f, 1.0f, - 1.0f, 0.0f, + 0.0f, -1.0f, + -1.0f, 0.0f, }, [WL_OUTPUT_TRANSFORM_FLIPPED_180] = { 1.0f, 0.0f, 0.0f, -1.0f, }, [WL_OUTPUT_TRANSFORM_FLIPPED_270] = { - 0.0f, -1.0f, - -1.0f, 0.0f, + 0.0f, 1.0f, + 1.0f, 0.0f, }, }; diff --git a/types/wlr_box.c b/types/wlr_box.c index 88f4b8ab..5e910b2e 100644 --- a/types/wlr_box.c +++ b/types/wlr_box.c @@ -99,16 +99,16 @@ void wlr_box_transform(struct wlr_box *box, dest->y = box->y; break; case WL_OUTPUT_TRANSFORM_FLIPPED_90: - dest->x = box->y; - dest->y = box->x; + dest->x = box->height - box->y; + dest->y = box->width - box->x; break; case WL_OUTPUT_TRANSFORM_FLIPPED_180: dest->x = box->x; dest->y = box->height - box->y; break; case WL_OUTPUT_TRANSFORM_FLIPPED_270: - dest->x = box->height - box->y; - dest->y = box->width - box->x; + dest->x = box->y; + dest->y = box->x; break; } } From acc8f368945d540796078f0530234734e5cb1b37 Mon Sep 17 00:00:00 2001 From: emersion Date: Fri, 1 Dec 2017 16:08:01 +0100 Subject: [PATCH 04/21] Fix pointer input for transformed surfaces --- rootston/desktop.c | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/rootston/desktop.c b/rootston/desktop.c index bb3af258..8cede819 100644 --- a/rootston/desktop.c +++ b/rootston/desktop.c @@ -295,10 +295,8 @@ static bool view_at(struct roots_view *view, double lx, double ly, struct wlr_surface_state *state = view->wlr_surface->current; struct wlr_box box = { - .x = 0, - .y = 0, - .width = state->buffer_width / state->scale, - .height = state->buffer_height / state->scale, + .x = 0, .y = 0, + .width = state->width, .height = state->height, }; if (view->rotation != 0.0) { // Coordinates relative to the center of the view From d1b29a54b92c5d13739430fe98b12ff169964272 Mon Sep 17 00:00:00 2001 From: emersion Date: Fri, 1 Dec 2017 16:13:24 +0100 Subject: [PATCH 05/21] Refactor: rename matrices in rootston --- rootston/output.c | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/rootston/output.c b/rootston/output.c index c1d2e9f3..9df95a91 100644 --- a/rootston/output.c +++ b/rootston/output.c @@ -49,38 +49,38 @@ static void render_surface(struct wlr_surface *surface, lx, ly, lx + render_width, ly + render_height)) { float matrix[16]; - float translate_origin[16]; - wlr_matrix_translate(&translate_origin, + float translate_center[16]; + wlr_matrix_translate(&translate_center, (int)ox + render_width / 2, (int)oy + render_height / 2, 0); float rotate[16]; wlr_matrix_rotate(&rotate, rotation); - float translate_center[16]; - wlr_matrix_translate(&translate_center, -render_width / 2, + float translate_origin[16]; + wlr_matrix_translate(&translate_origin, -render_width / 2, -render_height / 2, 0); float scale[16]; wlr_matrix_scale(&scale, render_width, render_height, 1); - float translate_mdr[16]; - wlr_matrix_translate(&translate_mdr, 0.5, 0.5, 0); + float surface_translate_center[16]; + wlr_matrix_translate(&surface_translate_center, 0.5, 0.5, 0); float surface_transform[16]; wlr_matrix_transform(surface_transform, - wlr_output_transform_invert(surface->current->transform)); // TODO + wlr_output_transform_invert(surface->current->transform)); - float translate_mdr2[16]; - wlr_matrix_translate(&translate_mdr2, -0.5, -0.5, 0); + float surface_translate_origin[16]; + wlr_matrix_translate(&surface_translate_origin, -0.5, -0.5, 0); float transform[16]; - wlr_matrix_mul(&translate_origin, &rotate, &transform); - wlr_matrix_mul(&transform, &translate_center, &transform); + wlr_matrix_mul(&translate_center, &rotate, &transform); + wlr_matrix_mul(&transform, &translate_origin, &transform); wlr_matrix_mul(&transform, &scale, &transform); - wlr_matrix_mul(&transform, &translate_mdr, &transform); + wlr_matrix_mul(&transform, &surface_translate_center, &transform); wlr_matrix_mul(&transform, &surface_transform, &transform); - wlr_matrix_mul(&transform, &translate_mdr2, &transform); + wlr_matrix_mul(&transform, &surface_translate_origin, &transform); wlr_matrix_mul(&wlr_output->transform_matrix, &transform, &matrix); From eb763439f75d3c9174280d0c75ef44941dd99340 Mon Sep 17 00:00:00 2001 From: emersion Date: Fri, 1 Dec 2017 16:20:09 +0100 Subject: [PATCH 06/21] optimize a bit rootston renderer if there's no surface transform --- rootston/output.c | 30 +++++++++++++++++------------- 1 file changed, 17 insertions(+), 13 deletions(-) diff --git a/rootston/output.c b/rootston/output.c index 9df95a91..996f819d 100644 --- a/rootston/output.c +++ b/rootston/output.c @@ -63,24 +63,28 @@ static void render_surface(struct wlr_surface *surface, float scale[16]; wlr_matrix_scale(&scale, render_width, render_height, 1); - float surface_translate_center[16]; - wlr_matrix_translate(&surface_translate_center, 0.5, 0.5, 0); - - float surface_transform[16]; - wlr_matrix_transform(surface_transform, - wlr_output_transform_invert(surface->current->transform)); - - float surface_translate_origin[16]; - wlr_matrix_translate(&surface_translate_origin, -0.5, -0.5, 0); - float transform[16]; wlr_matrix_mul(&translate_center, &rotate, &transform); wlr_matrix_mul(&transform, &translate_origin, &transform); wlr_matrix_mul(&transform, &scale, &transform); - wlr_matrix_mul(&transform, &surface_translate_center, &transform); - wlr_matrix_mul(&transform, &surface_transform, &transform); - wlr_matrix_mul(&transform, &surface_translate_origin, &transform); + if (surface->current->transform != WL_OUTPUT_TRANSFORM_NORMAL) { + float surface_translate_center[16]; + wlr_matrix_translate(&surface_translate_center, 0.5, 0.5, 0); + + float surface_transform[16]; + wlr_matrix_transform(surface_transform, + wlr_output_transform_invert(surface->current->transform)); + + float surface_translate_origin[16]; + wlr_matrix_translate(&surface_translate_origin, -0.5, -0.5, 0); + + wlr_matrix_mul(&transform, &surface_translate_center, + &transform); + wlr_matrix_mul(&transform, &surface_transform, &transform); + wlr_matrix_mul(&transform, &surface_translate_origin, + &transform); + } wlr_matrix_mul(&wlr_output->transform_matrix, &transform, &matrix); From dfe11a2b0ef3806d3c2241e3bc8e69fc93905d8c Mon Sep 17 00:00:00 2001 From: emersion Date: Thu, 7 Dec 2017 00:13:06 +0100 Subject: [PATCH 07/21] Add display destroy listener in screenshooter, destroy display in rootston --- include/wlr/types/wlr_screenshooter.h | 8 +++++ rootston/main.c | 2 ++ types/wlr_screenshooter.c | 49 +++++++++++++++++++++------ 3 files changed, 49 insertions(+), 10 deletions(-) diff --git a/include/wlr/types/wlr_screenshooter.h b/include/wlr/types/wlr_screenshooter.h index 4bda3d3c..e8b59669 100644 --- a/include/wlr/types/wlr_screenshooter.h +++ b/include/wlr/types/wlr_screenshooter.h @@ -5,6 +5,13 @@ struct wlr_screenshooter { struct wl_global *wl_global; struct wlr_renderer *renderer; + struct wl_list screenshots; // wlr_screenshot::link + + struct wl_listener display_destroy; + + struct { + struct wl_signal destroy; + } events; void *data; }; @@ -12,6 +19,7 @@ struct wlr_screenshooter { struct wlr_screenshot { struct wl_resource *resource; struct wl_resource *output_resource; + struct wl_list link; struct wlr_output *output; struct wlr_screenshooter *screenshooter; diff --git a/rootston/main.c b/rootston/main.c index 46548094..3b65a067 100644 --- a/rootston/main.c +++ b/rootston/main.c @@ -51,6 +51,7 @@ int main(int argc, char **argv) { if (!wlr_backend_start(server.backend)) { wlr_log(L_ERROR, "Failed to start backend"); wlr_backend_destroy(server.backend); + wl_display_destroy(server.wl_display); return 1; } @@ -69,5 +70,6 @@ int main(int argc, char **argv) { wl_display_run(server.wl_display); wlr_backend_destroy(server.backend); + wl_display_destroy(server.wl_display); return 0; } diff --git a/types/wlr_screenshooter.c b/types/wlr_screenshooter.c index 94b45384..d62820e6 100644 --- a/types/wlr_screenshooter.c +++ b/types/wlr_screenshooter.c @@ -26,6 +26,18 @@ struct screenshot_state { struct wl_listener frame_listener; }; +static void screenshot_destroy(struct wlr_screenshot *screenshot) { + wl_list_remove(&screenshot->link); + free(screenshot); +} + +static void handle_screenshot_resource_destroy( + struct wl_resource *screenshot_resource) { + struct wlr_screenshot *screenshot = + wl_resource_get_user_data(screenshot_resource); + screenshot_destroy(screenshot); +} + static void output_frame_notify(struct wl_listener *listener, void *_data) { struct screenshot_state *state = wl_container_of(listener, state, frame_listener); @@ -102,7 +114,8 @@ static void screenshooter_shoot(struct wl_client *client, return; } wl_resource_set_implementation(screenshot->resource, NULL, screenshot, - NULL); + handle_screenshot_resource_destroy); + wl_list_insert(&screenshooter->screenshots, &screenshot->link); wlr_log(L_DEBUG, "new screenshot %p (res %p)", screenshot, screenshot->resource); @@ -144,6 +157,25 @@ static void screenshooter_bind(struct wl_client *wl_client, void *data, screenshooter, NULL); } +void wlr_screenshooter_destroy(struct wlr_screenshooter *screenshooter) { + if (!screenshooter) { + return; + } + wl_signal_emit(&screenshooter->events.destroy, screenshooter); + struct wlr_screenshot *screenshot, *tmp; + wl_list_for_each_safe(screenshot, tmp, &screenshooter->screenshots, link) { + screenshot_destroy(screenshot); + } + wl_global_destroy(screenshooter->wl_global); + free(screenshooter); +} + +static void handle_display_destroy(struct wl_listener *listener, void *data) { + struct wlr_screenshooter *screenshooter = + wl_container_of(listener, screenshooter, display_destroy); + wlr_screenshooter_destroy(screenshooter); +} + struct wlr_screenshooter *wlr_screenshooter_create(struct wl_display *display, struct wlr_renderer *renderer) { struct wlr_screenshooter *screenshooter = @@ -153,6 +185,12 @@ struct wlr_screenshooter *wlr_screenshooter_create(struct wl_display *display, } screenshooter->renderer = renderer; + wl_list_init(&screenshooter->screenshots); + wl_signal_init(&screenshooter->events.destroy); + + screenshooter->display_destroy.notify = handle_display_destroy; + wl_display_add_destroy_listener(display, &screenshooter->display_destroy); + screenshooter->wl_global = wl_global_create(display, &orbital_screenshooter_interface, 1, screenshooter, screenshooter_bind); if (screenshooter->wl_global == NULL) { @@ -162,12 +200,3 @@ struct wlr_screenshooter *wlr_screenshooter_create(struct wl_display *display, return screenshooter; } - -void wlr_screenshooter_destroy(struct wlr_screenshooter *screenshooter) { - if (!screenshooter) { - return; - } - // TODO: this segfault (wl_display->registry_resource_list is not init) - // wl_global_destroy(screenshooter->wl_global); - free(screenshooter); -} From 32591d66d61e2a4ef06db89276f8d158d22d81a8 Mon Sep 17 00:00:00 2001 From: emersion Date: Thu, 7 Dec 2017 00:36:05 +0100 Subject: [PATCH 08/21] Add display destroy listener in server decoration --- include/wlr/types/wlr_server_decoration.h | 2 + types/wlr_screenshooter.c | 5 ++- types/wlr_server_decoration.c | 46 ++++++++++++++--------- 3 files changed, 34 insertions(+), 19 deletions(-) diff --git a/include/wlr/types/wlr_server_decoration.h b/include/wlr/types/wlr_server_decoration.h index 474a9386..e457a0aa 100644 --- a/include/wlr/types/wlr_server_decoration.h +++ b/include/wlr/types/wlr_server_decoration.h @@ -33,6 +33,8 @@ struct wlr_server_decoration_manager { uint32_t default_mode; // enum wlr_server_decoration_manager_mode + struct wl_listener display_destroy; + struct { struct wl_signal new_decoration; } events; diff --git a/types/wlr_screenshooter.c b/types/wlr_screenshooter.c index d62820e6..cb831827 100644 --- a/types/wlr_screenshooter.c +++ b/types/wlr_screenshooter.c @@ -28,6 +28,7 @@ struct screenshot_state { static void screenshot_destroy(struct wlr_screenshot *screenshot) { wl_list_remove(&screenshot->link); + wl_resource_set_user_data(screenshot->resource, NULL); free(screenshot); } @@ -35,7 +36,9 @@ static void handle_screenshot_resource_destroy( struct wl_resource *screenshot_resource) { struct wlr_screenshot *screenshot = wl_resource_get_user_data(screenshot_resource); - screenshot_destroy(screenshot); + if (screenshot != NULL) { + screenshot_destroy(screenshot); + } } static void output_frame_notify(struct wl_listener *listener, void *_data) { diff --git a/types/wlr_server_decoration.c b/types/wlr_server_decoration.c index 60cc6369..3531c453 100644 --- a/types/wlr_server_decoration.c +++ b/types/wlr_server_decoration.c @@ -140,6 +140,30 @@ static void server_decoration_manager_bind(struct wl_client *client, void *data, manager->default_mode); } +void wlr_server_decoration_manager_destroy( + struct wlr_server_decoration_manager *manager) { + if (manager == NULL) { + return; + } + struct wlr_server_decoration *decoration, *tmp_decoration; + wl_list_for_each_safe(decoration, tmp_decoration, &manager->decorations, + link) { + server_decoration_destroy(decoration); + } + struct wl_resource *resource, *tmp_resource; + wl_resource_for_each_safe(resource, tmp_resource, &manager->wl_resources) { + server_decoration_manager_destroy_resource(resource); + } + wl_global_destroy(manager->wl_global); + free(manager); +} + +static void handle_display_destroy(struct wl_listener *listener, void *data) { + struct wlr_server_decoration_manager *manager = + wl_container_of(listener, manager, display_destroy); + wlr_server_decoration_manager_destroy(manager); +} + struct wlr_server_decoration_manager *wlr_server_decoration_manager_create( struct wl_display *display) { struct wlr_server_decoration_manager *manager = @@ -158,23 +182,9 @@ struct wlr_server_decoration_manager *wlr_server_decoration_manager_create( wl_list_init(&manager->wl_resources); wl_list_init(&manager->decorations); wl_signal_init(&manager->events.new_decoration); - return manager; -} -void wlr_server_decoration_manager_destroy( - struct wlr_server_decoration_manager *manager) { - if (manager == NULL) { - return; - } - struct wlr_server_decoration *decoration, *tmp_decoration; - wl_list_for_each_safe(decoration, tmp_decoration, &manager->decorations, - link) { - server_decoration_destroy(decoration); - } - struct wl_resource *resource, *tmp_resource; - wl_resource_for_each_safe(resource, tmp_resource, &manager->wl_resources) { - server_decoration_manager_destroy_resource(resource); - } - wl_global_destroy(manager->wl_global); - free(manager); + manager->display_destroy.notify = handle_display_destroy; + wl_display_add_destroy_listener(display, &manager->display_destroy); + + return manager; } From e6d2e72401200743ba13e20945dfad9f458a87e1 Mon Sep 17 00:00:00 2001 From: emersion Date: Thu, 7 Dec 2017 00:44:45 +0100 Subject: [PATCH 09/21] Remove display destroy listeners, add compositor listener --- include/wlr/types/wlr_compositor.h | 7 ++----- types/wlr_compositor.c | 16 +++++++++++++++- types/wlr_screenshooter.c | 1 + types/wlr_server_decoration.c | 1 + 4 files changed, 19 insertions(+), 6 deletions(-) diff --git a/include/wlr/types/wlr_compositor.h b/include/wlr/types/wlr_compositor.h index b2f5a972..ceeb64ca 100644 --- a/include/wlr/types/wlr_compositor.h +++ b/include/wlr/types/wlr_compositor.h @@ -9,7 +9,8 @@ struct wlr_compositor { struct wl_list wl_resources; struct wlr_renderer *renderer; struct wl_list surfaces; - struct wl_listener destroy_surface_listener; + + struct wl_listener display_destroy; struct { struct wl_signal create_surface; @@ -20,8 +21,4 @@ void wlr_compositor_destroy(struct wlr_compositor *wlr_compositor); struct wlr_compositor *wlr_compositor_create(struct wl_display *display, struct wlr_renderer *renderer); -struct wlr_surface; -void wl_compositor_surface_destroyed(struct wlr_compositor *wlr_compositor, - struct wlr_surface *surface); - #endif diff --git a/types/wlr_compositor.c b/types/wlr_compositor.c index 986b7e60..919d0e32 100644 --- a/types/wlr_compositor.c +++ b/types/wlr_compositor.c @@ -78,6 +78,7 @@ static void wl_compositor_bind(struct wl_client *wl_client, void *data, } void wlr_compositor_destroy(struct wlr_compositor *compositor) { + wl_list_remove(&compositor->display_destroy.link); wl_global_destroy(compositor->wl_global); free(compositor); } @@ -151,6 +152,12 @@ static void subcompositor_bind(struct wl_client *client, void *data, compositor, NULL); } +static void handle_display_destroy(struct wl_listener *listener, void *data) { + struct wlr_compositor *compositor = + wl_container_of(listener, compositor, display_destroy); + wlr_compositor_destroy(compositor); +} + struct wlr_compositor *wlr_compositor_create(struct wl_display *display, struct wlr_renderer *renderer) { struct wlr_compositor *compositor = @@ -162,7 +169,11 @@ struct wlr_compositor *wlr_compositor_create(struct wl_display *display, struct wl_global *compositor_global = wl_global_create(display, &wl_compositor_interface, 4, compositor, wl_compositor_bind); - + if (!compositor_global) { + wlr_log_errno(L_ERROR, "Could not allocate compositor global"); + free(compositor); + return NULL; + } compositor->wl_global = compositor_global; compositor->renderer = renderer; @@ -173,5 +184,8 @@ struct wlr_compositor *wlr_compositor_create(struct wl_display *display, wl_list_init(&compositor->surfaces); wl_signal_init(&compositor->events.create_surface); + compositor->display_destroy.notify = handle_display_destroy; + wl_display_add_destroy_listener(display, &compositor->display_destroy); + return compositor; } diff --git a/types/wlr_screenshooter.c b/types/wlr_screenshooter.c index cb831827..8939f073 100644 --- a/types/wlr_screenshooter.c +++ b/types/wlr_screenshooter.c @@ -165,6 +165,7 @@ void wlr_screenshooter_destroy(struct wlr_screenshooter *screenshooter) { return; } wl_signal_emit(&screenshooter->events.destroy, screenshooter); + wl_list_remove(&screenshooter->display_destroy.link); struct wlr_screenshot *screenshot, *tmp; wl_list_for_each_safe(screenshot, tmp, &screenshooter->screenshots, link) { screenshot_destroy(screenshot); diff --git a/types/wlr_server_decoration.c b/types/wlr_server_decoration.c index 3531c453..09909bb6 100644 --- a/types/wlr_server_decoration.c +++ b/types/wlr_server_decoration.c @@ -145,6 +145,7 @@ void wlr_server_decoration_manager_destroy( if (manager == NULL) { return; } + wl_list_remove(&manager->display_destroy.link); struct wlr_server_decoration *decoration, *tmp_decoration; wl_list_for_each_safe(decoration, tmp_decoration, &manager->decorations, link) { From c7900663bc24e575b13a83ce77b7d46e8837d784 Mon Sep 17 00:00:00 2001 From: emersion Date: Thu, 7 Dec 2017 00:50:55 +0100 Subject: [PATCH 10/21] Add display destroy listener to data device --- include/wlr/types/wlr_data_device.h | 2 ++ types/wlr_compositor.c | 3 +++ types/wlr_data_device.c | 26 ++++++++++++++++++-------- 3 files changed, 23 insertions(+), 8 deletions(-) diff --git a/include/wlr/types/wlr_data_device.h b/include/wlr/types/wlr_data_device.h index 12b25e45..70d79a19 100644 --- a/include/wlr/types/wlr_data_device.h +++ b/include/wlr/types/wlr_data_device.h @@ -15,6 +15,8 @@ wlr_touch_grab_interface wlr_data_device_touch_drag_interface; struct wlr_data_device_manager { struct wl_global *global; + + struct wl_listener display_destroy; }; struct wlr_data_offer { diff --git a/types/wlr_compositor.c b/types/wlr_compositor.c index 919d0e32..cf8c3f8f 100644 --- a/types/wlr_compositor.c +++ b/types/wlr_compositor.c @@ -78,6 +78,9 @@ static void wl_compositor_bind(struct wl_client *wl_client, void *data, } void wlr_compositor_destroy(struct wlr_compositor *compositor) { + if (compositor == NULL) { + return; + } wl_list_remove(&compositor->display_destroy.link); wl_global_destroy(compositor->wl_global); free(compositor); diff --git a/types/wlr_data_device.c b/types/wlr_data_device.c index a8d69cc0..dacb2a45 100644 --- a/types/wlr_data_device.c +++ b/types/wlr_data_device.c @@ -949,6 +949,21 @@ static void data_device_manager_bind(struct wl_client *client, NULL, NULL); } +void wlr_data_device_manager_destroy(struct wlr_data_device_manager *manager) { + if (!manager) { + return; + } + wl_list_remove(&manager->display_destroy.link); + wl_global_destroy(manager->global); + free(manager); +} + +static void handle_display_destroy(struct wl_listener *listener, void *data) { + struct wlr_data_device_manager *manager = + wl_container_of(listener, manager, display_destroy); + wlr_data_device_manager_destroy(manager); +} + struct wlr_data_device_manager *wlr_data_device_manager_create( struct wl_display *display) { struct wlr_data_device_manager *manager = @@ -967,13 +982,8 @@ struct wlr_data_device_manager *wlr_data_device_manager_create( return NULL; } - return manager; -} + manager->display_destroy.notify = handle_display_destroy; + wl_display_add_destroy_listener(display, &manager->display_destroy); -void wlr_data_device_manager_destroy(struct wlr_data_device_manager *manager) { - if (!manager) { - return; - } - wl_global_destroy(manager->global); - free(manager); + return manager; } From 088ff9a614904181f68c861769655fac119bd8a0 Mon Sep 17 00:00:00 2001 From: emersion Date: Thu, 7 Dec 2017 00:55:50 +0100 Subject: [PATCH 11/21] Listen to display destroy in gamma control manager --- include/wlr/types/wlr_gamma_control.h | 4 +- types/wlr_gamma_control.c | 63 ++++++++++++++++----------- 2 files changed, 40 insertions(+), 27 deletions(-) diff --git a/include/wlr/types/wlr_gamma_control.h b/include/wlr/types/wlr_gamma_control.h index 59f18494..d3f4b8e5 100644 --- a/include/wlr/types/wlr_gamma_control.h +++ b/include/wlr/types/wlr_gamma_control.h @@ -5,7 +5,9 @@ struct wlr_gamma_control_manager { struct wl_global *wl_global; - struct wl_list controls; // list of wlr_gamma_control + struct wl_list controls; // wlr_gamma_control::link + + struct wl_listener display_destroy; void *data; }; diff --git a/types/wlr_gamma_control.c b/types/wlr_gamma_control.c index 355e56a2..eb154b6b 100644 --- a/types/wlr_gamma_control.c +++ b/types/wlr_gamma_control.c @@ -12,6 +12,9 @@ static void resource_destroy(struct wl_client *client, } static void gamma_control_destroy(struct wlr_gamma_control *gamma_control) { + if (gamma_control == NULL) { + return; + } wl_signal_emit(&gamma_control->events.destroy, gamma_control); wl_list_remove(&gamma_control->output_destroy_listener.link); wl_resource_set_user_data(gamma_control->resource, NULL); @@ -67,7 +70,7 @@ static const struct gamma_control_interface gamma_control_impl = { static void gamma_control_manager_get_gamma_control(struct wl_client *client, struct wl_resource *gamma_control_manager_resource, uint32_t id, struct wl_resource *output_resource) { - struct wlr_gamma_control_manager *gamma_control_manager = + struct wlr_gamma_control_manager *manager = wl_resource_get_user_data(gamma_control_manager_resource); struct wlr_output *output = wl_resource_get_user_data(output_resource); @@ -99,7 +102,7 @@ static void gamma_control_manager_get_gamma_control(struct wl_client *client, gamma_control->output_destroy_listener.notify = gamma_control_handle_output_destroy; - wl_list_insert(&gamma_control_manager->controls, &gamma_control->link); + wl_list_insert(&manager->controls, &gamma_control->link); gamma_control_send_gamma_size(gamma_control->resource, wlr_output_get_gamma_size(output)); @@ -111,8 +114,8 @@ static struct gamma_control_manager_interface gamma_control_manager_impl = { static void gamma_control_manager_bind(struct wl_client *client, void *data, uint32_t version, uint32_t id) { - struct wlr_gamma_control_manager *gamma_control_manager = data; - assert(client && gamma_control_manager); + struct wlr_gamma_control_manager *manager = data; + assert(client && manager); struct wl_resource *resource = wl_resource_create(client, &gamma_control_manager_interface, version, id); @@ -121,41 +124,49 @@ static void gamma_control_manager_bind(struct wl_client *client, void *data, return; } wl_resource_set_implementation(resource, &gamma_control_manager_impl, - gamma_control_manager, NULL); + manager, NULL); +} + +void wlr_gamma_control_manager_destroy( + struct wlr_gamma_control_manager *manager) { + if (!manager) { + return; + } + wl_list_remove(&manager->display_destroy.link); + struct wlr_gamma_control *gamma_control, *tmp; + wl_list_for_each_safe(gamma_control, tmp, &manager->controls, link) { + gamma_control_destroy(gamma_control); + } + wl_global_destroy(manager->wl_global); + free(manager); +} + +static void handle_display_destroy(struct wl_listener *listener, void *data) { + struct wlr_gamma_control_manager *manager = + wl_container_of(listener, manager, display_destroy); + wlr_gamma_control_manager_destroy(manager); } struct wlr_gamma_control_manager *wlr_gamma_control_manager_create( struct wl_display *display) { - struct wlr_gamma_control_manager *gamma_control_manager = + struct wlr_gamma_control_manager *manager = calloc(1, sizeof(struct wlr_gamma_control_manager)); - if (!gamma_control_manager) { + if (!manager) { return NULL; } struct wl_global *wl_global = wl_global_create(display, - &gamma_control_manager_interface, 1, gamma_control_manager, + &gamma_control_manager_interface, 1, manager, gamma_control_manager_bind); if (!wl_global) { - free(gamma_control_manager); + free(manager); return NULL; } - gamma_control_manager->wl_global = wl_global; + manager->wl_global = wl_global; - wl_list_init(&gamma_control_manager->controls); + wl_list_init(&manager->controls); - return gamma_control_manager; -} + manager->display_destroy.notify = handle_display_destroy; + wl_display_add_destroy_listener(display, &manager->display_destroy); -void wlr_gamma_control_manager_destroy( - struct wlr_gamma_control_manager *gamma_control_manager) { - if (!gamma_control_manager) { - return; - } - struct wlr_gamma_control *gamma_control, *tmp; - wl_list_for_each_safe(gamma_control, tmp, &gamma_control_manager->controls, - link) { - gamma_control_destroy(gamma_control); - } - // TODO: this segfault (wl_display->registry_resource_list is not init) - // wl_global_destroy(gamma_control_manager->wl_global); - free(gamma_control_manager); + return manager; } From 983ea3033fdbf477e0810582501b0bbc1460f1c9 Mon Sep 17 00:00:00 2001 From: emersion Date: Thu, 7 Dec 2017 01:02:07 +0100 Subject: [PATCH 12/21] Listen to display destroy in seat --- include/wlr/types/wlr_seat.h | 1 + types/wlr_seat.c | 51 ++++++++++++++++++++++-------------- 2 files changed, 32 insertions(+), 20 deletions(-) diff --git a/include/wlr/types/wlr_seat.h b/include/wlr/types/wlr_seat.h index 6d59315b..fa2df530 100644 --- a/include/wlr/types/wlr_seat.h +++ b/include/wlr/types/wlr_seat.h @@ -183,6 +183,7 @@ struct wlr_seat { struct wlr_seat_keyboard_state keyboard_state; struct wlr_seat_touch_state touch_state; + struct wl_listener display_destroy; struct wl_listener selection_data_source_destroy; struct { diff --git a/types/wlr_seat.c b/types/wlr_seat.c index ff1cbf7f..c64cbef9 100644 --- a/types/wlr_seat.c +++ b/types/wlr_seat.c @@ -337,6 +337,34 @@ static const struct wlr_touch_grab_interface default_touch_grab_impl = { }; +void wlr_seat_destroy(struct wlr_seat *wlr_seat) { + if (!wlr_seat) { + return; + } + + wl_list_remove(&wlr_seat->display_destroy.link); + + struct wlr_seat_client *client, *tmp; + wl_list_for_each_safe(client, tmp, &wlr_seat->clients, link) { + // will destroy other resources as well + wl_resource_destroy(client->wl_resource); + } + + wl_global_destroy(wlr_seat->wl_global); + free(wlr_seat->pointer_state.default_grab); + free(wlr_seat->keyboard_state.default_grab); + free(wlr_seat->touch_state.default_grab); + free(wlr_seat->data_device); + free(wlr_seat->name); + free(wlr_seat); +} + +static void handle_display_destroy(struct wl_listener *listener, void *data) { + struct wlr_seat *seat = + wl_container_of(listener, seat, display_destroy); + wlr_seat_destroy(seat); +} + struct wlr_seat *wlr_seat_create(struct wl_display *display, const char *name) { struct wlr_seat *wlr_seat = calloc(1, sizeof(struct wlr_seat)); if (!wlr_seat) { @@ -418,27 +446,10 @@ struct wlr_seat *wlr_seat_create(struct wl_display *display, const char *name) { wl_signal_init(&wlr_seat->events.touch_grab_begin); wl_signal_init(&wlr_seat->events.touch_grab_end); - return wlr_seat; -} - -void wlr_seat_destroy(struct wlr_seat *wlr_seat) { - if (!wlr_seat) { - return; - } - - struct wlr_seat_client *client, *tmp; - wl_list_for_each_safe(client, tmp, &wlr_seat->clients, link) { - // will destroy other resources as well - wl_resource_destroy(client->wl_resource); - } + wlr_seat->display_destroy.notify = handle_display_destroy; + wl_display_add_destroy_listener(display, &wlr_seat->display_destroy); - wl_global_destroy(wlr_seat->wl_global); - free(wlr_seat->pointer_state.default_grab); - free(wlr_seat->keyboard_state.default_grab); - free(wlr_seat->touch_state.default_grab); - free(wlr_seat->data_device); - free(wlr_seat->name); - free(wlr_seat); + return wlr_seat; } struct wlr_seat_client *wlr_seat_client_for_wl_client(struct wlr_seat *wlr_seat, From 9ddaea14a1b24bf755417bffce792b0d4a23a87c Mon Sep 17 00:00:00 2001 From: emersion Date: Thu, 7 Dec 2017 01:05:49 +0100 Subject: [PATCH 13/21] Listen to display destroy in wl-shell --- include/wlr/types/wlr_wl_shell.h | 2 ++ types/wlr_wl_shell.c | 14 ++++++++++++-- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/include/wlr/types/wlr_wl_shell.h b/include/wlr/types/wlr_wl_shell.h index 986f92e8..29446b04 100644 --- a/include/wlr/types/wlr_wl_shell.h +++ b/include/wlr/types/wlr_wl_shell.h @@ -12,6 +12,8 @@ struct wlr_wl_shell { struct wl_list popup_grabs; uint32_t ping_timeout; + struct wl_listener display_destroy; + struct { struct wl_signal new_surface; } events; diff --git a/types/wlr_wl_shell.c b/types/wlr_wl_shell.c index ee72856c..78154245 100644 --- a/types/wlr_wl_shell.c +++ b/types/wlr_wl_shell.c @@ -564,6 +564,12 @@ static void shell_bind(struct wl_client *wl_client, void *data, wl_list_insert(&wl_shell->wl_resources, wl_resource_get_link(wl_resource)); } +static void handle_display_destroy(struct wl_listener *listener, void *data) { + struct wlr_wl_shell *wl_shell = + wl_container_of(listener, wl_shell, display_destroy); + wlr_wl_shell_destroy(wl_shell); +} + struct wlr_wl_shell *wlr_wl_shell_create(struct wl_display *display) { struct wlr_wl_shell *wl_shell = calloc(1, sizeof(struct wlr_wl_shell)); if (!wl_shell) { @@ -581,6 +587,10 @@ struct wlr_wl_shell *wlr_wl_shell_create(struct wl_display *display) { wl_list_init(&wl_shell->surfaces); wl_list_init(&wl_shell->popup_grabs); wl_signal_init(&wl_shell->events.new_surface); + + wl_shell->display_destroy.notify = handle_display_destroy; + wl_display_add_destroy_listener(display, &wl_shell->display_destroy); + return wl_shell; } @@ -588,14 +598,14 @@ void wlr_wl_shell_destroy(struct wlr_wl_shell *wlr_wl_shell) { if (!wlr_wl_shell) { return; } + wl_list_remove(&wlr_wl_shell->display_destroy.link); struct wl_resource *resource = NULL, *temp = NULL; wl_resource_for_each_safe(resource, temp, &wlr_wl_shell->wl_resources) { struct wl_list *link = wl_resource_get_link(resource); wl_list_remove(link); } // TODO: destroy surfaces - // TODO: this segfault (wl_display->registry_resource_list is not init) - // wl_global_destroy(wlr_wl_shell->wl_global); + wl_global_destroy(wlr_wl_shell->wl_global); free(wlr_wl_shell); } From cbf5763c226661765fb87dcf30d60b04999c126b Mon Sep 17 00:00:00 2001 From: emersion Date: Thu, 7 Dec 2017 01:08:35 +0100 Subject: [PATCH 14/21] Listen to display destroy in xdg-shell-v6 --- include/wlr/types/wlr_xdg_shell_v6.h | 2 ++ types/wlr_xdg_shell_v6.c | 13 +++++++++++-- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/include/wlr/types/wlr_xdg_shell_v6.h b/include/wlr/types/wlr_xdg_shell_v6.h index 4eb957be..b723a950 100644 --- a/include/wlr/types/wlr_xdg_shell_v6.h +++ b/include/wlr/types/wlr_xdg_shell_v6.h @@ -11,6 +11,8 @@ struct wlr_xdg_shell_v6 { struct wl_list popup_grabs; uint32_t ping_timeout; + struct wl_listener display_destroy; + struct { struct wl_signal new_surface; } events; diff --git a/types/wlr_xdg_shell_v6.c b/types/wlr_xdg_shell_v6.c index 2627246f..23d36b6f 100644 --- a/types/wlr_xdg_shell_v6.c +++ b/types/wlr_xdg_shell_v6.c @@ -1250,6 +1250,12 @@ static void xdg_shell_bind(struct wl_client *wl_client, void *data, } } +static void handle_display_destroy(struct wl_listener *listener, void *data) { + struct wlr_xdg_shell_v6 *xdg_shell = + wl_container_of(listener, xdg_shell, display_destroy); + wlr_xdg_shell_v6_destroy(xdg_shell); +} + struct wlr_xdg_shell_v6 *wlr_xdg_shell_v6_create(struct wl_display *display) { struct wlr_xdg_shell_v6 *xdg_shell = calloc(1, sizeof(struct wlr_xdg_shell_v6)); @@ -1272,6 +1278,9 @@ struct wlr_xdg_shell_v6 *wlr_xdg_shell_v6_create(struct wl_display *display) { wl_signal_init(&xdg_shell->events.new_surface); + xdg_shell->display_destroy.notify = handle_display_destroy; + wl_display_add_destroy_listener(display, &xdg_shell->display_destroy); + return xdg_shell; } @@ -1279,8 +1288,8 @@ void wlr_xdg_shell_v6_destroy(struct wlr_xdg_shell_v6 *xdg_shell) { if (!xdg_shell) { return; } - // TODO: this segfault (wl_display->registry_resource_list is not init) - // wl_global_destroy(xdg_shell->wl_global); + wl_list_remove(&xdg_shell->display_destroy.link); + wl_global_destroy(xdg_shell->wl_global); free(xdg_shell); } From 0e066f0d7634b051981fbfb2fb75b2f8a4fe8594 Mon Sep 17 00:00:00 2001 From: emersion Date: Thu, 7 Dec 2017 01:12:04 +0100 Subject: [PATCH 15/21] Listen to display destroy in output --- include/wlr/types/wlr_output.h | 2 ++ types/wlr_output.c | 11 +++++++++++ 2 files changed, 13 insertions(+) diff --git a/include/wlr/types/wlr_output.h b/include/wlr/types/wlr_output.h index 42c46233..04662820 100644 --- a/include/wlr/types/wlr_output.h +++ b/include/wlr/types/wlr_output.h @@ -74,6 +74,8 @@ struct wlr_output { // the output position in layout space reported to clients int32_t lx, ly; + struct wl_listener display_destroy; + void *data; }; diff --git a/types/wlr_output.c b/types/wlr_output.c index ed74004d..dda63181 100644 --- a/types/wlr_output.c +++ b/types/wlr_output.c @@ -112,6 +112,12 @@ static void wl_output_bind(struct wl_client *wl_client, void *data, wl_output_send_to_resource(wl_resource); } +static void handle_display_destroy(struct wl_listener *listener, void *data) { + struct wlr_output *output = + wl_container_of(listener, output, display_destroy); + wlr_output_destroy_global(output); +} + struct wl_global *wlr_output_create_global(struct wlr_output *wlr_output, struct wl_display *display) { if (wlr_output->wl_global != NULL) { @@ -120,6 +126,10 @@ struct wl_global *wlr_output_create_global(struct wlr_output *wlr_output, struct wl_global *wl_global = wl_global_create(display, &wl_output_interface, 3, wlr_output, wl_output_bind); wlr_output->wl_global = wl_global; + + wlr_output->display_destroy.notify = handle_display_destroy; + wl_display_add_destroy_listener(display, &wlr_output->display_destroy); + return wl_global; } @@ -127,6 +137,7 @@ void wlr_output_destroy_global(struct wlr_output *wlr_output) { if (wlr_output->wl_global == NULL) { return; } + wl_list_remove(&wlr_output->display_destroy.link); struct wl_resource *resource, *tmp; wl_resource_for_each_safe(resource, tmp, &wlr_output->wl_resources) { struct wl_list *link = wl_resource_get_link(resource); From bdb6e0b84ce69bea105dc54d3da013da4111f4ae Mon Sep 17 00:00:00 2001 From: emersion Date: Thu, 7 Dec 2017 01:19:41 +0100 Subject: [PATCH 16/21] Remove destroy event in screenshooter for now --- include/wlr/types/wlr_screenshooter.h | 4 ---- types/wlr_screenshooter.c | 2 -- 2 files changed, 6 deletions(-) diff --git a/include/wlr/types/wlr_screenshooter.h b/include/wlr/types/wlr_screenshooter.h index e8b59669..4c66be3f 100644 --- a/include/wlr/types/wlr_screenshooter.h +++ b/include/wlr/types/wlr_screenshooter.h @@ -9,10 +9,6 @@ struct wlr_screenshooter { struct wl_listener display_destroy; - struct { - struct wl_signal destroy; - } events; - void *data; }; diff --git a/types/wlr_screenshooter.c b/types/wlr_screenshooter.c index 8939f073..4d591c45 100644 --- a/types/wlr_screenshooter.c +++ b/types/wlr_screenshooter.c @@ -164,7 +164,6 @@ void wlr_screenshooter_destroy(struct wlr_screenshooter *screenshooter) { if (!screenshooter) { return; } - wl_signal_emit(&screenshooter->events.destroy, screenshooter); wl_list_remove(&screenshooter->display_destroy.link); struct wlr_screenshot *screenshot, *tmp; wl_list_for_each_safe(screenshot, tmp, &screenshooter->screenshots, link) { @@ -190,7 +189,6 @@ struct wlr_screenshooter *wlr_screenshooter_create(struct wl_display *display, screenshooter->renderer = renderer; wl_list_init(&screenshooter->screenshots); - wl_signal_init(&screenshooter->events.destroy); screenshooter->display_destroy.notify = handle_display_destroy; wl_display_add_destroy_listener(display, &screenshooter->display_destroy); From 9d43adaafa6bbe63116b3feba796844169f91b25 Mon Sep 17 00:00:00 2001 From: emersion Date: Thu, 7 Dec 2017 23:44:59 +0100 Subject: [PATCH 17/21] Listen to display destroy in backends --- backend/drm/backend.c | 19 ++++- backend/libinput/backend.c | 25 +++++-- backend/wayland/backend.c | 14 +++- backend/x11/backend.c | 137 ++++++++++++++++++++----------------- include/backend/drm/drm.h | 1 + include/backend/libinput.h | 1 + include/backend/wayland.h | 1 + include/backend/x11.h | 2 + 8 files changed, 130 insertions(+), 70 deletions(-) diff --git a/backend/drm/backend.c b/backend/drm/backend.c index 87a4f708..6be5e5d5 100644 --- a/backend/drm/backend.c +++ b/backend/drm/backend.c @@ -34,6 +34,10 @@ static void wlr_drm_backend_destroy(struct wlr_backend *backend) { wlr_output_destroy(&conn->output); } + wl_list_remove(&drm->display_destroy.link); + wl_list_remove(&drm->session_signal.link); + wl_list_remove(&drm->drm_invalidated.link); + wlr_drm_resources_free(drm); wlr_drm_renderer_finish(&drm->renderer); wlr_session_close_file(drm->session, drm->fd); @@ -57,7 +61,8 @@ bool wlr_backend_is_drm(struct wlr_backend *b) { } static void session_signal(struct wl_listener *listener, void *data) { - struct wlr_drm_backend *drm = wl_container_of(listener, drm, session_signal); + struct wlr_drm_backend *drm = + wl_container_of(listener, drm, session_signal); struct wlr_session *session = data; if (session->active) { @@ -84,7 +89,8 @@ static void session_signal(struct wl_listener *listener, void *data) { } static void drm_invalidated(struct wl_listener *listener, void *data) { - struct wlr_drm_backend *drm = wl_container_of(listener, drm, drm_invalidated); + struct wlr_drm_backend *drm = + wl_container_of(listener, drm, drm_invalidated); char *name = drmGetDeviceNameFromFd2(drm->fd); wlr_log(L_DEBUG, "%s invalidated", name); @@ -93,6 +99,12 @@ static void drm_invalidated(struct wl_listener *listener, void *data) { wlr_drm_scan_connectors(drm); } +static void handle_display_destroy(struct wl_listener *listener, void *data) { + struct wlr_drm_backend *drm = + wl_container_of(listener, drm, display_destroy); + wlr_drm_backend_destroy(&drm->backend); +} + struct wlr_backend *wlr_drm_backend_create(struct wl_display *display, struct wlr_session *session, int gpu_fd, struct wlr_backend *parent) { assert(display && session && gpu_fd >= 0); @@ -150,6 +162,9 @@ struct wlr_backend *wlr_drm_backend_create(struct wl_display *display, wlr_log(L_INFO, "Failed to bind egl/wl display: %s", egl_error()); } + drm->display_destroy.notify = handle_display_destroy; + wl_display_add_destroy_listener(display, &drm->display_destroy); + return &drm->backend; error_event: diff --git a/backend/libinput/backend.c b/backend/libinput/backend.c index 1028e238..c9352051 100644 --- a/backend/libinput/backend.c +++ b/backend/libinput/backend.c @@ -43,10 +43,12 @@ static void wlr_libinput_log(struct libinput *libinput_context, } static bool wlr_libinput_backend_start(struct wlr_backend *_backend) { - struct wlr_libinput_backend *backend = (struct wlr_libinput_backend *)_backend; + struct wlr_libinput_backend *backend = + (struct wlr_libinput_backend *)_backend; wlr_log(L_DEBUG, "Initializing libinput"); - backend->libinput_context = libinput_udev_create_context(&libinput_impl, backend, - backend->session->udev); + + backend->libinput_context = libinput_udev_create_context(&libinput_impl, + backend, backend->session->udev); if (!backend->libinput_context) { wlr_log(L_ERROR, "Failed to create libinput context"); return false; @@ -99,6 +101,7 @@ static void wlr_libinput_backend_destroy(struct wlr_backend *_backend) { } struct wlr_libinput_backend *backend = (struct wlr_libinput_backend *)_backend; + for (size_t i = 0; i < backend->wlr_device_lists.length; i++) { struct wl_list *wlr_devices = backend->wlr_device_lists.items[i]; struct wlr_input_device *wlr_dev, *next; @@ -108,6 +111,10 @@ static void wlr_libinput_backend_destroy(struct wlr_backend *_backend) { } free(wlr_devices); } + + wl_list_remove(&backend->display_destroy.link); + wl_list_remove(&backend->session_signal.link); + wlr_list_finish(&backend->wlr_device_lists); wl_event_source_remove(backend->input_event); libinput_unref(backend->libinput_context); @@ -124,7 +131,8 @@ bool wlr_backend_is_libinput(struct wlr_backend *b) { } static void session_signal(struct wl_listener *listener, void *data) { - struct wlr_libinput_backend *backend = wl_container_of(listener, backend, session_signal); + struct wlr_libinput_backend *backend = + wl_container_of(listener, backend, session_signal); struct wlr_session *session = data; if (!backend->libinput_context) { @@ -138,6 +146,12 @@ static void session_signal(struct wl_listener *listener, void *data) { } } +static void handle_display_destroy(struct wl_listener *listener, void *data) { + struct wlr_libinput_backend *backend = + wl_container_of(listener, backend, display_destroy); + wlr_libinput_backend_destroy(&backend->backend); +} + struct wlr_backend *wlr_libinput_backend_create(struct wl_display *display, struct wlr_session *session) { assert(display && session); @@ -160,6 +174,9 @@ struct wlr_backend *wlr_libinput_backend_create(struct wl_display *display, backend->session_signal.notify = session_signal; wl_signal_add(&session->session_signal, &backend->session_signal); + backend->display_destroy.notify = handle_display_destroy; + wl_display_add_destroy_listener(display, &backend->display_destroy); + return &backend->backend; error_backend: free(backend); diff --git a/backend/wayland/backend.c b/backend/wayland/backend.c index 1801f3e0..cb241490 100644 --- a/backend/wayland/backend.c +++ b/backend/wayland/backend.c @@ -78,6 +78,8 @@ static void wlr_wl_backend_destroy(struct wlr_backend *_backend) { wlr_input_device_destroy(input_device); } + wl_list_remove(&backend->local_display_destroy.link); + free(backend->seat_name); wl_event_source_remove(backend->remote_display_src); @@ -117,6 +119,12 @@ struct wlr_wl_backend_output *wlr_wl_output_for_surface( return NULL; } +static void handle_display_destroy(struct wl_listener *listener, void *data) { + struct wlr_wl_backend *backend = + wl_container_of(listener, backend, local_display_destroy); + wlr_wl_backend_destroy(&backend->backend); +} + struct wlr_backend *wlr_wl_backend_create(struct wl_display *display) { wlr_log(L_INFO, "Creating wayland backend"); @@ -143,8 +151,12 @@ struct wlr_backend *wlr_wl_backend_create(struct wl_display *display) { return false; } - wlr_egl_init(&backend->egl, EGL_PLATFORM_WAYLAND_EXT, WL_SHM_FORMAT_ARGB8888, backend->remote_display); + wlr_egl_init(&backend->egl, EGL_PLATFORM_WAYLAND_EXT, + WL_SHM_FORMAT_ARGB8888, backend->remote_display); wlr_egl_bind_display(&backend->egl, backend->local_display); + backend->local_display_destroy.notify = handle_display_destroy; + wl_display_add_destroy_listener(display, &backend->local_display_destroy); + return &backend->backend; } diff --git a/backend/x11/backend.c b/backend/x11/backend.c index b798daf6..a4ca55b0 100644 --- a/backend/x11/backend.c +++ b/backend/x11/backend.c @@ -184,69 +184,6 @@ static void init_atom(struct wlr_x11_backend *x11, struct wlr_x11_atom *atom, atom->reply = xcb_intern_atom_reply(x11->xcb_conn, atom->cookie, NULL); } -struct wlr_backend *wlr_x11_backend_create(struct wl_display *display, - const char *x11_display) { - struct wlr_x11_backend *x11 = calloc(1, sizeof(*x11)); - if (!x11) { - return NULL; - } - - wlr_backend_init(&x11->backend, &backend_impl); - x11->wl_display = display; - - x11->xlib_conn = XOpenDisplay(x11_display); - if (!x11->xlib_conn) { - wlr_log(L_ERROR, "Failed to open X connection"); - return NULL; - } - - x11->xcb_conn = XGetXCBConnection(x11->xlib_conn); - if (!x11->xcb_conn || xcb_connection_has_error(x11->xcb_conn)) { - wlr_log(L_ERROR, "Failed to open xcb connection"); - goto error_x11; - } - - XSetEventQueueOwner(x11->xlib_conn, XCBOwnsEventQueue); - - int fd = xcb_get_file_descriptor(x11->xcb_conn); - struct wl_event_loop *ev = wl_display_get_event_loop(display); - int events = WL_EVENT_READABLE | WL_EVENT_ERROR | WL_EVENT_HANGUP; - x11->event_source = wl_event_loop_add_fd(ev, fd, events, x11_event, x11); - if (!x11->event_source) { - wlr_log(L_ERROR, "Could not create event source"); - goto error_x11; - } - - x11->frame_timer = wl_event_loop_add_timer(ev, signal_frame, x11); - - x11->screen = xcb_setup_roots_iterator(xcb_get_setup(x11->xcb_conn)).data; - - if (!wlr_egl_init(&x11->egl, EGL_PLATFORM_X11_KHR, - x11->screen->root_visual, x11->xlib_conn)) { - goto error_event; - } - - wlr_input_device_init(&x11->keyboard_dev, WLR_INPUT_DEVICE_KEYBOARD, - NULL, "X11 keyboard", 0, 0); - wlr_keyboard_init(&x11->keyboard, NULL); - x11->keyboard_dev.keyboard = &x11->keyboard; - - wlr_input_device_init(&x11->pointer_dev, WLR_INPUT_DEVICE_POINTER, - NULL, "X11 pointer", 0, 0); - wlr_pointer_init(&x11->pointer, NULL); - x11->pointer_dev.pointer = &x11->pointer; - - return &x11->backend; - -error_event: - wl_event_source_remove(x11->event_source); -error_x11: - xcb_disconnect(x11->xcb_conn); - XCloseDisplay(x11->xlib_conn); - free(x11); - return NULL; -} - static bool wlr_x11_backend_start(struct wlr_backend *backend) { struct wlr_x11_backend *x11 = (struct wlr_x11_backend *)backend; struct wlr_x11_output *output = &x11->output; @@ -307,6 +244,8 @@ static void wlr_x11_backend_destroy(struct wlr_backend *backend) { struct wlr_x11_output *output = &x11->output; wlr_output_destroy(&output->wlr_output); + wl_list_remove(&x11->display_destroy.link); + wl_event_source_remove(x11->frame_timer); wlr_egl_free(&x11->egl); @@ -329,6 +268,78 @@ static struct wlr_backend_impl backend_impl = { .get_egl = wlr_x11_backend_get_egl, }; +static void handle_display_destroy(struct wl_listener *listener, void *data) { + struct wlr_x11_backend *x11 = + wl_container_of(listener, x11, display_destroy); + wlr_x11_backend_destroy(&x11->backend); +} + +struct wlr_backend *wlr_x11_backend_create(struct wl_display *display, + const char *x11_display) { + struct wlr_x11_backend *x11 = calloc(1, sizeof(*x11)); + if (!x11) { + return NULL; + } + + wlr_backend_init(&x11->backend, &backend_impl); + x11->wl_display = display; + + x11->xlib_conn = XOpenDisplay(x11_display); + if (!x11->xlib_conn) { + wlr_log(L_ERROR, "Failed to open X connection"); + return NULL; + } + + x11->xcb_conn = XGetXCBConnection(x11->xlib_conn); + if (!x11->xcb_conn || xcb_connection_has_error(x11->xcb_conn)) { + wlr_log(L_ERROR, "Failed to open xcb connection"); + goto error_x11; + } + + XSetEventQueueOwner(x11->xlib_conn, XCBOwnsEventQueue); + + int fd = xcb_get_file_descriptor(x11->xcb_conn); + struct wl_event_loop *ev = wl_display_get_event_loop(display); + int events = WL_EVENT_READABLE | WL_EVENT_ERROR | WL_EVENT_HANGUP; + x11->event_source = wl_event_loop_add_fd(ev, fd, events, x11_event, x11); + if (!x11->event_source) { + wlr_log(L_ERROR, "Could not create event source"); + goto error_x11; + } + + x11->frame_timer = wl_event_loop_add_timer(ev, signal_frame, x11); + + x11->screen = xcb_setup_roots_iterator(xcb_get_setup(x11->xcb_conn)).data; + + if (!wlr_egl_init(&x11->egl, EGL_PLATFORM_X11_KHR, + x11->screen->root_visual, x11->xlib_conn)) { + goto error_event; + } + + wlr_input_device_init(&x11->keyboard_dev, WLR_INPUT_DEVICE_KEYBOARD, + NULL, "X11 keyboard", 0, 0); + wlr_keyboard_init(&x11->keyboard, NULL); + x11->keyboard_dev.keyboard = &x11->keyboard; + + wlr_input_device_init(&x11->pointer_dev, WLR_INPUT_DEVICE_POINTER, + NULL, "X11 pointer", 0, 0); + wlr_pointer_init(&x11->pointer, NULL); + x11->pointer_dev.pointer = &x11->pointer; + + x11->display_destroy.notify = handle_display_destroy; + wl_display_add_destroy_listener(display, &x11->display_destroy); + + return &x11->backend; + +error_event: + wl_event_source_remove(x11->event_source); +error_x11: + xcb_disconnect(x11->xcb_conn); + XCloseDisplay(x11->xlib_conn); + free(x11); + return NULL; +} + static void output_transform(struct wlr_output *wlr_output, enum wl_output_transform transform) { struct wlr_x11_output *output = (struct wlr_x11_output *)wlr_output; output->wlr_output.transform = transform; diff --git a/include/backend/drm/drm.h b/include/backend/drm/drm.h index 4fad9678..1f70a103 100644 --- a/include/backend/drm/drm.h +++ b/include/backend/drm/drm.h @@ -91,6 +91,7 @@ struct wlr_drm_backend { struct wl_display *display; struct wl_event_source *drm_event; + struct wl_listener display_destroy; struct wl_listener session_signal; struct wl_listener drm_invalidated; diff --git a/include/backend/libinput.h b/include/backend/libinput.h index be2557ed..6ec718c6 100644 --- a/include/backend/libinput.h +++ b/include/backend/libinput.h @@ -16,6 +16,7 @@ struct wlr_libinput_backend { struct libinput *libinput_context; struct wl_event_source *input_event; + struct wl_listener display_destroy; struct wl_listener session_signal; struct wlr_list wlr_device_lists; // list of struct wl_list diff --git a/include/backend/wayland.h b/include/backend/wayland.h index 1e8a55d2..713c291c 100644 --- a/include/backend/wayland.h +++ b/include/backend/wayland.h @@ -21,6 +21,7 @@ struct wlr_wl_backend { struct wl_list outputs; struct wlr_egl egl; size_t requested_outputs; + struct wl_listener local_display_destroy; /* remote state */ struct wl_display *remote_display; struct wl_event_source *remote_display_src; diff --git a/include/backend/x11.h b/include/backend/x11.h index f5ec56bc..31a5138b 100644 --- a/include/backend/x11.h +++ b/include/backend/x11.h @@ -51,6 +51,8 @@ struct wlr_x11_backend { // The time we last received an event xcb_timestamp_t time; + + struct wl_listener display_destroy; }; #endif From 18eb1eee3f97e169ff5fca67625452cbd81627c5 Mon Sep 17 00:00:00 2001 From: emersion Date: Fri, 8 Dec 2017 00:59:37 +0100 Subject: [PATCH 18/21] Listen to display destroy in xwayland, rename wlr_egl_free --- backend/drm/renderer.c | 4 ++-- backend/wayland/backend.c | 2 +- backend/x11/backend.c | 2 +- include/wlr/render/egl.h | 3 ++- include/wlr/xwayland.h | 1 + render/egl.c | 2 +- types/wlr_wl_shell.c | 4 ++-- xwayland/xwayland.c | 18 ++++++++++++++++-- 8 files changed, 26 insertions(+), 10 deletions(-) diff --git a/backend/drm/renderer.c b/backend/drm/renderer.c index 87064914..8a8d7d1f 100644 --- a/backend/drm/renderer.c +++ b/backend/drm/renderer.c @@ -39,7 +39,7 @@ bool wlr_drm_renderer_init(struct wlr_drm_backend *drm, return true; error_egl: - wlr_egl_free(&renderer->egl); + wlr_egl_finish(&renderer->egl); error_gbm: gbm_device_destroy(renderer->gbm); return false; @@ -51,7 +51,7 @@ void wlr_drm_renderer_finish(struct wlr_drm_renderer *renderer) { } wlr_renderer_destroy(renderer->wlr_rend); - wlr_egl_free(&renderer->egl); + wlr_egl_finish(&renderer->egl); gbm_device_destroy(renderer->gbm); } diff --git a/backend/wayland/backend.c b/backend/wayland/backend.c index cb241490..2bf155a5 100644 --- a/backend/wayland/backend.c +++ b/backend/wayland/backend.c @@ -83,7 +83,7 @@ static void wlr_wl_backend_destroy(struct wlr_backend *_backend) { free(backend->seat_name); wl_event_source_remove(backend->remote_display_src); - wlr_egl_free(&backend->egl); + wlr_egl_finish(&backend->egl); if (backend->seat) wl_seat_destroy(backend->seat); if (backend->shm) wl_shm_destroy(backend->shm); if (backend->shell) zxdg_shell_v6_destroy(backend->shell); diff --git a/backend/x11/backend.c b/backend/x11/backend.c index a4ca55b0..6c75405c 100644 --- a/backend/x11/backend.c +++ b/backend/x11/backend.c @@ -247,7 +247,7 @@ static void wlr_x11_backend_destroy(struct wlr_backend *backend) { wl_list_remove(&x11->display_destroy.link); wl_event_source_remove(x11->frame_timer); - wlr_egl_free(&x11->egl); + wlr_egl_finish(&x11->egl); xcb_disconnect(x11->xcb_conn); free(x11); diff --git a/include/wlr/render/egl.h b/include/wlr/render/egl.h index 9ab4d9ce..67a81f37 100644 --- a/include/wlr/render/egl.h +++ b/include/wlr/render/egl.h @@ -4,6 +4,7 @@ #include #include #include +#include struct wlr_egl { EGLDisplay display; @@ -27,7 +28,7 @@ bool wlr_egl_init(struct wlr_egl *egl, EGLenum platform, EGLint visual_id, void * Frees all related egl resources, makes the context not-current and * unbinds a bound wayland display. */ -void wlr_egl_free(struct wlr_egl *egl); +void wlr_egl_finish(struct wlr_egl *egl); /** * Binds the given display to the egl instance. diff --git a/include/wlr/xwayland.h b/include/wlr/xwayland.h index 792d2b88..455b1026 100644 --- a/include/wlr/xwayland.h +++ b/include/wlr/xwayland.h @@ -24,6 +24,7 @@ struct wlr_xwayland { struct wl_event_source *sigusr1_source; struct wl_listener destroy_listener; + struct wl_listener display_destroy; struct wlr_xwm *xwm; struct wlr_xwayland_cursor *cursor; diff --git a/render/egl.c b/render/egl.c index 22d58df2..e895df8d 100644 --- a/render/egl.c +++ b/render/egl.c @@ -141,7 +141,7 @@ error: return false; } -void wlr_egl_free(struct wlr_egl *egl) { +void wlr_egl_finish(struct wlr_egl *egl) { eglMakeCurrent(EGL_NO_DISPLAY, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT); if (egl->wl_display && eglUnbindWaylandDisplayWL) { eglUnbindWaylandDisplayWL(egl->display, egl->wl_display); diff --git a/types/wlr_wl_shell.c b/types/wlr_wl_shell.c index 78154245..5476ddb6 100644 --- a/types/wlr_wl_shell.c +++ b/types/wlr_wl_shell.c @@ -601,8 +601,8 @@ void wlr_wl_shell_destroy(struct wlr_wl_shell *wlr_wl_shell) { wl_list_remove(&wlr_wl_shell->display_destroy.link); struct wl_resource *resource = NULL, *temp = NULL; wl_resource_for_each_safe(resource, temp, &wlr_wl_shell->wl_resources) { - struct wl_list *link = wl_resource_get_link(resource); - wl_list_remove(link); + // shell_destroy will remove the resource from the list + wl_resource_destroy(resource); } // TODO: destroy surfaces wl_global_destroy(wlr_wl_shell->wl_global); diff --git a/xwayland/xwayland.c b/xwayland/xwayland.c index ecec785c..778b959d 100644 --- a/xwayland/xwayland.c +++ b/xwayland/xwayland.c @@ -147,6 +147,7 @@ static void wlr_xwayland_finish(struct wlr_xwayland *wlr_xwayland) { if (!wlr_xwayland || wlr_xwayland->display == -1) { return; } + if (wlr_xwayland->client) { wl_list_remove(&wlr_xwayland->destroy_listener.link); wl_client_destroy(wlr_xwayland->client); @@ -164,6 +165,8 @@ static void wlr_xwayland_finish(struct wlr_xwayland *wlr_xwayland) { safe_close(wlr_xwayland->wm_fd[0]); safe_close(wlr_xwayland->wm_fd[1]); + wl_list_remove(&wlr_xwayland->display_destroy.link); + unlink_display_sockets(wlr_xwayland->display); wlr_xwayland->display = -1; unsetenv("DISPLAY"); @@ -173,6 +176,12 @@ static void wlr_xwayland_finish(struct wlr_xwayland *wlr_xwayland) { */ } +static void handle_display_destroy(struct wl_listener *listener, void *data) { + struct wlr_xwayland *wlr_xwayland = + wl_container_of(listener, wlr_xwayland, display_destroy); + wlr_xwayland_destroy(wlr_xwayland); +} + static int xserver_handle_ready(int signal_number, void *data) { struct wlr_xwayland *wlr_xwayland = data; @@ -227,6 +236,9 @@ static bool wlr_xwayland_init(struct wlr_xwayland *wlr_xwayland, wl_signal_init(&wlr_xwayland->events.new_surface); wl_signal_init(&wlr_xwayland->events.ready); + wlr_xwayland->display_destroy.notify = handle_display_destroy; + wl_display_add_destroy_listener(wl_display, &wlr_xwayland->display_destroy); + wlr_xwayland->display = open_display_sockets(wlr_xwayland->x_fd); if (wlr_xwayland->display < 0) { wlr_xwayland_finish(wlr_xwayland); @@ -253,10 +265,12 @@ static bool wlr_xwayland_init(struct wlr_xwayland *wlr_xwayland, wlr_xwayland->wl_fd[0] = -1; /* not ours anymore */ wlr_xwayland->destroy_listener.notify = xwayland_destroy_event; - wl_client_add_destroy_listener(wlr_xwayland->client, &wlr_xwayland->destroy_listener); + wl_client_add_destroy_listener(wlr_xwayland->client, + &wlr_xwayland->destroy_listener); struct wl_event_loop *loop = wl_display_get_event_loop(wl_display); - wlr_xwayland->sigusr1_source = wl_event_loop_add_signal(loop, SIGUSR1, xserver_handle_ready, wlr_xwayland); + wlr_xwayland->sigusr1_source = wl_event_loop_add_signal(loop, SIGUSR1, + xserver_handle_ready, wlr_xwayland); if ((wlr_xwayland->pid = fork()) == 0) { /* Double-fork, but we need to forward SIGUSR1 once Xserver(1) From b06535718dd61d99ca904e4dbb503b8857456c46 Mon Sep 17 00:00:00 2001 From: emersion Date: Fri, 8 Dec 2017 01:38:45 +0100 Subject: [PATCH 19/21] Do not destroy wl_client in wlr_xwayland_finish --- include/wlr/xwayland.h | 2 +- xwayland/xwayland.c | 53 +++++++++++++++++++++++------------------- xwayland/xwm.c | 9 +++++++ 3 files changed, 39 insertions(+), 25 deletions(-) diff --git a/include/wlr/xwayland.h b/include/wlr/xwayland.h index 455b1026..f6b43851 100644 --- a/include/wlr/xwayland.h +++ b/include/wlr/xwayland.h @@ -23,7 +23,7 @@ struct wlr_xwayland { time_t server_start; struct wl_event_source *sigusr1_source; - struct wl_listener destroy_listener; + struct wl_listener client_destroy; struct wl_listener display_destroy; struct wlr_xwm *xwm; struct wlr_xwayland_cursor *cursor; diff --git a/xwayland/xwayland.c b/xwayland/xwayland.c index 778b959d..a4b79e73 100644 --- a/xwayland/xwayland.c +++ b/xwayland/xwayland.c @@ -125,39 +125,26 @@ static void exec_xwayland(struct wlr_xwayland *wlr_xwayland) { execvp("Xwayland", argv); } -static bool wlr_xwayland_init(struct wlr_xwayland *wlr_xwayland, - struct wl_display *wl_display, struct wlr_compositor *compositor); -static void wlr_xwayland_finish(struct wlr_xwayland *wlr_xwayland); - -static void xwayland_destroy_event(struct wl_listener *listener, void *data) { - struct wlr_xwayland *wlr_xwayland = wl_container_of(listener, wlr_xwayland, destroy_listener); - - /* don't call client destroy */ - wlr_xwayland->client = NULL; - wl_list_remove(&wlr_xwayland->destroy_listener.link); - wlr_xwayland_finish(wlr_xwayland); - - if (time(NULL) - wlr_xwayland->server_start > 5) { - wlr_xwayland_init(wlr_xwayland, wlr_xwayland->wl_display, - wlr_xwayland->compositor); - } -} - static void wlr_xwayland_finish(struct wlr_xwayland *wlr_xwayland) { if (!wlr_xwayland || wlr_xwayland->display == -1) { return; } + if (wlr_xwayland->cursor != NULL) { + free(wlr_xwayland->cursor); + } + + xwm_destroy(wlr_xwayland->xwm); + if (wlr_xwayland->client) { - wl_list_remove(&wlr_xwayland->destroy_listener.link); - wl_client_destroy(wlr_xwayland->client); + wl_list_remove(&wlr_xwayland->client_destroy.link); + // TODO: this segfaults in /usr/lib/libEGL_mesa.so.0 + //wl_client_destroy(wlr_xwayland->client); } if (wlr_xwayland->sigusr1_source) { wl_event_source_remove(wlr_xwayland->sigusr1_source); } - xwm_destroy(wlr_xwayland->xwm); - safe_close(wlr_xwayland->x_fd[0]); safe_close(wlr_xwayland->x_fd[1]); safe_close(wlr_xwayland->wl_fd[0]); @@ -176,6 +163,24 @@ static void wlr_xwayland_finish(struct wlr_xwayland *wlr_xwayland) { */ } +static bool wlr_xwayland_init(struct wlr_xwayland *wlr_xwayland, + struct wl_display *wl_display, struct wlr_compositor *compositor); + +static void handle_client_destroy(struct wl_listener *listener, void *data) { + struct wlr_xwayland *wlr_xwayland = + wl_container_of(listener, wlr_xwayland, client_destroy); + + /* don't call client destroy */ + wlr_xwayland->client = NULL; + wl_list_remove(&wlr_xwayland->client_destroy.link); + wlr_xwayland_finish(wlr_xwayland); + + if (time(NULL) - wlr_xwayland->server_start > 5) { + wlr_xwayland_init(wlr_xwayland, wlr_xwayland->wl_display, + wlr_xwayland->compositor); + } +} + static void handle_display_destroy(struct wl_listener *listener, void *data) { struct wlr_xwayland *wlr_xwayland = wl_container_of(listener, wlr_xwayland, display_destroy); @@ -264,9 +269,9 @@ static bool wlr_xwayland_init(struct wlr_xwayland *wlr_xwayland, wlr_xwayland->wl_fd[0] = -1; /* not ours anymore */ - wlr_xwayland->destroy_listener.notify = xwayland_destroy_event; + wlr_xwayland->client_destroy.notify = handle_client_destroy; wl_client_add_destroy_listener(wlr_xwayland->client, - &wlr_xwayland->destroy_listener); + &wlr_xwayland->client_destroy); struct wl_event_loop *loop = wl_display_get_event_loop(wl_display); wlr_xwayland->sigusr1_source = wl_event_loop_add_signal(loop, SIGUSR1, diff --git a/xwayland/xwm.c b/xwayland/xwm.c index 562f2052..3250a97d 100644 --- a/xwayland/xwm.c +++ b/xwayland/xwm.c @@ -1045,6 +1045,12 @@ void xwm_destroy(struct wlr_xwm *xwm) { if (xwm->cursor) { xcb_free_cursor(xwm->xcb_conn, xwm->cursor); } + if (xwm->colormap) { + xcb_free_colormap(xwm->xcb_conn, xwm->colormap); + } + if (xwm->window) { + xcb_destroy_window(xwm->xcb_conn, xwm->window); + } if (xwm->event_source) { wl_event_source_remove(xwm->event_source); } @@ -1052,6 +1058,9 @@ void xwm_destroy(struct wlr_xwm *xwm) { wl_list_for_each_safe(xsurface, tmp, &xwm->surfaces, link) { wlr_xwayland_surface_destroy(xsurface); } + wl_list_for_each_safe(xsurface, tmp, &xwm->unpaired_surfaces, link) { + wlr_xwayland_surface_destroy(xsurface); + } wl_list_remove(&xwm->compositor_surface_create.link); xcb_disconnect(xwm->xcb_conn); From e30b943fbd33c71f633deaf3f55279d30f214929 Mon Sep 17 00:00:00 2001 From: emersion Date: Fri, 8 Dec 2017 10:58:36 +0100 Subject: [PATCH 20/21] Destroy xwayland client when possible --- xwayland/xwayland.c | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/xwayland/xwayland.c b/xwayland/xwayland.c index a4b79e73..59851898 100644 --- a/xwayland/xwayland.c +++ b/xwayland/xwayland.c @@ -138,8 +138,7 @@ static void wlr_xwayland_finish(struct wlr_xwayland *wlr_xwayland) { if (wlr_xwayland->client) { wl_list_remove(&wlr_xwayland->client_destroy.link); - // TODO: this segfaults in /usr/lib/libEGL_mesa.so.0 - //wl_client_destroy(wlr_xwayland->client); + wl_client_destroy(wlr_xwayland->client); } if (wlr_xwayland->sigusr1_source) { wl_event_source_remove(wlr_xwayland->sigusr1_source); @@ -170,12 +169,14 @@ static void handle_client_destroy(struct wl_listener *listener, void *data) { struct wlr_xwayland *wlr_xwayland = wl_container_of(listener, wlr_xwayland, client_destroy); - /* don't call client destroy */ + // Don't call client destroy: it's being destroyed already wlr_xwayland->client = NULL; wl_list_remove(&wlr_xwayland->client_destroy.link); + wlr_xwayland_finish(wlr_xwayland); if (time(NULL) - wlr_xwayland->server_start > 5) { + wlr_log(L_INFO, "Restarting Xwayland"); wlr_xwayland_init(wlr_xwayland, wlr_xwayland->wl_display, wlr_xwayland->compositor); } @@ -184,6 +185,13 @@ static void handle_client_destroy(struct wl_listener *listener, void *data) { static void handle_display_destroy(struct wl_listener *listener, void *data) { struct wlr_xwayland *wlr_xwayland = wl_container_of(listener, wlr_xwayland, display_destroy); + + // Don't call client destroy: the display is being destroyed, it's too late + if (wlr_xwayland->client) { + wlr_xwayland->client = NULL; + wl_list_remove(&wlr_xwayland->client_destroy.link); + } + wlr_xwayland_destroy(wlr_xwayland); } From 6001235b0465ba1961f8abd66a860bcf1f14fc22 Mon Sep 17 00:00:00 2001 From: emersion Date: Fri, 8 Dec 2017 11:17:56 +0100 Subject: [PATCH 21/21] Listen to display destroy in session It's not the backend's responsibility to destroy the session anymore. --- backend/multi/backend.c | 1 - backend/session/session.c | 13 ++++++++++++- include/wlr/backend/session.h | 2 ++ 3 files changed, 14 insertions(+), 2 deletions(-) diff --git a/backend/multi/backend.c b/backend/multi/backend.c index c35303e0..fc0c5b81 100644 --- a/backend/multi/backend.c +++ b/backend/multi/backend.c @@ -35,7 +35,6 @@ static void multi_backend_destroy(struct wlr_backend *_backend) { wlr_backend_destroy(sub->backend); free(sub); } - wlr_session_destroy(backend->session); free(backend); } diff --git a/backend/session/session.c b/backend/session/session.c index 657558fd..0ed54499 100644 --- a/backend/session/session.c +++ b/backend/session/session.c @@ -58,6 +58,12 @@ out: return 1; } +static void handle_display_destroy(struct wl_listener *listener, void *data) { + struct wlr_session *session = + wl_container_of(listener, session, display_destroy); + wlr_session_destroy(session); +} + struct wlr_session *wlr_session_create(struct wl_display *disp) { struct wlr_session *session = NULL; const struct session_impl **iter; @@ -100,6 +106,9 @@ struct wlr_session *wlr_session_create(struct wl_display *disp) { goto error_mon; } + session->display_destroy.notify = handle_display_destroy; + wl_display_add_destroy_listener(disp, &session->display_destroy); + return session; error_mon: @@ -107,7 +116,7 @@ error_mon: error_udev: udev_unref(session->udev); error_session: - wlr_session_destroy(session); + session->impl->destroy(session); return NULL; } @@ -116,6 +125,8 @@ void wlr_session_destroy(struct wlr_session *session) { return; } + wl_list_remove(&session->display_destroy.link); + wl_event_source_remove(session->udev_event); udev_monitor_unref(session->mon); udev_unref(session->udev); diff --git a/include/wlr/backend/session.h b/include/wlr/backend/session.h index 5c822ea9..3242a13f 100644 --- a/include/wlr/backend/session.h +++ b/include/wlr/backend/session.h @@ -33,6 +33,8 @@ struct wlr_session { struct wl_event_source *udev_event; struct wl_list devices; + + struct wl_listener display_destroy; }; /*