add swallow patch + reindent everything

This commit is contained in:
Luca Bilke 2023-02-03 18:10:30 +01:00
parent 2d637e2496
commit a5ef4d240f
No known key found for this signature in database
GPG Key ID: 7B77C51E8C779E75
4 changed files with 2436 additions and 2186 deletions

1
.clang-format Normal file
View File

@ -0,0 +1 @@
IndentWidth: 4

View File

@ -18,10 +18,11 @@ FREETYPELIBS = -lfontconfig -lXft
FREETYPEINC = /usr/include/freetype2 FREETYPEINC = /usr/include/freetype2
# OpenBSD (uncomment) # OpenBSD (uncomment)
#FREETYPEINC = ${X11INC}/freetype2 #FREETYPEINC = ${X11INC}/freetype2
+#KVMLIB = -lkvm
# includes and libs # includes and libs
INCS = -I${X11INC} -I${FREETYPEINC} INCS = -I${X11INC} -I${FREETYPEINC}
LIBS = -L${X11LIB} -lX11 ${XINERAMALIBS} ${FREETYPELIBS} LIBS = -L${X11LIB} -lX11 ${XINERAMALIBS} ${FREETYPELIBS} -lX11-xcb -lxcb -lxcb-res ${KVMLIB}
# flags # flags
CPPFLAGS = -D_DEFAULT_SOURCE -D_BSD_SOURCE -D_POSIX_C_SOURCE=200809L -DVERSION=\"${VERSION}\" ${XINERAMAFLAGS} CPPFLAGS = -D_DEFAULT_SOURCE -D_BSD_SOURCE -D_POSIX_C_SOURCE=200809L -DVERSION=\"${VERSION}\" ${XINERAMAFLAGS}

344
dwm.c
View File

