2 * Copyright (c) 2015 Andy Spencer <andy753421@gmail.com>
4 * Permission to use, copy, modify, and/or distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
22 #include <xcb/xcb_event.h>
23 #include <xcb/xcb_keysyms.h>
24 #include <xcb/xinerama.h>
33 static int border = 2;
34 static int stack = 25;
35 static int no_capture = 0;
37 /* Internal structures */
39 xcb_window_t xcb; // xcb window id
40 xcb_event_mask_t events; // currently watch events
41 int override; // normal vs override redirect
42 int mapped; // window is currently mapped
46 static xcb_connection_t *conn;
47 static xcb_key_symbols_t *keysyms;
48 static xcb_window_t root;
49 static xcb_event_mask_t events;
50 static list_t *screens;
54 /************************
55 * Conversion functions *
56 ************************/
69 { EV_PAGEUP, 0xFF55 },
70 { EV_PAGEDOWN, 0xFF56 },
85 static xcb_keycode_t *event_to_keycodes(event_t ev)
87 xcb_keycode_t *codes = NULL;
90 xcb_keysym_t keysym = map_get(keysym_map, ev, ev, sym, ev);
93 if (!(codes = xcb_key_symbols_get_keycode(keysyms, keysym)))
94 warn("no keycode found for %d->%d", ev, keysym);
99 static event_t keycode_to_event(xcb_keycode_t code)
102 xcb_keysym_t keysym = xcb_key_symbols_get_keysym(keysyms, code, 0);
105 return map_get(keysym_map, sym,keysym, ev,keysym);
109 static event_t button_to_event(xcb_button_t btn)
111 return EV_MOUSE0 + btn;
114 static xcb_button_t event_to_button(event_t ev)
116 return ev - EV_MOUSE0;
120 static xcb_mod_mask_t mod_to_mask(mod_t mod)
122 xcb_mod_mask_t mask = 0;
123 if (mod.alt) mask |= XCB_MOD_MASK_1;
124 if (mod.ctrl) mask |= XCB_MOD_MASK_CONTROL;
125 if (mod.shift) mask |= XCB_MOD_MASK_SHIFT;
126 if (mod.win) mask |= XCB_MOD_MASK_4;
130 static mod_t mask_to_mod(xcb_mod_mask_t mask, int up)
132 mod_t mod = { .up = up };
133 if (mask & XCB_MOD_MASK_1) mod.alt = 1;
134 if (mask & XCB_MOD_MASK_CONTROL) mod.ctrl = 1;
135 if (mask & XCB_MOD_MASK_SHIFT) mod.shift = 1;
136 if (mask & XCB_MOD_MASK_4) mod.win = 1;
141 static ptr_t list_to_ptr(int16_t *list)
144 ptr.rx = list[0]; // root_x
145 ptr.ry = list[1]; // root_y
146 ptr.x = list[2]; // event_x
147 ptr.y = list[3]; // event_y
151 /********************
153 ********************/
155 static int win_cmp(const void *_a, const void *_b)
159 if (a->sys->xcb < b->sys->xcb) return -1;
160 if (a->sys->xcb > b->sys->xcb) return 1;
164 static win_t *win_get(xcb_window_t xcb)
166 win_sys_t sys = { .xcb = xcb };
167 win_t key = { .sys = &sys };
169 win_t **win = tfind(&key, &cache, win_cmp);
172 warn("no window for %u", xcb);
183 static int do_query_tree(xcb_window_t win, xcb_window_t **kids)
185 xcb_query_tree_cookie_t cookie =
186 xcb_query_tree(conn, win);
187 if (!cookie.sequence)
188 return warn("do_query_tree: %d - bad cookie", win);
190 xcb_query_tree_reply_t *reply =
191 xcb_query_tree_reply(conn, cookie, NULL);
193 return warn("do_query_tree: %d - no reply", win);
195 int nkids = xcb_query_tree_children_length(reply);
196 *kids = xcb_query_tree_children(reply);
197 printf("do_query_tree: %d - n=%d\n", win, nkids);
201 static int do_get_geometry(xcb_window_t win,
202 int *x, int *y, int *w, int *h)
204 xcb_get_geometry_cookie_t cookie =
205 xcb_get_geometry(conn, win);
206 if (!cookie.sequence)
207 return warn("do_get_geometry: %d - bad cookie", win);
209 xcb_get_geometry_reply_t *reply =
210 xcb_get_geometry_reply(conn, cookie, NULL);
212 return warn("do_get_geometry: %d - no reply", win);
214 printf("do_get_geometry: %d - %dx%d @ %d,%d\n",
215 win, reply->width, reply->height, reply->x, reply->y);
223 static int do_get_window_attributes(xcb_window_t win,
224 int *override, int *mapped)
226 xcb_get_window_attributes_cookie_t cookie =
227 xcb_get_window_attributes(conn, win);
228 if (!cookie.sequence)
229 return warn("do_get_window_attributes: %d - bad cookie", win);
231 xcb_get_window_attributes_reply_t *reply =
232 xcb_get_window_attributes_reply(conn, cookie, NULL);
234 return warn("do_get_window_attributes: %d - no reply ", win);
236 printf("do_get_window_attributes: %d - %d\n",
237 win, reply->override_redirect);
238 *override = reply->override_redirect;
239 *mapped = reply->map_state != XCB_MAP_STATE_UNMAPPED;
243 static int do_xinerama_check(void)
245 const xcb_query_extension_reply_t *data =
246 xcb_get_extension_data(conn, &xcb_xinerama_id);
247 if (!data || !data->present)
248 return warn("do_xinerama_check: no ext");
250 xcb_xinerama_is_active_cookie_t cookie =
251 xcb_xinerama_is_active(conn);
252 if (!cookie.sequence)
253 return warn("do_xinerama_check: no cookie");
255 xcb_xinerama_is_active_reply_t *reply =
256 xcb_xinerama_is_active_reply(conn, cookie, NULL);
258 warn("do_xinerama_check: no reply");
260 printf("do_xinerama_check: %d\n", reply->state);
261 return reply && reply->state;
264 static int do_query_screens(xcb_xinerama_screen_info_t **info)
266 xcb_xinerama_query_screens_cookie_t cookie =
267 xcb_xinerama_query_screens(conn);
268 if (!cookie.sequence)
269 return warn("do_query_screens: bad cookie");
271 xcb_xinerama_query_screens_reply_t *reply =
272 xcb_xinerama_query_screens_reply(conn, cookie, NULL);
274 return warn("do_query_screens: no reply");
276 int ninfo = xcb_xinerama_query_screens_screen_info_length(reply);
277 *info = xcb_xinerama_query_screens_screen_info(reply);
278 printf("do_query_screens: %d screens\n", ninfo);
282 /**********************
283 * X11 Event Handlers *
284 **********************/
286 /* Specific events */
287 static void on_key_event(xcb_key_press_event_t *event, int up)
289 xcb_window_t xcb = event->event == root ?
290 event->child : event->event;
291 win_t *win = win_get(xcb);
292 event_t ev = keycode_to_event(event->detail);
293 mod_t mod = mask_to_mod(event->state, up);
294 ptr_t ptr = list_to_ptr(&event->root_x);
295 printf("on_key_event: xcb=%u -> win=%p\n", xcb, win);
296 wm_handle_event(win, ev, mod, ptr);
299 static void on_button_event(xcb_button_press_event_t *event, int up)
301 xcb_window_t xcb = event->event == root ?
302 event->child : event->event;
303 win_t *win = win_get(xcb);
304 event_t ev = button_to_event(event->detail);
305 mod_t mod = mask_to_mod(event->state, up);
306 ptr_t ptr = list_to_ptr(&event->root_x);
307 printf("on_button_event: xcb=%u -> win=%p\n", xcb, win);
309 if (!wm_handle_event(win, ev, mod, ptr))
310 xcb_allow_events(conn, XCB_ALLOW_REPLAY_POINTER, event->time);
312 xcb_grab_pointer(conn, 1, xcb,
313 XCB_EVENT_MASK_POINTER_MOTION |
314 XCB_EVENT_MASK_BUTTON_RELEASE,
317 0, 0, XCB_CURRENT_TIME);
319 xcb_ungrab_pointer(conn, XCB_CURRENT_TIME);
323 static void on_motion_notify(xcb_motion_notify_event_t *event)
325 xcb_window_t xcb = event->event == root ?
326 event->child : event->event;
327 win_t *win = win_get(xcb);
328 ptr_t ptr = list_to_ptr(&event->root_x);
329 printf("on_motion_notify: xcb=%u -> win=%p - %d,%d / %d.%d\n", xcb, win,
330 ptr.x, ptr.y, ptr.rx, ptr.ry);
331 wm_handle_ptr(win, ptr);
334 static void on_create_notify(xcb_create_notify_event_t *event)
336 win_t *win = new0(win_t);
337 win_sys_t *sys = new0(win_sys_t);
339 printf("on_create_notify: xcb=%u -> win=%p\n",
344 win->w = event->width;
345 win->h = event->height;
348 sys->xcb = event->window;
349 sys->override = event->override_redirect;
351 tsearch(win, &cache, win_cmp);
354 static void on_destroy_notify(win_t *win, xcb_destroy_notify_event_t *event)
356 printf("on_destroy_notify: xcb=%u -> win=%p\n",
359 tdelete(win, &cache, win_cmp);
365 static void on_map_request(win_t *win, xcb_map_request_event_t *event)
367 printf("on_map_request: xcb=%u -> win=%p\n",
370 if (!win->sys->override && !win->sys->mapped)
372 win->sys->mapped = 1;
374 xcb_map_window(conn, win->sys->xcb);
375 sys_move(win, win->x, win->y, win->w, win->h);
378 static void on_configure_request(win_t *win, xcb_configure_request_event_t *event)
380 printf("on_configure_request: xcb=%u -> win=%p -- %dx%d @ %d,%d\n",
382 event->width, event->height,
387 win->w = event->width;
388 win->h = event->height;
390 xcb_configure_notify_event_t resp = {
391 .response_type = XCB_CONFIGURE_NOTIFY,
392 .event = win->sys->xcb,
393 .window = win->sys->xcb,
398 .border_width = border,
401 xcb_send_event(conn, 0, win->sys->xcb,
402 XCB_EVENT_MASK_STRUCTURE_NOTIFY,
403 (const char *)&resp);
407 static void on_event(xcb_generic_event_t *event)
411 int type = XCB_EVENT_RESPONSE_TYPE(event);
412 int sent = XCB_EVENT_SENT(event);
413 const char *name = NULL;
418 on_key_event((xcb_key_press_event_t *)event, 0);
420 case XCB_KEY_RELEASE:
421 on_key_event((xcb_key_release_event_t *)event, 1);
423 case XCB_BUTTON_PRESS:
424 on_button_event((xcb_button_press_event_t *)event, 0);
426 case XCB_BUTTON_RELEASE:
427 on_button_event((xcb_button_release_event_t *)event, 1);
429 case XCB_MOTION_NOTIFY:
430 on_motion_notify((xcb_motion_notify_event_t *)event);
433 /* Window management */
434 case XCB_CREATE_NOTIFY:
435 on_create_notify((xcb_create_notify_event_t *)event);
437 case XCB_DESTROY_NOTIFY:
438 if ((win = win_get(((xcb_destroy_notify_event_t *)event)->window)))
439 on_destroy_notify(win, (xcb_destroy_notify_event_t *)event);
441 case XCB_MAP_REQUEST:
442 if ((win = win_get(((xcb_map_request_event_t *)event)->window)))
443 on_map_request(win, (xcb_map_request_event_t *)event);
445 case XCB_CONFIGURE_REQUEST:
446 if ((win = win_get(((xcb_configure_request_event_t *)event)->window)))
447 on_configure_request(win, (xcb_configure_request_event_t *)event);
452 name = xcb_event_get_label(type);
453 printf("on_event: %d:%02X -> %s\n",
454 !!sent, type, name?:"unknown_event");
459 /********************
461 ********************/
463 void sys_move(win_t *win, int x, int y, int w, int h)
465 printf("sys_move: %p - %dx%d @ %d,%d\n",
473 uint16_t mask = XCB_CONFIG_WINDOW_X
474 | XCB_CONFIG_WINDOW_Y
475 | XCB_CONFIG_WINDOW_WIDTH
476 | XCB_CONFIG_WINDOW_HEIGHT;
477 uint32_t list[] = {x, y, w, h};
479 xcb_configure_window(conn, win->sys->xcb, mask, list);
482 void sys_raise(win_t *win)
484 printf("sys_raise: %p\n", win);
487 void sys_focus(win_t *win)
489 printf("sys_focus: %p\n", win);
492 void sys_show(win_t *win, state_t state)
494 printf("sys_show: %p - %d\n", win, state);
497 void sys_watch(win_t *win, event_t ev, mod_t mod)
499 printf("sys_watch: %p - 0x%X,0x%X\n", win, ev, mod2int(mod));
500 xcb_window_t xcb = win ? win->sys->xcb : root;
501 xcb_event_mask_t *mask = win ? &win->sys->events : &events;
502 xcb_mod_mask_t mods = 0;
503 xcb_button_t btn = 0;
504 xcb_keycode_t *code = 0;
508 *mask |= XCB_EVENT_MASK_ENTER_WINDOW;
509 xcb_change_window_attributes(conn, xcb, XCB_CW_EVENT_MASK, mask);
513 *mask |= XCB_EVENT_MASK_LEAVE_WINDOW;
514 xcb_change_window_attributes(conn, xcb, XCB_CW_EVENT_MASK, mask);
519 *mask |= XCB_EVENT_MASK_FOCUS_CHANGE;
520 xcb_change_window_attributes(conn, xcb, XCB_CW_EVENT_MASK, mask);
523 case EV_MOUSE0...EV_MOUSE7:
524 btn = event_to_button(ev);
525 mods = mod_to_mask(mod);
526 *mask |= mod.up ? XCB_EVENT_MASK_BUTTON_RELEASE
527 : XCB_EVENT_MASK_BUTTON_PRESS;
528 xcb_grab_button(conn, 0, xcb, *mask,
535 code = event_to_keycodes(ev);
536 mods = mod_to_mask(mod);
537 for (int i = 0; code && code[i] != XCB_NO_SYMBOL; i++)
538 xcb_grab_key(conn, 1, xcb, mods, code[i],
540 XCB_GRAB_MODE_ASYNC);
546 void sys_unwatch(win_t *win, event_t ev, mod_t mod)
548 printf("sys_unwatch: %p - 0x%X,0x%X\n", win, ev, mod2int(mod));
551 list_t *sys_info(void)
553 printf("sys_info\n");
555 if (screens == NULL && do_xinerama_check()) {
556 /* Add Xinerama screens */
557 xcb_xinerama_screen_info_t *info = NULL;
558 int ninfo = do_query_screens(&info);
559 for (int i = 0; i < ninfo; i++) {
560 win_t *screen = new0(win_t);
562 screen->x = info[i].x_org;
563 screen->y = info[i].y_org;
564 screen->w = info[i].width;
565 screen->h = info[i].height;
567 screens = list_insert(NULL, screen);
569 printf("sys_info: xinerama screen - %dx%d @ %d,%d\n",
570 screen->w, screen->h,
571 screen->x, screen->y);
575 if (screens == NULL) {
576 /* No xinerama support */
577 const xcb_setup_t *setup = xcb_get_setup(conn);
578 xcb_screen_t *geom = xcb_setup_roots_iterator(setup).data;
580 win_t *screen = new0(win_t);
582 screen->w = geom->width_in_pixels;
583 screen->h = geom->height_in_pixels;
585 screens = list_insert(NULL, screen);
587 printf("sys_info: root screen - %dx%d\n",
588 screen->w, screen->h);
596 printf("sys_init\n");
598 /* Load configuration */
599 stack = conf_get_int("main.stack", stack);
600 border = conf_get_int("main.border", border);
601 no_capture = conf_get_int("main.no-capture", no_capture);
603 /* Connect to display */
604 if (!(conn = xcb_connect(NULL, NULL)))
605 error("xcb connect failed");
606 if (xcb_connection_has_error(conn))
607 error("xcb connection has errors");
609 /* Allocate key symbols */
610 if (!(keysyms = xcb_key_symbols_alloc(conn)))
611 error("cannot allocate key symbols");
613 /* Get root window */
614 const xcb_setup_t *setup = xcb_get_setup(conn);
615 xcb_screen_iterator_t iter = xcb_setup_roots_iterator(setup);
616 root = iter.data->root;
618 /* Request substructure redirect */
619 xcb_void_cookie_t cookie;
620 xcb_generic_error_t *err;
621 events = XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT |
622 XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY;
623 cookie = xcb_change_window_attributes_checked(conn, root,
624 XCB_CW_EVENT_MASK, &events);
625 if ((err = xcb_request_check(conn, cookie)))
626 error("another window manager is already running");
633 /* Add each initial window */
635 xcb_window_t *kids = NULL;
636 int nkids = do_query_tree(root, &kids);
637 for(int i = 0; i < nkids; i++) {
638 win_t *win = new0(win_t);
639 win->sys = new0(win_sys_t);
640 win->sys->xcb = kids[i];
641 do_get_geometry(kids[i], &win->x, &win->y, &win->w, &win->h);
642 do_get_window_attributes(kids[i],
643 &win->sys->override, &win->sys->mapped);
644 tsearch(win, &cache, win_cmp);
645 if (!win->sys->override && win->sys->mapped)
655 xcb_generic_event_t *event;
656 if (!(event = xcb_wait_for_event(conn)))
660 if (!(status = xcb_flush(conn)))
667 printf("sys_exit\n");
669 xcb_disconnect(conn);
675 printf("sys_free\n");
677 xcb_disconnect(conn);