]> Pileus Git - wmpus/blobdiff - sys-xcb.c
Various icccm and ewmh properties
[wmpus] / sys-xcb.c
index f876511134e8eb9dd083943af6ad9d50221c2cfc..9a0111cc9bfff4003b1bd1a09cc719f18466f403 100644 (file)
--- a/sys-xcb.c
+++ b/sys-xcb.c
@@ -22,6 +22,8 @@
 #include <xcb/xcb.h>
 #include <xcb/xcb_event.h>
 #include <xcb/xcb_keysyms.h>
+#include <xcb/xcb_icccm.h>
+#include <xcb/xcb_ewmh.h>
 #include <xcb/xinerama.h>
 
 #include "util.h"
@@ -36,35 +38,59 @@ static int stack      = 25;
 static int no_capture = 0;
 
 /* Internal structures */
+typedef struct {
+       int left;
+       int right;
+       int top;
+       int bottom;
+} strut_t;
+
 struct win_sys {
        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
+       strut_t          strut;  // toolbar struts
+       state_t          state;  // window state if not mapped
+       int mapped;              // window is managed by wm
+       int managed;             // window is managed by wm
 };
 
-typedef enum {
-       CLR_FOCUS,
-       CLR_UNFOCUS,
-       CLR_URGENT,
-       NCOLORS
-} color_t;
-
 /* Global data */
-static xcb_connection_t  *conn;
-static xcb_key_symbols_t *keysyms;
-static xcb_colormap_t     colormap;
-static xcb_window_t       root;
-static xcb_event_mask_t   events;
-static list_t            *screens;
-static void              *cache;
-static xcb_pixmap_t       colors[NCOLORS];
-static unsigned int       grabbed;
+static xcb_connection_t      *conn;
+static xcb_ewmh_connection_t  ewmh;
+static xcb_key_symbols_t     *keysyms;
+static xcb_colormap_t         colormap;
+static xcb_window_t           root;
+static xcb_event_mask_t       events;
+static list_t                *screens;
+static list_t                *struts;
+static void                  *cache;
+static unsigned int           grabbed;
+static int                    running;
+static xcb_window_t           control;
+
+static xcb_pixmap_t           clr_focus;
+static xcb_pixmap_t           clr_unfocus;
+static xcb_pixmap_t           clr_urgent;
+
+static xcb_atom_t             wm_protos;
+static xcb_atom_t             wm_delete;
+static xcb_atom_t             wm_nhints;
 
 /************************
  * Conversion functions *
  ************************/
 
