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