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