diff options
| author | Federico Angelilli <code@fedang.net> | 2024-11-16 02:32:11 +0100 |
|---|---|---|
| committer | Federico Angelilli <code@fedang.net> | 2024-11-16 02:32:11 +0100 |
| commit | 7d2b6ded634ef926cd6baf25dd8e89db7e45c7c2 (patch) | |
| tree | c2741b5c92efef0438c01a39e3bd06ae0f17d0fa | |
| parent | 1d8910e9be1811996794cedb71996a9d2dc25fbc (diff) | |
Add script blockcustom-block
| -rw-r--r-- | .gitignore | 2 | ||||
| -rw-r--r-- | comet.conf | 52 | ||||
| -rw-r--r-- | src/block.c | 2 | ||||
| -rw-r--r-- | src/blocks/date.c | 1 | ||||
| -rw-r--r-- | src/blocks/fs.c | 1 | ||||
| -rw-r--r-- | src/blocks/ram.c | 1 | ||||
| -rw-r--r-- | src/blocks/script.c | 87 | ||||
| -rw-r--r-- | src/event.c | 16 |
8 files changed, 108 insertions, 54 deletions
@@ -1,4 +1,4 @@ *.o *.old *.swp -*.bin +comet @@ -6,11 +6,17 @@ height = 40 background = #abc - blocks = date, perf, try2 + blocks = date, perf, try2, sus [jib] sus = false +[block.sus] + type = script + color = #fafaf2 + text-color = #000 + text = echo 2 + [block.try2] type=group blocks = try @@ -55,47 +61,3 @@ text = "%A %d %B %H:%M" color = #18baf2 text-color = #fff - -;[block.main] -; type = 0 -; text = CIAO -; color = #123 -; text-color = #fff -; -; event-click-left = ex -; -;;;[action.] -;;; err = true -; -;[action.ex] -; -; animation = shine -; prop = newvalue -; ;text = ${shell: tail -f /file } -; -;;[block.label] -;; type = text | group -;; x-padding = 0 -;; y-padding = 0 -;; color = col -;; line-width = 0 -;; line-color = col -;; hidden = false -;; interval = 1111 -;; min-width = 0 -;; max-width = 0 -;; -;; ; only text -;; text = ... -;; text-color = col -;; text-align = right | center | left -;; text-size = 1 -;; -;; ; only group -;; spacing = 10 -;; children = label2 another blockus -; -;[block.cpu] -; type = cpu -; interval = 1 -; text = "CPU: ${percentage}" diff --git a/src/block.c b/src/block.c index 11b725e..44a6241 100644 --- a/src/block.c +++ b/src/block.c @@ -10,6 +10,7 @@ extern const block_scheme_t block_group_scheme; extern const block_scheme_t block_ram_scheme; extern const block_scheme_t block_fs_scheme; extern const block_scheme_t block_date_scheme; +extern const block_scheme_t block_script_scheme; const block_scheme_t *block_schemes[] = { &block_text_scheme, @@ -17,6 +18,7 @@ const block_scheme_t *block_schemes[] = { &block_ram_scheme, &block_fs_scheme, &block_date_scheme, + &block_script_scheme, NULL, }; diff --git a/src/blocks/date.c b/src/blocks/date.c index adb248e..f4dbc72 100644 --- a/src/blocks/date.c +++ b/src/blocks/date.c @@ -44,6 +44,7 @@ static void block_date_clean(block_t *block) { block_date_t *date = (block_date_t *)block; free(date->format); + free(date->block.text); } static bool block_date_validate(block_t *block, const block_scheme_t *scheme) diff --git a/src/blocks/fs.c b/src/blocks/fs.c index 45ea0a3..7cd3bc8 100644 --- a/src/blocks/fs.c +++ b/src/blocks/fs.c @@ -67,6 +67,7 @@ static void block_fs_clean(block_t *block) block_fs_t *fs = (block_fs_t *)block; free(fs->format); free(fs->path); + free(fs->block.text); } static bool block_fs_validate(block_t *block, const block_scheme_t *scheme) diff --git a/src/blocks/ram.c b/src/blocks/ram.c index 158583c..5ea7760 100644 --- a/src/blocks/ram.c +++ b/src/blocks/ram.c @@ -77,6 +77,7 @@ static void block_ram_clean(block_t *block) { block_ram_t *ram = (block_ram_t *)block; free(ram->format); + free(ram->block.text); } static bool block_ram_validate(block_t *block, const block_scheme_t *scheme) diff --git a/src/blocks/script.c b/src/blocks/script.c new file mode 100644 index 0000000..fa3a9d9 --- /dev/null +++ b/src/blocks/script.c @@ -0,0 +1,87 @@ +#include <string.h> +#include <stdio.h> +#include <assert.h> +#include <stdlib.h> + +#include "../block.h" + +#include "../any_log.h" + +typedef struct { + block_text_t block; + char *script; +} block_script_t; + +static const struct timespec block_script_interval = { + .tv_sec = 30, + .tv_nsec = 0, +}; + +static void block_script_update(block_t *block) +{ + block_script_t *script = (block_script_t *)block; + + FILE *output = popen(script->script, "r"); + if (output == NULL) { + log_value_debug("Failed to execute a script", + "s:label", block->label, + "s:script", script->script, + "i:errno", errno); + return; + } + + char buffer[128] = { 0 }; + fgets(buffer, sizeof(buffer), output); + pclose(output); + + log_value_debug("Executed a script", + "s:label", block->label, + "s:script", script->script); + + free(script->block.text); + script->block.text = strcopy(buffer); + assert(script->block.text != NULL); +} + +static block_t *block_script_alloc(const block_scheme_t *scheme) +{ + block_t *block = calloc(1, sizeof(block_script_t)); + block->type = BLOCK_TEXT; + block->update_interval = block_script_interval; + block->update_fn = block_script_update; + return block; +} + +static void block_script_clean(block_t *block) +{ + block_script_t *script = (block_script_t *)block; + free(script->script); + free(script->block.text); +} + +static bool block_script_validate(block_t *block, const block_scheme_t *scheme) +{ + block_script_t *script = (block_script_t *)block; + + if (script->block.text == NULL) { + log_error("Block '%s' requires key '%s'", block->label, "text"); + return false; + } + + script->script = script->block.text; + script->block.text = NULL; + return true; +} + +static const config_entry_t block_script_entries[] = { + { 0 }, +}; + +const block_scheme_t block_script_scheme = { + .name = "script", + .entries = block_script_entries, + .alloc_fn = block_script_alloc, + .clean_fn = block_script_clean, + .validate_fn = block_script_validate, + .resolve_fn = NULL, +}; diff --git a/src/event.c b/src/event.c index 3783029..1aaf603 100644 --- a/src/event.c +++ b/src/event.c @@ -37,14 +37,14 @@ static bool event_click(layout_t *layout, event_t event) : NULL; log_value_debug("Block was clicked", - "s:type", even_type_to_string(event.type), - "i:x", event.x, - "i:y", event.y, - "s:block_label", layout->block->label, - "i:block_x", layout->x, - "i:block_y", layout->y, - "i:block_width", layout->width, - "i:block_height", layout->height, + "s:event_type", even_type_to_string(event.type), + "i:event_x", event.x, + "i:event_y", event.y, + "s:label", layout->block->label, + "i:x", layout->x, + "i:y", layout->y, + "i:width", layout->width, + "i:height", layout->height, "b:callback", event_fn != NULL); if (event_fn != NULL) { |
