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