]> git.sur5r.net Git - i3/i3/blob - src/commands.c
2a2399d74f6e5cd4cf6bf6bdaba1cb0d2fc14194
[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                 client_warp_pointer_into(conn, current_client);
641                 set_focus(conn, current_client, true);
642         }
643 }
644
645 /*
646  * Jumps to the given window class / title.
647  * Title is matched using strstr, that is, matches if it appears anywhere
648  * in the string. Regular expressions seem to be a bit overkill here. However,
649  * if we need them for something else somewhen, we may introduce them here, too.
650  *
651  */
652 static void jump_to_window(xcb_connection_t *conn, const char *arguments) {
653         char *classtitle;
654         Client *client;
655
656         /* The first character is a quote, this was checked before */
657         classtitle = sstrdup(arguments+1);
658         /* The last character is a quote, we just set it to NULL */
659         classtitle[strlen(classtitle)-1] = '\0';
660
661         if ((client = get_matching_client(conn, classtitle, NULL)) == NULL) {
662                 free(classtitle);
663                 ELOG("No matching client found.\n");
664                 return;
665         }
666
667         free(classtitle);
668         set_focus(conn, client, true);
669 }
670
671 /*
672  * Jump directly to the specified workspace, row and col.
673  * Great for reaching windows that you always keep in the same spot (hello irssi, I'm looking at you)
674  *
675  */
676 static void jump_to_container(xcb_connection_t *conn, const char *arguments) {
677         int ws, row, col;
678         int result;
679
680         result = sscanf(arguments, "%d %d %d", &ws, &col, &row);
681         LOG("Jump called with %d parameters (\"%s\")\n", result, arguments);
682
683         /* No match? Either no arguments were specified, or no numbers */
684         if (result < 1) {
685                 ELOG("At least one valid argument required\n");
686                 return;
687         }
688
689         /* Move to the target workspace */
690         workspace_show(conn, ws);
691
692         if (result < 3)
693                 return;
694
695         DLOG("Boundary-checking col %d, row %d... (max cols %d, max rows %d)\n", col, row, c_ws->cols, c_ws->rows);
696
697         /* Move to row/col */
698         if (row >= c_ws->rows)
699                 row = c_ws->rows - 1;
700         if (col >= c_ws->cols)
701                 col = c_ws->cols - 1;
702
703         DLOG("Jumping to col %d, row %d\n", col, row);
704         if (c_ws->table[col][row]->currently_focused != NULL)
705                 set_focus(conn, c_ws->table[col][row]->currently_focused, true);
706 }
707
708 /*
709  * Travels the focus stack by the given number of times (or once, if no argument
710  * was specified). That is, selects the window you were in before you focused
711  * the current window.
712  *
713  * The special values 'floating' (select the next floating window), 'tiling'
714  * (select the next tiling window), 'ft' (if the current window is floating,
715  * select the next tiling window and vice-versa) are also valid
716  *
717  */
718 static void travel_focus_stack(xcb_connection_t *conn, const char *arguments) {
719         /* Start count at -1 to always skip the first element */
720         int times, count = -1;
721         Client *current;
722         bool floating_criteria;
723
724         /* Either it’s one of the special values… */
725         if (strcasecmp(arguments, "floating") == 0) {
726                 floating_criteria = true;
727         } else if (strcasecmp(arguments, "tiling") == 0) {
728                 floating_criteria = false;
729         } else if (strcasecmp(arguments, "ft") == 0) {
730                 Client *last_focused = SLIST_FIRST(&(c_ws->focus_stack));
731                 if (last_focused == SLIST_END(&(c_ws->focus_stack))) {
732                         ELOG("Cannot select the next floating/tiling client because there is no client at all\n");
733                         return;
734                 }
735
736                 floating_criteria = !client_is_floating(last_focused);
737         } else {
738                 /* …or a number was specified */
739                 if (sscanf(arguments, "%u", &times) != 1) {
740                         ELOG("No or invalid argument given (\"%s\"), using default of 1 times\n", arguments);
741                         times = 1;
742                 }
743
744                 SLIST_FOREACH(current, &(CUR_CELL->workspace->focus_stack), focus_clients) {
745                         if (++count < times) {
746                                 DLOG("Skipping\n");
747                                 continue;
748                         }
749
750                         DLOG("Focussing\n");
751                         set_focus(conn, current, true);
752                         break;
753                 }
754                 return;
755         }
756
757         /* Select the next client matching the criteria parsed above */
758         SLIST_FOREACH(current, &(CUR_CELL->workspace->focus_stack), focus_clients)
759                 if (client_is_floating(current) == floating_criteria) {
760                         set_focus(conn, current, true);
761                         break;
762                 }
763 }
764
765 /* 
766  * Switch to next or previous existing workspace
767  *
768  */
769 static void next_previous_workspace(xcb_connection_t *conn, int direction) {
770         Workspace *ws = c_ws;
771
772         if (direction == 'n') {
773                 while (1) {
774                         ws = TAILQ_NEXT(ws, workspaces);
775
776                         if (ws == TAILQ_END(workspaces))
777                                 ws = TAILQ_FIRST(workspaces);
778
779                         if (ws == c_ws)
780                                 return;
781
782                         if (ws->screen == NULL)
783                                 continue;
784
785                         workspace_show(conn, ws->num + 1);
786                         return;
787                 }
788         } else if (direction == 'p') {
789                 while (1) {
790                         ws = TAILQ_PREV(ws, workspaces_head, workspaces);
791
792                         if (ws == TAILQ_END(workspaces))
793                                 ws = TAILQ_LAST(workspaces, workspaces_head);
794
795                         if (ws == c_ws)
796                                 return;
797
798                         if (ws->screen == NULL)
799                                 continue;
800
801                         workspace_show(conn, ws->num + 1);
802                         return;
803                 }
804         }
805 }
806
807 static void parse_resize_command(xcb_connection_t *conn, Client *last_focused, const char *command) {
808         int first, second;
809         resize_orientation_t orientation = O_VERTICAL;
810         Container *con = last_focused->container;
811         Workspace *ws = last_focused->workspace;
812
813         if (client_is_floating(last_focused)) {
814                 DLOG("Resizing a floating client\n");
815                 if (STARTS_WITH(command, "left")) {
816                         command += strlen("left");
817                         last_focused->rect.width += atoi(command);
818                         last_focused->rect.x -= atoi(command);
819                 } else if (STARTS_WITH(command, "right")) {
820                         command += strlen("right");
821                         last_focused->rect.width += atoi(command);
822                 } else if (STARTS_WITH(command, "top")) {
823                         command += strlen("top");
824                         last_focused->rect.height += atoi(command);
825                         last_focused->rect.y -= atoi(command);
826                 } else if (STARTS_WITH(command, "bottom")) {
827                         command += strlen("bottom");
828                         last_focused->rect.height += atoi(command);
829                 } else {
830                         ELOG("Syntax: resize <left|right|top|bottom> [+|-]<pixels>\n");
831                         return;
832                 }
833
834                 /* resize_client flushes */
835                 resize_client(conn, last_focused);
836
837                 return;
838         }
839
840         if (STARTS_WITH(command, "left")) {
841                 if (con->col == 0)
842                         return;
843                 first = con->col - 1;
844                 second = con->col;
845                 command += strlen("left");
846         } else if (STARTS_WITH(command, "right")) {
847                 first = con->col + (con->colspan - 1);
848                 DLOG("column %d\n", first);
849
850                 if (!cell_exists(ws, first, con->row) ||
851                     (first == (ws->cols-1)))
852                         return;
853
854                 second = first + 1;
855                 command += strlen("right");
856         } else if (STARTS_WITH(command, "top")) {
857                 if (con->row == 0)
858                         return;
859                 first = con->row - 1;
860                 second = con->row;
861                 orientation = O_HORIZONTAL;
862                 command += strlen("top");
863         } else if (STARTS_WITH(command, "bottom")) {
864                 first = con->row + (con->rowspan - 1);
865                 if (!cell_exists(ws, con->col, first) ||
866                     (first == (ws->rows-1)))
867                         return;
868
869                 second = first + 1;
870                 orientation = O_HORIZONTAL;
871                 command += strlen("bottom");
872         } else {
873                 ELOG("Syntax: resize <left|right|top|bottom> [+|-]<pixels>\n");
874                 return;
875         }
876
877         int pixels = atoi(command);
878         if (pixels == 0)
879                 return;
880
881         resize_container(conn, ws, first, second, orientation, pixels);
882 }
883
884 /*
885  * Parses a command, see file CMDMODE for more information
886  *
887  */
888 void parse_command(xcb_connection_t *conn, const char *command) {
889         LOG("--- parsing command \"%s\" ---\n", command);
890         /* Get the first client from focus stack because floating clients are not
891          * in any container, therefore CUR_CELL is not appropriate. */
892         Client *last_focused = SLIST_FIRST(&(c_ws->focus_stack));
893         if (last_focused == SLIST_END(&(c_ws->focus_stack)))
894                 last_focused = NULL;
895
896         /* Hmm, just to be sure */
897         if (command[0] == '\0')
898                 return;
899
900         /* Is it an <exec>? Then execute the given command. */
901         if (STARTS_WITH(command, "exec ")) {
902                 LOG("starting \"%s\"\n", command + strlen("exec "));
903                 start_application(command+strlen("exec "));
904                 return;
905         }
906
907         if (STARTS_WITH(command, "mark")) {
908                 if (last_focused == NULL) {
909                         ELOG("There is no window to mark\n");
910                         return;
911                 }
912                 const char *rest = command + strlen("mark");
913                 while (*rest == ' ')
914                         rest++;
915                 if (*rest == '\0') {
916                         DLOG("interactive mark starting\n");
917                         start_application("i3-input -p 'mark ' -l 1 -P 'Mark: '");
918                 } else {
919                         LOG("mark with \"%s\"\n", rest);
920                         client_mark(conn, last_focused, rest);
921                 }
922                 return;
923         }
924
925         if (STARTS_WITH(command, "goto")) {
926                 const char *rest = command + strlen("goto");
927                 while (*rest == ' ')
928                         rest++;
929                 if (*rest == '\0') {
930                         DLOG("interactive go to mark starting\n");
931                         start_application("i3-input -p 'goto ' -l 1 -P 'Goto: '");
932                 } else {
933                         LOG("go to \"%s\"\n", rest);
934                         jump_to_mark(conn, rest);
935                 }
936                 return;
937         }
938
939         if (STARTS_WITH(command, "stack-limit ")) {
940                 if (last_focused == NULL || client_is_floating(last_focused)) {
941                         ELOG("No container focused\n");
942                         return;
943                 }
944                 const char *rest = command + strlen("stack-limit ");
945                 if (strncmp(rest, "rows ", strlen("rows ")) == 0) {
946                         last_focused->container->stack_limit = STACK_LIMIT_ROWS;
947                         rest += strlen("rows ");
948                 } else if (strncmp(rest, "cols ", strlen("cols ")) == 0) {
949                         last_focused->container->stack_limit = STACK_LIMIT_COLS;
950                         rest += strlen("cols ");
951                 } else {
952                         ELOG("Syntax: stack-limit <cols|rows> <limit>\n");
953                         return;
954                 }
955
956                 last_focused->container->stack_limit_value = atoi(rest);
957                 if (last_focused->container->stack_limit_value == 0)
958                         last_focused->container->stack_limit = STACK_LIMIT_NONE;
959
960                 return;
961         }
962
963         if (STARTS_WITH(command, "resize ")) {
964                 if (last_focused == NULL)
965                         return;
966                 const char *rest = command + strlen("resize ");
967                 parse_resize_command(conn, last_focused, rest);
968                 return;
969         }
970
971         if (STARTS_WITH(command, "mode ")) {
972                 const char *rest = command + strlen("mode ");
973                 switch_mode(conn, rest);
974                 return;
975         }
976
977         /* Is it an <exit>? */
978         if (STARTS_WITH(command, "exit")) {
979                 LOG("User issued exit-command, exiting without error.\n");
980                 exit(EXIT_SUCCESS);
981         }
982
983         /* Is it a <reload>? */
984         if (STARTS_WITH(command, "reload")) {
985                 load_configuration(conn, NULL, true);
986                 return;
987         }
988
989         /* Is it <restart>? Then restart in place. */
990         if (STARTS_WITH(command, "restart")) {
991                 i3_restart();
992         }
993
994         if (STARTS_WITH(command, "kill")) {
995                 if (last_focused == NULL) {
996                         ELOG("There is no window to kill\n");
997                         return;
998                 }
999
1000                 LOG("Killing current window\n");
1001                 client_kill(conn, last_focused);
1002                 return;
1003         }
1004
1005         /* Is it a jump to a specified workspace, row, col? */
1006         if (STARTS_WITH(command, "jump ")) {
1007                 const char *arguments = command + strlen("jump ");
1008                 if (arguments[0] == '"')
1009                         jump_to_window(conn, arguments);
1010                 else jump_to_container(conn, arguments);
1011                 return;
1012         }
1013
1014         /* Should we travel the focus stack? */
1015         if (STARTS_WITH(command, "focus")) {
1016                 const char *arguments = command + strlen("focus ");
1017                 travel_focus_stack(conn, arguments);
1018                 return;
1019         }
1020
1021         /* Is it 'f' for fullscreen? */
1022         if (command[0] == 'f') {
1023                 if (last_focused == NULL)
1024                         return;
1025                 client_toggle_fullscreen(conn, last_focused);
1026                 return;
1027         }
1028
1029         /* Is it just 's' for stacking or 'd' for default? */
1030         if ((command[0] == 's' || command[0] == 'd' || command[0] == 'T') && (command[1] == '\0')) {
1031                 if (last_focused != NULL && client_is_floating(last_focused)) {
1032                         ELOG("not switching, this is a floating client\n");
1033                         return;
1034                 }
1035                 LOG("Switching mode for current container\n");
1036                 int new_mode = MODE_DEFAULT;
1037                 if (command[0] == 's')
1038                         new_mode = MODE_STACK;
1039                 if (command[0] == 'T')
1040                         new_mode = MODE_TABBED;
1041                 switch_layout_mode(conn, CUR_CELL, new_mode);
1042                 return;
1043         }
1044
1045         /* Is it 'bn' (border normal), 'bp' (border 1pixel) or 'bb' (border borderless)? */
1046         /* or even 'bt' (toggle border: 'bp' -> 'bb' -> 'bn' ) */
1047         if (command[0] == 'b') {
1048                 if (last_focused == NULL) {
1049                         ELOG("No window focused, cannot change border type\n");
1050                         return;
1051                 }
1052
1053                 char com = command[1];
1054                 if (command[1] == 't') {
1055                         if (last_focused->titlebar_position == TITLEBAR_TOP &&
1056                             !last_focused->borderless)
1057                             com = 'p';
1058                         else if (last_focused->titlebar_position == TITLEBAR_OFF &&
1059                                  !last_focused->borderless)
1060                             com = 'b';
1061                         else com = 'n';
1062                 }
1063
1064                 client_change_border(conn, last_focused, com);
1065                 return;
1066         }
1067
1068         if (command[0] == 'H') {
1069                 LOG("Hiding all floating windows\n");
1070                 floating_toggle_hide(conn, c_ws);
1071                 return;
1072         }
1073
1074         enum { WITH_WINDOW, WITH_CONTAINER, WITH_WORKSPACE, WITH_SCREEN } with = WITH_WINDOW;
1075
1076         /* Is it a <with>? */
1077         if (command[0] == 'w') {
1078                 command++;
1079                 /* TODO: implement */
1080                 if (command[0] == 'c') {
1081                         with = WITH_CONTAINER;
1082                         command++;
1083                 } else if (command[0] == 'w') {
1084                         with = WITH_WORKSPACE;
1085                         command++;
1086                 } else if (command[0] == 's') {
1087                         with = WITH_SCREEN;
1088                         command++;
1089                 } else {
1090                         ELOG("not yet implemented.\n");
1091                         return;
1092                 }
1093         }
1094
1095         /* Is it 't' for toggle tiling/floating? */
1096         if (command[0] == 't') {
1097                 if (with == WITH_WORKSPACE) {
1098                         c_ws->auto_float = !c_ws->auto_float;
1099                         LOG("autofloat is now %d\n", c_ws->auto_float);
1100                         return;
1101                 }
1102                 if (last_focused == NULL) {
1103                         ELOG("Cannot toggle tiling/floating: workspace empty\n");
1104                         return;
1105                 }
1106
1107                 Workspace *ws = last_focused->workspace;
1108
1109                 toggle_floating_mode(conn, last_focused, false);
1110                 /* delete all empty columns/rows */
1111                 cleanup_table(conn, ws);
1112
1113                 /* Fix colspan/rowspan if it’d overlap */
1114                 fix_colrowspan(conn, ws);
1115
1116                 render_workspace(conn, ws->screen, ws);
1117
1118                 /* Re-focus the client because cleanup_table sets the focus to the last
1119                  * focused client inside a container only. */
1120                 set_focus(conn, last_focused, true);
1121
1122                 return;
1123         }
1124
1125         /* Is it 'n' or 'p' for next/previous workspace? (nw) */
1126         if ((command[0] == 'n' || command[0] == 'p') && command[1] == 'w') {
1127                next_previous_workspace(conn, command[0]);
1128                return;
1129         }
1130
1131         /* It’s a normal <cmd> */
1132         char *rest = NULL;
1133         enum { ACTION_FOCUS, ACTION_MOVE, ACTION_SNAP } action = ACTION_FOCUS;
1134         direction_t direction;
1135         int times = strtol(command, &rest, 10);
1136         if (rest == NULL) {
1137                 ELOG("Invalid command (\"%s\")\n", command);
1138                 return;
1139         }
1140
1141         if (*rest == '\0') {
1142                 /* No rest? This was a workspace number, not a times specification */
1143                 workspace_show(conn, times);
1144                 return;
1145         }
1146
1147         if (*rest == 'm' || *rest == 's') {
1148                 action = (*rest == 'm' ? ACTION_MOVE : ACTION_SNAP);
1149                 rest++;
1150         }
1151
1152         int workspace = strtol(rest, &rest, 10);
1153
1154         if (rest == NULL) {
1155                 ELOG("Invalid command (\"%s\")\n", command);
1156                 return;
1157         }
1158
1159         if (*rest == '\0') {
1160                 if (last_focused != NULL && client_is_floating(last_focused))
1161                         move_floating_window_to_workspace(conn, last_focused, workspace);
1162                 else move_current_window_to_workspace(conn, workspace);
1163                 return;
1164         }
1165
1166         if (last_focused == NULL) {
1167                 ELOG("Not performing (no window found)\n");
1168                 return;
1169         }
1170
1171         if (client_is_floating(last_focused) &&
1172             (action != ACTION_FOCUS && action != ACTION_MOVE)) {
1173                 ELOG("Not performing (floating)\n");
1174                 return;
1175         }
1176
1177         /* Now perform action to <where> */
1178         while (*rest != '\0') {
1179                 if (*rest == 'h')
1180                         direction = D_LEFT;
1181                 else if (*rest == 'j')
1182                         direction = D_DOWN;
1183                 else if (*rest == 'k')
1184                         direction = D_UP;
1185                 else if (*rest == 'l')
1186                         direction = D_RIGHT;
1187                 else {
1188                         ELOG("unknown direction: %c\n", *rest);
1189                         return;
1190                 }
1191                 rest++;
1192
1193                 if (action == ACTION_FOCUS) {
1194                         if (with == WITH_SCREEN) {
1195                                 focus_thing(conn, direction, THING_SCREEN);
1196                                 continue;
1197                         }
1198                         if (client_is_floating(last_focused)) {
1199                                 floating_focus_direction(conn, last_focused, direction);
1200                                 continue;
1201                         }
1202                         focus_thing(conn, direction, (with == WITH_WINDOW ? THING_WINDOW : THING_CONTAINER));
1203                         continue;
1204                 }
1205
1206                 if (action == ACTION_MOVE) {
1207                         if (with == WITH_SCREEN) {
1208                                 /* TODO: this should swap the screen’s contents
1209                                  * (e.g. all workspaces) with the next/previous/…
1210                                  * screen */
1211                                 ELOG("Not yet implemented\n");
1212                                 continue;
1213                         }
1214                         if (client_is_floating(last_focused)) {
1215                                 floating_move(conn, last_focused, direction);
1216                                 continue;
1217                         }
1218                         if (with == WITH_WINDOW)
1219                                 move_current_window(conn, direction);
1220                         else move_current_container(conn, direction);
1221                         continue;
1222                 }
1223
1224                 if (action == ACTION_SNAP) {
1225                         if (with == WITH_SCREEN) {
1226                                 ELOG("You cannot snap a screen (it makes no sense).\n");
1227                                 continue;
1228                         }
1229                         snap_current_container(conn, direction);
1230                         continue;
1231                 }
1232         }
1233 }