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