summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLibravatar taiyu <taiyu.len@gmail.com>2015-09-06 22:48:43 -0700
committerLibravatar taiyu <taiyu.len@gmail.com>2015-09-06 22:48:43 -0700
commit5408d34c9c8fe9860bd70245977a1c6f1f868bb5 (patch)
tree50a1eb4d5f28b59ef62219c8601fb0dad339789a
parentAdd fosspay badge (diff)
downloadsway-5408d34c9c8fe9860bd70245977a1c6f1f868bb5.tar.gz
sway-5408d34c9c8fe9860bd70245977a1c6f1f868bb5.tar.zst
sway-5408d34c9c8fe9860bd70245977a1c6f1f868bb5.zip
config_path cleanup
-rw-r--r--sway/config.c120
-rw-r--r--sway/handlers.c1
2 files changed, 58 insertions, 63 deletions
diff --git a/sway/config.c b/sway/config.c
index c9a9cc74..02805136 100644
--- a/sway/config.c
+++ b/sway/config.c
@@ -13,20 +13,23 @@
13 13
14struct sway_config *config; 14struct sway_config *config;
15 15
16static bool exists(const char *path) { 16static bool file_exists(const char *path) {
17 return access(path, R_OK) != -1; 17 return access(path, R_OK) != -1;
18} 18}
19 19
20void config_defaults(struct sway_config *config) { 20static void config_defaults(struct sway_config *config) {
21 config->symbols = create_list(); 21 config->symbols = create_list();
22 config->modes = create_list(); 22 config->modes = create_list();
23 config->cmd_queue = create_list();
24 config->workspace_outputs = create_list(); 23 config->workspace_outputs = create_list();
25 config->output_configs = create_list(); 24 config->output_configs = create_list();
25
26 config->cmd_queue = create_list();
27
26 config->current_mode = malloc(sizeof(struct sway_mode)); 28 config->current_mode = malloc(sizeof(struct sway_mode));
27 config->current_mode->name = NULL; 29 config->current_mode->name = NULL;
28 config->current_mode->bindings = create_list(); 30 config->current_mode->bindings = create_list();
29 list_add(config->modes, config->current_mode); 31 list_add(config->modes, config->current_mode);
32
30 config->floating_mod = 0; 33 config->floating_mod = 0;
31 config->default_layout = L_NONE; 34 config->default_layout = L_NONE;
32 config->default_orientation = L_NONE; 35 config->default_orientation = L_NONE;
@@ -44,7 +47,14 @@ void config_defaults(struct sway_config *config) {
44 47
45void free_mode(struct sway_mode *mode) { 48void free_mode(struct sway_mode *mode) {
46 free(mode->name); 49 free(mode->name);
47 free_flat_list(mode->bindings); 50 int i;
51 for (i = 0; i < mode->bindings->length; ++i) {
52 struct sway_binding *bind = mode->bindings->items[i];
53 list_free(bind->keys);
54 free(bind->command);
55 free(bind);
56 }
57 list_free(mode->bindings);
48} 58}
49 59
50void free_config(struct sway_config *config) { 60void free_config(struct sway_config *config) {
@@ -69,88 +79,74 @@ void free_config(struct sway_config *config) {
69 free_flat_list(config->output_configs); 79 free_flat_list(config->output_configs);
70} 80}
71 81
72static const char *search_paths[] = {
73 "$home/.sway/config",
74 "$config/sway/config",
75 "/etc/sway/config",
76 "$home/.i3/config",
77 "$config/.i3/config",
78 "/etc/i3/config"
79};
80 82
81static char *get_config_path() { 83static char *get_config_path(void) {
82 char *home = getenv("HOME"); 84 char *config_path = NULL;
83 if (home) { 85 char *paths[3] = {getenv("HOME"), getenv("XDG_CONFIG_HOME"), ""};
84 home = strdup(getenv("HOME")); 86 int pathlen[3] = {0, 0, 0};
85 } 87 int i;
86 char *config = getenv("XDG_CONFIG_HOME"); 88#define home paths[0]
87 if (config) { 89#define conf paths[1]
88 config = strdup(getenv("XDG_CONFIG_HOME")); 90 // Get home and config directories
91 home = home ? strdup(home) : NULL;
92 if (conf) {
93 conf = strdup(conf);
89 } else if (home) { 94 } else if (home) {
90 const char *def = "/.config"; 95 const char *def = "/.config";
91 config = malloc(strlen(home) + strlen(def) + 1); 96 conf = malloc(strlen(home) + strlen(def) + 1);
92 strcpy(config, home); 97 strcpy(conf, home);
93 strcat(config, def); 98 strcat(conf, def);
94 } else { 99 } else {
95 home = strdup(""); 100 home = strdup("");
96 config = strdup(""); 101 conf = strdup("");
97 } 102 }
98 103 pathlen[0] = strlen(home);
99 // Set up a temporary config for holding set variables 104 pathlen[1] = strlen(conf);
100 struct sway_config *temp_config = malloc(sizeof(struct sway_config)); 105#undef home
101 config_defaults(temp_config); 106#undef conf
102 const char *set_home = "set $home "; 107 // Search for config file from search paths
103 char *_home = malloc(strlen(home) + strlen(set_home) + 1); 108 static const char *search_paths[] = {
104 strcpy(_home, set_home); 109 "/.sway/config", // Prepend with $home
105 strcat(_home, home); 110 "/sway/config", // Prepend with $config
106 handle_command(temp_config, _home); 111 "/etc/sway/config",
107 free(_home); 112 "/.i3/config", // $home
108 const char *set_config = "set $config "; 113 "/.i3/config", // $config
109 char *_config = malloc(strlen(config) + strlen(set_config) + 1); 114 "/etc/i3/config"
110 strcpy(_config, set_config); 115 };
111 strcat(_config, config);
112 handle_command(temp_config, _config);
113 free(_config);
114
115 char *test = NULL;
116 int i;
117 for (i = 0; i < (int)(sizeof(search_paths) / sizeof(char *)); ++i) { 116 for (i = 0; i < (int)(sizeof(search_paths) / sizeof(char *)); ++i) {
118 test = strdup(search_paths[i]); 117 char *test = malloc(pathlen[i%3] + strlen(search_paths[i]) + 1);
119 test = do_var_replacement(temp_config, test); 118 strcpy(test, paths[i%3]);
119 strcat(test, search_paths[i]);
120 sway_log(L_DEBUG, "Checking for config at %s", test); 120 sway_log(L_DEBUG, "Checking for config at %s", test);
121 if (exists(test)) { 121 if (file_exists(test)) {
122 goto _continue; 122 config_path = test;
123 goto cleanup;
123 } 124 }
124 free(test); 125 free(test);
125 test = NULL;
126 } 126 }
127 127
128 sway_log(L_DEBUG, "Trying to find config in XDG_CONFIG_DIRS"); 128 sway_log(L_DEBUG, "Trying to find config in XDG_CONFIG_DIRS");
129 char *xdg_config_dirs = getenv("XDG_CONFIG_DIRS"); 129 char *xdg_config_dirs = getenv("XDG_CONFIG_DIRS");
130 if (xdg_config_dirs != NULL) { 130 if (xdg_config_dirs) {
131 list_t *paths = split_string(xdg_config_dirs, ":"); 131 list_t *paths = split_string(xdg_config_dirs, ":");
132 char *name = "/sway/config"; 132 const char *name = "/sway/config";
133 int i;
134 for (i = 0; i < paths->length; i++ ) { 133 for (i = 0; i < paths->length; i++ ) {
135 test = malloc(strlen(paths->items[i]) + strlen(name) + 1); 134 char *test = malloc(strlen(paths->items[i]) + strlen(name) + 1);
136 strcpy(test, paths->items[i]); 135 strcpy(test, paths->items[i]);
137 strcat(test, name); 136 strcat(test, name);
138 if (exists(test)) { 137 if (file_exists(test)) {
139 free_config(temp_config); 138 config_path = test;
140 free_flat_list(paths); 139 break;
141 return test;
142 } 140 }
143 free(test); 141 free(test);
144 test = NULL;
145 } 142 }
146 free_flat_list(paths); 143 free_flat_list(paths);
147 } 144 }
148 145
149_continue: 146cleanup:
150 free_config(temp_config); 147 free(paths[0]);
151 free(home); 148 free(paths[1]);
152 free(config); 149 return config_path;
153 return test;
154} 150}
155 151
156bool load_config(const char *file) { 152bool load_config(const char *file) {
diff --git a/sway/handlers.c b/sway/handlers.c
index 413c17fe..482d52c6 100644
--- a/sway/handlers.c
+++ b/sway/handlers.c
@@ -334,7 +334,6 @@ static bool handle_key(wlc_handle view, uint32_t time, const struct wlc_modifier
334 release_key(sym, key); 334 release_key(sym, key);
335 } 335 }
336 336
337 // TODO: reminder to check conflicts with mod+q+a versus mod+q
338 for (i = 0; i < mode->bindings->length; ++i) { 337 for (i = 0; i < mode->bindings->length; ++i) {
339 struct sway_binding *binding = mode->bindings->items[i]; 338 struct sway_binding *binding = mode->bindings->items[i];
340 339