diff --git a/config.def.h b/config.def.h index a855fb6..f1e356f 100644 --- a/config.def.h +++ b/config.def.h @@ -50,6 +50,7 @@ static const Layout layouts[] = { /* key definitions */ #define MODKEY Mod1Mask +#define STATUSBAR "dwmblocks" #define TAGKEYS(KEY,TAG) \ { MODKEY, KEY, view, {.ui = 1 << TAG} }, \ { MODKEY|ControlMask, KEY, toggleview, {.ui = 1 << TAG} }, \ @@ -112,7 +113,12 @@ static const Button buttons[] = { { ClkLtSymbol, 0, Button1, setlayout, {0} }, { ClkLtSymbol, 0, Button3, setlayout, {.v = &layouts[2]} }, { ClkWinTitle, 0, Button2, zoom, {0} }, - { ClkStatusText, 0, Button2, spawn, {.v = termcmd } }, + { ClkStatusText, 0, Button1, sigstatusbar, {.i = 1} }, + { ClkStatusText, 0, Button2, sigstatusbar, {.i = 2} }, + { ClkStatusText, 0, Button3, sigstatusbar, {.i = 3} }, + { ClkStatusText, 0, Button4, sigstatusbar, {.i = 4} }, + { ClkStatusText, 0, Button5, sigstatusbar, {.i = 5} }, + { ClkStatusText, ShiftMask, Button1, sigstatusbar, {.i = 6} }, { ClkClientWin, MODKEY, Button1, movemouse, {0} }, { ClkClientWin, MODKEY, Button2, togglefloating, {0} }, { ClkClientWin, MODKEY, Button3, resizemouse, {0} }, diff --git a/dwm.c b/dwm.c index df44da7..ce4d4b5 100644 --- a/dwm.c +++ b/dwm.c @@ -20,6 +20,7 @@ * * To understand everything else, start reading main(). */ +#include #include #include #include @@ -174,6 +175,7 @@ static void focusstack(const Arg *arg); static Atom getatomprop(Client *c, Atom prop); static int getrootptr(int *x, int *y); static long getstate(Window w); +static pid_t getstatusbarpid(); static int gettextprop(Window w, Atom atom, char *text, unsigned int size); static void grabbuttons(Client *c, int focused); static void grabkeys(void); @@ -208,6 +210,7 @@ static void setmfact(const Arg *arg); static void setup(void); static void seturgent(Client *c, int urg); static void showhide(Client *c); +static void sigstatusbar(const Arg *arg); static void spawn(const Arg *arg); static void tag(const Arg *arg); static void tagmon(const Arg *arg); @@ -240,6 +243,9 @@ static void zoom(const Arg *arg); /* variables */ static const char broken[] = "broken"; static char stext[256]; +static int statusw; +static int statussig; +static pid_t statuspid = -1; static int screen; static int sw, sh; /* X display screen geometry width, height */ static int bh; /* bar height */ @@ -435,6 +441,7 @@ buttonpress(XEvent *e) Client *c; Monitor *m; XButtonPressedEvent *ev = &e->xbutton; + char *text, *s, ch; click = ClkRootWin; /* focus monitor if necessary */ @@ -453,9 +460,27 @@ buttonpress(XEvent *e) arg.ui = 1 << i; } else if (ev->x < x + TEXTW(selmon->ltsymbol)) click = ClkLtSymbol; - else if (ev->x > selmon->ww - (int)TEXTW(stext)) + else if (ev->x > selmon->ww - statusw) { + x = selmon->ww - statusw; click = ClkStatusText; - else + statussig = 0; + for (text = s = stext; *s && x <= ev->x; s++) { + if ((unsigned char)(*s) < ' ') { + ch = *s; + *s = '\0'; + x += TEXTW(text) - lrpad; + *s = ch; + text = s + 1; + if (x >= ev->x) + break; + /* reset on matching signal raw byte */ + if (ch == statussig) + statussig = 0; + else + statussig = ch; + } + } + } else click = ClkWinTitle; } else if ((c = wintoclient(ev->window))) { focus(c); @@ -738,9 +763,24 @@ drawbar(Monitor *m) /* draw status first so it can be overdrawn by tags later */ if (m == selmon) { /* status is only drawn on selected monitor */ + char *text, *s, ch; drw_setscheme(drw, scheme[SchemeNorm]); - tw = TEXTW(stext) - lrpad + 2; /* 2px right padding */ - drw_text(drw, m->ww - tw, 0, tw, bh, 0, stext, 0); + + x = 0; + for (text = s = stext; *s; s++) { + if ((unsigned char)(*s) < ' ') { + ch = *s; + *s = '\0'; + tw = TEXTW(text) - lrpad; + drw_text(drw, m->ww - statusw + x, 0, tw, bh, 0, text, 0); + x += tw; + *s = ch; + text = s + 1; + } + } + tw = TEXTW(text) - lrpad + 2; + drw_text(drw, m->ww - statusw + x, 0, tw, bh, 0, text, 0); + tw = statusw; } for (c = m->clients; c; c = c->next) { @@ -907,6 +947,58 @@ getatomprop(Client *c, Atom prop) return atom; } +pid_t +getstatusbarpid() +{ + char path[280], name[256], *str, *c; + FILE *fp; + DIR *dir; + struct dirent *ent; + size_t n; + + /* validate cached PID */ + if (statuspid > 0) { + snprintf(path, sizeof(path), "/proc/%u/cmdline", (unsigned)statuspid); + if ((fp = fopen(path, "r"))) { + n = fread(name, 1, sizeof(name) - 1, fp); + fclose(fp); + if (n > 0) { + name[n] = '\0'; + str = name; + while ((c = strchr(str, '/'))) + str = c + 1; + if (!strcmp(str, STATUSBAR)) + return statuspid; + } + } + } + + /* scan /proc for the process */ + if (!(dir = opendir("/proc"))) + return -1; + while ((ent = readdir(dir))) { + if (ent->d_name[0] < '0' || ent->d_name[0] > '9') + continue; + snprintf(path, sizeof(path), "/proc/%s/cmdline", ent->d_name); + if (!(fp = fopen(path, "r"))) + continue; + n = fread(name, 1, sizeof(name) - 1, fp); + fclose(fp); + if (n > 0) { + name[n] = '\0'; + str = name; + while ((c = strchr(str, '/'))) + str = c + 1; + if (!strcmp(str, STATUSBAR)) { + closedir(dir); + return strtol(ent->d_name, NULL, 10); + } + } + } + closedir(dir); + return -1; +} + int getrootptr(int *x, int *y) { @@ -1684,6 +1776,20 @@ showhide(Client *c) } } +void +sigstatusbar(const Arg *arg) +{ + union sigval sv; + + if (!statussig || statussig > SIGRTMAX - SIGRTMIN) + return; + sv.sival_int = arg->i; + if ((statuspid = getstatusbarpid()) <= 0) + return; + + sigqueue(statuspid, SIGRTMIN+statussig, sv); +} + void spawn(const Arg *arg) { @@ -2069,8 +2175,25 @@ updatesizehints(Client *c) void updatestatus(void) { - if (!gettextprop(root, XA_WM_NAME, stext, sizeof(stext))) + if (!gettextprop(root, XA_WM_NAME, stext, sizeof(stext))) { strcpy(stext, "dwm-"VERSION); + statusw = TEXTW(stext) - lrpad + 2; + } else { + char *text, *s, ch; + + statusw = 0; + for (text = s = stext; *s; s++) { + if ((unsigned char)(*s) < ' ') { + ch = *s; + *s = '\0'; + statusw += TEXTW(text) - lrpad; + *s = ch; + text = s + 1; + } + } + statusw += TEXTW(text) - lrpad + 2; + + } drawbar(selmon); }