]> git.sur5r.net Git - i3/i3/blob - src/commands.c
Refactor the interface of commands.c
[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 /** When the command did not include match criteria (!), we use the currently
16  * focused command. Do not confuse this case with a command which included
17  * criteria but which did not match any windows. This macro has to be called in
18  * every command.
19  */
20 #define HANDLE_EMPTY_MATCH do { \
21     if (match_is_empty(current_match)) { \
22         owindow *ow = smalloc(sizeof(owindow)); \
23         ow->con = focused; \
24         TAILQ_INIT(&owindows); \
25         TAILQ_INSERT_TAIL(&owindows, ow, owindows); \
26     } \
27 } while (0)
28
29 static owindows_head owindows;
30
31 /*
32  * Returns true if a is definitely greater than b (using the given epsilon)
33  *
34  */
35 static bool definitelyGreaterThan(float a, float b, float epsilon) {
36     return (a - b) > ( (fabs(a) < fabs(b) ? fabs(b) : fabs(a)) * epsilon);
37 }
38
39 /*
40  * Returns an 'output' corresponding to one of left/right/down/up or a specific
41  * output name.
42  *
43  */
44 static Output *get_output_from_string(Output *current_output, const char *output_str) {
45     Output *output;
46
47     if (strcasecmp(output_str, "left") == 0) {
48         output = get_output_next(D_LEFT, current_output);
49         if (!output)
50             output = get_output_most(D_RIGHT, current_output);
51     } else if (strcasecmp(output_str, "right") == 0) {
52         output = get_output_next(D_RIGHT, current_output);
53         if (!output)
54             output = get_output_most(D_LEFT, current_output);
55     } else if (strcasecmp(output_str, "up") == 0) {
56         output = get_output_next(D_UP, current_output);
57         if (!output)
58             output = get_output_most(D_DOWN, current_output);
59     } else if (strcasecmp(output_str, "down") == 0) {
60         output = get_output_next(D_DOWN, current_output);
61         if (!output)
62             output = get_output_most(D_UP, current_output);
63     } else output = get_output_by_name(output_str);
64
65     return output;
66 }
67
68 // This code is commented out because we might recycle it for popping up error
69 // messages on parser errors.
70 #if 0
71 static pid_t migration_pid = -1;
72
73 /*
74  * Handler which will be called when we get a SIGCHLD for the nagbar, meaning
75  * it exited (or could not be started, depending on the exit code).
76  *
77  */
78 static void nagbar_exited(EV_P_ ev_child *watcher, int revents) {
79     ev_child_stop(EV_A_ watcher);
80     if (!WIFEXITED(watcher->rstatus)) {
81         fprintf(stderr, "ERROR: i3-nagbar did not exit normally.\n");
82         return;
83     }
84
85     int exitcode = WEXITSTATUS(watcher->rstatus);
86     printf("i3-nagbar process exited with status %d\n", exitcode);
87     if (exitcode == 2) {
88         fprintf(stderr, "ERROR: i3-nagbar could not be found. Is it correctly installed on your system?\n");
89     }
90
91     migration_pid = -1;
92 }
93
94 /* We need ev >= 4 for the following code. Since it is not *that* important (it
95  * only makes sure that there are no i3-nagbar instances left behind) we still
96  * support old systems with libev 3. */
97 #if EV_VERSION_MAJOR >= 4
98 /*
99  * Cleanup handler. Will be called when i3 exits. Kills i3-nagbar with signal
100  * SIGKILL (9) to make sure there are no left-over i3-nagbar processes.
101  *
102  */
103 static void nagbar_cleanup(EV_P_ ev_cleanup *watcher, int revent) {
104     if (migration_pid != -1) {
105         LOG("Sending SIGKILL (9) to i3-nagbar with PID %d\n", migration_pid);
106         kill(migration_pid, SIGKILL);
107     }
108 }
109 #endif
110
111 void cmd_MIGRATION_start_nagbar() {
112     if (migration_pid != -1) {
113         fprintf(stderr, "i3-nagbar already running.\n");
114         return;
115     }
116     fprintf(stderr, "Starting i3-nagbar, command parsing differs from expected output.\n");
117     ELOG("Please report this on IRC or in the bugtracker. Make sure to include the full debug level logfile:\n");
118     ELOG("i3-dump-log | gzip -9c > /tmp/i3.log.gz\n");
119     ELOG("FYI: Your i3 version is " I3_VERSION "\n");
120     migration_pid = fork();
121     if (migration_pid == -1) {
122         warn("Could not fork()");
123         return;
124     }
125
126     /* child */
127     if (migration_pid == 0) {
128         char *pageraction;
129         sasprintf(&pageraction, "i3-sensible-terminal -e i3-sensible-pager \"%s\"", errorfilename);
130         char *argv[] = {
131             NULL, /* will be replaced by the executable path */
132             "-t",
133             "error",
134             "-m",
135             "You found a parsing error. Please, please, please, report it!",
136             "-b",
137             "show errors",
138             pageraction,
139             NULL
140         };
141         exec_i3_utility("i3-nagbar", argv);
142     }
143
144     /* parent */
145     /* install a child watcher */
146     ev_child *child = smalloc(sizeof(ev_child));
147     ev_child_init(child, &nagbar_exited, migration_pid, 0);
148     ev_child_start(main_loop, child);
149
150 /* We need ev >= 4 for the following code. Since it is not *that* important (it
151  * only makes sure that there are no i3-nagbar instances left behind) we still
152  * support old systems with libev 3. */
153 #if EV_VERSION_MAJOR >= 4
154     /* install a cleanup watcher (will be called when i3 exits and i3-nagbar is
155      * still running) */
156     ev_cleanup *cleanup = smalloc(sizeof(ev_cleanup));
157     ev_cleanup_init(cleanup, nagbar_cleanup);
158     ev_cleanup_start(main_loop, cleanup);
159 #endif
160 }
161
162 #endif
163
164 /*******************************************************************************
165  * Criteria functions.
166  ******************************************************************************/
167
168 /*
169  * Initializes the specified 'Match' data structure and the initial state of
170  * commands.c for matching target windows of a command.
171  *
172  */
173 void cmd_criteria_init(I3_CMD) {
174     Con *con;
175     owindow *ow;
176
177     DLOG("Initializing criteria, current_match = %p\n", current_match);
178     match_init(current_match);
179     while (!TAILQ_EMPTY(&owindows)) {
180         ow = TAILQ_FIRST(&owindows);
181         TAILQ_REMOVE(&owindows, ow, owindows);
182         free(ow);
183     }
184     TAILQ_INIT(&owindows);
185     /* copy all_cons */
186     TAILQ_FOREACH(con, &all_cons, all_cons) {
187         ow = smalloc(sizeof(owindow));
188         ow->con = con;
189         TAILQ_INSERT_TAIL(&owindows, ow, owindows);
190     }
191 }
192
193 /*
194  * A match specification just finished (the closing square bracket was found),
195  * so we filter the list of owindows.
196  *
197  */
198 void cmd_criteria_match_windows(I3_CMD) {
199     owindow *next, *current;
200
201     DLOG("match specification finished, matching...\n");
202     /* copy the old list head to iterate through it and start with a fresh
203      * list which will contain only matching windows */
204     struct owindows_head old = owindows;
205     TAILQ_INIT(&owindows);
206     for (next = TAILQ_FIRST(&old); next != TAILQ_END(&old);) {
207         /* make a copy of the next pointer and advance the pointer to the
208          * next element as we are going to invalidate the element’s
209          * next/prev pointers by calling TAILQ_INSERT_TAIL later */
210         current = next;
211         next = TAILQ_NEXT(next, owindows);
212
213         DLOG("checking if con %p / %s matches\n", current->con, current->con->name);
214         if (current_match->con_id != NULL) {
215             if (current_match->con_id == current->con) {
216                 DLOG("matches container!\n");
217                 TAILQ_INSERT_TAIL(&owindows, current, owindows);
218             }
219         } else if (current_match->mark != NULL && current->con->mark != NULL &&
220                    regex_matches(current_match->mark, current->con->mark)) {
221             DLOG("match by mark\n");
222             TAILQ_INSERT_TAIL(&owindows, current, owindows);
223         } else {
224             if (current->con->window == NULL)
225                 continue;
226             if (match_matches_window(current_match, current->con->window)) {
227                 DLOG("matches window!\n");
228                 TAILQ_INSERT_TAIL(&owindows, current, owindows);
229             } else {
230                 DLOG("doesnt match\n");
231                 free(current);
232             }
233         }
234     }
235
236     TAILQ_FOREACH(current, &owindows, owindows) {
237         DLOG("matching: %p / %s\n", current->con, current->con->name);
238     }
239 }
240
241 /*
242  * Interprets a ctype=cvalue pair and adds it to the current match
243  * specification.
244  *
245  */
246 void cmd_criteria_add(I3_CMD, char *ctype, char *cvalue) {
247     DLOG("ctype=*%s*, cvalue=*%s*\n", ctype, cvalue);
248
249     if (strcmp(ctype, "class") == 0) {
250         current_match->class = regex_new(cvalue);
251         return;
252     }
253
254     if (strcmp(ctype, "instance") == 0) {
255         current_match->instance = regex_new(cvalue);
256         return;
257     }
258
259     if (strcmp(ctype, "window_role") == 0) {
260         current_match->role = regex_new(cvalue);
261         return;
262     }
263
264     if (strcmp(ctype, "con_id") == 0) {
265         char *end;
266         long parsed = strtol(cvalue, &end, 10);
267         if (parsed == LONG_MIN ||
268             parsed == LONG_MAX ||
269             parsed < 0 ||
270             (end && *end != '\0')) {
271             ELOG("Could not parse con id \"%s\"\n", cvalue);
272         } else {
273             current_match->con_id = (Con*)parsed;
274             printf("id as int = %p\n", current_match->con_id);
275         }
276         return;
277     }
278
279     if (strcmp(ctype, "id") == 0) {
280         char *end;
281         long parsed = strtol(cvalue, &end, 10);
282         if (parsed == LONG_MIN ||
283             parsed == LONG_MAX ||
284             parsed < 0 ||
285             (end && *end != '\0')) {
286             ELOG("Could not parse window id \"%s\"\n", cvalue);
287         } else {
288             current_match->id = parsed;
289             printf("window id as int = %d\n", current_match->id);
290         }
291         return;
292     }
293
294     if (strcmp(ctype, "con_mark") == 0) {
295         current_match->mark = regex_new(cvalue);
296         return;
297     }
298
299     if (strcmp(ctype, "title") == 0) {
300         current_match->title = regex_new(cvalue);
301         return;
302     }
303
304     if (strcmp(ctype, "urgent") == 0) {
305         if (strcasecmp(cvalue, "latest") == 0 ||
306             strcasecmp(cvalue, "newest") == 0 ||
307             strcasecmp(cvalue, "recent") == 0 ||
308             strcasecmp(cvalue, "last") == 0) {
309             current_match->urgent = U_LATEST;
310         } else if (strcasecmp(cvalue, "oldest") == 0 ||
311                    strcasecmp(cvalue, "first") == 0) {
312             current_match->urgent = U_OLDEST;
313         }
314         return;
315     }
316
317     ELOG("Unknown criterion: %s\n", ctype);
318 }
319
320 /*
321  * Implementation of 'move [window|container] [to] workspace
322  * next|prev|next_on_output|prev_on_output'.
323  *
324  */
325 void cmd_move_con_to_workspace(I3_CMD, char *which) {
326     owindow *current;
327
328     DLOG("which=%s\n", which);
329
330     HANDLE_EMPTY_MATCH;
331
332     /* get the workspace */
333     Con *ws;
334     if (strcmp(which, "next") == 0)
335         ws = workspace_next();
336     else if (strcmp(which, "prev") == 0)
337         ws = workspace_prev();
338     else if (strcmp(which, "next_on_output") == 0)
339         ws = workspace_next_on_output();
340     else if (strcmp(which, "prev_on_output") == 0)
341         ws = workspace_prev_on_output();
342     else {
343         ELOG("BUG: called with which=%s\n", which);
344         cmd_output->json_output = sstrdup("{\"sucess\": false}");
345         return;
346     }
347
348     TAILQ_FOREACH(current, &owindows, owindows) {
349         DLOG("matching: %p / %s\n", current->con, current->con->name);
350         con_move_to_workspace(current->con, ws, true, false);
351     }
352
353     cmd_output->needs_tree_render = true;
354     // XXX: default reply for now, make this a better reply
355     cmd_output->json_output = sstrdup("{\"success\": true}");
356 }
357
358 /*
359  * Implementation of 'move [window|container] [to] workspace <name>'.
360  *
361  */
362 void cmd_move_con_to_workspace_name(I3_CMD, char *name) {
363     if (strncasecmp(name, "__i3_", strlen("__i3_")) == 0) {
364         LOG("You cannot switch to the i3 internal workspaces.\n");
365         cmd_output->json_output = sstrdup("{\"sucess\": false}");
366         return;
367     }
368
369     owindow *current;
370
371     /* Error out early to not create a non-existing workspace (in
372      * workspace_get()) if we are not actually able to move anything. */
373     if (match_is_empty(current_match) && focused->type == CT_WORKSPACE) {
374         cmd_output->json_output = sstrdup("{\"sucess\": false}");
375         return;
376     }
377
378     LOG("should move window to workspace %s\n", name);
379     /* get the workspace */
380     Con *ws = workspace_get(name, NULL);
381
382     HANDLE_EMPTY_MATCH;
383
384     TAILQ_FOREACH(current, &owindows, owindows) {
385         DLOG("matching: %p / %s\n", current->con, current->con->name);
386         con_move_to_workspace(current->con, ws, true, false);
387     }
388
389     cmd_output->needs_tree_render = true;
390     // XXX: default reply for now, make this a better reply
391     cmd_output->json_output = sstrdup("{\"success\": true}");
392 }
393
394 /*
395  * Implementation of 'resize grow|shrink <direction> [<px> px] [or <ppt> ppt]'.
396  *
397  */
398 void cmd_resize(I3_CMD, char *way, char *direction, char *resize_px, char *resize_ppt) {
399     /* resize <grow|shrink> <direction> [<px> px] [or <ppt> ppt] */
400     DLOG("resizing in way %s, direction %s, px %s or ppt %s\n", way, direction, resize_px, resize_ppt);
401     // 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
402     int px = atoi(resize_px);
403     int ppt = atoi(resize_ppt);
404     if (strcmp(way, "shrink") == 0) {
405         px *= -1;
406         ppt *= -1;
407     }
408
409     Con *floating_con;
410     if ((floating_con = con_inside_floating(focused))) {
411         printf("floating resize\n");
412         if (strcmp(direction, "up") == 0) {
413             floating_con->rect.y -= px;
414             floating_con->rect.height += px;
415         } else if (strcmp(direction, "down") == 0) {
416             floating_con->rect.height += px;
417         } else if (strcmp(direction, "left") == 0) {
418             floating_con->rect.x -= px;
419             floating_con->rect.width += px;
420         } else {
421             floating_con->rect.width += px;
422         }
423     } else {
424         LOG("tiling resize\n");
425         /* get the appropriate current container (skip stacked/tabbed cons) */
426         Con *current = focused;
427         while (current->parent->layout == L_STACKED ||
428                current->parent->layout == L_TABBED)
429             current = current->parent;
430
431         /* Then further go up until we find one with the matching orientation. */
432         orientation_t search_orientation =
433             (strcmp(direction, "left") == 0 || strcmp(direction, "right") == 0 ? HORIZ : VERT);
434
435         while (current->type != CT_WORKSPACE &&
436                current->type != CT_FLOATING_CON &&
437                current->parent->orientation != search_orientation)
438             current = current->parent;
439
440         /* get the default percentage */
441         int children = con_num_children(current->parent);
442         Con *other;
443         LOG("ins. %d children\n", children);
444         double percentage = 1.0 / children;
445         LOG("default percentage = %f\n", percentage);
446
447         orientation_t orientation = current->parent->orientation;
448
449         if ((orientation == HORIZ &&
450              (strcmp(direction, "up") == 0 || strcmp(direction, "down") == 0)) ||
451             (orientation == VERT &&
452              (strcmp(direction, "left") == 0 || strcmp(direction, "right") == 0))) {
453             LOG("You cannot resize in that direction. Your focus is in a %s split container currently.\n",
454                 (orientation == HORIZ ? "horizontal" : "vertical"));
455             cmd_output->json_output = sstrdup("{\"sucess\": false}");
456             return;
457         }
458
459         if (strcmp(direction, "up") == 0 || strcmp(direction, "left") == 0) {
460             other = TAILQ_PREV(current, nodes_head, nodes);
461         } else {
462             other = TAILQ_NEXT(current, nodes);
463         }
464         if (other == TAILQ_END(workspaces)) {
465             LOG("No other container in this direction found, cannot resize.\n");
466             cmd_output->json_output = sstrdup("{\"sucess\": false}");
467             return;
468         }
469         LOG("other->percent = %f\n", other->percent);
470         LOG("current->percent before = %f\n", current->percent);
471         if (current->percent == 0.0)
472             current->percent = percentage;
473         if (other->percent == 0.0)
474             other->percent = percentage;
475         double new_current_percent = current->percent + ((double)ppt / 100.0);
476         double new_other_percent = other->percent - ((double)ppt / 100.0);
477         LOG("new_current_percent = %f\n", new_current_percent);
478         LOG("new_other_percent = %f\n", new_other_percent);
479         /* Ensure that the new percentages are positive and greater than
480          * 0.05 to have a reasonable minimum size. */
481         if (definitelyGreaterThan(new_current_percent, 0.05, DBL_EPSILON) &&
482             definitelyGreaterThan(new_other_percent, 0.05, DBL_EPSILON)) {
483             current->percent += ((double)ppt / 100.0);
484             other->percent -= ((double)ppt / 100.0);
485             LOG("current->percent after = %f\n", current->percent);
486             LOG("other->percent after = %f\n", other->percent);
487         } else {
488             LOG("Not resizing, already at minimum size\n");
489         }
490     }
491
492     cmd_output->needs_tree_render = true;
493     // XXX: default reply for now, make this a better reply
494     cmd_output->json_output = sstrdup("{\"success\": true}");
495 }
496
497 /*
498  * Implementation of 'border normal|none|1pixel|toggle'.
499  *
500  */
501 void cmd_border(I3_CMD, char *border_style_str) {
502     DLOG("border style should be changed to %s\n", border_style_str);
503     owindow *current;
504
505     HANDLE_EMPTY_MATCH;
506
507     TAILQ_FOREACH(current, &owindows, owindows) {
508         DLOG("matching: %p / %s\n", current->con, current->con->name);
509         int border_style = current->con->border_style;
510         if (strcmp(border_style_str, "toggle") == 0) {
511             border_style++;
512             border_style %= 3;
513         } else {
514             if (strcmp(border_style_str, "normal") == 0)
515                 border_style = BS_NORMAL;
516             else if (strcmp(border_style_str, "none") == 0)
517                 border_style = BS_NONE;
518             else if (strcmp(border_style_str, "1pixel") == 0)
519                 border_style = BS_1PIXEL;
520             else {
521                 ELOG("BUG: called with border_style=%s\n", border_style_str);
522                 cmd_output->json_output = sstrdup("{\"sucess\": false}");
523                 return;
524             }
525         }
526         con_set_border_style(current->con, border_style);
527     }
528
529     cmd_output->needs_tree_render = true;
530     // XXX: default reply for now, make this a better reply
531     cmd_output->json_output = sstrdup("{\"success\": true}");
532 }
533
534 /*
535  * Implementation of 'nop <comment>'.
536  *
537  */
538 void cmd_nop(I3_CMD, char *comment) {
539     LOG("-------------------------------------------------\n");
540     LOG("  NOP: %s\n", comment);
541     LOG("-------------------------------------------------\n");
542 }
543
544 /*
545  * Implementation of 'append_layout <path>'.
546  *
547  */
548 void cmd_append_layout(I3_CMD, char *path) {
549     LOG("Appending layout \"%s\"\n", path);
550     tree_append_json(path);
551
552     cmd_output->needs_tree_render = true;
553     // XXX: default reply for now, make this a better reply
554     cmd_output->json_output = sstrdup("{\"success\": true}");
555 }
556
557 /*
558  * Implementation of 'workspace next|prev|next_on_output|prev_on_output'.
559  *
560  */
561 void cmd_workspace(I3_CMD, char *which) {
562     Con *ws;
563
564     DLOG("which=%s\n", which);
565
566     if (strcmp(which, "next") == 0)
567         ws = workspace_next();
568     else if (strcmp(which, "prev") == 0)
569         ws = workspace_prev();
570     else if (strcmp(which, "next_on_output") == 0)
571         ws = workspace_next_on_output();
572     else if (strcmp(which, "prev_on_output") == 0)
573         ws = workspace_prev_on_output();
574     else {
575         ELOG("BUG: called with which=%s\n", which);
576         cmd_output->json_output = sstrdup("{\"sucess\": false}");
577         return;
578     }
579
580     workspace_show(ws);
581
582     cmd_output->needs_tree_render = true;
583     // XXX: default reply for now, make this a better reply
584     cmd_output->json_output = sstrdup("{\"success\": true}");
585 }
586
587 /*
588  * Implementation of 'workspace back_and_forth'.
589  *
590  */
591 void cmd_workspace_back_and_forth(I3_CMD) {
592     workspace_back_and_forth();
593
594     cmd_output->needs_tree_render = true;
595     // XXX: default reply for now, make this a better reply
596     cmd_output->json_output = sstrdup("{\"success\": true}");
597 }
598
599 /*
600  * Implementation of 'workspace <name>'
601  *
602  */
603 void cmd_workspace_name(I3_CMD, char *name) {
604     if (strncasecmp(name, "__i3_", strlen("__i3_")) == 0) {
605         LOG("You cannot switch to the i3 internal workspaces.\n");
606         cmd_output->json_output = sstrdup("{\"sucess\": false}");
607         return;
608     }
609
610     DLOG("should switch to workspace %s\n", name);
611
612     Con *ws = con_get_workspace(focused);
613
614     /* Check if the command wants to switch to the current workspace */
615     if (strcmp(ws->name, name) == 0) {
616         DLOG("This workspace is already focused.\n");
617         if (config.workspace_auto_back_and_forth) {
618             workspace_back_and_forth();
619             tree_render();
620         }
621         cmd_output->json_output = sstrdup("{\"sucess\": false}");
622         return;
623     }
624
625     workspace_show_by_name(name);
626
627     cmd_output->needs_tree_render = true;
628     // XXX: default reply for now, make this a better reply
629     cmd_output->json_output = sstrdup("{\"success\": true}");
630 }
631
632 /*
633  * Implementation of 'mark <mark>'
634  *
635  */
636 void cmd_mark(I3_CMD, char *mark) {
637     DLOG("Clearing all windows which have that mark first\n");
638
639     Con *con;
640     TAILQ_FOREACH(con, &all_cons, all_cons) {
641         if (con->mark && strcmp(con->mark, mark) == 0)
642             FREE(con->mark);
643     }
644
645     DLOG("marking window with str %s\n", mark);
646     owindow *current;
647
648     HANDLE_EMPTY_MATCH;
649
650     TAILQ_FOREACH(current, &owindows, owindows) {
651         DLOG("matching: %p / %s\n", current->con, current->con->name);
652         current->con->mark = sstrdup(mark);
653     }
654
655     cmd_output->needs_tree_render = true;
656     // XXX: default reply for now, make this a better reply
657     cmd_output->json_output = sstrdup("{\"success\": true}");
658 }
659
660 /*
661  * Implementation of 'mode <string>'.
662  *
663  */
664 void cmd_mode(I3_CMD, char *mode) {
665     DLOG("mode=%s\n", mode);
666     switch_mode(mode);
667
668     // XXX: default reply for now, make this a better reply
669     cmd_output->json_output = sstrdup("{\"success\": true}");
670 }
671
672 /*
673  * Implementation of 'move [window|container] [to] output <str>'.
674  *
675  */
676 void cmd_move_con_to_output(I3_CMD, char *name) {
677     owindow *current;
678
679     DLOG("should move window to output %s\n", name);
680
681     HANDLE_EMPTY_MATCH;
682
683     /* get the output */
684     Output *current_output = NULL;
685     Output *output;
686
687     // TODO: fix the handling of criteria
688     TAILQ_FOREACH(current, &owindows, owindows)
689         current_output = get_output_containing(current->con->rect.x, current->con->rect.y);
690
691     assert(current_output != NULL);
692
693     // TODO: clean this up with commands.spec as soon as we switched away from the lex/yacc command parser
694     if (strcasecmp(name, "up") == 0)
695         output = get_output_next(D_UP, current_output);
696     else if (strcasecmp(name, "down") == 0)
697         output = get_output_next(D_DOWN, current_output);
698     else if (strcasecmp(name, "left") == 0)
699         output = get_output_next(D_LEFT, current_output);
700     else if (strcasecmp(name, "right") == 0)
701         output = get_output_next(D_RIGHT, current_output);
702     else
703         output = get_output_by_name(name);
704
705     if (!output) {
706         LOG("No such output found.\n");
707         cmd_output->json_output = sstrdup("{\"sucess\": false}");
708         return;
709     }
710
711     /* get visible workspace on output */
712     Con *ws = NULL;
713     GREP_FIRST(ws, output_get_content(output->con), workspace_is_visible(child));
714     if (!ws) {
715         cmd_output->json_output = sstrdup("{\"sucess\": false}");
716         return;
717     }
718
719     TAILQ_FOREACH(current, &owindows, owindows) {
720         DLOG("matching: %p / %s\n", current->con, current->con->name);
721         con_move_to_workspace(current->con, ws, true, false);
722     }
723
724     cmd_output->needs_tree_render = true;
725     // XXX: default reply for now, make this a better reply
726     cmd_output->json_output = sstrdup("{\"success\": true}");
727 }
728
729 /*
730  * Implementation of 'floating enable|disable|toggle'
731  *
732  */
733 void cmd_floating(I3_CMD, char *floating_mode) {
734     owindow *current;
735
736     DLOG("floating_mode=%s\n", floating_mode);
737
738     HANDLE_EMPTY_MATCH;
739
740     TAILQ_FOREACH(current, &owindows, owindows) {
741         DLOG("matching: %p / %s\n", current->con, current->con->name);
742         if (strcmp(floating_mode, "toggle") == 0) {
743             DLOG("should toggle mode\n");
744             toggle_floating_mode(current->con, false);
745         } else {
746             DLOG("should switch mode to %s\n", floating_mode);
747             if (strcmp(floating_mode, "enable") == 0) {
748                 floating_enable(current->con, false);
749             } else {
750                 floating_disable(current->con, false);
751             }
752         }
753     }
754
755     cmd_output->needs_tree_render = true;
756     // XXX: default reply for now, make this a better reply
757     cmd_output->json_output = sstrdup("{\"success\": true}");
758 }
759
760 /*
761  * Implementation of 'move workspace to [output] <str>'.
762  *
763  */
764 void cmd_move_workspace_to_output(I3_CMD, char *name) {
765     DLOG("should move workspace to output %s\n", name);
766
767     HANDLE_EMPTY_MATCH;
768
769     owindow *current;
770     TAILQ_FOREACH(current, &owindows, owindows) {
771         Output *current_output = get_output_containing(current->con->rect.x,
772                                                        current->con->rect.y);
773         Output *output = get_output_from_string(current_output, name);
774         if (!output) {
775             LOG("No such output\n");
776             cmd_output->json_output = sstrdup("{\"sucess\": false}");
777             return;
778         }
779
780         Con *content = output_get_content(output->con);
781         LOG("got output %p with content %p\n", output, content);
782
783         Con *ws = con_get_workspace(current->con);
784         LOG("should move workspace %p / %s\n", ws, ws->name);
785
786         if (con_num_children(ws->parent) == 1) {
787             LOG("Creating a new workspace to replace \"%s\" (last on its output).\n", ws->name);
788
789             /* check if we can find a workspace assigned to this output */
790             bool used_assignment = false;
791             struct Workspace_Assignment *assignment;
792             TAILQ_FOREACH(assignment, &ws_assignments, ws_assignments) {
793                 if (strcmp(assignment->output, current_output->name) != 0)
794                     continue;
795
796                 /* check if this workspace is already attached to the tree */
797                 Con *workspace = NULL, *out;
798                 TAILQ_FOREACH(out, &(croot->nodes_head), nodes)
799                     GREP_FIRST(workspace, output_get_content(out),
800                                !strcasecmp(child->name, assignment->name));
801                 if (workspace != NULL)
802                     continue;
803
804                 /* so create the workspace referenced to by this assignment */
805                 LOG("Creating workspace from assignment %s.\n", assignment->name);
806                 workspace_get(assignment->name, NULL);
807                 used_assignment = true;
808                 break;
809             }
810
811             /* if we couldn't create the workspace using an assignment, create
812              * it on the output */
813             if (!used_assignment)
814                 create_workspace_on_output(current_output, ws->parent);
815
816             /* notify the IPC listeners */
817             ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"init\"}");
818         }
819
820         /* detach from the old output and attach to the new output */
821         bool workspace_was_visible = workspace_is_visible(ws);
822         Con *old_content = ws->parent;
823         con_detach(ws);
824         if (workspace_was_visible) {
825             /* The workspace which we just detached was visible, so focus
826              * the next one in the focus-stack. */
827             Con *focus_ws = TAILQ_FIRST(&(old_content->focus_head));
828             LOG("workspace was visible, focusing %p / %s now\n", focus_ws, focus_ws->name);
829             workspace_show(focus_ws);
830         }
831         con_attach(ws, content, false);
832
833         /* fix the coordinates of the floating containers */
834         Con *floating_con;
835         TAILQ_FOREACH(floating_con, &(ws->floating_head), floating_windows)
836             floating_fix_coordinates(floating_con, &(old_content->rect), &(content->rect));
837
838         ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"move\"}");
839         if (workspace_was_visible) {
840             /* Focus the moved workspace on the destination output. */
841             workspace_show(ws);
842         }
843     }
844
845     cmd_output->needs_tree_render = true;
846     // XXX: default reply for now, make this a better reply
847     cmd_output->json_output = sstrdup("{\"success\": true}");
848 }
849
850 /*
851  * Implementation of 'split v|h|vertical|horizontal'.
852  *
853  */
854 void cmd_split(I3_CMD, char *direction) {
855     /* TODO: use matches */
856     LOG("splitting in direction %c\n", direction[0]);
857     tree_split(focused, (direction[0] == 'v' ? VERT : HORIZ));
858
859     cmd_output->needs_tree_render = true;
860     // XXX: default reply for now, make this a better reply
861     cmd_output->json_output = sstrdup("{\"success\": true}");
862 }
863
864 /*
865  * Implementaiton of 'kill [window|client]'.
866  *
867  */
868 void cmd_kill(I3_CMD, char *kill_mode_str) {
869     if (kill_mode_str == NULL)
870         kill_mode_str = "window";
871     owindow *current;
872
873     DLOG("kill_mode=%s\n", kill_mode_str);
874
875     int kill_mode;
876     if (strcmp(kill_mode_str, "window") == 0)
877         kill_mode = KILL_WINDOW;
878     else if (strcmp(kill_mode_str, "client") == 0)
879         kill_mode = KILL_CLIENT;
880     else {
881         ELOG("BUG: called with kill_mode=%s\n", kill_mode_str);
882         cmd_output->json_output = sstrdup("{\"sucess\": false}");
883         return;
884     }
885
886     /* check if the match is empty, not if the result is empty */
887     if (match_is_empty(current_match))
888         tree_close_con(kill_mode);
889     else {
890         TAILQ_FOREACH(current, &owindows, owindows) {
891             DLOG("matching: %p / %s\n", current->con, current->con->name);
892             tree_close(current->con, kill_mode, false, false);
893         }
894     }
895
896     cmd_output->needs_tree_render = true;
897     // XXX: default reply for now, make this a better reply
898     cmd_output->json_output = sstrdup("{\"success\": true}");
899 }
900
901 /*
902  * Implementation of 'exec [--no-startup-id] <command>'.
903  *
904  */
905 void cmd_exec(I3_CMD, char *nosn, char *command) {
906     bool no_startup_id = (nosn != NULL);
907
908     DLOG("should execute %s, no_startup_id = %d\n", command, no_startup_id);
909     start_application(command, no_startup_id);
910
911     // XXX: default reply for now, make this a better reply
912     cmd_output->json_output = sstrdup("{\"success\": true}");
913 }
914
915 /*
916  * Implementation of 'focus left|right|up|down'.
917  *
918  */
919 void cmd_focus_direction(I3_CMD, char *direction) {
920     if (focused &&
921         focused->type != CT_WORKSPACE &&
922         focused->fullscreen_mode != CF_NONE) {
923         LOG("Cannot change focus while in fullscreen mode.\n");
924         cmd_output->json_output = sstrdup("{\"sucess\": false}");
925         return;
926     }
927
928     DLOG("direction = *%s*\n", direction);
929
930     if (strcmp(direction, "left") == 0)
931         tree_next('p', HORIZ);
932     else if (strcmp(direction, "right") == 0)
933         tree_next('n', HORIZ);
934     else if (strcmp(direction, "up") == 0)
935         tree_next('p', VERT);
936     else if (strcmp(direction, "down") == 0)
937         tree_next('n', VERT);
938     else {
939         ELOG("Invalid focus direction (%s)\n", direction);
940         cmd_output->json_output = sstrdup("{\"sucess\": false}");
941         return;
942     }
943
944     cmd_output->needs_tree_render = true;
945     // XXX: default reply for now, make this a better reply
946     cmd_output->json_output = sstrdup("{\"success\": true}");
947 }
948
949 /*
950  * Implementation of 'focus tiling|floating|mode_toggle'.
951  *
952  */
953 void cmd_focus_window_mode(I3_CMD, char *window_mode) {
954     if (focused &&
955         focused->type != CT_WORKSPACE &&
956         focused->fullscreen_mode != CF_NONE) {
957         LOG("Cannot change focus while in fullscreen mode.\n");
958         cmd_output->json_output = sstrdup("{\"sucess\": false}");
959         return;
960     }
961
962     DLOG("window_mode = %s\n", window_mode);
963
964     Con *ws = con_get_workspace(focused);
965     Con *current;
966     if (ws != NULL) {
967         if (strcmp(window_mode, "mode_toggle") == 0) {
968             current = TAILQ_FIRST(&(ws->focus_head));
969             if (current != NULL && current->type == CT_FLOATING_CON)
970                 window_mode = "tiling";
971             else window_mode = "floating";
972         }
973         TAILQ_FOREACH(current, &(ws->focus_head), focused) {
974             if ((strcmp(window_mode, "floating") == 0 && current->type != CT_FLOATING_CON) ||
975                 (strcmp(window_mode, "tiling") == 0 && current->type == CT_FLOATING_CON))
976                 continue;
977
978             con_focus(con_descend_focused(current));
979             break;
980         }
981     }
982
983     cmd_output->needs_tree_render = true;
984     // XXX: default reply for now, make this a better reply
985     cmd_output->json_output = sstrdup("{\"success\": true}");
986 }
987
988 /*
989  * Implementation of 'focus parent|child'.
990  *
991  */
992 void cmd_focus_level(I3_CMD, char *level) {
993     if (focused &&
994         focused->type != CT_WORKSPACE &&
995         focused->fullscreen_mode != CF_NONE) {
996         LOG("Cannot change focus while in fullscreen mode.\n");
997         cmd_output->json_output = sstrdup("{\"sucess\": false}");
998         return;
999     }
1000
1001     DLOG("level = %s\n", level);
1002
1003     if (strcmp(level, "parent") == 0)
1004         level_up();
1005     else level_down();
1006
1007     cmd_output->needs_tree_render = true;
1008     // XXX: default reply for now, make this a better reply
1009     cmd_output->json_output = sstrdup("{\"success\": true}");
1010 }
1011
1012 /*
1013  * Implementation of 'focus'.
1014  *
1015  */
1016 void cmd_focus(I3_CMD) {
1017     DLOG("current_match = %p\n", current_match);
1018     if (focused &&
1019         focused->type != CT_WORKSPACE &&
1020         focused->fullscreen_mode != CF_NONE) {
1021         LOG("Cannot change focus while in fullscreen mode.\n");
1022         cmd_output->json_output = sstrdup("{\"sucess\": false}");
1023         return;
1024     }
1025
1026     owindow *current;
1027
1028     if (match_is_empty(current_match)) {
1029         ELOG("You have to specify which window/container should be focused.\n");
1030         ELOG("Example: [class=\"urxvt\" title=\"irssi\"] focus\n");
1031
1032         sasprintf(&(cmd_output->json_output),
1033                   "{\"success\":false, \"error\":\"You have to "
1034                   "specify which window/container should be focused\"}");
1035         return;
1036     }
1037
1038     int count = 0;
1039     TAILQ_FOREACH(current, &owindows, owindows) {
1040         Con *ws = con_get_workspace(current->con);
1041         /* If no workspace could be found, this was a dock window.
1042          * Just skip it, you cannot focus dock windows. */
1043         if (!ws)
1044             continue;
1045
1046         /* If the container is not on the current workspace,
1047          * workspace_show() will switch to a different workspace and (if
1048          * enabled) trigger a mouse pointer warp to the currently focused
1049          * container (!) on the target workspace.
1050          *
1051          * Therefore, before calling workspace_show(), we make sure that
1052          * 'current' will be focused on the workspace. However, we cannot
1053          * just con_focus(current) because then the pointer will not be
1054          * warped at all (the code thinks we are already there).
1055          *
1056          * So we focus 'current' to make it the currently focused window of
1057          * the target workspace, then revert focus. */
1058         Con *currently_focused = focused;
1059         con_focus(current->con);
1060         con_focus(currently_focused);
1061
1062         /* Now switch to the workspace, then focus */
1063         workspace_show(ws);
1064         LOG("focusing %p / %s\n", current->con, current->con->name);
1065         con_focus(current->con);
1066         count++;
1067     }
1068
1069     if (count > 1)
1070         LOG("WARNING: Your criteria for the focus command matches %d containers, "
1071             "while only exactly one container can be focused at a time.\n", count);
1072
1073     cmd_output->needs_tree_render = true;
1074     // XXX: default reply for now, make this a better reply
1075     cmd_output->json_output = sstrdup("{\"success\": true}");
1076 }
1077
1078 /*
1079  * Implementation of 'fullscreen [global]'.
1080  *
1081  */
1082 void cmd_fullscreen(I3_CMD, char *fullscreen_mode) {
1083     if (fullscreen_mode == NULL)
1084         fullscreen_mode = "output";
1085     DLOG("toggling fullscreen, mode = %s\n", fullscreen_mode);
1086     owindow *current;
1087
1088     HANDLE_EMPTY_MATCH;
1089
1090     TAILQ_FOREACH(current, &owindows, owindows) {
1091         printf("matching: %p / %s\n", current->con, current->con->name);
1092         con_toggle_fullscreen(current->con, (strcmp(fullscreen_mode, "global") == 0 ? CF_GLOBAL : CF_OUTPUT));
1093     }
1094
1095     cmd_output->needs_tree_render = true;
1096     // XXX: default reply for now, make this a better reply
1097     cmd_output->json_output = sstrdup("{\"success\": true}");
1098 }
1099
1100 /*
1101  * Implementation of 'move <direction> [<pixels> [px]]'.
1102  *
1103  */
1104 void cmd_move_direction(I3_CMD, char *direction, char *move_px) {
1105     // 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
1106     int px = atoi(move_px);
1107
1108     /* TODO: make 'move' work with criteria. */
1109     DLOG("moving in direction %s, px %s\n", direction, move_px);
1110     if (con_is_floating(focused)) {
1111         DLOG("floating move with %d pixels\n", px);
1112         Rect newrect = focused->parent->rect;
1113         if (strcmp(direction, "left") == 0) {
1114             newrect.x -= px;
1115         } else if (strcmp(direction, "right") == 0) {
1116             newrect.x += px;
1117         } else if (strcmp(direction, "up") == 0) {
1118             newrect.y -= px;
1119         } else if (strcmp(direction, "down") == 0) {
1120             newrect.y += px;
1121         }
1122         floating_reposition(focused->parent, newrect);
1123     } else {
1124         tree_move((strcmp(direction, "right") == 0 ? D_RIGHT :
1125                    (strcmp(direction, "left") == 0 ? D_LEFT :
1126                     (strcmp(direction, "up") == 0 ? D_UP :
1127                      D_DOWN))));
1128         cmd_output->needs_tree_render = true;
1129     }
1130
1131     // XXX: default reply for now, make this a better reply
1132     cmd_output->json_output = sstrdup("{\"success\": true}");
1133 }
1134
1135 /*
1136  * Implementation of 'layout default|stacked|stacking|tabbed'.
1137  *
1138  */
1139 void cmd_layout(I3_CMD, char *layout_str) {
1140     if (strcmp(layout_str, "stacking") == 0)
1141         layout_str = "stacked";
1142     DLOG("changing layout to %s\n", layout_str);
1143     owindow *current;
1144     int layout = (strcmp(layout_str, "default") == 0 ? L_DEFAULT :
1145                   (strcmp(layout_str, "stacked") == 0 ? L_STACKED :
1146                    L_TABBED));
1147
1148     /* check if the match is empty, not if the result is empty */
1149     if (match_is_empty(current_match))
1150         con_set_layout(focused->parent, layout);
1151     else {
1152         TAILQ_FOREACH(current, &owindows, owindows) {
1153             DLOG("matching: %p / %s\n", current->con, current->con->name);
1154             con_set_layout(current->con, layout);
1155         }
1156     }
1157
1158     cmd_output->needs_tree_render = true;
1159     // XXX: default reply for now, make this a better reply
1160     cmd_output->json_output = sstrdup("{\"success\": true}");
1161 }
1162
1163 /*
1164  * Implementaiton of 'exit'.
1165  *
1166  */
1167 void cmd_exit(I3_CMD) {
1168     LOG("Exiting due to user command.\n");
1169     exit(0);
1170
1171     /* unreached */
1172 }
1173
1174 /*
1175  * Implementaiton of 'reload'.
1176  *
1177  */
1178 void cmd_reload(I3_CMD) {
1179     LOG("reloading\n");
1180     kill_configerror_nagbar(false);
1181     load_configuration(conn, NULL, true);
1182     x_set_i3_atoms();
1183     /* Send an IPC event just in case the ws names have changed */
1184     ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"reload\"}");
1185
1186     // XXX: default reply for now, make this a better reply
1187     cmd_output->json_output = sstrdup("{\"success\": true}");
1188 }
1189
1190 /*
1191  * Implementaiton of 'restart'.
1192  *
1193  */
1194 void cmd_restart(I3_CMD) {
1195     LOG("restarting i3\n");
1196     i3_restart(false);
1197
1198     // XXX: default reply for now, make this a better reply
1199     cmd_output->json_output = sstrdup("{\"success\": true}");
1200 }
1201
1202 /*
1203  * Implementaiton of 'open'.
1204  *
1205  */
1206 void cmd_open(I3_CMD) {
1207     LOG("opening new container\n");
1208     Con *con = tree_open_con(NULL, NULL);
1209     con_focus(con);
1210     sasprintf(&(cmd_output->json_output),
1211               "{\"success\":true, \"id\":%ld}", (long int)con);
1212
1213     cmd_output->needs_tree_render = true;
1214 }
1215
1216 /*
1217  * Implementation of 'focus output <output>'.
1218  *
1219  */
1220 void cmd_focus_output(I3_CMD, char *name) {
1221     owindow *current;
1222
1223     DLOG("name = %s\n", name);
1224
1225     HANDLE_EMPTY_MATCH;
1226
1227     /* get the output */
1228     Output *current_output = NULL;
1229     Output *output;
1230
1231     TAILQ_FOREACH(current, &owindows, owindows)
1232         current_output = get_output_containing(current->con->rect.x, current->con->rect.y);
1233     assert(current_output != NULL);
1234
1235     output = get_output_from_string(current_output, name);
1236
1237     if (!output) {
1238         LOG("No such output found.\n");
1239         cmd_output->json_output = sstrdup("{\"sucess\": false}");
1240         return;
1241     }
1242
1243     /* get visible workspace on output */
1244     Con *ws = NULL;
1245     GREP_FIRST(ws, output_get_content(output->con), workspace_is_visible(child));
1246     if (!ws) {
1247         cmd_output->json_output = sstrdup("{\"sucess\": false}");
1248         return;
1249     }
1250
1251     workspace_show(ws);
1252
1253     cmd_output->needs_tree_render = true;
1254     // XXX: default reply for now, make this a better reply
1255     cmd_output->json_output = sstrdup("{\"success\": true}");
1256 }
1257
1258 /*
1259  * Implementation of 'move scratchpad'.
1260  *
1261  */
1262 void cmd_move_scratchpad(I3_CMD) {
1263     DLOG("should move window to scratchpad\n");
1264     owindow *current;
1265
1266     HANDLE_EMPTY_MATCH;
1267
1268     TAILQ_FOREACH(current, &owindows, owindows) {
1269         DLOG("matching: %p / %s\n", current->con, current->con->name);
1270         scratchpad_move(current->con);
1271     }
1272
1273     cmd_output->needs_tree_render = true;
1274     // XXX: default reply for now, make this a better reply
1275     cmd_output->json_output = sstrdup("{\"success\": true}");
1276 }
1277
1278 /*
1279  * Implementation of 'scratchpad show'.
1280  *
1281  */
1282 void cmd_scratchpad_show(I3_CMD) {
1283     DLOG("should show scratchpad window\n");
1284     owindow *current;
1285
1286     if (match_is_empty(current_match)) {
1287         scratchpad_show(NULL);
1288     } else {
1289         TAILQ_FOREACH(current, &owindows, owindows) {
1290             DLOG("matching: %p / %s\n", current->con, current->con->name);
1291             scratchpad_show(current->con);
1292         }
1293     }
1294
1295     cmd_output->needs_tree_render = true;
1296     // XXX: default reply for now, make this a better reply
1297     cmd_output->json_output = sstrdup("{\"success\": true}");
1298 }