]> git.sur5r.net Git - i3/i3/blob - src/resize.c
Update function names, variable names and documentation for the RandR changes
[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  * Renders the resize window between the first/second container and resizes
35  * the table column/row.
36  *
37  */
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) {
40         int new_position;
41         struct xoutput *screen = get_output_containing(event->root_x, event->root_y);
42         if (screen == NULL) {
43                 ELOG("BUG: No screen found at this position (%d, %d)\n", event->root_x, event->root_y);
44                 return 1;
45         }
46
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);
54
55         DLOG("event->event_x = %d, event->root_x = %d\n", event->event_x, event->root_x);
56
57         DLOG("Screen dimensions: (%d, %d) %d x %d\n", screen->rect.x, screen->rect.y, screen->rect.width, screen->rect.height);
58
59         uint32_t mask = 0;
60         uint32_t values[2];
61
62         mask = XCB_CW_OVERRIDE_REDIRECT;
63         values[0] = 1;
64
65         /* Open a new window, the resizebar. Grab the pointer and move the window around
66            as the user moves the pointer. */
67         Rect grabrect = {0,
68                          0,
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);
72
73         Rect helprect;
74         if (orientation == O_VERTICAL) {
75                 helprect.x = event->root_x;
76                 helprect.y = screen->rect.y;
77                 helprect.width = 2;
78                 helprect.height = screen->rect.height;
79                 new_position = event->root_x;
80         } else {
81                 helprect.x = screen->rect.x;
82                 helprect.y = event->root_y;
83                 helprect.width = screen->rect.width;
84                 helprect.height = 2;
85                 new_position = event->root_y;
86         }
87
88         mask = XCB_CW_BACK_PIXEL;
89         values[0] = config.client.focused.border;
90
91         mask |= XCB_CW_OVERRIDE_REDIRECT;
92         values[1] = 1;
93
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);
98
99         xcb_circulate_window(conn, XCB_CIRCULATE_RAISE_LOWEST, helpwin);
100
101         xcb_flush(conn);
102
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))
109                                 return;
110
111                         values[0] = new_position = new_x;
112                         xcb_configure_window(conn, helpwin, XCB_CONFIG_WINDOW_X, values);
113                 } else {
114                         if (new_y > (screen->rect.y + screen->rect.height - 25) ||
115                             new_y < (screen->rect.y + 25))
116                                 return;
117
118                         values[0] = new_position = new_y;
119                         xcb_configure_window(conn, helpwin, XCB_CONFIG_WINDOW_Y, values);
120                 }
121
122                 xcb_flush(conn);
123         }
124
125         drag_pointer(conn, NULL, event, grabwin, BORDER_TOP, resize_callback);
126
127         xcb_destroy_window(conn, helpwin);
128         xcb_destroy_window(conn, grabwin);
129         xcb_flush(conn);
130
131         int pixels;
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);
136
137         return 1;
138 }
139
140 /*
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)
144  *
145  */
146 void resize_container(xcb_connection_t *conn, Workspace *ws, int first, int second,
147                       resize_orientation_t orientation, int pixels) {
148
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);
153
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;
157
158                 if (old_unoccupied_x == 0)
159                         old_unoccupied_x = ws->rect.width;
160
161                 if (ws->width_factor[first] == 0)
162                         new_unoccupied_x += default_width;
163
164                 if (ws->width_factor[second] == 0)
165                         new_unoccupied_x += default_width;
166
167                 DLOG("\n\n\n");
168                 DLOG("old = %d, new = %d\n", old_unoccupied_x, new_unoccupied_x);
169
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)
174                                 cols_without_wf++;
175
176                 DLOG("old_unoccupied_x = %d\n", old_unoccupied_x);
177
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;
183
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;
188
189                 DLOG("middle = %f\n", ws->width_factor[first]);
190
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)
196                                         continue;
197
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]);
201                         }
202
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;
207
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]);
211
212
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;
216
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]);
221
222                 DLOG("new unoccupied_x = %d\n", get_unoccupied_x(ws));
223
224                 DLOG("\n\n\n");
225         } else {
226                 int ws_height = workspace_height(ws);
227                 int default_height = ws_height / ws->rows;
228                 int old_unoccupied_y = get_unoccupied_y(ws);
229
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;
233
234                 if (old_unoccupied_y == 0)
235                         old_unoccupied_y = ws_height;
236
237                 if (ws->height_factor[first] == 0)
238                         new_unoccupied_y += default_height;
239
240                 if (ws->height_factor[second] == 0)
241                         new_unoccupied_y += default_height;
242
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)
247                                 cols_without_hf++;
248
249                 DLOG("old_unoccupied_y = %d\n", old_unoccupied_y);
250
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;
256
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;
261
262                 DLOG("middle = %f\n", ws->height_factor[first]);
263
264
265                 DLOG("\n\n\n");
266                 DLOG("old = %d, new = %d\n", old_unoccupied_y, new_unoccupied_y);
267
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)
273                                         continue;
274
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]);
278                         }
279
280
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;
285
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]);
289
290
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]);
298
299                 DLOG("new unoccupied_y = %d\n", get_unoccupied_y(ws));
300
301                 DLOG("\n\n\n");
302         }
303
304         render_layout(conn);
305 }