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