Compare commits

..

No commits in common. "50a6f1a716b78abc6ef4abce3df8137eaf05dc6f" and "9f876162e5e0f979dc93eb7f7e9de83b86d148bd" have entirely different histories.

14 changed files with 260 additions and 326 deletions

View File

@ -14,7 +14,7 @@ load_single_node_config(const config_setting_t *config_member)
}
result.name = config_setting_name(config_member);
if (!result.name) {
config_setting_lookup_string(config_member, "name", &result.name);
config_setting_lookup_string(config_member, "namd", &result.name);
}
config_setting_lookup_string(config_member, "type", &result.type);
result.options = config_setting_get_member(config_member, "options");
@ -58,8 +58,8 @@ load_channel_end_config(const config_setting_t *config_member, const char **name
if (!kv) {
return;
}
*name_ptr = config_setting_name(kv);
*index_ptr = config_setting_get_int64(kv);
*name_ptr = config_setting_name(config_member);
*index_ptr = config_setting_get_int64(config_member);
return;
}
*name_ptr = config_setting_get_string_elem(config_member, 0);
@ -123,39 +123,6 @@ load_channels_section(const config_setting_t *config_section)
return result;
}
static void
load_constants_section(const config_setting_t *config_section, ConstantRegistry *constants)
{
ssize_t length = config_setting_length(config_section);
if (length <= 0) {
return;
}
for (ssize_t i = 0; i < length; ++i) {
config_setting_t *item = config_setting_get_elem(config_section, i);
if (!item) {
continue;
}
const char *name;
long long value;
if (!(name = config_setting_name(item))) {
config_setting_lookup_string(item, "name", &name);
}
if (!name) {
continue;
}
if (config_setting_is_scalar(item) == CONFIG_TRUE) {
value = config_setting_get_int64(item);
} else if (config_setting_is_group(item) == CONFIG_TRUE) {
if (config_setting_lookup_int64(item, "value", &value) == CONFIG_FALSE) {
continue;
}
} else {
continue;
}
hash_table_insert(constants, hash_table_key_from_cstr(name), &value);
}
}
bool
load_config(const config_setting_t *config_root, FullConfig *config)
{
@ -164,9 +131,6 @@ load_config(const config_setting_t *config_root, FullConfig *config)
}
const config_setting_t *node_config = config_setting_get_member(config_root, "nodes");
const config_setting_t *channel_config = config_setting_get_member(config_root, "channels");
const config_setting_t *constants_config = config_setting_get_member(config_root, "constants");
hash_table_init(&config->constants, NULL);
load_constants_section(constants_config, &config->constants);
config->nodes = load_nodes_section(node_config);
config->channels = load_channels_section(channel_config);
return true;
@ -188,25 +152,4 @@ reset_config(FullConfig *config)
config->channels.items = NULL;
config->channels.length = 0;
}
hash_table_deinit(&config->constants);
}
long long
resolve_constant(const ConstantRegistry * registry, const config_setting_t * setting)
{
if (!setting) {
return 0;
}
if (config_setting_type(setting) == CONFIG_TYPE_STRING) {
if (!registry) {
return 1;
}
const char *name = config_setting_get_string(setting);
HashTableIndex idx = hash_table_find(registry, hash_table_key_from_cstr(name));
if (idx < 0) {
return 0;
}
return registry->value_array[idx];
}
return config_setting_get_int64(setting);
}

View File

