From 915b623c32d3edb881d248153706338e7ae9054c Mon Sep 17 00:00:00 2001 From: ne_mene Date: Sun, 1 Mar 2026 15:54:19 +0100 Subject: create, layout and example bar --- Makefile | 4 +-- examplebar.sh | 4 +++ src/bardata.c | 79 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/bardata.h | 8 ++++++ src/pipe.c | 19 +++++++++++--- src/pipe.h | 4 +-- 6 files changed, 111 insertions(+), 7 deletions(-) create mode 100755 examplebar.sh create mode 100644 src/bardata.c create mode 100644 src/bardata.h diff --git a/Makefile b/Makefile index 6fdd962..bb47057 100644 --- a/Makefile +++ b/Makefile @@ -4,8 +4,8 @@ LDFLAGS+=-lX11 -lxcb TSAR_OUT=tsarbar -HEADERS=src/pipe.h -SRC=src/tsar.c src/pipe.c +HEADERS=src/pipe.h src/bardata.h +SRC=src/tsar.c src/pipe.c src/bardata.c .PHONY: default run clean diff --git a/examplebar.sh b/examplebar.sh new file mode 100755 index 0000000..b11383d --- /dev/null +++ b/examplebar.sh @@ -0,0 +1,4 @@ +#! /bin/sh + +./tsarc create test_comp +./tsarc layout -left test_comp -center test_comp -right test_comp test_comp diff --git a/src/bardata.c b/src/bardata.c new file mode 100644 index 0000000..a35f3b5 --- /dev/null +++ b/src/bardata.c @@ -0,0 +1,79 @@ +#include +#include + +#include "bardata.h" +#include "pipe.h" + +typedef enum { + SIDE_LEFT, + SIDE_CENTER, + SIDE_RIGHT, +} comp_side_t; + +typedef struct { +} color_t; + +typedef struct { + char name[16]; + char data[64]; + color_t fg, bg; +} comp_t; + +// Global state, oh naur D: +comp_t comps[32]; +size_t comps_size = 0; +comp_t *comps_left[16], *comps_right[16], *comps_center[16]; +size_t left_size = 0, right_size = 0, center_size = 0; + +void new_component(char *name) { + strcpy(comps[comps_size].name, name); + comps_size++; +} + +comp_t *find_comp(char *name) { + for (size_t i = 0; i < comps_size; i++) { + if (strcmp(comps[i].name, name) == 0) { + return &comps[i]; + } + } + return NULL; +} + +void add_component(comp_side_t side, char *name) { + comp_t *comp = find_comp(name); + if (side == SIDE_LEFT) { + comps_left[left_size] = comp; + left_size++; + return; + } + if (side == SIDE_RIGHT) { + comps_right[right_size] = comp; + right_size++; + return; + } + if (side == SIDE_CENTER) { + comps_center[center_size] = comp; + center_size++; + return; + } +} + +void set_layout(char argv[MAX_ARGS][MAX_ARG_LEN], int argc) { + comp_side_t side = SIDE_LEFT; + + for (int i = 1; i < argc; i++) { + if (strcmp(argv[i], "-left") == 0) { + side = SIDE_LEFT; + continue; + } + if (strcmp(argv[i], "-center") == 0) { + side = SIDE_CENTER; + continue; + } + if (strcmp(argv[i], "-right") == 0) { + side = SIDE_RIGHT; + continue; + } + add_component(side, argv[i]); + } +} diff --git a/src/bardata.h b/src/bardata.h new file mode 100644 index 0000000..0c4028b --- /dev/null +++ b/src/bardata.h @@ -0,0 +1,8 @@ +#ifndef BAR_H +#define BAR_H +#include "pipe.h" + +void new_component(char *name); +void set_layout(char argv[MAX_ARGS][MAX_ARG_LEN], int argc); + +#endif diff --git a/src/pipe.c b/src/pipe.c index 88006a4..edce5de 100644 --- a/src/pipe.c +++ b/src/pipe.c @@ -1,10 +1,14 @@ -#include "pipe.h" + #include #include #include #include +#include #include +#include "pipe.h" +#include "bardata.h" + FILE *named_pipe; void init_pipe() { @@ -25,9 +29,9 @@ void await_change() { printf("Received: %s\n", buff); int argc = 0, chr_on = 0, i = 0; - char argv[4][256]; + char argv[MAX_ARGS][MAX_ARG_LEN]; - while (argc < 4) { + while (argc < MAX_ARGS) { char chr = buff[i]; if (chr == '\0') { break; @@ -58,4 +62,13 @@ void make_change(char argv[MAX_ARGS][MAX_ARG_LEN], int argc) { for (int i = 0; i < argc; i++) { printf("Arg %d: %s\n", i + 1, argv[i]); } + + char *cmd = argv[0]; + if (strcmp(cmd, "create") == 0) { + new_component(argv[1]); + return; + } + if (strcmp(cmd, "layout") == 0) { + set_layout(argv, argc); + } } diff --git a/src/pipe.h b/src/pipe.h index 3f81432..e0e6301 100644 --- a/src/pipe.h +++ b/src/pipe.h @@ -3,8 +3,8 @@ #include #define PIPE_PATH "/tmp/tsarc.pipe" -#define MAX_ARG_LEN 256 -#define MAX_ARGS 4 +#define MAX_ARG_LEN 128 +#define MAX_ARGS 16 void init_pipe(); void await_change(); -- cgit v1.3-2-g0d8e