]> git.lizzy.rs Git - bspwm.git/blob - pointer.c
Handle min/max window size hints
[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_floating(c)) {
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);
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     int delta_x, delta_y, x = 0, y = 0, w = 1, h = 1;
177
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;
187
188     delta_x = root_x - frozen_pointer->position.x;
189     delta_y = root_y - frozen_pointer->position.y;
190
191     switch (pac) {
192         case ACTION_MOVE:
193             if (frozen_pointer->is_tiled) {
194                 xcb_window_t pwin = XCB_NONE;
195                 query_pointer(&pwin, NULL);
196                 if (pwin == win)
197                     return;
198                 coordinates_t loc;
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);
202                     arrange(m, d);
203                 } else {
204                     if (is_managed && loc.monitor == m) {
205                         return;
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) {
210                             return;
211                         } else {
212                             loc.monitor = pmon;
213                             loc.desktop = pmon->desk;
214                         }
215                     }
216                     bool focused = (n == mon->desk->focus);
217                     transfer_node(m, d, n, loc.monitor, loc.desktop, loc.desktop->focus);
218                     if (focused)
219                         focus_node(loc.monitor, loc.desktop, n);
220                     frozen_pointer->monitor = loc.monitor;
221                     frozen_pointer->desktop = loc.desktop;
222                 }
223             } else {
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)
232                     return;
233                 bool focused = (n == mon->desk->focus);
234                 transfer_node(m, d, n, pmon, pmon->desk, pmon->desk->focus);
235                 if (focused)
236                     focus_node(pmon, pmon->desk, n);
237                 frozen_pointer->monitor = pmon;
238                 frozen_pointer->desktop = pmon->desk;
239             }
240             break;
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;
246                     sr = MAX(0, sr);
247                     sr = MIN(1, sr);
248                     vertical_fence->split_ratio = sr;
249                 }
250                 if (horizontal_fence != NULL) {
251                     double sr = frozen_pointer->horizontal_ratio + (double) delta_y / horizontal_fence->rectangle.height;
252                     sr = MAX(0, sr);
253                     sr = MIN(1, sr);
254                     horizontal_fence->split_ratio = sr;
255                 }
256                 arrange(m, d);
257             } else {
258                 if (pac == ACTION_RESIZE_SIDE) {
259                     switch (frozen_pointer->side) {
260                         case SIDE_TOP:
261                             x = rect.x;
262                             y = rect.y + delta_y;
263                             w = rect.width;
264                             h = rect.height - delta_y;
265                             break;
266                         case SIDE_RIGHT:
267                             x = rect.x;
268                             y = rect.y;
269                             w = rect.width + delta_x;
270                             h = rect.height;
271                             break;
272                         case SIDE_BOTTOM:
273                             x = rect.x;
274                             y = rect.y;
275                             w = rect.width;
276                             h = rect.height + delta_y;
277                             break;
278                         case SIDE_LEFT:
279                             x = rect.x + delta_x;
280                             y = rect.y;
281                             w = rect.width - delta_x;
282                             h = rect.height;
283                             break;
284                     }
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;
292                             break;
293                         case CORNER_TOP_RIGHT:
294                             x = rect.x;
295                             y = rect.y + delta_y;
296                             w = rect.width + delta_x;
297                             h = rect.height - delta_y;
298                             break;
299                         case CORNER_BOTTOM_LEFT:
300                             x = rect.x + delta_x;
301                             y = rect.y;
302                             w = rect.width - delta_x;
303                             h = rect.height + delta_y;
304                             break;
305                         case CORNER_BOTTOM_RIGHT:
306                             x = rect.x;
307                             y = rect.y;
308                             w = rect.width + delta_x;
309                             h = rect.height + delta_y;
310                             break;
311                     }
312                 }
313
314                 int oldw = w, oldh = h;
315                 restrain_floating_size(c, &w, &h);
316
317                 if (c->pseudo_tiled) {
318                     c->floating_rectangle.width = w;
319                     c->floating_rectangle.height = h;
320                     arrange(m, d);
321                 } else {
322                     if (oldw == w) {
323                         c->floating_rectangle.x = x;
324                         c->floating_rectangle.width = w;
325                     }
326                     if (oldh == h) {
327                         c->floating_rectangle.y = y;
328                         c->floating_rectangle.height = h;
329                     }
330                     window_move_resize(win, c->floating_rectangle.x,
331                                             c->floating_rectangle.y,
332                                             c->floating_rectangle.width,
333                                             c->floating_rectangle.height);
334                 }
335             }
336             break;
337         case ACTION_FOCUS:
338         case ACTION_NONE:
339             break;
340     }
341 }
342
343 void ungrab_pointer(void)
344 {
345     frozen_pointer->action = ACTION_NONE;
346 }