summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--accent.h52
-rw-r--r--dmenu/dmenu.c40
-rw-r--r--dwm/dwm.c44
-rw-r--r--tools/exofetch.c39
4 files changed, 78 insertions, 97 deletions
diff --git a/accent.h b/accent.h
new file mode 100644
index 0000000..8c9c6d4
--- /dev/null
+++ b/accent.h
@@ -0,0 +1,52 @@
+#ifndef ACCENT_H
+#define ACCENT_H
+#include <stdint.h>
+#include <string.h>
+#include <sys/mman.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+
+#define SHMNAME "/breathing_color_shm"
+#define SHM_MAGIC 0xBEEFCAFEu
+#define SHM_VERSION 1u
+
+typedef struct {
+ uint32_t magic;
+ uint32_t version;
+ volatile uint32_t seq;
+ char color[8];
+} ColorShm;
+
+static inline ColorShm *
+mapaccent(void)
+{
+ int fd;
+ ColorShm *blk;
+ if ((fd = shm_open(SHMNAME, O_RDONLY, 0)) < 0)
+ return NULL;
+ blk = mmap(NULL, sizeof(ColorShm), PROT_READ, MAP_SHARED, fd, 0);
+ close(fd);
+ if (blk == MAP_FAILED)
+ return NULL;
+ if (blk->magic != SHM_MAGIC || blk->version != SHM_VERSION) {
+ munmap(blk, sizeof(ColorShm));
+ return NULL;
+ }
+ return blk;
+}
+
+static inline int
+readaccent(ColorShm **blk, char out[8])
+{
+ uint32_t s1, s2;
+ if (!*blk && !(*blk = mapaccent()))
+ return 0;
+ do {
+ s1 = (*blk)->seq;
+ memcpy(out, (*blk)->color, 8);
+ s2 = (*blk)->seq;
+ } while (s1 != s2);
+ return out[0] == '#';
+}
+
+#endif /* ACCENT_H */
diff --git a/dmenu/dmenu.c b/dmenu/dmenu.c
index 066d20f..54a14e2 100644
--- a/dmenu/dmenu.c
+++ b/dmenu/dmenu.c
@@ -57,18 +57,13 @@ static XIC xic;
static Drw *drw;
static Clr *scheme[SchemeLast];
-#define SHMNAME "/breathing_color_shm"
-typedef struct {
-uint32_t seq;
-char color[8];
-} ColorShm;
+#include "../accent.h"
static ColorShm *accentshm;
static char accentcol[8] = "#005577";
static const double opacity = 0.85;
static void initaccent(void);
static void updateaccent(void);
-static int openaccent(void);
static void setopacity(Window w, double opacity);
#include "config.h"
@@ -130,40 +125,11 @@ cleanup(void)
XCloseDisplay(dpy);
}
-static int
-openaccent(void)
-{
- int fd;
- if (accentshm)
- return 1;
- if ((fd = shm_open(SHMNAME, O_RDONLY, 0)) < 0)
- return 0;
- accentshm = mmap(NULL, sizeof(ColorShm), PROT_READ, MAP_SHARED, fd, 0);
- close(fd);
- if (accentshm == MAP_FAILED) {
- accentshm = NULL;
- return 0;
- }
- return 1;
-}
-
static void
updateaccent(void)
{
- uint32_t s1, s2;
- char tmp[8];
-
- if (!accentshm && !openaccent())
- return;
- do {
- s1 = accentshm->seq;
- memcpy(tmp, accentshm->color, 8);
- s2 = accentshm->seq;
- } while (s1 != s2);
- if (tmp[0] != '#')
- return;
- memcpy(accentcol, tmp, 8);
- drw_clr_create(drw, &scheme[SchemeSel][ColBg], accentcol);
+ if (readaccent(&accentshm, accentcol))
+ drw_clr_create(drw, &scheme[SchemeSel][ColBg], accentcol);
}
static void
diff --git a/dwm/dwm.c b/dwm/dwm.c
index 7a15419..8eccf8f 100644
--- a/dwm/dwm.c
+++ b/dwm/dwm.c
@@ -318,6 +318,19 @@ typedef struct {
static ColorShm *accentshm;
static char accentcol[8] = "#005577";
+#ifndef BAROPACITY
+#define BAROPACITY 1.0
+#endif
+
+/* tag used for scratchpad clients */
+static const unsigned int scratchtag = 1 << LENGTH(tags);
+
+#include "../accent.h"
+
+/* state for the shared accent color */
+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]; };
@@ -697,34 +710,17 @@ createmon(void)
static void
updateaccent(void) {
- if (!accentshm)
- return;
- uint32_t s1, s2;
- char tmp[8];
- do {
- s1 = accentshm->seq;
- memcpy(tmp, accentshm->color, 8);
- s2 = accentshm->seq;
- } while (s1 != s2);
- if (tmp[0] != '#')
- return;
- memcpy(accentcol, tmp, 8);
- drw_clr_create(drw, &scheme[SchemeSel][ColBg], accentcol);
- drw_clr_create(drw, &scheme[SchemeSel][ColBorder], accentcol);
- for (Monitor *m = mons; m; m = m->next)
- if (m->sel)
- XSetWindowBorder(dpy, m->sel->win, scheme[SchemeSel][ColBorder].pixel);
+ if (readaccent(&accentshm, accentcol)) {
+ drw_clr_create(drw, &scheme[SchemeSel][ColBg], accentcol);
+ drw_clr_create(drw, &scheme[SchemeSel][ColBorder], accentcol);
+ for (Monitor *m = mons; m; m = m->next)
+ if (m->sel)
+ XSetWindowBorder(dpy, m->sel->win, scheme[SchemeSel][ColBorder].pixel);
+ }
}
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();
}
diff --git a/tools/exofetch.c b/tools/exofetch.c
index bc5548a..826464d 100644
--- a/tools/exofetch.c
+++ b/tools/exofetch.c
@@ -10,48 +10,16 @@
#include <sys/stat.h>
#include <fcntl.h>
#include <locale.h>
-
-#define SHMNAME "/breathing_color_shm"
-
-typedef struct {
- uint32_t seq;
- char color[8];
-} ColorShm;
+#include "../accent.h"
static ColorShm *accentshm;
static char accentcol[8] = "#005577";
-static int
-openaccent(void)
-{
- int fd;
- if (accentshm)
- return 0;
- if ((fd = shm_open(SHMNAME, O_RDONLY, 0)) < 0)
- return -1;
- accentshm = mmap(NULL, sizeof(ColorShm), PROT_READ, MAP_SHARED, fd, 0);
- close(fd);
- if (accentshm == MAP_FAILED) {
- accentshm = NULL;
- return -1;
- }
- return 0;
-}
-
static void
updateaccent(void)
{
- uint32_t s1, s2;
- char tmp[8];
- if (!accentshm && openaccent() < 0)
- return;
- do {
- s1 = accentshm->seq;
- memcpy(tmp, accentshm->color, 8);
- s2 = accentshm->seq;
- } while (s1 != s2);
- if (tmp[0] == '#')
- memcpy(accentcol, tmp, 8);
+ if (!readaccent(&accentshm, accentcol))
+ memcpy(accentcol, "#005577", 8);
}
static void
@@ -127,7 +95,6 @@ int
main(void)
{
setlocale(LC_ALL, "");
- openaccent();
updateaccent();
int r, g, b;
hex2rgb(accentcol, &r, &g, &b);