]> git.sur5r.net Git - i3/i3/blob - src/commands.c
Implement Xinerama (workspaces have a specific screen)
[i3/i3] / src / commands.c
1 /*
2  * vim:ts=8:expandtab
3  *
4  * i3 - an improved dynamic tiling window manager
5  *
6  * (c) 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
26 static bool focus_window_in_container(xcb_connection_t *connection, Container *container,
27                 direction_t direction) {
28         /* If this container is empty, we’re done */
29         if (container->currently_focused == NULL)
30                 return false;
31
32         Client *candidad = NULL;
33         if (direction == D_UP)
34                 candidad = CIRCLEQ_PREV(container->currently_focused, clients);
35         else if (direction == D_DOWN)
36                 candidad = CIRCLEQ_NEXT(container->currently_focused, clients);
37         else printf("Direction not implemented!\n");
38
39         /* If we don’t have anything to select, we’re done */
40         if (candidad == CIRCLEQ_END(&(container->clients)))
41                 return false;
42
43         /* Set focus if we could successfully move */
44         set_focus(connection, candidad);
45
46         return true;
47 }
48
49 static void focus_window(xcb_connection_t *connection, direction_t direction) {
50         printf("focusing direction %d\n", direction);
51         /* TODO: for horizontal default layout, this has to be expanded to LEFT/RIGHT */
52         if (direction == D_UP || direction == D_DOWN) {
53                 /* Let’s see if we can perform up/down focus in the current container */
54                 Container *container = CUR_CELL;
55
56                 /* There always is a container. If not, current_col or current_row is wrong */
57                 assert(container != NULL);
58
59                 if (focus_window_in_container(connection, container, direction))
60                         return;
61         } else if (direction == D_LEFT || direction == D_RIGHT) {
62                 if (direction == D_RIGHT && cell_exists(current_col+1, current_row))
63                         current_col++;
64                 else if (direction == D_LEFT && cell_exists(current_col-1, current_row))
65                         current_col--;
66                 else {
67                         printf("nah, not possible\n");
68                         return;
69                 }
70                 if (CUR_CELL->currently_focused != NULL) {
71                         xcb_set_input_focus(connection, XCB_INPUT_FOCUS_POINTER_ROOT,
72                                         CUR_CELL->currently_focused->child, XCB_CURRENT_TIME);
73                         render_layout(connection);
74                 }
75
76         } else {
77                 printf("direction unhandled\n");
78         }
79 }
80
81 /*
82  * Tries to move the window inside its current container.
83  *
84  * Returns true if the window could be moved, false otherwise.
85  *
86  */
87 static bool move_current_window_in_container(xcb_connection_t *connection, Client *client,
88                 direction_t direction) {
89         Client *other = (direction == D_UP ? CIRCLEQ_PREV(client, clients) :
90                                                 CIRCLEQ_NEXT(client, clients));
91
92         if (other == CIRCLEQ_END(&(client->container->clients)))
93                 return false;
94
95         printf("i can do that\n");
96         /* We can move the client inside its current container */
97         CIRCLEQ_REMOVE(&(client->container->clients), client, clients);
98         if (direction == D_UP)
99                 CIRCLEQ_INSERT_BEFORE(&(client->container->clients), other, client, clients);
100         else CIRCLEQ_INSERT_AFTER(&(client->container->clients), other, client, clients);
101         render_layout(connection);
102         return true;
103 }
104
105 /*
106  * Moves the current window to the given direction, creating a column/row if
107  * necessary
108  *
109  */
110 static void move_current_window(xcb_connection_t *connection, direction_t direction) {
111         printf("moving window to direction %d\n", direction);
112         /* Get current window */
113         Container *container = CUR_CELL,
114                   *new = NULL;
115
116         /* There has to be a container, see focus_window() */
117         assert(container != NULL);
118
119         /* If there is no window, we’re done */
120         if (container->currently_focused == NULL)
121                 return;
122
123         /* As soon as the client is moved away, the next client in the old
124          * container needs to get focus, if any. Therefore, we save it here. */
125         Client *current_client = container->currently_focused;
126         Client *to_focus = CIRCLEQ_NEXT(current_client, clients);
127         if (to_focus == CIRCLEQ_END(&(container->clients)))
128                 to_focus = NULL;
129
130         switch (direction) {
131                 case D_LEFT:
132                         if (current_col == 0)
133                                 return;
134
135                         new = CUR_TABLE[--current_col][current_row];
136                         break;
137                 case D_RIGHT:
138                         if (current_col == (c_ws->cols-1))
139                                 expand_table_cols(c_ws);
140
141                         new = CUR_TABLE[++current_col][current_row];
142                         break;
143                 case D_UP:
144                         /* TODO: if we’re at the up-most position, move the rest of the table down */
145                         if (move_current_window_in_container(connection, current_client, D_UP) ||
146                                 current_row == 0)
147                                 return;
148
149                         new = CUR_TABLE[current_col][--current_row];
150                         break;
151                 case D_DOWN:
152                         if (move_current_window_in_container(connection, current_client, D_DOWN))
153                                 return;
154
155                         if (current_row == (c_ws->rows-1))
156                                 expand_table_rows(c_ws);
157
158                         new = CUR_TABLE[current_col][++current_row];
159                         break;
160         }
161
162         /* Remove it from the old container and put it into the new one */
163         CIRCLEQ_REMOVE(&(container->clients), current_client, clients);
164         CIRCLEQ_INSERT_TAIL(&(new->clients), current_client, clients);
165
166         /* Update data structures */
167         current_client->container = new;
168         container->currently_focused = to_focus;
169         new->currently_focused = current_client;
170
171         /* TODO: delete all empty columns/rows */
172
173         render_layout(connection);
174 }
175
176 /*
177  * "Snaps" the current container (not possible for windows, because it works at table base)
178  * to the given direction, that is, adjusts cellspan/rowspan
179  *
180  */
181 static void snap_current_container(xcb_connection_t *connection, direction_t direction) {
182         printf("snapping container to direction %d\n", direction);
183
184         Container *container = CUR_CELL;
185         int i;
186
187         assert(container != NULL);
188
189         switch (direction) {
190                 case D_LEFT:
191                         /* Snap to the left is actually a move to the left and then a snap right */
192                         move_current_window(connection, D_LEFT);
193                         snap_current_container(connection, D_RIGHT);
194                         return;
195                 case D_RIGHT:
196                         /* Check if the cell is used */
197                         if (!cell_exists(container->col + 1, container->row) ||
198                                 CUR_TABLE[container->col+1][container->row]->currently_focused != NULL) {
199                                 printf("cannot snap to right - the cell is already used\n");
200                                 return;
201                         }
202
203                         /* Check if there are other cells with rowspan, which are in our way.
204                          * If so, reduce their rowspan. */
205                         for (i = container->row-1; i >= 0; i--) {
206                                 printf("we got cell %d, %d with rowspan %d\n",
207                                                 container->col+1, i, CUR_TABLE[container->col+1][i]->rowspan);
208                                 while ((CUR_TABLE[container->col+1][i]->rowspan-1) >= (container->row - i))
209                                         CUR_TABLE[container->col+1][i]->rowspan--;
210                                 printf("new rowspan = %d\n", CUR_TABLE[container->col+1][i]->rowspan);
211                         }
212
213                         container->colspan++;
214                         break;
215                 case D_UP:
216                         move_current_window(connection, D_UP);
217                         snap_current_container(connection, D_DOWN);
218                         return;
219                 case D_DOWN:
220                         printf("snapping down\n");
221                         if (!cell_exists(container->col, container->row+1) ||
222                                 CUR_TABLE[container->col][container->row+1]->currently_focused != NULL) {
223                                 printf("cannot snap down - the cell is already used\n");
224                                 return;
225                         }
226
227                         for (i = container->col-1; i >= 0; i--) {
228                                 printf("we got cell %d, %d with colspan %d\n",
229                                                 i, container->row+1, CUR_TABLE[i][container->row+1]->colspan);
230                                 while ((CUR_TABLE[i][container->row+1]->colspan-1) >= (container->col - i))
231                                         CUR_TABLE[i][container->row+1]->colspan--;
232                                 printf("new colspan = %d\n", CUR_TABLE[i][container->row+1]->colspan);
233
234                         }
235
236                         container->rowspan++;
237                         break;
238         }
239
240         render_layout(connection);
241 }
242
243 static void show_workspace(xcb_connection_t *conn, int workspace) {
244         int cols, rows;
245         Client *client;
246         xcb_window_t root = xcb_setup_roots_iterator(xcb_get_setup(conn)).data->root;
247         /* t_ws (to workspace) is just a convenience pointer to the workspace we’re switching to */
248         Workspace *t_ws = &(workspaces[workspace-1]);
249
250         printf("show_workspace(%d)\n", workspace);
251
252         /* Store current_row/current_col */
253         c_ws->current_row = current_row;
254         c_ws->current_col = current_col;
255
256         /* Check if the workspace has not been used yet */
257         if (t_ws->screen == NULL) {
258                 printf("initializing new workspace, setting num to %d\n", workspace);
259                 t_ws->screen = c_ws->screen;
260                 /* Copy the dimensions from the virtual screen */
261                 memcpy(&(t_ws->rect), &(t_ws->screen->rect), sizeof(Rect));
262         }
263
264         if (c_ws->screen != t_ws->screen) {
265                 /* We need to switch to the other screen first */
266                 printf("Just moving over to other screen.\n");
267                 c_ws = &(workspaces[t_ws->screen->current_workspace]);
268                 current_col = c_ws->current_col;
269                 current_row = c_ws->current_row;
270                 if (CUR_CELL->currently_focused != NULL)
271                         warp_pointer_into(conn, CUR_CELL->currently_focused);
272                 else {
273                         Rect *dims = &(c_ws->screen->rect);
274                         xcb_warp_pointer(conn, XCB_NONE, root, 0, 0, 0, 0,
275                                          dims->x + (dims->width / 2), dims->y + (dims->height / 2));
276                 }
277         }
278
279         /* Check if we need to change something or if we’re already there */
280         if (c_ws->screen->current_workspace == (workspace-1))
281                 return;
282
283         t_ws->screen->current_workspace = workspace-1;
284
285         /* TODO: does grabbing the server actually bring us any (speed)advantages? */
286         //xcb_grab_server(conn);
287
288         /* Unmap all clients of the current workspace */
289         for (cols = 0; cols < c_ws->cols; cols++)
290                 for (rows = 0; rows < c_ws->rows; rows++) {
291                         CIRCLEQ_FOREACH(client, &(c_ws->table[cols][rows]->clients), clients)
292                                 xcb_unmap_window(conn, client->frame);
293                 }
294
295         c_ws = &workspaces[workspace-1];
296         current_row = c_ws->current_row;
297         current_col = c_ws->current_col;
298         printf("new current row = %d, current col = %d\n", current_row, current_col);
299
300         /* Map all clients on the new workspace */
301         for (cols = 0; cols < c_ws->cols; cols++)
302                 for (rows = 0; rows < c_ws->rows; rows++) {
303                         CIRCLEQ_FOREACH(client, &(c_ws->table[cols][rows]->clients), clients)
304                                 xcb_map_window(conn, client->frame);
305                 }
306
307         /* Restore focus on the new workspace */
308         if (CUR_CELL->currently_focused != NULL)
309                 xcb_set_input_focus(conn, XCB_INPUT_FOCUS_POINTER_ROOT, CUR_CELL->currently_focused->child, XCB_CURRENT_TIME);
310         else xcb_set_input_focus(conn, XCB_INPUT_FOCUS_POINTER_ROOT, root, XCB_CURRENT_TIME);
311
312         //xcb_ungrab_server(conn);
313
314         render_layout(conn);
315 }
316
317 /*
318  * Parses a command, see file CMDMODE for more information
319  *
320  */
321 void parse_command(xcb_connection_t *conn, const char *command) {
322         printf("--- parsing command \"%s\" ---\n", command);
323         /* Hmm, just to be sure */
324         if (command[0] == '\0')
325                 return;
326
327         /* Is it an <exec>? */
328         if (strncmp(command, "exec ", strlen("exec ")) == 0) {
329                 printf("starting \"%s\"\n", command + strlen("exec "));
330                 start_application(command+strlen("exec "));
331                 return;
332         }
333
334         /* Is it 'f' for fullscreen? */
335         if (command[0] == 'f') {
336                 if (CUR_CELL->currently_focused == NULL)
337                         return;
338                 toggle_fullscreen(conn, CUR_CELL->currently_focused);
339                 return;
340         }
341
342         /* Is it a <with>? */
343         if (command[0] == 'w') {
344                 /* TODO: implement */
345                 printf("not yet implemented.\n");
346                 return;
347         }
348
349         /* It's a normal <cmd> */
350         int times;
351         char *rest = NULL;
352         enum { ACTION_FOCUS, ACTION_MOVE, ACTION_SNAP } action = ACTION_FOCUS;
353         direction_t direction;
354         times = strtol(command, &rest, 10);
355         if (rest == NULL) {
356                 printf("Invalid command (\"%s\")\n", command);
357                 return;
358         }
359         if (*rest == 'm' || *rest == 's') {
360                 action = (*rest == 'm' ? ACTION_MOVE : ACTION_SNAP);
361                 rest++;
362         }
363
364         if (*rest == '\0') {
365                 /* No rest? This was a tag number, not a times specification */
366                 show_workspace(conn, times);
367                 return;
368         }
369
370         /* Now perform action to <where> */
371         while (*rest != '\0') {
372                 if (*rest == 'h')
373                         direction = D_LEFT;
374                 else if (*rest == 'j')
375                         direction = D_DOWN;
376                 else if (*rest == 'k')
377                         direction = D_UP;
378                 else if (*rest == 'l')
379                         direction = D_RIGHT;
380                 else {
381                         printf("unknown direction: %c\n", *rest);
382                         return;
383                 }
384
385                 if (action == ACTION_FOCUS)
386                         focus_window(conn, direction);
387                 else if (action == ACTION_MOVE)
388                         move_current_window(conn, direction);
389                 else if (action == ACTION_SNAP)
390                         snap_current_container(conn, direction);
391
392                 rest++;
393         }
394
395         printf("--- done ---\n");
396 }