]> git.lizzy.rs Git - bspwm.git/blob - parse.c
Don't needlessly ungrab/grab the buttons
[bspwm.git] / parse.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <stdbool.h>
5 #include <errno.h>
6 #include "parse.h"
7
8 bool parse_bool(char *value, bool *b)
9 {
10         if (streq("true", value) || streq("on", value)) {
11                 *b = true;
12                 return true;
13         } else if (streq("false", value) || streq("off", value)) {
14                 *b = false;
15                 return true;
16         }
17         return false;
18 }
19
20 bool parse_split_type(char *s, split_type_t *t)
21 {
22         if (streq("horizontal", s)) {
23                 *t = TYPE_HORIZONTAL;
24                 return true;
25         } else if (streq("vertical", s)) {
26                 *t = TYPE_VERTICAL;
27                 return true;
28         }
29         return false;
30 }
31
32 bool parse_split_mode(char *s, split_mode_t *m)
33 {
34         if (streq("automatic", s)) {
35                 *m = MODE_AUTOMATIC;
36                 return true;
37         } else if (streq("vertical", s)) {
38                 *m = MODE_MANUAL;
39                 return true;
40         }
41         return false;
42 }
43
44 bool parse_layout(char *s, layout_t *l)
45 {
46         if (streq("monocle", s)) {
47                 *l = LAYOUT_MONOCLE;
48                 return true;
49         } else if (streq("tiled", s)) {
50                 *l = LAYOUT_TILED;
51                 return true;
52         }
53         return false;
54 }
55
56 bool parse_client_state(char *s, client_state_t *t)
57 {
58         if (streq("tiled", s)) {
59                 *t = STATE_TILED;
60                 return true;
61         } else if (streq("pseudo_tiled", s)) {
62                 *t = STATE_PSEUDO_TILED;
63                 return true;
64         } else if (streq("floating", s)) {
65                 *t = STATE_FLOATING;
66                 return true;
67         } else if (streq("fullscreen", s)) {
68                 *t = STATE_FULLSCREEN;
69                 return true;
70         }
71         return false;
72 }
73
74 bool parse_stack_layer(char *s, stack_layer_t *l)
75 {
76         if (streq("below", s)) {
77                 *l = LAYER_BELOW;
78                 return true;
79         } else if (streq("normal", s)) {
80                 *l = LAYER_NORMAL;
81                 return true;
82         } else if (streq("above", s)) {
83                 *l = LAYER_ABOVE;
84                 return true;
85         }
86         return false;
87 }
88
89 bool parse_direction(char *s, direction_t *d)
90 {
91         if (streq("north", s)) {
92                 *d = DIR_NORTH;
93                 return true;
94         } else if (streq("west", s)) {
95                 *d = DIR_WEST;
96                 return true;
97         } else if (streq("south", s)) {
98                 *d = DIR_SOUTH;
99                 return true;
100         } else if (streq("east", s)) {
101                 *d = DIR_EAST;
102                 return true;
103         }
104         return false;
105 }
106
107 bool parse_cycle_direction(char *s, cycle_dir_t *d)
108 {
109         if (streq("next", s)) {
110                 *d = CYCLE_NEXT;
111                 return true;
112         } else if (streq("prev", s)) {
113                 *d = CYCLE_PREV;
114                 return true;
115         }
116         return false;
117 }
118
119 bool parse_circulate_direction(char *s, circulate_dir_t *d)
120 {
121         if (streq("forward", s)) {
122                 *d = CIRCULATE_FORWARD;
123                 return true;
124         } else if (streq("backward", s)) {
125                 *d = CIRCULATE_BACKWARD;
126                 return true;
127         }
128         return false;
129 }
130
131 bool parse_history_direction(char *s, history_dir_t *d)
132 {
133         if (streq("older", s)) {
134                 *d = HISTORY_OLDER;
135                 return true;
136         } else if (streq("newer", s)) {
137                 *d = HISTORY_NEWER;
138                 return true;
139         }
140         return false;
141 }
142
143
144 bool parse_flip(char *s, flip_t *f)
145 {
146         if (streq("horizontal", s)) {
147                 *f = FLIP_HORIZONTAL;
148                 return true;
149         } else if (streq("vertical", s)) {
150                 *f = FLIP_VERTICAL;
151                 return true;
152         }
153         return false;
154 }
155
156 bool parse_resize_handle(char *s, resize_handle_t *h)
157 {
158         if (streq("left", s)) {
159                 *h = HANDLE_LEFT;
160                 return true;
161         } else if (streq("top", s)) {
162                 *h = HANDLE_TOP;
163                 return true;
164         } else if (streq("right", s)) {
165                 *h = HANDLE_RIGHT;
166                 return true;
167         } else if (streq("bottom", s)) {
168                 *h = HANDLE_BOTTOM;
169                 return true;
170         } else if (streq("top_left", s)) {
171                 *h = HANDLE_TOP_LEFT;
172                 return true;
173         } else if (streq("top_right", s)) {
174                 *h = HANDLE_TOP_RIGHT;
175                 return true;
176         } else if (streq("bottom_right", s)) {
177                 *h = HANDLE_BOTTOM_RIGHT;
178                 return true;
179         } else if (streq("bottom_left", s)) {
180                 *h = HANDLE_BOTTOM_LEFT;
181                 return true;
182         }
183         return false;
184 }
185
186 bool parse_modifier_mask(char *s, uint16_t *m)
187 {
188         if (strcmp(s, "shift") == 0) {
189                 *m = XCB_MOD_MASK_SHIFT;
190                 return true;
191         } else if (strcmp(s, "control") == 0) {
192                 *m = XCB_MOD_MASK_CONTROL;
193                 return true;
194         } else if (strcmp(s, "lock") == 0) {
195                 *m = XCB_MOD_MASK_LOCK;
196                 return true;
197         } else if (strcmp(s, "mod1") == 0) {
198                 *m = XCB_MOD_MASK_1;
199                 return true;
200         } else if (strcmp(s, "mod2") == 0) {
201                 *m = XCB_MOD_MASK_2;
202                 return true;
203         } else if (strcmp(s, "mod3") == 0) {
204                 *m = XCB_MOD_MASK_3;
205                 return true;
206         } else if (strcmp(s, "mod4") == 0) {
207                 *m = XCB_MOD_MASK_4;
208                 return true;
209         } else if (strcmp(s, "mod5") == 0) {
210                 *m = XCB_MOD_MASK_5;
211                 return true;
212         }
213         return false;
214 }
215
216 bool parse_pointer_action(char *s, pointer_action_t *a)
217 {
218         if (streq("move", s)) {
219                 *a = ACTION_MOVE;
220                 return true;
221         } else if (streq("resize_corner", s)) {
222                 *a = ACTION_RESIZE_CORNER;
223                 return true;
224         } else if (streq("resize_side", s)) {
225                 *a = ACTION_RESIZE_SIDE;
226                 return true;
227         } else if (streq("focus", s)) {
228                 *a = ACTION_FOCUS;
229                 return true;
230         }
231         return false;
232 }
233
234 bool parse_child_polarity(char *s, child_polarity_t *p)
235 {
236         if (streq("first_child", s)) {
237                 *p = FIRST_CHILD;
238                 return true;
239         } else if (streq("second_child", s)) {
240                 *p = SECOND_CHILD;
241                 return true;
242         }
243         return false;
244 }
245
246 bool parse_degree(char *s, int *d)
247 {
248         int i = atoi(s);
249         while (i < 0)
250                 i += 360;
251         while (i > 359)
252                 i -= 360;
253         if ((i % 90) != 0) {
254                 return false;
255         } else {
256                 *d = i;
257                 return true;
258         }
259 }
260
261 bool parse_id(char *s, uint32_t *id)
262 {
263         char *end;
264         errno = 0;
265         uint32_t v = strtol(s, &end, 0);
266         if (errno != 0 || *end != '\0') {
267                 return false;
268         }
269         *id = v;
270         return true;
271 }
272
273 bool parse_bool_declaration(char *s, char **key, bool *value, alter_state_t *state)
274 {
275         *key = strtok(s, EQL_TOK);
276         char *v = strtok(NULL, EQL_TOK);
277         if (v == NULL) {
278                 *state = ALTER_TOGGLE;
279                 return true;
280         } else {
281                 if (parse_bool(v, value)) {
282                         *state = ALTER_SET;
283                         return true;
284                 } else {
285                         return false;
286                 }
287         }
288         return false;
289 }
290
291 bool parse_index(char *s, uint16_t *idx)
292 {
293         return (sscanf(s, "^%hu", idx) == 1);
294 }
295
296 bool parse_rectangle(char *s, xcb_rectangle_t *r)
297 {
298         uint16_t w, h;
299         int16_t x, y;
300         if (sscanf(s, "%hux%hu+%hi+%hi", &w, &h, &x, &y) != 4) {
301                 return false;
302         }
303         r->width = w;
304         r->height = h;
305         r->x = x;
306         r->y = y;
307         return true;
308 }
309
310 bool parse_subscriber_mask(char *s, subscriber_mask_t *mask)
311 {
312         if (streq("all", s)) {
313                 *mask = SBSC_MASK_ALL;
314         } else if (streq("node", s)) {
315                 *mask = SBSC_MASK_NODE;
316         } else if (streq("desktop", s)) {
317                 *mask = SBSC_MASK_DESKTOP;
318         } else if (streq("monitor", s)) {
319                 *mask = SBSC_MASK_MONITOR;
320         } else if (streq("node_manage", s)) {
321                 *mask = SBSC_MASK_NODE_MANAGE;
322         } else if (streq("node_unmanage", s)) {
323                 *mask = SBSC_MASK_NODE_UNMANAGE;
324         } else if (streq("node_swap", s)) {
325                 *mask = SBSC_MASK_NODE_SWAP;
326         } else if (streq("node_transfer", s)) {
327                 *mask = SBSC_MASK_NODE_TRANSFER;
328         } else if (streq("node_focus", s)) {
329                 *mask = SBSC_MASK_NODE_FOCUS;
330         } else if (streq("node_presel", s)) {
331                 *mask = SBSC_MASK_NODE_PRESEL;
332         } else if (streq("node_stack", s)) {
333                 *mask = SBSC_MASK_NODE_STACK;
334         } else if (streq("node_activate", s)) {
335                 *mask = SBSC_MASK_NODE_ACTIVATE;
336         } else if (streq("node_geometry", s)) {
337                 *mask = SBSC_MASK_NODE_GEOMETRY;
338         } else if (streq("node_state", s)) {
339                 *mask = SBSC_MASK_NODE_STATE;
340         } else if (streq("node_flag", s)) {
341                 *mask = SBSC_MASK_NODE_FLAG;
342         } else if (streq("node_layer", s)) {
343                 *mask = SBSC_MASK_NODE_LAYER;
344         } else if (streq("desktop_add", s)) {
345                 *mask = SBSC_MASK_DESKTOP_ADD;
346         } else if (streq("desktop_rename", s)) {
347                 *mask = SBSC_MASK_DESKTOP_RENAME;
348         } else if (streq("desktop_remove", s)) {
349                 *mask = SBSC_MASK_DESKTOP_REMOVE;
350         } else if (streq("desktop_swap", s)) {
351                 *mask = SBSC_MASK_DESKTOP_SWAP;
352         } else if (streq("desktop_transfer", s)) {
353                 *mask = SBSC_MASK_DESKTOP_TRANSFER;
354         } else if (streq("desktop_focus", s)) {
355                 *mask = SBSC_MASK_DESKTOP_FOCUS;
356         } else if (streq("desktop_activate", s)) {
357                 *mask = SBSC_MASK_DESKTOP_ACTIVATE;
358         } else if (streq("desktop_layout", s)) {
359                 *mask = SBSC_MASK_DESKTOP_LAYOUT;
360         } else if (streq("monitor_add", s)) {
361                 *mask = SBSC_MASK_MONITOR_ADD;
362         } else if (streq("monitor_rename", s)) {
363                 *mask = SBSC_MASK_MONITOR_RENAME;
364         } else if (streq("monitor_remove", s)) {
365                 *mask = SBSC_MASK_MONITOR_REMOVE;
366         } else if (streq("monitor_swap", s)) {
367                 *mask = SBSC_MASK_MONITOR_SWAP;
368         } else if (streq("monitor_focus", s)) {
369                 *mask = SBSC_MASK_MONITOR_FOCUS;
370         } else if (streq("monitor_geometry", s)) {
371                 *mask = SBSC_MASK_MONITOR_GEOMETRY;
372         } else if (streq("report", s)) {
373                 *mask = SBSC_MASK_REPORT;
374         } else {
375                 return false;
376         }
377         return true;
378 }
379
380
381 #define GET_MOD(k) \
382         } else if (streq(#k, tok)) { \
383                 sel->k = OPTION_TRUE; \
384         } else if (streq("!" #k, tok)) { \
385                 sel->k = OPTION_FALSE;
386
387 bool parse_monitor_modifiers(char *desc, monitor_select_t *sel)
388 {
389         char *tok;
390         while ((tok = strrchr(desc, CAT_CHR)) != NULL) {
391                 tok[0] = '\0';
392                 tok++;
393                 if (streq("occupied", tok)) {
394                         sel->occupied = OPTION_TRUE;
395                 } else if (streq("!occupied", tok)) {
396                         sel->occupied = OPTION_FALSE;
397                 GET_MOD(focused)
398                 } else {
399                         return false;
400                 }
401         }
402         return true;
403 }
404
405 bool parse_desktop_modifiers(char *desc, desktop_select_t *sel)
406 {
407         char *tok;
408         while ((tok = strrchr(desc, CAT_CHR)) != NULL) {
409                 tok[0] = '\0';
410                 tok++;
411                 if (streq("occupied", tok)) {
412                         sel->occupied = OPTION_TRUE;
413                 } else if (streq("!occupied", tok)) {
414                         sel->occupied = OPTION_FALSE;
415                 GET_MOD(focused)
416                 GET_MOD(urgent)
417                 GET_MOD(local)
418                 } else {
419                         return false;
420                 }
421         }
422         return true;
423
424 }
425
426 bool parse_node_modifiers(char *desc, node_select_t *sel)
427 {
428         char *tok;
429         while ((tok = strrchr(desc, CAT_CHR)) != NULL) {
430                 tok[0] = '\0';
431                 tok++;
432                 if (streq("tiled", tok)) {
433                         sel->tiled = OPTION_TRUE;
434                 } else if (streq("!tiled", tok)) {
435                         sel->tiled = OPTION_FALSE;
436                 GET_MOD(automatic)
437                 GET_MOD(focused)
438                 GET_MOD(local)
439                 GET_MOD(leaf)
440                 GET_MOD(window)
441                 GET_MOD(pseudo_tiled)
442                 GET_MOD(floating)
443                 GET_MOD(fullscreen)
444                 GET_MOD(locked)
445                 GET_MOD(sticky)
446                 GET_MOD(private)
447                 GET_MOD(urgent)
448                 GET_MOD(same_class)
449                 GET_MOD(below)
450                 GET_MOD(normal)
451                 GET_MOD(above)
452                 } else {
453                         return false;
454                 }
455         }
456         return true;
457 }
458
459 #undef GET_MOD