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