+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;
+}
+
+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 int do_get_transient(xcb_window_t xcb, xcb_window_t *parent)
+{
+ xcb_get_property_cookie_t cookie;
+
+ cookie = xcb_icccm_get_wm_transient_for(conn, xcb);
+ if (!cookie.sequence)
+ return warn("do_get_transient: bad cookie");
+
+ if (!xcb_icccm_get_wm_transient_for_reply(conn, cookie, parent, NULL))
+ return warn("do_get_transient: no sizes");
+
+ printf("do_get_transient: %d -> %d\n", xcb, *parent);
+ return 1;
+}
+
+static xcb_pixmap_t do_alloc_color(uint32_t rgb)
+{
+ uint16_t r = (rgb & 0xFF0000) >> 8;
+ uint16_t g = (rgb & 0x00FF00);
+ uint16_t b = (rgb & 0x0000FF) << 8;
+ xcb_alloc_color_cookie_t cookie =
+ xcb_alloc_color(conn, colormap, r, g, b);
+ if (!cookie.sequence)
+ return warn("do_alloc_color: bad cookie");
+
+ xcb_alloc_color_reply_t *reply =
+ xcb_alloc_color_reply(conn, cookie, NULL);
+ if (!reply)
+ return warn("do_alloc_color: no reply");
+
+ printf("do_alloc_color: %06x -> %06x\n", rgb, reply->pixel);
+ xcb_pixmap_t pixel = reply->pixel;
+ free(reply);
+ return pixel;
+}
+
+static void do_grab_pointer(xcb_event_mask_t mask)
+{
+ if (!grabbed)
+ xcb_grab_pointer(conn, 0, root, mask,
+ XCB_GRAB_MODE_ASYNC,
+ XCB_GRAB_MODE_ASYNC,
+ 0, 0, XCB_CURRENT_TIME);
+ grabbed++;
+}
+
+static void do_ungrab_pointer(void)
+{
+ grabbed--;
+ if (!grabbed)
+ 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 *
+ **************************/
+
+/* Send event info */
+static int send_event(event_t ev, xcb_window_t ewin)
+{
+ win_t *win = win_get(ewin);
+ do_grab_pointer(0);
+ int status = wm_handle_event(win, ev, MOD(), PTR());
+ do_ungrab_pointer();
+ return status;
+}
+
+/* Send event info */
+static int send_event_info(event_t ev, xcb_mod_mask_t mask, int up, int16_t *pos,
+ xcb_window_t rwin, xcb_window_t ewin, xcb_window_t cwin)
+{
+ xcb_window_t xcb = ewin == rwin ? cwin : ewin;
+ win_t *win = win_get(xcb);
+ mod_t mod = mask_to_mod(mask, up);
+ ptr_t ptr = list_to_ptr(pos);
+ do_grab_pointer(0);
+ int status = wm_handle_event(win, ev, mod, ptr);
+ do_ungrab_pointer();
+ return status;
+}
+
+/* Send pointer motion info */
+static int send_pointer(int16_t *pos,
+ xcb_window_t rwin, xcb_window_t ewin, xcb_window_t cwin)
+{
+ xcb_window_t xcb = ewin == rwin ? cwin : ewin;
+ win_t *win = win_get(xcb);
+ ptr_t ptr = list_to_ptr(pos);
+ do_grab_pointer(0);
+ int status = wm_handle_ptr(win, ptr);
+ do_ungrab_pointer();
+ return status;
+}
+
+/* Send window state info */
+static void send_manage(win_t *win, int managed)
+{
+ 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);
+}
+