Add "router" node type

This commit is contained in:
Vftdan 2024-08-17 11:35:36 +02:00
parent 2676b5acdf
commit a46131716e
3 changed files with 108 additions and 3 deletions

View File

@ -12,7 +12,7 @@ CPPFLAGS += $(shell pkg-config --cflags $(DEPS))
LDLIBS += $(shell pkg-config --libs $(DEPS)) LDLIBS += $(shell pkg-config --libs $(DEPS))
INTERP ?= INTERP ?=
MAIN = main 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 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
all: $(MAIN) all: $(MAIN)

View File

@ -39,14 +39,23 @@ nodes = {
namespace = "namespaces.clickpad"; namespace = "namespaces.clickpad";
}; };
}; };
select_key_events = {
type = "router";
options = {
predicates = ["key_event"];
};
};
}; };
channels = ({ channels = ({
from: ("stdin", 0); from: ("stdin", 0);
to: ("print", 0); to: ("select_key_events", 0);
}, { }, {
from: ("clickpad", 0); from: ("clickpad", 0);
to: ("print", 1); to: ("select_key_events", 1);
}, {
from: ("select_key_events", 0);
to: ("print", 0);
}); });
// vim: ft=libconfig // vim: ft=libconfig

96
nodes/router.c Normal file
View File

@ -0,0 +1,96 @@
#include "../graph.h"
#include "../module_registry.h"
typedef struct {
GraphNode as_GraphNode;
size_t length;
EventPredicateHandle * predicates;
} RouterGraphNode;
static bool
handle_event(EventPositionBase * self, EventNode * event)
{
RouterGraphNode *node = DOWNCAST(RouterGraphNode, GraphNode, DOWNCAST(GraphNode, EventPositionBase, self));
for (size_t i = 0; i < node->length; ++i) {
if (i >= node->as_GraphNode.outputs.length) {
break;
}
if (event_predicate_apply(node->predicates[i], event) == EVPREDRES_ACCEPTED) {
if (event_replicate(event, 1)) {
event->next->position = &node->as_GraphNode.outputs.elements[i]->as_EventPositionBase;
}
}
}
event_destroy(event);
return true;
}
static GraphNode *
create(GraphNodeSpecification * spec, GraphNodeConfig * config, InitializationEnvironment * env)
{
(void) config;
(void) env;
RouterGraphNode * node = T_ALLOC(1, RouterGraphNode);
if (!node) {
return NULL;
}
config_setting_t *predicates_setting = config_setting_get_member(config->options, "predicates");
if (!predicates_setting) {
free(node);
return NULL;
}
EventPredicateHandle *predicates = NULL;
size_t length = config_setting_length(predicates_setting);
if (length > 0) {
predicates = T_ALLOC(length, EventPredicateHandle);
if (!predicates) {
free(node);
return NULL;
}
for (size_t i = 0; i < length; ++i) {
predicates[i] = env_resolve_event_predicate(env, config_setting_get_elem(predicates_setting, i));
}
}
*node = (RouterGraphNode) {
.as_GraphNode = {
.as_EventPositionBase = {
.handle_event = &handle_event,
.waiting_new_event = false,
},
.specification = spec,
.inputs = EMPTY_GRAPH_CHANNEL_LIST,
.outputs = EMPTY_GRAPH_CHANNEL_LIST,
},
.length = length,
.predicates = predicates,
};
return &node->as_GraphNode;
}
static void destroy
(GraphNodeSpecification * self, GraphNode * target)
{
(void) self;
RouterGraphNode * node = DOWNCAST(RouterGraphNode, GraphNode, target);
if (node->predicates) {
free(node->predicates);
node->predicates = NULL;
node->length = 0;
}
free(target);
}
GraphNodeSpecification nodespec_router = (GraphNodeSpecification) {
.create = &create,
.destroy = &destroy,
.register_io = NULL,
.name = "router",
};
MODULE_CONSTRUCTOR(init)
{
register_graph_node_specification(&nodespec_router);
}