]> git.lizzy.rs Git - bspwm.git/blob - messages.c
Reset stacking client list whenever appropriate
[bspwm.git] / messages.c
1 #include <stdio.h>
2 #include <string.h>
3 #include <stdlib.h>
4 #include "settings.h"
5 #include "messages.h"
6 #include "common.h"
7 #include "types.h"
8 #include "bspwm.h"
9 #include "ewmh.h"
10 #include "helpers.h"
11 #include "window.h"
12 #include "events.h"
13 #include "tree.h"
14 #include "rules.h"
15
16 void process_message(char *msg, char *rsp)
17 {
18     char *cmd = strtok(msg, TOK_SEP);
19
20     if (cmd == NULL)
21         return;
22
23     if (strcmp(cmd, "get") == 0) {
24         char *name = strtok(NULL, TOK_SEP);
25         get_setting(name, rsp);
26     } else if (strcmp(cmd, "set") == 0) {
27         char *name = strtok(NULL, TOK_SEP);
28         char *value = strtok(NULL, TOK_SEP);
29         set_setting(name, value, rsp);
30     } else if (strcmp(cmd, "list") == 0) {
31         char *name = strtok(NULL, TOK_SEP);
32         if (name != NULL) {
33             desktop_location_t loc;
34             if (locate_desktop(name, &loc))
35                 list(loc.desktop, loc.desktop->root, rsp, 0);
36         } else {
37             list(mon->desk, mon->desk->root, rsp, 0);
38         }
39     } else if (strcmp(cmd, "list_monitors") == 0) {
40         char *arg = strtok(NULL, TOK_SEP);
41         list_option_t opt;
42         if (parse_list_option(arg, &opt))
43             list_monitors(opt, rsp);
44     } else if (strcmp(cmd, "list_desktops") == 0) {
45         char *arg = strtok(NULL, TOK_SEP);
46         list_option_t opt;
47         if (parse_list_option(arg, &opt))
48             list_desktops(mon, opt, 0, rsp);
49     } else if (strcmp(cmd, "list_windows") == 0) {
50         list_windows(rsp);
51     } else if (strcmp(cmd, "list_history") == 0) {
52         list_history(rsp);
53     } else if (strcmp(cmd, "list_rules") == 0) {
54         list_rules(rsp);
55     } else if (strcmp(cmd, "close") == 0) {
56         window_close(mon->desk->focus);
57     } else if (strcmp(cmd, "kill") == 0) {
58         window_kill(mon, mon->desk, mon->desk->focus);
59     } else if (strcmp(cmd, "rotate") == 0) {
60         char *deg = strtok(NULL, TOK_SEP);
61         if (deg != NULL) {
62             rotate_t r;
63             if (parse_rotate(deg, &r))
64                 rotate_tree(mon->desk->root, r);
65         }
66         arrange(mon, mon->desk);
67     } else if (strcmp(cmd, "flip") == 0) {
68         char *flp = strtok(NULL, TOK_SEP);
69         if (flp != NULL) {
70             flip_t f;
71             if (parse_flip(flp, &f))
72                 flip_tree(mon->desk->root, f);
73         }
74         arrange(mon, mon->desk);
75     } else if (strcmp(cmd, "balance") == 0) {
76         balance_tree(mon->desk->root);
77         arrange(mon, mon->desk);
78     } else if (strcmp(cmd, "grab_pointer") == 0) {
79         char *pac = strtok(NULL, TOK_SEP);
80         if (pac != NULL) {
81             pointer_action_t a;
82             if (parse_pointer_action(pac, &a))
83                 grab_pointer(a);
84         }
85     } else if (strcmp(cmd, "track_pointer") == 0) {
86         char *arg1 = strtok(NULL, TOK_SEP);
87         char *arg2 = strtok(NULL, TOK_SEP);
88         if (arg1 == NULL || arg2 == NULL)
89             return;
90         int root_x, root_y;
91         if (sscanf(arg1, "%i", &root_x) == 1 && sscanf(arg2, "%i", &root_y) == 1)
92             track_pointer(root_x, root_y);
93     } else if (strcmp(cmd, "ungrab_pointer") == 0) {
94         ungrab_pointer();
95     } else if (strcmp(cmd, "layout") == 0) {
96         char *lyt = strtok(NULL, TOK_SEP);
97         if (lyt != NULL) {
98             layout_t l;
99             if (parse_layout(lyt, &l)) {
100                 char *name = strtok(NULL, TOK_SEP);
101                 if (name == NULL) {
102                     change_layout(mon, mon->desk, l);
103                 } else {
104                     desktop_location_t loc;
105                     do {
106                         if (locate_desktop(name, &loc))
107                             change_layout(loc.monitor, loc.desktop, l);
108                     } while ((name = strtok(NULL, TOK_SEP)) != NULL);
109                 }
110             }
111         }
112     } else if (strcmp(cmd, "cycle_layout") == 0) {
113         if (mon->desk->layout == LAYOUT_MONOCLE)
114             change_layout(mon, mon->desk, LAYOUT_TILED);
115         else
116             change_layout(mon, mon->desk, LAYOUT_MONOCLE);
117     } else if (strcmp(cmd, "shift") == 0) {
118         char *dir = strtok(NULL, TOK_SEP);
119         if (dir != NULL) {
120             direction_t d;
121             if (parse_direction(dir, &d)) {
122                 node_t *n = nearest_neighbor(mon->desk, mon->desk->focus, d);
123                 if (n != NULL) {
124                     swap_nodes(mon->desk->focus, n);
125                     arrange(mon, mon->desk);
126                 } else if (monitor_focus_fallback) {
127                     monitor_t *m = nearest_monitor(d);
128                     if (m != NULL)
129                         transfer_node(mon, mon->desk, m, m->desk, mon->desk->focus);
130                 }
131             }
132         }
133     } else if (strcmp(cmd, "toggle_fullscreen") == 0) {
134         toggle_fullscreen(mon->desk, mon->desk->focus);
135         arrange(mon, mon->desk);
136     } else if (strcmp(cmd, "toggle_floating") == 0) {
137         toggle_floating(mon->desk, mon->desk->focus);
138         arrange(mon, mon->desk);
139     } else if (strcmp(cmd, "toggle_locked") == 0) {
140         toggle_locked(mon, mon->desk, mon->desk->focus);
141     } else if (strcmp(cmd, "toggle_visibility") == 0) {
142         toggle_visibility();
143     } else if (strcmp(cmd, "pad") == 0) {
144         char *name = strtok(NULL, TOK_SEP);
145         if (name != NULL) {
146             monitor_t *m = find_monitor(name);
147             if (m != NULL) {
148                 char args[BUFSIZ] = {0}, *s;
149                 while ((s = strtok(NULL, TOK_SEP)) != NULL) {
150                     strncat(args, s, REMLEN(args));
151                     strncat(args, TOK_SEP, REMLEN(args));
152                 }
153                 if (strlen(args) > 0) {
154                     sscanf(args, "%i %i %i %i", &m->top_padding, &m->right_padding, &m->bottom_padding, &m->left_padding);
155                     arrange(m, m->desk);
156                 } else {
157                     snprintf(rsp, BUFSIZ, "%i %i %i %i\n", m->top_padding, m->right_padding, m->bottom_padding, m->left_padding);
158                 }
159             }
160         }
161     } else if (strcmp(cmd, "ratio") == 0) {
162         char *value;
163         if (mon->desk->focus != NULL && (value = strtok(NULL, TOK_SEP)) != NULL &&
164                 sscanf(value, "%lf", &mon->desk->focus->split_ratio) == 1)
165             window_draw_border(mon->desk->focus, true, true);
166     } else if (strcmp(cmd, "cancel") == 0) {
167         if (mon->desk->focus == NULL)
168             return;
169         char *opt = strtok(NULL, TOK_SEP);
170         cancel_option_t o;
171         if (parse_cancel_option(opt, &o))
172             reset_mode(mon->desk, mon->desk->focus, o);
173     } else if (strcmp(cmd, "presel") == 0) {
174         if (mon->desk->focus == NULL || !is_tiled(mon->desk->focus->client) || mon->desk->layout != LAYOUT_TILED)
175             return;
176         char *dir = strtok(NULL, TOK_SEP);
177         if (dir != NULL) {
178             direction_t d;
179             if (parse_direction(dir, &d)) {
180                 mon->desk->focus->split_mode = MODE_MANUAL;
181                 mon->desk->focus->split_dir = d;
182                 char *rat = strtok(NULL, TOK_SEP);
183                 if (rat != NULL)
184                     sscanf(rat, "%lf", &mon->desk->focus->split_ratio);
185                 window_draw_border(mon->desk->focus, true, true);
186             }
187         }
188     } else if (strcmp(cmd, "push") == 0 || strcmp(cmd, "pull") == 0) {
189         char *dir = strtok(NULL, TOK_SEP);
190         if (dir != NULL) {
191             fence_move_t m;
192             direction_t d;
193             if (parse_fence_move(cmd, &m) && parse_direction(dir, &d)) {
194                 move_fence(mon->desk->focus, d, m);
195                 arrange(mon, mon->desk);
196             }
197         }
198     } else if (strcmp(cmd, "fence_ratio") == 0) {
199         char *dir = strtok(NULL, TOK_SEP);
200         if (dir != NULL) {
201             direction_t d;
202             node_t *n;
203             if (parse_direction(dir, &d) && (n = find_fence(mon->desk->focus, d)) != NULL) {
204                 char *value = strtok(NULL, TOK_SEP);
205                 if (value != NULL && sscanf(value, "%lf", &n->split_ratio) == 1)
206                     arrange(mon, mon->desk);
207             }
208         }
209     } else if (strcmp(cmd, "drop_to_monitor") == 0) {
210         char *dir = strtok(NULL, TOK_SEP);
211         if (dir != NULL) {
212             cycle_dir_t d;
213             if (parse_cycle_direction(dir, &d)) {
214                 monitor_t *m;
215                 if (d == CYCLE_NEXT)
216                     m = ((mon->next == NULL ? mon_head : mon->next));
217                 else
218                     m = ((mon->prev == NULL ? mon_tail : mon->prev));
219                 transfer_node(mon, mon->desk, m, m->desk, mon->desk->focus);
220                 char *opt = strtok(NULL, TOK_SEP);
221                 send_option_t o;
222                 if (parse_send_option(opt, &o) && o == SEND_OPTION_FOLLOW)
223                     focus_node(m, m->desk, m->desk->focus);
224             }
225         }
226     } else if (strcmp(cmd, "send_to_monitor") == 0) {
227         char *name = strtok(NULL, TOK_SEP);
228         if (name != NULL) {
229             monitor_t *m = find_monitor(name);
230             if (m != NULL && m != mon) {
231                 transfer_node(mon, mon->desk, m, m->desk, mon->desk->focus);
232                 char *opt = strtok(NULL, TOK_SEP);
233                 send_option_t o;
234                 if (parse_send_option(opt, &o) && o == SEND_OPTION_FOLLOW)
235                     focus_node(m, m->desk, m->desk->focus);
236             }
237         }
238     } else if (strcmp(cmd, "drop_to") == 0) {
239         char *dir = strtok(NULL, TOK_SEP);
240         if (dir != NULL) {
241             cycle_dir_t c;
242             if (parse_cycle_direction(dir, &c)) {
243                 desktop_t *d;
244                 if (c == CYCLE_NEXT)
245                     d = ((mon->desk->next == NULL ? mon->desk_head : mon->desk->next));
246                 else
247                     d = ((mon->desk->prev == NULL ? mon->desk_tail : mon->desk->prev));
248                 transfer_node(mon, mon->desk, mon, d, mon->desk->focus);
249                 char *opt = strtok(NULL, TOK_SEP);
250                 send_option_t o;
251                 if (parse_send_option(opt, &o) && o == SEND_OPTION_FOLLOW)
252                     focus_node(mon, d, d->focus);
253             }
254         }
255     } else if (strcmp(cmd, "send_to") == 0) {
256         char *name = strtok(NULL, TOK_SEP);
257         if (name != NULL) {
258             desktop_location_t loc;
259             if (locate_desktop(name, &loc)) {
260                 transfer_node(mon, mon->desk, loc.monitor, loc.desktop, mon->desk->focus);
261                 char *opt = strtok(NULL, TOK_SEP);
262                 send_option_t o;
263                 if (parse_send_option(opt, &o) && o == SEND_OPTION_FOLLOW)
264                     focus_node(loc.monitor, loc.desktop, loc.desktop->focus);
265             }
266         }
267     } else if (strcmp(cmd, "rename_monitor") == 0) {
268         char *cur_name = strtok(NULL, TOK_SEP);
269         if (cur_name != NULL) {
270             monitor_t *m = find_monitor(cur_name);
271             if (m != NULL) {
272                 char *new_name = strtok(NULL, TOK_SEP);
273                 if (new_name != NULL) {
274                     strncpy(m->name, new_name, sizeof(m->name));
275                     put_status();
276                 }
277             }
278         }
279     } else if (strcmp(cmd, "rename") == 0) {
280         char *cur_name = strtok(NULL, TOK_SEP);
281         if (cur_name != NULL) {
282             desktop_location_t loc;
283             if (locate_desktop(cur_name, &loc)) {
284                 char *new_name = strtok(NULL, TOK_SEP);
285                 if (new_name != NULL) {
286                     strncpy(loc.desktop->name, new_name, sizeof(loc.desktop->name));
287                     ewmh_update_desktop_names();
288                     put_status();
289                 }
290             }
291         }
292     } else if (strcmp(cmd, "use_monitor") == 0) {
293         char *name = strtok(NULL, TOK_SEP);
294         if (name != NULL) {
295             monitor_t *m = find_monitor(name);
296             if (m != NULL) {
297                 if (auto_alternate && m == mon && last_mon != NULL)
298                     m = last_mon;
299                 focus_node(m, m->desk, m->desk->focus);
300             }
301         }
302     } else if (strcmp(cmd, "focus_monitor") == 0) {
303         char *dir = strtok(NULL, TOK_SEP);
304         if (dir != NULL) {
305             direction_t d;
306             if (parse_direction(dir, &d)) {
307                 monitor_t *m = nearest_monitor(d);
308                 if (m != NULL)
309                     focus_node(m, m->desk, m->desk->focus);
310             }
311         }
312     } else if (strcmp(cmd, "use") == 0) {
313         char *name = strtok(NULL, TOK_SEP);
314         if (name != NULL) {
315             desktop_location_t loc;
316             if (locate_desktop(name, &loc)) {
317                 if (auto_alternate && loc.desktop == mon->desk && mon->last_desk != NULL)
318                     focus_node(mon, mon->last_desk, mon->last_desk->focus);
319                 else
320                     focus_node(loc.monitor, loc.desktop, loc.desktop->focus);
321             }
322         }
323     } else if (strcmp(cmd, "cycle_monitor") == 0) {
324         char *dir = strtok(NULL, TOK_SEP);
325         if (dir != NULL) {
326             cycle_dir_t d;
327             if (parse_cycle_direction(dir, &d))
328                 cycle_monitor(d);
329         }
330     } else if (strcmp(cmd, "cycle_desktop") == 0) {
331         char *dir = strtok(NULL, TOK_SEP);
332         if (dir != NULL) {
333             cycle_dir_t d;
334             if (parse_cycle_direction(dir, &d)) {
335                 skip_desktop_t k;
336                 char *skip = strtok(NULL, TOK_SEP);
337                 if (parse_skip_desktop(skip, &k))
338                     cycle_desktop(mon, mon->desk, d, k);
339             }
340         }
341     } else if (strcmp(cmd, "cycle") == 0) {
342         if (mon->desk->focus != NULL && mon->desk->focus->client->fullscreen)
343             return;
344         char *dir = strtok(NULL, TOK_SEP);
345         if (dir != NULL) {
346             cycle_dir_t d;
347             if (parse_cycle_direction(dir, &d)) {
348                 skip_client_t k;
349                 char *skip = strtok(NULL, TOK_SEP);
350                 if (parse_skip_client(skip, &k))
351                     cycle_leaf(mon, mon->desk, mon->desk->focus, d, k);
352             }
353         }
354     } else if (strcmp(cmd, "nearest") == 0) {
355         if (mon->desk->focus != NULL && mon->desk->focus->client->fullscreen)
356             return;
357         char *arg = strtok(NULL, TOK_SEP);
358         if (arg != NULL) {
359             nearest_arg_t a;
360             if (parse_nearest_argument(arg, &a)) {
361                 skip_client_t k;
362                 char *skip = strtok(NULL, TOK_SEP);
363                 if (parse_skip_client(skip, &k))
364                     nearest_leaf(mon, mon->desk, mon->desk->focus, a, k);
365             }
366         }
367     } else if (strcmp(cmd, "biggest") == 0) {
368         node_t *n = find_biggest(mon->desk);
369         if (n != NULL)
370             snprintf(rsp, BUFSIZ, "0x%X", n->client->window);
371     } else if (strcmp(cmd, "circulate") == 0) {
372         if (mon->desk->layout == LAYOUT_MONOCLE
373                 || (mon->desk->focus != NULL && !is_tiled(mon->desk->focus->client)))
374             return;
375         char *dir = strtok(NULL, TOK_SEP);
376         if (dir != NULL) {
377             circulate_dir_t d;
378             if (parse_circulate_direction(dir, &d))
379                 circulate_leaves(mon, mon->desk, d);
380         }
381         arrange(mon, mon->desk);
382     } else if (strcmp(cmd, "rule") == 0) {
383         char *name = strtok(NULL, TOK_SEP);
384         if (name != NULL) {
385             rule_t *rule = make_rule();
386             strncpy(rule->cause.name, name, sizeof(rule->cause.name));
387             char *arg = strtok(NULL, TOK_SEP);
388             while (arg != NULL) {
389                 if (strcmp(arg, "floating") == 0) {
390                     rule->effect.floating = true;
391                 } else if (strcmp(arg, "follow") == 0) {
392                     rule->effect.follow = true;
393                 } else {
394                     desktop_location_t loc;
395                     if (locate_desktop(arg, &loc)) {
396                         rule->effect.monitor = loc.monitor;
397                         rule->effect.desktop = loc.desktop;
398                     }
399                 }
400                 arg = strtok(NULL, TOK_SEP);
401             }
402             add_rule(rule);
403         }
404     } else if (strcmp(cmd, "remove_rule") == 0) {
405         char *arg;
406         unsigned int uid;
407         while ((arg = strtok(NULL, TOK_SEP)) != NULL)
408             if (sscanf(arg, "%X", &uid) > 0)
409                 remove_rule_by_uid(uid);
410     } else if (strcmp(cmd, "swap") == 0) {
411         char *opt = strtok(NULL, TOK_SEP);
412         swap_option_t o;
413         if (!parse_swap_option(opt, &o))
414             return;
415         node_t *last_focus = history_get(mon->desk->history, 1);
416         swap_nodes(mon->desk->focus, last_focus);
417         arrange(mon, mon->desk);
418         if (o == SWAP_OPTION_SWAP_FOCUS)
419             focus_node(mon, mon->desk, last_focus);
420     } else if (strcmp(cmd, "alternate") == 0) {
421         focus_node(mon, mon->desk, history_get(mon->desk->history, 1));
422     } else if (strcmp(cmd, "alternate_desktop") == 0) {
423         if (mon->last_desk != NULL)
424             focus_node(mon, mon->last_desk, mon->last_desk->focus);
425     } else if (strcmp(cmd, "alternate_monitor") == 0) {
426         if (last_mon != NULL)
427             focus_node(last_mon, last_mon->desk, last_mon->desk->focus);
428     } else if (strcmp(cmd, "add_in") == 0) {
429         char *name = strtok(NULL, TOK_SEP);
430         if (name != NULL) {
431             monitor_t *m = find_monitor(name);
432             if (m != NULL)
433                 for (name = strtok(NULL, TOK_SEP); name != NULL; name = strtok(NULL, TOK_SEP))
434                     add_desktop(m, make_desktop(name));
435         }
436     } else if (strcmp(cmd, "add") == 0) {
437         for (char *name = strtok(NULL, TOK_SEP); name != NULL; name = strtok(NULL, TOK_SEP))
438             add_desktop(mon, make_desktop(name));
439     } else if (strcmp(cmd, "remove_desktop") == 0) {
440         for (char *name = strtok(NULL, TOK_SEP); name != NULL; name = strtok(NULL, TOK_SEP)) {
441             desktop_location_t loc;
442             if (locate_desktop(name, &loc)) {
443                 if (loc.desktop->root == NULL && loc.monitor->desk_head != loc.monitor->desk_tail) {
444                     remove_desktop(loc.monitor, loc.desktop);
445                     desktop_show(loc.monitor->desk);
446                 }
447             }
448         }
449         update_current();
450     } else if (strcmp(cmd, "send_desktop_to") == 0) {
451         if (mon->desk_head == mon->desk_tail)
452             return;
453         char *name = strtok(NULL, TOK_SEP);
454         if (name != NULL) {
455             monitor_t *m = find_monitor(name);
456             if (m != NULL && m != mon) {
457                 char *opt = strtok(NULL, TOK_SEP);
458                 send_option_t o;
459                 if (!parse_send_option(opt, &o))
460                     return;
461                 desktop_t *d = mon->desk;
462                 transfer_desktop(mon, m, d);
463                 if (o == SEND_OPTION_FOLLOW)
464                     focus_node(m, d, d->focus);
465                 else if (o == SEND_OPTION_DONT_FOLLOW)
466                     update_current();
467             }
468         }
469     } else if (strcmp(cmd, "focus") == 0) {
470         char *dir = strtok(NULL, TOK_SEP);
471         if (dir != NULL) {
472             direction_t d;
473             if (parse_direction(dir, &d)) {
474                 node_t *n = nearest_neighbor(mon->desk, mon->desk->focus, d);
475                 if (n != NULL) {
476                     focus_node(mon, mon->desk, n);
477                 } else if (monitor_focus_fallback) {
478                     monitor_t *m = nearest_monitor(d);
479                     if (m != NULL)
480                         focus_node(m, m->desk, m->desk->focus);
481                 }
482             }
483         }
484     } else if (strcmp(cmd, "put_status") == 0) {
485         put_status();
486     } else if (strcmp(cmd, "adopt_orphans") == 0) {
487         adopt_orphans();
488     } else if (strcmp(cmd, "restore_layout") == 0) {
489         char *arg = strtok(NULL, TOK_SEP);
490         restore_layout(arg);
491     } else if (strcmp(cmd, "restore_history") == 0) {
492         char *arg = strtok(NULL, TOK_SEP);
493         restore_history(arg);
494     } else if (strcmp(cmd, "quit") == 0) {
495         char *arg = strtok(NULL, TOK_SEP);
496         if (arg != NULL)
497             sscanf(arg, "%i", &exit_status);
498         quit();
499     } else {
500         snprintf(rsp, BUFSIZ, "unknown command: %s", cmd);
501     }
502 }
503
504 void set_setting(char *name, char *value, char *rsp)
505 {
506     if (name == NULL || value == NULL)
507         return;
508
509     if (strcmp(name, "border_width") == 0) {
510         sscanf(value, "%u", &border_width);
511     } else if (strcmp(name, "window_gap") == 0) {
512         sscanf(value, "%i", &window_gap);
513     } else if (strcmp(name, "split_ratio") == 0) {
514         sscanf(value, "%lf", &split_ratio);
515     } else if (strcmp(name, "left_padding") == 0) {
516         sscanf(value, "%i", &mon->left_padding);
517     } else if (strcmp(name, "right_padding") == 0) {
518         sscanf(value, "%i", &mon->right_padding);
519     } else if (strcmp(name, "top_padding") == 0) {
520         sscanf(value, "%i", &mon->top_padding);
521     } else if (strcmp(name, "bottom_padding") == 0) {
522         sscanf(value, "%i", &mon->bottom_padding);
523     } else if (strcmp(name, "focused_border_color") == 0) {
524         strncpy(focused_border_color, value, sizeof(focused_border_color));
525         focused_border_color_pxl = get_color(focused_border_color);
526     } else if (strcmp(name, "active_border_color") == 0) {
527         strncpy(active_border_color, value, sizeof(active_border_color));
528         active_border_color_pxl = get_color(active_border_color);
529     } else if (strcmp(name, "normal_border_color") == 0) {
530         strncpy(normal_border_color, value, sizeof(normal_border_color));
531         normal_border_color_pxl = get_color(normal_border_color);
532     } else if (strcmp(name, "presel_border_color") == 0) {
533         strncpy(presel_border_color, value, sizeof(presel_border_color));
534         presel_border_color_pxl = get_color(presel_border_color);
535     } else if (strcmp(name, "focused_locked_border_color") == 0) {
536         strncpy(focused_locked_border_color, value, sizeof(focused_locked_border_color));
537         focused_locked_border_color_pxl = get_color(focused_locked_border_color);
538     } else if (strcmp(name, "active_locked_border_color") == 0) {
539         strncpy(active_locked_border_color, value, sizeof(active_locked_border_color));
540         active_locked_border_color_pxl = get_color(active_locked_border_color);
541     } else if (strcmp(name, "normal_locked_border_color") == 0) {
542         strncpy(normal_locked_border_color, value, sizeof(normal_locked_border_color));
543         normal_locked_border_color_pxl = get_color(normal_locked_border_color);
544     } else if (strcmp(name, "urgent_border_color") == 0) {
545         strncpy(urgent_border_color, value, sizeof(urgent_border_color));
546         urgent_border_color_pxl = get_color(urgent_border_color);
547     } else if (strcmp(name, "borderless_monocle") == 0) {
548         bool b;
549         if (parse_bool(value, &b))
550             borderless_monocle = b;
551     } else if (strcmp(name, "gapless_monocle") == 0) {
552         bool b;
553         if (parse_bool(value, &b))
554             gapless_monocle = b;
555     } else if (strcmp(name, "focus_follows_pointer") == 0) {
556         bool b;
557         if (parse_bool(value, &b) && b != focus_follows_pointer) {
558             uint32_t values[] = {(focus_follows_pointer ? CLIENT_EVENT_MASK : CLIENT_EVENT_MASK_FFP)};
559             for (monitor_t *m = mon_head; m != NULL; m = m->next)
560                 for (desktop_t *d = m->desk_head; d != NULL; d = d->next)
561                     for (node_t *n = first_extrema(d->root); n != NULL; n = next_leaf(n, d->root))
562                         xcb_change_window_attributes(dpy, n->client->window, XCB_CW_EVENT_MASK, values);
563             if (focus_follows_pointer)
564                 disable_motion_recorder();
565             else
566                 enable_motion_recorder();
567             focus_follows_pointer = b;
568         }
569         return;
570     } else if (strcmp(name, "pointer_follows_monitor") == 0) {
571         bool b;
572         if (parse_bool(value, &b))
573             pointer_follows_monitor = b;
574         return;
575     } else if (strcmp(name, "monitor_focus_fallback") == 0) {
576         bool b;
577         if (parse_bool(value, &b))
578             monitor_focus_fallback = b;
579         return;
580     } else if (strcmp(name, "adaptative_raise") == 0) {
581         bool b;
582         if (parse_bool(value, &b))
583             adaptative_raise = b;
584         return;
585     } else if (strcmp(name, "apply_shadow_property") == 0) {
586         bool b;
587         if (parse_bool(value, &b))
588             apply_shadow_property = b;
589         return;
590     } else if (strcmp(name, "auto_alternate") == 0) {
591         bool b;
592         if (parse_bool(value, &b))
593             auto_alternate = b;
594         return;
595     } else if (strcmp(name, "focus_by_distance") == 0) {
596         bool b;
597         if (parse_bool(value, &b))
598             focus_by_distance = b;
599         return;
600     } else if (strcmp(name, "history_aware_focus") == 0) {
601         bool b;
602         if (parse_bool(value, &b))
603             history_aware_focus = b;
604         return;
605     } else if (strcmp(name, "wm_name") == 0) {
606         strncpy(wm_name, value, sizeof(wm_name));
607         ewmh_update_wm_name();
608         return;
609     } else {
610         snprintf(rsp, BUFSIZ, "unknown setting: %s", name);
611         return;
612     }
613
614     for (monitor_t *m = mon_head; m != NULL; m = m->next)
615         for (desktop_t *d = m->desk_head; d != NULL; d = d->next)
616             arrange(m, d);
617 }
618
619 void get_setting(char *name, char* rsp)
620 {
621     if (name == NULL)
622         return;
623
624     if (strcmp(name, "border_width") == 0)
625         snprintf(rsp, BUFSIZ, "%u", border_width);
626     else if (strcmp(name, "window_gap") == 0)
627         snprintf(rsp, BUFSIZ, "%i", window_gap);
628     else if (strcmp(name, "split_ratio") == 0)
629         snprintf(rsp, BUFSIZ, "%lf", split_ratio);
630     else if (strcmp(name, "left_padding") == 0)
631         snprintf(rsp, BUFSIZ, "%i", mon->left_padding);
632     else if (strcmp(name, "right_padding") == 0)
633         snprintf(rsp, BUFSIZ, "%i", mon->right_padding);
634     else if (strcmp(name, "top_padding") == 0)
635         snprintf(rsp, BUFSIZ, "%i", mon->top_padding);
636     else if (strcmp(name, "bottom_padding") == 0)
637         snprintf(rsp, BUFSIZ, "%i", mon->bottom_padding);
638     else if (strcmp(name, "focused_border_color") == 0)
639         snprintf(rsp, BUFSIZ, "%s (%06X)", focused_border_color, focused_border_color_pxl);
640     else if (strcmp(name, "active_border_color") == 0)
641         snprintf(rsp, BUFSIZ, "%s (%06X)", active_border_color, active_border_color_pxl);
642     else if (strcmp(name, "normal_border_color") == 0)
643         snprintf(rsp, BUFSIZ, "%s (%06X)", normal_border_color, normal_border_color_pxl);
644     else if (strcmp(name, "presel_border_color") == 0)
645         snprintf(rsp, BUFSIZ, "%s (%06X)", presel_border_color, presel_border_color_pxl);
646     else if (strcmp(name, "focused_locked_border_color") == 0)
647         snprintf(rsp, BUFSIZ, "%s (%06X)", focused_locked_border_color, focused_locked_border_color_pxl);
648     else if (strcmp(name, "active_locked_border_color") == 0)
649         snprintf(rsp, BUFSIZ, "%s (%06X)", active_locked_border_color, active_locked_border_color_pxl);
650     else if (strcmp(name, "normal_locked_border_color") == 0)
651         snprintf(rsp, BUFSIZ, "%s (%06X)", normal_locked_border_color, normal_locked_border_color_pxl);
652     else if (strcmp(name, "urgent_border_color") == 0)
653         snprintf(rsp, BUFSIZ, "%s (%06X)", urgent_border_color, urgent_border_color_pxl);
654     else if (strcmp(name, "borderless_monocle") == 0)
655         snprintf(rsp, BUFSIZ, "%s", BOOLSTR(borderless_monocle));
656     else if (strcmp(name, "gapless_monocle") == 0)
657         snprintf(rsp, BUFSIZ, "%s", BOOLSTR(gapless_monocle));
658     else if (strcmp(name, "focus_follows_pointer") == 0)
659         snprintf(rsp, BUFSIZ, "%s", BOOLSTR(focus_follows_pointer));
660     else if (strcmp(name, "pointer_follows_monitor") == 0)
661         snprintf(rsp, BUFSIZ, "%s", BOOLSTR(pointer_follows_monitor));
662     else if (strcmp(name, "monitor_focus_fallback") == 0)
663         snprintf(rsp, BUFSIZ, "%s", BOOLSTR(monitor_focus_fallback));
664     else if (strcmp(name, "adaptative_raise") == 0)
665         snprintf(rsp, BUFSIZ, "%s", BOOLSTR(adaptative_raise));
666     else if (strcmp(name, "apply_shadow_property") == 0)
667         snprintf(rsp, BUFSIZ, "%s", BOOLSTR(apply_shadow_property));
668     else if (strcmp(name, "auto_alternate") == 0)
669         snprintf(rsp, BUFSIZ, "%s", BOOLSTR(auto_alternate));
670     else if (strcmp(name, "focus_by_distance") == 0)
671         snprintf(rsp, BUFSIZ, "%s", BOOLSTR(focus_by_distance));
672     else if (strcmp(name, "history_aware_focus") == 0)
673         snprintf(rsp, BUFSIZ, "%s", BOOLSTR(history_aware_focus));
674     else if (strcmp(name, "wm_name") == 0)
675         snprintf(rsp, BUFSIZ, "%s", wm_name);
676     else
677         snprintf(rsp, BUFSIZ, "unknown setting: %s", name);
678 }
679
680 bool parse_bool(char *value, bool *b)
681 {
682     if (strcmp(value, "true") == 0) {
683         *b = true;
684         return true;
685     } else if (strcmp(value, "false") == 0) {
686         *b = false;
687         return true;
688     }
689     return false;
690 }
691
692 bool parse_layout(char *s, layout_t *l)
693 {
694     if (strcmp(s, "monocle") == 0) {
695         *l = LAYOUT_MONOCLE;
696         return true;
697     } else if (strcmp(s, "tiled") == 0) {
698         *l = LAYOUT_TILED;
699         return true;
700     }
701     return false;
702 }
703
704 bool parse_direction(char *s, direction_t *d)
705 {
706     if (strcmp(s, "up") == 0) {
707         *d = DIR_UP;
708         return true;
709     } else if (strcmp(s, "down") == 0) {
710         *d = DIR_DOWN;
711         return true;
712     } else if (strcmp(s, "left") == 0) {
713         *d = DIR_LEFT;
714         return true;
715     } else if (strcmp(s, "right") == 0) {
716         *d = DIR_RIGHT;
717         return true;
718     }
719     return false;
720 }
721
722 bool parse_nearest_argument(char *s, nearest_arg_t *a)
723 {
724     if (strcmp(s, "older") == 0) {
725         *a = NEAREST_OLDER;
726         return true;
727     } else if (strcmp(s, "newer") == 0) {
728         *a = NEAREST_NEWER;
729         return true;
730     }
731     return false;
732 }
733
734 bool parse_cycle_direction(char *s, cycle_dir_t *d)
735 {
736     if (strcmp(s, "prev") == 0) {
737         *d = CYCLE_PREV;
738         return true;
739     } else if (strcmp(s, "next") == 0) {
740         *d = CYCLE_NEXT;
741         return true;
742     }
743     return false;
744 }
745
746 bool parse_circulate_direction(char *s, circulate_dir_t *d)
747 {
748     if (strcmp(s, "forward") == 0) {
749         *d = CIRCULATE_FORWARD;
750         return true;
751     } else if (strcmp(s, "backward") == 0) {
752         *d = CIRCULATE_BACKWARD;
753         return true;
754     }
755     return false;
756 }
757
758 bool parse_skip_client(char *s, skip_client_t *k)
759 {
760     if (s == NULL) {
761         *k = CLIENT_SKIP_NONE;
762         return true;
763     } else if (strcmp(s, "--skip-floating") == 0) {
764         *k = CLIENT_SKIP_FLOATING;
765         return true;
766     } else if (strcmp(s, "--skip-tiled") == 0) {
767         *k = CLIENT_SKIP_TILED;
768         return true;
769     } else if (strcmp(s, "--skip-class-equal") == 0) {
770         *k = CLIENT_SKIP_CLASS_EQUAL;
771         return true;
772     } else if (strcmp(s, "--skip-class-differ") == 0) {
773         *k = CLIENT_SKIP_CLASS_DIFFER;
774         return true;
775     }
776     return false;
777 }
778
779 bool parse_skip_desktop(char *s, skip_desktop_t *k)
780 {
781     if (s == NULL) {
782         *k = DESKTOP_SKIP_NONE;
783         return true;
784     } else if (strcmp(s, "--skip-free") == 0) {
785         *k = DESKTOP_SKIP_FREE;
786         return true;
787     } else if (strcmp(s, "--skip-occupied") == 0) {
788         *k = DESKTOP_SKIP_OCCUPIED;
789         return true;
790     }
791     return false;
792 }
793
794 bool parse_list_option(char *s, list_option_t *o)
795 {
796     if (s == NULL) {
797         *o = LIST_OPTION_VERBOSE;
798         return true;
799     } else if (strcmp(s, "--quiet") == 0) {
800         *o = LIST_OPTION_QUIET;
801         return true;
802     }
803     return false;
804 }
805
806 bool parse_send_option(char *s, send_option_t *o)
807 {
808     if (s == NULL) {
809         *o = SEND_OPTION_DONT_FOLLOW;
810         return true;
811     } else if (strcmp(s, "--follow") == 0) {
812         *o = SEND_OPTION_FOLLOW;
813         return true;
814     }
815     return false;
816 }
817
818 bool parse_swap_option(char *s, swap_option_t *o)
819 {
820     if (s == NULL) {
821         *o = SWAP_OPTION_SWAP_FOCUS;
822         return true;
823     } else if (strcmp(s, "--keep-focus") == 0) {
824         *o = SWAP_OPTION_KEEP_FOCUS;
825         return true;
826     }
827     return false;
828 }
829
830 bool parse_cancel_option(char *s, cancel_option_t *o)
831 {
832     if (s == NULL) {
833         *o = CANCEL_OPTION_FOCUSED;
834         return true;
835     } else if (strcmp(s, "--all") == 0) {
836         *o = CANCEL_OPTION_ALL;
837         return true;
838     }
839     return false;
840 }
841
842 bool parse_rotate(char *s, rotate_t *r)
843 {
844     if (strcmp(s, "clockwise") == 0) {
845         *r = ROTATE_CLOCKWISE;
846         return true;
847     } else if (strcmp(s, "counter_clockwise") == 0) {
848         *r = ROTATE_COUNTER_CLOCKWISE;
849         return true;
850     } else if (strcmp(s, "full_cycle") == 0) {
851         *r = ROTATE_FULL_CYCLE;
852         return true;
853     }
854     return false;
855 }
856
857 bool parse_flip(char *s, flip_t *f)
858 {
859     if (strcmp(s, "horizontal") == 0) {
860         *f = FLIP_HORIZONTAL;
861         return true;
862     } else if (strcmp(s, "vertical") == 0) {
863         *f = FLIP_VERTICAL;
864         return true;
865     }
866     return false;
867 }
868
869 bool parse_fence_move(char *s, fence_move_t *m)
870 {
871     if (strcmp(s, "push") == 0) {
872         *m = MOVE_PUSH;
873         return true;
874     } else if (strcmp(s, "pull") == 0) {
875         *m = MOVE_PULL;
876         return true;
877     }
878     return false;
879 }
880
881 bool parse_pointer_action(char *s, pointer_action_t *a)
882 {
883     if (strcmp(s, "move") == 0) {
884         *a = ACTION_MOVE;
885         return true;
886     } else if (strcmp(s, "resize_corner") == 0) {
887         *a = ACTION_RESIZE_CORNER;
888         return true;
889     } else if (strcmp(s, "resize_side") == 0) {
890         *a = ACTION_RESIZE_SIDE;
891         return true;
892     } else if (strcmp(s, "focus") == 0) {
893         *a = ACTION_FOCUS;
894         return true;
895     }
896     return false;
897 }