]> git.sur5r.net Git - i3/i3/blob - src/commands.c
Bugfix: Change the event mask to ignore enter notifies when rendering the layout...
[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
27 static bool focus_window_in_container(xcb_connection_t *conn, Container *container,
28                 direction_t direction) {
29         /* If this container is empty, we’re done */
30         if (container->currently_focused == NULL)
31                 return false;
32
33         /* Get the previous/next client or wrap around */
34         Client *candidate = NULL;
35         if (direction == D_UP) {
36                 if ((candidate = CIRCLEQ_PREV_OR_NULL(&(container->clients), container->currently_focused, clients)) == NULL)
37                         candidate = CIRCLEQ_LAST(&(container->clients));
38         }
39         else if (direction == D_DOWN) {
40                 if ((candidate = CIRCLEQ_NEXT_OR_NULL(&(container->clients), container->currently_focused, clients)) == NULL)
41                         candidate = CIRCLEQ_FIRST(&(container->clients));
42         } else LOG("Direction not implemented!\n");
43
44         /* If we could not switch, the container contains exactly one client. We return false */
45         if (candidate == container->currently_focused)
46                 return false;
47
48         /* Set focus */
49         set_focus(conn, candidate);
50
51         return true;
52 }
53
54 typedef enum { THING_WINDOW, THING_CONTAINER } thing_t;
55
56 static void focus_thing(xcb_connection_t *conn, direction_t direction, thing_t thing) {
57         LOG("focusing direction %d\n", direction);
58
59         int new_row = current_row,
60             new_col = current_col;
61
62         Container *container = CUR_CELL;
63         Workspace *t_ws = c_ws;
64
65         /* There always is a container. If not, current_col or current_row is wrong */
66         assert(container != NULL);
67
68         if (container->workspace->fullscreen_client != NULL) {
69                 LOG("You're in fullscreen mode. Won't switch focus\n");
70                 return;
71         }
72
73         /* TODO: for horizontal default layout, this has to be expanded to LEFT/RIGHT */
74         if (direction == D_UP || direction == D_DOWN) {
75                 if (thing == THING_WINDOW)
76                         /* Let’s see if we can perform up/down focus in the current container */
77                         if (focus_window_in_container(conn, container, direction))
78                                 return;
79
80                 if (direction == D_DOWN && cell_exists(current_col, current_row+1))
81                         new_row++;
82                 else if (direction == D_UP && cell_exists(current_col, current_row-1))
83                         new_row--;
84                 else {
85                         /* Let’s see if there is a screen down/up there to which we can switch */
86                         LOG("container is at %d with height %d\n", container->y, container->height);
87                         i3Screen *screen;
88                         int destination_y = (direction == D_UP ? (container->y - 1) : (container->y + container->height + 1));
89                         if ((screen = get_screen_containing(container->x, destination_y)) == NULL) {
90                                 LOG("Wrapping screen around vertically\n");
91                                 /* No screen found? Then wrap */
92                                 screen = get_screen_most((direction == D_UP ? D_DOWN : D_UP));
93                         }
94                         t_ws = &(workspaces[screen->current_workspace]);
95                         new_row = (direction == D_UP ? (t_ws->rows - 1) : 0);
96                 }
97         } else if (direction == D_LEFT || direction == D_RIGHT) {
98                 if (direction == D_RIGHT && cell_exists(current_col+1, current_row))
99                         new_col++;
100                 else if (direction == D_LEFT && cell_exists(current_col-1, current_row))
101                         new_col--;
102                 else {
103                         /* Let’s see if there is a screen left/right here to which we can switch */
104                         LOG("container is at %d with width %d\n", container->x, container->width);
105                         i3Screen *screen;
106                         int destination_x = (direction == D_LEFT ? (container->x - 1) : (container->x + container->width + 1));
107                         if ((screen = get_screen_containing(destination_x, container->y)) == NULL) {
108                                 LOG("Wrapping screen around horizontally\n");
109                                 screen = get_screen_most((direction == D_LEFT ? D_RIGHT : D_LEFT));
110                         }
111                         t_ws = &(workspaces[screen->current_workspace]);
112                         new_col = (direction == D_LEFT ? (t_ws->cols - 1) : 0);
113                 }
114         } else {
115                 LOG("direction unhandled\n");
116                 return;
117         }
118
119         /* Bounds checking */
120         if (new_col >= t_ws->cols)
121                 new_col = (t_ws->cols - 1);
122         if (new_row >= t_ws->rows)
123                 new_row = (t_ws->rows - 1);
124
125         if (t_ws->table[new_col][new_row]->currently_focused != NULL)
126                 set_focus(conn, t_ws->table[new_col][new_row]->currently_focused);
127 }
128
129 /*
130  * Tries to move the window inside its current container.
131  *
132  * Returns true if the window could be moved, false otherwise.
133  *
134  */
135 static bool move_current_window_in_container(xcb_connection_t *conn, Client *client,
136                 direction_t direction) {
137         assert(client->container != NULL);
138
139         Client *other = (direction == D_UP ? CIRCLEQ_PREV(client, clients) :
140                                              CIRCLEQ_NEXT(client, clients));
141
142         if (other == CIRCLEQ_END(&(client->container->clients)))
143                 return false;
144
145         LOG("i can do that\n");
146         /* We can move the client inside its current container */
147         CIRCLEQ_REMOVE(&(client->container->clients), client, clients);
148         if (direction == D_UP)
149                 CIRCLEQ_INSERT_BEFORE(&(client->container->clients), other, client, clients);
150         else CIRCLEQ_INSERT_AFTER(&(client->container->clients), other, client, clients);
151         render_layout(conn);
152         return true;
153 }
154
155 /*
156  * Moves the current window to the given direction, creating a column/row if
157  * necessary
158  *
159  */
160 static void move_current_window(xcb_connection_t *conn, direction_t direction) {
161         LOG("moving window to direction %s\n", (direction == D_UP ? "up" : (direction == D_DOWN ? "down" :
162                                         (direction == D_LEFT ? "left" : "right"))));
163         /* Get current window */
164         Container *container = CUR_CELL,
165                   *new = NULL;
166
167         /* There has to be a container, see focus_window() */
168         assert(container != NULL);
169
170         /* If there is no window or the dock window is focused, we’re done */
171         if (container->currently_focused == NULL ||
172             container->currently_focused->dock)
173                 return;
174
175         /* As soon as the client is moved away, the next client in the old
176          * container needs to get focus, if any. Therefore, we save it here. */
177         Client *current_client = container->currently_focused;
178         Client *to_focus = CIRCLEQ_NEXT_OR_NULL(&(container->clients), current_client, clients);
179         if (to_focus == NULL)
180                 to_focus = CIRCLEQ_PREV_OR_NULL(&(container->clients), current_client, clients);
181
182         switch (direction) {
183                 case D_LEFT:
184                         /* TODO: If we’re at the left-most position, move the rest of the table right */
185                         if (current_col == 0)
186                                 return;
187
188                         new = CUR_TABLE[--current_col][current_row];
189                         break;
190                 case D_RIGHT:
191                         if (current_col == (c_ws->cols-1))
192                                 expand_table_cols(c_ws);
193
194                         new = CUR_TABLE[++current_col][current_row];
195                         break;
196                 case D_UP:
197                         /* TODO: if we’re at the up-most position, move the rest of the table down */
198                         if (move_current_window_in_container(conn, current_client, D_UP) ||
199                                 current_row == 0)
200                                 return;
201
202                         new = CUR_TABLE[current_col][--current_row];
203                         break;
204                 case D_DOWN:
205                         if (move_current_window_in_container(conn, current_client, D_DOWN))
206                                 return;
207
208                         if (current_row == (c_ws->rows-1))
209                                 expand_table_rows(c_ws);
210
211                         new = CUR_TABLE[current_col][++current_row];
212                         break;
213         }
214
215         /* Remove it from the old container and put it into the new one */
216         remove_client_from_container(conn, current_client, container);
217         CIRCLEQ_INSERT_TAIL(&(new->clients), current_client, clients);
218
219         /* Update data structures */
220         current_client->container = new;
221         container->currently_focused = to_focus;
222         new->currently_focused = current_client;
223
224         Workspace *workspace = container->workspace;
225
226         /* delete all empty columns/rows */
227         cleanup_table(conn, workspace);
228
229         /* Fix colspan/rowspan if it’d overlap */
230         fix_colrowspan(conn, workspace);
231
232         render_layout(conn);
233
234         set_focus(conn, current_client);
235 }
236
237 /*
238  * "Snaps" the current container (not possible for windows, because it works at table base)
239  * to the given direction, that is, adjusts cellspan/rowspan
240  *
241  */
242 static void snap_current_container(xcb_connection_t *conn, direction_t direction) {
243         LOG("snapping container to direction %d\n", direction);
244
245         Container *container = CUR_CELL;
246
247         assert(container != NULL);
248
249         switch (direction) {
250                 case D_LEFT:
251                         /* Snap to the left is actually a move to the left and then a snap right */
252                         if (!cell_exists(container->col - 1, container->row) ||
253                                 CUR_TABLE[container->col-1][container->row]->currently_focused != NULL) {
254                                 LOG("cannot snap to left - the cell is already used\n");
255                                 return;
256                         }
257
258                         move_current_window(conn, D_LEFT);
259                         snap_current_container(conn, D_RIGHT);
260                         return;
261                 case D_RIGHT: {
262                         /* Check if the cell is used */
263                         int new_col = container->col + container->colspan;
264                         if (!cell_exists(new_col, container->row) ||
265                                 CUR_TABLE[new_col][container->row]->currently_focused != NULL) {
266                                 LOG("cannot snap to right - the cell is already used\n");
267                                 return;
268                         }
269
270                         /* Check if there are other cells with rowspan, which are in our way.
271                          * If so, reduce their rowspan. */
272                         for (int i = container->row-1; i >= 0; i--) {
273                                 LOG("we got cell %d, %d with rowspan %d\n",
274                                                 new_col, i, CUR_TABLE[new_col][i]->rowspan);
275                                 while ((CUR_TABLE[new_col][i]->rowspan-1) >= (container->row - i))
276                                         CUR_TABLE[new_col][i]->rowspan--;
277                                 LOG("new rowspan = %d\n", CUR_TABLE[new_col][i]->rowspan);
278                         }
279
280                         container->colspan++;
281                         break;
282                 }
283                 case D_UP:
284                         if (!cell_exists(container->col, container->row - 1) ||
285                                 CUR_TABLE[container->col][container->row-1]->currently_focused != NULL) {
286                                 LOG("cannot snap to top - the cell is already used\n");
287                                 return;
288                         }
289
290                         move_current_window(conn, D_UP);
291                         snap_current_container(conn, D_DOWN);
292                         return;
293                 case D_DOWN: {
294                         LOG("snapping down\n");
295                         int new_row = container->row + container->rowspan;
296                         if (!cell_exists(container->col, new_row) ||
297                                 CUR_TABLE[container->col][new_row]->currently_focused != NULL) {
298                                 LOG("cannot snap down - the cell is already used\n");
299                                 return;
300                         }
301
302                         for (int i = container->col-1; i >= 0; i--) {
303                                 LOG("we got cell %d, %d with colspan %d\n",
304                                                 i, new_row, CUR_TABLE[i][new_row]->colspan);
305                                 while ((CUR_TABLE[i][new_row]->colspan-1) >= (container->col - i))
306                                         CUR_TABLE[i][new_row]->colspan--;
307                                 LOG("new colspan = %d\n", CUR_TABLE[i][new_row]->colspan);
308
309                         }
310
311                         container->rowspan++;
312                         break;
313                 }
314         }
315
316         render_layout(conn);
317 }
318
319 /*
320  * Moves the currently selected window to the given workspace
321  *
322  */
323 static void move_current_window_to_workspace(xcb_connection_t *conn, int workspace) {
324         LOG("Moving current window to workspace %d\n", workspace);
325
326         Container *container = CUR_CELL;
327
328         assert(container != NULL);
329
330         /* t_ws (to workspace) is just a container pointer to the workspace we’re switching to */
331         Workspace *t_ws = &(workspaces[workspace-1]);
332
333         Client *current_client = container->currently_focused;
334         if (current_client == NULL) {
335                 LOG("No currently focused client in current container.\n");
336                 return;
337         }
338         Client *to_focus = CIRCLEQ_NEXT_OR_NULL(&(container->clients), current_client, clients);
339         if (to_focus == NULL)
340                 to_focus = CIRCLEQ_PREV_OR_NULL(&(container->clients), current_client, clients);
341
342         if (t_ws->screen == NULL) {
343                 LOG("initializing new workspace, setting num to %d\n", workspace-1);
344                 t_ws->screen = container->workspace->screen;
345                 /* Copy the dimensions from the virtual screen */
346                 memcpy(&(t_ws->rect), &(container->workspace->screen->rect), sizeof(Rect));
347         }
348
349         Container *to_container = t_ws->table[t_ws->current_col][t_ws->current_row];
350
351         assert(to_container != NULL);
352
353         CIRCLEQ_REMOVE(&(container->clients), current_client, clients);
354         SLIST_REMOVE(&(container->workspace->focus_stack), current_client, Client, focus_clients);
355
356         CIRCLEQ_INSERT_TAIL(&(to_container->clients), current_client, clients);
357         SLIST_INSERT_HEAD(&(to_container->workspace->focus_stack), current_client, focus_clients);
358         LOG("Moved.\n");
359
360         current_client->container = to_container;
361         container->currently_focused = to_focus;
362         to_container->currently_focused = current_client;
363
364         /* If we’re moving it to an invisible screen, we need to unmap it */
365         if (to_container->workspace->screen->current_workspace != to_container->workspace->num) {
366                 LOG("This workspace is not visible, unmapping\n");
367                 xcb_unmap_window(conn, current_client->frame);
368         }
369
370         /* delete all empty columns/rows */
371         cleanup_table(conn, container->workspace);
372
373         render_layout(conn);
374 }
375
376 static void show_workspace(xcb_connection_t *conn, int workspace) {
377         Client *client;
378         xcb_window_t root = xcb_setup_roots_iterator(xcb_get_setup(conn)).data->root;
379         /* t_ws (to workspace) is just a convenience pointer to the workspace we’re switching to */
380         Workspace *t_ws = &(workspaces[workspace-1]);
381
382         LOG("show_workspace(%d)\n", workspace);
383
384         /* Store current_row/current_col */
385         c_ws->current_row = current_row;
386         c_ws->current_col = current_col;
387
388         /* Check if the workspace has not been used yet */
389         if (t_ws->screen == NULL) {
390                 LOG("initializing new workspace, setting num to %d\n", workspace);
391                 t_ws->screen = c_ws->screen;
392                 /* Copy the dimensions from the virtual screen */
393                 memcpy(&(t_ws->rect), &(t_ws->screen->rect), sizeof(Rect));
394         }
395
396         if (c_ws->screen != t_ws->screen) {
397                 /* We need to switch to the other screen first */
398                 LOG("moving over to other screen.\n");
399                 c_ws = &(workspaces[t_ws->screen->current_workspace]);
400                 current_col = c_ws->current_col;
401                 current_row = c_ws->current_row;
402                 if (CUR_CELL->currently_focused != NULL)
403                         warp_pointer_into(conn, CUR_CELL->currently_focused);
404                 else {
405                         Rect *dims = &(c_ws->screen->rect);
406                         xcb_warp_pointer(conn, XCB_NONE, root, 0, 0, 0, 0,
407                                          dims->x + (dims->width / 2), dims->y + (dims->height / 2));
408                 }
409         }
410
411         /* Check if we need to change something or if we’re already there */
412         if (c_ws->screen->current_workspace == (workspace-1))
413                 return;
414
415         t_ws->screen->current_workspace = workspace-1;
416
417         /* TODO: does grabbing the server actually bring us any (speed)advantages? */
418         //xcb_grab_server(conn);
419
420         ignore_enter_notify_forall(conn, c_ws, true);
421
422         /* Unmap all clients of the current workspace */
423         int unmapped_clients = 0;
424         FOR_TABLE(c_ws)
425                 CIRCLEQ_FOREACH(client, &(c_ws->table[cols][rows]->clients), clients) {
426                         xcb_unmap_window(conn, client->frame);
427                         unmapped_clients++;
428                 }
429
430         /* If we did not unmap any clients, the workspace is empty and we can destroy it */
431         if (unmapped_clients == 0)
432                 c_ws->screen = NULL;
433
434         /* Unmap the stack windows on the current workspace, if any */
435         struct Stack_Window *stack_win;
436         SLIST_FOREACH(stack_win, &stack_wins, stack_windows)
437                 if (stack_win->container->workspace == c_ws)
438                         xcb_unmap_window(conn, stack_win->window);
439
440         ignore_enter_notify_forall(conn, c_ws, false);
441
442         c_ws = &workspaces[workspace-1];
443         current_row = c_ws->current_row;
444         current_col = c_ws->current_col;
445         LOG("new current row = %d, current col = %d\n", current_row, current_col);
446
447         ignore_enter_notify_forall(conn, c_ws, true);
448
449         /* Map all clients on the new workspace */
450         FOR_TABLE(c_ws)
451                 CIRCLEQ_FOREACH(client, &(c_ws->table[cols][rows]->clients), clients)
452                         xcb_map_window(conn, client->frame);
453
454         /* Map all stack windows, if any */
455         SLIST_FOREACH(stack_win, &stack_wins, stack_windows)
456                 if (stack_win->container->workspace == c_ws)
457                         xcb_map_window(conn, stack_win->window);
458
459         ignore_enter_notify_forall(conn, c_ws, false);
460
461         /* Restore focus on the new workspace */
462         if (CUR_CELL->currently_focused != NULL)
463                 set_focus(conn, CUR_CELL->currently_focused);
464         else xcb_set_input_focus(conn, XCB_INPUT_FOCUS_POINTER_ROOT, root, XCB_CURRENT_TIME);
465
466         //xcb_ungrab_server(conn);
467
468         render_layout(conn);
469 }
470
471 /*
472  * Parses a command, see file CMDMODE for more information
473  *
474  */
475 void parse_command(xcb_connection_t *conn, const char *command) {
476         LOG("--- parsing command \"%s\" ---\n", command);
477         /* Hmm, just to be sure */
478         if (command[0] == '\0')
479                 return;
480
481         /* Is it an <exec>? */
482         if (strncmp(command, "exec ", strlen("exec ")) == 0) {
483                 LOG("starting \"%s\"\n", command + strlen("exec "));
484                 start_application(command+strlen("exec "));
485                 return;
486         }
487
488         /* Is it <restart>? */
489         if (strncmp(command, "restart", strlen("restart")) == 0) {
490                 LOG("restarting \"%s\"...\n", application_path);
491                 execl(application_path, application_path, NULL);
492                 /* not reached */
493         }
494
495         /* Is it 'f' for fullscreen? */
496         if (command[0] == 'f') {
497                 if (CUR_CELL->currently_focused == NULL)
498                         return;
499                 toggle_fullscreen(conn, CUR_CELL->currently_focused);
500                 return;
501         }
502
503         /* Is it just 's' for stacking or 'd' for default? */
504         if ((command[0] == 's' || command[0] == 'd') && (command[1] == '\0')) {
505                 LOG("Switching mode for current container\n");
506                 switch_layout_mode(conn, CUR_CELL, (command[0] == 's' ? MODE_STACK : MODE_DEFAULT));
507                 return;
508         }
509
510         enum { WITH_WINDOW, WITH_CONTAINER } with = WITH_WINDOW;
511
512         /* Is it a <with>? */
513         if (command[0] == 'w') {
514                 command++;
515                 /* TODO: implement */
516                 if (command[0] == 'c') {
517                         with = WITH_CONTAINER;
518                         command++;
519                 } else {
520                         LOG("not yet implemented.\n");
521                         return;
522                 }
523         }
524
525         /* It's a normal <cmd> */
526         char *rest = NULL;
527         enum { ACTION_FOCUS, ACTION_MOVE, ACTION_SNAP } action = ACTION_FOCUS;
528         direction_t direction;
529         int times = strtol(command, &rest, 10);
530         if (rest == NULL) {
531                 LOG("Invalid command (\"%s\")\n", command);
532                 return;
533         }
534
535         if (*rest == '\0') {
536                 /* No rest? This was a tag number, not a times specification */
537                 show_workspace(conn, times);
538                 return;
539         }
540
541         if (*rest == 'm' || *rest == 's') {
542                 action = (*rest == 'm' ? ACTION_MOVE : ACTION_SNAP);
543                 rest++;
544         }
545
546         int workspace = strtol(rest, &rest, 10);
547
548         if (rest == NULL) {
549                 LOG("Invalid command (\"%s\")\n", command);
550                 return;
551         }
552
553         if (*rest == '\0') {
554                 move_current_window_to_workspace(conn, workspace);
555                 return;
556         }
557
558         /* Now perform action to <where> */
559         while (*rest != '\0') {
560                 if (*rest == 'h')
561                         direction = D_LEFT;
562                 else if (*rest == 'j')
563                         direction = D_DOWN;
564                 else if (*rest == 'k')
565                         direction = D_UP;
566                 else if (*rest == 'l')
567                         direction = D_RIGHT;
568                 else {
569                         LOG("unknown direction: %c\n", *rest);
570                         return;
571                 }
572
573                 if (action == ACTION_FOCUS) {
574                         focus_thing(conn, direction, (with == WITH_WINDOW ? THING_WINDOW : THING_CONTAINER));
575                 }
576                 else if (action == ACTION_MOVE)
577                         move_current_window(conn, direction);
578                 else if (action == ACTION_SNAP)
579                         snap_current_container(conn, direction);
580
581                 rest++;
582         }
583
584         LOG("--- done ---\n");
585 }