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