]> git.sur5r.net Git - i3/i3/blob - src/scratchpad.c
Merge branch 'fix-fullscreen-scratch'
[i3/i3] / src / scratchpad.c
1 /*
2  * vim:ts=4:sw=4:expandtab
3  *
4  * i3 - an improved dynamic tiling window manager
5  * © 2009-2011 Michael Stapelberg and contributors (see also: LICENSE)
6  *
7  * scratchpad.c: Moving windows to the scratchpad and making them visible again.
8  *
9  */
10 #include "all.h"
11
12 /*
13  * Moves the specified window to the __i3_scratch workspace, making it floating
14  * and setting the appropriate scratchpad_state.
15  *
16  * Gets called upon the command 'move scratchpad'.
17  *
18  */
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);
23         Con *current;
24         current = TAILQ_FIRST(&(con->focus_head));
25         while (current) {
26             Con *next = TAILQ_NEXT(current, focused);
27             scratchpad_move(current);
28             current = next;
29         }
30         return;
31     }
32     DLOG("should move con %p to __i3_scratch\n", con);
33
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");
37         return;
38     }
39
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);
44     con = con->parent;
45
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);
50
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;
57     }
58
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     if (con_get_workspace(focus_next) == con_get_workspace(focused))
62         con_focus(focus_next);
63 }
64
65 /*
66  * Either shows the top-most scratchpad window (con == NULL) or shows the
67  * specified con (if it is scratchpad window).
68  *
69  * When called with con == NULL and the currently focused window is a
70  * scratchpad window, this serves as a shortcut to hide it again (so the user
71  * can press the same key to quickly look something up).
72  *
73  */
74 void scratchpad_show(Con *con) {
75     DLOG("should show scratchpad window %p\n", con);
76     Con *__i3_scratch = workspace_get("__i3_scratch", NULL);
77     Con *floating;
78
79     /* If the current con or any of its parents are in fullscreen mode, we
80      * first need to disable it before showing the scratchpad con. */
81     Con *fs = focused;
82     while (fs && fs->fullscreen_mode == CF_NONE)
83         fs = fs->parent;
84
85     if (fs->type != CT_WORKSPACE) {
86         con_toggle_fullscreen(focused, CF_OUTPUT);
87     }
88
89     /* If this was 'scratchpad show' without criteria, we check if the
90      * currently focused window is a scratchpad window and should be hidden
91      * again. */
92     if (!con &&
93         (floating = con_inside_floating(focused)) &&
94         floating->scratchpad_state != SCRATCHPAD_NONE) {
95         DLOG("Focused window is a scratchpad window, hiding it.\n");
96         scratchpad_move(focused);
97         return;
98     }
99
100     /* If this was 'scratchpad show' with criteria, we check if it matches a
101      * currently visible scratchpad window and hide it. */
102     Con *active = con_get_workspace(focused);
103     Con *current = con_get_workspace(con);
104     if (con &&
105         (floating = con_inside_floating(con)) &&
106         floating->scratchpad_state != SCRATCHPAD_NONE &&
107         current != __i3_scratch) {
108         /* If scratchpad window is on the active workspace, then we should hide
109          * it, otherwise we should move it to the active workspace. */
110         if (current == active) {
111             DLOG("Window is a scratchpad window, hiding it.\n");
112             scratchpad_move(con);
113             return;
114         }
115     }
116
117     if (con == NULL) {
118         /* Use the container on __i3_scratch which is highest in the focus
119          * stack. When moving windows to __i3_scratch, they get inserted at the
120          * bottom of the stack. */
121         con = TAILQ_FIRST(&(__i3_scratch->floating_head));
122
123         if (!con) {
124             LOG("You don't have any scratchpad windows yet.\n");
125             LOG("Use 'move scratchpad' to move a window to the scratchpad.\n");
126             return;
127         }
128     }
129
130     /* 1: Move the window from __i3_scratch to the current workspace. */
131     con_move_to_workspace(con, active, true, false);
132
133     /* 2: Adjust the size if this window was not adjusted yet. */
134     if (con->scratchpad_state == SCRATCHPAD_FRESH) {
135         DLOG("Adjusting size of this window.\n");
136         Con *output = con_get_output(con);
137         con->rect.width = output->rect.width * 0.5;
138         con->rect.height = output->rect.height * 0.75;
139         con->rect.x = output->rect.x +
140                       ((output->rect.width / 2.0) - (con->rect.width / 2.0));
141         con->rect.y = output->rect.y +
142                       ((output->rect.height / 2.0) - (con->rect.height / 2.0));
143         con->scratchpad_state = SCRATCHPAD_CHANGED;
144     }
145
146     /* Activate active workspace if window is from another workspace to ensure
147      * proper focus. */
148     if (current != active) {
149         workspace_show(active);
150     }
151
152     con_focus(con_descend_focused(con));
153 }