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-2011 Michael Stapelberg and contributors (see also: LICENSE)
6  *
7  * resize.c: Interactive resizing.
8  *
9  */
10 #include "all.h"
11 
12 extern xcb_connection_t *conn;
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 int resize_graphical_handler(Con *first, Con *second, orientation_t orientation, const xcb_button_press_event_t *event) {
53  DLOG("resize handler\n");
54 
55  uint32_t new_position;
56 
57  /* TODO: previously, we were getting a rect containing all screens. why? */
58  Con *output = con_get_output(first);
59  DLOG("x = %d, width = %d\n", output->rect.x, output->rect.width);
60 
61  x_mask_event_mask(~XCB_EVENT_MASK_ENTER_WINDOW);
62  xcb_flush(conn);
63 
64  uint32_t mask = 0;
65  uint32_t values[2];
66 
67  mask = XCB_CW_OVERRIDE_REDIRECT;
68  values[0] = 1;
69 
70  /* Open a new window, the resizebar. Grab the pointer and move the window around
71  as the user moves the pointer. */
72  xcb_window_t grabwin = create_window(conn, output->rect, XCB_COPY_FROM_PARENT, XCB_COPY_FROM_PARENT,
73  XCB_WINDOW_CLASS_INPUT_ONLY, XCURSOR_CURSOR_POINTER, true, mask, values);
74 
75  Rect helprect;
76  if (orientation == HORIZ) {
77  helprect.x = event->root_x;
78  helprect.y = output->rect.y;
79  helprect.width = 2;
80  helprect.height = output->rect.height;
81  new_position = event->root_x;
82  } else {
83  helprect.x = output->rect.x;
84  helprect.y = event->root_y;
85  helprect.width = output->rect.width;
86  helprect.height = 2;
87  new_position = event->root_y;
88  }
89 
90  mask = XCB_CW_BACK_PIXEL;
91  values[0] = config.client.focused.border;
92 
93  mask |= XCB_CW_OVERRIDE_REDIRECT;
94  values[1] = 1;
95 
96  xcb_window_t helpwin = create_window(conn, helprect, XCB_COPY_FROM_PARENT, XCB_COPY_FROM_PARENT,
97  XCB_WINDOW_CLASS_INPUT_OUTPUT, (orientation == HORIZ ?
99  XCURSOR_CURSOR_RESIZE_VERTICAL), true, mask, values);
100 
101  xcb_circulate_window(conn, XCB_CIRCULATE_RAISE_LOWEST, helpwin);
102 
103  xcb_flush(conn);
104 
105  const struct callback_params params = { orientation, output, helpwin, &new_position };
106 
107  drag_pointer(NULL, event, grabwin, BORDER_TOP, resize_callback, &params);
108 
109  xcb_destroy_window(conn, helpwin);
110  xcb_destroy_window(conn, grabwin);
111  xcb_flush(conn);
112 
113  int pixels;
114  if (orientation == HORIZ)
115  pixels = (new_position - event->root_x);
116  else pixels = (new_position - event->root_y);
117 
118  DLOG("Done, pixels = %d\n", pixels);
119 
120  // if we got thus far, the containers must have
121  // percentages associated with them
122  assert(first->percent > 0.0);
123  assert(second->percent > 0.0);
124 
125  // calculate the new percentage for the first container
126  double new_percent, difference;
127  double percent = first->percent;
128  DLOG("percent = %f\n", percent);
129  int original = (orientation == HORIZ ? first->rect.width : first->rect.height);
130  DLOG("original = %d\n", original);
131  new_percent = (original + pixels) * (percent / original);
132  difference = percent - new_percent;
133  DLOG("difference = %f\n", difference);
134  DLOG("new percent = %f\n", new_percent);
135  first->percent = new_percent;
136 
137  // calculate the new percentage for the second container
138  double s_percent = second->percent;
139  second->percent = s_percent + difference;
140  DLOG("second->percent = %f\n", second->percent);
141 
142  // now we must make sure that the sum of the percentages remain 1.0
143  con_fix_percent(first->parent);
144 
145  return 0;
146 }