]> git.lizzy.rs Git - bspwm.git/blob - pointer.c
Handle clients outside of every monitors
[bspwm.git] / pointer.c
1 /* * Copyright (c) 2012-2013 Bastien Dejean
2  * All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without modification,
5  * are permitted provided that the following conditions are met:
6  *
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.
12  *
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.
23  */
24
25 #include "bspwm.h"
26 #include "query.h"
27 #include "settings.h"
28 #include "stack.h"
29 #include "tree.h"
30 #include "monitor.h"
31 #include "window.h"
32 #include "pointer.h"
33
34 void grab_pointer(pointer_action_t pac)
35 {
36     PRINTF("grab pointer %u\n", pac);
37
38     xcb_window_t win = XCB_NONE;
39     xcb_point_t pos;
40
41     query_pointer(&win, &pos);
42
43     coordinates_t loc;
44     if (locate_window(win, &loc)) {
45         client_t *c = NULL;
46         frozen_pointer->position = pos;
47         frozen_pointer->action = pac;
48         c = loc.node->client;
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;
56
57         switch (pac)  {
58             case ACTION_FOCUS:
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);
66                 }
67                 frozen_pointer->action = ACTION_NONE;
68                 break;
69             case ACTION_MOVE:
70             case ACTION_RESIZE_SIDE:
71             case ACTION_RESIZE_CORNER:
72                 if (is_tiled(c)) {
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;
78                 } else {
79                     frozen_pointer->action = ACTION_NONE;
80                     return;
81                 }
82                 if (pac == ACTION_RESIZE_SIDE) {
83                     float W = frozen_pointer->rectangle.width;
84                     float H = frozen_pointer->rectangle.height;
85                     float ratio = W / H;
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;
90                     if (x < diag_a) {
91                         if (x < diag_b)
92                             frozen_pointer->side = SIDE_LEFT;
93                         else
94                             frozen_pointer->side = SIDE_BOTTOM;
95                     } else {
96                         if (x < diag_b)
97                             frozen_pointer->side = SIDE_TOP;
98                         else
99                             frozen_pointer->side = SIDE_RIGHT;
100                     }
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);
104                     if (pos.x > mid_x) {
105                         if (pos.y > mid_y)
106                             frozen_pointer->corner = CORNER_BOTTOM_RIGHT;
107                         else
108                             frozen_pointer->corner = CORNER_TOP_RIGHT;
109                     } else {
110                         if (pos.y > mid_y)
111                             frozen_pointer->corner = CORNER_BOTTOM_LEFT;
112                         else
113                             frozen_pointer->corner = CORNER_TOP_LEFT;
114                     }
115                 }
116                 if (frozen_pointer->is_tiled) {
117                     if (pac == ACTION_RESIZE_SIDE) {
118                         switch (frozen_pointer->side) {
119                             case SIDE_TOP:
120                                 frozen_pointer->horizontal_fence = find_fence(loc.node, DIR_UP);
121                                 break;
122                             case SIDE_RIGHT:
123                                 frozen_pointer->vertical_fence = find_fence(loc.node, DIR_RIGHT);
124                                 break;
125                             case SIDE_BOTTOM:
126                                 frozen_pointer->horizontal_fence = find_fence(loc.node, DIR_DOWN);
127                                 break;
128                             case SIDE_LEFT:
129                                 frozen_pointer->vertical_fence = find_fence(loc.node, DIR_LEFT);
130                                 break;
131                         }
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);
137                                 break;
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);
141                                 break;
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);
145                                 break;
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);
149                                 break;
150                         }
151                     }
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;
156                 }
157                 break;
158             case ACTION_NONE:
159                 break;
160         }
161     } else {
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);
166         }
167         frozen_pointer->action = ACTION_NONE;
168     }
169 }
170
171 void track_pointer(int root_x, int root_y)
172 {
173     if (frozen_pointer->action == ACTION_NONE)
174         return;
175
176     int16_t delta_x, delta_y, x = 0, y = 0, w = 1, h = 1;
177     uint16_t width, height;
178
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;
188
189     delta_x = root_x - frozen_pointer->position.x;
190     delta_y = root_y - frozen_pointer->position.y;
191
192     switch (pac) {
193         case ACTION_MOVE:
194             if (frozen_pointer->is_tiled) {
195                 xcb_window_t pwin = XCB_NONE;
196                 query_pointer(&pwin, NULL);
197                 if (pwin == win)
198                     return;
199                 coordinates_t loc;
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);
203                     arrange(m, d);
204                 } else {
205                     if (is_managed && loc.monitor == m) {
206                         return;
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) {
211                             return;
212                         } else {
213                             loc.monitor = pmon;
214                             loc.desktop = pmon->desk;
215                         }
216                     }
217                     bool focused = (n == mon->desk->focus);
218                     transfer_node(m, d, n, loc.monitor, loc.desktop, loc.desktop->focus);
219                     if (focused)
220                         focus_node(loc.monitor, loc.desktop, n);
221                     frozen_pointer->monitor = loc.monitor;
222                     frozen_pointer->desktop = loc.desktop;
223                 }
224             } else {
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)
233                     return;
234                 bool focused = (n == mon->desk->focus);
235                 transfer_node(m, d, n, pmon, pmon->desk, pmon->desk->focus);
236                 if (focused)
237                     focus_node(pmon, pmon->desk, n);
238                 frozen_pointer->monitor = pmon;
239                 frozen_pointer->desktop = pmon->desk;
240             }
241             break;
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;
247                     sr = MAX(0, sr);
248                     sr = MIN(1, sr);
249                     vertical_fence->split_ratio = sr;
250                 }
251                 if (horizontal_fence != NULL) {
252                     double sr = frozen_pointer->horizontal_ratio + (double) delta_y / horizontal_fence->rectangle.height;
253                     sr = MAX(0, sr);
254                     sr = MIN(1, sr);
255                     horizontal_fence->split_ratio = sr;
256                 }
257                 arrange(mon, mon->desk);
258             } else {
259                 if (pac == ACTION_RESIZE_SIDE) {
260                     switch (frozen_pointer->side) {
261                         case SIDE_TOP:
262                             x = rect.x;
263                             y = rect.y + delta_y;
264                             w = rect.width;
265                             h = rect.height - delta_y;
266                             break;
267                         case SIDE_RIGHT:
268                             x = rect.x;
269                             y = rect.y;
270                             w = rect.width + delta_x;
271                             h = rect.height;
272                             break;
273                         case SIDE_BOTTOM:
274                             x = rect.x;
275                             y = rect.y;
276                             w = rect.width;
277                             h = rect.height + delta_y;
278                             break;
279                         case SIDE_LEFT:
280                             x = rect.x + delta_x;
281                             y = rect.y;
282                             w = rect.width - delta_x;
283                             h = rect.height;
284                             break;
285                     }
286                     width = MAX(1, w);
287                     height = MAX(1, h);
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;
298                             break;
299                         case CORNER_TOP_RIGHT:
300                             x = rect.x;
301                             y = rect.y + delta_y;
302                             w = rect.width + delta_x;
303                             h = rect.height - delta_y;
304                             break;
305                         case CORNER_BOTTOM_LEFT:
306                             x = rect.x + delta_x;
307                             y = rect.y;
308                             w = rect.width - delta_x;
309                             h = rect.height + delta_y;
310                             break;
311                         case CORNER_BOTTOM_RIGHT:
312                             x = rect.x;
313                             y = rect.y;
314                             w = rect.width + delta_x;
315                             h = rect.height + delta_y;
316                             break;
317                     }
318                     width = MAX(1, w);
319                     height = MAX(1, h);
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);
323                 }
324             }
325             break;
326         case ACTION_FOCUS:
327         case ACTION_NONE:
328             break;
329     }
330 }
331
332 void ungrab_pointer(void)
333 {
334     frozen_pointer->action = ACTION_NONE;
335 }