]> git.sur5r.net Git - i3/i3/blob - src/commands.c
7cb0bdca3f328b43f0ed50536c79e962fb62caad
[i3/i3] / src / commands.c
1 /*
2  * vim:ts=8:expandtab
3  *
4  * i3 - an improved dynamic tiling window manager
5  *
6  * © 2009 Michael Stapelberg and contributors
7  *
8  * See file LICENSE for license information.
9  *
10  */
11 #include <stdbool.h>
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <assert.h>
15 #include <unistd.h>
16 #include <string.h>
17
18 #include <xcb/xcb.h>
19
20 #include "util.h"
21 #include "data.h"
22 #include "table.h"
23 #include "layout.h"
24 #include "i3.h"
25 #include "xinerama.h"
26 #include "client.h"
27 #include "floating.h"
28 #include "xcb.h"
29 #include "config.h"
30 #include "workspace.h"
31 #include "commands.h"
32 #include "resize.h"
33 #include "log.h"
34
35 bool focus_window_in_container(xcb_connection_t *conn, Container *container, direction_t direction) {
36         /* If this container is empty, we’re done */
37         if (container->currently_focused == NULL)
38                 return false;
39
40         /* Get the previous/next client or wrap around */
41         Client *candidate = NULL;
42         if (direction == D_UP) {
43                 if ((candidate = CIRCLEQ_PREV_OR_NULL(&(container->clients), container->currently_focused, clients)) == NULL)
44                         candidate = CIRCLEQ_LAST(&(container->clients));
45         }
46         else if (direction == D_DOWN) {
47                 if ((candidate = CIRCLEQ_NEXT_OR_NULL(&(container->clients), container->currently_focused, clients)) == NULL)
48                         candidate = CIRCLEQ_FIRST(&(container->clients));
49         } else ELOG("Direction not implemented!\n");
50
51         /* If we could not switch, the container contains exactly one client. We return false */
52         if (candidate == container->currently_focused)
53                 return false;
54
55         /* Set focus */
56         set_focus(conn, candidate, true);
57
58         return true;
59 }
60
61 typedef enum { THING_WINDOW, THING_CONTAINER, THING_SCREEN } thing_t;
62
63 static void jump_to_mark(xcb_connection_t *conn, const char *mark) {
64         Client *current;
65         LOG("Jumping to \"%s\"\n", mark);
66
67         Workspace *ws;
68         TAILQ_FOREACH(ws, workspaces, workspaces)
69                 SLIST_FOREACH(current, &(ws->focus_stack), focus_clients) {
70                         if (current->mark == NULL || strcmp(current->mark, mark) != 0)
71                                 continue;
72
73                         workspace_show(conn, current->workspace->num + 1);
74                         set_focus(conn, current, true);
75                         return;
76                 }
77
78         ELOG("No window with this mark found\n");
79 }
80
81 static void focus_thing(xcb_connection_t *conn, direction_t direction, thing_t thing) {
82         DLOG("focusing direction %d\n", direction);
83
84         int new_row = current_row,
85             new_col = current_col;
86         Container *container = CUR_CELL;
87         Workspace *t_ws = c_ws;
88
89         /* Makes sure new_col and new_row are within bounds of the new workspace */
90         void check_colrow_boundaries() {
91                 if (new_col >= t_ws->cols)
92                         new_col = (t_ws->cols - 1);
93                 if (new_row >= t_ws->rows)
94                         new_row = (t_ws->rows - 1);
95         }
96
97         /* There always is a container. If not, current_col or current_row is wrong */
98         assert(container != NULL);
99
100         if (container->workspace->fullscreen_client != NULL) {
101                 LOG("You're in fullscreen mode. Won't switch focus\n");
102                 return;
103         }
104
105         /* For focusing screens, situation is different: we get the rect
106          * of the current screen, then get the screen which is on its
107          * right/left/bottom/top and just switch to the workspace on
108          * the target screen. */
109         if (thing == THING_SCREEN) {
110                 i3Screen *cs = c_ws->screen;
111                 assert(cs != NULL);
112                 Rect bounds = cs->rect;
113
114                 if (direction == D_RIGHT)
115                         bounds.x += bounds.width;
116                 else if (direction == D_LEFT)
117                         bounds.x -= bounds.width;
118                 else if (direction == D_UP)
119                         bounds.y -= bounds.height;
120                 else bounds.y += bounds.height;
121
122                 i3Screen *target = get_screen_containing(bounds.x, bounds.y);
123                 if (target == NULL) {
124                         DLOG("Target screen NULL\n");
125                         /* Wrap around if the target screen is out of bounds */
126                         if (direction == D_RIGHT)
127                                 target = get_screen_most(D_LEFT, cs);
128                         else if (direction == D_LEFT)
129                                 target = get_screen_most(D_RIGHT, cs);
130                         else if (direction == D_UP)
131                                 target = get_screen_most(D_DOWN, cs);
132                         else target = get_screen_most(D_UP, cs);
133                 }
134
135                 DLOG("Switching to ws %d\n", target->current_workspace + 1);
136                 workspace_show(conn, target->current_workspace->num + 1);
137                 return;
138         }
139
140         /* TODO: for horizontal default layout, this has to be expanded to LEFT/RIGHT */
141         if (direction == D_UP || direction == D_DOWN) {
142                 if (thing == THING_WINDOW)
143                         /* Let’s see if we can perform up/down focus in the current container */
144                         if (focus_window_in_container(conn, container, direction))
145                                 return;
146
147                 if (direction == D_DOWN && cell_exists(current_col, current_row+1))
148                         new_row = current_row + t_ws->table[current_col][current_row]->rowspan;
149                 else if (direction == D_UP && cell_exists(current_col, current_row-1)) {
150                         /* Set new_row as a sane default, but it may get overwritten in a second */
151                         new_row--;
152
153                         /* Search from the top to correctly handle rowspanned containers */
154                         for (int rows = 0; rows < current_row; rows += t_ws->table[current_col][rows]->rowspan) {
155                                 if (new_row > (rows + (t_ws->table[current_col][rows]->rowspan - 1)))
156                                         continue;
157
158                                 new_row = rows;
159                                 break;
160                         }
161                 } else {
162                         /* Let’s see if there is a screen down/up there to which we can switch */
163                         DLOG("container is at %d with height %d\n", container->y, container->height);
164                         i3Screen *screen;
165                         int destination_y = (direction == D_UP ? (container->y - 1) : (container->y + container->height + 1));
166                         if ((screen = get_screen_containing(container->x, destination_y)) == NULL) {
167                                 DLOG("Wrapping screen around vertically\n");
168                                 /* No screen found? Then wrap */
169                                 screen = get_screen_most((direction == D_UP ? D_DOWN : D_UP), container->workspace->screen);
170                         }
171                         t_ws = screen->current_workspace;
172                         new_row = (direction == D_UP ? (t_ws->rows - 1) : 0);
173                 }
174
175                 check_colrow_boundaries();
176
177                 DLOG("new_col = %d, new_row = %d\n", new_col, new_row);
178                 if (t_ws->table[new_col][new_row]->currently_focused == NULL) {
179                         DLOG("Cell empty, checking for colspanned client above...\n");
180                         for (int cols = 0; cols < new_col; cols += t_ws->table[cols][new_row]->colspan) {
181                                 if (new_col > (cols + (t_ws->table[cols][new_row]->colspan - 1)))
182                                         continue;
183
184                                 new_col = cols;
185                                 break;
186                         }
187                         DLOG("Fixed it to new col %d\n", new_col);
188                 }
189         } else if (direction == D_LEFT || direction == D_RIGHT) {
190                 if (direction == D_RIGHT && cell_exists(current_col+1, current_row))
191                         new_col = current_col + t_ws->table[current_col][current_row]->colspan;
192                 else if (direction == D_LEFT && cell_exists(current_col-1, current_row)) {
193                         /* Set new_col as a sane default, but it may get overwritten in a second */
194                         new_col--;
195
196                         /* Search from the left to correctly handle colspanned containers */
197                         for (int cols = 0; cols < current_col; cols += t_ws->table[cols][current_row]->colspan) {
198                                 if (new_col > (cols + (t_ws->table[cols][current_row]->colspan - 1)))
199                                         continue;
200
201                                 new_col = cols;
202                                 break;
203                         }
204                 } else {
205                         /* Let’s see if there is a screen left/right here to which we can switch */
206                         DLOG("container is at %d with width %d\n", container->x, container->width);
207                         i3Screen *screen;
208                         int destination_x = (direction == D_LEFT ? (container->x - 1) : (container->x + container->width + 1));
209                         if ((screen = get_screen_containing(destination_x, container->y)) == NULL) {
210                                 DLOG("Wrapping screen around horizontally\n");
211                                 screen = get_screen_most((direction == D_LEFT ? D_RIGHT : D_LEFT), container->workspace->screen);
212                         }
213                         t_ws = screen->current_workspace;
214                         new_col = (direction == D_LEFT ? (t_ws->cols - 1) : 0);
215                 }
216
217                 check_colrow_boundaries();
218
219                 DLOG("new_col = %d, new_row = %d\n", new_col, new_row);
220                 if (t_ws->table[new_col][new_row]->currently_focused == NULL) {
221                         DLOG("Cell empty, checking for rowspanned client above...\n");
222                         for (int rows = 0; rows < new_row; rows += t_ws->table[new_col][rows]->rowspan) {
223                                 if (new_row > (rows + (t_ws->table[new_col][rows]->rowspan - 1)))
224                                         continue;
225
226                                 new_row = rows;
227                                 break;
228                         }
229                         DLOG("Fixed it to new row %d\n", new_row);
230                 }
231         } else {
232                 ELOG("direction unhandled\n");
233                 return;
234         }
235
236         check_colrow_boundaries();
237
238         if (t_ws->table[new_col][new_row]->currently_focused != NULL)
239                 set_focus(conn, t_ws->table[new_col][new_row]->currently_focused, true);
240 }
241
242 /*
243  * Tries to move the window inside its current container.
244  *
245  * Returns true if the window could be moved, false otherwise.
246  *
247  */
248 static bool move_current_window_in_container(xcb_connection_t *conn, Client *client,
249                 direction_t direction) {
250         assert(client->container != NULL);
251
252         Client *other = (direction == D_UP ? CIRCLEQ_PREV(client, clients) :
253                                              CIRCLEQ_NEXT(client, clients));
254
255         if (other == CIRCLEQ_END(&(client->container->clients)))
256                 return false;
257
258         DLOG("i can do that\n");
259         /* We can move the client inside its current container */
260         CIRCLEQ_REMOVE(&(client->container->clients), client, clients);
261         if (direction == D_UP)
262                 CIRCLEQ_INSERT_BEFORE(&(client->container->clients), other, client, clients);
263         else CIRCLEQ_INSERT_AFTER(&(client->container->clients), other, client, clients);
264         render_layout(conn);
265         return true;
266 }
267
268 /*
269  * Moves the current window or whole container to the given direction, creating a column/row if
270  * necessary.
271  *
272  */
273 static void move_current_window(xcb_connection_t *conn, direction_t direction) {
274         LOG("moving window to direction %s\n", (direction == D_UP ? "up" : (direction == D_DOWN ? "down" :
275                                             (direction == D_LEFT ? "left" : "right"))));
276         /* Get current window */
277         Container *container = CUR_CELL,
278                   *new = NULL;
279
280         /* There has to be a container, see focus_window() */
281         assert(container != NULL);
282
283         /* If there is no window or the dock window is focused, we’re done */
284         if (container->currently_focused == NULL ||
285             container->currently_focused->dock)
286                 return;
287
288         /* As soon as the client is moved away, the last focused client in the old
289          * container needs to get focus, if any. Therefore, we save it here. */
290         Client *current_client = container->currently_focused;
291         Client *to_focus = get_last_focused_client(conn, container, current_client);
292
293         if (to_focus == NULL) {
294                 to_focus = CIRCLEQ_NEXT_OR_NULL(&(container->clients), current_client, clients);
295                 if (to_focus == NULL)
296                         to_focus = CIRCLEQ_PREV_OR_NULL(&(container->clients), current_client, clients);
297         }
298
299         switch (direction) {
300                 case D_LEFT:
301                         /* If we’re at the left-most position, move the rest of the table right */
302                         if (current_col == 0) {
303                                 expand_table_cols_at_head(c_ws);
304                                 new = CUR_CELL;
305                         } else
306                                 new = CUR_TABLE[--current_col][current_row];
307                         break;
308                 case D_RIGHT:
309                         if (current_col == (c_ws->cols-1))
310                                 expand_table_cols(c_ws);
311
312                         new = CUR_TABLE[++current_col][current_row];
313                         break;
314                 case D_UP:
315                         if (move_current_window_in_container(conn, current_client, D_UP))
316                                 return;
317
318                         /* if we’re at the up-most position, move the rest of the table down */
319                         if (current_row == 0) {
320                                 expand_table_rows_at_head(c_ws);
321                                 new = CUR_CELL;
322                         } else
323                                 new = CUR_TABLE[current_col][--current_row];
324                         break;
325                 case D_DOWN:
326                         if (move_current_window_in_container(conn, current_client, D_DOWN))
327                                 return;
328
329                         if (current_row == (c_ws->rows-1))
330                                 expand_table_rows(c_ws);
331
332                         new = CUR_TABLE[current_col][++current_row];
333                         break;
334                 /* To make static analyzers happy: */
335                 default:
336                         return;
337         }
338
339         /* Remove it from the old container and put it into the new one */
340         client_remove_from_container(conn, current_client, container, true);
341
342         if (new->currently_focused != NULL)
343                 CIRCLEQ_INSERT_AFTER(&(new->clients), new->currently_focused, current_client, clients);
344         else CIRCLEQ_INSERT_TAIL(&(new->clients), current_client, clients);
345         SLIST_INSERT_HEAD(&(new->workspace->focus_stack), current_client, focus_clients);
346
347         /* Update data structures */
348         current_client->container = new;
349         current_client->workspace = new->workspace;
350         container->currently_focused = to_focus;
351         new->currently_focused = current_client;
352
353         Workspace *workspace = container->workspace;
354
355         /* delete all empty columns/rows */
356         cleanup_table(conn, workspace);
357
358         /* Fix colspan/rowspan if it’d overlap */
359         fix_colrowspan(conn, workspace);
360
361         render_workspace(conn, workspace->screen, workspace);
362         xcb_flush(conn);
363
364         set_focus(conn, current_client, true);
365 }
366
367 static void move_current_container(xcb_connection_t *conn, direction_t direction) {
368         LOG("moving container to direction %s\n", (direction == D_UP ? "up" : (direction == D_DOWN ? "down" :
369                                             (direction == D_LEFT ? "left" : "right"))));
370         /* Get current window */
371         Container *container = CUR_CELL,
372                   *new = NULL;
373
374         Container **old = &CUR_CELL;
375
376         /* There has to be a container, see focus_window() */
377         assert(container != NULL);
378
379         switch (direction) {
380                 case D_LEFT:
381                         /* If we’re at the left-most position, move the rest of the table right */
382                         if (current_col == 0) {
383                                 expand_table_cols_at_head(c_ws);
384                                 new = CUR_CELL;
385                                 old = &CUR_TABLE[current_col+1][current_row];
386                         } else
387                                 new = CUR_TABLE[--current_col][current_row];
388                         break;
389                 case D_RIGHT:
390                         if (current_col == (c_ws->cols-1))
391                                 expand_table_cols(c_ws);
392
393                         new = CUR_TABLE[++current_col][current_row];
394                         break;
395                 case D_UP:
396                         /* if we’re at the up-most position, move the rest of the table down */
397                         if (current_row == 0) {
398                                 expand_table_rows_at_head(c_ws);
399                                 new = CUR_CELL;
400                                 old = &CUR_TABLE[current_col][current_row+1];
401                         } else
402                                 new = CUR_TABLE[current_col][--current_row];
403                         break;
404                 case D_DOWN:
405                         if (current_row == (c_ws->rows-1))
406                                 expand_table_rows(c_ws);
407
408                         new = CUR_TABLE[current_col][++current_row];
409                         break;
410                 /* To make static analyzers happy: */
411                 default:
412                         return;
413         }
414
415         DLOG("old = %d,%d and new = %d,%d\n", container->col, container->row, new->col, new->row);
416
417         /* Swap the containers */
418         int col = new->col;
419         int row = new->row;
420
421         *old = new;
422         new->col = container->col;
423         new->row = container->row;
424
425         CUR_CELL = container;
426         container->col = col;
427         container->row = row;
428
429         Workspace *workspace = container->workspace;
430
431         /* delete all empty columns/rows */
432         cleanup_table(conn, workspace);
433
434         /* Fix colspan/rowspan if it’d overlap */
435         fix_colrowspan(conn, workspace);
436
437         render_layout(conn);
438 }
439
440 /*
441  * "Snaps" the current container (not possible for windows, because it works at table base)
442  * to the given direction, that is, adjusts cellspan/rowspan
443  *
444  */
445 static void snap_current_container(xcb_connection_t *conn, direction_t direction) {
446         LOG("snapping container to direction %d\n", direction);
447
448         Container *container = CUR_CELL;
449
450         assert(container != NULL);
451
452         switch (direction) {
453                 case D_LEFT:
454                         /* Snap to the left is actually a move to the left and then a snap right */
455                         if (!cell_exists(container->col - 1, container->row) ||
456                             CUR_TABLE[container->col-1][container->row]->currently_focused != NULL) {
457                                 ELOG("cannot snap to left - the cell is already used\n");
458                                 return;
459                         }
460
461                         move_current_window(conn, D_LEFT);
462                         snap_current_container(conn, D_RIGHT);
463                         return;
464                 case D_RIGHT: {
465                         /* Check if the cell is used */
466                         int new_col = container->col + container->colspan;
467                         for (int i = 0; i < container->rowspan; i++)
468                                 if (!cell_exists(new_col, container->row + i) ||
469                                     CUR_TABLE[new_col][container->row + i]->currently_focused != NULL) {
470                                         ELOG("cannot snap to right - the cell is already used\n");
471                                         return;
472                                 }
473
474                         /* Check if there are other cells with rowspan, which are in our way.
475                          * If so, reduce their rowspan. */
476                         for (int i = container->row-1; i >= 0; i--) {
477                                 DLOG("we got cell %d, %d with rowspan %d\n",
478                                                 new_col, i, CUR_TABLE[new_col][i]->rowspan);
479                                 while ((CUR_TABLE[new_col][i]->rowspan-1) >= (container->row - i))
480                                         CUR_TABLE[new_col][i]->rowspan--;
481                                 DLOG("new rowspan = %d\n", CUR_TABLE[new_col][i]->rowspan);
482                         }
483
484                         container->colspan++;
485                         break;
486                 }
487                 case D_UP:
488                         if (!cell_exists(container->col, container->row - 1) ||
489                             CUR_TABLE[container->col][container->row-1]->currently_focused != NULL) {
490                                 ELOG("cannot snap to top - the cell is already used\n");
491                                 return;
492                         }
493
494                         move_current_window(conn, D_UP);
495                         snap_current_container(conn, D_DOWN);
496                         return;
497                 case D_DOWN: {
498                         DLOG("snapping down\n");
499                         int new_row = container->row + container->rowspan;
500                         for (int i = 0; i < container->colspan; i++)
501                                 if (!cell_exists(container->col + i, new_row) ||
502                                     CUR_TABLE[container->col + i][new_row]->currently_focused != NULL) {
503                                         ELOG("cannot snap down - the cell is already used\n");
504                                         return;
505                                 }
506
507                         for (int i = container->col-1; i >= 0; i--) {
508                                 DLOG("we got cell %d, %d with colspan %d\n",
509                                                 i, new_row, CUR_TABLE[i][new_row]->colspan);
510                                 while ((CUR_TABLE[i][new_row]->colspan-1) >= (container->col - i))
511                                         CUR_TABLE[i][new_row]->colspan--;
512                                 DLOG("new colspan = %d\n", CUR_TABLE[i][new_row]->colspan);
513
514                         }
515
516                         container->rowspan++;
517                         break;
518                 }
519                 /* To make static analyzers happy: */
520                 default:
521                         return;
522         }
523
524         render_layout(conn);
525 }
526
527 static void move_floating_window_to_workspace(xcb_connection_t *conn, Client *client, int workspace) {
528         /* t_ws (to workspace) is just a container pointer to the workspace we’re switching to */
529         Workspace *t_ws = workspace_get(workspace-1),
530                   *old_ws = client->workspace;
531
532         LOG("moving floating\n");
533
534         workspace_initialize(t_ws, c_ws->screen);
535
536         /* Check if there is already a fullscreen client on the destination workspace and
537          * stop moving if so. */
538         if (client->fullscreen && (t_ws->fullscreen_client != NULL)) {
539                 ELOG("Not moving: Fullscreen client already existing on destination workspace.\n");
540                 return;
541         }
542
543         floating_assign_to_workspace(client, t_ws);
544
545         /* If we’re moving it to an invisible screen, we need to unmap it */
546         if (!workspace_is_visible(t_ws)) {
547                 DLOG("This workspace is not visible, unmapping\n");
548                 client_unmap(conn, client);
549         } else {
550                 /* If this is not the case, we move the window to a workspace
551                  * which is on another screen, so we also need to adjust its
552                  * coordinates. */
553                 DLOG("before x = %d, y = %d\n", client->rect.x, client->rect.y);
554                 uint32_t relative_x = client->rect.x - old_ws->rect.x,
555                          relative_y = client->rect.y - old_ws->rect.y;
556                 DLOG("rel_x = %d, rel_y = %d\n", relative_x, relative_y);
557                 client->rect.x = t_ws->rect.x + relative_x;
558                 client->rect.y = t_ws->rect.y + relative_y;
559                 DLOG("after x = %d, y = %d\n", client->rect.x, client->rect.y);
560                 reposition_client(conn, client);
561                 xcb_flush(conn);
562         }
563
564         DLOG("done\n");
565
566         render_layout(conn);
567
568         if (workspace_is_visible(t_ws))
569                 set_focus(conn, client, true);
570 }
571
572 /*
573  * Moves the currently selected window to the given workspace
574  *
575  */
576 static void move_current_window_to_workspace(xcb_connection_t *conn, int workspace) {
577         LOG("Moving current window to workspace %d\n", workspace);
578
579         Container *container = CUR_CELL;
580
581         assert(container != NULL);
582
583         /* t_ws (to workspace) is just a container pointer to the workspace we’re switching to */
584         Workspace *t_ws = workspace_get(workspace-1);
585
586         Client *current_client = container->currently_focused;
587         if (current_client == NULL) {
588                 ELOG("No currently focused client in current container.\n");
589                 return;
590         }
591         Client *to_focus = CIRCLEQ_NEXT_OR_NULL(&(container->clients), current_client, clients);
592         if (to_focus == NULL)
593                 to_focus = CIRCLEQ_PREV_OR_NULL(&(container->clients), current_client, clients);
594
595         workspace_initialize(t_ws, container->workspace->screen);
596         /* Check if there is already a fullscreen client on the destination workspace and
597          * stop moving if so. */
598         if (current_client->fullscreen && (t_ws->fullscreen_client != NULL)) {
599                 ELOG("Not moving: Fullscreen client already existing on destination workspace.\n");
600                 return;
601         }
602
603         Container *to_container = t_ws->table[t_ws->current_col][t_ws->current_row];
604
605         assert(to_container != NULL);
606
607         client_remove_from_container(conn, current_client, container, true);
608         if (container->workspace->fullscreen_client == current_client)
609                 container->workspace->fullscreen_client = NULL;
610
611         /* TODO: insert it to the correct position */
612         CIRCLEQ_INSERT_TAIL(&(to_container->clients), current_client, clients);
613
614         SLIST_INSERT_HEAD(&(to_container->workspace->focus_stack), current_client, focus_clients);
615         DLOG("Moved.\n");
616
617         current_client->container = to_container;
618         current_client->workspace = to_container->workspace;
619         container->currently_focused = to_focus;
620         to_container->currently_focused = current_client;
621
622         /* If we’re moving it to an invisible screen, we need to unmap it */
623         if (!workspace_is_visible(to_container->workspace)) {
624                 DLOG("This workspace is not visible, unmapping\n");
625                 client_unmap(conn, current_client);
626         } else {
627                 if (current_client->fullscreen) {
628                         DLOG("Calling client_enter_fullscreen again\n");
629                         client_enter_fullscreen(conn, current_client);
630                 }
631         }
632
633         /* delete all empty columns/rows */
634         cleanup_table(conn, container->workspace);
635
636         render_layout(conn);
637
638         if (workspace_is_visible(to_container->workspace))
639                 set_focus(conn, current_client, true);
640 }
641
642 /*
643  * Jumps to the given window class / title.
644  * Title is matched using strstr, that is, matches if it appears anywhere
645  * in the string. Regular expressions seem to be a bit overkill here. However,
646  * if we need them for something else somewhen, we may introduce them here, too.
647  *
648  */
649 static void jump_to_window(xcb_connection_t *conn, const char *arguments) {
650         char *classtitle;
651         Client *client;
652
653         /* The first character is a quote, this was checked before */
654         classtitle = sstrdup(arguments+1);
655         /* The last character is a quote, we just set it to NULL */
656         classtitle[strlen(classtitle)-1] = '\0';
657
658         if ((client = get_matching_client(conn, classtitle, NULL)) == NULL) {
659                 free(classtitle);
660                 ELOG("No matching client found.\n");
661                 return;
662         }
663
664         free(classtitle);
665         set_focus(conn, client, true);
666 }
667
668 /*
669  * Jump directly to the specified workspace, row and col.
670  * Great for reaching windows that you always keep in the same spot (hello irssi, I'm looking at you)
671  *
672  */
673 static void jump_to_container(xcb_connection_t *conn, const char *arguments) {
674         int ws, row, col;
675         int result;
676
677         result = sscanf(arguments, "%d %d %d", &ws, &col, &row);
678         LOG("Jump called with %d parameters (\"%s\")\n", result, arguments);
679
680         /* No match? Either no arguments were specified, or no numbers */
681         if (result < 1) {
682                 ELOG("At least one valid argument required\n");
683                 return;
684         }
685
686         /* Move to the target workspace */
687         workspace_show(conn, ws);
688
689         if (result < 3)
690                 return;
691
692         DLOG("Boundary-checking col %d, row %d... (max cols %d, max rows %d)\n", col, row, c_ws->cols, c_ws->rows);
693
694         /* Move to row/col */
695         if (row >= c_ws->rows)
696                 row = c_ws->rows - 1;
697         if (col >= c_ws->cols)
698                 col = c_ws->cols - 1;
699
700         DLOG("Jumping to col %d, row %d\n", col, row);
701         if (c_ws->table[col][row]->currently_focused != NULL)
702                 set_focus(conn, c_ws->table[col][row]->currently_focused, true);
703 }
704
705 /*
706  * Travels the focus stack by the given number of times (or once, if no argument
707  * was specified). That is, selects the window you were in before you focused
708  * the current window.
709  *
710  * The special values 'floating' (select the next floating window), 'tiling'
711  * (select the next tiling window), 'ft' (if the current window is floating,
712  * select the next tiling window and vice-versa) are also valid
713  *
714  */
715 static void travel_focus_stack(xcb_connection_t *conn, const char *arguments) {
716         /* Start count at -1 to always skip the first element */
717         int times, count = -1;
718         Client *current;
719         bool floating_criteria;
720
721         /* Either it’s one of the special values… */
722         if (strcasecmp(arguments, "floating") == 0) {
723                 floating_criteria = true;
724         } else if (strcasecmp(arguments, "tiling") == 0) {
725                 floating_criteria = false;
726         } else if (strcasecmp(arguments, "ft") == 0) {
727                 Client *last_focused = SLIST_FIRST(&(c_ws->focus_stack));
728                 if (last_focused == SLIST_END(&(c_ws->focus_stack))) {
729                         ELOG("Cannot select the next floating/tiling client because there is no client at all\n");
730                         return;
731                 }
732
733                 floating_criteria = !client_is_floating(last_focused);
734         } else {
735                 /* …or a number was specified */
736                 if (sscanf(arguments, "%u", &times) != 1) {
737                         ELOG("No or invalid argument given (\"%s\"), using default of 1 times\n", arguments);
738                         times = 1;
739                 }
740
741                 SLIST_FOREACH(current, &(CUR_CELL->workspace->focus_stack), focus_clients) {
742                         if (++count < times) {
743                                 DLOG("Skipping\n");
744                                 continue;
745                         }
746
747                         DLOG("Focussing\n");
748                         set_focus(conn, current, true);
749                         break;
750                 }
751                 return;
752         }
753
754         /* Select the next client matching the criteria parsed above */
755         SLIST_FOREACH(current, &(CUR_CELL->workspace->focus_stack), focus_clients)
756                 if (client_is_floating(current) == floating_criteria) {
757                         set_focus(conn, current, true);
758                         break;
759                 }
760 }
761
762 /*
763  * Goes through the list of arguments (for exec()) and checks if the given argument
764  * is present. If not, it copies the arguments (because we cannot realloc it) and
765  * appends the given argument.
766  *
767  */
768 static char **append_argument(char **original, char *argument) {
769         int num_args;
770         for (num_args = 0; original[num_args] != NULL; num_args++) {
771                 DLOG("original argument: \"%s\"\n", original[num_args]);
772                 /* If the argument is already present we return the original pointer */
773                 if (strcmp(original[num_args], argument) == 0)
774                         return original;
775         }
776         /* Copy the original array */
777         char **result = smalloc((num_args+2) * sizeof(char*));
778         memcpy(result, original, num_args * sizeof(char*));
779         result[num_args] = argument;
780         result[num_args+1] = NULL;
781
782         return result;
783 }
784
785 /* 
786  * Switch to next or previous existing workspace
787  *
788  */
789 static void next_previous_workspace(xcb_connection_t *conn, int direction) {
790         Workspace *ws = c_ws;
791
792         if (direction == 'n') {
793                 while ((ws = TAILQ_NEXT(ws, workspaces)) != TAILQ_END(workspaces_head)) {
794                         if (ws->screen == NULL)
795                                 continue;
796
797                         workspace_show(conn, ws->num + 1);
798                         return;
799                 }
800         } else if (direction == 'p') {
801                 while ((ws = TAILQ_PREV(ws, workspaces_head, workspaces)) != TAILQ_END(workspaces)) {
802                         if (ws->screen == NULL)
803                                 continue;
804
805                         workspace_show(conn, ws->num + 1);
806                         return;
807                 }
808         }
809 }
810
811 static void parse_resize_command(xcb_connection_t *conn, Client *last_focused, const char *command) {
812         int first, second;
813         resize_orientation_t orientation = O_VERTICAL;
814         Container *con = last_focused->container;
815         Workspace *ws = con->workspace;
816
817         if (STARTS_WITH(command, "left")) {
818                 if (con->col == 0)
819                         return;
820                 first = con->col - 1;
821                 second = con->col;
822                 command += strlen("left");
823         } else if (STARTS_WITH(command, "right")) {
824                 first = con->col + (con->colspan - 1);
825                 DLOG("column %d\n", first);
826
827                 if (!cell_exists(first, con->row) ||
828                     (first == (ws->cols-1)))
829                         return;
830
831                 second = first + 1;
832                 command += strlen("right");
833         } else if (STARTS_WITH(command, "top")) {
834                 if (con->row == 0)
835                         return;
836                 first = con->row - 1;
837                 second = con->row;
838                 orientation = O_HORIZONTAL;
839                 command += strlen("top");
840         } else if (STARTS_WITH(command, "bottom")) {
841                 first = con->row + (con->rowspan - 1);
842                 if (!cell_exists(con->col, first) ||
843                     (first == (ws->rows-1)))
844                         return;
845
846                 second = first + 1;
847                 orientation = O_HORIZONTAL;
848                 command += strlen("bottom");
849         } else {
850                 ELOG("Syntax: resize <left|right|top|bottom> [+|-]<pixels>\n");
851                 return;
852         }
853
854         int pixels = atoi(command);
855         if (pixels == 0)
856                 return;
857
858         resize_container(conn, ws, first, second, orientation, pixels);
859 }
860
861 /*
862  * Parses a command, see file CMDMODE for more information
863  *
864  */
865 void parse_command(xcb_connection_t *conn, const char *command) {
866         LOG("--- parsing command \"%s\" ---\n", command);
867         /* Get the first client from focus stack because floating clients are not
868          * in any container, therefore CUR_CELL is not appropriate. */
869         Client *last_focused = SLIST_FIRST(&(c_ws->focus_stack));
870         if (last_focused == SLIST_END(&(c_ws->focus_stack)))
871                 last_focused = NULL;
872
873         /* Hmm, just to be sure */
874         if (command[0] == '\0')
875                 return;
876
877         /* Is it an <exec>? Then execute the given command. */
878         if (STARTS_WITH(command, "exec ")) {
879                 LOG("starting \"%s\"\n", command + strlen("exec "));
880                 start_application(command+strlen("exec "));
881                 return;
882         }
883
884         if (STARTS_WITH(command, "mark")) {
885                 if (last_focused == NULL) {
886                         ELOG("There is no window to mark\n");
887                         return;
888                 }
889                 const char *rest = command + strlen("mark");
890                 while (*rest == ' ')
891                         rest++;
892                 if (*rest == '\0') {
893                         DLOG("interactive mark starting\n");
894                         start_application("i3-input -p 'mark ' -l 1 -P 'Mark: '");
895                 } else {
896                         LOG("mark with \"%s\"\n", rest);
897                         client_mark(conn, last_focused, rest);
898                 }
899                 return;
900         }
901
902         if (STARTS_WITH(command, "goto")) {
903                 const char *rest = command + strlen("goto");
904                 while (*rest == ' ')
905                         rest++;
906                 if (*rest == '\0') {
907                         DLOG("interactive go to mark starting\n");
908                         start_application("i3-input -p 'goto ' -l 1 -P 'Goto: '");
909                 } else {
910                         LOG("go to \"%s\"\n", rest);
911                         jump_to_mark(conn, rest);
912                 }
913                 return;
914         }
915
916         if (STARTS_WITH(command, "stack-limit ")) {
917                 if (last_focused == NULL || client_is_floating(last_focused)) {
918                         ELOG("No container focused\n");
919                         return;
920                 }
921                 const char *rest = command + strlen("stack-limit ");
922                 if (strncmp(rest, "rows ", strlen("rows ")) == 0) {
923                         last_focused->container->stack_limit = STACK_LIMIT_ROWS;
924                         rest += strlen("rows ");
925                 } else if (strncmp(rest, "cols ", strlen("cols ")) == 0) {
926                         last_focused->container->stack_limit = STACK_LIMIT_COLS;
927                         rest += strlen("cols ");
928                 } else {
929                         ELOG("Syntax: stack-limit <cols|rows> <limit>\n");
930                         return;
931                 }
932
933                 last_focused->container->stack_limit_value = atoi(rest);
934                 if (last_focused->container->stack_limit_value == 0)
935                         last_focused->container->stack_limit = STACK_LIMIT_NONE;
936
937                 return;
938         }
939
940         if (STARTS_WITH(command, "resize ")) {
941                 if (last_focused == NULL)
942                         return;
943                 const char *rest = command + strlen("resize ");
944                 parse_resize_command(conn, last_focused, rest);
945                 return;
946         }
947
948         if (STARTS_WITH(command, "mode ")) {
949                 const char *rest = command + strlen("mode ");
950                 switch_mode(conn, rest);
951                 return;
952         }
953
954         /* Is it an <exit>? */
955         if (STARTS_WITH(command, "exit")) {
956                 LOG("User issued exit-command, exiting without error.\n");
957                 exit(EXIT_SUCCESS);
958         }
959
960         /* Is it a <reload>? */
961         if (STARTS_WITH(command, "reload")) {
962                 load_configuration(conn, NULL, true);
963                 return;
964         }
965
966         /* Is it <restart>? Then restart in place. */
967         if (STARTS_WITH(command, "restart")) {
968                 LOG("restarting \"%s\"...\n", start_argv[0]);
969                 /* make sure -a is in the argument list or append it */
970                 start_argv = append_argument(start_argv, "-a");
971
972                 execvp(start_argv[0], start_argv);
973                 /* not reached */
974         }
975
976         if (STARTS_WITH(command, "kill")) {
977                 if (last_focused == NULL) {
978                         ELOG("There is no window to kill\n");
979                         return;
980                 }
981
982                 LOG("Killing current window\n");
983                 client_kill(conn, last_focused);
984                 return;
985         }
986
987         /* Is it a jump to a specified workspace, row, col? */
988         if (STARTS_WITH(command, "jump ")) {
989                 const char *arguments = command + strlen("jump ");
990                 if (arguments[0] == '"')
991                         jump_to_window(conn, arguments);
992                 else jump_to_container(conn, arguments);
993                 return;
994         }
995
996         /* Should we travel the focus stack? */
997         if (STARTS_WITH(command, "focus")) {
998                 const char *arguments = command + strlen("focus ");
999                 travel_focus_stack(conn, arguments);
1000                 return;
1001         }
1002
1003         /* Is it 'f' for fullscreen? */
1004         if (command[0] == 'f') {
1005                 if (last_focused == NULL)
1006                         return;
1007                 client_toggle_fullscreen(conn, last_focused);
1008                 return;
1009         }
1010
1011         /* Is it just 's' for stacking or 'd' for default? */
1012         if ((command[0] == 's' || command[0] == 'd' || command[0] == 'T') && (command[1] == '\0')) {
1013                 if (last_focused != NULL && client_is_floating(last_focused)) {
1014                         ELOG("not switching, this is a floating client\n");
1015                         return;
1016                 }
1017                 LOG("Switching mode for current container\n");
1018                 int new_mode = MODE_DEFAULT;
1019                 if (command[0] == 's')
1020                         new_mode = MODE_STACK;
1021                 if (command[0] == 'T')
1022                         new_mode = MODE_TABBED;
1023                 switch_layout_mode(conn, CUR_CELL, new_mode);
1024                 return;
1025         }
1026
1027         /* Is it 'bn' (border normal), 'bp' (border 1pixel) or 'bb' (border borderless)? */
1028         /* or even 'bt' (toggle border: 'bp' -> 'bb' -> 'bn' ) */
1029         if (command[0] == 'b') {
1030                 if (last_focused == NULL) {
1031                         ELOG("No window focused, cannot change border type\n");
1032                         return;
1033                 }
1034
1035                 char com = command[1];
1036                 if (command[1] == 't') {
1037                         if (last_focused->titlebar_position == TITLEBAR_TOP &&
1038                             !last_focused->borderless)
1039                             com = 'p';
1040                         else if (last_focused->titlebar_position == TITLEBAR_OFF &&
1041                                  !last_focused->borderless)
1042                             com = 'b';
1043                         else com = 'n';
1044                 }
1045
1046                 client_change_border(conn, last_focused, com);
1047                 return;
1048         }
1049
1050         if (command[0] == 'H') {
1051                 LOG("Hiding all floating windows\n");
1052                 floating_toggle_hide(conn, c_ws);
1053                 return;
1054         }
1055
1056         enum { WITH_WINDOW, WITH_CONTAINER, WITH_WORKSPACE, WITH_SCREEN } with = WITH_WINDOW;
1057
1058         /* Is it a <with>? */
1059         if (command[0] == 'w') {
1060                 command++;
1061                 /* TODO: implement */
1062                 if (command[0] == 'c') {
1063                         with = WITH_CONTAINER;
1064                         command++;
1065                 } else if (command[0] == 'w') {
1066                         with = WITH_WORKSPACE;
1067                         command++;
1068                 } else if (command[0] == 's') {
1069                         with = WITH_SCREEN;
1070                         command++;
1071                 } else {
1072                         ELOG("not yet implemented.\n");
1073                         return;
1074                 }
1075         }
1076
1077         /* Is it 't' for toggle tiling/floating? */
1078         if (command[0] == 't') {
1079                 if (with == WITH_WORKSPACE) {
1080                         c_ws->auto_float = !c_ws->auto_float;
1081                         LOG("autofloat is now %d\n", c_ws->auto_float);
1082                         return;
1083                 }
1084                 if (last_focused == NULL) {
1085                         ELOG("Cannot toggle tiling/floating: workspace empty\n");
1086                         return;
1087                 }
1088
1089                 Workspace *ws = last_focused->workspace;
1090
1091                 toggle_floating_mode(conn, last_focused, false);
1092                 /* delete all empty columns/rows */
1093                 cleanup_table(conn, ws);
1094
1095                 /* Fix colspan/rowspan if it’d overlap */
1096                 fix_colrowspan(conn, ws);
1097
1098                 render_workspace(conn, ws->screen, ws);
1099
1100                 /* Re-focus the client because cleanup_table sets the focus to the last
1101                  * focused client inside a container only. */
1102                 set_focus(conn, last_focused, true);
1103
1104                 return;
1105         }
1106
1107         /* Is it 'n' or 'p' for next/previous workspace? (nw) */
1108         if ((command[0] == 'n' || command[0] == 'p') && command[1] == 'w') {
1109                next_previous_workspace(conn, command[0]);
1110                return;
1111         }
1112
1113         /* It’s a normal <cmd> */
1114         char *rest = NULL;
1115         enum { ACTION_FOCUS, ACTION_MOVE, ACTION_SNAP } action = ACTION_FOCUS;
1116         direction_t direction;
1117         int times = strtol(command, &rest, 10);
1118         if (rest == NULL) {
1119                 ELOG("Invalid command (\"%s\")\n", command);
1120                 return;
1121         }
1122
1123         if (*rest == '\0') {
1124                 /* No rest? This was a workspace number, not a times specification */
1125                 workspace_show(conn, times);
1126                 return;
1127         }
1128
1129         if (*rest == 'm' || *rest == 's') {
1130                 action = (*rest == 'm' ? ACTION_MOVE : ACTION_SNAP);
1131                 rest++;
1132         }
1133
1134         int workspace = strtol(rest, &rest, 10);
1135
1136         if (rest == NULL) {
1137                 ELOG("Invalid command (\"%s\")\n", command);
1138                 return;
1139         }
1140
1141         if (*rest == '\0') {
1142                 if (last_focused != NULL && client_is_floating(last_focused))
1143                         move_floating_window_to_workspace(conn, last_focused, workspace);
1144                 else move_current_window_to_workspace(conn, workspace);
1145                 return;
1146         }
1147
1148         if (last_focused == NULL) {
1149                 ELOG("Not performing (no window found)\n");
1150                 return;
1151         }
1152
1153         if (client_is_floating(last_focused) &&
1154             (action != ACTION_FOCUS && action != ACTION_MOVE)) {
1155                 ELOG("Not performing (floating)\n");
1156                 return;
1157         }
1158
1159         /* Now perform action to <where> */
1160         while (*rest != '\0') {
1161                 if (*rest == 'h')
1162                         direction = D_LEFT;
1163                 else if (*rest == 'j')
1164                         direction = D_DOWN;
1165                 else if (*rest == 'k')
1166                         direction = D_UP;
1167                 else if (*rest == 'l')
1168                         direction = D_RIGHT;
1169                 else {
1170                         ELOG("unknown direction: %c\n", *rest);
1171                         return;
1172                 }
1173                 rest++;
1174
1175                 if (action == ACTION_FOCUS) {
1176                         if (with == WITH_SCREEN) {
1177                                 focus_thing(conn, direction, THING_SCREEN);
1178                                 continue;
1179                         }
1180                         if (client_is_floating(last_focused)) {
1181                                 floating_focus_direction(conn, last_focused, direction);
1182                                 continue;
1183                         }
1184                         focus_thing(conn, direction, (with == WITH_WINDOW ? THING_WINDOW : THING_CONTAINER));
1185                         continue;
1186                 }
1187
1188                 if (action == ACTION_MOVE) {
1189                         if (with == WITH_SCREEN) {
1190                                 /* TODO: this should swap the screen’s contents
1191                                  * (e.g. all workspaces) with the next/previous/…
1192                                  * screen */
1193                                 ELOG("Not yet implemented\n");
1194                                 continue;
1195                         }
1196                         if (client_is_floating(last_focused)) {
1197                                 floating_move(conn, last_focused, direction);
1198                                 continue;
1199                         }
1200                         if (with == WITH_WINDOW)
1201                                 move_current_window(conn, direction);
1202                         else move_current_container(conn, direction);
1203                         continue;
1204                 }
1205
1206                 if (action == ACTION_SNAP) {
1207                         if (with == WITH_SCREEN) {
1208                                 ELOG("You cannot snap a screen (it makes no sense).\n");
1209                                 continue;
1210                         }
1211                         snap_current_container(conn, direction);
1212                         continue;
1213                 }
1214         }
1215 }