]> Pileus Git - wmpus/blobdiff - sys-xcb.c
Add key and mouse events
[wmpus] / sys-xcb.c
index 7c73a4d67588d25ba3a2fca718e5fe7eb1750f8f..e826148693730eeeb1097d9189432c532a9d122e 100644 (file)
--- a/sys-xcb.c
+++ b/sys-xcb.c
@@ -19,6 +19,8 @@
 #include <search.h>
 
 #include <xcb/xcb.h>
+#include <xcb/xcb_event.h>
+#include <xcb/xcb_keysyms.h>
 #include <xcb/xinerama.h>
 
 #include "util.h"
@@ -34,41 +36,118 @@ static int no_capture = 0;
 
 /* Internal structures */
 struct win_sys {
-       xcb_window_t xcb;
-       int override; // normal vs override redirect
-       int managed;  // window is currently managed by wm
+       xcb_window_t     xcb;    // xcb window id
+       xcb_event_mask_t events; // currently watch events
+       int override;            // normal vs override redirect
+       int mapped;              // window is currently mapped
 };
 
 /* Global data */
-static xcb_connection_t *conn;
-static xcb_window_t      root;
-static list_t           *screens;
-static void             *cache;
-
-/*****************
- * Constant data *
- *****************/
-
-const char *event_names[] = {
-       [XCB_KEY_PRESS        ] "key_press",         [XCB_KEY_RELEASE      ] "key_release",
-       [XCB_BUTTON_PRESS     ] "button_press",      [XCB_BUTTON_RELEASE   ] "button_release",
-       [XCB_MOTION_NOTIFY    ] "motion_notify",     [XCB_ENTER_NOTIFY     ] "enter_notify",
-       [XCB_LEAVE_NOTIFY     ] "leave_notify",      [XCB_FOCUS_IN         ] "focus_in",
-       [XCB_FOCUS_OUT        ] "focus_out",         [XCB_KEYMAP_NOTIFY    ] "keymap_notify",
-       [XCB_EXPOSE           ] "expose",            [XCB_GRAPHICS_EXPOSURE] "graphics_exposure",
-       [XCB_NO_EXPOSURE      ] "no_exposure",       [XCB_VISIBILITY_NOTIFY] "visibility_notify",
-       [XCB_CREATE_NOTIFY    ] "create_notify",     [XCB_DESTROY_NOTIFY   ] "destroy_notify",
-       [XCB_UNMAP_NOTIFY     ] "unmap_notify",      [XCB_MAP_NOTIFY       ] "map_notify",
-       [XCB_MAP_REQUEST      ] "map_request",       [XCB_REPARENT_NOTIFY  ] "reparent_notify",
-       [XCB_CONFIGURE_NOTIFY ] "configure_notify",  [XCB_CONFIGURE_REQUEST] "configure_request",
-       [XCB_GRAVITY_NOTIFY   ] "gravity_notify",    [XCB_RESIZE_REQUEST   ] "resize_request",
-       [XCB_CIRCULATE_NOTIFY ] "circulate_notify",  [XCB_CIRCULATE_REQUEST] "circulate_request",
-       [XCB_PROPERTY_NOTIFY  ] "property_notify",   [XCB_SELECTION_CLEAR  ] "selection_clear",
-       [XCB_SELECTION_REQUEST] "selection_request", [XCB_SELECTION_NOTIFY ] "selection_notify",
-       [XCB_COLORMAP_NOTIFY  ] "colormap_notify",   [XCB_CLIENT_MESSAGE   ] "client_message",
-       [XCB_MAPPING_NOTIFY   ] "mapping_notify",    [XCB_GE_GENERIC       ] "ge_generic",
+static xcb_connection_t  *conn;
+static xcb_key_symbols_t *keysyms;
+static xcb_window_t       root;
+static xcb_event_mask_t   events;
+static list_t            *screens;
+static void              *cache;
+
+
+/************************
+ * Conversion functions *
+ ************************/
+
+/* Key presses */
+static struct {
+       event_t      ev;
+       xcb_keysym_t sym;
+} keysym_map[] = {
+       { EV_LEFT,     0xFF51 },
+       { EV_RIGHT,    0xFF53 },
+       { EV_UP,       0xFF52 },
+       { EV_DOWN,     0xFF54 },
+       { EV_HOME,     0xFF50 },
+       { EV_END,      0xFF57 },
+       { EV_PAGEUP,   0xFF55 },
+       { EV_PAGEDOWN, 0xFF56 },
+       { EV_F1,       0xFFBE },
+       { EV_F2,       0xFFBF },
+       { EV_F3,       0xFFC0 },
+       { EV_F4,       0xFFC1 },
+       { EV_F5,       0xFFC2 },
+       { EV_F6,       0xFFC3 },
+       { EV_F7,       0xFFC4 },
+       { EV_F8,       0xFFC5 },
+       { EV_F9,       0xFFC6 },
+       { EV_F10,      0xFFC7 },
+       { EV_F11,      0xFFC8 },
+       { EV_F12,      0xFFC9 },
 };
 
+static xcb_keycode_t *event_to_keycodes(event_t ev)
+{
+       xcb_keycode_t *codes = NULL;
+
+       /* Get keysym */
+       xcb_keysym_t keysym = map_get(keysym_map, ev, ev, sym, ev);
+
+       /* Get keycodes */
+       if (!(codes = xcb_key_symbols_get_keycode(keysyms, keysym)))
+               warn("no keycode found for %d->%d", ev, keysym);
+
+       return codes;
+}
+
+static event_t keycode_to_event(xcb_keycode_t code)
+{
+       /* Get keysym */
+       xcb_keysym_t keysym = xcb_key_symbols_get_keysym(keysyms, code, 0);
+
+       /* Get event */
+       return map_get(keysym_map, sym,keysym, ev,keysym);
+}
+
+/* Button presses */
+static event_t button_to_event(xcb_button_t btn)
+{
+       return EV_MOUSE0 + btn;
+}
+
+static xcb_button_t event_to_button(event_t ev)
+{
+       return ev - EV_MOUSE0;
+}
+
+/* Modifier masks */
+static xcb_mod_mask_t mod_to_mask(mod_t mod)
+{
+       xcb_mod_mask_t mask = 0;
+       if (mod.alt)   mask |= XCB_MOD_MASK_1;
+       if (mod.ctrl)  mask |= XCB_MOD_MASK_CONTROL;
+       if (mod.shift) mask |= XCB_MOD_MASK_SHIFT;
+       if (mod.win)   mask |= XCB_MOD_MASK_4;
+       return mask;
+}
+
+static mod_t mask_to_mod(xcb_mod_mask_t mask, int up)
+{
+       mod_t mod = { .up = up };
+       if (mask & XCB_MOD_MASK_1)       mod.alt   = 1;
+       if (mask & XCB_MOD_MASK_CONTROL) mod.ctrl  = 1;
+       if (mask & XCB_MOD_MASK_SHIFT)   mod.shift = 1;
+       if (mask & XCB_MOD_MASK_4)       mod.win   = 1;
+       return mod;
+}
+
+/* Mouse pointers */
+static ptr_t list_to_ptr(int16_t *list)
+{
+       ptr_t ptr = {};
+       ptr.rx = list[0]; // root_x
+       ptr.ry = list[1]; // root_y
+       ptr.x  = list[2]; // event_x
+       ptr.y  = list[3]; // event_y
+       return ptr;
+}
+
 /********************
  * Window functions *
  ********************/
@@ -90,7 +169,7 @@ static win_t *win_get(xcb_window_t xcb)
        win_t   **win = tfind(&key, &cache, win_cmp);
 
        if (!win) {
-               printf("Warning: no window for %u\n", xcb);
+               warn("no window for %u", xcb);
                return NULL;
        }
 
@@ -101,42 +180,64 @@ static win_t *win_get(xcb_window_t xcb)
  * XCB Wrappers *
  ****************/
 
-static xcb_query_tree_reply_t *do_query_tree(xcb_window_t win)
+static int do_query_tree(xcb_window_t win, xcb_window_t **kids)
 {
        xcb_query_tree_cookie_t cookie =
                xcb_query_tree(conn, win);
+       if (!cookie.sequence)
+               return warn("do_query_tree: %d - bad cookie", win);
+
        xcb_query_tree_reply_t *reply =
                xcb_query_tree_reply(conn, cookie, NULL);
        if (!reply)
-               error("do_query_tree: %d - no reply", win);
-       printf("do_query_tree: %d\n", win);
-       return reply;
+               return warn("do_query_tree: %d - no reply", win);
+
+       int nkids = xcb_query_tree_children_length(reply);
+       *kids = xcb_query_tree_children(reply);
+       printf("do_query_tree: %d - n=%d\n", win, nkids);
+       return nkids;
 }
 
-static xcb_get_geometry_reply_t *do_get_geometry(xcb_window_t win)
+static int do_get_geometry(xcb_window_t win,
+               int *x, int *y, int *w, int *h)
 {
        xcb_get_geometry_cookie_t cookie =
                xcb_get_geometry(conn, win);
+       if (!cookie.sequence)
+               return warn("do_get_geometry: %d - bad cookie", win);
+
        xcb_get_geometry_reply_t *reply =
                xcb_get_geometry_reply(conn, cookie, NULL);
        if (!reply)
-               error("do_get_geometry: %d - no reply", win);
+               return warn("do_get_geometry: %d - no reply", win);
+
        printf("do_get_geometry: %d - %dx%d @ %d,%d\n",
                        win, reply->width, reply->height, reply->x, reply->y);
-       return reply;
+       *x = reply->x;
+       *y = reply->y;
+       *w = reply->width;
+       *h = reply->height;
+       return 1;
 }
 
-static xcb_get_window_attributes_reply_t *do_get_window_attributes(xcb_window_t win)
+static int do_get_window_attributes(xcb_window_t win,
+               int *override, int *mapped)
 {
        xcb_get_window_attributes_cookie_t cookie =
                xcb_get_window_attributes(conn, win);
+       if (!cookie.sequence)
+               return warn("do_get_window_attributes: %d - bad cookie", win);
+
        xcb_get_window_attributes_reply_t *reply =
                xcb_get_window_attributes_reply(conn, cookie, NULL);
        if (!reply)
-               error("do_get_window_attributes: %d - no reply ", win);
+               return warn("do_get_window_attributes: %d - no reply ", win);
+
        printf("do_get_window_attributes: %d - %d\n",
                        win, reply->override_redirect);
-       return reply;
+       *override = reply->override_redirect;
+       *mapped   = reply->map_state != XCB_MAP_STATE_UNMAPPED;
+       return 1;
 }
 
 static int do_xinerama_check(void)
@@ -144,34 +245,38 @@ static int do_xinerama_check(void)
        const xcb_query_extension_reply_t *data =
                xcb_get_extension_data(conn, &xcb_xinerama_id);
        if (!data || !data->present)
-               return printf("do_xinerama_check: no ext\n"), 0;
+               return warn("do_xinerama_check: no ext");
 
        xcb_xinerama_is_active_cookie_t cookie =
                xcb_xinerama_is_active(conn);
        if (!cookie.sequence)
-               return printf("do_xinerama_check: no cookie\n"), 0;
+               return warn("do_xinerama_check: no cookie");
 
        xcb_xinerama_is_active_reply_t *reply =
                xcb_xinerama_is_active_reply(conn, cookie, NULL);
        if (!reply)
-               printf("do_xinerama_check: no reply\n"), 0;
-       else
-               printf("do_xinerama_check: %d\n", reply->state);
+               warn("do_xinerama_check: no reply");
+
+       printf("do_xinerama_check: %d\n", reply->state);
        return reply && reply->state;
 }
 
