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 grab_buttons(void)
54 grab_button(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 grab_button(buttons[i], pointer_modifier);
62 void grab_button(uint8_t button, uint16_t modifier)
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);
71 if (num_lock != XCB_NO_SYMBOL && caps_lock != XCB_NO_SYMBOL) {
72 GRAB(button, modifier | num_lock | caps_lock);
74 if (caps_lock != XCB_NO_SYMBOL && scroll_lock != XCB_NO_SYMBOL) {
75 GRAB(button, modifier | caps_lock | scroll_lock);
77 if (num_lock != XCB_NO_SYMBOL && scroll_lock != XCB_NO_SYMBOL) {
78 GRAB(button, modifier | num_lock | scroll_lock);
80 if (num_lock != XCB_NO_SYMBOL) {
81 GRAB(button, modifier | num_lock);
83 if (caps_lock != XCB_NO_SYMBOL) {
84 GRAB(button, modifier | caps_lock);
86 if (scroll_lock != XCB_NO_SYMBOL) {
87 GRAB(button, modifier | scroll_lock);
92 void ungrab_buttons(void)
94 xcb_ungrab_button(dpy, XCB_BUTTON_INDEX_ANY, root, XCB_MOD_MASK_ANY);
97 int16_t modfield_from_keysym(xcb_keysym_t keysym)
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);
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) {
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) {
118 for (xcb_keycode_t *k = keycodes; *k != XCB_NO_SYMBOL; k++) {
120 modfield |= (1 << i);
127 xcb_key_symbols_free(symbols);
133 resize_handle_t get_handle(node_t *n, xcb_point_t pos, pointer_action_t pac)
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;
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;
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);
163 rh = HANDLE_BOTTOM_RIGHT;
165 rh = HANDLE_TOP_RIGHT;
169 rh = HANDLE_BOTTOM_LEFT;
171 rh = HANDLE_TOP_LEFT;
178 void grab_pointer(pointer_action_t pac)
180 xcb_window_t win = XCB_NONE;
183 query_pointer(&win, &pos);
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);
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);
206 if (loc.node->client->state == STATE_FULLSCREEN) {
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);
212 if (reply == NULL || reply->status != XCB_GRAB_STATUS_SUCCESS) {
216 track_pointer(loc, pac, pos);
219 void track_pointer(coordinates_t loc, pointer_action_t pac, xcb_point_t pos)
221 node_t *n = loc.node;
222 resize_handle_t rh = get_handle(loc.node, pos, pac);
224 uint16_t last_motion_x = pos.x, last_motion_y = pos.y;
225 xcb_timestamp_t last_motion_time = 0;
227 xcb_generic_event_t *evt = NULL;
234 while ((evt = xcb_wait_for_event(dpy)) == NULL) {
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;
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);
250 resize_client(&loc, rh, dx, dy);
252 last_motion_x = e->root_x;
253 last_motion_y = e->root_y;
255 } else if (resp_type == XCB_BUTTON_RELEASE) {
260 } while (grabbing && grabbed_node != NULL);
262 xcb_ungrab_pointer(dpy, XCB_CURRENT_TIME);
264 if (grabbed_node == NULL) {
269 xcb_rectangle_t r = get_rectangle(NULL, n);
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);
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)) {
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);