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, bool both_sides) {
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 = orientation_from_direction(direction);
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  if (second == NULL && both_sides == true) {
78  second = TAILQ_NEXT(first, nodes);
79  }
80  } else {
81  second = TAILQ_NEXT(first, nodes);
82  if (second == NULL && both_sides == true) {
83  second = TAILQ_PREV(first, nodes_head, nodes);
84  }
85  }
86 
87  if (second == NULL) {
88  DLOG("No second container in this direction found, trying to look further up in the tree...\n");
89  first = first->parent;
90  }
91  }
92 
93  DLOG("Found participants: first=%p and second=%p.\n", first, second);
94  *current = first;
95  *other = second;
96  if (first == NULL || second == NULL) {
97  DLOG("Could not find two participants for this resize request.\n");
98  return false;
99  }
100 
101  return true;
102 }
103 
104 /*
105  * Calculate the given container's new percent given a change in pixels.
106  *
107  */
108 double px_resize_to_percent(Con *con, int px_diff) {
109  Con *parent = con->parent;
110  const orientation_t o = con_orientation(parent);
111  const int total = (o == HORIZ ? parent->rect.width : parent->rect.height);
112  /* deco_rect.height is subtracted from each child in render_con_split */
113  const int target = px_diff + (o == HORIZ ? con->rect.width : con->rect.height + con->deco_rect.height);
114  return ((double)target / (double)total);
115 }
116 
117 /*
118  * Calculate the minimum percent needed for the given container to be at least 1
119  * pixel.
120  *
121  */
122 double percent_for_1px(Con *con) {
123  Con *parent = con->parent;
124  const orientation_t o = con_orientation(parent);
125  const int total = (o == HORIZ ? parent->rect.width : parent->rect.height);
126  const int target = (o == HORIZ ? 1 : 1 + con->deco_rect.height);
127  return ((double)target / (double)total);
128 }
129 
130 /*
131  * Resize the two given containers using the given amount of pixels or
132  * percentage points. One of the two needs to be 0. A positive amount means
133  * growing the first container while a negative means shrinking it.
134  * Returns false when the resize would result in one of the two containers
135  * having less than 1 pixel of size.
136  *
137  */
138 bool resize_neighboring_cons(Con *first, Con *second, int px, int ppt) {
139  assert(px * ppt == 0);
140 
141  Con *parent = first->parent;
142  double new_first_percent;
143  double new_second_percent;
144  if (ppt) {
145  new_first_percent = first->percent + ((double)ppt / 100.0);
146  new_second_percent = second->percent - ((double)ppt / 100.0);
147  } else {
148  new_first_percent = px_resize_to_percent(first, px);
149  new_second_percent = second->percent + first->percent - new_first_percent;
150  }
151  /* Ensure that no container will be less than 1 pixel in the resizing
152  * direction. */
153  if (new_first_percent < percent_for_1px(first) || new_second_percent < percent_for_1px(second)) {
154  return false;
155  }
156 
157  first->percent = new_first_percent;
158  second->percent = new_second_percent;
159  con_fix_percent(parent);
160  return true;
161 }
162 
163 void resize_graphical_handler(Con *first, Con *second, orientation_t orientation, const xcb_button_press_event_t *event) {
164  Con *output = con_get_output(first);
165  DLOG("x = %d, width = %d\n", output->rect.x, output->rect.width);
166 
167  x_mask_event_mask(~XCB_EVENT_MASK_ENTER_WINDOW);
168  xcb_flush(conn);
169 
170  uint32_t mask = 0;
171  uint32_t values[2];
172 
173  mask = XCB_CW_OVERRIDE_REDIRECT;
174  values[0] = 1;
175 
176  /* Open a new window, the resizebar. Grab the pointer and move the window
177  * around as the user moves the pointer. */
178  xcb_window_t grabwin = create_window(conn, output->rect, XCB_COPY_FROM_PARENT, XCB_COPY_FROM_PARENT,
179  XCB_WINDOW_CLASS_INPUT_ONLY, XCURSOR_CURSOR_POINTER, true, mask, values);
180 
181  /* Keep track of the coordinate orthogonal to motion so we can determine the
182  * length of the resize afterward. */
183  uint32_t initial_position, new_position;
184 
185  /* Configure the resizebar and snap the pointer. The resizebar runs along
186  * the rect of the second con and follows the motion of the pointer. */
187  Rect helprect;
188  helprect.x = second->rect.x;
189  helprect.y = second->rect.y;
190  if (orientation == HORIZ) {
191  helprect.width = logical_px(2);
192  helprect.height = second->rect.height;
193  initial_position = second->rect.x;
194  xcb_warp_pointer(conn, XCB_NONE, event->root, 0, 0, 0, 0,
195  second->rect.x, event->root_y);
196  } else {
197  helprect.width = second->rect.width;
198  helprect.height = logical_px(2);
199  initial_position = second->rect.y;
200  xcb_warp_pointer(conn, XCB_NONE, event->root, 0, 0, 0, 0,
201  event->root_x, second->rect.y);
202  }
203 
204  mask = XCB_CW_BACK_PIXEL;
206 
207  mask |= XCB_CW_OVERRIDE_REDIRECT;
208  values[1] = 1;
209 
210  xcb_window_t helpwin = create_window(conn, helprect, XCB_COPY_FROM_PARENT, XCB_COPY_FROM_PARENT,
211  XCB_WINDOW_CLASS_INPUT_OUTPUT, (orientation == HORIZ ? XCURSOR_CURSOR_RESIZE_HORIZONTAL : XCURSOR_CURSOR_RESIZE_VERTICAL), true, mask, values);
212 
213  xcb_circulate_window(conn, XCB_CIRCULATE_RAISE_LOWEST, helpwin);
214 
215  xcb_flush(conn);
216 
217  /* `new_position' will be updated by the `resize_callback'. */
218  new_position = initial_position;
219 
220  const struct callback_params params = {orientation, output, helpwin, &new_position};
221 
222  /* `drag_pointer' blocks until the drag is completed. */
223  drag_result_t drag_result = drag_pointer(NULL, event, grabwin, BORDER_TOP, 0, resize_callback, &params);
224 
225  xcb_destroy_window(conn, helpwin);
226  xcb_destroy_window(conn, grabwin);
227  xcb_flush(conn);
228 
229  /* User cancelled the drag so no action should be taken. */
230  if (drag_result == DRAG_REVERT) {
231  return;
232  }
233 
234  int pixels = (new_position - initial_position);
235  DLOG("Done, pixels = %d\n", pixels);
236 
237  /* if we got thus far, the containers must have valid percentages. */
238  assert(first->percent > 0.0);
239  assert(second->percent > 0.0);
240  const bool result = resize_neighboring_cons(first, second, pixels, 0);
241  DLOG("Graphical resize %s: first->percent = %f, second->percent = %f.\n",
242  result ? "successful" : "failed", first->percent, second->percent);
243 }
void con_fix_percent(Con *con)
Updates the percent attribute of the children of the given container.
Definition: con.c:952
double percent_for_1px(Con *con)
Calculate the minimum percent needed for the given container to be at least 1 pixel.
Definition: resize.c:122
orientation_t orientation_from_direction(direction_t direction)
Convert a direction to its corresponding orientation.
Definition: util.c:514
uint32_t height
Definition: data.h:178
uint32_t * new_position
Definition: resize.c:23
int logical_px(const int logical)
Convert a logical amount of pixels (e.g.
struct Con * parent
Definition: data.h:662
bool resize_find_tiling_participants(Con **current, Con **other, direction_t direction, bool both_sides)
Definition: resize.c:50
double percent
Definition: data.h:692
#define DLOG(fmt,...)
Definition: libi3.h:104
struct Rect deco_rect
Definition: data.h:672
xcb_connection_t * conn
XCB connection and root screen.
Definition: main.c:44
Definition: data.h:60
uint32_t colorpixel
Definition: libi3.h:422
bool resize_neighboring_cons(Con *first, Con *second, int px, int ppt)
Resize the two given containers using the given amount of pixels or percentage points.
Definition: resize.c:138
uint32_t y
Definition: data.h:176
orientation_t orientation
Definition: resize.c:20
uint32_t width
Definition: data.h:177
Definition: data.h:57
Con * con_get_output(Con *con)
Gets the output container (first container with CT_OUTPUT in hierarchy) this node is on...
Definition: con.c:405
Stores a rectangle, for example the size of a window, the child window etc.
Definition: data.h:174
orientation_t con_orientation(Con *con)
Returns the orientation of the given container (for stacked containers, vertical orientation is used ...
Definition: con.c:1422
#define TAILQ_NEXT(elm, field)
Definition: queue.h:338
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:810
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:1343
orientation_t
Definition: data.h:59
struct Rect rect
Definition: data.h:666
enum Con::@20 type
void resize_graphical_handler(Con *first, Con *second, orientation_t orientation, const xcb_button_press_event_t *event)
Definition: resize.c:163
struct Config::config_client client[QUBE_NUM_LABELS]
xcb_window_t helpwin
Definition: resize.c:22
layout_t layout
Definition: data.h:740
color_t border
Definition: configuration.h:55
A &#39;Con&#39; represents everything from the X11 root window down to a single X11 window.
Definition: data.h:630
Con * output
Definition: resize.c:21
uint32_t x
Definition: data.h:175
DRAGGING_CB(resize_callback)
Definition: resize.c:26
#define TAILQ_PREV(elm, headname, field)
Definition: queue.h:342
Definition: data.h:94
direction_t
Definition: data.h:55
Definition: data.h:55
Definition: data.h:93
Config config
Definition: config.c:17
drag_result_t
This is the return value of a drag operation like drag_pointer.
Definition: floating.h:120
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
double px_resize_to_percent(Con *con, int px_diff)
Calculate the given container&#39;s new percent given a change in pixels.
Definition: resize.c:108
struct Colortriple focused