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