4 * i3 - an improved dynamic tiling window manager
6 * © 2009 Michael Stapelberg and contributors
8 * See file LICENSE for license information.
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)
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");
40 /* If we don’t have anything to select, we’re done */
41 if (candidate == CIRCLEQ_END(&(container->clients)))
44 /* Set focus if we could successfully move */
45 set_focus(conn, candidate);
50 static void focus_window(xcb_connection_t *conn, direction_t direction) {
51 printf("focusing direction %d\n", direction);
53 int new_row = current_row,
54 new_col = current_col;
56 Container *container = CUR_CELL;
57 Workspace *t_ws = c_ws;
59 /* There always is a container. If not, current_col or current_row is wrong */
60 assert(container != NULL);
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(conn, container, direction))
68 if (direction == D_DOWN && cell_exists(current_col, current_row+1))
70 else if (direction == D_UP && cell_exists(current_col, current_row-1))
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);
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");
81 t_ws = &(workspaces[screen->current_workspace]);
82 new_row = (direction == D_UP ? (t_ws->rows - 1) : 0);
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);
89 } else if (direction == D_LEFT || direction == D_RIGHT) {
90 if (direction == D_RIGHT && cell_exists(current_col+1, current_row))
92 else if (direction == D_LEFT && cell_exists(current_col-1, current_row))
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);
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");
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);
112 printf("direction unhandled\n");
116 if (t_ws->table[new_col][new_row]->currently_focused != NULL)
117 set_focus(conn, t_ws->table[new_col][new_row]->currently_focused);
121 * Tries to move the window inside its current container.
123 * Returns true if the window could be moved, false otherwise.
126 static bool move_current_window_in_container(xcb_connection_t *conn, Client *client,
127 direction_t direction) {
128 assert(client->container != NULL);
130 Client *other = (direction == D_UP ? CIRCLEQ_PREV(client, clients) :
131 CIRCLEQ_NEXT(client, clients));
133 if (other == CIRCLEQ_END(&(client->container->clients)))
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);
147 * Moves the current window to the given direction, creating a column/row if
151 static void move_current_window(xcb_connection_t *conn, 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,
158 /* There has to be a container, see focus_window() */
159 assert(container != NULL);
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)
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);
175 /* TODO: If we’re at the left-most position, move the rest of the table right */
176 if (current_col == 0)
179 new = CUR_TABLE[--current_col][current_row];
182 if (current_col == (c_ws->cols-1))
183 expand_table_cols(c_ws);
185 new = CUR_TABLE[++current_col][current_row];
188 /* TODO: if we’re at the up-most position, move the rest of the table down */
189 if (move_current_window_in_container(conn, current_client, D_UP) ||
193 new = CUR_TABLE[current_col][--current_row];
196 if (move_current_window_in_container(conn, current_client, D_DOWN))
199 if (current_row == (c_ws->rows-1))
200 expand_table_rows(c_ws);
202 new = CUR_TABLE[current_col][++current_row];
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);
210 /* Update data structures */
211 current_client->container = new;
212 container->currently_focused = to_focus;
213 new->currently_focused = current_client;
215 /* delete all empty columns/rows */
216 cleanup_table(conn, container->workspace);
219 set_focus(conn, current_client);
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
227 static void snap_current_container(xcb_connection_t *conn, direction_t direction) {
228 printf("snapping container to direction %d\n", direction);
230 Container *container = CUR_CELL;
232 assert(container != NULL);
236 /* Snap to the left is actually a move to the left and then a snap right */
237 move_current_window(conn, D_LEFT);
238 snap_current_container(conn, 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");
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);
258 container->colspan++;
261 move_current_window(conn, D_UP);
262 snap_current_container(conn, 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");
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);
281 container->rowspan++;
289 * Moves the currently selected window to the given workspace
292 static void move_current_window_to_workspace(xcb_connection_t *conn, int workspace) {
293 printf("Moving current window to workspace %d\n", workspace);
295 Container *container = CUR_CELL;
297 assert(container != NULL);
299 /* t_ws (to workspace) is just a container pointer to the workspace we’re switching to */
300 Workspace *t_ws = &(workspaces[workspace-1]);
302 Client *current_client = container->currently_focused;
303 if (current_client == NULL) {
304 printf("No currently focused client in current container.\n");
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);
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));
318 Container *to_container = t_ws->table[t_ws->current_col][t_ws->current_row];
320 assert(to_container != NULL);
322 CIRCLEQ_REMOVE(&(container->clients), current_client, clients);
323 CIRCLEQ_INSERT_TAIL(&(to_container->clients), current_client, clients);
326 current_client->container = to_container;
327 container->currently_focused = to_focus;
328 to_container->currently_focused = current_client;
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(conn, current_client->frame);
336 /* delete all empty columns/rows */
337 cleanup_table(conn, container->workspace);
342 static void show_workspace(xcb_connection_t *conn, int workspace) {
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]);
348 printf("show_workspace(%d)\n", workspace);
350 /* Store current_row/current_col */
351 c_ws->current_row = current_row;
352 c_ws->current_col = current_col;
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));
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);
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));
377 /* Check if we need to change something or if we’re already there */
378 if (c_ws->screen->current_workspace == (workspace-1))
381 t_ws->screen->current_workspace = workspace-1;
383 /* TODO: does grabbing the server actually bring us any (speed)advantages? */
384 //xcb_grab_server(conn);
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);
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);
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);
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);
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);
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);
419 //xcb_ungrab_server(conn);
425 * Parses a command, see file CMDMODE for more information
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')
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 "));
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);
448 /* Is it 'f' for fullscreen? */
449 if (command[0] == 'f') {
450 if (CUR_CELL->currently_focused == NULL)
452 toggle_fullscreen(conn, CUR_CELL->currently_focused);
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));
463 /* Is it a <with>? */
464 if (command[0] == 'w') {
465 /* TODO: implement */
466 printf("not yet implemented.\n");
470 /* It's a normal <cmd> */
472 enum { ACTION_FOCUS, ACTION_MOVE, ACTION_SNAP } action = ACTION_FOCUS;
473 direction_t direction;
474 int times = strtol(command, &rest, 10);
476 printf("Invalid command (\"%s\")\n", command);
481 /* No rest? This was a tag number, not a times specification */
482 show_workspace(conn, times);
486 if (*rest == 'm' || *rest == 's') {
487 action = (*rest == 'm' ? ACTION_MOVE : ACTION_SNAP);
491 int workspace = strtol(rest, &rest, 10);
494 printf("Invalid command (\"%s\")\n", command);
499 move_current_window_to_workspace(conn, workspace);
503 /* Now perform action to <where> */
504 while (*rest != '\0') {
507 else if (*rest == 'j')
509 else if (*rest == 'k')
511 else if (*rest == 'l')
514 printf("unknown direction: %c\n", *rest);
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);
528 printf("--- done ---\n");