2019-10-22 17:08:00 +00:00
|
|
|
#include <X11/Xlib-xcb.h>
|
|
|
|
#include <xcb/res.h>
|
2020-09-11 15:21:38 +00:00
|
|
|
#ifdef __OpenBSD__
|
|
|
|
#include <sys/sysctl.h>
|
|
|
|
#include <kvm.h>
|
|
|
|
#endif /* __OpenBSD__ */
|
2019-10-22 17:08:00 +00:00
|
|
|
|
2020-09-11 09:47:57 +00:00
|
|
|
static int scanner;
|
2019-10-22 17:08:00 +00:00
|
|
|
static xcb_connection_t *xcon;
|
|
|
|
|
2020-09-12 12:21:51 +00:00
|
|
|
int
|
2019-10-22 17:08:00 +00:00
|
|
|
swallow(Client *p, Client *c)
|
|
|
|
{
|
2020-09-11 09:47:57 +00:00
|
|
|
Client *s;
|
2021-08-07 10:31:43 +00:00
|
|
|
XWindowChanges wc;
|
2020-09-11 09:47:57 +00:00
|
|
|
|
|
|
|
if (c->noswallow > 0 || c->isterminal)
|
2020-09-12 12:21:51 +00:00
|
|
|
return 0;
|
2020-09-11 09:47:57 +00:00
|
|
|
if (c->noswallow < 0 && !swallowfloating && c->isfloating)
|
2020-09-12 12:21:51 +00:00
|
|
|
return 0;
|
2019-10-22 17:08:00 +00:00
|
|
|
|
2020-10-10 13:25:35 +00:00
|
|
|
XMapWindow(dpy, c->win);
|
|
|
|
|
2019-10-22 17:08:00 +00:00
|
|
|
detach(c);
|
|
|
|
detachstack(c);
|
|
|
|
|
|
|
|
setclientstate(c, WithdrawnState);
|
|
|
|
XUnmapWindow(dpy, p->win);
|
|
|
|
|
|
|
|
p->swallowing = c;
|
|
|
|
c->mon = p->mon;
|
|
|
|
|
|
|
|
Window w = p->win;
|
|
|
|
p->win = c->win;
|
|
|
|
c->win = w;
|
2020-09-11 09:47:57 +00:00
|
|
|
|
|
|
|
XChangeProperty(dpy, c->win, netatom[NetClientList], XA_WINDOW, 32, PropModeReplace,
|
|
|
|
(unsigned char *) &(p->win), 1);
|
|
|
|
|
2023-02-03 20:04:00 +00:00
|
|
|
#if BAR_WINICON_PATCH
|
|
|
|
updateicon(p);
|
|
|
|
#endif
|
2019-10-22 17:08:00 +00:00
|
|
|
updatetitle(p);
|
2020-09-11 09:47:57 +00:00
|
|
|
s = scanner ? c : p;
|
2020-10-11 09:53:43 +00:00
|
|
|
#if BAR_EWMHTAGS_PATCH
|
|
|
|
setfloatinghint(s);
|
|
|
|
#endif // BAR_EWMHTAGS_PATCH
|
2021-08-07 10:31:43 +00:00
|
|
|
|
|
|
|
wc.border_width = p->bw;
|
|
|
|
XConfigureWindow(dpy, p->win, CWBorderWidth, &wc);
|
2020-09-11 09:47:57 +00:00
|
|
|
XMoveResizeWindow(dpy, p->win, s->x, s->y, s->w, s->h);
|
2021-08-07 10:31:43 +00:00
|
|
|
#if !BAR_FLEXWINTITLE_PATCH
|
|
|
|
XSetWindowBorder(dpy, p->win, scheme[SchemeNorm][ColBorder].pixel);
|
|
|
|
#endif // BAR_FLEXWINTITLE_PATCH
|
|
|
|
|
2019-10-22 17:08:00 +00:00
|
|
|
arrange(p->mon);
|
|
|
|
configure(p);
|
|
|
|
updateclientlist();
|
2020-09-12 12:21:51 +00:00
|
|
|
|
|
|
|
return 1;
|
2019-10-22 17:08:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
unswallow(Client *c)
|
|
|
|
{
|
2021-08-07 10:31:43 +00:00
|
|
|
XWindowChanges wc;
|
2019-10-22 17:08:00 +00:00
|
|
|
c->win = c->swallowing->win;
|
|
|
|
|
|
|
|
free(c->swallowing);
|
|
|
|
c->swallowing = NULL;
|
|
|
|
|
2020-09-11 09:47:57 +00:00
|
|
|
XDeleteProperty(dpy, c->win, netatom[NetClientList]);
|
|
|
|
|
|
|
|
/* unfullscreen the client */
|
|
|
|
setfullscreen(c, 0);
|
2023-02-03 20:04:00 +00:00
|
|
|
#if BAR_WINICON_PATCH
|
|
|
|
updateicon(c);
|
|
|
|
#endif
|
2019-10-22 17:08:00 +00:00
|
|
|
updatetitle(c);
|
|
|
|
arrange(c->mon);
|
|
|
|
XMapWindow(dpy, c->win);
|
2021-08-07 10:31:43 +00:00
|
|
|
|
|
|
|
wc.border_width = c->bw;
|
|
|
|
XConfigureWindow(dpy, c->win, CWBorderWidth, &wc);
|
2019-10-22 17:08:00 +00:00
|
|
|
XMoveResizeWindow(dpy, c->win, c->x, c->y, c->w, c->h);
|
2021-08-07 10:31:43 +00:00
|
|
|
#if !BAR_FLEXWINTITLE_PATCH
|
|
|
|
XSetWindowBorder(dpy, c->win, scheme[SchemeNorm][ColBorder].pixel);
|
|
|
|
#endif // BAR_FLEXWINTITLE_PATCH
|
|
|
|
|
2020-10-11 09:53:43 +00:00
|
|
|
#if BAR_EWMHTAGS_PATCH
|
|
|
|
setfloatinghint(c);
|
|
|
|
#endif // BAR_EWMHTAGS_PATCH
|
2019-10-22 17:08:00 +00:00
|
|
|
setclientstate(c, NormalState);
|
2020-09-11 09:47:57 +00:00
|
|
|
focus(NULL);
|
|
|
|
arrange(c->mon);
|
2019-10-22 17:08:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pid_t
|
|
|
|
winpid(Window w)
|
|
|
|
{
|
|
|
|
pid_t result = 0;
|
|
|
|
|
2020-09-11 15:21:38 +00:00
|
|
|
#ifdef __linux__
|
2019-10-22 17:08:00 +00:00
|
|
|
xcb_res_client_id_spec_t spec = {0};
|
|
|
|
spec.client = w;
|
|
|
|
spec.mask = XCB_RES_CLIENT_ID_MASK_LOCAL_CLIENT_PID;
|
|
|
|
|
|
|
|
xcb_generic_error_t *e = NULL;
|
|
|
|
xcb_res_query_client_ids_cookie_t c = xcb_res_query_client_ids(xcon, 1, &spec);
|
|
|
|
xcb_res_query_client_ids_reply_t *r = xcb_res_query_client_ids_reply(xcon, c, &e);
|
|
|
|
|
|
|
|
if (!r)
|
|
|
|
return (pid_t)0;
|
|
|
|
|
|
|
|
xcb_res_client_id_value_iterator_t i = xcb_res_query_client_ids_ids_iterator(r);
|
|
|
|
for (; i.rem; xcb_res_client_id_value_next(&i)) {
|
|
|
|
spec = i.data->spec;
|
|
|
|
if (spec.mask & XCB_RES_CLIENT_ID_MASK_LOCAL_CLIENT_PID) {
|
|
|
|
uint32_t *t = xcb_res_client_id_value_value(i.data);
|
|
|
|
result = *t;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
free(r);
|
|
|
|
|
|
|
|
if (result == (pid_t)-1)
|
|
|
|
result = 0;
|
2020-09-11 15:21:38 +00:00
|
|
|
|
|
|
|
#endif /* __linux__ */
|
|
|
|
#ifdef __OpenBSD__
|
|
|
|
Atom type;
|
|
|
|
int format;
|
|
|
|
unsigned long len, bytes;
|
|
|
|
unsigned char *prop;
|
|
|
|
pid_t ret;
|
|
|
|
|
|
|
|
if (XGetWindowProperty(dpy, w, XInternAtom(dpy, "_NET_WM_PID", 1), 0, 1, False, AnyPropertyType, &type, &format, &len, &bytes, &prop) != Success || !prop)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
ret = *(pid_t*)prop;
|
|
|
|
XFree(prop);
|
|
|
|
result = ret;
|
|
|
|
#endif /* __OpenBSD__ */
|
|
|
|
|
2019-10-22 17:08:00 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
pid_t
|
|
|
|
getparentprocess(pid_t p)
|
|
|
|
{
|
|
|
|
unsigned int v = 0;
|
|
|
|
|
2020-09-11 15:21:38 +00:00
|
|
|
#ifdef __linux__
|
2019-10-22 17:08:00 +00:00
|
|
|
FILE *f;
|
|
|
|
char buf[256];
|
|
|
|
snprintf(buf, sizeof(buf) - 1, "/proc/%u/stat", (unsigned)p);
|
|
|
|
|
|
|
|
if (!(f = fopen(buf, "r")))
|
2020-09-11 09:47:57 +00:00
|
|
|
return (pid_t)0;
|
2019-10-22 17:08:00 +00:00
|
|
|
|
2020-09-11 09:47:57 +00:00
|
|
|
if (fscanf(f, "%*u %*s %*c %u", (unsigned *)&v) != 1)
|
|
|
|
v = (pid_t)0;
|
2019-10-22 17:08:00 +00:00
|
|
|
fclose(f);
|
2020-09-11 15:21:38 +00:00
|
|
|
#endif /* __linux__ */
|
|
|
|
#ifdef __OpenBSD__
|
|
|
|
int n;
|
|
|
|
kvm_t *kd;
|
|
|
|
struct kinfo_proc *kp;
|
|
|
|
|
|
|
|
kd = kvm_openfiles(NULL, NULL, NULL, KVM_NO_FILES, NULL);
|
|
|
|
if (!kd)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
kp = kvm_getprocs(kd, KERN_PROC_PID, p, sizeof(*kp), &n);
|
|
|
|
v = kp->p_ppid;
|
|
|
|
#endif /* __OpenBSD__ */
|
2019-10-22 17:08:00 +00:00
|
|
|
return (pid_t)v;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
isdescprocess(pid_t p, pid_t c)
|
|
|
|
{
|
|
|
|
while (p != c && c != 0)
|
|
|
|
c = getparentprocess(c);
|
|
|
|
|
|
|
|
return (int)c;
|
|
|
|
}
|
|
|
|
|
|
|
|
Client *
|
|
|
|
termforwin(const Client *w)
|
|
|
|
{
|
|
|
|
Client *c;
|
|
|
|
Monitor *m;
|
|
|
|
|
|
|
|
if (!w->pid || w->isterminal)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
c = selmon->sel;
|
|
|
|
if (c && c->isterminal && !c->swallowing && c->pid && isdescprocess(c->pid, w->pid))
|
|
|
|
return c;
|
|
|
|
|
|
|
|
for (m = mons; m; m = m->next) {
|
|
|
|
for (c = m->clients; c; c = c->next) {
|
|
|
|
if (c->isterminal && !c->swallowing && c->pid && isdescprocess(c->pid, w->pid))
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
Client *
|
|
|
|
swallowingclient(Window w)
|
|
|
|
{
|
|
|
|
Client *c;
|
|
|
|
Monitor *m;
|
|
|
|
|
|
|
|
for (m = mons; m; m = m->next) {
|
|
|
|
for (c = m->clients; c; c = c->next) {
|
|
|
|
if (c->swallowing && c->swallowing->win == w)
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
2021-06-14 05:16:17 +00:00
|
|
|
|