+/* State names */
+static char *state_map[] = {
+       [ST_HIDE ] "hide ",
+       [ST_SHOW ] "show ",
+       [ST_FULL ] "full ",
+       [ST_MAX  ] "max  ",
+       [ST_SHADE] "shade",
+       [ST_ICON ] "icon ",
+       [ST_CLOSE] "close",
+};
+
 /* Key presses */
 static struct {
        event_t      ev;
@@ -92,6 +118,10 @@ static struct {
        { EV_F12,      0xFFC9 },
 };
 
+/************************
+ * Conversion functions *
+ ************************/
+
 static xcb_keycode_t *event_to_keycodes(event_t ev)
 {
        xcb_keycode_t *codes = NULL;
@@ -188,67 +218,128 @@ static win_t *win_get(xcb_window_t xcb)
        return *win;
 }
 
+static win_t *win_new(xcb_window_t xcb)
+{
+       win_t *win = new0(win_t);
+       win->sys = new0(win_sys_t);
+       win->sys->xcb = xcb;
+
+       win_t **old = tfind(win, &cache, win_cmp);
+       if (old) {
+               warn("duplicate window for %u\n", xcb);
+               free(win->sys);
+               free(win);
+               return *old;
+       }
+
+       tsearch(win, &cache, win_cmp);
+       printf("win_new: xcb=%-8u -> win=%p\n",
+                       win->sys->xcb, win);
+       return win;
+}
+
+static void win_free(win_t *win)
+{
+       printf("win_free: xcb=%-8u -> win=%p\n",
+                       win->sys->xcb, win);
+       free(win->sys);
+       free(win);
+}
+
+static void win_add_strut(win_t *win)
+{
+       win->type = TYPE_TOOLBAR;
+       for (list_t *cur = screens; cur; cur = cur->next) {
+               win_t   *screen = cur->data;
+               strut_t *strut  = &win->sys->strut;
+               screen->x += strut->left;
+               screen->y += strut->top;
+               screen->w -= strut->left + strut->right;
+               screen->h -= strut->top  + strut->bottom;
+       }
+       struts = list_insert(struts, win);
+
+}
+
+static void win_del_strut(win_t *win)
+{
+       list_t *link = list_find(struts, win);
+       if (!link)
+               return;
+       for (list_t *cur = screens; cur; cur = cur->next) {
+               win_t   *screen = cur->data;
+               strut_t *strut  = &win->sys->strut;
+               screen->x -= strut->left;
+               screen->y -= strut->top;
+               screen->w += strut->left + strut->right;
+               screen->h += strut->top  + strut->bottom;
+       }
+       struts = list_remove(struts, link, 0);
+}
+
 /****************
  * XCB Wrappers *
  ****************/
 
-static int do_query_tree(xcb_window_t win, xcb_window_t **kids)
+static void *do_query_tree(xcb_window_t xcb, xcb_window_t **kids, int *nkids)
 {
        xcb_query_tree_cookie_t cookie =
-               xcb_query_tree(conn, win);
+               xcb_query_tree(conn, xcb);
        if (!cookie.sequence)
-               return warn("do_query_tree: %d - bad cookie", win);
+               return warn("do_query_tree: %d - bad cookie", xcb), NULL;
 
        xcb_query_tree_reply_t *reply =
                xcb_query_tree_reply(conn, cookie, NULL);
        if (!reply)
-               return warn("do_query_tree: %d - no reply", win);
+               return warn("do_query_tree: %d - no reply", xcb), NULL;
 
-       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;
+       *nkids = xcb_query_tree_children_length(reply);
+       *kids  = xcb_query_tree_children(reply);
+       printf("do_query_tree: %d - n=%d\n", xcb, *nkids);
+       return reply;
 }
 
-static int do_get_geometry(xcb_window_t win,
+static int do_get_geometry(xcb_window_t xcb,
                int *x, int *y, int *w, int *h)
 {
        xcb_get_geometry_cookie_t cookie =
-               xcb_get_geometry(conn, win);
+               xcb_get_geometry(conn, xcb);
        if (!cookie.sequence)
-               return warn("do_get_geometry: %d - bad cookie", win);
+               return warn("do_get_geometry: %d - bad cookie", xcb);
 
        xcb_get_geometry_reply_t *reply =
                xcb_get_geometry_reply(conn, cookie, NULL);
        if (!reply)
-               return warn("do_get_geometry: %d - no reply", win);
+               return warn("do_get_geometry: %d - no reply", xcb);
 
        printf("do_get_geometry: %d - %dx%d @ %d,%d\n",
-                       win, reply->width, reply->height, reply->x, reply->y);
+                       xcb, reply->width, reply->height, reply->x, reply->y);
        *x = reply->x;
        *y = reply->y;
        *w = reply->width;
        *h = reply->height;
+       free(reply);
        return 1;
 }
 
-static int do_get_window_attributes(xcb_window_t win,
+static int do_get_window_attributes(xcb_window_t xcb,
                int *override, int *mapped)
 {
        xcb_get_window_attributes_cookie_t cookie =
-               xcb_get_window_attributes(conn, win);
+               xcb_get_window_attributes(conn, xcb);
        if (!cookie.sequence)
-               return warn("do_get_window_attributes: %d - bad cookie", win);
+               return warn("do_get_window_attributes: %d - bad cookie", xcb);
 
        xcb_get_window_attributes_reply_t *reply =
                xcb_get_window_attributes_reply(conn, cookie, NULL);
        if (!reply)
-               return warn("do_get_window_attributes: %d - no reply ", win);
+               return warn("do_get_window_attributes: %d - no reply ", xcb);
 
        printf("do_get_window_attributes: %d - %d\n",
-                       win, reply->override_redirect);
+                       xcb, reply->override_redirect);
        *override = reply->override_redirect;
        *mapped   = reply->map_state != XCB_MAP_STATE_UNMAPPED;
+       free(reply);
        return 1;
 }
 
@@ -267,28 +358,182 @@ static int do_xinerama_check(void)
        xcb_xinerama_is_active_reply_t *reply =
                xcb_xinerama_is_active_reply(conn, cookie, NULL);
        if (!reply)
-               warn("do_xinerama_check: no reply");
+               return warn("do_xinerama_check: no reply");
 
        printf("do_xinerama_check: %d\n", reply->state);
-       return reply && reply->state;
+       int state = reply->state;
+       free(reply);
+       return state;
 }
 
-static int do_query_screens(xcb_xinerama_screen_info_t **info)
+static void *do_query_screens(xcb_xinerama_screen_info_t **info, int *ninfo)
 {
        xcb_xinerama_query_screens_cookie_t cookie =
                xcb_xinerama_query_screens(conn);
        if (!cookie.sequence)
-               return warn("do_query_screens: bad cookie");
+               return warn("do_query_screens: bad cookie"), NULL;
 
        xcb_xinerama_query_screens_reply_t *reply =
                xcb_xinerama_query_screens_reply(conn, cookie, NULL);
        if (!reply)
-               return warn("do_query_screens: no reply");
+               return warn("do_query_screens: no reply"), NULL;
+
+       *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 reply;
+}
+
+static int do_get_input_focus(void)
+{
+       xcb_get_input_focus_cookie_t cookie =
+               xcb_get_input_focus(conn);
+       if (!cookie.sequence)
+               return warn("do_get_input_focus: bad cookie");
+
+       xcb_get_input_focus_reply_t *reply =
+               xcb_get_input_focus_reply(conn, cookie, NULL);
+       if (!reply)
+               return warn("do_get_input_focus: no reply");
+
+       int focus = reply->focus;
+       free(reply);
+       return focus;
+}
+
+static xcb_atom_t do_intern_atom(const char *name)
+{
+       xcb_intern_atom_cookie_t cookie =
+               xcb_intern_atom(conn, 0, strlen(name), name);
+       if (!cookie.sequence)
+               return warn("do_intern_atom: bad cookie");
+
+       xcb_intern_atom_reply_t *reply =
+               xcb_intern_atom_reply(conn, cookie, NULL);
+       if (!reply)
+               return warn("do_intern_atom: no reply");
+
+       xcb_atom_t atom = reply->atom;
+       free(reply);
+       return atom;
+}
+
+static char *do_get_atom_name(xcb_atom_t atom)
+{
+       xcb_get_atom_name_cookie_t cookie =
+               xcb_get_atom_name(conn, atom);
+       if (!cookie.sequence)
+               return warn("do_get_atom_name: bad cookie"), NULL;
+
+       xcb_get_atom_name_reply_t *reply =
+               xcb_get_atom_name_reply(conn, cookie, NULL);
+       if (!reply)
+               return warn("do_get_atom_name: no reply"), NULL;
+
+       char *name = xcb_get_atom_name_name(reply);
+       int len = xcb_get_atom_name_name_length(reply);
+       char *str = strndup(name, len);
+       free(reply);
+       return str;
+}
+
+static int do_ewmh_init_atoms(void)
+{
+       xcb_intern_atom_cookie_t *cookies =
+               xcb_ewmh_init_atoms(conn, &ewmh);
+       if (!cookies)
+               return warn("do_ewmh_init_atoms: no cookies");
+
+       int status =
+               xcb_ewmh_init_atoms_replies(&ewmh, cookies, NULL);
+       if (!status)
+               return warn("do_ewmh_init_atoms: no status");
+       return status;
+}
 
