2 * vim:ts=4:sw=4:expandtab
4 * i3 - an improved dynamic tiling window manager
5 * © 2009-2011 Michael Stapelberg and contributors (see also: LICENSE)
7 * scratchpad.c: Moving windows to the scratchpad and making them visible again.
13 * Moves the specified window to the __i3_scratch workspace, making it floating
14 * and setting the appropriate scratchpad_state.
16 * Gets called upon the command 'move scratchpad'.
19 void scratchpad_move(Con *con) {
20 if (con->type == CT_WORKSPACE) {
21 LOG("'move scratchpad' used on a workspace \"%s\". Calling it "
22 "recursively on all windows on this workspace.\n", con->name);
24 current = TAILQ_FIRST(&(con->focus_head));
26 Con *next = TAILQ_NEXT(current, focused);
27 scratchpad_move(current);
32 DLOG("should move con %p to __i3_scratch\n", con);
34 Con *__i3_scratch = workspace_get("__i3_scratch", NULL);
35 if (con_get_workspace(con) == __i3_scratch) {
36 DLOG("This window is already on __i3_scratch.\n");
40 /* 1: Ensure the window is floating. From now on, we deal with the
41 * CT_FLOATING_CON. We use automatic == false because the user made the
42 * choice that this window should be a scratchpad (and floating). */
43 floating_enable(con, false);
46 /* 2: Send the window to the __i3_scratch workspace, mainting its
47 * coordinates and not warping the pointer. */
48 Con *focus_next = con_next_focused(con);
49 con_move_to_workspace(con, __i3_scratch, true, true);
51 /* 3: If this is the first time this window is used as a scratchpad, we set
52 * the scratchpad_state to SCRATCHPAD_FRESH. The window will then be
53 * adjusted in size according to what the user specifies. */
54 if (con->scratchpad_state == SCRATCHPAD_NONE) {
55 DLOG("This window was never used as a scratchpad before.\n");
56 con->scratchpad_state = SCRATCHPAD_FRESH;
59 /* 4: Fix focus. Normally, when moving a window to a different output, the
60 * destination output gets focused. In this case, we don’t want that. */
61 con_focus(focus_next);
65 * Either shows the top-most scratchpad window (con == NULL) or shows the
66 * specified con (if it is scratchpad window).
68 * When called with con == NULL and the currently focused window is a
69 * scratchpad window, this serves as a shortcut to hide it again (so the user
70 * can press the same key to quickly look something up).
73 void scratchpad_show(Con *con) {
74 DLOG("should show scratchpad window %p\n", con);
75 Con *__i3_scratch = workspace_get("__i3_scratch", NULL);
78 /* If this was 'scratchpad show' without criteria, we check if the
79 * currently focused window is a scratchpad window and should be hidden
82 (floating = con_inside_floating(focused)) &&
83 floating->scratchpad_state != SCRATCHPAD_NONE) {
84 DLOG("Focused window is a scratchpad window, hiding it.\n");
85 scratchpad_move(focused);
89 /* If this was 'scratchpad show' with criteria, we check if it matches a
90 * currently visible scratchpad window and hide it. */
91 Con *active = con_get_workspace(focused);
92 Con *current = con_get_workspace(con);
94 (floating = con_inside_floating(con)) &&
95 floating->scratchpad_state != SCRATCHPAD_NONE &&
96 current != __i3_scratch) {
97 /* If scratchpad window is on the active workspace, then we should hide
98 * it, otherwise we should move it to the active workspace. */
99 if (current == active) {
100 DLOG("Window is a scratchpad window, hiding it.\n");
101 scratchpad_move(con);
107 /* Use the container on __i3_scratch which is highest in the focus
108 * stack. When moving windows to __i3_scratch, they get inserted at the
109 * bottom of the stack. */
110 con = TAILQ_FIRST(&(__i3_scratch->floating_head));
113 LOG("You don't have any scratchpad windows yet.\n");
114 LOG("Use 'move scratchpad' to move a window to the scratchpad.\n");
119 /* 1: Move the window from __i3_scratch to the current workspace. */
120 con_move_to_workspace(con, active, true, false);
122 /* 2: Adjust the size if this window was not adjusted yet. */
123 if (con->scratchpad_state == SCRATCHPAD_FRESH) {
124 DLOG("Adjusting size of this window.\n");
125 Con *output = con_get_output(con);
126 con->rect.width = output->rect.width * 0.5;
127 con->rect.height = output->rect.height * 0.75;
128 con->rect.x = output->rect.x +
129 ((output->rect.width / 2.0) - (con->rect.width / 2.0));
130 con->rect.y = output->rect.y +
131 ((output->rect.height / 2.0) - (con->rect.height / 2.0));
132 con->scratchpad_state = SCRATCHPAD_CHANGED;
135 /* Activate active workspace if window is from another workspace to ensure
137 if (current != active) {
138 workspace_show(active);
141 con_focus(con_descend_focused(con));