]> git.sur5r.net Git - i3/i3/blob - src/resize.c
Bugfix: Correctly initialize workspaces for floating clients, too
[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         xcb_screen_t *root_screen = xcb_setup_roots_iterator(xcb_get_setup(conn)).data;
40         i3Screen *screen = get_screen_containing(event->root_x, event->root_y);
41         if (screen == NULL) {
42                 LOG("BUG: No screen found at this position (%d, %d)\n", event->root_x, event->root_y);
43                 return 1;
44         }
45
46         LOG("event->event_x = %d, event->root_x = %d\n", event->event_x, event->root_x);
47
48         LOG("Screen dimensions: (%d, %d) %d x %d\n", screen->rect.x, screen->rect.y, screen->rect.width, screen->rect.height);
49
50         uint32_t mask = 0;
51         uint32_t values[2];
52
53         mask = XCB_CW_OVERRIDE_REDIRECT;
54         values[0] = 1;
55
56         /* Open a new window, the resizebar. Grab the pointer and move the window around
57            as the user moves the pointer. */
58         Rect grabrect = {0, 0, root_screen->width_in_pixels, root_screen->height_in_pixels};
59         xcb_window_t grabwin = create_window(conn, grabrect, XCB_WINDOW_CLASS_INPUT_ONLY, -1, true, mask, values);
60
61         Rect helprect;
62         if (orientation == O_VERTICAL) {
63                 helprect.x = event->root_x;
64                 helprect.y = screen->rect.y;
65                 helprect.width = 2;
66                 helprect.height = screen->rect.height;
67                 new_position = event->root_x;
68         } else {
69                 helprect.x = 0;
70                 helprect.y = event->root_y;
71                 helprect.width = root_screen->width_in_pixels;
72                 helprect.height = 2;
73                 new_position = event->root_y;
74         }
75
76         mask = XCB_CW_BACK_PIXEL;
77         values[0] = config.client.focused.border;
78
79         mask |= XCB_CW_OVERRIDE_REDIRECT;
80         values[1] = 1;
81
82         xcb_window_t helpwin = create_window(conn, helprect, XCB_WINDOW_CLASS_INPUT_OUTPUT,
83                                              (orientation == O_VERTICAL ?
84                                               XCB_CURSOR_SB_V_DOUBLE_ARROW :
85                                               XCB_CURSOR_SB_H_DOUBLE_ARROW), true, mask, values);
86
87         xcb_circulate_window(conn, XCB_CIRCULATE_RAISE_LOWEST, helpwin);
88
89         xcb_flush(conn);
90
91         void resize_callback(Rect *old_rect, uint32_t new_x, uint32_t new_y) {
92                 LOG("new x = %d, y = %d\n", new_x, new_y);
93                 if (orientation == O_VERTICAL) {
94                         /* Check if the new coordinates are within screen boundaries */
95                         if (new_x > (screen->rect.x + screen->rect.width - 25) ||
96                             new_x < (screen->rect.x + 25))
97                                 return;
98
99                         values[0] = new_position = new_x;
100                         xcb_configure_window(conn, helpwin, XCB_CONFIG_WINDOW_X, values);
101                 } else {
102                         if (new_y > (screen->rect.y + screen->rect.height - 25) ||
103                             new_y < (screen->rect.y + 25))
104                                 return;
105
106                         values[0] = new_position = new_y;
107                         xcb_configure_window(conn, helpwin, XCB_CONFIG_WINDOW_Y, values);
108                 }
109
110                 xcb_flush(conn);
111         }
112
113         drag_pointer(conn, NULL, event, grabwin, BORDER_TOP, resize_callback);
114
115         xcb_destroy_window(conn, helpwin);
116         xcb_destroy_window(conn, grabwin);
117         xcb_flush(conn);
118
119         /* TODO: refactor this, both blocks are very identical */
120         if (orientation == O_VERTICAL) {
121                 LOG("Resize was from X = %d to X = %d\n", event->root_x, new_position);
122                 if (event->root_x == new_position) {
123                         LOG("Nothing changed, not updating anything\n");
124                         return 1;
125                 }
126
127                 int default_width = ws->rect.width / ws->cols;
128                 int old_unoccupied_x = get_unoccupied_x(ws);
129
130                 /* We pre-calculate the unoccupied space to see if we need to adapt sizes before
131                  * doing the resize */
132                 int new_unoccupied_x = old_unoccupied_x;
133
134                 if (old_unoccupied_x == 0)
135                         old_unoccupied_x = ws->rect.width;
136
137                 if (ws->width_factor[first] == 0)
138                         new_unoccupied_x += default_width;
139
140                 if (ws->width_factor[second] == 0)
141                         new_unoccupied_x += default_width;
142
143                 LOG("\n\n\n");
144                 LOG("old = %d, new = %d\n", old_unoccupied_x, new_unoccupied_x);
145
146                 /* If the space used for customly resized columns has changed we need to adapt the
147                  * other customly resized columns, if any */
148                 if (new_unoccupied_x != old_unoccupied_x)
149                         for (int col = 0; col < ws->cols; col++) {
150                                 if (ws->width_factor[col] == 0)
151                                         continue;
152
153                                 LOG("Updating other column (%d) (current width_factor = %f)\n", col, ws->width_factor[col]);
154                                 ws->width_factor[col] = (ws->width_factor[col] * old_unoccupied_x) / new_unoccupied_x;
155                                 LOG("to %f\n", ws->width_factor[col]);
156                         }
157
158                 LOG("old_unoccupied_x = %d\n", old_unoccupied_x);
159
160                 LOG("Updating first (before = %f)\n", ws->width_factor[first]);
161                 /* Convert 0 (for default width_factor) to actual numbers */
162                 if (ws->width_factor[first] == 0)
163                         ws->width_factor[first] = ((float)ws->rect.width / ws->cols) / new_unoccupied_x;
164
165                 LOG("middle = %f\n", ws->width_factor[first]);
166                 int old_width = ws->width_factor[first] * old_unoccupied_x;
167                 LOG("first->width = %d, new_position = %d, event->root_x = %d\n", old_width, new_position, event->root_x);
168                 ws->width_factor[first] *= (float)(old_width + (new_position - event->root_x)) / old_width;
169                 LOG("-> %f\n", ws->width_factor[first]);
170
171
172                 LOG("Updating second (before = %f)\n", ws->width_factor[second]);
173                 if (ws->width_factor[second] == 0)
174                         ws->width_factor[second] = ((float)ws->rect.width / ws->cols) / new_unoccupied_x;
175                 LOG("middle = %f\n", ws->width_factor[second]);
176                 old_width = ws->width_factor[second] * old_unoccupied_x;
177                 LOG("second->width = %d, new_position = %d, event->root_x = %d\n", old_width, new_position, event->root_x);
178                 ws->width_factor[second] *= (float)(old_width - (new_position - event->root_x)) / old_width;
179                 LOG("-> %f\n", ws->width_factor[second]);
180
181                 LOG("new unoccupied_x = %d\n", get_unoccupied_x(ws));
182
183                 LOG("\n\n\n");
184         } else {
185                 LOG("Resize was from X = %d to X = %d\n", event->root_y, new_position);
186                 if (event->root_y == new_position) {
187                         LOG("Nothing changed, not updating anything\n");
188                         return 1;
189                 }
190
191                 int default_height = ws->rect.height / ws->rows;
192                 int old_unoccupied_y = get_unoccupied_y(ws);
193
194                 /* We pre-calculate the unoccupied space to see if we need to adapt sizes before
195                  * doing the resize */
196                 int new_unoccupied_y = old_unoccupied_y;
197
198                 if (old_unoccupied_y == 0)
199                         old_unoccupied_y = ws->rect.height;
200
201                 if (ws->height_factor[first] == 0)
202                         new_unoccupied_y += default_height;
203
204                 if (ws->height_factor[second] == 0)
205                         new_unoccupied_y += default_height;
206
207                 LOG("\n\n\n");
208                 LOG("old = %d, new = %d\n", old_unoccupied_y, new_unoccupied_y);
209
210                 /* If the space used for customly resized columns has changed we need to adapt the
211                  * other customly resized columns, if any */
212                 if (new_unoccupied_y != old_unoccupied_y)
213                         for (int row = 0; row < ws->rows; row++) {
214                                 if (ws->height_factor[row] == 0)
215                                         continue;
216
217                                 LOG("Updating other column (%d) (current width_factor = %f)\n", row, ws->height_factor[row]);
218                                 ws->height_factor[row] = (ws->height_factor[row] * old_unoccupied_y) / new_unoccupied_y;
219                                 LOG("to %f\n", ws->height_factor[row]);
220                         }
221
222                 LOG("old_unoccupied_y = %d\n", old_unoccupied_y);
223
224                 LOG("Updating first (before = %f)\n", ws->height_factor[first]);
225                 /* Convert 0 (for default width_factor) to actual numbers */
226                 if (ws->height_factor[first] == 0)
227                         ws->height_factor[first] = ((float)ws->rect.height / ws->rows) / new_unoccupied_y;
228
229                 LOG("middle = %f\n", ws->height_factor[first]);
230                 int old_height = ws->height_factor[first] * old_unoccupied_y;
231                 LOG("first->width = %d, new_position = %d, event->root_x = %d\n", old_height, new_position, event->root_y);
232                 ws->height_factor[first] *= (float)(old_height + (new_position - event->root_y)) / old_height;
233                 LOG("-> %f\n", ws->height_factor[first]);
234
235
236                 LOG("Updating second (before = %f)\n", ws->height_factor[second]);
237                 if (ws->height_factor[second] == 0)
238                         ws->height_factor[second] = ((float)ws->rect.height / ws->rows) / new_unoccupied_y;
239                 LOG("middle = %f\n", ws->height_factor[second]);
240                 old_height = ws->height_factor[second] * old_unoccupied_y;
241                 LOG("second->width = %d, new_position = %d, event->root_x = %d\n", old_height, new_position, event->root_y);
242                 ws->height_factor[second] *= (float)(old_height - (new_position - event->root_y)) / old_height;
243                 LOG("-> %f\n", ws->height_factor[second]);
244
245                 LOG("new unoccupied_y = %d\n", get_unoccupied_y(ws));
246
247                 LOG("\n\n\n");
248         }
249
250         render_layout(conn);
251
252         return 1;
253 }