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