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