From 6111297d91faf5f2820acaa14d76d6389b469b77 Mon Sep 17 00:00:00 2001 From: Kenny Levinsen Date: Mon, 11 Nov 2024 12:16:44 +0100 Subject: [PATCH 1/4] config: Force modeset before running deferred configs Some commands require outputs to be enabled. These commands are deferred to allow outputs to be discovered, but the delayed modeset might only run some time later. Force a modeset to occur before running deferred commands. Fixes: https://github.com/swaywm/sway/issues/8433 --- include/sway/config.h | 1 + sway/desktop/output.c | 14 +++++++++----- sway/main.c | 1 + 3 files changed, 11 insertions(+), 5 deletions(-) diff --git a/include/sway/config.h b/include/sway/config.h index 71721393e2..013853c85c 100644 --- a/include/sway/config.h +++ b/include/sway/config.h @@ -705,6 +705,7 @@ struct output_config *find_output_config(struct sway_output *output); void free_output_config(struct output_config *oc); void request_modeset(void); +void force_modeset(void); bool spawn_swaybg(void); diff --git a/sway/desktop/output.c b/sway/desktop/output.c index 8682cc6564..5c2332ceb3 100644 --- a/sway/desktop/output.c +++ b/sway/desktop/output.c @@ -408,6 +408,14 @@ void request_modeset(void) { } } +void force_modeset(void) { + if (server.delayed_modeset != NULL) { + wl_event_source_remove(server.delayed_modeset); + server.delayed_modeset = NULL; + } + apply_stored_output_configs(); +} + static void begin_destroy(struct sway_output *output) { if (output->enabled) { output_disable(output); @@ -493,12 +501,8 @@ static void handle_request_state(struct wl_listener *listener, void *data) { // We do not expect or support any other changes here assert(committed == 0); store_output_config(oc); - apply_stored_output_configs(); - if (server.delayed_modeset != NULL) { - wl_event_source_remove(server.delayed_modeset); - server.delayed_modeset = NULL; - } + force_modeset(); } static unsigned int last_headless_num = 0; diff --git a/sway/main.c b/sway/main.c index 1c4939aa0a..165ccc095c 100644 --- a/sway/main.c +++ b/sway/main.c @@ -361,6 +361,7 @@ int main(int argc, char **argv) { } config->active = true; + force_modeset(); load_swaybars(); run_deferred_commands(); run_deferred_bindings(); From a2c73c9b8b426417e0253ea0420b98298af1c963 Mon Sep 17 00:00:00 2001 From: Kenny Levinsen Date: Sun, 17 Nov 2024 12:38:32 +0100 Subject: [PATCH 2/4] ipc-server: Force modeset if needed after executing commands IPC clients generally expect executed commands to have taken effect when the command completes, while delayed modeset means that it can take several milliseconds more before e.g. an output is enabled. However, modesetting on every output command in the IPC call could on systems with already slow modesetting behavior lead to an unresponsive system for a not insignificant period of time. To strike a balance, force modeset once all the commands of this IPC call have executed if a modeset is pending. --- include/sway/config.h | 1 + sway/desktop/output.c | 4 ++++ sway/ipc-server.c | 6 ++++++ 3 files changed, 11 insertions(+) diff --git a/include/sway/config.h b/include/sway/config.h index 013853c85c..bb770c6f7f 100644 --- a/include/sway/config.h +++ b/include/sway/config.h @@ -706,6 +706,7 @@ void free_output_config(struct output_config *oc); void request_modeset(void); void force_modeset(void); +bool modeset_is_pending(void); bool spawn_swaybg(void); diff --git a/sway/desktop/output.c b/sway/desktop/output.c index 5c2332ceb3..c7ad8f9372 100644 --- a/sway/desktop/output.c +++ b/sway/desktop/output.c @@ -408,6 +408,10 @@ void request_modeset(void) { } } +bool modeset_is_pending(void) { + return server.delayed_modeset != NULL; +} + void force_modeset(void) { if (server.delayed_modeset != NULL) { wl_event_source_remove(server.delayed_modeset); diff --git a/sway/ipc-server.c b/sway/ipc-server.c index 7f353c0ec3..b934bb568d 100644 --- a/sway/ipc-server.c +++ b/sway/ipc-server.c @@ -648,6 +648,12 @@ void ipc_client_handle_command(struct ipc_client *client, uint32_t payload_lengt } list_t *res_list = execute_command(buf, NULL, NULL); + if (modeset_is_pending()) { + // IPC expects commands to have taken immediate effect, so we need + // to force a modeset after output commands. We do a single modeset + // here to avoid modesetting for every output command in sequence. + force_modeset(); + } transaction_commit_dirty(); char *json = cmd_results_to_json(res_list); int length = strlen(json); From fec3da7d58c06421355e4b3dd1dcf291fd20627c Mon Sep 17 00:00:00 2001 From: mtvare6 Date: Wed, 13 Nov 2024 22:32:00 +0530 Subject: [PATCH 3/4] commands/include: handle many files in single line i3 supports including multiple files in a single line, whereas sway limits to single file per line. --- sway/commands/include.c | 6 +++--- sway/sway.5.scd | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/sway/commands/include.c b/sway/commands/include.c index d4c14c35f6..e0d0c06408 100644 --- a/sway/commands/include.c +++ b/sway/commands/include.c @@ -3,12 +3,12 @@ struct cmd_results *cmd_include(int argc, char **argv) { struct cmd_results *error = NULL; - if ((error = checkarg(argc, "include", EXPECTED_EQUAL_TO, 1))) { + if ((error = checkarg(argc, "include", EXPECTED_AT_LEAST, 1))) { return error; } - + char *files = join_args(argv, argc); // We don't care if the included config(s) fails to load. - load_include_configs(argv[0], config, &config->swaynag_config_errors); + load_include_configs(files, config, &config->swaynag_config_errors); return cmd_results_new(CMD_SUCCESS, NULL); } diff --git a/sway/sway.5.scd b/sway/sway.5.scd index bb958ebfd8..50de552f03 100644 --- a/sway/sway.5.scd +++ b/sway/sway.5.scd @@ -66,8 +66,8 @@ The following commands may only be used in the configuration file. *default_orientation* horizontal|vertical|auto Sets the default container layout for tiled containers. -*include* - Includes another file from _path_. _path_ can be either a full path or a +*include* + Include files from _paths_. _paths_ can include either a full path or a path relative to the parent config, and expands shell syntax (see *wordexp*(3) for details). The same include file can only be included once; subsequent attempts will be ignored. From 5312376077254d6431bb92ba22de3840b9933f67 Mon Sep 17 00:00:00 2001 From: Kenny Levinsen Date: Sun, 17 Nov 2024 21:43:31 +0100 Subject: [PATCH 4/4] desktop/output: Clear repaint timer earlier in destroy The teardown of a sway_output is split in two: begin_destroy and output_destroy. The former clears some state such as NULL'ing the reference to wlr_output, while the latter frees the struct and its remaining resources. If an output is destroyed while a repaint timer is pending, future frame callbacks will no longer occur as the listener is torn down in begin_destroy, but the repaint timer is not torn down and may still fire until output_destroy is hit. As begin_destroy cleared the reference to wlr_output, this leads to a NULL-pointer dereference. Tear down the repaint timer in begin_destroy as there is no need for it. Fixes: fdc4318ac66d ("desktop/output: Clear frame_pending even output is disabled") --- sway/desktop/output.c | 3 +++ sway/tree/output.c | 1 - 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/sway/desktop/output.c b/sway/desktop/output.c index c7ad8f9372..607eca2c37 100644 --- a/sway/desktop/output.c +++ b/sway/desktop/output.c @@ -440,6 +440,9 @@ static void begin_destroy(struct sway_output *output) { output->wlr_output->data = NULL; output->wlr_output = NULL; + wl_event_source_remove(output->repaint_timer); + output->repaint_timer = NULL; + request_modeset(); } diff --git a/sway/tree/output.c b/sway/tree/output.c index 44b941ca22..65d9e3e718 100644 --- a/sway/tree/output.c +++ b/sway/tree/output.c @@ -273,7 +273,6 @@ void output_destroy(struct sway_output *output) { destroy_scene_layers(output); list_free(output->workspaces); list_free(output->current.workspaces); - wl_event_source_remove(output->repaint_timer); wlr_color_transform_unref(output->color_transform); free(output); }