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_WINDOW_CLASS_INPUT_ONLY, XCURSOR_CURSOR_POINTER, true, mask, values);
00073 
00074     Rect helprect;
00075     if (orientation == HORIZ) {
00076         helprect.x = event->root_x;
00077         helprect.y = output->rect.y;
00078         helprect.width = 2;
00079         helprect.height = output->rect.height;
00080         new_position = event->root_x;
00081     } else {
00082         helprect.x = output->rect.x;
00083         helprect.y = event->root_y;
00084         helprect.width = output->rect.width;
00085         helprect.height = 2;
00086         new_position = event->root_y;
00087     }
00088 
00089     mask = XCB_CW_BACK_PIXEL;
00090     values[0] = config.client.focused.border;
00091 
00092     mask |= XCB_CW_OVERRIDE_REDIRECT;
00093     values[1] = 1;
00094 
00095     xcb_window_t helpwin = create_window(conn, helprect, XCB_WINDOW_CLASS_INPUT_OUTPUT,
00096                                          (orientation == HORIZ ?
00097                                           XCURSOR_CURSOR_RESIZE_HORIZONTAL :
00098                                           XCURSOR_CURSOR_RESIZE_VERTICAL), true, mask, values);
00099 
00100     xcb_circulate_window(conn, XCB_CIRCULATE_RAISE_LOWEST, helpwin);
00101 
00102     xcb_flush(conn);
00103 
00104     const struct callback_params params = { orientation, output, helpwin, &new_position };
00105 
00106     drag_pointer(NULL, event, grabwin, BORDER_TOP, resize_callback, &params);
00107 
00108     xcb_destroy_window(conn, helpwin);
00109     xcb_destroy_window(conn, grabwin);
00110     xcb_flush(conn);
00111 
00112     int pixels;
00113     if (orientation == HORIZ)
00114         pixels = (new_position - event->root_x);
00115     else pixels = (new_position - event->root_y);
00116 
00117     DLOG("Done, pixels = %d\n", pixels);
00118 
00119     // if we got thus far, the containers must have
00120     // percentages associated with them
00121     assert(first->percent > 0.0);
00122     assert(second->percent > 0.0);
00123 
00124     // calculate the new percentage for the first container
00125     double new_percent, difference;
00126     double percent = first->percent;
00127     DLOG("percent = %f\n", percent);
00128     int original = (orientation == HORIZ ? first->rect.width : first->rect.height);
00129     DLOG("original = %d\n", original);
00130     new_percent = (original + pixels) * (percent / original);
00131     difference = percent - new_percent;
00132     DLOG("difference = %f\n", difference);
00133     DLOG("new percent = %f\n", new_percent);
00134     first->percent = new_percent;
00135 
00136     // calculate the new percentage for the second container
00137     double s_percent = second->percent;
00138     second->percent = s_percent + difference;
00139     DLOG("second->percent = %f\n", second->percent);
00140 
00141     // now we must make sure that the sum of the percentages remain 1.0
00142     con_fix_percent(first->parent);
00143 
00144     return 0;
00145 }