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