Factor out unconditional tee-like event forwarding into a global function

This commit is contained in:
Vftdan 2024-08-20 13:43:24 +02:00
parent 851788d562
commit 3a9eed56bb
8 changed files with 43 additions and 93 deletions

34
graph.c
View File

@ -165,3 +165,37 @@ graph_channel_list_deinit(GraphChannelList * lst)
}
lst->length = 0;
}
ssize_t
graph_node_broadcast_forward_event(const GraphNode * source, EventNode * event)
{
if (!event) {
return -1;
}
if (!source) {
event_destroy(event);
return -1;
}
size_t count = source->outputs.length;
if (!count) {
event_destroy(event);
return 0;
}
if (count > 1) {
count = event_replicate(event, count - 1) + 1;
}
size_t succeses = count;
for (size_t i = 0; i < count; ++i) {
EventPositionBase *output = &source->outputs.elements[i]->as_EventPositionBase;
if (!output) {
--succeses;
EventNode *orphaned = event;
event = orphaned->next;
event_destroy(orphaned);
continue;
}
event->position = output;
event = event->next;
}
return succeses;
}

View File

@ -42,5 +42,6 @@ void graph_node_delete(GraphNode * self);
void graph_node_register_io(GraphNode * self, ProcessingState * state);
void graph_channel_list_init(GraphChannelList * lst);
void graph_channel_list_deinit(GraphChannelList * lst);
ssize_t graph_node_broadcast_forward_event(const GraphNode * source, EventNode * event /* may become invalid afterward */);
#endif /* end of include guard: GRAPH_H_ */

View File

@ -30,18 +30,7 @@ handle_event(EventPositionBase * self, EventNode * event)
event->data.payload = node->source.payload;
}
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;
if (!event->position) {
EventNode *orphaned = event;
event = orphaned->prev;
event_destroy(orphaned);
}
event = event->next;
}
graph_node_broadcast_forward_event(&node->as_GraphNode, event);
return true;
}

View File

@ -20,18 +20,7 @@ handle_event(EventPositionBase * self, EventNode * event)
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;
if (!event->position) {
EventNode *orphaned = event;
event = orphaned->prev;
event_destroy(orphaned);
}
event = event->next;
}
graph_node_broadcast_forward_event(&node->as_GraphNode, event);
return true;
}

View File

@ -17,18 +17,7 @@ handle_event(EventPositionBase * self, EventNode * event)
return true;
}
modifier_set_operation_from(&event->data.modifiers, node->modifiers, node->operation);
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;
if (!event->position) {
EventNode *orphaned = event;
event = orphaned->prev;
event_destroy(orphaned);
}
event = event->next;
}
graph_node_broadcast_forward_event(&node->as_GraphNode, event);
return true;
}

View File

@ -34,18 +34,7 @@ handle_event(EventPositionBase * self, EventNode * event)
value += node->center;
event->data.payload = value;
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;
if (!event->position) {
EventNode *orphaned = event;
event = orphaned->prev;
event_destroy(orphaned);
}
event = event->next;
}
graph_node_broadcast_forward_event(&node->as_GraphNode, event);
return true;
}

View File

@ -5,23 +5,7 @@ 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;
if (!event->position) {
EventNode *orphaned = event;
event = orphaned->prev;
event_destroy(orphaned);
}
event = event->next;
}
graph_node_broadcast_forward_event(node, event);
return true;
}

View File

@ -42,31 +42,6 @@ event_set_del(EventSet * set, const EventNode * element) {
return hash_table_delete_by_key(set, hash_event_ptr(element));
}
static void
broadcast_event(GraphNode * source, EventNode * event)
{
if (!event) {
return;
}
size_t count = source->outputs.length;
if (!count) {
event_destroy(event);
return;
}
if (count > 1) {
count = event_replicate(event, count - 1) + 1;
}
for (size_t i = 0; i < count; ++i) {
event->position = &source->outputs.elements[i]->as_EventPositionBase;
if (!event->position) {
EventNode *orphaned = event;
event = orphaned->prev;
event_destroy(orphaned);
}
event = event->next;
}
}
static EventNode *
replicate_and_advance(EventNode ** ptr)
{
@ -94,7 +69,7 @@ trigger_new_window(WindowGraphNode * node, EventNode * base)
terminator->data.code = node->terminator_prototype.code;
terminator->data.modifiers = modifier_set_copy(node->terminator_prototype.modifiers);
terminator->data.payload = node->terminator_prototype.payload;
broadcast_event(&node->as_GraphNode, terminator);
graph_node_broadcast_forward_event(&node->as_GraphNode, terminator);
}
}
@ -129,7 +104,7 @@ trigger_new_window(WindowGraphNode * node, EventNode * base)
}
recipient->data = orig->data;
recipient->data.modifiers = modifier_set_copy(orig->data.modifiers);
broadcast_event(&node->as_GraphNode, recipient);
graph_node_broadcast_forward_event(&node->as_GraphNode, recipient);
}
event_destroy(base);
@ -181,7 +156,7 @@ handle_event(EventPositionBase * self, EventNode * event)
}
if (event_replicate(event, 1)) {
broadcast_event(&node->as_GraphNode, event->next);
graph_node_broadcast_forward_event(&node->as_GraphNode, event->next);
}
queue_put(&node->buffer, (QueueValue){.as_ptr = event});
event_set_add(&node->buffered_set, event);