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