1 /* Copyright (c) 2012, Bastien Dejean
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
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.
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.
25 #include <xcb/xcb_keysyms.h>
34 #include "subscribe.h"
39 void pointer_init(void)
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;
51 void window_grab_buttons(xcb_window_t win)
54 window_grab_button(win, XCB_BUTTON_INDEX_1, XCB_NONE);
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);
64 void window_grab_button(xcb_window_t win, uint8_t button, uint16_t modifier)
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);
73 if (num_lock != XCB_NO_SYMBOL && caps_lock != XCB_NO_SYMBOL) {
74 GRAB(button, modifier | num_lock | caps_lock);
76 if (caps_lock != XCB_NO_SYMBOL && scroll_lock != XCB_NO_SYMBOL) {
77 GRAB(button, modifier | caps_lock | scroll_lock);
79 if (num_lock != XCB_NO_SYMBOL && scroll_lock != XCB_NO_SYMBOL) {
80 GRAB(button, modifier | num_lock | scroll_lock);
82 if (num_lock != XCB_NO_SYMBOL) {
83 GRAB(button, modifier | num_lock);
85 if (caps_lock != XCB_NO_SYMBOL) {
86 GRAB(button, modifier | caps_lock);
88 if (scroll_lock != XCB_NO_SYMBOL) {
89 GRAB(button, modifier | scroll_lock);
94 void grab_buttons(void)
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);
108 void ungrab_buttons(void)
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);
119 int16_t modfield_from_keysym(xcb_keysym_t keysym)
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);
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) {
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) {
140 for (xcb_keycode_t *k = keycodes; *k != XCB_NO_SYMBOL; k++) {
142 modfield |= (1 << i);
149 xcb_key_symbols_free(symbols);
155 resize_handle_t get_handle(node_t *n, xcb_point_t pos, pointer_action_t pac)
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;
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;
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);
185 rh = HANDLE_BOTTOM_RIGHT;
187 rh = HANDLE_TOP_RIGHT;
191 rh = HANDLE_BOTTOM_LEFT;
193 rh = HANDLE_TOP_LEFT;
200 bool grab_pointer(pointer_action_t pac)
202 xcb_window_t win = XCB_NONE;
205 query_pointer(&win, &pos);
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);
220 if (pac == ACTION_FOCUS) {
221 if (loc.node != mon->desk->focus) {
222 focus_node(loc.monitor, loc.desktop, loc.node);
224 } else if (focus_follows_pointer) {
225 stack(loc.desktop, loc.node, true);
230 if (loc.node->client->state == STATE_FULLSCREEN) {
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);
236 if (reply == NULL || reply->status != XCB_GRAB_STATUS_SUCCESS) {
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);
250 track_pointer(loc, pac, pos);
255 void track_pointer(coordinates_t loc, pointer_action_t pac, xcb_point_t pos)
257 node_t *n = loc.node;
258 resize_handle_t rh = get_handle(loc.node, pos, pac);
260 uint16_t last_motion_x = pos.x, last_motion_y = pos.y;
261 xcb_timestamp_t last_motion_time = 0;
263 xcb_generic_event_t *evt = NULL;
270 while ((evt = xcb_wait_for_event(dpy)) == NULL) {
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) {
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);
286 resize_client(&loc, rh, dx, dy);
288 last_motion_x = e->root_x;
289 last_motion_y = e->root_y;
291 } else if (resp_type == XCB_BUTTON_RELEASE) {
296 } while (grabbing && grabbed_node != NULL);
299 xcb_ungrab_pointer(dpy, XCB_CURRENT_TIME);
301 if (grabbed_node == NULL) {
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);
314 xcb_rectangle_t r = get_rectangle(NULL, NULL, n);
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);
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)) {
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);