]> git.lizzy.rs Git - bspwm.git/blob - src/pointer.c
5275cd39befe0c239da8213059dd14eb683c5977
[bspwm.git] / src / pointer.c
1 /* Copyright (c) 2012, Bastien Dejean
2  * All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
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.
12  *
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.
23  */
24
25 #include <xcb/xcb_keysyms.h>
26 #include <stdlib.h>
27 #include <stdbool.h>
28 #include "bspwm.h"
29 #include "query.h"
30 #include "settings.h"
31 #include "stack.h"
32 #include "tree.h"
33 #include "monitor.h"
34 #include "subscribe.h"
35 #include "events.h"
36 #include "window.h"
37 #include "pointer.h"
38
39 void pointer_init(void)
40 {
41         num_lock = modfield_from_keysym(XK_Num_Lock);
42         caps_lock = modfield_from_keysym(XK_Caps_Lock);
43         scroll_lock = modfield_from_keysym(XK_Scroll_Lock);
44         if (caps_lock == XCB_NO_SYMBOL) {
45                 caps_lock = XCB_MOD_MASK_LOCK;
46         }
47         grabbing = false;
48         grabbed_node = NULL;
49 }
50
51 void window_grab_buttons(xcb_window_t win)
52 {
53         if (click_to_focus) {
54                 window_grab_button(win, XCB_BUTTON_INDEX_1, XCB_NONE);
55         }
56         uint8_t buttons[] = {XCB_BUTTON_INDEX_1, XCB_BUTTON_INDEX_2, XCB_BUTTON_INDEX_3};
57         for (unsigned int i = 0; i < LENGTH(buttons); i++) {
58                 if (pointer_actions[i] != ACTION_NONE) {
59                         window_grab_button(win, buttons[i], pointer_modifier);
60                 }
61         }
62 }
63
64 void window_grab_button(xcb_window_t win, uint8_t button, uint16_t modifier)
65 {
66 #define GRAB(b, m) \
67         xcb_grab_button(dpy, false, win, XCB_EVENT_MASK_BUTTON_PRESS, \
68                         XCB_GRAB_MODE_SYNC, XCB_GRAB_MODE_ASYNC, XCB_NONE, XCB_NONE, b, m)
69                 GRAB(button, modifier);
70                 if (num_lock != XCB_NO_SYMBOL && caps_lock != XCB_NO_SYMBOL && scroll_lock != XCB_NO_SYMBOL) {
71                         GRAB(button, modifier | num_lock | caps_lock | scroll_lock);
72                 }
73                 if (num_lock != XCB_NO_SYMBOL && caps_lock != XCB_NO_SYMBOL) {
74                         GRAB(button, modifier | num_lock | caps_lock);
75                 }
76                 if (caps_lock != XCB_NO_SYMBOL && scroll_lock != XCB_NO_SYMBOL) {
77                         GRAB(button, modifier | caps_lock | scroll_lock);
78                 }
79                 if (num_lock != XCB_NO_SYMBOL && scroll_lock != XCB_NO_SYMBOL) {
80                         GRAB(button, modifier | num_lock | scroll_lock);
81                 }
82                 if (num_lock != XCB_NO_SYMBOL) {
83                         GRAB(button, modifier | num_lock);
84                 }
85                 if (caps_lock != XCB_NO_SYMBOL) {
86                         GRAB(button, modifier | caps_lock);
87                 }
88                 if (scroll_lock != XCB_NO_SYMBOL) {
89                         GRAB(button, modifier | scroll_lock);
90                 }
91 #undef GRAB
92 }
93
94 void grab_buttons(void)
95 {
96         for (monitor_t *m = mon_head; m != NULL; m = m->next) {
97                 for (desktop_t *d = m->desk_head; d != NULL; d = d->next) {
98                         for (node_t *n = first_extrema(d->root); n != NULL; n = next_leaf(n, d->root)) {
99                                 window_grab_buttons(n->id);
100                                 if (n->presel != NULL) {
101                                         window_grab_buttons(n->presel->feedback);
102                                 }
103                         }
104                 }
105         }
106 }
107
108 void ungrab_buttons(void)
109 {
110         for (monitor_t *m = mon_head; m != NULL; m = m->next) {
111                 for (desktop_t *d = m->desk_head; d != NULL; d = d->next) {
112                         for (node_t *n = first_extrema(d->root); n != NULL; n = next_leaf(n, d->root)) {
113                                 xcb_ungrab_button(dpy, XCB_BUTTON_INDEX_ANY, n->id, XCB_MOD_MASK_ANY);
114                         }
115                 }
116         }
117 }
118
119 int16_t modfield_from_keysym(xcb_keysym_t keysym)
120 {
121         uint16_t modfield = 0;
122         xcb_keycode_t *keycodes = NULL, *mod_keycodes = NULL;
123         xcb_get_modifier_mapping_reply_t *reply = NULL;
124         xcb_key_symbols_t *symbols = xcb_key_symbols_alloc(dpy);
125
126         if ((keycodes = xcb_key_symbols_get_keycode(symbols, keysym)) == NULL ||
127             (reply = xcb_get_modifier_mapping_reply(dpy, xcb_get_modifier_mapping(dpy), NULL)) == NULL ||
128             reply->keycodes_per_modifier < 1 ||
129             (mod_keycodes = xcb_get_modifier_mapping_keycodes(reply)) == NULL) {
130                 goto end;
131         }
132
133         unsigned int num_mod = xcb_get_modifier_mapping_keycodes_length(reply) / reply->keycodes_per_modifier;
134         for (unsigned int i = 0; i < num_mod; i++) {
135                 for (unsigned int j = 0; j < reply->keycodes_per_modifier; j++) {
136                         xcb_keycode_t mk = mod_keycodes[i * reply->keycodes_per_modifier + j];
137                         if (mk == XCB_NO_SYMBOL) {
138                                 continue;
139                         }
140                         for (xcb_keycode_t *k = keycodes; *k != XCB_NO_SYMBOL; k++) {
141                                 if (*k == mk) {
142                                         modfield |= (1 << i);
143                                 }
144                         }
145                 }
146         }
147
148 end:
149         xcb_key_symbols_free(symbols);
150         free(keycodes);
151         free(reply);
152         return modfield;
153 }
154
155 resize_handle_t get_handle(node_t *n, xcb_point_t pos, pointer_action_t pac)
156 {
157         resize_handle_t rh = HANDLE_BOTTOM_RIGHT;
158         xcb_rectangle_t rect = get_rectangle(NULL, NULL, n);
159         if (pac == ACTION_RESIZE_SIDE) {
160                 float W = rect.width;
161                 float H = rect.height;
162                 float ratio = W / H;
163                 float x = pos.x - rect.x;
164                 float y = pos.y - rect.y;
165                 float diag_a = ratio * y;
166                 float diag_b = W - diag_a;
167                 if (x < diag_a) {
168                         if (x < diag_b) {
169                                 rh = HANDLE_LEFT;
170                         } else {
171                                 rh = HANDLE_BOTTOM;
172                         }
173                 } else {
174                         if (x < diag_b) {
175                                 rh = HANDLE_TOP;
176                         } else {
177                                 rh = HANDLE_RIGHT;
178                         }
179                 }
180         } else if (pac == ACTION_RESIZE_CORNER) {
181                 int16_t mid_x = rect.x + (rect.width / 2);
182                 int16_t mid_y = rect.y + (rect.height / 2);
183                 if (pos.x > mid_x) {
184                         if (pos.y > mid_y) {
185                                 rh = HANDLE_BOTTOM_RIGHT;
186                         } else {
187                                 rh = HANDLE_TOP_RIGHT;
188                         }
189                 } else {
190                         if (pos.y > mid_y) {
191                                 rh = HANDLE_BOTTOM_LEFT;
192                         } else {
193                                 rh = HANDLE_TOP_LEFT;
194                         }
195                 }
196         }
197         return rh;
198 }
199
200 bool grab_pointer(pointer_action_t pac)
201 {
202         xcb_window_t win = XCB_NONE;
203         xcb_point_t pos;
204
205         query_pointer(&win, &pos);
206
207         coordinates_t loc;
208
209         if (!locate_window(win, &loc)) {
210                 if (pac == ACTION_FOCUS) {
211                         monitor_t *m = monitor_from_point(pos);
212                         if (m != NULL && m != mon && (win == XCB_NONE || win == m->root)) {
213                                 focus_node(m, m->desk, m->desk->focus);
214                                 return true;
215                         }
216                 }
217                 return false;
218         }
219
220         if (pac == ACTION_FOCUS) {
221                 if (loc.node != mon->desk->focus) {
222                         focus_node(loc.monitor, loc.desktop, loc.node);
223                         return true;
224                 } else if (focus_follows_pointer) {
225                         stack(loc.desktop, loc.node, true);
226                 }
227                 return false;
228         }
229
230         if (loc.node->client->state == STATE_FULLSCREEN) {
231                 return true;
232         }
233
234         xcb_grab_pointer_reply_t *reply = xcb_grab_pointer_reply(dpy, xcb_grab_pointer(dpy, 0, root, XCB_EVENT_MASK_BUTTON_RELEASE|XCB_EVENT_MASK_BUTTON_MOTION, XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_ASYNC, XCB_NONE, XCB_NONE, XCB_CURRENT_TIME), NULL);
235
236         if (reply == NULL || reply->status != XCB_GRAB_STATUS_SUCCESS) {
237                 free(reply);
238                 return true;
239         }
240         free(reply);
241
242         if (pac == ACTION_MOVE) {
243                 put_status(SBSC_MASK_POINTER_ACTION, "pointer_action 0x%08X 0x%08X 0x%08X move begin\n", loc.monitor->id, loc.desktop->id, loc.node->id);
244         } else if (pac == ACTION_RESIZE_CORNER) {
245                 put_status(SBSC_MASK_POINTER_ACTION, "pointer_action 0x%08X 0x%08X 0x%08X resize_corner begin\n", loc.monitor->id, loc.desktop->id, loc.node->id);
246         } else if (pac == ACTION_RESIZE_SIDE) {
247                 put_status(SBSC_MASK_POINTER_ACTION, "pointer_action 0x%08X 0x%08X 0x%08X resize_side begin\n", loc.monitor->id, loc.desktop->id, loc.node->id);
248         }
249
250         track_pointer(loc, pac, pos);
251
252         return true;
253 }
254
255 void track_pointer(coordinates_t loc, pointer_action_t pac, xcb_point_t pos)
256 {
257         node_t *n = loc.node;
258         resize_handle_t rh = get_handle(loc.node, pos, pac);
259
260         uint16_t last_motion_x = pos.x, last_motion_y = pos.y;
261         xcb_timestamp_t last_motion_time = 0;
262
263         xcb_generic_event_t *evt = NULL;
264
265         grabbing = true;
266         grabbed_node = n;
267
268         do {
269                 free(evt);
270                 while ((evt = xcb_wait_for_event(dpy)) == NULL) {
271                         xcb_flush(dpy);
272                 }
273                 uint8_t resp_type = XCB_EVENT_RESPONSE_TYPE(evt);
274                 if (resp_type == XCB_MOTION_NOTIFY) {
275                         xcb_motion_notify_event_t *e = (xcb_motion_notify_event_t*) evt;
276                         uint32_t dtime = e->time - last_motion_time;
277                         if (dtime < pointer_motion_interval) {
278                                 continue;
279                         }
280                         last_motion_time = e->time;
281                         int16_t dx = e->root_x - last_motion_x;
282                         int16_t dy = e->root_y - last_motion_y;
283                         if (pac == ACTION_MOVE) {
284                                 move_client(&loc, dx, dy);
285                         } else {
286                                 resize_client(&loc, rh, dx, dy);
287                         }
288                         last_motion_x = e->root_x;
289                         last_motion_y = e->root_y;
290                         xcb_flush(dpy);
291                 } else if (resp_type == XCB_BUTTON_RELEASE) {
292                         grabbing = false;
293                 } else {
294                         handle_event(evt);
295                 }
296         } while (grabbing && grabbed_node != NULL);
297         free(evt);
298
299         xcb_ungrab_pointer(dpy, XCB_CURRENT_TIME);
300
301         if (grabbed_node == NULL) {
302                 grabbing = false;
303                 return;
304         }
305
306         if (pac == ACTION_MOVE) {
307                 put_status(SBSC_MASK_POINTER_ACTION, "pointer_action 0x%08X 0x%08X 0x%08X move end\n", loc.monitor->id, loc.desktop->id, n->id);
308         } else if (pac == ACTION_RESIZE_CORNER) {
309                 put_status(SBSC_MASK_POINTER_ACTION, "pointer_action 0x%08X 0x%08X 0x%08X resize_corner end\n", loc.monitor->id, loc.desktop->id, n->id);
310         } else if (pac == ACTION_RESIZE_SIDE) {
311                 put_status(SBSC_MASK_POINTER_ACTION, "pointer_action 0x%08X 0x%08X 0x%08X resize_side end\n", loc.monitor->id, loc.desktop->id, n->id);
312         }
313
314         xcb_rectangle_t r = get_rectangle(NULL, NULL, n);
315
316         put_status(SBSC_MASK_NODE_GEOMETRY, "node_geometry 0x%08X 0x%08X 0x%08X %ux%u+%i+%i\n", loc.monitor->id, loc.desktop->id, loc.node->id, r.width, r.height, r.x, r.y);
317
318         if ((pac == ACTION_MOVE && IS_TILED(n->client)) ||
319             ((pac == ACTION_RESIZE_CORNER || pac == ACTION_RESIZE_SIDE) &&
320              n->client->state == STATE_TILED)) {
321                 for (node_t *f = first_extrema(loc.desktop->root); f != NULL; f = next_leaf(f, loc.desktop->root)) {
322                         if (f == n || f->client == NULL || !IS_TILED(f->client)) {
323                                 continue;
324                         }
325                         xcb_rectangle_t r = f->client->tiled_rectangle;
326                         put_status(SBSC_MASK_NODE_GEOMETRY, "node_geometry 0x%08X 0x%08X 0x%08X %ux%u+%i+%i\n", loc.monitor->id, loc.desktop->id, f->id, r.width, r.height, r.x, r.y);
327                 }
328         }
329 }