]> Pileus Git - wmpus/blobdiff - sys-xcb.c
Work on memory cleanup
[wmpus] / sys-xcb.c
index af650cd2c1d648fefd60cc357f8788ca6149f463..b1176a82612215265861193b73eea64b05ac35cf 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,9 +38,17 @@ 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
+       strut_t          strut;  // toolbar struts
        int managed;             // window is managed by wm
 };
 
@@ -50,15 +60,18 @@ typedef enum {
 } 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 void                  *cache;
+static xcb_pixmap_t           colors[NCOLORS];
+static unsigned int           grabbed;
+static xcb_atom_t             wm_protos;
+static xcb_atom_t             wm_delete;
 
 /************************
  * Conversion functions *
@@ -320,6 +333,58 @@ static int do_get_input_focus(void)
        return reply->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 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;
+}
+
+static int do_get_strut(xcb_window_t win, strut_t *strut)
+{
+       xcb_get_property_cookie_t cookie =
+               xcb_ewmh_get_wm_strut(&ewmh, win);
+       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)
 {
        uint16_t r = (rgb & 0xFF0000) >> 8;
@@ -336,7 +401,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)
@@ -382,6 +449,40 @@ static void do_configure_window(xcb_window_t win,
        xcb_configure_window(conn, win, mask, list);
 }
 
+static int do_client_message(xcb_window_t win, xcb_atom_t atom)
+{
+       /* Get protocols */
+       xcb_get_property_cookie_t cookie =
+               xcb_icccm_get_wm_protocols(conn, win, wm_protos);
+       if (!cookie.sequence)
+               return warn("do_client_message: %d - bad cookie", win);
+
+       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", win);
+
+       /* Search for the atom */
+       int found = 0;
+       for (int i = 0; i < protos.atoms_len; i++)
+               if (protos.atoms[i] == atom)
+                       found = 1;
+       if (!found)
+               return warn("do_client_message: %d - no atom", win);
+
+       /* Send the message */
+       xcb_client_message_event_t msg = {
+               .response_type  = XCB_CLIENT_MESSAGE,
+               .format         = 32,
+               .window         = win,
+               .type           = wm_protos,
+               .data.data32[0] = atom,
+               .data.data32[1] = XCB_CURRENT_TIME,
+       };
+       xcb_send_event(conn, 0, win, XCB_EVENT_MASK_NO_EVENT,
+                       (const char *)&msg);
+       return 1;
+}
+
 /**************************
  * Window Manager Helpers *
  **************************/
@@ -590,6 +691,11 @@ static void on_map_request(xcb_map_request_event_t *event)
                        event->window, win);
        if (!win) return;
 
+       if (do_get_strut(win->sys->xcb, &win->sys->strut))
+               printf("Map: Got a strut!\n");
+       else
+               printf("Map: No struts here!\n");
+
        send_state(win, ST_SHOW);
        xcb_map_window(conn, win->sys->xcb);
        sys_move(win, win->x, win->y, win->w, win->h);
@@ -789,7 +895,8 @@ void sys_show(win_t *win, state_t state)
                        break;
 
                case ST_CLOSE:
-                       xcb_kill_client(conn, xcb);
+                       if (!do_client_message(xcb, wm_delete))
+                               xcb_kill_client(conn, xcb);
                        break;
        }
 
@@ -915,6 +1022,16 @@ void sys_init(void)
        root     = iter.data->root;
        colormap = iter.data->default_colormap;
 
+       /* Setup X Atoms */
+       wm_protos = do_intern_atom("WM_PROTOCOLS");
+       wm_delete = do_intern_atom("WM_DELETE_WINDOW");
+       if (!wm_protos || !wm_delete)
+               error("unable to setup atoms");
+
+       /* Setup EWMH connection */
+       if (!do_ewmh_init_atoms())
+               error("ewmh setup failed");
+
        /* Setup for for ST_CLOSE */
        xcb_set_close_down_mode(conn, XCB_CLOSE_DOWN_DESTROY_ALL);
 
@@ -987,7 +1104,9 @@ void sys_exit(void)
 void sys_free(void)
 {
        printf("sys_free\n");
-       if (conn)
+       if (conn) {
+               xcb_ewmh_connection_wipe(&ewmh);
                xcb_disconnect(conn);
+       }
        conn = NULL;
 }