]> git.sur5r.net Git - i3/i3/blob - src/scratchpad.c
Merge branch 'master' into next
[i3/i3] / src / scratchpad.c
1 #undef I3__FILE__
2 #define I3__FILE__ "scratchpad.c"
3 /*
4  * vim:ts=4:sw=4:expandtab
5  *
6  * i3 - an improved dynamic tiling window manager
7  * © 2009-2011 Michael Stapelberg and contributors (see also: LICENSE)
8  *
9  * scratchpad.c: Moving windows to the scratchpad and making them visible again.
10  *
11  */
12 #include "all.h"
13
14 /*
15  * Moves the specified window to the __i3_scratch workspace, making it floating
16  * and setting the appropriate scratchpad_state.
17  *
18  * Gets called upon the command 'move scratchpad'.
19  *
20  */
21 void scratchpad_move(Con *con) {
22     if (con->type == CT_WORKSPACE) {
23         LOG("'move scratchpad' used on a workspace \"%s\". Calling it "
24             "recursively on all windows on this workspace.\n", con->name);
25         Con *current;
26         current = TAILQ_FIRST(&(con->focus_head));
27         while (current) {
28             Con *next = TAILQ_NEXT(current, focused);
29             scratchpad_move(current);
30             current = next;
31         }
32         return;
33     }
34     DLOG("should move con %p to __i3_scratch\n", con);
35
36     Con *__i3_scratch = workspace_get("__i3_scratch", NULL);
37     if (con_get_workspace(con) == __i3_scratch) {
38         DLOG("This window is already on __i3_scratch.\n");
39         return;
40     }
41
42     /* 1: Ensure the window or any parent is floating. From now on, we deal
43      * with the CT_FLOATING_CON. We use automatic == false because the user
44      * made the choice that this window should be a scratchpad (and floating).
45      */
46     Con *maybe_floating_con = con_inside_floating(con);
47     if (maybe_floating_con == NULL) {
48         floating_enable(con, false);
49         con = con->parent;
50     } else {
51         con = maybe_floating_con;
52     }
53
54     /* 2: Send the window to the __i3_scratch workspace, mainting its
55      * coordinates and not warping the pointer. */
56     Con *focus_next = con_next_focused(con);
57     con_move_to_workspace(con, __i3_scratch, true, true);
58
59     /* 3: If this is the first time this window is used as a scratchpad, we set
60      * the scratchpad_state to SCRATCHPAD_FRESH. The window will then be
61      * adjusted in size according to what the user specifies. */
62     if (con->scratchpad_state == SCRATCHPAD_NONE) {
63         DLOG("This window was never used as a scratchpad before.\n");
64         con->scratchpad_state = SCRATCHPAD_FRESH;
65     }
66
67     /* 4: Fix focus. Normally, when moving a window to a different output, the
68      * destination output gets focused. In this case, we don’t want that. */
69     if (con_get_workspace(focus_next) == con_get_workspace(focused))
70         con_focus(focus_next);
71 }
72
73 /*
74  * Either shows the top-most scratchpad window (con == NULL) or shows the
75  * specified con (if it is scratchpad window).
76  *
77  * When called with con == NULL and the currently focused window is a
78  * scratchpad window, this serves as a shortcut to hide it again (so the user
79  * can press the same key to quickly look something up).
80  *
81  */
82 void scratchpad_show(Con *con) {
83     DLOG("should show scratchpad window %p\n", con);
84     Con *__i3_scratch = workspace_get("__i3_scratch", NULL);
85     Con *floating;
86
87     /* If the current con or any of its parents are in fullscreen mode, we
88      * first need to disable it before showing the scratchpad con. */
89     Con *fs = focused;
90     while (fs && fs->fullscreen_mode == CF_NONE)
91         fs = fs->parent;
92
93     if (fs->type != CT_WORKSPACE) {
94         con_toggle_fullscreen(focused, CF_OUTPUT);
95     }
96
97     /* If this was 'scratchpad show' without criteria, we check if there is a
98      * visible scratchpad window on another workspace. In this case we move it
99      * to the current workspace. */
100     Con *walk_con;
101     Con *focused_ws = con_get_workspace(focused);
102     TAILQ_FOREACH(walk_con, &all_cons, all_cons) {
103         Con *walk_ws = con_get_workspace(walk_con);
104         if (walk_ws &&
105             !con_is_internal(walk_ws) && focused_ws != walk_ws &&
106             (floating = con_inside_floating(walk_con)) &&
107             floating->scratchpad_state != SCRATCHPAD_NONE) {
108             DLOG("Found a visible scratchpad window on another workspace,\n");
109             DLOG("moving it to this workspace: con = %p\n", walk_con);
110             con_move_to_workspace(walk_con, focused_ws, true, false);
111             return;
112         }
113     }
114
115     /* If this was 'scratchpad show' without criteria, we check if the
116      * currently focused window is a scratchpad window and should be hidden
117      * again. */
118     if (!con &&
119         (floating = con_inside_floating(focused)) &&
120         floating->scratchpad_state != SCRATCHPAD_NONE) {
121         DLOG("Focused window is a scratchpad window, hiding it.\n");
122         scratchpad_move(focused);
123         return;
124     }
125
126     /* If this was 'scratchpad show' with criteria, we check if it matches a
127      * currently visible scratchpad window and hide it. */
128     Con *active = con_get_workspace(focused);
129     Con *current = con_get_workspace(con);
130     if (con &&
131         (floating = con_inside_floating(con)) &&
132         floating->scratchpad_state != SCRATCHPAD_NONE &&
133         current != __i3_scratch) {
134         /* If scratchpad window is on the active workspace, then we should hide
135          * it, otherwise we should move it to the active workspace. */
136         if (current == active) {
137             DLOG("Window is a scratchpad window, hiding it.\n");
138             scratchpad_move(con);
139             return;
140         }
141     }
142
143     if (con == NULL) {
144         /* Use the container on __i3_scratch which is highest in the focus
145          * stack. When moving windows to __i3_scratch, they get inserted at the
146          * bottom of the stack. */
147         con = TAILQ_FIRST(&(__i3_scratch->floating_head));
148
149         if (!con) {
150             LOG("You don't have any scratchpad windows yet.\n");
151             LOG("Use 'move scratchpad' to move a window to the scratchpad.\n");
152             return;
153         }
154     }
155
156     /* 1: Move the window from __i3_scratch to the current workspace. */
157     con_move_to_workspace(con, active, true, false);
158
159     /* 2: Adjust the size if this window was not adjusted yet. */
160     if (con->scratchpad_state == SCRATCHPAD_FRESH) {
161         DLOG("Adjusting size of this window.\n");
162         Con *output = con_get_output(con);
163         con->rect.width = output->rect.width * 0.5;
164         con->rect.height = output->rect.height * 0.75;
165         con->rect.x = output->rect.x +
166                       ((output->rect.width / 2.0) - (con->rect.width / 2.0));
167         con->rect.y = output->rect.y +
168                       ((output->rect.height / 2.0) - (con->rect.height / 2.0));
169         con->scratchpad_state = SCRATCHPAD_CHANGED;
170     }
171
172     /* Activate active workspace if window is from another workspace to ensure
173      * proper focus. */
174     if (current != active) {
175         workspace_show(active);
176     }
177
178     con_focus(con_descend_focused(con));
179 }
180
181 /*
182  * Greatest common divisor, implemented only for the least common multiple
183  * below.
184  *
185  */
186 static int _gcd(const int m, const int n) {
187     if (n == 0)
188         return m;
189     return _gcd(n, (m % n));
190 }
191
192 /*
193  * Least common multiple. We use it to determine the (ideally not too large)
194  * resolution for the __i3 pseudo-output on which the scratchpad is on (see
195  * below). We could just multiply the resolutions, but for some pathetic cases
196  * (many outputs), using the LCM will achieve better results.
197  *
198  * Man, when you were learning about these two algorithms for the first time,
199  * did you think you’d ever need them in a real-world software project of
200  * yours? I certainly didn’t until now. :-D
201  *
202  */
203 static int _lcm(const int m, const int n) {
204     const int o = _gcd(m, n);
205     return ((m * n) / o);
206 }
207
208 /*
209  * When starting i3 initially (and after each change to the connected outputs),
210  * this function fixes the resolution of the __i3 pseudo-output. When that
211  * resolution is not set to a function which shares a common divisor with every
212  * active output’s resolution, floating point calculation errors will lead to
213  * the scratchpad window moving when shown repeatedly.
214  *
215  */
216 void scratchpad_fix_resolution(void) {
217     Con *__i3_scratch = workspace_get("__i3_scratch", NULL);
218     Con *__i3_output = con_get_output(__i3_scratch);
219     DLOG("Current resolution: (%d, %d) %d x %d\n",
220          __i3_output->rect.x, __i3_output->rect.y,
221          __i3_output->rect.width, __i3_output->rect.height);
222     Con *output;
223     int new_width = -1,
224         new_height = -1;
225     TAILQ_FOREACH(output, &(croot->nodes_head), nodes) {
226         if (output == __i3_output)
227             continue;
228         DLOG("output %s's resolution: (%d, %d) %d x %d\n",
229              output->name, output->rect.x, output->rect.y,
230              output->rect.width, output->rect.height);
231         if (new_width == -1) {
232             new_width = output->rect.width;
233             new_height = output->rect.height;
234         } else {
235             new_width = _lcm(new_width, output->rect.width);
236             new_height = _lcm(new_height, output->rect.height);
237         }
238     }
239     DLOG("new width = %d, new height = %d\n",
240          new_width, new_height);
241     __i3_output->rect.width = new_width;
242     __i3_output->rect.height = new_height;
243 }