]> git.sur5r.net Git - i3/i3/blob - src/scratchpad.c
Merge pull request #3179 from orestisf1993/issue-2733
[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 bool 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 true;
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 true;
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 true;
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 false;
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 true;
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 false;
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     return true;
211 }
212
213 /*
214  * Greatest common divisor, implemented only for the least common multiple
215  * below.
216  *
217  */
218 static int _gcd(const int m, const int n) {
219     if (n == 0)
220         return m;
221     return _gcd(n, (m % n));
222 }
223
224 /*
225  * Least common multiple. We use it to determine the (ideally not too large)
226  * resolution for the __i3 pseudo-output on which the scratchpad is on (see
227  * below). We could just multiply the resolutions, but for some pathetic cases
228  * (many outputs), using the LCM will achieve better results.
229  *
230  * Man, when you were learning about these two algorithms for the first time,
231  * did you think you’d ever need them in a real-world software project of
232  * yours? I certainly didn’t until now. :-D
233  *
234  */
235 static int _lcm(const int m, const int n) {
236     const int o = _gcd(m, n);
237     return ((m * n) / o);
238 }
239
240 /*
241  * When starting i3 initially (and after each change to the connected outputs),
242  * this function fixes the resolution of the __i3 pseudo-output. When that
243  * resolution is not set to a function which shares a common divisor with every
244  * active output’s resolution, floating point calculation errors will lead to
245  * the scratchpad window moving when shown repeatedly.
246  *
247  */
248 void scratchpad_fix_resolution(void) {
249     Con *__i3_scratch = workspace_get("__i3_scratch", NULL);
250     Con *__i3_output = con_get_output(__i3_scratch);
251     DLOG("Current resolution: (%d, %d) %d x %d\n",
252          __i3_output->rect.x, __i3_output->rect.y,
253          __i3_output->rect.width, __i3_output->rect.height);
254     Con *output;
255     int new_width = -1,
256         new_height = -1;
257     TAILQ_FOREACH(output, &(croot->nodes_head), nodes) {
258         if (output == __i3_output)
259             continue;
260         DLOG("output %s's resolution: (%d, %d) %d x %d\n",
261              output->name, output->rect.x, output->rect.y,
262              output->rect.width, output->rect.height);
263         if (new_width == -1) {
264             new_width = output->rect.width;
265             new_height = output->rect.height;
266         } else {
267             new_width = _lcm(new_width, output->rect.width);
268             new_height = _lcm(new_height, output->rect.height);
269         }
270     }
271
272     Rect old_rect = __i3_output->rect;
273
274     DLOG("new width = %d, new height = %d\n",
275          new_width, new_height);
276     __i3_output->rect.width = new_width;
277     __i3_output->rect.height = new_height;
278
279     Rect new_rect = __i3_output->rect;
280
281     if (memcmp(&old_rect, &new_rect, sizeof(Rect)) == 0) {
282         DLOG("Scratchpad size unchanged.\n");
283         return;
284     }
285
286     DLOG("Fixing coordinates of scratchpad windows\n");
287     Con *con;
288     TAILQ_FOREACH(con, &(__i3_scratch->floating_head), floating_windows) {
289         floating_fix_coordinates(con, &old_rect, &new_rect);
290     }
291 }