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