1 /* Copyright (c) 2012-2014, Bastien Dejean
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
7 * 1. Redistributions of source code must retain the above copyright notice, this
8 * list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright notice,
10 * this list of conditions and the following disclaimer in the documentation
11 * and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
14 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
15 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
16 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
17 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
18 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
19 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
20 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
22 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 * The views and conclusions contained in the software and documentation are those
25 * of the authors and should not be interpreted as representing official policies,
26 * either expressed or implied, of the FreeBSD Project.
38 void grab_pointer(pointer_action_t pac)
40 PRINTF("grab pointer %u\n", pac);
42 xcb_window_t win = XCB_NONE;
45 query_pointer(&win, &pos);
48 if (locate_window(win, &loc)) {
50 frozen_pointer->position = pos;
51 frozen_pointer->action = pac;
53 frozen_pointer->monitor = loc.monitor;
54 frozen_pointer->desktop = loc.desktop;
55 frozen_pointer->node = loc.node;
56 frozen_pointer->client = c;
57 frozen_pointer->window = c->window;
58 frozen_pointer->horizontal_fence = NULL;
59 frozen_pointer->vertical_fence = NULL;
63 if (loc.node != mon->desk->focus) {
64 bool backup = pointer_follows_monitor;
65 pointer_follows_monitor = false;
66 focus_node(loc.monitor, loc.desktop, loc.node);
67 pointer_follows_monitor = backup;
68 } else if (focus_follows_pointer) {
69 stack(loc.node, STACK_ABOVE);
71 frozen_pointer->action = ACTION_NONE;
74 case ACTION_RESIZE_SIDE:
75 case ACTION_RESIZE_CORNER:
77 frozen_pointer->rectangle = c->floating_rectangle;
78 frozen_pointer->is_tiled = false;
79 } else if (is_tiled(c)) {
80 frozen_pointer->rectangle = c->tiled_rectangle;
81 frozen_pointer->is_tiled = (pac == ACTION_MOVE || !c->pseudo_tiled);
83 frozen_pointer->action = ACTION_NONE;
86 if (pac == ACTION_RESIZE_SIDE) {
87 float W = frozen_pointer->rectangle.width;
88 float H = frozen_pointer->rectangle.height;
90 float x = pos.x - frozen_pointer->rectangle.x;
91 float y = pos.y - frozen_pointer->rectangle.y;
92 float diag_a = ratio * y;
93 float diag_b = W - diag_a;
96 frozen_pointer->side = SIDE_LEFT;
98 frozen_pointer->side = SIDE_BOTTOM;
101 frozen_pointer->side = SIDE_TOP;
103 frozen_pointer->side = SIDE_RIGHT;
105 } else if (pac == ACTION_RESIZE_CORNER) {
106 int16_t mid_x = frozen_pointer->rectangle.x + (frozen_pointer->rectangle.width / 2);
107 int16_t mid_y = frozen_pointer->rectangle.y + (frozen_pointer->rectangle.height / 2);
110 frozen_pointer->corner = CORNER_BOTTOM_RIGHT;
112 frozen_pointer->corner = CORNER_TOP_RIGHT;
115 frozen_pointer->corner = CORNER_BOTTOM_LEFT;
117 frozen_pointer->corner = CORNER_TOP_LEFT;
120 if (frozen_pointer->is_tiled) {
121 if (pac == ACTION_RESIZE_SIDE) {
122 switch (frozen_pointer->side) {
124 frozen_pointer->horizontal_fence = find_fence(loc.node, DIR_UP);
127 frozen_pointer->vertical_fence = find_fence(loc.node, DIR_RIGHT);
130 frozen_pointer->horizontal_fence = find_fence(loc.node, DIR_DOWN);
133 frozen_pointer->vertical_fence = find_fence(loc.node, DIR_LEFT);
136 } else if (pac == ACTION_RESIZE_CORNER) {
137 switch (frozen_pointer->corner) {
138 case CORNER_TOP_LEFT:
139 frozen_pointer->horizontal_fence = find_fence(loc.node, DIR_UP);
140 frozen_pointer->vertical_fence = find_fence(loc.node, DIR_LEFT);
142 case CORNER_TOP_RIGHT:
143 frozen_pointer->horizontal_fence = find_fence(loc.node, DIR_UP);
144 frozen_pointer->vertical_fence = find_fence(loc.node, DIR_RIGHT);
146 case CORNER_BOTTOM_RIGHT:
147 frozen_pointer->horizontal_fence = find_fence(loc.node, DIR_DOWN);
148 frozen_pointer->vertical_fence = find_fence(loc.node, DIR_RIGHT);
150 case CORNER_BOTTOM_LEFT:
151 frozen_pointer->horizontal_fence = find_fence(loc.node, DIR_DOWN);
152 frozen_pointer->vertical_fence = find_fence(loc.node, DIR_LEFT);
156 if (frozen_pointer->horizontal_fence != NULL)
157 frozen_pointer->horizontal_ratio = frozen_pointer->horizontal_fence->split_ratio;
158 if (frozen_pointer->vertical_fence != NULL)
159 frozen_pointer->vertical_ratio = frozen_pointer->vertical_fence->split_ratio;
166 if (pac == ACTION_FOCUS) {
167 monitor_t *m = monitor_from_point(pos);
168 if (m != NULL && m != mon)
169 focus_node(m, m->desk, m->desk->focus);
171 frozen_pointer->action = ACTION_NONE;
175 void track_pointer(int root_x, int root_y)
177 if (frozen_pointer->action == ACTION_NONE)
180 int delta_x, delta_y, x = 0, y = 0, w = 1, h = 1;
182 pointer_action_t pac = frozen_pointer->action;
183 monitor_t *m = frozen_pointer->monitor;
184 desktop_t *d = frozen_pointer->desktop;
185 node_t *n = frozen_pointer->node;
186 client_t *c = frozen_pointer->client;
187 xcb_window_t win = frozen_pointer->window;
188 xcb_rectangle_t rect = frozen_pointer->rectangle;
189 node_t *vertical_fence = frozen_pointer->vertical_fence;
190 node_t *horizontal_fence = frozen_pointer->horizontal_fence;
192 delta_x = root_x - frozen_pointer->position.x;
193 delta_y = root_y - frozen_pointer->position.y;
197 if (frozen_pointer->is_tiled) {
198 xcb_window_t pwin = XCB_NONE;
199 query_pointer(&pwin, NULL);
203 bool is_managed = (pwin == XCB_NONE ? false : locate_window(pwin, &loc));
204 if (is_managed && is_tiled(loc.node->client) && loc.monitor == m) {
205 swap_nodes(m, d, n, m, d, loc.node);
208 if (is_managed && loc.monitor == m) {
210 } else if (!is_managed) {
211 xcb_point_t pt = (xcb_point_t) {root_x, root_y};
212 monitor_t *pmon = monitor_from_point(pt);
213 if (pmon == NULL || pmon == m) {
217 loc.desktop = pmon->desk;
220 bool focused = (n == mon->desk->focus);
221 transfer_node(m, d, n, loc.monitor, loc.desktop, loc.desktop->focus);
223 focus_node(loc.monitor, loc.desktop, n);
224 frozen_pointer->monitor = loc.monitor;
225 frozen_pointer->desktop = loc.desktop;
228 x = rect.x + delta_x;
229 y = rect.y + delta_y;
230 window_move(win, x, y);
231 c->floating_rectangle.x = x;
232 c->floating_rectangle.y = y;
233 xcb_point_t pt = (xcb_point_t) {root_x, root_y};
234 monitor_t *pmon = monitor_from_point(pt);
235 if (pmon == NULL || pmon == m)
237 bool focused = (n == mon->desk->focus);
238 transfer_node(m, d, n, pmon, pmon->desk, pmon->desk->focus);
240 focus_node(pmon, pmon->desk, n);
241 frozen_pointer->monitor = pmon;
242 frozen_pointer->desktop = pmon->desk;
245 case ACTION_RESIZE_SIDE:
246 case ACTION_RESIZE_CORNER:
247 if (frozen_pointer->is_tiled) {
248 if (vertical_fence != NULL) {
249 double sr = frozen_pointer->vertical_ratio + (double) delta_x / vertical_fence->rectangle.width;
252 vertical_fence->split_ratio = sr;
254 if (horizontal_fence != NULL) {
255 double sr = frozen_pointer->horizontal_ratio + (double) delta_y / horizontal_fence->rectangle.height;
258 horizontal_fence->split_ratio = sr;
262 if (pac == ACTION_RESIZE_SIDE) {
263 switch (frozen_pointer->side) {
266 y = rect.y + delta_y;
268 h = rect.height - delta_y;
273 w = rect.width + delta_x;
280 h = rect.height + delta_y;
283 x = rect.x + delta_x;
285 w = rect.width - delta_x;
289 } else if (pac == ACTION_RESIZE_CORNER) {
290 switch (frozen_pointer->corner) {
291 case CORNER_TOP_LEFT:
292 x = rect.x + delta_x;
293 y = rect.y + delta_y;
294 w = rect.width - delta_x;
295 h = rect.height - delta_y;
297 case CORNER_TOP_RIGHT:
299 y = rect.y + delta_y;
300 w = rect.width + delta_x;
301 h = rect.height - delta_y;
303 case CORNER_BOTTOM_LEFT:
304 x = rect.x + delta_x;
306 w = rect.width - delta_x;
307 h = rect.height + delta_y;
309 case CORNER_BOTTOM_RIGHT:
312 w = rect.width + delta_x;
313 h = rect.height + delta_y;
318 int oldw = w, oldh = h;
319 restrain_floating_size(c, &w, &h);
321 if (c->pseudo_tiled) {
322 c->floating_rectangle.width = w;
323 c->floating_rectangle.height = h;
327 c->floating_rectangle.x = x;
328 c->floating_rectangle.width = w;
331 c->floating_rectangle.y = y;
332 c->floating_rectangle.height = h;
334 window_move_resize(win, c->floating_rectangle.x,
335 c->floating_rectangle.y,
336 c->floating_rectangle.width,
337 c->floating_rectangle.height);
347 void ungrab_pointer(void)
349 frozen_pointer->action = ACTION_NONE;