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  */
19 void 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", NULL);
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 */
43  if (focused && focused->type != CT_WORKSPACE && focused->fullscreen_mode != CF_NONE) {
45  }
46 
47  /* 1: Ensure the window or any parent is floating. From now on, we deal
48  * with the CT_FLOATING_CON. We use automatic == false because the user
49  * made the choice that this window should be a scratchpad (and floating).
50  */
51  Con *maybe_floating_con = con_inside_floating(con);
52  if (maybe_floating_con == NULL) {
53  floating_enable(con, false);
54  con = con->parent;
55  } else {
56  con = maybe_floating_con;
57  }
58 
59  /* 2: Send the window to the __i3_scratch workspace, mainting its
60  * coordinates and not warping the pointer. */
61  con_move_to_workspace(con, __i3_scratch, true, true, false);
62 
63  /* 3: If this is the first time this window is used as a scratchpad, we set
64  * the scratchpad_state to SCRATCHPAD_FRESH. The window will then be
65  * adjusted in size according to what the user specifies. */
66  if (con->scratchpad_state == SCRATCHPAD_NONE) {
67  DLOG("This window was never used as a scratchpad before.\n");
68  if (con == maybe_floating_con) {
69  DLOG("It was in floating mode before, set scratchpad state to changed.\n");
70  con->scratchpad_state = SCRATCHPAD_CHANGED;
71  } else {
72  DLOG("It was in tiling mode before, set scratchpad state to fresh.\n");
73  con->scratchpad_state = SCRATCHPAD_FRESH;
74  }
75  }
76 }
77 
78 /*
79  * Either shows the top-most scratchpad window (con == NULL) or shows the
80  * specified con (if it is scratchpad window).
81  *
82  * When called with con == NULL and the currently focused window is a
83  * scratchpad window, this serves as a shortcut to hide it again (so the user
84  * can press the same key to quickly look something up).
85  *
86  */
87 bool scratchpad_show(Con *con) {
88  DLOG("should show scratchpad window %p\n", con);
89  Con *__i3_scratch = workspace_get("__i3_scratch", NULL);
90  Con *floating;
91 
92  /* If this was 'scratchpad show' without criteria, we check if the
93  * currently focused window is a scratchpad window and should be hidden
94  * again. */
95  if (!con &&
96  (floating = con_inside_floating(focused)) &&
97  floating->scratchpad_state != SCRATCHPAD_NONE) {
98  DLOG("Focused window is a scratchpad window, hiding it.\n");
100  return true;
101  }
102 
103  /* If the current con or any of its parents are in fullscreen mode, we
104  * first need to disable it before showing the scratchpad con. */
105  Con *fs = focused;
106  while (fs && fs->fullscreen_mode == CF_NONE)
107  fs = fs->parent;
108 
109  if (fs && fs->type != CT_WORKSPACE) {
111  }
112 
113  /* If this was 'scratchpad show' without criteria, we check if there is a
114  * unfocused scratchpad on the current workspace and focus it */
115  Con *walk_con;
116  Con *focused_ws = con_get_workspace(focused);
117  TAILQ_FOREACH(walk_con, &(focused_ws->floating_head), floating_windows) {
118  if (!con && (floating = con_inside_floating(walk_con)) &&
119  floating->scratchpad_state != SCRATCHPAD_NONE &&
120  floating != con_inside_floating(focused)) {
121  DLOG("Found an unfocused scratchpad window on this workspace\n");
122  DLOG("Focusing it: %p\n", walk_con);
123  /* use con_descend_tiling_focused to get the last focused
124  * window inside this scratch container in order to
125  * keep the focus the same within this container */
127  return true;
128  }
129  }
130 
131  /* If this was 'scratchpad show' without criteria, we check if there is a
132  * visible scratchpad window on another workspace. In this case we move it
133  * to the current workspace. */
134  focused_ws = con_get_workspace(focused);
135  TAILQ_FOREACH(walk_con, &all_cons, all_cons) {
136  Con *walk_ws = con_get_workspace(walk_con);
137  if (!con && walk_ws &&
138  !con_is_internal(walk_ws) && focused_ws != walk_ws &&
139  (floating = con_inside_floating(walk_con)) &&
140  floating->scratchpad_state != SCRATCHPAD_NONE) {
141  DLOG("Found a visible scratchpad window on another workspace,\n");
142  DLOG("moving it to this workspace: con = %p\n", walk_con);
143  con_move_to_workspace(walk_con, focused_ws, true, false, false);
145  return true;
146  }
147  }
148 
149  /* If this was 'scratchpad show' with criteria, we check if the window
150  * is actually in the scratchpad */
151  if (con && con->parent->scratchpad_state == SCRATCHPAD_NONE) {
152  DLOG("Window is not in the scratchpad, doing nothing.\n");
153  return false;
154  }
155 
156  /* If this was 'scratchpad show' with criteria, we check if it matches a
157  * currently visible scratchpad window and hide it. */
158  Con *active = con_get_workspace(focused);
159  Con *current = con_get_workspace(con);
160  if (con &&
161  (floating = con_inside_floating(con)) &&
162  floating->scratchpad_state != SCRATCHPAD_NONE &&
163  current != __i3_scratch) {
164  /* If scratchpad window is on the active workspace, then we should hide
165  * it, otherwise we should move it to the active workspace. */
166  if (current == active) {
167  DLOG("Window is a scratchpad window, hiding it.\n");
168  scratchpad_move(con);
169  return true;
170  }
171  }
172 
173  if (con == NULL) {
174  /* Use the container on __i3_scratch which is highest in the focus
175  * stack. When moving windows to __i3_scratch, they get inserted at the
176  * bottom of the stack. */
177  con = TAILQ_FIRST(&(__i3_scratch->floating_head));
178 
179  if (!con) {
180  LOG("You don't have any scratchpad windows yet.\n");
181  LOG("Use 'move scratchpad' to move a window to the scratchpad.\n");
182  return false;
183  }
184  } else {
185  /* We used a criterion, so we need to do what follows (moving,
186  * resizing) on the floating parent. */
187  con = con_inside_floating(con);
188  }
189 
190  /* 1: Move the window from __i3_scratch to the current workspace. */
191  con_move_to_workspace(con, active, true, false, false);
192 
193  /* 2: Adjust the size if this window was not adjusted yet. */
194  if (con->scratchpad_state == SCRATCHPAD_FRESH) {
195  DLOG("Adjusting size of this window.\n");
196  Con *output = con_get_output(con);
197  con->rect.width = output->rect.width * 0.5;
198  con->rect.height = output->rect.height * 0.75;
199  floating_check_size(con, false);
200  floating_center(con, con_get_workspace(con)->rect);
201  }
202 
203  /* Activate active workspace if window is from another workspace to ensure
204  * proper focus. */
205  if (current != active) {
206  workspace_show(active);
207  }
208 
210 
211  return true;
212 }
213 
214 /*
215  * Greatest common divisor, implemented only for the least common multiple
216  * below.
217  *
218  */
219 static int _gcd(const int m, const int n) {
220  if (n == 0)
221  return m;
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  */
236 static 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", NULL);
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  DLOG("output %s's resolution: (%d, %d) %d x %d\n",
262  output->name, output->rect.x, output->rect.y,
263  output->rect.width, output->rect.height);
264  if (new_width == -1) {
265  new_width = output->rect.width;
266  new_height = output->rect.height;
267  } else {
268  new_width = _lcm(new_width, output->rect.width);
269  new_height = _lcm(new_height, output->rect.height);
270  }
271  }
272 
273  Rect old_rect = __i3_output->rect;
274 
275  DLOG("new width = %d, new height = %d\n",
276  new_width, new_height);
277  __i3_output->rect.width = new_width;
278  __i3_output->rect.height = new_height;
279 
280  Rect new_rect = __i3_output->rect;
281 
282  if (memcmp(&old_rect, &new_rect, sizeof(Rect)) == 0) {
283  DLOG("Scratchpad size unchanged.\n");
284  return;
285  }
286 
287  DLOG("Fixing coordinates of scratchpad windows\n");
288  Con *con;
289  TAILQ_FOREACH(con, &(__i3_scratch->floating_head), floating_windows) {
290  floating_fix_coordinates(con, &old_rect, &new_rect);
291  }
292 }
uint32_t height
Definition: data.h:161
static int _gcd(const int m, const int n)
Definition: scratchpad.c:219
void floating_check_size(Con *floating_con, bool prefer_height)
Called when a floating window is created or resized.
Definition: floating.c:73
void 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:224
void workspace_show(Con *workspace)
Switches to the given workspace.
Definition: workspace.c:412
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
struct Con * croot
Definition: tree.c:12
#define DLOG(fmt,...)
Definition: libi3.h:104
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
enum Con::@20 type
struct Con * parent
Definition: data.h:645
Con * con_inside_floating(Con *con)
Checks if the given container is either floating or inside some floating container.
Definition: con.c:564
Stores a rectangle, for example the size of a window, the child window etc.
Definition: data.h:157
uint32_t x
Definition: data.h:158
void floating_center(Con *con, Rect rect)
Centers a floating con above the specified rect.
Definition: floating.c:522
nodes_head
Definition: data.h:694
struct Rect rect
Definition: data.h:649
enum Con::@22 scratchpad_state
void con_toggle_fullscreen(Con *con, int fullscreen_mode)
Toggles fullscreen mode for the given container.
Definition: con.c:998
A 'Con' represents everything from the X11 root window down to a single X11 window.
Definition: data.h:613
#define TAILQ_FIRST(head)
Definition: queue.h:336
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:1000
Con * con_get_workspace(Con *con)
Gets the workspace container this node is on.
Definition: con.c:418
Con * con_get_output(Con *con)
Gets the output container (first container with CT_OUTPUT in hierarchy) this node is on...
Definition: con.c:404
#define TAILQ_NEXT(elm, field)
Definition: queue.h:338
focus_head
Definition: data.h:697
floating_head
Definition: data.h:691
char * name
Definition: data.h:659
fullscreen_mode_t fullscreen_mode
Definition: data.h:702
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:1367
uint32_t y
Definition: data.h:159
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:87
static int _lcm(const int m, const int n)
Definition: scratchpad.c:236
bool con_is_internal(Con *con)
Returns true if the container is internal, such as __i3_scratch.
Definition: con.c:532
#define LOG(fmt,...)
Definition: libi3.h:94
uint32_t width
Definition: data.h:160
Con * workspace_get(const char *num, bool *created)
Returns a pointer to the workspace with the given number (starting at 0), creating the workspace if n...
Definition: workspace.c:125
struct Con * focused
Definition: tree.c:13
struct all_cons_head all_cons
Definition: tree.c:15
Con * con_descend_focused(Con *con)
Returns the focused con inside this client, descending the tree as far as possible.
Definition: con.c:1491
Definition: data.h:598
Con * con_descend_tiling_focused(Con *con)
Returns the focused con inside this client, descending the tree as far as possible.
Definition: con.c:1506
void con_activate(Con *con)
Sets input focus to the given container and raises it to the top.
Definition: con.c:263
#define TAILQ_FOREACH(var, head, field)
Definition: queue.h:347