summaryrefslogtreecommitdiff
path: root/oldresources/breathing-exo-daemon/v4-clean-shm/exo.c
blob: 9ab1f24af41274bb5649c842c95a366f5cb10f74 (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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
// breathing.d (hardened)
#define _POSIX_C_SOURCE 200809L
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdatomic.h>
#include <string.h>
#include <math.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <time.h>
#include <signal.h>
#include <errno.h>

#define SHM_NAME "/breathing_color_shm"
#define MAGIC 0xBEEFCAFEu
#define VERSION 1u

// ~12–18s full loop as before
#define STEP_MS 90
#define STEP_T  0.02

// For your 7950X3D (32 threads), make “stress” mean it:
#define STRESS_THRESHOLD 16.0

// Sounds (optional)
#define SOUND_FILE_STRESS "/usr/local/share/sounds/AresEnraged.wav"
#define SOUND_FILE_NORMAL "/usr/local/share/sounds/ThanatosVent.wav"

// Shared block (16B aligned, seqlock protocol: even seq = stable)
typedef struct {
    uint32_t magic;
    uint32_t version;
    _Atomic uint32_t seq;
    char color[8]; // "#RRGGBB" + '\0' guaranteed
} shm_color_t;

static const double default_colors[][3] = {
    {1.0, 0.91, 0.40}, // Y
    {0.95, 0.44, 0.28}, // O
    {0.24, 0.85, 0.38}, // G
};
static const size_t DEFAULT_COLORS = sizeof(default_colors)/sizeof(default_colors[0]);

static const double stress_colors[][3] = {
    {0.96, 0.00, 0.00},
    {0.55, 0.00, 0.00},
    {0.64, 0.00, 0.00},
    {0.35, 0.00, 0.00},
};
static const size_t STRESS_COLORS = sizeof(stress_colors)/sizeof(stress_colors[0]);

static inline double clamp01(double v) {
    if (isnan(v) || isinf(v)) return 0.0;
    if (v < 0.0) return 0.0;
    if (v > 1.0) return 1.0;
    return v;
}
static inline double srgb_to_linear(double v) {
    v = clamp01(v);
    return (v <= 0.04045) ? (v / 12.92) : pow((v + 0.055)/1.055, 2.4);
}
static inline double linear_to_srgb(double v) {
    v = clamp01(v);
    return (v <= 0.0031308) ? (v * 12.92) : (1.055*pow(v, 1.0/2.4) - 0.055);
}
static void lerp_color(const double c1[3], const double c2[3], double t, char out[8]) {
    double lr = srgb_to_linear(c1[0]) + t*(srgb_to_linear(c2[0]) - srgb_to_linear(c1[0]));
    double lg = srgb_to_linear(c1[1]) + t*(srgb_to_linear(c2[1]) - srgb_to_linear(c1[1]));
    double lb = srgb_to_linear(c1[2]) + t*(srgb_to_linear(c2[2]) - srgb_to_linear(c1[2]));
    unsigned r = (unsigned)lround(clamp01(linear_to_srgb(lr))*255.0);
    unsigned g = (unsigned)lround(clamp01(linear_to_srgb(lg))*255.0);
    unsigned b = (unsigned)lround(clamp01(linear_to_srgb(lb))*255.0);
    snprintf(out, 8, "#%02x%02x%02x", r, g, b);
}
static double sys_load(void) {
    double l[3] = {0,0,0};
    if (getloadavg(l, 1) == 1) return l[0];
    return 0.0;
}
static void play_once(const char *file) {
    pid_t p = fork();
    if (p == 0) { // child
        // Try PipeWire first, then Pulse
        // execlp("pw-play", "pw-play", file, (char*)NULL); // TRIM: Pulse not installed 
        execlp("paplay",  "paplay",  file, (char*)NULL);
        _exit(127);
    }
}
static void sleep_ms(long ms) {
    struct timespec req = { .tv_sec = ms/1000, .tv_nsec = (ms%1000)*1000000L };
    while (clock_nanosleep(CLOCK_MONOTONIC, 0, &req, &req) == EINTR) {}
}

int main(void) {
    // Don’t accumulate zombies if sound player exits
    struct sigaction sa = {0};
    sa.sa_handler = SIG_IGN; sigaction(SIGCHLD, &sa, NULL);

    int fd = shm_open(SHM_NAME, O_CREAT|O_RDWR, 0666);
    if (fd < 0) { perror("shm_open"); return 1; }
    if (ftruncate(fd, sizeof(shm_color_t)) != 0) { perror("ftruncate"); return 1; }

    shm_color_t *blk = mmap(NULL, sizeof(shm_color_t), PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
    if (blk == MAP_FAILED) { perror("mmap"); return 1; }
    close(fd);

    // Initialize header
    blk->magic = MAGIC;
    blk->version = VERSION;
    atomic_store(&blk->seq, 0);
    memcpy(blk->color, "#9ab5ff", 8);

    size_t idx = 0;
    double t = 0.0;
    int stress = 0;
    int last_stress = -1;

    for (;;) {
        // mode & palette
        double load = sys_load();
        stress = (load > STRESS_THRESHOLD);

        const double (*pal)[3] = stress ? stress_colors : default_colors;
        size_t n = stress ? STRESS_COLORS : DEFAULT_COLORS;

        // sound on edge only
        if (stress != last_stress) {
            play_once(stress ? SOUND_FILE_STRESS : SOUND_FILE_NORMAL);
            last_stress = stress;
        }

        // color
        char hex[8];
        const double *c1 = pal[idx];
        const double *c2 = pal[(idx+1)%n];
        lerp_color(c1, c2, t, hex);

        // seqlock write: odd while writing, even when stable
        uint32_t s = atomic_load_explicit(&blk->seq, memory_order_relaxed);
        atomic_store_explicit(&blk->seq, s+1, memory_order_release); // mark write begin
        memcpy(blk->color, hex, 8);
        atomic_store_explicit(&blk->seq, s+2, memory_order_release); // mark write end

        // step
        t += STEP_T;
        if (t >= 1.0) { t = 0.0; idx = (idx+1)%n; }

        sleep_ms(STEP_MS);
    }

    // unreachable in simple daemon mode
    // munmap(blk, sizeof(*blk)); shm_unlink(SHM_NAME);
    // return 0;
}