]> git.sur5r.net Git - i3/i3/blob - src/scratchpad.c
Merge branch 'fix-scratchpad-focus'
[i3/i3] / src / scratchpad.c
1 /*
2  * vim:ts=4:sw=4:expandtab
3  *
4  * i3 - an improved dynamic tiling window manager
5  * © 2009-2011 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", con->name);
23         Con *current;
24         current = TAILQ_FIRST(&(con->focus_head));
25         while (current) {
26             Con *next = TAILQ_NEXT(current, focused);
27             scratchpad_move(current);
28             current = next;
29         }
30         return;
31     }
32     DLOG("should move con %p to __i3_scratch\n", con);
33
34     Con *__i3_scratch = workspace_get("__i3_scratch", NULL);
35     if (con_get_workspace(con) == __i3_scratch) {
36         DLOG("This window is already on __i3_scratch.\n");
37         return;
38     }
39
40     /* 1: Ensure the window is floating. From now on, we deal with the
41      * CT_FLOATING_CON. We use automatic == false because the user made the
42      * choice that this window should be a scratchpad (and floating). */
43     floating_enable(con, false);
44     con = con->parent;
45
46     /* 2: Send the window to the __i3_scratch workspace, mainting its
47      * coordinates and not warping the pointer. */
48     Con *focus_next = con_next_focused(con);
49     con_move_to_workspace(con, __i3_scratch, true, true);
50
51     /* 3: If this is the first time this window is used as a scratchpad, we set
52      * the scratchpad_state to SCRATCHPAD_FRESH. The window will then be
53      * adjusted in size according to what the user specifies. */
54     if (con->scratchpad_state == SCRATCHPAD_NONE) {
55         DLOG("This window was never used as a scratchpad before.\n");
56         con->scratchpad_state = SCRATCHPAD_FRESH;
57     }
58
59     /* 4: Fix focus. Normally, when moving a window to a different output, the
60      * destination output gets focused. In this case, we don’t want that. */
61     if (con_get_workspace(focus_next) == con_get_workspace(focused))
62         con_focus(focus_next);
63 }
64
65 /*
66  * Either shows the top-most scratchpad window (con == NULL) or shows the
67  * specified con (if it is scratchpad window).
68  *
69  * When called with con == NULL and the currently focused window is a
70  * scratchpad window, this serves as a shortcut to hide it again (so the user
71  * can press the same key to quickly look something up).
72  *
73  */
74 void scratchpad_show(Con *con) {
75     DLOG("should show scratchpad window %p\n", con);
76     Con *__i3_scratch = workspace_get("__i3_scratch", NULL);
77     Con *floating;
78
79     /* If this was 'scratchpad show' without criteria, we check if the
80      * currently focused window is a scratchpad window and should be hidden
81      * again. */
82     if (!con &&
83         (floating = con_inside_floating(focused)) &&
84         floating->scratchpad_state != SCRATCHPAD_NONE) {
85         DLOG("Focused window is a scratchpad window, hiding it.\n");
86         scratchpad_move(focused);
87         return;
88     }
89
90     /* If this was 'scratchpad show' with criteria, we check if it matches a
91      * currently visible scratchpad window and hide it. */
92     Con *active = con_get_workspace(focused);
93     Con *current = con_get_workspace(con);
94     if (con &&
95         (floating = con_inside_floating(con)) &&
96         floating->scratchpad_state != SCRATCHPAD_NONE &&
97         current != __i3_scratch) {
98         /* If scratchpad window is on the active workspace, then we should hide
99          * it, otherwise we should move it to the active workspace. */
100         if (current == active) {
101             DLOG("Window is a scratchpad window, hiding it.\n");
102             scratchpad_move(con);
103             return;
104         }
105     }
106
107     if (con == NULL) {
108         /* Use the container on __i3_scratch which is highest in the focus
109          * stack. When moving windows to __i3_scratch, they get inserted at the
110          * bottom of the stack. */
111         con = TAILQ_FIRST(&(__i3_scratch->floating_head));
112
113         if (!con) {
114             LOG("You don't have any scratchpad windows yet.\n");
115             LOG("Use 'move scratchpad' to move a window to the scratchpad.\n");
116             return;
117         }
118     }
119
120     /* 1: Move the window from __i3_scratch to the current workspace. */
121     con_move_to_workspace(con, active, true, false);
122
123     /* 2: Adjust the size if this window was not adjusted yet. */
124     if (con->scratchpad_state == SCRATCHPAD_FRESH) {
125         DLOG("Adjusting size of this window.\n");
126         Con *output = con_get_output(con);
127         con->rect.width = output->rect.width * 0.5;
128         con->rect.height = output->rect.height * 0.75;
129         con->rect.x = output->rect.x +
130                       ((output->rect.width / 2.0) - (con->rect.width / 2.0));
131         con->rect.y = output->rect.y +
132                       ((output->rect.height / 2.0) - (con->rect.height / 2.0));
133         con->scratchpad_state = SCRATCHPAD_CHANGED;
134     }
135
136     /* Activate active workspace if window is from another workspace to ensure
137      * proper focus. */
138     if (current != active) {
139         workspace_show(active);
140     }
141
142     con_focus(con_descend_focused(con));
143 }