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