aboutsummaryrefslogtreecommitdiffstats
path: root/sway/config.c
diff options
context:
space:
mode:
Diffstat (limited to 'sway/config.c')
-rw-r--r--sway/config.c103
1 files changed, 56 insertions, 47 deletions
diff --git a/sway/config.c b/sway/config.c
index 6e665434..e3daacda 100644
--- a/sway/config.c
+++ b/sway/config.c
@@ -26,7 +26,7 @@
26#include "sway/tree/arrange.h" 26#include "sway/tree/arrange.h"
27#include "sway/tree/root.h" 27#include "sway/tree/root.h"
28#include "sway/tree/workspace.h" 28#include "sway/tree/workspace.h"
29#include "cairo.h" 29#include "cairo_util.h"
30#include "pango.h" 30#include "pango.h"
31#include "stringop.h" 31#include "stringop.h"
32#include "list.h" 32#include "list.h"
@@ -236,7 +236,6 @@ static void config_defaults(struct sway_config *config) {
236 config->default_layout = L_NONE; 236 config->default_layout = L_NONE;
237 config->default_orientation = L_NONE; 237 config->default_orientation = L_NONE;
238 if (!(config->font = strdup("monospace 10"))) goto cleanup; 238 if (!(config->font = strdup("monospace 10"))) goto cleanup;
239 config->font_height = 17; // height of monospace 10
240 config->urgent_timeout = 500; 239 config->urgent_timeout = 500;
241 config->focus_on_window_activation = FOWA_URGENT; 240 config->focus_on_window_activation = FOWA_URGENT;
242 config->popup_during_fullscreen = POPUP_SMART; 241 config->popup_during_fullscreen = POPUP_SMART;
@@ -338,35 +337,62 @@ static bool file_exists(const char *path) {
338 return path && access(path, R_OK) != -1; 337 return path && access(path, R_OK) != -1;
339} 338}
340 339
340static char *config_path(const char *prefix, const char *config_folder) {
341 if (!prefix || !prefix[0] || !config_folder || !config_folder[0]) {
342 return NULL;
343 }
344
345 const char *filename = "config";
346
347 size_t size = 3 + strlen(prefix) + strlen(config_folder) + strlen(filename);
348 char *path = calloc(size, sizeof(char));
349 snprintf(path, size, "%s/%s/%s", prefix, config_folder, filename);
350 return path;
351}
352
341static char *get_config_path(void) { 353static char *get_config_path(void) {
342 static const char *config_paths[] = { 354 char *path = NULL;
343 "$HOME/.sway/config", 355 const char *home = getenv("HOME");
344 "$XDG_CONFIG_HOME/sway/config", 356 char *config_home_fallback = NULL;
345 "$HOME/.i3/config", 357
346 "$XDG_CONFIG_HOME/i3/config", 358 const char *config_home = getenv("XDG_CONFIG_HOME");
347 SYSCONFDIR "/sway/config", 359 if ((config_home == NULL || config_home[0] == '\0') && home != NULL) {
348 SYSCONFDIR "/i3/config", 360 size_t size_fallback = 1 + strlen(home) + strlen("/.config");
361 config_home_fallback = calloc(size_fallback, sizeof(char));
362 if (config_home_fallback != NULL)
363 snprintf(config_home_fallback, size_fallback, "%s/.config", home);
364 config_home = config_home_fallback;
365 }
366
367 struct config_path {
368 const char *prefix;
369 const char *config_folder;
349 }; 370 };
350 371
351 char *config_home = getenv("XDG_CONFIG_HOME"); 372 struct config_path config_paths[] = {
352 if (!config_home || !*config_home) { 373 { .prefix = home, .config_folder = ".sway"},
353 config_paths[1] = "$HOME/.config/sway/config"; 374 { .prefix = config_home, .config_folder = "sway"},
354 config_paths[3] = "$HOME/.config/i3/config"; 375 { .prefix = home, .config_folder = ".i3"},
355 } 376 { .prefix = config_home, .config_folder = "i3"},
377 { .prefix = SYSCONFDIR, .config_folder = "sway"},
378 { .prefix = SYSCONFDIR, .config_folder = "i3"}
379 };
356 380
357 for (size_t i = 0; i < sizeof(config_paths) / sizeof(char *); ++i) { 381 size_t num_config_paths = sizeof(config_paths)/sizeof(config_paths[0]);
358 wordexp_t p; 382 for (size_t i = 0; i < num_config_paths; i++) {
359 if (wordexp(config_paths[i], &p, WRDE_UNDEF) == 0) { 383 path = config_path(config_paths[i].prefix, config_paths[i].config_folder);
360 char *path = strdup(p.we_wordv[0]); 384 if (!path) {
361 wordfree(&p); 385 continue;
362 if (file_exists(path)) { 386 }
363 return path; 387 if (file_exists(path)) {
364 } 388 break;
365 free(path);
366 } 389 }
390 free(path);
391 path = NULL;
367 } 392 }
368 393
369 return NULL; 394 free(config_home_fallback);
395 return path;
370} 396}
371 397
372static bool load_config(const char *path, struct sway_config *config, 398static bool load_config(const char *path, struct sway_config *config,
@@ -514,6 +540,9 @@ bool load_main_config(const char *file, bool is_active, bool validating) {
514 return success; 540 return success;
515 } 541 }
516 542
543 // Only really necessary if not explicitly `font` is set in the config.
544 config_update_font_height();
545
517 if (is_active && !validating) { 546 if (is_active && !validating) {
518 input_manager_verify_fallback_seat(); 547 input_manager_verify_fallback_seat();
519 548
@@ -964,31 +993,11 @@ int workspace_output_cmp_workspace(const void *a, const void *b) {
964 return lenient_strcmp(wsa->workspace, wsb->workspace); 993 return lenient_strcmp(wsa->workspace, wsb->workspace);
965} 994}
966 995
967static void find_font_height_iterator(struct sway_container *con, void *data) {
968 size_t amount_below_baseline = con->title_height - con->title_baseline;
969 size_t extended_height = config->font_baseline + amount_below_baseline;
970 if (extended_height > config->font_height) {
971 config->font_height = extended_height;
972 }
973}
974
975static void find_baseline_iterator(struct sway_container *con, void *data) {
976 bool *recalculate = data;
977 if (*recalculate) {
978 container_calculate_title_height(con);
979 }
980 if (con->title_baseline > config->font_baseline) {
981 config->font_baseline = con->title_baseline;
982 }
983}
984 996
985void config_update_font_height(bool recalculate) { 997void config_update_font_height(void) {
986 size_t prev_max_height = config->font_height; 998 int prev_max_height = config->font_height;
987 config->font_height = 0;
988 config->font_baseline = 0;
989 999
990 root_for_each_container(find_baseline_iterator, &recalculate); 1000 get_text_metrics(config->font, &config->font_height, &config->font_baseline);
991 root_for_each_container(find_font_height_iterator, NULL);
992 1001
993 if (config->font_height != prev_max_height) { 1002 if (config->font_height != prev_max_height) {
994 arrange_root(); 1003 arrange_root();