summaryrefslogtreecommitdiff
path: root/src/somebarinfo.c
diff options
context:
space:
mode:
authorSam Light <sam@lightscale.co.uk>2025-09-10 19:37:28 +0100
committerSam Light <sam@lightscale.co.uk>2025-09-10 19:37:28 +0100
commitd9b456d738e5013d48f41f5e6315ef7c22d6acf0 (patch)
tree2e3722f5697cbf883c4b16edc87726b9e510bce1 /src/somebarinfo.c
Initial commit
Diffstat (limited to 'src/somebarinfo.c')
-rw-r--r--src/somebarinfo.c86
1 files changed, 86 insertions, 0 deletions
diff --git a/src/somebarinfo.c b/src/somebarinfo.c
new file mode 100644
index 0000000..0dc2d50
--- /dev/null
+++ b/src/somebarinfo.c
@@ -0,0 +1,86 @@
+#include "signal.h"
+#include "text.h"
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <string.h>
+
+#define SOMEBAR_CMD "status"
+
+struct state {
+ FILE * file;
+ struct text * text;
+};
+
+static int
+init_state(struct state * s)
+{
+ char * dir = getenv("XDG_RUNTIME_DIR");
+ char * file = "/somebar-0";
+ size_t dir_len = strlen(dir);
+ size_t file_len = strlen(file);
+ size_t path_len = dir_len + file_len + 1;
+ char path[path_len];
+
+ memset(path, '\0', path_len);
+
+ strncat(path, dir, dir_len);
+ strncat(path, file, file_len);
+
+ s->text = NULL;
+
+ s->file = fopen(path, "a");
+ if(s->file == NULL) return -1;
+
+ s->text = text_new();
+ return 0;
+}
+
+static void
+deinit_state(struct state * s)
+{
+ if(s->file) fclose(s->file);
+ if(s->text) text_free(s->text);
+}
+
+static void
+update_status(struct state * s)
+{
+ const size_t
+ cmd_len = strlen(SOMEBAR_CMD),
+ msg_len = TEXT_SIZE + cmd_len + 3;
+ char msg[msg_len];
+
+ text_update(s->text);
+
+ snprintf(msg, msg_len, "%s %s\n", SOMEBAR_CMD, s->text->value);
+
+ fputs(msg, s->file);
+ fflush(s->file);
+}
+
+static void
+run_loop(struct state * s)
+{
+ while(signal_running) {
+ update_status(s);
+ usleep(1000 * 800);
+ }
+}
+
+int main(void)
+{
+ int rc;
+ struct state s;
+
+ signal_setup_actions();
+
+ rc = init_state(&s);
+
+ if(rc == 0) run_loop(&s);
+
+ deinit_state(&s);
+
+ return 0;
+}