i3
scratchpad.c
Go to the documentation of this file.
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 */
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 if (fs && fs->type != CT_WORKSPACE) {
109 }
110
111 /* If this was 'scratchpad show' without criteria, we check if there is a
112 * unfocused scratchpad on the current workspace and focus it */
113 Con *walk_con;
114 Con *focused_ws = con_get_workspace(focused);
115 TAILQ_FOREACH (walk_con, &(focused_ws->floating_head), floating_windows) {
116 if (!con && (floating = con_inside_floating(walk_con)) &&
117 floating->scratchpad_state != SCRATCHPAD_NONE &&
118 floating != con_inside_floating(focused)) {
119 DLOG("Found an unfocused scratchpad window on this workspace\n");
120 DLOG("Focusing it: %p\n", walk_con);
121 /* use con_descend_tiling_focused to get the last focused
122 * window inside this scratch container in order to
123 * keep the focus the same within this container */
125 return true;
126 }
127 }
128
129 /* If this was 'scratchpad show' without criteria, we check if there is a
130 * visible scratchpad window on another workspace. In this case we move it
131 * to the current workspace. */
132 focused_ws = con_get_workspace(focused);
133 TAILQ_FOREACH (walk_con, &all_cons, all_cons) {
134 Con *walk_ws = con_get_workspace(walk_con);
135 if (!con && walk_ws &&
136 !con_is_internal(walk_ws) && focused_ws != walk_ws &&
137 (floating = con_inside_floating(walk_con)) &&
138 floating->scratchpad_state != SCRATCHPAD_NONE) {
139 DLOG("Found a visible scratchpad window on another workspace,\n");
140 DLOG("moving it to this workspace: con = %p\n", walk_con);
141 con_move_to_workspace(floating, focused_ws, true, false, false);
143 return true;
144 }
145 }
146
147 /* If this was 'scratchpad show' with criteria, we check if the window
148 * is actually in the scratchpad */
149 if (con && con->parent->scratchpad_state == SCRATCHPAD_NONE) {
150 DLOG("Window is not in the scratchpad, doing nothing.\n");
151 return false;
152 }
153
154 /* If this was 'scratchpad show' with criteria, we check if it matches a
155 * currently visible scratchpad window and hide it. */
156 Con *active = con_get_workspace(focused);
157 Con *current = con_get_workspace(con);
158 if (con &&
159 (floating = con_inside_floating(con)) &&
160 floating->scratchpad_state != SCRATCHPAD_NONE &&
161 current != __i3_scratch) {
162 /* If scratchpad window is on the active workspace, then we should hide
163 * it, otherwise we should move it to the active workspace. */
164 if (current == active) {
165 DLOG("Window is a scratchpad window, hiding it.\n");
166 scratchpad_move(con);
167 return true;
168 }
169 }
170
171 if (con == NULL) {
172 /* Use the container on __i3_scratch which is highest in the focus
173 * stack. When moving windows to __i3_scratch, they get inserted at the
174 * bottom of the stack. */
175 con = TAILQ_FIRST(&(__i3_scratch->floating_head));
176
177 if (!con) {
178 LOG("You don't have any scratchpad windows yet.\n");
179 LOG("Use 'move scratchpad' to move a window to the scratchpad.\n");
180 return false;
181 }
182 } else {
183 /* We used a criterion, so we need to do what follows (moving,
184 * resizing) on the floating parent. */
185 con = con_inside_floating(con);
186 }
187
188 /* 1: Move the window from __i3_scratch to the current workspace. */
189 con_move_to_workspace(con, active, true, false, false);
190
191 /* 2: Adjust the size if this window was not adjusted yet. */
192 if (con->scratchpad_state == SCRATCHPAD_FRESH) {
193 DLOG("Adjusting size of this window.\n");
194 Con *output = con_get_output(con);
195 con->rect.width = output->rect.width * 0.5;
196 con->rect.height = output->rect.height * 0.75;
197 floating_check_size(con, false);
198 floating_center(con, con_get_workspace(con)->rect);
199 }
200
201 /* Activate active workspace if window is from another workspace to ensure
202 * proper focus. */
203 if (current != active) {
204 workspace_show(active);
205 }
206
208
209 return true;
210}
211
212/*
213 * Greatest common divisor, implemented only for the least common multiple
214 * below.
215 *
216 */
217static int _gcd(const int m, const int n) {
218 if (n == 0)
219 return m;
220 return _gcd(n, (m % n));
221}
222
223/*
224 * Least common multiple. We use it to determine the (ideally not too large)
225 * resolution for the __i3 pseudo-output on which the scratchpad is on (see
226 * below). We could just multiply the resolutions, but for some pathetic cases
227 * (many outputs), using the LCM will achieve better results.
228 *
229 * Man, when you were learning about these two algorithms for the first time,
230 * did you think you’d ever need them in a real-world software project of
231 * yours? I certainly didn’t until now. :-D
232 *
233 */
234static int _lcm(const int m, const int n) {
235 const int o = _gcd(m, n);
236 return ((m * n) / o);
237}
238
239/*
240 * When starting i3 initially (and after each change to the connected outputs),
241 * this function fixes the resolution of the __i3 pseudo-output. When that
242 * resolution is not set to a function which shares a common divisor with every
243 * active output’s resolution, floating point calculation errors will lead to
244 * the scratchpad window moving when shown repeatedly.
245 *
246 */
248 Con *__i3_scratch = workspace_get("__i3_scratch");
249 Con *__i3_output = con_get_output(__i3_scratch);
250 DLOG("Current resolution: (%d, %d) %d x %d\n",
251 __i3_output->rect.x, __i3_output->rect.y,
252 __i3_output->rect.width, __i3_output->rect.height);
253 Con *output;
254 int new_width = -1,
255 new_height = -1;
256 TAILQ_FOREACH (output, &(croot->nodes_head), nodes) {
257 if (output == __i3_output)
258 continue;
259 DLOG("output %s's resolution: (%d, %d) %d x %d\n",
260 output->name, output->rect.x, output->rect.y,
261 output->rect.width, output->rect.height);
262 if (new_width == -1) {
263 new_width = output->rect.width;
264 new_height = output->rect.height;
265 } else {
266 new_width = _lcm(new_width, output->rect.width);
267 new_height = _lcm(new_height, output->rect.height);
268 }
269 }
270
271 Rect old_rect = __i3_output->rect;
272
273 DLOG("new width = %d, new height = %d\n",
274 new_width, new_height);
275 __i3_output->rect.width = new_width;
276 __i3_output->rect.height = new_height;
277
278 Rect new_rect = __i3_output->rect;
279
280 if (rect_equals(new_rect, old_rect)) {
281 DLOG("Scratchpad size unchanged.\n");
282 return;
283 }
284
285 DLOG("Fixing coordinates of scratchpad windows\n");
286 Con *con;
287 TAILQ_FOREACH (con, &(__i3_scratch->floating_head), floating_windows) {
288 floating_fix_coordinates(con, &old_rect, &new_rect);
289 }
290}
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:1466
Con * con_get_workspace(Con *con)
Gets the workspace container this node is on.
Definition con.c:477
Con * con_descend_tiling_focused(Con *con)
Returns the focused con inside this client, descending the tree as far as possible.
Definition con.c:1605
void con_disable_fullscreen(Con *con)
Disables fullscreen mode for the given container, if necessary.
Definition con.c:1195
Con * con_inside_floating(Con *con)
Checks if the given container is either floating or inside some floating container.
Definition con.c:620
bool con_is_internal(Con *con)
Returns true if the container is internal, such as __i3_scratch.
Definition con.c:588
void con_toggle_fullscreen(Con *con, int fullscreen_mode)
Toggles fullscreen mode for the given container.
Definition con.c:1095
void con_activate(Con *con)
Sets input focus to the given container and raises it to the top.
Definition con.c:287
Con * con_get_output(Con *con)
Gets the output container (first container with CT_OUTPUT in hierarchy) this node is on.
Definition con.c:463
Con * con_descend_focused(Con *con)
Returns the focused con inside this client, descending the tree as far as possible.
Definition con.c:1590
void floating_check_size(Con *floating_con, bool prefer_height)
Called when a floating window is created or resized.
Definition floating.c:75
void floating_center(Con *con, Rect rect)
Centers a floating con above the specified rect.
Definition floating.c:527
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:799
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:232
void scratchpad_fix_resolution(void)
When starting i3 initially (and after each change to the connected outputs), this function fixes the ...
Definition scratchpad.c:247
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:234
static int _gcd(const int m, const int n)
Definition scratchpad.c:217
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:428
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:634
@ CF_NONE
Definition data.h:633
#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:189
uint32_t height
Definition data.h:193
uint32_t x
Definition data.h:190
uint32_t y
Definition data.h:191
uint32_t width
Definition data.h:192
A 'Con' represents everything from the X11 root window down to a single X11 window.
Definition data.h:647
struct Con * parent
Definition data.h:682
enum Con::@20 scratchpad_state
enum Con::@18 type
struct Rect rect
Definition data.h:686
char * name
Definition data.h:696
fullscreen_mode_t fullscreen_mode
Definition data.h:738