From: Consolatis <35009135+Consolatis@users.noreply.github.com> Date: Sat, 4 May 2024 20:17:24 +0000 (+0200) Subject: src/main: delay startup of applications until event loop is ready X-Git-Url: https://git.mdlowis.com/?a=commitdiff_plain;h=941290fb4b5e471d5b2be1b0e56f7f8a50d8bca1;p=proto%2Flabwc.git src/main: delay startup of applications until event loop is ready This mostly allows for using scripts that trigger a labwc SIGHUP to work as expected. Before this patch, there was a race between the event loop starting up and the autostart scripts executing. One example of such a use-case is dynamically setting the keyboard layout or cursor theme via environment variable based on feedback from a dbus service like discussed in #1588. --- diff --git a/src/main.c b/src/main.c index 1fdfcd3a..539d318c 100644 --- a/src/main.c +++ b/src/main.c @@ -80,6 +80,35 @@ send_signal_to_labwc_pid(int signal) kill(pid, signal); } +struct idle_ctx { + struct server *server; + const char *primary_client; + const char *startup_cmd; +}; + +static void +idle_callback(void *data) +{ + /* Idle callbacks destroy automatically once triggerd */ + struct idle_ctx *ctx = data; + + /* Start session-manager if one is specified by -S|--session */ + if (ctx->primary_client) { + ctx->server->primary_client_pid = spawn_primary_client(ctx->primary_client); + if (ctx->server->primary_client_pid < 0) { + wlr_log(WLR_ERROR, "fatal error starting primary client: %s", + ctx->primary_client); + wl_display_terminate(ctx->server->wl_display); + return; + } + } + + session_autostart_init(ctx->server); + if (ctx->startup_cmd) { + spawn_async_no_shell(ctx->startup_cmd); + } +} + int main(int argc, char *argv[]) { @@ -177,24 +206,16 @@ main(int argc, char *argv[]) menu_init(&server); - /* Start session-manager if one is specified by -S|--session */ - if (primary_client) { - server.primary_client_pid = spawn_primary_client(primary_client); - if (server.primary_client_pid < 0) { - wlr_log(WLR_ERROR, "fatal error starting primary client: %s", - primary_client); - goto out; - } - } - - session_autostart_init(&server); - if (startup_cmd) { - spawn_async_no_shell(startup_cmd); - } + /* Delay startup of applications until the event loop is ready */ + struct idle_ctx idle_ctx = { + .server = &server, + .primary_client = primary_client, + .startup_cmd = startup_cmd + }; + wl_event_loop_add_idle(server.wl_event_loop, idle_callback, &idle_ctx); wl_display_run(server.wl_display); -out: session_shutdown(&server); server_finish(&server);