]> Pileus Git - wmpus/blobdiff - sys-xcb.c
Rename xcb windows to xcb
[wmpus] / sys-xcb.c
index b1176a82612215265861193b73eea64b05ac35cf..f412bcd3b87e6da585ec79efd299751ab210ef9e 100644 (file)
--- a/sys-xcb.c
+++ b/sys-xcb.c
@@ -52,13 +52,6 @@ struct win_sys {
        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_ewmh_connection_t  ewmh;
@@ -67,9 +60,16 @@ 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 xcb_pixmap_t           colors[NCOLORS];
 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;
 
@@ -215,67 +215,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;
 }
 
@@ -294,28 +355,30 @@ 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;
 
-       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;
+       *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)
@@ -330,7 +393,9 @@ static int do_get_input_focus(void)
        if (!reply)
                return warn("do_get_input_focus: no reply");
 
-       return reply->focus;
+       int focus = reply->focus;
+       free(reply);
+       return focus;
 }
 
 static xcb_atom_t do_intern_atom(const char *name)
@@ -364,10 +429,10 @@ static int do_ewmh_init_atoms(void)
        return status;
 }
 
-static int do_get_strut(xcb_window_t win, strut_t *strut)
+static int do_get_strut(xcb_window_t xcb, strut_t *strut)
 {
        xcb_get_property_cookie_t cookie =
-               xcb_ewmh_get_wm_strut(&ewmh, win);
+               xcb_ewmh_get_wm_strut(&ewmh, xcb);
        if (!cookie.sequence)
                return warn("do_get_strut: bad cookie");
 
@@ -423,7 +488,7 @@ static void do_ungrab_pointer(void)
                xcb_ungrab_pointer(conn, XCB_CURRENT_TIME);
 }
 
-static void do_configure_window(xcb_window_t win,
+static void do_configure_window(xcb_window_t xcb,
                int x, int y, int w, int h,
                int b, int s, int r)
 {
@@ -446,39 +511,40 @@ static void do_configure_window(xcb_window_t win,
                }
        }
 
-       xcb_configure_window(conn, win, mask, list);
+       xcb_configure_window(conn, xcb, mask, list);
 }
 
-static int do_client_message(xcb_window_t win, xcb_atom_t atom)
+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, win, wm_protos);
+               xcb_icccm_get_wm_protocols(conn, xcb, wm_protos);
        if (!cookie.sequence)
-               return warn("do_client_message: %d - bad cookie", win);
+               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", win);
+               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", win);
+               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         = win,
+               .window         = xcb,
                .type           = wm_protos,
                .data.data32[0] = atom,
                .data.data32[1] = XCB_CURRENT_TIME,
        };
-       xcb_send_event(conn, 0, win, XCB_EVENT_MASK_NO_EVENT,
+       xcb_send_event(conn, 0, xcb, XCB_EVENT_MASK_NO_EVENT,
                        (const char *)&msg);
        return 1;
 }
@@ -610,7 +676,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);
 }
@@ -622,31 +688,24 @@ 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 -> win=%p\n",
-                       event->window, win);
+       printf("on_create_notify:     xcb=%-8u\n", event->window);
 
-       win->x        = event->x;
-       win->y        = event->y;
-       win->w        = event->width;
-       win->h        = event->height;
-       win->sys      = sys;
+       win_t *win = win_new(event->window);
 
-       sys->xcb      = event->window;
+       win->x = event->x;
+       win->y = event->y;
+       win->w = event->width;
+       win->h = event->height;
 
        if (!event->override_redirect)
                send_manage(win, 1);
-
-       tsearch(win, &cache, win_cmp);
 }
 
 static void on_destroy_notify(xcb_destroy_notify_event_t *event)
@@ -657,11 +716,8 @@ static void on_destroy_notify(xcb_destroy_notify_event_t *event)
        if (!win) return;
 
        send_manage(win, 0);
-
        tdelete(win, &cache, win_cmp);
-
-       free(win->sys);
-       free(win);
+       win_free(win);
 }
 
 static void on_unmap_notify(xcb_unmap_notify_event_t *event)
@@ -671,6 +727,7 @@ static void on_unmap_notify(xcb_unmap_notify_event_t *event)
                        event->window, win);
        if (!win) return;
 
+       win_del_strut(win);
        send_state(win, ST_HIDE);
 }
 
@@ -692,10 +749,7 @@ static void on_map_request(xcb_map_request_event_t *event)
        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");
-
+               win_add_strut(win);
        send_state(win, ST_SHOW);
        xcb_map_window(conn, win->sys->xcb);
        sys_move(win, win->x, win->y, win->w, win->h);
@@ -710,10 +764,12 @@ static void on_configure_request(xcb_configure_request_event_t *event)
                        event->x, event->y);
        if (!win) return;
 
-       win->x = event->x;
-       win->y = event->y;
-       win->w = event->width;
-       win->h = event->height;
+       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,
@@ -731,6 +787,15 @@ static void on_configure_request(xcb_configure_request_event_t *event)
                        (const char *)&resp);
 }
 
+static void on_client_message(xcb_client_message_event_t *event)
+{
+       printf("on_client_message: xcb=%-8u\n", event->window);
+       if (event->window         == control   &&
+           event->type           == wm_protos &&
+           event->data.data32[0] == wm_delete)
+               running = 0;
+}
+
 /* Generic Event */
 static void on_event(xcb_generic_event_t *event)
 {
@@ -785,6 +850,9 @@ static void on_event(xcb_generic_event_t *event)
                case XCB_CONFIGURE_REQUEST:
                        on_configure_request((xcb_configure_request_event_t *)event);
                        break;
+               case XCB_CLIENT_MESSAGE:
+                       on_client_message((xcb_client_message_event_t *)event);
+                       break;
 
                /* Unknown events */
                default:
@@ -948,7 +1016,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;
        }
 }
@@ -964,8 +1032,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);
 
@@ -980,6 +1049,7 @@ list_t *sys_info(void)
                                        screen->w, screen->h,
                                        screen->x, screen->y);
                }
+               free(reply);
        }
 
        if (screens == NULL) {
@@ -1005,6 +1075,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);
@@ -1022,6 +1095,14 @@ void sys_init(void)
        root     = iter.data->root;
        colormap = iter.data->default_colormap;
 
+       /* Request 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, &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");
@@ -1032,6 +1113,22 @@ void sys_init(void)
        if (!do_ewmh_init_atoms())
                error("ewmh setup failed");
 
+       /* 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);
 
@@ -1040,19 +1137,9 @@ void sys_init(void)
                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;
-       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");
+       clr_focus   = do_alloc_color(0xFF6060);
+       clr_unfocus = do_alloc_color(0xD8D8FF);
+       clr_urgent  = do_alloc_color(0xFF0000);
 }
 
 void sys_run(void)
@@ -1061,26 +1148,34 @@ 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++) {
                        int override=0, mapped=0;
-                       win_t *win = new0(win_t);
-                       win->sys = new0(win_sys_t);
-                       win->sys->xcb = kids[i];
-                       tsearch(win, &cache, win_cmp);
+                       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], &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"   : "");
                        if (!override)
                                send_manage(win, 1);
                        if (mapped)
                                send_state(win, ST_SHOW);
                }
+               free(reply);
                xcb_flush(conn);
        }
 
        /* Main loop */
-       while (1)
+       running = 1;
+       while (running)
        {
                int status;
                xcb_generic_event_t *event;
@@ -1096,17 +1191,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_ewmh_connection_wipe(&ewmh);
-               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);
 }