添加自启动程序支持

This commit is contained in:
2026-01-31 23:30:10 +08:00
parent a748159dcd
commit 5dd042384d
2 changed files with 43 additions and 0 deletions

View File

@@ -96,3 +96,10 @@ static const layout_t layout_list[] = {
{.symbol = "[M]", .arrange = monocle}, {.symbol = "[M]", .arrange = monocle},
{.symbol = "><=", .arrange = nullptr}, {.symbol = "><=", .arrange = nullptr},
}; };
static const char *const autostart_list[] = {
"gentoo-pipewire-launcher restart",
"picom -b --backend xrender",
"fcitx5 -d",
nullptr,
};

View File

@@ -5,6 +5,7 @@
#include <glibconfig.h> #include <glibconfig.h>
#include <signal.h> #include <signal.h>
#include <stdint.h> #include <stdint.h>
#include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <unistd.h> #include <unistd.h>
@@ -46,6 +47,9 @@ static void wm_setup(void);
static void wm_get_xres_config(void); static void wm_get_xres_config(void);
static void wm_init_color_set(void); static void wm_init_color_set(void);
static void wm_setup_keybindings(void); static void wm_setup_keybindings(void);
static void wm_run_autostart(const char *const commands[]);
static void run_once(const char *command);
static bool command_already_running(const char *command);
wm_t wm; wm_t wm;
@@ -429,6 +433,36 @@ void wm_setup_keybindings(void) {
xwindow_grab_keys(wm.screen->root, key_list, countof(key_list)); xwindow_grab_keys(wm.screen->root, key_list, countof(key_list));
} }
/**
* @brief 允许自启动程序
* @param commands 命令字符串数组(以 nullptr 结尾)
*/
void wm_run_autostart(const char *const commands[]) {
for (int i = 0; commands[i]; i++) run_once(commands[i]);
}
void run_once(const char *command) {
if (command_already_running(command)) return;
g_spawn_command_line_async(command, nullptr);
}
bool command_already_running(const char *command) {
char shell_cmd[1024];
const char *user = getenv("USER");
snprintf(shell_cmd, sizeof(shell_cmd), "pgrep -u %s -fx '%s'", user, command);
char *output = nullptr;
char *error = nullptr;
g_spawn_command_line_sync(shell_cmd, &output, &error, nullptr, nullptr);
bool result =
(error == nullptr || !strlen(error)) && (output && strlen(output));
p_delete(&output);
p_delete(&error);
return result;
}
void wm_restart(void) { void wm_restart(void) {
wm.need_restart = true; wm.need_restart = true;
if (g_main_loop_is_running(wm.loop)) { if (g_main_loop_is_running(wm.loop)) {
@@ -552,6 +586,8 @@ int main(int argc, char *argv[]) {
debug_show_monitor_list(); debug_show_monitor_list();
wm_run_autostart(autostart_list);
if (wm.loop == nullptr) { if (wm.loop == nullptr) {
wm.loop = g_main_loop_new(nullptr, FALSE); wm.loop = g_main_loop_new(nullptr, FALSE);
g_main_loop_run(wm.loop); g_main_loop_run(wm.loop);