summaryrefslogtreecommitdiff
path: root/archive/breathing-exo-daemon/v4-clean-shm
diff options
context:
space:
mode:
Diffstat (limited to 'archive/breathing-exo-daemon/v4-clean-shm')
-rw-r--r--archive/breathing-exo-daemon/v4-clean-shm/Makefile17
-rw-r--r--archive/breathing-exo-daemon/v4-clean-shm/breathing.dbin0 -> 20224 bytes
-rw-r--r--archive/breathing-exo-daemon/v4-clean-shm/exo.c158
-rw-r--r--archive/breathing-exo-daemon/v4-clean-shm/loadtest.c10
-rw-r--r--archive/breathing-exo-daemon/v4-clean-shm/testbin0 -> 15496 bytes
5 files changed, 185 insertions, 0 deletions
diff --git a/archive/breathing-exo-daemon/v4-clean-shm/Makefile b/archive/breathing-exo-daemon/v4-clean-shm/Makefile
new file mode 100644
index 0000000..2fef9cc
--- /dev/null
+++ b/archive/breathing-exo-daemon/v4-clean-shm/Makefile
@@ -0,0 +1,17 @@
+CC := gcc
+CFLAGS := -std=c11 -O2 -Wall -Wextra -pedantic -D_GNU_SOURCE
+LDFLAGS := -lm
+TARGET := breathing.d
+
+SRC := exo.c
+
+all: $(TARGET)
+
+$(TARGET): $(SRC)
+ $(CC) $(CFLAGS) -o $@ $^ $(LDFLAGS)
+
+clean:
+ rm -f $(TARGET) *.o
+
+.PHONY: all clean
+
diff --git a/archive/breathing-exo-daemon/v4-clean-shm/breathing.d b/archive/breathing-exo-daemon/v4-clean-shm/breathing.d
new file mode 100644
index 0000000..cc85b67
--- /dev/null
+++ b/archive/breathing-exo-daemon/v4-clean-shm/breathing.d
Binary files differ
diff --git a/archive/breathing-exo-daemon/v4-clean-shm/exo.c b/archive/breathing-exo-daemon/v4-clean-shm/exo.c
new file mode 100644
index 0000000..9ab1f24
--- /dev/null
+++ b/archive/breathing-exo-daemon/v4-clean-shm/exo.c
@@ -0,0 +1,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;
+}
+
diff --git a/archive/breathing-exo-daemon/v4-clean-shm/loadtest.c b/archive/breathing-exo-daemon/v4-clean-shm/loadtest.c
new file mode 100644
index 0000000..8cb3162
--- /dev/null
+++ b/archive/breathing-exo-daemon/v4-clean-shm/loadtest.c
@@ -0,0 +1,10 @@
+#include <stdio.h>
+#include <stdlib.h>
+#define _GNU_SOURCE
+int main() {
+ double l[3];
+ if (getloadavg(l, 3) != -1)
+ printf("load: %.2f %.2f %.2f\n", l[0], l[1], l[2]);
+ return 0;
+}
+
diff --git a/archive/breathing-exo-daemon/v4-clean-shm/test b/archive/breathing-exo-daemon/v4-clean-shm/test
new file mode 100644
index 0000000..501cf97
--- /dev/null
+++ b/archive/breathing-exo-daemon/v4-clean-shm/test
Binary files differ