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