From 6719ccca2ba4f5f28c1504ac9a56ede7987df9b2 Mon Sep 17 00:00:00 2001 From: Zedhugh Chen Date: Tue, 2 Sep 2025 01:04:50 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E8=87=AA=E5=8A=A8=E8=BF=90?= =?UTF-8?q?=E8=A1=8C=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/config.h | 5 +++++ src/main.c | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+) diff --git a/src/config.h b/src/config.h index 7d23518..09ecca9 100644 --- a/src/config.h +++ b/src/config.h @@ -172,3 +172,8 @@ const uint32_t client_name_x_padding = 10; const uint32_t status_x_padding = 4; const uint32_t status_icon_padding = 2; const uint32_t border_width = 1; + +const char *autostart_list[] = { + "gentoo-pipewire-launcher restart", + "picom -b --backend xrender", +}; diff --git a/src/main.c b/src/main.c index c4a06a3..675145e 100644 --- a/src/main.c +++ b/src/main.c @@ -40,6 +40,9 @@ static void get_xresource_config(void); static void update_status(status_t *status); static void draw_all_monitor_bars(void); static void draw_monitor_bar(monitor_t *monitor); +static void run_autostart(void); +static void run_once(const char *command); +static bool command_already_running(const char *command); static void init_xcb_event_loop(void); static void xcb_event_handler(generic_event_t *event, void *user_data); static void key_press(key_press_event_t *event); @@ -720,6 +723,34 @@ void draw_monitor_bar(monitor_t *m) { flush_xcb_connection(); } +void run_autostart(void) { + for (int i = 0; i < LENGTH(autostart_list); i++) run_once(autostart_list[i]); +} + +void run_once(const char *command) { + if (command_already_running(command)) return; + + g_spawn_command_line_async(command, NULL); +} + +bool command_already_running(const char *command) { + char shell_cmd[1024]; + const char *user = g_getenv("USER"); + snprintf(shell_cmd, sizeof(shell_cmd), "pgrep -u %s -fx '%s'", user, command); + + char *output = NULL, *error = NULL; + g_spawn_command_line_sync(shell_cmd, &output, &error, NULL, NULL); + + bool result = (error == NULL || !strlen(error)) && (output && strlen(output)); + + free(output); + free(error); + output = NULL; + error = NULL; + + return result; +} + void init_xcb_event_loop(void) { adapter = create_event_adapter(); event_adapter_set_handler(adapter, xcb_event_handler, NULL); @@ -1460,6 +1491,8 @@ int main(int argc, char *argv[]) { init_xcb_event_loop(); + run_autostart(); + g_main_loop_run(loop); cleanup(need_restart);