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