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