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