-       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;
+static int do_get_type(xcb_window_t xcb, type_t *type)
+{
+       xcb_get_property_cookie_t cookie =
+               xcb_ewmh_get_wm_window_type(&ewmh, xcb);
+       if (!cookie.sequence)
+               return warn("do_get_type: bad cookie");
+
+       xcb_ewmh_get_atoms_reply_t reply;
+       if (!xcb_ewmh_get_wm_window_type_reply(&ewmh, cookie, &reply, NULL))
+               return warn("do_get_type: no reply");
+
+       type_t prev = *type;
+       for (int i = 0; i < reply.atoms_len; i++) {
+               if (reply.atoms[i] == ewmh._NET_WM_WINDOW_TYPE_DOCK)
+                       *type = TYPE_TOOLBAR;
+               if (reply.atoms[i] == ewmh._NET_WM_WINDOW_TYPE_TOOLBAR)
+                       *type = TYPE_TOOLBAR;
+               if (reply.atoms[i] == ewmh._NET_WM_WINDOW_TYPE_DIALOG)
+                       *type = TYPE_DIALOG;
+       }
+       printf("do_get_type: %d -> %d\n", prev, *type);
+       return 1;
+}
+
+static int do_get_icccm_state(xcb_window_t xcb, state_t *state)
+{
+       xcb_get_property_cookie_t cookie;
+
+       cookie = xcb_icccm_get_wm_normal_hints(conn, xcb);
+       if (!cookie.sequence)
+               return warn("do_get_icccm_state: bad cookie1");
+
+       xcb_size_hints_t sizes;
+       if (!xcb_icccm_get_wm_normal_hints_reply(conn, cookie, &sizes, NULL))
+               return warn("do_get_icccm_state: no sizes");
+
+       state_t prev = *state;
+       printf("do_get_icccm_state: %s -> %s\n",
+                       state_map[prev], state_map[*state]);
+       return 1;
+}
+
+static int do_get_ewmh_state(xcb_window_t xcb, state_t *state)
+{
+       xcb_get_property_cookie_t cookie;
+
+       cookie = xcb_ewmh_get_wm_state(&ewmh, xcb);
+       if (!cookie.sequence)
+               return warn("do_get_ewmh_state: bad cookie2");
+
+       xcb_ewmh_get_atoms_reply_t states;
+       if (!xcb_ewmh_get_wm_state_reply(&ewmh, cookie, &states, NULL))
+               return warn("do_get_ewmh_state: no reply2");
+
+       state_t prev = *state;
+       for (int i = 0; i < states.atoms_len; i++)
+               if (states.atoms[i] == ewmh._NET_WM_STATE_FULLSCREEN)
+                       *state = ST_FULL;
+
+       printf("do_get_ewmh_state: %s -> %s\n",
+                       state_map[prev], state_map[*state]);
+       return 1;
+}
+
+static int do_get_strut(xcb_window_t xcb, strut_t *strut)
+{
+       xcb_get_property_cookie_t cookie =
+               xcb_ewmh_get_wm_strut(&ewmh, xcb);
+       if (!cookie.sequence)
+               return warn("do_get_strut: bad cookie");
+
+       xcb_ewmh_get_extents_reply_t ext = {};
+       int status =
+               xcb_ewmh_get_wm_strut_reply(&ewmh, cookie, &ext, NULL);
+       if (!status)
+               return warn("do_get_strut: no status");
+
+       strut->left   = ext.left;
+       strut->right  = ext.right;
+       strut->top    = ext.top;
+       strut->bottom = ext.bottom;
+
+       return ext.left || ext.right || ext.top || ext.bottom;
 }
 
 static xcb_pixmap_t do_alloc_color(uint32_t rgb)
@@ -307,7 +552,9 @@ static xcb_pixmap_t do_alloc_color(uint32_t rgb)
                return warn("do_alloc_color: no reply");
 
        printf("do_alloc_color: %06x -> %06x\n", rgb, reply->pixel);
