main: translate command line options to overrides

Instead of special casing configuration affecting command line
options (like --font, --fullscreen, --maximized etc), translate them
to overrides, and let the configuration system handle them.

This also fixes an issue where -f,--font did not set csd.font, if
csd.font were otherwise unset.
This commit is contained in:
Daniel Eklöf 2023-07-31 16:26:17 +02:00
parent 33dcb4d49a
commit 0b4f1b4af2
No known key found for this signature in database
GPG key ID: 5BBD4992C116573F
2 changed files with 22 additions and 89 deletions

View file

@ -51,6 +51,10 @@
### Deprecated ### Deprecated
### Removed ### Removed
### Fixed ### Fixed
* `-f,--font` command line option not affecting `csd.font` (if unset).
### Security ### Security
### Contributors ### Contributors

107
main.c
View file

@ -222,21 +222,11 @@ main(int argc, char *const *argv)
bool check_config = false; bool check_config = false;
const char *conf_path = NULL; const char *conf_path = NULL;
const char *conf_term = NULL;
const char *conf_title = NULL;
const char *conf_app_id = NULL;
const char *custom_cwd = NULL; const char *custom_cwd = NULL;
bool login_shell = false;
tll(char *) conf_fonts = tll_init();
enum conf_size_type conf_size_type = CONF_SIZE_PX;
int conf_width = -1;
int conf_height = -1;
bool as_server = false; bool as_server = false;
const char *conf_server_socket_path = NULL; const char *conf_server_socket_path = NULL;
bool presentation_timings = false; bool presentation_timings = false;
bool hold = false; bool hold = false;
bool maximized = false;
bool fullscreen = false;
bool unlink_pid_file = false; bool unlink_pid_file = false;
const char *pid_file = NULL; const char *pid_file = NULL;
enum log_class log_level = LOG_CLASS_WARNING; enum log_class log_level = LOG_CLASS_WARNING;
@ -261,23 +251,23 @@ main(int argc, char *const *argv)
break; break;
case 'o': case 'o':
tll_push_back(overrides, optarg); tll_push_back(overrides, xstrdup(optarg));
break; break;
case 't': case 't':
conf_term = optarg; tll_push_back(overrides, xasprintf("term=%s", optarg));
break; break;
case 'L': case 'L':
login_shell = true; tll_push_back(overrides, xstrdup("login-shell=yes"));
break; break;
case 'T': case 'T':
conf_title = optarg; tll_push_back(overrides, xasprintf("title%s", optarg));
break; break;
case 'a': case 'a':
conf_app_id = optarg; tll_push_back(overrides, xasprintf("app-id=%s", optarg));
break; break;
case 'D': { case 'D': {
@ -290,27 +280,11 @@ main(int argc, char *const *argv)
break; break;
} }
case 'f': case 'f': {
tll_free_and_free(conf_fonts, free); char *font_override = xasprintf("font=%s", optarg);
for (char *font = strtok(optarg, ","); font != NULL; font = strtok(NULL, ",")) { tll_push_back(overrides, font_override);
/* Strip leading spaces */
while (*font != '\0' && isspace(*font))
font++;
/* Strip trailing spaces */
char *end = font + strlen(font);
xassert(*end == '\0');
end--;
while (end > font && isspace(*end))
*(end--) = '\0';
if (strlen(font) == 0)
continue;
tll_push_back(conf_fonts, font);
}
break; break;
}
case 'w': { case 'w': {
unsigned width, height; unsigned width, height;
@ -319,9 +293,9 @@ main(int argc, char *const *argv)
return ret; return ret;
} }
conf_size_type = CONF_SIZE_PX; tll_push_back(
conf_width = width; overrides, xasprintf("initial-window-size-pixels=%ux%u",
conf_height = height; width, height));
break; break;
} }
@ -332,9 +306,9 @@ main(int argc, char *const *argv)
return ret; return ret;
} }
conf_size_type = CONF_SIZE_CELLS; tll_push_back(
conf_width = width; overrides, xasprintf("initial-window-size-chars=%ux%u",
conf_height = height; width, height));
break; break;
} }
@ -353,13 +327,11 @@ main(int argc, char *const *argv)
break; break;
case 'm': case 'm':
maximized = true; tll_push_back(overrides, xstrdup("initial-window-mode=maximized"));
fullscreen = false;
break; break;
case 'F': case 'F':
fullscreen = true; tll_push_back(overrides, xstrdup("initial-window-mode=fullscreen"));
maximized = false;
break; break;
case 'p': case 'p':
@ -494,7 +466,7 @@ main(int argc, char *const *argv)
bool conf_successful = config_load( bool conf_successful = config_load(
&conf, conf_path, &user_notifications, &overrides, check_config, as_server); &conf, conf_path, &user_notifications, &overrides, check_config, as_server);
tll_free(overrides); tll_free_and_free(overrides, free);
if (!conf_successful) { if (!conf_successful) {
config_free(&conf); config_free(&conf);
return ret; return ret;
@ -515,53 +487,10 @@ main(int argc, char *const *argv)
(enum fcft_log_class)log_level); (enum fcft_log_class)log_level);
fcft_set_scaling_filter(conf.tweak.fcft_filter); fcft_set_scaling_filter(conf.tweak.fcft_filter);
if (conf_term != NULL) {
free(conf.term);
conf.term = xstrdup(conf_term);
}
if (conf_title != NULL) {
free(conf.title);
conf.title = xstrdup(conf_title);
}
if (conf_app_id != NULL) {
free(conf.app_id);
conf.app_id = xstrdup(conf_app_id);
}
if (login_shell)
conf.login_shell = true;
if (tll_length(conf_fonts) > 0) {
for (size_t i = 0; i < ALEN(conf.fonts); i++)
config_font_list_destroy(&conf.fonts[i]);
struct config_font_list *font_list = &conf.fonts[0];
xassert(font_list->count == 0);
xassert(font_list->arr == NULL);
font_list->arr = xmalloc(
tll_length(conf_fonts) * sizeof(font_list->arr[0]));
tll_foreach(conf_fonts, it) {
struct config_font font;
if (!config_font_parse(it->item, &font)) {
LOG_ERR("%s: invalid font specification", it->item);
} else
font_list->arr[font_list->count++] = font;
}
tll_free(conf_fonts);
}
if (conf_width > 0 && conf_height > 0) {
conf.size.type = conf_size_type;
conf.size.width = conf_width;
conf.size.height = conf_height;
}
if (conf_server_socket_path != NULL) { if (conf_server_socket_path != NULL) {
free(conf.server_socket_path); free(conf.server_socket_path);
conf.server_socket_path = xstrdup(conf_server_socket_path); conf.server_socket_path = xstrdup(conf_server_socket_path);
} }
if (maximized)
conf.startup_mode = STARTUP_MAXIMIZED;
else if (fullscreen)
conf.startup_mode = STARTUP_FULLSCREEN;
conf.presentation_timings = presentation_timings; conf.presentation_timings = presentation_timings;
conf.hold_at_exit = hold; conf.hold_at_exit = hold;