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