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