2 #define I3__FILE__ "scratchpad.c"
4 * vim:ts=4:sw=4:expandtab
6 * i3 - an improved dynamic tiling window manager
7 * © 2009-2013 Michael Stapelberg and contributors (see also: LICENSE)
9 * scratchpad.c: Moving windows to the scratchpad and making them visible again.
15 * Moves the specified window to the __i3_scratch workspace, making it floating
16 * and setting the appropriate scratchpad_state.
18 * Gets called upon the command 'move scratchpad'.
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);
26 current = TAILQ_FIRST(&(con->focus_head));
28 Con *next = TAILQ_NEXT(current, focused);
29 scratchpad_move(current);
34 DLOG("should move con %p to __i3_scratch\n", con);
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");
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).
46 Con *maybe_floating_con = con_inside_floating(con);
47 if (maybe_floating_con == NULL) {
48 floating_enable(con, false);
51 con = maybe_floating_con;
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);
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;
68 * Either shows the top-most scratchpad window (con == NULL) or shows the
69 * specified con (if it is scratchpad window).
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).
76 void scratchpad_show(Con *con) {
77 DLOG("should show scratchpad window %p\n", con);
78 Con *__i3_scratch = workspace_get("__i3_scratch", NULL);
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. */
84 while (fs && fs->fullscreen_mode == CF_NONE)
87 if (fs->type != CT_WORKSPACE) {
88 con_toggle_fullscreen(focused, CF_OUTPUT);
91 /* If this was 'scratchpad show' without criteria, we check if there is a
92 * visible scratchpad window on another workspace. In this case we move it
93 * to the current workspace. */
95 Con *focused_ws = con_get_workspace(focused);
96 TAILQ_FOREACH(walk_con, &all_cons, all_cons) {
97 Con *walk_ws = con_get_workspace(walk_con);
99 !con_is_internal(walk_ws) && focused_ws != walk_ws &&
100 (floating = con_inside_floating(walk_con)) &&
101 floating->scratchpad_state != SCRATCHPAD_NONE) {
102 DLOG("Found a visible scratchpad window on another workspace,\n");
103 DLOG("moving it to this workspace: con = %p\n", walk_con);
104 con_move_to_workspace(walk_con, focused_ws, true, false);
109 /* If this was 'scratchpad show' without criteria, we check if the
110 * currently focused window is a scratchpad window and should be hidden
113 (floating = con_inside_floating(focused)) &&
114 floating->scratchpad_state != SCRATCHPAD_NONE) {
115 DLOG("Focused window is a scratchpad window, hiding it.\n");
116 scratchpad_move(focused);
120 /* If this was 'scratchpad show' with criteria, we check if it matches a
121 * currently visible scratchpad window and hide it. */
122 Con *active = con_get_workspace(focused);
123 Con *current = con_get_workspace(con);
125 (floating = con_inside_floating(con)) &&
126 floating->scratchpad_state != SCRATCHPAD_NONE &&
127 current != __i3_scratch) {
128 /* If scratchpad window is on the active workspace, then we should hide
129 * it, otherwise we should move it to the active workspace. */
130 if (current == active) {
131 DLOG("Window is a scratchpad window, hiding it.\n");
132 scratchpad_move(con);
138 /* Use the container on __i3_scratch which is highest in the focus
139 * stack. When moving windows to __i3_scratch, they get inserted at the
140 * bottom of the stack. */
141 con = TAILQ_FIRST(&(__i3_scratch->floating_head));
144 LOG("You don't have any scratchpad windows yet.\n");
145 LOG("Use 'move scratchpad' to move a window to the scratchpad.\n");
150 /* 1: Move the window from __i3_scratch to the current workspace. */
151 con_move_to_workspace(con, active, true, false);
153 /* 2: Adjust the size if this window was not adjusted yet. */
154 if (con->scratchpad_state == SCRATCHPAD_FRESH) {
155 DLOG("Adjusting size of this window.\n");
156 Con *output = con_get_output(con);
157 con->rect.width = output->rect.width * 0.5;
158 con->rect.height = output->rect.height * 0.75;
159 con->rect.x = output->rect.x +
160 ((output->rect.width / 2.0) - (con->rect.width / 2.0));
161 con->rect.y = output->rect.y +
162 ((output->rect.height / 2.0) - (con->rect.height / 2.0));
163 con->scratchpad_state = SCRATCHPAD_CHANGED;
166 /* Activate active workspace if window is from another workspace to ensure
168 if (current != active) {
169 workspace_show(active);
172 con_focus(con_descend_focused(con));
176 * Greatest common divisor, implemented only for the least common multiple
180 static int _gcd(const int m, const int n) {
183 return _gcd(n, (m % n));
187 * Least common multiple. We use it to determine the (ideally not too large)
188 * resolution for the __i3 pseudo-output on which the scratchpad is on (see
189 * below). We could just multiply the resolutions, but for some pathetic cases
190 * (many outputs), using the LCM will achieve better results.
192 * Man, when you were learning about these two algorithms for the first time,
193 * did you think you’d ever need them in a real-world software project of
194 * yours? I certainly didn’t until now. :-D
197 static int _lcm(const int m, const int n) {
198 const int o = _gcd(m, n);
199 return ((m * n) / o);
203 * When starting i3 initially (and after each change to the connected outputs),
204 * this function fixes the resolution of the __i3 pseudo-output. When that
205 * resolution is not set to a function which shares a common divisor with every
206 * active output’s resolution, floating point calculation errors will lead to
207 * the scratchpad window moving when shown repeatedly.
210 void scratchpad_fix_resolution(void) {
211 Con *__i3_scratch = workspace_get("__i3_scratch", NULL);
212 Con *__i3_output = con_get_output(__i3_scratch);
213 DLOG("Current resolution: (%d, %d) %d x %d\n",
214 __i3_output->rect.x, __i3_output->rect.y,
215 __i3_output->rect.width, __i3_output->rect.height);
219 TAILQ_FOREACH(output, &(croot->nodes_head), nodes) {
220 if (output == __i3_output)
222 DLOG("output %s's resolution: (%d, %d) %d x %d\n",
223 output->name, output->rect.x, output->rect.y,
224 output->rect.width, output->rect.height);
225 if (new_width == -1) {
226 new_width = output->rect.width;
227 new_height = output->rect.height;
229 new_width = _lcm(new_width, output->rect.width);
230 new_height = _lcm(new_height, output->rect.height);
234 Rect old_rect = __i3_output->rect;
236 DLOG("new width = %d, new height = %d\n",
237 new_width, new_height);
238 __i3_output->rect.width = new_width;
239 __i3_output->rect.height = new_height;
241 Rect new_rect = __i3_output->rect;
243 if (memcmp(&old_rect, &new_rect, sizeof(Rect)) == 0) {
244 DLOG("Scratchpad size unchanged.\n");
248 DLOG("Fixing coordinates of scratchpad windows\n");
250 TAILQ_FOREACH(con, &(__i3_scratch->floating_head), floating_windows) {
251 floating_fix_coordinates(con, &old_rect, &new_rect);