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 * Adjusts the container size factors according to the resizing parameters.
160 * This is an abstraction used by resize_container.
162 static void adjust_container_factors(float *factors, int ws_size, int unoccupied_size,
163 int num_items, int first, int second, int pixels) {
164 /* Find the current sizes */
165 int sizes[num_items];
166 for (int i = 0; i < num_items; ++i)
167 sizes[i] = factors[i] == 0 ? ws_size / num_items : unoccupied_size * factors[i];
170 sizes[first] += pixels;
171 sizes[second] -= pixels;
173 /* Calculate the new unoccupied size */
174 if (factors[first] == 0) unoccupied_size += ws_size / num_items;
175 if (factors[second] == 0) unoccupied_size += ws_size / num_items;
177 /* Calculate the new factors */
178 for (int i = 0; i < num_items; ++i) {
179 if (factors[i] != 0 || i == first || i == second)
180 factors[i] = (float)sizes[i] / unoccupied_size;
185 * Resizes a column/row by the given amount of pixels. Called by
186 * resize_graphical_handler (the user clicked) or parse_resize_command (the
187 * user issued the command)
190 void resize_container(xcb_connection_t *conn, Workspace *ws, int first, int second,
191 resize_orientation_t orientation, int pixels) {
192 if (orientation == O_VERTICAL) {
193 adjust_container_factors(ws->width_factor, ws->rect.width,
194 get_unoccupied_x(ws), ws->cols, first, second, pixels);
197 adjust_container_factors(ws->height_factor, workspace_height(ws),
198 get_unoccupied_y(ws), ws->rows, first, second, pixels);