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