2 #define I3__FILE__ "scratchpad.c"
4 * vim:ts=4:sw=4:expandtab
6 * i3 - an improved dynamic tiling window manager
7 * © 2009-2011 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 is floating. From now on, we deal with the
43 * CT_FLOATING_CON. We use automatic == false because the user made the
44 * choice that this window should be a scratchpad (and floating). */
45 floating_enable(con, false);
48 /* 2: Send the window to the __i3_scratch workspace, mainting its
49 * coordinates and not warping the pointer. */
50 Con *focus_next = con_next_focused(con);
51 con_move_to_workspace(con, __i3_scratch, true, true);
53 /* 3: If this is the first time this window is used as a scratchpad, we set
54 * the scratchpad_state to SCRATCHPAD_FRESH. The window will then be
55 * adjusted in size according to what the user specifies. */
56 if (con->scratchpad_state == SCRATCHPAD_NONE) {
57 DLOG("This window was never used as a scratchpad before.\n");
58 con->scratchpad_state = SCRATCHPAD_FRESH;
61 /* 4: Fix focus. Normally, when moving a window to a different output, the
62 * destination output gets focused. In this case, we don’t want that. */
63 con_focus(focus_next);
67 * Either shows the top-most scratchpad window (con == NULL) or shows the
68 * specified con (if it is scratchpad window).
70 * When called with con == NULL and the currently focused window is a
71 * scratchpad window, this serves as a shortcut to hide it again (so the user
72 * can press the same key to quickly look something up).
75 void scratchpad_show(Con *con) {
76 DLOG("should show scratchpad window %p\n", con);
77 Con *__i3_scratch = workspace_get("__i3_scratch", NULL);
80 /* If this was 'scratchpad show' without criteria, we check if the
81 * currently focused window is a scratchpad window and should be hidden
84 (floating = con_inside_floating(focused)) &&
85 floating->scratchpad_state != SCRATCHPAD_NONE) {
86 DLOG("Focused window is a scratchpad window, hiding it.\n");
87 scratchpad_move(focused);
91 /* If this was 'scratchpad show' with criteria, we check if it matches a
92 * currently visible scratchpad window and hide it. */
93 Con *active = con_get_workspace(focused);
94 Con *current = con_get_workspace(con);
96 (floating = con_inside_floating(con)) &&
97 floating->scratchpad_state != SCRATCHPAD_NONE &&
98 current != __i3_scratch) {
99 /* If scratchpad window is on the active workspace, then we should hide
100 * it, otherwise we should move it to the active workspace. */
101 if (current == active) {
102 DLOG("Window is a scratchpad window, hiding it.\n");
103 scratchpad_move(con);
109 /* Use the container on __i3_scratch which is highest in the focus
110 * stack. When moving windows to __i3_scratch, they get inserted at the
111 * bottom of the stack. */
112 con = TAILQ_FIRST(&(__i3_scratch->floating_head));
115 LOG("You don't have any scratchpad windows yet.\n");
116 LOG("Use 'move scratchpad' to move a window to the scratchpad.\n");
121 /* 1: Move the window from __i3_scratch to the current workspace. */
122 con_move_to_workspace(con, active, true, false);
124 /* 2: Adjust the size if this window was not adjusted yet. */
125 if (con->scratchpad_state == SCRATCHPAD_FRESH) {
126 DLOG("Adjusting size of this window.\n");
127 Con *output = con_get_output(con);
128 con->rect.width = output->rect.width * 0.5;
129 con->rect.height = output->rect.height * 0.75;
130 con->rect.x = output->rect.x +
131 ((output->rect.width / 2.0) - (con->rect.width / 2.0));
132 con->rect.y = output->rect.y +
133 ((output->rect.height / 2.0) - (con->rect.height / 2.0));
134 con->scratchpad_state = SCRATCHPAD_CHANGED;
137 /* Activate active workspace if window is from another workspace to ensure
139 if (current != active) {
140 workspace_show(active);
143 con_focus(con_descend_focused(con));
147 * Greatest common divisor, implemented only for the least common multiple
151 static int _gcd(const int m, const int n) {
154 return _gcd(n, (m % n));
158 * Least common multiple. We use it to determine the (ideally not too large)
159 * resolution for the __i3 pseudo-output on which the scratchpad is on (see
160 * below). We could just multiply the resolutions, but for some pathetic cases
161 * (many outputs), using the LCM will achieve better results.
163 * Man, when you were learning about these two algorithms for the first time,
164 * did you think you’d ever need them in a real-world software project of
165 * yours? I certainly didn’t until now. :-D
168 static int _lcm(const int m, const int n) {
169 const int o = _gcd(m, n);
170 return ((m * n) / o);
174 * When starting i3 initially (and after each change to the connected outputs),
175 * this function fixes the resolution of the __i3 pseudo-output. When that
176 * resolution is not set to a function which shares a common divisor with every
177 * active output’s resolution, floating point calculation errors will lead to
178 * the scratchpad window moving when shown repeatedly.
181 void scratchpad_fix_resolution(void) {
182 Con *__i3_scratch = workspace_get("__i3_scratch", NULL);
183 Con *__i3_output = con_get_output(__i3_scratch);
184 DLOG("Current resolution: (%d, %d) %d x %d\n",
185 __i3_output->rect.x, __i3_output->rect.y,
186 __i3_output->rect.width, __i3_output->rect.height);
190 TAILQ_FOREACH(output, &(croot->nodes_head), nodes) {
191 if (output == __i3_output)
193 DLOG("output %s's resolution: (%d, %d) %d x %d\n",
194 output->name, output->rect.x, output->rect.y,
195 output->rect.width, output->rect.height);
196 if (new_width == -1) {
197 new_width = output->rect.width;
198 new_height = output->rect.height;
200 new_width = _lcm(new_width, output->rect.width);
201 new_height = _lcm(new_height, output->rect.height);
204 DLOG("new width = %d, new height = %d\n",
205 new_width, new_height);
206 __i3_output->rect.width = new_width;
207 __i3_output->rect.height = new_height;