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->tiled_rectangle;
74 frozen_pointer->is_tiled = true;
75 } else if (is_floating(c)) {
76 frozen_pointer->rectangle = c->floating_rectangle;
77 frozen_pointer->is_tiled = false;
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 int16_t delta_x, delta_y, x = 0, y = 0, w = 1, h = 1;
177 uint16_t width, height;
179 pointer_action_t pac = frozen_pointer->action;
180 monitor_t *m = frozen_pointer->monitor;
181 desktop_t *d = frozen_pointer->desktop;
182 node_t *n = frozen_pointer->node;
183 client_t *c = frozen_pointer->client;
184 xcb_window_t win = frozen_pointer->window;
185 xcb_rectangle_t rect = frozen_pointer->rectangle;
186 node_t *vertical_fence = frozen_pointer->vertical_fence;
187 node_t *horizontal_fence = frozen_pointer->horizontal_fence;
189 delta_x = root_x - frozen_pointer->position.x;
190 delta_y = root_y - frozen_pointer->position.y;
194 if (frozen_pointer->is_tiled) {
195 xcb_window_t pwin = XCB_NONE;
196 query_pointer(&pwin, NULL);
200 bool is_managed = (pwin == XCB_NONE ? false : locate_window(pwin, &loc));
201 if (is_managed && is_tiled(loc.node->client) && loc.monitor == m) {
202 swap_nodes(m, d, n, m, d, loc.node);
205 if (is_managed && loc.monitor == m) {
207 } else if (!is_managed) {
208 xcb_point_t pt = (xcb_point_t) {root_x, root_y};
209 monitor_t *pmon = monitor_from_point(pt);
210 if (pmon == NULL || pmon == m) {
214 loc.desktop = pmon->desk;
217 bool focused = (n == mon->desk->focus);
218 transfer_node(m, d, n, loc.monitor, loc.desktop, loc.desktop->focus);
220 focus_node(loc.monitor, loc.desktop, n);
221 frozen_pointer->monitor = loc.monitor;
222 frozen_pointer->desktop = loc.desktop;
225 x = rect.x + delta_x;
226 y = rect.y + delta_y;
227 window_move(win, x, y);
228 c->floating_rectangle.x = x;
229 c->floating_rectangle.y = y;
230 xcb_point_t pt = (xcb_point_t) {root_x, root_y};
231 monitor_t *pmon = monitor_from_point(pt);
232 if (pmon == NULL || pmon == m)
234 bool focused = (n == mon->desk->focus);
235 transfer_node(m, d, n, pmon, pmon->desk, pmon->desk->focus);
237 focus_node(pmon, pmon->desk, n);
238 frozen_pointer->monitor = pmon;
239 frozen_pointer->desktop = pmon->desk;
242 case ACTION_RESIZE_SIDE:
243 case ACTION_RESIZE_CORNER:
244 if (frozen_pointer->is_tiled) {
245 if (vertical_fence != NULL) {
246 double sr = frozen_pointer->vertical_ratio + (double) delta_x / vertical_fence->rectangle.width;
249 vertical_fence->split_ratio = sr;
251 if (horizontal_fence != NULL) {
252 double sr = frozen_pointer->horizontal_ratio + (double) delta_y / horizontal_fence->rectangle.height;
255 horizontal_fence->split_ratio = sr;
257 arrange(mon, mon->desk);
259 if (pac == ACTION_RESIZE_SIDE) {
260 switch (frozen_pointer->side) {
263 y = rect.y + delta_y;
265 h = rect.height - delta_y;
270 w = rect.width + delta_x;
277 h = rect.height + delta_y;
280 x = rect.x + delta_x;
282 w = rect.width - delta_x;
288 window_move_resize(win, x, y, width, height);
289 c->floating_rectangle = (xcb_rectangle_t) {x, y, width, height};
290 window_draw_border(n, d->focus == n, mon == m);
291 } else if (pac == ACTION_RESIZE_CORNER) {
292 switch (frozen_pointer->corner) {
293 case CORNER_TOP_LEFT:
294 x = rect.x + delta_x;
295 y = rect.y + delta_y;
296 w = rect.width - delta_x;
297 h = rect.height - delta_y;
299 case CORNER_TOP_RIGHT:
301 y = rect.y + delta_y;
302 w = rect.width + delta_x;
303 h = rect.height - delta_y;
305 case CORNER_BOTTOM_LEFT:
306 x = rect.x + delta_x;
308 w = rect.width - delta_x;
309 h = rect.height + delta_y;
311 case CORNER_BOTTOM_RIGHT:
314 w = rect.width + delta_x;
315 h = rect.height + delta_y;
320 window_move_resize(win, x, y, width, height);
321 c->floating_rectangle = (xcb_rectangle_t) {x, y, width, height};
322 window_draw_border(n, d->focus == n, mon == m);
332 void ungrab_pointer(void)
334 frozen_pointer->action = ACTION_NONE;