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 int resize_graphical_handler(Con *first, Con *second, orientation_t orientation, const xcb_button_press_event_t *event) {
55  DLOG("resize handler\n");
56 
57  uint32_t new_position;
58 
59  /* TODO: previously, we were getting a rect containing all screens. why? */
60  Con *output = con_get_output(first);
61  DLOG("x = %d, width = %d\n", output->rect.x, output->rect.width);
62 
63  x_mask_event_mask(~XCB_EVENT_MASK_ENTER_WINDOW);
64  xcb_flush(conn);
65 
66  uint32_t mask = 0;
67  uint32_t values[2];
68 
69  mask = XCB_CW_OVERRIDE_REDIRECT;
70  values[0] = 1;
71 
72  /* Open a new window, the resizebar. Grab the pointer and move the window around
73  as the user moves the pointer. */
74  xcb_window_t grabwin = create_window(conn, output->rect, XCB_COPY_FROM_PARENT, XCB_COPY_FROM_PARENT,
75  XCB_WINDOW_CLASS_INPUT_ONLY, XCURSOR_CURSOR_POINTER, true, mask, values);
76 
77  Rect helprect;
78  if (orientation == HORIZ) {
79  helprect.x = event->root_x;
80  helprect.y = output->rect.y;
81  helprect.width = 2;
82  helprect.height = output->rect.height;
83  new_position = event->root_x;
84  } else {
85  helprect.x = output->rect.x;
86  helprect.y = event->root_y;
87  helprect.width = output->rect.width;
88  helprect.height = 2;
89  new_position = event->root_y;
90  }
91 
92  mask = XCB_CW_BACK_PIXEL;
93  values[0] = config.client.focused.border;
94 
95  mask |= XCB_CW_OVERRIDE_REDIRECT;
96  values[1] = 1;
97 
98  xcb_window_t helpwin = create_window(conn, helprect, XCB_COPY_FROM_PARENT, XCB_COPY_FROM_PARENT,
99  XCB_WINDOW_CLASS_INPUT_OUTPUT, (orientation == HORIZ ?
101  XCURSOR_CURSOR_RESIZE_VERTICAL), true, mask, values);
102 
103  xcb_circulate_window(conn, XCB_CIRCULATE_RAISE_LOWEST, helpwin);
104 
105  xcb_flush(conn);
106 
107  const struct callback_params params = { orientation, output, helpwin, &new_position };
108 
109  drag_pointer(NULL, event, grabwin, BORDER_TOP, 0, resize_callback, &params);
110 
111  xcb_destroy_window(conn, helpwin);
112  xcb_destroy_window(conn, grabwin);
113  xcb_flush(conn);
114 
115  int pixels;
116  if (orientation == HORIZ)
117  pixels = (new_position - event->root_x);
118  else pixels = (new_position - event->root_y);
119 
120  DLOG("Done, pixels = %d\n", pixels);
121 
122  // if we got thus far, the containers must have
123  // percentages associated with them
124  assert(first->percent > 0.0);
125  assert(second->percent > 0.0);
126 
127  // calculate the new percentage for the first container
128  double new_percent, difference;
129  double percent = first->percent;
130  DLOG("percent = %f\n", percent);
131  int original = (orientation == HORIZ ? first->rect.width : first->rect.height);
132  DLOG("original = %d\n", original);
133  new_percent = (original + pixels) * (percent / original);
134  difference = percent - new_percent;
135  DLOG("difference = %f\n", difference);
136  DLOG("new percent = %f\n", new_percent);
137  first->percent = new_percent;
138 
139  // calculate the new percentage for the second container
140  double s_percent = second->percent;
141  second->percent = s_percent + difference;
142  DLOG("second->percent = %f\n", second->percent);
143 
144  // now we must make sure that the sum of the percentages remain 1.0
145  con_fix_percent(first->parent);
146 
147  return 0;
148 }
#define DLOG(fmt,...)
Definition: log.h:28
A &#39;Con&#39; represents everything from the X11 root window down to a single X11 window.
Definition: data.h:457
Config config
Definition: config.c:19
Definition: data.h:56
Stores a rectangle, for example the size of a window, the child window etc.
Definition: data.h:107
uint32_t y
Definition: data.h:109
#define DRAGGING_CB(name)
Macro to create a callback function for dragging.
Definition: floating.h:19
struct Rect rect
Definition: data.h:492
xcb_window_t helpwin
Definition: resize.c:26
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:1107
int resize_graphical_handler(Con *first, Con *second, orientation_t orientation, const xcb_button_press_event_t *event)
Definition: resize.c:54
struct Config::config_client client
void con_fix_percent(Con *con)
Updates the percent attribute of the children of the given container.
Definition: con.c:533
uint32_t * new_position
Definition: resize.c:27
xcb_connection_t * conn
Definition: main.c:42
uint32_t border
Definition: config.h:54
void 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 lets you drag stuff around (borders).
Definition: floating.c:564
orientation_t
Definition: data.h:56
struct Colortriple focused
Definition: config.h:179
Con * output
Definition: resize.c:25
struct Con * parent
Definition: data.h:490
uint32_t x
Definition: data.h:108
Con * con_get_output(Con *con)
Gets the output container (first container with CT_OUTPUT in hierarchy) this node is on...
Definition: con.c:294
uint32_t height
Definition: data.h:111
double percent
Definition: data.h:508
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
orientation_t orientation
Definition: resize.c:24
uint32_t width
Definition: data.h:110