]> git.lizzy.rs Git - bspwm.git/blob - pointer.c
Accept an optional reference in all the selectors
[bspwm.git] / 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 grab_buttons(void)
52 {
53         if (click_to_focus) {
54                 grab_button(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                 grab_button(buttons[i], pointer_modifier);
59         }
60 }
61
62 void grab_button(uint8_t button, uint16_t modifier)
63 {
64 #define GRAB(b, m) \
65         xcb_grab_button(dpy, false, root, XCB_EVENT_MASK_BUTTON_PRESS, \
66                         XCB_GRAB_MODE_SYNC, XCB_GRAB_MODE_ASYNC, XCB_NONE, XCB_NONE, b, m)
67                 GRAB(button, modifier);
68                 if (num_lock != XCB_NO_SYMBOL && caps_lock != XCB_NO_SYMBOL && scroll_lock != XCB_NO_SYMBOL) {
69                         GRAB(button, modifier | num_lock | caps_lock | scroll_lock);
70                 }
71                 if (num_lock != XCB_NO_SYMBOL && caps_lock != XCB_NO_SYMBOL) {
72                         GRAB(button, modifier | num_lock | caps_lock);
73                 }
74                 if (caps_lock != XCB_NO_SYMBOL && scroll_lock != XCB_NO_SYMBOL) {
75                         GRAB(button, modifier | caps_lock | scroll_lock);
76                 }
77                 if (num_lock != XCB_NO_SYMBOL && scroll_lock != XCB_NO_SYMBOL) {
78                         GRAB(button, modifier | num_lock | scroll_lock);
79                 }
80                 if (num_lock != XCB_NO_SYMBOL) {
81                         GRAB(button, modifier | num_lock);
82                 }
83                 if (caps_lock != XCB_NO_SYMBOL) {
84                         GRAB(button, modifier | caps_lock);
85                 }
86                 if (scroll_lock != XCB_NO_SYMBOL) {
87                         GRAB(button, modifier | scroll_lock);
88                 }
89 #undef GRAB
90 }
91
92 void ungrab_buttons(void)
93 {
94         xcb_ungrab_button(dpy, XCB_BUTTON_INDEX_ANY, root, XCB_MOD_MASK_ANY);
95 }
96
97 int16_t modfield_from_keysym(xcb_keysym_t keysym)
98 {
99         uint16_t modfield = 0;
100         xcb_keycode_t *keycodes = NULL, *mod_keycodes = NULL;
101         xcb_get_modifier_mapping_reply_t *reply = NULL;
102         xcb_key_symbols_t *symbols = xcb_key_symbols_alloc(dpy);
103
104         if ((keycodes = xcb_key_symbols_get_keycode(symbols, keysym)) == NULL ||
105             (reply = xcb_get_modifier_mapping_reply(dpy, xcb_get_modifier_mapping(dpy), NULL)) == NULL ||
106             reply->keycodes_per_modifier < 1 ||
107             (mod_keycodes = xcb_get_modifier_mapping_keycodes(reply)) == NULL) {
108                 goto end;
109         }
110
111         unsigned int num_mod = xcb_get_modifier_mapping_keycodes_length(reply) / reply->keycodes_per_modifier;
112         for (unsigned int i = 0; i < num_mod; i++) {
113                 for (unsigned int j = 0; j < reply->keycodes_per_modifier; j++) {
114                         xcb_keycode_t mk = mod_keycodes[i * reply->keycodes_per_modifier + j];
115                         if (mk == XCB_NO_SYMBOL) {
116                                 continue;
117                         }
118                         for (xcb_keycode_t *k = keycodes; *k != XCB_NO_SYMBOL; k++) {
119                                 if (*k == mk) {
120                                         modfield |= (1 << i);
121                                 }
122                         }
123                 }
124         }
125
126 end:
127         xcb_key_symbols_free(symbols);
128         free(keycodes);
129         free(reply);
130         return modfield;
131 }
132
133 resize_handle_t get_handle(node_t *n, xcb_point_t pos, pointer_action_t pac)
134 {
135         resize_handle_t rh = HANDLE_BOTTOM_RIGHT;
136         xcb_rectangle_t rect = get_rectangle(NULL, n);
137         if (pac == ACTION_RESIZE_SIDE) {
138                 float W = rect.width;
139                 float H = rect.height;
140                 float ratio = W / H;
141                 float x = pos.x - rect.x;
142                 float y = pos.y - rect.y;
143                 float diag_a = ratio * y;
144                 float diag_b = W - diag_a;
145                 if (x < diag_a) {
146                         if (x < diag_b) {
147                                 rh = HANDLE_LEFT;
148                         } else {
149                                 rh = HANDLE_BOTTOM;
150                         }
151                 } else {
152                         if (x < diag_b) {
153                                 rh = HANDLE_TOP;
154                         } else {
155                                 rh = HANDLE_RIGHT;
156                         }
157                 }
158         } else if (pac == ACTION_RESIZE_CORNER) {
159                 int16_t mid_x = rect.x + (rect.width / 2);
160                 int16_t mid_y = rect.y + (rect.height / 2);
161                 if (pos.x > mid_x) {
162                         if (pos.y > mid_y) {
163                                 rh = HANDLE_BOTTOM_RIGHT;
164                         } else {
165                                 rh = HANDLE_TOP_RIGHT;
166                         }
167                 } else {
168                         if (pos.y > mid_y) {
169                                 rh = HANDLE_BOTTOM_LEFT;
170                         } else {
171                                 rh = HANDLE_TOP_LEFT;
172                         }
173                 }
174         }
175         return rh;
176 }
177
178 void grab_pointer(pointer_action_t pac)
179 {
180         xcb_window_t win = XCB_NONE;
181         xcb_point_t pos;
182
183         query_pointer(&win, &pos);
184
185         coordinates_t loc;
186
187         if (!locate_window(win, &loc)) {
188                 if (pac == ACTION_FOCUS) {
189                         monitor_t *m = monitor_from_point(pos);
190                         if (m != NULL && m != mon && (win == XCB_NONE || win == m->root)) {
191                                 focus_node(m, m->desk, m->desk->focus);
192                         }
193                 }
194                 return;
195         }
196
197         if (pac == ACTION_FOCUS) {
198                 if (loc.node != mon->desk->focus) {
199                         focus_node(loc.monitor, loc.desktop, loc.node);
200                 } else if (focus_follows_pointer) {
201                         stack(loc.desktop, loc.node, true);
202                 }
203                 return;
204         }
205
206         if (loc.node->client->state == STATE_FULLSCREEN) {
207                 return;
208         }
209
210         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);
211
212         if (reply == NULL || reply->status != XCB_GRAB_STATUS_SUCCESS) {
213                 return;
214         }
215
216         track_pointer(loc, pac, pos);
217 }
218
219 void track_pointer(coordinates_t loc, pointer_action_t pac, xcb_point_t pos)
220 {
221         node_t *n = loc.node;
222         resize_handle_t rh = get_handle(loc.node, pos, pac);
223
224         uint16_t last_motion_x = pos.x, last_motion_y = pos.y;
225         xcb_timestamp_t last_motion_time = 0;
226
227         xcb_generic_event_t *evt = NULL;
228
229         grabbing = true;
230         grabbed_node = n;
231
232         do {
233                 free(evt);
234                 while ((evt = xcb_wait_for_event(dpy)) == NULL) {
235                         xcb_flush(dpy);
236                 }
237                 uint8_t resp_type = XCB_EVENT_RESPONSE_TYPE(evt);
238                 if (resp_type == XCB_MOTION_NOTIFY) {
239                         xcb_motion_notify_event_t *e = (xcb_motion_notify_event_t*) evt;
240                         int64_t dtime = e->time - last_motion_time;
241                         if (dtime < 20) {
242                                 continue;
243                         }
244                         last_motion_time = e->time;
245                         int16_t dx = e->root_x - last_motion_x;
246                         int16_t dy = e->root_y - last_motion_y;
247                         if (pac == ACTION_MOVE) {
248                                 move_client(&loc, dx, dy);
249                         } else {
250                                 resize_client(&loc, rh, dx, dy);
251                         }
252                         last_motion_x = e->root_x;
253                         last_motion_y = e->root_y;
254                         xcb_flush(dpy);
255                 } else if (resp_type == XCB_BUTTON_RELEASE) {
256                         grabbing = false;
257                 } else {
258                         handle_event(evt);
259                 }
260         } while (grabbing && grabbed_node != NULL);
261
262         xcb_ungrab_pointer(dpy, XCB_CURRENT_TIME);
263
264         if (grabbed_node == NULL) {
265                 grabbing = false;
266                 return;
267         }
268
269         xcb_rectangle_t r = get_rectangle(NULL, n);
270
271         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);
272
273         if ((pac == ACTION_MOVE && IS_TILED(n->client)) ||
274             ((pac == ACTION_RESIZE_CORNER || pac == ACTION_RESIZE_SIDE) &&
275              n->client->state == STATE_TILED)) {
276                 for (node_t *f = first_extrema(loc.desktop->root); f != NULL; f = next_leaf(f, loc.desktop->root)) {
277                         if (f == n || f->client == NULL || !IS_TILED(f->client)) {
278                                 continue;
279                         }
280                         xcb_rectangle_t r = f->client->tiled_rectangle;
281                         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);
282                 }
283         }
284 }