1 From f0cdf40e0a7126838d051eb84d84b91421b771d6 Mon Sep 17 00:00:00 2001
2 From: 0x1bi <ben@0x1bi.net>
3 Date: Fri, 11 Dec 2020 10:16:25 -0500
4 Subject: [PATCH] fix swallow for openbsd
9 dwm.c | 235 +++++++++++++++++++++++++++++++++++++++++++++++++--
10 3 files changed, 237 insertions(+), 10 deletions(-)
12 diff --git a/config.def.h b/config.def.h
13 index 1c0b587..fe51476 100644
18 static const unsigned int borderpx = 1; /* border pixel of windows */
19 static const unsigned int snap = 32; /* snap pixel */
20 +static const int swallowfloating = 0; /* 1 means swallow floating windows by default */
21 static const int showbar = 1; /* 0 means no bar */
22 static const int topbar = 1; /* 0 means bottom bar */
23 static const char *fonts[] = { "monospace:size=10" };
24 @@ -26,9 +27,11 @@ static const Rule rules[] = {
25 * WM_CLASS(STRING) = instance, class
26 * WM_NAME(STRING) = title
28 - /* class instance title tags mask isfloating monitor */
29 - { "Gimp", NULL, NULL, 0, 1, -1 },
30 - { "Firefox", NULL, NULL, 1 << 8, 0, -1 },
31 + /* class instance title tags mask isfloating isterminal noswallow monitor */
32 + { "Gimp", NULL, NULL, 0, 1, 0, 0, -1 },
33 + { "Firefox", NULL, NULL, 1 << 8, 0, 0, -1, -1 },
34 + { "St", NULL, NULL, 0, 0, 1, 0, -1 },
35 + { NULL, NULL, "Event Tester", 0, 0, 0, 1, -1 }, /* xev */
39 diff --git a/config.mk b/config.mk
40 index 7084c33..ff9e508 100644
43 @@ -19,10 +19,11 @@ FREETYPELIBS = -lfontconfig -lXft
44 FREETYPEINC = /usr/include/freetype2
46 #FREETYPEINC = ${X11INC}/freetype2
50 INCS = -I${X11INC} -I${FREETYPEINC}
51 -LIBS = -L${X11LIB} -lX11 ${XINERAMALIBS} ${FREETYPELIBS}
52 +LIBS = -L${X11LIB} -lX11 ${XINERAMALIBS} ${FREETYPELIBS} -lX11-xcb -lxcb -lxcb-res ${KVMLIB}
55 CPPFLAGS = -D_DEFAULT_SOURCE -D_BSD_SOURCE -D_POSIX_C_SOURCE=200809L -DVERSION=\"${VERSION}\" ${XINERAMAFLAGS}
56 diff --git a/dwm.c b/dwm.c
57 index 664c527..0b20086 100644
61 #include <X11/extensions/Xinerama.h>
63 #include <X11/Xft/Xft.h>
64 +#include <X11/Xlib-xcb.h>
67 +#include <sys/sysctl.h>
69 +#endif /* __OpenBSD */
73 @@ -92,9 +98,11 @@ struct Client {
74 int basew, baseh, incw, inch, maxw, maxh, minw, minh;
77 - int isfixed, isfloating, isurgent, neverfocus, oldstate, isfullscreen;
78 + int isfixed, isfloating, isurgent, neverfocus, oldstate, isfullscreen, isterminal, noswallow;
86 @@ -138,6 +146,8 @@ typedef struct {
95 @@ -235,6 +245,12 @@ static int xerrordummy(Display *dpy, XErrorEvent *ee);
96 static int xerrorstart(Display *dpy, XErrorEvent *ee);
97 static void zoom(const Arg *arg);
99 +static pid_t getparentprocess(pid_t p);
100 +static int isdescprocess(pid_t p, pid_t c);
101 +static Client *swallowingclient(Window w);
102 +static Client *termforwin(const Client *c);
103 +static pid_t winpid(Window w);
106 static const char broken[] = "broken";
107 static char stext[256];
108 @@ -269,6 +285,8 @@ static Drw *drw;
109 static Monitor *mons, *selmon;
110 static Window root, wmcheckwin;
112 +static xcb_connection_t *xcon;
114 /* configuration, allows nested code to access above variables */
117 @@ -298,6 +316,8 @@ applyrules(Client *c)
118 && (!r->class || strstr(class, r->class))
119 && (!r->instance || strstr(instance, r->instance)))
121 + c->isterminal = r->isterminal;
122 + c->noswallow = r->noswallow;
123 c->isfloating = r->isfloating;
125 for (m = mons; m && m->num != r->monitor; m = m->next);
126 @@ -414,6 +434,53 @@ attachstack(Client *c)
131 +swallow(Client *p, Client *c)
134 + if (c->noswallow || c->isterminal)
136 + if (c->noswallow && !swallowfloating && c->isfloating)
142 + setclientstate(c, WithdrawnState);
143 + XUnmapWindow(dpy, p->win);
152 + XMoveResizeWindow(dpy, p->win, p->x, p->y, p->w, p->h);
155 + updateclientlist();
159 +unswallow(Client *c)
161 + c->win = c->swallowing->win;
163 + free(c->swallowing);
164 + c->swallowing = NULL;
166 + /* unfullscreen the client */
167 + setfullscreen(c, 0);
170 + XMapWindow(dpy, c->win);
171 + XMoveResizeWindow(dpy, c->win, c->x, c->y, c->w, c->h);
172 + setclientstate(c, NormalState);
178 buttonpress(XEvent *e)
180 @@ -653,6 +720,9 @@ destroynotify(XEvent *e)
182 if ((c = wintoclient(ev->window)))
185 + else if ((c = swallowingclient(ev->window)))
186 + unmanage(c->swallowing, 1);
190 @@ -1018,12 +1088,13 @@ killclient(const Arg *arg)
192 manage(Window w, XWindowAttributes *wa)
194 - Client *c, *t = NULL;
195 + Client *c, *t = NULL, *term = NULL;
199 c = ecalloc(1, sizeof(Client));
201 + c->pid = winpid(w);
203 c->x = c->oldx = wa->x;
204 c->y = c->oldy = wa->y;
205 @@ -1038,6 +1109,7 @@ manage(Window w, XWindowAttributes *wa)
209 + term = termforwin(c);
212 if (c->x + WIDTH(c) > c->mon->mx + c->mon->mw)
213 @@ -1074,6 +1146,8 @@ manage(Window w, XWindowAttributes *wa)
216 XMapWindow(dpy, c->win);
222 @@ -1768,6 +1842,20 @@ unmanage(Client *c, int destroyed)
226 + if (c->swallowing) {
231 + Client *s = swallowingclient(c->win);
233 + free(s->swallowing);
234 + s->swallowing = NULL;
243 @@ -1782,9 +1870,12 @@ unmanage(Client *c, int destroyed)
248 - updateclientlist();
254 + updateclientlist();
259 @@ -2047,6 +2138,136 @@ view(const Arg *arg)
270 + xcb_res_client_id_spec_t spec = {0};
272 + spec.mask = XCB_RES_CLIENT_ID_MASK_LOCAL_CLIENT_PID;
274 + xcb_generic_error_t *e = NULL;
275 + xcb_res_query_client_ids_cookie_t c = xcb_res_query_client_ids(xcon, 1, &spec);
276 + xcb_res_query_client_ids_reply_t *r = xcb_res_query_client_ids_reply(xcon, c, &e);
281 + xcb_res_client_id_value_iterator_t i = xcb_res_query_client_ids_ids_iterator(r);
282 + for (; i.rem; xcb_res_client_id_value_next(&i)) {
283 + spec = i.data->spec;
284 + if (spec.mask & XCB_RES_CLIENT_ID_MASK_LOCAL_CLIENT_PID) {
285 + uint32_t *t = xcb_res_client_id_value_value(i.data);
293 + if (result == (pid_t)-1)
296 +#endif /* __linux__ */
301 + unsigned long len, bytes;
302 + unsigned char *prop;
305 + if (XGetWindowProperty(dpy, w, XInternAtom(dpy, "_NET_WM_PID", 0), 0, 1, False, AnyPropertyType, &type, &format, &len, &bytes, &prop) != Success || !prop)
308 + ret = *(pid_t*)prop;
312 +#endif /* __OpenBSD__ */
317 +getparentprocess(pid_t p)
319 + unsigned int v = 0;
324 + snprintf(buf, sizeof(buf) - 1, "/proc/%u/stat", (unsigned)p);
326 + if (!(f = fopen(buf, "r")))
329 + fscanf(f, "%*u %*s %*c %u", &v);
331 +#endif /* __linux__*/
336 + struct kinfo_proc *kp;
338 + kd = kvm_openfiles(NULL, NULL, NULL, KVM_NO_FILES, NULL);
342 + kp = kvm_getprocs(kd, KERN_PROC_PID, p, sizeof(*kp), &n);
344 +#endif /* __OpenBSD__ */
350 +isdescprocess(pid_t p, pid_t c)
352 + while (p != c && c != 0)
353 + c = getparentprocess(c);
359 +termforwin(const Client *w)
364 + if (!w->pid || w->isterminal)
367 + for (m = mons; m; m = m->next) {
368 + for (c = m->clients; c; c = c->next) {
369 + if (c->isterminal && !c->swallowing && c->pid && isdescprocess(c->pid, w->pid))
378 +swallowingclient(Window w)
383 + for (m = mons; m; m = m->next) {
384 + for (c = m->clients; c; c = c->next) {
385 + if (c->swallowing && c->swallowing->win == w)
394 wintoclient(Window w)
396 @@ -2138,10 +2359,12 @@ main(int argc, char *argv[])
397 fputs("warning: no locale support\n", stderr);
398 if (!(dpy = XOpenDisplay(NULL)))
399 die("dwm: cannot open display");
400 + if (!(xcon = XGetXCBConnection(dpy)))
401 + die("dwm: cannot get xcb connection\n");
405 - if (pledge("stdio rpath proc exec", NULL) == -1)
406 + if (pledge("stdio rpath proc exec ps", NULL) == -1)
408 #endif /* __OpenBSD__ */