4 * i3 - an improved dynamic tiling window manager
6 * © 2009-2010 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"
34 * Renders the resize window between the first/second container and resizes
35 * the table column/row.
38 int resize_graphical_handler(xcb_connection_t *conn, Workspace *ws, int first, int second,
39 resize_orientation_t orientation, xcb_button_press_event_t *event) {
41 struct xoutput *screen = get_output_containing(event->root_x, event->root_y);
43 ELOG("BUG: No screen found at this position (%d, %d)\n", event->root_x, event->root_y);
47 /* We cannot use the X root window's width_in_pixels or height_in_pixels
48 * attributes here since they are not updated when you configure new
49 * screens during runtime. Instead, we just use the most right and most
50 * bottom Xinerama screen and use their position + width/height to get
51 * the area of pixels currently in use */
52 struct xoutput *most_right = get_output_most(D_RIGHT, screen),
53 *most_bottom = get_output_most(D_DOWN, screen);
55 DLOG("event->event_x = %d, event->root_x = %d\n", event->event_x, event->root_x);
57 DLOG("Screen dimensions: (%d, %d) %d x %d\n", screen->rect.x, screen->rect.y, screen->rect.width, screen->rect.height);
62 mask = XCB_CW_OVERRIDE_REDIRECT;
65 /* Open a new window, the resizebar. Grab the pointer and move the window around
66 as the user moves the pointer. */
69 most_right->rect.x + most_right->rect.width,
70 most_bottom->rect.x + most_bottom->rect.height};
71 xcb_window_t grabwin = create_window(conn, grabrect, XCB_WINDOW_CLASS_INPUT_ONLY, -1, true, mask, values);
74 if (orientation == O_VERTICAL) {
75 helprect.x = event->root_x;
76 helprect.y = screen->rect.y;
78 helprect.height = screen->rect.height;
79 new_position = event->root_x;
81 helprect.x = screen->rect.x;
82 helprect.y = event->root_y;
83 helprect.width = screen->rect.width;
85 new_position = event->root_y;
88 mask = XCB_CW_BACK_PIXEL;
89 values[0] = config.client.focused.border;
91 mask |= XCB_CW_OVERRIDE_REDIRECT;
94 xcb_window_t helpwin = create_window(conn, helprect, XCB_WINDOW_CLASS_INPUT_OUTPUT,
95 (orientation == O_VERTICAL ?
96 XCB_CURSOR_SB_H_DOUBLE_ARROW :
97 XCB_CURSOR_SB_V_DOUBLE_ARROW), true, mask, values);
99 xcb_circulate_window(conn, XCB_CIRCULATE_RAISE_LOWEST, helpwin);
103 void resize_callback(Rect *old_rect, uint32_t new_x, uint32_t new_y) {
104 DLOG("new x = %d, y = %d\n", new_x, new_y);
105 if (orientation == O_VERTICAL) {
106 /* Check if the new coordinates are within screen boundaries */
107 if (new_x > (screen->rect.x + screen->rect.width - 25) ||
108 new_x < (screen->rect.x + 25))
111 values[0] = new_position = new_x;
112 xcb_configure_window(conn, helpwin, XCB_CONFIG_WINDOW_X, values);
114 if (new_y > (screen->rect.y + screen->rect.height - 25) ||
115 new_y < (screen->rect.y + 25))
118 values[0] = new_position = new_y;
119 xcb_configure_window(conn, helpwin, XCB_CONFIG_WINDOW_Y, values);
125 drag_pointer(conn, NULL, event, grabwin, BORDER_TOP, resize_callback);
127 xcb_destroy_window(conn, helpwin);
128 xcb_destroy_window(conn, grabwin);
132 if (orientation == O_VERTICAL)
133 pixels = (new_position - event->root_x);
134 else pixels = (new_position - event->root_y);
135 resize_container(conn, ws, first, second, orientation, pixels);
141 * Resizes a column/row by the given amount of pixels. Called by
142 * resize_graphical_handler (the user clicked) or parse_resize_command (the
143 * user issued the command)
146 void resize_container(xcb_connection_t *conn, Workspace *ws, int first, int second,
147 resize_orientation_t orientation, int pixels) {
149 /* TODO: refactor this, both blocks are very identical */
150 if (orientation == O_VERTICAL) {
151 int default_width = ws->rect.width / ws->cols;
152 int old_unoccupied_x = get_unoccupied_x(ws);
154 /* We pre-calculate the unoccupied space to see if we need to adapt sizes before
155 * doing the resize */
156 int new_unoccupied_x = old_unoccupied_x;
158 if (old_unoccupied_x == 0)
159 old_unoccupied_x = ws->rect.width;
161 if (ws->width_factor[first] == 0)
162 new_unoccupied_x += default_width;
164 if (ws->width_factor[second] == 0)
165 new_unoccupied_x += default_width;
168 DLOG("old = %d, new = %d\n", old_unoccupied_x, new_unoccupied_x);
170 int cols_without_wf = 0;
171 int old_width, old_second_width;
172 for (int col = 0; col < ws->cols; col++)
173 if (ws->width_factor[col] == 0)
176 DLOG("old_unoccupied_x = %d\n", old_unoccupied_x);
178 DLOG("Updating first (before = %f)\n", ws->width_factor[first]);
179 /* Convert 0 (for default width_factor) to actual numbers */
180 if (ws->width_factor[first] == 0)
181 old_width = (old_unoccupied_x / max(cols_without_wf, 1));
182 else old_width = ws->width_factor[first] * old_unoccupied_x;
184 DLOG("second (before = %f)\n", ws->width_factor[second]);
185 if (ws->width_factor[second] == 0)
186 old_second_width = (old_unoccupied_x / max(cols_without_wf, 1));
187 else old_second_width = ws->width_factor[second] * old_unoccupied_x;
189 DLOG("middle = %f\n", ws->width_factor[first]);
191 /* If the space used for customly resized columns has changed we need to adapt the
192 * other customly resized columns, if any */
193 if (new_unoccupied_x != old_unoccupied_x)
194 for (int col = 0; col < ws->cols; col++) {
195 if (ws->width_factor[col] == 0)
198 DLOG("Updating other column (%d) (current width_factor = %f)\n", col, ws->width_factor[col]);
199 ws->width_factor[col] = (ws->width_factor[col] * old_unoccupied_x) / new_unoccupied_x;
200 DLOG("to %f\n", ws->width_factor[col]);
203 DLOG("Updating first (before = %f)\n", ws->width_factor[first]);
204 /* Convert 0 (for default width_factor) to actual numbers */
205 if (ws->width_factor[first] == 0)
206 ws->width_factor[first] = ((float)ws->rect.width / ws->cols) / new_unoccupied_x;
208 DLOG("first->width = %d, pixels = %d\n", old_width, pixels);
209 ws->width_factor[first] *= (float)(old_width + pixels) / old_width;
210 DLOG("-> %f\n", ws->width_factor[first]);
213 DLOG("Updating second (before = %f)\n", ws->width_factor[second]);
214 if (ws->width_factor[second] == 0)
215 ws->width_factor[second] = ((float)ws->rect.width / ws->cols) / new_unoccupied_x;
217 DLOG("middle = %f\n", ws->width_factor[second]);
218 DLOG("second->width = %d, pixels = %d\n", old_second_width, pixels);
219 ws->width_factor[second] *= (float)(old_second_width - pixels) / old_second_width;
220 DLOG("-> %f\n", ws->width_factor[second]);
222 DLOG("new unoccupied_x = %d\n", get_unoccupied_x(ws));
226 int ws_height = workspace_height(ws);
227 int default_height = ws_height / ws->rows;
228 int old_unoccupied_y = get_unoccupied_y(ws);
230 /* We pre-calculate the unoccupied space to see if we need to adapt sizes before
231 * doing the resize */
232 int new_unoccupied_y = old_unoccupied_y;
234 if (old_unoccupied_y == 0)
235 old_unoccupied_y = ws_height;
237 if (ws->height_factor[first] == 0)
238 new_unoccupied_y += default_height;
240 if (ws->height_factor[second] == 0)
241 new_unoccupied_y += default_height;
243 int cols_without_hf = 0;
244 int old_height, old_second_height;
245 for (int row = 0; row < ws->rows; row++)
246 if (ws->height_factor[row] == 0)
249 DLOG("old_unoccupied_y = %d\n", old_unoccupied_y);
251 DLOG("Updating first (before = %f)\n", ws->height_factor[first]);
252 /* Convert 0 (for default width_factor) to actual numbers */
253 if (ws->height_factor[first] == 0)
254 old_height = (old_unoccupied_y / max(cols_without_hf, 1));
255 else old_height = ws->height_factor[first] * old_unoccupied_y;
257 DLOG("second (before = %f)\n", ws->height_factor[second]);
258 if (ws->height_factor[second] == 0)
259 old_second_height = (old_unoccupied_y / max(cols_without_hf, 1));
260 else old_second_height = ws->height_factor[second] * old_unoccupied_y;
262 DLOG("middle = %f\n", ws->height_factor[first]);
266 DLOG("old = %d, new = %d\n", old_unoccupied_y, new_unoccupied_y);
268 /* If the space used for customly resized columns has changed we need to adapt the
269 * other customly resized columns, if any */
270 if (new_unoccupied_y != old_unoccupied_y)
271 for (int row = 0; row < ws->rows; row++) {
272 if (ws->height_factor[row] == 0)
275 DLOG("Updating other column (%d) (current width_factor = %f)\n", row, ws->height_factor[row]);
276 ws->height_factor[row] = (ws->height_factor[row] * old_unoccupied_y) / new_unoccupied_y;
277 DLOG("to %f\n", ws->height_factor[row]);
281 DLOG("Updating first (before = %f)\n", ws->height_factor[first]);
282 /* Convert 0 (for default width_factor) to actual numbers */
283 if (ws->height_factor[first] == 0)
284 ws->height_factor[first] = ((float)ws_height / ws->rows) / new_unoccupied_y;
286 DLOG("first->width = %d, pixels = %d\n", old_height, pixels);
287 ws->height_factor[first] *= (float)(old_height + pixels) / old_height;
288 DLOG("-> %f\n", ws->height_factor[first]);
291 DLOG("Updating second (before = %f)\n", ws->height_factor[second]);
292 if (ws->height_factor[second] == 0)
293 ws->height_factor[second] = ((float)ws_height / ws->rows) / new_unoccupied_y;
294 DLOG("middle = %f\n", ws->height_factor[second]);
295 DLOG("second->width = %d, pixels = %d\n", old_second_height, pixels);
296 ws->height_factor[second] *= (float)(old_second_height - pixels) / old_second_height;
297 DLOG("-> %f\n", ws->height_factor[second]);
299 DLOG("new unoccupied_y = %d\n", get_unoccupied_y(ws));