@ -41,6 +41,12 @@
#include <X11/extensions/Xinerama.h> #include <X11/extensions/Xinerama.h>
#endif /* XINERAMA */ #endif /* XINERAMA */
#include <X11/Xft/Xft.h> #include <X11/Xft/Xft.h>
#include <X11/Xlib-xcb.h>
#include <xcb/res.h>
#ifdef __OpenBSD__
#include <kvm.h>
#include <sys/sysctl.h>
#endif /* __OpenBSD */
#include "drw.h" #include "drw.h"
#include "util.h" #include "util.h"
@ -152,9 +158,12 @@ struct Client {
int basew, baseh, incw, inch, maxw, maxh, minw, minh, hintsvalid; int basew, baseh, incw, inch, maxw, maxh, minw, minh, hintsvalid;
int bw, oldbw; int bw, oldbw;
unsigned int tags; unsigned int tags;
int isfixed, isfloating, isurgent, neverfocus, oldstate, isfullscreen; int isfixed, isfloating, isurgent, neverfocus, oldstate, isfullscreen,
isterminal, noswallow;
pid_t pid;
Client *next; Client *next;
Client *snext; Client *snext;
Client *swallowing;
Monitor *mon; Monitor *mon;
Window win; Window win;
}; };
@ -198,6 +207,8 @@ typedef struct {
const char *title; const char *title;
unsigned int tags; unsigned int tags;
int isfloating; int isfloating;
int isterminal;
int noswallow;
int monitor; int monitor;
} Rule; } Rule;
@ -334,6 +345,12 @@ static void load_xresources(void);
static void resource_load(XrmDatabase db, char *name, enum resource_type rtype, static void resource_load(XrmDatabase db, char *name, enum resource_type rtype,
void *dst); void *dst);
static pid_t getparentprocess(pid_t p);
static int isdescprocess(pid_t p, pid_t c);
static Client *swallowingclient(Window w);
static Client *termforwin(const Client *c);
static pid_t winpid(Window w);
/* variables */ /* variables */
static Systray *systray = NULL; static Systray *systray = NULL;
static const char broken[] = "broken"; static const char broken[] = "broken";
@ -371,6 +388,8 @@ static Drw *drw;
static Monitor *mons, *selmon; static Monitor *mons, *selmon;
static Window root, wmcheckwin; static Window root, wmcheckwin;
static xcb_connection_t *xcon;
/* configuration, allows nested code to access above variables */ /* configuration, allows nested code to access above variables */
#include "config.h" #include "config.h"
@ -399,6 +418,8 @@ void applyrules(Client *c) {
if ((!r->title || strstr(c->name, r->title)) && if ((!r->title || strstr(c->name, r->title)) &&
(!r->class || strstr(class, r->class)) && (!r->class || strstr(class, r->class)) &&
(!r->instance || strstr(instance, r->instance))) { (!r->instance || strstr(instance, r->instance))) {
c->isterminal = r->isterminal;
c->noswallow = r->noswallow;
c->isfloating = r->isfloating; c->isfloating = r->isfloating;
c->tags |= r->tags; c->tags |= r->tags;
for (m = mons; m && m->num != r->monitor; m = m->next) for (m = mons; m && m->num != r->monitor; m = m->next)
@ -515,7 +536,8 @@ void attachtop(Client *c) {
below && below->next && below && below->next &&
(below->isfloating || !ISVISIBLEONTAG(below, c->tags) || (below->isfloating || !ISVISIBLEONTAG(below, c->tags) ||
n != m->nmaster); n != m->nmaster);
n = below->isfloating || !ISVISIBLEONTAG(below, c->tags) ? n + 0 : n + 1, n = below->isfloating || !ISVISIBLEONTAG(below, c->tags) ? n + 0
: n + 1,
below = below->next) below = below->next)
; ;
c->next = NULL; c->next = NULL;
@ -531,6 +553,49 @@ void attachstack(Client *c) {
c->mon->stack = c; c->mon->stack = c;
} }
void swallow(Client *p, Client *c) {
if (c->noswallow || c->isterminal)
return;
if (c->noswallow && !swallowfloating && c->isfloating)
return;
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;
updatetitle(p);
XMoveResizeWindow(dpy, p->win, p->x, p->y, p->w, p->h);
arrange(p->mon);
configure(p);
updateclientlist();
}
void unswallow(Client *c) {
c->win = c->swallowing->win;
free(c->swallowing);
c->swallowing = NULL;
/* unfullscreen the client */
setfullscreen(c, 0);
updatetitle(c);
arrange(c->mon);
XMapWindow(dpy, c->win);
XMoveResizeWindow(dpy, c->win, c->x, c->y, c->w, c->h);
setclientstate(c, NormalState);
focus(NULL);
arrange(c->mon);
}
static void bstack(Monitor *m) { static void bstack(Monitor *m) {
int w, h, mh, mx, tx, ty, tw; int w, h, mh, mx, tx, ty, tw;
unsigned int i, n; unsigned int i, n;
@ -639,8 +704,9 @@ void buttonpress(XEvent *e) {
if (click == buttons[i].click && buttons[i].func && if (click == buttons[i].click && buttons[i].func &&
buttons[i].button == ev->button && buttons[i].button == ev->button &&
CLEANMASK(buttons[i].mask) == CLEANMASK(ev->state)) CLEANMASK(buttons[i].mask) == CLEANMASK(ev->state))
buttons[i].func( buttons[i].func(click == ClkTagBar && buttons[i].arg.i == 0
click == ClkTagBar && buttons[i].arg.i == 0 ? &arg : &buttons[i].arg); ? &arg
: &buttons[i].arg);
} }
void centeredmaster(Monitor *m) { void centeredmaster(Monitor *m) {
@ -678,13 +744,15 @@ void centeredmaster(Monitor *m) {
/* nmaster clients are stacked vertically, in the center /* nmaster clients are stacked vertically, in the center
* of the screen */ * of the screen */
h = (m->wh - my) / (MIN(n, m->nmaster) - i); h = (m->wh - my) / (MIN(n, m->nmaster) - i);
resize(c, m->wx + mx, m->wy + my, mw - (2 * c->bw), h - (2 * c->bw), 0); resize(c, m->wx + mx, m->wy + my, mw - (2 * c->bw), h - (2 * c->bw),
0);
my += HEIGHT(c); my += HEIGHT(c);
} else { } else {
/* stack clients are stacked vertically */ /* stack clients are stacked vertically */
if ((i - m->nmaster) % 2) { if ((i - m->nmaster) % 2) {
h = (m->wh - ety) / ((1 + n - i) / 2); h = (m->wh - ety) / ((1 + n - i) / 2);
resize(c, m->wx, m->wy + ety, tw - (2 * c->bw), h - (2 * c->bw), 0); resize(c, m->wx, m->wy + ety, tw - (2 * c->bw), h - (2 * c->bw),
0);
ety += HEIGHT(c); ety += HEIGHT(c);
} else { } else {
h = (m->wh - oty) / ((1 + n - i) / 2); h = (m->wh - oty) / ((1 + n - i) / 2);
@ -730,12 +798,14 @@ void centeredfloatingmaster(Monitor *m) {
/* nmaster clients are stacked horizontally, in the center /* nmaster clients are stacked horizontally, in the center
* of the screen */ * of the screen */
w = (mw + mxo - mx) / (MIN(n, m->nmaster) - i); w = (mw + mxo - mx) / (MIN(n, m->nmaster) - i);
resize(c, m->wx + mx, m->wy + my, w - (2 * c->bw), mh - (2 * c->bw), 0); resize(c, m->wx + mx, m->wy + my, w - (2 * c->bw), mh - (2 * c->bw),
0);
mx += WIDTH(c); mx += WIDTH(c);
} else { } else {
/* stack clients are stacked horizontally */ /* stack clients are stacked horizontally */
w = (m->ww - tx) / (n - i); w = (m->ww - tx) / (n - i);
resize(c, m->wx + tx, m->wy, w - (2 * c->bw), m->wh - (2 * c->bw), 0); resize(c, m->wx + tx, m->wy, w - (2 * c->bw), m->wh - (2 * c->bw),
0);
tx += WIDTH(c); tx += WIDTH(c);
} }
} }
@ -845,12 +915,14 @@ void clientmessage(XEvent *e) {
XEMBED_EMBEDDED_VERSION); XEMBED_EMBEDDED_VERSION);
/* FIXME not sure if I have to send these events, too */ /* FIXME not sure if I have to send these events, too */
sendevent(c->win, netatom[Xembed], StructureNotifyMask, CurrentTime, sendevent(c->win, netatom[Xembed], StructureNotifyMask, CurrentTime,
XEMBED_FOCUS_IN, 0, systray->win, XEMBED_EMBEDDED_VERSION); XEMBED_FOCUS_IN, 0, systray->win,
XEMBED_EMBEDDED_VERSION);
sendevent(c->win, netatom[Xembed], StructureNotifyMask, CurrentTime, sendevent(c->win, netatom[Xembed], StructureNotifyMask, CurrentTime,
XEMBED_WINDOW_ACTIVATE, 0, systray->win, XEMBED_WINDOW_ACTIVATE, 0, systray->win,
XEMBED_EMBEDDED_VERSION); XEMBED_EMBEDDED_VERSION);
sendevent(c->win, netatom[Xembed], StructureNotifyMask, CurrentTime, sendevent(c->win, netatom[Xembed], StructureNotifyMask, CurrentTime,
XEMBED_MODALITY_ON, 0, systray->win, XEMBED_EMBEDDED_VERSION); XEMBED_MODALITY_ON, 0, systray->win,
XEMBED_EMBEDDED_VERSION);
XSync(dpy, False); XSync(dpy, False);
resizebarwin(selmon); resizebarwin(selmon);
updatesystray(); updatesystray();
@ -864,7 +936,8 @@ void clientmessage(XEvent *e) {
if (cme->message_type == netatom[NetWMState]) { if (cme->message_type == netatom[NetWMState]) {
if (cme->data.l[1] == netatom[NetWMFullscreen] || if (cme->data.l[1] == netatom[NetWMFullscreen] ||
cme->data.l[2] == netatom[NetWMFullscreen]) cme->data.l[2] == netatom[NetWMFullscreen])
setfullscreen(c, (cme->data.l[0] == 1 /* _NET_WM_STATE_ADD */ setfullscreen(c,
(cme->data.l[0] == 1 /* _NET_WM_STATE_ADD */
|| (cme->data.l[0] == 2 /* _NET_WM_STATE_TOGGLE */ && || (cme->data.l[0] == 2 /* _NET_WM_STATE_TOGGLE */ &&
!c->isfullscreen))); !c->isfullscreen)));
} else if (cme->message_type == netatom[NetActiveWindow]) { } else if (cme->message_type == netatom[NetActiveWindow]) {
@ -944,9 +1017,11 @@ void configurerequest(XEvent *e) {
c->h = ev->height; c->h = ev->height;
} }
if ((c->x + c->w) > m->mx + m->mw && c->isfloating) if ((c->x + c->w) > m->mx + m->mw && c->isfloating)
c->x = m->mx + (m->mw / 2 - WIDTH(c) / 2); /* center in x direction */ c->x = m->mx +
(m->mw / 2 - WIDTH(c) / 2); /* center in x direction */
if ((c->y + c->h) > m->my + m->mh && c->isfloating) if ((c->y + c->h) > m->my + m->mh && c->isfloating)
c->y = m->my + (m->mh / 2 - HEIGHT(c) / 2); /* center in y direction */ c->y = m->my +
(m->mh / 2 - HEIGHT(c) / 2); /* center in y direction */
if ((ev->value_mask & (CWX | CWY)) && if ((ev->value_mask & (CWX | CWY)) &&
!(ev->value_mask & (CWWidth | CWHeight))) !(ev->value_mask & (CWWidth | CWHeight)))
configure(c); configure(c);
@ -988,6 +1063,8 @@ void destroynotify(XEvent *e) {
if ((c = wintoclient(ev->window))) if ((c = wintoclient(ev->window)))
unmanage(c, 1); unmanage(c, 1);
else if ((c = swallowingclient(ev->window)))
unmanage(c->swallowing, 1);
else if ((c = wintosystrayicon(ev->window))) { else if ((c = wintosystrayicon(ev->window))) {
removesystrayicon(c); removesystrayicon(c);
resizebarwin(selmon); resizebarwin(selmon);
@ -1097,7 +1174,8 @@ void drawbar(Monitor *m) {
Fnt *f; Fnt *f;
Fnt *p; Fnt *p;
resetfntlist(fset, drw->fonts); resetfntlist(fset, drw->fonts);
for (p = NULL, f = fset; f && fi--; p = f, f = f->next) for (p = NULL, f = fset; f && fi--;
p = f, f = f->next)
; ;
if (f) { if (f) {
if (p) { if (p) {
@ -1136,11 +1214,13 @@ void drawbar(Monitor *m) {
pw = TEXTW(buffer) - lrpad; pw = TEXTW(buffer) - lrpad;
drw_text(drw, x, 0, pw + lp, bh, lp, buffer, fmt & REVERSE); drw_text(drw, x, 0, pw + lp, bh, lp, buffer, fmt & REVERSE);
if (fmt & UNDERLINE) if (fmt & UNDERLINE)
drw_rect(drw, x, (bh + drw->fonts->h) / 2, pw, 1, 1, fmt & REVERSE); drw_rect(drw, x, (bh + drw->fonts->h) / 2, pw, 1, 1,
fmt & REVERSE);
if (fmt & STRIKETHROUGH) if (fmt & STRIKETHROUGH)
drw_rect(drw, x, bh / 2, pw, 1, 1, fmt & REVERSE); drw_rect(drw, x, bh / 2, pw, 1, 1, fmt & REVERSE);
if (fmt & OVERLINE) if (fmt & OVERLINE)
drw_rect(drw, x, (bh - drw->fonts->h) / 2, pw, 1, 1, fmt & REVERSE); drw_rect(drw, x, (bh - drw->fonts->h) / 2, pw, 1, 1,
fmt & REVERSE);
x += pw + lp; x += pw + lp;
lp = 0; lp = 0;
@ -1168,7 +1248,8 @@ void drawbar(Monitor *m) {
Fnt *f; Fnt *f;
Fnt *p; Fnt *p;
resetfntlist(fset, drw->fonts); resetfntlist(fset, drw->fonts);
for (p = NULL, f = fset; f && fi--; p = f, f = f->next) for (p = NULL, f = fset; f && fi--;
p = f, f = f->next)
; ;
if (f) { if (f) {
if (p) { if (p) {
@ -1238,7 +1319,8 @@ void drawbar(Monitor *m) {
if (!(occ & 1 << i || m->tagset[m->seltags] & 1 << i)) if (!(occ & 1 << i || m->tagset[m->seltags] & 1 << i))
continue; continue;
w = TEXTW(tags[i]); w = TEXTW(tags[i]);
drw_setscheme(drw, scheme[m->tagset[m->seltags] & 1 << i ? SchemeTagsSel drw_setscheme(drw,
scheme[m->tagset[m->seltags] & 1 << i ? SchemeTagsSel
: SchemeTagsNorm]); : SchemeTagsNorm]);
drw_text(drw, x, 0, w, bh, lrpad / 2, tags[i], urg & 1 << i); drw_text(drw, x, 0, w, bh, lrpad / 2, tags[i], urg & 1 << i);
x += w; x += w;
@ -1249,7 +1331,8 @@ void drawbar(Monitor *m) {
if ((w = m->ww - tw - stw - x) > bh) { if ((w = m->ww - tw - stw - x) > bh) {
if (m->sel) { if (m->sel) {
drw_setscheme(drw, scheme[m == selmon ? SchemeInfoSel : SchemeInfoNorm]); drw_setscheme(drw,
scheme[m == selmon ? SchemeInfoSel : SchemeInfoNorm]);
drw_text(drw, x, 0, w, bh, lrpad / 2, m->sel->name, 0); drw_text(drw, x, 0, w, bh, lrpad / 2, m->sel->name, 0);
if (m->sel->isfloating) if (m->sel->isfloating)
drw_rect(drw, x + boxs, boxs, boxw, boxw, m->sel->isfixed, 0); drw_rect(drw, x + boxs, boxs, boxw, boxw, m->sel->isfixed, 0);
@ -1485,9 +1568,10 @@ void grabbuttons(Client *c, int focused) {
for (i = 0; i < LENGTH(buttons); i++) for (i = 0; i < LENGTH(buttons); i++)
if (buttons[i].click == ClkClientWin) if (buttons[i].click == ClkClientWin)
for (j = 0; j < LENGTH(modifiers); j++) for (j = 0; j < LENGTH(modifiers); j++)
XGrabButton(dpy, buttons[i].button, buttons[i].mask | modifiers[j], XGrabButton(dpy, buttons[i].button,
c->win, False, BUTTONMASK, GrabModeAsync, GrabModeSync, buttons[i].mask | modifiers[j], c->win, False,
None, None); BUTTONMASK, GrabModeAsync, GrabModeSync, None,
None);
} }
} }
@ -1562,12 +1646,13 @@ void killclient(const Arg *arg) {
} }
void manage(Window w, XWindowAttributes *wa) { void manage(Window w, XWindowAttributes *wa) {
Client *c, *t = NULL; Client *c, *t = NULL, *term = NULL;
Window trans = None; Window trans = None;
XWindowChanges wc; XWindowChanges wc;
c = ecalloc(1, sizeof(Client)); c = ecalloc(1, sizeof(Client));
c->win = w; c->win = w;
c->pid = winpid(w);
/* geometry */ /* geometry */
c->x = c->oldx = wa->x; c->x = c->oldx = wa->x;
c->y = c->oldy = wa->y; c->y = c->oldy = wa->y;
@ -1582,6 +1667,7 @@ void manage(Window w, XWindowAttributes *wa) {
} else { } else {
c->mon = selmon; c->mon = selmon;
applyrules(c); applyrules(c);
term = termforwin(c);
} }
if (c->x + WIDTH(c) > c->mon->wx + c->mon->ww) if (c->x + WIDTH(c) > c->mon->wx + c->mon->ww)
@ -1619,6 +1705,8 @@ void manage(Window w, XWindowAttributes *wa) {
c->mon->sel = c; c->mon->sel = c;
arrange(c->mon); arrange(c->mon);
XMapWindow(dpy, c->win); XMapWindow(dpy, c->win);
if (term)
swallow(term, c);
focus(NULL); focus(NULL);
} }
@ -1637,7 +1725,8 @@ void maprequest(XEvent *e) {
Client *i; Client *i;
if ((i = wintosystrayicon(ev->window))) { if ((i = wintosystrayicon(ev->window))) {
sendevent(i->win, netatom[Xembed], StructureNotifyMask, CurrentTime, sendevent(i->win, netatom[Xembed], StructureNotifyMask, CurrentTime,
XEMBED_WINDOW_ACTIVATE, 0, systray->win, XEMBED_EMBEDDED_VERSION); XEMBED_WINDOW_ACTIVATE, 0, systray->win,
XEMBED_EMBEDDED_VERSION);
resizebarwin(selmon); resizebarwin(selmon);
updatesystray(); updatesystray();
} }
@ -1696,7 +1785,8 @@ void movemouse(const Arg *arg) {
if (!getrootptr(&x, &y)) if (!getrootptr(&x, &y))
return; return;
do { do {
XMaskEvent(dpy, MOUSEMASK | ExposureMask | SubstructureRedirectMask, &ev); XMaskEvent(dpy, MOUSEMASK | ExposureMask | SubstructureRedirectMask,
&ev);
switch (ev.type) { switch (ev.type) {
case ConfigureRequest: case ConfigureRequest:
case Expose: case Expose:
@ -1868,8 +1958,8 @@ void resizeclient(Client *c, int x, int y, int w, int h) {
c->oldh = c->h; c->oldh = c->h;
c->h = wc.height = h; c->h = wc.height = h;
wc.border_width = c->bw; wc.border_width = c->bw;
XConfigureWindow(dpy, c->win, CWX | CWY | CWWidth | CWHeight | CWBorderWidth, XConfigureWindow(dpy, c->win,
&wc); CWX | CWY | CWWidth | CWHeight | CWBorderWidth, &wc);
configure(c); configure(c);
XSync(dpy, False); XSync(dpy, False);
} }
@ -1896,7 +1986,8 @@ void resizemouse(const Arg *arg) {
ocx2 = c->x + c->w; ocx2 = c->x + c->w;
ocy2 = c->y + c->h; ocy2 = c->y + c->h;
if (XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync, if (XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
None, cursor[CurResize]->cursor, CurrentTime) != GrabSuccess) None, cursor[CurResize]->cursor,
CurrentTime) != GrabSuccess)
return; return;
if (!XQueryPointer(dpy, c->win, &dummy, &dummy, &di, &di, &nx, &ny, &dui)) if (!XQueryPointer(dpy, c->win, &dummy, &dummy, &di, &di, &nx, &ny, &dui))
return; return;
@ -1906,7 +1997,8 @@ void resizemouse(const Arg *arg) {
horizcorner ? (-c->bw) : (c->w + c->bw - 1), horizcorner ? (-c->bw) : (c->w + c->bw - 1),
vertcorner ? (-c->bw) : (c->h + c->bw - 1)); vertcorner ? (-c->bw) : (c->h + c->bw - 1));
do { do {
XMaskEvent(dpy, MOUSEMASK | ExposureMask | SubstructureRedirectMask, &ev); XMaskEvent(dpy, MOUSEMASK | ExposureMask | SubstructureRedirectMask,
&ev);
switch (ev.type) { switch (ev.type) {
case ConfigureRequest: case ConfigureRequest:
case Expose: case Expose:
@ -1920,9 +2012,11 @@ void resizemouse(const Arg *arg) {
nx = horizcorner ? ev.xmotion.x : c->x; nx = horizcorner ? ev.xmotion.x : c->x;
ny = vertcorner ? ev.xmotion.y : c->y; ny = vertcorner ? ev.xmotion.y : c->y;
nw = MAX(horizcorner ? (ocx2 - nx) : (ev.xmotion.x - ocx - 2 * c->bw + 1), nw = MAX(horizcorner ? (ocx2 - nx)
: (ev.xmotion.x - ocx - 2 * c->bw + 1),
1); 1);
nh = MAX(vertcorner ? (ocy2 - ny) : (ev.xmotion.y - ocy - 2 * c->bw + 1), nh = MAX(vertcorner ? (ocy2 - ny)
: (ev.xmotion.y - ocy - 2 * c->bw + 1),
1); 1);
if (c->mon->wx + nw >= selmon->wx && if (c->mon->wx + nw >= selmon->wx &&
@ -2002,8 +2096,8 @@ void scan(void) {
if (XQueryTree(dpy, root, &d1, &d2, &wins, &num)) { if (XQueryTree(dpy, root, &d1, &d2, &wins, &num)) {
for (i = 0; i < num; i++) { for (i = 0; i < num; i++) {
if (!XGetWindowAttributes(dpy, wins[i], &wa) || wa.override_redirect || if (!XGetWindowAttributes(dpy, wins[i], &wa) ||
XGetTransientForHint(dpy, wins[i], &d1)) wa.override_redirect || XGetTransientForHint(dpy, wins[i], &d1))
continue; continue;
if (wa.map_state == IsViewable || getstate(wins[i]) == IconicState) if (wa.map_state == IsViewable || getstate(wins[i]) == IconicState)
manage(wins[i], &wa); manage(wins[i], &wa);
@ -2012,7 +2106,8 @@ void scan(void) {
if (!XGetWindowAttributes(dpy, wins[i], &wa)) if (!XGetWindowAttributes(dpy, wins[i], &wa))
continue; continue;
if (XGetTransientForHint(dpy, wins[i], &d1) && if (XGetTransientForHint(dpy, wins[i], &d1) &&
(wa.map_state == IsViewable || getstate(wins[i]) == IconicState)) (wa.map_state == IsViewable ||
getstate(wins[i]) == IconicState))
manage(wins[i], &wa); manage(wins[i], &wa);
} }
if (wins) if (wins)
@ -2087,8 +2182,8 @@ void setfocus(Client *c) {
void setfullscreen(Client *c, int fullscreen) { void setfullscreen(Client *c, int fullscreen) {
if (fullscreen && !c->isfullscreen) { if (fullscreen && !c->isfullscreen) {
XChangeProperty(dpy, c->win, netatom[NetWMState], XA_ATOM, 32, XChangeProperty(dpy, c->win, netatom[NetWMState], XA_ATOM, 32,
PropModeReplace, (unsigned char *)&netatom[NetWMFullscreen], PropModeReplace,
1); (unsigned char *)&netatom[NetWMFullscreen], 1);
c->isfullscreen = 1; c->isfullscreen = 1;
c->oldstate = c->isfloating; c->oldstate = c->isfloating;
c->oldbw = c->bw; c->oldbw = c->bw;
@ -2176,7 +2271,8 @@ void setup(void) {
netatom[NetActiveWindow] = XInternAtom(dpy, "_NET_ACTIVE_WINDOW", False); netatom[NetActiveWindow] = XInternAtom(dpy, "_NET_ACTIVE_WINDOW", False);
netatom[NetSupported] = XInternAtom(dpy, "_NET_SUPPORTED", False); netatom[NetSupported] = XInternAtom(dpy, "_NET_SUPPORTED", False);
netatom[NetSystemTray] = XInternAtom(dpy, "_NET_SYSTEM_TRAY_S0", False); netatom[NetSystemTray] = XInternAtom(dpy, "_NET_SYSTEM_TRAY_S0", False);
netatom[NetSystemTrayOP] = XInternAtom(dpy, "_NET_SYSTEM_TRAY_OPCODE", False); netatom[NetSystemTrayOP] =
XInternAtom(dpy, "_NET_SYSTEM_TRAY_OPCODE", False);
netatom[NetSystemTrayOrientation] = netatom[NetSystemTrayOrientation] =
XInternAtom(dpy, "_NET_SYSTEM_TRAY_ORIENTATION", False); XInternAtom(dpy, "_NET_SYSTEM_TRAY_ORIENTATION", False);
netatom[NetSystemTrayOrientationHorz] = netatom[NetSystemTrayOrientationHorz] =
@ -2240,7 +2336,8 @@ void seturgent(Client *c, int urg) {
c->isurgent = urg; c->isurgent = urg;
if (!(wmh = XGetWMHints(dpy, c->win))) if (!(wmh = XGetWMHints(dpy, c->win)))
return; return;
wmh->flags = urg ? (wmh->flags | XUrgencyHint) : (wmh->flags & ~XUrgencyHint); wmh->flags =
urg ? (wmh->flags | XUrgencyHint) : (wmh->flags & ~XUrgencyHint);
XSetWMHints(dpy, c->win, wmh); XSetWMHints(dpy, c->win, wmh);
XFree(wmh); XFree(wmh);
} }
@ -2311,7 +2408,8 @@ int stackpos(const Arg *arg) {
; ;
return MOD(i + GETINC(arg->i), n); return MOD(i + GETINC(arg->i), n);
} else if (arg->i < 0) { } else if (arg->i < 0) {
for (i = 0, c = selmon->clients; c; i += ISVISIBLE(c) ? 1 : 0, c = c->next) for (i = 0, c = selmon->clients; c;
i += ISVISIBLE(c) ? 1 : 0, c = c->next)
; ;
return MAX(i + arg->i, 0); return MAX(i + arg->i, 0);
} else } else
@ -2435,6 +2533,20 @@ void unmanage(Client *c, int destroyed) {
Monitor *m = c->mon; Monitor *m = c->mon;
XWindowChanges wc; XWindowChanges wc;
if (c->swallowing) {
unswallow(c);
return;
}
Client *s = swallowingclient(c->win);
if (s) {
free(s->swallowing);
s->swallowing = NULL;
arrange(m);
focus(NULL);
return;
}
detach(c); detach(c);
detachstack(c); detachstack(c);
if (!destroyed) { if (!destroyed) {
@ -2450,9 +2562,11 @@ void unmanage(Client *c, int destroyed) {
XUngrabServer(dpy); XUngrabServer(dpy);
} }
free(c); free(c);
if (!s) {
arrange(m);
focus(NULL); focus(NULL);
updateclientlist(); updateclientlist();
arrange(m); }
} }
void unmapnotify(XEvent *e) { void unmapnotify(XEvent *e) {
@ -2550,8 +2664,9 @@ int updategeom(void) {
mons = createmon(); mons = createmon();
} }
for (i = 0, m = mons; i < nn && m; m = m->next, i++) for (i = 0, m = mons; i < nn && m; m = m->next, i++)
if (i >= n || unique[i].x_org != m->mx || unique[i].y_org != m->my || if (i >= n || unique[i].x_org != m->mx ||
unique[i].width != m->mw || unique[i].height != m->mh) { unique[i].y_org != m->my || unique[i].width != m->mw ||
unique[i].height != m->mh) {
dirty = 1; dirty = 1;
m->num = i; m->num = i;
m->mx = m->wx = unique[i].x_org; m->mx = m->wx = unique[i].x_org;
@ -2648,7 +2763,8 @@ void updatesizehints(Client *c) {
c->maxa = (float)size.max_aspect.x / size.max_aspect.y; c->maxa = (float)size.max_aspect.x / size.max_aspect.y;
} else } else
c->maxa = c->mina = 0.0; c->maxa = c->mina = 0.0;
c->isfixed = (c->maxw && c->maxh && c->maxw == c->minw && c->maxh == c->minh); c->isfixed =
(c->maxw && c->maxh && c->maxw == c->minw && c->maxh == c->minh);
c->hintsvalid = 1; c->hintsvalid = 1;
} }
@ -2729,11 +2845,14 @@ void updatesystray(void) {
XSelectInput(dpy, systray->win, SubstructureNotifyMask); XSelectInput(dpy, systray->win, SubstructureNotifyMask);
XChangeProperty(dpy, systray->win, netatom[NetSystemTrayOrientation], XChangeProperty(dpy, systray->win, netatom[NetSystemTrayOrientation],
XA_CARDINAL, 32, PropModeReplace, XA_CARDINAL, 32, PropModeReplace,
(unsigned char *)&netatom[NetSystemTrayOrientationHorz], 1); (unsigned char *)&netatom[NetSystemTrayOrientationHorz],
XChangeWindowAttributes( 1);
dpy, systray->win, CWEventMask | CWOverrideRedirect | CWBackPixel, &wa); XChangeWindowAttributes(dpy, systray->win,
CWEventMask | CWOverrideRedirect | CWBackPixel,
&wa);
XMapRaised(dpy, systray->win); XMapRaised(dpy, systray->win);
XSetSelectionOwner(dpy, netatom[NetSystemTray], systray->win, CurrentTime); XSetSelectionOwner(dpy, netatom[NetSystemTray], systray->win,
CurrentTime);
if (XGetSelectionOwner(dpy, netatom[NetSystemTray]) == systray->win) { if (XGetSelectionOwner(dpy, netatom[NetSystemTray]) == systray->win) {
sendevent(root, xatom[Manager], StructureNotifyMask, CurrentTime, sendevent(root, xatom[Manager], StructureNotifyMask, CurrentTime,
netatom[NetSystemTray], systray->win, 0, 0); netatom[NetSystemTray], systray->win, 0, 0);
@ -2821,6 +2940,133 @@ void view(const Arg *arg) {
arrange(selmon); arrange(selmon);
} }
pid_t winpid(Window w) {
pid_t result = 0;
#ifdef __linux__
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;
#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", 0), 0, 1,
False, AnyPropertyType, &type, &format, &len, &bytes,
&prop) != Success ||
!prop)
return 0;
ret = *(pid_t *)prop;
XFree(prop);
result = ret;
#endif /* __OpenBSD__ */
return result;
}
pid_t getparentprocess(pid_t p) {
unsigned int v = 0;
#ifdef __linux__
FILE *f;
char buf[256];
snprintf(buf, sizeof(buf) - 1, "/proc/%u/stat", (unsigned)p);
if (!(f = fopen(buf, "r")))
return 0;
fscanf(f, "%*u %*s %*c %u", &v);
fclose(f);
#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__ */
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;
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;
}
Client *wintoclient(Window w) { Client *wintoclient(Window w) {
Client *c; Client *c;
Monitor *m; Monitor *m;
@ -2972,12 +3218,14 @@ int main(int argc, char *argv[]) {
fputs("warning: no locale support\n", stderr); fputs("warning: no locale support\n", stderr);
if (!(dpy = XOpenDisplay(NULL))) if (!(dpy = XOpenDisplay(NULL)))
die("dwm: cannot open display"); die("dwm: cannot open display");
if (!(xcon = XGetXCBConnection(dpy)))
die("dwm: cannot get xcb connection\n");
checkotherwm(); checkotherwm();
XrmInitialize(); XrmInitialize();
load_xresources(); load_xresources();
setup(); setup();
#ifdef __OpenBSD__ #ifdef __OpenBSD__
if (pledge("stdio rpath proc exec", NULL) == -1) if (pledge("stdio rpath proc exec ps", NULL) == -1)
die("pledge"); die("pledge");
#endif /* __OpenBSD__ */ #endif /* __OpenBSD__ */
scan(); scan();