Compare commits

...

4 Commits

Author SHA1 Message Date
Vftdan 9f876162e5 Add "tee" node type 2024-08-15 22:12:23 +02:00
Vftdan deea29898a Fix linked list bug in event_replicate() 2024-08-15 22:10:55 +02:00
Vftdan c2a723098f Add global event list destructor 2024-08-15 21:26:21 +02:00
Vftdan 5875575d0b Add FullConfig destructor 2024-08-15 21:25:18 +02:00
7 changed files with 96 additions and 2 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 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) all: $(MAIN)

View File

@ -135,3 +135,21 @@ load_config(const config_setting_t *config_root, FullConfig *config)
config->channels = load_channels_section(channel_config); config->channels = load_channels_section(channel_config);
return true; return true;
} }
void
reset_config(FullConfig *config)
{
if (!config) {
return;
}
if (config->nodes.items) {
free(config->nodes.items);
config->nodes.items = NULL;
config->nodes.length = 0;
}
if (config->channels.items) {
free(config->channels.items);
config->channels.items = NULL;
config->channels.length = 0;
}
}

View File

@ -33,5 +33,6 @@ typedef struct {
} FullConfig; } FullConfig;
bool load_config(const config_setting_t *config_root, FullConfig *config); bool load_config(const config_setting_t *config_root, FullConfig *config);
void reset_config(FullConfig *config);
#endif /* end of include guard: CONFIG_H_ */ #endif /* end of include guard: CONFIG_H_ */

View File

@ -25,7 +25,7 @@ event_replicate(EventNode * source, size_t count)
replica->data.modifiers = modifier_set_copy(source->data.modifiers); replica->data.modifiers = modifier_set_copy(source->data.modifiers);
replica->prev = source; replica->prev = source;
replica->next = source->next; replica->next = source->next;
source->next->next->prev = replica; source->next->prev = replica;
source->next = replica; source->next = replica;
} }
return i; return i;
@ -72,3 +72,15 @@ event_destroy(EventNode * self)
self->next = NULL; self->next = NULL;
free(self); free(self);
} }
void event_destroy_all()
{
EventNode *ev;
while ((ev = FIRST_EVENT) != &END_EVENTS) {
event_destroy(ev);
if (ev == FIRST_EVENT) {
fprintf(stderr, "Broken doubly linked event list invariant\n");
abort();
}
}
}

View File

@ -47,5 +47,6 @@ extern EventNode END_EVENTS;
size_t event_replicate(EventNode * source, size_t count); size_t event_replicate(EventNode * source, size_t count);
EventNode * event_create(const EventData * content); EventNode * event_create(const EventData * content);
void event_destroy(EventNode * self); void event_destroy(EventNode * self);
void event_destroy_all();
#endif /* end of include guard: EVENTS_H_ */ #endif /* end of include guard: EVENTS_H_ */

2
main.c
View File

@ -82,9 +82,11 @@ main(int argc, char ** argv)
for (ssize_t i = loaded_config.nodes.length - 1; i >= 0; --i) { for (ssize_t i = loaded_config.nodes.length - 1; i >= 0; --i) {
graph_node_delete(nodes[i]); graph_node_delete(nodes[i]);
} }
event_destroy_all();
free(channels); free(channels);
free(nodes); free(nodes);
reset_config(&loaded_config);
config_destroy(&config_tree); config_destroy(&config_tree);
io_subscription_list_deinit(&state.wait_output); io_subscription_list_deinit(&state.wait_output);

60
nodes/tee.c Normal file
View File

@ -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);
}