-       return reply->pixel;
+       xcb_pixmap_t pixel = reply->pixel;
+       free(reply);
+       return pixel;
 }
 
 static void do_grab_pointer(xcb_event_mask_t mask)
@@ -327,6 +574,67 @@ static void do_ungrab_pointer(void)
                xcb_ungrab_pointer(conn, XCB_CURRENT_TIME);
 }
 
+static void do_configure_window(xcb_window_t xcb,
+               int x, int y, int w, int h,
+               int b, int s, int r)
+{
+       int table[][2] = {
+               { x, XCB_CONFIG_WINDOW_X            },
+               { y, XCB_CONFIG_WINDOW_Y            },
+               { w, XCB_CONFIG_WINDOW_WIDTH        },
+               { h, XCB_CONFIG_WINDOW_HEIGHT       },
+               { b, XCB_CONFIG_WINDOW_BORDER_WIDTH },
+               { s, XCB_CONFIG_WINDOW_SIBLING      },
+               { r, XCB_CONFIG_WINDOW_STACK_MODE   },
+       };
+
+       uint16_t mask = 0;
+       uint32_t list[countof(table)];
+       for (int i=0,j=0; i < countof(table); i++) {
+               if (table[i][0] >= 0) {
+                       list[j++] = table[i][0];
+                       mask     |= table[i][1];
+               }
+       }
+
+       xcb_configure_window(conn, xcb, mask, list);
+}
+
+static int do_client_message(xcb_window_t xcb, xcb_atom_t atom)
+{
+       /* Get protocols */
+       xcb_get_property_cookie_t cookie =
+               xcb_icccm_get_wm_protocols(conn, xcb, wm_protos);
+       if (!cookie.sequence)
+               return warn("do_client_message: %d - bad cookie", xcb);
+
+       xcb_icccm_get_wm_protocols_reply_t protos = {};
+       if (!xcb_icccm_get_wm_protocols_reply(conn, cookie, &protos, NULL))
+               return warn("do_client_message: %d - no reply", xcb);
+
+       /* Search for the atom */
+       int found = 0;
+       for (int i = 0; i < protos.atoms_len; i++)
+               if (protos.atoms[i] == atom)
+                       found = 1;
+       xcb_icccm_get_wm_protocols_reply_wipe(&protos);
+       if (!found)
+               return warn("do_client_message: %d - no atom", xcb);
+
+       /* Send the message */
+       xcb_client_message_event_t msg = {
+               .response_type  = XCB_CLIENT_MESSAGE,
+               .format         = 32,
+               .window         = xcb,
+               .type           = wm_protos,
+               .data.data32[0] = atom,
+               .data.data32[1] = XCB_CURRENT_TIME,
+       };
+       xcb_send_event(conn, 0, xcb, XCB_EVENT_MASK_NO_EVENT,
+                       (const char *)&msg);
+       return 1;
+}
+
 /**************************
  * Window Manager Helpers *
  **************************/
@@ -369,11 +677,30 @@ static int send_pointer(int16_t *pos,
 }
 
 /* Send window state info */
-static int send_state(void)
+static void send_manage(win_t *win, int managed)
 {
-       return 0;
+       if (win->sys->managed == managed)
+               return;
+       if (managed)
+               wm_insert(win);
+       else
+               wm_remove(win);
+       win->sys->managed = managed;
 }
 
