i3
resize.c
Go to the documentation of this file.
1 /*
2  * vim:ts=4:sw=4:expandtab
3  *
4  * i3 - an improved dynamic tiling window manager
5  * © 2009 Michael Stapelberg and contributors (see also: LICENSE)
6  *
7  * resize.c: Interactive resizing.
8  *
9  */
10 #include "all.h"
11 
12 /*
13  * This is an ugly data structure which we need because there is no standard
14  * way of having nested functions (only available as a gcc extension at the
15  * moment, clang doesn’t support it) or blocks (only available as a clang
16  * extension and only on Mac OS X systems at the moment).
17  *
18  */
22  xcb_window_t helpwin;
23  uint32_t *new_position;
24 };
25 
26 DRAGGING_CB(resize_callback) {
27  const struct callback_params *params = extra;
28  Con *output = params->output;
29  DLOG("new x = %d, y = %d\n", new_x, new_y);
30  if (params->orientation == HORIZ) {
31  /* Check if the new coordinates are within screen boundaries */
32  if (new_x > (output->rect.x + output->rect.width - 25) ||
33  new_x < (output->rect.x + 25))
34  return;
35 
36  *(params->new_position) = new_x;
37  xcb_configure_window(conn, params->helpwin, XCB_CONFIG_WINDOW_X, params->new_position);
38  } else {
39  if (new_y > (output->rect.y + output->rect.height - 25) ||
40  new_y < (output->rect.y + 25))
41  return;
42 
43  *(params->new_position) = new_y;
44  xcb_configure_window(conn, params->helpwin, XCB_CONFIG_WINDOW_Y, params->new_position);
45  }
46 
47  xcb_flush(conn);
48 }
49 
50 bool resize_find_tiling_participants(Con **current, Con **other, direction_t direction) {
51  DLOG("Find two participants for resizing container=%p in direction=%i\n", other, direction);
52  Con *first = *current;
53  Con *second = NULL;
54  if (first == NULL) {
55  DLOG("Current container is NULL, aborting.\n");
56  return false;
57  }
58 
59  /* Go up in the tree and search for a container to resize */
60  const orientation_t search_orientation = ((direction == D_LEFT || direction == D_RIGHT) ? HORIZ : VERT);
61  const bool dir_backwards = (direction == D_UP || direction == D_LEFT);
62  while (first->type != CT_WORKSPACE &&
63  first->type != CT_FLOATING_CON &&
64  second == NULL) {
65  /* get the appropriate first container with the matching
66  * orientation (skip stacked/tabbed cons) */
67  if ((con_orientation(first->parent) != search_orientation) ||
68  (first->parent->layout == L_STACKED) ||
69  (first->parent->layout == L_TABBED)) {
70  first = first->parent;
71  continue;
72  }
73 
74  /* get the counterpart for this resizement */
75  if (dir_backwards) {
76  second = TAILQ_PREV(first, nodes_head, nodes);
77  } else {
78  second = TAILQ_NEXT(first, nodes);
79  }
80 
81  if (second == NULL) {
82  DLOG("No second container in this direction found, trying to look further up in the tree...\n");
83  first = first->parent;
84  }
85  }
86 
87  DLOG("Found participants: first=%p and second=%p.\n", first, second);
88  *current = first;
89  *other = second;
90  if (first == NULL || second == NULL) {
91  DLOG("Could not find two participants for this resize request.\n");
92  return false;
93  }
94 
95  return true;
96 }
97 
98 int resize_graphical_handler(Con *first, Con *second, orientation_t orientation, const xcb_button_press_event_t *event) {
99  DLOG("resize handler\n");
100 
101  /* TODO: previously, we were getting a rect containing all screens. why? */
102  Con *output = con_get_output(first);
103  DLOG("x = %d, width = %d\n", output->rect.x, output->rect.width);
104 
105  x_mask_event_mask(~XCB_EVENT_MASK_ENTER_WINDOW);
106  xcb_flush(conn);
107 
108  uint32_t mask = 0;
109  uint32_t values[2];
110 
111  mask = XCB_CW_OVERRIDE_REDIRECT;
112  values[0] = 1;
113 
114  /* Open a new window, the resizebar. Grab the pointer and move the window around
115  as the user moves the pointer. */
116  xcb_window_t grabwin = create_window(conn, output->rect, XCB_COPY_FROM_PARENT, XCB_COPY_FROM_PARENT,
117  XCB_WINDOW_CLASS_INPUT_ONLY, XCURSOR_CURSOR_POINTER, true, mask, values);
118 
119  /* Keep track of the coordinate orthogonal to motion so we can determine
120  * the length of the resize afterward. */
121  uint32_t initial_position, new_position;
122 
123  /* Configure the resizebar and snap the pointer. The resizebar runs along
124  * the rect of the second con and follows the motion of the pointer. */
125  Rect helprect;
126  if (orientation == HORIZ) {
127  helprect.x = second->rect.x;
128  helprect.y = second->rect.y;
129  helprect.width = logical_px(2);
130  helprect.height = second->rect.height;
131  initial_position = second->rect.x;
132  xcb_warp_pointer(conn, XCB_NONE, event->root, 0, 0, 0, 0,
133  second->rect.x, event->root_y);
134  } else {
135  helprect.x = second->rect.x;
136  helprect.y = second->rect.y;
137  helprect.width = second->rect.width;
138  helprect.height = logical_px(2);
139  initial_position = second->rect.y;
140  xcb_warp_pointer(conn, XCB_NONE, event->root, 0, 0, 0, 0,
141  event->root_x, second->rect.y);
142  }
143 
144  mask = XCB_CW_BACK_PIXEL;
145  values[0] = config.client.focused.border.colorpixel;
146 
147  mask |= XCB_CW_OVERRIDE_REDIRECT;
148  values[1] = 1;
149 
150  xcb_window_t helpwin = create_window(conn, helprect, XCB_COPY_FROM_PARENT, XCB_COPY_FROM_PARENT,
151  XCB_WINDOW_CLASS_INPUT_OUTPUT, (orientation == HORIZ ? XCURSOR_CURSOR_RESIZE_HORIZONTAL : XCURSOR_CURSOR_RESIZE_VERTICAL), true, mask, values);
152 
153  xcb_circulate_window(conn, XCB_CIRCULATE_RAISE_LOWEST, helpwin);
154 
155  xcb_flush(conn);
156 
157  /* `new_position' will be updated by the `resize_callback'. */
158  new_position = initial_position;
159 
160  const struct callback_params params = {orientation, output, helpwin, &new_position};
161 
162  /* `drag_pointer' blocks until the drag is completed. */
163  drag_result_t drag_result = drag_pointer(NULL, event, grabwin, BORDER_TOP, 0, resize_callback, &params);
164 
165  xcb_destroy_window(conn, helpwin);
166  xcb_destroy_window(conn, grabwin);
167  xcb_flush(conn);
168 
169  /* User cancelled the drag so no action should be taken. */
170  if (drag_result == DRAG_REVERT)
171  return 0;
172 
173  int pixels = (new_position - initial_position);
174 
175  DLOG("Done, pixels = %d\n", pixels);
176 
177  // if we got thus far, the containers must have
178  // percentages associated with them
179  assert(first->percent > 0.0);
180  assert(second->percent > 0.0);
181 
182  // calculate the new percentage for the first container
183  double new_percent, difference;
184  double percent = first->percent;
185  DLOG("percent = %f\n", percent);
186  int original = (orientation == HORIZ ? first->rect.width : first->rect.height);
187  DLOG("original = %d\n", original);
188  new_percent = (original + pixels) * (percent / original);
189  difference = percent - new_percent;
190  DLOG("difference = %f\n", difference);
191  DLOG("new percent = %f\n", new_percent);
192  first->percent = new_percent;
193 
194  // calculate the new percentage for the second container
195  double s_percent = second->percent;
196  second->percent = s_percent + difference;
197  DLOG("second->percent = %f\n", second->percent);
198 
199  // now we must make sure that the sum of the percentages remain 1.0
200  con_fix_percent(first->parent);
201 
202  return 0;
203 }
void con_fix_percent(Con *con)
Updates the percent attribute of the children of the given container.
Definition: con.c:861
drag_result_t
This is the return value of a drag operation like drag_pointer.
Definition: floating.h:120
void x_mask_event_mask(uint32_t mask)
Applies the given mask to the event mask of every i3 window decoration X11 window.
Definition: x.c:1266
direction_t
Definition: data.h:55
A &#39;Con&#39; represents everything from the X11 root window down to a single X11 window.
Definition: data.h:591
#define TAILQ_PREV(elm, headname, field)
Definition: queue.h:342
Definition: data.h:93
struct Rect rect
Definition: data.h:627
xcb_connection_t * conn
XCB connection and root screen.
Definition: main.c:46
int resize_graphical_handler(Con *first, Con *second, orientation_t orientation, const xcb_button_press_event_t *event)
Definition: resize.c:98
enum Con::@20 type
Definition: data.h:57
orientation_t orientation
Definition: resize.c:20
xcb_window_t create_window(xcb_connection_t *conn, Rect dims, uint16_t depth, xcb_visualid_t visual, uint16_t window_class, enum xcursor_cursor_t cursor, bool map, uint32_t mask, uint32_t *values)
Convenience wrapper around xcb_create_window which takes care of depth, generating an ID and checking...
Definition: xcb.c:19
Definition: data.h:60
struct Config::config_client client
uint32_t * new_position
Definition: resize.c:23
uint32_t x
Definition: data.h:149
Con * con_get_output(Con *con)
Gets the output container (first container with CT_OUTPUT in hierarchy) this node is on...
Definition: con.c:391
bool resize_find_tiling_participants(Con **current, Con **other, direction_t direction)
Definition: resize.c:50
Con * output
Definition: resize.c:21
orientation_t con_orientation(Con *con)
Returns the orientation of the given container (for stacked containers, vertical orientation is used ...
Definition: con.c:1317
drag_result_t drag_pointer(Con *con, const xcb_button_press_event_t *event, xcb_window_t confine_to, border_t border, int cursor, callback_t callback, const void *extra)
This function grabs your pointer and keyboard and lets you drag stuff around (borders).
Definition: floating.c:778
uint32_t colorpixel
Definition: libi3.h:409
layout_t layout
Definition: data.h:701
xcb_window_t helpwin
Definition: resize.c:22
Definition: data.h:61
Definition: data.h:55
struct Colortriple focused
uint32_t y
Definition: data.h:150
double percent
Definition: data.h:653
uint32_t height
Definition: data.h:152
int logical_px(const int logical)
Convert a logical amount of pixels (e.g.
uint32_t width
Definition: data.h:151
#define DLOG(fmt,...)
Definition: libi3.h:104
Definition: data.h:94
Definition: data.h:56
#define TAILQ_NEXT(elm, field)
Definition: queue.h:338
DRAGGING_CB(resize_callback)
Definition: resize.c:26
orientation_t
Definition: data.h:59
Config config
Definition: config.c:17
color_t border
Definition: configuration.h:55
Stores a rectangle, for example the size of a window, the child window etc.
Definition: data.h:148
struct Con * parent
Definition: data.h:623