]> git.sur5r.net Git - i3/i3/blob - src/commands.c
Merge pull request #3102 from jolange/fix3071
[i3/i3] / src / commands.c
1 /*
2  * vim:ts=4:sw=4:expandtab
3  *
4  * i3 - an improved dynamic tiling window manager
5  * © 2009 Michael Stapelberg and contributors (see also: LICENSE)
6  *
7  * commands.c: all command functions (see commands_parser.c)
8  *
9  */
10 #include "all.h"
11
12 #include <stdint.h>
13 #include <float.h>
14 #include <stdarg.h>
15
16 #ifdef I3_ASAN_ENABLED
17 #include <sanitizer/lsan_interface.h>
18 #endif
19
20 #include "shmlog.h"
21
22 // Macros to make the YAJL API a bit easier to use.
23 #define y(x, ...) (cmd_output->json_gen != NULL ? yajl_gen_##x(cmd_output->json_gen, ##__VA_ARGS__) : 0)
24 #define ystr(str) (cmd_output->json_gen != NULL ? yajl_gen_string(cmd_output->json_gen, (unsigned char *)str, strlen(str)) : 0)
25 #define ysuccess(success)                   \
26     do {                                    \
27         if (cmd_output->json_gen != NULL) { \
28             y(map_open);                    \
29             ystr("success");                \
30             y(bool, success);               \
31             y(map_close);                   \
32         }                                   \
33     } while (0)
34 #define yerror(format, ...)                             \
35     do {                                                \
36         if (cmd_output->json_gen != NULL) {             \
37             char *message;                              \
38             sasprintf(&message, format, ##__VA_ARGS__); \
39             y(map_open);                                \
40             ystr("success");                            \
41             y(bool, false);                             \
42             ystr("error");                              \
43             ystr(message);                              \
44             y(map_close);                               \
45             free(message);                              \
46         }                                               \
47     } while (0)
48
49 /** If an error occurred during parsing of the criteria, we want to exit instead
50  * of relying on fallback behavior. See #2091. */
51 #define HANDLE_INVALID_MATCH                                   \
52     do {                                                       \
53         if (current_match->error != NULL) {                    \
54             yerror("Invalid match: %s", current_match->error); \
55             return;                                            \
56         }                                                      \
57     } while (0)
58
59 /** When the command did not include match criteria (!), we use the currently
60  * focused container. Do not confuse this case with a command which included
61  * criteria but which did not match any windows. This macro has to be called in
62  * every command.
63  */
64 #define HANDLE_EMPTY_MATCH                              \
65     do {                                                \
66         HANDLE_INVALID_MATCH;                           \
67                                                         \
68         if (match_is_empty(current_match)) {            \
69             while (!TAILQ_EMPTY(&owindows)) {           \
70                 owindow *ow = TAILQ_FIRST(&owindows);   \
71                 TAILQ_REMOVE(&owindows, ow, owindows);  \
72                 free(ow);                               \
73             }                                           \
74             owindow *ow = smalloc(sizeof(owindow));     \
75             ow->con = focused;                          \
76             TAILQ_INIT(&owindows);                      \
77             TAILQ_INSERT_TAIL(&owindows, ow, owindows); \
78         }                                               \
79     } while (0)
80
81 /*
82  * Checks whether we switched to a new workspace and returns false in that case,
83  * signaling that further workspace switching should be done by the calling function
84  * If not, calls workspace_back_and_forth() if workspace_auto_back_and_forth is set
85  * and return true, signaling that no further workspace switching should occur in the calling function.
86  *
87  */
88 static bool maybe_back_and_forth(struct CommandResultIR *cmd_output, const char *name) {
89     Con *ws = con_get_workspace(focused);
90
91     /* If we switched to a different workspace, do nothing */
92     if (strcmp(ws->name, name) != 0)
93         return false;
94
95     DLOG("This workspace is already focused.\n");
96     if (config.workspace_auto_back_and_forth) {
97         workspace_back_and_forth();
98         cmd_output->needs_tree_render = true;
99     }
100     return true;
101 }
102
103 /*
104  * Return the passed workspace unless it is the current one and auto back and
105  * forth is enabled, in which case the back_and_forth workspace is returned.
106  */
107 static Con *maybe_auto_back_and_forth_workspace(Con *workspace) {
108     Con *current, *baf;
109
110     if (!config.workspace_auto_back_and_forth)
111         return workspace;
112
113     current = con_get_workspace(focused);
114
115     if (current == workspace) {
116         baf = workspace_back_and_forth_get();
117         if (baf != NULL) {
118             DLOG("Substituting workspace with back_and_forth, as it is focused.\n");
119             return baf;
120         }
121     }
122
123     return workspace;
124 }
125
126 /*******************************************************************************
127  * Criteria functions.
128  ******************************************************************************/
129
130 /*
131  * Helper data structure for an operation window (window on which the operation
132  * will be performed). Used to build the TAILQ owindows.
133  *
134  */
135 typedef struct owindow {
136     Con *con;
137
138     TAILQ_ENTRY(owindow)
139     owindows;
140 } owindow;
141
142 typedef TAILQ_HEAD(owindows_head, owindow) owindows_head;
143
144 static owindows_head owindows;
145
146 /*
147  * Initializes the specified 'Match' data structure and the initial state of
148  * commands.c for matching target windows of a command.
149  *
150  */
151 void cmd_criteria_init(I3_CMD) {
152     Con *con;
153     owindow *ow;
154
155     DLOG("Initializing criteria, current_match = %p\n", current_match);
156     match_free(current_match);
157     match_init(current_match);
158     while (!TAILQ_EMPTY(&owindows)) {
159         ow = TAILQ_FIRST(&owindows);
160         TAILQ_REMOVE(&owindows, ow, owindows);
161         free(ow);
162     }
163     TAILQ_INIT(&owindows);
164     /* copy all_cons */
165     TAILQ_FOREACH(con, &all_cons, all_cons) {
166         ow = smalloc(sizeof(owindow));
167         ow->con = con;
168         TAILQ_INSERT_TAIL(&owindows, ow, owindows);
169     }
170 }
171
172 /*
173  * A match specification just finished (the closing square bracket was found),
174  * so we filter the list of owindows.
175  *
176  */
177 void cmd_criteria_match_windows(I3_CMD) {
178     owindow *next, *current;
179
180     DLOG("match specification finished, matching...\n");
181     /* copy the old list head to iterate through it and start with a fresh
182      * list which will contain only matching windows */
183     struct owindows_head old = owindows;
184     TAILQ_INIT(&owindows);
185     for (next = TAILQ_FIRST(&old); next != TAILQ_END(&old);) {
186         /* make a copy of the next pointer and advance the pointer to the
187          * next element as we are going to invalidate the element’s
188          * next/prev pointers by calling TAILQ_INSERT_TAIL later */
189         current = next;
190         next = TAILQ_NEXT(next, owindows);
191
192         DLOG("checking if con %p / %s matches\n", current->con, current->con->name);
193
194         /* We use this flag to prevent matching on window-less containers if
195          * only window-specific criteria were specified. */
196         bool accept_match = false;
197
198         if (current_match->con_id != NULL) {
199             accept_match = true;
200
201             if (current_match->con_id == current->con) {
202                 DLOG("con_id matched.\n");
203             } else {
204                 DLOG("con_id does not match.\n");
205                 FREE(current);
206                 continue;
207             }
208         }
209
210         if (current_match->mark != NULL && !TAILQ_EMPTY(&(current->con->marks_head))) {
211             accept_match = true;
212             bool matched_by_mark = false;
213
214             mark_t *mark;
215             TAILQ_FOREACH(mark, &(current->con->marks_head), marks) {
216                 if (!regex_matches(current_match->mark, mark->name))
217                     continue;
218
219                 DLOG("match by mark\n");
220                 matched_by_mark = true;
221                 break;
222             }
223
224             if (!matched_by_mark) {
225                 DLOG("mark does not match.\n");
226                 FREE(current);
227                 continue;
228             }
229         }
230
231         if (current->con->window != NULL) {
232             if (match_matches_window(current_match, current->con->window)) {
233                 DLOG("matches window!\n");
234                 accept_match = true;
235             } else {
236                 DLOG("doesn't match\n");
237                 FREE(current);
238                 continue;
239             }
240         }
241
242         if (accept_match) {
243             TAILQ_INSERT_TAIL(&owindows, current, owindows);
244         } else {
245             FREE(current);
246             continue;
247         }
248     }
249
250     TAILQ_FOREACH(current, &owindows, owindows) {
251         DLOG("matching: %p / %s\n", current->con, current->con->name);
252     }
253 }
254
255 /*
256  * Interprets a ctype=cvalue pair and adds it to the current match
257  * specification.
258  *
259  */
260 void cmd_criteria_add(I3_CMD, const char *ctype, const char *cvalue) {
261     match_parse_property(current_match, ctype, cvalue);
262 }
263
264 /*
265  * Implementation of 'move [window|container] [to] workspace
266  * next|prev|next_on_output|prev_on_output|current'.
267  *
268  */
269 void cmd_move_con_to_workspace(I3_CMD, const char *which) {
270     owindow *current;
271
272     DLOG("which=%s\n", which);
273
274     /* We have nothing to move:
275      *  when criteria was specified but didn't match any window or
276      *  when criteria wasn't specified and we don't have any window focused. */
277     if ((!match_is_empty(current_match) && TAILQ_EMPTY(&owindows)) ||
278         (match_is_empty(current_match) && focused->type == CT_WORKSPACE &&
279          !con_has_children(focused))) {
280         ysuccess(false);
281         return;
282     }
283
284     HANDLE_EMPTY_MATCH;
285
286     /* get the workspace */
287     Con *ws;
288     if (strcmp(which, "next") == 0)
289         ws = workspace_next();
290     else if (strcmp(which, "prev") == 0)
291         ws = workspace_prev();
292     else if (strcmp(which, "next_on_output") == 0)
293         ws = workspace_next_on_output();
294     else if (strcmp(which, "prev_on_output") == 0)
295         ws = workspace_prev_on_output();
296     else if (strcmp(which, "current") == 0)
297         ws = con_get_workspace(focused);
298     else {
299         ELOG("BUG: called with which=%s\n", which);
300         ysuccess(false);
301         return;
302     }
303
304     TAILQ_FOREACH(current, &owindows, owindows) {
305         DLOG("matching: %p / %s\n", current->con, current->con->name);
306         con_move_to_workspace(current->con, ws, true, false, false);
307     }
308
309     cmd_output->needs_tree_render = true;
310     // XXX: default reply for now, make this a better reply
311     ysuccess(true);
312 }
313
314 /**
315  * Implementation of 'move [window|container] [to] workspace back_and_forth'.
316  *
317  */
318 void cmd_move_con_to_workspace_back_and_forth(I3_CMD) {
319     owindow *current;
320     Con *ws;
321
322     ws = workspace_back_and_forth_get();
323
324     if (ws == NULL) {
325         yerror("No workspace was previously active.");
326         return;
327     }
328
329     HANDLE_EMPTY_MATCH;
330
331     TAILQ_FOREACH(current, &owindows, owindows) {
332         DLOG("matching: %p / %s\n", current->con, current->con->name);
333         con_move_to_workspace(current->con, ws, true, false, false);
334     }
335
336     cmd_output->needs_tree_render = true;
337     // XXX: default reply for now, make this a better reply
338     ysuccess(true);
339 }
340
341 /*
342  * Implementation of 'move [--no-auto-back-and-forth] [window|container] [to] workspace <name>'.
343  *
344  */
345 void cmd_move_con_to_workspace_name(I3_CMD, const char *name, const char *_no_auto_back_and_forth) {
346     if (strncasecmp(name, "__", strlen("__")) == 0) {
347         LOG("You cannot move containers to i3-internal workspaces (\"%s\").\n", name);
348         ysuccess(false);
349         return;
350     }
351
352     const bool no_auto_back_and_forth = (_no_auto_back_and_forth != NULL);
353     owindow *current;
354
355     /* We have nothing to move:
356      *  when criteria was specified but didn't match any window or
357      *  when criteria wasn't specified and we don't have any window focused. */
358     if (!match_is_empty(current_match) && TAILQ_EMPTY(&owindows)) {
359         ELOG("No windows match your criteria, cannot move.\n");
360         ysuccess(false);
361         return;
362     } else if (match_is_empty(current_match) && focused->type == CT_WORKSPACE &&
363                !con_has_children(focused)) {
364         ysuccess(false);
365         return;
366     }
367
368     LOG("should move window to workspace %s\n", name);
369     /* get the workspace */
370     Con *ws = workspace_get(name, NULL);
371
372     if (!no_auto_back_and_forth)
373         ws = maybe_auto_back_and_forth_workspace(ws);
374
375     HANDLE_EMPTY_MATCH;
376
377     TAILQ_FOREACH(current, &owindows, owindows) {
378         DLOG("matching: %p / %s\n", current->con, current->con->name);
379         con_move_to_workspace(current->con, ws, true, false, false);
380     }
381
382     cmd_output->needs_tree_render = true;
383     // XXX: default reply for now, make this a better reply
384     ysuccess(true);
385 }
386
387 /*
388  * Implementation of 'move [--no-auto-back-and-forth] [window|container] [to] workspace number <name>'.
389  *
390  */
391 void cmd_move_con_to_workspace_number(I3_CMD, const char *which, const char *_no_auto_back_and_forth) {
392     const bool no_auto_back_and_forth = (_no_auto_back_and_forth != NULL);
393     owindow *current;
394
395     /* We have nothing to move:
396      *  when criteria was specified but didn't match any window or
397      *  when criteria wasn't specified and we don't have any window focused. */
398     if ((!match_is_empty(current_match) && TAILQ_EMPTY(&owindows)) ||
399         (match_is_empty(current_match) && focused->type == CT_WORKSPACE &&
400          !con_has_children(focused))) {
401         ysuccess(false);
402         return;
403     }
404
405     LOG("should move window to workspace %s\n", which);
406     /* get the workspace */
407     Con *output, *workspace = NULL;
408
409     long parsed_num = ws_name_to_number(which);
410
411     if (parsed_num == -1) {
412         LOG("Could not parse initial part of \"%s\" as a number.\n", which);
413         yerror("Could not parse number \"%s\"", which);
414         return;
415     }
416
417     TAILQ_FOREACH(output, &(croot->nodes_head), nodes)
418     GREP_FIRST(workspace, output_get_content(output),
419                child->num == parsed_num);
420
421     if (!workspace) {
422         workspace = workspace_get(which, NULL);
423     }
424
425     if (!no_auto_back_and_forth)
426         workspace = maybe_auto_back_and_forth_workspace(workspace);
427
428     HANDLE_EMPTY_MATCH;
429
430     TAILQ_FOREACH(current, &owindows, owindows) {
431         DLOG("matching: %p / %s\n", current->con, current->con->name);
432         con_move_to_workspace(current->con, workspace, true, false, false);
433     }
434
435     cmd_output->needs_tree_render = true;
436     // XXX: default reply for now, make this a better reply
437     ysuccess(true);
438 }
439
440 static void cmd_resize_floating(I3_CMD, const char *way, const char *direction, Con *floating_con, int px) {
441     LOG("floating resize\n");
442     Rect old_rect = floating_con->rect;
443     Con *focused_con = con_descend_focused(floating_con);
444
445     /* ensure that resize will take place even if pixel increment is smaller than
446      * height increment or width increment.
447      * fixes #1011 */
448     const i3Window *window = focused_con->window;
449     if (window != NULL) {
450         if (strcmp(direction, "up") == 0 || strcmp(direction, "down") == 0 ||
451             strcmp(direction, "height") == 0) {
452             if (px < 0)
453                 px = (-px < window->height_increment) ? -window->height_increment : px;
454             else
455                 px = (px < window->height_increment) ? window->height_increment : px;
456         } else if (strcmp(direction, "left") == 0 || strcmp(direction, "right") == 0) {
457             if (px < 0)
458                 px = (-px < window->width_increment) ? -window->width_increment : px;
459             else
460                 px = (px < window->width_increment) ? window->width_increment : px;
461         }
462     }
463
464     if (strcmp(direction, "up") == 0) {
465         floating_con->rect.height += px;
466     } else if (strcmp(direction, "down") == 0 || strcmp(direction, "height") == 0) {
467         floating_con->rect.height += px;
468     } else if (strcmp(direction, "left") == 0) {
469         floating_con->rect.width += px;
470     } else {
471         floating_con->rect.width += px;
472     }
473
474     floating_check_size(floating_con);
475
476     /* Did we actually resize anything or did the size constraints prevent us?
477      * If we could not resize, exit now to not move the window. */
478     if (memcmp(&old_rect, &(floating_con->rect), sizeof(Rect)) == 0)
479         return;
480
481     if (strcmp(direction, "up") == 0) {
482         floating_con->rect.y -= (floating_con->rect.height - old_rect.height);
483     } else if (strcmp(direction, "left") == 0) {
484         floating_con->rect.x -= (floating_con->rect.width - old_rect.width);
485     }
486
487     /* If this is a scratchpad window, don't auto center it from now on. */
488     if (floating_con->scratchpad_state == SCRATCHPAD_FRESH)
489         floating_con->scratchpad_state = SCRATCHPAD_CHANGED;
490 }
491
492 static bool cmd_resize_tiling_direction(I3_CMD, Con *current, const char *way, const char *direction, int ppt) {
493     LOG("tiling resize\n");
494     Con *second = NULL;
495     Con *first = current;
496     direction_t search_direction;
497     if (!strcmp(direction, "left"))
498         search_direction = D_LEFT;
499     else if (!strcmp(direction, "right"))
500         search_direction = D_RIGHT;
501     else if (!strcmp(direction, "up"))
502         search_direction = D_UP;
503     else
504         search_direction = D_DOWN;
505
506     bool res = resize_find_tiling_participants(&first, &second, search_direction, false);
507     if (!res) {
508         LOG("No second container in this direction found.\n");
509         ysuccess(false);
510         return false;
511     }
512
513     /* get the default percentage */
514     int children = con_num_children(first->parent);
515     LOG("ins. %d children\n", children);
516     double percentage = 1.0 / children;
517     LOG("default percentage = %f\n", percentage);
518
519     /* resize */
520     LOG("first->percent before = %f\n", first->percent);
521     LOG("second->percent before = %f\n", second->percent);
522     if (first->percent == 0.0)
523         first->percent = percentage;
524     if (second->percent == 0.0)
525         second->percent = percentage;
526     double new_first_percent = first->percent + ((double)ppt / 100.0);
527     double new_second_percent = second->percent - ((double)ppt / 100.0);
528     LOG("new_first_percent = %f\n", new_first_percent);
529     LOG("new_second_percent = %f\n", new_second_percent);
530     /* Ensure that the new percentages are positive. */
531     if (new_first_percent > 0.0 && new_second_percent > 0.0) {
532         first->percent = new_first_percent;
533         second->percent = new_second_percent;
534         LOG("first->percent after = %f\n", first->percent);
535         LOG("second->percent after = %f\n", second->percent);
536     } else {
537         LOG("Not resizing, already at minimum size\n");
538     }
539
540     return true;
541 }
542
543 static bool cmd_resize_tiling_width_height(I3_CMD, Con *current, const char *way, const char *direction, int ppt) {
544     LOG("width/height resize\n");
545
546     /* get the appropriate current container (skip stacked/tabbed cons) */
547     Con *dummy = NULL;
548     direction_t search_direction = (strcmp(direction, "width") == 0 ? D_LEFT : D_DOWN);
549     bool search_result = resize_find_tiling_participants(&current, &dummy, search_direction, true);
550     if (search_result == false) {
551         ysuccess(false);
552         return false;
553     }
554
555     /* get the default percentage */
556     int children = con_num_children(current->parent);
557     LOG("ins. %d children\n", children);
558     double percentage = 1.0 / children;
559     LOG("default percentage = %f\n", percentage);
560
561     /* Ensure all the other children have a percentage set. */
562     Con *child;
563     TAILQ_FOREACH(child, &(current->parent->nodes_head), nodes) {
564         LOG("child->percent = %f (child %p)\n", child->percent, child);
565         if (child->percent == 0.0)
566             child->percent = percentage;
567     }
568
569     double new_current_percent = current->percent + ((double)ppt / 100.0);
570     double subtract_percent = ((double)ppt / 100.0) / (children - 1);
571     LOG("new_current_percent = %f\n", new_current_percent);
572     LOG("subtract_percent = %f\n", subtract_percent);
573     /* Ensure that the new percentages are positive. */
574     TAILQ_FOREACH(child, &(current->parent->nodes_head), nodes) {
575         if (child == current)
576             continue;
577         if (child->percent - subtract_percent <= 0.0) {
578             LOG("Not resizing, already at minimum size (child %p would end up with a size of %.f\n", child, child->percent - subtract_percent);
579             ysuccess(false);
580             return false;
581         }
582     }
583     if (new_current_percent <= 0.0) {
584         LOG("Not resizing, already at minimum size\n");
585         ysuccess(false);
586         return false;
587     }
588
589     current->percent = new_current_percent;
590     LOG("current->percent after = %f\n", current->percent);
591
592     TAILQ_FOREACH(child, &(current->parent->nodes_head), nodes) {
593         if (child == current)
594             continue;
595         child->percent -= subtract_percent;
596         LOG("child->percent after (%p) = %f\n", child, child->percent);
597     }
598
599     return true;
600 }
601
602 /*
603  * Implementation of 'resize grow|shrink <direction> [<px> px] [or <ppt> ppt]'.
604  *
605  */
606 void cmd_resize(I3_CMD, const char *way, const char *direction, long resize_px, long resize_ppt) {
607     DLOG("resizing in way %s, direction %s, px %ld or ppt %ld\n", way, direction, resize_px, resize_ppt);
608     if (strcmp(way, "shrink") == 0) {
609         resize_px *= -1;
610         resize_ppt *= -1;
611     }
612
613     HANDLE_EMPTY_MATCH;
614
615     owindow *current;
616     TAILQ_FOREACH(current, &owindows, owindows) {
617         /* Don't handle dock windows (issue #1201) */
618         if (current->con->window && current->con->window->dock) {
619             DLOG("This is a dock window. Not resizing (con = %p)\n)", current->con);
620             continue;
621         }
622
623         Con *floating_con;
624         if ((floating_con = con_inside_floating(current->con))) {
625             cmd_resize_floating(current_match, cmd_output, way, direction, floating_con, resize_px);
626         } else {
627             if (strcmp(direction, "width") == 0 ||
628                 strcmp(direction, "height") == 0) {
629                 if (!cmd_resize_tiling_width_height(current_match, cmd_output,
630                                                     current->con, way, direction, resize_ppt))
631                     return;
632             } else {
633                 if (!cmd_resize_tiling_direction(current_match, cmd_output,
634                                                  current->con, way, direction, resize_ppt))
635                     return;
636             }
637         }
638     }
639
640     cmd_output->needs_tree_render = true;
641     // XXX: default reply for now, make this a better reply
642     ysuccess(true);
643 }
644
645 /*
646  * Implementation of 'resize set <width> [px | ppt] <height> [px | ppt]'.
647  *
648  */
649 void cmd_resize_set(I3_CMD, long cwidth, const char *mode_width, long cheight, const char *mode_height) {
650     DLOG("resizing to %ld %s x %ld %s\n", cwidth, mode_width, cheight, mode_height);
651     if (cwidth < 0 || cheight < 0) {
652         ELOG("Resize failed: dimensions cannot be negative (was %ld %s x %ld %s)\n", cwidth, mode_width, cheight, mode_height);
653         return;
654     }
655
656     HANDLE_EMPTY_MATCH;
657
658     owindow *current;
659     bool success = true;
660     TAILQ_FOREACH(current, &owindows, owindows) {
661         Con *floating_con;
662         if ((floating_con = con_inside_floating(current->con))) {
663             Con *output = con_get_output(floating_con);
664             if (cwidth == 0) {
665                 cwidth = output->rect.width;
666             } else if (mode_width && strcmp(mode_width, "ppt") == 0) {
667                 cwidth = output->rect.width * ((double)cwidth / 100.0);
668             }
669             if (cheight == 0) {
670                 cheight = output->rect.height;
671             } else if (mode_height && strcmp(mode_height, "ppt") == 0) {
672                 cheight = output->rect.height * ((double)cheight / 100.0);
673             }
674             floating_resize(floating_con, cwidth, cheight);
675         } else {
676             if (current->con->window && current->con->window->dock) {
677                 DLOG("This is a dock window. Not resizing (con = %p)\n)", current->con);
678                 continue;
679             }
680
681             if (cwidth > 0 && mode_width && strcmp(mode_width, "ppt") == 0) {
682                 /* get the appropriate current container (skip stacked/tabbed cons) */
683                 Con *target = current->con;
684                 Con *dummy;
685                 resize_find_tiling_participants(&target, &dummy, D_LEFT, true);
686
687                 /* Calculate new size for the target container */
688                 double current_percent = target->percent;
689                 char *action_string;
690                 long adjustment;
691
692                 if (current_percent > cwidth) {
693                     action_string = "shrink";
694                     adjustment = (int)(current_percent * 100) - cwidth;
695                 } else {
696                     action_string = "grow";
697                     adjustment = cwidth - (int)(current_percent * 100);
698                 }
699
700                 /* perform resizing and report failure if not possible */
701                 if (!cmd_resize_tiling_width_height(current_match, cmd_output,
702                                                     target, action_string, "width", adjustment)) {
703                     success = false;
704                 }
705             }
706
707             if (cheight > 0 && mode_width && strcmp(mode_width, "ppt") == 0) {
708                 /* get the appropriate current container (skip stacked/tabbed cons) */
709                 Con *target = current->con;
710                 Con *dummy;
711                 resize_find_tiling_participants(&target, &dummy, D_DOWN, true);
712
713                 /* Calculate new size for the target container */
714                 double current_percent = target->percent;
715                 char *action_string;
716                 long adjustment;
717
718                 if (current_percent > cheight) {
719                     action_string = "shrink";
720                     adjustment = (int)(current_percent * 100) - cheight;
721                 } else {
722                     action_string = "grow";
723                     adjustment = cheight - (int)(current_percent * 100);
724                 }
725
726                 /* perform resizing and report failure if not possible */
727                 if (!cmd_resize_tiling_width_height(current_match, cmd_output,
728                                                     target, action_string, "height", adjustment)) {
729                     success = false;
730                 }
731             }
732         }
733     }
734
735     cmd_output->needs_tree_render = true;
736     ysuccess(success);
737 }
738
739 /*
740  * Implementation of 'border normal|pixel [<n>]', 'border none|1pixel|toggle'.
741  *
742  */
743 void cmd_border(I3_CMD, const char *border_style_str, long border_width) {
744     DLOG("border style should be changed to %s with border width %ld\n", border_style_str, border_width);
745     owindow *current;
746
747     HANDLE_EMPTY_MATCH;
748
749     TAILQ_FOREACH(current, &owindows, owindows) {
750         DLOG("matching: %p / %s\n", current->con, current->con->name);
751         int border_style = current->con->border_style;
752         int con_border_width = border_width;
753
754         if (strcmp(border_style_str, "toggle") == 0) {
755             border_style++;
756             border_style %= 3;
757             if (border_style == BS_NORMAL)
758                 con_border_width = 2;
759             else if (border_style == BS_NONE)
760                 con_border_width = 0;
761             else if (border_style == BS_PIXEL)
762                 con_border_width = 1;
763         } else {
764             if (strcmp(border_style_str, "normal") == 0) {
765                 border_style = BS_NORMAL;
766             } else if (strcmp(border_style_str, "pixel") == 0) {
767                 border_style = BS_PIXEL;
768             } else if (strcmp(border_style_str, "1pixel") == 0) {
769                 border_style = BS_PIXEL;
770                 con_border_width = 1;
771             } else if (strcmp(border_style_str, "none") == 0) {
772                 border_style = BS_NONE;
773             } else {
774                 ELOG("BUG: called with border_style=%s\n", border_style_str);
775                 ysuccess(false);
776                 return;
777             }
778         }
779
780         con_set_border_style(current->con, border_style, logical_px(con_border_width));
781     }
782
783     cmd_output->needs_tree_render = true;
784     // XXX: default reply for now, make this a better reply
785     ysuccess(true);
786 }
787
788 /*
789  * Implementation of 'nop <comment>'.
790  *
791  */
792 void cmd_nop(I3_CMD, const char *comment) {
793     LOG("-------------------------------------------------\n");
794     LOG("  NOP: %s\n", comment);
795     LOG("-------------------------------------------------\n");
796     ysuccess(true);
797 }
798
799 /*
800  * Implementation of 'append_layout <path>'.
801  *
802  */
803 void cmd_append_layout(I3_CMD, const char *cpath) {
804     char *path = sstrdup(cpath);
805     LOG("Appending layout \"%s\"\n", path);
806
807     /* Make sure we allow paths like '~/.i3/layout.json' */
808     path = resolve_tilde(path);
809
810     char *buf = NULL;
811     ssize_t len;
812     if ((len = slurp(path, &buf)) < 0) {
813         /* slurp already logged an error. */
814         goto out;
815     }
816
817     if (!json_validate(buf, len)) {
818         ELOG("Could not parse \"%s\" as JSON, not loading.\n", path);
819         yerror("Could not parse \"%s\" as JSON.", path);
820         goto out;
821     }
822
823     json_content_t content = json_determine_content(buf, len);
824     LOG("JSON content = %d\n", content);
825     if (content == JSON_CONTENT_UNKNOWN) {
826         ELOG("Could not determine the contents of \"%s\", not loading.\n", path);
827         yerror("Could not determine the contents of \"%s\".", path);
828         goto out;
829     }
830
831     Con *parent = focused;
832     if (content == JSON_CONTENT_WORKSPACE) {
833         parent = output_get_content(con_get_output(parent));
834     } else {
835         /* We need to append the layout to a split container, since a leaf
836          * container must not have any children (by definition).
837          * Note that we explicitly check for workspaces, since they are okay for
838          * this purpose, but con_accepts_window() returns false for workspaces. */
839         while (parent->type != CT_WORKSPACE && !con_accepts_window(parent))
840             parent = parent->parent;
841     }
842     DLOG("Appending to parent=%p instead of focused=%p\n", parent, focused);
843     char *errormsg = NULL;
844     tree_append_json(parent, buf, len, &errormsg);
845     if (errormsg != NULL) {
846         yerror(errormsg);
847         free(errormsg);
848         /* Note that we continue executing since tree_append_json() has
849          * side-effects — user-provided layouts can be partly valid, partly
850          * invalid, leading to half of the placeholder containers being
851          * created. */
852     } else {
853         ysuccess(true);
854     }
855
856     // XXX: This is a bit of a kludge. Theoretically, render_con(parent,
857     // false); should be enough, but when sending 'workspace 4; append_layout
858     // /tmp/foo.json', the needs_tree_render == true of the workspace command
859     // is not executed yet and will be batched with append_layout’s
860     // needs_tree_render after the parser finished. We should check if that is
861     // necessary at all.
862     render_con(croot, false);
863
864     restore_open_placeholder_windows(parent);
865
866     if (content == JSON_CONTENT_WORKSPACE)
867         ipc_send_workspace_event("restored", parent, NULL);
868
869     cmd_output->needs_tree_render = true;
870 out:
871     free(path);
872     free(buf);
873 }
874
875 /*
876  * Implementation of 'workspace next|prev|next_on_output|prev_on_output'.
877  *
878  */
879 void cmd_workspace(I3_CMD, const char *which) {
880     Con *ws;
881
882     DLOG("which=%s\n", which);
883
884     if (con_get_fullscreen_con(croot, CF_GLOBAL)) {
885         LOG("Cannot switch workspace while in global fullscreen\n");
886         ysuccess(false);
887         return;
888     }
889
890     if (strcmp(which, "next") == 0)
891         ws = workspace_next();
892     else if (strcmp(which, "prev") == 0)
893         ws = workspace_prev();
894     else if (strcmp(which, "next_on_output") == 0)
895         ws = workspace_next_on_output();
896     else if (strcmp(which, "prev_on_output") == 0)
897         ws = workspace_prev_on_output();
898     else {
899         ELOG("BUG: called with which=%s\n", which);
900         ysuccess(false);
901         return;
902     }
903
904     workspace_show(ws);
905
906     cmd_output->needs_tree_render = true;
907     // XXX: default reply for now, make this a better reply
908     ysuccess(true);
909 }
910
911 /*
912  * Implementation of 'workspace [--no-auto-back-and-forth] number <name>'
913  *
914  */
915 void cmd_workspace_number(I3_CMD, const char *which, const char *_no_auto_back_and_forth) {
916     const bool no_auto_back_and_forth = (_no_auto_back_and_forth != NULL);
917     Con *output, *workspace = NULL;
918
919     if (con_get_fullscreen_con(croot, CF_GLOBAL)) {
920         LOG("Cannot switch workspace while in global fullscreen\n");
921         ysuccess(false);
922         return;
923     }
924
925     long parsed_num = ws_name_to_number(which);
926
927     if (parsed_num == -1) {
928         LOG("Could not parse initial part of \"%s\" as a number.\n", which);
929         yerror("Could not parse number \"%s\"", which);
930         return;
931     }
932
933     TAILQ_FOREACH(output, &(croot->nodes_head), nodes)
934     GREP_FIRST(workspace, output_get_content(output),
935                child->num == parsed_num);
936
937     if (!workspace) {
938         LOG("There is no workspace with number %ld, creating a new one.\n", parsed_num);
939         ysuccess(true);
940         workspace_show_by_name(which);
941         cmd_output->needs_tree_render = true;
942         return;
943     }
944     if (!no_auto_back_and_forth && maybe_back_and_forth(cmd_output, workspace->name)) {
945         ysuccess(true);
946         return;
947     }
948     workspace_show(workspace);
949
950     cmd_output->needs_tree_render = true;
951     // XXX: default reply for now, make this a better reply
952     ysuccess(true);
953 }
954
955 /*
956  * Implementation of 'workspace back_and_forth'.
957  *
958  */
959 void cmd_workspace_back_and_forth(I3_CMD) {
960     if (con_get_fullscreen_con(croot, CF_GLOBAL)) {
961         LOG("Cannot switch workspace while in global fullscreen\n");
962         ysuccess(false);
963         return;
964     }
965
966     workspace_back_and_forth();
967
968     cmd_output->needs_tree_render = true;
969     // XXX: default reply for now, make this a better reply
970     ysuccess(true);
971 }
972
973 /*
974  * Implementation of 'workspace [--no-auto-back-and-forth] <name>'
975  *
976  */
977 void cmd_workspace_name(I3_CMD, const char *name, const char *_no_auto_back_and_forth) {
978     const bool no_auto_back_and_forth = (_no_auto_back_and_forth != NULL);
979
980     if (strncasecmp(name, "__", strlen("__")) == 0) {
981         LOG("You cannot switch to the i3-internal workspaces (\"%s\").\n", name);
982         ysuccess(false);
983         return;
984     }
985
986     if (con_get_fullscreen_con(croot, CF_GLOBAL)) {
987         LOG("Cannot switch workspace while in global fullscreen\n");
988         ysuccess(false);
989         return;
990     }
991
992     DLOG("should switch to workspace %s\n", name);
993     if (!no_auto_back_and_forth && maybe_back_and_forth(cmd_output, name)) {
994         ysuccess(true);
995         return;
996     }
997     workspace_show_by_name(name);
998
999     cmd_output->needs_tree_render = true;
1000     // XXX: default reply for now, make this a better reply
1001     ysuccess(true);
1002 }
1003
1004 /*
1005  * Implementation of 'mark [--add|--replace] [--toggle] <mark>'
1006  *
1007  */
1008 void cmd_mark(I3_CMD, const char *mark, const char *mode, const char *toggle) {
1009     HANDLE_EMPTY_MATCH;
1010
1011     owindow *current = TAILQ_FIRST(&owindows);
1012     if (current == NULL) {
1013         ysuccess(false);
1014         return;
1015     }
1016
1017     /* Marks must be unique, i.e., no two windows must have the same mark. */
1018     if (current != TAILQ_LAST(&owindows, owindows_head)) {
1019         yerror("A mark must not be put onto more than one window");
1020         return;
1021     }
1022
1023     DLOG("matching: %p / %s\n", current->con, current->con->name);
1024
1025     mark_mode_t mark_mode = (mode == NULL || strcmp(mode, "--replace") == 0) ? MM_REPLACE : MM_ADD;
1026     if (toggle != NULL) {
1027         con_mark_toggle(current->con, mark, mark_mode);
1028     } else {
1029         con_mark(current->con, mark, mark_mode);
1030     }
1031
1032     cmd_output->needs_tree_render = true;
1033     // XXX: default reply for now, make this a better reply
1034     ysuccess(true);
1035 }
1036
1037 /*
1038  * Implementation of 'unmark [mark]'
1039  *
1040  */
1041 void cmd_unmark(I3_CMD, const char *mark) {
1042     if (match_is_empty(current_match)) {
1043         con_unmark(NULL, mark);
1044     } else {
1045         owindow *current;
1046         TAILQ_FOREACH(current, &owindows, owindows) {
1047             con_unmark(current->con, mark);
1048         }
1049     }
1050
1051     cmd_output->needs_tree_render = true;
1052     // XXX: default reply for now, make this a better reply
1053     ysuccess(true);
1054 }
1055
1056 /*
1057  * Implementation of 'mode <string>'.
1058  *
1059  */
1060 void cmd_mode(I3_CMD, const char *mode) {
1061     DLOG("mode=%s\n", mode);
1062     switch_mode(mode);
1063
1064     // XXX: default reply for now, make this a better reply
1065     ysuccess(true);
1066 }
1067
1068 /*
1069  * Implementation of 'move [window|container] [to] output <str>'.
1070  *
1071  */
1072 void cmd_move_con_to_output(I3_CMD, const char *name) {
1073     DLOG("Should move window to output \"%s\".\n", name);
1074     HANDLE_EMPTY_MATCH;
1075
1076     owindow *current;
1077     bool had_error = false;
1078     TAILQ_FOREACH(current, &owindows, owindows) {
1079         DLOG("matching: %p / %s\n", current->con, current->con->name);
1080
1081         had_error |= !con_move_to_output_name(current->con, name, true);
1082     }
1083
1084     cmd_output->needs_tree_render = true;
1085     ysuccess(!had_error);
1086 }
1087
1088 /*
1089  * Implementation of 'move [container|window] [to] mark <str>'.
1090  *
1091  */
1092 void cmd_move_con_to_mark(I3_CMD, const char *mark) {
1093     DLOG("moving window to mark \"%s\"\n", mark);
1094
1095     HANDLE_EMPTY_MATCH;
1096
1097     bool result = true;
1098     owindow *current;
1099     TAILQ_FOREACH(current, &owindows, owindows) {
1100         DLOG("moving matched window %p / %s to mark \"%s\"\n", current->con, current->con->name, mark);
1101         result &= con_move_to_mark(current->con, mark);
1102     }
1103
1104     cmd_output->needs_tree_render = true;
1105     ysuccess(result);
1106 }
1107
1108 /*
1109  * Implementation of 'floating enable|disable|toggle'
1110  *
1111  */
1112 void cmd_floating(I3_CMD, const char *floating_mode) {
1113     owindow *current;
1114
1115     DLOG("floating_mode=%s\n", floating_mode);
1116
1117     HANDLE_EMPTY_MATCH;
1118
1119     TAILQ_FOREACH(current, &owindows, owindows) {
1120         DLOG("matching: %p / %s\n", current->con, current->con->name);
1121         if (strcmp(floating_mode, "toggle") == 0) {
1122             DLOG("should toggle mode\n");
1123             toggle_floating_mode(current->con, false);
1124         } else {
1125             DLOG("should switch mode to %s\n", floating_mode);
1126             if (strcmp(floating_mode, "enable") == 0) {
1127                 floating_enable(current->con, false);
1128             } else {
1129                 floating_disable(current->con, false);
1130             }
1131         }
1132     }
1133
1134     cmd_output->needs_tree_render = true;
1135     // XXX: default reply for now, make this a better reply
1136     ysuccess(true);
1137 }
1138
1139 /*
1140  * Implementation of 'move workspace to [output] <str>'.
1141  *
1142  */
1143 void cmd_move_workspace_to_output(I3_CMD, const char *name) {
1144     DLOG("should move workspace to output %s\n", name);
1145
1146     HANDLE_EMPTY_MATCH;
1147
1148     owindow *current;
1149     TAILQ_FOREACH(current, &owindows, owindows) {
1150         Con *ws = con_get_workspace(current->con);
1151         if (con_is_internal(ws)) {
1152             continue;
1153         }
1154
1155         bool success = workspace_move_to_output(ws, name);
1156         if (!success) {
1157             ELOG("Failed to move workspace to output.\n");
1158             ysuccess(false);
1159             return;
1160         }
1161     }
1162
1163     cmd_output->needs_tree_render = true;
1164     // XXX: default reply for now, make this a better reply
1165     ysuccess(true);
1166 }
1167
1168 /*
1169  * Implementation of 'split v|h|t|vertical|horizontal|toggle'.
1170  *
1171  */
1172 void cmd_split(I3_CMD, const char *direction) {
1173     HANDLE_EMPTY_MATCH;
1174
1175     owindow *current;
1176     LOG("splitting in direction %c\n", direction[0]);
1177     TAILQ_FOREACH(current, &owindows, owindows) {
1178         if (con_is_docked(current->con)) {
1179             ELOG("Cannot split a docked container, skipping.\n");
1180             continue;
1181         }
1182
1183         DLOG("matching: %p / %s\n", current->con, current->con->name);
1184         if (direction[0] == 't') {
1185             layout_t current_layout;
1186             if (current->con->type == CT_WORKSPACE) {
1187                 current_layout = current->con->layout;
1188             } else {
1189                 current_layout = current->con->parent->layout;
1190             }
1191             /* toggling split orientation */
1192             if (current_layout == L_SPLITH) {
1193                 tree_split(current->con, VERT);
1194             } else {
1195                 tree_split(current->con, HORIZ);
1196             }
1197         } else {
1198             tree_split(current->con, (direction[0] == 'v' ? VERT : HORIZ));
1199         }
1200     }
1201
1202     cmd_output->needs_tree_render = true;
1203     // XXX: default reply for now, make this a better reply
1204     ysuccess(true);
1205 }
1206
1207 /*
1208  * Implementation of 'kill [window|client]'.
1209  *
1210  */
1211 void cmd_kill(I3_CMD, const char *kill_mode_str) {
1212     if (kill_mode_str == NULL)
1213         kill_mode_str = "window";
1214
1215     DLOG("kill_mode=%s\n", kill_mode_str);
1216
1217     int kill_mode;
1218     if (strcmp(kill_mode_str, "window") == 0)
1219         kill_mode = KILL_WINDOW;
1220     else if (strcmp(kill_mode_str, "client") == 0)
1221         kill_mode = KILL_CLIENT;
1222     else {
1223         ELOG("BUG: called with kill_mode=%s\n", kill_mode_str);
1224         ysuccess(false);
1225         return;
1226     }
1227
1228     HANDLE_EMPTY_MATCH;
1229
1230     owindow *current;
1231     TAILQ_FOREACH(current, &owindows, owindows) {
1232         con_close(current->con, kill_mode);
1233     }
1234
1235     cmd_output->needs_tree_render = true;
1236     // XXX: default reply for now, make this a better reply
1237     ysuccess(true);
1238 }
1239
1240 /*
1241  * Implementation of 'exec [--no-startup-id] <command>'.
1242  *
1243  */
1244 void cmd_exec(I3_CMD, const char *nosn, const char *command) {
1245     bool no_startup_id = (nosn != NULL);
1246
1247     DLOG("should execute %s, no_startup_id = %d\n", command, no_startup_id);
1248     start_application(command, no_startup_id);
1249
1250     // XXX: default reply for now, make this a better reply
1251     ysuccess(true);
1252 }
1253
1254 /*
1255  * Implementation of 'focus left|right|up|down'.
1256  *
1257  */
1258 void cmd_focus_direction(I3_CMD, const char *direction) {
1259     DLOG("direction = *%s*\n", direction);
1260
1261     if (strcmp(direction, "left") == 0)
1262         tree_next('p', HORIZ);
1263     else if (strcmp(direction, "right") == 0)
1264         tree_next('n', HORIZ);
1265     else if (strcmp(direction, "up") == 0)
1266         tree_next('p', VERT);
1267     else if (strcmp(direction, "down") == 0)
1268         tree_next('n', VERT);
1269     else {
1270         ELOG("Invalid focus direction (%s)\n", direction);
1271         ysuccess(false);
1272         return;
1273     }
1274
1275     cmd_output->needs_tree_render = true;
1276     // XXX: default reply for now, make this a better reply
1277     ysuccess(true);
1278 }
1279
1280 /*
1281  * Focus a container and disable any other fullscreen container not permitting the focus.
1282  *
1283  */
1284 static void cmd_focus_force_focus(Con *con) {
1285     /* Disable fullscreen container in workspace with container to be focused. */
1286     Con *ws = con_get_workspace(con);
1287     Con *fullscreen_on_ws = (focused && focused->fullscreen_mode == CF_GLOBAL) ? focused : con_get_fullscreen_con(ws, CF_OUTPUT);
1288     if (fullscreen_on_ws && fullscreen_on_ws != con && !con_has_parent(con, fullscreen_on_ws)) {
1289         con_disable_fullscreen(fullscreen_on_ws);
1290     }
1291     con_activate(con);
1292 }
1293
1294 /*
1295  * Implementation of 'focus tiling|floating|mode_toggle'.
1296  *
1297  */
1298 void cmd_focus_window_mode(I3_CMD, const char *window_mode) {
1299     DLOG("window_mode = %s\n", window_mode);
1300
1301     bool to_floating = false;
1302     if (strcmp(window_mode, "mode_toggle") == 0) {
1303         to_floating = !con_inside_floating(focused);
1304     } else if (strcmp(window_mode, "floating") == 0) {
1305         to_floating = true;
1306     } else if (strcmp(window_mode, "tiling") == 0) {
1307         to_floating = false;
1308     }
1309
1310     Con *ws = con_get_workspace(focused);
1311     Con *current;
1312     bool success = false;
1313     TAILQ_FOREACH(current, &(ws->focus_head), focused) {
1314         if ((to_floating && current->type != CT_FLOATING_CON) ||
1315             (!to_floating && current->type == CT_FLOATING_CON))
1316             continue;
1317
1318         cmd_focus_force_focus(con_descend_focused(current));
1319         success = true;
1320         break;
1321     }
1322
1323     if (success) {
1324         cmd_output->needs_tree_render = true;
1325         ysuccess(true);
1326     } else {
1327         yerror("Failed to find a %s container in workspace.", to_floating ? "floating" : "tiling");
1328     }
1329 }
1330
1331 /*
1332  * Implementation of 'focus parent|child'.
1333  *
1334  */
1335 void cmd_focus_level(I3_CMD, const char *level) {
1336     DLOG("level = %s\n", level);
1337     bool success = false;
1338
1339     /* Focusing the parent can only be allowed if the newly
1340      * focused container won't escape the fullscreen container. */
1341     if (strcmp(level, "parent") == 0) {
1342         if (focused && focused->parent) {
1343             if (con_fullscreen_permits_focusing(focused->parent))
1344                 success = level_up();
1345             else
1346                 ELOG("'focus parent': Currently in fullscreen, not going up\n");
1347         }
1348     }
1349
1350     /* Focusing a child should always be allowed. */
1351     else
1352         success = level_down();
1353
1354     cmd_output->needs_tree_render = success;
1355     // XXX: default reply for now, make this a better reply
1356     ysuccess(success);
1357 }
1358
1359 /*
1360  * Implementation of 'focus'.
1361  *
1362  */
1363 void cmd_focus(I3_CMD) {
1364     DLOG("current_match = %p\n", current_match);
1365
1366     if (match_is_empty(current_match)) {
1367         ELOG("You have to specify which window/container should be focused.\n");
1368         ELOG("Example: [class=\"urxvt\" title=\"irssi\"] focus\n");
1369
1370         yerror("You have to specify which window/container should be focused");
1371
1372         return;
1373     }
1374
1375     Con *__i3_scratch = workspace_get("__i3_scratch", NULL);
1376     int count = 0;
1377     owindow *current;
1378     TAILQ_FOREACH(current, &owindows, owindows) {
1379         Con *ws = con_get_workspace(current->con);
1380         /* If no workspace could be found, this was a dock window.
1381          * Just skip it, you cannot focus dock windows. */
1382         if (!ws)
1383             continue;
1384
1385         /* In case this is a scratchpad window, call scratchpad_show(). */
1386         if (ws == __i3_scratch) {
1387             scratchpad_show(current->con);
1388             count++;
1389             /* While for the normal focus case we can change focus multiple
1390              * times and only a single window ends up focused, we could show
1391              * multiple scratchpad windows. So, rather break here. */
1392             break;
1393         }
1394
1395         /* If the container is not on the current workspace,
1396          * workspace_show() will switch to a different workspace and (if
1397          * enabled) trigger a mouse pointer warp to the currently focused
1398          * container (!) on the target workspace.
1399          *
1400          * Therefore, before calling workspace_show(), we make sure that
1401          * 'current' will be focused on the workspace. However, we cannot
1402          * just con_focus(current) because then the pointer will not be
1403          * warped at all (the code thinks we are already there).
1404          *
1405          * So we focus 'current' to make it the currently focused window of
1406          * the target workspace, then revert focus. */
1407         Con *currently_focused = focused;
1408         cmd_focus_force_focus(current->con);
1409         con_activate(currently_focused);
1410
1411         /* Now switch to the workspace, then focus */
1412         workspace_show(ws);
1413         LOG("focusing %p / %s\n", current->con, current->con->name);
1414         con_activate(current->con);
1415         count++;
1416     }
1417
1418     if (count > 1)
1419         LOG("WARNING: Your criteria for the focus command matches %d containers, "
1420             "while only exactly one container can be focused at a time.\n",
1421             count);
1422
1423     cmd_output->needs_tree_render = true;
1424     ysuccess(count > 0);
1425 }
1426
1427 /*
1428  * Implementation of 'fullscreen enable|toggle [global]' and
1429  *                   'fullscreen disable'
1430  *
1431  */
1432 void cmd_fullscreen(I3_CMD, const char *action, const char *fullscreen_mode) {
1433     fullscreen_mode_t mode = strcmp(fullscreen_mode, "global") == 0 ? CF_GLOBAL : CF_OUTPUT;
1434     DLOG("%s fullscreen, mode = %s\n", action, fullscreen_mode);
1435     owindow *current;
1436
1437     HANDLE_EMPTY_MATCH;
1438
1439     TAILQ_FOREACH(current, &owindows, owindows) {
1440         DLOG("matching: %p / %s\n", current->con, current->con->name);
1441         if (strcmp(action, "toggle") == 0) {
1442             con_toggle_fullscreen(current->con, mode);
1443         } else if (strcmp(action, "enable") == 0) {
1444             con_enable_fullscreen(current->con, mode);
1445         } else if (strcmp(action, "disable") == 0) {
1446             con_disable_fullscreen(current->con);
1447         }
1448     }
1449
1450     cmd_output->needs_tree_render = true;
1451     // XXX: default reply for now, make this a better reply
1452     ysuccess(true);
1453 }
1454
1455 /*
1456  * Implementation of 'sticky enable|disable|toggle'.
1457  *
1458  */
1459 void cmd_sticky(I3_CMD, const char *action) {
1460     DLOG("%s sticky on window\n", action);
1461     HANDLE_EMPTY_MATCH;
1462
1463     owindow *current;
1464     TAILQ_FOREACH(current, &owindows, owindows) {
1465         if (current->con->window == NULL) {
1466             ELOG("only containers holding a window can be made sticky, skipping con = %p\n", current->con);
1467             continue;
1468         }
1469         DLOG("setting sticky for container = %p / %s\n", current->con, current->con->name);
1470
1471         bool sticky = false;
1472         if (strcmp(action, "enable") == 0)
1473             sticky = true;
1474         else if (strcmp(action, "disable") == 0)
1475             sticky = false;
1476         else if (strcmp(action, "toggle") == 0)
1477             sticky = !current->con->sticky;
1478
1479         current->con->sticky = sticky;
1480         ewmh_update_sticky(current->con->window->id, sticky);
1481     }
1482
1483     /* A window we made sticky might not be on a visible workspace right now, so we need to make
1484      * sure it gets pushed to the front now. */
1485     output_push_sticky_windows(focused);
1486
1487     ewmh_update_wm_desktop();
1488
1489     cmd_output->needs_tree_render = true;
1490     ysuccess(true);
1491 }
1492
1493 /*
1494  * Implementation of 'move <direction> [<pixels> [px]]'.
1495  *
1496  */
1497 void cmd_move_direction(I3_CMD, const char *direction, long move_px) {
1498     owindow *current;
1499     HANDLE_EMPTY_MATCH;
1500
1501     Con *initially_focused = focused;
1502
1503     TAILQ_FOREACH(current, &owindows, owindows) {
1504         DLOG("moving in direction %s, px %ld\n", direction, move_px);
1505         if (con_is_floating(current->con)) {
1506             DLOG("floating move with %ld pixels\n", move_px);
1507             Rect newrect = current->con->parent->rect;
1508             if (strcmp(direction, "left") == 0) {
1509                 newrect.x -= move_px;
1510             } else if (strcmp(direction, "right") == 0) {
1511                 newrect.x += move_px;
1512             } else if (strcmp(direction, "up") == 0) {
1513                 newrect.y -= move_px;
1514             } else if (strcmp(direction, "down") == 0) {
1515                 newrect.y += move_px;
1516             }
1517             floating_reposition(current->con->parent, newrect);
1518         } else {
1519             tree_move(current->con, (strcmp(direction, "right") == 0 ? D_RIGHT : (strcmp(direction, "left") == 0 ? D_LEFT : (strcmp(direction, "up") == 0 ? D_UP : D_DOWN))));
1520             cmd_output->needs_tree_render = true;
1521         }
1522     }
1523
1524     /* the move command should not disturb focus */
1525     if (focused != initially_focused)
1526         con_activate(initially_focused);
1527
1528     // XXX: default reply for now, make this a better reply
1529     ysuccess(true);
1530 }
1531
1532 /*
1533  * Implementation of 'layout default|stacked|stacking|tabbed|splitv|splith'.
1534  *
1535  */
1536 void cmd_layout(I3_CMD, const char *layout_str) {
1537     HANDLE_EMPTY_MATCH;
1538
1539     layout_t layout;
1540     if (!layout_from_name(layout_str, &layout)) {
1541         ELOG("Unknown layout \"%s\", this is a mismatch between code and parser spec.\n", layout_str);
1542         return;
1543     }
1544
1545     DLOG("changing layout to %s (%d)\n", layout_str, layout);
1546
1547     owindow *current;
1548     TAILQ_FOREACH(current, &owindows, owindows) {
1549         if (con_is_docked(current->con)) {
1550             ELOG("cannot change layout of a docked container, skipping it.\n");
1551             continue;
1552         }
1553
1554         DLOG("matching: %p / %s\n", current->con, current->con->name);
1555         con_set_layout(current->con, layout);
1556     }
1557
1558     cmd_output->needs_tree_render = true;
1559     // XXX: default reply for now, make this a better reply
1560     ysuccess(true);
1561 }
1562
1563 /*
1564  * Implementation of 'layout toggle [all|split]'.
1565  *
1566  */
1567 void cmd_layout_toggle(I3_CMD, const char *toggle_mode) {
1568     owindow *current;
1569
1570     if (toggle_mode == NULL)
1571         toggle_mode = "default";
1572
1573     DLOG("toggling layout (mode = %s)\n", toggle_mode);
1574
1575     /* check if the match is empty, not if the result is empty */
1576     if (match_is_empty(current_match))
1577         con_toggle_layout(focused, toggle_mode);
1578     else {
1579         TAILQ_FOREACH(current, &owindows, owindows) {
1580             DLOG("matching: %p / %s\n", current->con, current->con->name);
1581             con_toggle_layout(current->con, toggle_mode);
1582         }
1583     }
1584
1585     cmd_output->needs_tree_render = true;
1586     // XXX: default reply for now, make this a better reply
1587     ysuccess(true);
1588 }
1589
1590 /*
1591  * Implementation of 'exit'.
1592  *
1593  */
1594 void cmd_exit(I3_CMD) {
1595     LOG("Exiting due to user command.\n");
1596 #ifdef I3_ASAN_ENABLED
1597     __lsan_do_leak_check();
1598 #endif
1599     ipc_shutdown(SHUTDOWN_REASON_EXIT);
1600     unlink(config.ipc_socket_path);
1601     xcb_disconnect(conn);
1602     exit(0);
1603
1604     /* unreached */
1605 }
1606
1607 /*
1608  * Implementation of 'reload'.
1609  *
1610  */
1611 void cmd_reload(I3_CMD) {
1612     LOG("reloading\n");
1613     kill_nagbar(&config_error_nagbar_pid, false);
1614     kill_nagbar(&command_error_nagbar_pid, false);
1615     load_configuration(conn, NULL, true);
1616     x_set_i3_atoms();
1617     /* Send an IPC event just in case the ws names have changed */
1618     ipc_send_workspace_event("reload", NULL, NULL);
1619     /* Send an update event for the barconfig just in case it has changed */
1620     update_barconfig();
1621
1622     // XXX: default reply for now, make this a better reply
1623     ysuccess(true);
1624 }
1625
1626 /*
1627  * Implementation of 'restart'.
1628  *
1629  */
1630 void cmd_restart(I3_CMD) {
1631     LOG("restarting i3\n");
1632     ipc_shutdown(SHUTDOWN_REASON_RESTART);
1633     unlink(config.ipc_socket_path);
1634     /* We need to call this manually since atexit handlers don’t get called
1635      * when exec()ing */
1636     purge_zerobyte_logfile();
1637     i3_restart(false);
1638
1639     // XXX: default reply for now, make this a better reply
1640     ysuccess(true);
1641 }
1642
1643 /*
1644  * Implementation of 'open'.
1645  *
1646  */
1647 void cmd_open(I3_CMD) {
1648     LOG("opening new container\n");
1649     Con *con = tree_open_con(NULL, NULL);
1650     con->layout = L_SPLITH;
1651     con_activate(con);
1652
1653     y(map_open);
1654     ystr("success");
1655     y(bool, true);
1656     ystr("id");
1657     y(integer, (uintptr_t)con);
1658     y(map_close);
1659
1660     cmd_output->needs_tree_render = true;
1661 }
1662
1663 /*
1664  * Implementation of 'focus output <output>'.
1665  *
1666  */
1667 void cmd_focus_output(I3_CMD, const char *name) {
1668     owindow *current;
1669
1670     DLOG("name = %s\n", name);
1671
1672     HANDLE_EMPTY_MATCH;
1673
1674     /* get the output */
1675     Output *current_output = NULL;
1676     Output *output;
1677
1678     TAILQ_FOREACH(current, &owindows, owindows)
1679     current_output = get_output_for_con(current->con);
1680     assert(current_output != NULL);
1681
1682     output = get_output_from_string(current_output, name);
1683
1684     if (!output) {
1685         LOG("No such output found.\n");
1686         ysuccess(false);
1687         return;
1688     }
1689
1690     /* get visible workspace on output */
1691     Con *ws = NULL;
1692     GREP_FIRST(ws, output_get_content(output->con), workspace_is_visible(child));
1693     if (!ws) {
1694         ysuccess(false);
1695         return;
1696     }
1697
1698     workspace_show(ws);
1699
1700     cmd_output->needs_tree_render = true;
1701     // XXX: default reply for now, make this a better reply
1702     ysuccess(true);
1703 }
1704
1705 /*
1706  * Implementation of 'move [window|container] [to] [absolute] position <px> [px] <px> [px]
1707  *
1708  */
1709 void cmd_move_window_to_position(I3_CMD, const char *method, long x, long y) {
1710     bool has_error = false;
1711
1712     owindow *current;
1713     HANDLE_EMPTY_MATCH;
1714
1715     TAILQ_FOREACH(current, &owindows, owindows) {
1716         if (!con_is_floating(current->con)) {
1717             ELOG("Cannot change position. The window/container is not floating\n");
1718
1719             if (!has_error) {
1720                 yerror("Cannot change position of a window/container because it is not floating.");
1721                 has_error = true;
1722             }
1723
1724             continue;
1725         }
1726
1727         if (strcmp(method, "absolute") == 0) {
1728             current->con->parent->rect.x = x;
1729             current->con->parent->rect.y = y;
1730
1731             DLOG("moving to absolute position %ld %ld\n", x, y);
1732             floating_maybe_reassign_ws(current->con->parent);
1733             cmd_output->needs_tree_render = true;
1734         }
1735
1736         if (strcmp(method, "position") == 0) {
1737             Rect newrect = current->con->parent->rect;
1738
1739             DLOG("moving to position %ld %ld\n", x, y);
1740             newrect.x = x;
1741             newrect.y = y;
1742
1743             floating_reposition(current->con->parent, newrect);
1744         }
1745     }
1746
1747     // XXX: default reply for now, make this a better reply
1748     if (!has_error)
1749         ysuccess(true);
1750 }
1751
1752 /*
1753  * Implementation of 'move [window|container] [to] [absolute] position center
1754  *
1755  */
1756 void cmd_move_window_to_center(I3_CMD, const char *method) {
1757     bool has_error = false;
1758     HANDLE_EMPTY_MATCH;
1759
1760     owindow *current;
1761     TAILQ_FOREACH(current, &owindows, owindows) {
1762         Con *floating_con = con_inside_floating(current->con);
1763         if (floating_con == NULL) {
1764             ELOG("con %p / %s is not floating, cannot move it to the center.\n",
1765                  current->con, current->con->name);
1766
1767             if (!has_error) {
1768                 yerror("Cannot change position of a window/container because it is not floating.");
1769                 has_error = true;
1770             }
1771
1772             continue;
1773         }
1774
1775         if (strcmp(method, "absolute") == 0) {
1776             DLOG("moving to absolute center\n");
1777             floating_center(floating_con, croot->rect);
1778
1779             floating_maybe_reassign_ws(floating_con);
1780             cmd_output->needs_tree_render = true;
1781         }
1782
1783         if (strcmp(method, "position") == 0) {
1784             DLOG("moving to center\n");
1785             floating_center(floating_con, con_get_workspace(floating_con)->rect);
1786
1787             cmd_output->needs_tree_render = true;
1788         }
1789     }
1790
1791     // XXX: default reply for now, make this a better reply
1792     if (!has_error)
1793         ysuccess(true);
1794 }
1795
1796 /*
1797  * Implementation of 'move [window|container] [to] position mouse'
1798  *
1799  */
1800 void cmd_move_window_to_mouse(I3_CMD) {
1801     HANDLE_EMPTY_MATCH;
1802
1803     owindow *current;
1804     TAILQ_FOREACH(current, &owindows, owindows) {
1805         Con *floating_con = con_inside_floating(current->con);
1806         if (floating_con == NULL) {
1807             DLOG("con %p / %s is not floating, cannot move it to the mouse position.\n",
1808                  current->con, current->con->name);
1809             continue;
1810         }
1811
1812         DLOG("moving floating container %p / %s to cursor position\n", floating_con, floating_con->name);
1813         floating_move_to_pointer(floating_con);
1814     }
1815
1816     cmd_output->needs_tree_render = true;
1817     ysuccess(true);
1818 }
1819
1820 /*
1821  * Implementation of 'move scratchpad'.
1822  *
1823  */
1824 void cmd_move_scratchpad(I3_CMD) {
1825     DLOG("should move window to scratchpad\n");
1826     owindow *current;
1827
1828     HANDLE_EMPTY_MATCH;
1829
1830     TAILQ_FOREACH(current, &owindows, owindows) {
1831         DLOG("matching: %p / %s\n", current->con, current->con->name);
1832         scratchpad_move(current->con);
1833     }
1834
1835     cmd_output->needs_tree_render = true;
1836     // XXX: default reply for now, make this a better reply
1837     ysuccess(true);
1838 }
1839
1840 /*
1841  * Implementation of 'scratchpad show'.
1842  *
1843  */
1844 void cmd_scratchpad_show(I3_CMD) {
1845     DLOG("should show scratchpad window\n");
1846     owindow *current;
1847
1848     if (match_is_empty(current_match)) {
1849         scratchpad_show(NULL);
1850     } else {
1851         TAILQ_FOREACH(current, &owindows, owindows) {
1852             DLOG("matching: %p / %s\n", current->con, current->con->name);
1853             scratchpad_show(current->con);
1854         }
1855     }
1856
1857     cmd_output->needs_tree_render = true;
1858     // XXX: default reply for now, make this a better reply
1859     ysuccess(true);
1860 }
1861
1862 /*
1863  * Implementation of 'swap [container] [with] id|con_id|mark <arg>'.
1864  *
1865  */
1866 void cmd_swap(I3_CMD, const char *mode, const char *arg) {
1867     HANDLE_EMPTY_MATCH;
1868
1869     owindow *match = TAILQ_FIRST(&owindows);
1870     if (match == NULL) {
1871         DLOG("No match found for swapping.\n");
1872         return;
1873     }
1874
1875     Con *con;
1876     if (strcmp(mode, "id") == 0) {
1877         long target;
1878         if (!parse_long(arg, &target, 0)) {
1879             yerror("Failed to parse %s into a window id.\n", arg);
1880             return;
1881         }
1882
1883         con = con_by_window_id(target);
1884     } else if (strcmp(mode, "con_id") == 0) {
1885         long target;
1886         if (!parse_long(arg, &target, 0)) {
1887             yerror("Failed to parse %s into a container id.\n", arg);
1888             return;
1889         }
1890
1891         con = con_by_con_id(target);
1892     } else if (strcmp(mode, "mark") == 0) {
1893         con = con_by_mark(arg);
1894     } else {
1895         yerror("Unhandled swap mode \"%s\". This is a bug.\n", mode);
1896         return;
1897     }
1898
1899     if (con == NULL) {
1900         yerror("Could not find container for %s = %s\n", mode, arg);
1901         return;
1902     }
1903
1904     if (match != TAILQ_LAST(&owindows, owindows_head)) {
1905         DLOG("More than one container matched the swap command, only using the first one.");
1906     }
1907
1908     if (match->con == NULL) {
1909         DLOG("Match %p has no container.\n", match);
1910         ysuccess(false);
1911         return;
1912     }
1913
1914     DLOG("Swapping %p with %p.\n", match->con, con);
1915     bool result = con_swap(match->con, con);
1916
1917     cmd_output->needs_tree_render = true;
1918     ysuccess(result);
1919 }
1920
1921 /*
1922  * Implementation of 'title_format <format>'
1923  *
1924  */
1925 void cmd_title_format(I3_CMD, const char *format) {
1926     DLOG("setting title_format to \"%s\"\n", format);
1927     HANDLE_EMPTY_MATCH;
1928
1929     owindow *current;
1930     TAILQ_FOREACH(current, &owindows, owindows) {
1931         DLOG("setting title_format for %p / %s\n", current->con, current->con->name);
1932         FREE(current->con->title_format);
1933
1934         /* If we only display the title without anything else, we can skip the parsing step,
1935          * so we remove the title format altogether. */
1936         if (strcasecmp(format, "%title") != 0) {
1937             current->con->title_format = sstrdup(format);
1938
1939             if (current->con->window != NULL) {
1940                 i3String *formatted_title = con_parse_title_format(current->con);
1941                 ewmh_update_visible_name(current->con->window->id, i3string_as_utf8(formatted_title));
1942                 I3STRING_FREE(formatted_title);
1943             }
1944         } else {
1945             if (current->con->window != NULL) {
1946                 /* We can remove _NET_WM_VISIBLE_NAME since we don't display a custom title. */
1947                 ewmh_update_visible_name(current->con->window->id, NULL);
1948             }
1949         }
1950
1951         if (current->con->window != NULL) {
1952             /* Make sure the window title is redrawn immediately. */
1953             current->con->window->name_x_changed = true;
1954         } else {
1955             /* For windowless containers we also need to force the redrawing. */
1956             FREE(current->con->deco_render_params);
1957         }
1958     }
1959
1960     cmd_output->needs_tree_render = true;
1961     ysuccess(true);
1962 }
1963
1964 /*
1965  * Implementation of 'rename workspace [<name>] to <name>'
1966  *
1967  */
1968 void cmd_rename_workspace(I3_CMD, const char *old_name, const char *new_name) {
1969     if (strncasecmp(new_name, "__", strlen("__")) == 0) {
1970         LOG("Cannot rename workspace to \"%s\": names starting with __ are i3-internal.\n", new_name);
1971         ysuccess(false);
1972         return;
1973     }
1974     if (old_name) {
1975         LOG("Renaming workspace \"%s\" to \"%s\"\n", old_name, new_name);
1976     } else {
1977         LOG("Renaming current workspace to \"%s\"\n", new_name);
1978     }
1979
1980     Con *output, *workspace = NULL;
1981     if (old_name) {
1982         TAILQ_FOREACH(output, &(croot->nodes_head), nodes)
1983         GREP_FIRST(workspace, output_get_content(output),
1984                    !strcasecmp(child->name, old_name));
1985     } else {
1986         workspace = con_get_workspace(focused);
1987         old_name = workspace->name;
1988     }
1989
1990     if (!workspace) {
1991         yerror("Old workspace \"%s\" not found", old_name);
1992         return;
1993     }
1994
1995     Con *check_dest = NULL;
1996     TAILQ_FOREACH(output, &(croot->nodes_head), nodes)
1997     GREP_FIRST(check_dest, output_get_content(output),
1998                !strcasecmp(child->name, new_name));
1999
2000     /* If check_dest == workspace, the user might be changing the case of the
2001      * workspace, or it might just be a no-op. */
2002     if (check_dest != NULL && check_dest != workspace) {
2003         yerror("New workspace \"%s\" already exists", new_name);
2004         return;
2005     }
2006
2007     /* Change the name and try to parse it as a number. */
2008     /* old_name might refer to workspace->name, so copy it before free()ing */
2009     char *old_name_copy = sstrdup(old_name);
2010     FREE(workspace->name);
2011     workspace->name = sstrdup(new_name);
2012
2013     workspace->num = ws_name_to_number(new_name);
2014     LOG("num = %d\n", workspace->num);
2015
2016     /* By re-attaching, the sort order will be correct afterwards. */
2017     Con *previously_focused = focused;
2018     Con *parent = workspace->parent;
2019     con_detach(workspace);
2020     con_attach(workspace, parent, false);
2021
2022     /* Move the workspace to the correct output if it has an assignment */
2023     struct Workspace_Assignment *assignment = NULL;
2024     TAILQ_FOREACH(assignment, &ws_assignments, ws_assignments) {
2025         if (assignment->output == NULL)
2026             continue;
2027         if (strcmp(assignment->name, workspace->name) != 0 && (!name_is_digits(assignment->name) || ws_name_to_number(assignment->name) != workspace->num)) {
2028             continue;
2029         }
2030
2031         workspace_move_to_output(workspace, assignment->output);
2032
2033         if (previously_focused)
2034             workspace_show(con_get_workspace(previously_focused));
2035
2036         break;
2037     }
2038
2039     /* Restore the previous focus since con_attach messes with the focus. */
2040     con_activate(previously_focused);
2041
2042     cmd_output->needs_tree_render = true;
2043     ysuccess(true);
2044
2045     ipc_send_workspace_event("rename", workspace, NULL);
2046     ewmh_update_desktop_names();
2047     ewmh_update_desktop_viewport();
2048     ewmh_update_current_desktop();
2049
2050     startup_sequence_rename_workspace(old_name_copy, new_name);
2051     free(old_name_copy);
2052 }
2053
2054 /*
2055  * Implementation of 'bar mode dock|hide|invisible|toggle [<bar_id>]'
2056  *
2057  */
2058 bool cmd_bar_mode(const char *bar_mode, const char *bar_id) {
2059     int mode = M_DOCK;
2060     bool toggle = false;
2061     if (strcmp(bar_mode, "dock") == 0)
2062         mode = M_DOCK;
2063     else if (strcmp(bar_mode, "hide") == 0)
2064         mode = M_HIDE;
2065     else if (strcmp(bar_mode, "invisible") == 0)
2066         mode = M_INVISIBLE;
2067     else if (strcmp(bar_mode, "toggle") == 0)
2068         toggle = true;
2069     else {
2070         ELOG("Unknown bar mode \"%s\", this is a mismatch between code and parser spec.\n", bar_mode);
2071         return false;
2072     }
2073
2074     bool changed_sth = false;
2075     Barconfig *current = NULL;
2076     TAILQ_FOREACH(current, &barconfigs, configs) {
2077         if (bar_id && strcmp(current->id, bar_id) != 0)
2078             continue;
2079
2080         if (toggle)
2081             mode = (current->mode + 1) % 2;
2082
2083         DLOG("Changing bar mode of bar_id '%s' to '%s (%d)'\n", current->id, bar_mode, mode);
2084         current->mode = mode;
2085         changed_sth = true;
2086
2087         if (bar_id)
2088             break;
2089     }
2090
2091     if (bar_id && !changed_sth) {
2092         DLOG("Changing bar mode of bar_id %s failed, bar_id not found.\n", bar_id);
2093         return false;
2094     }
2095
2096     return true;
2097 }
2098
2099 /*
2100  * Implementation of 'bar hidden_state hide|show|toggle [<bar_id>]'
2101  *
2102  */
2103 bool cmd_bar_hidden_state(const char *bar_hidden_state, const char *bar_id) {
2104     int hidden_state = S_SHOW;
2105     bool toggle = false;
2106     if (strcmp(bar_hidden_state, "hide") == 0)
2107         hidden_state = S_HIDE;
2108     else if (strcmp(bar_hidden_state, "show") == 0)
2109         hidden_state = S_SHOW;
2110     else if (strcmp(bar_hidden_state, "toggle") == 0)
2111         toggle = true;
2112     else {
2113         ELOG("Unknown bar state \"%s\", this is a mismatch between code and parser spec.\n", bar_hidden_state);
2114         return false;
2115     }
2116
2117     bool changed_sth = false;
2118     Barconfig *current = NULL;
2119     TAILQ_FOREACH(current, &barconfigs, configs) {
2120         if (bar_id && strcmp(current->id, bar_id) != 0)
2121             continue;
2122
2123         if (toggle)
2124             hidden_state = (current->hidden_state + 1) % 2;
2125
2126         DLOG("Changing bar hidden_state of bar_id '%s' to '%s (%d)'\n", current->id, bar_hidden_state, hidden_state);
2127         current->hidden_state = hidden_state;
2128         changed_sth = true;
2129
2130         if (bar_id)
2131             break;
2132     }
2133
2134     if (bar_id && !changed_sth) {
2135         DLOG("Changing bar hidden_state of bar_id %s failed, bar_id not found.\n", bar_id);
2136         return false;
2137     }
2138
2139     return true;
2140 }
2141
2142 /*
2143  * Implementation of 'bar (hidden_state hide|show|toggle)|(mode dock|hide|invisible|toggle) [<bar_id>]'
2144  *
2145  */
2146 void cmd_bar(I3_CMD, const char *bar_type, const char *bar_value, const char *bar_id) {
2147     bool ret;
2148     if (strcmp(bar_type, "mode") == 0)
2149         ret = cmd_bar_mode(bar_value, bar_id);
2150     else if (strcmp(bar_type, "hidden_state") == 0)
2151         ret = cmd_bar_hidden_state(bar_value, bar_id);
2152     else {
2153         ELOG("Unknown bar option type \"%s\", this is a mismatch between code and parser spec.\n", bar_type);
2154         ret = false;
2155     }
2156
2157     ysuccess(ret);
2158     if (!ret)
2159         return;
2160
2161     update_barconfig();
2162 }
2163
2164 /*
2165  * Implementation of 'shmlog <size>|toggle|on|off'
2166  *
2167  */
2168 void cmd_shmlog(I3_CMD, const char *argument) {
2169     if (!strcmp(argument, "toggle"))
2170         /* Toggle shm log, if size is not 0. If it is 0, set it to default. */
2171         shmlog_size = shmlog_size ? -shmlog_size : default_shmlog_size;
2172     else if (!strcmp(argument, "on"))
2173         shmlog_size = default_shmlog_size;
2174     else if (!strcmp(argument, "off"))
2175         shmlog_size = 0;
2176     else {
2177         /* If shm logging now, restart logging with the new size. */
2178         if (shmlog_size > 0) {
2179             shmlog_size = 0;
2180             LOG("Restarting shm logging...\n");
2181             init_logging();
2182         }
2183         shmlog_size = atoi(argument);
2184         /* Make a weakly attempt at ensuring the argument is valid. */
2185         if (shmlog_size <= 0)
2186             shmlog_size = default_shmlog_size;
2187     }
2188     LOG("%s shm logging\n", shmlog_size > 0 ? "Enabling" : "Disabling");
2189     init_logging();
2190     update_shmlog_atom();
2191     // XXX: default reply for now, make this a better reply
2192     ysuccess(true);
2193 }
2194
2195 /*
2196  * Implementation of 'debuglog toggle|on|off'
2197  *
2198  */
2199 void cmd_debuglog(I3_CMD, const char *argument) {
2200     bool logging = get_debug_logging();
2201     if (!strcmp(argument, "toggle")) {
2202         LOG("%s debug logging\n", logging ? "Disabling" : "Enabling");
2203         set_debug_logging(!logging);
2204     } else if (!strcmp(argument, "on") && !logging) {
2205         LOG("Enabling debug logging\n");
2206         set_debug_logging(true);
2207     } else if (!strcmp(argument, "off") && logging) {
2208         LOG("Disabling debug logging\n");
2209         set_debug_logging(false);
2210     }
2211     // XXX: default reply for now, make this a better reply
2212     ysuccess(true);
2213 }