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