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