+/* Send window state info */
+static void send_state(win_t *win, state_t next)
+{
+       if (!win->sys->managed)
+               return;
+       if (!win->sys->mapped)
+               return;
+       if (win->state == next)
+               return;
+       state_t prev = win->state;
+       win->state = next;
+       wm_handle_state(win, prev, next);
+}
 
 /**********************
  * X11 Event Handlers *
@@ -383,9 +710,10 @@ static int send_state(void)
 static void on_key_event(xcb_key_press_event_t *event, int up)
 {
        printf("on_key_event:         xcb=%-8u\n", event->event);
+       xcb_window_t focus = do_get_input_focus();
        event_t ev = keycode_to_event(event->detail);
        send_event_info(ev, event->state, up, &event->root_x,
-               event->root, event->event, event->child);
+               event->root, focus, event->child);
 }
 
 static void on_button_event(xcb_button_press_event_t *event, int up)
@@ -436,7 +764,7 @@ static void on_focus_in(xcb_focus_in_event_t *event)
                return;
        printf("on_focus_in:          xcb=%-8u mode=%d\n", event->event, event->mode);
        xcb_change_window_attributes(conn, event->event,
-                       XCB_CW_BORDER_PIXEL, &colors[CLR_FOCUS]);
+                       XCB_CW_BORDER_PIXEL, &clr_focus);
        if (event->mode == XCB_NOTIFY_MODE_NORMAL)
                send_event(EV_FOCUS, event->event);
 }
@@ -448,66 +776,99 @@ static void on_focus_out(xcb_focus_out_event_t *event)
                return;
        printf("on_focus_out:         xcb=%-8u mode=%d\n", event->event, event->mode);
        xcb_change_window_attributes(conn, event->event,
-                       XCB_CW_BORDER_PIXEL, &colors[CLR_UNFOCUS]);
+                       XCB_CW_BORDER_PIXEL, &clr_unfocus);
        if (event->mode == XCB_NOTIFY_MODE_NORMAL)
                send_event(EV_UNFOCUS, event->event);
 }
 
 static void on_create_notify(xcb_create_notify_event_t *event)
 {
-       win_t     *win = new0(win_t);
-       win_sys_t *sys = new0(win_sys_t);
+       printf("on_create_notify:     xcb=%-8u\n", event->window);
 
-       printf("on_create_notify:     xcb=%-8u -> win=%p\n",
-                       event->window, win);
+       win_t *win = win_new(event->window);
 
-       win->x        = event->x;
-       win->y        = event->y;
-       win->w        = event->width;
-       win->h        = event->height;
-       win->sys      = sys;
+       win->x = event->x;
+       win->y = event->y;
+       win->w = event->width;
+       win->h = event->height;
 
-       sys->xcb      = event->window;
-       sys->override = event->override_redirect;
+       win->sys->state  = ST_SHOW;
+       win->sys->events = XCB_EVENT_MASK_PROPERTY_CHANGE;
+       xcb_change_window_attributes(conn, event->window,
+                       XCB_CW_EVENT_MASK, &win->sys->events);
 
-       tsearch(win, &cache, win_cmp);
+       if (!event->override_redirect)
+               send_manage(win, 1);
 }
 
-static void on_destroy_notify(win_t *win, xcb_destroy_notify_event_t *event)
+static void on_destroy_notify(xcb_destroy_notify_event_t *event)
 {
+       win_t *win = win_get(event->window);
        printf("on_destroy_notify:    xcb=%-8u -> win=%p\n",
                        event->window, win);
+       if (!win) return;
 
+       send_manage(win, 0);
        tdelete(win, &cache, win_cmp);
+       win_free(win);
+}
 
-       free(win->sys);
-       free(win);
+static void on_unmap_notify(xcb_unmap_notify_event_t *event)
+{
+       win_t *win = win_get(event->window);
+       printf("on_unmap_notify:      xcb=%-8u -> win=%p\n",
+                       event->window, win);
+       if (!win) return;
+
+       win_del_strut(win);
+       send_state(win, ST_HIDE);
+       win->sys->mapped = 0;
 }
 
-static void on_map_request(win_t *win, xcb_map_request_event_t *event)
+static void on_map_notify(xcb_map_notify_event_t *event)
 {
-       printf("on_map_request:       xcb=%-8u -> win=%p\n",
+       win_t *win = win_get(event->window);
+       printf("on_map_notify:        xcb=%-8u -> win=%p\n",
                        event->window, win);
+       if (!win) return;
 
-       if (!win->sys->override && !win->sys->mapped)
-               wm_insert(win);
        win->sys->mapped = 1;
+       send_state(win, win->sys->state);
+}
 
+static void on_map_request(xcb_map_request_event_t *event)
+{
+       win_t *win = win_get(event->window);
+       printf("on_map_request:       xcb=%-8u -> win=%p\n",
+                       event->window, win);
+       if (!win) return;
+
+       win->sys->mapped = 1;
+       send_state(win, win->sys->state);
        xcb_map_window(conn, win->sys->xcb);
-       sys_move(win, win->x, win->y, win->w, win->h);
+       if (!win->sys->managed)
+               sys_move(win, win->x, win->y, win->w, win->h);
 }
 
-static void on_configure_request(win_t *win, xcb_configure_request_event_t *event)
+static void on_configure_request(xcb_configure_request_event_t *event)
 {
+       win_t *win = win_get(event->window);
        printf("on_configure_request: xcb=%-8u -> win=%p -- %dx%d @ %d,%d\n",
                        event->window, win,
                        event->width, event->height,
                        event->x, event->y);
-
-       win->x = event->x;
-       win->y = event->y;
-       win->w = event->width;
-       win->h = event->height;
+       if (!win) return;
+       printf("on_configure_request: xcb=%-8u -> win=%p << %dx%d @ %d,%d\n",
+                       event->window, win,
+                       win->w, win->h,
+                       win->x, win->y);
+
+       if (!win->sys->managed) {
+               win->x = event->x;
+               win->y = event->y;
+               win->w = event->width;
+               win->h = event->height;
+       }
 
        xcb_configure_notify_event_t resp = {
                .response_type = XCB_CONFIGURE_NOTIFY,
@@ -525,14 +886,80 @@ static void on_configure_request(win_t *win, xcb_configure_request_event_t *even
                        (const char *)&resp);
 }
 
+static void on_property_notify(xcb_property_notify_event_t *event)
+{
+       win_t *win = win_get(event->window);
+       char *name = do_get_atom_name(event->atom);
+       printf("on_property_notify: xcb=%-8u -> win=%p - %s\n",
+                       event->window, win, name);
+       if (name) free(name);
+       if (!win) return;
+
+       /* Check window type */
+       if (event->atom == ewmh._NET_WM_WINDOW_TYPE)
+               do_get_type(event->window, &win->type);
+
+       /* Check struts */
+       if (event->atom == ewmh._NET_WM_STRUT)
+               if (do_get_strut(win->sys->xcb, &win->sys->strut))
+                       win_add_strut(win);
+
+       /* Check window state */
+       if (event->atom == wm_nhints)
+               if (do_get_icccm_state(event->window, &win->sys->state))
+                       send_state(win, win->sys->state);
+       if (event->atom == ewmh._NET_WM_STATE)
+               if (do_get_ewmh_state(event->window, &win->sys->state))
+                       send_state(win, win->sys->state);
+}
+
+static void on_client_message(xcb_client_message_event_t *event)
+{
+       win_t *win = win_get(event->window);
+       char *name = do_get_atom_name(event->type);
+       printf("on_client_message: xcb=%-8u -> win=%p - %s=[%d,%d,%d,%d]\n",
+                       event->window, win, name,
+                       event->data.data32[0], event->data.data32[1],
+                       event->data.data32[2], event->data.data32[3]);
+       if (name) free(name);
+       if (!win) return;
+
+       /* Exit request */
+       if (event->window         == control   &&
+           event->type           == wm_protos &&
+           event->data.data32[0] == wm_delete) {
+               printf("on_client_message: shutdown request");
+               running = 0;
+       }
+
+       /* Close request */
+       if (event->type == ewmh._NET_CLOSE_WINDOW) {
+               printf("on_client_message: close request");
+               sys_show(win, ST_CLOSE);
+       }
+
+       /* Fullscreen request  */
+       if ((event->type           == ewmh._NET_WM_STATE) &&
+           (event->data.data32[1] == ewmh._NET_WM_STATE_FULLSCREEN ||
+            event->data.data32[2] == ewmh._NET_WM_STATE_FULLSCREEN)) {
+               printf("on_client_message: fullscreen request");
+               int full = win->state == ST_FULL;
+               switch (event->data.data32[0]) {
+                       case XCB_EWMH_WM_STATE_REMOVE: full  = 0; break;
+                       case XCB_EWMH_WM_STATE_ADD:    full  = 1; break;
+                       case XCB_EWMH_WM_STATE_TOGGLE: full ^= 1; break;
+               }
+               win->sys->state = full ? ST_FULL : ST_SHOW;
+               send_state(win, win->sys->state);
+               sys_show(win, win->sys->state);
+       }
+
+}
+
 /* Generic Event */
 static void on_event(xcb_generic_event_t *event)
 {
-       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 */
@@ -569,23 +996,33 @@ static void on_event(xcb_generic_event_t *event)
                        on_create_notify((xcb_create_notify_event_t *)event);
                        break;
                case XCB_DESTROY_NOTIFY:
-                       if ((win = win_get(((xcb_destroy_notify_event_t *)event)->window)))
-                               on_destroy_notify(win, (xcb_destroy_notify_event_t *)event);
+                       on_destroy_notify((xcb_destroy_notify_event_t *)event);
+                       break;
+               case XCB_UNMAP_NOTIFY:
+                       on_unmap_notify((xcb_unmap_notify_event_t *)event);
+                       break;
+               case XCB_MAP_NOTIFY:
+                       on_map_notify((xcb_map_notify_event_t *)event);
                        break;
                case XCB_MAP_REQUEST:
-                       if ((win = win_get(((xcb_map_request_event_t *)event)->window)))
-                               on_map_request(win, (xcb_map_request_event_t *)event);
+                       on_map_request((xcb_map_request_event_t *)event);
                        break;
                case XCB_CONFIGURE_REQUEST:
-                       if ((win = win_get(((xcb_configure_request_event_t *)event)->window)))
-                               on_configure_request(win, (xcb_configure_request_event_t *)event);
+                       on_configure_request((xcb_configure_request_event_t *)event);
+                       break;
+               case XCB_PROPERTY_NOTIFY:
+                       on_property_notify((xcb_property_notify_event_t *)event);
+                       break;
+               case XCB_CLIENT_MESSAGE:
+                       on_client_message((xcb_client_message_event_t *)event);
                        break;
 
                /* Unknown events */
                default:
-                       name = xcb_event_get_label(type);
                        printf("on_event: %d:%02X -> %s\n",
-                               !!sent, type, name?:"unknown_event");
+                               XCB_EVENT_SENT(event) != 0,
+                               XCB_EVENT_RESPONSE_TYPE(event),
+                               xcb_event_get_label(type) ?: "unknown_event");
                        break;
        }
 }
