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 */
22 xcb_window_t helpwin;
23 uint32_t *new_position;
25};
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
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;
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
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, &params);
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}
orientation_t con_orientation(Con *con)
Returns the orientation of the given container (for stacked containers, vertical orientation is used ...
Definition: con.c:1514
uint32_t con_rect_size_in_orientation(Con *con)
Returns given container's rect size depending on its orientation.
Definition: con.c:2501
void con_fix_percent(Con *con)
Updates the percent attribute of the children of the given container.
Definition: con.c:1044
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
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:174
xcb_connection_t * conn
XCB connection and root screen.
Definition: main.c:54
DRAGGING_CB(resize_callback)
Definition: resize.c:27
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:1472
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
@ L_STACKED
Definition: data.h:95
@ L_TABBED
Definition: data.h:96
orientation_t
Definition: data.h:59
@ HORIZ
Definition: data.h:60
direction_t
Definition: data.h:55
@ D_LEFT
Definition: data.h:55
@ D_UP
Definition: data.h:57
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
Definition: tiling_drag.c:70
color_t border
Definition: configuration.h:54
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:158
uint32_t height
Definition: data.h:162
uint32_t x
Definition: data.h:159
uint32_t y
Definition: data.h:160
uint32_t width
Definition: data.h:161
A 'Con' represents everything from the X11 root window down to a single X11 window.
Definition: data.h:614
struct Con * parent
Definition: data.h:646
struct Rect deco_rect
Definition: data.h:656
enum Con::@18 type
double percent
Definition: data.h:680
struct Rect rect
Definition: data.h:650
layout_t layout
Definition: data.h:723
uint32_t colorpixel
Definition: libi3.h:433