]> git.sur5r.net Git - i3/i3/blob - src/commands.c
778b4a1a9da95b43c71f3a936515d65f0c852d6c
[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         if (current_output == NULL) {
1118             yerror("Cannot get current output. This is a bug in i3.");
1119             return;
1120         }
1121
1122         Output *target_output = get_output_from_string(current_output, name);
1123         if (!target_output) {
1124             yerror("Could not get output from string \"%s\"", name);
1125             return;
1126         }
1127
1128         bool success = workspace_move_to_output(ws, target_output);
1129         if (!success) {
1130             yerror("Failed to move workspace to output.");
1131             return;
1132         }
1133     }
1134
1135     cmd_output->needs_tree_render = true;
1136     ysuccess(true);
1137 }
1138
1139 /*
1140  * Implementation of 'split v|h|t|vertical|horizontal|toggle'.
1141  *
1142  */
1143 void cmd_split(I3_CMD, const char *direction) {
1144     HANDLE_EMPTY_MATCH;
1145
1146     owindow *current;
1147     LOG("splitting in direction %c\n", direction[0]);
1148     TAILQ_FOREACH(current, &owindows, owindows) {
1149         if (con_is_docked(current->con)) {
1150             ELOG("Cannot split a docked container, skipping.\n");
1151             continue;
1152         }
1153
1154         DLOG("matching: %p / %s\n", current->con, current->con->name);
1155         if (direction[0] == 't') {
1156             layout_t current_layout;
1157             if (current->con->type == CT_WORKSPACE) {
1158                 current_layout = current->con->layout;
1159             } else {
1160                 current_layout = current->con->parent->layout;
1161             }
1162             /* toggling split orientation */
1163             if (current_layout == L_SPLITH) {
1164                 tree_split(current->con, VERT);
1165             } else {
1166                 tree_split(current->con, HORIZ);
1167             }
1168         } else {
1169             tree_split(current->con, (direction[0] == 'v' ? VERT : HORIZ));
1170         }
1171     }
1172
1173     cmd_output->needs_tree_render = true;
1174     // XXX: default reply for now, make this a better reply
1175     ysuccess(true);
1176 }
1177
1178 /*
1179  * Implementation of 'kill [window|client]'.
1180  *
1181  */
1182 void cmd_kill(I3_CMD, const char *kill_mode_str) {
1183     if (kill_mode_str == NULL)
1184         kill_mode_str = "window";
1185
1186     DLOG("kill_mode=%s\n", kill_mode_str);
1187
1188     int kill_mode;
1189     if (strcmp(kill_mode_str, "window") == 0)
1190         kill_mode = KILL_WINDOW;
1191     else if (strcmp(kill_mode_str, "client") == 0)
1192         kill_mode = KILL_CLIENT;
1193     else {
1194         yerror("BUG: called with kill_mode=%s", kill_mode_str);
1195         return;
1196     }
1197
1198     HANDLE_EMPTY_MATCH;
1199
1200     owindow *current;
1201     TAILQ_FOREACH(current, &owindows, owindows) {
1202         con_close(current->con, kill_mode);
1203     }
1204
1205     cmd_output->needs_tree_render = true;
1206     // XXX: default reply for now, make this a better reply
1207     ysuccess(true);
1208 }
1209
1210 /*
1211  * Implementation of 'exec [--no-startup-id] <command>'.
1212  *
1213  */
1214 void cmd_exec(I3_CMD, const char *nosn, const char *command) {
1215     bool no_startup_id = (nosn != NULL);
1216
1217     DLOG("should execute %s, no_startup_id = %d\n", command, no_startup_id);
1218     start_application(command, no_startup_id);
1219
1220     ysuccess(true);
1221 }
1222
1223 /*
1224  * Implementation of 'focus left|right|up|down'.
1225  *
1226  */
1227 void cmd_focus_direction(I3_CMD, const char *direction) {
1228     switch (parse_direction(direction)) {
1229         case D_LEFT:
1230             tree_next('p', HORIZ);
1231             break;
1232         case D_RIGHT:
1233             tree_next('n', HORIZ);
1234             break;
1235         case D_UP:
1236             tree_next('p', VERT);
1237             break;
1238         case D_DOWN:
1239             tree_next('n', VERT);
1240             break;
1241     }
1242
1243     cmd_output->needs_tree_render = true;
1244     // XXX: default reply for now, make this a better reply
1245     ysuccess(true);
1246 }
1247
1248 /*
1249  * Focus a container and disable any other fullscreen container not permitting the focus.
1250  *
1251  */
1252 static void cmd_focus_force_focus(Con *con) {
1253     /* Disable fullscreen container in workspace with container to be focused. */
1254     Con *ws = con_get_workspace(con);
1255     Con *fullscreen_on_ws = con_get_fullscreen_covering_ws(ws);
1256     if (fullscreen_on_ws && fullscreen_on_ws != con && !con_has_parent(con, fullscreen_on_ws)) {
1257         con_disable_fullscreen(fullscreen_on_ws);
1258     }
1259     con_activate(con);
1260 }
1261
1262 /*
1263  * Implementation of 'focus tiling|floating|mode_toggle'.
1264  *
1265  */
1266 void cmd_focus_window_mode(I3_CMD, const char *window_mode) {
1267     DLOG("window_mode = %s\n", window_mode);
1268
1269     bool to_floating = false;
1270     if (strcmp(window_mode, "mode_toggle") == 0) {
1271         to_floating = !con_inside_floating(focused);
1272     } else if (strcmp(window_mode, "floating") == 0) {
1273         to_floating = true;
1274     } else if (strcmp(window_mode, "tiling") == 0) {
1275         to_floating = false;
1276     }
1277
1278     Con *ws = con_get_workspace(focused);
1279     Con *current;
1280     bool success = false;
1281     TAILQ_FOREACH(current, &(ws->focus_head), focused) {
1282         if ((to_floating && current->type != CT_FLOATING_CON) ||
1283             (!to_floating && current->type == CT_FLOATING_CON))
1284             continue;
1285
1286         cmd_focus_force_focus(con_descend_focused(current));
1287         success = true;
1288         break;
1289     }
1290
1291     if (success) {
1292         cmd_output->needs_tree_render = true;
1293         ysuccess(true);
1294     } else {
1295         yerror("Failed to find a %s container in workspace.", to_floating ? "floating" : "tiling");
1296     }
1297 }
1298
1299 /*
1300  * Implementation of 'focus parent|child'.
1301  *
1302  */
1303 void cmd_focus_level(I3_CMD, const char *level) {
1304     DLOG("level = %s\n", level);
1305     bool success = false;
1306
1307     /* Focusing the parent can only be allowed if the newly
1308      * focused container won't escape the fullscreen container. */
1309     if (strcmp(level, "parent") == 0) {
1310         if (focused && focused->parent) {
1311             if (con_fullscreen_permits_focusing(focused->parent))
1312                 success = level_up();
1313             else
1314                 ELOG("'focus parent': Currently in fullscreen, not going up\n");
1315         }
1316     }
1317
1318     /* Focusing a child should always be allowed. */
1319     else
1320         success = level_down();
1321
1322     cmd_output->needs_tree_render = success;
1323     // XXX: default reply for now, make this a better reply
1324     ysuccess(success);
1325 }
1326
1327 /*
1328  * Implementation of 'focus'.
1329  *
1330  */
1331 void cmd_focus(I3_CMD) {
1332     DLOG("current_match = %p\n", current_match);
1333
1334     if (match_is_empty(current_match)) {
1335         ELOG("You have to specify which window/container should be focused.\n");
1336         ELOG("Example: [class=\"urxvt\" title=\"irssi\"] focus\n");
1337
1338         yerror("You have to specify which window/container should be focused");
1339
1340         return;
1341     }
1342
1343     Con *__i3_scratch = workspace_get("__i3_scratch", NULL);
1344     int count = 0;
1345     owindow *current;
1346     TAILQ_FOREACH(current, &owindows, owindows) {
1347         Con *ws = con_get_workspace(current->con);
1348         /* If no workspace could be found, this was a dock window.
1349          * Just skip it, you cannot focus dock windows. */
1350         if (!ws)
1351             continue;
1352
1353         /* In case this is a scratchpad window, call scratchpad_show(). */
1354         if (ws == __i3_scratch) {
1355             scratchpad_show(current->con);
1356             count++;
1357             /* While for the normal focus case we can change focus multiple
1358              * times and only a single window ends up focused, we could show
1359              * multiple scratchpad windows. So, rather break here. */
1360             break;
1361         }
1362
1363         /* If the container is not on the current workspace,
1364          * workspace_show() will switch to a different workspace and (if
1365          * enabled) trigger a mouse pointer warp to the currently focused
1366          * container (!) on the target workspace.
1367          *
1368          * Therefore, before calling workspace_show(), we make sure that
1369          * 'current' will be focused on the workspace. However, we cannot
1370          * just con_focus(current) because then the pointer will not be
1371          * warped at all (the code thinks we are already there).
1372          *
1373          * So we focus 'current' to make it the currently focused window of
1374          * the target workspace, then revert focus. */
1375         Con *currently_focused = focused;
1376         cmd_focus_force_focus(current->con);
1377         con_activate(currently_focused);
1378
1379         /* Now switch to the workspace, then focus */
1380         workspace_show(ws);
1381         LOG("focusing %p / %s\n", current->con, current->con->name);
1382         con_activate(current->con);
1383         count++;
1384     }
1385
1386     if (count > 1)
1387         LOG("WARNING: Your criteria for the focus command matches %d containers, "
1388             "while only exactly one container can be focused at a time.\n",
1389             count);
1390
1391     cmd_output->needs_tree_render = true;
1392     ysuccess(count > 0);
1393 }
1394
1395 /*
1396  * Implementation of 'fullscreen enable|toggle [global]' and
1397  *                   'fullscreen disable'
1398  *
1399  */
1400 void cmd_fullscreen(I3_CMD, const char *action, const char *fullscreen_mode) {
1401     fullscreen_mode_t mode = strcmp(fullscreen_mode, "global") == 0 ? CF_GLOBAL : CF_OUTPUT;
1402     DLOG("%s fullscreen, mode = %s\n", action, fullscreen_mode);
1403     owindow *current;
1404
1405     HANDLE_EMPTY_MATCH;
1406
1407     TAILQ_FOREACH(current, &owindows, owindows) {
1408         DLOG("matching: %p / %s\n", current->con, current->con->name);
1409         if (strcmp(action, "toggle") == 0) {
1410             con_toggle_fullscreen(current->con, mode);
1411         } else if (strcmp(action, "enable") == 0) {
1412             con_enable_fullscreen(current->con, mode);
1413         } else if (strcmp(action, "disable") == 0) {
1414             con_disable_fullscreen(current->con);
1415         }
1416     }
1417
1418     cmd_output->needs_tree_render = true;
1419     // XXX: default reply for now, make this a better reply
1420     ysuccess(true);
1421 }
1422
1423 /*
1424  * Implementation of 'sticky enable|disable|toggle'.
1425  *
1426  */
1427 void cmd_sticky(I3_CMD, const char *action) {
1428     DLOG("%s sticky on window\n", action);
1429     HANDLE_EMPTY_MATCH;
1430
1431     owindow *current;
1432     TAILQ_FOREACH(current, &owindows, owindows) {
1433         if (current->con->window == NULL) {
1434             ELOG("only containers holding a window can be made sticky, skipping con = %p\n", current->con);
1435             continue;
1436         }
1437         DLOG("setting sticky for container = %p / %s\n", current->con, current->con->name);
1438
1439         bool sticky = false;
1440         if (strcmp(action, "enable") == 0)
1441             sticky = true;
1442         else if (strcmp(action, "disable") == 0)
1443             sticky = false;
1444         else if (strcmp(action, "toggle") == 0)
1445             sticky = !current->con->sticky;
1446
1447         current->con->sticky = sticky;
1448         ewmh_update_sticky(current->con->window->id, sticky);
1449     }
1450
1451     /* A window we made sticky might not be on a visible workspace right now, so we need to make
1452      * sure it gets pushed to the front now. */
1453     output_push_sticky_windows(focused);
1454
1455     ewmh_update_wm_desktop();
1456
1457     cmd_output->needs_tree_render = true;
1458     ysuccess(true);
1459 }
1460
1461 /*
1462  * Implementation of 'move <direction> [<pixels> [px]]'.
1463  *
1464  */
1465 void cmd_move_direction(I3_CMD, const char *direction_str, long move_px) {
1466     owindow *current;
1467     HANDLE_EMPTY_MATCH;
1468
1469     Con *initially_focused = focused;
1470     direction_t direction = parse_direction(direction_str);
1471
1472     TAILQ_FOREACH(current, &owindows, owindows) {
1473         DLOG("moving in direction %s, px %ld\n", direction_str, move_px);
1474         if (con_is_floating(current->con)) {
1475             DLOG("floating move with %ld pixels\n", move_px);
1476             Rect newrect = current->con->parent->rect;
1477
1478             switch (direction) {
1479                 case D_LEFT:
1480                     newrect.x -= move_px;
1481                     break;
1482                 case D_RIGHT:
1483                     newrect.x += move_px;
1484                     break;
1485                 case D_UP:
1486                     newrect.y -= move_px;
1487                     break;
1488                 case D_DOWN:
1489                     newrect.y += move_px;
1490                     break;
1491             }
1492
1493             floating_reposition(current->con->parent, newrect);
1494         } else {
1495             tree_move(current->con, direction);
1496             cmd_output->needs_tree_render = true;
1497         }
1498     }
1499
1500     /* the move command should not disturb focus */
1501     if (focused != initially_focused)
1502         con_activate(initially_focused);
1503
1504     // XXX: default reply for now, make this a better reply
1505     ysuccess(true);
1506 }
1507
1508 /*
1509  * Implementation of 'layout default|stacked|stacking|tabbed|splitv|splith'.
1510  *
1511  */
1512 void cmd_layout(I3_CMD, const char *layout_str) {
1513     HANDLE_EMPTY_MATCH;
1514
1515     layout_t layout;
1516     if (!layout_from_name(layout_str, &layout)) {
1517         yerror("Unknown layout \"%s\", this is a mismatch between code and parser spec.", layout_str);
1518         return;
1519     }
1520
1521     DLOG("changing layout to %s (%d)\n", layout_str, layout);
1522
1523     owindow *current;
1524     TAILQ_FOREACH(current, &owindows, owindows) {
1525         if (con_is_docked(current->con)) {
1526             ELOG("cannot change layout of a docked container, skipping it.\n");
1527             continue;
1528         }
1529
1530         DLOG("matching: %p / %s\n", current->con, current->con->name);
1531         con_set_layout(current->con, layout);
1532     }
1533
1534     cmd_output->needs_tree_render = true;
1535     // XXX: default reply for now, make this a better reply
1536     ysuccess(true);
1537 }
1538
1539 /*
1540  * Implementation of 'layout toggle [all|split]'.
1541  *
1542  */
1543 void cmd_layout_toggle(I3_CMD, const char *toggle_mode) {
1544     owindow *current;
1545
1546     if (toggle_mode == NULL)
1547         toggle_mode = "default";
1548
1549     DLOG("toggling layout (mode = %s)\n", toggle_mode);
1550
1551     /* check if the match is empty, not if the result is empty */
1552     if (match_is_empty(current_match))
1553         con_toggle_layout(focused, toggle_mode);
1554     else {
1555         TAILQ_FOREACH(current, &owindows, owindows) {
1556             DLOG("matching: %p / %s\n", current->con, current->con->name);
1557             con_toggle_layout(current->con, toggle_mode);
1558         }
1559     }
1560
1561     cmd_output->needs_tree_render = true;
1562     // XXX: default reply for now, make this a better reply
1563     ysuccess(true);
1564 }
1565
1566 /*
1567  * Implementation of 'exit'.
1568  *
1569  */
1570 void cmd_exit(I3_CMD) {
1571     LOG("Exiting due to user command.\n");
1572     exit(0);
1573
1574     /* unreached */
1575 }
1576
1577 /*
1578  * Implementation of 'reload'.
1579  *
1580  */
1581 void cmd_reload(I3_CMD) {
1582     LOG("reloading\n");
1583     kill_nagbar(&config_error_nagbar_pid, false);
1584     kill_nagbar(&command_error_nagbar_pid, false);
1585     load_configuration(conn, NULL, true);
1586     x_set_i3_atoms();
1587     /* Send an IPC event just in case the ws names have changed */
1588     ipc_send_workspace_event("reload", NULL, NULL);
1589     /* Send an update event for the barconfig just in case it has changed */
1590     update_barconfig();
1591
1592     // XXX: default reply for now, make this a better reply
1593     ysuccess(true);
1594 }
1595
1596 /*
1597  * Implementation of 'restart'.
1598  *
1599  */
1600 void cmd_restart(I3_CMD) {
1601     LOG("restarting i3\n");
1602     ipc_shutdown(SHUTDOWN_REASON_RESTART);
1603     unlink(config.ipc_socket_path);
1604     /* We need to call this manually since atexit handlers don’t get called
1605      * when exec()ing */
1606     purge_zerobyte_logfile();
1607     i3_restart(false);
1608
1609     // XXX: default reply for now, make this a better reply
1610     ysuccess(true);
1611 }
1612
1613 /*
1614  * Implementation of 'open'.
1615  *
1616  */
1617 void cmd_open(I3_CMD) {
1618     LOG("opening new container\n");
1619     Con *con = tree_open_con(NULL, NULL);
1620     con->layout = L_SPLITH;
1621     con_activate(con);
1622
1623     y(map_open);
1624     ystr("success");
1625     y(bool, true);
1626     ystr("id");
1627     y(integer, (uintptr_t)con);
1628     y(map_close);
1629
1630     cmd_output->needs_tree_render = true;
1631 }
1632
1633 /*
1634  * Implementation of 'focus output <output>'.
1635  *
1636  */
1637 void cmd_focus_output(I3_CMD, const char *name) {
1638     owindow *current;
1639
1640     DLOG("name = %s\n", name);
1641
1642     HANDLE_EMPTY_MATCH;
1643
1644     /* get the output */
1645     Output *current_output = NULL;
1646     Output *output;
1647
1648     TAILQ_FOREACH(current, &owindows, owindows)
1649     current_output = get_output_for_con(current->con);
1650     assert(current_output != NULL);
1651
1652     output = get_output_from_string(current_output, name);
1653
1654     if (!output) {
1655         yerror("No such output found.");
1656         return;
1657     }
1658
1659     /* get visible workspace on output */
1660     Con *ws = NULL;
1661     GREP_FIRST(ws, output_get_content(output->con), workspace_is_visible(child));
1662     if (!ws) {
1663         yerror("BUG: No workspace found on output.");
1664         return;
1665     }
1666
1667     workspace_show(ws);
1668
1669     cmd_output->needs_tree_render = true;
1670     // XXX: default reply for now, make this a better reply
1671     ysuccess(true);
1672 }
1673
1674 /*
1675  * Implementation of 'move [window|container] [to] [absolute] position <px> [px] <px> [px]
1676  *
1677  */
1678 void cmd_move_window_to_position(I3_CMD, long x, long y) {
1679     bool has_error = false;
1680
1681     owindow *current;
1682     HANDLE_EMPTY_MATCH;
1683
1684     TAILQ_FOREACH(current, &owindows, owindows) {
1685         if (!con_is_floating(current->con)) {
1686             ELOG("Cannot change position. The window/container is not floating\n");
1687
1688             if (!has_error) {
1689                 yerror("Cannot change position of a window/container because it is not floating.");
1690                 has_error = true;
1691             }
1692
1693             continue;
1694         }
1695
1696         Rect newrect = current->con->parent->rect;
1697
1698         DLOG("moving to position %ld %ld\n", x, y);
1699         newrect.x = x;
1700         newrect.y = y;
1701
1702         if (!floating_reposition(current->con->parent, newrect)) {
1703             yerror("Cannot move window/container out of bounds.");
1704             has_error = true;
1705         }
1706     }
1707
1708     if (!has_error)
1709         ysuccess(true);
1710 }
1711
1712 /*
1713  * Implementation of 'move [window|container] [to] [absolute] position center
1714  *
1715  */
1716 void cmd_move_window_to_center(I3_CMD, const char *method) {
1717     bool has_error = false;
1718     HANDLE_EMPTY_MATCH;
1719
1720     owindow *current;
1721     TAILQ_FOREACH(current, &owindows, owindows) {
1722         Con *floating_con = con_inside_floating(current->con);
1723         if (floating_con == NULL) {
1724             ELOG("con %p / %s is not floating, cannot move it to the center.\n",
1725                  current->con, current->con->name);
1726
1727             if (!has_error) {
1728                 yerror("Cannot change position of a window/container because it is not floating.");
1729                 has_error = true;
1730             }
1731
1732             continue;
1733         }
1734
1735         if (strcmp(method, "absolute") == 0) {
1736             DLOG("moving to absolute center\n");
1737             floating_center(floating_con, croot->rect);
1738
1739             floating_maybe_reassign_ws(floating_con);
1740             cmd_output->needs_tree_render = true;
1741         }
1742
1743         if (strcmp(method, "position") == 0) {
1744             DLOG("moving to center\n");
1745             floating_center(floating_con, con_get_workspace(floating_con)->rect);
1746
1747             cmd_output->needs_tree_render = true;
1748         }
1749     }
1750
1751     // XXX: default reply for now, make this a better reply
1752     if (!has_error)
1753         ysuccess(true);
1754 }
1755
1756 /*
1757  * Implementation of 'move [window|container] [to] position mouse'
1758  *
1759  */
1760 void cmd_move_window_to_mouse(I3_CMD) {
1761     HANDLE_EMPTY_MATCH;
1762
1763     owindow *current;
1764     TAILQ_FOREACH(current, &owindows, owindows) {
1765         Con *floating_con = con_inside_floating(current->con);
1766         if (floating_con == NULL) {
1767             DLOG("con %p / %s is not floating, cannot move it to the mouse position.\n",
1768                  current->con, current->con->name);
1769             continue;
1770         }
1771
1772         DLOG("moving floating container %p / %s to cursor position\n", floating_con, floating_con->name);
1773         floating_move_to_pointer(floating_con);
1774     }
1775
1776     cmd_output->needs_tree_render = true;
1777     ysuccess(true);
1778 }
1779
1780 /*
1781  * Implementation of 'move scratchpad'.
1782  *
1783  */
1784 void cmd_move_scratchpad(I3_CMD) {
1785     DLOG("should move window to scratchpad\n");
1786     owindow *current;
1787
1788     HANDLE_EMPTY_MATCH;
1789
1790     TAILQ_FOREACH(current, &owindows, owindows) {
1791         DLOG("matching: %p / %s\n", current->con, current->con->name);
1792         scratchpad_move(current->con);
1793     }
1794
1795     cmd_output->needs_tree_render = true;
1796     // XXX: default reply for now, make this a better reply
1797     ysuccess(true);
1798 }
1799
1800 /*
1801  * Implementation of 'scratchpad show'.
1802  *
1803  */
1804 void cmd_scratchpad_show(I3_CMD) {
1805     DLOG("should show scratchpad window\n");
1806     owindow *current;
1807     bool result = false;
1808
1809     if (match_is_empty(current_match)) {
1810         result = scratchpad_show(NULL);
1811     } else {
1812         TAILQ_FOREACH(current, &owindows, owindows) {
1813             DLOG("matching: %p / %s\n", current->con, current->con->name);
1814             result |= scratchpad_show(current->con);
1815         }
1816     }
1817
1818     cmd_output->needs_tree_render = true;
1819
1820     ysuccess(result);
1821 }
1822
1823 /*
1824  * Implementation of 'swap [container] [with] id|con_id|mark <arg>'.
1825  *
1826  */
1827 void cmd_swap(I3_CMD, const char *mode, const char *arg) {
1828     HANDLE_EMPTY_MATCH;
1829
1830     owindow *match = TAILQ_FIRST(&owindows);
1831     if (match == NULL) {
1832         yerror("No match found for swapping.");
1833         return;
1834     }
1835     if (match->con == NULL) {
1836         yerror("Match %p has no container.", match);
1837         return;
1838     }
1839
1840     Con *con;
1841     if (strcmp(mode, "id") == 0) {
1842         long target;
1843         if (!parse_long(arg, &target, 0)) {
1844             yerror("Failed to parse %s into a window id.", arg);
1845             return;
1846         }
1847
1848         con = con_by_window_id(target);
1849     } else if (strcmp(mode, "con_id") == 0) {
1850         long target;
1851         if (!parse_long(arg, &target, 0)) {
1852             yerror("Failed to parse %s into a container id.", arg);
1853             return;
1854         }
1855
1856         con = con_by_con_id(target);
1857     } else if (strcmp(mode, "mark") == 0) {
1858         con = con_by_mark(arg);
1859     } else {
1860         yerror("Unhandled swap mode \"%s\". This is a bug.", mode);
1861         return;
1862     }
1863
1864     if (con == NULL) {
1865         yerror("Could not find container for %s = %s", mode, arg);
1866         return;
1867     }
1868
1869     if (match != TAILQ_LAST(&owindows, owindows_head)) {
1870         LOG("More than one container matched the swap command, only using the first one.");
1871     }
1872
1873     DLOG("Swapping %p with %p.\n", match->con, con);
1874     bool result = con_swap(match->con, con);
1875
1876     cmd_output->needs_tree_render = true;
1877     // XXX: default reply for now, make this a better reply
1878     ysuccess(result);
1879 }
1880
1881 /*
1882  * Implementation of 'title_format <format>'
1883  *
1884  */
1885 void cmd_title_format(I3_CMD, const char *format) {
1886     DLOG("setting title_format to \"%s\"\n", format);
1887     HANDLE_EMPTY_MATCH;
1888
1889     owindow *current;
1890     TAILQ_FOREACH(current, &owindows, owindows) {
1891         DLOG("setting title_format for %p / %s\n", current->con, current->con->name);
1892         FREE(current->con->title_format);
1893
1894         /* If we only display the title without anything else, we can skip the parsing step,
1895          * so we remove the title format altogether. */
1896         if (strcasecmp(format, "%title") != 0) {
1897             current->con->title_format = sstrdup(format);
1898
1899             if (current->con->window != NULL) {
1900                 i3String *formatted_title = con_parse_title_format(current->con);
1901                 ewmh_update_visible_name(current->con->window->id, i3string_as_utf8(formatted_title));
1902                 I3STRING_FREE(formatted_title);
1903             }
1904         } else {
1905             if (current->con->window != NULL) {
1906                 /* We can remove _NET_WM_VISIBLE_NAME since we don't display a custom title. */
1907                 ewmh_update_visible_name(current->con->window->id, NULL);
1908             }
1909         }
1910
1911         if (current->con->window != NULL) {
1912             /* Make sure the window title is redrawn immediately. */
1913             current->con->window->name_x_changed = true;
1914         } else {
1915             /* For windowless containers we also need to force the redrawing. */
1916             FREE(current->con->deco_render_params);
1917         }
1918     }
1919
1920     cmd_output->needs_tree_render = true;
1921     ysuccess(true);
1922 }
1923
1924 /*
1925  * Implementation of 'rename workspace [<name>] to <name>'
1926  *
1927  */
1928 void cmd_rename_workspace(I3_CMD, const char *old_name, const char *new_name) {
1929     if (strncasecmp(new_name, "__", strlen("__")) == 0) {
1930         yerror("Cannot rename workspace to \"%s\": names starting with __ are i3-internal.", new_name);
1931         return;
1932     }
1933     if (old_name) {
1934         LOG("Renaming workspace \"%s\" to \"%s\"\n", old_name, new_name);
1935     } else {
1936         LOG("Renaming current workspace to \"%s\"\n", new_name);
1937     }
1938
1939     Con *workspace;
1940     if (old_name) {
1941         workspace = get_existing_workspace_by_name(old_name);
1942     } else {
1943         workspace = con_get_workspace(focused);
1944         old_name = workspace->name;
1945     }
1946
1947     if (!workspace) {
1948         yerror("Old workspace \"%s\" not found", old_name);
1949         return;
1950     }
1951
1952     Con *check_dest = get_existing_workspace_by_name(new_name);
1953
1954     /* If check_dest == workspace, the user might be changing the case of the
1955      * workspace, or it might just be a no-op. */
1956     if (check_dest != NULL && check_dest != workspace) {
1957         yerror("New workspace \"%s\" already exists", new_name);
1958         return;
1959     }
1960
1961     /* Change the name and try to parse it as a number. */
1962     /* old_name might refer to workspace->name, so copy it before free()ing */
1963     char *old_name_copy = sstrdup(old_name);
1964     FREE(workspace->name);
1965     workspace->name = sstrdup(new_name);
1966
1967     workspace->num = ws_name_to_number(new_name);
1968     LOG("num = %d\n", workspace->num);
1969
1970     /* By re-attaching, the sort order will be correct afterwards. */
1971     Con *previously_focused = focused;
1972     Con *previously_focused_content = focused->type == CT_WORKSPACE ? focused->parent : NULL;
1973     Con *parent = workspace->parent;
1974     con_detach(workspace);
1975     con_attach(workspace, parent, false);
1976     ipc_send_workspace_event("rename", workspace, NULL);
1977
1978     /* Move the workspace to the correct output if it has an assignment */
1979     struct Workspace_Assignment *assignment = NULL;
1980     TAILQ_FOREACH(assignment, &ws_assignments, ws_assignments) {
1981         if (assignment->output == NULL)
1982             continue;
1983         if (strcmp(assignment->name, workspace->name) != 0 && (!name_is_digits(assignment->name) || ws_name_to_number(assignment->name) != workspace->num)) {
1984             continue;
1985         }
1986
1987         Output *target_output = get_output_by_name(assignment->output, true);
1988         if (!target_output) {
1989             LOG("Could not get output named \"%s\"\n", assignment->output);
1990             continue;
1991         }
1992         if (!output_triggers_assignment(target_output, assignment)) {
1993             continue;
1994         }
1995         workspace_move_to_output(workspace, target_output);
1996
1997         break;
1998     }
1999
2000     bool can_restore_focus = previously_focused != NULL;
2001     /* NB: If previously_focused is a workspace we can't work directly with it
2002      * since it might have been cleaned up by workspace_show() already,
2003      * depending on the focus order/number of other workspaces on the output.
2004      * Instead, we loop through the available workspaces and only focus
2005      * previously_focused if we still find it. */
2006     if (previously_focused_content) {
2007         Con *workspace = NULL;
2008         GREP_FIRST(workspace, previously_focused_content, child == previously_focused);
2009         can_restore_focus &= (workspace != NULL);
2010     }
2011
2012     if (can_restore_focus) {
2013         /* Restore the previous focus since con_attach messes with the focus. */
2014         workspace_show(con_get_workspace(previously_focused));
2015         con_focus(previously_focused);
2016     }
2017
2018     cmd_output->needs_tree_render = true;
2019     ysuccess(true);
2020
2021     ewmh_update_desktop_names();
2022     ewmh_update_desktop_viewport();
2023     ewmh_update_current_desktop();
2024
2025     startup_sequence_rename_workspace(old_name_copy, new_name);
2026     free(old_name_copy);
2027 }
2028
2029 /*
2030  * Implementation of 'bar mode dock|hide|invisible|toggle [<bar_id>]'
2031  *
2032  */
2033 static bool cmd_bar_mode(const char *bar_mode, const char *bar_id) {
2034     int mode = M_DOCK;
2035     bool toggle = false;
2036     if (strcmp(bar_mode, "dock") == 0)
2037         mode = M_DOCK;
2038     else if (strcmp(bar_mode, "hide") == 0)
2039         mode = M_HIDE;
2040     else if (strcmp(bar_mode, "invisible") == 0)
2041         mode = M_INVISIBLE;
2042     else if (strcmp(bar_mode, "toggle") == 0)
2043         toggle = true;
2044     else {
2045         ELOG("Unknown bar mode \"%s\", this is a mismatch between code and parser spec.\n", bar_mode);
2046         return false;
2047     }
2048
2049     bool changed_sth = false;
2050     Barconfig *current = NULL;
2051     TAILQ_FOREACH(current, &barconfigs, configs) {
2052         if (bar_id && strcmp(current->id, bar_id) != 0)
2053             continue;
2054
2055         if (toggle)
2056             mode = (current->mode + 1) % 2;
2057
2058         DLOG("Changing bar mode of bar_id '%s' to '%s (%d)'\n", current->id, bar_mode, mode);
2059         current->mode = mode;
2060         changed_sth = true;
2061
2062         if (bar_id)
2063             break;
2064     }
2065
2066     if (bar_id && !changed_sth) {
2067         DLOG("Changing bar mode of bar_id %s failed, bar_id not found.\n", bar_id);
2068         return false;
2069     }
2070
2071     return true;
2072 }
2073
2074 /*
2075  * Implementation of 'bar hidden_state hide|show|toggle [<bar_id>]'
2076  *
2077  */
2078 static bool cmd_bar_hidden_state(const char *bar_hidden_state, const char *bar_id) {
2079     int hidden_state = S_SHOW;
2080     bool toggle = false;
2081     if (strcmp(bar_hidden_state, "hide") == 0)
2082         hidden_state = S_HIDE;
2083     else if (strcmp(bar_hidden_state, "show") == 0)
2084         hidden_state = S_SHOW;
2085     else if (strcmp(bar_hidden_state, "toggle") == 0)
2086         toggle = true;
2087     else {
2088         ELOG("Unknown bar state \"%s\", this is a mismatch between code and parser spec.\n", bar_hidden_state);
2089         return false;
2090     }
2091
2092     bool changed_sth = false;
2093     Barconfig *current = NULL;
2094     TAILQ_FOREACH(current, &barconfigs, configs) {
2095         if (bar_id && strcmp(current->id, bar_id) != 0)
2096             continue;
2097
2098         if (toggle)
2099             hidden_state = (current->hidden_state + 1) % 2;
2100
2101         DLOG("Changing bar hidden_state of bar_id '%s' to '%s (%d)'\n", current->id, bar_hidden_state, hidden_state);
2102         current->hidden_state = hidden_state;
2103         changed_sth = true;
2104
2105         if (bar_id)
2106             break;
2107     }
2108
2109     if (bar_id && !changed_sth) {
2110         DLOG("Changing bar hidden_state of bar_id %s failed, bar_id not found.\n", bar_id);
2111         return false;
2112     }
2113
2114     return true;
2115 }
2116
2117 /*
2118  * Implementation of 'bar (hidden_state hide|show|toggle)|(mode dock|hide|invisible|toggle) [<bar_id>]'
2119  *
2120  */
2121 void cmd_bar(I3_CMD, const char *bar_type, const char *bar_value, const char *bar_id) {
2122     bool ret;
2123     if (strcmp(bar_type, "mode") == 0)
2124         ret = cmd_bar_mode(bar_value, bar_id);
2125     else if (strcmp(bar_type, "hidden_state") == 0)
2126         ret = cmd_bar_hidden_state(bar_value, bar_id);
2127     else {
2128         ELOG("Unknown bar option type \"%s\", this is a mismatch between code and parser spec.\n", bar_type);
2129         ret = false;
2130     }
2131
2132     ysuccess(ret);
2133     if (!ret)
2134         return;
2135
2136     update_barconfig();
2137 }
2138
2139 /*
2140  * Implementation of 'shmlog <size>|toggle|on|off'
2141  *
2142  */
2143 void cmd_shmlog(I3_CMD, const char *argument) {
2144     if (!strcmp(argument, "toggle"))
2145         /* Toggle shm log, if size is not 0. If it is 0, set it to default. */
2146         shmlog_size = shmlog_size ? -shmlog_size : default_shmlog_size;
2147     else if (!strcmp(argument, "on"))
2148         shmlog_size = default_shmlog_size;
2149     else if (!strcmp(argument, "off"))
2150         shmlog_size = 0;
2151     else {
2152         long new_size = 0;
2153         if (!parse_long(argument, &new_size, 0)) {
2154             yerror("Failed to parse %s into a shmlog size.", argument);
2155             return;
2156         }
2157         /* If shm logging now, restart logging with the new size. */
2158         if (shmlog_size > 0) {
2159             shmlog_size = 0;
2160             LOG("Restarting shm logging...\n");
2161             init_logging();
2162         }
2163         shmlog_size = (int)new_size;
2164     }
2165     LOG("%s shm logging\n", shmlog_size > 0 ? "Enabling" : "Disabling");
2166     init_logging();
2167     update_shmlog_atom();
2168     ysuccess(true);
2169 }
2170
2171 /*
2172  * Implementation of 'debuglog toggle|on|off'
2173  *
2174  */
2175 void cmd_debuglog(I3_CMD, const char *argument) {
2176     bool logging = get_debug_logging();
2177     if (!strcmp(argument, "toggle")) {
2178         LOG("%s debug logging\n", logging ? "Disabling" : "Enabling");
2179         set_debug_logging(!logging);
2180     } else if (!strcmp(argument, "on") && !logging) {
2181         LOG("Enabling debug logging\n");
2182         set_debug_logging(true);
2183     } else if (!strcmp(argument, "off") && logging) {
2184         LOG("Disabling debug logging\n");
2185         set_debug_logging(false);
2186     }
2187     // XXX: default reply for now, make this a better reply
2188     ysuccess(true);
2189 }