From fd92de24ffcbe6ccae3959ea299868284a798289 Mon Sep 17 00:00:00 2001 From: speedie Date: Tue, 6 Jun 2023 17:59:03 +0200 Subject: [PATCH] allow overriding path to theme file and binds file in the regular config file, and allow overriding config file path in options.h --- docs/spmenu.conf | 2 ++ libs/argv.c | 18 +++++++++--------- libs/conf/config.c | 20 ++++++++++++++------ libs/theme/theme.c | 6 +++--- options.h | 7 +++++++ spmenu.c | 7 ------- 6 files changed, 35 insertions(+), 25 deletions(-) diff --git a/docs/spmenu.conf b/docs/spmenu.conf index 0894074..00c4b13 100644 --- a/docs/spmenu.conf +++ b/docs/spmenu.conf @@ -203,6 +203,8 @@ spmenu = { global = 1; // Read global .Xresources colors, programs like Pywal use this. (*.color0, *.color1, etc.) (0/1) theme = 1; // Load theme (~/.config/spmenu/theme.conf) on runtime binds = 1; // Load binds (~/.config/spmenu/binds.conf) on runtime + themefile = "NULL"; // Path to theme file to load on runtime. NULL means default. + bindsfile = "NULL"; // Path to binds file to load on runtime. NULL means default. } ); /* Input options */ diff --git a/libs/argv.c b/libs/argv.c index 033249b..c41004e 100644 --- a/libs/argv.c +++ b/libs/argv.c @@ -28,19 +28,19 @@ void readargs(int argc, char *argv[]) { #if USECONFIG } else if (!strcmp(argv[j], "-cf") || (!strcmp(argv[j], "--config-file"))) { // specify a config file if (argv[j+1]) { - argconf = argv[++j]; + configfile = argv[++j]; } else { die("This argument requires a second argument."); } } else if (!strcmp(argv[j], "-bf") || (!strcmp(argv[j], "--bind-file"))) { // specify a bind file if (argv[j+1]) { - argbinds = argv[++j]; + bindsfile = argv[++j]; } else { die("This argument requires a second argument."); } } else if (!strcmp(argv[j], "-tm") || (!strcmp(argv[j], "--theme"))) { // specify a theme if (argv[j+1]) { - argtheme = argv[++j]; + themefile = argv[++j]; } else { die("This argument requires a second argument."); } @@ -251,13 +251,13 @@ void readargs(int argc, char *argv[]) { #if USECONFIG || !strcmp(argv[i], "-cf") || !strcmp(argv[i], "--config-file") - || (argconf && !strcmp(argv[i], argconf)) + || (configfile && !strcmp(argv[i], configfile)) || !strcmp(argv[i], "-tm") || !strcmp(argv[i], "--theme") - || (argtheme && !strcmp(argv[i], argtheme)) + || (themefile && !strcmp(argv[i], themefile)) || !strcmp(argv[i], "-bf") || !strcmp(argv[i], "--bind-file") - || (argbinds && !strcmp(argv[i], argbinds)) + || (bindsfile && !strcmp(argv[i], bindsfile)) #endif )) continue; @@ -485,13 +485,13 @@ void readargs(int argc, char *argv[]) { #if USECONFIG || !strcmp(argv[i], "-cf") || !strcmp(argv[i], "--config-file") - || (argconf && !strcmp(argv[i], argconf)) + || (configfile && !strcmp(argv[i], configfile)) || !strcmp(argv[i], "-tm") || !strcmp(argv[i], "--theme") - || (argtheme && !strcmp(argv[i], argtheme)) + || (themefile && !strcmp(argv[i], themefile)) || !strcmp(argv[i], "-bf") || !strcmp(argv[i], "--bind-file") - || (argbinds && !strcmp(argv[i], argbinds)) + || (bindsfile && !strcmp(argv[i], bindsfile)) #endif )) continue; diff --git a/libs/conf/config.c b/libs/conf/config.c index 6282616..773c175 100644 --- a/libs/conf/config.c +++ b/libs/conf/config.c @@ -10,7 +10,7 @@ void conf_init(void) { const char *dest; // get path for configuration file - if (!argconf) { + if (!configfile) { if (!(xdg_conf = getenv("XDG_CONFIG_HOME"))) { // ~/.config/spmenu/spmenu.conf home = getenv("HOME"); @@ -31,11 +31,11 @@ void conf_init(void) { sprintf(cfgfile, "%s/%s", xdg_conf, "spmenu/spmenu.conf"); } } else { // custom config path - if (!(cfgfile = malloc(snprintf(NULL, 0, "%s", argconf) + 1))) { + if (!(cfgfile = malloc(snprintf(NULL, 0, "%s", configfile) + 1))) { die("spmenu: failed to malloc cfgfile"); } - sprintf(cfgfile, "%s", argconf); + sprintf(cfgfile, "%s", configfile); } // don't bother trying to load if it doesn't exist. @@ -424,6 +424,14 @@ void conf_init(void) { config_setting_lookup_int(conf, "binds", &loadbinds); // spmenu.file.binds config_setting_lookup_int(conf, "global", &globalcolors); // spmenu.file.global config_setting_lookup_int(conf, "xresources", &xresources); // spmenu.file.xresources + + if (config_setting_lookup_string(conf, "themefile", &dest)) { + themefile = strdup(dest); + } + + if (config_setting_lookup_string(conf, "bindsfile", &dest)) { + bindsfile = strdup(dest); + } } } @@ -716,7 +724,7 @@ void conf_init(void) { theme_load(); } - if (!argbinds) { + if (!bindsfile || !strcmp(bindsfile, "NULL")) { if (!(xdg_conf = getenv("XDG_CONFIG_HOME"))) { home = getenv("HOME"); @@ -733,11 +741,11 @@ void conf_init(void) { sprintf(bindfile, "%s/%s", xdg_conf, "spmenu/binds.conf"); } } else { // custom keys path - if (!(bindfile = malloc(snprintf(NULL, 0, "%s", argbinds) + 1))) { + if (!(bindfile = malloc(snprintf(NULL, 0, "%s", bindsfile) + 1))) { die("spmenu: failed to malloc bindfile"); } - sprintf(bindfile, "%s", argbinds); + sprintf(bindfile, "%s", bindsfile); } // don't bother trying to load if it doesn't exist. diff --git a/libs/theme/theme.c b/libs/theme/theme.c index d23ce5a..51432c5 100644 --- a/libs/theme/theme.c +++ b/libs/theme/theme.c @@ -10,7 +10,7 @@ void theme_load(void) { if (!loadtheme) return; // get path for configuration file - if (!argtheme) { + if (!themefile || !strcmp(themefile, "NULL")) { if (!(xdg_conf = getenv("XDG_CONFIG_HOME"))) { // ~/.config/spmenu/theme.conf home = getenv("HOME"); @@ -31,11 +31,11 @@ void theme_load(void) { sprintf(theme, "%s/%s", xdg_conf, "spmenu/theme.conf"); } } else { // custom config path - if (!(theme = malloc(snprintf(NULL, 0, "%s", argtheme) + 1))) { + if (!(theme = malloc(snprintf(NULL, 0, "%s", themefile) + 1))) { die("spmenu: failed to malloc theme"); } - sprintf(theme, "%s", argtheme); + sprintf(theme, "%s", themefile); } // don't bother trying to load if it doesn't exist. diff --git a/options.h b/options.h index 723019d..8ee8c5c 100644 --- a/options.h +++ b/options.h @@ -15,6 +15,13 @@ static int loadtheme = 1; /* Load theme (~/.config/spmenu/them static int loadbinds = 1; /* Load keybind file (~/.config/spmenu/binds.conf) on runtime */ static int mon = -1; /* Monitor to run spmenu on */ +/* Config file options */ +#if USECONFIG +static char *configfile = NULL; /* Config file path. Default is ~/.config/spmenu/spmenu.conf */ +static char *themefile = NULL; /* Theme file path. Default is ~/.config/spmenu/theme.conf */ +static char *bindsfile = NULL; /* Keybind file path. Default is ~/.config/spmenu/binds.conf */ +#endif + /* Window options */ static int alpha = 1; /* Enable alpha */ static int menuposition = 2; /* Position of the menu (0: Bottom, 1: Top, 2: Center */ diff --git a/spmenu.c b/spmenu.c index b4d0b71..6ef8998 100644 --- a/spmenu.c +++ b/spmenu.c @@ -164,13 +164,6 @@ static int oh = 0; #endif static int fullscreen = 0; -// config file -#if USECONFIG -static char *argconf = NULL; // arg config path -static char *argtheme = NULL; // arg theme path -static char *argbinds = NULL; // arg binds path -#endif - // set an integer to 1 if we have rtl enabled, this saves a lot of lines and duplicate code #if USERTL static int isrtl = 1;