]> git.sur5r.net Git - i3/i3/blob - src/resize.c
Return DRAG_ABORT on UnmapNotify from drag_pointer
[i3/i3] / src / resize.c
1 #undef I3__FILE__
2 #define I3__FILE__ "resize.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  * resize.c: Interactive resizing.
10  *
11  */
12 #include "all.h"
13
14 extern xcb_connection_t *conn;
15
16 /*
17  * This is an ugly data structure which we need because there is no standard
18  * way of having nested functions (only available as a gcc extension at the
19  * moment, clang doesn’t support it) or blocks (only available as a clang
20  * extension and only on Mac OS X systems at the moment).
21  *
22  */
23 struct callback_params {
24     orientation_t orientation;
25     Con *output;
26     xcb_window_t helpwin;
27     uint32_t *new_position;
28 };
29
30 DRAGGING_CB(resize_callback) {
31     const struct callback_params *params = extra;
32     Con *output = params->output;
33     DLOG("new x = %d, y = %d\n", new_x, new_y);
34     if (params->orientation == HORIZ) {
35         /* Check if the new coordinates are within screen boundaries */
36         if (new_x > (output->rect.x + output->rect.width - 25) ||
37             new_x < (output->rect.x + 25))
38             return;
39
40         *(params->new_position) = new_x;
41         xcb_configure_window(conn, params->helpwin, XCB_CONFIG_WINDOW_X, params->new_position);
42     } else {
43         if (new_y > (output->rect.y + output->rect.height - 25) ||
44             new_y < (output->rect.y + 25))
45             return;
46
47         *(params->new_position) = new_y;
48         xcb_configure_window(conn, params->helpwin, XCB_CONFIG_WINDOW_Y, params->new_position);
49     }
50
51     xcb_flush(conn);
52 }
53
54 bool resize_find_tiling_participants(Con **current, Con **other, direction_t direction) {
55     DLOG("Find two participants for resizing container=%p in direction=%i\n", other, direction);
56     Con *first = *current;
57     Con *second = NULL;
58     if (first == NULL) {
59         DLOG("Current container is NULL, aborting.\n");
60         return false;
61     }
62
63     /* Go up in the tree and search for a container to resize */
64     const orientation_t search_orientation = ((direction == D_LEFT || direction == D_RIGHT) ? HORIZ : VERT);
65     const bool dir_backwards = (direction == D_UP || direction == D_LEFT);
66     while (first->type != CT_WORKSPACE &&
67            first->type != CT_FLOATING_CON &&
68            second == NULL) {
69         /* get the appropriate first container with the matching
70          * orientation (skip stacked/tabbed cons) */
71         if ((con_orientation(first->parent) != search_orientation) ||
72             (first->parent->layout == L_STACKED) ||
73             (first->parent->layout == L_TABBED)) {
74             first = first->parent;
75             continue;
76         }
77
78         /* get the counterpart for this resizement */
79         if (dir_backwards) {
80             second = TAILQ_PREV(first, nodes_head, nodes);
81         } else {
82             second = TAILQ_NEXT(first, nodes);
83         }
84
85         if (second == NULL) {
86             DLOG("No second container in this direction found, trying to look further up in the tree...\n");
87             first = first->parent;
88         }
89     }
90
91     DLOG("Found participants: first=%p and second=%p.", first, second);
92     *current = first;
93     *other = second;
94     if (first == NULL || second == NULL) {
95         DLOG("Could not find two participants for this resize request.\n");
96         return false;
97     }
98
99     return true;
100 }
101
102 int resize_graphical_handler(Con *first, Con *second, orientation_t orientation, const xcb_button_press_event_t *event) {
103     DLOG("resize handler\n");
104
105     uint32_t new_position;
106
107     /* TODO: previously, we were getting a rect containing all screens. why? */
108     Con *output = con_get_output(first);
109     DLOG("x = %d, width = %d\n", output->rect.x, output->rect.width);
110
111     x_mask_event_mask(~XCB_EVENT_MASK_ENTER_WINDOW);
112     xcb_flush(conn);
113
114     uint32_t mask = 0;
115     uint32_t values[2];
116
117     mask = XCB_CW_OVERRIDE_REDIRECT;
118     values[0] = 1;
119
120     /* Open a new window, the resizebar. Grab the pointer and move the window around
121        as the user moves the pointer. */
122     xcb_window_t grabwin = create_window(conn, output->rect, XCB_COPY_FROM_PARENT, XCB_COPY_FROM_PARENT,
123             XCB_WINDOW_CLASS_INPUT_ONLY, XCURSOR_CURSOR_POINTER, true, mask, values);
124
125     Rect helprect;
126     if (orientation == HORIZ) {
127         helprect.x = event->root_x;
128         helprect.y = output->rect.y;
129         helprect.width = 2;
130         helprect.height = output->rect.height;
131         new_position = event->root_x;
132     } else {
133         helprect.x = output->rect.x;
134         helprect.y = event->root_y;
135         helprect.width = output->rect.width;
136         helprect.height = 2;
137         new_position = event->root_y;
138     }
139
140     mask = XCB_CW_BACK_PIXEL;
141     values[0] = config.client.focused.border;
142
143     mask |= XCB_CW_OVERRIDE_REDIRECT;
144     values[1] = 1;
145
146     xcb_window_t helpwin = create_window(conn, helprect, XCB_COPY_FROM_PARENT, XCB_COPY_FROM_PARENT,
147             XCB_WINDOW_CLASS_INPUT_OUTPUT, (orientation == HORIZ ?
148                                           XCURSOR_CURSOR_RESIZE_HORIZONTAL :
149                                           XCURSOR_CURSOR_RESIZE_VERTICAL), true, mask, values);
150
151     xcb_circulate_window(conn, XCB_CIRCULATE_RAISE_LOWEST, helpwin);
152
153     xcb_flush(conn);
154
155     const struct callback_params params = { orientation, output, helpwin, &new_position };
156
157     drag_result_t drag_result = drag_pointer(NULL, event, grabwin, BORDER_TOP, 0, resize_callback, &params);
158
159     xcb_destroy_window(conn, helpwin);
160     xcb_destroy_window(conn, grabwin);
161     xcb_flush(conn);
162
163     /* User cancelled the drag so no action should be taken. */
164     if (drag_result == DRAG_REVERT)
165         return 0;
166
167     int pixels;
168     if (orientation == HORIZ)
169         pixels = (new_position - event->root_x);
170     else pixels = (new_position - event->root_y);
171
172     DLOG("Done, pixels = %d\n", pixels);
173
174     // if we got thus far, the containers must have
175     // percentages associated with them
176     assert(first->percent > 0.0);
177     assert(second->percent > 0.0);
178
179     // calculate the new percentage for the first container
180     double new_percent, difference;
181     double percent = first->percent;
182     DLOG("percent = %f\n", percent);
183     int original = (orientation == HORIZ ? first->rect.width : first->rect.height);
184     DLOG("original = %d\n", original);
185     new_percent = (original + pixels) * (percent / original);
186     difference = percent - new_percent;
187     DLOG("difference = %f\n", difference);
188     DLOG("new percent = %f\n", new_percent);
189     first->percent = new_percent;
190
191     // calculate the new percentage for the second container
192     double s_percent = second->percent;
193     second->percent = s_percent + difference;
194     DLOG("second->percent = %f\n", second->percent);
195
196     // now we must make sure that the sum of the percentages remain 1.0
197     con_fix_percent(first->parent);
198
199     return 0;
200 }