i3
scratchpad.c
Go to the documentation of this file.
1/*
2 * vim:ts=4:sw=4:expandtab
3 *
4 * i3 - an improved 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 */
19void 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");
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 */
44
45 /* 1: Ensure the window or any parent is floating. From now on, we deal
46 * with the CT_FLOATING_CON. We use automatic == false because the user
47 * made the choice that this window should be a scratchpad (and floating).
48 */
49 Con *maybe_floating_con = con_inside_floating(con);
50 if (maybe_floating_con == NULL) {
51 floating_enable(con, false);
52 con = con->parent;
53 } else {
54 con = maybe_floating_con;
55 }
56
57 /* 2: Send the window to the __i3_scratch workspace, mainting its
58 * coordinates and not warping the pointer. */
59 con_move_to_workspace(con, __i3_scratch, true, true, false);
60
61 /* 3: If this is the first time this window is used as a scratchpad, we set
62 * the scratchpad_state to SCRATCHPAD_FRESH. The window will then be
63 * adjusted in size according to what the user specifies. */
64 if (con->scratchpad_state == SCRATCHPAD_NONE) {
65 DLOG("This window was never used as a scratchpad before.\n");
66 if (con == maybe_floating_con) {
67 DLOG("It was in floating mode before, set scratchpad state to changed.\n");
68 con->scratchpad_state = SCRATCHPAD_CHANGED;
69 } else {
70 DLOG("It was in tiling mode before, set scratchpad state to fresh.\n");
71 con->scratchpad_state = SCRATCHPAD_FRESH;
72 }
73 }
74}
75
76/*
77 * Either shows the top-most scratchpad window (con == NULL) or shows the
78 * specified con (if it is scratchpad window).
79 *
80 * When called with con == NULL and the currently focused window is a
81 * scratchpad window, this serves as a shortcut to hide it again (so the user
82 * can press the same key to quickly look something up).
83 *
84 */
85bool scratchpad_show(Con *con) {
86 DLOG("should show scratchpad window %p\n", con);
87 Con *__i3_scratch = workspace_get("__i3_scratch");
88 Con *floating;
89
90 /* If this was 'scratchpad show' without criteria, we check if the
91 * currently focused window is a scratchpad window and should be hidden
92 * again. */
93 if (!con &&
94 (floating = con_inside_floating(focused)) &&
95 floating->scratchpad_state != SCRATCHPAD_NONE) {
96 DLOG("Focused window is a scratchpad window, hiding it.\n");
98 return true;
99 }
100
101 /* If the current con or any of its parents are in fullscreen mode, we
102 * first need to disable it before showing the scratchpad con. */
103 Con *fs = focused;
104 while (fs && fs->fullscreen_mode == CF_NONE) {
105 fs = fs->parent;
106 }
107
108 if (fs && fs->type != CT_WORKSPACE) {
110 }
111
112 /* If this was 'scratchpad show' without criteria, we check if there is a
113 * unfocused scratchpad on the current workspace and focus it */
114 Con *walk_con;
115 Con *focused_ws = con_get_workspace(focused);
116 TAILQ_FOREACH (walk_con, &(focused_ws->floating_head), floating_windows) {
117 if (!con && (floating = con_inside_floating(walk_con)) &&
118 floating->scratchpad_state != SCRATCHPAD_NONE &&
119 floating != con_inside_floating(focused)) {
120 DLOG("Found an unfocused scratchpad window on this workspace\n");
121 DLOG("Focusing it: %p\n", walk_con);
122 /* use con_descend_tiling_focused to get the last focused
123 * window inside this scratch container in order to
124 * keep the focus the same within this container */
126 return true;
127 }
128 }
129
130 /* If this was 'scratchpad show' without criteria, we check if there is a
131 * visible scratchpad window on another workspace. In this case we move it
132 * to the current workspace. */
133 focused_ws = con_get_workspace(focused);
134 TAILQ_FOREACH (walk_con, &all_cons, all_cons) {
135 Con *walk_ws = con_get_workspace(walk_con);
136 if (!con && walk_ws &&
137 !con_is_internal(walk_ws) && focused_ws != walk_ws &&
138 (floating = con_inside_floating(walk_con)) &&
139 floating->scratchpad_state != SCRATCHPAD_NONE) {
140 DLOG("Found a visible scratchpad window on another workspace,\n");
141 DLOG("moving it to this workspace: con = %p\n", walk_con);
142 con_move_to_workspace(floating, 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, false);
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
209
210 return true;
211}
212
213/*
214 * Greatest common divisor, implemented only for the least common multiple
215 * below.
216 *
217 */
218static int _gcd(const int m, const int n) {
219 if (n == 0) {
220 return m;
221 }
222 return _gcd(n, (m % n));
223}
224
225/*
226 * Least common multiple. We use it to determine the (ideally not too large)
227 * resolution for the __i3 pseudo-output on which the scratchpad is on (see
228 * below). We could just multiply the resolutions, but for some pathetic cases
229 * (many outputs), using the LCM will achieve better results.
230 *
231 * Man, when you were learning about these two algorithms for the first time,
232 * did you think you’d ever need them in a real-world software project of
233 * yours? I certainly didn’t until now. :-D
234 *
235 */
236static int _lcm(const int m, const int n) {
237 const int o = _gcd(m, n);
238 return ((m * n) / o);
239}
240
241/*
242 * When starting i3 initially (and after each change to the connected outputs),
243 * this function fixes the resolution of the __i3 pseudo-output. When that
244 * resolution is not set to a function which shares a common divisor with every
245 * active output’s resolution, floating point calculation errors will lead to
246 * the scratchpad window moving when shown repeatedly.
247 *
248 */
250 Con *__i3_scratch = workspace_get("__i3_scratch");
251 Con *__i3_output = con_get_output(__i3_scratch);
252 DLOG("Current resolution: (%d, %d) %d x %d\n",
253 __i3_output->rect.x, __i3_output->rect.y,
254 __i3_output->rect.width, __i3_output->rect.height);
255 Con *output;
256 int new_width = -1,
257 new_height = -1;
258 TAILQ_FOREACH (output, &(croot->nodes_head), nodes) {
259 if (output == __i3_output) {
260 continue;
261 }
262 DLOG("output %s's resolution: (%d, %d) %d x %d\n",
263 output->name, output->rect.x, output->rect.y,
264 output->rect.width, output->rect.height);
265 if (new_width == -1) {
266 new_width = output->rect.width;
267 new_height = output->rect.height;
268 } else {
269 new_width = _lcm(new_width, output->rect.width);
270 new_height = _lcm(new_height, output->rect.height);
271 }
272 }
273
274 Rect old_rect = __i3_output->rect;
275
276 DLOG("new width = %d, new height = %d\n",
277 new_width, new_height);
278 __i3_output->rect.width = new_width;
279 __i3_output->rect.height = new_height;
280
281 Rect new_rect = __i3_output->rect;
282
283 if (rect_equals(new_rect, old_rect)) {
284 DLOG("Scratchpad size unchanged.\n");
285 return;
286 }
287
288 DLOG("Fixing coordinates of scratchpad windows\n");
289 Con *con;
290 TAILQ_FOREACH (con, &(__i3_scratch->floating_head), floating_windows) {
291 floating_fix_coordinates(con, &old_rect, &new_rect);
292 }
293}
void con_move_to_workspace(Con *con, Con *workspace, bool fix_coordinates, bool dont_warp, bool ignore_focus)
Moves the given container to the currently focused container on the given workspace.
Definition con.c:1574
Con * con_get_workspace(Con *con)
Gets the workspace container this node is on.
Definition con.c:547
Con * con_descend_tiling_focused(Con *con)
Returns the focused con inside this client, descending the tree as far as possible.
Definition con.c:1714
void con_disable_fullscreen(Con *con)
Disables fullscreen mode for the given container, if necessary.
Definition con.c:1299
Con * con_inside_floating(Con *con)
Checks if the given container is either floating or inside some floating container.
Definition con.c:696
bool con_is_internal(Con *con)
Returns true if the container is internal, such as __i3_scratch.
Definition con.c:662
void con_toggle_fullscreen(Con *con, int fullscreen_mode)
Toggles fullscreen mode for the given container.
Definition con.c:1192
void con_activate(Con *con)
Sets input focus to the given container and raises it to the top.
Definition con.c:292
Con * con_get_output(Con *con)
Gets the output container (first container with CT_OUTPUT in hierarchy) this node is on.
Definition con.c:532
Con * con_descend_focused(Con *con)
Returns the focused con inside this client, descending the tree as far as possible.
Definition con.c:1698
void floating_check_size(Con *floating_con, bool prefer_height)
Called when a floating window is created or resized.
Definition floating.c:76
void floating_center(Con *con, Rect rect)
Centers a floating con above the specified rect.
Definition floating.c:529
void floating_fix_coordinates(Con *con, Rect *old_rect, Rect *new_rect)
Fixes the coordinates of the floating window whenever the window gets reassigned to a different outpu...
Definition floating.c:816
bool floating_enable(Con *con, bool automatic)
Enables floating mode for the given container by detaching it from its parent, creating a new contain...
Definition floating.c:233
void scratchpad_fix_resolution(void)
When starting i3 initially (and after each change to the connected outputs), this function fixes the ...
Definition scratchpad.c:249
bool scratchpad_show(Con *con)
Either shows the top-most scratchpad window (con == NULL) or shows the specified con (if it is scratc...
Definition scratchpad.c:85
static int _lcm(const int m, const int n)
Definition scratchpad.c:236
static int _gcd(const int m, const int n)
Definition scratchpad.c:218
void scratchpad_move(Con *con)
Moves the specified window to the __i3_scratch workspace, making it floating and setting the appropri...
Definition scratchpad.c:19
struct Con * focused
Definition tree.c:13
struct Con * croot
Definition tree.c:12
struct all_cons_head all_cons
Definition tree.c:15
bool rect_equals(Rect a, Rect b)
Definition util.c:59
void workspace_show(Con *workspace)
Switches to the given workspace.
Definition workspace.c:438
Con * workspace_get(const char *num)
Returns a pointer to the workspace with the given number (starting at 0), creating the workspace if n...
Definition workspace.c:131
@ CF_OUTPUT
Definition data.h:630
@ CF_NONE
Definition data.h:629
#define DLOG(fmt,...)
Definition libi3.h:105
#define LOG(fmt,...)
Definition libi3.h:95
#define TAILQ_FOREACH(var, head, field)
Definition queue.h:347
#define TAILQ_FIRST(head)
Definition queue.h:336
#define TAILQ_NEXT(elm, field)
Definition queue.h:338
Stores a rectangle, for example the size of a window, the child window etc.
Definition data.h:185
uint32_t height
Definition data.h:189
uint32_t x
Definition data.h:186
uint32_t y
Definition data.h:187
uint32_t width
Definition data.h:188
A 'Con' represents everything from the X11 root window down to a single X11 window.
Definition data.h:643
struct Con * parent
Definition data.h:678
enum Con::@20 scratchpad_state
enum Con::@18 type
struct Rect rect
Definition data.h:682
char * name
Definition data.h:692
fullscreen_mode_t fullscreen_mode
Definition data.h:734