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