From 12af4b35c7c3534aacc0bdbd879d31536fccdfb9 Mon Sep 17 00:00:00 2001 From: Vftdan Date: Mon, 19 Aug 2024 11:01:19 +0200 Subject: [PATCH] Add "differentiate" node type Can be used to transform absolute movement to relative movement --- Makefile | 2 +- nodes/differentiate.c | 84 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 85 insertions(+), 1 deletion(-) create mode 100644 nodes/differentiate.c diff --git a/Makefile b/Makefile index 789dfe1..ccd6710 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 event_code_names.o hash_table.o module_registry.o event_predicate.o nodes/getchar.o nodes/print.o nodes/evdev.o nodes/tee.o nodes/router.o nodes/modifiers.o nodes/modify_predicate.o nodes/uinput.o nodes/assign.o +OBJS = main.o events.o processing.o graph.o config.o event_code_names.o hash_table.o module_registry.o event_predicate.o nodes/getchar.o nodes/print.o nodes/evdev.o nodes/tee.o nodes/router.o nodes/modifiers.o nodes/modify_predicate.o nodes/uinput.o nodes/assign.o nodes/differentiate.o all: $(MAIN) diff --git a/nodes/differentiate.c b/nodes/differentiate.c new file mode 100644 index 0000000..5546ca7 --- /dev/null +++ b/nodes/differentiate.c @@ -0,0 +1,84 @@ +#include "../graph.h" +#include "../module_registry.h" + +typedef struct { + GraphNode as_GraphNode; + int64_t previous; +} DifferentiateGraphNode; + +static bool +handle_event(EventPositionBase * self, EventNode * event) +{ + DifferentiateGraphNode *node = DOWNCAST(DifferentiateGraphNode, GraphNode, DOWNCAST(GraphNode, EventPositionBase, self)); + size_t count = node->as_GraphNode.outputs.length; + if (!count) { + event_destroy(event); + return true; + } + + int64_t current = event->data.payload; + event->data.payload = current - node->previous; + node->previous = current; + + if (count > 1) { + count = event_replicate(event, count - 1) + 1; + } + for (size_t i = 0; i < count; ++i) { + event->position = &node->as_GraphNode.outputs.elements[i]->as_EventPositionBase; + event = event->next; + } + return true; +} + +static GraphNode * +create(GraphNodeSpecification * spec, GraphNodeConfig * config, InitializationEnvironment * env) +{ + (void) config; + (void) env; + DifferentiateGraphNode * node = T_ALLOC(1, DifferentiateGraphNode); + if (!node) { + return NULL; + } + + int64_t initial = 0; + if (config->options) { + // FIXME check config->options for NULL in all node types + initial = env_resolve_constant(env, config_setting_get_member(config->options, "initial")); + } + + *node = (DifferentiateGraphNode) { + .as_GraphNode = { + .as_EventPositionBase = { + .handle_event = &handle_event, + .waiting_new_event = false, + }, + .specification = spec, + .inputs = EMPTY_GRAPH_CHANNEL_LIST, + .outputs = EMPTY_GRAPH_CHANNEL_LIST, + }, + .previous = initial, + }; + return &node->as_GraphNode; +} + +static void destroy +(GraphNodeSpecification * self, GraphNode * target) +{ + (void) self; + free(target); +} + +GraphNodeSpecification nodespec_differentiate = (GraphNodeSpecification) { + .create = &create, + .destroy = &destroy, + .register_io = NULL, + .name = "differentiate", + .documentation = "Subtracts the previous event payload from the current one\nAccepts events on any connector\nSends events on all connectors" + "\nOption 'initial' (optional): the value to subtract from the first event payload" + , +}; + +MODULE_CONSTRUCTOR(init) +{ + register_graph_node_specification(&nodespec_differentiate); +}