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