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
23 #include <xcb/xcb_event.h>
24 #include <xcb/xcb_keysyms.h>
25 #include <xcb/xcb_icccm.h>
26 #include <xcb/xcb_ewmh.h>
27 #include <xcb/xinerama.h>
36 static int border = 2;
37 static int stack = 25;
38 static int no_capture = 0;
40 /* Internal structures */
49 xcb_window_t xcb; // xcb window id
50 xcb_event_mask_t events; // currently watch events
51 strut_t strut; // toolbar struts
52 int managed; // window is managed by wm
56 static xcb_connection_t *conn;
57 static xcb_ewmh_connection_t ewmh;
58 static xcb_key_symbols_t *keysyms;
59 static xcb_colormap_t colormap;
60 static xcb_window_t root;
61 static xcb_event_mask_t events;
62 static list_t *screens;
64 static unsigned int grabbed;
66 static xcb_window_t control;
68 static xcb_pixmap_t clr_focus;
69 static xcb_pixmap_t clr_unfocus;
70 static xcb_pixmap_t clr_urgent;
72 static xcb_atom_t wm_protos;
73 static xcb_atom_t wm_delete;
75 /************************
76 * Conversion functions *
77 ************************/
80 static char *state_map[] = {
101 { EV_PAGEUP, 0xFF55 },
102 { EV_PAGEDOWN, 0xFF56 },
117 /************************
118 * Conversion functions *
119 ************************/
121 static xcb_keycode_t *event_to_keycodes(event_t ev)
123 xcb_keycode_t *codes = NULL;
126 xcb_keysym_t keysym = map_get(keysym_map, ev, ev, sym, ev);
129 if (!(codes = xcb_key_symbols_get_keycode(keysyms, keysym)))
130 warn("no keycode found for %d->%d", ev, keysym);
135 static event_t keycode_to_event(xcb_keycode_t code)
138 xcb_keysym_t keysym = xcb_key_symbols_get_keysym(keysyms, code, 0);
141 return map_get(keysym_map, sym,keysym, ev,keysym);
145 static event_t button_to_event(xcb_button_t btn)
147 return EV_MOUSE0 + btn;
150 static xcb_button_t event_to_button(event_t ev)
152 return ev - EV_MOUSE0;
156 static xcb_mod_mask_t mod_to_mask(mod_t mod)
158 xcb_mod_mask_t mask = 0;
159 if (mod.alt) mask |= XCB_MOD_MASK_1;
160 if (mod.ctrl) mask |= XCB_MOD_MASK_CONTROL;
161 if (mod.shift) mask |= XCB_MOD_MASK_SHIFT;
162 if (mod.win) mask |= XCB_MOD_MASK_4;
166 static mod_t mask_to_mod(xcb_mod_mask_t mask, int up)
168 mod_t mod = { .up = up };
169 if (mask & XCB_MOD_MASK_1) mod.alt = 1;
170 if (mask & XCB_MOD_MASK_CONTROL) mod.ctrl = 1;
171 if (mask & XCB_MOD_MASK_SHIFT) mod.shift = 1;
172 if (mask & XCB_MOD_MASK_4) mod.win = 1;
177 static ptr_t list_to_ptr(int16_t *list)
181 ptr.rx = list[0]; // root_x
182 ptr.ry = list[1]; // root_y
183 ptr.x = list[2]; // event_x
184 ptr.y = list[3]; // event_y
189 /********************
191 ********************/
193 static int win_cmp(const void *_a, const void *_b)
197 if (a->sys->xcb < b->sys->xcb) return -1;
198 if (a->sys->xcb > b->sys->xcb) return 1;
202 static win_t *win_get(xcb_window_t xcb)
204 win_sys_t sys = { .xcb = xcb };
205 win_t key = { .sys = &sys };
207 win_t **win = tfind(&key, &cache, win_cmp);
210 warn("no window for %u", xcb);
217 static win_t *win_new(xcb_window_t xcb)
219 win_t *win = new0(win_t);
220 win->sys = new0(win_sys_t);
223 win_t **old = tfind(win, &cache, win_cmp);
225 warn("duplicate window for %u\n", xcb);
231 tsearch(win, &cache, win_cmp);
232 printf("win_new: xcb=%-8u -> win=%p\n",
237 static void win_free(win_t *win)
239 printf("win_free: xcb=%-8u -> win=%p\n",
249 static void *do_query_tree(xcb_window_t win, xcb_window_t **kids, int *nkids)
251 xcb_query_tree_cookie_t cookie =
252 xcb_query_tree(conn, win);
253 if (!cookie.sequence)
254 return warn("do_query_tree: %d - bad cookie", win), NULL;
256 xcb_query_tree_reply_t *reply =
257 xcb_query_tree_reply(conn, cookie, NULL);
259 return warn("do_query_tree: %d - no reply", win), NULL;
261 *nkids = xcb_query_tree_children_length(reply);
262 *kids = xcb_query_tree_children(reply);
263 printf("do_query_tree: %d - n=%d\n", win, *nkids);
267 static int do_get_geometry(xcb_window_t win,
268 int *x, int *y, int *w, int *h)
270 xcb_get_geometry_cookie_t cookie =
271 xcb_get_geometry(conn, win);
272 if (!cookie.sequence)
273 return warn("do_get_geometry: %d - bad cookie", win);
275 xcb_get_geometry_reply_t *reply =
276 xcb_get_geometry_reply(conn, cookie, NULL);
278 return warn("do_get_geometry: %d - no reply", win);
280 printf("do_get_geometry: %d - %dx%d @ %d,%d\n",
281 win, reply->width, reply->height, reply->x, reply->y);
290 static int do_get_window_attributes(xcb_window_t win,
291 int *override, int *mapped)
293 xcb_get_window_attributes_cookie_t cookie =
294 xcb_get_window_attributes(conn, win);
295 if (!cookie.sequence)
296 return warn("do_get_window_attributes: %d - bad cookie", win);
298 xcb_get_window_attributes_reply_t *reply =
299 xcb_get_window_attributes_reply(conn, cookie, NULL);
301 return warn("do_get_window_attributes: %d - no reply ", win);
303 printf("do_get_window_attributes: %d - %d\n",
304 win, reply->override_redirect);
305 *override = reply->override_redirect;
306 *mapped = reply->map_state != XCB_MAP_STATE_UNMAPPED;
311 static int do_xinerama_check(void)
313 const xcb_query_extension_reply_t *data =
314 xcb_get_extension_data(conn, &xcb_xinerama_id);
315 if (!data || !data->present)
316 return warn("do_xinerama_check: no ext");
318 xcb_xinerama_is_active_cookie_t cookie =
319 xcb_xinerama_is_active(conn);
320 if (!cookie.sequence)
321 return warn("do_xinerama_check: no cookie");
323 xcb_xinerama_is_active_reply_t *reply =
324 xcb_xinerama_is_active_reply(conn, cookie, NULL);
326 return warn("do_xinerama_check: no reply");
328 printf("do_xinerama_check: %d\n", reply->state);
329 int state = reply->state;
334 static void *do_query_screens(xcb_xinerama_screen_info_t **info, int *ninfo)
336 xcb_xinerama_query_screens_cookie_t cookie =
337 xcb_xinerama_query_screens(conn);
338 if (!cookie.sequence)
339 return warn("do_query_screens: bad cookie"), NULL;
341 xcb_xinerama_query_screens_reply_t *reply =
342 xcb_xinerama_query_screens_reply(conn, cookie, NULL);
344 return warn("do_query_screens: no reply"), NULL;
346 *ninfo = xcb_xinerama_query_screens_screen_info_length(reply);
347 *info = xcb_xinerama_query_screens_screen_info(reply);
348 printf("do_query_screens: %d screens\n", *ninfo);
352 static int do_get_input_focus(void)
354 xcb_get_input_focus_cookie_t cookie =
355 xcb_get_input_focus(conn);
356 if (!cookie.sequence)
357 return warn("do_get_input_focus: bad cookie");
359 xcb_get_input_focus_reply_t *reply =
360 xcb_get_input_focus_reply(conn, cookie, NULL);
362 return warn("do_get_input_focus: no reply");
364 int focus = reply->focus;
369 static xcb_atom_t do_intern_atom(const char *name)
371 xcb_intern_atom_cookie_t cookie =
372 xcb_intern_atom(conn, 0, strlen(name), name);
373 if (!cookie.sequence)
374 return warn("do_intern_atom: bad cookie");
376 xcb_intern_atom_reply_t *reply =
377 xcb_intern_atom_reply(conn, cookie, NULL);
379 return warn("do_intern_atom: no reply");
381 xcb_atom_t atom = reply->atom;
386 static int do_ewmh_init_atoms(void)
388 xcb_intern_atom_cookie_t *cookies =
389 xcb_ewmh_init_atoms(conn, &ewmh);
391 return warn("do_ewmh_init_atoms: no cookies");
394 xcb_ewmh_init_atoms_replies(&ewmh, cookies, NULL);
396 return warn("do_ewmh_init_atoms: no status");
400 static int do_get_strut(xcb_window_t win, strut_t *strut)
402 xcb_get_property_cookie_t cookie =
403 xcb_ewmh_get_wm_strut(&ewmh, win);
404 if (!cookie.sequence)
405 return warn("do_get_strut: bad cookie");
407 xcb_ewmh_get_extents_reply_t ext = {};
409 xcb_ewmh_get_wm_strut_reply(&ewmh, cookie, &ext, NULL);
411 return warn("do_get_strut: no status");
413 strut->left = ext.left;
414 strut->right = ext.right;
415 strut->top = ext.top;
416 strut->bottom = ext.bottom;
418 return ext.left || ext.right || ext.top || ext.bottom;
421 static xcb_pixmap_t do_alloc_color(uint32_t rgb)
423 uint16_t r = (rgb & 0xFF0000) >> 8;
424 uint16_t g = (rgb & 0x00FF00);
425 uint16_t b = (rgb & 0x0000FF) << 8;
426 xcb_alloc_color_cookie_t cookie =
427 xcb_alloc_color(conn, colormap, r, g, b);
428 if (!cookie.sequence)
429 return warn("do_alloc_color: bad cookie");
431 xcb_alloc_color_reply_t *reply =
432 xcb_alloc_color_reply(conn, cookie, NULL);
434 return warn("do_alloc_color: no reply");
436 printf("do_alloc_color: %06x -> %06x\n", rgb, reply->pixel);
437 xcb_pixmap_t pixel = reply->pixel;
442 static void do_grab_pointer(xcb_event_mask_t mask)
445 xcb_grab_pointer(conn, 0, root, mask,
448 0, 0, XCB_CURRENT_TIME);
452 static void do_ungrab_pointer(void)
456 xcb_ungrab_pointer(conn, XCB_CURRENT_TIME);
459 static void do_configure_window(xcb_window_t win,
460 int x, int y, int w, int h,
464 { x, XCB_CONFIG_WINDOW_X },
465 { y, XCB_CONFIG_WINDOW_Y },
466 { w, XCB_CONFIG_WINDOW_WIDTH },
467 { h, XCB_CONFIG_WINDOW_HEIGHT },
468 { b, XCB_CONFIG_WINDOW_BORDER_WIDTH },
469 { s, XCB_CONFIG_WINDOW_SIBLING },
470 { r, XCB_CONFIG_WINDOW_STACK_MODE },
474 uint32_t list[7] = {};
475 for (int i = 0; i < 7; i++) {
476 if (table[i][0] >= 0) {
477 list[i] = table[i][0];
482 xcb_configure_window(conn, win, mask, list);
485 static int do_client_message(xcb_window_t win, xcb_atom_t atom)
488 xcb_get_property_cookie_t cookie =
489 xcb_icccm_get_wm_protocols(conn, win, wm_protos);
490 if (!cookie.sequence)
491 return warn("do_client_message: %d - bad cookie", win);
493 xcb_icccm_get_wm_protocols_reply_t protos = {};
494 if (!xcb_icccm_get_wm_protocols_reply(conn, cookie, &protos, NULL))
495 return warn("do_client_message: %d - no reply", win);
497 /* Search for the atom */
499 for (int i = 0; i < protos.atoms_len; i++)
500 if (protos.atoms[i] == atom)
502 xcb_icccm_get_wm_protocols_reply_wipe(&protos);
504 return warn("do_client_message: %d - no atom", win);
506 /* Send the message */
507 xcb_client_message_event_t msg = {
508 .response_type = XCB_CLIENT_MESSAGE,
512 .data.data32[0] = atom,
513 .data.data32[1] = XCB_CURRENT_TIME,
515 xcb_send_event(conn, 0, win, XCB_EVENT_MASK_NO_EVENT,
520 /**************************
521 * Window Manager Helpers *
522 **************************/
524 /* Send event info */
525 static int send_event(event_t ev, xcb_window_t ewin)
527 win_t *win = win_get(ewin);
529 int status = wm_handle_event(win, ev, MOD(), PTR());
534 /* Send event info */
535 static int send_event_info(event_t ev, xcb_mod_mask_t mask, int up, int16_t *pos,
536 xcb_window_t rwin, xcb_window_t ewin, xcb_window_t cwin)
538 xcb_window_t xcb = ewin == rwin ? cwin : ewin;
539 win_t *win = win_get(xcb);
540 mod_t mod = mask_to_mod(mask, up);
541 ptr_t ptr = list_to_ptr(pos);
543 int status = wm_handle_event(win, ev, mod, ptr);
548 /* Send pointer motion info */
549 static int send_pointer(int16_t *pos,
550 xcb_window_t rwin, xcb_window_t ewin, xcb_window_t cwin)
552 xcb_window_t xcb = ewin == rwin ? cwin : ewin;
553 win_t *win = win_get(xcb);
554 ptr_t ptr = list_to_ptr(pos);
556 int status = wm_handle_ptr(win, ptr);
561 /* Send window state info */
562 static void send_manage(win_t *win, int managed)
564 if (win->sys->managed == managed)
570 win->sys->managed = managed;
573 /* Send window state info */
574 static void send_state(win_t *win, state_t next)
576 if (!win->sys->managed)
578 if (win->state == next)
580 state_t prev = win->state;
582 wm_handle_state(win, prev, next);
585 /**********************
586 * X11 Event Handlers *
587 **********************/
589 /* Specific events */
590 static void on_key_event(xcb_key_press_event_t *event, int up)
592 printf("on_key_event: xcb=%-8u\n", event->event);
593 xcb_window_t focus = do_get_input_focus();
594 event_t ev = keycode_to_event(event->detail);
595 send_event_info(ev, event->state, up, &event->root_x,
596 event->root, focus, event->child);
599 static void on_button_event(xcb_button_press_event_t *event, int up)
601 printf("on_button_event: xcb=%-8u\n", event->event);
602 event_t ev = button_to_event(event->detail);
603 if (!send_event_info(ev, event->state, up, &event->root_x,
604 event->root, event->event, event->child))
605 xcb_allow_events(conn, XCB_ALLOW_REPLAY_POINTER, event->time);
607 do_grab_pointer(XCB_EVENT_MASK_POINTER_MOTION |
608 XCB_EVENT_MASK_BUTTON_RELEASE);
614 static void on_motion_notify(xcb_motion_notify_event_t *event)
616 printf("on_motion_notify: xcb=%-8u - %d,%d / %d.%d\n", event->event,
617 event->event_x, event->event_y,
618 event->root_x, event->root_y);
619 send_pointer(&event->root_x, event->root, event->event, event->child);
622 static void on_enter_notify(xcb_enter_notify_event_t *event)
624 if (event->mode != XCB_NOTIFY_MODE_NORMAL)
626 printf("on_enter_notify: xcb=%-8u\n", event->event);
627 send_event_info(EV_ENTER, event->state, 0, &event->root_x,
628 event->root, event->event, event->child);
631 static void on_leave_notify(xcb_leave_notify_event_t *event)
633 if (event->mode != XCB_NOTIFY_MODE_NORMAL)
635 printf("on_leave_notify: xcb=%-8u\n", event->event);
636 send_event_info(EV_LEAVE, event->state, 0, &event->root_x,
637 event->root, event->event, event->child);
640 static void on_focus_in(xcb_focus_in_event_t *event)
642 if (event->mode != XCB_NOTIFY_MODE_NORMAL &&
643 event->mode != XCB_NOTIFY_MODE_WHILE_GRABBED)
645 printf("on_focus_in: xcb=%-8u mode=%d\n", event->event, event->mode);
646 xcb_change_window_attributes(conn, event->event,
647 XCB_CW_BORDER_PIXEL, &clr_focus);
648 if (event->mode == XCB_NOTIFY_MODE_NORMAL)
649 send_event(EV_FOCUS, event->event);
652 static void on_focus_out(xcb_focus_out_event_t *event)
654 if (event->mode != XCB_NOTIFY_MODE_NORMAL &&
655 event->mode != XCB_NOTIFY_MODE_WHILE_GRABBED)
657 printf("on_focus_out: xcb=%-8u mode=%d\n", event->event, event->mode);
658 xcb_change_window_attributes(conn, event->event,
659 XCB_CW_BORDER_PIXEL, &clr_unfocus);
660 if (event->mode == XCB_NOTIFY_MODE_NORMAL)
661 send_event(EV_UNFOCUS, event->event);
664 static void on_create_notify(xcb_create_notify_event_t *event)
666 printf("on_create_notify: xcb=%-8u\n", event->window);
668 win_t *win = win_new(event->window);
672 win->w = event->width;
673 win->h = event->height;
675 if (!event->override_redirect)
679 static void on_destroy_notify(xcb_destroy_notify_event_t *event)
681 win_t *win = win_get(event->window);
682 printf("on_destroy_notify: xcb=%-8u -> win=%p\n",
687 tdelete(win, &cache, win_cmp);
691 static void on_unmap_notify(xcb_unmap_notify_event_t *event)
693 win_t *win = win_get(event->window);
694 printf("on_unmap_notify: xcb=%-8u -> win=%p\n",
698 send_state(win, ST_HIDE);
701 static void on_map_notify(xcb_map_notify_event_t *event)
703 win_t *win = win_get(event->window);
704 printf("on_map_notify: xcb=%-8u -> win=%p\n",
708 send_state(win, ST_SHOW);
711 static void on_map_request(xcb_map_request_event_t *event)
713 win_t *win = win_get(event->window);
714 printf("on_map_request: xcb=%-8u -> win=%p\n",
718 if (do_get_strut(win->sys->xcb, &win->sys->strut))
719 printf("Map: Got a strut!\n");
721 printf("Map: No struts here!\n");
723 send_state(win, ST_SHOW);
724 xcb_map_window(conn, win->sys->xcb);
725 sys_move(win, win->x, win->y, win->w, win->h);
728 static void on_configure_request(xcb_configure_request_event_t *event)
730 win_t *win = win_get(event->window);
731 printf("on_configure_request: xcb=%-8u -> win=%p -- %dx%d @ %d,%d\n",
733 event->width, event->height,
737 if (!win->sys->managed) {
740 win->w = event->width;
741 win->h = event->height;
744 xcb_configure_notify_event_t resp = {
745 .response_type = XCB_CONFIGURE_NOTIFY,
746 .event = win->sys->xcb,
747 .window = win->sys->xcb,
752 .border_width = border,
755 xcb_send_event(conn, 0, win->sys->xcb,
756 XCB_EVENT_MASK_STRUCTURE_NOTIFY,
757 (const char *)&resp);
760 static void on_client_message(xcb_client_message_event_t *event)
762 printf("on_client_message: xcb=%-8u\n", event->window);
763 if (event->window == control &&
764 event->type == wm_protos &&
765 event->data.data32[0] == wm_delete)
770 static void on_event(xcb_generic_event_t *event)
772 int type = XCB_EVENT_RESPONSE_TYPE(event);
777 on_key_event((xcb_key_press_event_t *)event, 0);
779 case XCB_KEY_RELEASE:
780 on_key_event((xcb_key_release_event_t *)event, 1);
782 case XCB_BUTTON_PRESS:
783 on_button_event((xcb_button_press_event_t *)event, 0);
785 case XCB_BUTTON_RELEASE:
786 on_button_event((xcb_button_release_event_t *)event, 1);
788 case XCB_MOTION_NOTIFY:
789 on_motion_notify((xcb_motion_notify_event_t *)event);
791 case XCB_ENTER_NOTIFY:
792 on_enter_notify((xcb_enter_notify_event_t *)event);
794 case XCB_LEAVE_NOTIFY:
795 on_leave_notify((xcb_leave_notify_event_t *)event);
798 on_focus_in((xcb_focus_in_event_t *)event);
801 on_focus_out((xcb_focus_out_event_t *)event);
804 /* Window management */
805 case XCB_CREATE_NOTIFY:
806 on_create_notify((xcb_create_notify_event_t *)event);
808 case XCB_DESTROY_NOTIFY:
809 on_destroy_notify((xcb_destroy_notify_event_t *)event);
811 case XCB_UNMAP_NOTIFY:
812 on_unmap_notify((xcb_unmap_notify_event_t *)event);
815 on_map_notify((xcb_map_notify_event_t *)event);
817 case XCB_MAP_REQUEST:
818 on_map_request((xcb_map_request_event_t *)event);
820 case XCB_CONFIGURE_REQUEST:
821 on_configure_request((xcb_configure_request_event_t *)event);
823 case XCB_CLIENT_MESSAGE:
824 on_client_message((xcb_client_message_event_t *)event);
829 printf("on_event: %d:%02X -> %s\n",
830 XCB_EVENT_SENT(event) != 0,
831 XCB_EVENT_RESPONSE_TYPE(event),
832 xcb_event_get_label(type) ?: "unknown_event");
837 /********************
839 ********************/
841 void sys_move(win_t *win, int x, int y, int w, int h)
843 printf("sys_move: %p - %dx%d @ %d,%d\n",
855 do_configure_window(win->sys->xcb, x, y, w, h, -1, -1, -1);
858 void sys_raise(win_t *win)
860 printf("sys_raise: %p\n", win);
862 uint16_t mask = XCB_CONFIG_WINDOW_STACK_MODE;
863 uint32_t list = XCB_STACK_MODE_ABOVE;
865 xcb_configure_window(conn, win->sys->xcb, mask, &list);
868 void sys_focus(win_t *win)
870 printf("sys_focus: %p\n", win);
871 xcb_window_t xcb = win ? win->sys->xcb : root;
873 xcb_set_input_focus(conn, XCB_INPUT_FOCUS_POINTER_ROOT,
874 xcb, XCB_CURRENT_TIME);
877 void sys_show(win_t *win, state_t state)
879 printf("sys_show: %p - %s -> %s\n", win,
880 state_map[win->state], state_map[state]);
881 xcb_window_t xcb = win ? win->sys->xcb : root;
884 win_t *screen = NULL;
885 if (state == ST_FULL || state == ST_MAX) {
886 for (list_t *cur = screens; cur; cur = cur->next) {
888 if (win->x >= screen->x && win->x <= screen->x+screen->w &&
889 win->y >= screen->y && win->y <= screen->y+screen->h)
894 /* Change window state */
897 xcb_unmap_window(conn, xcb);
901 xcb_map_window(conn, xcb);
902 do_configure_window(xcb, win->x, win->y,
903 MAX(win->w - 2*border, 1),
904 MAX(win->h - 2*border, 1),
909 xcb_map_window(conn, xcb);
910 do_configure_window(xcb, screen->x, screen->y, screen->w, screen->h,
911 0, -1, XCB_STACK_MODE_ABOVE);
912 xcb_circulate_window(conn, XCB_CIRCULATE_RAISE_LOWEST, xcb);
916 xcb_map_window(conn, xcb);
917 do_configure_window(xcb, screen->x, screen->y,
918 MAX(screen->w - 2*border, 1),
919 MAX(screen->h - 2*border, 1),
920 border, -1, XCB_STACK_MODE_ABOVE);
924 xcb_map_window(conn, xcb);
925 do_configure_window(xcb, -1, -1, -1, stack,
930 xcb_map_window(conn, xcb);
931 do_configure_window(xcb, -1, -1, 100, 100,
936 if (!do_client_message(xcb, wm_delete))
937 xcb_kill_client(conn, xcb);
945 void sys_watch(win_t *win, event_t ev, mod_t mod)
947 printf("sys_watch: %p - 0x%X,0x%X\n", win, ev, mod2int(mod));
948 xcb_window_t xcb = win ? win->sys->xcb : root;
949 xcb_event_mask_t *mask = win ? &win->sys->events : &events;
950 xcb_mod_mask_t mods = 0;
951 xcb_button_t btn = 0;
952 xcb_keycode_t *code = 0;
956 *mask |= XCB_EVENT_MASK_ENTER_WINDOW;
957 xcb_change_window_attributes(conn, xcb, XCB_CW_EVENT_MASK, mask);
961 *mask |= XCB_EVENT_MASK_LEAVE_WINDOW;
962 xcb_change_window_attributes(conn, xcb, XCB_CW_EVENT_MASK, mask);
967 *mask |= XCB_EVENT_MASK_FOCUS_CHANGE;
968 xcb_change_window_attributes(conn, xcb, XCB_CW_EVENT_MASK, mask);
971 case EV_MOUSE0...EV_MOUSE7:
972 btn = event_to_button(ev);
973 mods = mod_to_mask(mod);
974 *mask |= mod.up ? XCB_EVENT_MASK_BUTTON_RELEASE
975 : XCB_EVENT_MASK_BUTTON_PRESS;
976 xcb_grab_button(conn, 0, xcb, *mask,
983 code = event_to_keycodes(ev);
984 mods = mod_to_mask(mod);
985 for (int i = 0; code && code[i] != XCB_NO_SYMBOL; i++)
986 xcb_grab_key(conn, 1, xcb, mods, code[i],
988 XCB_GRAB_MODE_ASYNC);
994 void sys_unwatch(win_t *win, event_t ev, mod_t mod)
996 printf("sys_unwatch: %p - 0x%X,0x%X\n", win, ev, mod2int(mod));
999 list_t *sys_info(void)
1001 printf("sys_info\n");
1003 if (screens == NULL && do_xinerama_check()) {
1004 /* Add Xinerama screens */
1006 xcb_xinerama_screen_info_t *info = NULL;
1007 void *reply = do_query_screens(&info, &ninfo);
1008 for (int i = 0; i < ninfo; i++) {
1009 win_t *screen = new0(win_t);
1011 screen->x = info[i].x_org;
1012 screen->y = info[i].y_org;
1013 screen->w = info[i].width;
1014 screen->h = info[i].height;
1016 screens = list_insert(NULL, screen);
1018 printf("sys_info: xinerama screen - %dx%d @ %d,%d\n",
1019 screen->w, screen->h,
1020 screen->x, screen->y);
1025 if (screens == NULL) {
1026 /* No xinerama support */
1027 const xcb_setup_t *setup = xcb_get_setup(conn);
1028 xcb_screen_t *geom = xcb_setup_roots_iterator(setup).data;
1030 win_t *screen = new0(win_t);
1032 screen->w = geom->width_in_pixels;
1033 screen->h = geom->height_in_pixels;
1035 screens = list_insert(NULL, screen);
1037 printf("sys_info: root screen - %dx%d\n",
1038 screen->w, screen->h);
1046 printf("sys_init\n");
1048 xcb_void_cookie_t cookie;
1049 xcb_generic_error_t *err;
1051 /* Load configuration */
1052 stack = conf_get_int("main.stack", stack);
1053 border = conf_get_int("main.border", border);
1054 no_capture = conf_get_int("main.no-capture", no_capture);
1056 /* Connect to display */
1057 if (!(conn = xcb_connect(NULL, NULL)))
1058 error("xcb connect failed");
1059 if (xcb_connection_has_error(conn))
1060 error("xcb connection has errors");
1062 /* Get root window */
1063 const xcb_setup_t *setup = xcb_get_setup(conn);
1064 xcb_screen_iterator_t iter = xcb_setup_roots_iterator(setup);
1065 root = iter.data->root;
1066 colormap = iter.data->default_colormap;
1068 /* Request substructure redirect */
1069 events = XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT |
1070 XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY;
1071 cookie = xcb_change_window_attributes_checked(conn, root,
1072 XCB_CW_EVENT_MASK, &events);
1073 if ((err = xcb_request_check(conn, cookie)))
1074 error("another window manager is already running");
1077 wm_protos = do_intern_atom("WM_PROTOCOLS");
1078 wm_delete = do_intern_atom("WM_DELETE_WINDOW");
1079 if (!wm_protos || !wm_delete)
1080 error("unable to setup atoms");
1082 /* Setup EWMH connection */
1083 if (!do_ewmh_init_atoms())
1084 error("ewmh setup failed");
1086 /* Set EWMH wm window */
1087 uint32_t override = 1;
1088 control = xcb_generate_id(conn);
1089 printf("control window: %d\n", control);
1090 cookie = xcb_create_window_checked(conn, 0, control, root,
1091 0, 0, 1, 1, 0, 0, 0,
1092 XCB_CW_OVERRIDE_REDIRECT, &override);
1093 if ((err = xcb_request_check(conn, cookie)))
1094 error("can't create control window");
1095 cookie = xcb_ewmh_set_wm_name_checked(&ewmh, control, 5, "wmpus");
1096 if ((err = xcb_request_check(conn, cookie)))
1097 error("can't set wm name");
1098 cookie = xcb_ewmh_set_supporting_wm_check_checked(&ewmh, root, control);
1099 if ((err = xcb_request_check(conn, cookie)))
1100 error("can't set control window");
1102 /* Setup for for ST_CLOSE */
1103 xcb_set_close_down_mode(conn, XCB_CLOSE_DOWN_DESTROY_ALL);
1105 /* Allocate key symbols */
1106 if (!(keysyms = xcb_key_symbols_alloc(conn)))
1107 error("cannot allocate key symbols");
1109 /* Read color information */
1110 clr_focus = do_alloc_color(0xFF6060);
1111 clr_unfocus = do_alloc_color(0xD8D8FF);
1112 clr_urgent = do_alloc_color(0xFF0000);
1117 printf("sys_run\n");
1119 /* Add each initial window */
1122 xcb_window_t *kids = NULL;
1123 void *reply = do_query_tree(root, &kids, &nkids);
1124 for(int i = 0; i < nkids; i++) {
1125 int override=0, mapped=0;
1126 if (kids[i] == control)
1128 win_t *win = win_new(kids[i]);
1129 do_get_geometry(kids[i], &win->x, &win->y, &win->w, &win->h);
1130 do_get_window_attributes(kids[i], &override, &mapped);
1131 printf(" found %-8u %dx%d @ %d,%d --%s%s\n", kids[i],
1132 win->w, win->h, win->x, win->y,
1133 override ? " override" : "",
1134 mapped ? " mapped" : "");
1136 send_manage(win, 1);
1138 send_state(win, ST_SHOW);
1149 xcb_generic_event_t *event;
1150 if (!(event = xcb_wait_for_event(conn)))
1154 if (!(status = xcb_flush(conn)))
1161 printf("sys_exit\n");
1163 xcb_client_message_event_t msg = {
1164 .response_type = XCB_CLIENT_MESSAGE,
1168 .data.data32[0] = wm_delete,
1169 .data.data32[1] = XCB_CURRENT_TIME,
1171 xcb_send_event(conn, 0, control, XCB_EVENT_MASK_NO_EVENT,
1172 (const char *)&msg);
1178 printf("sys_free\n");
1180 xcb_void_cookie_t cookie;
1181 xcb_generic_error_t *err;
1184 cookie = xcb_delete_property_checked(conn, root,
1185 ewmh._NET_SUPPORTING_WM_CHECK);
1186 if ((err = xcb_request_check(conn, cookie)))
1187 warn("can't remove control window");
1189 cookie = xcb_destroy_window_checked(conn, control);
1190 if ((err = xcb_request_check(conn, cookie)))
1191 warn("can't destroy control window");
1193 /* close connection */
1194 xcb_ewmh_connection_wipe(&ewmh);
1195 xcb_key_symbols_free(keysyms);
1196 xcb_disconnect(conn);
1198 /* free local data */
1200 screens = list_remove(screens, screens, 1);
1201 tdestroy(cache, (void(*)(void*))win_free);