i3
resize.c
Go to the documentation of this file.
1/*
2 * vim:ts=4:sw=4:expandtab
3 *
4 * i3 - an improved 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 */
26
27DRAGGING_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
57 *(params->new_position) = new_x;
58 xcb_configure_window(conn, params->helpwin, XCB_CONFIG_WINDOW_X, params->new_position);
59 } else {
60 if (new_y > (output->rect.y + output->rect.height - 25) ||
61 new_y < (output->rect.y + 25)) {
62 return;
63 }
64
65 *(params->new_position) = new_y;
66 xcb_configure_window(conn, params->helpwin, XCB_CONFIG_WINDOW_Y, params->new_position);
67 }
68
69 xcb_flush(conn);
70}
71
72bool resize_find_tiling_participants(Con **current, Con **other, direction_t direction, bool both_sides) {
73 DLOG("Find two participants for resizing container=%p in direction=%i\n", other, direction);
74 Con *first = *current;
75 Con *second = NULL;
76 if (first == NULL) {
77 DLOG("Current container is NULL, aborting.\n");
78 return false;
79 }
80
81 /* Go up in the tree and search for a container to resize */
82 const orientation_t search_orientation = orientation_from_direction(direction);
83 const bool dir_backwards = (direction == D_UP || direction == D_LEFT);
84 while (first->type != CT_WORKSPACE &&
85 first->type != CT_FLOATING_CON &&
86 second == NULL) {
87 /* get the appropriate first container with the matching
88 * orientation (skip stacked/tabbed cons) */
89 if ((con_orientation(first->parent) != search_orientation) ||
90 (first->parent->layout == L_STACKED) ||
91 (first->parent->layout == L_TABBED)) {
92 first = first->parent;
93 continue;
94 }
95
96 /* get the counterpart for this resizement */
97 if (dir_backwards) {
98 second = TAILQ_PREV(first, nodes_head, nodes);
99 if (second == NULL && both_sides == true) {
100 second = TAILQ_NEXT(first, nodes);
101 }
102 } else {
103 second = TAILQ_NEXT(first, nodes);
104 if (second == NULL && both_sides == true) {
105 second = TAILQ_PREV(first, nodes_head, nodes);
106 }
107 }
108
109 if (second == NULL) {
110 DLOG("No second container in this direction found, trying to look further up in the tree...\n");
111 first = first->parent;
112 }
113 }
114
115 DLOG("Found participants: first=%p and second=%p.\n", first, second);
116 *current = first;
117 *other = second;
118 if (first == NULL || second == NULL) {
119 DLOG("Could not find two participants for this resize request.\n");
120 return false;
121 }
122
123 return true;
124}
125
126/*
127 * Calculate the minimum percent needed for the given container to be at least 1
128 * pixel.
129 *
130 */
131double percent_for_1px(Con *con) {
132 const int parent_size = con_rect_size_in_orientation(con->parent);
133 /* deco_rect.height is subtracted from each child in render_con_split */
134 const int min_size = (con_orientation(con->parent) == HORIZ ? 1 : 1 + con->deco_rect.height);
135 return ((double)min_size / (double)parent_size);
136}
137
138/*
139 * Resize the two given containers using the given amount of pixels or
140 * percentage points. One of the two needs to be 0. A positive amount means
141 * growing the first container while a negative means shrinking it.
142 * Returns false when the resize would result in one of the two containers
143 * having less than 1 pixel of size.
144 *
145 */
146bool resize_neighboring_cons(Con *first, Con *second, int px, int ppt) {
147 assert(px * ppt == 0);
148
149 Con *parent = first->parent;
150 double new_first_percent;
151 double new_second_percent;
152 if (ppt) {
153 new_first_percent = first->percent + ((double)ppt / 100.0);
154 new_second_percent = second->percent - ((double)ppt / 100.0);
155 } else {
156 /* Convert px change to change in percentages */
157 const double pct = (double)px / (double)con_rect_size_in_orientation(first->parent);
158 new_first_percent = first->percent + pct;
159 new_second_percent = second->percent - pct;
160 }
161 /* Ensure that no container will be less than 1 pixel in the resizing
162 * direction. */
163 if (new_first_percent < percent_for_1px(first) || new_second_percent < percent_for_1px(second)) {
164 return false;
165 }
166
167 first->percent = new_first_percent;
168 second->percent = new_second_percent;
169 con_fix_percent(parent);
170 return true;
171}
172
174 const xcb_button_press_event_t *event,
175 bool use_threshold) {
176 Con *output = con_get_output(first);
177 DLOG("x = %d, width = %d\n", output->rect.x, output->rect.width);
178 DLOG("first = %p / %s\n", first, first->name);
179 DLOG("second = %p / %s\n", second, second->name);
180
181 x_mask_event_mask(~XCB_EVENT_MASK_ENTER_WINDOW);
182 xcb_flush(conn);
183
184 uint32_t mask = 0;
185 uint32_t values[2];
186
187 mask = XCB_CW_OVERRIDE_REDIRECT;
188 values[0] = 1;
189
190 /* Open a new window, the resizebar. Grab the pointer and move the window
191 * around as the user moves the pointer. */
192 xcb_window_t grabwin = create_window(conn, output->rect, XCB_COPY_FROM_PARENT, XCB_COPY_FROM_PARENT,
193 XCB_WINDOW_CLASS_INPUT_ONLY, XCURSOR_CURSOR_POINTER, true, mask, values);
194
195 /* Keep track of the coordinate orthogonal to motion so we can determine the
196 * length of the resize afterward. */
197 uint32_t initial_position, new_position;
198
199 /* Configure the resizebar and snap the pointer. The resizebar runs along
200 * the rect of the second con and follows the motion of the pointer. */
201 Rect helprect;
202 helprect.x = second->rect.x;
203 helprect.y = second->rect.y;
204 /* Resizes might happen between a split container and a leaf
205 * container. Because gaps happen *within* a split container, we need to
206 * work with (any) leaf window inside the split, so descend focused. */
207 Con *ffirst = con_descend_focused(first);
208 Con *fsecond = con_descend_focused(second);
209 if (orientation == HORIZ) {
210 helprect.width = logical_px(2);
211 helprect.height = second->rect.height;
212 const uint32_t ffirst_right = ffirst->rect.x + ffirst->rect.width;
213 const uint32_t gap = (fsecond->rect.x - ffirst_right);
214 const uint32_t middle = fsecond->rect.x - (gap / 2);
215 DLOG("ffirst->rect = {.x = %u, .width = %u}\n", ffirst->rect.x, ffirst->rect.width);
216 DLOG("fsecond->rect = {.x = %u, .width = %u}\n", fsecond->rect.x, fsecond->rect.width);
217 DLOG("gap = %u, middle = %u\n", gap, middle);
218 initial_position = middle;
219 } else {
220 helprect.width = second->rect.width;
221 helprect.height = logical_px(2);
222 const uint32_t ffirst_bottom = ffirst->rect.y + ffirst->rect.height;
223 const uint32_t gap = (fsecond->rect.y - ffirst_bottom);
224 const uint32_t middle = fsecond->rect.y - (gap / 2);
225 DLOG("ffirst->rect = {.y = %u, .height = %u}\n", ffirst->rect.y, ffirst->rect.height);
226 DLOG("fsecond->rect = {.y = %u, .height = %u}\n", fsecond->rect.y, fsecond->rect.height);
227 DLOG("gap = %u, middle = %u\n", gap, middle);
228 initial_position = middle;
229 }
230
231 mask = XCB_CW_BACK_PIXEL;
233
234 mask |= XCB_CW_OVERRIDE_REDIRECT;
235 values[1] = 1;
236
237 xcb_window_t helpwin = create_window(conn, helprect, XCB_COPY_FROM_PARENT, XCB_COPY_FROM_PARENT,
238 XCB_WINDOW_CLASS_INPUT_OUTPUT, (orientation == HORIZ ? XCURSOR_CURSOR_RESIZE_HORIZONTAL : XCURSOR_CURSOR_RESIZE_VERTICAL), false, mask, values);
239
240 if (!use_threshold) {
241 xcb_map_window(conn, helpwin);
242 if (orientation == HORIZ) {
243 xcb_warp_pointer(conn, XCB_NONE, event->root, 0, 0, 0, 0,
244 initial_position, event->root_y);
245 } else {
246 xcb_warp_pointer(conn, XCB_NONE, event->root, 0, 0, 0, 0,
247 event->root_x, initial_position);
248 }
249 }
250
251 xcb_circulate_window(conn, XCB_CIRCULATE_RAISE_LOWEST, helpwin);
252
253 xcb_flush(conn);
254
255 /* `new_position' will be updated by the `resize_callback'. */
256 new_position = initial_position;
257
258 bool threshold_exceeded = !use_threshold;
259
261
262 /* Re-render the tree before returning to the event loop (drag_pointer()
263 * runs its own event-loop) in case if there are unrendered updates. */
264 tree_render();
265
266 /* `drag_pointer' blocks until the drag is completed. */
267 drag_result_t drag_result = drag_pointer(NULL, event, grabwin, 0, use_threshold, resize_callback, &params);
268
269 xcb_destroy_window(conn, helpwin);
270 xcb_destroy_window(conn, grabwin);
271 xcb_flush(conn);
272
273 /* User cancelled the drag so no action should be taken. */
274 if (drag_result == DRAG_REVERT) {
275 return;
276 }
277
278 int pixels = (new_position - initial_position);
279 DLOG("Done, pixels = %d\n", pixels);
280
281 /* No change; no action needed. */
282 if (pixels == 0) {
283 return;
284 }
285
286 /* if we got thus far, the containers must have valid percentages. */
287 assert(first->percent > 0.0);
288 assert(second->percent > 0.0);
289 const bool result = resize_neighboring_cons(first, second, pixels, 0);
290 DLOG("Graphical resize %s: first->percent = %f, second->percent = %f.\n",
291 result ? "successful" : "failed", first->percent, second->percent);
292}
orientation_t con_orientation(Con *con)
Returns the orientation of the given container (for stacked containers, vertical orientation is used ...
Definition con.c:1625
uint32_t con_rect_size_in_orientation(Con *con)
Returns given container's rect size depending on its orientation.
Definition con.c:2707
void con_fix_percent(Con *con)
Updates the percent attribute of the children of the given container.
Definition con.c:1144
Con * con_get_output(Con *con)
Gets the output container (first container with CT_OUTPUT in hierarchy) this node is on.
Definition con.c:532
Con * con_descend_focused(Con *con)
Returns the focused con inside this client, descending the tree as far as possible.
Definition con.c:1698
Config config
Definition config.c:19
static bool threshold_exceeded(uint32_t x1, uint32_t y1, uint32_t x2, uint32_t y2)
Definition drag.c:43
drag_result_t drag_pointer(Con *con, const xcb_button_press_event_t *event, xcb_window_t confine_to, int cursor, bool use_threshold, callback_t callback, const void *extra)
This function grabs your pointer and keyboard and lets you drag stuff around (borders).
Definition drag.c:176
bool resize_find_tiling_participants(Con **current, Con **other, direction_t direction, bool both_sides)
Definition resize.c:72
void resize_graphical_handler(Con *first, Con *second, orientation_t orientation, const xcb_button_press_event_t *event, bool use_threshold)
Definition resize.c:173
double percent_for_1px(Con *con)
Calculate the minimum percent needed for the given container to be at least 1 pixel.
Definition resize.c:131
bool resize_neighboring_cons(Con *first, Con *second, int px, int ppt)
Resize the two given containers using the given amount of pixels or percentage points.
Definition resize.c:146
void tree_render(void)
Renders the tree, that is rendering all outputs using render_con() and pushing the changes to X11 usi...
Definition tree.c:455
orientation_t orientation_from_direction(direction_t direction)
Convert a direction to its corresponding orientation.
Definition util.c:466
void x_mask_event_mask(uint32_t mask)
Applies the given mask to the event mask of every i3 window decoration X11 window.
Definition x.c:1557
xcb_window_t create_window(xcb_connection_t *conn, Rect dims, uint16_t depth, xcb_visualid_t visual, uint16_t window_class, enum xcursor_cursor_t cursor, bool map, uint32_t mask, uint32_t *values)
Convenience wrapper around xcb_create_window which takes care of depth, generating an ID and checking...
Definition xcb.c:19
xcb_connection_t * conn
XCB connection and root screen.
Definition main.c:54
@ L_STACKED
Definition data.h:103
@ L_TABBED
Definition data.h:104
orientation_t
Definition data.h:60
@ HORIZ
Definition data.h:61
direction_t
Definition data.h:56
@ D_LEFT
Definition data.h:56
@ D_UP
Definition data.h:58
#define DRAGGING_CB(name)
Macro to create a callback function for dragging.
Definition drag.h:19
drag_result_t
This is the return value of a drag operation like drag_pointer.
Definition drag.h:39
@ DRAG_REVERT
Definition drag.h:42
int logical_px(const int logical)
Convert a logical amount of pixels (e.g.
#define DLOG(fmt,...)
Definition libi3.h:105
#define TAILQ_PREV(elm, headname, field)
Definition queue.h:342
#define TAILQ_NEXT(elm, field)
Definition queue.h:338
@ XCURSOR_CURSOR_RESIZE_HORIZONTAL
Definition xcursor.h:18
@ XCURSOR_CURSOR_RESIZE_VERTICAL
Definition xcursor.h:19
@ XCURSOR_CURSOR_POINTER
Definition xcursor.h:17
xcb_window_t helpwin
Definition resize.c:22
bool * threshold_exceeded
Definition resize.c:24
uint32_t * new_position
Definition resize.c:23
Con * output
Definition resize.c:21
orientation_t orientation
Definition resize.c:20
direction_t * direction
color_t border
struct Config::config_client client
struct Colortriple focused
Stores a rectangle, for example the size of a window, the child window etc.
Definition data.h:185
uint32_t height
Definition data.h:189
uint32_t x
Definition data.h:186
uint32_t y
Definition data.h:187
uint32_t width
Definition data.h:188
A 'Con' represents everything from the X11 root window down to a single X11 window.
Definition data.h:643
struct Con * parent
Definition data.h:678
struct Rect deco_rect
Definition data.h:688
enum Con::@18 type
double percent
Definition data.h:712
struct Rect rect
Definition data.h:682
layout_t layout
Definition data.h:755
char * name
Definition data.h:692
uint32_t colorpixel
Definition libi3.h:433