blob: 8c9c6d475443e14c529b619d19b3c1df16dbda67 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
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 */
|