i3
resize.c
Go to the documentation of this file.
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 */
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 *(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
70bool 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 */
129double 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 */
144bool 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
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 DLOG("first = %p / %s\n", first, first->name);
177 DLOG("second = %p / %s\n", second, second->name);
178
179 x_mask_event_mask(~XCB_EVENT_MASK_ENTER_WINDOW);
180 xcb_flush(conn);
181
182 uint32_t mask = 0;
183 uint32_t values[2];
184
185 mask = XCB_CW_OVERRIDE_REDIRECT;
186 values[0] = 1;
187
188 /* Open a new window, the resizebar. Grab the pointer and move the window
189 * around as the user moves the pointer. */
190 xcb_window_t grabwin = create_window(conn, output->rect, XCB_COPY_FROM_PARENT, XCB_COPY_FROM_PARENT,
191 XCB_WINDOW_CLASS_INPUT_ONLY, XCURSOR_CURSOR_POINTER, true, mask, values);
192
193 /* Keep track of the coordinate orthogonal to motion so we can determine the
194 * length of the resize afterward. */
195 uint32_t initial_position, new_position;
196
197 /* Configure the resizebar and snap the pointer. The resizebar runs along
198 * the rect of the second con and follows the motion of the pointer. */
199 Rect helprect;
200 helprect.x = second->rect.x;
201 helprect.y = second->rect.y;
202 /* Resizes might happen between a split container and a leaf
203 * container. Because gaps happen *within* a split container, we need to
204 * work with (any) leaf window inside the split, so descend focused. */
205 Con *ffirst = con_descend_focused(first);
206 Con *fsecond = con_descend_focused(second);
207 if (orientation == HORIZ) {
208 helprect.width = logical_px(2);
209 helprect.height = second->rect.height;
210 const uint32_t ffirst_right = ffirst->rect.x + ffirst->rect.width;
211 const uint32_t gap = (fsecond->rect.x - ffirst_right);
212 const uint32_t middle = fsecond->rect.x - (gap / 2);
213 DLOG("ffirst->rect = {.x = %u, .width = %u}\n", ffirst->rect.x, ffirst->rect.width);
214 DLOG("fsecond->rect = {.x = %u, .width = %u}\n", fsecond->rect.x, fsecond->rect.width);
215 DLOG("gap = %u, middle = %u\n", gap, middle);
216 initial_position = middle;
217 } else {
218 helprect.width = second->rect.width;
219 helprect.height = logical_px(2);
220 const uint32_t ffirst_bottom = ffirst->rect.y + ffirst->rect.height;
221 const uint32_t gap = (fsecond->rect.y - ffirst_bottom);
222 const uint32_t middle = fsecond->rect.y - (gap / 2);
223 DLOG("ffirst->rect = {.y = %u, .height = %u}\n", ffirst->rect.y, ffirst->rect.height);
224 DLOG("fsecond->rect = {.y = %u, .height = %u}\n", fsecond->rect.y, fsecond->rect.height);
225 DLOG("gap = %u, middle = %u\n", gap, middle);
226 initial_position = middle;
227 }
228
229 mask = XCB_CW_BACK_PIXEL;
231
232 mask |= XCB_CW_OVERRIDE_REDIRECT;
233 values[1] = 1;
234
235 xcb_window_t helpwin = create_window(conn, helprect, XCB_COPY_FROM_PARENT, XCB_COPY_FROM_PARENT,
236 XCB_WINDOW_CLASS_INPUT_OUTPUT, (orientation == HORIZ ? XCURSOR_CURSOR_RESIZE_HORIZONTAL : XCURSOR_CURSOR_RESIZE_VERTICAL), false, mask, values);
237
238 if (!use_threshold) {
239 xcb_map_window(conn, helpwin);
240 if (orientation == HORIZ) {
241 xcb_warp_pointer(conn, XCB_NONE, event->root, 0, 0, 0, 0,
242 initial_position, event->root_y);
243 } else {
244 xcb_warp_pointer(conn, XCB_NONE, event->root, 0, 0, 0, 0,
245 event->root_x, initial_position);
246 }
247 }
248
249 xcb_circulate_window(conn, XCB_CIRCULATE_RAISE_LOWEST, helpwin);
250
251 xcb_flush(conn);
252
253 /* `new_position' will be updated by the `resize_callback'. */
254 new_position = initial_position;
255
256 bool threshold_exceeded = !use_threshold;
257
259
260 /* Re-render the tree before returning to the event loop (drag_pointer()
261 * runs its own event-loop) in case if there are unrendered updates. */
262 tree_render();
263
264 /* `drag_pointer' blocks until the drag is completed. */
265 drag_result_t drag_result = drag_pointer(NULL, event, grabwin, 0, use_threshold, resize_callback, &params);
266
267 xcb_destroy_window(conn, helpwin);
268 xcb_destroy_window(conn, grabwin);
269 xcb_flush(conn);
270
271 /* User cancelled the drag so no action should be taken. */
272 if (drag_result == DRAG_REVERT) {
273 return;
274 }
275
276 int pixels = (new_position - initial_position);
277 DLOG("Done, pixels = %d\n", pixels);
278
279 /* No change; no action needed. */
280 if (pixels == 0) {
281 return;
282 }
283
284 /* if we got thus far, the containers must have valid percentages. */
285 assert(first->percent > 0.0);
286 assert(second->percent > 0.0);
287 const bool result = resize_neighboring_cons(first, second, pixels, 0);
288 DLOG("Graphical resize %s: first->percent = %f, second->percent = %f.\n",
289 result ? "successful" : "failed", first->percent, second->percent);
290}
orientation_t con_orientation(Con *con)
Returns the orientation of the given container (for stacked containers, vertical orientation is used ...
Definition con.c:1517
uint32_t con_rect_size_in_orientation(Con *con)
Returns given container's rect size depending on its orientation.
Definition con.c:2545
void con_fix_percent(Con *con)
Updates the percent attribute of the children of the given container.
Definition con.c:1047
Con * con_get_output(Con *con)
Gets the output container (first container with CT_OUTPUT in hierarchy) this node is on.
Definition con.c:463
Con * con_descend_focused(Con *con)
Returns the focused con inside this client, descending the tree as far as possible.
Definition con.c:1590
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:175
bool resize_find_tiling_participants(Con **current, Con **other, direction_t direction, bool both_sides)
Definition resize.c:70
void resize_graphical_handler(Con *first, Con *second, orientation_t orientation, const xcb_button_press_event_t *event, bool use_threshold)
Definition resize.c:171
double percent_for_1px(Con *con)
Calculate the minimum percent needed for the given container to be at least 1 pixel.
Definition resize.c:129
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:144
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:451
orientation_t orientation_from_direction(direction_t direction)
Convert a direction to its corresponding orientation.
Definition util.c:456
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:1488
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:107
@ L_TABBED
Definition data.h:108
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:189
uint32_t height
Definition data.h:193
uint32_t x
Definition data.h:190
uint32_t y
Definition data.h:191
uint32_t width
Definition data.h:192
A 'Con' represents everything from the X11 root window down to a single X11 window.
Definition data.h:647
struct Con * parent
Definition data.h:682
struct Rect deco_rect
Definition data.h:692
enum Con::@18 type
double percent
Definition data.h:716
struct Rect rect
Definition data.h:686
layout_t layout
Definition data.h:759
char * name
Definition data.h:696
uint32_t colorpixel
Definition libi3.h:433