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