]> git.sur5r.net Git - i3/i3/blob - src/commands.c
e0935525c8f863a297ee7aea75b6f6f3a29ec62c
[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
30 bool focus_window_in_container(xcb_connection_t *conn, Container *container, direction_t direction) {
31         /* If this container is empty, we’re done */
32         if (container->currently_focused == NULL)
33                 return false;
34
35         /* Get the previous/next client or wrap around */
36         Client *candidate = NULL;
37         if (direction == D_UP) {
38                 if ((candidate = CIRCLEQ_PREV_OR_NULL(&(container->clients), container->currently_focused, clients)) == NULL)
39                         candidate = CIRCLEQ_LAST(&(container->clients));
40         }
41         else if (direction == D_DOWN) {
42                 if ((candidate = CIRCLEQ_NEXT_OR_NULL(&(container->clients), container->currently_focused, clients)) == NULL)
43                         candidate = CIRCLEQ_FIRST(&(container->clients));
44         } else LOG("Direction not implemented!\n");
45
46         /* If we could not switch, the container contains exactly one client. We return false */
47         if (candidate == container->currently_focused)
48                 return false;
49
50         /* Set focus */
51         set_focus(conn, candidate, true);
52
53         return true;
54 }
55
56 typedef enum { THING_WINDOW, THING_CONTAINER } thing_t;
57
58 static void focus_thing(xcb_connection_t *conn, direction_t direction, thing_t thing) {
59         LOG("focusing direction %d\n", direction);
60
61         int new_row = current_row,
62             new_col = current_col;
63
64         Container *container = CUR_CELL;
65         Workspace *t_ws = c_ws;
66
67         /* There always is a container. If not, current_col or current_row is wrong */
68         assert(container != NULL);
69
70         if (container->workspace->fullscreen_client != NULL) {
71                 LOG("You're in fullscreen mode. Won't switch focus\n");
72                 return;
73         }
74
75         /* TODO: for horizontal default layout, this has to be expanded to LEFT/RIGHT */
76         if (direction == D_UP || direction == D_DOWN) {
77                 if (thing == THING_WINDOW)
78                         /* Let’s see if we can perform up/down focus in the current container */
79                         if (focus_window_in_container(conn, container, direction))
80                                 return;
81
82                 if (direction == D_DOWN && cell_exists(current_col, current_row+1))
83                         new_row = current_row + t_ws->table[current_col][current_row]->rowspan;
84                 else if (direction == D_UP && cell_exists(current_col, current_row-1)) {
85                         /* Set new_row as a sane default, but it may get overwritten in a second */
86                         new_row--;
87
88                         /* Search from the top to correctly handle rowspanned containers */
89                         for (int rows = 0; rows < current_row; rows += t_ws->table[current_col][rows]->rowspan) {
90                                 if (new_row > (rows + (t_ws->table[current_col][rows]->rowspan - 1)))
91                                         continue;
92
93                                 new_row = rows;
94                                 break;
95                         }
96                 } else {
97                         /* Let’s see if there is a screen down/up there to which we can switch */
98                         LOG("container is at %d with height %d\n", container->y, container->height);
99                         i3Screen *screen;
100                         int destination_y = (direction == D_UP ? (container->y - 1) : (container->y + container->height + 1));
101                         if ((screen = get_screen_containing(container->x, destination_y)) == NULL) {
102                                 LOG("Wrapping screen around vertically\n");
103                                 /* No screen found? Then wrap */
104                                 screen = get_screen_most((direction == D_UP ? D_DOWN : D_UP));
105                         }
106                         t_ws = &(workspaces[screen->current_workspace]);
107                         new_row = (direction == D_UP ? (t_ws->rows - 1) : 0);
108                 }
109
110                 LOG("new_col = %d, new_row = %d\n", new_col, new_row);
111                 if (t_ws->table[new_col][new_row]->currently_focused == NULL) {
112                         LOG("Cell empty, checking for colspanned client above...\n");
113                         for (int cols = 0; cols < new_col; cols += t_ws->table[cols][new_row]->colspan) {
114                                 if (new_col > (cols + (t_ws->table[cols][new_row]->colspan - 1)))
115                                         continue;
116
117                                 new_col = cols;
118                                 break;
119                         }
120                         LOG("Fixed it to new col %d\n", new_col);
121                 }
122         } else if (direction == D_LEFT || direction == D_RIGHT) {
123                 if (direction == D_RIGHT && cell_exists(current_col+1, current_row))
124                         new_col = current_col + t_ws->table[current_col][current_row]->colspan;
125                 else if (direction == D_LEFT && cell_exists(current_col-1, current_row)) {
126                         /* Set new_col as a sane default, but it may get overwritten in a second */
127                         new_col--;
128
129                         /* Search from the left to correctly handle colspanned containers */
130                         for (int cols = 0; cols < current_col; cols += t_ws->table[cols][current_row]->colspan) {
131                                 if (new_col > (cols + (t_ws->table[cols][current_row]->colspan - 1)))
132                                         continue;
133
134                                 new_col = cols;
135                                 break;
136                         }
137                 } else {
138                         /* Let’s see if there is a screen left/right here to which we can switch */
139                         LOG("container is at %d with width %d\n", container->x, container->width);
140                         i3Screen *screen;
141                         int destination_x = (direction == D_LEFT ? (container->x - 1) : (container->x + container->width + 1));
142                         if ((screen = get_screen_containing(destination_x, container->y)) == NULL) {
143                                 LOG("Wrapping screen around horizontally\n");
144                                 screen = get_screen_most((direction == D_LEFT ? D_RIGHT : D_LEFT));
145                         }
146                         t_ws = &(workspaces[screen->current_workspace]);
147                         new_col = (direction == D_LEFT ? (t_ws->cols - 1) : 0);
148                 }
149
150                 LOG("new_col = %d, new_row = %d\n", new_col, new_row);
151                 if (t_ws->table[new_col][new_row]->currently_focused == NULL) {
152                         LOG("Cell empty, checking for rowspanned client above...\n");
153                         for (int rows = 0; rows < new_row; rows += t_ws->table[new_col][rows]->rowspan) {
154                                 if (new_row > (rows + (t_ws->table[new_col][rows]->rowspan - 1)))
155                                         continue;
156
157                                 new_row = rows;
158                                 break;
159                         }
160                         LOG("Fixed it to new row %d\n", new_row);
161                 }
162         } else {
163                 LOG("direction unhandled\n");
164                 return;
165         }
166
167         /* Bounds checking */
168         if (new_col >= t_ws->cols)
169                 new_col = (t_ws->cols - 1);
170         if (new_row >= t_ws->rows)
171                 new_row = (t_ws->rows - 1);
172
173         if (t_ws->table[new_col][new_row]->currently_focused != NULL)
174                 set_focus(conn, t_ws->table[new_col][new_row]->currently_focused, true);
175 }
176
177 /*
178  * Tries to move the window inside its current container.
179  *
180  * Returns true if the window could be moved, false otherwise.
181  *
182  */
183 static bool move_current_window_in_container(xcb_connection_t *conn, Client *client,
184                 direction_t direction) {
185         assert(client->container != NULL);
186
187         Client *other = (direction == D_UP ? CIRCLEQ_PREV(client, clients) :
188                                              CIRCLEQ_NEXT(client, clients));
189
190         if (other == CIRCLEQ_END(&(client->container->clients)))
191                 return false;
192
193         LOG("i can do that\n");
194         /* We can move the client inside its current container */
195         CIRCLEQ_REMOVE(&(client->container->clients), client, clients);
196         if (direction == D_UP)
197                 CIRCLEQ_INSERT_BEFORE(&(client->container->clients), other, client, clients);
198         else CIRCLEQ_INSERT_AFTER(&(client->container->clients), other, client, clients);
199         render_layout(conn);
200         return true;
201 }
202
203 /*
204  * Moves the current window or whole container to the given direction, creating a column/row if
205  * necessary.
206  *
207  */
208 static void move_current_window(xcb_connection_t *conn, direction_t direction) {
209         LOG("moving window to direction %s\n", (direction == D_UP ? "up" : (direction == D_DOWN ? "down" :
210                                             (direction == D_LEFT ? "left" : "right"))));
211         /* Get current window */
212         Container *container = CUR_CELL,
213                   *new = NULL;
214
215         /* There has to be a container, see focus_window() */
216         assert(container != NULL);
217
218         /* If there is no window or the dock window is focused, we’re done */
219         if (container->currently_focused == NULL ||
220             container->currently_focused->dock)
221                 return;
222
223         /* As soon as the client is moved away, the last focused client in the old
224          * container needs to get focus, if any. Therefore, we save it here. */
225         Client *current_client = container->currently_focused;
226         Client *to_focus = get_last_focused_client(conn, container, current_client);
227
228         if (to_focus == NULL) {
229                 to_focus = CIRCLEQ_NEXT_OR_NULL(&(container->clients), current_client, clients);
230                 if (to_focus == NULL)
231                         to_focus = CIRCLEQ_PREV_OR_NULL(&(container->clients), current_client, clients);
232         }
233
234         switch (direction) {
235                 case D_LEFT:
236                         /* If we’re at the left-most position, move the rest of the table right */
237                         if (current_col == 0) {
238                                 expand_table_cols_at_head(c_ws);
239                                 new = CUR_CELL;
240                         } else
241                                 new = CUR_TABLE[--current_col][current_row];
242                         break;
243                 case D_RIGHT:
244                         if (current_col == (c_ws->cols-1))
245                                 expand_table_cols(c_ws);
246
247                         new = CUR_TABLE[++current_col][current_row];
248                         break;
249                 case D_UP:
250                         if (move_current_window_in_container(conn, current_client, D_UP))
251                                 return;
252
253                         /* if we’re at the up-most position, move the rest of the table down */
254                         if (current_row == 0) {
255                                 expand_table_rows_at_head(c_ws);
256                                 new = CUR_CELL;
257                         } else
258                                 new = CUR_TABLE[current_col][--current_row];
259                         break;
260                 case D_DOWN:
261                         if (move_current_window_in_container(conn, current_client, D_DOWN))
262                                 return;
263
264                         if (current_row == (c_ws->rows-1))
265                                 expand_table_rows(c_ws);
266
267                         new = CUR_TABLE[current_col][++current_row];
268                         break;
269                 /* To make static analyzers happy: */
270                 default:
271                         return;
272         }
273
274         /* Remove it from the old container and put it into the new one */
275         client_remove_from_container(conn, current_client, container, true);
276
277         if (new->currently_focused != NULL)
278                 CIRCLEQ_INSERT_AFTER(&(new->clients), new->currently_focused, current_client, clients);
279         else CIRCLEQ_INSERT_TAIL(&(new->clients), current_client, clients);
280         SLIST_INSERT_HEAD(&(new->workspace->focus_stack), current_client, focus_clients);
281
282         /* Update data structures */
283         current_client->container = new;
284         current_client->workspace = new->workspace;
285         container->currently_focused = to_focus;
286         new->currently_focused = current_client;
287
288         Workspace *workspace = container->workspace;
289
290         /* delete all empty columns/rows */
291         cleanup_table(conn, workspace);
292
293         /* Fix colspan/rowspan if it’d overlap */
294         fix_colrowspan(conn, workspace);
295
296         render_workspace(conn, workspace->screen, workspace);
297         xcb_flush(conn);
298
299         set_focus(conn, current_client, true);
300 }
301
302 static void move_current_container(xcb_connection_t *conn, direction_t direction) {
303         LOG("moving container to direction %s\n", (direction == D_UP ? "up" : (direction == D_DOWN ? "down" :
304                                             (direction == D_LEFT ? "left" : "right"))));
305         /* Get current window */
306         Container *container = CUR_CELL,
307                   *new = NULL;
308
309         Container **old = &CUR_CELL;
310
311         /* There has to be a container, see focus_window() */
312         assert(container != NULL);
313
314         switch (direction) {
315                 case D_LEFT:
316                         /* If we’re at the left-most position, move the rest of the table right */
317                         if (current_col == 0) {
318                                 expand_table_cols_at_head(c_ws);
319                                 new = CUR_CELL;
320                                 old = &CUR_TABLE[current_col+1][current_row];
321                         } else
322                                 new = CUR_TABLE[--current_col][current_row];
323                         break;
324                 case D_RIGHT:
325                         if (current_col == (c_ws->cols-1))
326                                 expand_table_cols(c_ws);
327
328                         new = CUR_TABLE[++current_col][current_row];
329                         break;
330                 case D_UP:
331                         /* if we’re at the up-most position, move the rest of the table down */
332                         if (current_row == 0) {
333                                 expand_table_rows_at_head(c_ws);
334                                 new = CUR_CELL;
335                                 old = &CUR_TABLE[current_col][current_row+1];
336                         } else
337                                 new = CUR_TABLE[current_col][--current_row];
338                         break;
339                 case D_DOWN:
340                         if (current_row == (c_ws->rows-1))
341                                 expand_table_rows(c_ws);
342
343                         new = CUR_TABLE[current_col][++current_row];
344                         break;
345                 /* To make static analyzers happy: */
346                 default:
347                         return;
348         }
349
350         LOG("old = %d,%d and new = %d,%d\n", container->col, container->row, new->col, new->row);
351
352         /* Swap the containers */
353         int col = new->col;
354         int row = new->row;
355
356         *old = new;
357         new->col = container->col;
358         new->row = container->row;
359
360         CUR_CELL = container;
361         container->col = col;
362         container->row = row;
363
364         Workspace *workspace = container->workspace;
365
366         /* delete all empty columns/rows */
367         cleanup_table(conn, workspace);
368
369         /* Fix colspan/rowspan if it’d overlap */
370         fix_colrowspan(conn, workspace);
371
372         render_layout(conn);
373 }
374
375 /*
376  * "Snaps" the current container (not possible for windows, because it works at table base)
377  * to the given direction, that is, adjusts cellspan/rowspan
378  *
379  */
380 static void snap_current_container(xcb_connection_t *conn, direction_t direction) {
381         LOG("snapping container to direction %d\n", direction);
382
383         Container *container = CUR_CELL;
384
385         assert(container != NULL);
386
387         switch (direction) {
388                 case D_LEFT:
389                         /* Snap to the left is actually a move to the left and then a snap right */
390                         if (!cell_exists(container->col - 1, container->row) ||
391                             CUR_TABLE[container->col-1][container->row]->currently_focused != NULL) {
392                                 LOG("cannot snap to left - the cell is already used\n");
393                                 return;
394                         }
395
396                         move_current_window(conn, D_LEFT);
397                         snap_current_container(conn, D_RIGHT);
398                         return;
399                 case D_RIGHT: {
400                         /* Check if the cell is used */
401                         int new_col = container->col + container->colspan;
402                         for (int i = 0; i < container->rowspan; i++)
403                                 if (!cell_exists(new_col, container->row + i) ||
404                                     CUR_TABLE[new_col][container->row + i]->currently_focused != NULL) {
405                                         LOG("cannot snap to right - the cell is already used\n");
406                                         return;
407                                 }
408
409                         /* Check if there are other cells with rowspan, which are in our way.
410                          * If so, reduce their rowspan. */
411                         for (int i = container->row-1; i >= 0; i--) {
412                                 LOG("we got cell %d, %d with rowspan %d\n",
413                                                 new_col, i, CUR_TABLE[new_col][i]->rowspan);
414                                 while ((CUR_TABLE[new_col][i]->rowspan-1) >= (container->row - i))
415                                         CUR_TABLE[new_col][i]->rowspan--;
416                                 LOG("new rowspan = %d\n", CUR_TABLE[new_col][i]->rowspan);
417                         }
418
419                         container->colspan++;
420                         break;
421                 }
422                 case D_UP:
423                         if (!cell_exists(container->col, container->row - 1) ||
424                             CUR_TABLE[container->col][container->row-1]->currently_focused != NULL) {
425                                 LOG("cannot snap to top - the cell is already used\n");
426                                 return;
427                         }
428
429                         move_current_window(conn, D_UP);
430                         snap_current_container(conn, D_DOWN);
431                         return;
432                 case D_DOWN: {
433                         LOG("snapping down\n");
434                         int new_row = container->row + container->rowspan;
435                         for (int i = 0; i < container->colspan; i++)
436                                 if (!cell_exists(container->col + i, new_row) ||
437                                     CUR_TABLE[container->col + i][new_row]->currently_focused != NULL) {
438                                         LOG("cannot snap down - the cell is already used\n");
439                                         return;
440                                 }
441
442                         for (int i = container->col-1; i >= 0; i--) {
443                                 LOG("we got cell %d, %d with colspan %d\n",
444                                                 i, new_row, CUR_TABLE[i][new_row]->colspan);
445                                 while ((CUR_TABLE[i][new_row]->colspan-1) >= (container->col - i))
446                                         CUR_TABLE[i][new_row]->colspan--;
447                                 LOG("new colspan = %d\n", CUR_TABLE[i][new_row]->colspan);
448
449                         }
450
451                         container->rowspan++;
452                         break;
453                 }
454                 /* To make static analyzers happy: */
455                 default:
456                         return;
457         }
458
459         render_layout(conn);
460 }
461
462 static void move_floating_window_to_workspace(xcb_connection_t *conn, Client *client, int workspace) {
463         /* t_ws (to workspace) is just a container pointer to the workspace we’re switching to */
464         Workspace *t_ws = &(workspaces[workspace-1]),
465                   *old_ws = client->workspace;
466
467         LOG("moving floating\n");
468
469         if (t_ws->screen == NULL) {
470                 LOG("initializing new workspace, setting num to %d\n", workspace-1);
471                 t_ws->screen = c_ws->screen;
472                 /* Copy the dimensions from the virtual screen */
473                 memcpy(&(t_ws->rect), &(t_ws->screen->rect), sizeof(Rect));
474         } else {
475                 /* Check if there is already a fullscreen client on the destination workspace and
476                  * stop moving if so. */
477                 if (client->fullscreen && (t_ws->fullscreen_client != NULL)) {
478                         LOG("Not moving: Fullscreen client already existing on destination workspace.\n");
479                         return;
480                 }
481         }
482
483         floating_assign_to_workspace(client, t_ws);
484
485         bool target_invisible = t_ws->screen->current_workspace != t_ws->num;
486
487         /* If we’re moving it to an invisible screen, we need to unmap it */
488         if (target_invisible) {
489                 LOG("This workspace is not visible, unmapping\n");
490                 xcb_unmap_window(conn, client->frame);
491         } else {
492                 /* If this is not the case, we move the window to a workspace
493                  * which is on another screen, so we also need to adjust its
494                  * coordinates. */
495                 LOG("before x = %d, y = %d\n", client->rect.x, client->rect.y);
496                 uint32_t relative_x = client->rect.x - old_ws->rect.x,
497                          relative_y = client->rect.y - old_ws->rect.y;
498                 LOG("rel_x = %d, rel_y = %d\n", relative_x, relative_y);
499                 client->rect.x = t_ws->rect.x + relative_x;
500                 client->rect.y = t_ws->rect.y + relative_y;
501                 LOG("after x = %d, y = %d\n", client->rect.x, client->rect.y);
502                 reposition_client(conn, client);
503                 xcb_flush(conn);
504         }
505
506         LOG("done\n");
507
508         render_layout(conn);
509
510         if (!target_invisible)
511                 set_focus(conn, client, true);
512 }
513
514 /*
515  * Moves the currently selected window to the given workspace
516  *
517  */
518 static void move_current_window_to_workspace(xcb_connection_t *conn, int workspace) {
519         LOG("Moving current window to workspace %d\n", workspace);
520
521         Container *container = CUR_CELL;
522
523         assert(container != NULL);
524
525         /* t_ws (to workspace) is just a container pointer to the workspace we’re switching to */
526         Workspace *t_ws = &(workspaces[workspace-1]);
527
528         Client *current_client = container->currently_focused;
529         if (current_client == NULL) {
530                 LOG("No currently focused client in current container.\n");
531                 return;
532         }
533         Client *to_focus = CIRCLEQ_NEXT_OR_NULL(&(container->clients), current_client, clients);
534         if (to_focus == NULL)
535                 to_focus = CIRCLEQ_PREV_OR_NULL(&(container->clients), current_client, clients);
536
537         if (t_ws->screen == NULL) {
538                 LOG("initializing new workspace, setting num to %d\n", workspace-1);
539                 t_ws->screen = container->workspace->screen;
540                 /* Copy the dimensions from the virtual screen */
541                 memcpy(&(t_ws->rect), &(container->workspace->screen->rect), sizeof(Rect));
542         } else {
543                 /* Check if there is already a fullscreen client on the destination workspace and
544                  * stop moving if so. */
545                 if (current_client->fullscreen && (t_ws->fullscreen_client != NULL)) {
546                         LOG("Not moving: Fullscreen client already existing on destination workspace.\n");
547                         return;
548                 }
549         }
550
551         Container *to_container = t_ws->table[t_ws->current_col][t_ws->current_row];
552
553         assert(to_container != NULL);
554
555         client_remove_from_container(conn, current_client, container, true);
556         if (container->workspace->fullscreen_client == current_client)
557                 container->workspace->fullscreen_client = NULL;
558
559         /* TODO: insert it to the correct position */
560         CIRCLEQ_INSERT_TAIL(&(to_container->clients), current_client, clients);
561
562         SLIST_INSERT_HEAD(&(to_container->workspace->focus_stack), current_client, focus_clients);
563         LOG("Moved.\n");
564
565         current_client->container = to_container;
566         current_client->workspace = to_container->workspace;
567         container->currently_focused = to_focus;
568         to_container->currently_focused = current_client;
569
570         bool target_invisible = (to_container->workspace->screen->current_workspace != to_container->workspace->num);
571
572         /* If we’re moving it to an invisible screen, we need to unmap it */
573         if (target_invisible) {
574                 LOG("This workspace is not visible, unmapping\n");
575                 xcb_unmap_window(conn, current_client->frame);
576         } else {
577                 if (current_client->fullscreen) {
578                         LOG("Calling client_enter_fullscreen again\n");
579                         client_enter_fullscreen(conn, current_client);
580                 }
581         }
582
583         /* delete all empty columns/rows */
584         cleanup_table(conn, container->workspace);
585
586         render_layout(conn);
587
588         if (!target_invisible)
589                 set_focus(conn, current_client, true);
590 }
591
592 /*
593  * Switches to the given workspace
594  *
595  */
596 void show_workspace(xcb_connection_t *conn, int workspace) {
597         Client *client;
598         bool need_warp = false;
599         xcb_window_t root = xcb_setup_roots_iterator(xcb_get_setup(conn)).data->root;
600         /* t_ws (to workspace) is just a convenience pointer to the workspace we’re switching to */
601         Workspace *t_ws = &(workspaces[workspace-1]);
602
603         LOG("show_workspace(%d)\n", workspace);
604
605         /* Store current_row/current_col */
606         c_ws->current_row = current_row;
607         c_ws->current_col = current_col;
608
609         /* Check if the workspace has not been used yet */
610         if (t_ws->screen == NULL) {
611                 LOG("initializing new workspace, setting num to %d\n", workspace);
612                 t_ws->screen = c_ws->screen;
613                 /* Copy the dimensions from the virtual screen */
614                 memcpy(&(t_ws->rect), &(t_ws->screen->rect), sizeof(Rect));
615         }
616
617         if (c_ws->screen != t_ws->screen) {
618                 /* We need to switch to the other screen first */
619                 LOG("moving over to other screen.\n");
620
621                 /* Store the old client */
622                 Client *old_client = CUR_CELL->currently_focused;
623
624                 c_ws = &(workspaces[t_ws->screen->current_workspace]);
625                 current_col = c_ws->current_col;
626                 current_row = c_ws->current_row;
627                 if (CUR_CELL->currently_focused != NULL)
628                         need_warp = true;
629                 else {
630                         Rect *dims = &(c_ws->screen->rect);
631                         xcb_warp_pointer(conn, XCB_NONE, root, 0, 0, 0, 0,
632                                          dims->x + (dims->width / 2), dims->y + (dims->height / 2));
633                 }
634
635                 /* Re-decorate the old client, it’s not focused anymore */
636                 if ((old_client != NULL) && !old_client->dock)
637                         redecorate_window(conn, old_client);
638                 else xcb_flush(conn);
639         }
640
641         /* Check if we need to change something or if we’re already there */
642         if (c_ws->screen->current_workspace == (workspace-1)) {
643                 Client *last_focused = SLIST_FIRST(&(c_ws->focus_stack));
644                 if (last_focused != SLIST_END(&(c_ws->focus_stack))) {
645                         set_focus(conn, last_focused, true);
646                         if (need_warp) {
647                                 client_warp_pointer_into(conn, last_focused);
648                                 xcb_flush(conn);
649                         }
650                 }
651
652                 return;
653         }
654
655         t_ws->screen->current_workspace = workspace-1;
656         Workspace *old_workspace = c_ws;
657         c_ws = &workspaces[workspace-1];
658
659         /* Unmap all clients of the old workspace */
660         unmap_workspace(conn, old_workspace);
661
662         current_row = c_ws->current_row;
663         current_col = c_ws->current_col;
664         LOG("new current row = %d, current col = %d\n", current_row, current_col);
665
666         ignore_enter_notify_forall(conn, c_ws, true);
667
668         /* Map all clients on the new workspace */
669         FOR_TABLE(c_ws)
670                 CIRCLEQ_FOREACH(client, &(c_ws->table[cols][rows]->clients), clients)
671                         xcb_map_window(conn, client->frame);
672
673         /* Map all floating clients */
674         if (!c_ws->floating_hidden)
675                 TAILQ_FOREACH(client, &(c_ws->floating_clients), floating_clients)
676                         xcb_map_window(conn, client->frame);
677
678         /* Map all stack windows, if any */
679         struct Stack_Window *stack_win;
680         SLIST_FOREACH(stack_win, &stack_wins, stack_windows)
681                 if (stack_win->container->workspace == c_ws)
682                         xcb_map_window(conn, stack_win->window);
683
684         ignore_enter_notify_forall(conn, c_ws, false);
685
686         /* Restore focus on the new workspace */
687         Client *last_focused = SLIST_FIRST(&(c_ws->focus_stack));
688         if (last_focused != SLIST_END(&(c_ws->focus_stack))) {
689                 set_focus(conn, last_focused, true);
690                 if (need_warp) {
691                         client_warp_pointer_into(conn, last_focused);
692                         xcb_flush(conn);
693                 }
694         } else xcb_set_input_focus(conn, XCB_INPUT_FOCUS_POINTER_ROOT, root, XCB_CURRENT_TIME);
695
696         render_layout(conn);
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                 LOG("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                 LOG("At least one valid argument required\n");
740                 return;
741         }
742
743         /* Move to the target workspace */
744         show_workspace(conn, ws);
745
746         if (result < 3)
747                 return;
748
749         LOG("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         LOG("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                         LOG("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                         LOG("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                                 LOG("Skipping\n");
801                                 continue;
802                         }
803
804                         LOG("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  * Goes through the list of arguments (for exec()) and checks if the given argument
821  * is present. If not, it copies the arguments (because we cannot realloc it) and
822  * appends the given argument.
823  *
824  */
825 static char **append_argument(char **original, char *argument) {
826         int num_args;
827         for (num_args = 0; original[num_args] != NULL; num_args++) {
828                 LOG("original argument: \"%s\"\n", original[num_args]);
829                 /* If the argument is already present we return the original pointer */
830                 if (strcmp(original[num_args], argument) == 0)
831                         return original;
832         }
833         /* Copy the original array */
834         char **result = smalloc((num_args+2) * sizeof(char*));
835         memcpy(result, original, num_args * sizeof(char*));
836         result[num_args] = argument;
837         result[num_args+1] = NULL;
838
839         return result;
840 }
841
842 /*
843  * Parses a command, see file CMDMODE for more information
844  *
845  */
846 void parse_command(xcb_connection_t *conn, const char *command) {
847         LOG("--- parsing command \"%s\" ---\n", command);
848         /* Get the first client from focus stack because floating clients are not
849          * in any container, therefore CUR_CELL is not appropriate. */
850         Client *last_focused = SLIST_FIRST(&(c_ws->focus_stack));
851         if (last_focused == SLIST_END(&(c_ws->focus_stack)))
852                 last_focused = NULL;
853
854         /* Hmm, just to be sure */
855         if (command[0] == '\0')
856                 return;
857
858         /* Is it an <exec>? Then execute the given command. */
859         if (STARTS_WITH(command, "exec ")) {
860                 LOG("starting \"%s\"\n", command + strlen("exec "));
861                 start_application(command+strlen("exec "));
862                 return;
863         }
864
865         /* Is it an <exit>? */
866         if (STARTS_WITH(command, "exit")) {
867                 LOG("User issued exit-command, exiting without error.\n");
868                 exit(EXIT_SUCCESS);
869         }
870
871         /* Is it <restart>? Then restart in place. */
872         if (STARTS_WITH(command, "restart")) {
873                 LOG("restarting \"%s\"...\n", start_argv[0]);
874                 /* make sure -a is in the argument list or append it */
875                 start_argv = append_argument(start_argv, "-a");
876
877                 execvp(start_argv[0], start_argv);
878                 /* not reached */
879         }
880
881         if (STARTS_WITH(command, "kill")) {
882                 if (last_focused == NULL) {
883                         LOG("There is no window to kill\n");
884                         return;
885                 }
886
887                 LOG("Killing current window\n");
888                 client_kill(conn, last_focused);
889                 return;
890         }
891
892         /* Is it a jump to a specified workspace, row, col? */
893         if (STARTS_WITH(command, "jump ")) {
894                 const char *arguments = command + strlen("jump ");
895                 if (arguments[0] == '"')
896                         jump_to_window(conn, arguments);
897                 else jump_to_container(conn, arguments);
898                 return;
899         }
900
901         /* Should we travel the focus stack? */
902         if (STARTS_WITH(command, "focus")) {
903                 const char *arguments = command + strlen("focus ");
904                 travel_focus_stack(conn, arguments);
905                 return;
906         }
907
908         /* Is it 'f' for fullscreen? */
909         if (command[0] == 'f') {
910                 if (last_focused == NULL)
911                         return;
912                 client_toggle_fullscreen(conn, last_focused);
913                 return;
914         }
915
916         /* Is it just 's' for stacking or 'd' for default? */
917         if ((command[0] == 's' || command[0] == 'd') && (command[1] == '\0')) {
918                 if (last_focused == NULL || client_is_floating(last_focused)) {
919                         LOG("not switching, this is a floating client\n");
920                         return;
921                 }
922                 LOG("Switching mode for current container\n");
923                 switch_layout_mode(conn, CUR_CELL, (command[0] == 's' ? MODE_STACK : MODE_DEFAULT));
924                 return;
925         }
926
927         if (command[0] == 'H') {
928                 LOG("Hiding all floating windows\n");
929                 floating_toggle_hide(conn, c_ws);
930                 return;
931         }
932
933         enum { WITH_WINDOW, WITH_CONTAINER, WITH_WORKSPACE } with = WITH_WINDOW;
934
935         /* Is it a <with>? */
936         if (command[0] == 'w') {
937                 command++;
938                 /* TODO: implement */
939                 if (command[0] == 'c') {
940                         with = WITH_CONTAINER;
941                         command++;
942                 } else if (command[0] == 'w') {
943                         with = WITH_WORKSPACE;
944                         command++;
945                 } else {
946                         LOG("not yet implemented.\n");
947                         return;
948                 }
949         }
950
951         /* Is it 't' for toggle tiling/floating? */
952         if (command[0] == 't') {
953                 if (with == WITH_WORKSPACE) {
954                         c_ws->auto_float = !c_ws->auto_float;
955                         LOG("autofloat is now %d\n", c_ws->auto_float);
956                         return;
957                 }
958                 if (last_focused == NULL) {
959                         LOG("Cannot toggle tiling/floating: workspace empty\n");
960                         return;
961                 }
962
963                 toggle_floating_mode(conn, last_focused, false);
964                 /* delete all empty columns/rows */
965                 cleanup_table(conn, last_focused->workspace);
966
967                 /* Fix colspan/rowspan if it’d overlap */
968                 fix_colrowspan(conn, last_focused->workspace);
969
970                 render_workspace(conn, last_focused->workspace->screen, last_focused->workspace);
971                 xcb_flush(conn);
972                 return;
973         }
974
975         /* It’s a normal <cmd> */
976         char *rest = NULL;
977         enum { ACTION_FOCUS, ACTION_MOVE, ACTION_SNAP } action = ACTION_FOCUS;
978         direction_t direction;
979         int times = strtol(command, &rest, 10);
980         if (rest == NULL) {
981                 LOG("Invalid command (\"%s\")\n", command);
982                 return;
983         }
984
985         if (*rest == '\0') {
986                 /* No rest? This was a workspace number, not a times specification */
987                 show_workspace(conn, times);
988                 return;
989         }
990
991         if (*rest == 'm' || *rest == 's') {
992                 action = (*rest == 'm' ? ACTION_MOVE : ACTION_SNAP);
993                 rest++;
994         }
995
996         int workspace = strtol(rest, &rest, 10);
997
998         if (rest == NULL) {
999                 LOG("Invalid command (\"%s\")\n", command);
1000                 return;
1001         }
1002
1003         if (*rest == '\0') {
1004                 if (last_focused != NULL && client_is_floating(last_focused))
1005                         move_floating_window_to_workspace(conn, last_focused, workspace);
1006                 else move_current_window_to_workspace(conn, workspace);
1007                 return;
1008         }
1009
1010         if (last_focused == NULL) {
1011                 LOG("Not performing (no window found)\n");
1012                 return;
1013         }
1014
1015         if (client_is_floating(last_focused) &&
1016             (action != ACTION_FOCUS && action != ACTION_MOVE)) {
1017                 LOG("Not performing (floating)\n");
1018                 return;
1019         }
1020
1021         /* Now perform action to <where> */
1022         while (*rest != '\0') {
1023                 if (*rest == 'h')
1024                         direction = D_LEFT;
1025                 else if (*rest == 'j')
1026                         direction = D_DOWN;
1027                 else if (*rest == 'k')
1028                         direction = D_UP;
1029                 else if (*rest == 'l')
1030                         direction = D_RIGHT;
1031                 else {
1032                         LOG("unknown direction: %c\n", *rest);
1033                         return;
1034                 }
1035                 rest++;
1036
1037                 if (action == ACTION_FOCUS) {
1038                         if (client_is_floating(last_focused)) {
1039                                 floating_focus_direction(conn, last_focused, direction);
1040                                 continue;
1041                         }
1042                         focus_thing(conn, direction, (with == WITH_WINDOW ? THING_WINDOW : THING_CONTAINER));
1043                         continue;
1044                 }
1045
1046                 if (action == ACTION_MOVE) {
1047                         if (client_is_floating(last_focused)) {
1048                                 floating_move(conn, last_focused, direction);
1049                                 continue;
1050                         }
1051                         if (with == WITH_WINDOW)
1052                                 move_current_window(conn, direction);
1053                         else move_current_container(conn, direction);
1054                         continue;
1055                 }
1056
1057                 if (action == ACTION_SNAP) {
1058                         snap_current_container(conn, direction);
1059                         continue;
1060                 }
1061         }
1062
1063         LOG("--- done ---\n");
1064 }