]> git.sur5r.net Git - i3/i3/blob - src/resize.c
Merge branch 'next'
[i3/i3] / src / resize.c
1 /*
2  * vim:ts=8:expandtab
3  *
4  * i3 - an improved dynamic tiling window manager
5  *
6  * © 2009-2010 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 "randr.h"
28 #include "config.h"
29 #include "floating.h"
30 #include "workspace.h"
31 #include "log.h"
32
33 /*
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).
38  *
39  */
40 struct callback_params {
41         resize_orientation_t orientation;
42         Output *screen;
43         xcb_window_t helpwin;
44         uint32_t *new_position;
45 };
46
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))
55                         return;
56
57                 *(params->new_position) = new_x;
58                 xcb_configure_window(conn, params->helpwin, XCB_CONFIG_WINDOW_X, params->new_position);
59         } else {
60                 if (new_y > (screen->rect.y + screen->rect.height - 25) ||
61                     new_y < (screen->rect.y + 25))
62                         return;
63
64                 *(params->new_position) = new_y;
65                 xcb_configure_window(conn, params->helpwin, XCB_CONFIG_WINDOW_Y, params->new_position);
66         }
67
68         xcb_flush(conn);
69 }
70
71 /*
72  * Renders the resize window between the first/second container and resizes
73  * the table column/row.
74  *
75  */
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);
80         if (screen == NULL) {
81                 ELOG("BUG: No screen found at this position (%d, %d)\n", event->root_x, event->root_y);
82                 return 1;
83         }
84
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);
92
93         DLOG("event->event_x = %d, event->root_x = %d\n", event->event_x, event->root_x);
94
95         DLOG("Screen dimensions: (%d, %d) %d x %d\n", screen->rect.x, screen->rect.y, screen->rect.width, screen->rect.height);
96
97         uint32_t mask = 0;
98         uint32_t values[2];
99
100         mask = XCB_CW_OVERRIDE_REDIRECT;
101         values[0] = 1;
102
103         /* Open a new window, the resizebar. Grab the pointer and move the window around
104            as the user moves the pointer. */
105         Rect grabrect = {0,
106                          0,
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);
110
111         Rect helprect;
112         if (orientation == O_VERTICAL) {
113                 helprect.x = event->root_x;
114                 helprect.y = screen->rect.y;
115                 helprect.width = 2;
116                 helprect.height = screen->rect.height;
117                 new_position = event->root_x;
118         } else {
119                 helprect.x = screen->rect.x;
120                 helprect.y = event->root_y;
121                 helprect.width = screen->rect.width;
122                 helprect.height = 2;
123                 new_position = event->root_y;
124         }
125
126         mask = XCB_CW_BACK_PIXEL;
127         values[0] = config.client.focused.border;
128
129         mask |= XCB_CW_OVERRIDE_REDIRECT;
130         values[1] = 1;
131
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);
136
137         xcb_circulate_window(conn, XCB_CIRCULATE_RAISE_LOWEST, helpwin);
138
139         xcb_flush(conn);
140
141         struct callback_params params = { orientation, screen, helpwin, &new_position };
142
143         drag_pointer(conn, NULL, event, grabwin, BORDER_TOP, resize_callback, &params);
144
145         xcb_destroy_window(conn, helpwin);
146         xcb_destroy_window(conn, grabwin);
147         xcb_flush(conn);
148
149         int pixels;
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);
154
155         return 1;
156 }
157
158 /*
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)
162  *
163  */
164 void resize_container(xcb_connection_t *conn, Workspace *ws, int first, int second,
165                       resize_orientation_t orientation, int pixels) {
166
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);
171
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;
175
176                 if (old_unoccupied_x == 0)
177                         old_unoccupied_x = ws->rect.width;
178
179                 if (ws->width_factor[first] == 0)
180                         new_unoccupied_x += default_width;
181
182                 if (ws->width_factor[second] == 0)
183                         new_unoccupied_x += default_width;
184
185                 DLOG("\n\n\n");
186                 DLOG("old = %d, new = %d\n", old_unoccupied_x, new_unoccupied_x);
187
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)
192                                 cols_without_wf++;
193
194                 DLOG("old_unoccupied_x = %d\n", old_unoccupied_x);
195
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;
201
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;
206
207                 DLOG("middle = %f\n", ws->width_factor[first]);
208
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)
214                                         continue;
215
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]);
219                         }
220
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;
225
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]);
229
230
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;
234
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]);
239
240                 DLOG("new unoccupied_x = %d\n", get_unoccupied_x(ws));
241
242                 DLOG("\n\n\n");
243         } else {
244                 int ws_height = workspace_height(ws);
245                 int default_height = ws_height / ws->rows;
246                 int old_unoccupied_y = get_unoccupied_y(ws);
247
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;
251
252                 if (old_unoccupied_y == 0)
253                         old_unoccupied_y = ws_height;
254
255                 if (ws->height_factor[first] == 0)
256                         new_unoccupied_y += default_height;
257
258                 if (ws->height_factor[second] == 0)
259                         new_unoccupied_y += default_height;
260
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)
265                                 cols_without_hf++;
266
267                 DLOG("old_unoccupied_y = %d\n", old_unoccupied_y);
268
269                 DLOG("Updating first (before = %f)\n", ws->height_factor[first]);
270
271                 /* Convert 0 (for default width_factor) to actual numbers */
272                 if (ws->height_factor[first] == 0)
273                         old_height = (old_unoccupied_y / max(cols_without_hf, 1));
274                 else old_height = ws->height_factor[first] * old_unoccupied_y;
275
276                 DLOG("second (before = %f)\n", ws->height_factor[second]);
277                 if (ws->height_factor[second] == 0)
278                         old_second_height = (old_unoccupied_y / max(cols_without_hf, 1));
279                 else old_second_height = ws->height_factor[second] * old_unoccupied_y;
280
281                 DLOG("middle = %f\n", ws->height_factor[first]);
282
283
284                 DLOG("\n\n\n");
285                 DLOG("old = %d, new = %d\n", old_unoccupied_y, new_unoccupied_y);
286
287                 /* If the space used for customly resized columns has changed we need to adapt the
288                  * other customly resized columns, if any */
289                 if (new_unoccupied_y != old_unoccupied_y)
290                         for (int row = 0; row < ws->rows; row++) {
291                                 if (ws->height_factor[row] == 0)
292                                         continue;
293
294                                 DLOG("Updating other column (%d) (current width_factor = %f)\n", row, ws->height_factor[row]);
295                                 ws->height_factor[row] = (ws->height_factor[row] * old_unoccupied_y) / new_unoccupied_y;
296                                 DLOG("to %f\n", ws->height_factor[row]);
297                         }
298
299
300                 DLOG("Updating first (before = %f)\n", ws->height_factor[first]);
301                 /* Convert 0 (for default width_factor) to actual numbers */
302                 if (ws->height_factor[first] == 0)
303                         ws->height_factor[first] = ((float)ws_height / ws->rows) / new_unoccupied_y;
304
305                 DLOG("first->width = %d, pixels = %d\n", old_height, pixels);
306                 ws->height_factor[first] *= (float)(old_height + pixels) / old_height;
307                 DLOG("-> %f\n", ws->height_factor[first]);
308
309
310                 DLOG("Updating second (before = %f)\n", ws->height_factor[second]);
311                 if (ws->height_factor[second] == 0)
312                         ws->height_factor[second] = ((float)ws_height / ws->rows) / new_unoccupied_y;
313                 DLOG("middle = %f\n", ws->height_factor[second]);
314                 DLOG("second->width = %d, pixels = %d\n", old_second_height, pixels);
315                 ws->height_factor[second] *= (float)(old_second_height - pixels) / old_second_height;
316                 DLOG("-> %f\n", ws->height_factor[second]);
317
318                 DLOG("new unoccupied_y = %d\n", get_unoccupied_y(ws));
319
320                 DLOG("\n\n\n");
321         }
322
323         render_layout(conn);
324 }