summaryrefslogtreecommitdiff
path: root/archive/breathing-exo-daemon/v3-shm
diff options
context:
space:
mode:
authorauric <auric7@protonmail.com>2025-09-08 21:27:55 -0500
committerauric <auric7@protonmail.com>2025-09-08 21:27:55 -0500
commitde07b49b3249da05605f8c802b991a8588ab63b3 (patch)
tree295d6003f08dfce1d1b779892ce9c0be97d426a3 /archive/breathing-exo-daemon/v3-shm
parent04cfeeb799b4ee6ac990e5d6e1b5302251133d77 (diff)
parente61da07522a060da98fa3a56db3d0360469b26cf (diff)
Resolved conflict, indulging new file layout
Diffstat (limited to 'archive/breathing-exo-daemon/v3-shm')
-rw-r--r--archive/breathing-exo-daemon/v3-shm/breathing.dbin0 -> 16480 bytes
-rw-r--r--archive/breathing-exo-daemon/v3-shm/exo.c178
-rw-r--r--archive/breathing-exo-daemon/v3-shm/exo.c.oldnosync222
3 files changed, 400 insertions, 0 deletions
diff --git a/archive/breathing-exo-daemon/v3-shm/breathing.d b/archive/breathing-exo-daemon/v3-shm/breathing.d
new file mode 100644
index 0000000..469ea6a
--- /dev/null
+++ b/archive/breathing-exo-daemon/v3-shm/breathing.d
Binary files differ
diff --git a/archive/breathing-exo-daemon/v3-shm/exo.c b/archive/breathing-exo-daemon/v3-shm/exo.c
new file mode 100644
index 0000000..5898280
--- /dev/null
+++ b/archive/breathing-exo-daemon/v3-shm/exo.c
@@ -0,0 +1,178 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <math.h>
+#include <unistd.h>
+#include <sys/mman.h>
+#include <fcntl.h>
+#include <sys/stat.h>
+#include <time.h>
+#include <sys/types.h>
+
+#define BREATHING_INTERVAL 90 // in milliseconds
+#define STRESS_THRESHOLD 8.0 // System load threshold for stress mode
+#define SHM_NAME "/breathing_color_shm" // Shared memory name
+#define SOUND_FILE_STRESS "/usr/local/share/sounds/AresEnraged.wav" // Sound for stress mode
+#define SOUND_FILE_NORMAL "/usr/local/share/sounds/ThanatosVent.wav" // Sound for normal mode
+#define DEFAULT_COLORS 3
+#define STRESS_COLORS 4
+
+const double default_colors[DEFAULT_COLORS][3] = {
+ {1.0, 0.91, 0.4}, //Y
+ {0.95, 0.44, 0.28}, //O
+ {0.24, 0.85, 0.38} //G
+};
+
+const double stress_colors[STRESS_COLORS][3] = {
+ {0.96, 0.0, 0.0}, //Bright
+ {0.55, 0.0, 0.0}, //Dark
+ {0.64, 0.0, 0.0}, //Muted
+ {0.35, 0.0, 0.0}, //Deep
+};
+
+// Convert Linear RGB to sRGB
+double linear_to_srgb(double value) {
+ if (value <= 0.0031308)
+ return value * 12.92;
+ else
+ return 1.055 * pow(value, 1.0 / 2.4) - 0.055;
+}
+
+// Function to interpolate between two colors in linear RGB space
+void interpolate_color(const double *color1, const double *color2, double t, char *output) {
+ unsigned int r1, g1, b1, r2, g2, b2;
+ double lr1, lg1, lb1, lr2, lg2, lb2, lr, lg, lb;
+
+ lr1 = color1[0];
+ lg1 = color1[1];
+ lb1 = color1[2];
+
+ lr2 = color2[0];
+ lg2 = color2[1];
+ lb2 = color2[2];
+
+ // Interpolate in linear RGB space
+ lr = lr1 + t * (lr2 - lr1);
+ lg = lg1 + t * (lg2 - lg1);
+ lb = lb1 + t * (lb2 - lb1);
+
+ // Convert back to sRGB
+ r1 = round(linear_to_srgb(lr) * 255);
+ g1 = round(linear_to_srgb(lg) * 255);
+ b1 = round(linear_to_srgb(lb) * 255);
+
+ // Output the interpolated color as hex
+ snprintf(output, 8, "#%02x%02x%02x", r1, g1, b1);
+}
+
+// Function to read system load
+double get_system_load() {
+ double load;
+ FILE *f = fopen("/proc/loadavg", "r");
+ if (f) {
+ fscanf(f, "%lf", &load);
+ fclose(f);
+ } else {
+ perror("Failed to read /proc/loadavg");
+ load = 0.0;
+ }
+ return load;
+}
+
+// Function to play the sound (non-blocking)
+void play_sound_background(const char *sound_file) {
+ if (fork() == 0) { // Child process
+ // Use paplay or any other sound player to play the sound
+ char command[100];
+ snprintf(command, sizeof(command), "paplay %s", sound_file);
+ system(command);
+ // If execlp fails, exit the child process
+ perror("execlp failed");
+ exit(1);
+ }
+}
+
+// Function to update breathing color
+void update_breathing_color(char *shm_ptr, double *last_load, int *in_stress_mode) {
+ static int color_index = 0;
+ static double t = 0.0;
+ char new_color[8];
+ double current_load = get_system_load();
+ int new_stress_mode = current_load > STRESS_THRESHOLD;
+
+ // Trigger sound on load crossing (up or down)
+ if (new_stress_mode != *in_stress_mode) {
+ if (new_stress_mode) {
+ play_sound_background(SOUND_FILE_STRESS); // Play stress sound
+ } else {
+ play_sound_background(SOUND_FILE_NORMAL); // Play normal sound
+ }
+ *in_stress_mode = new_stress_mode;
+ }
+
+ // Determine the current color set (default or stress)
+ const double (*current_colors)[3];
+ int num_colors;
+
+ if (new_stress_mode) {
+ current_colors = stress_colors;
+ num_colors = STRESS_COLORS;
+ } else {
+ current_colors = default_colors;
+ num_colors = DEFAULT_COLORS;
+ }
+
+ // Interpolate between the current and next color in the palette
+ interpolate_color(
+ current_colors[color_index],
+ current_colors[(color_index + 1) % num_colors],
+ t,
+ new_color
+ );
+
+ // Store the new color to shared memory (for example purposes)
+ snprintf(shm_ptr, 8, "%s", new_color);
+
+ // Update interpolation factor and color index
+ t += 0.02; // Adjust speed of interpolation here
+ if (t >= 1.0) {
+ t = 0.0;
+ color_index = (color_index + 1) % num_colors;
+ }
+}
+
+int main() {
+ struct timespec last_breathing_update = {0}, now;
+ double last_load = 0.0;
+ int in_stress_mode = 0;
+ char *shm_ptr;
+
+ // Initialize shared memory for color
+ int shm_fd = shm_open(SHM_NAME, O_CREAT | O_RDWR, 0666);
+ ftruncate(shm_fd, 8); // Set size of shared memory
+ shm_ptr = mmap(NULL, 8, PROT_READ | PROT_WRITE, MAP_SHARED, shm_fd, 0);
+
+ // Initialize breathing colors
+ clock_gettime(CLOCK_MONOTONIC, &last_breathing_update);
+
+ while (1) {
+ clock_gettime(CLOCK_MONOTONIC, &now);
+
+ // Update breathing colors
+ double elapsed_breathing = (now.tv_sec - last_breathing_update.tv_sec) * 1000.0 +
+ (now.tv_nsec - last_breathing_update.tv_nsec) / 1.0e6;
+ if (elapsed_breathing >= BREATHING_INTERVAL) {
+ update_breathing_color(shm_ptr, &last_load, &in_stress_mode);
+ last_breathing_update = now;
+ }
+
+ usleep(100000); // Sleep for 0.1 seconds to reduce CPU usage
+ }
+
+ // Clean up shared memory
+ munmap(shm_ptr, 8);
+ shm_unlink(SHM_NAME);
+
+ return 0;
+}
+
diff --git a/archive/breathing-exo-daemon/v3-shm/exo.c.oldnosync b/archive/breathing-exo-daemon/v3-shm/exo.c.oldnosync
new file mode 100644
index 0000000..29ac2a6
--- /dev/null
+++ b/archive/breathing-exo-daemon/v3-shm/exo.c.oldnosync
@@ -0,0 +1,222 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <math.h>
+#include <unistd.h>
+#include <sys/mman.h>
+#include <fcntl.h>
+#include <sys/stat.h>
+#include <time.h>
+#include <sys/types.h>
+#include <alsa/asoundlib.h>
+
+#define BREATHING_INTERVAL 90 // in milliseconds
+#define STRESS_THRESHOLD 8.0 // System load threshold for stress mode
+#define SHM_NAME "/breathing_color_shm" // Shared memory name
+#define SOUND_FILE_STRESS "/usr/local/share/sounds/AresEnraged.wav" // Sound for stress mode
+#define SOUND_FILE_NORMAL "/usr/local/share/sounds/ThanatosVent.wav" // Sound for normal mode
+
+// Default and stress colors
+const char *default_colors[] = {
+ "#ffe667", // Yellow
+ "#f27049", // Orange
+ "#3dd762" // Green
+};
+
+const char *stress_colors[] = {
+ "#f60000", // Bright Red
+ "#8e0000", // Dark Red
+ "#a10000", // Muted Red
+ "#5a0000" // Deep Crimson
+};
+
+// Convert sRGB to Linear RGB
+double srgb_to_linear(double value) {
+ if (value <= 0.04045)
+ return value / 12.92;
+ else
+ return pow((value + 0.055) / 1.055, 2.4);
+}
+
+// Convert Linear RGB to sRGB
+double linear_to_srgb(double value) {
+ if (value <= 0.0031308)
+ return value * 12.92;
+ else
+ return 1.055 * pow(value, 1.0 / 2.4) - 0.055;
+}
+
+// Function to interpolate between two colors in linear RGB space
+void interpolate_color(const char *color1, const char *color2, double t, char *output) {
+ unsigned int r1, g1, b1, r2, g2, b2;
+ double lr1, lg1, lb1, lr2, lg2, lb2, lr, lg, lb;
+
+ // Parse the input hex colors
+ sscanf(color1, "#%02x%02x%02x", &r1, &g1, &b1);
+ sscanf(color2, "#%02x%02x%02x", &r2, &g2, &b2);
+
+ // Convert sRGB to linear RGB
+ lr1 = srgb_to_linear(r1 / 255.0);
+ lg1 = srgb_to_linear(g1 / 255.0);
+ lb1 = srgb_to_linear(b1 / 255.0);
+
+ lr2 = srgb_to_linear(r2 / 255.0);
+ lg2 = srgb_to_linear(g2 / 255.0);
+ lb2 = srgb_to_linear(b2 / 255.0);
+
+ // Interpolate in linear RGB space
+ lr = lr1 + t * (lr2 - lr1);
+ lg = lg1 + t * (lg2 - lg1);
+ lb = lb1 + t * (lb2 - lb1);
+
+ // Convert back to sRGB
+ r1 = round(linear_to_srgb(lr) * 255);
+ g1 = round(linear_to_srgb(lg) * 255);
+ b1 = round(linear_to_srgb(lb) * 255);
+
+ // Output the interpolated color as hex
+ snprintf(output, 8, "#%02x%02x%02x", r1, g1, b1);
+}
+
+// Function to read system load
+double get_system_load() {
+ double load;
+ FILE *f = fopen("/proc/loadavg", "r");
+ if (f) {
+ fscanf(f, "%lf", &load);
+ fclose(f);
+ } else {
+ perror("Failed to read /proc/loadavg");
+ load = 0.0;
+ }
+ return load;
+}
+
+// Function to play the sound (once)
+void play_sound(const char *sound_file) {
+ snd_pcm_t *pcm_handle;
+ snd_pcm_hw_params_t *params;
+ snd_pcm_format_t format = SND_PCM_FORMAT_S16_LE;
+ int rate = 44100;
+ int channels = 2;
+ FILE *wav_file = fopen(sound_file, "rb");
+ if (!wav_file) {
+ perror("Failed to open WAV file");
+ return;
+ }
+
+ // Initialize ALSA PCM playback
+ if (snd_pcm_open(&pcm_handle, "default", SND_PCM_STREAM_PLAYBACK, 0) < 0) {
+ perror("Failed to open PCM device");
+ fclose(wav_file);
+ return;
+ }
+
+ snd_pcm_hw_params_alloca(&params);
+ snd_pcm_hw_params_any(pcm_handle, params);
+ snd_pcm_hw_params_set_access(pcm_handle, params, SND_PCM_ACCESS_RW_INTERLEAVED);
+ snd_pcm_hw_params_set_format(pcm_handle, params, format);
+ snd_pcm_hw_params_set_rate_near(pcm_handle, params, &rate, 0);
+ snd_pcm_hw_params_set_channels(pcm_handle, params, channels);
+ snd_pcm_hw_params(pcm_handle, params);
+
+ // Playback loop: Play the entire WAV file
+ short buffer[44100 * channels]; // 1 second of audio buffer
+ size_t bytes_read;
+
+ while ((bytes_read = fread(buffer, 1, sizeof(buffer), wav_file)) > 0) {
+ if (snd_pcm_writei(pcm_handle, buffer, bytes_read / (channels * 2)) < 0) {
+ perror("Error writing to PCM device");
+ break;
+ }
+ }
+
+ fclose(wav_file);
+ snd_pcm_drain(pcm_handle);
+ snd_pcm_close(pcm_handle);
+}
+
+// Function to update breathing color
+void update_breathing_color(char *shm_ptr, double *last_load, int *in_stress_mode) {
+ static int color_index = 0;
+ static double t = 0.0;
+ char new_color[8];
+ double current_load = get_system_load();
+ int new_stress_mode = current_load > STRESS_THRESHOLD;
+
+ // Trigger sound on load crossing (up or down)
+ if (new_stress_mode != *in_stress_mode) {
+ if (new_stress_mode) {
+ play_sound(SOUND_FILE_STRESS); // Play stress sound
+ } else {
+ play_sound(SOUND_FILE_NORMAL); // Play normal sound
+ }
+ *in_stress_mode = new_stress_mode;
+ }
+
+ // Determine the current color set (default or stress)
+ const char **current_colors;
+ int num_colors;
+
+ if (new_stress_mode) {
+ current_colors = stress_colors;
+ num_colors = sizeof(stress_colors) / sizeof(stress_colors[0]);
+ } else {
+ current_colors = default_colors;
+ num_colors = sizeof(default_colors) / sizeof(default_colors[0]);
+ }
+
+ // Interpolate between the current and next color in the palette
+ interpolate_color(
+ current_colors[color_index],
+ current_colors[(color_index + 1) % num_colors],
+ t,
+ new_color
+ );
+
+ // Store the new color to shared memory (for example purposes)
+ snprintf(shm_ptr, 8, "%s", new_color);
+
+ // Update interpolation factor and color index
+ t += 0.02; // Adjust speed of interpolation here
+ if (t >= 1.0) {
+ t = 0.0;
+ color_index = (color_index + 1) % num_colors;
+ }
+}
+
+int main() {
+ struct timespec last_breathing_update = {0}, now;
+ double last_load = 0.0;
+ int in_stress_mode = 0;
+ char *shm_ptr;
+
+ // Initialize shared memory for color
+ int shm_fd = shm_open(SHM_NAME, O_CREAT | O_RDWR, 0666);
+ ftruncate(shm_fd, 8); // Set size of shared memory
+ shm_ptr = mmap(NULL, 8, PROT_READ | PROT_WRITE, MAP_SHARED, shm_fd, 0);
+
+ // Initialize breathing colors
+ clock_gettime(CLOCK_MONOTONIC, &last_breathing_update);
+
+ while (1) {
+ clock_gettime(CLOCK_MONOTONIC, &now);
+
+ // Update breathing colors
+ double elapsed_breathing = (now.tv_sec - last_breathing_update.tv_sec) * 1000.0 +
+ (now.tv_nsec - last_breathing_update.tv_nsec) / 1.0e6;
+ if (elapsed_breathing >= BREATHING_INTERVAL) {
+ update_breathing_color(shm_ptr, &last_load, &in_stress_mode);
+ last_breathing_update = now;
+ }
+
+ usleep(100000); // Sleep for 0.1 seconds to reduce CPU usage
+ }
+
+ // Clean up shared memory
+ munmap(shm_ptr, 8);
+ shm_unlink(SHM_NAME);
+
+ return 0;
+}
+