summaryrefslogtreecommitdiff
path: root/archive/breathing-exo-daemon/display/display-shm-x.c
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/display/display-shm-x.c
parent04cfeeb799b4ee6ac990e5d6e1b5302251133d77 (diff)
parente61da07522a060da98fa3a56db3d0360469b26cf (diff)
Resolved conflict, indulging new file layout
Diffstat (limited to 'archive/breathing-exo-daemon/display/display-shm-x.c')
-rw-r--r--archive/breathing-exo-daemon/display/display-shm-x.c94
1 files changed, 94 insertions, 0 deletions
diff --git a/archive/breathing-exo-daemon/display/display-shm-x.c b/archive/breathing-exo-daemon/display/display-shm-x.c
new file mode 100644
index 0000000..2695fd0
--- /dev/null
+++ b/archive/breathing-exo-daemon/display/display-shm-x.c
@@ -0,0 +1,94 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include <sys/mman.h>
+#include <fcntl.h>
+#include <sys/stat.h>
+#include <X11/Xlib.h>
+
+#define SHM_NAME "/breathing_color_shm" // Shared memory name (same as in exo.c)
+#define COLOR_SIZE 8 // Size for one color (e.g., #RRGGBB)
+
+int main() {
+ // Open shared memory object
+ int shm_fd = shm_open(SHM_NAME, O_RDONLY, 0666);
+ if (shm_fd == -1) {
+ perror("Failed to open shared memory");
+ return 1;
+ }
+
+ // Map shared memory into the process's address space
+ char *shm_ptr = mmap(NULL, COLOR_SIZE, PROT_READ, MAP_SHARED, shm_fd, 0);
+ if (shm_ptr == MAP_FAILED) {
+ perror("Failed to map shared memory");
+ close(shm_fd);
+ return 1;
+ }
+
+ // Initialize X11
+ Display *display = XOpenDisplay(NULL);
+ if (display == NULL) {
+ perror("Unable to open X display");
+ munmap(shm_ptr, COLOR_SIZE);
+ close(shm_fd);
+ return 1;
+ }
+
+ // Create a window
+ int screen = DefaultScreen(display);
+ Window window = XCreateSimpleWindow(display, RootWindow(display, screen), 0, 0, 400, 400, 1, BlackPixel(display, screen), BlackPixel(display, screen));
+
+ // Set window properties
+ XStoreName(display, window, "Breathing Color Display");
+ XMapWindow(display, window);
+
+ // Set the window's background color initially
+ XFlush(display);
+
+ // Start reading colors and updating the window
+ while (1) {
+ // Read the current color from shared memory
+ XColor color;
+ if (XParseColor(display, DefaultColormap(display, screen), shm_ptr, &color) == 0) {
+ fprintf(stderr, "Invalid color in shared memory: %s\n", shm_ptr);
+ continue;
+ }
+
+ // Allocate the color in the X server
+ if (XAllocColor(display, DefaultColormap(display, screen), &color) == 0) {
+ fprintf(stderr, "Failed to allocate color: %s\n", shm_ptr);
+ continue;
+ }
+
+ // Change the background color of the window
+ XSetWindowBackground(display, window, color.pixel);
+ XClearWindow(display, window);
+
+ // Process X events
+ XEvent event;
+ while (XPending(display)) {
+ XNextEvent(display, &event);
+ if (event.type == ClientMessage) {
+ // Exit on close event (or any client message)
+ XDestroyWindow(display, window);
+ XCloseDisplay(display);
+ munmap(shm_ptr, COLOR_SIZE);
+ close(shm_fd);
+ return 0;
+ }
+ }
+
+ // Sleep a little before checking for the next color update
+ usleep(100000); // 0.1 seconds
+ }
+
+ // Clean up
+ XDestroyWindow(display, window);
+ XCloseDisplay(display);
+ munmap(shm_ptr, COLOR_SIZE);
+ close(shm_fd);
+
+ return 0;
+}
+