]> 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-2013 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     /* If the current con is in fullscreen mode, we need to disable that,
43      *  as a scratchpad window should never be in fullscreen mode */
44     if (focused && focused->type != CT_WORKSPACE && focused->fullscreen_mode != CF_NONE) {
45         con_toggle_fullscreen(focused, CF_OUTPUT);
46     }
47
48     /* 1: Ensure the window or any parent is floating. From now on, we deal
49      * with the CT_FLOATING_CON. We use automatic == false because the user
50      * made the choice that this window should be a scratchpad (and floating).
51      */
52     Con *maybe_floating_con = con_inside_floating(con);
53     if (maybe_floating_con == NULL) {
54         floating_enable(con, false);
55         con = con->parent;
56     } else {
57         con = maybe_floating_con;
58     }
59
60     /* 2: Send the window to the __i3_scratch workspace, mainting its
61      * coordinates and not warping the pointer. */
62     con_move_to_workspace(con, __i3_scratch, true, true);
63
64     /* 3: If this is the first time this window is used as a scratchpad, we set
65      * the scratchpad_state to SCRATCHPAD_FRESH. The window will then be
66      * adjusted in size according to what the user specifies. */
67     if (con->scratchpad_state == SCRATCHPAD_NONE) {
68         DLOG("This window was never used as a scratchpad before.\n");
69         con->scratchpad_state = SCRATCHPAD_FRESH;
70     }
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 this was 'scratchpad show' without criteria, we check if the
88      * currently focused window is a scratchpad window and should be hidden
89      * again. */
90     if (!con &&
91         (floating = con_inside_floating(focused)) &&
92         floating->scratchpad_state != SCRATCHPAD_NONE) {
93         DLOG("Focused window is a scratchpad window, hiding it.\n");
94         scratchpad_move(focused);
95         return;
96     }
97
98     /* If the current con or any of its parents are in fullscreen mode, we
99      * first need to disable it before showing the scratchpad con. */
100     Con *fs = focused;
101     while (fs && fs->fullscreen_mode == CF_NONE)
102         fs = fs->parent;
103
104     if (fs && fs->type != CT_WORKSPACE) {
105         con_toggle_fullscreen(fs, CF_OUTPUT);
106     }
107
108     /* If this was 'scratchpad show' without criteria, we check if there is a
109      * unfocused scratchpad on the current workspace and focus it */
110     Con *walk_con;
111     Con *focused_ws = con_get_workspace(focused);
112     TAILQ_FOREACH(walk_con, &(focused_ws->floating_head), floating_windows) {
113         if (!con && (floating = con_inside_floating(walk_con)) &&
114             floating->scratchpad_state != SCRATCHPAD_NONE &&
115             floating != con_inside_floating(focused)) {
116                 DLOG("Found an unfocused scratchpad window on this workspace\n");
117                 DLOG("Focusing it: %p\n", walk_con);
118                 /* use con_descend_tiling_focused to get the last focused
119                  * window inside this scratch container in order to
120                  * keep the focus the same within this container */
121                 con_focus(con_descend_tiling_focused(walk_con));
122                 return;
123             }
124     }
125
126     /* If this was 'scratchpad show' without criteria, we check if there is a
127      * visible scratchpad window on another workspace. In this case we move it
128      * to the current workspace. */
129     focused_ws = con_get_workspace(focused);
130     TAILQ_FOREACH(walk_con, &all_cons, all_cons) {
131         Con *walk_ws = con_get_workspace(walk_con);
132         if (!con && walk_ws &&
133             !con_is_internal(walk_ws) && focused_ws != walk_ws &&
134             (floating = con_inside_floating(walk_con)) &&
135             floating->scratchpad_state != SCRATCHPAD_NONE) {
136             DLOG("Found a visible scratchpad window on another workspace,\n");
137             DLOG("moving it to this workspace: con = %p\n", walk_con);
138             con_move_to_workspace(walk_con, focused_ws, true, false);
139             return;
140         }
141     }
142
143     /* If this was 'scratchpad show' with criteria, we check if the window
144      * is actually in the scratchpad */
145     if (con && con->parent->scratchpad_state == SCRATCHPAD_NONE) {
146         DLOG("Window is not in the scratchpad, doing nothing.\n");
147         return;
148     }
149
150     /* If this was 'scratchpad show' with criteria, we check if it matches a
151      * currently visible scratchpad window and hide it. */
152     Con *active = con_get_workspace(focused);
153     Con *current = con_get_workspace(con);
154     if (con &&
155         (floating = con_inside_floating(con)) &&
156         floating->scratchpad_state != SCRATCHPAD_NONE &&
157         current != __i3_scratch) {
158         /* If scratchpad window is on the active workspace, then we should hide
159          * it, otherwise we should move it to the active workspace. */
160         if (current == active) {
161             DLOG("Window is a scratchpad window, hiding it.\n");
162             scratchpad_move(con);
163             return;
164         }
165     }
166
167     if (con == NULL) {
168         /* Use the container on __i3_scratch which is highest in the focus
169          * stack. When moving windows to __i3_scratch, they get inserted at the
170          * bottom of the stack. */
171         con = TAILQ_FIRST(&(__i3_scratch->floating_head));
172
173         if (!con) {
174             LOG("You don't have any scratchpad windows yet.\n");
175             LOG("Use 'move scratchpad' to move a window to the scratchpad.\n");
176             return;
177         }
178     }
179
180     /* 1: Move the window from __i3_scratch to the current workspace. */
181     con_move_to_workspace(con, active, true, false);
182
183     /* 2: Adjust the size if this window was not adjusted yet. */
184     if (con->scratchpad_state == SCRATCHPAD_FRESH) {
185         DLOG("Adjusting size of this window.\n");
186         Con *output = con_get_output(con);
187         con->rect.width = output->rect.width * 0.5;
188         con->rect.height = output->rect.height * 0.75;
189         floating_check_size(con);
190         con->rect.x = output->rect.x +
191                       ((output->rect.width / 2.0) - (con->rect.width / 2.0));
192         con->rect.y = output->rect.y +
193                       ((output->rect.height / 2.0) - (con->rect.height / 2.0));
194     }
195
196     /* Activate active workspace if window is from another workspace to ensure
197      * proper focus. */
198     if (current != active) {
199         workspace_show(active);
200     }
201
202     con_focus(con_descend_focused(con));
203 }
204
205 /*
206  * Greatest common divisor, implemented only for the least common multiple
207  * below.
208  *
209  */
210 static int _gcd(const int m, const int n) {
211     if (n == 0)
212         return m;
213     return _gcd(n, (m % n));
214 }
215
216 /*
217  * Least common multiple. We use it to determine the (ideally not too large)
218  * resolution for the __i3 pseudo-output on which the scratchpad is on (see
219  * below). We could just multiply the resolutions, but for some pathetic cases
220  * (many outputs), using the LCM will achieve better results.
221  *
222  * Man, when you were learning about these two algorithms for the first time,
223  * did you think you’d ever need them in a real-world software project of
224  * yours? I certainly didn’t until now. :-D
225  *
226  */
227 static int _lcm(const int m, const int n) {
228     const int o = _gcd(m, n);
229     return ((m * n) / o);
230 }
231
232 /*
233  * When starting i3 initially (and after each change to the connected outputs),
234  * this function fixes the resolution of the __i3 pseudo-output. When that
235  * resolution is not set to a function which shares a common divisor with every
236  * active output’s resolution, floating point calculation errors will lead to
237  * the scratchpad window moving when shown repeatedly.
238  *
239  */
240 void scratchpad_fix_resolution(void) {
241     Con *__i3_scratch = workspace_get("__i3_scratch", NULL);
242     Con *__i3_output = con_get_output(__i3_scratch);
243     DLOG("Current resolution: (%d, %d) %d x %d\n",
244          __i3_output->rect.x, __i3_output->rect.y,
245          __i3_output->rect.width, __i3_output->rect.height);
246     Con *output;
247     int new_width = -1,
248         new_height = -1;
249     TAILQ_FOREACH(output, &(croot->nodes_head), nodes) {
250         if (output == __i3_output)
251             continue;
252         DLOG("output %s's resolution: (%d, %d) %d x %d\n",
253              output->name, output->rect.x, output->rect.y,
254              output->rect.width, output->rect.height);
255         if (new_width == -1) {
256             new_width = output->rect.width;
257             new_height = output->rect.height;
258         } else {
259             new_width = _lcm(new_width, output->rect.width);
260             new_height = _lcm(new_height, output->rect.height);
261         }
262     }
263
264     Rect old_rect = __i3_output->rect;
265
266     DLOG("new width = %d, new height = %d\n",
267          new_width, new_height);
268     __i3_output->rect.width = new_width;
269     __i3_output->rect.height = new_height;
270
271     Rect new_rect = __i3_output->rect;
272
273     if (memcmp(&old_rect, &new_rect, sizeof(Rect)) == 0) {
274         DLOG("Scratchpad size unchanged.\n");
275         return;
276     }
277
278     DLOG("Fixing coordinates of scratchpad windows\n");
279     Con *con;
280     TAILQ_FOREACH(con, &(__i3_scratch->floating_head), floating_windows) {
281         floating_fix_coordinates(con, &old_rect, &new_rect);
282     }
283 }