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 }