From 9d43adaafa6bbe63116b3feba796844169f91b25 Mon Sep 17 00:00:00 2001 From: emersion Date: Thu, 7 Dec 2017 23:44:59 +0100 Subject: Listen to display destroy in backends --- backend/x11/backend.c | 137 +++++++++++++++++++++++++++----------------------- 1 file changed, 74 insertions(+), 63 deletions(-) (limited to 'backend/x11') 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; -- cgit v1.2.3 From 18eb1eee3f97e169ff5fca67625452cbd81627c5 Mon Sep 17 00:00:00 2001 From: emersion Date: Fri, 8 Dec 2017 00:59:37 +0100 Subject: 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(-) (limited to 'backend/x11') 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) -- cgit v1.2.3