From 226c6b5bf79912b657c7cb4c5a679891030fa453 Mon Sep 17 00:00:00 2001 From: Federico Angelilli Date: Thu, 11 Jul 2024 13:08:26 +0200 Subject: Start working on block config --- src/block.c | 14 +++++++++++++ src/block.h | 13 +++++------- src/comet.c | 66 ++++++++++++++++++++++++++++++------------------------------ src/config.c | 9 +++++++++ src/config.h | 3 +++ src/event.c | 2 +- src/layout.c | 6 ++++-- src/util.h | 17 ++++++++++++++++ 8 files changed, 86 insertions(+), 44 deletions(-) diff --git a/src/block.c b/src/block.c index 3d558d1..e739ceb 100644 --- a/src/block.c +++ b/src/block.c @@ -23,3 +23,17 @@ void block_update(block_t *block) block_update(&block->group.children[i]); } } + +void block_free(block_t *block) +{ + free(block->label); + + if (block->type == BLOCK_TEXT) { + free(block->text.text); + } else if (block->type == BLOCK_GROUP) { + for (int i = 0; i < block->group.n_children; i++) + block_free(&block->group.children[i]); + + free(block->group.children); + } +} diff --git a/src/block.h b/src/block.h index 1a91596..b65fa6d 100644 --- a/src/block.h +++ b/src/block.h @@ -6,12 +6,7 @@ #include #include "event.h" - -// Color representation normalized to [0, 1] -// -typedef struct { - double r, g, b, a; -} color_t; +#include "util.h" // Element or text alignment // @@ -51,11 +46,11 @@ struct block { struct timespec update_last; block_update_t update_cb; block_event_t event_cb; - const char *label; + char *label; block_type_t type; union { struct { - const char *text; + char *text; color_t text_color; align_t text_align; int text_size; @@ -70,4 +65,6 @@ struct block { void block_update(block_t *block); +void block_free(block_t *block); + #endif diff --git a/src/comet.c b/src/comet.c index 0a16426..66a90c9 100644 --- a/src/comet.c +++ b/src/comet.c @@ -85,37 +85,37 @@ int main(int argc, char **argv) window_resize(&window, config.width, config.height); window_move(&window, x_padding, y_padding); - block_t blocks[2] = { - { - .color = { 0.2, 0.2, 0.2, 1 }, - .type = BLOCK_TEXT, - .text = { - .text = "A", - .text_color = { 0.8, 0.9, 0.3, 1 }, - }, - }, - { - .color = { 0.2, 0.2, 0.2, 1 }, - .type = BLOCK_TEXT, - .min_width = 100, - .text = { - .text = "B", - .text_color = { 0.8, 0.9, 0.3, 1 }, - }, - }, - }; - - block_t top = { - .hidden = false, - .color = { 0.3, 0.2, 0.5, 1 }, - .min_width = config.width, - .type = BLOCK_GROUP, - .group = { - .spacing = 10, - .n_children = 2, - .children = blocks, - }, - }; + //block_t blocks[2] = { + // { + // .color = { 0.2, 0.2, 0.2, 1 }, + // .type = BLOCK_TEXT, + // .text = { + // .text = "A", + // .text_color = { 0.8, 0.9, 0.3, 1 }, + // }, + // }, + // { + // .color = { 0.2, 0.2, 0.2, 1 }, + // .type = BLOCK_TEXT, + // .min_width = 100, + // .text = { + // .text = "B", + // .text_color = { 0.8, 0.9, 0.3, 1 }, + // }, + // }, + //}; + + //block_t top = { + // .hidden = false, + // .color = { 0.3, 0.2, 0.5, 1 }, + // .min_width = config.width, + // .type = BLOCK_GROUP, + // .group = { + // .spacing = 10, + // .n_children = 2, + // .children = blocks, + // }, + //}; // TODO: Allow ondemand rendering struct timespec rate, start, end, diff; @@ -127,10 +127,10 @@ int main(int argc, char **argv) while (true) { timespec_get(&start, TIME_UTC); - block_update(&top); + block_update(&config.block); layout_t layout; - layout_init(&layout, &top, info); + layout_init(&layout, &config.block, info); event_dispatch(&display, &layout); diff --git a/src/config.c b/src/config.c index 82d6231..deae82c 100644 --- a/src/config.c +++ b/src/config.c @@ -160,6 +160,10 @@ static bool config_entry(config_t *config, int line, const char *section, const void config_init(config_t *config) { const config_t config_default = { + .block = { + .color = color_rgb(100, 100, 100), + .type = BLOCK_GROUP, + }, .font = "monospace 10", .monitor = NULL, .height = 50, @@ -171,6 +175,8 @@ void config_init(config_t *config) // NOTE: Strings must be copied config->font = strcopy(config_default.font); config->monitor = strcopy(config_default.monitor); + + config->block.min_width = config->block.max_width = config->width; } void config_read(config_t *config, FILE *file) @@ -209,10 +215,13 @@ void config_read(config_t *config, FILE *file) if (errors > 0) log_panic("Config file contained %d errors", errors); + + config->block.min_width = config->block.max_width = config->width; } void config_free(config_t *config) { free(config->font); free(config->monitor); + block_free(&config->block); } diff --git a/src/config.h b/src/config.h index 3596882..007393b 100644 --- a/src/config.h +++ b/src/config.h @@ -3,7 +3,10 @@ #include +#include "block.h" + typedef struct { + block_t block; char *font; char *monitor; unsigned int height; diff --git a/src/event.c b/src/event.c index 293dec9..0942fa1 100644 --- a/src/event.c +++ b/src/event.c @@ -70,7 +70,7 @@ void event_dispatch(display_t *display, layout_t *layout) "s:name", name, "s:extension", extension ? extension : "none", "s:major", major, - "s:minor", minor ? minor : "none" + "s:minor", minor ? minor : "none", "u:resource", (unsigned int)error->resource_id, "u:sequence", (unsigned int)error->sequence); diff --git a/src/layout.c b/src/layout.c index c5969fb..ce6b281 100644 --- a/src/layout.c +++ b/src/layout.c @@ -32,8 +32,10 @@ void layout_init(layout_t *layout, block_t *block, layout_info_t info) layout->n_children++; } - layout_t *last = &layout->children[layout->n_children - 1]; - layout->width = last->x + last->width - x_offset; + if (layout->n_children > 0) { + layout_t *last = &layout->children[layout->n_children - 1]; + layout->width = last->x + last->width - x_offset; + } } else if (block->type == BLOCK_TEXT) { layout->pl = pango_layout_new(info.context); diff --git a/src/util.h b/src/util.h index 06258e1..e7a7863 100644 --- a/src/util.h +++ b/src/util.h @@ -5,6 +5,23 @@ #include #include +// Color representation normalized to [0, 1] +// +typedef struct { + double r, g, b, a; +} color_t; + +static inline color_t color_rgba(int r, int g, int b, int a) +{ + color_t color = { r / 255.0, g / 255.0, b / 255.0, a / 255.0 }; + return color; +} + +static inline color_t color_rgb(int r, int g, int b) +{ + return color_rgba(r, g, b, 255); +} + struct timespec timespec_diff(struct timespec a, struct timespec b); bool timespec_greater(struct timespec a, struct timespec b); -- cgit v1.2.3