diff options
Diffstat (limited to 'sway/config.c')
-rw-r--r-- | sway/config.c | 131 |
1 files changed, 50 insertions, 81 deletions
diff --git a/sway/config.c b/sway/config.c index e4745a5c..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 @@ | |||
36 | struct sway_config *config = NULL; | 37 | struct sway_config *config = NULL; |
37 | 38 | ||
38 | static struct xkb_state *keysym_translation_state_create( | 39 | static 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 | ||
50 | static void keysym_translation_state_destroy( | 55 | static 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,6 +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; |
254 | config->font_description = pango_font_description_from_string(config->font); | ||
239 | config->urgent_timeout = 500; | 255 | config->urgent_timeout = 500; |
240 | config->focus_on_window_activation = FOWA_URGENT; | 256 | config->focus_on_window_activation = FOWA_URGENT; |
241 | config->popup_during_fullscreen = POPUP_SMART; | 257 | config->popup_during_fullscreen = POPUP_SMART; |
@@ -265,6 +281,7 @@ static void config_defaults(struct sway_config *config) { | |||
265 | config->title_align = ALIGN_LEFT; | 281 | config->title_align = ALIGN_LEFT; |
266 | config->tiling_drag = true; | 282 | config->tiling_drag = true; |
267 | config->tiling_drag_threshold = 9; | 283 | config->tiling_drag_threshold = 9; |
284 | config->primary_selection = true; | ||
268 | 285 | ||
269 | config->smart_gaps = SMART_GAPS_OFF; | 286 | config->smart_gaps = SMART_GAPS_OFF; |
270 | config->gaps_inner = 0; | 287 | config->gaps_inner = 0; |
@@ -327,8 +344,14 @@ static void config_defaults(struct sway_config *config) { | |||
327 | 344 | ||
328 | // The keysym to keycode translation | 345 | // The keysym to keycode translation |
329 | struct xkb_rule_names rules = {0}; | 346 | struct xkb_rule_names rules = {0}; |
330 | config->keysym_translation_state = | 347 | config->keysym_translation_state = keysym_translation_state_create(rules, 0); |
331 | 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 | } | ||
332 | 355 | ||
333 | return; | 356 | return; |
334 | cleanup: | 357 | cleanup: |
@@ -343,13 +366,7 @@ static char *config_path(const char *prefix, const char *config_folder) { | |||
343 | if (!prefix || !prefix[0] || !config_folder || !config_folder[0]) { | 366 | if (!prefix || !prefix[0] || !config_folder || !config_folder[0]) { |
344 | return NULL; | 367 | return NULL; |
345 | } | 368 | } |
346 | 369 | return format_str("%s/%s/config", prefix, config_folder); | |
347 | const char *filename = "config"; | ||
348 | |||
349 | size_t size = 3 + strlen(prefix) + strlen(config_folder) + strlen(filename); | ||
350 | char *path = calloc(size, sizeof(char)); | ||
351 | snprintf(path, size, "%s/%s/%s", prefix, config_folder, filename); | ||
352 | return path; | ||
353 | } | 370 | } |
354 | 371 | ||
355 | static char *get_config_path(void) { | 372 | static char *get_config_path(void) { |
@@ -359,10 +376,7 @@ static char *get_config_path(void) { | |||
359 | 376 | ||
360 | const char *config_home = getenv("XDG_CONFIG_HOME"); | 377 | const char *config_home = getenv("XDG_CONFIG_HOME"); |
361 | if ((config_home == NULL || config_home[0] == '\0') && home != NULL) { | 378 | if ((config_home == NULL || config_home[0] == '\0') && home != NULL) { |
362 | size_t size_fallback = 1 + strlen(home) + strlen("/.config"); | 379 | config_home_fallback = format_str("%s/.config", home); |
363 | config_home_fallback = calloc(size_fallback, sizeof(char)); | ||
364 | if (config_home_fallback != NULL) | ||
365 | snprintf(config_home_fallback, size_fallback, "%s/.config", home); | ||
366 | config_home = config_home_fallback; | 380 | config_home = config_home_fallback; |
367 | } | 381 | } |
368 | 382 | ||
@@ -466,6 +480,11 @@ bool load_main_config(const char *file, bool is_active, bool validating) { | |||
466 | old_config->xwayland ? "enabled" : "disabled"); | 480 | old_config->xwayland ? "enabled" : "disabled"); |
467 | config->xwayland = old_config->xwayland; | 481 | config->xwayland = old_config->xwayland; |
468 | 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 | |||
469 | if (!config->validating) { | 488 | if (!config->validating) { |
470 | if (old_config->swaybg_client != NULL) { | 489 | if (old_config->swaybg_client != NULL) { |
471 | wl_client_destroy(old_config->swaybg_client); | 490 | wl_client_destroy(old_config->swaybg_client); |
@@ -485,56 +504,7 @@ bool load_main_config(const char *file, bool is_active, bool validating) { | |||
485 | 504 | ||
486 | config->reading = true; | 505 | config->reading = true; |
487 | 506 | ||
488 | // Read security configs | 507 | bool success = load_config(path, config, &config->swaynag_config_errors); |
489 | // TODO: Security | ||
490 | bool success = true; | ||
491 | /* | ||
492 | DIR *dir = opendir(SYSCONFDIR "/sway/security.d"); | ||
493 | if (!dir) { | ||
494 | sway_log(SWAY_ERROR, | ||
495 | "%s does not exist, sway will have no security configuration" | ||
496 | " and will probably be broken", SYSCONFDIR "/sway/security.d"); | ||
497 | } else { | ||
498 | list_t *secconfigs = create_list(); | ||
499 | char *base = SYSCONFDIR "/sway/security.d/"; | ||
500 | struct dirent *ent = readdir(dir); | ||
501 | struct stat s; | ||
502 | while (ent != NULL) { | ||
503 | char *_path = malloc(strlen(ent->d_name) + strlen(base) + 1); | ||
504 | strcpy(_path, base); | ||
505 | strcat(_path, ent->d_name); | ||
506 | lstat(_path, &s); | ||
507 | if (S_ISREG(s.st_mode) && ent->d_name[0] != '.') { | ||
508 | list_add(secconfigs, _path); | ||
509 | } | ||
510 | else { | ||
511 | free(_path); | ||
512 | } | ||
513 | ent = readdir(dir); | ||
514 | } | ||
515 | closedir(dir); | ||
516 | |||
517 | list_qsort(secconfigs, qstrcmp); | ||
518 | for (int i = 0; i < secconfigs->length; ++i) { | ||
519 | char *_path = secconfigs->items[i]; | ||
520 | if (stat(_path, &s) || s.st_uid != 0 || s.st_gid != 0 || | ||
521 | (((s.st_mode & 0777) != 0644) && | ||
522 | (s.st_mode & 0777) != 0444)) { | ||
523 | sway_log(SWAY_ERROR, | ||
524 | "Refusing to load %s - it must be owned by root " | ||
525 | "and mode 644 or 444", _path); | ||
526 | success = false; | ||
527 | } else { | ||
528 | success = success && load_config(_path, config); | ||
529 | } | ||
530 | } | ||
531 | |||
532 | list_free_items_and_destroy(secconfigs); | ||
533 | } | ||
534 | */ | ||
535 | |||
536 | success = success && load_config(path, config, | ||
537 | &config->swaynag_config_errors); | ||
538 | 508 | ||
539 | if (validating) { | 509 | if (validating) { |
540 | free_config(config); | 510 | free_config(config); |
@@ -562,7 +532,7 @@ bool load_main_config(const char *file, bool is_active, bool validating) { | |||
562 | } | 532 | } |
563 | sway_switch_retrigger_bindings_for_all(); | 533 | sway_switch_retrigger_bindings_for_all(); |
564 | 534 | ||
565 | reset_outputs(); | 535 | apply_all_output_configs(); |
566 | spawn_swaybg(); | 536 | spawn_swaybg(); |
567 | 537 | ||
568 | config->reloading = false; | 538 | config->reloading = false; |
@@ -915,23 +885,18 @@ void config_add_swaynag_warning(char *fmt, ...) { | |||
915 | if (config->reading && !config->validating) { | 885 | if (config->reading && !config->validating) { |
916 | va_list args; | 886 | va_list args; |
917 | va_start(args, fmt); | 887 | va_start(args, fmt); |
918 | size_t length = vsnprintf(NULL, 0, fmt, args) + 1; | 888 | char *str = vformat_str(fmt, args); |
919 | va_end(args); | 889 | va_end(args); |
920 | 890 | if (str == NULL) { | |
921 | char *temp = malloc(length + 1); | ||
922 | if (!temp) { | ||
923 | sway_log(SWAY_ERROR, "Failed to allocate buffer for warning."); | ||
924 | return; | 891 | return; |
925 | } | 892 | } |
926 | 893 | ||
927 | va_start(args, fmt); | ||
928 | vsnprintf(temp, length, fmt, args); | ||
929 | va_end(args); | ||
930 | |||
931 | swaynag_log(config->swaynag_command, &config->swaynag_config_errors, | 894 | swaynag_log(config->swaynag_command, &config->swaynag_config_errors, |
932 | "Warning on line %i (%s) '%s': %s", | 895 | "Warning on line %i (%s) '%s': %s", |
933 | config->current_config_line_number, config->current_config_path, | 896 | config->current_config_line_number, config->current_config_path, |
934 | config->current_config_line, temp); | 897 | config->current_config_line, str); |
898 | |||
899 | free(str); | ||
935 | } | 900 | } |
936 | } | 901 | } |
937 | 902 | ||
@@ -971,7 +936,7 @@ char *do_var_replacement(char *str) { | |||
971 | int offset = find - str; | 936 | int offset = find - str; |
972 | strncpy(newptr, str, offset); | 937 | strncpy(newptr, str, offset); |
973 | newptr += offset; | 938 | newptr += offset; |
974 | strncpy(newptr, var->value, vvlen); | 939 | memcpy(newptr, var->value, vvlen); |
975 | newptr += vvlen; | 940 | newptr += vvlen; |
976 | strcpy(newptr, find + vnlen); | 941 | strcpy(newptr, find + vnlen); |
977 | free(str); | 942 | free(str); |
@@ -999,7 +964,7 @@ int workspace_output_cmp_workspace(const void *a, const void *b) { | |||
999 | void config_update_font_height(void) { | 964 | void config_update_font_height(void) { |
1000 | int prev_max_height = config->font_height; | 965 | int prev_max_height = config->font_height; |
1001 | 966 | ||
1002 | get_text_metrics(config->font, &config->font_height, &config->font_baseline); | 967 | get_text_metrics(config->font_description, &config->font_height, &config->font_baseline); |
1003 | 968 | ||
1004 | if (config->font_height != prev_max_height) { | 969 | if (config->font_height != prev_max_height) { |
1005 | arrange_root(); | 970 | arrange_root(); |
@@ -1033,8 +998,12 @@ void translate_keysyms(struct input_config *input_config) { | |||
1033 | 998 | ||
1034 | struct xkb_rule_names rules = {0}; | 999 | struct xkb_rule_names rules = {0}; |
1035 | input_config_fill_rule_names(input_config, &rules); | 1000 | input_config_fill_rule_names(input_config, &rules); |
1036 | config->keysym_translation_state = | 1001 | config->keysym_translation_state = keysym_translation_state_create(rules, 0); |
1037 | 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 | } | ||
1038 | 1007 | ||
1039 | for (int i = 0; i < config->modes->length; ++i) { | 1008 | for (int i = 0; i < config->modes->length; ++i) { |
1040 | struct sway_mode *mode = config->modes->items[i]; | 1009 | struct sway_mode *mode = config->modes->items[i]; |