@@ -608,20 +1045,20 @@ void sys_move(win_t *win, int x, int y, int w, int h)
        w      = MAX(w-b,1);
        h      = MAX(h-b,1);
 
-       uint16_t mask   = XCB_CONFIG_WINDOW_X
-                       | XCB_CONFIG_WINDOW_Y
-                       | XCB_CONFIG_WINDOW_WIDTH
-                       | XCB_CONFIG_WINDOW_HEIGHT
-                       | XCB_CONFIG_WINDOW_BORDER_WIDTH;
-       uint32_t list[] = {x, y, w, h, border};
-
-       xcb_configure_window(conn, win->sys->xcb, mask, list);
+       do_configure_window(win->sys->xcb, x, y, w, h, -1, -1, -1);
 }
 
 void sys_raise(win_t *win)
 {
        printf("sys_raise: %p\n", win);
-       xcb_circulate_window(conn, XCB_CIRCULATE_RAISE_LOWEST, win->sys->xcb);
+
+       uint16_t mask = XCB_CONFIG_WINDOW_STACK_MODE;
+       uint32_t list = XCB_STACK_MODE_ABOVE;
+
+       xcb_configure_window(conn, win->sys->xcb, mask, &list);
+       for (list_t *cur = struts; cur; cur = cur->next)
+               xcb_configure_window(conn,
+                       ((win_t*)cur->data)->sys->xcb, mask, &list);
 }
 
 void sys_focus(win_t *win)
