diff --git a/src/config/parse_config.h b/src/config/parse_config.h index d7b32234..31447759 100644 --- a/src/config/parse_config.h +++ b/src/config/parse_config.h @@ -2943,7 +2943,7 @@ void set_default_key_bindings(Config *config) { config->key_bindings_count += default_key_bindings_count; } -void parse_config(void) { +void parse_config(const char *cli_config_file) { char filename[1024]; @@ -2989,30 +2989,34 @@ void parse_config(void) { create_config_keymap(); - // 获取 MANGOCONFIG 环境变量 - const char *mangoconfig = getenv("MANGOCONFIG"); - - // 如果 MANGOCONFIG 环境变量不存在或为空,则使用 HOME 环境变量 - if (!mangoconfig || mangoconfig[0] == '\0') { - // 获取当前用户家目录 - const char *homedir = getenv("HOME"); - if (!homedir) { - // 如果获取失败,则无法继续 - return; - } - // 构建日志文件路径 - snprintf(filename, sizeof(filename), "%s/.config/mango/config.conf", - homedir); - - // 检查文件是否存在 - if (access(filename, F_OK) != 0) { - // 如果文件不存在,则使用 /etc/mango/config.conf - snprintf(filename, sizeof(filename), "%s/mango/config.conf", - SYSCONFDIR); - } + if (cli_config_path != NULL) { + snprintf(filename, sizeof(filename), "%s", cli_config_path); } else { - // 使用 MANGOCONFIG 环境变量作为配置文件夹路径 - snprintf(filename, sizeof(filename), "%s/config.conf", mangoconfig); + // 获取 MANGOCONFIG 环境变量 + const char *mangoconfig = getenv("MANGOCONFIG"); + + // 如果 MANGOCONFIG 环境变量不存在或为空,则使用 HOME 环境变量 + if (!mangoconfig || mangoconfig[0] == '\0') { + // 获取当前用户家目录 + const char *homedir = getenv("HOME"); + if (!homedir) { + // 如果获取失败,则无法继续 + return; + } + // 构建日志文件路径 + snprintf(filename, sizeof(filename), "%s/.config/mango/config.conf", + homedir); + + // 检查文件是否存在 + if (access(filename, F_OK) != 0) { + // 如果文件不存在,则使用 /etc/mango/config.conf + snprintf(filename, sizeof(filename), "%s/mango/config.conf", + SYSCONFDIR); + } + } else { + // 使用 MANGOCONFIG 环境变量作为配置文件夹路径 + snprintf(filename, sizeof(filename), "%s/config.conf", mangoconfig); + } } set_value_default(); diff --git a/src/mango.c b/src/mango.c index 540a0257..cffad0d0 100644 --- a/src/mango.c +++ b/src/mango.c @@ -628,7 +628,7 @@ static void setgaps(int oh, int ov, int ih, int iv); static void setmon(Client *c, Monitor *m, unsigned int newtags, bool focus); static void setpsel(struct wl_listener *listener, void *data); static void setsel(struct wl_listener *listener, void *data); -static void setup(void); +static void setup(const char *config_file); static void startdrag(struct wl_listener *listener, void *data); static void unlocksession(struct wl_listener *listener, void *data); @@ -4852,11 +4852,11 @@ void create_output(struct wlr_backend *backend, void *data) { #endif } -void setup(void) { +void setup(const char *config_file) { setenv("XCURSOR_SIZE", "24", 1); setenv("XDG_CURRENT_DESKTOP", "mango", 1); - parse_config(); + parse_config(config_file); init_baked_points(); int drm_fd, i, sig[] = {SIGCHLD, SIGINT, SIGTERM, SIGPIPE}; @@ -5858,11 +5858,14 @@ static void setgeometrynotify(struct wl_listener *listener, void *data) { int main(int argc, char *argv[]) { char *startup_cmd = NULL; + char *config_file = NULL; int c; - while ((c = getopt(argc, argv, "s:hdv")) != -1) { + while ((c = getopt(argc, argv, "s:c:hdv")) != -1) { if (c == 's') startup_cmd = optarg; + else if (c == 'c') + config_file = optarg; else if (c == 'd') log_level = WLR_DEBUG; else if (c == 'v') @@ -5878,11 +5881,11 @@ int main(int argc, char *argv[]) { */ if (!getenv("XDG_RUNTIME_DIR")) die("XDG_RUNTIME_DIR must be set"); - setup(); + setup(config_file); run(startup_cmd); cleanup(); return EXIT_SUCCESS; usage: - die("Usage: %s [-v] [-d] [-s startup command]", argv[0]); + die("Usage: %s [-v] [-d] [-c config_file] [-s startup command]", argv[0]); }