Compare commits

..

2 Commits

12 changed files with 155 additions and 19 deletions

View File

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

View File

@ -449,3 +449,15 @@ resolve_event_predicate(EventPredicateHandleRegistry * registry, const ConstantR
}
return load_single_predicate(setting, registry, constants);
}
long long
env_resolve_constant_or(InitializationEnvironment * env, const config_setting_t * setting, long long dflt)
{
return resolve_constant_or(&env->constants, setting, dflt);
}
EventPredicateHandle
env_resolve_event_predicate(InitializationEnvironment * env, const config_setting_t * setting)
{
return resolve_event_predicate(&env->predicates, &env->constants, setting);
}

View File

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

View File

@ -31,12 +31,21 @@ typedef struct {
typedef TYPED_HASH_TABLE(long long) ConstantRegistry;
typedef TYPED_HASH_TABLE(EventPredicateHandle) EventPredicateHandleRegistry;
typedef struct initialization_environment InitializationEnvironment;
typedef struct {
GraphNodeConfigSection nodes;
GraphChannelConfigSection channels;
ConstantRegistry constants;
EventPredicateHandleRegistry predicates;
union {
struct {
ConstantRegistry constants;
EventPredicateHandleRegistry predicates;
};
struct initialization_environment {
const ConstantRegistry constants;
EventPredicateHandleRegistry predicates;
} environment;
};
} FullConfig;
bool load_config(const config_setting_t *config_root, FullConfig *config);
@ -44,10 +53,20 @@ void reset_config(FullConfig *config);
long long resolve_constant_or(const ConstantRegistry * registry, const config_setting_t * setting, long long dflt);
EventPredicateHandle resolve_event_predicate(EventPredicateHandleRegistry * registry, const ConstantRegistry * constants, const config_setting_t * setting);
// These are not inline to make non-breaking ABI changes
long long env_resolve_constant_or(InitializationEnvironment * env, const config_setting_t * setting, long long dflt);
EventPredicateHandle env_resolve_event_predicate(InitializationEnvironment * env, const config_setting_t * setting);
__attribute__((unused)) inline static long long
resolve_constant(const ConstantRegistry * registry, const config_setting_t * setting)
{
return resolve_constant_or(registry, setting, 0);
}
__attribute__((unused)) inline static long long
env_resolve_constant(InitializationEnvironment * env, const config_setting_t * setting)
{
return env_resolve_constant_or(env, setting, 0);
}
#endif /* end of include guard: CONFIG_H_ */

View File

@ -88,12 +88,12 @@ graph_channel_init(GraphChannel * ch, GraphNode * start, size_t start_idx, Graph
}
GraphNode *
graph_node_new(GraphNodeSpecification * spec, GraphNodeConfig * config, const ConstantRegistry * constants)
graph_node_new(GraphNodeSpecification * spec, GraphNodeConfig * config, InitializationEnvironment * env)
{
if (!spec || !spec->create) {
return NULL;
}
return spec->create(spec, config, constants);
return spec->create(spec, config, env);
}
void

View File

@ -29,14 +29,14 @@ struct graph_channel {
};
struct graph_node_specification {
GraphNode * (*create)(GraphNodeSpecification * self, GraphNodeConfig * config, const ConstantRegistry * constants);
GraphNode * (*create)(GraphNodeSpecification * self, GraphNodeConfig * config, InitializationEnvironment * env);
void (*destroy)(GraphNodeSpecification * self, GraphNode * target);
void (*register_io)(GraphNodeSpecification * self, GraphNode * target, ProcessingState * state);
char *name;
};
void graph_channel_init(GraphChannel * ch, GraphNode * start, size_t start_idx, GraphNode * end, size_t end_idx);
GraphNode *graph_node_new(GraphNodeSpecification * spec, GraphNodeConfig * config, const ConstantRegistry * constants);
GraphNode *graph_node_new(GraphNodeSpecification * spec, GraphNodeConfig * config, InitializationEnvironment * env);
void graph_node_delete(GraphNode * self);
void graph_node_register_io(GraphNode * self, ProcessingState * state);
void graph_channel_list_init(GraphChannelList * lst);

2
main.c
View File

@ -39,7 +39,7 @@ main(int argc, char ** argv)
fprintf(stderr, "Unknown node type \"%s\" for node %ld \"%s\"\n", type_name, i, loaded_config.nodes.items[i].name);
exit(1);
}
if (!(nodes[i] = graph_node_new(spec, &loaded_config.nodes.items[i], &loaded_config.constants))) {
if (!(nodes[i] = graph_node_new(spec, &loaded_config.nodes.items[i], &loaded_config.environment))) {
perror("Failed to create node");
fprintf(stderr, "%ld \"%s\"\n", i, loaded_config.nodes.items[i].name);
exit(1);

View File

@ -69,7 +69,7 @@ handle_io(EventPositionBase * self, int fd, bool is_output)
}
static GraphNode *
create(GraphNodeSpecification * spec, GraphNodeConfig * config, const ConstantRegistry * constants)
create(GraphNodeSpecification * spec, GraphNodeConfig * config, InitializationEnvironment * env)
{
EvdevGraphNode * node = T_ALLOC(1, EvdevGraphNode);
if (!node) {
@ -77,7 +77,7 @@ create(GraphNodeSpecification * spec, GraphNodeConfig * config, const ConstantRe
}
const char *filename = NULL;
if (config) {
node->namespace = resolve_constant(constants, config_setting_get_member(config->options, "namespace"));
node->namespace = env_resolve_constant(env, config_setting_get_member(config->options, "namespace"));
config_setting_lookup_string(config->options, "file", &filename);
}
if (filename == NULL) {

View File

@ -49,7 +49,7 @@ handle_io(EventPositionBase * self, int fd, bool is_output)
}
static GraphNode *
create(GraphNodeSpecification * spec, GraphNodeConfig * config, const ConstantRegistry * constants)
create(GraphNodeSpecification * spec, GraphNodeConfig * config, InitializationEnvironment * env)
{
GetcharGraphNode * node = T_ALLOC(1, GetcharGraphNode);
if (!node) {
@ -70,7 +70,7 @@ create(GraphNodeSpecification * spec, GraphNodeConfig * config, const ConstantRe
.handle_io = handle_io,
.enabled = true,
},
.namespace = resolve_constant(constants, config_setting_get_member(config->options, "namespace")),
.namespace = env_resolve_constant(env, config_setting_get_member(config->options, "namespace")),
};
return &node->as_GraphNode;
}

View File

@ -28,10 +28,10 @@ handle_event(EventPositionBase * self, EventNode * event)
}
static GraphNode *
create(GraphNodeSpecification * spec, GraphNodeConfig * config, const ConstantRegistry * constants)
create(GraphNodeSpecification * spec, GraphNodeConfig * config, InitializationEnvironment * env)
{
(void) config;
(void) constants;
(void) env;
GraphNode * node = T_ALLOC(1, GraphNode);
if (!node) {
return node;

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

View File

@ -21,10 +21,10 @@ handle_event(EventPositionBase * self, EventNode * event)
}
static GraphNode *
create(GraphNodeSpecification * spec, GraphNodeConfig * config, const ConstantRegistry * constants)
create(GraphNodeSpecification * spec, GraphNodeConfig * config, InitializationEnvironment * env)
{
(void) config;
(void) constants;
(void) env;
GraphNode * node = T_ALLOC(1, GraphNode);
if (!node) {
return node;