From 445bbe3469b057c8b518f7c787527128f724cc1b Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Wed, 30 Oct 2019 20:02:06 +0100 Subject: wayland: track multiple terminals The wayland 'term' member is gone and replaced by a list, 'terms'. This list contains all currently running terminal (windows). --- wayland.c | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) (limited to 'wayland.c') diff --git a/wayland.c b/wayland.c index 2fcf2340..ded823a5 100644 --- a/wayland.c +++ b/wayland.c @@ -123,8 +123,8 @@ output_scale(void *data, struct wl_output *wl_output, int32_t factor) mon->scale = factor; - struct terminal *term = mon->wayl->term; - if (term != NULL) { + tll_foreach(mon->wayl->terms, it) { + struct terminal *term = it->item; render_resize(term, term->width / term->scale, term->height / term->scale); wayl_reload_cursor_theme(mon->wayl, term); } @@ -394,7 +394,7 @@ fdm_wayl(struct fdm *fdm, int fd, int events, void *data) return false; } - return event_count != -1 && !wayl->term->quit; + return event_count != -1; } static bool @@ -763,14 +763,24 @@ wayl_update_cursor_surface(struct wayland *wayl, struct terminal *term) struct terminal * wayl_terminal_from_surface(struct wayland *wayl, struct wl_surface *surface) { - assert(surface == wayl->term->window->surface); - return wayl->term; + tll_foreach(wayl->terms, it) { + if (it->item->window->surface == surface) + return it->item; + } + + assert(false); + return NULL; } struct terminal * wayl_terminal_from_xdg_toplevel(struct wayland *wayl, struct xdg_toplevel *toplevel) { - assert(toplevel == wayl->term->window->xdg_toplevel); - return wayl->term; + tll_foreach(wayl->terms, it) { + if (it->item->window->xdg_toplevel == toplevel) + return it->item; + } + + assert(false); + return NULL; } -- cgit v1.3 From 2e0888bf3d62627365946688fefbdf90302e63d5 Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Wed, 30 Oct 2019 20:05:34 +0100 Subject: wayland: xdg_toplevel_close(): call term_shutdown() --- wayland.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) (limited to 'wayland.c') diff --git a/wayland.c b/wayland.c index ded823a5..63afc5b0 100644 --- a/wayland.c +++ b/wayland.c @@ -325,9 +325,7 @@ xdg_toplevel_close(void *data, struct xdg_toplevel *xdg_toplevel) struct wayland *wayl = data; struct terminal *term = wayl_terminal_from_xdg_toplevel(wayl, xdg_toplevel); LOG_DBG("xdg-toplevel: close"); - - term->quit = true; - wl_display_roundtrip(wayl->display); + term_shutdown(term); } static const struct xdg_toplevel_listener xdg_toplevel_listener = { -- cgit v1.3 From 883354ffb1856846ea002e570c1eccdfb54c09c5 Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Wed, 30 Oct 2019 20:25:16 +0100 Subject: wayland: wayl_win_destroy(): return early if win == NULL --- wayland.c | 3 +++ 1 file changed, 3 insertions(+) (limited to 'wayland.c') diff --git a/wayland.c b/wayland.c index 63afc5b0..bc813f2e 100644 --- a/wayland.c +++ b/wayland.c @@ -684,6 +684,9 @@ out: void wayl_win_destroy(struct wl_window *win) { + if (win == NULL) + return; + tll_free(win->on_outputs); if (win->search_sub_surface != NULL) wl_subsurface_destroy(win->search_sub_surface); -- cgit v1.3 From b793919aba8042fa8c3c43fa71d5f38015a5c121 Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Fri, 1 Nov 2019 19:59:39 +0100 Subject: wayland: fdm_del() now closes the FD --- wayland.c | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) (limited to 'wayland.c') diff --git a/wayland.c b/wayland.c index bc813f2e..5b2776fd 100644 --- a/wayland.c +++ b/wayland.c @@ -556,11 +556,10 @@ wayl_destroy(struct wayland *wayl) if (wayl == NULL) return; - if (wayl->kbd.repeat.fd != 0) { - fdm_del(wayl->fdm, wayl->kbd.repeat.fd); - close(wayl->kbd.repeat.fd); } + fdm_del(wayl->fdm, wayl->kbd.repeat.fd); + tll_foreach(wayl->monitors, it) { free(it->item.name); if (it->item.xdg != NULL) @@ -628,7 +627,7 @@ wayl_destroy(struct wayland *wayl) if (wayl->registry != NULL) wl_registry_destroy(wayl->registry); if (wayl->display != NULL) { - fdm_del(wayl->fdm, wl_display_get_fd(wayl->display)); + fdm_del_no_close(wayl->fdm, wl_display_get_fd(wayl->display)); wl_display_disconnect(wayl->display); } -- cgit v1.3 From 66b20972753ff705a0b081a02a969f7eb42b1c97 Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Fri, 1 Nov 2019 20:02:02 +0100 Subject: wayland: wayl_init(): call wl_display_roundtrip() when done --- wayland.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'wayland.c') diff --git a/wayland.c b/wayland.c index 5b2776fd..91c3ac28 100644 --- a/wayland.c +++ b/wayland.c @@ -514,6 +514,9 @@ wayl_init(struct fdm *fdm) } wayl->kbd.repeat.fd = timerfd_create(CLOCK_BOOTTIME, TFD_CLOEXEC | TFD_NONBLOCK); + /* All wayland initialization done - make it so */ + wl_display_roundtrip(wayl->display); + if (wayl->kbd.repeat.fd == -1) { LOG_ERRNO("failed to create keyboard repeat timer FD"); goto out; @@ -540,8 +543,6 @@ wayl_init(struct fdm *fdm) goto out; } - //wl_display_dispatch_pending(wayl->display); - //wl_display_flush(wayl->display); return wayl; out: -- cgit v1.3 From 95b7c405d464b0eb04141cbbfe077371f5a9d47c Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Fri, 1 Nov 2019 20:19:53 +0100 Subject: wayland: wayl_win_destroy(): unmap windows before destroying This will trigger e.d. keyboard_leave() and wl_pointer_leave() events, which ensures there aren't any references to the destroyed window from the global wayland struct. Call wl_display_roundtrip() to trigger those events *before* we destroy the window. --- wayland.c | 21 +++++++++++++++++++++ wayland.h | 2 ++ 2 files changed, 23 insertions(+) (limited to 'wayland.c') diff --git a/wayland.c b/wayland.c index 91c3ac28..6a1d0fae 100644 --- a/wayland.c +++ b/wayland.c @@ -639,6 +639,7 @@ struct wl_window * wayl_win_init(struct wayland *wayl) { struct wl_window *win = calloc(1, sizeof(*win)); + win->wayl = wayl; win->surface = wl_compositor_create_surface(wayl->compositor); if (win->surface == NULL) { @@ -687,6 +688,25 @@ wayl_win_destroy(struct wl_window *win) if (win == NULL) return; + /* + * First, unmap all surfaces to trigger things like + * keyboard_leave() and wl_pointer_leave(). + * + * This ensures we remove all references to *this* window from the + * global wayland struct (since it no longer has neither keyboard + * nor mouse focus). + */ + + /* Scrollback search */ + wl_surface_attach(win->search_surface, NULL, 0, 0); + wl_surface_commit(win->search_surface); + wl_display_roundtrip(win->wayl->display); + + /* Main window */ + wl_surface_attach(win->surface, NULL, 0, 0); + wl_surface_commit(win->surface); + wl_display_roundtrip(win->wayl->display); + tll_free(win->on_outputs); if (win->search_sub_surface != NULL) wl_subsurface_destroy(win->search_sub_surface); @@ -702,6 +722,7 @@ wayl_win_destroy(struct wl_window *win) xdg_surface_destroy(win->xdg_surface); if (win->surface != NULL) wl_surface_destroy(win->surface); + free(win); } diff --git a/wayland.h b/wayland.h index d0a8332f..7e7bfd4c 100644 --- a/wayland.h +++ b/wayland.h @@ -72,7 +72,9 @@ struct wl_primary { uint32_t serial; }; +struct wayland; struct wl_window { + struct wayland *wayl; struct wl_surface *surface; struct xdg_surface *xdg_surface; struct xdg_toplevel *xdg_toplevel; -- cgit v1.3 From 2e78dcc5e5cd2d5a2ae6a1f136467fc99a6dfd57 Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Fri, 1 Nov 2019 20:24:13 +0100 Subject: Don't use non-blocking FDs We use epoll() to determine when we can read/write FDs so there's absolutely no need for non-blocking. --- terminal.c | 24 +++++------------------- wayland.c | 15 +-------------- 2 files changed, 6 insertions(+), 33 deletions(-) (limited to 'wayland.c') diff --git a/terminal.c b/terminal.c index f32586ff..5dc75d7a 100644 --- a/terminal.c +++ b/terminal.c @@ -310,35 +310,21 @@ term_init(const struct config *conf, struct fdm *fdm, struct wayland *wayl, LOG_ERRNO("failed to open PTY"); goto close_fds; } - if ((flash_fd = timerfd_create(CLOCK_BOOTTIME, TFD_CLOEXEC | TFD_NONBLOCK)) == -1) { + if ((flash_fd = timerfd_create(CLOCK_BOOTTIME, TFD_CLOEXEC)) == -1) { LOG_ERRNO("failed to create flash timer FD"); goto close_fds; } - if ((blink_fd = timerfd_create(CLOCK_BOOTTIME, TFD_CLOEXEC | TFD_NONBLOCK)) == -1) { + if ((blink_fd = timerfd_create(CLOCK_BOOTTIME, TFD_CLOEXEC)) == -1) { LOG_ERRNO("failed to create blink timer FD"); goto close_fds; } - if ((delay_lower_fd = timerfd_create(CLOCK_BOOTTIME, TFD_CLOEXEC | TFD_NONBLOCK)) == -1 || - (delay_upper_fd = timerfd_create(CLOCK_BOOTTIME, TFD_CLOEXEC | TFD_NONBLOCK)) == -1) + if ((delay_lower_fd = timerfd_create(CLOCK_BOOTTIME, TFD_CLOEXEC)) == -1 || + (delay_upper_fd = timerfd_create(CLOCK_BOOTTIME, TFD_CLOEXEC)) == -1) { LOG_ERRNO("failed to create delayed rendering timer FDs"); goto close_fds; } - /* Read logic requires non-blocking mode */ - { - int fd_flags = fcntl(ptmx, F_GETFL); - if (fd_flags == -1) { - LOG_ERRNO("failed to set non blocking mode on PTY master"); - goto err; - } - - if (fcntl(ptmx, F_SETFL, fd_flags | O_NONBLOCK) == -1) { - LOG_ERRNO("failed to set non blocking mode on PTY master"); - goto err; - } - } - if (!fdm_add(fdm, ptmx, EPOLLIN, &fdm_ptmx, term)) { LOG_ERR("failed to add ptmx to FDM"); goto err; @@ -574,7 +560,7 @@ term_shutdown(struct terminal *term) * This is done by opening an event FD and adding it to the FDM. */ - int event_fd = eventfd(0, EFD_CLOEXEC | EFD_NONBLOCK); + int event_fd = eventfd(0, EFD_CLOEXEC); if (event_fd == -1) { LOG_ERRNO("failed to create terminal shutdown event FD"); return false; diff --git a/wayland.c b/wayland.c index 6a1d0fae..21ab1359 100644 --- a/wayland.c +++ b/wayland.c @@ -407,9 +407,6 @@ fdm_repeat(struct fdm *fdm, int fd, int events, void *data) wayl->kbd.repeat.fd, &expiration_count, sizeof(expiration_count)); if (ret < 0) { - if (errno == EAGAIN) - return true; - LOG_ERRNO("failed to read repeat key from repeat timer fd"); return false; } @@ -513,26 +510,16 @@ wayl_init(struct fdm *fdm) goto out; } - wayl->kbd.repeat.fd = timerfd_create(CLOCK_BOOTTIME, TFD_CLOEXEC | TFD_NONBLOCK); /* All wayland initialization done - make it so */ wl_display_roundtrip(wayl->display); + wayl->kbd.repeat.fd = timerfd_create(CLOCK_BOOTTIME, TFD_CLOEXEC); if (wayl->kbd.repeat.fd == -1) { LOG_ERRNO("failed to create keyboard repeat timer FD"); goto out; } int wl_fd = wl_display_get_fd(wayl->display); - int fd_flags = fcntl(wl_fd, F_GETFL); - if (fd_flags == -1) { - LOG_ERRNO("failed to set non blocking mode on Wayland display connection"); - goto out; - } - if (fcntl(wl_fd, F_SETFL, fd_flags | O_NONBLOCK) == -1) { - LOG_ERRNO("failed to set non blocking mode on Wayland display connection"); - goto out; - } - if (!fdm_add(fdm, wl_fd, EPOLLIN, &fdm_wayl, wayl)) { LOG_ERR("failed to register Wayland connection with the FDM"); goto out; -- cgit v1.3 From bc815a33dbe998a108e395634c308f47a6780276 Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Fri, 1 Nov 2019 20:25:08 +0100 Subject: wayland: wayl_destroy(): destroy any remaining terminals --- wayland.c | 9 +++++++++ 1 file changed, 9 insertions(+) (limited to 'wayland.c') diff --git a/wayland.c b/wayland.c index 21ab1359..97917e77 100644 --- a/wayland.c +++ b/wayland.c @@ -544,8 +544,17 @@ wayl_destroy(struct wayland *wayl) if (wayl == NULL) return; + tll_foreach(wayl->terms, it) { + static bool have_warned = false; + if (!have_warned) { + have_warned = true; + LOG_WARN("there are terminals still running"); + term_destroy(it->item); + } } + tll_free(wayl->terms); + fdm_del(wayl->fdm, wayl->kbd.repeat.fd); tll_foreach(wayl->monitors, it) { -- cgit v1.3 From 8b4ef78f7a9692c14e20a32dcf0c250f7812828a Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Fri, 1 Nov 2019 20:45:57 +0100 Subject: wayland: wayl_win_destroy(): looks like we need another roundtrip --- wayland.c | 2 ++ 1 file changed, 2 insertions(+) (limited to 'wayland.c') diff --git a/wayland.c b/wayland.c index 97917e77..3d19c4de 100644 --- a/wayland.c +++ b/wayland.c @@ -719,6 +719,8 @@ wayl_win_destroy(struct wl_window *win) if (win->surface != NULL) wl_surface_destroy(win->surface); + wl_display_roundtrip(win->wayl->display); + free(win); } -- cgit v1.3