diff options
-rw-r--r-- | sway/config.c | 136 |
1 files changed, 127 insertions, 9 deletions
diff --git a/sway/config.c b/sway/config.c index d96d23fc..17a75fbf 100644 --- a/sway/config.c +++ b/sway/config.c | |||
@@ -1,6 +1,7 @@ | |||
1 | #include <stdio.h> | 1 | #include <stdio.h> |
2 | #include <stdbool.h> | 2 | #include <stdbool.h> |
3 | #include <stdlib.h> | 3 | #include <stdlib.h> |
4 | #include <unistd.h> | ||
4 | #include "readline.h" | 5 | #include "readline.h" |
5 | #include "stringop.h" | 6 | #include "stringop.h" |
6 | #include "list.h" | 7 | #include "list.h" |
@@ -10,21 +11,138 @@ | |||
10 | 11 | ||
11 | struct sway_config *config; | 12 | struct sway_config *config; |
12 | 13 | ||
13 | bool load_config() { | 14 | static bool exists(const char *path) { |
14 | sway_log(L_INFO, "Loading config"); | 15 | return access(path, R_OK) != -1; |
15 | // TODO: Allow use of more config file locations | 16 | } |
16 | const char *name = "/.sway/config"; | 17 | |
18 | static char* get_config_path() { | ||
19 | char *name = "/.sway/config"; | ||
17 | const char *home = getenv("HOME"); | 20 | const char *home = getenv("HOME"); |
21 | |||
22 | // Check home dir | ||
23 | sway_log(L_DEBUG, "Trying to find config in ~/.sway/config"); | ||
18 | char *temp = malloc(strlen(home) + strlen(name) + 1); | 24 | char *temp = malloc(strlen(home) + strlen(name) + 1); |
19 | strcpy(temp, home); | 25 | strcpy(temp, home); |
20 | strcat(temp, name); | 26 | strcat(temp, name); |
21 | FILE *f = fopen(temp, "r"); | 27 | if (exists(temp)) { |
28 | return temp; | ||
29 | } | ||
30 | |||
31 | // Check XDG_CONFIG_HOME with fallback to ~/.config/ | ||
32 | sway_log(L_DEBUG, "Trying to find config in XDG_CONFIG_HOME/sway/config"); | ||
33 | char *xdg_config_home = getenv("XDG_CONFIG_HOME"); | ||
34 | if (xdg_config_home == NULL) { | ||
35 | sway_log(L_DEBUG, "Falling back to ~/.config/sway/config"); | ||
36 | name = "/.config/sway/config"; | ||
37 | temp = malloc(strlen(home) + strlen(name) + 1); | ||
38 | strcpy(temp, home); | ||
39 | strcat(temp, name); | ||
40 | } else { | ||
41 | name = "/sway/config"; | ||
42 | temp = malloc(strlen(xdg_config_home) + strlen(name) + 1); | ||
43 | strcpy(temp, home); | ||
44 | strcat(temp, name); | ||
45 | } | ||
46 | if (exists(temp)) { | ||
47 | return temp; | ||
48 | } | ||
49 | |||
50 | // Check /etc/ | ||
51 | sway_log(L_DEBUG, "Trying to find config in /etc/sway/config"); | ||
52 | strcpy(temp, "/etc/sway/config"); | ||
53 | if (exists(temp)) { | ||
54 | return temp; | ||
55 | } | ||
56 | |||
57 | // Check XDG_CONFIG_DIRS | ||
58 | sway_log(L_DEBUG, "Trying to find config in XDG_CONFIG_DIRS"); | ||
59 | char *xdg_config_dirs = getenv("XDG_CONFIG_DIRS"); | ||
60 | if (xdg_config_dirs != NULL) { | ||
61 | list_t *paths = split_string(xdg_config_dirs, ":"); | ||
62 | name = "/sway/config"; | ||
63 | int i; | ||
64 | for (i = 0; i < paths->length; i++ ) { | ||
65 | temp = malloc(strlen(paths->items[i]) + strlen(name) + 1); | ||
66 | strcpy(temp, paths->items[i]); | ||
67 | strcat(temp, name); | ||
68 | if (exists(temp)) { | ||
69 | free_flat_list(paths); | ||
70 | return temp; | ||
71 | } | ||
72 | } | ||
73 | free_flat_list(paths); | ||
74 | } | ||
75 | |||
76 | //Now fall back to i3 paths and try the same thing | ||
77 | name = "/.i3/config"; | ||
78 | sway_log(L_DEBUG, "Trying to find config in ~/.i3/config"); | ||
79 | temp = malloc(strlen(home) + strlen(name) + 1); | ||
80 | strcpy(temp, home); | ||
81 | strcat(temp, name); | ||
82 | if (exists(temp)) { | ||
83 | return temp; | ||
84 | } | ||
85 | |||
86 | sway_log(L_DEBUG, "Trying to find config in XDG_CONFIG_HOME/i3/config"); | ||
87 | if (xdg_config_home == NULL) { | ||
88 | sway_log(L_DEBUG, "Falling back to ~/.config/i3/config"); | ||
89 | name = "/.config/i3/config"; | ||
90 | temp = malloc(strlen(home) + strlen(name) + 1); | ||
91 | strcpy(temp, home); | ||
92 | strcat(temp, name); | ||
93 | } else { | ||
94 | name = "/i3/config"; | ||
95 | temp = malloc(strlen(xdg_config_home) + strlen(name) + 1); | ||
96 | strcpy(temp, home); | ||
97 | strcat(temp, name); | ||
98 | } | ||
99 | if (exists(temp)) { | ||
100 | return temp; | ||
101 | } | ||
102 | |||
103 | sway_log(L_DEBUG, "Trying to find config in /etc/i3/config"); | ||
104 | strcpy(temp, "/etc/i3/config"); | ||
105 | if (exists(temp)) { | ||
106 | return temp; | ||
107 | } | ||
108 | |||
109 | sway_log(L_DEBUG, "Trying to find config in XDG_CONFIG_DIRS"); | ||
110 | if (xdg_config_dirs != NULL) { | ||
111 | list_t *paths = split_string(xdg_config_dirs, ":"); | ||
112 | name = "/i3/config"; | ||
113 | int i; | ||
114 | for (i = 0; i < paths->length; i++ ) { | ||
115 | temp = malloc(strlen(paths->items[i]) + strlen(name) + 1); | ||
116 | strcpy(temp, paths->items[i]); | ||
117 | strcat(temp, name); | ||
118 | if (exists(temp)) { | ||
119 | free_flat_list(paths); | ||
120 | return temp; | ||
121 | } | ||
122 | } | ||
123 | free_flat_list(paths); | ||
124 | } | ||
125 | |||
126 | return NULL; | ||
127 | } | ||
128 | |||
129 | bool load_config() { | ||
130 | sway_log(L_INFO, "Loading config"); | ||
131 | |||
132 | char *path = get_config_path(); | ||
133 | |||
134 | if (path == NULL) { | ||
135 | sway_log(L_ERROR, "Unable to find a config file!"); | ||
136 | return false; | ||
137 | } | ||
138 | |||
139 | FILE *f = fopen(path, "r"); | ||
22 | if (!f) { | 140 | if (!f) { |
23 | fprintf(stderr, "Unable to open %s for reading", temp); | 141 | fprintf(stderr, "Unable to open %s for reading", path); |
24 | free(temp); | 142 | free(path); |
25 | return false; | 143 | return false; |
26 | } | 144 | } |
27 | free(temp); | 145 | free(path); |
28 | 146 | ||
29 | bool config_load_success; | 147 | bool config_load_success; |
30 | if (config) { | 148 | if (config) { |
@@ -95,7 +213,7 @@ bool read_config(FILE *file, bool is_active) { | |||
95 | sway_log(L_DEBUG, "Config load failed for line %s", line); | 213 | sway_log(L_DEBUG, "Config load failed for line %s", line); |
96 | success = false; | 214 | success = false; |
97 | temp_config->failed = true; | 215 | temp_config->failed = true; |
98 | } | 216 | } |
99 | list_free(args); | 217 | list_free(args); |
100 | 218 | ||
101 | _continue: | 219 | _continue: |