@@ -635,7 +1072,76 @@ 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 - %s -> %s\n", win,
+                       state_map[win->state], state_map[state]);
+       xcb_window_t xcb = win ? win->sys->xcb : root;
+
+       /* Find screen */
+       win_t full, max;
+       if (state == ST_FULL || state == ST_MAX) {
+               for (list_t *cur = screens; cur; cur = cur->next) {
+                       full = max = *(win_t*)cur->data;
+                       if (win->x >= max.x && win->x <= max.x+max.w &&
+                           win->y >= max.y && win->y <= max.y+max.h)
+                               break;
+               }
+               for (list_t *cur = struts; cur; cur = cur->next) {
+                       strut_t *strut = &((win_t*)cur->data)->sys->strut;
+                       full.x -= strut->left;
+                       full.y -= strut->top;
+                       full.w += strut->left + strut->right;
+                       full.h += strut->top  + strut->bottom;
+               }
+       }
+
+       /* Change window state */
+       switch (state) {
+               case ST_HIDE:
+                       xcb_unmap_window(conn, xcb);
+                       break;
+
+               case ST_SHOW:
+                       xcb_map_window(conn, xcb);
+                       do_configure_window(xcb, win->x, win->y,
+                                       MAX(win->w - 2*border, 1),
+                                       MAX(win->h - 2*border, 1),
+                                       border, -1, -1);
+                       break;
+
+               case ST_FULL:
+                       xcb_map_window(conn, xcb);
+                       do_configure_window(xcb, full.x, full.y, full.w, full.h,
+                                       0, -1, XCB_STACK_MODE_ABOVE);
+                       break;
+
+               case ST_MAX:
+                       xcb_map_window(conn, xcb);
+                       do_configure_window(xcb, max.x, max.y,
+                                       MAX(max.w - 2*border, 1),
+                                       MAX(max.h - 2*border, 1),
+                                       border, -1, XCB_STACK_MODE_ABOVE);
+                       break;
+
+               case ST_SHADE:
+                       xcb_map_window(conn, xcb);
+                       do_configure_window(xcb, -1, -1, -1, stack,
+                                       border, -1, -1);
+                       break;
+
+               case ST_ICON:
+                       xcb_map_window(conn, xcb);
+                       do_configure_window(xcb, -1, -1, 100, 100,
+                                       border, -1, -1);
+                       break;
+
+               case ST_CLOSE:
+                       if (!do_client_message(xcb, wm_delete))
+                               xcb_kill_client(conn, xcb);
+                       break;
+       }
+
+       /* Update state */
+       win->sys->state = win->state = state;
 }
 
 void sys_watch(win_t *win, event_t ev, mod_t mod)
@@ -682,7 +1188,7 @@ void sys_watch(win_t *win, event_t ev, mod_t mod)
                                xcb_grab_key(conn, 1, xcb, mods, code[i],
                                                XCB_GRAB_MODE_ASYNC,
                                                XCB_GRAB_MODE_ASYNC);
-
+                       free(code);
                        break;
        }
 }
@@ -698,8 +1204,9 @@ list_t *sys_info(void)
 
        if (screens == NULL && do_xinerama_check()) {
                /* Add Xinerama screens */
+               int ninfo = 0;
                xcb_xinerama_screen_info_t *info = NULL;
-               int ninfo = do_query_screens(&info);
+               void *reply = do_query_screens(&info, &ninfo);
                for (int i = 0; i < ninfo; i++) {
                        win_t *screen = new0(win_t);
 
@@ -714,6 +1221,7 @@ list_t *sys_info(void)
                                        screen->w, screen->h,
                                        screen->x, screen->y);
                }
