4 * i3 - an improved dynamic tiling window manager
6 * © 2009 Michael Stapelberg and contributors
8 * See file LICENSE for license information.
30 bool focus_window_in_container(xcb_connection_t *conn, Container *container, direction_t direction) {
31 /* If this container is empty, we’re done */
32 if (container->currently_focused == NULL)
35 /* Get the previous/next client or wrap around */
36 Client *candidate = NULL;
37 if (direction == D_UP) {
38 if ((candidate = CIRCLEQ_PREV_OR_NULL(&(container->clients), container->currently_focused, clients)) == NULL)
39 candidate = CIRCLEQ_LAST(&(container->clients));
41 else if (direction == D_DOWN) {
42 if ((candidate = CIRCLEQ_NEXT_OR_NULL(&(container->clients), container->currently_focused, clients)) == NULL)
43 candidate = CIRCLEQ_FIRST(&(container->clients));
44 } else LOG("Direction not implemented!\n");
46 /* If we could not switch, the container contains exactly one client. We return false */
47 if (candidate == container->currently_focused)
51 set_focus(conn, candidate, true);
56 typedef enum { THING_WINDOW, THING_CONTAINER } thing_t;
58 static void focus_thing(xcb_connection_t *conn, direction_t direction, thing_t thing) {
59 LOG("focusing direction %d\n", direction);
61 int new_row = current_row,
62 new_col = current_col;
63 Container *container = CUR_CELL;
64 Workspace *t_ws = c_ws;
66 /* Makes sure new_col and new_row are within bounds of the new workspace */
67 void check_colrow_boundaries() {
68 if (new_col >= t_ws->cols)
69 new_col = (t_ws->cols - 1);
70 if (new_row >= t_ws->rows)
71 new_row = (t_ws->rows - 1);
74 /* There always is a container. If not, current_col or current_row is wrong */
75 assert(container != NULL);
77 if (container->workspace->fullscreen_client != NULL) {
78 LOG("You're in fullscreen mode. Won't switch focus\n");
82 /* TODO: for horizontal default layout, this has to be expanded to LEFT/RIGHT */
83 if (direction == D_UP || direction == D_DOWN) {
84 if (thing == THING_WINDOW)
85 /* Let’s see if we can perform up/down focus in the current container */
86 if (focus_window_in_container(conn, container, direction))
89 if (direction == D_DOWN && cell_exists(current_col, current_row+1))
90 new_row = current_row + t_ws->table[current_col][current_row]->rowspan;
91 else if (direction == D_UP && cell_exists(current_col, current_row-1)) {
92 /* Set new_row as a sane default, but it may get overwritten in a second */
95 /* Search from the top to correctly handle rowspanned containers */
96 for (int rows = 0; rows < current_row; rows += t_ws->table[current_col][rows]->rowspan) {
97 if (new_row > (rows + (t_ws->table[current_col][rows]->rowspan - 1)))
104 /* Let’s see if there is a screen down/up there to which we can switch */
105 LOG("container is at %d with height %d\n", container->y, container->height);
107 int destination_y = (direction == D_UP ? (container->y - 1) : (container->y + container->height + 1));
108 if ((screen = get_screen_containing(container->x, destination_y)) == NULL) {
109 LOG("Wrapping screen around vertically\n");
110 /* No screen found? Then wrap */
111 screen = get_screen_most((direction == D_UP ? D_DOWN : D_UP));
113 t_ws = &(workspaces[screen->current_workspace]);
114 new_row = (direction == D_UP ? (t_ws->rows - 1) : 0);
117 check_colrow_boundaries();
119 LOG("new_col = %d, new_row = %d\n", new_col, new_row);
120 if (t_ws->table[new_col][new_row]->currently_focused == NULL) {
121 LOG("Cell empty, checking for colspanned client above...\n");
122 for (int cols = 0; cols < new_col; cols += t_ws->table[cols][new_row]->colspan) {
123 if (new_col > (cols + (t_ws->table[cols][new_row]->colspan - 1)))
129 LOG("Fixed it to new col %d\n", new_col);
131 } else if (direction == D_LEFT || direction == D_RIGHT) {
132 if (direction == D_RIGHT && cell_exists(current_col+1, current_row))
133 new_col = current_col + t_ws->table[current_col][current_row]->colspan;
134 else if (direction == D_LEFT && cell_exists(current_col-1, current_row)) {
135 /* Set new_col as a sane default, but it may get overwritten in a second */
138 /* Search from the left to correctly handle colspanned containers */
139 for (int cols = 0; cols < current_col; cols += t_ws->table[cols][current_row]->colspan) {
140 if (new_col > (cols + (t_ws->table[cols][current_row]->colspan - 1)))
147 /* Let’s see if there is a screen left/right here to which we can switch */
148 LOG("container is at %d with width %d\n", container->x, container->width);
150 int destination_x = (direction == D_LEFT ? (container->x - 1) : (container->x + container->width + 1));
151 if ((screen = get_screen_containing(destination_x, container->y)) == NULL) {
152 LOG("Wrapping screen around horizontally\n");
153 screen = get_screen_most((direction == D_LEFT ? D_RIGHT : D_LEFT));
155 t_ws = &(workspaces[screen->current_workspace]);
156 new_col = (direction == D_LEFT ? (t_ws->cols - 1) : 0);
159 check_colrow_boundaries();
161 LOG("new_col = %d, new_row = %d\n", new_col, new_row);
162 if (t_ws->table[new_col][new_row]->currently_focused == NULL) {
163 LOG("Cell empty, checking for rowspanned client above...\n");
164 for (int rows = 0; rows < new_row; rows += t_ws->table[new_col][rows]->rowspan) {
165 if (new_row > (rows + (t_ws->table[new_col][rows]->rowspan - 1)))
171 LOG("Fixed it to new row %d\n", new_row);
174 LOG("direction unhandled\n");
178 check_colrow_boundaries();
180 if (t_ws->table[new_col][new_row]->currently_focused != NULL)
181 set_focus(conn, t_ws->table[new_col][new_row]->currently_focused, true);
185 * Tries to move the window inside its current container.
187 * Returns true if the window could be moved, false otherwise.
190 static bool move_current_window_in_container(xcb_connection_t *conn, Client *client,
191 direction_t direction) {
192 assert(client->container != NULL);
194 Client *other = (direction == D_UP ? CIRCLEQ_PREV(client, clients) :
195 CIRCLEQ_NEXT(client, clients));
197 if (other == CIRCLEQ_END(&(client->container->clients)))
200 LOG("i can do that\n");
201 /* We can move the client inside its current container */
202 CIRCLEQ_REMOVE(&(client->container->clients), client, clients);
203 if (direction == D_UP)
204 CIRCLEQ_INSERT_BEFORE(&(client->container->clients), other, client, clients);
205 else CIRCLEQ_INSERT_AFTER(&(client->container->clients), other, client, clients);
211 * Moves the current window or whole container to the given direction, creating a column/row if
215 static void move_current_window(xcb_connection_t *conn, direction_t direction) {
216 LOG("moving window to direction %s\n", (direction == D_UP ? "up" : (direction == D_DOWN ? "down" :
217 (direction == D_LEFT ? "left" : "right"))));
218 /* Get current window */
219 Container *container = CUR_CELL,
222 /* There has to be a container, see focus_window() */
223 assert(container != NULL);
225 /* If there is no window or the dock window is focused, we’re done */
226 if (container->currently_focused == NULL ||
227 container->currently_focused->dock)
230 /* As soon as the client is moved away, the last focused client in the old
231 * container needs to get focus, if any. Therefore, we save it here. */
232 Client *current_client = container->currently_focused;
233 Client *to_focus = get_last_focused_client(conn, container, current_client);
235 if (to_focus == NULL) {
236 to_focus = CIRCLEQ_NEXT_OR_NULL(&(container->clients), current_client, clients);
237 if (to_focus == NULL)
238 to_focus = CIRCLEQ_PREV_OR_NULL(&(container->clients), current_client, clients);
243 /* If we’re at the left-most position, move the rest of the table right */
244 if (current_col == 0) {
245 expand_table_cols_at_head(c_ws);
248 new = CUR_TABLE[--current_col][current_row];
251 if (current_col == (c_ws->cols-1))
252 expand_table_cols(c_ws);
254 new = CUR_TABLE[++current_col][current_row];
257 if (move_current_window_in_container(conn, current_client, D_UP))
260 /* if we’re at the up-most position, move the rest of the table down */
261 if (current_row == 0) {
262 expand_table_rows_at_head(c_ws);
265 new = CUR_TABLE[current_col][--current_row];
268 if (move_current_window_in_container(conn, current_client, D_DOWN))
271 if (current_row == (c_ws->rows-1))
272 expand_table_rows(c_ws);
274 new = CUR_TABLE[current_col][++current_row];
276 /* To make static analyzers happy: */
281 /* Remove it from the old container and put it into the new one */
282 client_remove_from_container(conn, current_client, container, true);
284 if (new->currently_focused != NULL)
285 CIRCLEQ_INSERT_AFTER(&(new->clients), new->currently_focused, current_client, clients);
286 else CIRCLEQ_INSERT_TAIL(&(new->clients), current_client, clients);
287 SLIST_INSERT_HEAD(&(new->workspace->focus_stack), current_client, focus_clients);
289 /* Update data structures */
290 current_client->container = new;
291 current_client->workspace = new->workspace;
292 container->currently_focused = to_focus;
293 new->currently_focused = current_client;
295 Workspace *workspace = container->workspace;
297 /* delete all empty columns/rows */
298 cleanup_table(conn, workspace);
300 /* Fix colspan/rowspan if it’d overlap */
301 fix_colrowspan(conn, workspace);
303 render_workspace(conn, workspace->screen, workspace);
306 set_focus(conn, current_client, true);
309 static void move_current_container(xcb_connection_t *conn, direction_t direction) {
310 LOG("moving container to direction %s\n", (direction == D_UP ? "up" : (direction == D_DOWN ? "down" :
311 (direction == D_LEFT ? "left" : "right"))));
312 /* Get current window */
313 Container *container = CUR_CELL,
316 Container **old = &CUR_CELL;
318 /* There has to be a container, see focus_window() */
319 assert(container != NULL);
323 /* If we’re at the left-most position, move the rest of the table right */
324 if (current_col == 0) {
325 expand_table_cols_at_head(c_ws);
327 old = &CUR_TABLE[current_col+1][current_row];
329 new = CUR_TABLE[--current_col][current_row];
332 if (current_col == (c_ws->cols-1))
333 expand_table_cols(c_ws);
335 new = CUR_TABLE[++current_col][current_row];
338 /* if we’re at the up-most position, move the rest of the table down */
339 if (current_row == 0) {
340 expand_table_rows_at_head(c_ws);
342 old = &CUR_TABLE[current_col][current_row+1];
344 new = CUR_TABLE[current_col][--current_row];
347 if (current_row == (c_ws->rows-1))
348 expand_table_rows(c_ws);
350 new = CUR_TABLE[current_col][++current_row];
352 /* To make static analyzers happy: */
357 LOG("old = %d,%d and new = %d,%d\n", container->col, container->row, new->col, new->row);
359 /* Swap the containers */
364 new->col = container->col;
365 new->row = container->row;
367 CUR_CELL = container;
368 container->col = col;
369 container->row = row;
371 Workspace *workspace = container->workspace;
373 /* delete all empty columns/rows */
374 cleanup_table(conn, workspace);
376 /* Fix colspan/rowspan if it’d overlap */
377 fix_colrowspan(conn, workspace);
383 * "Snaps" the current container (not possible for windows, because it works at table base)
384 * to the given direction, that is, adjusts cellspan/rowspan
387 static void snap_current_container(xcb_connection_t *conn, direction_t direction) {
388 LOG("snapping container to direction %d\n", direction);
390 Container *container = CUR_CELL;
392 assert(container != NULL);
396 /* Snap to the left is actually a move to the left and then a snap right */
397 if (!cell_exists(container->col - 1, container->row) ||
398 CUR_TABLE[container->col-1][container->row]->currently_focused != NULL) {
399 LOG("cannot snap to left - the cell is already used\n");
403 move_current_window(conn, D_LEFT);
404 snap_current_container(conn, D_RIGHT);
407 /* Check if the cell is used */
408 int new_col = container->col + container->colspan;
409 for (int i = 0; i < container->rowspan; i++)
410 if (!cell_exists(new_col, container->row + i) ||
411 CUR_TABLE[new_col][container->row + i]->currently_focused != NULL) {
412 LOG("cannot snap to right - the cell is already used\n");
416 /* Check if there are other cells with rowspan, which are in our way.
417 * If so, reduce their rowspan. */
418 for (int i = container->row-1; i >= 0; i--) {
419 LOG("we got cell %d, %d with rowspan %d\n",
420 new_col, i, CUR_TABLE[new_col][i]->rowspan);
421 while ((CUR_TABLE[new_col][i]->rowspan-1) >= (container->row - i))
422 CUR_TABLE[new_col][i]->rowspan--;
423 LOG("new rowspan = %d\n", CUR_TABLE[new_col][i]->rowspan);
426 container->colspan++;
430 if (!cell_exists(container->col, container->row - 1) ||
431 CUR_TABLE[container->col][container->row-1]->currently_focused != NULL) {
432 LOG("cannot snap to top - the cell is already used\n");
436 move_current_window(conn, D_UP);
437 snap_current_container(conn, D_DOWN);
440 LOG("snapping down\n");
441 int new_row = container->row + container->rowspan;
442 for (int i = 0; i < container->colspan; i++)
443 if (!cell_exists(container->col + i, new_row) ||
444 CUR_TABLE[container->col + i][new_row]->currently_focused != NULL) {
445 LOG("cannot snap down - the cell is already used\n");
449 for (int i = container->col-1; i >= 0; i--) {
450 LOG("we got cell %d, %d with colspan %d\n",
451 i, new_row, CUR_TABLE[i][new_row]->colspan);
452 while ((CUR_TABLE[i][new_row]->colspan-1) >= (container->col - i))
453 CUR_TABLE[i][new_row]->colspan--;
454 LOG("new colspan = %d\n", CUR_TABLE[i][new_row]->colspan);
458 container->rowspan++;
461 /* To make static analyzers happy: */
469 static void move_floating_window_to_workspace(xcb_connection_t *conn, Client *client, int workspace) {
470 /* t_ws (to workspace) is just a container pointer to the workspace we’re switching to */
471 Workspace *t_ws = &(workspaces[workspace-1]),
472 *old_ws = client->workspace;
474 LOG("moving floating\n");
476 if (t_ws->screen == NULL) {
477 LOG("initializing new workspace, setting num to %d\n", workspace-1);
478 t_ws->screen = c_ws->screen;
479 /* Copy the dimensions from the virtual screen */
480 memcpy(&(t_ws->rect), &(t_ws->screen->rect), sizeof(Rect));
482 /* Check if there is already a fullscreen client on the destination workspace and
483 * stop moving if so. */
484 if (client->fullscreen && (t_ws->fullscreen_client != NULL)) {
485 LOG("Not moving: Fullscreen client already existing on destination workspace.\n");
490 floating_assign_to_workspace(client, t_ws);
492 bool target_invisible = t_ws->screen->current_workspace != t_ws->num;
494 /* If we’re moving it to an invisible screen, we need to unmap it */
495 if (target_invisible) {
496 LOG("This workspace is not visible, unmapping\n");
497 xcb_unmap_window(conn, client->frame);
499 /* If this is not the case, we move the window to a workspace
500 * which is on another screen, so we also need to adjust its
502 LOG("before x = %d, y = %d\n", client->rect.x, client->rect.y);
503 uint32_t relative_x = client->rect.x - old_ws->rect.x,
504 relative_y = client->rect.y - old_ws->rect.y;
505 LOG("rel_x = %d, rel_y = %d\n", relative_x, relative_y);
506 client->rect.x = t_ws->rect.x + relative_x;
507 client->rect.y = t_ws->rect.y + relative_y;
508 LOG("after x = %d, y = %d\n", client->rect.x, client->rect.y);
509 reposition_client(conn, client);
517 if (!target_invisible)
518 set_focus(conn, client, true);
522 * Moves the currently selected window to the given workspace
525 static void move_current_window_to_workspace(xcb_connection_t *conn, int workspace) {
526 LOG("Moving current window to workspace %d\n", workspace);
528 Container *container = CUR_CELL;
530 assert(container != NULL);
532 /* t_ws (to workspace) is just a container pointer to the workspace we’re switching to */
533 Workspace *t_ws = &(workspaces[workspace-1]);
535 Client *current_client = container->currently_focused;
536 if (current_client == NULL) {
537 LOG("No currently focused client in current container.\n");
540 Client *to_focus = CIRCLEQ_NEXT_OR_NULL(&(container->clients), current_client, clients);
541 if (to_focus == NULL)
542 to_focus = CIRCLEQ_PREV_OR_NULL(&(container->clients), current_client, clients);
544 if (t_ws->screen == NULL) {
545 LOG("initializing new workspace, setting num to %d\n", workspace-1);
546 t_ws->screen = container->workspace->screen;
547 /* Copy the dimensions from the virtual screen */
548 memcpy(&(t_ws->rect), &(container->workspace->screen->rect), sizeof(Rect));
550 /* Check if there is already a fullscreen client on the destination workspace and
551 * stop moving if so. */
552 if (current_client->fullscreen && (t_ws->fullscreen_client != NULL)) {
553 LOG("Not moving: Fullscreen client already existing on destination workspace.\n");
558 Container *to_container = t_ws->table[t_ws->current_col][t_ws->current_row];
560 assert(to_container != NULL);
562 client_remove_from_container(conn, current_client, container, true);
563 if (container->workspace->fullscreen_client == current_client)
564 container->workspace->fullscreen_client = NULL;
566 /* TODO: insert it to the correct position */
567 CIRCLEQ_INSERT_TAIL(&(to_container->clients), current_client, clients);
569 SLIST_INSERT_HEAD(&(to_container->workspace->focus_stack), current_client, focus_clients);
572 current_client->container = to_container;
573 current_client->workspace = to_container->workspace;
574 container->currently_focused = to_focus;
575 to_container->currently_focused = current_client;
577 bool target_invisible = (to_container->workspace->screen->current_workspace != to_container->workspace->num);
579 /* If we’re moving it to an invisible screen, we need to unmap it */
580 if (target_invisible) {
581 LOG("This workspace is not visible, unmapping\n");
582 xcb_unmap_window(conn, current_client->frame);
584 if (current_client->fullscreen) {
585 LOG("Calling client_enter_fullscreen again\n");
586 client_enter_fullscreen(conn, current_client);
590 /* delete all empty columns/rows */
591 cleanup_table(conn, container->workspace);
595 if (!target_invisible)
596 set_focus(conn, current_client, true);
600 * Switches to the given workspace
603 void show_workspace(xcb_connection_t *conn, int workspace) {
605 bool need_warp = false;
606 xcb_window_t root = xcb_setup_roots_iterator(xcb_get_setup(conn)).data->root;
607 /* t_ws (to workspace) is just a convenience pointer to the workspace we’re switching to */
608 Workspace *t_ws = &(workspaces[workspace-1]);
610 LOG("show_workspace(%d)\n", workspace);
612 /* Store current_row/current_col */
613 c_ws->current_row = current_row;
614 c_ws->current_col = current_col;
616 /* Check if the workspace has not been used yet */
617 if (t_ws->screen == NULL) {
618 LOG("initializing new workspace, setting num to %d\n", workspace);
619 t_ws->screen = c_ws->screen;
620 /* Copy the dimensions from the virtual screen */
621 memcpy(&(t_ws->rect), &(t_ws->screen->rect), sizeof(Rect));
624 if (c_ws->screen != t_ws->screen) {
625 /* We need to switch to the other screen first */
626 LOG("moving over to other screen.\n");
628 /* Store the old client */
629 Client *old_client = CUR_CELL->currently_focused;
631 c_ws = &(workspaces[t_ws->screen->current_workspace]);
632 current_col = c_ws->current_col;
633 current_row = c_ws->current_row;
634 if (CUR_CELL->currently_focused != NULL)
637 Rect *dims = &(c_ws->screen->rect);
638 xcb_warp_pointer(conn, XCB_NONE, root, 0, 0, 0, 0,
639 dims->x + (dims->width / 2), dims->y + (dims->height / 2));
642 /* Re-decorate the old client, it’s not focused anymore */
643 if ((old_client != NULL) && !old_client->dock)
644 redecorate_window(conn, old_client);
645 else xcb_flush(conn);
648 /* Check if we need to change something or if we’re already there */
649 if (c_ws->screen->current_workspace == (workspace-1)) {
650 Client *last_focused = SLIST_FIRST(&(c_ws->focus_stack));
651 if (last_focused != SLIST_END(&(c_ws->focus_stack))) {
652 set_focus(conn, last_focused, true);
654 client_warp_pointer_into(conn, last_focused);
662 t_ws->screen->current_workspace = workspace-1;
663 Workspace *old_workspace = c_ws;
664 c_ws = &workspaces[workspace-1];
666 /* Unmap all clients of the old workspace */
667 unmap_workspace(conn, old_workspace);
669 current_row = c_ws->current_row;
670 current_col = c_ws->current_col;
671 LOG("new current row = %d, current col = %d\n", current_row, current_col);
673 ignore_enter_notify_forall(conn, c_ws, true);
675 /* Map all clients on the new workspace */
677 CIRCLEQ_FOREACH(client, &(c_ws->table[cols][rows]->clients), clients)
678 xcb_map_window(conn, client->frame);
680 /* Map all floating clients */
681 if (!c_ws->floating_hidden)
682 TAILQ_FOREACH(client, &(c_ws->floating_clients), floating_clients)
683 xcb_map_window(conn, client->frame);
685 /* Map all stack windows, if any */
686 struct Stack_Window *stack_win;
687 SLIST_FOREACH(stack_win, &stack_wins, stack_windows)
688 if (stack_win->container->workspace == c_ws)
689 xcb_map_window(conn, stack_win->window);
691 ignore_enter_notify_forall(conn, c_ws, false);
693 /* Restore focus on the new workspace */
694 Client *last_focused = SLIST_FIRST(&(c_ws->focus_stack));
695 if (last_focused != SLIST_END(&(c_ws->focus_stack))) {
696 set_focus(conn, last_focused, true);
698 client_warp_pointer_into(conn, last_focused);
701 } else xcb_set_input_focus(conn, XCB_INPUT_FOCUS_POINTER_ROOT, root, XCB_CURRENT_TIME);
707 * Jumps to the given window class / title.
708 * Title is matched using strstr, that is, matches if it appears anywhere
709 * in the string. Regular expressions seem to be a bit overkill here. However,
710 * if we need them for something else somewhen, we may introduce them here, too.
713 static void jump_to_window(xcb_connection_t *conn, const char *arguments) {
717 /* The first character is a quote, this was checked before */
718 classtitle = sstrdup(arguments+1);
719 /* The last character is a quote, we just set it to NULL */
720 classtitle[strlen(classtitle)-1] = '\0';
722 if ((client = get_matching_client(conn, classtitle, NULL)) == NULL) {
724 LOG("No matching client found.\n");
729 set_focus(conn, client, true);
733 * Jump directly to the specified workspace, row and col.
734 * Great for reaching windows that you always keep in the same spot (hello irssi, I'm looking at you)
737 static void jump_to_container(xcb_connection_t *conn, const char *arguments) {
741 result = sscanf(arguments, "%d %d %d", &ws, &col, &row);
742 LOG("Jump called with %d parameters (\"%s\")\n", result, arguments);
744 /* No match? Either no arguments were specified, or no numbers */
746 LOG("At least one valid argument required\n");
750 /* Move to the target workspace */
751 show_workspace(conn, ws);
756 LOG("Boundary-checking col %d, row %d... (max cols %d, max rows %d)\n", col, row, c_ws->cols, c_ws->rows);
758 /* Move to row/col */
759 if (row >= c_ws->rows)
760 row = c_ws->rows - 1;
761 if (col >= c_ws->cols)
762 col = c_ws->cols - 1;
764 LOG("Jumping to col %d, row %d\n", col, row);
765 if (c_ws->table[col][row]->currently_focused != NULL)
766 set_focus(conn, c_ws->table[col][row]->currently_focused, true);
770 * Travels the focus stack by the given number of times (or once, if no argument
771 * was specified). That is, selects the window you were in before you focused
772 * the current window.
774 * The special values 'floating' (select the next floating window), 'tiling'
775 * (select the next tiling window), 'ft' (if the current window is floating,
776 * select the next tiling window and vice-versa) are also valid
779 static void travel_focus_stack(xcb_connection_t *conn, const char *arguments) {
780 /* Start count at -1 to always skip the first element */
781 int times, count = -1;
783 bool floating_criteria;
785 /* Either it’s one of the special values… */
786 if (strcasecmp(arguments, "floating") == 0) {
787 floating_criteria = true;
788 } else if (strcasecmp(arguments, "tiling") == 0) {
789 floating_criteria = false;
790 } else if (strcasecmp(arguments, "ft") == 0) {
791 Client *last_focused = SLIST_FIRST(&(c_ws->focus_stack));
792 if (last_focused == SLIST_END(&(c_ws->focus_stack))) {
793 LOG("Cannot select the next floating/tiling client because there is no client at all\n");
797 floating_criteria = !client_is_floating(last_focused);
799 /* …or a number was specified */
800 if (sscanf(arguments, "%u", ×) != 1) {
801 LOG("No or invalid argument given (\"%s\"), using default of 1 times\n", arguments);
805 SLIST_FOREACH(current, &(CUR_CELL->workspace->focus_stack), focus_clients) {
806 if (++count < times) {
812 set_focus(conn, current, true);
818 /* Select the next client matching the criteria parsed above */
819 SLIST_FOREACH(current, &(CUR_CELL->workspace->focus_stack), focus_clients)
820 if (client_is_floating(current) == floating_criteria) {
821 set_focus(conn, current, true);
827 * Goes through the list of arguments (for exec()) and checks if the given argument
828 * is present. If not, it copies the arguments (because we cannot realloc it) and
829 * appends the given argument.
832 static char **append_argument(char **original, char *argument) {
834 for (num_args = 0; original[num_args] != NULL; num_args++) {
835 LOG("original argument: \"%s\"\n", original[num_args]);
836 /* If the argument is already present we return the original pointer */
837 if (strcmp(original[num_args], argument) == 0)
840 /* Copy the original array */
841 char **result = smalloc((num_args+2) * sizeof(char*));
842 memcpy(result, original, num_args * sizeof(char*));
843 result[num_args] = argument;
844 result[num_args+1] = NULL;
850 * Parses a command, see file CMDMODE for more information
853 void parse_command(xcb_connection_t *conn, const char *command) {
854 LOG("--- parsing command \"%s\" ---\n", command);
855 /* Get the first client from focus stack because floating clients are not
856 * in any container, therefore CUR_CELL is not appropriate. */
857 Client *last_focused = SLIST_FIRST(&(c_ws->focus_stack));
858 if (last_focused == SLIST_END(&(c_ws->focus_stack)))
861 /* Hmm, just to be sure */
862 if (command[0] == '\0')
865 /* Is it an <exec>? Then execute the given command. */
866 if (STARTS_WITH(command, "exec ")) {
867 LOG("starting \"%s\"\n", command + strlen("exec "));
868 start_application(command+strlen("exec "));
872 /* Is it an <exit>? */
873 if (STARTS_WITH(command, "exit")) {
874 LOG("User issued exit-command, exiting without error.\n");
878 /* Is it <restart>? Then restart in place. */
879 if (STARTS_WITH(command, "restart")) {
880 LOG("restarting \"%s\"...\n", start_argv[0]);
881 /* make sure -a is in the argument list or append it */
882 start_argv = append_argument(start_argv, "-a");
884 execvp(start_argv[0], start_argv);
888 if (STARTS_WITH(command, "kill")) {
889 if (last_focused == NULL) {
890 LOG("There is no window to kill\n");
894 LOG("Killing current window\n");
895 client_kill(conn, last_focused);
899 /* Is it a jump to a specified workspace, row, col? */
900 if (STARTS_WITH(command, "jump ")) {
901 const char *arguments = command + strlen("jump ");
902 if (arguments[0] == '"')
903 jump_to_window(conn, arguments);
904 else jump_to_container(conn, arguments);
908 /* Should we travel the focus stack? */
909 if (STARTS_WITH(command, "focus")) {
910 const char *arguments = command + strlen("focus ");
911 travel_focus_stack(conn, arguments);
915 /* Is it 'f' for fullscreen? */
916 if (command[0] == 'f') {
917 if (last_focused == NULL)
919 client_toggle_fullscreen(conn, last_focused);
923 /* Is it just 's' for stacking or 'd' for default? */
924 if ((command[0] == 's' || command[0] == 'd') && (command[1] == '\0')) {
925 if (last_focused == NULL || client_is_floating(last_focused)) {
926 LOG("not switching, this is a floating client\n");
929 LOG("Switching mode for current container\n");
930 switch_layout_mode(conn, CUR_CELL, (command[0] == 's' ? MODE_STACK : MODE_DEFAULT));
934 if (command[0] == 'H') {
935 LOG("Hiding all floating windows\n");
936 floating_toggle_hide(conn, c_ws);
940 enum { WITH_WINDOW, WITH_CONTAINER, WITH_WORKSPACE } with = WITH_WINDOW;
942 /* Is it a <with>? */
943 if (command[0] == 'w') {
945 /* TODO: implement */
946 if (command[0] == 'c') {
947 with = WITH_CONTAINER;
949 } else if (command[0] == 'w') {
950 with = WITH_WORKSPACE;
953 LOG("not yet implemented.\n");
958 /* Is it 't' for toggle tiling/floating? */
959 if (command[0] == 't') {
960 if (with == WITH_WORKSPACE) {
961 c_ws->auto_float = !c_ws->auto_float;
962 LOG("autofloat is now %d\n", c_ws->auto_float);
965 if (last_focused == NULL) {
966 LOG("Cannot toggle tiling/floating: workspace empty\n");
970 toggle_floating_mode(conn, last_focused, false);
971 /* delete all empty columns/rows */
972 cleanup_table(conn, last_focused->workspace);
974 /* Fix colspan/rowspan if it’d overlap */
975 fix_colrowspan(conn, last_focused->workspace);
977 render_workspace(conn, last_focused->workspace->screen, last_focused->workspace);
979 /* Re-focus the client because cleanup_table sets the focus to the last
980 * focused client inside a container only. */
981 set_focus(conn, last_focused, true);
986 /* It’s a normal <cmd> */
988 enum { ACTION_FOCUS, ACTION_MOVE, ACTION_SNAP } action = ACTION_FOCUS;
989 direction_t direction;
990 int times = strtol(command, &rest, 10);
992 LOG("Invalid command (\"%s\")\n", command);
997 /* No rest? This was a workspace number, not a times specification */
998 show_workspace(conn, times);
1002 if (*rest == 'm' || *rest == 's') {
1003 action = (*rest == 'm' ? ACTION_MOVE : ACTION_SNAP);
1007 int workspace = strtol(rest, &rest, 10);
1010 LOG("Invalid command (\"%s\")\n", command);
1014 if (*rest == '\0') {
1015 if (last_focused != NULL && client_is_floating(last_focused))
1016 move_floating_window_to_workspace(conn, last_focused, workspace);
1017 else move_current_window_to_workspace(conn, workspace);
1021 if (last_focused == NULL) {
1022 LOG("Not performing (no window found)\n");
1026 if (client_is_floating(last_focused) &&
1027 (action != ACTION_FOCUS && action != ACTION_MOVE)) {
1028 LOG("Not performing (floating)\n");
1032 /* Now perform action to <where> */
1033 while (*rest != '\0') {
1036 else if (*rest == 'j')
1038 else if (*rest == 'k')
1040 else if (*rest == 'l')
1041 direction = D_RIGHT;
1043 LOG("unknown direction: %c\n", *rest);
1048 if (action == ACTION_FOCUS) {
1049 if (client_is_floating(last_focused)) {
1050 floating_focus_direction(conn, last_focused, direction);
1053 focus_thing(conn, direction, (with == WITH_WINDOW ? THING_WINDOW : THING_CONTAINER));
1057 if (action == ACTION_MOVE) {
1058 if (client_is_floating(last_focused)) {
1059 floating_move(conn, last_focused, direction);
1062 if (with == WITH_WINDOW)
1063 move_current_window(conn, direction);
1064 else move_current_container(conn, direction);
1068 if (action == ACTION_SNAP) {
1069 snap_current_container(conn, direction);
1074 LOG("--- done ---\n");