• Main Page
  • Data Structures
  • Files
  • File List
  • Globals

src/resize.c

Go to the documentation of this file.
00001 /*
00002  * vim:ts=8:expandtab
00003  *
00004  * i3 - an improved dynamic tiling window manager
00005  *
00006  * © 2009-2010 Michael Stapelberg and contributors
00007  *
00008  * See file LICENSE for license information.
00009  *
00010  * This file contains the functions for resizing table columns/rows because
00011  * it’s actually lots of work, compared to the other handlers.
00012  *
00013  */
00014 #include <stdlib.h>
00015 #include <assert.h>
00016 
00017 #include <xcb/xcb.h>
00018 #include <xcb/xcb_event.h>
00019 
00020 #include "i3.h"
00021 #include "data.h"
00022 #include "resize.h"
00023 #include "util.h"
00024 #include "xcb.h"
00025 #include "debug.h"
00026 #include "layout.h"
00027 #include "randr.h"
00028 #include "config.h"
00029 #include "floating.h"
00030 #include "workspace.h"
00031 #include "log.h"
00032 
00033 /*
00034  * This is an ugly data structure which we need because there is no standard
00035  * way of having nested functions (only available as a gcc extension at the
00036  * moment, clang doesn’t support it) or blocks (only available as a clang
00037  * extension and only on Mac OS X systems at the moment).
00038  *
00039  */
00040 struct callback_params {
00041         resize_orientation_t orientation;
00042         Output *screen;
00043         xcb_window_t helpwin;
00044         uint32_t *new_position;
00045 };
00046 
00047 DRAGGING_CB(resize_callback) {
00048         struct callback_params *params = extra;
00049         Output *screen = params->screen;
00050         DLOG("new x = %d, y = %d\n", new_x, new_y);
00051         if (params->orientation == O_VERTICAL) {
00052                 /* Check if the new coordinates are within screen boundaries */
00053                 if (new_x > (screen->rect.x + screen->rect.width - 25) ||
00054                     new_x < (screen->rect.x + 25))
00055                         return;
00056 
00057                 *(params->new_position) = new_x;
00058                 xcb_configure_window(conn, params->helpwin, XCB_CONFIG_WINDOW_X, params->new_position);
00059         } else {
00060                 if (new_y > (screen->rect.y + screen->rect.height - 25) ||
00061                     new_y < (screen->rect.y + 25))
00062                         return;
00063 
00064                 *(params->new_position) = new_y;
00065                 xcb_configure_window(conn, params->helpwin, XCB_CONFIG_WINDOW_Y, params->new_position);
00066         }
00067 
00068         xcb_flush(conn);
00069 }
00070 
00071 /*
00072  * Renders the resize window between the first/second container and resizes
00073  * the table column/row.
00074  *
00075  */
00076 int resize_graphical_handler(xcb_connection_t *conn, Workspace *ws, int first, int second,
00077                              resize_orientation_t orientation, xcb_button_press_event_t *event) {
00078         uint32_t new_position;
00079         Output *screen = get_output_containing(event->root_x, event->root_y);
00080         if (screen == NULL) {
00081                 ELOG("BUG: No screen found at this position (%d, %d)\n", event->root_x, event->root_y);
00082                 return 1;
00083         }
00084 
00085         /* We cannot use the X root window's width_in_pixels or height_in_pixels
00086          * attributes here since they are not updated when you configure new
00087          * screens during runtime. Instead, we just use the most right and most
00088          * bottom Xinerama screen and use their position + width/height to get
00089          * the area of pixels currently in use */
00090         Output *most_right = get_output_most(D_RIGHT, screen),
00091                *most_bottom = get_output_most(D_DOWN, screen);
00092 
00093         DLOG("event->event_x = %d, event->root_x = %d\n", event->event_x, event->root_x);
00094 
00095         DLOG("Screen dimensions: (%d, %d) %d x %d\n", screen->rect.x, screen->rect.y, screen->rect.width, screen->rect.height);
00096 
00097         uint32_t mask = 0;
00098         uint32_t values[2];
00099 
00100         mask = XCB_CW_OVERRIDE_REDIRECT;
00101         values[0] = 1;
00102 
00103         /* Open a new window, the resizebar. Grab the pointer and move the window around
00104            as the user moves the pointer. */
00105         Rect grabrect = {0,
00106                          0,
00107                          most_right->rect.x + most_right->rect.width,
00108                          most_bottom->rect.x + most_bottom->rect.height};
00109         xcb_window_t grabwin = create_window(conn, grabrect, XCB_WINDOW_CLASS_INPUT_ONLY, -1, true, mask, values);
00110 
00111         Rect helprect;
00112         if (orientation == O_VERTICAL) {
00113                 helprect.x = event->root_x;
00114                 helprect.y = screen->rect.y;
00115                 helprect.width = 2;
00116                 helprect.height = screen->rect.height;
00117                 new_position = event->root_x;
00118         } else {
00119                 helprect.x = screen->rect.x;
00120                 helprect.y = event->root_y;
00121                 helprect.width = screen->rect.width;
00122                 helprect.height = 2;
00123                 new_position = event->root_y;
00124         }
00125 
00126         mask = XCB_CW_BACK_PIXEL;
00127         values[0] = config.client.focused.border;
00128 
00129         mask |= XCB_CW_OVERRIDE_REDIRECT;
00130         values[1] = 1;
00131 
00132         xcb_window_t helpwin = create_window(conn, helprect, XCB_WINDOW_CLASS_INPUT_OUTPUT,
00133                                              (orientation == O_VERTICAL ?
00134                                               XCB_CURSOR_SB_H_DOUBLE_ARROW :
00135                                               XCB_CURSOR_SB_V_DOUBLE_ARROW), true, mask, values);
00136 
00137         xcb_circulate_window(conn, XCB_CIRCULATE_RAISE_LOWEST, helpwin);
00138 
00139         xcb_flush(conn);
00140 
00141         struct callback_params params = { orientation, screen, helpwin, &new_position };
00142 
00143         drag_pointer(conn, NULL, event, grabwin, BORDER_TOP, resize_callback, &params);
00144 
00145         xcb_destroy_window(conn, helpwin);
00146         xcb_destroy_window(conn, grabwin);
00147         xcb_flush(conn);
00148 
00149         int pixels;
00150         if (orientation == O_VERTICAL)
00151                 pixels = (new_position - event->root_x);
00152         else pixels = (new_position - event->root_y);
00153         resize_container(conn, ws, first, second, orientation, pixels);
00154 
00155         return 1;
00156 }
00157 
00158 /*
00159  * Adjusts the container size factors according to the resizing parameters.
00160  * This is an abstraction used by resize_container.
00161  */
00162 static void adjust_container_factors(float *factors, int ws_size, int unoccupied_size,
00163                 int num_items, int first, int second, int pixels) {
00164         /* Find the current sizes */
00165         int sizes[num_items];
00166         for (int i = 0; i < num_items; ++i)
00167                 sizes[i] = factors[i] == 0 ? ws_size / num_items : unoccupied_size * factors[i];
00168 
00169         /* Adjust them */
00170         sizes[first] += pixels;
00171         sizes[second] -= pixels;
00172 
00173         /* Calculate the new unoccupied size */
00174         if (factors[first] == 0) unoccupied_size += ws_size / num_items;
00175         if (factors[second] == 0) unoccupied_size += ws_size / num_items;
00176 
00177         /* Calculate the new factors */
00178         for (int i = 0; i < num_items; ++i) {
00179                 if (factors[i] != 0 || i == first || i == second)
00180                         factors[i] = (float)sizes[i] / unoccupied_size;
00181         }
00182 }
00183 
00184 /*
00185  * Resizes a column/row by the given amount of pixels. Called by
00186  * resize_graphical_handler (the user clicked) or parse_resize_command (the
00187  * user issued the command)
00188  *
00189  */
00190 void resize_container(xcb_connection_t *conn, Workspace *ws, int first, int second,
00191                       resize_orientation_t orientation, int pixels) {
00192         if (orientation == O_VERTICAL) {
00193                 adjust_container_factors(ws->width_factor, ws->rect.width,
00194                                 get_unoccupied_x(ws), ws->cols, first, second, pixels);
00195         }
00196         else {
00197                 adjust_container_factors(ws->height_factor, workspace_height(ws),
00198                                 get_unoccupied_y(ws), ws->rows, first, second, pixels);
00199         }
00200 
00201         render_layout(conn);
00202 }

Generated on Fri Feb 18 2011 for i3 by  doxygen 1.7.1