-static xcb_xinerama_query_screens_reply_t *do_query_screens(void)
+static int do_query_screens(xcb_xinerama_screen_info_t **info)
 {
        xcb_xinerama_query_screens_cookie_t cookie =
                xcb_xinerama_query_screens(conn);
+       if (!cookie.sequence)
+               return warn("do_query_screens: bad cookie");
+
        xcb_xinerama_query_screens_reply_t *reply =
                xcb_xinerama_query_screens_reply(conn, cookie, NULL);
        if (!reply)
-               printf("do_query_screens: no reply\n");
-       else
-               printf("do_query_screens: %d screens\n",
-                       xcb_xinerama_query_screens_screen_info_length(reply));
-       return reply;
+               return warn("do_query_screens: no reply");
+
+       int ninfo = xcb_xinerama_query_screens_screen_info_length(reply);
+       *info = xcb_xinerama_query_screens_screen_info(reply);
+       printf("do_query_screens: %d screens\n", ninfo);
+       return ninfo;
 }
 
 /**********************
@@ -179,6 +284,53 @@ static xcb_xinerama_query_screens_reply_t *do_query_screens(void)
  **********************/
 
 /* Specific events */
+static void on_key_event(xcb_key_press_event_t *event, int up)
+{
+       xcb_window_t xcb = event->event == root ?
+               event->child : event->event;
+       win_t  *win = win_get(xcb);
+       event_t ev  = keycode_to_event(event->detail);
+       mod_t   mod = mask_to_mod(event->state, up);
+       ptr_t   ptr = list_to_ptr(&event->root_x);
+       printf("on_key_event:         xcb=%u -> win=%p\n", xcb, win);
+       wm_handle_event(win, ev, mod, ptr);
+}
+
+static void on_button_event(xcb_button_press_event_t *event, int up)
+{
+       xcb_window_t xcb = event->event == root ?
+               event->child : event->event;
+       win_t  *win = win_get(xcb);
+       event_t ev  = button_to_event(event->detail);
+       mod_t   mod = mask_to_mod(event->state, up);
+       ptr_t   ptr = list_to_ptr(&event->root_x);
+       printf("on_button_event:      xcb=%u -> win=%p\n", xcb, win);
+
+       if (!wm_handle_event(win, ev, mod, ptr))
+               xcb_allow_events(conn, XCB_ALLOW_REPLAY_POINTER, event->time);
+       else if (!up)
+               xcb_grab_pointer(conn, 1, xcb,
+                               XCB_EVENT_MASK_POINTER_MOTION |
+                               XCB_EVENT_MASK_BUTTON_RELEASE,
+                               XCB_GRAB_MODE_ASYNC,
+                               XCB_GRAB_MODE_ASYNC,
+                               0, 0, XCB_CURRENT_TIME);
+       else
+               xcb_ungrab_pointer(conn, XCB_CURRENT_TIME);
+
+}
+
+static void on_motion_notify(xcb_motion_notify_event_t *event)
+{
+       xcb_window_t xcb = event->event == root ?
+               event->child : event->event;
+       win_t *win = win_get(xcb);
+       ptr_t  ptr = list_to_ptr(&event->root_x);
+       printf("on_motion_notify:     xcb=%u -> win=%p - %d,%d / %d.%d\n", xcb, win,
+                       ptr.x, ptr.y, ptr.rx, ptr.ry);
+       wm_handle_ptr(win, ptr);
+}
+
 static void on_create_notify(xcb_create_notify_event_t *event)
 {
        win_t     *win = new0(win_t);
@@ -215,10 +367,9 @@ static void on_map_request(win_t *win, xcb_map_request_event_t *event)
        printf("on_map_request:       xcb=%u -> win=%p\n",
                        event->window, win);
 
-       if (!win->sys->managed) {
+       if (!win->sys->override && !win->sys->mapped)
                wm_insert(win);
-               win->sys->managed = 1;
-       }
+       win->sys->mapped = 1;
 
        xcb_map_window(conn, win->sys->xcb);
        sys_move(win, win->x, win->y, win->w, win->h);
@@ -255,8 +406,31 @@ static void on_configure_request(win_t *win, xcb_configure_request_event_t *even
 /* Generic Event */
 static void on_event(xcb_generic_event_t *event)
 {
-       win_t *win = NULL;
-       switch (event->response_type) {
+       win_t   *win = NULL;
+
+       int type = XCB_EVENT_RESPONSE_TYPE(event);
+       int sent = XCB_EVENT_SENT(event);
+       const char *name = NULL;
+
+       switch (type) {
+               /* Input handling */
+               case XCB_KEY_PRESS:
+                       on_key_event((xcb_key_press_event_t *)event, 0);
+                       break;
+               case XCB_KEY_RELEASE:
+                       on_key_event((xcb_key_release_event_t *)event, 1);
+                       break;
+               case XCB_BUTTON_PRESS:
+                       on_button_event((xcb_button_press_event_t *)event, 0);
+                       break;
+               case XCB_BUTTON_RELEASE:
+                       on_button_event((xcb_button_release_event_t *)event, 1);
+                       break;
+               case XCB_MOTION_NOTIFY:
+                       on_motion_notify((xcb_motion_notify_event_t *)event);
+                       break;
+
+               /* Window management */
                case XCB_CREATE_NOTIFY:
                        on_create_notify((xcb_create_notify_event_t *)event);
                        break;
@@ -272,8 +446,12 @@ static void on_event(xcb_generic_event_t *event)
                        if ((win = win_get(((xcb_configure_request_event_t *)event)->window)))
                                on_configure_request(win, (xcb_configure_request_event_t *)event);
                        break;
+
+               /* Unknown events */
                default:
-                       printf("on_%s\n", event_names[event->response_type] ?: "unknown_event");
+                       name = xcb_event_get_label(type);
+                       printf("on_event: %d:%02X -> %s\n",
+                               !!sent, type, name?:"unknown_event");
                        break;
        }
 }
@@ -284,7 +462,7 @@ static void on_event(xcb_generic_event_t *event)
 
 void sys_move(win_t *win, int x, int y, int w, int h)
 {
-       printf("sys_move: %p - %dx%d @ %d,%d\n",
+       printf("sys_move:  %p - %dx%d @ %d,%d\n",
                        win, w, h, x, y);
 
        win->x = x;
@@ -313,12 +491,56 @@ void sys_focus(win_t *win)
 
 void sys_show(win_t *win, state_t state)
 {
-       printf("sys_show: %p - %d\n", win, state);
+       printf("sys_show:  %p - %d\n", win, state);
 }
 
 void sys_watch(win_t *win, event_t ev, mod_t mod)
 {
        printf("sys_watch: %p - 0x%X,0x%X\n", win, ev, mod2int(mod));
+       xcb_window_t      xcb  = win ? win->sys->xcb     : root;
+       xcb_event_mask_t *mask = win ? &win->sys->events : &events;
+       xcb_mod_mask_t    mods = 0;
+       xcb_button_t      btn  = 0;
+       xcb_keycode_t    *code = 0;
+
+       switch (ev) {
+               case EV_ENTER:
+                       *mask |= XCB_EVENT_MASK_ENTER_WINDOW;
+                       xcb_change_window_attributes(conn, xcb, XCB_CW_EVENT_MASK, mask);
+                       break;
+
+               case EV_LEAVE:
+                       *mask |= XCB_EVENT_MASK_LEAVE_WINDOW;
+                       xcb_change_window_attributes(conn, xcb, XCB_CW_EVENT_MASK, mask);
+                       break;
+
+               case EV_FOCUS:
+               case EV_UNFOCUS:
+                       *mask |= XCB_EVENT_MASK_FOCUS_CHANGE;
+                       xcb_change_window_attributes(conn, xcb, XCB_CW_EVENT_MASK, mask);
+                       break;
+
+               case EV_MOUSE0...EV_MOUSE7:
+                       btn    = event_to_button(ev);
+                       mods   = mod_to_mask(mod);
+                       *mask |= mod.up ? XCB_EVENT_MASK_BUTTON_RELEASE
+                                       : XCB_EVENT_MASK_BUTTON_PRESS;
+                       xcb_grab_button(conn, 0, xcb, *mask,
+                                       XCB_GRAB_MODE_ASYNC,
+                                       XCB_GRAB_MODE_ASYNC,
+                                       0, 0, btn, mods);
+                       break;
+
+               default:
+                       code = event_to_keycodes(ev);
+                       mods = mod_to_mask(mod);
+                       for (int i = 0; code && code[i] != XCB_NO_SYMBOL; i++)
+                               xcb_grab_key(conn, 1, xcb, mods, code[i],
+                                               XCB_GRAB_MODE_ASYNC,
+                                               XCB_GRAB_MODE_ASYNC);
+
+                       break;
+       }
 }
 
 void sys_unwatch(win_t *win, event_t ev, mod_t mod)
@@ -332,14 +554,8 @@ list_t *sys_info(void)
 
        if (screens == NULL && do_xinerama_check()) {
                /* Add Xinerama screens */
-               xcb_xinerama_query_screens_reply_t *query;
-               unsigned int ninfo;
-               xcb_xinerama_screen_info_t *info;
-
-               query = do_query_screens();
-               ninfo = xcb_xinerama_query_screens_screen_info_length(query);
-               info  = xcb_xinerama_query_screens_screen_info(query);
-
+               xcb_xinerama_screen_info_t *info = NULL;
+               int ninfo = do_query_screens(&info);
                for (int i = 0; i < ninfo; i++) {
                        win_t *screen = new0(win_t);
 
@@ -390,21 +606,24 @@ void sys_init(void)
        if (xcb_connection_has_error(conn))
                error("xcb connection has errors");
 
+       /* Allocate key symbols */
+       if (!(keysyms = xcb_key_symbols_alloc(conn)))
+               error("cannot allocate key symbols");
+
        /* Get root window */
        const xcb_setup_t     *setup = xcb_get_setup(conn);
        xcb_screen_iterator_t  iter  = xcb_setup_roots_iterator(setup);
        root = iter.data->root;
 
        /* Request substructure redirect */
-       xcb_event_mask_t     mask;
-       xcb_void_cookie_t    cookie;
+       xcb_void_cookie_t cookie;
        xcb_generic_error_t *err;
-       mask   = XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT |
+       events = XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT |
                 XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY;
        cookie = xcb_change_window_attributes_checked(conn, root,
-                       XCB_CW_EVENT_MASK, &mask);
+                       XCB_CW_EVENT_MASK, &events);
        if ((err = xcb_request_check(conn, cookie)))
-               error("Another window manager is already running");
+               error("another window manager is already running");
 }
 
 void sys_run(void)
@@ -413,41 +632,19 @@ void sys_run(void)
 
        /* Add each initial window */
        if (!no_capture) {
-               xcb_query_tree_reply_t *tree;
-               unsigned int nkids;
-               xcb_window_t *kids;
-
-               tree  = do_query_tree(root);
-               nkids = xcb_query_tree_children_length(tree);
-               kids  = xcb_query_tree_children(tree);
-
+               xcb_window_t *kids = NULL;
+               int nkids = do_query_tree(root, &kids);
                for(int i = 0; i < nkids; i++) {
-                       xcb_get_geometry_reply_t *geom;
-                       xcb_get_window_attributes_reply_t *attr;
-
-                       geom = do_get_geometry(kids[i]);
-                       attr = do_get_window_attributes(kids[i]);
-
-                       win_t     *win = new0(win_t);
-                       win_sys_t *sys = new0(win_sys_t);
-
-                       win->x        = geom->x;
-                       win->y        = geom->y;
-                       win->w        = geom->width;
-                       win->h        = geom->height;
-                       win->sys      = sys;
-
-                       sys->xcb      = kids[i];
-                       sys->override = attr->override_redirect;
-
+                       win_t *win = new0(win_t);
+                       win->sys = new0(win_sys_t);
+                       win->sys->xcb = kids[i];
+                       do_get_geometry(kids[i], &win->x, &win->y, &win->w, &win->h);
+                       do_get_window_attributes(kids[i],
+                               &win->sys->override, &win->sys->mapped);
                        tsearch(win, &cache, win_cmp);
-
-                       if (!attr->override_redirect) {
+                       if (!win->sys->override && win->sys->mapped)
                                wm_insert(win);
-                               sys->managed = 1;
-                       }
                }
-
                xcb_flush(conn);
        }