2 * vim:ts=4:sw=4:expandtab
4 * i3 - an improved dynamic tiling window manager
5 * © 2009 Michael Stapelberg and contributors (see also: LICENSE)
7 * resize.c: Interactive resizing.
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).
19 struct callback_params {
20 orientation_t orientation;
23 uint32_t *new_position;
26 DRAGGING_CB(resize_callback) {
27 const struct callback_params *params = extra;
28 Con *output = params->output;
29 DLOG("new x = %d, y = %d\n", new_x, new_y);
30 if (params->orientation == HORIZ) {
31 /* Check if the new coordinates are within screen boundaries */
32 if (new_x > (output->rect.x + output->rect.width - 25) ||
33 new_x < (output->rect.x + 25))
36 *(params->new_position) = new_x;
37 xcb_configure_window(conn, params->helpwin, XCB_CONFIG_WINDOW_X, params->new_position);
39 if (new_y > (output->rect.y + output->rect.height - 25) ||
40 new_y < (output->rect.y + 25))
43 *(params->new_position) = new_y;
44 xcb_configure_window(conn, params->helpwin, XCB_CONFIG_WINDOW_Y, params->new_position);
50 bool resize_find_tiling_participants(Con **current, Con **other, direction_t direction, bool both_sides) {
51 DLOG("Find two participants for resizing container=%p in direction=%i\n", other, direction);
52 Con *first = *current;
55 DLOG("Current container is NULL, aborting.\n");
59 /* Go up in the tree and search for a container to resize */
60 const orientation_t search_orientation = ((direction == D_LEFT || direction == D_RIGHT) ? HORIZ : VERT);
61 const bool dir_backwards = (direction == D_UP || direction == D_LEFT);
62 while (first->type != CT_WORKSPACE &&
63 first->type != CT_FLOATING_CON &&
65 /* get the appropriate first container with the matching
66 * orientation (skip stacked/tabbed cons) */
67 if ((con_orientation(first->parent) != search_orientation) ||
68 (first->parent->layout == L_STACKED) ||
69 (first->parent->layout == L_TABBED)) {
70 first = first->parent;
74 /* get the counterpart for this resizement */
76 second = TAILQ_PREV(first, nodes_head, nodes);
77 if (second == NULL && both_sides == true) {
78 second = TAILQ_NEXT(first, nodes);
81 second = TAILQ_NEXT(first, nodes);
82 if (second == NULL && both_sides == true) {
83 second = TAILQ_PREV(first, nodes_head, nodes);
88 DLOG("No second container in this direction found, trying to look further up in the tree...\n");
89 first = first->parent;
93 DLOG("Found participants: first=%p and second=%p.\n", first, second);
96 if (first == NULL || second == NULL) {
97 DLOG("Could not find two participants for this resize request.\n");
104 int resize_graphical_handler(Con *first, Con *second, orientation_t orientation, const xcb_button_press_event_t *event) {
105 DLOG("resize handler\n");
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);
111 x_mask_event_mask(~XCB_EVENT_MASK_ENTER_WINDOW);
117 mask = XCB_CW_OVERRIDE_REDIRECT;
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);
125 /* Keep track of the coordinate orthogonal to motion so we can determine
126 * the length of the resize afterward. */
127 uint32_t initial_position, new_position;
129 /* Configure the resizebar and snap the pointer. The resizebar runs along
130 * the rect of the second con and follows the motion of the pointer. */
132 if (orientation == HORIZ) {
133 helprect.x = second->rect.x;
134 helprect.y = second->rect.y;
135 helprect.width = logical_px(2);
136 helprect.height = second->rect.height;
137 initial_position = second->rect.x;
138 xcb_warp_pointer(conn, XCB_NONE, event->root, 0, 0, 0, 0,
139 second->rect.x, event->root_y);
141 helprect.x = second->rect.x;
142 helprect.y = second->rect.y;
143 helprect.width = second->rect.width;
144 helprect.height = logical_px(2);
145 initial_position = second->rect.y;
146 xcb_warp_pointer(conn, XCB_NONE, event->root, 0, 0, 0, 0,
147 event->root_x, second->rect.y);
150 mask = XCB_CW_BACK_PIXEL;
151 values[0] = config.client.focused.border.colorpixel;
153 mask |= XCB_CW_OVERRIDE_REDIRECT;
156 xcb_window_t helpwin = create_window(conn, helprect, XCB_COPY_FROM_PARENT, XCB_COPY_FROM_PARENT,
157 XCB_WINDOW_CLASS_INPUT_OUTPUT, (orientation == HORIZ ? XCURSOR_CURSOR_RESIZE_HORIZONTAL : XCURSOR_CURSOR_RESIZE_VERTICAL), true, mask, values);
159 xcb_circulate_window(conn, XCB_CIRCULATE_RAISE_LOWEST, helpwin);
163 /* `new_position' will be updated by the `resize_callback'. */
164 new_position = initial_position;
166 const struct callback_params params = {orientation, output, helpwin, &new_position};
168 /* `drag_pointer' blocks until the drag is completed. */
169 drag_result_t drag_result = drag_pointer(NULL, event, grabwin, BORDER_TOP, 0, resize_callback, ¶ms);
171 xcb_destroy_window(conn, helpwin);
172 xcb_destroy_window(conn, grabwin);
175 /* User cancelled the drag so no action should be taken. */
176 if (drag_result == DRAG_REVERT)
179 int pixels = (new_position - initial_position);
181 DLOG("Done, pixels = %d\n", pixels);
183 // if we got thus far, the containers must have
184 // percentages associated with them
185 assert(first->percent > 0.0);
186 assert(second->percent > 0.0);
188 // calculate the new percentage for the first container
189 double new_percent, difference;
190 double percent = first->percent;
191 DLOG("percent = %f\n", percent);
192 int original = (orientation == HORIZ ? first->rect.width : first->rect.height);
193 DLOG("original = %d\n", original);
194 new_percent = (original + pixels) * (percent / original);
195 difference = percent - new_percent;
196 DLOG("difference = %f\n", difference);
197 DLOG("new percent = %f\n", new_percent);
198 first->percent = new_percent;
200 // calculate the new percentage for the second container
201 double s_percent = second->percent;
202 second->percent = s_percent + difference;
203 DLOG("second->percent = %f\n", second->percent);
205 // now we must make sure that the sum of the percentages remain 1.0
206 con_fix_percent(first->parent);