]> git.sur5r.net Git - i3/i3/blob - src/resize.c
normalize modelines/headers across src/*.c
[i3/i3] / src / resize.c
1 /*
2  * vim:ts=4:sw=4:expandtab
3  *
4  * i3 - an improved dynamic tiling window manager
5  * © 2009-2011 Michael Stapelberg and contributors (see also: LICENSE)
6  *
7  * resize.c: Interactive resizing.
8  *
9  */
10 #include "all.h"
11
12 extern xcb_connection_t *conn;
13
14 /*
15  * This is an ugly data structure which we need because there is no standard
16  * way of having nested functions (only available as a gcc extension at the
17  * moment, clang doesn’t support it) or blocks (only available as a clang
18  * extension and only on Mac OS X systems at the moment).
19  *
20  */
21 struct callback_params {
22     orientation_t orientation;
23     Con *output;
24     xcb_window_t helpwin;
25     uint32_t *new_position;
26 };
27
28 DRAGGING_CB(resize_callback) {
29     struct callback_params *params = extra;
30     Con *output = params->output;
31     DLOG("new x = %d, y = %d\n", new_x, new_y);
32     if (params->orientation == HORIZ) {
33         /* Check if the new coordinates are within screen boundaries */
34         if (new_x > (output->rect.x + output->rect.width - 25) ||
35             new_x < (output->rect.x + 25))
36             return;
37
38         *(params->new_position) = new_x;
39         xcb_configure_window(conn, params->helpwin, XCB_CONFIG_WINDOW_X, params->new_position);
40     } else {
41         if (new_y > (output->rect.y + output->rect.height - 25) ||
42             new_y < (output->rect.y + 25))
43             return;
44
45         *(params->new_position) = new_y;
46         xcb_configure_window(conn, params->helpwin, XCB_CONFIG_WINDOW_Y, params->new_position);
47     }
48
49     xcb_flush(conn);
50 }
51
52 int resize_graphical_handler(Con *first, Con *second, orientation_t orientation, xcb_button_press_event_t *event) {
53     DLOG("resize handler\n");
54
55     uint32_t new_position;
56
57     /* TODO: previously, we were getting a rect containing all screens. why? */
58     Con *output = con_get_output(first);
59     DLOG("x = %d, width = %d\n", output->rect.x, output->rect.width);
60
61     uint32_t mask = 0;
62     uint32_t values[2];
63
64     mask = XCB_CW_OVERRIDE_REDIRECT;
65     values[0] = 1;
66
67     /* Open a new window, the resizebar. Grab the pointer and move the window around
68        as the user moves the pointer. */
69     xcb_window_t grabwin = create_window(conn, output->rect, XCB_WINDOW_CLASS_INPUT_ONLY, XCURSOR_CURSOR_POINTER, true, mask, values);
70
71     Rect helprect;
72     if (orientation == HORIZ) {
73         helprect.x = event->root_x;
74         helprect.y = output->rect.y;
75         helprect.width = 2;
76         helprect.height = output->rect.height;
77         new_position = event->root_x;
78     } else {
79         helprect.x = output->rect.x;
80         helprect.y = event->root_y;
81         helprect.width = output->rect.width;
82         helprect.height = 2;
83         new_position = event->root_y;
84     }
85
86     mask = XCB_CW_BACK_PIXEL;
87     values[0] = config.client.focused.border;
88
89     mask |= XCB_CW_OVERRIDE_REDIRECT;
90     values[1] = 1;
91
92     xcb_window_t helpwin = create_window(conn, helprect, XCB_WINDOW_CLASS_INPUT_OUTPUT,
93                                          (orientation == HORIZ ?
94                                           XCURSOR_CURSOR_RESIZE_HORIZONTAL :
95                                           XCURSOR_CURSOR_RESIZE_VERTICAL), true, mask, values);
96
97     xcb_circulate_window(conn, XCB_CIRCULATE_RAISE_LOWEST, helpwin);
98
99     xcb_flush(conn);
100
101     struct callback_params params = { orientation, output, helpwin, &new_position };
102
103     drag_pointer(NULL, event, grabwin, BORDER_TOP, resize_callback, &params);
104
105     xcb_destroy_window(conn, helpwin);
106     xcb_destroy_window(conn, grabwin);
107     xcb_flush(conn);
108
109     int pixels;
110     if (orientation == HORIZ)
111         pixels = (new_position - event->root_x);
112     else pixels = (new_position - event->root_y);
113
114     DLOG("Done, pixels = %d\n", pixels);
115
116     // if we got thus far, the containers must have
117     // percentages associated with them
118     assert(first->percent > 0.0);
119     assert(second->percent > 0.0);
120
121     // calculate the new percentage for the first container
122     double new_percent, difference;
123     double percent = first->percent;
124     DLOG("percent = %f\n", percent);
125     int original = (orientation == HORIZ ? first->rect.width : first->rect.height);
126     DLOG("original = %d\n", original);
127     new_percent = (original + pixels) * (percent / original);
128     difference = percent - new_percent;
129     DLOG("difference = %f\n", difference);
130     DLOG("new percent = %f\n", new_percent);
131     first->percent = new_percent;
132
133     // calculate the new percentage for the second container
134     double s_percent = second->percent;
135     second->percent = s_percent + difference;
136     DLOG("second->percent = %f\n", second->percent);
137
138     // now we must make sure that the sum of the percentages remain 1.0
139     con_fix_percent(first->parent);
140
141     return 0;
142 }