@ -1,13 +1,8 @@
constants = {
ns_stdin = 0;
ns_clickpad = 1;
};
nodes = {
stdin = {
type = "getchar";
options = {
namespace = "ns_stdin";
namespace = 0;
};
};
print = {
@ -18,7 +13,7 @@ nodes = {
type = "evdev";
options = {
file = "/dev/input/event7";
namespace = "ns_clickpad";
namespace = 1;
};
};
};

View File

@ -3,7 +3,6 @@
#include <libconfig.h>
#include "defs.h"
#include "hash_table.h"
typedef struct {
const char *name;
@ -28,16 +27,12 @@ typedef struct {
GraphChannelConfig *items;
} GraphChannelConfigSection;
typedef TYPED_HASH_TABLE(long long) ConstantRegistry;
typedef struct {
GraphNodeConfigSection nodes;
GraphChannelConfigSection channels;
ConstantRegistry constants;
} FullConfig;
bool load_config(const config_setting_t *config_root, FullConfig *config);
void reset_config(FullConfig *config);
long long resolve_constant(const ConstantRegistry * registry, const config_setting_t * setting);
#endif /* end of include guard: CONFIG_H_ */

View File

@ -1,190 +0,0 @@
#include <limits.h>
#include "event_predicate.h"
typedef struct {
size_t capacity;
size_t length;
EventPredicate *values;
} EventPredicateList;
static EventPredicateList predicates = {
.length = 0,
.capacity = 0,
.values = NULL,
};
static bool
event_predicate_list_extend(EventPredicateList * lst)
{
size_t capacity = lst->capacity;
capacity = capacity + (capacity >> 1) + 1;
EventPredicate *new_values;
if (lst->values) {
new_values = reallocarray(lst->values, capacity, sizeof(EventPredicate));
} else {
new_values = T_ALLOC(capacity, EventPredicate);
}
if (!new_values) {
return false;
}
lst->values = new_values;
lst->capacity = capacity;
return true;
}
static void
event_predicate_list_clear(EventPredicateList * lst)
{
if (lst->values) {
free(lst->values);
lst->values = NULL;
}
lst->capacity = 0;
lst->length = 0;
}
EventPredicateHandle
event_predicate_register(EventPredicate filter)
{
size_t i = predicates.length;
if (i >= INT32_MAX) {
return -1;
}
while (i <= predicates.capacity) {
event_predicate_list_extend(&predicates);
}
predicates.values[i] = filter;
predicates.length = i + 1;
return (EventPredicateHandle) i;
}
static EventPredicate*
event_predicate_get_ptr(EventPredicateHandle handle)
{
if (handle < 0 || (size_t) handle >= predicates.length) {
return NULL;
}
return &predicates.values[handle];
}
EventPredicate
event_predicate_get(EventPredicateHandle handle)
{
EventPredicate *ptr = event_predicate_get_ptr(handle);
if (ptr) {
return *ptr;
} else {
return (EventPredicate) {
.type = EVPRED_INVALID,
.enabled = false,
.inverted = false,
};
}
}
EventPredicateResult
event_predicate_apply(EventPredicateHandle handle, EventNode * event)
{
EventPredicate *ptr = event_predicate_get_ptr(handle);
if (!ptr) {
return EVPREDRES_DISABLED;
}
if (!ptr->enabled) {
return EVPREDRES_DISABLED;
}
bool accepted = false;
switch (ptr->type) {
case EVPRED_INVALID:
return EVPREDRES_DISABLED;
case EVPRED_ACCEPT:
accepted = true;
break;
case EVPRED_CODE_NS...EVPRED_PAYLOAD:
if (!event) {
return EVPREDRES_DISABLED;
}
{
int64_t actual;
switch (ptr->type) {
case EVPRED_CODE_NS:
actual = event->data.code.ns;
break;
case EVPRED_CODE_MAJOR:
actual = event->data.code.major;
break;
case EVPRED_CODE_MINOR:
actual = event->data.code.minor;
break;
case EVPRED_PAYLOAD:
actual = event->data.payload;
break;
default:
return EVPREDRES_DISABLED;
}
accepted = ptr->range_data.min_value <= actual && actual <= ptr->range_data.max_value;
}
break;
case EVPRED_CONJUNCTION:
case EVPRED_DISJUNCTION:
if (!ptr->aggregate_data.handles) {
return EVPREDRES_DISABLED;
}
{
bool disjunction = ptr->type == EVPRED_DISJUNCTION;
accepted = !disjunction;
for (size_t i = 0; i < ptr->aggregate_data.length; ++i) {
EventPredicateResult child_result = event_predicate_apply(ptr->aggregate_data.handles[i], event); // TODO protect from stack overflow
bool should_stop = false;
switch (child_result) {
case EVPREDRES_DISABLED:
continue;
case EVPREDRES_ACCEPTED:
should_stop = false;
break;
case EVPREDRES_REJECTED:
should_stop = true;
break;
}
if (disjunction) {
should_stop = !should_stop;
}
if (should_stop) {
accepted = disjunction;
break;
}
}
}
break;
}
if (ptr->inverted) {
accepted = !accepted;
}
return accepted ? EVPREDRES_ACCEPTED : EVPREDRES_REJECTED;
}
void
event_predicate_set_enabled(EventPredicateHandle handle, bool enabled)
{
EventPredicate *ptr = event_predicate_get_ptr(handle);
if (!ptr) {
return;
}
ptr->enabled = enabled;
}
void event_predicate_set_inverted(EventPredicateHandle handle, bool inverted)
{
EventPredicate *ptr = event_predicate_get_ptr(handle);
if (!ptr) {
return;
}
ptr->inverted = inverted;
}
void
event_predicate_reset()
{
event_predicate_list_clear(&predicates);
}

View File

@ -1,51 +0,0 @@
#ifndef PREDS_H_
#define PREDS_H_
#include "events.h"
typedef struct event_predicate EventPredicate;
typedef int32_t EventPredicateHandle;
typedef enum {
EVPRED_INVALID,
EVPRED_ACCEPT,
// Range
EVPRED_CODE_NS,
EVPRED_CODE_MAJOR,
EVPRED_CODE_MINOR,
EVPRED_PAYLOAD,
// Aggregation
EVPRED_CONJUNCTION,
EVPRED_DISJUNCTION,
} EventPredicateType;
typedef enum {
EVPREDRES_DISABLED = -1,
EVPREDRES_REJECTED = 0,
EVPREDRES_ACCEPTED = 1,
} EventPredicateResult;
struct event_predicate {
EventPredicateType type;
bool enabled;
bool inverted;
union {
struct {
int64_t min_value;
int64_t max_value;
} range_data;
struct {
size_t length;
EventPredicateHandle *handles;
} aggregate_data;
};
};
EventPredicateHandle event_predicate_register(EventPredicate filter);
EventPredicate event_predicate_get(EventPredicateHandle handle);
EventPredicateResult event_predicate_apply(EventPredicateHandle handle, EventNode * event);
void event_predicate_set_enabled(EventPredicateHandle handle, bool enabled);
void event_predicate_set_inverted(EventPredicateHandle handle, bool inverted);
void event_predicate_reset();
#endif /* end of include guard: PREDS_H_ */

190
filters.c Normal file
View File

@ -0,0 +1,190 @@
#include <limits.h>
#include "filters.h"
typedef struct {
size_t capacity;
size_t length;
EventFilter *values;
} EventFilterList;
static EventFilterList filters = {
.length = 0,
.capacity = 0,
.values = NULL,
};
static bool
event_filter_list_extend(EventFilterList * lst)
{
size_t capacity = lst->capacity;
capacity = capacity + (capacity >> 1) + 1;
EventFilter *new_values;
if (lst->values) {
new_values = reallocarray(lst->values, capacity, sizeof(EventFilter));
} else {
new_values = T_ALLOC(capacity, EventFilter);
}
if (!new_values) {
return false;
}
lst->values = new_values;
lst->capacity = capacity;
return true;
}
static void
event_filter_list_clear(EventFilterList * lst)
{
if (lst->values) {
free(lst->values);
lst->values = NULL;
}
lst->capacity = 0;
lst->length = 0;
}
EventFilterHandle
event_filter_register(EventFilter filter)
{
size_t i = filters.length;
if (i >= INT32_MAX) {
return -1;
}
while (i <= filters.capacity) {
event_filter_list_extend(&filters);
}
filters.values[i] = filter;
filters.length = i + 1;
return (EventFilterHandle) i;
}
static EventFilter*
event_filter_get_ptr(EventFilterHandle handle)
{
if (handle < 0 || (size_t) handle >= filters.length) {
return NULL;
}
return &filters.values[handle];
}
EventFilter
event_filter_get(EventFilterHandle handle)
{
EventFilter *ptr = event_filter_get_ptr(handle);
if (ptr) {
return *ptr;
} else {
return (EventFilter) {
.type = EVFILTER_INVALID,
.enabled = false,
.inverted = false,
};
}
}
EventFilterResult
event_filter_apply(EventFilterHandle handle, EventNode * event)
{
EventFilter *ptr = event_filter_get_ptr(handle);
if (!ptr) {
return EVFILTERRES_DISABLED;
}
if (!ptr->enabled) {
return EVFILTERRES_DISABLED;
}
bool accepted = false;
switch (ptr->type) {
case EVFILTER_INVALID:
return EVFILTERRES_DISABLED;
case EVFILTER_ACCEPT:
accepted = true;
break;
case EVFILTER_CODE_NS...EVFILTER_PAYLOAD:
if (!event) {
return EVFILTERRES_DISABLED;
}
{
int64_t actual;
switch (ptr->type) {
case EVFILTER_CODE_NS:
actual = event->data.code.ns;
break;
case EVFILTER_CODE_MAJOR:
actual = event->data.code.major;
break;
case EVFILTER_CODE_MINOR:
actual = event->data.code.minor;
break;
case EVFILTER_PAYLOAD:
actual = event->data.payload;
break;
default:
return EVFILTERRES_DISABLED;
}
accepted = ptr->range_data.min_value <= actual && actual <= ptr->range_data.max_value;
}
break;
case EVFILTER_CONJUNCTION:
case EVFILTER_DISJUNCTION:
if (!ptr->aggregate_data.handles) {
return EVFILTERRES_DISABLED;
}
{
bool disjunction = ptr->type == EVFILTER_DISJUNCTION;
accepted = !disjunction;
for (size_t i = 0; i < ptr->aggregate_data.length; ++i) {
EventFilterResult child_result = event_filter_apply(ptr->aggregate_data.handles[i], event); // TODO protect from stack overflow
bool should_stop = false;
switch (child_result) {
case EVFILTERRES_DISABLED:
continue;
case EVFILTERRES_ACCEPTED:
should_stop = false;
break;
case EVFILTERRES_REJECTED:
should_stop = true;
break;
}
if (disjunction) {
should_stop = !should_stop;
}
if (should_stop) {
accepted = disjunction;
break;
}
}
}
break;
}
if (ptr->inverted) {
accepted = !accepted;
}
return accepted ? EVFILTERRES_ACCEPTED : EVFILTERRES_REJECTED;
}
void
event_filter_set_enabled(EventFilterHandle handle, bool enabled)
{
EventFilter *ptr = event_filter_get_ptr(handle);
if (!ptr) {
return;
}
ptr->enabled = enabled;
}
void event_filter_set_inverted(EventFilterHandle handle, bool inverted)
{
EventFilter *ptr = event_filter_get_ptr(handle);
if (!ptr) {
return;
}
ptr->inverted = inverted;
}
void
event_filter_reset()
{
event_filter_list_clear(&filters);
}

51
filters.h Normal file
View File

@ -0,0 +1,51 @@
#ifndef FILTERS_H_
#define FILTERS_H_
#include "events.h"
typedef struct event_filter EventFilter;
typedef int32_t EventFilterHandle;
typedef enum {
EVFILTER_INVALID,
EVFILTER_ACCEPT,
// Range
EVFILTER_CODE_NS,
EVFILTER_CODE_MAJOR,
EVFILTER_CODE_MINOR,
EVFILTER_PAYLOAD,
// Aggregation
EVFILTER_CONJUNCTION,
EVFILTER_DISJUNCTION,
} EventFilterType;
typedef enum {
EVFILTERRES_DISABLED = -1,
EVFILTERRES_REJECTED = 0,
EVFILTERRES_ACCEPTED = 1,
} EventFilterResult;
struct event_filter {
EventFilterType type;
bool enabled;
bool inverted;
union {
struct {
int64_t min_value;
int64_t max_value;
} range_data;
struct {
size_t length;
EventFilterHandle *handles;
} aggregate_data;
};
};
EventFilterHandle event_filter_register(EventFilter filter);
EventFilter event_filter_get(EventFilterHandle handle);
EventFilterResult event_filter_apply(EventFilterHandle handle, EventNode * event);
void event_filter_set_enabled(EventFilterHandle handle, bool enabled);
void event_filter_set_inverted(EventFilterHandle handle, bool inverted);
void event_filter_reset();
#endif /* end of include guard: FILTERS_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)
{
if (!spec || !spec->create) {
return NULL;
}
return spec->create(spec, config, constants);
return spec->create(spec, config);
}
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);
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);
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]))) {
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)
{
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"));
config_setting_lookup_int(config->options, "namespace", &node->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)
{
GetcharGraphNode * node = T_ALLOC(1, GetcharGraphNode);
if (!node) {
@ -70,8 +70,11 @@ 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 = 0,
};
if (config) {
config_setting_lookup_int(config->options, "namespace", &node->namespace);
}
return &node->as_GraphNode;
}

View File

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

View File

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