4 * i3 - an improved dynamic tiling window manager
6 * © 2009 Michael Stapelberg and contributors
8 * See file LICENSE for license information.
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)
32 Client *candidate = NULL;
33 if (direction == D_UP)
34 candidate = CIRCLEQ_PREV(container->currently_focused, clients);
35 else if (direction == D_DOWN)
36 candidate = CIRCLEQ_NEXT(container->currently_focused, clients);
37 else printf("Direction not implemented!\n");
39 /* If we don’t have anything to select, we’re done */
40 if (candidate == CIRCLEQ_END(&(container->clients)))
43 /* Set focus if we could successfully move */
44 set_focus(connection, candidate);
49 static void focus_window(xcb_connection_t *connection, direction_t direction) {
50 printf("focusing direction %d\n", direction);
52 int new_row = current_row,
53 new_col = current_col;
55 /* TODO: for horizontal default layout, this has to be expanded to LEFT/RIGHT */
56 if (direction == D_UP || direction == D_DOWN) {
57 /* Let’s see if we can perform up/down focus in the current container */
58 Container *container = CUR_CELL;
60 /* There always is a container. If not, current_col or current_row is wrong */
61 assert(container != NULL);
63 if (focus_window_in_container(connection, container, direction))
66 if (direction == D_DOWN && cell_exists(current_col, current_row+1))
68 else if (direction == D_UP && cell_exists(current_col, current_row-1))
70 } else if (direction == D_LEFT || direction == D_RIGHT) {
71 if (direction == D_RIGHT && cell_exists(current_col+1, current_row))
73 else if (direction == D_LEFT && cell_exists(current_col-1, current_row))
76 printf("nah, not possible\n");
80 printf("direction unhandled\n");
84 if (c_ws->table[new_col][new_row]->currently_focused != NULL)
85 set_focus(connection, c_ws->table[new_col][new_row]->currently_focused);
89 * Tries to move the window inside its current container.
91 * Returns true if the window could be moved, false otherwise.
94 static bool move_current_window_in_container(xcb_connection_t *connection, Client *client,
95 direction_t direction) {
96 assert(client->container != NULL);
98 Client *other = (direction == D_UP ? CIRCLEQ_PREV(client, clients) :
99 CIRCLEQ_NEXT(client, clients));
101 if (other == CIRCLEQ_END(&(client->container->clients)))
104 printf("i can do that\n");
105 /* We can move the client inside its current container */
106 CIRCLEQ_REMOVE(&(client->container->clients), client, clients);
107 if (direction == D_UP)
108 CIRCLEQ_INSERT_BEFORE(&(client->container->clients), other, client, clients);
109 else CIRCLEQ_INSERT_AFTER(&(client->container->clients), other, client, clients);
110 render_layout(connection);
115 * Moves the current window to the given direction, creating a column/row if
119 static void move_current_window(xcb_connection_t *connection, direction_t direction) {
120 printf("moving window to direction %s\n", (direction == D_UP ? "up" : (direction == D_DOWN ? "down" :
121 (direction == D_LEFT ? "left" : "right"))));
122 /* Get current window */
123 Container *container = CUR_CELL,
126 /* There has to be a container, see focus_window() */
127 assert(container != NULL);
129 /* If there is no window or the dock window is focused, we’re done */
130 if (container->currently_focused == NULL ||
131 container->currently_focused->dock)
134 /* As soon as the client is moved away, the next client in the old
135 * container needs to get focus, if any. Therefore, we save it here. */
136 Client *current_client = container->currently_focused;
137 Client *to_focus = CIRCLEQ_NEXT_OR_NULL(&(container->clients), current_client, clients);
138 if (to_focus == NULL)
139 to_focus = CIRCLEQ_PREV_OR_NULL(&(container->clients), current_client, clients);
143 if (current_col == 0)
146 new = CUR_TABLE[--current_col][current_row];
149 if (current_col == (c_ws->cols-1))
150 expand_table_cols(c_ws);
152 new = CUR_TABLE[++current_col][current_row];
155 /* TODO: if we’re at the up-most position, move the rest of the table down */
156 if (move_current_window_in_container(connection, current_client, D_UP) ||
160 new = CUR_TABLE[current_col][--current_row];
163 if (move_current_window_in_container(connection, current_client, D_DOWN))
166 if (current_row == (c_ws->rows-1))
167 expand_table_rows(c_ws);
169 new = CUR_TABLE[current_col][++current_row];
173 /* Remove it from the old container and put it into the new one */
174 CIRCLEQ_REMOVE(&(container->clients), current_client, clients);
175 CIRCLEQ_INSERT_TAIL(&(new->clients), current_client, clients);
177 /* Update data structures */
178 current_client->container = new;
179 container->currently_focused = to_focus;
180 new->currently_focused = current_client;
182 /* delete all empty columns/rows */
183 cleanup_table(connection, container->workspace);
184 render_layout(connection);
186 set_focus(connection, current_client);
190 * "Snaps" the current container (not possible for windows, because it works at table base)
191 * to the given direction, that is, adjusts cellspan/rowspan
194 static void snap_current_container(xcb_connection_t *connection, direction_t direction) {
195 printf("snapping container to direction %d\n", direction);
197 Container *container = CUR_CELL;
199 assert(container != NULL);
203 /* Snap to the left is actually a move to the left and then a snap right */
204 move_current_window(connection, D_LEFT);
205 snap_current_container(connection, D_RIGHT);
208 /* Check if the cell is used */
209 if (!cell_exists(container->col + 1, container->row) ||
210 CUR_TABLE[container->col+1][container->row]->currently_focused != NULL) {
211 printf("cannot snap to right - the cell is already used\n");
215 /* Check if there are other cells with rowspan, which are in our way.
216 * If so, reduce their rowspan. */
217 for (int i = container->row-1; i >= 0; i--) {
218 printf("we got cell %d, %d with rowspan %d\n",
219 container->col+1, i, CUR_TABLE[container->col+1][i]->rowspan);
220 while ((CUR_TABLE[container->col+1][i]->rowspan-1) >= (container->row - i))
221 CUR_TABLE[container->col+1][i]->rowspan--;
222 printf("new rowspan = %d\n", CUR_TABLE[container->col+1][i]->rowspan);
225 container->colspan++;
228 move_current_window(connection, D_UP);
229 snap_current_container(connection, D_DOWN);
232 printf("snapping down\n");
233 if (!cell_exists(container->col, container->row+1) ||
234 CUR_TABLE[container->col][container->row+1]->currently_focused != NULL) {
235 printf("cannot snap down - the cell is already used\n");
239 for (int i = container->col-1; i >= 0; i--) {
240 printf("we got cell %d, %d with colspan %d\n",
241 i, container->row+1, CUR_TABLE[i][container->row+1]->colspan);
242 while ((CUR_TABLE[i][container->row+1]->colspan-1) >= (container->col - i))
243 CUR_TABLE[i][container->row+1]->colspan--;
244 printf("new colspan = %d\n", CUR_TABLE[i][container->row+1]->colspan);
248 container->rowspan++;
252 render_layout(connection);
255 static void show_workspace(xcb_connection_t *conn, int workspace) {
257 xcb_window_t root = xcb_setup_roots_iterator(xcb_get_setup(conn)).data->root;
258 /* t_ws (to workspace) is just a convenience pointer to the workspace we’re switching to */
259 Workspace *t_ws = &(workspaces[workspace-1]);
261 printf("show_workspace(%d)\n", workspace);
263 /* Store current_row/current_col */
264 c_ws->current_row = current_row;
265 c_ws->current_col = current_col;
267 /* Check if the workspace has not been used yet */
268 if (t_ws->screen == NULL) {
269 printf("initializing new workspace, setting num to %d\n", workspace);
270 t_ws->screen = c_ws->screen;
271 /* Copy the dimensions from the virtual screen */
272 memcpy(&(t_ws->rect), &(t_ws->screen->rect), sizeof(Rect));
275 if (c_ws->screen != t_ws->screen) {
276 /* We need to switch to the other screen first */
277 printf("Just moving over to other screen.\n");
278 c_ws = &(workspaces[t_ws->screen->current_workspace]);
279 current_col = c_ws->current_col;
280 current_row = c_ws->current_row;
281 if (CUR_CELL->currently_focused != NULL)
282 warp_pointer_into(conn, CUR_CELL->currently_focused);
284 Rect *dims = &(c_ws->screen->rect);
285 xcb_warp_pointer(conn, XCB_NONE, root, 0, 0, 0, 0,
286 dims->x + (dims->width / 2), dims->y + (dims->height / 2));
290 /* Check if we need to change something or if we’re already there */
291 if (c_ws->screen->current_workspace == (workspace-1))
294 t_ws->screen->current_workspace = workspace-1;
296 /* TODO: does grabbing the server actually bring us any (speed)advantages? */
297 //xcb_grab_server(conn);
299 /* Unmap all clients of the current workspace */
300 for (int cols = 0; cols < c_ws->cols; cols++)
301 for (int rows = 0; rows < c_ws->rows; rows++)
302 CIRCLEQ_FOREACH(client, &(c_ws->table[cols][rows]->clients), clients)
303 xcb_unmap_window(conn, client->frame);
305 /* Unmap the stack windows on the current workspace, if any */
306 struct Stack_Window *stack_win;
307 SLIST_FOREACH(stack_win, &stack_wins, stack_windows)
308 if (stack_win->container->workspace == c_ws)
309 xcb_unmap_window(conn, stack_win->window);
311 c_ws = &workspaces[workspace-1];
312 current_row = c_ws->current_row;
313 current_col = c_ws->current_col;
314 printf("new current row = %d, current col = %d\n", current_row, current_col);
316 /* Map all clients on the new workspace */
317 for (int cols = 0; cols < c_ws->cols; cols++)
318 for (int rows = 0; rows < c_ws->rows; rows++)
319 CIRCLEQ_FOREACH(client, &(c_ws->table[cols][rows]->clients), clients)
320 xcb_map_window(conn, client->frame);
322 /* Map all stack windows, if any */
323 SLIST_FOREACH(stack_win, &stack_wins, stack_windows)
324 if (stack_win->container->workspace == c_ws)
325 xcb_map_window(conn, stack_win->window);
327 /* Restore focus on the new workspace */
328 if (CUR_CELL->currently_focused != NULL)
329 set_focus(conn, CUR_CELL->currently_focused);
330 else xcb_set_input_focus(conn, XCB_INPUT_FOCUS_POINTER_ROOT, root, XCB_CURRENT_TIME);
332 //xcb_ungrab_server(conn);
338 * Parses a command, see file CMDMODE for more information
341 void parse_command(xcb_connection_t *conn, const char *command) {
342 printf("--- parsing command \"%s\" ---\n", command);
343 /* Hmm, just to be sure */
344 if (command[0] == '\0')
347 /* Is it an <exec>? */
348 if (strncmp(command, "exec ", strlen("exec ")) == 0) {
349 printf("starting \"%s\"\n", command + strlen("exec "));
350 start_application(command+strlen("exec "));
354 /* Is it <restart>? */
355 if (strncmp(command, "restart", strlen("restart")) == 0) {
356 printf("restarting \"%s\"...\n", application_path);
357 execl(application_path, application_path, NULL);
361 /* Is it 'f' for fullscreen? */
362 if (command[0] == 'f') {
363 if (CUR_CELL->currently_focused == NULL)
365 toggle_fullscreen(conn, CUR_CELL->currently_focused);
369 /* Is it just 's' for stacking or 'd' for default? */
370 if ((command[0] == 's' || command[0] == 'd') && (command[1] == '\0')) {
371 printf("Switching mode for current container\n");
372 switch_layout_mode(conn, CUR_CELL, (command[0] == 's' ? MODE_STACK : MODE_DEFAULT));
376 /* Is it a <with>? */
377 if (command[0] == 'w') {
378 /* TODO: implement */
379 printf("not yet implemented.\n");
383 /* It's a normal <cmd> */
386 enum { ACTION_FOCUS, ACTION_MOVE, ACTION_SNAP } action = ACTION_FOCUS;
387 direction_t direction;
388 times = strtol(command, &rest, 10);
390 printf("Invalid command (\"%s\")\n", command);
395 /* No rest? This was a tag number, not a times specification */
396 show_workspace(conn, times);
400 if (*rest == 'm' || *rest == 's') {
401 action = (*rest == 'm' ? ACTION_MOVE : ACTION_SNAP);
405 /* Now perform action to <where> */
406 while (*rest != '\0') {
409 else if (*rest == 'j')
411 else if (*rest == 'k')
413 else if (*rest == 'l')
416 printf("unknown direction: %c\n", *rest);
420 if (action == ACTION_FOCUS)
421 focus_window(conn, direction);
422 else if (action == ACTION_MOVE)
423 move_current_window(conn, direction);
424 else if (action == ACTION_SNAP)
425 snap_current_container(conn, direction);
430 printf("--- done ---\n");