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 * This is an ugly data structure which we need because there is no standard
35 * way of having nested functions (only available as a gcc extension at the
36 * moment, clang doesn’t support it) or blocks (only available as a clang
37 * extension and only on Mac OS X systems at the moment).
40 struct callback_params {
41 resize_orientation_t orientation;
44 uint32_t *new_position;
47 DRAGGING_CB(resize_callback) {
48 struct callback_params *params = extra;
49 Output *screen = params->screen;
50 DLOG("new x = %d, y = %d\n", new_x, new_y);
51 if (params->orientation == O_VERTICAL) {
52 /* Check if the new coordinates are within screen boundaries */
53 if (new_x > (screen->rect.x + screen->rect.width - 25) ||
54 new_x < (screen->rect.x + 25))
57 *(params->new_position) = new_x;
58 xcb_configure_window(conn, params->helpwin, XCB_CONFIG_WINDOW_X, params->new_position);
60 if (new_y > (screen->rect.y + screen->rect.height - 25) ||
61 new_y < (screen->rect.y + 25))
64 *(params->new_position) = new_y;
65 xcb_configure_window(conn, params->helpwin, XCB_CONFIG_WINDOW_Y, params->new_position);
72 * Renders the resize window between the first/second container and resizes
73 * the table column/row.
76 int resize_graphical_handler(xcb_connection_t *conn, Workspace *ws, int first, int second,
77 resize_orientation_t orientation, xcb_button_press_event_t *event) {
78 uint32_t new_position;
79 Output *screen = get_output_containing(event->root_x, event->root_y);
81 ELOG("BUG: No screen found at this position (%d, %d)\n", event->root_x, event->root_y);
85 /* We cannot use the X root window's width_in_pixels or height_in_pixels
86 * attributes here since they are not updated when you configure new
87 * screens during runtime. Instead, we just use the most right and most
88 * bottom Xinerama screen and use their position + width/height to get
89 * the area of pixels currently in use */
90 Output *most_right = get_output_most(D_RIGHT, screen),
91 *most_bottom = get_output_most(D_DOWN, screen);
93 DLOG("event->event_x = %d, event->root_x = %d\n", event->event_x, event->root_x);
95 DLOG("Screen dimensions: (%d, %d) %d x %d\n", screen->rect.x, screen->rect.y, screen->rect.width, screen->rect.height);
100 mask = XCB_CW_OVERRIDE_REDIRECT;
103 /* Open a new window, the resizebar. Grab the pointer and move the window around
104 as the user moves the pointer. */
107 most_right->rect.x + most_right->rect.width,
108 most_bottom->rect.x + most_bottom->rect.height};
109 xcb_window_t grabwin = create_window(conn, grabrect, XCB_WINDOW_CLASS_INPUT_ONLY, -1, true, mask, values);
112 if (orientation == O_VERTICAL) {
113 helprect.x = event->root_x;
114 helprect.y = screen->rect.y;
116 helprect.height = screen->rect.height;
117 new_position = event->root_x;
119 helprect.x = screen->rect.x;
120 helprect.y = event->root_y;
121 helprect.width = screen->rect.width;
123 new_position = event->root_y;
126 mask = XCB_CW_BACK_PIXEL;
127 values[0] = config.client.focused.border;
129 mask |= XCB_CW_OVERRIDE_REDIRECT;
132 xcb_window_t helpwin = create_window(conn, helprect, XCB_WINDOW_CLASS_INPUT_OUTPUT,
133 (orientation == O_VERTICAL ?
134 XCB_CURSOR_SB_H_DOUBLE_ARROW :
135 XCB_CURSOR_SB_V_DOUBLE_ARROW), true, mask, values);
137 xcb_circulate_window(conn, XCB_CIRCULATE_RAISE_LOWEST, helpwin);
141 struct callback_params params = { orientation, screen, helpwin, &new_position };
143 drag_pointer(conn, NULL, event, grabwin, BORDER_TOP, resize_callback, ¶ms);
145 xcb_destroy_window(conn, helpwin);
146 xcb_destroy_window(conn, grabwin);
150 if (orientation == O_VERTICAL)
151 pixels = (new_position - event->root_x);
152 else pixels = (new_position - event->root_y);
153 resize_container(conn, ws, first, second, orientation, pixels);
159 * Resizes a column/row by the given amount of pixels. Called by
160 * resize_graphical_handler (the user clicked) or parse_resize_command (the
161 * user issued the command)
164 void resize_container(xcb_connection_t *conn, Workspace *ws, int first, int second,
165 resize_orientation_t orientation, int pixels) {
167 /* TODO: refactor this, both blocks are very identical */
168 if (orientation == O_VERTICAL) {
169 int default_width = ws->rect.width / ws->cols;
170 int old_unoccupied_x = get_unoccupied_x(ws);
172 /* We pre-calculate the unoccupied space to see if we need to adapt sizes before
173 * doing the resize */
174 int new_unoccupied_x = old_unoccupied_x;
176 if (old_unoccupied_x == 0)
177 old_unoccupied_x = ws->rect.width;
179 if (ws->width_factor[first] == 0)
180 new_unoccupied_x += default_width;
182 if (ws->width_factor[second] == 0)
183 new_unoccupied_x += default_width;
186 DLOG("old = %d, new = %d\n", old_unoccupied_x, new_unoccupied_x);
188 int cols_without_wf = 0;
189 int old_width, old_second_width;
190 for (int col = 0; col < ws->cols; col++)
191 if (ws->width_factor[col] == 0)
194 DLOG("old_unoccupied_x = %d\n", old_unoccupied_x);
196 DLOG("Updating first (before = %f)\n", ws->width_factor[first]);
197 /* Convert 0 (for default width_factor) to actual numbers */
198 if (ws->width_factor[first] == 0)
199 old_width = (old_unoccupied_x / max(cols_without_wf, 1));
200 else old_width = ws->width_factor[first] * old_unoccupied_x;
202 DLOG("second (before = %f)\n", ws->width_factor[second]);
203 if (ws->width_factor[second] == 0)
204 old_second_width = (old_unoccupied_x / max(cols_without_wf, 1));
205 else old_second_width = ws->width_factor[second] * old_unoccupied_x;
207 DLOG("middle = %f\n", ws->width_factor[first]);
209 /* If the space used for customly resized columns has changed we need to adapt the
210 * other customly resized columns, if any */
211 if (new_unoccupied_x != old_unoccupied_x)
212 for (int col = 0; col < ws->cols; col++) {
213 if (ws->width_factor[col] == 0)
216 DLOG("Updating other column (%d) (current width_factor = %f)\n", col, ws->width_factor[col]);
217 ws->width_factor[col] = (ws->width_factor[col] * old_unoccupied_x) / new_unoccupied_x;
218 DLOG("to %f\n", ws->width_factor[col]);
221 DLOG("Updating first (before = %f)\n", ws->width_factor[first]);
222 /* Convert 0 (for default width_factor) to actual numbers */
223 if (ws->width_factor[first] == 0)
224 ws->width_factor[first] = ((float)ws->rect.width / ws->cols) / new_unoccupied_x;
226 DLOG("first->width = %d, pixels = %d\n", old_width, pixels);
227 ws->width_factor[first] *= (float)(old_width + pixels) / old_width;
228 DLOG("-> %f\n", ws->width_factor[first]);
231 DLOG("Updating second (before = %f)\n", ws->width_factor[second]);
232 if (ws->width_factor[second] == 0)
233 ws->width_factor[second] = ((float)ws->rect.width / ws->cols) / new_unoccupied_x;
235 DLOG("middle = %f\n", ws->width_factor[second]);
236 DLOG("second->width = %d, pixels = %d\n", old_second_width, pixels);
237 ws->width_factor[second] *= (float)(old_second_width - pixels) / old_second_width;
238 DLOG("-> %f\n", ws->width_factor[second]);
240 DLOG("new unoccupied_x = %d\n", get_unoccupied_x(ws));
244 int ws_height = workspace_height(ws);
245 int default_height = ws_height / ws->rows;
246 int old_unoccupied_y = get_unoccupied_y(ws);
248 /* We pre-calculate the unoccupied space to see if we need to adapt sizes before
249 * doing the resize */
250 int new_unoccupied_y = old_unoccupied_y;
252 if (old_unoccupied_y == 0)
253 old_unoccupied_y = ws_height;
255 if (ws->height_factor[first] == 0)
256 new_unoccupied_y += default_height;
258 if (ws->height_factor[second] == 0)
259 new_unoccupied_y += default_height;
261 int cols_without_hf = 0;
262 int old_height, old_second_height;
263 for (int row = 0; row < ws->rows; row++)
264 if (ws->height_factor[row] == 0)
267 DLOG("old_unoccupied_y = %d\n", old_unoccupied_y);
269 DLOG("Updating first (before = %f)\n", ws->height_factor[first]);
270 /* Convert 0 (for default width_factor) to actual numbers */
271 if (ws->height_factor[first] == 0)
272 old_height = (old_unoccupied_y / max(cols_without_hf, 1));
273 else old_height = ws->height_factor[first] * old_unoccupied_y;
275 DLOG("second (before = %f)\n", ws->height_factor[second]);
276 if (ws->height_factor[second] == 0)
277 old_second_height = (old_unoccupied_y / max(cols_without_hf, 1));
278 else old_second_height = ws->height_factor[second] * old_unoccupied_y;
280 DLOG("middle = %f\n", ws->height_factor[first]);
284 DLOG("old = %d, new = %d\n", old_unoccupied_y, new_unoccupied_y);
286 /* If the space used for customly resized columns has changed we need to adapt the
287 * other customly resized columns, if any */
288 if (new_unoccupied_y != old_unoccupied_y)
289 for (int row = 0; row < ws->rows; row++) {
290 if (ws->height_factor[row] == 0)
293 DLOG("Updating other column (%d) (current width_factor = %f)\n", row, ws->height_factor[row]);
294 ws->height_factor[row] = (ws->height_factor[row] * old_unoccupied_y) / new_unoccupied_y;
295 DLOG("to %f\n", ws->height_factor[row]);
299 DLOG("Updating first (before = %f)\n", ws->height_factor[first]);
300 /* Convert 0 (for default width_factor) to actual numbers */
301 if (ws->height_factor[first] == 0)
302 ws->height_factor[first] = ((float)ws_height / ws->rows) / new_unoccupied_y;
304 DLOG("first->width = %d, pixels = %d\n", old_height, pixels);
305 ws->height_factor[first] *= (float)(old_height + pixels) / old_height;
306 DLOG("-> %f\n", ws->height_factor[first]);
309 DLOG("Updating second (before = %f)\n", ws->height_factor[second]);
310 if (ws->height_factor[second] == 0)
311 ws->height_factor[second] = ((float)ws_height / ws->rows) / new_unoccupied_y;
312 DLOG("middle = %f\n", ws->height_factor[second]);
313 DLOG("second->width = %d, pixels = %d\n", old_second_height, pixels);
314 ws->height_factor[second] *= (float)(old_second_height - pixels) / old_second_height;
315 DLOG("-> %f\n", ws->height_factor[second]);
317 DLOG("new unoccupied_y = %d\n", get_unoccupied_y(ws));