]> git.sur5r.net Git - i3/i3/blob - src/scratchpad.c
Merge branch 'master' into next
[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     con_focus(focus_next);
62 }
63
64 /*
65  * Either shows the top-most scratchpad window (con == NULL) or shows the
66  * specified con (if it is scratchpad window).
67  *
68  * When called with con == NULL and the currently focused window is a
69  * scratchpad window, this serves as a shortcut to hide it again (so the user
70  * can press the same key to quickly look something up).
71  *
72  */
73 void scratchpad_show(Con *con) {
74     DLOG("should show scratchpad window %p\n", con);
75     Con *__i3_scratch = workspace_get("__i3_scratch", NULL);
76     Con *floating;
77
78     /* If this was 'scratchpad show' without criteria, we check if the
79      * currently focused window is a scratchpad window and should be hidden
80      * again. */
81     if (!con &&
82         (floating = con_inside_floating(focused)) &&
83         floating->scratchpad_state != SCRATCHPAD_NONE) {
84         DLOG("Focused window is a scratchpad window, hiding it.\n");
85         scratchpad_move(focused);
86         return;
87     }
88
89     /* If this was 'scratchpad show' with criteria, we check if it matches a
90      * currently visible scratchpad window and hide it. */
91     if (con &&
92         (floating = con_inside_floating(con)) &&
93         floating->scratchpad_state != SCRATCHPAD_NONE &&
94         con_get_workspace(con) != __i3_scratch) {
95         DLOG("Window is a scratchpad window, hiding it.\n");
96         scratchpad_move(con);
97         return;
98     }
99
100     Con *ws = con_get_workspace(focused);
101     if (con == NULL) {
102         /* Use the container on __i3_scratch which is highest in the focus
103          * stack. When moving windows to __i3_scratch, they get inserted at the
104          * bottom of the stack. */
105         con = TAILQ_FIRST(&(__i3_scratch->floating_head));
106
107         if (!con) {
108             LOG("You don't have any scratchpad windows yet.\n");
109             LOG("Use 'move scratchpad' to move a window to the scratchpad.\n");
110             return;
111         }
112     }
113
114     /* 1: Move the window from __i3_scratch to the current workspace. */
115     con_move_to_workspace(con, ws, true, false);
116
117     /* 2: Adjust the size if this window was not adjusted yet. */
118     if (con->scratchpad_state == SCRATCHPAD_FRESH) {
119         DLOG("Adjusting size of this window.\n");
120         Con *output = con_get_output(con);
121         con->rect.width = output->rect.width * 0.5;
122         con->rect.height = output->rect.height * 0.75;
123         con->rect.x = output->rect.x +
124                       ((output->rect.width / 2.0) - (con->rect.width / 2.0));
125         con->rect.y = output->rect.y +
126                       ((output->rect.height / 2.0) - (con->rect.height / 2.0));
127         con->scratchpad_state = SCRATCHPAD_CHANGED;
128     }
129
130     con_focus(con_descend_focused(con));
131 }