2011-12-22 00:17:52 +01:00
|
|
|
|
/*
|
|
|
|
|
* vim:ts=4:sw=4:expandtab
|
|
|
|
|
*
|
|
|
|
|
* i3 - an improved dynamic tiling window manager
|
2015-04-04 02:17:56 +02:00
|
|
|
|
* © 2009 Michael Stapelberg and contributors (see also: LICENSE)
|
2011-12-22 00:17:52 +01:00
|
|
|
|
*
|
|
|
|
|
* scratchpad.c: Moving windows to the scratchpad and making them visible again.
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
#include "all.h"
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Moves the specified window to the __i3_scratch workspace, making it floating
|
|
|
|
|
* and setting the appropriate scratchpad_state.
|
|
|
|
|
*
|
|
|
|
|
* Gets called upon the command 'move scratchpad'.
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
void scratchpad_move(Con *con) {
|
2012-01-07 18:36:30 +01:00
|
|
|
|
if (con->type == CT_WORKSPACE) {
|
|
|
|
|
LOG("'move scratchpad' used on a workspace \"%s\". Calling it "
|
2014-06-15 19:07:02 +02:00
|
|
|
|
"recursively on all windows on this workspace.\n",
|
|
|
|
|
con->name);
|
2012-01-07 18:36:30 +01:00
|
|
|
|
Con *current;
|
|
|
|
|
current = TAILQ_FIRST(&(con->focus_head));
|
|
|
|
|
while (current) {
|
|
|
|
|
Con *next = TAILQ_NEXT(current, focused);
|
|
|
|
|
scratchpad_move(current);
|
|
|
|
|
current = next;
|
|
|
|
|
}
|
|
|
|
|
return;
|
|
|
|
|
}
|
2011-12-22 00:17:52 +01:00
|
|
|
|
DLOG("should move con %p to __i3_scratch\n", con);
|
|
|
|
|
|
|
|
|
|
Con *__i3_scratch = workspace_get("__i3_scratch", NULL);
|
|
|
|
|
if (con_get_workspace(con) == __i3_scratch) {
|
|
|
|
|
DLOG("This window is already on __i3_scratch.\n");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2013-05-24 15:02:04 +02:00
|
|
|
|
/* If the current con is in fullscreen mode, we need to disable that,
|
|
|
|
|
* as a scratchpad window should never be in fullscreen mode */
|
|
|
|
|
if (focused && focused->type != CT_WORKSPACE && focused->fullscreen_mode != CF_NONE) {
|
|
|
|
|
con_toggle_fullscreen(focused, CF_OUTPUT);
|
|
|
|
|
}
|
|
|
|
|
|
2012-09-28 00:38:02 +02:00
|
|
|
|
/* 1: Ensure the window or any parent is floating. From now on, we deal
|
|
|
|
|
* with the CT_FLOATING_CON. We use automatic == false because the user
|
|
|
|
|
* made the choice that this window should be a scratchpad (and floating).
|
|
|
|
|
*/
|
|
|
|
|
Con *maybe_floating_con = con_inside_floating(con);
|
|
|
|
|
if (maybe_floating_con == NULL) {
|
|
|
|
|
floating_enable(con, false);
|
|
|
|
|
con = con->parent;
|
|
|
|
|
} else {
|
|
|
|
|
con = maybe_floating_con;
|
|
|
|
|
}
|
2011-12-22 00:17:52 +01:00
|
|
|
|
|
|
|
|
|
/* 2: Send the window to the __i3_scratch workspace, mainting its
|
|
|
|
|
* coordinates and not warping the pointer. */
|
2015-08-26 21:06:53 +02:00
|
|
|
|
con_move_to_workspace(con, __i3_scratch, true, true, false);
|
2011-12-22 00:17:52 +01:00
|
|
|
|
|
|
|
|
|
/* 3: If this is the first time this window is used as a scratchpad, we set
|
|
|
|
|
* the scratchpad_state to SCRATCHPAD_FRESH. The window will then be
|
|
|
|
|
* adjusted in size according to what the user specifies. */
|
|
|
|
|
if (con->scratchpad_state == SCRATCHPAD_NONE) {
|
|
|
|
|
DLOG("This window was never used as a scratchpad before.\n");
|
2013-09-27 11:03:52 +02:00
|
|
|
|
if (con == maybe_floating_con) {
|
|
|
|
|
DLOG("It was in floating mode before, set scratchpad state to changed.\n");
|
|
|
|
|
con->scratchpad_state = SCRATCHPAD_CHANGED;
|
|
|
|
|
} else {
|
|
|
|
|
DLOG("It was in tiling mode before, set scratchpad state to fresh.\n");
|
|
|
|
|
con->scratchpad_state = SCRATCHPAD_FRESH;
|
|
|
|
|
}
|
2011-12-22 00:17:52 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Either shows the top-most scratchpad window (con == NULL) or shows the
|
|
|
|
|
* specified con (if it is scratchpad window).
|
|
|
|
|
*
|
|
|
|
|
* When called with con == NULL and the currently focused window is a
|
|
|
|
|
* scratchpad window, this serves as a shortcut to hide it again (so the user
|
|
|
|
|
* can press the same key to quickly look something up).
|
|
|
|
|
*
|
|
|
|
|
*/
|
2018-04-06 18:00:38 +02:00
|
|
|
|
bool scratchpad_show(Con *con) {
|
2011-12-22 00:17:52 +01:00
|
|
|
|
DLOG("should show scratchpad window %p\n", con);
|
|
|
|
|
Con *__i3_scratch = workspace_get("__i3_scratch", NULL);
|
|
|
|
|
Con *floating;
|
|
|
|
|
|
2013-03-26 18:59:32 +01:00
|
|
|
|
/* If this was 'scratchpad show' without criteria, we check if the
|
|
|
|
|
* currently focused window is a scratchpad window and should be hidden
|
|
|
|
|
* again. */
|
|
|
|
|
if (!con &&
|
|
|
|
|
(floating = con_inside_floating(focused)) &&
|
|
|
|
|
floating->scratchpad_state != SCRATCHPAD_NONE) {
|
|
|
|
|
DLOG("Focused window is a scratchpad window, hiding it.\n");
|
|
|
|
|
scratchpad_move(focused);
|
2018-04-06 18:00:38 +02:00
|
|
|
|
return true;
|
2013-03-26 18:59:32 +01:00
|
|
|
|
}
|
|
|
|
|
|
2013-05-24 15:02:04 +02:00
|
|
|
|
/* If the current con or any of its parents are in fullscreen mode, we
|
|
|
|
|
* first need to disable it before showing the scratchpad con. */
|
|
|
|
|
Con *fs = focused;
|
|
|
|
|
while (fs && fs->fullscreen_mode == CF_NONE)
|
|
|
|
|
fs = fs->parent;
|
|
|
|
|
|
|
|
|
|
if (fs && fs->type != CT_WORKSPACE) {
|
|
|
|
|
con_toggle_fullscreen(fs, CF_OUTPUT);
|
|
|
|
|
}
|
|
|
|
|
|
2012-12-21 22:13:31 +01:00
|
|
|
|
/* If this was 'scratchpad show' without criteria, we check if there is a
|
2013-01-28 10:11:53 +01:00
|
|
|
|
* unfocused scratchpad on the current workspace and focus it */
|
2012-12-21 22:13:31 +01:00
|
|
|
|
Con *walk_con;
|
|
|
|
|
Con *focused_ws = con_get_workspace(focused);
|
2014-06-19 11:20:32 +02:00
|
|
|
|
TAILQ_FOREACH(walk_con, &(focused_ws->floating_head), floating_windows) {
|
2013-03-26 18:59:32 +01:00
|
|
|
|
if (!con && (floating = con_inside_floating(walk_con)) &&
|
2013-01-28 10:11:53 +01:00
|
|
|
|
floating->scratchpad_state != SCRATCHPAD_NONE &&
|
|
|
|
|
floating != con_inside_floating(focused)) {
|
2014-06-15 19:07:02 +02:00
|
|
|
|
DLOG("Found an unfocused scratchpad window on this workspace\n");
|
|
|
|
|
DLOG("Focusing it: %p\n", walk_con);
|
|
|
|
|
/* use con_descend_tiling_focused to get the last focused
|
2018-12-09 01:06:29 +01:00
|
|
|
|
* window inside this scratch container in order to
|
|
|
|
|
* keep the focus the same within this container */
|
2017-12-06 00:58:47 +01:00
|
|
|
|
con_activate(con_descend_tiling_focused(walk_con));
|
2018-04-06 18:00:38 +02:00
|
|
|
|
return true;
|
2014-06-15 19:07:02 +02:00
|
|
|
|
}
|
2013-01-28 10:11:53 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* If this was 'scratchpad show' without criteria, we check if there is a
|
|
|
|
|
* visible scratchpad window on another workspace. In this case we move it
|
|
|
|
|
* to the current workspace. */
|
|
|
|
|
focused_ws = con_get_workspace(focused);
|
2014-06-19 11:20:32 +02:00
|
|
|
|
TAILQ_FOREACH(walk_con, &all_cons, all_cons) {
|
2012-12-21 22:13:31 +01:00
|
|
|
|
Con *walk_ws = con_get_workspace(walk_con);
|
2013-03-26 18:59:32 +01:00
|
|
|
|
if (!con && walk_ws &&
|
2012-12-21 22:13:31 +01:00
|
|
|
|
!con_is_internal(walk_ws) && focused_ws != walk_ws &&
|
|
|
|
|
(floating = con_inside_floating(walk_con)) &&
|
|
|
|
|
floating->scratchpad_state != SCRATCHPAD_NONE) {
|
|
|
|
|
DLOG("Found a visible scratchpad window on another workspace,\n");
|
|
|
|
|
DLOG("moving it to this workspace: con = %p\n", walk_con);
|
2015-08-26 21:06:53 +02:00
|
|
|
|
con_move_to_workspace(walk_con, focused_ws, true, false, false);
|
2018-08-17 11:34:20 +02:00
|
|
|
|
con_activate(con_descend_focused(walk_con));
|
2018-04-06 18:00:38 +02:00
|
|
|
|
return true;
|
2012-12-21 22:13:31 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-03-26 18:59:32 +01:00
|
|
|
|
/* If this was 'scratchpad show' with criteria, we check if the window
|
|
|
|
|
* is actually in the scratchpad */
|
|
|
|
|
if (con && con->parent->scratchpad_state == SCRATCHPAD_NONE) {
|
|
|
|
|
DLOG("Window is not in the scratchpad, doing nothing.\n");
|
2018-04-06 18:00:38 +02:00
|
|
|
|
return false;
|
2011-12-22 00:17:52 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* If this was 'scratchpad show' with criteria, we check if it matches a
|
|
|
|
|
* currently visible scratchpad window and hide it. */
|
2012-03-20 23:52:38 +01:00
|
|
|
|
Con *active = con_get_workspace(focused);
|
|
|
|
|
Con *current = con_get_workspace(con);
|
2011-12-22 00:17:52 +01:00
|
|
|
|
if (con &&
|
|
|
|
|
(floating = con_inside_floating(con)) &&
|
|
|
|
|
floating->scratchpad_state != SCRATCHPAD_NONE &&
|
2012-03-20 23:52:38 +01:00
|
|
|
|
current != __i3_scratch) {
|
|
|
|
|
/* If scratchpad window is on the active workspace, then we should hide
|
|
|
|
|
* it, otherwise we should move it to the active workspace. */
|
|
|
|
|
if (current == active) {
|
|
|
|
|
DLOG("Window is a scratchpad window, hiding it.\n");
|
|
|
|
|
scratchpad_move(con);
|
2018-04-06 18:00:38 +02:00
|
|
|
|
return true;
|
2012-03-20 23:52:38 +01:00
|
|
|
|
}
|
2011-12-22 00:17:52 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (con == NULL) {
|
|
|
|
|
/* Use the container on __i3_scratch which is highest in the focus
|
|
|
|
|
* stack. When moving windows to __i3_scratch, they get inserted at the
|
|
|
|
|
* bottom of the stack. */
|
|
|
|
|
con = TAILQ_FIRST(&(__i3_scratch->floating_head));
|
|
|
|
|
|
|
|
|
|
if (!con) {
|
|
|
|
|
LOG("You don't have any scratchpad windows yet.\n");
|
|
|
|
|
LOG("Use 'move scratchpad' to move a window to the scratchpad.\n");
|
2018-04-06 18:00:38 +02:00
|
|
|
|
return false;
|
2011-12-22 00:17:52 +01:00
|
|
|
|
}
|
2013-07-10 22:18:18 +02:00
|
|
|
|
} else {
|
|
|
|
|
/* We used a criterion, so we need to do what follows (moving,
|
|
|
|
|
* resizing) on the floating parent. */
|
|
|
|
|
con = con_inside_floating(con);
|
2011-12-22 00:17:52 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 1: Move the window from __i3_scratch to the current workspace. */
|
2015-08-26 21:06:53 +02:00
|
|
|
|
con_move_to_workspace(con, active, true, false, false);
|
2011-12-22 00:17:52 +01:00
|
|
|
|
|
|
|
|
|
/* 2: Adjust the size if this window was not adjusted yet. */
|
|
|
|
|
if (con->scratchpad_state == SCRATCHPAD_FRESH) {
|
|
|
|
|
DLOG("Adjusting size of this window.\n");
|
|
|
|
|
Con *output = con_get_output(con);
|
|
|
|
|
con->rect.width = output->rect.width * 0.5;
|
|
|
|
|
con->rect.height = output->rect.height * 0.75;
|
2018-10-01 18:47:33 +02:00
|
|
|
|
floating_check_size(con, false);
|
2015-03-30 09:10:40 +02:00
|
|
|
|
floating_center(con, con_get_workspace(con)->rect);
|
2011-12-22 00:17:52 +01:00
|
|
|
|
}
|
|
|
|
|
|
2012-03-20 23:52:38 +01:00
|
|
|
|
/* Activate active workspace if window is from another workspace to ensure
|
|
|
|
|
* proper focus. */
|
|
|
|
|
if (current != active) {
|
|
|
|
|
workspace_show(active);
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-06 00:58:47 +01:00
|
|
|
|
con_activate(con_descend_focused(con));
|
2018-04-06 18:00:38 +02:00
|
|
|
|
|
|
|
|
|
return true;
|
2011-12-22 00:17:52 +01:00
|
|
|
|
}
|
2012-08-08 16:22:03 +02:00
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Greatest common divisor, implemented only for the least common multiple
|
|
|
|
|
* below.
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
static int _gcd(const int m, const int n) {
|
|
|
|
|
if (n == 0)
|
|
|
|
|
return m;
|
|
|
|
|
return _gcd(n, (m % n));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Least common multiple. We use it to determine the (ideally not too large)
|
|
|
|
|
* resolution for the __i3 pseudo-output on which the scratchpad is on (see
|
|
|
|
|
* below). We could just multiply the resolutions, but for some pathetic cases
|
|
|
|
|
* (many outputs), using the LCM will achieve better results.
|
|
|
|
|
*
|
|
|
|
|
* Man, when you were learning about these two algorithms for the first time,
|
|
|
|
|
* did you think you’d ever need them in a real-world software project of
|
|
|
|
|
* yours? I certainly didn’t until now. :-D
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
static int _lcm(const int m, const int n) {
|
|
|
|
|
const int o = _gcd(m, n);
|
|
|
|
|
return ((m * n) / o);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* When starting i3 initially (and after each change to the connected outputs),
|
|
|
|
|
* this function fixes the resolution of the __i3 pseudo-output. When that
|
|
|
|
|
* resolution is not set to a function which shares a common divisor with every
|
|
|
|
|
* active output’s resolution, floating point calculation errors will lead to
|
|
|
|
|
* the scratchpad window moving when shown repeatedly.
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
void scratchpad_fix_resolution(void) {
|
|
|
|
|
Con *__i3_scratch = workspace_get("__i3_scratch", NULL);
|
|
|
|
|
Con *__i3_output = con_get_output(__i3_scratch);
|
|
|
|
|
DLOG("Current resolution: (%d, %d) %d x %d\n",
|
|
|
|
|
__i3_output->rect.x, __i3_output->rect.y,
|
|
|
|
|
__i3_output->rect.width, __i3_output->rect.height);
|
|
|
|
|
Con *output;
|
|
|
|
|
int new_width = -1,
|
|
|
|
|
new_height = -1;
|
2014-06-19 11:20:32 +02:00
|
|
|
|
TAILQ_FOREACH(output, &(croot->nodes_head), nodes) {
|
2012-08-08 16:22:03 +02:00
|
|
|
|
if (output == __i3_output)
|
|
|
|
|
continue;
|
|
|
|
|
DLOG("output %s's resolution: (%d, %d) %d x %d\n",
|
|
|
|
|
output->name, output->rect.x, output->rect.y,
|
|
|
|
|
output->rect.width, output->rect.height);
|
|
|
|
|
if (new_width == -1) {
|
|
|
|
|
new_width = output->rect.width;
|
|
|
|
|
new_height = output->rect.height;
|
|
|
|
|
} else {
|
|
|
|
|
new_width = _lcm(new_width, output->rect.width);
|
|
|
|
|
new_height = _lcm(new_height, output->rect.height);
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-01-25 15:47:43 +01:00
|
|
|
|
|
|
|
|
|
Rect old_rect = __i3_output->rect;
|
|
|
|
|
|
2012-08-08 16:22:03 +02:00
|
|
|
|
DLOG("new width = %d, new height = %d\n",
|
|
|
|
|
new_width, new_height);
|
|
|
|
|
__i3_output->rect.width = new_width;
|
|
|
|
|
__i3_output->rect.height = new_height;
|
2013-01-25 15:47:43 +01:00
|
|
|
|
|
|
|
|
|
Rect new_rect = __i3_output->rect;
|
|
|
|
|
|
|
|
|
|
if (memcmp(&old_rect, &new_rect, sizeof(Rect)) == 0) {
|
|
|
|
|
DLOG("Scratchpad size unchanged.\n");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
DLOG("Fixing coordinates of scratchpad windows\n");
|
|
|
|
|
Con *con;
|
2014-06-19 11:20:32 +02:00
|
|
|
|
TAILQ_FOREACH(con, &(__i3_scratch->floating_head), floating_windows) {
|
2013-01-25 15:47:43 +01:00
|
|
|
|
floating_fix_coordinates(con, &old_rect, &new_rect);
|
|
|
|
|
}
|
2012-08-08 16:22:03 +02:00
|
|
|
|
}
|