aboutsummaryrefslogtreecommitdiffstats
path: root/sway/config.c
diff options
context:
space:
mode:
Diffstat (limited to 'sway/config.c')
-rw-r--r--sway/config.c163
1 files changed, 58 insertions, 105 deletions
diff --git a/sway/config.c b/sway/config.c
index fde386c7..f9131e0f 100644
--- a/sway/config.c
+++ b/sway/config.c
@@ -1,3 +1,4 @@
1#undef _POSIX_C_SOURCE
1#define _XOPEN_SOURCE 700 // for realpath 2#define _XOPEN_SOURCE 700 // for realpath
2#include <stdio.h> 3#include <stdio.h>
3#include <stdbool.h> 4#include <stdbool.h>
@@ -36,19 +37,26 @@
36struct sway_config *config = NULL; 37struct sway_config *config = NULL;
37 38
38static struct xkb_state *keysym_translation_state_create( 39static struct xkb_state *keysym_translation_state_create(
39 struct xkb_rule_names rules) { 40 struct xkb_rule_names rules, uint32_t context_flags) {
40 struct xkb_context *context = xkb_context_new(XKB_CONTEXT_NO_FLAGS); 41 struct xkb_context *context = xkb_context_new(context_flags | XKB_CONTEXT_NO_SECURE_GETENV);
41 struct xkb_keymap *xkb_keymap = xkb_keymap_new_from_names( 42 struct xkb_keymap *xkb_keymap = xkb_keymap_new_from_names(
42 context, 43 context,
43 &rules, 44 &rules,
44 XKB_KEYMAP_COMPILE_NO_FLAGS); 45 XKB_KEYMAP_COMPILE_NO_FLAGS);
45
46 xkb_context_unref(context); 46 xkb_context_unref(context);
47 if (xkb_keymap == NULL) {
48 sway_log(SWAY_ERROR, "Failed to compile keysym translation XKB keymap");
49 return NULL;
50 }
51
47 return xkb_state_new(xkb_keymap); 52 return xkb_state_new(xkb_keymap);
48} 53}
49 54
50static void keysym_translation_state_destroy( 55static void keysym_translation_state_destroy(
51 struct xkb_state *state) { 56 struct xkb_state *state) {
57 if (state == NULL) {
58 return;
59 }
52 xkb_keymap_unref(xkb_state_get_keymap(state)); 60 xkb_keymap_unref(xkb_state_get_keymap(state));
53 xkb_state_unref(state); 61 xkb_state_unref(state);
54} 62}
@@ -82,6 +90,12 @@ static void free_mode(struct sway_mode *mode) {
82 } 90 }
83 list_free(mode->switch_bindings); 91 list_free(mode->switch_bindings);
84 } 92 }
93 if (mode->gesture_bindings) {
94 for (int i = 0; i < mode->gesture_bindings->length; i++) {
95 free_gesture_binding(mode->gesture_bindings->items[i]);
96 }
97 list_free(mode->gesture_bindings);
98 }
85 free(mode); 99 free(mode);
86} 100}
87 101
@@ -222,6 +236,7 @@ static void config_defaults(struct sway_config *config) {
222 if (!(config->current_mode->keycode_bindings = create_list())) goto cleanup; 236 if (!(config->current_mode->keycode_bindings = create_list())) goto cleanup;
223 if (!(config->current_mode->mouse_bindings = create_list())) goto cleanup; 237 if (!(config->current_mode->mouse_bindings = create_list())) goto cleanup;
224 if (!(config->current_mode->switch_bindings = create_list())) goto cleanup; 238 if (!(config->current_mode->switch_bindings = create_list())) goto cleanup;
239 if (!(config->current_mode->gesture_bindings = create_list())) goto cleanup;
225 list_add(config->modes, config->current_mode); 240 list_add(config->modes, config->current_mode);
226 241
227 config->floating_mod = 0; 242 config->floating_mod = 0;
@@ -236,7 +251,7 @@ static void config_defaults(struct sway_config *config) {
236 config->default_layout = L_NONE; 251 config->default_layout = L_NONE;
237 config->default_orientation = L_NONE; 252 config->default_orientation = L_NONE;
238 if (!(config->font = strdup("monospace 10"))) goto cleanup; 253 if (!(config->font = strdup("monospace 10"))) goto cleanup;
239 config->font_height = 17; // height of monospace 10 254 config->font_description = pango_font_description_from_string(config->font);
240 config->urgent_timeout = 500; 255 config->urgent_timeout = 500;
241 config->focus_on_window_activation = FOWA_URGENT; 256 config->focus_on_window_activation = FOWA_URGENT;
242 config->popup_during_fullscreen = POPUP_SMART; 257 config->popup_during_fullscreen = POPUP_SMART;
@@ -266,8 +281,9 @@ static void config_defaults(struct sway_config *config) {
266 config->title_align = ALIGN_LEFT; 281 config->title_align = ALIGN_LEFT;
267 config->tiling_drag = true; 282 config->tiling_drag = true;
268 config->tiling_drag_threshold = 9; 283 config->tiling_drag_threshold = 9;
284 config->primary_selection = true;
269 285
270 config->smart_gaps = false; 286 config->smart_gaps = SMART_GAPS_OFF;
271 config->gaps_inner = 0; 287 config->gaps_inner = 0;
272 config->gaps_outer.top = 0; 288 config->gaps_outer.top = 0;
273 config->gaps_outer.right = 0; 289 config->gaps_outer.right = 0;
@@ -291,6 +307,8 @@ static void config_defaults(struct sway_config *config) {
291 config->hide_edge_borders_smart = ESMART_OFF; 307 config->hide_edge_borders_smart = ESMART_OFF;
292 config->hide_lone_tab = false; 308 config->hide_lone_tab = false;
293 309
310 config->has_focused_tab_title = false;
311
294 // border colors 312 // border colors
295 color_to_rgba(config->border_colors.focused.border, 0x4C7899FF); 313 color_to_rgba(config->border_colors.focused.border, 0x4C7899FF);
296 color_to_rgba(config->border_colors.focused.background, 0x285577FF); 314 color_to_rgba(config->border_colors.focused.background, 0x285577FF);
@@ -326,8 +344,14 @@ static void config_defaults(struct sway_config *config) {
326 344
327 // The keysym to keycode translation 345 // The keysym to keycode translation
328 struct xkb_rule_names rules = {0}; 346 struct xkb_rule_names rules = {0};
329 config->keysym_translation_state = 347 config->keysym_translation_state = keysym_translation_state_create(rules, 0);
330 keysym_translation_state_create(rules); 348 if (config->keysym_translation_state == NULL) {
349 config->keysym_translation_state = keysym_translation_state_create(rules,
350 XKB_CONTEXT_NO_ENVIRONMENT_NAMES);
351 }
352 if (config->keysym_translation_state == NULL) {
353 goto cleanup;
354 }
331 355
332 return; 356 return;
333cleanup: 357cleanup:
@@ -342,13 +366,7 @@ static char *config_path(const char *prefix, const char *config_folder) {
342 if (!prefix || !prefix[0] || !config_folder || !config_folder[0]) { 366 if (!prefix || !prefix[0] || !config_folder || !config_folder[0]) {
343 return NULL; 367 return NULL;
344 } 368 }
345 369 return format_str("%s/%s/config", prefix, config_folder);
346 const char *filename = "config";
347
348 size_t size = 3 + strlen(prefix) + strlen(config_folder) + strlen(filename);
349 char *path = calloc(size, sizeof(char));
350 snprintf(path, size, "%s/%s/%s", prefix, config_folder, filename);
351 return path;
352} 370}
353 371
354static char *get_config_path(void) { 372static char *get_config_path(void) {
@@ -358,10 +376,7 @@ static char *get_config_path(void) {
358 376
359 const char *config_home = getenv("XDG_CONFIG_HOME"); 377 const char *config_home = getenv("XDG_CONFIG_HOME");
360 if ((config_home == NULL || config_home[0] == '\0') && home != NULL) { 378 if ((config_home == NULL || config_home[0] == '\0') && home != NULL) {
361 size_t size_fallback = 1 + strlen(home) + strlen("/.config"); 379 config_home_fallback = format_str("%s/.config", home);
362 config_home_fallback = calloc(size_fallback, sizeof(char));
363 if (config_home_fallback != NULL)
364 snprintf(config_home_fallback, size_fallback, "%s/.config", home);
365 config_home = config_home_fallback; 380 config_home = config_home_fallback;
366 } 381 }
367 382
@@ -465,6 +480,11 @@ bool load_main_config(const char *file, bool is_active, bool validating) {
465 old_config->xwayland ? "enabled" : "disabled"); 480 old_config->xwayland ? "enabled" : "disabled");
466 config->xwayland = old_config->xwayland; 481 config->xwayland = old_config->xwayland;
467 482
483 // primary_selection can only be enabled/disabled at launch
484 sway_log(SWAY_DEBUG, "primary_selection will remain %s",
485 old_config->primary_selection ? "enabled" : "disabled");
486 config->primary_selection = old_config->primary_selection;
487
468 if (!config->validating) { 488 if (!config->validating) {
469 if (old_config->swaybg_client != NULL) { 489 if (old_config->swaybg_client != NULL) {
470 wl_client_destroy(old_config->swaybg_client); 490 wl_client_destroy(old_config->swaybg_client);
@@ -484,56 +504,7 @@ bool load_main_config(const char *file, bool is_active, bool validating) {
484 504
485 config->reading = true; 505 config->reading = true;
486 506
487 // Read security configs 507 bool success = load_config(path, config, &config->swaynag_config_errors);
488 // TODO: Security
489 bool success = true;
490 /*
491 DIR *dir = opendir(SYSCONFDIR "/sway/security.d");
492 if (!dir) {
493 sway_log(SWAY_ERROR,
494 "%s does not exist, sway will have no security configuration"
495 " and will probably be broken", SYSCONFDIR "/sway/security.d");
496 } else {
497 list_t *secconfigs = create_list();
498 char *base = SYSCONFDIR "/sway/security.d/";
499 struct dirent *ent = readdir(dir);
500 struct stat s;
501 while (ent != NULL) {
502 char *_path = malloc(strlen(ent->d_name) + strlen(base) + 1);
503 strcpy(_path, base);
504 strcat(_path, ent->d_name);
505 lstat(_path, &s);
506 if (S_ISREG(s.st_mode) && ent->d_name[0] != '.') {
507 list_add(secconfigs, _path);
508 }
509 else {
510 free(_path);
511 }
512 ent = readdir(dir);
513 }
514 closedir(dir);
515
516 list_qsort(secconfigs, qstrcmp);
517 for (int i = 0; i < secconfigs->length; ++i) {
518 char *_path = secconfigs->items[i];
519 if (stat(_path, &s) || s.st_uid != 0 || s.st_gid != 0 ||
520 (((s.st_mode & 0777) != 0644) &&
521 (s.st_mode & 0777) != 0444)) {
522 sway_log(SWAY_ERROR,
523 "Refusing to load %s - it must be owned by root "
524 "and mode 644 or 444", _path);
525 success = false;
526 } else {
527 success = success && load_config(_path, config);
528 }
529 }
530
531 list_free_items_and_destroy(secconfigs);
532 }
533 */
534
535 success = success && load_config(path, config,
536 &config->swaynag_config_errors);
537 508
538 if (validating) { 509 if (validating) {
539 free_config(config); 510 free_config(config);
@@ -541,6 +512,9 @@ bool load_main_config(const char *file, bool is_active, bool validating) {
541 return success; 512 return success;
542 } 513 }
543 514
515 // Only really necessary if not explicitly `font` is set in the config.
516 config_update_font_height();
517
544 if (is_active && !validating) { 518 if (is_active && !validating) {
545 input_manager_verify_fallback_seat(); 519 input_manager_verify_fallback_seat();
546 520
@@ -558,7 +532,7 @@ bool load_main_config(const char *file, bool is_active, bool validating) {
558 } 532 }
559 sway_switch_retrigger_bindings_for_all(); 533 sway_switch_retrigger_bindings_for_all();
560 534
561 reset_outputs(); 535 apply_all_output_configs();
562 spawn_swaybg(); 536 spawn_swaybg();
563 537
564 config->reloading = false; 538 config->reloading = false;
@@ -911,23 +885,18 @@ void config_add_swaynag_warning(char *fmt, ...) {
911 if (config->reading && !config->validating) { 885 if (config->reading && !config->validating) {
912 va_list args; 886 va_list args;
913 va_start(args, fmt); 887 va_start(args, fmt);
914 size_t length = vsnprintf(NULL, 0, fmt, args) + 1; 888 char *str = vformat_str(fmt, args);
915 va_end(args); 889 va_end(args);
916 890 if (str == NULL) {
917 char *temp = malloc(length + 1);
918 if (!temp) {
919 sway_log(SWAY_ERROR, "Failed to allocate buffer for warning.");
920 return; 891 return;
921 } 892 }
922 893
923 va_start(args, fmt);
924 vsnprintf(temp, length, fmt, args);
925 va_end(args);
926
927 swaynag_log(config->swaynag_command, &config->swaynag_config_errors, 894 swaynag_log(config->swaynag_command, &config->swaynag_config_errors,
928 "Warning on line %i (%s) '%s': %s", 895 "Warning on line %i (%s) '%s': %s",
929 config->current_config_line_number, config->current_config_path, 896 config->current_config_line_number, config->current_config_path,
930 config->current_config_line, temp); 897 config->current_config_line, str);
898
899 free(str);
931 } 900 }
932} 901}
933 902
@@ -967,7 +936,7 @@ char *do_var_replacement(char *str) {
967 int offset = find - str; 936 int offset = find - str;
968 strncpy(newptr, str, offset); 937 strncpy(newptr, str, offset);
969 newptr += offset; 938 newptr += offset;
970 strncpy(newptr, var->value, vvlen); 939 memcpy(newptr, var->value, vvlen);
971 newptr += vvlen; 940 newptr += vvlen;
972 strcpy(newptr, find + vnlen); 941 strcpy(newptr, find + vnlen);
973 free(str); 942 free(str);
@@ -991,31 +960,11 @@ int workspace_output_cmp_workspace(const void *a, const void *b) {
991 return lenient_strcmp(wsa->workspace, wsb->workspace); 960 return lenient_strcmp(wsa->workspace, wsb->workspace);
992} 961}
993 962
994static void find_font_height_iterator(struct sway_container *con, void *data) {
995 size_t amount_below_baseline = con->title_height - con->title_baseline;
996 size_t extended_height = config->font_baseline + amount_below_baseline;
997 if (extended_height > config->font_height) {
998 config->font_height = extended_height;
999 }
1000}
1001
1002static void find_baseline_iterator(struct sway_container *con, void *data) {
1003 bool *recalculate = data;
1004 if (*recalculate) {
1005 container_calculate_title_height(con);
1006 }
1007 if (con->title_baseline > config->font_baseline) {
1008 config->font_baseline = con->title_baseline;
1009 }
1010}
1011 963
1012void config_update_font_height(bool recalculate) { 964void config_update_font_height(void) {
1013 size_t prev_max_height = config->font_height; 965 int prev_max_height = config->font_height;
1014 config->font_height = 0;
1015 config->font_baseline = 0;
1016 966
1017 root_for_each_container(find_baseline_iterator, &recalculate); 967 get_text_metrics(config->font_description, &config->font_height, &config->font_baseline);
1018 root_for_each_container(find_font_height_iterator, NULL);
1019 968
1020 if (config->font_height != prev_max_height) { 969 if (config->font_height != prev_max_height) {
1021 arrange_root(); 970 arrange_root();
@@ -1049,8 +998,12 @@ void translate_keysyms(struct input_config *input_config) {
1049 998
1050 struct xkb_rule_names rules = {0}; 999 struct xkb_rule_names rules = {0};
1051 input_config_fill_rule_names(input_config, &rules); 1000 input_config_fill_rule_names(input_config, &rules);
1052 config->keysym_translation_state = 1001 config->keysym_translation_state = keysym_translation_state_create(rules, 0);
1053 keysym_translation_state_create(rules); 1002 if (config->keysym_translation_state == NULL) {
1003 sway_log(SWAY_ERROR, "Failed to create keysym translation XKB state "
1004 "for device '%s'", input_config->identifier);
1005 return;
1006 }
1054 1007
1055 for (int i = 0; i < config->modes->length; ++i) { 1008 for (int i = 0; i < config->modes->length; ++i) {
1056 struct sway_mode *mode = config->modes->items[i]; 1009 struct sway_mode *mode = config->modes->items[i];