1 /* * Copyright (c) 2012-2013 Bastien Dejean
4 * Redistribution and use in source and binary forms, with or without modification,
5 * are permitted provided that the following conditions are met:
7 * * Redistributions of source code must retain the above copyright notice, this
8 * list of conditions and the following disclaimer.
9 * * Redistributions in binary form must reproduce the above copyright notice,
10 * this list of conditions and the following disclaimer in the documentation and/or
11 * other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS''
14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
16 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER 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 ON
20 * 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.
34 void grab_pointer(pointer_action_t pac)
36 PRINTF("grab pointer %u\n", pac);
38 xcb_window_t win = XCB_NONE;
41 query_pointer(&win, &pos);
44 if (locate_window(win, &loc)) {
46 frozen_pointer->position = pos;
47 frozen_pointer->action = pac;
49 frozen_pointer->monitor = loc.monitor;
50 frozen_pointer->desktop = loc.desktop;
51 frozen_pointer->node = loc.node;
52 frozen_pointer->client = c;
53 frozen_pointer->window = c->window;
54 frozen_pointer->horizontal_fence = NULL;
55 frozen_pointer->vertical_fence = NULL;
59 if (loc.node != mon->desk->focus) {
60 bool backup = pointer_follows_monitor;
61 pointer_follows_monitor = false;
62 focus_node(loc.monitor, loc.desktop, loc.node);
63 pointer_follows_monitor = backup;
64 } else if (focus_follows_pointer) {
65 stack(loc.node, STACK_ABOVE);
67 frozen_pointer->action = ACTION_NONE;
70 case ACTION_RESIZE_SIDE:
71 case ACTION_RESIZE_CORNER:
73 frozen_pointer->rectangle = c->floating_rectangle;
74 frozen_pointer->is_tiled = false;
75 } else if (is_tiled(c)) {
76 frozen_pointer->rectangle = c->tiled_rectangle;
77 frozen_pointer->is_tiled = (pac == ACTION_MOVE || !c->pseudo_tiled);
79 frozen_pointer->action = ACTION_NONE;
82 if (pac == ACTION_RESIZE_SIDE) {
83 float W = frozen_pointer->rectangle.width;
84 float H = frozen_pointer->rectangle.height;
86 float x = pos.x - frozen_pointer->rectangle.x;
87 float y = pos.y - frozen_pointer->rectangle.y;
88 float diag_a = ratio * y;
89 float diag_b = W - diag_a;
92 frozen_pointer->side = SIDE_LEFT;
94 frozen_pointer->side = SIDE_BOTTOM;
97 frozen_pointer->side = SIDE_TOP;
99 frozen_pointer->side = SIDE_RIGHT;
101 } else if (pac == ACTION_RESIZE_CORNER) {
102 int16_t mid_x = frozen_pointer->rectangle.x + (frozen_pointer->rectangle.width / 2);
103 int16_t mid_y = frozen_pointer->rectangle.y + (frozen_pointer->rectangle.height / 2);
106 frozen_pointer->corner = CORNER_BOTTOM_RIGHT;
108 frozen_pointer->corner = CORNER_TOP_RIGHT;
111 frozen_pointer->corner = CORNER_BOTTOM_LEFT;
113 frozen_pointer->corner = CORNER_TOP_LEFT;
116 if (frozen_pointer->is_tiled) {
117 if (pac == ACTION_RESIZE_SIDE) {
118 switch (frozen_pointer->side) {
120 frozen_pointer->horizontal_fence = find_fence(loc.node, DIR_UP);
123 frozen_pointer->vertical_fence = find_fence(loc.node, DIR_RIGHT);
126 frozen_pointer->horizontal_fence = find_fence(loc.node, DIR_DOWN);
129 frozen_pointer->vertical_fence = find_fence(loc.node, DIR_LEFT);
132 } else if (pac == ACTION_RESIZE_CORNER) {
133 switch (frozen_pointer->corner) {
134 case CORNER_TOP_LEFT:
135 frozen_pointer->horizontal_fence = find_fence(loc.node, DIR_UP);
136 frozen_pointer->vertical_fence = find_fence(loc.node, DIR_LEFT);
138 case CORNER_TOP_RIGHT:
139 frozen_pointer->horizontal_fence = find_fence(loc.node, DIR_UP);
140 frozen_pointer->vertical_fence = find_fence(loc.node, DIR_RIGHT);
142 case CORNER_BOTTOM_RIGHT:
143 frozen_pointer->horizontal_fence = find_fence(loc.node, DIR_DOWN);
144 frozen_pointer->vertical_fence = find_fence(loc.node, DIR_RIGHT);
146 case CORNER_BOTTOM_LEFT:
147 frozen_pointer->horizontal_fence = find_fence(loc.node, DIR_DOWN);
148 frozen_pointer->vertical_fence = find_fence(loc.node, DIR_LEFT);
152 if (frozen_pointer->horizontal_fence != NULL)
153 frozen_pointer->horizontal_ratio = frozen_pointer->horizontal_fence->split_ratio;
154 if (frozen_pointer->vertical_fence != NULL)
155 frozen_pointer->vertical_ratio = frozen_pointer->vertical_fence->split_ratio;
162 if (pac == ACTION_FOCUS) {
163 monitor_t *m = monitor_from_point(pos);
164 if (m != NULL && m != mon)
165 focus_node(m, m->desk, m->desk->focus);
167 frozen_pointer->action = ACTION_NONE;
171 void track_pointer(int root_x, int root_y)
173 if (frozen_pointer->action == ACTION_NONE)
176 int delta_x, delta_y, x = 0, y = 0, w = 1, h = 1;
178 pointer_action_t pac = frozen_pointer->action;
179 monitor_t *m = frozen_pointer->monitor;
180 desktop_t *d = frozen_pointer->desktop;
181 node_t *n = frozen_pointer->node;
182 client_t *c = frozen_pointer->client;
183 xcb_window_t win = frozen_pointer->window;
184 xcb_rectangle_t rect = frozen_pointer->rectangle;
185 node_t *vertical_fence = frozen_pointer->vertical_fence;
186 node_t *horizontal_fence = frozen_pointer->horizontal_fence;
188 delta_x = root_x - frozen_pointer->position.x;
189 delta_y = root_y - frozen_pointer->position.y;
193 if (frozen_pointer->is_tiled) {
194 xcb_window_t pwin = XCB_NONE;
195 query_pointer(&pwin, NULL);
199 bool is_managed = (pwin == XCB_NONE ? false : locate_window(pwin, &loc));
200 if (is_managed && is_tiled(loc.node->client) && loc.monitor == m) {
201 swap_nodes(m, d, n, m, d, loc.node);
204 if (is_managed && loc.monitor == m) {
206 } else if (!is_managed) {
207 xcb_point_t pt = (xcb_point_t) {root_x, root_y};
208 monitor_t *pmon = monitor_from_point(pt);
209 if (pmon == NULL || pmon == m) {
213 loc.desktop = pmon->desk;
216 bool focused = (n == mon->desk->focus);
217 transfer_node(m, d, n, loc.monitor, loc.desktop, loc.desktop->focus);
219 focus_node(loc.monitor, loc.desktop, n);
220 frozen_pointer->monitor = loc.monitor;
221 frozen_pointer->desktop = loc.desktop;
224 x = rect.x + delta_x;
225 y = rect.y + delta_y;
226 window_move(win, x, y);
227 c->floating_rectangle.x = x;
228 c->floating_rectangle.y = y;
229 xcb_point_t pt = (xcb_point_t) {root_x, root_y};
230 monitor_t *pmon = monitor_from_point(pt);
231 if (pmon == NULL || pmon == m)
233 bool focused = (n == mon->desk->focus);
234 transfer_node(m, d, n, pmon, pmon->desk, pmon->desk->focus);
236 focus_node(pmon, pmon->desk, n);
237 frozen_pointer->monitor = pmon;
238 frozen_pointer->desktop = pmon->desk;
241 case ACTION_RESIZE_SIDE:
242 case ACTION_RESIZE_CORNER:
243 if (frozen_pointer->is_tiled) {
244 if (vertical_fence != NULL) {
245 double sr = frozen_pointer->vertical_ratio + (double) delta_x / vertical_fence->rectangle.width;
248 vertical_fence->split_ratio = sr;
250 if (horizontal_fence != NULL) {
251 double sr = frozen_pointer->horizontal_ratio + (double) delta_y / horizontal_fence->rectangle.height;
254 horizontal_fence->split_ratio = sr;
258 if (pac == ACTION_RESIZE_SIDE) {
259 switch (frozen_pointer->side) {
262 y = rect.y + delta_y;
264 h = rect.height - delta_y;
269 w = rect.width + delta_x;
276 h = rect.height + delta_y;
279 x = rect.x + delta_x;
281 w = rect.width - delta_x;
285 } else if (pac == ACTION_RESIZE_CORNER) {
286 switch (frozen_pointer->corner) {
287 case CORNER_TOP_LEFT:
288 x = rect.x + delta_x;
289 y = rect.y + delta_y;
290 w = rect.width - delta_x;
291 h = rect.height - delta_y;
293 case CORNER_TOP_RIGHT:
295 y = rect.y + delta_y;
296 w = rect.width + delta_x;
297 h = rect.height - delta_y;
299 case CORNER_BOTTOM_LEFT:
300 x = rect.x + delta_x;
302 w = rect.width - delta_x;
303 h = rect.height + delta_y;
305 case CORNER_BOTTOM_RIGHT:
308 w = rect.width + delta_x;
309 h = rect.height + delta_y;
314 int oldw = w, oldh = h;
315 restrain_floating_size(c, &w, &h);
317 if (c->pseudo_tiled) {
318 c->floating_rectangle.width = w;
319 c->floating_rectangle.height = h;
323 c->floating_rectangle.x = x;
324 c->floating_rectangle.width = w;
327 c->floating_rectangle.y = y;
328 c->floating_rectangle.height = h;
330 window_move_resize(win, c->floating_rectangle.x,
331 c->floating_rectangle.y,
332 c->floating_rectangle.width,
333 c->floating_rectangle.height);
343 void ungrab_pointer(void)
345 frozen_pointer->action = ACTION_NONE;