+               free(reply);
        }
 
        if (screens == NULL) {
@@ -739,6 +1247,9 @@ void sys_init(void)
 {
        printf("sys_init\n");
 
+       xcb_void_cookie_t cookie;
+       xcb_generic_error_t *err;
+
        /* Load configuration */
        stack      = conf_get_int("main.stack",      stack);
        border     = conf_get_int("main.border",     border);
@@ -756,24 +1267,53 @@ void sys_init(void)
        root     = iter.data->root;
        colormap = iter.data->default_colormap;
 
-       /* Allocate key symbols */
-       if (!(keysyms = xcb_key_symbols_alloc(conn)))
-               error("cannot allocate key symbols");
-
-       /* Read color information */
-       colors[CLR_FOCUS]   = do_alloc_color(0xFF6060);
-       colors[CLR_UNFOCUS] = do_alloc_color(0xD8D8FF);
-       colors[CLR_URGENT]  = do_alloc_color(0xFF0000);
-
        /* Request substructure redirect */
-       xcb_void_cookie_t cookie;
-       xcb_generic_error_t *err;
        events = XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT |
-                XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY;
+                XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY;
        cookie = xcb_change_window_attributes_checked(conn, root,
                        XCB_CW_EVENT_MASK, &events);
        if ((err = xcb_request_check(conn, cookie)))
                error("another window manager is already running");
+
+       /* Setup X Atoms */
+       wm_protos = do_intern_atom("WM_PROTOCOLS");
+       wm_delete = do_intern_atom("WM_DELETE_WINDOW");
+       wm_nhints = do_intern_atom("WM_NORMAL_HINTS");
+       if (!wm_protos || !wm_delete || !wm_nhints)
+               error("unable to setup atoms");
+
+       /* Setup EWMH connection */
+       if (!do_ewmh_init_atoms())
+               error("ewmh setup failed");
+       xcb_ewmh_set_supported(&ewmh, 0, 82, &ewmh._NET_SUPPORTED);
+
+       /* Set EWMH wm window */
+       uint32_t override = 1;
+       control = xcb_generate_id(conn);
+       printf("control window: %d\n", control);
+       cookie  = xcb_create_window_checked(conn, 0, control, root,
+                       0, 0, 1, 1, 0, 0, 0,
+                       XCB_CW_OVERRIDE_REDIRECT, &override);
+       if ((err = xcb_request_check(conn, cookie)))
+               error("can't create control window");
+       cookie = xcb_ewmh_set_wm_name_checked(&ewmh, control, 5, "wmpus");
+       if ((err = xcb_request_check(conn, cookie)))
+               error("can't set wm name");
+       cookie = xcb_ewmh_set_supporting_wm_check_checked(&ewmh, root, control);
+       if ((err = xcb_request_check(conn, cookie)))
+               error("can't set control window");
+
+       /* Setup for for ST_CLOSE */
+       xcb_set_close_down_mode(conn, XCB_CLOSE_DOWN_DESTROY_ALL);
+
+       /* Allocate key symbols */
+       if (!(keysyms = xcb_key_symbols_alloc(conn)))
+               error("cannot allocate key symbols");
+
+       /* Read color information */
+       clr_focus   = do_alloc_color(0xFF6060);
+       clr_unfocus = do_alloc_color(0xD8D8FF);
+       clr_urgent  = do_alloc_color(0xFF0000);
 }
 
 void sys_run(void)
@@ -782,24 +1322,37 @@ void sys_run(void)
 
        /* Add each initial window */
        if (!no_capture) {
+               int nkids = 0;
                xcb_window_t *kids = NULL;
-               int nkids = do_query_tree(root, &kids);
+               void *reply = do_query_tree(root, &kids, &nkids);
                for(int i = 0; i < nkids; i++) {
-                       win_t *win = new0(win_t);
-                       win->sys = new0(win_sys_t);
-                       win->sys->xcb = kids[i];
+                       int override=0, mapped=0;
+                       if (kids[i] == control)
+                               continue;
+                       win_t *win = win_new(kids[i]);
+                       if (do_get_strut(win->sys->xcb, &win->sys->strut))
+                               win_add_strut(win);
                        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 (!win->sys->override && win->sys->mapped)
-                               wm_insert(win);
+                       do_get_window_attributes(kids[i], &override, &mapped);
+                       printf("  found %-8u %dx%d @ %d,%d --%s%s\n", kids[i],
+                                       win->w, win->h, win->x, win->y,
+                                       override ? " override" : "",
+                                       mapped   ? " mapped"   : "");
+                       state_t state = mapped ? ST_SHOW : ST_HIDE;
+                       win->sys->mapped = mapped;
+                       do_get_type(kids[i], &win->type);
+                       do_get_icccm_state(kids[i], &state);
+                       do_get_ewmh_state(kids[i], &state);
+                       send_manage(win, !override);
+                       send_state(win, state);
                }
+               free(reply);
                xcb_flush(conn);
        }
 
        /* Main loop */
-       while (1)
+       running = 1;
+       while (running)
        {
                int status;
                xcb_generic_event_t *event;
@@ -815,15 +1368,44 @@ void sys_run(void)
 void sys_exit(void)
 {
        printf("sys_exit\n");
-       if (conn)
-               xcb_disconnect(conn);
-       conn = NULL;
+
+       xcb_client_message_event_t msg = {
+               .response_type  = XCB_CLIENT_MESSAGE,
+               .format         = 32,
+               .window         = control,
+               .type           = wm_protos,
+               .data.data32[0] = wm_delete,
+               .data.data32[1] = XCB_CURRENT_TIME,
+       };
+       xcb_send_event(conn, 0, control, XCB_EVENT_MASK_NO_EVENT,
+                       (const char *)&msg);
+       xcb_flush(conn);
 }
 
 void sys_free(void)
 {
        printf("sys_free\n");
-       if (conn)
-               xcb_disconnect(conn);
-       conn = NULL;
+
+       xcb_void_cookie_t cookie;
+       xcb_generic_error_t *err;
+
+       /* unregister wm */ 
+       cookie = xcb_delete_property_checked(conn, root,
+                       ewmh._NET_SUPPORTING_WM_CHECK);
+       if ((err = xcb_request_check(conn, cookie)))
+               warn("can't remove control window");
+
+       cookie = xcb_destroy_window_checked(conn, control);
+       if ((err = xcb_request_check(conn, cookie)))
+               warn("can't destroy control window");
+
+       /* close connection */
+       xcb_ewmh_connection_wipe(&ewmh);
+       xcb_key_symbols_free(keysyms);
+       xcb_disconnect(conn);
+
+       /* free local data */
+       while (screens)
+               screens = list_remove(screens, screens, 1);
+       tdestroy(cache, (void(*)(void*))win_free);
 }