1 /*
2 * vim:ts=4:sw=4:expandtab
3 *
4 * i3 - an improved dynamic tiling window manager
5 * © 2009 Michael Stapelberg and contributors (see also: LICENSE)
6 *
7 * resize.c: Interactive resizing.
8 *
9 */
10 #include "all.h"
11
12 /*
13 * This is an ugly data structure which we need because there is no standard
14 * way of having nested functions (only available as a gcc extension at the
15 * moment, clang doesn’t support it) or blocks (only available as a clang
16 * extension and only on Mac OS X systems at the moment).
17 *
18 */
19 struct callback_params {
20 orientation_t orientation;
21 Con *output;
22 xcb_window_t helpwin;
23 uint32_t *new_position;
24 bool *threshold_exceeded;
25 };
26
DRAGGING_CB(resize_callback)27 DRAGGING_CB(resize_callback) {
28 const struct callback_params *params = extra;
29 Con *output = params->output;
30 DLOG("new x = %d, y = %d\n", new_x, new_y);
31
32 if (!*params->threshold_exceeded) {
33 xcb_map_window(conn, params->helpwin);
34 /* Warp pointer in the same way as resize_graphical_handler() would do
35 * if threshold wasn't enabled, but also take into account travelled
36 * distance. */
37 if (params->orientation == HORIZ) {
38 xcb_warp_pointer(conn, XCB_NONE, event->root, 0, 0, 0, 0,
39 *params->new_position + new_x - event->root_x,
40 new_y);
41 } else {
42 xcb_warp_pointer(conn, XCB_NONE, event->root, 0, 0, 0, 0,
43 new_x,
44 *params->new_position + new_y - event->root_y);
45 }
46 *params->threshold_exceeded = true;
47 return;
48 }
49
50 if (params->orientation == HORIZ) {
51 /* Check if the new coordinates are within screen boundaries */
52 if (new_x > (output->rect.x + output->rect.width - 25) ||
53 new_x < (output->rect.x + 25))
54 return;
55
56 *(params->new_position) = new_x;
57 xcb_configure_window(conn, params->helpwin, XCB_CONFIG_WINDOW_X, params->new_position);
58 } else {
59 if (new_y > (output->rect.y + output->rect.height - 25) ||
60 new_y < (output->rect.y + 25))
61 return;
62
63 *(params->new_position) = new_y;
64 xcb_configure_window(conn, params->helpwin, XCB_CONFIG_WINDOW_Y, params->new_position);
65 }
66
67 xcb_flush(conn);
68 }
69
resize_find_tiling_participants(Con ** current,Con ** other,direction_t direction,bool both_sides)70 bool resize_find_tiling_participants(Con **current, Con **other, direction_t direction, bool both_sides) {
71 DLOG("Find two participants for resizing container=%p in direction=%i\n", other, direction);
72 Con *first = *current;
73 Con *second = NULL;
74 if (first == NULL) {
75 DLOG("Current container is NULL, aborting.\n");
76 return false;
77 }
78
79 /* Go up in the tree and search for a container to resize */
80 const orientation_t search_orientation = orientation_from_direction(direction);
81 const bool dir_backwards = (direction == D_UP || direction == D_LEFT);
82 while (first->type != CT_WORKSPACE &&
83 first->type != CT_FLOATING_CON &&
84 second == NULL) {
85 /* get the appropriate first container with the matching
86 * orientation (skip stacked/tabbed cons) */
87 if ((con_orientation(first->parent) != search_orientation) ||
88 (first->parent->layout == L_STACKED) ||
89 (first->parent->layout == L_TABBED)) {
90 first = first->parent;
91 continue;
92 }
93
94 /* get the counterpart for this resizement */
95 if (dir_backwards) {
96 second = TAILQ_PREV(first, nodes_head, nodes);
97 if (second == NULL && both_sides == true) {
98 second = TAILQ_NEXT(first, nodes);
99 }
100 } else {
101 second = TAILQ_NEXT(first, nodes);
102 if (second == NULL && both_sides == true) {
103 second = TAILQ_PREV(first, nodes_head, nodes);
104 }
105 }
106
107 if (second == NULL) {
108 DLOG("No second container in this direction found, trying to look further up in the tree...\n");
109 first = first->parent;
110 }
111 }
112
113 DLOG("Found participants: first=%p and second=%p.\n", first, second);
114 *current = first;
115 *other = second;
116 if (first == NULL || second == NULL) {
117 DLOG("Could not find two participants for this resize request.\n");
118 return false;
119 }
120
121 return true;
122 }
123
124 /*
125 * Calculate the minimum percent needed for the given container to be at least 1
126 * pixel.
127 *
128 */
percent_for_1px(Con * con)129 double percent_for_1px(Con *con) {
130 const int parent_size = con_rect_size_in_orientation(con->parent);
131 /* deco_rect.height is subtracted from each child in render_con_split */
132 const int min_size = (con_orientation(con->parent) == HORIZ ? 1 : 1 + con->deco_rect.height);
133 return ((double)min_size / (double)parent_size);
134 }
135
136 /*
137 * Resize the two given containers using the given amount of pixels or
138 * percentage points. One of the two needs to be 0. A positive amount means
139 * growing the first container while a negative means shrinking it.
140 * Returns false when the resize would result in one of the two containers
141 * having less than 1 pixel of size.
142 *
143 */
resize_neighboring_cons(Con * first,Con * second,int px,int ppt)144 bool resize_neighboring_cons(Con *first, Con *second, int px, int ppt) {
145 assert(px * ppt == 0);
146
147 Con *parent = first->parent;
148 double new_first_percent;
149 double new_second_percent;
150 if (ppt) {
151 new_first_percent = first->percent + ((double)ppt / 100.0);
152 new_second_percent = second->percent - ((double)ppt / 100.0);
153 } else {
154 /* Convert px change to change in percentages */
155 const double pct = (double)px / (double)con_rect_size_in_orientation(first->parent);
156 new_first_percent = first->percent + pct;
157 new_second_percent = second->percent - pct;
158 }
159 /* Ensure that no container will be less than 1 pixel in the resizing
160 * direction. */
161 if (new_first_percent < percent_for_1px(first) || new_second_percent < percent_for_1px(second)) {
162 return false;
163 }
164
165 first->percent = new_first_percent;
166 second->percent = new_second_percent;
167 con_fix_percent(parent);
168 return true;
169 }
170
resize_graphical_handler(Con * first,Con * second,orientation_t orientation,const xcb_button_press_event_t * event,bool use_threshold)171 void resize_graphical_handler(Con *first, Con *second, orientation_t orientation,
172 const xcb_button_press_event_t *event,
173 bool use_threshold) {
174 Con *output = con_get_output(first);
175 DLOG("x = %d, width = %d\n", output->rect.x, output->rect.width);
176
177 x_mask_event_mask(~XCB_EVENT_MASK_ENTER_WINDOW);
178 xcb_flush(conn);
179
180 uint32_t mask = 0;
181 uint32_t values[2];
182
183 mask = XCB_CW_OVERRIDE_REDIRECT;
184 values[0] = 1;
185
186 /* Open a new window, the resizebar. Grab the pointer and move the window
187 * around as the user moves the pointer. */
188 xcb_window_t grabwin = create_window(conn, output->rect, XCB_COPY_FROM_PARENT, XCB_COPY_FROM_PARENT,
189 XCB_WINDOW_CLASS_INPUT_ONLY, XCURSOR_CURSOR_POINTER, true, mask, values);
190
191 /* Keep track of the coordinate orthogonal to motion so we can determine the
192 * length of the resize afterward. */
193 uint32_t initial_position, new_position;
194
195 /* Configure the resizebar and snap the pointer. The resizebar runs along
196 * the rect of the second con and follows the motion of the pointer. */
197 Rect helprect;
198 helprect.x = second->rect.x;
199 helprect.y = second->rect.y;
200 if (orientation == HORIZ) {
201 helprect.width = logical_px(2);
202 helprect.height = second->rect.height;
203 initial_position = second->rect.x;
204 } else {
205 helprect.width = second->rect.width;
206 helprect.height = logical_px(2);
207 initial_position = second->rect.y;
208 }
209
210 mask = XCB_CW_BACK_PIXEL;
211 values[0] = config.client.focused.border.colorpixel;
212
213 mask |= XCB_CW_OVERRIDE_REDIRECT;
214 values[1] = 1;
215
216 xcb_window_t helpwin = create_window(conn, helprect, XCB_COPY_FROM_PARENT, XCB_COPY_FROM_PARENT,
217 XCB_WINDOW_CLASS_INPUT_OUTPUT, (orientation == HORIZ ? XCURSOR_CURSOR_RESIZE_HORIZONTAL : XCURSOR_CURSOR_RESIZE_VERTICAL), false, mask, values);
218
219 if (!use_threshold) {
220 xcb_map_window(conn, helpwin);
221 if (orientation == HORIZ) {
222 xcb_warp_pointer(conn, XCB_NONE, event->root, 0, 0, 0, 0,
223 second->rect.x, event->root_y);
224 } else {
225 xcb_warp_pointer(conn, XCB_NONE, event->root, 0, 0, 0, 0,
226 event->root_x, second->rect.y);
227 }
228 }
229
230 xcb_circulate_window(conn, XCB_CIRCULATE_RAISE_LOWEST, helpwin);
231
232 xcb_flush(conn);
233
234 /* `new_position' will be updated by the `resize_callback'. */
235 new_position = initial_position;
236
237 bool threshold_exceeded = !use_threshold;
238
239 const struct callback_params params = {orientation, output, helpwin, &new_position, &threshold_exceeded};
240
241 /* Re-render the tree before returning to the event loop (drag_pointer()
242 * runs its own event-loop) in case if there are unrendered updates. */
243 tree_render();
244
245 /* `drag_pointer' blocks until the drag is completed. */
246 drag_result_t drag_result = drag_pointer(NULL, event, grabwin, 0, use_threshold, resize_callback, ¶ms);
247
248 xcb_destroy_window(conn, helpwin);
249 xcb_destroy_window(conn, grabwin);
250 xcb_flush(conn);
251
252 /* User cancelled the drag so no action should be taken. */
253 if (drag_result == DRAG_REVERT) {
254 return;
255 }
256
257 int pixels = (new_position - initial_position);
258 DLOG("Done, pixels = %d\n", pixels);
259
260 /* No change; no action needed. */
261 if (pixels == 0) {
262 return;
263 }
264
265 /* if we got thus far, the containers must have valid percentages. */
266 assert(first->percent > 0.0);
267 assert(second->percent > 0.0);
268 const bool result = resize_neighboring_cons(first, second, pixels, 0);
269 DLOG("Graphical resize %s: first->percent = %f, second->percent = %f.\n",
270 result ? "successful" : "failed", first->percent, second->percent);
271 }
272