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