]> git.sur5r.net Git - i3/i3/blob - src/resize.c
Fixed overflow and underflow bugs when resizing.
[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  * Adjusts the container size factors according to the resizing parameters.
160  * This is an abstraction used by resize_container.
161  */
162 static void adjust_container_factors(float *factors, int ws_size, int unoccupied_size,
163                 int num_items, int first, int second, int pixels) {
164         /* Find the current sizes */
165         int sizes[num_items];
166         for (int i = 0; i < num_items; ++i)
167                 sizes[i] = factors[i] == 0 ? ws_size / num_items : unoccupied_size * factors[i];
168
169         /* Adjust them */
170         sizes[first] += pixels;
171         sizes[second] -= pixels;
172
173         /* Calculate the new unoccupied size */
174         if (factors[first] == 0) unoccupied_size += ws_size / num_items;
175         if (factors[second] == 0) unoccupied_size += ws_size / num_items;
176
177         /* Calculate the new factors */
178         for (int i = 0; i < num_items; ++i) {
179                 if (factors[i] != 0 || i == first || i == second)
180                         factors[i] = (float)sizes[i] / unoccupied_size;
181         }
182 }
183
184 /*
185  * Resizes a column/row by the given amount of pixels. Called by
186  * resize_graphical_handler (the user clicked) or parse_resize_command (the
187  * user issued the command)
188  *
189  */
190 void resize_container(xcb_connection_t *conn, Workspace *ws, int first, int second,
191                       resize_orientation_t orientation, int pixels) {
192         if (orientation == O_VERTICAL) {
193                 adjust_container_factors(ws->width_factor, ws->rect.width,
194                                 get_unoccupied_x(ws), ws->cols, first, second, pixels);
195         }
196         else {
197                 adjust_container_factors(ws->height_factor, workspace_height(ws),
198                                 get_unoccupied_y(ws), ws->rows, first, second, pixels);
199         }
200
201         render_layout(conn);
202 }