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