4 * i3 - an improved dynamic tiling window manager
6 * © 2009 Michael Stapelberg and contributors
8 * See file LICENSE for license information.
10 * This file contains the functions for resizing table columns/rows because
11 * it’s actually lots of work, compared to the other handlers.
18 #include <xcb/xcb_event.h>
30 #include "workspace.h"
33 * Renders the resize window between the first/second container and resizes
34 * the table column/row.
37 int resize_graphical_handler(xcb_connection_t *conn, Workspace *ws, int first, int second,
38 resize_orientation_t orientation, xcb_button_press_event_t *event) {
40 i3Screen *screen = get_screen_containing(event->root_x, event->root_y);
42 LOG("BUG: No screen found at this position (%d, %d)\n", event->root_x, event->root_y);
46 /* We cannot use the X root window's width_in_pixels or height_in_pixels
47 * attributes here since they are not updated when you configure new
48 * screens during runtime. Instead, we just use the most right and most
49 * bottom Xinerama screen and use their position + width/height to get
50 * the area of pixels currently in use */
51 i3Screen *most_right = get_screen_most(D_RIGHT, screen),
52 *most_bottom = get_screen_most(D_DOWN, screen);
54 LOG("event->event_x = %d, event->root_x = %d\n", event->event_x, event->root_x);
56 LOG("Screen dimensions: (%d, %d) %d x %d\n", screen->rect.x, screen->rect.y, screen->rect.width, screen->rect.height);
61 mask = XCB_CW_OVERRIDE_REDIRECT;
64 /* Open a new window, the resizebar. Grab the pointer and move the window around
65 as the user moves the pointer. */
68 most_right->rect.x + most_right->rect.width,
69 most_bottom->rect.x + most_bottom->rect.height};
70 xcb_window_t grabwin = create_window(conn, grabrect, XCB_WINDOW_CLASS_INPUT_ONLY, -1, true, mask, values);
73 if (orientation == O_VERTICAL) {
74 helprect.x = event->root_x;
75 helprect.y = screen->rect.y;
77 helprect.height = screen->rect.height;
78 new_position = event->root_x;
80 helprect.x = screen->rect.x;
81 helprect.y = event->root_y;
82 helprect.width = screen->rect.width;
84 new_position = event->root_y;
87 mask = XCB_CW_BACK_PIXEL;
88 values[0] = config.client.focused.border;
90 mask |= XCB_CW_OVERRIDE_REDIRECT;
93 xcb_window_t helpwin = create_window(conn, helprect, XCB_WINDOW_CLASS_INPUT_OUTPUT,
94 (orientation == O_VERTICAL ?
95 XCB_CURSOR_SB_V_DOUBLE_ARROW :
96 XCB_CURSOR_SB_H_DOUBLE_ARROW), true, mask, values);
98 xcb_circulate_window(conn, XCB_CIRCULATE_RAISE_LOWEST, helpwin);
102 void resize_callback(Rect *old_rect, uint32_t new_x, uint32_t new_y) {
103 LOG("new x = %d, y = %d\n", new_x, new_y);
104 if (orientation == O_VERTICAL) {
105 /* Check if the new coordinates are within screen boundaries */
106 if (new_x > (screen->rect.x + screen->rect.width - 25) ||
107 new_x < (screen->rect.x + 25))
110 values[0] = new_position = new_x;
111 xcb_configure_window(conn, helpwin, XCB_CONFIG_WINDOW_X, values);
113 if (new_y > (screen->rect.y + screen->rect.height - 25) ||
114 new_y < (screen->rect.y + 25))
117 values[0] = new_position = new_y;
118 xcb_configure_window(conn, helpwin, XCB_CONFIG_WINDOW_Y, values);
124 drag_pointer(conn, NULL, event, grabwin, BORDER_TOP, resize_callback);
126 xcb_destroy_window(conn, helpwin);
127 xcb_destroy_window(conn, grabwin);
131 if (orientation == O_VERTICAL)
132 pixels = (new_position - event->root_x);
133 else pixels = (new_position - event->root_y);
134 resize_container(conn, ws, first, second, orientation, pixels);
140 * Resizes a column/row by the given amount of pixels. Called by
141 * resize_graphical_handler (the user clicked) or parse_resize_command (the
142 * user issued the command)
145 void resize_container(xcb_connection_t *conn, Workspace *ws, int first, int second,
146 resize_orientation_t orientation, int pixels) {
148 /* TODO: refactor this, both blocks are very identical */
149 if (orientation == O_VERTICAL) {
150 int default_width = ws->rect.width / ws->cols;
151 int old_unoccupied_x = get_unoccupied_x(ws);
153 /* We pre-calculate the unoccupied space to see if we need to adapt sizes before
154 * doing the resize */
155 int new_unoccupied_x = old_unoccupied_x;
157 if (old_unoccupied_x == 0)
158 old_unoccupied_x = ws->rect.width;
160 if (ws->width_factor[first] == 0)
161 new_unoccupied_x += default_width;
163 if (ws->width_factor[second] == 0)
164 new_unoccupied_x += default_width;
167 LOG("old = %d, new = %d\n", old_unoccupied_x, new_unoccupied_x);
169 /* If the space used for customly resized columns has changed we need to adapt the
170 * other customly resized columns, if any */
171 if (new_unoccupied_x != old_unoccupied_x)
172 for (int col = 0; col < ws->cols; col++) {
173 if (ws->width_factor[col] == 0)
176 LOG("Updating other column (%d) (current width_factor = %f)\n", col, ws->width_factor[col]);
177 ws->width_factor[col] = (ws->width_factor[col] * old_unoccupied_x) / new_unoccupied_x;
178 LOG("to %f\n", ws->width_factor[col]);
181 LOG("old_unoccupied_x = %d\n", old_unoccupied_x);
183 LOG("Updating first (before = %f)\n", ws->width_factor[first]);
184 /* Convert 0 (for default width_factor) to actual numbers */
185 if (ws->width_factor[first] == 0)
186 ws->width_factor[first] = ((float)ws->rect.width / ws->cols) / new_unoccupied_x;
188 LOG("middle = %f\n", ws->width_factor[first]);
189 int old_width = ws->width_factor[first] * old_unoccupied_x;
190 LOG("first->width = %d, pixels = %d\n", pixels);
191 ws->width_factor[first] *= (float)(old_width + pixels) / old_width;
192 LOG("-> %f\n", ws->width_factor[first]);
195 LOG("Updating second (before = %f)\n", ws->width_factor[second]);
196 if (ws->width_factor[second] == 0)
197 ws->width_factor[second] = ((float)ws->rect.width / ws->cols) / new_unoccupied_x;
198 LOG("middle = %f\n", ws->width_factor[second]);
199 old_width = ws->width_factor[second] * old_unoccupied_x;
200 LOG("second->width = %d, pixels = %d\n", pixels);
201 ws->width_factor[second] *= (float)(old_width - pixels) / old_width;
202 LOG("-> %f\n", ws->width_factor[second]);
204 LOG("new unoccupied_x = %d\n", get_unoccupied_x(ws));
208 int ws_height = workspace_height(ws);
209 int default_height = ws_height / ws->rows;
210 int old_unoccupied_y = get_unoccupied_y(ws);
212 /* We pre-calculate the unoccupied space to see if we need to adapt sizes before
213 * doing the resize */
214 int new_unoccupied_y = old_unoccupied_y;
216 if (old_unoccupied_y == 0)
217 old_unoccupied_y = ws_height;
219 if (ws->height_factor[first] == 0)
220 new_unoccupied_y += default_height;
222 if (ws->height_factor[second] == 0)
223 new_unoccupied_y += default_height;
225 int cols_without_hf = 0;
226 int old_height, old_second_height;
227 for (int row = 0; row < ws->rows; row++)
228 if (ws->height_factor[row] == 0)
231 LOG("old_unoccupied_y = %d\n", old_unoccupied_y);
233 LOG("Updating first (before = %f)\n", ws->height_factor[first]);
234 /* Convert 0 (for default width_factor) to actual numbers */
235 if (ws->height_factor[first] == 0)
236 old_height = (old_unoccupied_y / max(cols_without_hf, 1));
237 else old_height = ws->height_factor[first] * old_unoccupied_y;
239 LOG("second (before = %f)\n", ws->height_factor[second]);
240 if (ws->height_factor[second] == 0)
241 old_second_height = (old_unoccupied_y / max(cols_without_hf, 1));
242 else old_second_height = ws->height_factor[second] * old_unoccupied_y;
244 LOG("middle = %f\n", ws->height_factor[first]);
248 LOG("old = %d, new = %d\n", old_unoccupied_y, new_unoccupied_y);
250 /* If the space used for customly resized columns has changed we need to adapt the
251 * other customly resized columns, if any */
252 if (new_unoccupied_y != old_unoccupied_y)
253 for (int row = 0; row < ws->rows; row++) {
254 if (ws->height_factor[row] == 0)
257 LOG("Updating other column (%d) (current width_factor = %f)\n", row, ws->height_factor[row]);
258 ws->height_factor[row] = (ws->height_factor[row] * old_unoccupied_y) / new_unoccupied_y;
259 LOG("to %f\n", ws->height_factor[row]);
263 LOG("Updating first (before = %f)\n", ws->height_factor[first]);
264 /* Convert 0 (for default width_factor) to actual numbers */
265 if (ws->height_factor[first] == 0)
266 ws->height_factor[first] = ((float)ws_height / ws->rows) / new_unoccupied_y;
268 LOG("first->width = %d, pixels = %d\n", old_height, pixels);
269 ws->height_factor[first] *= (float)(old_height + pixels) / old_height;
270 LOG("-> %f\n", ws->height_factor[first]);
273 LOG("Updating second (before = %f)\n", ws->height_factor[second]);
274 if (ws->height_factor[second] == 0)
275 ws->height_factor[second] = ((float)ws_height / ws->rows) / new_unoccupied_y;
276 LOG("middle = %f\n", ws->height_factor[second]);
277 LOG("second->width = %d, pixels = %d\n", old_second_height, pixels);
278 ws->height_factor[second] *= (float)(old_second_height - pixels) / old_second_height;
279 LOG("-> %f\n", ws->height_factor[second]);
281 LOG("new unoccupied_y = %d\n", get_unoccupied_y(ws));