From 9f876162e5e0f979dc93eb7f7e9de83b86d148bd Mon Sep 17 00:00:00 2001 From: Vftdan Date: Thu, 15 Aug 2024 22:12:23 +0200 Subject: [PATCH] Add "tee" node type --- Makefile | 2 +- nodes/tee.c | 60 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+), 1 deletion(-) create mode 100644 nodes/tee.c diff --git a/Makefile b/Makefile index 7c7e4df..aa31924 100644 --- a/Makefile +++ b/Makefile @@ -12,7 +12,7 @@ CPPFLAGS += $(shell pkg-config --cflags $(DEPS)) LDLIBS += $(shell pkg-config --libs $(DEPS)) INTERP ?= MAIN = main -OBJS = main.o events.o processing.o graph.o config.o hash_table.o module_registry.o nodes/getchar.o nodes/print.o nodes/evdev.o +OBJS = main.o events.o processing.o graph.o config.o hash_table.o module_registry.o nodes/getchar.o nodes/print.o nodes/evdev.o nodes/tee.o all: $(MAIN) diff --git a/nodes/tee.c b/nodes/tee.c new file mode 100644 index 0000000..a02f8af --- /dev/null +++ b/nodes/tee.c @@ -0,0 +1,60 @@ +#include "../graph.h" +#include "../module_registry.h" + +static bool +handle_event(EventPositionBase * self, EventNode * event) +{ + GraphNode *node = DOWNCAST(GraphNode, EventPositionBase, self); + size_t count = node->outputs.length; + if (!count) { + event_destroy(event); + return true; + } + if (count > 1) { + count = event_replicate(event, count - 1) + 1; + } + for (size_t i = 0; i < count; ++i) { + event->position = &node->outputs.elements[i]->as_EventPositionBase; + event = event->next; + } + return true; +} + +static GraphNode * +create(GraphNodeSpecification * spec, GraphNodeConfig * config) +{ + (void) config; + GraphNode * node = T_ALLOC(1, GraphNode); + if (!node) { + return node; + } + *node = (GraphNode) { + .as_EventPositionBase = { + .handle_event = &handle_event, + .waiting_new_event = false, + }, + .specification = spec, + .inputs = EMPTY_GRAPH_CHANNEL_LIST, + .outputs = EMPTY_GRAPH_CHANNEL_LIST, + }; + return node; +} + +static void destroy +(GraphNodeSpecification * self, GraphNode * target) +{ + (void) self; + free(target); +} + +GraphNodeSpecification nodespec_tee = (GraphNodeSpecification) { + .create = &create, + .destroy = &destroy, + .register_io = NULL, + .name = "tee", +}; + +MODULE_CONSTRUCTOR(init) +{ + register_graph_node_specification(&nodespec_tee); +}