1 /* Copyright (c) 2012, 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.
32 #include "subscribe.h"
35 void grab_pointer(pointer_action_t pac)
37 xcb_window_t win = XCB_NONE;
40 query_pointer(&win, &pos);
43 if (locate_window(win, &loc)) {
44 client_t *c = loc.node->client;
46 frozen_pointer->position = pos;
47 frozen_pointer->action = pac;
48 frozen_pointer->monitor = loc.monitor;
49 frozen_pointer->desktop = loc.desktop;
50 frozen_pointer->node = loc.node;
51 frozen_pointer->client = c;
52 frozen_pointer->window = loc.node->id;
53 frozen_pointer->horizontal_fence = NULL;
54 frozen_pointer->vertical_fence = NULL;
58 if (loc.node != mon->desk->focus) {
59 bool backup = pointer_follows_monitor;
60 pointer_follows_monitor = false;
61 focus_node(loc.monitor, loc.desktop, loc.node);
62 pointer_follows_monitor = backup;
63 } else if (focus_follows_pointer) {
64 stack(loc.desktop, loc.node, true);
66 frozen_pointer->action = ACTION_NONE;
69 case ACTION_RESIZE_SIDE:
70 case ACTION_RESIZE_CORNER:
72 frozen_pointer->rectangle = c->floating_rectangle;
73 frozen_pointer->is_tiled = false;
74 } else if (IS_TILED(c)) {
75 frozen_pointer->rectangle = c->tiled_rectangle;
76 frozen_pointer->is_tiled = (pac == ACTION_MOVE || c->state != STATE_PSEUDO_TILED);
78 frozen_pointer->action = ACTION_NONE;
81 if (pac == ACTION_RESIZE_SIDE) {
82 float W = frozen_pointer->rectangle.width;
83 float H = frozen_pointer->rectangle.height;
85 float x = pos.x - frozen_pointer->rectangle.x;
86 float y = pos.y - frozen_pointer->rectangle.y;
87 float diag_a = ratio * y;
88 float diag_b = W - diag_a;
91 frozen_pointer->side = SIDE_LEFT;
93 frozen_pointer->side = SIDE_BOTTOM;
97 frozen_pointer->side = SIDE_TOP;
99 frozen_pointer->side = SIDE_RIGHT;
102 } else if (pac == ACTION_RESIZE_CORNER) {
103 int16_t mid_x = frozen_pointer->rectangle.x + (frozen_pointer->rectangle.width / 2);
104 int16_t mid_y = frozen_pointer->rectangle.y + (frozen_pointer->rectangle.height / 2);
107 frozen_pointer->corner = CORNER_BOTTOM_RIGHT;
109 frozen_pointer->corner = CORNER_TOP_RIGHT;
113 frozen_pointer->corner = CORNER_BOTTOM_LEFT;
115 frozen_pointer->corner = CORNER_TOP_LEFT;
119 if (frozen_pointer->is_tiled) {
120 if (pac == ACTION_RESIZE_SIDE) {
121 switch (frozen_pointer->side) {
123 frozen_pointer->horizontal_fence = find_fence(loc.node, DIR_NORTH);
126 frozen_pointer->vertical_fence = find_fence(loc.node, DIR_EAST);
129 frozen_pointer->horizontal_fence = find_fence(loc.node, DIR_SOUTH);
132 frozen_pointer->vertical_fence = find_fence(loc.node, DIR_WEST);
135 } else if (pac == ACTION_RESIZE_CORNER) {
136 switch (frozen_pointer->corner) {
137 case CORNER_TOP_LEFT:
138 frozen_pointer->horizontal_fence = find_fence(loc.node, DIR_NORTH);
139 frozen_pointer->vertical_fence = find_fence(loc.node, DIR_WEST);
141 case CORNER_TOP_RIGHT:
142 frozen_pointer->horizontal_fence = find_fence(loc.node, DIR_NORTH);
143 frozen_pointer->vertical_fence = find_fence(loc.node, DIR_EAST);
145 case CORNER_BOTTOM_RIGHT:
146 frozen_pointer->horizontal_fence = find_fence(loc.node, DIR_SOUTH);
147 frozen_pointer->vertical_fence = find_fence(loc.node, DIR_EAST);
149 case CORNER_BOTTOM_LEFT:
150 frozen_pointer->horizontal_fence = find_fence(loc.node, DIR_SOUTH);
151 frozen_pointer->vertical_fence = find_fence(loc.node, DIR_WEST);
155 if (frozen_pointer->horizontal_fence != NULL) {
156 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;
167 if (pac == ACTION_FOCUS) {
168 monitor_t *m = monitor_from_point(pos);
169 if (m != NULL && m != mon) {
170 focus_node(m, m->desk, m->desk->focus);
173 frozen_pointer->action = ACTION_NONE;
177 void track_pointer(int root_x, int root_y)
179 if (frozen_pointer->action == ACTION_NONE) {
183 int delta_x, delta_y, x = 0, y = 0, w = 1, h = 1;
185 pointer_action_t pac = frozen_pointer->action;
186 monitor_t *m = frozen_pointer->monitor;
187 desktop_t *d = frozen_pointer->desktop;
188 node_t *n = frozen_pointer->node;
189 client_t *c = frozen_pointer->client;
190 xcb_window_t win = frozen_pointer->window;
191 xcb_rectangle_t rect = frozen_pointer->rectangle;
192 node_t *vertical_fence = frozen_pointer->vertical_fence;
193 node_t *horizontal_fence = frozen_pointer->horizontal_fence;
195 delta_x = root_x - frozen_pointer->position.x;
196 delta_y = root_y - frozen_pointer->position.y;
200 if (frozen_pointer->is_tiled) {
201 xcb_window_t pwin = XCB_NONE;
202 query_pointer(&pwin, NULL);
207 bool is_managed = (pwin == XCB_NONE ? false : locate_window(pwin, &loc));
208 if (is_managed && !IS_FLOATING(loc.node->client) && loc.monitor == m) {
209 swap_nodes(m, d, n, m, d, loc.node);
211 if (is_managed && loc.monitor == m) {
213 } else if (!is_managed) {
214 xcb_point_t pt = (xcb_point_t) {root_x, root_y};
215 monitor_t *pmon = monitor_from_point(pt);
216 if (pmon == NULL || pmon == m) {
220 loc.desktop = pmon->desk;
223 bool focused = (n == mon->desk->focus);
224 transfer_node(m, d, n, loc.monitor, loc.desktop, loc.desktop->focus);
226 focus_node(loc.monitor, loc.desktop, n);
228 frozen_pointer->monitor = loc.monitor;
229 frozen_pointer->desktop = loc.desktop;
232 x = rect.x + delta_x;
233 y = rect.y + delta_y;
234 window_move(win, x, y);
235 c->floating_rectangle.x = x;
236 c->floating_rectangle.y = y;
237 xcb_point_t pt = (xcb_point_t) {root_x, root_y};
238 monitor_t *pmon = monitor_from_point(pt);
239 if (pmon == NULL || pmon == m) {
242 bool focused = (n == mon->desk->focus);
243 transfer_node(m, d, n, pmon, pmon->desk, pmon->desk->focus);
245 focus_node(pmon, pmon->desk, n);
247 frozen_pointer->monitor = pmon;
248 frozen_pointer->desktop = pmon->desk;
251 case ACTION_RESIZE_SIDE:
252 case ACTION_RESIZE_CORNER:
253 if (frozen_pointer->is_tiled) {
254 if (vertical_fence != NULL) {
255 double sr = frozen_pointer->vertical_ratio + (double) delta_x / vertical_fence->rectangle.width;
258 vertical_fence->split_ratio = sr;
260 if (horizontal_fence != NULL) {
261 double sr = frozen_pointer->horizontal_ratio + (double) delta_y / horizontal_fence->rectangle.height;
264 horizontal_fence->split_ratio = sr;
268 if (pac == ACTION_RESIZE_SIDE) {
269 switch (frozen_pointer->side) {
272 y = rect.y + delta_y;
274 h = rect.height - delta_y;
279 w = rect.width + delta_x;
286 h = rect.height + delta_y;
289 x = rect.x + delta_x;
291 w = rect.width - delta_x;
295 } else if (pac == ACTION_RESIZE_CORNER) {
296 switch (frozen_pointer->corner) {
297 case CORNER_TOP_LEFT:
298 x = rect.x + delta_x;
299 y = rect.y + delta_y;
300 w = rect.width - delta_x;
301 h = rect.height - delta_y;
303 case CORNER_TOP_RIGHT:
305 y = rect.y + delta_y;
306 w = rect.width + delta_x;
307 h = rect.height - delta_y;
309 case CORNER_BOTTOM_LEFT:
310 x = rect.x + delta_x;
312 w = rect.width - delta_x;
313 h = rect.height + delta_y;
315 case CORNER_BOTTOM_RIGHT:
318 w = rect.width + delta_x;
319 h = rect.height + delta_y;
324 int oldw = w, oldh = h;
325 restrain_floating_size(c, &w, &h);
327 if (c->state == STATE_FLOATING) {
329 c->floating_rectangle.x = x;
330 c->floating_rectangle.width = w;
333 c->floating_rectangle.y = y;
334 c->floating_rectangle.height = h;
336 window_move_resize(win, c->floating_rectangle.x,
337 c->floating_rectangle.y,
338 c->floating_rectangle.width,
339 c->floating_rectangle.height);
341 c->floating_rectangle.width = w;
342 c->floating_rectangle.height = h;
353 void ungrab_pointer(void)
355 if (frozen_pointer->action != ACTION_NONE) {
356 xcb_rectangle_t r = get_rectangle(frozen_pointer->monitor, frozen_pointer->desktop, frozen_pointer->node);
357 put_status(SBSC_MASK_NODE_GEOMETRY, "node_geometry %s %s 0x%X %ux%u+%i+%i\n", frozen_pointer->monitor->name, frozen_pointer->desktop->name, frozen_pointer->window, r.width, r.height, r.x, r.y);
359 frozen_pointer->action = ACTION_NONE;