]> git.sur5r.net Git - i3/i3/blob - src/commands.c
Introduce splith/splitv layouts, remove orientation
[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|current'.
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     /* We have nothing to move:
363      *  when criteria was specified but didn't match any window or
364      *  when criteria wasn't specified and we don't have any window focused. */
365     if ((!match_is_empty(current_match) && TAILQ_EMPTY(&owindows)) ||
366         (match_is_empty(current_match) && focused->type == CT_WORKSPACE)) {
367         ysuccess(false);
368         return;
369     }
370
371     HANDLE_EMPTY_MATCH;
372
373     /* get the workspace */
374     Con *ws;
375     if (strcmp(which, "next") == 0)
376         ws = workspace_next();
377     else if (strcmp(which, "prev") == 0)
378         ws = workspace_prev();
379     else if (strcmp(which, "next_on_output") == 0)
380         ws = workspace_next_on_output();
381     else if (strcmp(which, "prev_on_output") == 0)
382         ws = workspace_prev_on_output();
383     else if (strcmp(which, "current") == 0)
384         ws = con_get_workspace(focused);
385     else {
386         ELOG("BUG: called with which=%s\n", which);
387         ysuccess(false);
388         return;
389     }
390
391     TAILQ_FOREACH(current, &owindows, owindows) {
392         DLOG("matching: %p / %s\n", current->con, current->con->name);
393         con_move_to_workspace(current->con, ws, true, false);
394     }
395
396     cmd_output->needs_tree_render = true;
397     // XXX: default reply for now, make this a better reply
398     ysuccess(true);
399 }
400
401 /*
402  * Implementation of 'move [window|container] [to] workspace <name>'.
403  *
404  */
405 void cmd_move_con_to_workspace_name(I3_CMD, char *name) {
406     if (strncasecmp(name, "__i3_", strlen("__i3_")) == 0) {
407         LOG("You cannot switch to the i3 internal workspaces.\n");
408         ysuccess(false);
409         return;
410     }
411
412     owindow *current;
413
414     /* We have nothing to move:
415      *  when criteria was specified but didn't match any window or
416      *  when criteria wasn't specified and we don't have any window focused. */
417     if ((!match_is_empty(current_match) && TAILQ_EMPTY(&owindows)) ||
418         (match_is_empty(current_match) && focused->type == CT_WORKSPACE)) {
419         ysuccess(false);
420         return;
421     }
422
423     LOG("should move window to workspace %s\n", name);
424     /* get the workspace */
425     Con *ws = workspace_get(name, NULL);
426
427     HANDLE_EMPTY_MATCH;
428
429     TAILQ_FOREACH(current, &owindows, owindows) {
430         DLOG("matching: %p / %s\n", current->con, current->con->name);
431         con_move_to_workspace(current->con, ws, true, false);
432     }
433
434     cmd_output->needs_tree_render = true;
435     // XXX: default reply for now, make this a better reply
436     ysuccess(true);
437 }
438
439 /*
440  * Implementation of 'move [window|container] [to] workspace number <number>'.
441  *
442  */
443 void cmd_move_con_to_workspace_number(I3_CMD, char *which) {
444     owindow *current;
445
446     /* We have nothing to move:
447      *  when criteria was specified but didn't match any window or
448      *  when criteria wasn't specified and we don't have any window focused. */
449     if ((!match_is_empty(current_match) && TAILQ_EMPTY(&owindows)) ||
450         (match_is_empty(current_match) && focused->type == CT_WORKSPACE)) {
451         ysuccess(false);
452         return;
453     }
454
455     LOG("should move window to workspace with number %d\n", which);
456     /* get the workspace */
457     Con *output, *workspace = NULL;
458
459     char *endptr = NULL;
460     long parsed_num = strtol(which, &endptr, 10);
461     if (parsed_num == LONG_MIN ||
462         parsed_num == LONG_MAX ||
463         parsed_num < 0 ||
464         *endptr != '\0') {
465         LOG("Could not parse \"%s\" as a number.\n", which);
466         y(map_open);
467         ystr("success");
468         y(bool, false);
469         ystr("error");
470         // TODO: better error message
471         ystr("Could not parse number");
472         y(map_close);
473         return;
474     }
475
476     TAILQ_FOREACH(output, &(croot->nodes_head), nodes)
477         GREP_FIRST(workspace, output_get_content(output),
478             child->num == parsed_num);
479
480     if (!workspace) {
481         y(map_open);
482         ystr("success");
483         y(bool, false);
484         ystr("error");
485         // TODO: better error message
486         ystr("No such workspace");
487         y(map_close);
488         return;
489     }
490
491     HANDLE_EMPTY_MATCH;
492
493     TAILQ_FOREACH(current, &owindows, owindows) {
494         DLOG("matching: %p / %s\n", current->con, current->con->name);
495         con_move_to_workspace(current->con, workspace, true, false);
496     }
497
498     cmd_output->needs_tree_render = true;
499     // XXX: default reply for now, make this a better reply
500     ysuccess(true);
501 }
502
503 static void cmd_resize_floating(I3_CMD, char *way, char *direction, Con *floating_con, int px) {
504     LOG("floating resize\n");
505     if (strcmp(direction, "up") == 0) {
506         floating_con->rect.y -= px;
507         floating_con->rect.height += px;
508     } else if (strcmp(direction, "down") == 0) {
509         floating_con->rect.height += px;
510     } else if (strcmp(direction, "left") == 0) {
511         floating_con->rect.x -= px;
512         floating_con->rect.width += px;
513     } else {
514         floating_con->rect.width += px;
515     }
516 }
517
518 static bool cmd_resize_tiling_direction(I3_CMD, char *way, char *direction, int ppt) {
519     LOG("tiling resize\n");
520     /* get the appropriate current container (skip stacked/tabbed cons) */
521     Con *current = focused;
522     Con *other = NULL;
523     double percentage = 0;
524     while (current->parent->layout == L_STACKED ||
525            current->parent->layout == L_TABBED)
526         current = current->parent;
527
528     /* Then further go up until we find one with the matching orientation. */
529     orientation_t search_orientation =
530         (strcmp(direction, "left") == 0 || strcmp(direction, "right") == 0 ? HORIZ : VERT);
531
532     do {
533         if (con_orientation(current->parent) != search_orientation) {
534             current = current->parent;
535             continue;
536         }
537
538         /* get the default percentage */
539         int children = con_num_children(current->parent);
540         LOG("ins. %d children\n", children);
541         percentage = 1.0 / children;
542         LOG("default percentage = %f\n", percentage);
543
544         orientation_t orientation = con_orientation(current->parent);
545
546         if ((orientation == HORIZ &&
547              (strcmp(direction, "up") == 0 || strcmp(direction, "down") == 0)) ||
548             (orientation == VERT &&
549              (strcmp(direction, "left") == 0 || strcmp(direction, "right") == 0))) {
550             LOG("You cannot resize in that direction. Your focus is in a %s split container currently.\n",
551                 (orientation == HORIZ ? "horizontal" : "vertical"));
552             ysuccess(false);
553             return false;
554         }
555
556         if (strcmp(direction, "up") == 0 || strcmp(direction, "left") == 0) {
557             other = TAILQ_PREV(current, nodes_head, nodes);
558         } else {
559             other = TAILQ_NEXT(current, nodes);
560         }
561         if (other == TAILQ_END(workspaces)) {
562             LOG("No other container in this direction found, trying to look further up in the tree...\n");
563             current = current->parent;
564             continue;
565         }
566         break;
567     } while (current->type != CT_WORKSPACE &&
568              current->type != CT_FLOATING_CON);
569
570     if (other == NULL) {
571         LOG("No other container in this direction found, trying to look further up in the tree...\n");
572         ysuccess(false);
573         return false;
574     }
575
576     LOG("other->percent = %f\n", other->percent);
577     LOG("current->percent before = %f\n", current->percent);
578     if (current->percent == 0.0)
579         current->percent = percentage;
580     if (other->percent == 0.0)
581         other->percent = percentage;
582     double new_current_percent = current->percent + ((double)ppt / 100.0);
583     double new_other_percent = other->percent - ((double)ppt / 100.0);
584     LOG("new_current_percent = %f\n", new_current_percent);
585     LOG("new_other_percent = %f\n", new_other_percent);
586     /* Ensure that the new percentages are positive and greater than
587      * 0.05 to have a reasonable minimum size. */
588     if (definitelyGreaterThan(new_current_percent, 0.05, DBL_EPSILON) &&
589         definitelyGreaterThan(new_other_percent, 0.05, DBL_EPSILON)) {
590         current->percent += ((double)ppt / 100.0);
591         other->percent -= ((double)ppt / 100.0);
592         LOG("current->percent after = %f\n", current->percent);
593         LOG("other->percent after = %f\n", other->percent);
594     } else {
595         LOG("Not resizing, already at minimum size\n");
596     }
597
598     return true;
599 }
600
601 static bool cmd_resize_tiling_width_height(I3_CMD, char *way, char *direction, int ppt) {
602     LOG("width/height resize\n");
603     /* get the appropriate current container (skip stacked/tabbed cons) */
604     Con *current = focused;
605     while (current->parent->layout == L_STACKED ||
606            current->parent->layout == L_TABBED)
607         current = current->parent;
608
609     /* Then further go up until we find one with the matching orientation. */
610     orientation_t search_orientation =
611         (strcmp(direction, "width") == 0 ? HORIZ : VERT);
612
613     while (current->type != CT_WORKSPACE &&
614            current->type != CT_FLOATING_CON &&
615            con_orientation(current->parent) != search_orientation)
616         current = current->parent;
617
618     /* get the default percentage */
619     int children = con_num_children(current->parent);
620     LOG("ins. %d children\n", children);
621     double percentage = 1.0 / children;
622     LOG("default percentage = %f\n", percentage);
623
624     orientation_t orientation = con_orientation(current->parent);
625
626     if ((orientation == HORIZ &&
627          strcmp(direction, "height") == 0) ||
628         (orientation == VERT &&
629          strcmp(direction, "width") == 0)) {
630         LOG("You cannot resize in that direction. Your focus is in a %s split container currently.\n",
631             (orientation == HORIZ ? "horizontal" : "vertical"));
632         ysuccess(false);
633         return false;
634     }
635
636     if (children == 1) {
637         LOG("This is the only container, cannot resize.\n");
638         ysuccess(false);
639         return false;
640     }
641
642     /* Ensure all the other children have a percentage set. */
643     Con *child;
644     TAILQ_FOREACH(child, &(current->parent->nodes_head), nodes) {
645         LOG("child->percent = %f (child %p)\n", child->percent, child);
646         if (child->percent == 0.0)
647             child->percent = percentage;
648     }
649
650     double new_current_percent = current->percent + ((double)ppt / 100.0);
651     double subtract_percent = ((double)ppt / 100.0) / (children - 1);
652     LOG("new_current_percent = %f\n", new_current_percent);
653     LOG("subtract_percent = %f\n", subtract_percent);
654     /* Ensure that the new percentages are positive and greater than
655      * 0.05 to have a reasonable minimum size. */
656     TAILQ_FOREACH(child, &(current->parent->nodes_head), nodes) {
657         if (child == current)
658             continue;
659         if (!definitelyGreaterThan(child->percent - subtract_percent, 0.05, DBL_EPSILON)) {
660             LOG("Not resizing, already at minimum size (child %p would end up with a size of %.f\n", child, child->percent - subtract_percent);
661             ysuccess(false);
662             return false;
663         }
664     }
665     if (!definitelyGreaterThan(new_current_percent, 0.05, DBL_EPSILON)) {
666         LOG("Not resizing, already at minimum size\n");
667         ysuccess(false);
668         return false;
669     }
670
671     current->percent += ((double)ppt / 100.0);
672     LOG("current->percent after = %f\n", current->percent);
673
674     TAILQ_FOREACH(child, &(current->parent->nodes_head), nodes) {
675         if (child == current)
676             continue;
677         child->percent -= subtract_percent;
678         LOG("child->percent after (%p) = %f\n", child, child->percent);
679     }
680
681     return true;
682 }
683
684 /*
685  * Implementation of 'resize grow|shrink <direction> [<px> px] [or <ppt> ppt]'.
686  *
687  */
688 void cmd_resize(I3_CMD, char *way, char *direction, char *resize_px, char *resize_ppt) {
689     /* resize <grow|shrink> <direction> [<px> px] [or <ppt> ppt] */
690     DLOG("resizing in way %s, direction %s, px %s or ppt %s\n", way, direction, resize_px, resize_ppt);
691     // 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
692     int px = atoi(resize_px);
693     int ppt = atoi(resize_ppt);
694     if (strcmp(way, "shrink") == 0) {
695         px *= -1;
696         ppt *= -1;
697     }
698
699     Con *floating_con;
700     if ((floating_con = con_inside_floating(focused))) {
701         cmd_resize_floating(current_match, cmd_output, way, direction, floating_con, px);
702     } else {
703         if (strcmp(direction, "width") == 0 ||
704             strcmp(direction, "height") == 0) {
705             if (!cmd_resize_tiling_width_height(current_match, cmd_output, way, direction, ppt))
706                 return;
707         } else {
708             if (!cmd_resize_tiling_direction(current_match, cmd_output, way, direction, ppt))
709                 return;
710         }
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 'border normal|none|1pixel|toggle'.
720  *
721  */
722 void cmd_border(I3_CMD, char *border_style_str) {
723     DLOG("border style should be changed to %s\n", border_style_str);
724     owindow *current;
725
726     HANDLE_EMPTY_MATCH;
727
728     TAILQ_FOREACH(current, &owindows, owindows) {
729         DLOG("matching: %p / %s\n", current->con, current->con->name);
730         int border_style = current->con->border_style;
731         if (strcmp(border_style_str, "toggle") == 0) {
732             border_style++;
733             border_style %= 3;
734         } else {
735             if (strcmp(border_style_str, "normal") == 0)
736                 border_style = BS_NORMAL;
737             else if (strcmp(border_style_str, "none") == 0)
738                 border_style = BS_NONE;
739             else if (strcmp(border_style_str, "1pixel") == 0)
740                 border_style = BS_1PIXEL;
741             else {
742                 ELOG("BUG: called with border_style=%s\n", border_style_str);
743                 ysuccess(false);
744                 return;
745             }
746         }
747         con_set_border_style(current->con, border_style);
748     }
749
750     cmd_output->needs_tree_render = true;
751     // XXX: default reply for now, make this a better reply
752     ysuccess(true);
753 }
754
755 /*
756  * Implementation of 'nop <comment>'.
757  *
758  */
759 void cmd_nop(I3_CMD, char *comment) {
760     LOG("-------------------------------------------------\n");
761     LOG("  NOP: %s\n", comment);
762     LOG("-------------------------------------------------\n");
763 }
764
765 /*
766  * Implementation of 'append_layout <path>'.
767  *
768  */
769 void cmd_append_layout(I3_CMD, char *path) {
770     LOG("Appending layout \"%s\"\n", path);
771     tree_append_json(path);
772
773     cmd_output->needs_tree_render = true;
774     // XXX: default reply for now, make this a better reply
775     ysuccess(true);
776 }
777
778 /*
779  * Implementation of 'workspace next|prev|next_on_output|prev_on_output'.
780  *
781  */
782 void cmd_workspace(I3_CMD, char *which) {
783     Con *ws;
784
785     DLOG("which=%s\n", which);
786
787     if (strcmp(which, "next") == 0)
788         ws = workspace_next();
789     else if (strcmp(which, "prev") == 0)
790         ws = workspace_prev();
791     else if (strcmp(which, "next_on_output") == 0)
792         ws = workspace_next_on_output();
793     else if (strcmp(which, "prev_on_output") == 0)
794         ws = workspace_prev_on_output();
795     else {
796         ELOG("BUG: called with which=%s\n", which);
797         ysuccess(false);
798         return;
799     }
800
801     workspace_show(ws);
802
803     cmd_output->needs_tree_render = true;
804     // XXX: default reply for now, make this a better reply
805     ysuccess(true);
806 }
807
808 /*
809  * Implementation of 'workspace number <number>'
810  *
811  */
812 void cmd_workspace_number(I3_CMD, char *which) {
813     Con *output, *workspace = NULL;
814
815     char *endptr = NULL;
816     long parsed_num = strtol(which, &endptr, 10);
817     if (parsed_num == LONG_MIN ||
818         parsed_num == LONG_MAX ||
819         parsed_num < 0 ||
820         *endptr != '\0') {
821         LOG("Could not parse \"%s\" as a number.\n", which);
822         y(map_open);
823         ystr("success");
824         y(bool, false);
825         ystr("error");
826         // TODO: better error message
827         ystr("Could not parse number");
828         y(map_close);
829
830         return;
831     }
832
833     TAILQ_FOREACH(output, &(croot->nodes_head), nodes)
834         GREP_FIRST(workspace, output_get_content(output),
835             child->num == parsed_num);
836
837     if (!workspace) {
838         LOG("There is no workspace with number %d, creating a new one.\n", parsed_num);
839         ysuccess(true);
840         /* terminate the which string after the endposition of the number */
841         *endptr = '\0';
842         if (maybe_back_and_forth(cmd_output, which))
843             return;
844         workspace_show_by_name(which);
845         cmd_output->needs_tree_render = true;
846         return;
847     }
848     if (maybe_back_and_forth(cmd_output, which))
849         return;
850     workspace_show(workspace);
851
852     cmd_output->needs_tree_render = true;
853     // XXX: default reply for now, make this a better reply
854     ysuccess(true);
855 }
856
857 /*
858  * Implementation of 'workspace back_and_forth'.
859  *
860  */
861 void cmd_workspace_back_and_forth(I3_CMD) {
862     workspace_back_and_forth();
863
864     cmd_output->needs_tree_render = true;
865     // XXX: default reply for now, make this a better reply
866     ysuccess(true);
867 }
868
869 /*
870  * Implementation of 'workspace <name>'
871  *
872  */
873 void cmd_workspace_name(I3_CMD, char *name) {
874     if (strncasecmp(name, "__i3_", strlen("__i3_")) == 0) {
875         LOG("You cannot switch to the i3 internal workspaces.\n");
876         ysuccess(false);
877         return;
878     }
879
880     DLOG("should switch to workspace %s\n", name);
881     if (maybe_back_and_forth(cmd_output, name))
882        return;
883     workspace_show_by_name(name);
884
885     cmd_output->needs_tree_render = true;
886     // XXX: default reply for now, make this a better reply
887     ysuccess(true);
888 }
889
890 /*
891  * Implementation of 'mark <mark>'
892  *
893  */
894 void cmd_mark(I3_CMD, char *mark) {
895     DLOG("Clearing all windows which have that mark first\n");
896
897     Con *con;
898     TAILQ_FOREACH(con, &all_cons, all_cons) {
899         if (con->mark && strcmp(con->mark, mark) == 0)
900             FREE(con->mark);
901     }
902
903     DLOG("marking window with str %s\n", mark);
904     owindow *current;
905
906     HANDLE_EMPTY_MATCH;
907
908     TAILQ_FOREACH(current, &owindows, owindows) {
909         DLOG("matching: %p / %s\n", current->con, current->con->name);
910         current->con->mark = sstrdup(mark);
911     }
912
913     cmd_output->needs_tree_render = true;
914     // XXX: default reply for now, make this a better reply
915     ysuccess(true);
916 }
917
918 /*
919  * Implementation of 'mode <string>'.
920  *
921  */
922 void cmd_mode(I3_CMD, char *mode) {
923     DLOG("mode=%s\n", mode);
924     switch_mode(mode);
925
926     // XXX: default reply for now, make this a better reply
927     ysuccess(true);
928 }
929
930 /*
931  * Implementation of 'move [window|container] [to] output <str>'.
932  *
933  */
934 void cmd_move_con_to_output(I3_CMD, char *name) {
935     owindow *current;
936
937     DLOG("should move window to output %s\n", name);
938
939     HANDLE_EMPTY_MATCH;
940
941     /* get the output */
942     Output *current_output = NULL;
943     Output *output;
944
945     // TODO: fix the handling of criteria
946     TAILQ_FOREACH(current, &owindows, owindows)
947         current_output = get_output_containing(current->con->rect.x, current->con->rect.y);
948
949     assert(current_output != NULL);
950
951     // TODO: clean this up with commands.spec as soon as we switched away from the lex/yacc command parser
952     if (strcasecmp(name, "up") == 0)
953         output = get_output_next(D_UP, current_output);
954     else if (strcasecmp(name, "down") == 0)
955         output = get_output_next(D_DOWN, current_output);
956     else if (strcasecmp(name, "left") == 0)
957         output = get_output_next(D_LEFT, current_output);
958     else if (strcasecmp(name, "right") == 0)
959         output = get_output_next(D_RIGHT, current_output);
960     else
961         output = get_output_by_name(name);
962
963     if (!output) {
964         LOG("No such output found.\n");
965         ysuccess(false);
966         return;
967     }
968
969     /* get visible workspace on output */
970     Con *ws = NULL;
971     GREP_FIRST(ws, output_get_content(output->con), workspace_is_visible(child));
972     if (!ws) {
973         ysuccess(false);
974         return;
975     }
976
977     TAILQ_FOREACH(current, &owindows, owindows) {
978         DLOG("matching: %p / %s\n", current->con, current->con->name);
979         con_move_to_workspace(current->con, ws, true, false);
980     }
981
982     cmd_output->needs_tree_render = true;
983     // XXX: default reply for now, make this a better reply
984     ysuccess(true);
985 }
986
987 /*
988  * Implementation of 'floating enable|disable|toggle'
989  *
990  */
991 void cmd_floating(I3_CMD, char *floating_mode) {
992     owindow *current;
993
994     DLOG("floating_mode=%s\n", floating_mode);
995
996     HANDLE_EMPTY_MATCH;
997
998     TAILQ_FOREACH(current, &owindows, owindows) {
999         DLOG("matching: %p / %s\n", current->con, current->con->name);
1000         if (strcmp(floating_mode, "toggle") == 0) {
1001             DLOG("should toggle mode\n");
1002             toggle_floating_mode(current->con, false);
1003         } else {
1004             DLOG("should switch mode to %s\n", floating_mode);
1005             if (strcmp(floating_mode, "enable") == 0) {
1006                 floating_enable(current->con, false);
1007             } else {
1008                 floating_disable(current->con, false);
1009             }
1010         }
1011     }
1012
1013     cmd_output->needs_tree_render = true;
1014     // XXX: default reply for now, make this a better reply
1015     ysuccess(true);
1016 }
1017
1018 /*
1019  * Implementation of 'move workspace to [output] <str>'.
1020  *
1021  */
1022 void cmd_move_workspace_to_output(I3_CMD, char *name) {
1023     DLOG("should move workspace to output %s\n", name);
1024
1025     HANDLE_EMPTY_MATCH;
1026
1027     owindow *current;
1028     TAILQ_FOREACH(current, &owindows, owindows) {
1029         Output *current_output = get_output_containing(current->con->rect.x,
1030                                                        current->con->rect.y);
1031         Output *output = get_output_from_string(current_output, name);
1032         if (!output) {
1033             LOG("No such output\n");
1034             ysuccess(false);
1035             return;
1036         }
1037
1038         Con *content = output_get_content(output->con);
1039         LOG("got output %p with content %p\n", output, content);
1040
1041         Con *ws = con_get_workspace(current->con);
1042         LOG("should move workspace %p / %s\n", ws, ws->name);
1043
1044         if (con_num_children(ws->parent) == 1) {
1045             LOG("Creating a new workspace to replace \"%s\" (last on its output).\n", ws->name);
1046
1047             /* check if we can find a workspace assigned to this output */
1048             bool used_assignment = false;
1049             struct Workspace_Assignment *assignment;
1050             TAILQ_FOREACH(assignment, &ws_assignments, ws_assignments) {
1051                 if (strcmp(assignment->output, current_output->name) != 0)
1052                     continue;
1053
1054                 /* check if this workspace is already attached to the tree */
1055                 Con *workspace = NULL, *out;
1056                 TAILQ_FOREACH(out, &(croot->nodes_head), nodes)
1057                     GREP_FIRST(workspace, output_get_content(out),
1058                                !strcasecmp(child->name, assignment->name));
1059                 if (workspace != NULL)
1060                     continue;
1061
1062                 /* so create the workspace referenced to by this assignment */
1063                 LOG("Creating workspace from assignment %s.\n", assignment->name);
1064                 workspace_get(assignment->name, NULL);
1065                 used_assignment = true;
1066                 break;
1067             }
1068
1069             /* if we couldn't create the workspace using an assignment, create
1070              * it on the output */
1071             if (!used_assignment)
1072                 create_workspace_on_output(current_output, ws->parent);
1073
1074             /* notify the IPC listeners */
1075             ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"init\"}");
1076         }
1077
1078         /* detach from the old output and attach to the new output */
1079         bool workspace_was_visible = workspace_is_visible(ws);
1080         Con *old_content = ws->parent;
1081         con_detach(ws);
1082         if (workspace_was_visible) {
1083             /* The workspace which we just detached was visible, so focus
1084              * the next one in the focus-stack. */
1085             Con *focus_ws = TAILQ_FIRST(&(old_content->focus_head));
1086             LOG("workspace was visible, focusing %p / %s now\n", focus_ws, focus_ws->name);
1087             workspace_show(focus_ws);
1088         }
1089         con_attach(ws, content, false);
1090
1091         /* fix the coordinates of the floating containers */
1092         Con *floating_con;
1093         TAILQ_FOREACH(floating_con, &(ws->floating_head), floating_windows)
1094             floating_fix_coordinates(floating_con, &(old_content->rect), &(content->rect));
1095
1096         ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"move\"}");
1097         if (workspace_was_visible) {
1098             /* Focus the moved workspace on the destination output. */
1099             workspace_show(ws);
1100         }
1101     }
1102
1103     cmd_output->needs_tree_render = true;
1104     // XXX: default reply for now, make this a better reply
1105     ysuccess(true);
1106 }
1107
1108 /*
1109  * Implementation of 'split v|h|vertical|horizontal'.
1110  *
1111  */
1112 void cmd_split(I3_CMD, char *direction) {
1113     /* TODO: use matches */
1114     LOG("splitting in direction %c\n", direction[0]);
1115     tree_split(focused, (direction[0] == 'v' ? VERT : HORIZ));
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  * Implementaiton of 'kill [window|client]'.
1124  *
1125  */
1126 void cmd_kill(I3_CMD, char *kill_mode_str) {
1127     if (kill_mode_str == NULL)
1128         kill_mode_str = "window";
1129     owindow *current;
1130
1131     DLOG("kill_mode=%s\n", kill_mode_str);
1132
1133     int kill_mode;
1134     if (strcmp(kill_mode_str, "window") == 0)
1135         kill_mode = KILL_WINDOW;
1136     else if (strcmp(kill_mode_str, "client") == 0)
1137         kill_mode = KILL_CLIENT;
1138     else {
1139         ELOG("BUG: called with kill_mode=%s\n", kill_mode_str);
1140         ysuccess(false);
1141         return;
1142     }
1143
1144     /* check if the match is empty, not if the result is empty */
1145     if (match_is_empty(current_match))
1146         tree_close_con(kill_mode);
1147     else {
1148         TAILQ_FOREACH(current, &owindows, owindows) {
1149             DLOG("matching: %p / %s\n", current->con, current->con->name);
1150             tree_close(current->con, kill_mode, false, false);
1151         }
1152     }
1153
1154     cmd_output->needs_tree_render = true;
1155     // XXX: default reply for now, make this a better reply
1156     ysuccess(true);
1157 }
1158
1159 /*
1160  * Implementation of 'exec [--no-startup-id] <command>'.
1161  *
1162  */
1163 void cmd_exec(I3_CMD, char *nosn, char *command) {
1164     bool no_startup_id = (nosn != NULL);
1165
1166     DLOG("should execute %s, no_startup_id = %d\n", command, no_startup_id);
1167     start_application(command, no_startup_id);
1168
1169     // XXX: default reply for now, make this a better reply
1170     ysuccess(true);
1171 }
1172
1173 /*
1174  * Implementation of 'focus left|right|up|down'.
1175  *
1176  */
1177 void cmd_focus_direction(I3_CMD, char *direction) {
1178     if (focused &&
1179         focused->type != CT_WORKSPACE &&
1180         focused->fullscreen_mode != CF_NONE) {
1181         LOG("Cannot change focus while in fullscreen mode.\n");
1182         ysuccess(false);
1183         return;
1184     }
1185
1186     DLOG("direction = *%s*\n", direction);
1187
1188     if (strcmp(direction, "left") == 0)
1189         tree_next('p', HORIZ);
1190     else if (strcmp(direction, "right") == 0)
1191         tree_next('n', HORIZ);
1192     else if (strcmp(direction, "up") == 0)
1193         tree_next('p', VERT);
1194     else if (strcmp(direction, "down") == 0)
1195         tree_next('n', VERT);
1196     else {
1197         ELOG("Invalid focus direction (%s)\n", direction);
1198         ysuccess(false);
1199         return;
1200     }
1201
1202     cmd_output->needs_tree_render = true;
1203     // XXX: default reply for now, make this a better reply
1204     ysuccess(true);
1205 }
1206
1207 /*
1208  * Implementation of 'focus tiling|floating|mode_toggle'.
1209  *
1210  */
1211 void cmd_focus_window_mode(I3_CMD, char *window_mode) {
1212     if (focused &&
1213         focused->type != CT_WORKSPACE &&
1214         focused->fullscreen_mode != CF_NONE) {
1215         LOG("Cannot change focus while in fullscreen mode.\n");
1216         ysuccess(false);
1217         return;
1218     }
1219
1220     DLOG("window_mode = %s\n", window_mode);
1221
1222     Con *ws = con_get_workspace(focused);
1223     Con *current;
1224     if (ws != NULL) {
1225         if (strcmp(window_mode, "mode_toggle") == 0) {
1226             current = TAILQ_FIRST(&(ws->focus_head));
1227             if (current != NULL && current->type == CT_FLOATING_CON)
1228                 window_mode = "tiling";
1229             else window_mode = "floating";
1230         }
1231         TAILQ_FOREACH(current, &(ws->focus_head), focused) {
1232             if ((strcmp(window_mode, "floating") == 0 && current->type != CT_FLOATING_CON) ||
1233                 (strcmp(window_mode, "tiling") == 0 && current->type == CT_FLOATING_CON))
1234                 continue;
1235
1236             con_focus(con_descend_focused(current));
1237             break;
1238         }
1239     }
1240
1241     cmd_output->needs_tree_render = true;
1242     // XXX: default reply for now, make this a better reply
1243     ysuccess(true);
1244 }
1245
1246 /*
1247  * Implementation of 'focus parent|child'.
1248  *
1249  */
1250 void cmd_focus_level(I3_CMD, char *level) {
1251     DLOG("level = %s\n", level);
1252     bool success = false;
1253
1254     /* Focusing the parent can only be allowed if the newly
1255      * focused container won't escape the fullscreen container. */
1256     if (strcmp(level, "parent") == 0) {
1257         if (focused && focused->parent) {
1258             if (con_fullscreen_permits_focusing(focused->parent))
1259                 success = level_up();
1260             else
1261                 LOG("Currently in fullscreen, not going up\n");
1262         }
1263     }
1264
1265     /* Focusing a child should always be allowed. */
1266     else success = level_down();
1267
1268     cmd_output->needs_tree_render = success;
1269     // XXX: default reply for now, make this a better reply
1270     ysuccess(success);
1271 }
1272
1273 /*
1274  * Implementation of 'focus'.
1275  *
1276  */
1277 void cmd_focus(I3_CMD) {
1278     DLOG("current_match = %p\n", current_match);
1279
1280     if (match_is_empty(current_match)) {
1281         ELOG("You have to specify which window/container should be focused.\n");
1282         ELOG("Example: [class=\"urxvt\" title=\"irssi\"] focus\n");
1283
1284         y(map_open);
1285         ystr("success");
1286         y(bool, false);
1287         ystr("error");
1288         ystr("You have to specify which window/container should be focused");
1289         y(map_close);
1290
1291         return;
1292     }
1293
1294     int count = 0;
1295     owindow *current;
1296     TAILQ_FOREACH(current, &owindows, owindows) {
1297         Con *ws = con_get_workspace(current->con);
1298         /* If no workspace could be found, this was a dock window.
1299          * Just skip it, you cannot focus dock windows. */
1300         if (!ws)
1301             continue;
1302
1303         /* Check the fullscreen focus constraints. */
1304         if (!con_fullscreen_permits_focusing(current->con)) {
1305             LOG("Cannot change focus while in fullscreen mode (fullscreen rules).\n");
1306             ysuccess(false);
1307             return;
1308         }
1309
1310         /* If the container is not on the current workspace,
1311          * workspace_show() will switch to a different workspace and (if
1312          * enabled) trigger a mouse pointer warp to the currently focused
1313          * container (!) on the target workspace.
1314          *
1315          * Therefore, before calling workspace_show(), we make sure that
1316          * 'current' will be focused on the workspace. However, we cannot
1317          * just con_focus(current) because then the pointer will not be
1318          * warped at all (the code thinks we are already there).
1319          *
1320          * So we focus 'current' to make it the currently focused window of
1321          * the target workspace, then revert focus. */
1322         Con *currently_focused = focused;
1323         con_focus(current->con);
1324         con_focus(currently_focused);
1325
1326         /* Now switch to the workspace, then focus */
1327         workspace_show(ws);
1328         LOG("focusing %p / %s\n", current->con, current->con->name);
1329         con_focus(current->con);
1330         count++;
1331     }
1332
1333     if (count > 1)
1334         LOG("WARNING: Your criteria for the focus command matches %d containers, "
1335             "while only exactly one container can be focused at a time.\n", count);
1336
1337     cmd_output->needs_tree_render = true;
1338     // XXX: default reply for now, make this a better reply
1339     ysuccess(true);
1340 }
1341
1342 /*
1343  * Implementation of 'fullscreen [global]'.
1344  *
1345  */
1346 void cmd_fullscreen(I3_CMD, char *fullscreen_mode) {
1347     if (fullscreen_mode == NULL)
1348         fullscreen_mode = "output";
1349     DLOG("toggling fullscreen, mode = %s\n", fullscreen_mode);
1350     owindow *current;
1351
1352     HANDLE_EMPTY_MATCH;
1353
1354     TAILQ_FOREACH(current, &owindows, owindows) {
1355         printf("matching: %p / %s\n", current->con, current->con->name);
1356         con_toggle_fullscreen(current->con, (strcmp(fullscreen_mode, "global") == 0 ? CF_GLOBAL : CF_OUTPUT));
1357     }
1358
1359     cmd_output->needs_tree_render = true;
1360     // XXX: default reply for now, make this a better reply
1361     ysuccess(true);
1362 }
1363
1364 /*
1365  * Implementation of 'move <direction> [<pixels> [px]]'.
1366  *
1367  */
1368 void cmd_move_direction(I3_CMD, char *direction, char *move_px) {
1369     // 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
1370     int px = atoi(move_px);
1371
1372     /* TODO: make 'move' work with criteria. */
1373     DLOG("moving in direction %s, px %s\n", direction, move_px);
1374     if (con_is_floating(focused)) {
1375         DLOG("floating move with %d pixels\n", px);
1376         Rect newrect = focused->parent->rect;
1377         if (strcmp(direction, "left") == 0) {
1378             newrect.x -= px;
1379         } else if (strcmp(direction, "right") == 0) {
1380             newrect.x += px;
1381         } else if (strcmp(direction, "up") == 0) {
1382             newrect.y -= px;
1383         } else if (strcmp(direction, "down") == 0) {
1384             newrect.y += px;
1385         }
1386         floating_reposition(focused->parent, newrect);
1387     } else {
1388         tree_move((strcmp(direction, "right") == 0 ? D_RIGHT :
1389                    (strcmp(direction, "left") == 0 ? D_LEFT :
1390                     (strcmp(direction, "up") == 0 ? D_UP :
1391                      D_DOWN))));
1392         cmd_output->needs_tree_render = true;
1393     }
1394
1395     // XXX: default reply for now, make this a better reply
1396     ysuccess(true);
1397 }
1398
1399 /*
1400  * Implementation of 'layout default|stacked|stacking|tabbed|splitv|splith'.
1401  *
1402  */
1403 void cmd_layout(I3_CMD, char *layout_str) {
1404     if (strcmp(layout_str, "stacking") == 0)
1405         layout_str = "stacked";
1406     owindow *current;
1407     int layout;
1408     /* default is a special case which will be handled in con_set_layout(). */
1409     if (strcmp(layout_str, "default") == 0)
1410         layout = L_DEFAULT;
1411     else if (strcmp(layout_str, "stacked") == 0)
1412         layout = L_STACKED;
1413     else if (strcmp(layout_str, "tabbed") == 0)
1414         layout = L_TABBED;
1415     else if (strcmp(layout_str, "splitv") == 0)
1416         layout = L_SPLITV;
1417     else if (strcmp(layout_str, "splith") == 0)
1418         layout = L_SPLITH;
1419
1420     DLOG("changing layout to %s (%d)\n", layout_str, layout);
1421
1422     /* check if the match is empty, not if the result is empty */
1423     if (match_is_empty(current_match))
1424         con_set_layout(focused->parent, layout);
1425     else {
1426         TAILQ_FOREACH(current, &owindows, owindows) {
1427             DLOG("matching: %p / %s\n", current->con, current->con->name);
1428             con_set_layout(current->con, layout);
1429         }
1430     }
1431
1432     cmd_output->needs_tree_render = true;
1433     // XXX: default reply for now, make this a better reply
1434     ysuccess(true);
1435 }
1436
1437 /*
1438  * Implementation of 'layout toggle [all|split]'.
1439  *
1440  */
1441 void cmd_layout_toggle(I3_CMD, char *toggle_mode) {
1442     owindow *current;
1443
1444     if (toggle_mode == NULL)
1445         toggle_mode = "default";
1446
1447     DLOG("toggling layout (mode = %s)\n", toggle_mode);
1448
1449     /* check if the match is empty, not if the result is empty */
1450     if (match_is_empty(current_match))
1451         con_toggle_layout(focused->parent, toggle_mode);
1452     else {
1453         TAILQ_FOREACH(current, &owindows, owindows) {
1454             DLOG("matching: %p / %s\n", current->con, current->con->name);
1455             con_toggle_layout(current->con, toggle_mode);
1456         }
1457     }
1458
1459     cmd_output->needs_tree_render = true;
1460     // XXX: default reply for now, make this a better reply
1461     ysuccess(true);
1462 }
1463
1464 /*
1465  * Implementaiton of 'exit'.
1466  *
1467  */
1468 void cmd_exit(I3_CMD) {
1469     LOG("Exiting due to user command.\n");
1470     xcb_disconnect(conn);
1471     exit(0);
1472
1473     /* unreached */
1474 }
1475
1476 /*
1477  * Implementaiton of 'reload'.
1478  *
1479  */
1480 void cmd_reload(I3_CMD) {
1481     LOG("reloading\n");
1482     kill_configerror_nagbar(false);
1483     kill_commanderror_nagbar(false);
1484     load_configuration(conn, NULL, true);
1485     x_set_i3_atoms();
1486     /* Send an IPC event just in case the ws names have changed */
1487     ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"reload\"}");
1488
1489     // XXX: default reply for now, make this a better reply
1490     ysuccess(true);
1491 }
1492
1493 /*
1494  * Implementaiton of 'restart'.
1495  *
1496  */
1497 void cmd_restart(I3_CMD) {
1498     LOG("restarting i3\n");
1499     i3_restart(false);
1500
1501     // XXX: default reply for now, make this a better reply
1502     ysuccess(true);
1503 }
1504
1505 /*
1506  * Implementaiton of 'open'.
1507  *
1508  */
1509 void cmd_open(I3_CMD) {
1510     LOG("opening new container\n");
1511     Con *con = tree_open_con(NULL, NULL);
1512     con->layout = L_SPLITH;
1513     con_focus(con);
1514
1515     y(map_open);
1516     ystr("success");
1517     y(bool, true);
1518     ystr("id");
1519     y(integer, (long int)con);
1520     y(map_close);
1521
1522     cmd_output->needs_tree_render = true;
1523 }
1524
1525 /*
1526  * Implementation of 'focus output <output>'.
1527  *
1528  */
1529 void cmd_focus_output(I3_CMD, char *name) {
1530     owindow *current;
1531
1532     DLOG("name = %s\n", name);
1533
1534     HANDLE_EMPTY_MATCH;
1535
1536     /* get the output */
1537     Output *current_output = NULL;
1538     Output *output;
1539
1540     TAILQ_FOREACH(current, &owindows, owindows)
1541         current_output = get_output_containing(current->con->rect.x, current->con->rect.y);
1542     assert(current_output != NULL);
1543
1544     output = get_output_from_string(current_output, name);
1545
1546     if (!output) {
1547         LOG("No such output found.\n");
1548         ysuccess(false);
1549         return;
1550     }
1551
1552     /* get visible workspace on output */
1553     Con *ws = NULL;
1554     GREP_FIRST(ws, output_get_content(output->con), workspace_is_visible(child));
1555     if (!ws) {
1556         ysuccess(false);
1557         return;
1558     }
1559
1560     workspace_show(ws);
1561
1562     cmd_output->needs_tree_render = true;
1563     // XXX: default reply for now, make this a better reply
1564     ysuccess(true);
1565 }
1566
1567 /*
1568  * Implementation of 'move [window|container] [to] [absolute] position <px> [px] <px> [px]
1569  *
1570  */
1571 void cmd_move_window_to_position(I3_CMD, char *method, char *cx, char *cy) {
1572
1573     int x = atoi(cx);
1574     int y = atoi(cy);
1575
1576     if (!con_is_floating(focused)) {
1577         ELOG("Cannot change position. The window/container is not floating\n");
1578         y(map_open);
1579         ystr("success");
1580         y(bool, false);
1581         ystr("error");
1582         ystr("Cannot change position. The window/container is not floating.");
1583         y(map_close);
1584         return;
1585     }
1586
1587     if (strcmp(method, "absolute") == 0) {
1588         focused->parent->rect.x = x;
1589         focused->parent->rect.y = y;
1590
1591         DLOG("moving to absolute position %d %d\n", x, y);
1592         floating_maybe_reassign_ws(focused->parent);
1593         cmd_output->needs_tree_render = true;
1594     }
1595
1596     if (strcmp(method, "position") == 0) {
1597         Rect newrect = focused->parent->rect;
1598
1599         DLOG("moving to position %d %d\n", x, y);
1600         newrect.x = x;
1601         newrect.y = y;
1602
1603         floating_reposition(focused->parent, newrect);
1604     }
1605
1606     // XXX: default reply for now, make this a better reply
1607     ysuccess(true);
1608 }
1609
1610 /*
1611  * Implementation of 'move [window|container] [to] [absolute] position center
1612  *
1613  */
1614 void cmd_move_window_to_center(I3_CMD, char *method) {
1615
1616     if (!con_is_floating(focused)) {
1617         ELOG("Cannot change position. The window/container is not floating\n");
1618         y(map_open);
1619         ystr("success");
1620         y(bool, false);
1621         ystr("error");
1622         ystr("Cannot change position. The window/container is not floating.");
1623         y(map_close);
1624     }
1625
1626     if (strcmp(method, "absolute") == 0) {
1627         Rect *rect = &focused->parent->rect;
1628
1629         DLOG("moving to absolute center\n");
1630         rect->x = croot->rect.width/2 - rect->width/2;
1631         rect->y = croot->rect.height/2 - rect->height/2;
1632
1633         floating_maybe_reassign_ws(focused->parent);
1634         cmd_output->needs_tree_render = true;
1635     }
1636
1637     if (strcmp(method, "position") == 0) {
1638         Rect *wsrect = &con_get_workspace(focused)->rect;
1639         Rect newrect = focused->parent->rect;
1640
1641         DLOG("moving to center\n");
1642         newrect.x = wsrect->width/2 - newrect.width/2;
1643         newrect.y = wsrect->height/2 - newrect.height/2;
1644
1645         floating_reposition(focused->parent, newrect);
1646     }
1647
1648     // XXX: default reply for now, make this a better reply
1649     ysuccess(true);
1650 }
1651
1652 /*
1653  * Implementation of 'move scratchpad'.
1654  *
1655  */
1656 void cmd_move_scratchpad(I3_CMD) {
1657     DLOG("should move window to scratchpad\n");
1658     owindow *current;
1659
1660     HANDLE_EMPTY_MATCH;
1661
1662     TAILQ_FOREACH(current, &owindows, owindows) {
1663         DLOG("matching: %p / %s\n", current->con, current->con->name);
1664         scratchpad_move(current->con);
1665     }
1666
1667     cmd_output->needs_tree_render = true;
1668     // XXX: default reply for now, make this a better reply
1669     ysuccess(true);
1670 }
1671
1672 /*
1673  * Implementation of 'scratchpad show'.
1674  *
1675  */
1676 void cmd_scratchpad_show(I3_CMD) {
1677     DLOG("should show scratchpad window\n");
1678     owindow *current;
1679
1680     if (match_is_empty(current_match)) {
1681         scratchpad_show(NULL);
1682     } else {
1683         TAILQ_FOREACH(current, &owindows, owindows) {
1684             DLOG("matching: %p / %s\n", current->con, current->con->name);
1685             scratchpad_show(current->con);
1686         }
1687     }
1688
1689     cmd_output->needs_tree_render = true;
1690     // XXX: default reply for now, make this a better reply
1691     ysuccess(true);
1692 }
1693
1694 /*
1695  * Implementation of 'rename workspace <name> to <name>'
1696  *
1697  */
1698 void cmd_rename_workspace(I3_CMD, char *old_name, char *new_name) {
1699     LOG("Renaming workspace \"%s\" to \"%s\"\n", old_name, new_name);
1700
1701     Con *output, *workspace = NULL;
1702     TAILQ_FOREACH(output, &(croot->nodes_head), nodes)
1703         GREP_FIRST(workspace, output_get_content(output),
1704             !strcasecmp(child->name, old_name));
1705
1706     if (!workspace) {
1707         // TODO: we should include the old workspace name here and use yajl for
1708         // generating the reply.
1709         y(map_open);
1710         ystr("success");
1711         y(bool, false);
1712         ystr("error");
1713         // TODO: better error message
1714         ystr("Old workspace not found");
1715         y(map_close);
1716         return;
1717     }
1718
1719     Con *check_dest = NULL;
1720     TAILQ_FOREACH(output, &(croot->nodes_head), nodes)
1721         GREP_FIRST(check_dest, output_get_content(output),
1722             !strcasecmp(child->name, new_name));
1723
1724     if (check_dest != NULL) {
1725         // TODO: we should include the new workspace name here and use yajl for
1726         // generating the reply.
1727         y(map_open);
1728         ystr("success");
1729         y(bool, false);
1730         ystr("error");
1731         // TODO: better error message
1732         ystr("New workspace already exists");
1733         y(map_close);
1734         return;
1735     }
1736
1737     /* Change the name and try to parse it as a number. */
1738     FREE(workspace->name);
1739     workspace->name = sstrdup(new_name);
1740     char *endptr = NULL;
1741     long parsed_num = strtol(new_name, &endptr, 10);
1742     if (parsed_num == LONG_MIN ||
1743         parsed_num == LONG_MAX ||
1744         parsed_num < 0 ||
1745         endptr == new_name)
1746         workspace->num = -1;
1747     else workspace->num = parsed_num;
1748     LOG("num = %d\n", workspace->num);
1749
1750     /* By re-attaching, the sort order will be correct afterwards. */
1751     Con *previously_focused = focused;
1752     Con *parent = workspace->parent;
1753     con_detach(workspace);
1754     con_attach(workspace, parent, false);
1755     /* Restore the previous focus since con_attach messes with the focus. */
1756     con_focus(previously_focused);
1757
1758     cmd_output->needs_tree_render = true;
1759     ysuccess(true);
1760
1761     ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"rename\"}");
1762 }