From 03f952cf4dcf45d909ce209838a3beb4304358e5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Ekl=C3=B6f?= Date: Sat, 31 Jul 2021 19:08:51 +0200 Subject: [PATCH] term: consolidate shutdown related state into an anonymous struct --- input.c | 2 +- render.c | 6 +++--- server.c | 6 ++++-- terminal.c | 58 +++++++++++++++++++++++++++++------------------------- terminal.h | 15 ++++++++------ 5 files changed, 48 insertions(+), 39 deletions(-) diff --git a/input.c b/input.c index 9cf18b53..c7077e0d 100644 --- a/input.c +++ b/input.c @@ -1471,7 +1471,7 @@ wl_pointer_leave(void *data, struct wl_pointer *wl_pointer, case TERM_SURF_BUTTON_MINIMIZE: case TERM_SURF_BUTTON_MAXIMIZE: case TERM_SURF_BUTTON_CLOSE: - if (old_moused->is_shutting_down) + if (old_moused->shutdown.in_progress) break; render_refresh_csd(old_moused); diff --git a/render.c b/render.c index 3fbbb579..d338301c 100644 --- a/render.c +++ b/render.c @@ -2340,7 +2340,7 @@ dirty_cursor(struct terminal *term) static void grid_render(struct terminal *term) { - if (term->is_shutting_down) + if (term->shutdown.in_progress) return; struct timeval start_time, start_double_buffering = {0}, stop_double_buffering = {0}; @@ -3318,7 +3318,7 @@ send_dimensions_to_client(struct terminal *term) static bool maybe_resize(struct terminal *term, int width, int height, bool force) { - if (term->is_shutting_down) + if (term->shutdown.in_progress) return false; if (!term->window->is_configured) @@ -3640,7 +3640,7 @@ fdm_hook_refresh_pending_terminals(struct fdm *fdm, void *data) tll_foreach(renderer->wayl->terms, it) { struct terminal *term = it->item; - if (unlikely(term->is_shutting_down || !term->window->is_configured)) + if (unlikely(term->shutdown.in_progress || !term->window->is_configured)) continue; bool grid = term->render.refresh.grid; diff --git a/server.c b/server.c index 06f477db..7d3d3ca0 100644 --- a/server.c +++ b/server.c @@ -334,9 +334,11 @@ shutdown: fdm_del(fdm, fd); client->fd = -1; - if (client->instance != NULL && !client->instance->terminal->is_shutting_down) + if (client->instance != NULL && + !client->instance->terminal->shutdown.in_progress) + { term_shutdown(client->instance->terminal); - else + } else client_destroy(client); return true; diff --git a/terminal.c b/terminal.c index 4a51fc1a..72d7b9f9 100644 --- a/terminal.c +++ b/terminal.c @@ -19,7 +19,7 @@ #include #define LOG_MODULE "terminal" -#define LOG_ENABLE_DBG 0 +#define LOG_ENABLE_DBG 1 #include "log.h" #include "async.h" @@ -1166,9 +1166,11 @@ term_init(const struct config *conf, struct fdm *fdm, struct reaper *reaper, .max_width = SIXEL_MAX_WIDTH, .max_height = SIXEL_MAX_HEIGHT, }, - .slave_terminate_timeout_fd = -1, - .shutdown_cb = shutdown_cb, - .shutdown_data = shutdown_data, + .shutdown = { + .terminate_timeout_fd = -1, + .cb = shutdown_cb, + .cb_data = shutdown_data, + }, .foot_exe = xstrdup(foot_exe), .cwd = xstrdup(cwd), #if defined(FOOT_IME_ENABLED) && FOOT_IME_ENABLED @@ -1243,7 +1245,7 @@ term_init(const struct config *conf, struct fdm *fdm, struct reaper *reaper, return term; err: - term->is_shutting_down = true; + term->shutdown.in_progress = true; term_destroy(term); return NULL; @@ -1263,7 +1265,7 @@ void term_window_configured(struct terminal *term) { /* Enable ptmx FDM callback */ - if (!term->is_shutting_down) { + if (!term->shutdown.in_progress) { xassert(term->window->is_configured); fdm_add(term->fdm, term->ptmx, EPOLLIN, &fdm_ptmx, term); } @@ -1336,10 +1338,11 @@ term_window_configured(struct terminal *term) static void shutdown_maybe_done(struct terminal *term) { - bool shutdown_done = term->window == NULL && term->slave_has_been_reaped; + bool shutdown_done = + term->window == NULL && term->shutdown.client_has_terminated; LOG_DBG("window=%p, slave-has-been-reaped=%d --> %s", - (void *)term->window, term->slave_has_been_reaped, + (void *)term->window, term->shutdown.client_has_terminated, (shutdown_done ? "shutdown done, calling term_destroy()" : "no action")); @@ -1347,8 +1350,8 @@ shutdown_maybe_done(struct terminal *term) if (!shutdown_done) return; - void (*cb)(void *, int) = term->shutdown_cb; - void *cb_data = term->shutdown_data; + void (*cb)(void *, int) = term->shutdown.cb; + void *cb_data = term->shutdown.cb_data; int exit_code = term_destroy(term); if (cb != NULL) @@ -1361,15 +1364,15 @@ fdm_client_terminated(struct reaper *reaper, pid_t pid, int status, void *data) struct terminal *term = data; LOG_DBG("slave (PID=%u) died", pid); - term->slave_has_been_reaped = true; - term->exit_status = status; + term->shutdown.client_has_terminated = true; + term->shutdown.exit_status = status; - if (term->slave_terminate_timeout_fd >= 0) { - fdm_del(term->fdm, term->slave_terminate_timeout_fd); - term->slave_terminate_timeout_fd = -1; + if (term->shutdown.terminate_timeout_fd >= 0) { + fdm_del(term->fdm, term->shutdown.terminate_timeout_fd); + term->shutdown.terminate_timeout_fd = -1; } - if (term->is_shutting_down) + if (term->shutdown.in_progress) shutdown_maybe_done(term); else if (!term->conf->hold_at_exit) term_shutdown(term); @@ -1418,7 +1421,7 @@ fdm_terminate_timeout(struct fdm *fdm, int fd, int events, void *data) } struct terminal *term = data; - xassert(!term->slave_has_been_reaped); + xassert(!term->shutdown.client_has_terminated); LOG_DBG("slave (PID=%u) has not terminated, sending SIGKILL (%d)", term->slave, SIGKILL); @@ -1430,10 +1433,10 @@ fdm_terminate_timeout(struct fdm *fdm, int fd, int events, void *data) bool term_shutdown(struct terminal *term) { - if (term->is_shutting_down) + if (term->shutdown.in_progress) return true; - term->is_shutting_down = true; + term->shutdown.in_progress = true; /* * Close FDs then postpone self-destruction to the next poll @@ -1456,7 +1459,7 @@ term_shutdown(struct terminal *term) else close(term->ptmx); - if (!term->slave_has_been_reaped) { + if (!term->shutdown.client_has_terminated) { LOG_DBG("initiating asynchronous terminate of slave (PID=%u)", term->slave); @@ -1475,8 +1478,8 @@ term_shutdown(struct terminal *term) return false; } - xassert(term->slave_terminate_timeout_fd < 0); - term->slave_terminate_timeout_fd = timeout_fd; + xassert(term->shutdown.terminate_timeout_fd < 0); + term->shutdown.terminate_timeout_fd = timeout_fd; } term->selection.auto_scroll.fd = -1; @@ -1539,7 +1542,8 @@ term_destroy(struct terminal *term) fdm_del(term->fdm, term->blink.fd); fdm_del(term->fdm, term->flash.fd); fdm_del(term->fdm, term->ptmx); - xassert(term->slave_terminate_timeout_fd < 0); + if (term->shutdown.terminate_timeout_fd >= 0) + fdm_del(term->fdm, term->shutdown.terminate_timeout_fd); if (term->window != NULL) { wayl_win_destroy(term->window); @@ -1635,8 +1639,8 @@ term_destroy(struct terminal *term) int exit_status; - if (term->slave_has_been_reaped) - exit_status = term->exit_status; + if (term->shutdown.client_has_terminated) + exit_status = term->shutdown.exit_status; else { LOG_DBG("initiating blocking terminate of slave (PID=%u)", term->slave); @@ -2448,11 +2452,11 @@ void term_cursor_blink_update(struct terminal *term) { bool enable = term->cursor_blink.decset || term->cursor_blink.deccsusr; - bool activate = !term->is_shutting_down && enable && term->kbd_focus; + bool activate = !term->shutdown.in_progress && enable && term->kbd_focus; LOG_DBG("decset=%d, deccsrusr=%d, focus=%d, shutting-down=%d, enable=%d, activate=%d", term->cursor_blink.decset, term->cursor_blink.deccsusr, - term->kbd_focus, term->is_shutting_down, + term->kbd_focus, term->shutdown.in_progress, enable, activate); if (activate && term->cursor_blink.fd < 0) { diff --git a/terminal.h b/terminal.h index e0441635..6e8aa264 100644 --- a/terminal.h +++ b/terminal.h @@ -593,12 +593,15 @@ struct terminal { bool ime_enabled; #endif - bool is_shutting_down; - bool slave_has_been_reaped; - int slave_terminate_timeout_fd; - int exit_status; - void (*shutdown_cb)(void *data, int exit_code); - void *shutdown_data; + struct { + bool in_progress; + bool client_has_terminated; + int terminate_timeout_fd; + int exit_status; + + void (*cb)(void *data, int exit_code); + void *cb_data; + } shutdown; char *foot_exe; char *cwd;