#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"
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
+ int managed; // window is managed by wm
};
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 int running;
+static xcb_window_t control;
+static xcb_atom_t wm_protos;
+static xcb_atom_t wm_delete;
/************************
* Conversion functions *
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;
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)
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 *
**************************/
}
/* 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->state == next)
+ return;
+ state_t prev = win->state;
+ win->state = next;
+ wm_handle_state(win, prev, next);
+}
/**********************
* X11 Event Handlers *
win->sys = sys;
sys->xcb = event->window;
- sys->override = event->override_redirect;
+
+ if (!event->override_redirect)
+ send_manage(win, 1);
tsearch(win, &cache, win_cmp);
}
event->window, win);
if (!win) return;
+ send_manage(win, 0);
+
tdelete(win, &cache, win_cmp);
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;
+
+ send_state(win, ST_HIDE);
+}
+
+static void on_map_notify(xcb_map_notify_event_t *event)
+{
+ win_t *win = win_get(event->window);
+ printf("on_map_notify: xcb=%-8u -> win=%p\n",
+ event->window, win);
+ if (!win) return;
+
+ send_state(win, ST_SHOW);
+}
+
static void on_map_request(xcb_map_request_event_t *event)
{
win_t *win = win_get(event->window);
event->window, win);
if (!win) return;
- if (!win->sys->override && !win->sys->mapped)
- wm_insert(win);
- win->sys->mapped = 1;
+ 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);
}
(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)
{
case XCB_DESTROY_NOTIFY:
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:
on_map_request((xcb_map_request_event_t *)event);
break;
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:
break;
case ST_CLOSE:
- xcb_kill_client(conn, xcb);
+ if (!do_client_message(xcb, wm_delete))
+ xcb_kill_client(conn, xcb);
break;
}
{
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);
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");
+ if (!wm_protos || !wm_delete)
+ error("unable to setup atoms");
+
+ /* Setup EWMH connection */
+ if (!do_ewmh_init_atoms())
+ error("ewmh setup failed");
+
+ /* Set EWMH wm window */
+ control = xcb_generate_id(conn);
+ cookie = xcb_create_window_checked(conn, 0, control, root,
+ 0, 0, 1, 1, 0, 0, 0, 0, NULL);
+ 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);
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");
}
void sys_run(void)
xcb_window_t *kids = NULL;
int nkids = do_query_tree(root, &kids);
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];
- 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_geometry(kids[i], &win->x, &win->y, &win->w, &win->h);
+ do_get_window_attributes(kids[i], &override, &mapped);
+ if (!override)
+ send_manage(win, 1);
+ if (mapped)
+ send_state(win, ST_SHOW);
}
xcb_flush(conn);
}
/* Main loop */
- while (1)
+ running = 1;
+ while (running)
{
int status;
xcb_generic_event_t *event;
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;
+
+ 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");
+
+ xcb_ewmh_connection_wipe(&ewmh);
+ xcb_disconnect(conn);
}