]> git.sur5r.net Git - i3/i3/blob - src/scratchpad.c
Merge branch 'master' into next
[i3/i3] / src / scratchpad.c
1 #undef I3__FILE__
2 #define I3__FILE__ "scratchpad.c"
3 /*
4  * vim:ts=4:sw=4:expandtab
5  *
6  * i3 - an improved dynamic tiling window manager
7  * © 2009-2011 Michael Stapelberg and contributors (see also: LICENSE)
8  *
9  * scratchpad.c: Moving windows to the scratchpad and making them visible again.
10  *
11  */
12 #include "all.h"
13
14 /*
15  * Moves the specified window to the __i3_scratch workspace, making it floating
16  * and setting the appropriate scratchpad_state.
17  *
18  * Gets called upon the command 'move scratchpad'.
19  *
20  */
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);
25         Con *current;
26         current = TAILQ_FIRST(&(con->focus_head));
27         while (current) {
28             Con *next = TAILQ_NEXT(current, focused);
29             scratchpad_move(current);
30             current = next;
31         }
32         return;
33     }
34     DLOG("should move con %p to __i3_scratch\n", con);
35
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");
39         return;
40     }
41
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);
46     con = con->parent;
47
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);
52
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;
59     }
60
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     if (con_get_workspace(focus_next) == con_get_workspace(focused))
64         con_focus(focus_next);
65 }
66
67 /*
68  * Either shows the top-most scratchpad window (con == NULL) or shows the
69  * specified con (if it is scratchpad window).
70  *
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).
74  *
75  */
76 void scratchpad_show(Con *con) {
77     DLOG("should show scratchpad window %p\n", con);
78     Con *__i3_scratch = workspace_get("__i3_scratch", NULL);
79     Con *floating;
80
81     /* If this was 'scratchpad show' without criteria, we check if the
82      * currently focused window is a scratchpad window and should be hidden
83      * again. */
84     if (!con &&
85         (floating = con_inside_floating(focused)) &&
86         floating->scratchpad_state != SCRATCHPAD_NONE) {
87         DLOG("Focused window is a scratchpad window, hiding it.\n");
88         scratchpad_move(focused);
89         return;
90     }
91
92     /* If this was 'scratchpad show' with criteria, we check if it matches a
93      * currently visible scratchpad window and hide it. */
94     Con *active = con_get_workspace(focused);
95     Con *current = con_get_workspace(con);
96     if (con &&
97         (floating = con_inside_floating(con)) &&
98         floating->scratchpad_state != SCRATCHPAD_NONE &&
99         current != __i3_scratch) {
100         /* If scratchpad window is on the active workspace, then we should hide
101          * it, otherwise we should move it to the active workspace. */
102         if (current == active) {
103             DLOG("Window is a scratchpad window, hiding it.\n");
104             scratchpad_move(con);
105             return;
106         }
107     }
108
109     if (con == NULL) {
110         /* Use the container on __i3_scratch which is highest in the focus
111          * stack. When moving windows to __i3_scratch, they get inserted at the
112          * bottom of the stack. */
113         con = TAILQ_FIRST(&(__i3_scratch->floating_head));
114
115         if (!con) {
116             LOG("You don't have any scratchpad windows yet.\n");
117             LOG("Use 'move scratchpad' to move a window to the scratchpad.\n");
118             return;
119         }
120     }
121
122     /* 1: Move the window from __i3_scratch to the current workspace. */
123     con_move_to_workspace(con, active, true, false);
124
125     /* 2: Adjust the size if this window was not adjusted yet. */
126     if (con->scratchpad_state == SCRATCHPAD_FRESH) {
127         DLOG("Adjusting size of this window.\n");
128         Con *output = con_get_output(con);
129         con->rect.width = output->rect.width * 0.5;
130         con->rect.height = output->rect.height * 0.75;
131         con->rect.x = output->rect.x +
132                       ((output->rect.width / 2.0) - (con->rect.width / 2.0));
133         con->rect.y = output->rect.y +
134                       ((output->rect.height / 2.0) - (con->rect.height / 2.0));
135         con->scratchpad_state = SCRATCHPAD_CHANGED;
136     }
137
138     /* Activate active workspace if window is from another workspace to ensure
139      * proper focus. */
140     if (current != active) {
141         workspace_show(active);
142     }
143
144     con_focus(con_descend_focused(con));
145 }
146
147 /*
148  * Greatest common divisor, implemented only for the least common multiple
149  * below.
150  *
151  */
152 static int _gcd(const int m, const int n) {
153     if (n == 0)
154         return m;
155     return _gcd(n, (m % n));
156 }
157
158 /*
159  * Least common multiple. We use it to determine the (ideally not too large)
160  * resolution for the __i3 pseudo-output on which the scratchpad is on (see
161  * below). We could just multiply the resolutions, but for some pathetic cases
162  * (many outputs), using the LCM will achieve better results.
163  *
164  * Man, when you were learning about these two algorithms for the first time,
165  * did you think you’d ever need them in a real-world software project of
166  * yours? I certainly didn’t until now. :-D
167  *
168  */
169 static int _lcm(const int m, const int n) {
170     const int o = _gcd(m, n);
171     return ((m * n) / o);
172 }
173
174 /*
175  * When starting i3 initially (and after each change to the connected outputs),
176  * this function fixes the resolution of the __i3 pseudo-output. When that
177  * resolution is not set to a function which shares a common divisor with every
178  * active output’s resolution, floating point calculation errors will lead to
179  * the scratchpad window moving when shown repeatedly.
180  *
181  */
182 void scratchpad_fix_resolution(void) {
183     Con *__i3_scratch = workspace_get("__i3_scratch", NULL);
184     Con *__i3_output = con_get_output(__i3_scratch);
185     DLOG("Current resolution: (%d, %d) %d x %d\n",
186          __i3_output->rect.x, __i3_output->rect.y,
187          __i3_output->rect.width, __i3_output->rect.height);
188     Con *output;
189     int new_width = -1,
190         new_height = -1;
191     TAILQ_FOREACH(output, &(croot->nodes_head), nodes) {
192         if (output == __i3_output)
193             continue;
194         DLOG("output %s's resolution: (%d, %d) %d x %d\n",
195              output->name, output->rect.x, output->rect.y,
196              output->rect.width, output->rect.height);
197         if (new_width == -1) {
198             new_width = output->rect.width;
199             new_height = output->rect.height;
200         } else {
201             new_width = _lcm(new_width, output->rect.width);
202             new_height = _lcm(new_height, output->rect.height);
203         }
204     }
205     DLOG("new width = %d, new height = %d\n",
206          new_width, new_height);
207     __i3_output->rect.width = new_width;
208     __i3_output->rect.height = new_height;
209 }