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