diff options
| author | auric <104602845+ihateamongus@users.noreply.github.com> | 2025-09-07 16:33:30 -0500 |
|---|---|---|
| committer | auric <104602845+ihateamongus@users.noreply.github.com> | 2025-09-07 16:33:30 -0500 |
| commit | b87cd2c058b61968988f0a15d153e6e9cf3b2e08 (patch) | |
| tree | 18259ae2973f2a71cb8dbb4cfb90391761deaf56 | |
| parent | b59b8b8e5d16af7f0daccb280a4991e3fe428e41 (diff) | |
Enable dynamic accent color in dmenu
| -rw-r--r-- | dmenu/dmenu.c | 129 | ||||
| -rw-r--r-- | dwm/config.def.h | 7 | ||||
| -rw-r--r-- | dwm/dwm.c | 113 |
3 files changed, 203 insertions, 46 deletions
diff --git a/dmenu/dmenu.c b/dmenu/dmenu.c index fd49549..0eb6d45 100644 --- a/dmenu/dmenu.c +++ b/dmenu/dmenu.c @@ -7,6 +7,11 @@ #include <strings.h> #include <time.h> #include <unistd.h> +#include <stdint.h> +#include <sys/mman.h> +#include <sys/stat.h> +#include <fcntl.h> +#include <sys/select.h> #include <X11/Xlib.h> #include <X11/Xatom.h> @@ -52,6 +57,17 @@ static XIC xic; static Drw *drw; static Clr *scheme[SchemeLast]; +#define SHMNAME "/breathing_color_shm" +typedef struct { +uint32_t seq; +char color[8]; +} ColorShm; +static ColorShm *accentshm; +static char accentcol[8] = "#005577"; + +static void initaccent(void); +static void updateaccent(void); + #include "config.h" static int (*fstrncmp)(const char *, const char *, size_t) = strncmp; @@ -111,6 +127,34 @@ cleanup(void) XCloseDisplay(dpy); } +static void +updateaccent(void) +{ +uint32_t s1, s2; + +if (!accentshm) +return; +do { +s1 = accentshm->seq; +memcpy(accentcol, accentshm->color, 8); +s2 = accentshm->seq; +} while (s1 != s2); +drw_clr_create(drw, &scheme[SchemeSel][ColBg], accentcol); +} + +static void +initaccent(void) +{ +int fd = shm_open(SHMNAME, O_RDONLY, 0); +if (fd >= 0) { +accentshm = mmap(NULL, sizeof(ColorShm), PROT_READ, + MAP_SHARED, fd, 0); +if (accentshm == MAP_FAILED) +accentshm = NULL; +close(fd); +} +updateaccent(); +} static char * cistrstr(const char *h, const char *n) { @@ -575,39 +619,53 @@ readstdin(void) static void run(void) { - XEvent ev; - - while (!XNextEvent(dpy, &ev)) { - if (XFilterEvent(&ev, win)) - continue; - switch(ev.type) { - case DestroyNotify: - if (ev.xdestroywindow.window != win) - break; - cleanup(); - exit(1); - case Expose: - if (ev.xexpose.count == 0) - drw_map(drw, win, 0, 0, mw, mh); - break; - case FocusIn: - /* regrab focus from parent window */ - if (ev.xfocus.window != win) - grabfocus(); - break; - case KeyPress: - keypress(&ev.xkey); - break; - case SelectionNotify: - if (ev.xselection.property == utf8) - paste(); - break; - case VisibilityNotify: - if (ev.xvisibility.state != VisibilityUnobscured) - XRaiseWindow(dpy, win); - break; - } - } + XEvent ev; + int xfd; + fd_set fds; + struct timeval tv; + + xfd = ConnectionNumber(dpy); + for (;;) { + FD_ZERO(&fds); + FD_SET(xfd, &fds); + tv.tv_sec = 0; + tv.tv_usec = 50000; + if (select(xfd + 1, &fds, NULL, NULL, &tv) > 0) { + while (XPending(dpy)) { + XNextEvent(dpy, &ev); + if (XFilterEvent(&ev, win)) + continue; + switch (ev.type) { + case DestroyNotify: + if (ev.xdestroywindow.window != win) + break; + cleanup(); + exit(1); + case Expose: + if (ev.xexpose.count == 0) + drw_map(drw, win, 0, 0, mw, mh); + break; + case FocusIn: + if (ev.xfocus.window != win) + grabfocus(); + break; + case KeyPress: + keypress(&ev.xkey); + break; + case SelectionNotify: + if (ev.xselection.property == utf8) + paste(); + break; + case VisibilityNotify: + if (ev.xvisibility.state != VisibilityUnobscured) + XRaiseWindow(dpy, win); + break; + } + } + } + updateaccent(); + drawmenu(); + } } static void @@ -707,8 +765,9 @@ setup(void) } grabfocus(); } - drw_resize(drw, mw, mh); - drawmenu(); + drw_resize(drw, mw, mh); + initaccent(); + drawmenu(); } static void diff --git a/dwm/config.def.h b/dwm/config.def.h index 81c3fc0..3b5b789 100644 --- a/dwm/config.def.h +++ b/dwm/config.def.h @@ -2,6 +2,7 @@ /* appearance */ static const unsigned int borderpx = 1; /* border pixel of windows */ +static const unsigned int gappx = 5; /* gaps between windows */ static const unsigned int snap = 32; /* snap pixel */ static const int showbar = 1; /* 0 means no bar */ static const int topbar = 1; /* 0 means bottom bar */ @@ -60,11 +61,14 @@ static const Layout layouts[] = { static char dmenumon[2] = "0"; /* component of dmenucmd, manipulated in spawn() */ static const char *dmenucmd[] = { "dmenu_run", "-m", dmenumon, "-fn", dmenufont, "-nb", col_gray1, "-nf", col_gray3, "-sb", col_cyan, "-sf", col_gray4, NULL }; static const char *termcmd[] = { "st", NULL }; +static const char scratchpadname[] = "scratchpad"; +static const char *scratchpadcmd[] = { "st", "-t", scratchpadname, "-g", "120x34", NULL }; static const Key keys[] = { /* modifier key function argument */ { MODKEY, XK_p, spawn, {.v = dmenucmd } }, { MODKEY|ShiftMask, XK_Return, spawn, {.v = termcmd } }, + { MODKEY, XK_grave, togglescratch, {.v = scratchpadcmd } }, { MODKEY, XK_b, togglebar, {0} }, { MODKEY, XK_j, focusstack, {.i = +1 } }, { MODKEY, XK_k, focusstack, {.i = -1 } }, @@ -86,6 +90,9 @@ static const Key keys[] = { { MODKEY, XK_period, focusmon, {.i = +1 } }, { MODKEY|ShiftMask, XK_comma, tagmon, {.i = -1 } }, { MODKEY|ShiftMask, XK_period, tagmon, {.i = +1 } }, + { MODKEY, XK_minus, setgaps, {.i = -1 } }, + { MODKEY, XK_equal, setgaps, {.i = +1 } }, + { MODKEY|ShiftMask, XK_equal, setgaps, {.i = 0 } }, TAGKEYS( XK_1, 0) TAGKEYS( XK_2, 1) TAGKEYS( XK_3, 2) @@ -30,6 +30,10 @@ #include <unistd.h> #include <sys/types.h> #include <sys/wait.h> +#include <stdint.h> +#include <sys/mman.h> +#include <sys/stat.h> +#include <fcntl.h> #include <X11/cursorfont.h> #include <X11/keysym.h> #include <X11/Xatom.h> @@ -118,6 +122,7 @@ struct Monitor { int by; /* bar geometry */ int mx, my, mw, mh; /* screen size */ int wx, wy, ww, wh; /* window area */ + int gappx; /* gaps between windows */ unsigned int seltags; unsigned int sellt; unsigned int tagset[2]; @@ -199,9 +204,12 @@ static void sendmon(Client *c, Monitor *m); static void setclientstate(Client *c, long state); static void setfocus(Client *c); static void setfullscreen(Client *c, int fullscreen); +static void setgaps(const Arg *arg); static void setlayout(const Arg *arg); static void setmfact(const Arg *arg); static void setup(void); +static void initaccent(void); +static void updateaccent(void); static void seturgent(Client *c, int urg); static void showhide(Client *c); static void spawn(const Arg *arg); @@ -210,6 +218,7 @@ static void tagmon(const Arg *arg); static void tile(Monitor *m); static void togglebar(const Arg *arg); static void togglefloating(const Arg *arg); +static void togglescratch(const Arg *arg); static void toggletag(const Arg *arg); static void toggleview(const Arg *arg); static void unfocus(Client *c, int setfocus); @@ -270,6 +279,19 @@ static Window root, wmcheckwin; /* configuration, allows nested code to access above variables */ #include "config.h" +static unsigned int scratchtag = 1 << LENGTH(tags); + +#define SHMNAME "/breathing_color_shm" +typedef struct { + uint32_t magic; + uint32_t version; + volatile uint32_t seq; + char color[8]; +} ColorShm; + +static ColorShm *accentshm; +static char accentcol[8] = "#005577"; + /* compile-time check if all tags fit into an unsigned int bit array. */ struct NumTags { char limitexceeded[LENGTH(tags) > 31 ? -1 : 1]; }; @@ -640,12 +662,38 @@ createmon(void) m->nmaster = nmaster; m->showbar = showbar; m->topbar = topbar; + m->gappx = gappx; m->lt[0] = &layouts[0]; m->lt[1] = &layouts[1 % LENGTH(layouts)]; strncpy(m->ltsymbol, layouts[0].symbol, sizeof m->ltsymbol); return m; } +static void +updateaccent(void) { + if (!accentshm) return; + uint32_t s1, s2; + do { + s1 = accentshm->seq; + memcpy(accentcol, accentshm->color, 8); + s2 = accentshm->seq; + } while (s1 != s2); + drw_clr_create(drw, &scheme[SchemeSel][ColBg], accentcol); + drw_clr_create(drw, &scheme[SchemeSel][ColBorder], accentcol); +} + +static void +initaccent(void) { + int fd = shm_open(SHMNAME, O_RDONLY, 0); + if (fd >= 0) { + accentshm = mmap(NULL, sizeof(ColorShm), PROT_READ, MAP_SHARED, fd, 0); + close(fd); + if (accentshm == MAP_FAILED) + accentshm = NULL; + } + updateaccent(); +} + void destroynotify(XEvent *e) { @@ -697,6 +745,7 @@ dirtomon(int dir) void drawbar(Monitor *m) { + updateaccent(); int x, w, tw = 0; int boxs = drw->fonts->h / 9; int boxw = drw->fonts->h / 6 + 2; @@ -1060,6 +1109,14 @@ manage(Window w, XWindowAttributes *wa) c->y = MAX(c->y, c->mon->wy); c->bw = borderpx; + selmon->tagset[selmon->seltags] &= ~scratchtag; + if (!strcmp(c->name, scratchpadname)) { + c->mon->tagset[c->mon->seltags] |= c->tags = scratchtag; + c->isfloating = True; + c->x = c->mon->wx + (c->mon->ww / 2 - WIDTH(c) / 2); + c->y = c->mon->wy + (c->mon->wh / 2 - HEIGHT(c) / 2); + } + wc.border_width = c->bw; XConfigureWindow(dpy, w, CWBorderWidth, &wc); XSetWindowBorder(dpy, w, scheme[SchemeNorm][ColBorder].pixel); @@ -1507,6 +1564,16 @@ setfullscreen(Client *c, int fullscreen) } void +setgaps(const Arg *arg) +{ + if ((arg->i == 0) || (selmon->gappx + arg->i < 0)) + selmon->gappx = 0; + else + selmon->gappx += arg->i; + arrange(selmon); +} + +void setlayout(const Arg *arg) { if (!arg || !arg->v || arg->v != selmon->lt[selmon->sellt]) @@ -1586,6 +1653,7 @@ setup(void) scheme = ecalloc(LENGTH(colors), sizeof(Clr *)); for (i = 0; i < LENGTH(colors); i++) scheme[i] = drw_scm_create(drw, colors[i], 3); + initaccent(); /* init bars */ updatebars(); updatestatus(); @@ -1650,6 +1718,7 @@ spawn(const Arg *arg) if (arg->v == dmenucmd) dmenumon[0] = '0' + selmon->num; + selmon->tagset[selmon->seltags] &= ~scratchtag; if (fork() == 0) { if (dpy) close(ConnectionNumber(dpy)); @@ -1696,18 +1765,18 @@ tile(Monitor *m) if (n > m->nmaster) mw = m->nmaster ? m->ww * m->mfact : 0; else - mw = m->ww; - for (i = my = ty = 0, c = nexttiled(m->clients); c; c = nexttiled(c->next), i++) - if (i < m->nmaster) { - h = (m->wh - my) / (MIN(n, m->nmaster) - i); - resize(c, m->wx, m->wy + my, mw - (2*c->bw), h - (2*c->bw), 0); - if (my + HEIGHT(c) < m->wh) - my += HEIGHT(c); + mw = m->ww - m->gappx; + for (i = 0, my = ty = m->gappx, c = nexttiled(m->clients); c; c = nexttiled(c->next), i++) + if (i < m->nmaster) { + h = (m->wh - my) / (MIN(n, m->nmaster) - i) - m->gappx; + resize(c, m->wx + m->gappx, m->wy + my, mw - (2*c->bw) - m->gappx, h - (2*c->bw), 0); + if (my + HEIGHT(c) + m->gappx < m->wh) + my += HEIGHT(c) + m->gappx; } else { - h = (m->wh - ty) / (n - i); - resize(c, m->wx + mw, m->wy + ty, m->ww - mw - (2*c->bw), h - (2*c->bw), 0); - if (ty + HEIGHT(c) < m->wh) - ty += HEIGHT(c); + h = (m->wh - ty) / (n - i) - m->gappx; + resize(c, m->wx + mw + m->gappx, m->wy + ty, m->ww - mw - (2*c->bw) - 2*m->gappx, h - (2*c->bw), 0); + if (ty + HEIGHT(c) + m->gappx < m->wh) + ty += HEIGHT(c) + m->gappx; } } @@ -1735,6 +1804,28 @@ togglefloating(const Arg *arg) } void +togglescratch(const Arg *arg) +{ + Client *c; + unsigned int found = 0; + + for (c = selmon->clients; c && !(found = c->tags & scratchtag); c = c->next); + if (found) { + unsigned int newtagset = selmon->tagset[selmon->seltags] ^ scratchtag; + if (newtagset) { + selmon->tagset[selmon->seltags] = newtagset; + focus(NULL); + arrange(selmon); + } + if (ISVISIBLE(c)) { + focus(c); + restack(selmon); + } + } else + spawn(arg); +} + +void toggletag(const Arg *arg) { unsigned int newtags; |
