]> git.sur5r.net Git - i3/i3/blob - src/resize.c
Make i3 compatible with the very latest xcb
[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
19 #include "i3.h"
20 #include "data.h"
21 #include "resize.h"
22 #include "util.h"
23 #include "xcb.h"
24 #include "debug.h"
25 #include "layout.h"
26 #include "randr.h"
27 #include "config.h"
28 #include "floating.h"
29 #include "workspace.h"
30 #include "log.h"
31
32 /*
33  * This is an ugly data structure which we need because there is no standard
34  * way of having nested functions (only available as a gcc extension at the
35  * moment, clang doesn’t support it) or blocks (only available as a clang
36  * extension and only on Mac OS X systems at the moment).
37  *
38  */
39 struct callback_params {
40         resize_orientation_t orientation;
41         Output *screen;
42         xcb_window_t helpwin;
43         uint32_t *new_position;
44 };
45
46 DRAGGING_CB(resize_callback) {
47         struct callback_params *params = extra;
48         Output *screen = params->screen;
49         DLOG("new x = %d, y = %d\n", new_x, new_y);
50         if (params->orientation == O_VERTICAL) {
51                 /* Check if the new coordinates are within screen boundaries */
52                 if (new_x > (screen->rect.x + screen->rect.width - 25) ||
53                     new_x < (screen->rect.x + 25))
54                         return;
55
56                 *(params->new_position) = new_x;
57                 xcb_configure_window(conn, params->helpwin, XCB_CONFIG_WINDOW_X, params->new_position);
58         } else {
59                 if (new_y > (screen->rect.y + screen->rect.height - 25) ||
60                     new_y < (screen->rect.y + 25))
61                         return;
62
63                 *(params->new_position) = new_y;
64                 xcb_configure_window(conn, params->helpwin, XCB_CONFIG_WINDOW_Y, params->new_position);
65         }
66
67         xcb_flush(conn);
68 }
69
70 /*
71  * Renders the resize window between the first/second container and resizes
72  * the table column/row.
73  *
74  */
75 int resize_graphical_handler(xcb_connection_t *conn, Workspace *ws, int first, int second,
76                              resize_orientation_t orientation, xcb_button_press_event_t *event) {
77         uint32_t new_position;
78         Output *screen = get_output_containing(event->root_x, event->root_y);
79         if (screen == NULL) {
80                 ELOG("BUG: No screen found at this position (%d, %d)\n", event->root_x, event->root_y);
81                 return 1;
82         }
83
84         /* We cannot use the X root window's width_in_pixels or height_in_pixels
85          * attributes here since they are not updated when you configure new
86          * screens during runtime. Instead, we just use the most right and most
87          * bottom Xinerama screen and use their position + width/height to get
88          * the area of pixels currently in use */
89         Output *most_right = get_output_most(D_RIGHT, screen),
90                *most_bottom = get_output_most(D_DOWN, screen);
91
92         DLOG("event->event_x = %d, event->root_x = %d\n", event->event_x, event->root_x);
93
94         DLOG("Screen dimensions: (%d, %d) %d x %d\n", screen->rect.x, screen->rect.y, screen->rect.width, screen->rect.height);
95
96         uint32_t mask = 0;
97         uint32_t values[2];
98
99         mask = XCB_CW_OVERRIDE_REDIRECT;
100         values[0] = 1;
101
102         /* Open a new window, the resizebar. Grab the pointer and move the window around
103            as the user moves the pointer. */
104         Rect grabrect = {0,
105                          0,
106                          most_right->rect.x + most_right->rect.width,
107                          most_bottom->rect.x + most_bottom->rect.height};
108         xcb_window_t grabwin = create_window(conn, grabrect, XCB_WINDOW_CLASS_INPUT_ONLY, -1, true, mask, values);
109
110         Rect helprect;
111         if (orientation == O_VERTICAL) {
112                 helprect.x = event->root_x;
113                 helprect.y = screen->rect.y;
114                 helprect.width = 2;
115                 helprect.height = screen->rect.height;
116                 new_position = event->root_x;
117         } else {
118                 helprect.x = screen->rect.x;
119                 helprect.y = event->root_y;
120                 helprect.width = screen->rect.width;
121                 helprect.height = 2;
122                 new_position = event->root_y;
123         }
124
125         mask = XCB_CW_BACK_PIXEL;
126         values[0] = config.client.focused.border;
127
128         mask |= XCB_CW_OVERRIDE_REDIRECT;
129         values[1] = 1;
130
131         xcb_window_t helpwin = create_window(conn, helprect, XCB_WINDOW_CLASS_INPUT_OUTPUT,
132                                              (orientation == O_VERTICAL ?
133                                               XCB_CURSOR_SB_H_DOUBLE_ARROW :
134                                               XCB_CURSOR_SB_V_DOUBLE_ARROW), true, mask, values);
135
136         xcb_circulate_window(conn, XCB_CIRCULATE_RAISE_LOWEST, helpwin);
137
138         xcb_flush(conn);
139
140         struct callback_params params = { orientation, screen, helpwin, &new_position };
141
142         drag_pointer(conn, NULL, event, grabwin, BORDER_TOP, resize_callback, &params);
143
144         xcb_destroy_window(conn, helpwin);
145         xcb_destroy_window(conn, grabwin);
146         xcb_flush(conn);
147
148         int pixels;
149         if (orientation == O_VERTICAL)
150                 pixels = (new_position - event->root_x);
151         else pixels = (new_position - event->root_y);
152         resize_container(conn, ws, first, second, orientation, pixels);
153
154         return 1;
155 }
156
157 /*
158  * Adjusts the container size factors according to the resizing parameters.
159  * This is an abstraction used by resize_container.
160  */
161 static void adjust_container_factors(float *factors, int ws_size, int unoccupied_size,
162                 int num_items, int first, int second, int pixels) {
163         /* Find the current sizes */
164         int sizes[num_items];
165         for (int i = 0; i < num_items; ++i)
166                 sizes[i] = factors[i] == 0 ? ws_size / num_items : unoccupied_size * factors[i];
167
168         /* Adjust them */
169         sizes[first] += pixels;
170         sizes[second] -= pixels;
171
172         /* Calculate the new unoccupied size */
173         if (factors[first] == 0) unoccupied_size += ws_size / num_items;
174         if (factors[second] == 0) unoccupied_size += ws_size / num_items;
175
176         /* Calculate the new factors */
177         for (int i = 0; i < num_items; ++i) {
178                 if (factors[i] != 0 || i == first || i == second)
179                         factors[i] = (float)sizes[i] / unoccupied_size;
180         }
181 }
182
183 /*
184  * Resizes a column/row by the given amount of pixels. Called by
185  * resize_graphical_handler (the user clicked) or parse_resize_command (the
186  * user issued the command)
187  *
188  */
189 void resize_container(xcb_connection_t *conn, Workspace *ws, int first, int second,
190                       resize_orientation_t orientation, int pixels) {
191         if (orientation == O_VERTICAL) {
192                 adjust_container_factors(ws->width_factor, ws->rect.width,
193                                 get_unoccupied_x(ws), ws->cols, first, second, pixels);
194         }
195         else {
196                 adjust_container_factors(ws->height_factor, workspace_height(ws),
197                                 get_unoccupied_y(ws), ws->rows, first, second, pixels);
198         }
199
200         render_layout(conn);
201 }