i3
src/resize.c
Go to the documentation of this file.
00001 /*
00002  * vim:ts=4:sw=4:expandtab
00003  *
00004  * i3 - an improved dynamic tiling window manager
00005  * © 2009-2011 Michael Stapelberg and contributors (see also: LICENSE)
00006  *
00007  * resize.c: Interactive resizing.
00008  *
00009  */
00010 #include "all.h"
00011 
00012 extern xcb_connection_t *conn;
00013 
00014 /*
00015  * This is an ugly data structure which we need because there is no standard
00016  * way of having nested functions (only available as a gcc extension at the
00017  * moment, clang doesn’t support it) or blocks (only available as a clang
00018  * extension and only on Mac OS X systems at the moment).
00019  *
00020  */
00021 struct callback_params {
00022     orientation_t orientation;
00023     Con *output;
00024     xcb_window_t helpwin;
00025     uint32_t *new_position;
00026 };
00027 
00028 DRAGGING_CB(resize_callback) {
00029     const struct callback_params *params = extra;
00030     Con *output = params->output;
00031     DLOG("new x = %d, y = %d\n", new_x, new_y);
00032     if (params->orientation == HORIZ) {
00033         /* Check if the new coordinates are within screen boundaries */
00034         if (new_x > (output->rect.x + output->rect.width - 25) ||
00035             new_x < (output->rect.x + 25))
00036             return;
00037 
00038         *(params->new_position) = new_x;
00039         xcb_configure_window(conn, params->helpwin, XCB_CONFIG_WINDOW_X, params->new_position);
00040     } else {
00041         if (new_y > (output->rect.y + output->rect.height - 25) ||
00042             new_y < (output->rect.y + 25))
00043             return;
00044 
00045         *(params->new_position) = new_y;
00046         xcb_configure_window(conn, params->helpwin, XCB_CONFIG_WINDOW_Y, params->new_position);
00047     }
00048 
00049     xcb_flush(conn);
00050 }
00051 
00052 int resize_graphical_handler(Con *first, Con *second, orientation_t orientation, const xcb_button_press_event_t *event) {
00053     DLOG("resize handler\n");
00054 
00055     uint32_t new_position;
00056 
00057     /* TODO: previously, we were getting a rect containing all screens. why? */
00058     Con *output = con_get_output(first);
00059     DLOG("x = %d, width = %d\n", output->rect.x, output->rect.width);
00060 
00061     x_mask_event_mask(~XCB_EVENT_MASK_ENTER_WINDOW);
00062     xcb_flush(conn);
00063 
00064     uint32_t mask = 0;
00065     uint32_t values[2];
00066 
00067     mask = XCB_CW_OVERRIDE_REDIRECT;
00068     values[0] = 1;
00069 
00070     /* Open a new window, the resizebar. Grab the pointer and move the window around
00071        as the user moves the pointer. */
00072     xcb_window_t grabwin = create_window(conn, output->rect, XCB_COPY_FROM_PARENT, XCB_COPY_FROM_PARENT,
00073             XCB_WINDOW_CLASS_INPUT_ONLY, XCURSOR_CURSOR_POINTER, true, mask, values);
00074 
00075     Rect helprect;
00076     if (orientation == HORIZ) {
00077         helprect.x = event->root_x;
00078         helprect.y = output->rect.y;
00079         helprect.width = 2;
00080         helprect.height = output->rect.height;
00081         new_position = event->root_x;
00082     } else {
00083         helprect.x = output->rect.x;
00084         helprect.y = event->root_y;
00085         helprect.width = output->rect.width;
00086         helprect.height = 2;
00087         new_position = event->root_y;
00088     }
00089 
00090     mask = XCB_CW_BACK_PIXEL;
00091     values[0] = config.client.focused.border;
00092 
00093     mask |= XCB_CW_OVERRIDE_REDIRECT;
00094     values[1] = 1;
00095 
00096     xcb_window_t helpwin = create_window(conn, helprect, XCB_COPY_FROM_PARENT, XCB_COPY_FROM_PARENT,
00097             XCB_WINDOW_CLASS_INPUT_OUTPUT, (orientation == HORIZ ?
00098                                           XCURSOR_CURSOR_RESIZE_HORIZONTAL :
00099                                           XCURSOR_CURSOR_RESIZE_VERTICAL), true, mask, values);
00100 
00101     xcb_circulate_window(conn, XCB_CIRCULATE_RAISE_LOWEST, helpwin);
00102 
00103     xcb_flush(conn);
00104 
00105     const struct callback_params params = { orientation, output, helpwin, &new_position };
00106 
00107     drag_pointer(NULL, event, grabwin, BORDER_TOP, resize_callback, &params);
00108 
00109     xcb_destroy_window(conn, helpwin);
00110     xcb_destroy_window(conn, grabwin);
00111     xcb_flush(conn);
00112 
00113     int pixels;
00114     if (orientation == HORIZ)
00115         pixels = (new_position - event->root_x);
00116     else pixels = (new_position - event->root_y);
00117 
00118     DLOG("Done, pixels = %d\n", pixels);
00119 
00120     // if we got thus far, the containers must have
00121     // percentages associated with them
00122     assert(first->percent > 0.0);
00123     assert(second->percent > 0.0);
00124 
00125     // calculate the new percentage for the first container
00126     double new_percent, difference;
00127     double percent = first->percent;
00128     DLOG("percent = %f\n", percent);
00129     int original = (orientation == HORIZ ? first->rect.width : first->rect.height);
00130     DLOG("original = %d\n", original);
00131     new_percent = (original + pixels) * (percent / original);
00132     difference = percent - new_percent;
00133     DLOG("difference = %f\n", difference);
00134     DLOG("new percent = %f\n", new_percent);
00135     first->percent = new_percent;
00136 
00137     // calculate the new percentage for the second container
00138     double s_percent = second->percent;
00139     second->percent = s_percent + difference;
00140     DLOG("second->percent = %f\n", second->percent);
00141 
00142     // now we must make sure that the sum of the percentages remain 1.0
00143     con_fix_percent(first->parent);
00144 
00145     return 0;
00146 }