]> git.sur5r.net Git - i3/i3/blob - src/resize.c
Bugfix: Correctly calculate width when resizing (Thanks Merovius)
[i3/i3] / src / resize.c
1 /*
2  * vim:ts=8:expandtab
3  *
4  * i3 - an improved dynamic tiling window manager
5  *
6  * © 2009 Michael Stapelberg and contributors
7  *
8  * See file LICENSE for license information.
9  *
10  * This file contains the functions for resizing table columns/rows because
11  * it’s actually lots of work, compared to the other handlers.
12  *
13  */
14 #include <stdlib.h>
15 #include <assert.h>
16
17 #include <xcb/xcb.h>
18 #include <xcb/xcb_event.h>
19
20 #include "i3.h"
21 #include "data.h"
22 #include "resize.h"
23 #include "util.h"
24 #include "xcb.h"
25 #include "debug.h"
26 #include "layout.h"
27 #include "xinerama.h"
28 #include "config.h"
29 #include "floating.h"
30
31 /*
32  * Renders the resize window between the first/second container and resizes
33  * the table column/row.
34  *
35  */
36 int resize_graphical_handler(xcb_connection_t *conn, Workspace *ws, int first, int second,
37                              resize_orientation_t orientation, xcb_button_press_event_t *event) {
38         int new_position;
39         i3Screen *screen = get_screen_containing(event->root_x, event->root_y);
40         if (screen == NULL) {
41                 LOG("BUG: No screen found at this position (%d, %d)\n", event->root_x, event->root_y);
42                 return 1;
43         }
44
45         /* We cannot use the X root window's width_in_pixels or height_in_pixels
46          * attributes here since they are not updated when you configure new
47          * screens during runtime. Instead, we just use the most right and most
48          * bottom Xinerama screen and use their position + width/height to get
49          * the area of pixels currently in use */
50         i3Screen *most_right = get_screen_most(D_RIGHT, screen),
51                  *most_bottom = get_screen_most(D_DOWN, screen);
52
53         LOG("event->event_x = %d, event->root_x = %d\n", event->event_x, event->root_x);
54
55         LOG("Screen dimensions: (%d, %d) %d x %d\n", screen->rect.x, screen->rect.y, screen->rect.width, screen->rect.height);
56
57         uint32_t mask = 0;
58         uint32_t values[2];
59
60         mask = XCB_CW_OVERRIDE_REDIRECT;
61         values[0] = 1;
62
63         /* Open a new window, the resizebar. Grab the pointer and move the window around
64            as the user moves the pointer. */
65         Rect grabrect = {0,
66                          0,
67                          most_right->rect.x + most_right->rect.width,
68                          most_bottom->rect.x + most_bottom->rect.height};
69         xcb_window_t grabwin = create_window(conn, grabrect, XCB_WINDOW_CLASS_INPUT_ONLY, -1, true, mask, values);
70
71         Rect helprect;
72         if (orientation == O_VERTICAL) {
73                 helprect.x = event->root_x;
74                 helprect.y = screen->rect.y;
75                 helprect.width = 2;
76                 helprect.height = screen->rect.height;
77                 new_position = event->root_x;
78         } else {
79                 helprect.x = screen->rect.x;
80                 helprect.y = event->root_y;
81                 helprect.width = screen->rect.width;
82                 helprect.height = 2;
83                 new_position = event->root_y;
84         }
85
86         mask = XCB_CW_BACK_PIXEL;
87         values[0] = config.client.focused.border;
88
89         mask |= XCB_CW_OVERRIDE_REDIRECT;
90         values[1] = 1;
91
92         xcb_window_t helpwin = create_window(conn, helprect, XCB_WINDOW_CLASS_INPUT_OUTPUT,
93                                              (orientation == O_VERTICAL ?
94                                               XCB_CURSOR_SB_V_DOUBLE_ARROW :
95                                               XCB_CURSOR_SB_H_DOUBLE_ARROW), true, mask, values);
96
97         xcb_circulate_window(conn, XCB_CIRCULATE_RAISE_LOWEST, helpwin);
98
99         xcb_flush(conn);
100
101         void resize_callback(Rect *old_rect, uint32_t new_x, uint32_t new_y) {
102                 LOG("new x = %d, y = %d\n", new_x, new_y);
103                 if (orientation == O_VERTICAL) {
104                         /* Check if the new coordinates are within screen boundaries */
105                         if (new_x > (screen->rect.x + screen->rect.width - 25) ||
106                             new_x < (screen->rect.x + 25))
107                                 return;
108
109                         values[0] = new_position = new_x;
110                         xcb_configure_window(conn, helpwin, XCB_CONFIG_WINDOW_X, values);
111                 } else {
112                         if (new_y > (screen->rect.y + screen->rect.height - 25) ||
113                             new_y < (screen->rect.y + 25))
114                                 return;
115
116                         values[0] = new_position = new_y;
117                         xcb_configure_window(conn, helpwin, XCB_CONFIG_WINDOW_Y, values);
118                 }
119
120                 xcb_flush(conn);
121         }
122
123         drag_pointer(conn, NULL, event, grabwin, BORDER_TOP, resize_callback);
124
125         xcb_destroy_window(conn, helpwin);
126         xcb_destroy_window(conn, grabwin);
127         xcb_flush(conn);
128
129         int pixels;
130         if (orientation == O_VERTICAL)
131                 pixels = (new_position - event->root_x);
132         else pixels = (new_position - event->root_y);
133         resize_container(conn, ws, first, second, orientation, pixels);
134
135         return 1;
136 }
137
138 /*
139  * Resizes a column/row by the given amount of pixels. Called by
140  * resize_graphical_handler (the user clicked) or parse_resize_command (the
141  * user issued the command)
142  *
143  */
144 void resize_container(xcb_connection_t *conn, Workspace *ws, int first, int second,
145                       resize_orientation_t orientation, int pixels) {
146
147         /* TODO: refactor this, both blocks are very identical */
148         if (orientation == O_VERTICAL) {
149                 int default_width = ws->rect.width / ws->cols;
150                 int old_unoccupied_x = get_unoccupied_x(ws);
151
152                 /* We pre-calculate the unoccupied space to see if we need to adapt sizes before
153                  * doing the resize */
154                 int new_unoccupied_x = old_unoccupied_x;
155
156                 if (old_unoccupied_x == 0)
157                         old_unoccupied_x = ws->rect.width;
158
159                 if (ws->width_factor[first] == 0)
160                         new_unoccupied_x += default_width;
161
162                 if (ws->width_factor[second] == 0)
163                         new_unoccupied_x += default_width;
164
165                 LOG("\n\n\n");
166                 LOG("old = %d, new = %d\n", old_unoccupied_x, new_unoccupied_x);
167
168                 int cols_without_wf = 0;
169                 int old_width, old_second_width;
170                 for (int col = 0; col < ws->cols; col++)
171                         if (ws->width_factor[col] == 0)
172                                 cols_without_wf++;
173
174                 LOG("old_unoccupied_x = %d\n", old_unoccupied_x);
175
176                 LOG("Updating first (before = %f)\n", ws->width_factor[first]);
177                 /* Convert 0 (for default width_factor) to actual numbers */
178                 if (ws->width_factor[first] == 0)
179                         old_width = (old_unoccupied_x / max(cols_without_wf, 1));
180                 else old_width = ws->width_factor[first] * old_unoccupied_x;
181
182                 LOG("second (before = %f)\n", ws->width_factor[second]);
183                 if (ws->width_factor[second] == 0)
184                         old_second_width = (old_unoccupied_x / max(cols_without_wf, 1));
185                 else old_second_width = ws->width_factor[second] * old_unoccupied_x;
186
187                 LOG("middle = %f\n", ws->width_factor[first]);
188
189                 /* If the space used for customly resized columns has changed we need to adapt the
190                  * other customly resized columns, if any */
191                 if (new_unoccupied_x != old_unoccupied_x)
192                         for (int col = 0; col < ws->cols; col++) {
193                                 if (ws->width_factor[col] == 0)
194                                         continue;
195
196                                 LOG("Updating other column (%d) (current width_factor = %f)\n", col, ws->width_factor[col]);
197                                 ws->width_factor[col] = (ws->width_factor[col] * old_unoccupied_x) / new_unoccupied_x;
198                                 LOG("to %f\n", ws->width_factor[col]);
199                         }
200
201                 LOG("Updating first (before = %f)\n", ws->width_factor[first]);
202                 /* Convert 0 (for default width_factor) to actual numbers */
203                 if (ws->width_factor[first] == 0)
204                         ws->width_factor[first] = ((float)ws->rect.width / ws->cols) / new_unoccupied_x;
205
206                 LOG("first->width = %d, pixels = %d\n", old_width, pixels);
207                 ws->width_factor[first] *= (float)(old_width + pixels) / old_width;
208                 LOG("-> %f\n", ws->width_factor[first]);
209
210
211                 LOG("Updating second (before = %f)\n", ws->width_factor[second]);
212                 if (ws->width_factor[second] == 0)
213                         ws->width_factor[second] = ((float)ws->rect.width / ws->cols) / new_unoccupied_x;
214
215                 LOG("middle = %f\n", ws->width_factor[second]);
216                 LOG("second->width = %d, pixels = %d\n", old_second_width, pixels);
217                 ws->width_factor[second] *= (float)(old_second_width - pixels) / old_second_width;
218                 LOG("-> %f\n", ws->width_factor[second]);
219
220                 LOG("new unoccupied_x = %d\n", get_unoccupied_x(ws));
221
222                 LOG("\n\n\n");
223         } else {
224                 int default_height = ws->rect.height / ws->rows;
225                 int old_unoccupied_y = get_unoccupied_y(ws);
226
227                 /* We pre-calculate the unoccupied space to see if we need to adapt sizes before
228                  * doing the resize */
229                 int new_unoccupied_y = old_unoccupied_y;
230
231                 if (old_unoccupied_y == 0)
232                         old_unoccupied_y = ws->rect.height;
233
234                 if (ws->height_factor[first] == 0)
235                         new_unoccupied_y += default_height;
236
237                 if (ws->height_factor[second] == 0)
238                         new_unoccupied_y += default_height;
239
240                 LOG("\n\n\n");
241                 LOG("old = %d, new = %d\n", old_unoccupied_y, new_unoccupied_y);
242
243                 /* If the space used for customly resized columns has changed we need to adapt the
244                  * other customly resized columns, if any */
245                 if (new_unoccupied_y != old_unoccupied_y)
246                         for (int row = 0; row < ws->rows; row++) {
247                                 if (ws->height_factor[row] == 0)
248                                         continue;
249
250                                 LOG("Updating other column (%d) (current width_factor = %f)\n", row, ws->height_factor[row]);
251                                 ws->height_factor[row] = (ws->height_factor[row] * old_unoccupied_y) / new_unoccupied_y;
252                                 LOG("to %f\n", ws->height_factor[row]);
253                         }
254
255                 LOG("old_unoccupied_y = %d\n", old_unoccupied_y);
256
257                 LOG("Updating first (before = %f)\n", ws->height_factor[first]);
258                 /* Convert 0 (for default width_factor) to actual numbers */
259                 if (ws->height_factor[first] == 0)
260                         ws->height_factor[first] = ((float)ws->rect.height / ws->rows) / new_unoccupied_y;
261
262                 LOG("middle = %f\n", ws->height_factor[first]);
263                 int old_height = ws->height_factor[first] * old_unoccupied_y;
264                 LOG("first->width = %d, pixels = %d\n", pixels);
265                 ws->height_factor[first] *= (float)(old_height + pixels) / old_height;
266                 LOG("-> %f\n", ws->height_factor[first]);
267
268
269                 LOG("Updating second (before = %f)\n", ws->height_factor[second]);
270                 if (ws->height_factor[second] == 0)
271                         ws->height_factor[second] = ((float)ws->rect.height / ws->rows) / new_unoccupied_y;
272                 LOG("middle = %f\n", ws->height_factor[second]);
273                 old_height = ws->height_factor[second] * old_unoccupied_y;
274                 LOG("second->width = %d, pixels = %d\n", pixels);
275                 ws->height_factor[second] *= (float)(old_height - pixels) / old_height;
276                 LOG("-> %f\n", ws->height_factor[second]);
277
278                 LOG("new unoccupied_y = %d\n", get_unoccupied_y(ws));
279
280                 LOG("\n\n\n");
281         }
282
283         render_layout(conn);
284 }