diff options
Diffstat (limited to 'sway/old/config.c')
-rw-r--r-- | sway/old/config.c | 1313 |
1 files changed, 1313 insertions, 0 deletions
diff --git a/sway/old/config.c b/sway/old/config.c new file mode 100644 index 00000000..d14653e3 --- /dev/null +++ b/sway/old/config.c | |||
@@ -0,0 +1,1313 @@ | |||
1 | #define _POSIX_C_SOURCE 200809L | ||
2 | #define _XOPEN_SOURCE 700 | ||
3 | #include <stdio.h> | ||
4 | #include <stdbool.h> | ||
5 | #include <stdlib.h> | ||
6 | #include <unistd.h> | ||
7 | #include <libgen.h> | ||
8 | #include <wordexp.h> | ||
9 | #include <sys/types.h> | ||
10 | #include <sys/wait.h> | ||
11 | #include <sys/stat.h> | ||
12 | #include <signal.h> | ||
13 | #include <libinput.h> | ||
14 | #include <limits.h> | ||
15 | #include <float.h> | ||
16 | #include <dirent.h> | ||
17 | #include <strings.h> | ||
18 | #include <wlr/types/wlr_output.h> | ||
19 | #include "wayland-desktop-shell-server-protocol.h" | ||
20 | #include "sway/commands.h" | ||
21 | #include "sway/config.h" | ||
22 | #include "sway/layout.h" | ||
23 | #include "sway/input_state.h" | ||
24 | #include "sway/criteria.h" | ||
25 | #include "sway/input.h" | ||
26 | #include "sway/border.h" | ||
27 | #include "readline.h" | ||
28 | #include "stringop.h" | ||
29 | #include "list.h" | ||
30 | #include "log.h" | ||
31 | |||
32 | struct sway_config *config = NULL; | ||
33 | |||
34 | static void terminate_swaybar(pid_t pid); | ||
35 | |||
36 | static void free_variable(struct sway_variable *var) { | ||
37 | if (!var) { | ||
38 | return; | ||
39 | } | ||
40 | free(var->name); | ||
41 | free(var->value); | ||
42 | free(var); | ||
43 | } | ||
44 | |||
45 | static void free_binding(struct sway_binding *bind) { | ||
46 | if (!bind) { | ||
47 | return; | ||
48 | } | ||
49 | free_flat_list(bind->keys); | ||
50 | free(bind->command); | ||
51 | free(bind); | ||
52 | } | ||
53 | |||
54 | static void free_mode(struct sway_mode *mode) { | ||
55 | if (!mode) { | ||
56 | return; | ||
57 | } | ||
58 | free(mode->name); | ||
59 | int i; | ||
60 | for (i = 0; mode->bindings && i < mode->bindings->length; ++i) { | ||
61 | free_binding(mode->bindings->items[i]); | ||
62 | } | ||
63 | list_free(mode->bindings); | ||
64 | free(mode); | ||
65 | } | ||
66 | |||
67 | static void free_bar(struct bar_config *bar) { | ||
68 | if (!bar) { | ||
69 | return; | ||
70 | } | ||
71 | free(bar->mode); | ||
72 | free(bar->hidden_state); | ||
73 | #ifdef ENABLE_TRAY | ||
74 | free(bar->tray_output); | ||
75 | free(bar->icon_theme); | ||
76 | #endif | ||
77 | free(bar->status_command); | ||
78 | free(bar->font); | ||
79 | free(bar->separator_symbol); | ||
80 | int i; | ||
81 | for (i = 0; bar->bindings && i < bar->bindings->length; ++i) { | ||
82 | free_sway_mouse_binding(bar->bindings->items[i]); | ||
83 | } | ||
84 | list_free(bar->bindings); | ||
85 | |||
86 | if (bar->outputs) { | ||
87 | free_flat_list(bar->outputs); | ||
88 | } | ||
89 | |||
90 | if (bar->pid != 0) { | ||
91 | terminate_swaybar(bar->pid); | ||
92 | } | ||
93 | |||
94 | free(bar->colors.background); | ||
95 | free(bar->colors.statusline); | ||
96 | free(bar->colors.separator); | ||
97 | free(bar->colors.focused_background); | ||
98 | free(bar->colors.focused_statusline); | ||
99 | free(bar->colors.focused_separator); | ||
100 | free(bar->colors.focused_workspace_border); | ||
101 | free(bar->colors.focused_workspace_bg); | ||
102 | free(bar->colors.focused_workspace_text); | ||
103 | free(bar->colors.active_workspace_border); | ||
104 | free(bar->colors.active_workspace_bg); | ||
105 | free(bar->colors.active_workspace_text); | ||
106 | free(bar->colors.inactive_workspace_border); | ||
107 | free(bar->colors.inactive_workspace_bg); | ||
108 | free(bar->colors.inactive_workspace_text); | ||
109 | free(bar->colors.urgent_workspace_border); | ||
110 | free(bar->colors.urgent_workspace_bg); | ||
111 | free(bar->colors.urgent_workspace_text); | ||
112 | free(bar->colors.binding_mode_border); | ||
113 | free(bar->colors.binding_mode_bg); | ||
114 | free(bar->colors.binding_mode_text); | ||
115 | |||
116 | free(bar); | ||
117 | } | ||
118 | |||
119 | void free_input_config(struct input_config *ic) { | ||
120 | if (!ic) { | ||
121 | return; | ||
122 | } | ||
123 | free(ic->identifier); | ||
124 | free(ic); | ||
125 | } | ||
126 | |||
127 | void free_output_config(struct output_config *oc) { | ||
128 | if (!oc) { | ||
129 | return; | ||
130 | } | ||
131 | free(oc->name); | ||
132 | free(oc->background); | ||
133 | free(oc->background_option); | ||
134 | free(oc); | ||
135 | } | ||
136 | |||
137 | static void free_workspace_output(struct workspace_output *wo) { | ||
138 | if (!wo) { | ||
139 | return; | ||
140 | } | ||
141 | free(wo->output); | ||
142 | free(wo->workspace); | ||
143 | free(wo); | ||
144 | } | ||
145 | |||
146 | static void pid_workspace_cleanup() { | ||
147 | struct timespec ts; | ||
148 | struct pid_workspace *pw = NULL; | ||
149 | |||
150 | clock_gettime(CLOCK_MONOTONIC, &ts); | ||
151 | |||
152 | // work backwards through list and remove any entries | ||
153 | // older than PID_WORKSPACE_TIMEOUT | ||
154 | for (int i = config->pid_workspaces->length - 1; i > -1; i--) { | ||
155 | pw = config->pid_workspaces->items[i]; | ||
156 | |||
157 | if (difftime(ts.tv_sec, *pw->time_added) >= PID_WORKSPACE_TIMEOUT) { | ||
158 | free_pid_workspace(config->pid_workspaces->items[i]); | ||
159 | list_del(config->pid_workspaces, i); | ||
160 | } | ||
161 | } | ||
162 | } | ||
163 | |||
164 | // de-dupe pid_workspaces to ensure pid uniqueness | ||
165 | void pid_workspace_add(struct pid_workspace *pw) { | ||
166 | struct pid_workspace *list_pw = NULL; | ||
167 | struct timespec ts; | ||
168 | time_t *now = malloc(sizeof(time_t)); | ||
169 | if (!now) { | ||
170 | sway_log(L_ERROR, "Allocating time for pid_workspace failed"); | ||
171 | return; | ||
172 | } | ||
173 | |||
174 | pid_workspace_cleanup(); | ||
175 | |||
176 | // add current time to pw | ||
177 | clock_gettime(CLOCK_MONOTONIC, &ts); | ||
178 | *now = ts.tv_sec; | ||
179 | |||
180 | pw->time_added = now; | ||
181 | |||
182 | // work backwards through list and delete any entries that | ||
183 | // have the same pid as that in our new pid_workspace | ||
184 | for (int i = config->pid_workspaces->length - 1; i > -1; i--) { | ||
185 | list_pw = config->pid_workspaces->items[i]; | ||
186 | |||
187 | if (pw->pid == list_pw->pid) { | ||
188 | free_pid_workspace(config->pid_workspaces->items[i]); | ||
189 | list_del(config->pid_workspaces, i); | ||
190 | } | ||
191 | } | ||
192 | |||
193 | list_add(config->pid_workspaces, pw); | ||
194 | } | ||
195 | |||
196 | void free_pid_workspace(struct pid_workspace *pw) { | ||
197 | if (!pw) { | ||
198 | return; | ||
199 | } | ||
200 | free(pw->pid); | ||
201 | free(pw->workspace); | ||
202 | free(pw->time_added); | ||
203 | free(pw); | ||
204 | } | ||
205 | |||
206 | void free_command_policy(struct command_policy *policy) { | ||
207 | if (!policy) { | ||
208 | return; | ||
209 | } | ||
210 | free(policy->command); | ||
211 | free(policy); | ||
212 | } | ||
213 | |||
214 | void free_feature_policy(struct feature_policy *policy) { | ||
215 | if (!policy) { | ||
216 | return; | ||
217 | } | ||
218 | free(policy->program); | ||
219 | free(policy); | ||
220 | } | ||
221 | |||
222 | void free_config(struct sway_config *config) { | ||
223 | if (!config) { | ||
224 | return; | ||
225 | } | ||
226 | int i; | ||
227 | for (i = 0; config->symbols && i < config->symbols->length; ++i) { | ||
228 | free_variable(config->symbols->items[i]); | ||
229 | } | ||
230 | list_free(config->symbols); | ||
231 | |||
232 | for (i = 0; config->modes && i < config->modes->length; ++i) { | ||
233 | free_mode(config->modes->items[i]); | ||
234 | } | ||
235 | list_free(config->modes); | ||
236 | |||
237 | for (i = 0; config->bars && i < config->bars->length; ++i) { | ||
238 | free_bar(config->bars->items[i]); | ||
239 | } | ||
240 | list_free(config->bars); | ||
241 | |||
242 | free_flat_list(config->cmd_queue); | ||
243 | |||
244 | for (i = 0; config->workspace_outputs && i < config->workspace_outputs->length; ++i) { | ||
245 | free_workspace_output(config->workspace_outputs->items[i]); | ||
246 | } | ||
247 | list_free(config->workspace_outputs); | ||
248 | |||
249 | for (i = 0; config->pid_workspaces && i < config->pid_workspaces->length; ++i) { | ||
250 | free_pid_workspace(config->pid_workspaces->items[i]); | ||
251 | } | ||
252 | list_free(config->pid_workspaces); | ||
253 | |||
254 | for (i = 0; config->criteria && i < config->criteria->length; ++i) { | ||
255 | free_criteria(config->criteria->items[i]); | ||
256 | } | ||
257 | list_free(config->criteria); | ||
258 | |||
259 | for (i = 0; config->no_focus && i < config->no_focus->length; ++i) { | ||
260 | free_criteria(config->no_focus->items[i]); | ||
261 | } | ||
262 | list_free(config->no_focus); | ||
263 | |||
264 | for (i = 0; config->input_configs && i < config->input_configs->length; ++i) { | ||
265 | free_input_config(config->input_configs->items[i]); | ||
266 | } | ||
267 | list_free(config->input_configs); | ||
268 | |||
269 | for (i = 0; config->output_configs && i < config->output_configs->length; ++i) { | ||
270 | free_output_config(config->output_configs->items[i]); | ||
271 | } | ||
272 | list_free(config->output_configs); | ||
273 | |||
274 | for (i = 0; config->command_policies && i < config->command_policies->length; ++i) { | ||
275 | free_command_policy(config->command_policies->items[i]); | ||
276 | } | ||
277 | list_free(config->command_policies); | ||
278 | |||
279 | for (i = 0; config->feature_policies && i < config->feature_policies->length; ++i) { | ||
280 | free_feature_policy(config->feature_policies->items[i]); | ||
281 | } | ||
282 | list_free(config->feature_policies); | ||
283 | |||
284 | list_free(config->active_bar_modifiers); | ||
285 | free_flat_list(config->config_chain); | ||
286 | free(config->font); | ||
287 | free(config->floating_scroll_up_cmd); | ||
288 | free(config->floating_scroll_down_cmd); | ||
289 | free(config->floating_scroll_left_cmd); | ||
290 | free(config->floating_scroll_right_cmd); | ||
291 | free(config); | ||
292 | } | ||
293 | |||
294 | |||
295 | static bool file_exists(const char *path) { | ||
296 | return path && access(path, R_OK) != -1; | ||
297 | } | ||
298 | |||
299 | static void config_defaults(struct sway_config *config) { | ||
300 | if (!(config->symbols = create_list())) goto cleanup; | ||
301 | if (!(config->modes = create_list())) goto cleanup; | ||
302 | if (!(config->bars = create_list())) goto cleanup; | ||
303 | if (!(config->workspace_outputs = create_list())) goto cleanup; | ||
304 | if (!(config->pid_workspaces = create_list())) goto cleanup; | ||
305 | if (!(config->criteria = create_list())) goto cleanup; | ||
306 | if (!(config->no_focus = create_list())) goto cleanup; | ||
307 | if (!(config->input_configs = create_list())) goto cleanup; | ||
308 | if (!(config->output_configs = create_list())) goto cleanup; | ||
309 | |||
310 | if (!(config->cmd_queue = create_list())) goto cleanup; | ||
311 | |||
312 | if (!(config->current_mode = malloc(sizeof(struct sway_mode)))) goto cleanup; | ||
313 | if (!(config->current_mode->name = malloc(sizeof("default")))) goto cleanup; | ||
314 | strcpy(config->current_mode->name, "default"); | ||
315 | if (!(config->current_mode->bindings = create_list())) goto cleanup; | ||
316 | list_add(config->modes, config->current_mode); | ||
317 | |||
318 | config->floating_mod = 0; | ||
319 | config->dragging_key = M_LEFT_CLICK; | ||
320 | config->resizing_key = M_RIGHT_CLICK; | ||
321 | if (!(config->floating_scroll_up_cmd = strdup(""))) goto cleanup; | ||
322 | if (!(config->floating_scroll_down_cmd = strdup(""))) goto cleanup; | ||
323 | if (!(config->floating_scroll_left_cmd = strdup(""))) goto cleanup; | ||
324 | if (!(config->floating_scroll_right_cmd = strdup(""))) goto cleanup; | ||
325 | config->default_layout = L_NONE; | ||
326 | config->default_orientation = L_NONE; | ||
327 | if (!(config->font = strdup("monospace 10"))) goto cleanup; | ||
328 | config->font_height = get_font_text_height(config->font); | ||
329 | |||
330 | // floating view | ||
331 | config->floating_maximum_width = 0; | ||
332 | config->floating_maximum_height = 0; | ||
333 | config->floating_minimum_width = 75; | ||
334 | config->floating_minimum_height = 50; | ||
335 | |||
336 | // Flags | ||
337 | config->focus_follows_mouse = true; | ||
338 | config->mouse_warping = true; | ||
339 | config->reloading = false; | ||
340 | config->active = false; | ||
341 | config->failed = false; | ||
342 | config->auto_back_and_forth = false; | ||
343 | config->seamless_mouse = true; | ||
344 | config->reading = false; | ||
345 | config->show_marks = true; | ||
346 | |||
347 | config->edge_gaps = true; | ||
348 | config->smart_gaps = false; | ||
349 | config->gaps_inner = 0; | ||
350 | config->gaps_outer = 0; | ||
351 | |||
352 | if (!(config->active_bar_modifiers = create_list())) goto cleanup; | ||
353 | |||
354 | if (!(config->config_chain = create_list())) goto cleanup; | ||
355 | config->current_config = NULL; | ||
356 | |||
357 | // borders | ||
358 | config->border = B_NORMAL; | ||
359 | config->floating_border = B_NORMAL; | ||
360 | config->border_thickness = 2; | ||
361 | config->floating_border_thickness = 2; | ||
362 | config->hide_edge_borders = E_NONE; | ||
363 | |||
364 | // border colors | ||
365 | config->border_colors.focused.border = 0x4C7899FF; | ||
366 | config->border_colors.focused.background = 0x285577FF; | ||
367 | config->border_colors.focused.text = 0xFFFFFFFF; | ||
368 | config->border_colors.focused.indicator = 0x2E9EF4FF; | ||
369 | config->border_colors.focused.child_border = 0x285577FF; | ||
370 | |||
371 | config->border_colors.focused_inactive.border = 0x333333FF; | ||
372 | config->border_colors.focused_inactive.background = 0x5F676AFF; | ||
373 | config->border_colors.focused_inactive.text = 0xFFFFFFFF; | ||
374 | config->border_colors.focused_inactive.indicator = 0x484E50FF; | ||
375 | config->border_colors.focused_inactive.child_border = 0x5F676AFF; | ||
376 | |||
377 | config->border_colors.unfocused.border = 0x333333FF; | ||
378 | config->border_colors.unfocused.background = 0x222222FF; | ||
379 | config->border_colors.unfocused.text = 0x888888FF; | ||
380 | config->border_colors.unfocused.indicator = 0x292D2EFF; | ||
381 | config->border_colors.unfocused.child_border = 0x222222FF; | ||
382 | |||
383 | config->border_colors.urgent.border = 0x2F343AFF; | ||
384 | config->border_colors.urgent.background = 0x900000FF; | ||
385 | config->border_colors.urgent.text = 0xFFFFFFFF; | ||
386 | config->border_colors.urgent.indicator = 0x900000FF; | ||
387 | config->border_colors.urgent.child_border = 0x900000FF; | ||
388 | |||
389 | config->border_colors.placeholder.border = 0x000000FF; | ||
390 | config->border_colors.placeholder.background = 0x0C0C0CFF; | ||
391 | config->border_colors.placeholder.text = 0xFFFFFFFF; | ||
392 | config->border_colors.placeholder.indicator = 0x000000FF; | ||
393 | config->border_colors.placeholder.child_border = 0x0C0C0CFF; | ||
394 | |||
395 | config->border_colors.background = 0xFFFFFFFF; | ||
396 | |||
397 | // Security | ||
398 | if (!(config->command_policies = create_list())) goto cleanup; | ||
399 | if (!(config->feature_policies = create_list())) goto cleanup; | ||
400 | if (!(config->ipc_policies = create_list())) goto cleanup; | ||
401 | |||
402 | return; | ||
403 | cleanup: | ||
404 | sway_abort("Unable to allocate config structures"); | ||
405 | } | ||
406 | |||
407 | static int compare_modifiers(const void *left, const void *right) { | ||
408 | uint32_t a = *(uint32_t *)left; | ||
409 | uint32_t b = *(uint32_t *)right; | ||
410 | |||
411 | return a - b; | ||
412 | } | ||
413 | |||
414 | void update_active_bar_modifiers() { | ||
415 | if (config->active_bar_modifiers->length > 0) { | ||
416 | list_free(config->active_bar_modifiers); | ||
417 | config->active_bar_modifiers = create_list(); | ||
418 | } | ||
419 | |||
420 | struct bar_config *bar; | ||
421 | int i; | ||
422 | for (i = 0; i < config->bars->length; ++i) { | ||
423 | bar = config->bars->items[i]; | ||
424 | if (strcmp(bar->mode, "hide") == 0 && strcmp(bar->hidden_state, "hide") == 0) { | ||
425 | if (list_seq_find(config->active_bar_modifiers, compare_modifiers, &bar->modifier) < 0) { | ||
426 | list_add(config->active_bar_modifiers, &bar->modifier); | ||
427 | } | ||
428 | } | ||
429 | } | ||
430 | } | ||
431 | |||
432 | static char *get_config_path(void) { | ||
433 | static const char *config_paths[] = { | ||
434 | "$HOME/.sway/config", | ||
435 | "$XDG_CONFIG_HOME/sway/config", | ||
436 | "$HOME/.i3/config", | ||
437 | "$XDG_CONFIG_HOME/i3/config", | ||
438 | SYSCONFDIR "/sway/config", | ||
439 | SYSCONFDIR "/i3/config", | ||
440 | }; | ||
441 | |||
442 | if (!getenv("XDG_CONFIG_HOME")) { | ||
443 | char *home = getenv("HOME"); | ||
444 | char *config_home = malloc(strlen(home) + strlen("/.config") + 1); | ||
445 | if (!config_home) { | ||
446 | sway_log(L_ERROR, "Unable to allocate $HOME/.config"); | ||
447 | } else { | ||
448 | strcpy(config_home, home); | ||
449 | strcat(config_home, "/.config"); | ||
450 | setenv("XDG_CONFIG_HOME", config_home, 1); | ||
451 | sway_log(L_DEBUG, "Set XDG_CONFIG_HOME to %s", config_home); | ||
452 | free(config_home); | ||
453 | } | ||
454 | } | ||
455 | |||
456 | wordexp_t p; | ||
457 | char *path; | ||
458 | |||
459 | int i; | ||
460 | for (i = 0; i < (int)(sizeof(config_paths) / sizeof(char *)); ++i) { | ||
461 | if (wordexp(config_paths[i], &p, 0) == 0) { | ||
462 | path = strdup(p.we_wordv[0]); | ||
463 | wordfree(&p); | ||
464 | if (file_exists(path)) { | ||
465 | return path; | ||
466 | } | ||
467 | } | ||
468 | } | ||
469 | |||
470 | return NULL; // Not reached | ||
471 | } | ||
472 | |||
473 | const char *current_config_path; | ||
474 | |||
475 | static bool load_config(const char *path, struct sway_config *config) { | ||
476 | sway_log(L_INFO, "Loading config from %s", path); | ||
477 | current_config_path = path; | ||
478 | |||
479 | struct stat sb; | ||
480 | if (stat(path, &sb) == 0 && S_ISDIR(sb.st_mode)) { | ||
481 | return false; | ||
482 | } | ||
483 | |||
484 | if (path == NULL) { | ||
485 | sway_log(L_ERROR, "Unable to find a config file!"); | ||
486 | return false; | ||
487 | } | ||
488 | |||
489 | FILE *f = fopen(path, "r"); | ||
490 | if (!f) { | ||
491 | sway_log(L_ERROR, "Unable to open %s for reading", path); | ||
492 | return false; | ||
493 | } | ||
494 | |||
495 | bool config_load_success = read_config(f, config); | ||
496 | fclose(f); | ||
497 | |||
498 | if (!config_load_success) { | ||
499 | sway_log(L_ERROR, "Error(s) loading config!"); | ||
500 | } | ||
501 | |||
502 | current_config_path = NULL; | ||
503 | return true; | ||
504 | } | ||
505 | |||
506 | static int qstrcmp(const void* a, const void* b) { | ||
507 | return strcmp(*((char**) a), *((char**) b)); | ||
508 | } | ||
509 | |||
510 | bool load_main_config(const char *file, bool is_active) { | ||
511 | input_init(); | ||
512 | |||
513 | char *path; | ||
514 | if (file != NULL) { | ||
515 | path = strdup(file); | ||
516 | } else { | ||
517 | path = get_config_path(); | ||
518 | } | ||
519 | |||
520 | struct sway_config *old_config = config; | ||
521 | config = calloc(1, sizeof(struct sway_config)); | ||
522 | if (!config) { | ||
523 | sway_abort("Unable to allocate config"); | ||
524 | } | ||
525 | |||
526 | config_defaults(config); | ||
527 | if (is_active) { | ||
528 | sway_log(L_DEBUG, "Performing configuration file reload"); | ||
529 | config->reloading = true; | ||
530 | config->active = true; | ||
531 | } | ||
532 | |||
533 | config->current_config = path; | ||
534 | list_add(config->config_chain, path); | ||
535 | |||
536 | config->reading = true; | ||
537 | |||
538 | // Read security configs | ||
539 | bool success = true; | ||
540 | DIR *dir = opendir(SYSCONFDIR "/sway/security.d"); | ||
541 | if (!dir) { | ||
542 | sway_log(L_ERROR, "%s does not exist, sway will have no security configuration" | ||
543 | " and will probably be broken", SYSCONFDIR "/sway/security.d"); | ||
544 | } else { | ||
545 | list_t *secconfigs = create_list(); | ||
546 | char *base = SYSCONFDIR "/sway/security.d/"; | ||
547 | struct dirent *ent = readdir(dir); | ||
548 | struct stat s; | ||
549 | while (ent != NULL) { | ||
550 | char *_path = malloc(strlen(ent->d_name) + strlen(base) + 1); | ||
551 | strcpy(_path, base); | ||
552 | strcat(_path, ent->d_name); | ||
553 | lstat(_path, &s); | ||
554 | if (S_ISREG(s.st_mode) && ent->d_name[0] != '.') { | ||
555 | list_add(secconfigs, _path); | ||
556 | } | ||
557 | else { | ||
558 | free(_path); | ||
559 | } | ||
560 | ent = readdir(dir); | ||
561 | } | ||
562 | closedir(dir); | ||
563 | |||
564 | list_qsort(secconfigs, qstrcmp); | ||
565 | for (int i = 0; i < secconfigs->length; ++i) { | ||
566 | char *_path = secconfigs->items[i]; | ||
567 | if (stat(_path, &s) || s.st_uid != 0 || s.st_gid != 0 || (((s.st_mode & 0777) != 0644) && (s.st_mode & 0777) != 0444)) { | ||
568 | sway_log(L_ERROR, "Refusing to load %s - it must be owned by root and mode 644 or 444", _path); | ||
569 | success = false; | ||
570 | } else { | ||
571 | success = success && load_config(_path, config); | ||
572 | } | ||
573 | } | ||
574 | |||
575 | free_flat_list(secconfigs); | ||
576 | } | ||
577 | |||
578 | success = success && load_config(path, config); | ||
579 | |||
580 | if (is_active) { | ||
581 | config->reloading = false; | ||
582 | } | ||
583 | |||
584 | if (old_config) { | ||
585 | free_config(old_config); | ||
586 | } | ||
587 | config->reading = false; | ||
588 | |||
589 | if (success) { | ||
590 | update_active_bar_modifiers(); | ||
591 | } | ||
592 | |||
593 | return success; | ||
594 | } | ||
595 | |||
596 | static bool load_include_config(const char *path, const char *parent_dir, struct sway_config *config) { | ||
597 | // save parent config | ||
598 | const char *parent_config = config->current_config; | ||
599 | |||
600 | char *full_path = strdup(path); | ||
601 | int len = strlen(path); | ||
602 | if (len >= 1 && path[0] != '/') { | ||
603 | len = len + strlen(parent_dir) + 2; | ||
604 | full_path = malloc(len * sizeof(char)); | ||
605 | if (!full_path) { | ||
606 | sway_log(L_ERROR, "Unable to allocate full path to included config"); | ||
607 | return false; | ||
608 | } | ||
609 | snprintf(full_path, len, "%s/%s", parent_dir, path); | ||
610 | } | ||
611 | |||
612 | char *real_path = realpath(full_path, NULL); | ||
613 | free(full_path); | ||
614 | |||
615 | if (real_path == NULL) { | ||
616 | sway_log(L_DEBUG, "%s not found.", path); | ||
617 | return false; | ||
618 | } | ||
619 | |||
620 | // check if config has already been included | ||
621 | int j; | ||
622 | for (j = 0; j < config->config_chain->length; ++j) { | ||
623 | char *old_path = config->config_chain->items[j]; | ||
624 | if (strcmp(real_path, old_path) == 0) { | ||
625 | sway_log(L_DEBUG, "%s already included once, won't be included again.", real_path); | ||
626 | free(real_path); | ||
627 | return false; | ||
628 | } | ||
629 | } | ||
630 | |||
631 | config->current_config = real_path; | ||
632 | list_add(config->config_chain, real_path); | ||
633 | int index = config->config_chain->length - 1; | ||
634 | |||
635 | if (!load_config(real_path, config)) { | ||
636 | free(real_path); | ||
637 | config->current_config = parent_config; | ||
638 | list_del(config->config_chain, index); | ||
639 | return false; | ||
640 | } | ||
641 | |||
642 | // restore current_config | ||
643 | config->current_config = parent_config; | ||
644 | return true; | ||
645 | } | ||
646 | |||
647 | bool load_include_configs(const char *path, struct sway_config *config) { | ||
648 | char *wd = getcwd(NULL, 0); | ||
649 | char *parent_path = strdup(config->current_config); | ||
650 | const char *parent_dir = dirname(parent_path); | ||
651 | |||
652 | if (chdir(parent_dir) < 0) { | ||
653 | free(parent_path); | ||
654 | free(wd); | ||
655 | return false; | ||
656 | } | ||
657 | |||
658 | wordexp_t p; | ||
659 | |||
660 | if (wordexp(path, &p, 0) < 0) { | ||
661 | free(parent_path); | ||
662 | free(wd); | ||
663 | return false; | ||
664 | } | ||
665 | |||
666 | char **w = p.we_wordv; | ||
667 | size_t i; | ||
668 | for (i = 0; i < p.we_wordc; ++i) { | ||
669 | load_include_config(w[i], parent_dir, config); | ||
670 | } | ||
671 | free(parent_path); | ||
672 | wordfree(&p); | ||
673 | |||
674 | // restore wd | ||
675 | if (chdir(wd) < 0) { | ||
676 | free(wd); | ||
677 | sway_log(L_ERROR, "failed to restore working directory"); | ||
678 | return false; | ||
679 | } | ||
680 | |||
681 | free(wd); | ||
682 | return true; | ||
683 | } | ||
684 | |||
685 | struct cmd_results *check_security_config() { | ||
686 | if (!current_config_path || strncmp(SYSCONFDIR "/sway/security.d/", current_config_path, | ||
687 | strlen(SYSCONFDIR "/sway/security.d/")) != 0) { | ||
688 | return cmd_results_new(CMD_INVALID, "permit", | ||
689 | "This command is only permitted to run from " SYSCONFDIR "/sway/security.d/*"); | ||
690 | } | ||
691 | return NULL; | ||
692 | } | ||
693 | |||
694 | bool read_config(FILE *file, struct sway_config *config) { | ||
695 | bool success = true; | ||
696 | enum cmd_status block = CMD_BLOCK_END; | ||
697 | |||
698 | int line_number = 0; | ||
699 | char *line; | ||
700 | while (!feof(file)) { | ||
701 | line = read_line(file); | ||
702 | if (!line) { | ||
703 | continue; | ||
704 | } | ||
705 | line_number++; | ||
706 | line = strip_whitespace(line); | ||
707 | if (line[0] == '#') { | ||
708 | free(line); | ||
709 | continue; | ||
710 | } | ||
711 | struct cmd_results *res; | ||
712 | if (block == CMD_BLOCK_COMMANDS) { | ||
713 | // Special case | ||
714 | res = config_commands_command(line); | ||
715 | } else { | ||
716 | res = config_command(line, block); | ||
717 | } | ||
718 | switch(res->status) { | ||
719 | case CMD_FAILURE: | ||
720 | case CMD_INVALID: | ||
721 | sway_log(L_ERROR, "Error on line %i '%s': %s (%s)", line_number, line, | ||
722 | res->error, config->current_config); | ||
723 | success = false; | ||
724 | break; | ||
725 | |||
726 | case CMD_DEFER: | ||
727 | sway_log(L_DEBUG, "Defferring command `%s'", line); | ||
728 | list_add(config->cmd_queue, strdup(line)); | ||
729 | break; | ||
730 | |||
731 | case CMD_BLOCK_MODE: | ||
732 | if (block == CMD_BLOCK_END) { | ||
733 | block = CMD_BLOCK_MODE; | ||
734 | } else { | ||
735 | sway_log(L_ERROR, "Invalid block '%s'", line); | ||
736 | } | ||
737 | break; | ||
738 | |||
739 | case CMD_BLOCK_INPUT: | ||
740 | if (block == CMD_BLOCK_END) { | ||
741 | block = CMD_BLOCK_INPUT; | ||
742 | } else { | ||
743 | sway_log(L_ERROR, "Invalid block '%s'", line); | ||
744 | } | ||
745 | break; | ||
746 | |||
747 | case CMD_BLOCK_BAR: | ||
748 | if (block == CMD_BLOCK_END) { | ||
749 | block = CMD_BLOCK_BAR; | ||
750 | } else { | ||
751 | sway_log(L_ERROR, "Invalid block '%s'", line); | ||
752 | } | ||
753 | break; | ||
754 | |||
755 | case CMD_BLOCK_BAR_COLORS: | ||
756 | if (block == CMD_BLOCK_BAR) { | ||
757 | block = CMD_BLOCK_BAR_COLORS; | ||
758 | } else { | ||
759 | sway_log(L_ERROR, "Invalid block '%s'", line); | ||
760 | } | ||
761 | break; | ||
762 | |||
763 | case CMD_BLOCK_COMMANDS: | ||
764 | if (block == CMD_BLOCK_END) { | ||
765 | block = CMD_BLOCK_COMMANDS; | ||
766 | } else { | ||
767 | sway_log(L_ERROR, "Invalid block '%s'", line); | ||
768 | } | ||
769 | break; | ||
770 | |||
771 | case CMD_BLOCK_IPC: | ||
772 | if (block == CMD_BLOCK_END) { | ||
773 | block = CMD_BLOCK_IPC; | ||
774 | } else { | ||
775 | sway_log(L_ERROR, "Invalid block '%s'", line); | ||
776 | } | ||
777 | break; | ||
778 | |||
779 | case CMD_BLOCK_IPC_EVENTS: | ||
780 | if (block == CMD_BLOCK_IPC) { | ||
781 | block = CMD_BLOCK_IPC_EVENTS; | ||
782 | } else { | ||
783 | sway_log(L_ERROR, "Invalid block '%s'", line); | ||
784 | } | ||
785 | break; | ||
786 | |||
787 | case CMD_BLOCK_END: | ||
788 | switch(block) { | ||
789 | case CMD_BLOCK_MODE: | ||
790 | sway_log(L_DEBUG, "End of mode block"); | ||
791 | config->current_mode = config->modes->items[0]; | ||
792 | block = CMD_BLOCK_END; | ||
793 | break; | ||
794 | |||
795 | case CMD_BLOCK_INPUT: | ||
796 | sway_log(L_DEBUG, "End of input block"); | ||
797 | current_input_config = NULL; | ||
798 | block = CMD_BLOCK_END; | ||
799 | break; | ||
800 | |||
801 | case CMD_BLOCK_BAR: | ||
802 | sway_log(L_DEBUG, "End of bar block"); | ||
803 | config->current_bar = NULL; | ||
804 | block = CMD_BLOCK_END; | ||
805 | break; | ||
806 | |||
807 | case CMD_BLOCK_BAR_COLORS: | ||
808 | sway_log(L_DEBUG, "End of bar colors block"); | ||
809 | block = CMD_BLOCK_BAR; | ||
810 | break; | ||
811 | |||
812 | case CMD_BLOCK_COMMANDS: | ||
813 | sway_log(L_DEBUG, "End of commands block"); | ||
814 | block = CMD_BLOCK_END; | ||
815 | break; | ||
816 | |||
817 | case CMD_BLOCK_IPC: | ||
818 | sway_log(L_DEBUG, "End of IPC block"); | ||
819 | block = CMD_BLOCK_END; | ||
820 | break; | ||
821 | |||
822 | case CMD_BLOCK_IPC_EVENTS: | ||
823 | sway_log(L_DEBUG, "End of IPC events block"); | ||
824 | block = CMD_BLOCK_IPC; | ||
825 | break; | ||
826 | |||
827 | case CMD_BLOCK_END: | ||
828 | sway_log(L_ERROR, "Unmatched }"); | ||
829 | break; | ||
830 | |||
831 | default:; | ||
832 | } | ||
833 | default:; | ||
834 | } | ||
835 | free(line); | ||
836 | free_cmd_results(res); | ||
837 | } | ||
838 | |||
839 | return success; | ||
840 | } | ||
841 | |||
842 | int input_identifier_cmp(const void *item, const void *data) { | ||
843 | const struct input_config *ic = item; | ||
844 | const char *identifier = data; | ||
845 | return strcmp(ic->identifier, identifier); | ||
846 | } | ||
847 | |||
848 | int output_name_cmp(const void *item, const void *data) { | ||
849 | const struct output_config *output = item; | ||
850 | const char *name = data; | ||
851 | |||
852 | return strcmp(output->name, name); | ||
853 | } | ||
854 | |||
855 | void merge_input_config(struct input_config *dst, struct input_config *src) { | ||
856 | if (src->identifier) { | ||
857 | if (dst->identifier) { | ||
858 | free(dst->identifier); | ||
859 | } | ||
860 | dst->identifier = strdup(src->identifier); | ||
861 | } | ||
862 | if (src->accel_profile != INT_MIN) { | ||
863 | dst->accel_profile = src->accel_profile; | ||
864 | } | ||
865 | if (src->click_method != INT_MIN) { | ||
866 | dst->click_method = src->click_method; | ||
867 | } | ||
868 | if (src->drag_lock != INT_MIN) { | ||
869 | dst->drag_lock = src->drag_lock; | ||
870 | } | ||
871 | if (src->dwt != INT_MIN) { | ||
872 | dst->dwt = src->dwt; | ||
873 | } | ||
874 | if (src->middle_emulation != INT_MIN) { | ||
875 | dst->middle_emulation = src->middle_emulation; | ||
876 | } | ||
877 | if (src->natural_scroll != INT_MIN) { | ||
878 | dst->natural_scroll = src->natural_scroll; | ||
879 | } | ||
880 | if (src->pointer_accel != FLT_MIN) { | ||
881 | dst->pointer_accel = src->pointer_accel; | ||
882 | } | ||
883 | if (src->scroll_method != INT_MIN) { | ||
884 | dst->scroll_method = src->scroll_method; | ||
885 | } | ||
886 | if (src->send_events != INT_MIN) { | ||
887 | dst->send_events = src->send_events; | ||
888 | } | ||
889 | if (src->tap != INT_MIN) { | ||
890 | dst->tap = src->tap; | ||
891 | } | ||
892 | } | ||
893 | |||
894 | void merge_output_config(struct output_config *dst, struct output_config *src) { | ||
895 | if (src->name) { | ||
896 | if (dst->name) { | ||
897 | free(dst->name); | ||
898 | } | ||
899 | dst->name = strdup(src->name); | ||
900 | } | ||
901 | if (src->enabled != -1) { | ||
902 | dst->enabled = src->enabled; | ||
903 | } | ||
904 | if (src->width != -1) { | ||
905 | dst->width = src->width; | ||
906 | } | ||
907 | if (src->height != -1) { | ||
908 | dst->height = src->height; | ||
909 | } | ||
910 | if (src->x != -1) { | ||
911 | dst->x = src->x; | ||
912 | } | ||
913 | if (src->y != -1) { | ||
914 | dst->y = src->y; | ||
915 | } | ||
916 | if (src->scale != -1) { | ||
917 | dst->scale = src->scale; | ||
918 | } | ||
919 | if (src->background) { | ||
920 | if (dst->background) { | ||
921 | free(dst->background); | ||
922 | } | ||
923 | dst->background = strdup(src->background); | ||
924 | } | ||
925 | if (src->background_option) { | ||
926 | if (dst->background_option) { | ||
927 | free(dst->background_option); | ||
928 | } | ||
929 | dst->background_option = strdup(src->background_option); | ||
930 | } | ||
931 | } | ||
932 | |||
933 | static void invoke_swaybar(struct bar_config *bar) { | ||
934 | return; // TODO WLR | ||
935 | sway_log(L_DEBUG, "Invoking swaybar for bar id '%s'", bar->id); | ||
936 | // Pipe to communicate errors | ||
937 | int filedes[2]; | ||
938 | if (pipe(filedes) == -1) { | ||
939 | sway_log(L_ERROR, "Pipe setup failed! Cannot fork into bar"); | ||
940 | return; | ||
941 | } | ||
942 | |||
943 | bar->pid = fork(); | ||
944 | if (bar->pid == 0) { | ||
945 | close(filedes[0]); | ||
946 | if (!bar->swaybar_command) { | ||
947 | char *const cmd[] = { | ||
948 | "swaybar", | ||
949 | "-b", | ||
950 | bar->id, | ||
951 | NULL, | ||
952 | }; | ||
953 | |||
954 | close(filedes[1]); | ||
955 | execvp(cmd[0], cmd); | ||
956 | _exit(EXIT_SUCCESS); | ||
957 | } else { | ||
958 | // run custom swaybar | ||
959 | int len = strlen(bar->swaybar_command) + strlen(bar->id) + 5; | ||
960 | char *command = malloc(len * sizeof(char)); | ||
961 | if (!command) { | ||
962 | const char msg[] = "Unable to allocate swaybar command string"; | ||
963 | int len = sizeof(msg); | ||
964 | if (write(filedes[1], &len, sizeof(int))) {}; | ||
965 | if (write(filedes[1], msg, len)) {}; | ||
966 | close(filedes[1]); | ||
967 | _exit(EXIT_FAILURE); | ||
968 | } | ||
969 | snprintf(command, len, "%s -b %s", bar->swaybar_command, bar->id); | ||
970 | |||
971 | char *const cmd[] = { | ||
972 | "sh", | ||
973 | "-c", | ||
974 | command, | ||
975 | NULL, | ||
976 | }; | ||
977 | |||
978 | close(filedes[1]); | ||
979 | execvp(cmd[0], cmd); | ||
980 | free(command); | ||
981 | _exit(EXIT_SUCCESS); | ||
982 | } | ||
983 | } | ||
984 | close(filedes[0]); | ||
985 | int len; | ||
986 | if(read(filedes[1], &len, sizeof(int)) == sizeof(int)) { | ||
987 | char *buf = malloc(len); | ||
988 | if(!buf) { | ||
989 | sway_log(L_ERROR, "Cannot allocate error string"); | ||
990 | return; | ||
991 | } | ||
992 | if(read(filedes[1], buf, len)) { | ||
993 | sway_log(L_ERROR, "%s", buf); | ||
994 | } | ||
995 | free(buf); | ||
996 | } | ||
997 | close(filedes[1]); | ||
998 | } | ||
999 | |||
1000 | static void terminate_swaybar(pid_t pid) { | ||
1001 | return; // TODO WLR | ||
1002 | int ret = kill(pid, SIGTERM); | ||
1003 | if (ret != 0) { | ||
1004 | sway_log(L_ERROR, "Unable to terminate swaybar [pid: %d]", pid); | ||
1005 | } else { | ||
1006 | int status; | ||
1007 | waitpid(pid, &status, 0); | ||
1008 | } | ||
1009 | } | ||
1010 | |||
1011 | void terminate_swaybg(pid_t pid) { | ||
1012 | int ret = kill(pid, SIGTERM); | ||
1013 | if (ret != 0) { | ||
1014 | sway_log(L_ERROR, "Unable to terminate swaybg [pid: %d]", pid); | ||
1015 | } else { | ||
1016 | int status; | ||
1017 | waitpid(pid, &status, 0); | ||
1018 | } | ||
1019 | } | ||
1020 | |||
1021 | static bool active_output(const char *name) { | ||
1022 | int i; | ||
1023 | swayc_t *cont = NULL; | ||
1024 | for (i = 0; i < root_container.children->length; ++i) { | ||
1025 | cont = root_container.children->items[i]; | ||
1026 | if (cont->type == C_OUTPUT && strcasecmp(name, cont->name) == 0) { | ||
1027 | return true; | ||
1028 | } | ||
1029 | } | ||
1030 | |||
1031 | return false; | ||
1032 | } | ||
1033 | |||
1034 | void load_swaybars() { | ||
1035 | // Check for bars | ||
1036 | list_t *bars = create_list(); | ||
1037 | struct bar_config *bar = NULL; | ||
1038 | int i; | ||
1039 | for (i = 0; i < config->bars->length; ++i) { | ||
1040 | bar = config->bars->items[i]; | ||
1041 | bool apply = false; | ||
1042 | if (bar->outputs) { | ||
1043 | int j; | ||
1044 | for (j = 0; j < bar->outputs->length; ++j) { | ||
1045 | char *o = bar->outputs->items[j]; | ||
1046 | if (!strcmp(o, "*") || active_output(o)) { | ||
1047 | apply = true; | ||
1048 | break; | ||
1049 | } | ||
1050 | } | ||
1051 | } else { | ||
1052 | apply = true; | ||
1053 | } | ||
1054 | if (apply) { | ||
1055 | list_add(bars, bar); | ||
1056 | } | ||
1057 | } | ||
1058 | |||
1059 | for (i = 0; i < bars->length; ++i) { | ||
1060 | bar = bars->items[i]; | ||
1061 | if (bar->pid != 0) { | ||
1062 | terminate_swaybar(bar->pid); | ||
1063 | } | ||
1064 | invoke_swaybar(bar); | ||
1065 | } | ||
1066 | |||
1067 | list_free(bars); | ||
1068 | } | ||
1069 | |||
1070 | char *do_var_replacement(char *str) { | ||
1071 | int i; | ||
1072 | char *find = str; | ||
1073 | while ((find = strchr(find, '$'))) { | ||
1074 | // Skip if escaped. | ||
1075 | if (find > str && find[-1] == '\\') { | ||
1076 | if (find == str + 1 || !(find > str + 1 && find[-2] == '\\')) { | ||
1077 | ++find; | ||
1078 | continue; | ||
1079 | } | ||
1080 | } | ||
1081 | // Find matching variable | ||
1082 | for (i = 0; i < config->symbols->length; ++i) { | ||
1083 | struct sway_variable *var = config->symbols->items[i]; | ||
1084 | int vnlen = strlen(var->name); | ||
1085 | if (strncmp(find, var->name, vnlen) == 0) { | ||
1086 | int vvlen = strlen(var->value); | ||
1087 | char *newstr = malloc(strlen(str) - vnlen + vvlen + 1); | ||
1088 | if (!newstr) { | ||
1089 | sway_log(L_ERROR, | ||
1090 | "Unable to allocate replacement during variable expansion"); | ||
1091 | break; | ||
1092 | } | ||
1093 | char *newptr = newstr; | ||
1094 | int offset = find - str; | ||
1095 | strncpy(newptr, str, offset); | ||
1096 | newptr += offset; | ||
1097 | strncpy(newptr, var->value, vvlen); | ||
1098 | newptr += vvlen; | ||
1099 | strcpy(newptr, find + vnlen); | ||
1100 | free(str); | ||
1101 | str = newstr; | ||
1102 | find = str + offset + vvlen; | ||
1103 | break; | ||
1104 | } | ||
1105 | } | ||
1106 | if (i == config->symbols->length) { | ||
1107 | ++find; | ||
1108 | } | ||
1109 | } | ||
1110 | return str; | ||
1111 | } | ||
1112 | |||
1113 | // the naming is intentional (albeit long): a workspace_output_cmp function | ||
1114 | // would compare two structs in full, while this method only compares the | ||
1115 | // workspace. | ||
1116 | int workspace_output_cmp_workspace(const void *a, const void *b) { | ||
1117 | const struct workspace_output *wsa = a, *wsb = b; | ||
1118 | return lenient_strcmp(wsa->workspace, wsb->workspace); | ||
1119 | } | ||
1120 | |||
1121 | int sway_binding_cmp_keys(const void *a, const void *b) { | ||
1122 | const struct sway_binding *binda = a, *bindb = b; | ||
1123 | |||
1124 | // Count keys pressed for this binding. important so we check long before | ||
1125 | // short ones. for example mod+a+b before mod+a | ||
1126 | unsigned int moda = 0, modb = 0, i; | ||
1127 | |||
1128 | // Count how any modifiers are pressed | ||
1129 | for (i = 0; i < 8 * sizeof(binda->modifiers); ++i) { | ||
1130 | moda += (binda->modifiers & 1 << i) != 0; | ||
1131 | modb += (bindb->modifiers & 1 << i) != 0; | ||
1132 | } | ||
1133 | if (bindb->keys->length + modb != binda->keys->length + moda) { | ||
1134 | return (bindb->keys->length + modb) - (binda->keys->length + moda); | ||
1135 | } | ||
1136 | |||
1137 | // Otherwise compare keys | ||
1138 | if (binda->modifiers > bindb->modifiers) { | ||
1139 | return 1; | ||
1140 | } else if (binda->modifiers < bindb->modifiers) { | ||
1141 | return -1; | ||
1142 | } | ||
1143 | struct wlc_modifiers no_mods = { 0, 0 }; | ||
1144 | for (int i = 0; i < binda->keys->length; i++) { | ||
1145 | xkb_keysym_t ka = *(xkb_keysym_t *)binda->keys->items[i], | ||
1146 | kb = *(xkb_keysym_t *)bindb->keys->items[i]; | ||
1147 | if (binda->bindcode) { | ||
1148 | uint32_t *keycode = binda->keys->items[i]; | ||
1149 | ka = wlc_keyboard_get_keysym_for_key(*keycode, &no_mods); | ||
1150 | } | ||
1151 | |||
1152 | if (bindb->bindcode) { | ||
1153 | uint32_t *keycode = bindb->keys->items[i]; | ||
1154 | kb = wlc_keyboard_get_keysym_for_key(*keycode, &no_mods); | ||
1155 | } | ||
1156 | |||
1157 | if (ka > kb) { | ||
1158 | return 1; | ||
1159 | } else if (ka < kb) { | ||
1160 | return -1; | ||
1161 | } | ||
1162 | } | ||
1163 | |||
1164 | return 0; | ||
1165 | } | ||
1166 | |||
1167 | int sway_binding_cmp(const void *a, const void *b) { | ||
1168 | int cmp = 0; | ||
1169 | if ((cmp = sway_binding_cmp_keys(a, b)) != 0) { | ||
1170 | return cmp; | ||
1171 | } | ||
1172 | const struct sway_binding *binda = a, *bindb = b; | ||
1173 | return lenient_strcmp(binda->command, bindb->command); | ||
1174 | } | ||
1175 | |||
1176 | int sway_binding_cmp_qsort(const void *a, const void *b) { | ||
1177 | return sway_binding_cmp(*(void **)a, *(void **)b); | ||
1178 | } | ||
1179 | |||
1180 | void free_sway_binding(struct sway_binding *binding) { | ||
1181 | if (binding->keys) { | ||
1182 | for (int i = 0; i < binding->keys->length; i++) { | ||
1183 | free(binding->keys->items[i]); | ||
1184 | } | ||
1185 | list_free(binding->keys); | ||
1186 | } | ||
1187 | if (binding->command) { | ||
1188 | free(binding->command); | ||
1189 | } | ||
1190 | free(binding); | ||
1191 | } | ||
1192 | |||
1193 | int sway_mouse_binding_cmp_buttons(const void *a, const void *b) { | ||
1194 | const struct sway_mouse_binding *binda = a, *bindb = b; | ||
1195 | if (binda->button > bindb->button) { | ||
1196 | return 1; | ||
1197 | } | ||
1198 | if (binda->button < bindb->button) { | ||
1199 | return -1; | ||
1200 | } | ||
1201 | return 0; | ||
1202 | } | ||
1203 | |||
1204 | int sway_mouse_binding_cmp(const void *a, const void *b) { | ||
1205 | int cmp = 0; | ||
1206 | if ((cmp = sway_binding_cmp_keys(a, b)) != 0) { | ||
1207 | return cmp; | ||
1208 | } | ||
1209 | const struct sway_mouse_binding *binda = a, *bindb = b; | ||
1210 | return lenient_strcmp(binda->command, bindb->command); | ||
1211 | } | ||
1212 | |||
1213 | int sway_mouse_binding_cmp_qsort(const void *a, const void *b) { | ||
1214 | return sway_mouse_binding_cmp(*(void **)a, *(void **)b); | ||
1215 | } | ||
1216 | |||
1217 | void free_sway_mouse_binding(struct sway_mouse_binding *binding) { | ||
1218 | if (binding->command) { | ||
1219 | free(binding->command); | ||
1220 | } | ||
1221 | free(binding); | ||
1222 | } | ||
1223 | |||
1224 | struct sway_binding *sway_binding_dup(struct sway_binding *sb) { | ||
1225 | struct sway_binding *new_sb = malloc(sizeof(struct sway_binding)); | ||
1226 | if (!new_sb) { | ||
1227 | return NULL; | ||
1228 | } | ||
1229 | |||
1230 | new_sb->order = sb->order; | ||
1231 | new_sb->modifiers = sb->modifiers; | ||
1232 | new_sb->command = strdup(sb->command); | ||
1233 | |||
1234 | new_sb->keys = create_list(); | ||
1235 | int i; | ||
1236 | for (i = 0; i < sb->keys->length; ++i) { | ||
1237 | xkb_keysym_t *key = malloc(sizeof(xkb_keysym_t)); | ||
1238 | if (!key) { | ||
1239 | free_sway_binding(new_sb); | ||
1240 | return NULL; | ||
1241 | } | ||
1242 | *key = *(xkb_keysym_t *)sb->keys->items[i]; | ||
1243 | list_add(new_sb->keys, key); | ||
1244 | } | ||
1245 | |||
1246 | return new_sb; | ||
1247 | } | ||
1248 | |||
1249 | struct bar_config *default_bar_config(void) { | ||
1250 | struct bar_config *bar = NULL; | ||
1251 | bar = malloc(sizeof(struct bar_config)); | ||
1252 | if (!bar) { | ||
1253 | return NULL; | ||
1254 | } | ||
1255 | if (!(bar->mode = strdup("dock"))) goto cleanup; | ||
1256 | if (!(bar->hidden_state = strdup("hide"))) goto cleanup; | ||
1257 | bar->modifier = WLC_BIT_MOD_LOGO; | ||
1258 | bar->outputs = NULL; | ||
1259 | bar->position = DESKTOP_SHELL_PANEL_POSITION_BOTTOM; | ||
1260 | if (!(bar->bindings = create_list())) goto cleanup; | ||
1261 | if (!(bar->status_command = strdup("while :; do date +'%Y-%m-%d %l:%M:%S %p'; sleep 1; done"))) goto cleanup; | ||
1262 | bar->pango_markup = false; | ||
1263 | bar->swaybar_command = NULL; | ||
1264 | bar->font = NULL; | ||
1265 | bar->height = -1; | ||
1266 | bar->workspace_buttons = true; | ||
1267 | bar->wrap_scroll = false; | ||
1268 | bar->separator_symbol = NULL; | ||
1269 | bar->strip_workspace_numbers = false; | ||
1270 | bar->binding_mode_indicator = true; | ||
1271 | #ifdef ENABLE_TRAY | ||
1272 | bar->tray_output = NULL; | ||
1273 | bar->icon_theme = NULL; | ||
1274 | bar->tray_padding = 2; | ||
1275 | bar->activate_button = 0x110; /* BTN_LEFT */ | ||
1276 | bar->context_button = 0x111; /* BTN_RIGHT */ | ||
1277 | bar->secondary_button = 0x112; /* BTN_MIDDLE */ | ||
1278 | #endif | ||
1279 | bar->verbose = false; | ||
1280 | bar->pid = 0; | ||
1281 | // set default colors | ||
1282 | if (!(bar->colors.background = strndup("#000000ff", 9))) goto cleanup; | ||
1283 | if (!(bar->colors.statusline = strndup("#ffffffff", 9))) goto cleanup; | ||
1284 | if (!(bar->colors.separator = strndup("#666666ff", 9))) goto cleanup; | ||
1285 | if (!(bar->colors.focused_workspace_border = strndup("#4c7899ff", 9))) goto cleanup; | ||
1286 | if (!(bar->colors.focused_workspace_bg = strndup("#285577ff", 9))) goto cleanup; | ||
1287 | if (!(bar->colors.focused_workspace_text = strndup("#ffffffff", 9))) goto cleanup; | ||
1288 | if (!(bar->colors.active_workspace_border = strndup("#333333ff", 9))) goto cleanup; | ||
1289 | if (!(bar->colors.active_workspace_bg = strndup("#5f676aff", 9))) goto cleanup; | ||
1290 | if (!(bar->colors.active_workspace_text = strndup("#ffffffff", 9))) goto cleanup; | ||
1291 | if (!(bar->colors.inactive_workspace_border = strndup("#333333ff", 9))) goto cleanup; | ||
1292 | if (!(bar->colors.inactive_workspace_bg = strndup("#222222ff", 9))) goto cleanup; | ||
1293 | if (!(bar->colors.inactive_workspace_text = strndup("#888888ff", 9))) goto cleanup; | ||
1294 | if (!(bar->colors.urgent_workspace_border = strndup("#2f343aff", 9))) goto cleanup; | ||
1295 | if (!(bar->colors.urgent_workspace_bg = strndup("#900000ff", 9))) goto cleanup; | ||
1296 | if (!(bar->colors.urgent_workspace_text = strndup("#ffffffff", 9))) goto cleanup; | ||
1297 | // if the following colors stay undefined, they fall back to background, | ||
1298 | // statusline, separator and urgent_workspace_*. | ||
1299 | bar->colors.focused_background = NULL; | ||
1300 | bar->colors.focused_statusline = NULL; | ||
1301 | bar->colors.focused_separator = NULL; | ||
1302 | bar->colors.binding_mode_border = NULL; | ||
1303 | bar->colors.binding_mode_bg = NULL; | ||
1304 | bar->colors.binding_mode_text = NULL; | ||
1305 | |||
1306 | list_add(config->bars, bar); | ||
1307 | |||
1308 | return bar; | ||
1309 | |||
1310 | cleanup: | ||
1311 | free_bar(bar); | ||
1312 | return NULL; | ||
1313 | } | ||