From 275fa01164bf8f8929f4a9327a1503bb3d3b4181 Mon Sep 17 00:00:00 2001 From: stupidcomputer Date: Mon, 15 Apr 2024 19:34:16 -0500 Subject: [PATCH] add a new module to the statusbar make it such that notifications can occur. that is, write to ~/.cache/statusbar_notification, and things appear on the bar. pretty cool right? --- builds/utils/c/status/Makefile | 2 +- builds/utils/c/status/main.c | 13 +++++-------- builds/utils/c/status/message.c | 34 +++++++++++++++++++++++++++++++++ builds/utils/c/status/message.h | 6 ++++++ 4 files changed, 46 insertions(+), 9 deletions(-) create mode 100644 builds/utils/c/status/message.c create mode 100644 builds/utils/c/status/message.h diff --git a/builds/utils/c/status/Makefile b/builds/utils/c/status/Makefile index 46a4153..15d17f7 100644 --- a/builds/utils/c/status/Makefile +++ b/builds/utils/c/status/Makefile @@ -1,6 +1,6 @@ LDFLAGS=`pkg-config --cflags --libs xcb` CFLAGS=-ggdb -fsanitize=address -main: battery.o bspwm.o time.o battstatus.o +main: battery.o bspwm.o time.o battstatus.o message.o clean: rm *.o main diff --git a/builds/utils/c/status/main.c b/builds/utils/c/status/main.c index e75d943..9395463 100644 --- a/builds/utils/c/status/main.c +++ b/builds/utils/c/status/main.c @@ -17,12 +17,14 @@ #include "battstatus.h" #include "bspwm.h" #include "time.h" +#include "message.h" struct module mods[] = { {mod_battery, "battery", "BAT0", { '\0' }}, {mod_battstatus, "battstatus", "BAT0", { '\0' }}, {mod_time, "time", "", { '\0' }}, {mod_bspwm, "bspwm", "", { '\0' }}, + {mod_message, "message", "/home/usr/.cache/statusbar_notification", { '\0' }}, }; void create_module_proc(int index, char *pipename) { @@ -59,17 +61,12 @@ void redraw() { static char NAMED_PIPE[] = "/home/usr/.cache/statusbar_pipe"; int main(void) { - char pipename[BUFFER_SIZE]; srand(time(NULL)); - strcpy(pipename, &NAMED_PIPE); - pipename[sizeof(NAMED_PIPE) - 1] = 'A' + (rand() % 26); - pipename[sizeof(NAMED_PIPE)] = 'A' + (rand() % 26); - pipename[sizeof(NAMED_PIPE) + 1] = '\0'; - mkfifo(pipename, 0666); - int fd = open(pipename, O_RDWR); + mkfifo(&NAMED_PIPE, 0666); /* it's okay if this fails */ + int fd = open(&NAMED_PIPE, O_RDWR); struct message msg; - create_module_procs(pipename); + create_module_procs(&NAMED_PIPE); for (;;) { int ret = read(fd, &msg, sizeof(msg)); diff --git a/builds/utils/c/status/message.c b/builds/utils/c/status/message.c new file mode 100644 index 0000000..fcc63c9 --- /dev/null +++ b/builds/utils/c/status/message.c @@ -0,0 +1,34 @@ +#include +#include +#include +#include +#include + +#include "message.h" +#include "common.h" + +int mod_message(char *config, char *name, char *pipename) { + struct message msg; + struct inotify_event buf; + strcpy(msg.name, name); + + int fd = inotify_init(); + int outfd = open(pipename, O_WRONLY); + + for(;;) { + int watchdesc = inotify_add_watch(fd, config, IN_MODIFY); + read(fd, &buf, sizeof(struct inotify_event)); + + /* the file's changed, so reread it */ + int filefd = open(config, O_RDONLY, 0); + int read_in = read(filefd, msg.content, sizeof(msg.content)); + msg.content[read_in - 1] = '\0'; + close(filefd); + + /* write the new */ + write(outfd, &msg, sizeof(msg)); + inotify_rm_watch(fd, watchdesc); /* not sure why this is needed */ + } + + return 0; +} diff --git a/builds/utils/c/status/message.h b/builds/utils/c/status/message.h new file mode 100644 index 0000000..e23be77 --- /dev/null +++ b/builds/utils/c/status/message.h @@ -0,0 +1,6 @@ +#ifndef STATUS_MESSAGE_H +#define STATUS_MESSAGE_H + +int mod_message(char *config, char *name, char *pipename); + +#endif