+/*
+ * Supported EWMH hints should be added to
+ * both the enum and the ewmh array
+ */
+enum { _NET_ACTIVE_WINDOW, _NET_MOVERESIZE_WINDOW, _NET_CLOSE_WINDOW,
+ _NET_WM_WINDOW_TYPE, _NET_WM_WINDOW_TYPE_DOCK,
+ _NET_WM_WINDOW_TYPE_TOOLBAR, _NET_WM_WINDOW_TYPE_UTILITY,
+ _NET_WM_WINDOW_TYPE_SPLASH, _NET_WM_WINDOW_TYPE_DIALOG,
+ _NET_WM_WINDOW_TYPE_NORMAL, _NET_WM_STATE,
+ _NET_WM_STATE_MAXIMIZED_HORZ, _NET_WM_STATE_MAXIMIZED_VERT,
+ _NET_WM_STATE_SKIP_TASKBAR, _NET_WM_STATE_SKIP_PAGER,
+ _NET_WM_STATE_HIDDEN, _NET_WM_STATE_ABOVE, _SWM_WM_STATE_MANUAL,
+ _NET_WM_STATE_FULLSCREEN, _NET_WM_ALLOWED_ACTIONS, _NET_WM_ACTION_MOVE,
+ _NET_WM_ACTION_RESIZE, _NET_WM_ACTION_FULLSCREEN, _NET_WM_ACTION_CLOSE,
+ SWM_EWMH_HINT_MAX };
+
+struct ewmh_hint {
+ char *name;
+ Atom atom;
+} ewmh[SWM_EWMH_HINT_MAX] = {
+ /* must be in same order as in the enum */
+ {"_NET_ACTIVE_WINDOW", None},
+ {"_NET_MOVERESIZE_WINDOW", None},
+ {"_NET_CLOSE_WINDOW", None},
+ {"_NET_WM_WINDOW_TYPE", None},
+ {"_NET_WM_WINDOW_TYPE_DOCK", None},
+ {"_NET_WM_WINDOW_TYPE_TOOLBAR", None},
+ {"_NET_WM_WINDOW_TYPE_UTILITY", None},
+ {"_NET_WM_WINDOW_TYPE_SPLASH", None},
+ {"_NET_WM_WINDOW_TYPE_DIALOG", None},
+ {"_NET_WM_WINDOW_TYPE_NORMAL", None},
+ {"_NET_WM_STATE", None},
+ {"_NET_WM_STATE_MAXIMIZED_HORZ", None},
+ {"_NET_WM_STATE_MAXIMIZED_VERT", None},
+ {"_NET_WM_STATE_SKIP_TASKBAR", None},
+ {"_NET_WM_STATE_SKIP_PAGER", None},
+ {"_NET_WM_STATE_HIDDEN", None},
+ {"_NET_WM_STATE_ABOVE", None},
+ {"_SWM_WM_STATE_MANUAL", None},
+ {"_NET_WM_STATE_FULLSCREEN", None},
+ {"_NET_WM_ALLOWED_ACTIONS", None},
+ {"_NET_WM_ACTION_MOVE", None},
+ {"_NET_WM_ACTION_RESIZE", None},
+ {"_NET_WM_ACTION_FULLSCREEN", None},
+ {"_NET_WM_ACTION_CLOSE", None},
+};
+
+void store_float_geom(struct ws_win *win, struct swm_region *r);
+int floating_toggle_win(struct ws_win *win);
+
+int
+get_property(Window id, Atom atom, long count, Atom type,
+ unsigned long *n, unsigned char **data)
+{
+ int format, status;
+ unsigned long tmp, extra;
+ unsigned long *nitems;
+ Atom real;
+
+ nitems = n != NULL ? n : &tmp;
+ status = XGetWindowProperty(display, id, atom, 0L, count, False, type,
+ &real, &format, nitems, &extra, data);
+
+ if (status != Success)
+ return False;
+ if (real != type)
+ return False;
+
+ return True;
+}
+
+void
+setup_ewmh(void)
+{
+ int i,j;
+ Atom sup_list;
+
+ sup_list = XInternAtom(display, "_NET_SUPPORTED", False);
+
+ for (i = 0; i < LENGTH(ewmh); i++)
+ ewmh[i].atom = XInternAtom(display, ewmh[i].name, False);
+
+ for (i = 0; i < ScreenCount(display); i++) {
+ /* Support check window will be created by workaround(). */
+
+ /* Report supported atoms */
+ XDeleteProperty(display, screens[i].root, sup_list);
+ for (j = 0; j < LENGTH(ewmh); j++)
+ XChangeProperty(display, screens[i].root, sup_list, XA_ATOM, 32,
+ PropModeAppend, (unsigned char *)&ewmh[j].atom,1);
+ }
+}
+
+void
+teardown_ewmh(void)
+{
+ int i, success;
+ unsigned char *data = NULL;
+ unsigned long n;
+ Atom sup_check, sup_list;
+ Window id;
+
+ sup_check = XInternAtom(display, "_NET_SUPPORTING_WM_CHECK", False);
+ sup_list = XInternAtom(display, "_NET_SUPPORTED", False);
+
+ for (i = 0; i < ScreenCount(display); i++) {
+ /* Get the support check window and destroy it */
+ success = get_property(screens[i].root, sup_check, 1, XA_WINDOW,
+ &n, &data);
+
+ if (success) {
+ id = data[0];
+ XDestroyWindow(display, id);
+ XDeleteProperty(display, screens[i].root, sup_check);
+ XDeleteProperty(display, screens[i].root, sup_list);
+ }
+
+ XFree(data);
+ }
+}
+
+void
+ewmh_autoquirk(struct ws_win *win)
+{
+ int success, i;
+ unsigned long *data = NULL;
+ unsigned long n;
+ Atom type;
+
+ success = get_property(win->id, ewmh[_NET_WM_WINDOW_TYPE].atom, (~0L),
+ XA_ATOM, &n, (unsigned char **)&data);
+
+ if (!success) {
+ XFree(data);
+ return;
+ }
+
+ for (i = 0; i < n; i++) {
+ type = data[i];
+ if (type == ewmh[_NET_WM_WINDOW_TYPE_NORMAL].atom)
+ break;
+ if (type == ewmh[_NET_WM_WINDOW_TYPE_DOCK].atom ||
+ type == ewmh[_NET_WM_WINDOW_TYPE_TOOLBAR].atom ||
+ type == ewmh[_NET_WM_WINDOW_TYPE_UTILITY].atom) {
+ win->floating = 1;
+ win->quirks = SWM_Q_FLOAT | SWM_Q_ANYWHERE;
+ break;
+ }
+ if (type == ewmh[_NET_WM_WINDOW_TYPE_SPLASH].atom ||
+ type == ewmh[_NET_WM_WINDOW_TYPE_DIALOG].atom) {
+ win->floating = 1;
+ win->quirks = SWM_Q_FLOAT;
+ break;
+ }
+ }
+
+ XFree(data);
+}
+
+#define SWM_EWMH_ACTION_COUNT_MAX (6)
+#define EWMH_F_FULLSCREEN (1<<0)
+#define EWMH_F_ABOVE (1<<1)
+#define EWMH_F_HIDDEN (1<<2)
+#define EWMH_F_SKIP_PAGER (1<<3)
+#define EWMH_F_SKIP_TASKBAR (1<<4)
+#define SWM_F_MANUAL (1<<5)
+
+int
+ewmh_set_win_fullscreen(struct ws_win *win, int fs)
+{
+ struct swm_geometry rg;
+
+ if (!win->ws->r)
+ return 0;
+
+ if (!win->floating)
+ return 0;
+
+ DNPRINTF(SWM_D_MISC, "ewmh_set_win_fullscreen: win 0x%lx fs: %d\n",
+ win->id, fs);
+
+ rg = win->ws->r->g;
+
+ if (fs) {
+ store_float_geom(win, win->ws->r);
+
+ win->g.x = rg.x;
+ win->g.y = rg.y;
+ win->g.w = rg.w;
+ win->g.h = rg.h;
+ } else {
+ if (win->g_floatvalid) {
+ /* refloat at last floating relative position */
+ win->g.x = win->g_float.x - win->rg_float.x + rg.x;
+ win->g.y = win->g_float.y - win->rg_float.y + rg.y;
+ win->g.w = win->g_float.w;
+ win->g.h = win->g_float.h;
+ }
+ }
+
+ return 1;
+}
+
+void
+ewmh_update_actions(struct ws_win *win)
+{
+ Atom actions[SWM_EWMH_ACTION_COUNT_MAX];
+ int n = 0;
+
+ if (win == NULL)
+ return;
+
+ actions[n++] = ewmh[_NET_WM_ACTION_CLOSE].atom;
+
+ if (win->floating) {
+ actions[n++] = ewmh[_NET_WM_ACTION_MOVE].atom;
+ actions[n++] = ewmh[_NET_WM_ACTION_RESIZE].atom;
+ }
+
+ XChangeProperty(display, win->id, ewmh[_NET_WM_ALLOWED_ACTIONS].atom,
+ XA_ATOM, 32, PropModeReplace, (unsigned char *)actions, n);
+}
+
+#define _NET_WM_STATE_REMOVE 0 /* remove/unset property */
+#define _NET_WM_STATE_ADD 1 /* add/set property */
+#define _NET_WM_STATE_TOGGLE 2 /* toggle property */
+
+void
+ewmh_update_win_state(struct ws_win *win, long state, long action)
+{
+ unsigned int mask = 0;
+ unsigned int changed = 0;
+ unsigned int orig_flags;
+
+ if (win == NULL)
+ return;
+
+ if (state == ewmh[_NET_WM_STATE_FULLSCREEN].atom)
+ mask = EWMH_F_FULLSCREEN;
+ if (state == ewmh[_NET_WM_STATE_ABOVE].atom)
+ mask = EWMH_F_ABOVE;
+ if (state == ewmh[_SWM_WM_STATE_MANUAL].atom)
+ mask = SWM_F_MANUAL;
+ if (state == ewmh[_NET_WM_STATE_SKIP_PAGER].atom)
+ mask = EWMH_F_SKIP_PAGER;
+ if (state == ewmh[_NET_WM_STATE_SKIP_TASKBAR].atom)
+ mask = EWMH_F_SKIP_TASKBAR;
+
+
+ orig_flags = win->ewmh_flags;
+
+ switch (action) {
+ case _NET_WM_STATE_REMOVE:
+ win->ewmh_flags &= ~mask;
+ break;
+ case _NET_WM_STATE_ADD:
+ win->ewmh_flags |= mask;
+ break;
+ case _NET_WM_STATE_TOGGLE:
+ win->ewmh_flags ^= mask;
+ break;
+ }
+
+ changed = (win->ewmh_flags & mask) ^ (orig_flags & mask) ? 1 : 0;
+
+ if (state == ewmh[_NET_WM_STATE_ABOVE].atom)
+ if (changed)
+ if (!floating_toggle_win(win))
+ win->ewmh_flags = orig_flags; /* revert */
+ if (state == ewmh[_SWM_WM_STATE_MANUAL].atom)
+ if (changed)
+ win->manual = (win->ewmh_flags & SWM_F_MANUAL) != 0;
+ if (state == ewmh[_NET_WM_STATE_FULLSCREEN].atom)
+ if (changed)
+ if (!ewmh_set_win_fullscreen(win, win->ewmh_flags & EWMH_F_FULLSCREEN))
+ win->ewmh_flags = orig_flags; /* revert */
+
+ XDeleteProperty(display, win->id, ewmh[_NET_WM_STATE].atom);
+
+ if (win->ewmh_flags & EWMH_F_FULLSCREEN)
+ XChangeProperty(display, win->id, ewmh[_NET_WM_STATE].atom,
+ XA_ATOM, 32, PropModeAppend,
+ (unsigned char *)&ewmh[_NET_WM_STATE_FULLSCREEN].atom, 1);
+ if (win->ewmh_flags & EWMH_F_SKIP_PAGER)
+ XChangeProperty(display, win->id, ewmh[_NET_WM_STATE].atom,
+ XA_ATOM, 32, PropModeAppend,
+ (unsigned char *)&ewmh[_NET_WM_STATE_SKIP_PAGER].atom, 1);
+ if (win->ewmh_flags & EWMH_F_SKIP_TASKBAR)
+ XChangeProperty(display, win->id, ewmh[_NET_WM_STATE].atom,
+ XA_ATOM, 32, PropModeAppend,
+ (unsigned char *)&ewmh[_NET_WM_STATE_SKIP_TASKBAR].atom, 1);
+ if (win->ewmh_flags & EWMH_F_ABOVE)
+ XChangeProperty(display, win->id, ewmh[_NET_WM_STATE].atom,
+ XA_ATOM, 32, PropModeAppend,
+ (unsigned char *)&ewmh[_NET_WM_STATE_ABOVE].atom, 1);
+ if (win->ewmh_flags & SWM_F_MANUAL)
+ XChangeProperty(display, win->id, ewmh[_NET_WM_STATE].atom,
+ XA_ATOM, 32, PropModeAppend,
+ (unsigned char *)&ewmh[_SWM_WM_STATE_MANUAL].atom, 1);
+}
+
+void
+ewmh_get_win_state(struct ws_win *win)
+{
+ int success, i;
+ unsigned long n;
+ Atom *states;
+
+ if (win == NULL)
+ return;
+
+ win->ewmh_flags = 0;
+ if (win->floating)
+ win->ewmh_flags |= EWMH_F_ABOVE;
+ if (win->manual)
+ win->ewmh_flags |= SWM_F_MANUAL;
+
+ success = get_property(win->id, ewmh[_NET_WM_STATE].atom, (~0L), XA_ATOM,
+ &n, (unsigned char **)&states);
+
+ if (!success)
+ return;
+
+ for (i = 0; i < n; i++)
+ ewmh_update_win_state(win, states[i], _NET_WM_STATE_ADD);
+
+ XFree(states);
+}
+