1
0
Fork 0
forked from hanez/xtc

Phase 4: Add modular graph editor and transactional graph edits

This commit is contained in:
Johannes Findeisen 2026-10-26 23:55:35 +01:00
commit 5c41d7f6da
27 changed files with 1023 additions and 64 deletions

View file

@ -151,6 +151,19 @@ xtc_result xtc_backend_submit(xtc_backend *backend, const xtc_graph *graph)
if (!backend->ready) return XTC_ERR_INVALID_STATE;
if (__atomic_load_n(&backend->lost, __ATOMIC_ACQUIRE) ||
__atomic_load_n(&backend->rate_changed, __ATOMIC_ACQUIRE)) return XTC_ERR_INVALID_STATE;
if (!backend->running) {
xtc_realtime *replacement = NULL;
xtc_result result = xtc_realtime_create((double)backend->expected_rate, backend->max_frames, &replacement);
if (result != XTC_OK) return result;
result = xtc_realtime_submit(replacement, graph);
if (result != XTC_OK) { xtc_realtime_destroy(replacement); return result; }
/* Inactive client, or successful deactivate: process callback quiescent.
* Notifications never access engine. Preserve old engine until ready. */
xtc_realtime_destroy(backend->engine);
backend->engine = replacement;
__atomic_store_n(&backend->process_result, (unsigned int)XTC_OK, __ATOMIC_RELAXED);
return XTC_OK;
}
return xtc_realtime_submit(backend->engine, graph);
}
void xtc_backend_collect(xtc_backend *backend)
@ -168,6 +181,8 @@ xtc_result xtc_backend_get_status(const xtc_backend *backend, xtc_backend_status
out_status->sample_rate_changed = (int)__atomic_load_n(&backend->rate_changed, __ATOMIC_ACQUIRE);
out_status->running = backend->running && !out_status->server_lost;
out_status->processed_blocks = engine_status.processed_blocks;
out_status->submitted_revision = engine_status.submitted_revision;
out_status->active_revision = engine_status.active_revision;
out_status->process_result = (xtc_result)__atomic_load_n(&backend->process_result, __ATOMIC_RELAXED);
return XTC_OK;
}

View file

@ -35,13 +35,13 @@ xtc_result xtc_graph_compile(const xtc_graph *graph, double sample_rate,
{
xtc_plan *plan;
xtc_result result;
size_t node, edge, cursor = 0U;
size_t node, edge, storage_nodes, cursor = 0U;
int has_output = 0;
if (out_plan == NULL) return XTC_ERR_INVALID_ARGUMENT;
*out_plan = NULL;
if (graph == NULL || !isfinite(sample_rate) || sample_rate <= 0.0 ||
max_frames == 0U || max_frames > XTC_MAX_FRAMES) return XTC_ERR_INVALID_ARGUMENT;
if (graph->node_count == 0U) return XTC_ERR_INVALID_STATE;
storage_nodes = graph->node_count == 0U ? 1U : graph->node_count;
for (node = 0U; node < graph->node_count; ++node) {
if (graph->nodes[node].type == XTC_NODE_OSCILLATOR &&
(double)graph->nodes[node].value > sample_rate * 0.5)
@ -55,14 +55,14 @@ xtc_result xtc_graph_compile(const xtc_graph *graph, double sample_rate,
plan->max_frames = max_frames;
plan->sample_rate = sample_rate;
plan->input_gain = 1.0f;
plan->nodes = calloc(plan->node_count, sizeof(*plan->nodes));
plan->order = calloc(plan->node_count, sizeof(*plan->order));
plan->nodes = calloc(storage_nodes, sizeof(*plan->nodes));
plan->order = calloc(storage_nodes, sizeof(*plan->order));
plan->offsets = calloc(plan->node_count + 1U, sizeof(*plan->offsets));
plan->sources = calloc(graph->edge_count + 1U, sizeof(*plan->sources));
plan->render = calloc(plan->node_count, sizeof(*plan->render));
plan->buffers = calloc(plan->node_count * max_frames, sizeof(*plan->buffers));
plan->peaks = calloc(plan->node_count, sizeof(*plan->peaks));
plan->phases = calloc(plan->node_count, sizeof(*plan->phases));
plan->render = calloc(storage_nodes, sizeof(*plan->render));
plan->buffers = calloc(storage_nodes * max_frames, sizeof(*plan->buffers));
plan->peaks = calloc(storage_nodes, sizeof(*plan->peaks));
plan->phases = calloc(storage_nodes, sizeof(*plan->phases));
if (plan->nodes == NULL || plan->order == NULL || plan->offsets == NULL ||
plan->sources == NULL || plan->render == NULL || plan->buffers == NULL ||
plan->peaks == NULL || plan->phases == NULL) {
@ -80,7 +80,7 @@ xtc_result xtc_graph_compile(const xtc_graph *graph, double sample_rate,
plan->sources[cursor++] = graph->edges[edge].source;
if (graph->edges[edge].source == node) terminal = 0;
}
plan->render[node] = (unsigned char)(has_output ?
plan->render[node] = (unsigned char)(has_output || graph->require_output_nodes ?
plan->nodes[node].type == XTC_NODE_OUTPUT : terminal);
}
plan->offsets[plan->node_count] = cursor;
@ -99,7 +99,11 @@ xtc_result xtc_plan_reset(xtc_plan *plan)
xtc_result xtc_plan_meter_peak(const xtc_plan *plan, size_t node_id, float *out_peak)
{
if (plan == NULL || out_peak == NULL) return XTC_ERR_INVALID_ARGUMENT;
if (node_id >= plan->node_count) return XTC_ERR_NOT_FOUND;
size_t index;
for (index = 0U; index < plan->node_count; ++index)
if (plan->nodes[index].id == node_id) break;
if (index == plan->node_count) return XTC_ERR_NOT_FOUND;
node_id = index;
if (plan->nodes[node_id].type != XTC_NODE_METER) return XTC_ERR_INVALID_ARGUMENT;
*out_peak = plan->peaks[node_id];
return XTC_OK;

View file

@ -11,6 +11,7 @@ struct xtc_realtime {
xtc_plan *active; /* Audio owner only, until consumer is stopped/joined. */
unsigned int blocks;
unsigned int result;
unsigned int submitted_revision, pending_revision, active_revision;
};
xtc_result xtc_realtime_create(double sample_rate, size_t max_frames,
@ -59,6 +60,11 @@ xtc_result xtc_realtime_submit(xtc_realtime *engine, const xtc_graph *graph)
return XTC_ERR_CAPACITY;
result = xtc_graph_compile(graph, engine->sample_rate, engine->max_frames, &plan);
if (result != XTC_OK) return result;
{
unsigned int revision = __atomic_load_n(&engine->submitted_revision, __ATOMIC_RELAXED) + 1U;
__atomic_store_n(&engine->pending_revision, revision, __ATOMIC_RELAXED);
__atomic_store_n(&engine->submitted_revision, revision, __ATOMIC_RELAXED);
}
__atomic_store_n(&engine->pending, plan, __ATOMIC_RELEASE);
return XTC_OK;
}
@ -68,6 +74,8 @@ xtc_result xtc_realtime_get_status(const xtc_realtime *engine,
{
if (engine == NULL || out_status == NULL) return XTC_ERR_INVALID_ARGUMENT;
out_status->processed_blocks = __atomic_load_n(&engine->blocks, __ATOMIC_RELAXED);
out_status->submitted_revision = __atomic_load_n(&engine->submitted_revision, __ATOMIC_RELAXED);
out_status->active_revision = __atomic_load_n(&engine->active_revision, __ATOMIC_RELAXED);
out_status->last_result = (xtc_result)__atomic_load_n(&engine->result, __ATOMIC_RELAXED);
return XTC_OK;
}
@ -85,10 +93,15 @@ xtc_result xtc_realtime_process(xtc_realtime *engine, const float *input,
/* A full retirement slot delays adoption without delaying processing.
* acquire pairs with control's release exchange after its last read. */
if (__atomic_load_n(&engine->retired, __ATOMIC_ACQUIRE) == NULL) {
xtc_plan *next = __atomic_exchange_n(&engine->pending, NULL, __ATOMIC_ACQUIRE);
xtc_plan *next = __atomic_load_n(&engine->pending, __ATOMIC_ACQUIRE);
if (next != NULL) {
xtc_plan *old = engine->active;
/* Read revision BEFORE releasing the pending slot. The producer
* cannot reuse pending_revision until its acquire observes NULL. */
unsigned int revision = __atomic_load_n(&engine->pending_revision, __ATOMIC_RELAXED);
(void)__atomic_exchange_n(&engine->pending, NULL, __ATOMIC_ACQ_REL);
engine->active = next;
__atomic_store_n(&engine->active_revision, revision, __ATOMIC_RELAXED);
/* No reference to old is used after this release. */
__atomic_store_n(&engine->retired, old, __ATOMIC_RELEASE);
}

View file

@ -1,6 +1,7 @@
#include "graph_internal.h"
#include <math.h>
#include <stdlib.h>
#include <string.h>
xtc_result xtc_graph_create(size_t max_nodes, xtc_graph **out_graph)
{
@ -30,15 +31,23 @@ void xtc_graph_destroy(xtc_graph *graph)
}
}
static size_t node_index(const xtc_graph *graph, size_t id)
{
size_t i;
for (i = 0U; i < graph->node_count; ++i) if (graph->nodes[i].id == id) return i;
return SIZE_MAX;
}
static xtc_result add_node(xtc_graph *graph, xtc_node_type type, float value,
size_t *out_node_id)
{
if (graph == NULL || out_node_id == NULL || !isfinite(value))
return XTC_ERR_INVALID_ARGUMENT;
if (graph->node_count == graph->max_nodes) return XTC_ERR_CAPACITY;
if (graph->node_count == graph->max_nodes || graph->next_id == SIZE_MAX) return XTC_ERR_CAPACITY;
graph->nodes[graph->node_count].type = type;
graph->nodes[graph->node_count].value = value;
*out_node_id = graph->node_count++;
graph->nodes[graph->node_count++].id = graph->next_id;
*out_node_id = graph->next_id++;
return XTC_OK;
}
xtc_result xtc_graph_add_gain(xtc_graph *graph, float gain, size_t *out_node_id)
@ -58,7 +67,8 @@ xtc_result xtc_graph_get_port(const xtc_graph *graph, size_t node_id,
{
xtc_node_type type;
if (graph == NULL || out_port == NULL) return XTC_ERR_INVALID_ARGUMENT;
if (node_id >= graph->node_count) return XTC_ERR_NOT_FOUND;
node_id = node_index(graph, node_id);
if (node_id == SIZE_MAX) return XTC_ERR_NOT_FOUND;
type = graph->nodes[node_id].type;
if (port_id > 1U || (port_id == 0U && type == XTC_NODE_OSCILLATOR) ||
(port_id == 1U && type == XTC_NODE_OUTPUT)) return XTC_ERR_NOT_FOUND;
@ -108,6 +118,8 @@ xtc_result xtc_graph_connect_ports(xtc_graph *graph, size_t source_node,
if (source.direction != XTC_PORT_OUTPUT || dest.direction != XTC_PORT_INPUT ||
source.signal_type != dest.signal_type || source.channel_count != dest.channel_count)
return XTC_ERR_INVALID_ARGUMENT;
source_node = node_index(graph, source_node);
destination_node = node_index(graph, destination_node);
for (i = 0U; i < graph->edge_count; ++i)
if (graph->edges[i].source == source_node && graph->edges[i].destination == destination_node)
return XTC_ERR_INVALID_STATE;
@ -120,3 +132,115 @@ xtc_result xtc_graph_connect_ports(xtc_graph *graph, size_t source_node,
}
xtc_result xtc_graph_connect(xtc_graph *graph, size_t source_node, size_t destination_node)
{ return xtc_graph_connect_ports(graph, source_node, 1U, destination_node, 0U); }
xtc_result xtc_graph_get_counts(const xtc_graph *graph, size_t *out_nodes, size_t *out_connections)
{
if (graph == NULL || out_nodes == NULL || out_connections == NULL) return XTC_ERR_INVALID_ARGUMENT;
*out_nodes = graph->node_count;
*out_connections = graph->edge_count;
return XTC_OK;
}
xtc_result xtc_graph_get_node(const xtc_graph *graph, size_t index, xtc_node_info *out_node)
{
if (graph == NULL || out_node == NULL) return XTC_ERR_INVALID_ARGUMENT;
if (index >= graph->node_count) return XTC_ERR_NOT_FOUND;
out_node->id = graph->nodes[index].id;
out_node->type = graph->nodes[index].type;
out_node->value = graph->nodes[index].value;
return XTC_OK;
}
xtc_result xtc_graph_get_connection(const xtc_graph *graph, size_t index, xtc_connection_info *out_connection)
{
if (graph == NULL || out_connection == NULL) return XTC_ERR_INVALID_ARGUMENT;
if (index >= graph->edge_count) return XTC_ERR_NOT_FOUND;
out_connection->source_node = graph->nodes[graph->edges[index].source].id;
out_connection->destination_node = graph->nodes[graph->edges[index].destination].id;
out_connection->source_port = 1U;
out_connection->destination_port = 0U;
return XTC_OK;
}
xtc_result xtc_graph_require_output_nodes(xtc_graph *graph, int required)
{
if (graph == NULL || (required != 0 && required != 1)) return XTC_ERR_INVALID_ARGUMENT;
graph->require_output_nodes = required;
return XTC_OK;
}
static xtc_result remove_node(xtc_graph *graph, size_t id)
{
size_t index = node_index(graph, id), i, count = 0U;
if (index == SIZE_MAX) return XTC_ERR_NOT_FOUND;
for (i = 0U; i < graph->edge_count; ++i) {
xtc_edge edge = graph->edges[i];
if (edge.source == index || edge.destination == index) continue;
if (edge.source > index) --edge.source;
if (edge.destination > index) --edge.destination;
graph->edges[count++] = edge;
}
graph->edge_count = count;
memmove(graph->nodes + index, graph->nodes + index + 1U,
(graph->node_count - index - 1U) * sizeof(*graph->nodes));
--graph->node_count;
return XTC_OK;
}
static xtc_result disconnect_ports(xtc_graph *graph, const xtc_connection_info *connection)
{
size_t i;
xtc_port_info port;
xtc_result result = xtc_graph_get_port(graph, connection->source_node, connection->source_port, &port);
if (result != XTC_OK) return result;
if (port.direction != XTC_PORT_OUTPUT) return XTC_ERR_INVALID_ARGUMENT;
result = xtc_graph_get_port(graph, connection->destination_node, connection->destination_port, &port);
if (result != XTC_OK) return result;
if (port.direction != XTC_PORT_INPUT) return XTC_ERR_INVALID_ARGUMENT;
for (i = 0U; i < graph->edge_count; ++i) {
if (graph->edges[i].source == node_index(graph, connection->source_node) &&
graph->edges[i].destination == node_index(graph, connection->destination_node)) {
memmove(graph->edges + i, graph->edges + i + 1U,
(graph->edge_count - i - 1U) * sizeof(*graph->edges));
--graph->edge_count;
return XTC_OK;
}
}
return XTC_ERR_NOT_FOUND;
}
xtc_result xtc_graph_apply_edit(const xtc_graph *source, const xtc_graph_edit *edit,
xtc_graph **out_graph, size_t *out_added_id)
{
xtc_graph *graph;
xtc_result result;
size_t added_id = 0U;
if (out_graph == NULL) return XTC_ERR_INVALID_ARGUMENT;
*out_graph = NULL;
if (source == NULL || edit == NULL) return XTC_ERR_INVALID_ARGUMENT;
result = xtc_graph_create(source->max_nodes, &graph);
if (result != XTC_OK) return result;
graph->node_count = source->node_count;
graph->edge_count = source->edge_count;
graph->next_id = source->next_id;
graph->require_output_nodes = source->require_output_nodes;
memcpy(graph->nodes, source->nodes, source->node_count * sizeof(*graph->nodes));
memcpy(graph->edges, source->edges, source->edge_count * sizeof(*graph->edges));
switch (edit->kind) {
case XTC_EDIT_ADD_NODE:
switch (edit->data.add.type) {
case XTC_NODE_GAIN: result = xtc_graph_add_gain(graph, edit->data.add.value, &added_id); break;
case XTC_NODE_OSCILLATOR: result = xtc_graph_add_oscillator(graph, edit->data.add.value, &added_id); break;
case XTC_NODE_METER: result = xtc_graph_add_meter(graph, &added_id); break;
case XTC_NODE_OUTPUT: result = xtc_graph_add_output(graph, &added_id); break;
default: result = XTC_ERR_INVALID_ARGUMENT; break;
}
break;
case XTC_EDIT_REMOVE_NODE: result = remove_node(graph, edit->data.node_id); break;
case XTC_EDIT_CONNECT:
result = xtc_graph_connect_ports(graph, edit->data.connection.source_node,
edit->data.connection.source_port, edit->data.connection.destination_node,
edit->data.connection.destination_port);
break;
case XTC_EDIT_DISCONNECT: result = disconnect_ports(graph, &edit->data.connection); break;
default: result = XTC_ERR_INVALID_ARGUMENT; break;
}
if (result != XTC_OK) { xtc_graph_destroy(graph); return result; }
if (out_added_id != NULL && edit->kind == XTC_EDIT_ADD_NODE) *out_added_id = added_id;
*out_graph = graph;
return XTC_OK;
}

View file

@ -3,10 +3,11 @@
#include "xtc/xtc_engine.h"
#define XTC_MAX_NODES 256U
#define XTC_MAX_FRAMES 65536U
typedef struct { xtc_node_type type; float value; } xtc_node;
typedef struct { xtc_node_type type; float value; size_t id; } xtc_node;
typedef struct { size_t source; size_t destination; } xtc_edge;
struct xtc_graph {
size_t max_nodes, node_count, edge_count;
size_t max_nodes, node_count, edge_count, next_id;
int require_output_nodes;
xtc_node *nodes;
xtc_edge *edges;
};

260
src/gui/graph_widget.cpp Normal file
View file

@ -0,0 +1,260 @@
#include "graph_widget.hpp"
#include <FL/Fl.H>
#include <FL/Fl_Box.H>
#include <FL/fl_draw.H>
#include <algorithm>
#include <cmath>
#include <cstdio>
namespace {
constexpr int node_width = 150, node_height = 74;
const char *node_name(xtc_node_type type)
{
switch (type) {
case XTC_NODE_OSCILLATOR: return "Oscillator";
case XTC_NODE_GAIN: return "Gain";
case XTC_NODE_METER: return "Meter";
case XTC_NODE_OUTPUT: return "Output";
}
return "Unknown";
}
}
GraphWidget::GraphWidget(int left, int top, int width, int height, const xtc_graph *graph,
EditHandler handler, void *context, Fl_Box *feedback_box)
: Fl_Widget(left, top, width, height), graph_(graph), handler_(handler),
context_(context), feedback_(feedback_box)
{ set_graph(graph); }
void GraphWidget::set_graph(const xtc_graph *graph)
{
graph_ = graph;
size_t nodes = 0U, edges = 0U;
if (xtc_graph_get_counts(graph_, &nodes, &edges) != XTC_OK) { count_ = 0U; redraw(); return; }
auto old = positions_;
size_t old_count = count_;
count_ = 0U;
for (size_t i = 0U; i < nodes && i < positions_.size(); ++i) {
xtc_node_info node{};
if (xtc_graph_get_node(graph_, i, &node) != XTC_OK) continue;
Position position;
position.id = node.id;
bool found = false;
for (size_t j = 0U; j < old_count; ++j)
if (old[j].id == node.id) { position = old[j]; found = true; break; }
if (!found) {
for (size_t slot = 0U; slot <= positions_.size(); ++slot) {
position.left = 40 + static_cast<int>(slot % 8U) * 210;
position.top = 40 + static_cast<int>(slot / 8U) * 115;
bool occupied = false;
for (size_t j = 0U; j < old_count; ++j)
if (std::abs(old[j].left - position.left) < node_width + 12 &&
std::abs(old[j].top - position.top) < node_height + 12) occupied = true;
if (!occupied) break;
}
}
positions_[count_++] = position;
}
int left, top;
if (!node_position(selected_node_, left, top)) selected_node_ = SIZE_MAX;
if (edge_selected_ && !select_connection(selected_edge_.source_node, selected_edge_.destination_node))
edge_selected_ = false;
redraw();
}
bool GraphWidget::node_position(size_t id, int &left, int &top) const
{
for (size_t i = 0U; i < count_; ++i) if (positions_[i].id == id) {
left = positions_[i].left; top = positions_[i].top; return true;
}
return false;
}
bool GraphWidget::move_node(size_t id, int left, int top)
{
for (size_t i = 0U; i < count_; ++i) if (positions_[i].id == id) {
positions_[i].left = std::max(12, std::min(left, w() - node_width - 12));
positions_[i].top = std::max(12, std::min(top, h() - node_height - 12));
redraw(); return true;
}
return false;
}
bool GraphWidget::select_node(size_t id)
{
int left, top;
if (!node_position(id, left, top)) return false;
selected_node_ = id; edge_selected_ = false; redraw(); return true;
}
bool GraphWidget::select_connection(size_t source, size_t destination)
{
size_t nodes, edges;
if (xtc_graph_get_counts(graph_, &nodes, &edges) != XTC_OK) return false;
for (size_t i = 0U; i < edges; ++i) {
xtc_connection_info edge{};
if (xtc_graph_get_connection(graph_, i, &edge) == XTC_OK &&
edge.source_node == source && edge.destination_node == destination) {
selected_edge_ = edge; edge_selected_ = true; selected_node_ = SIZE_MAX; redraw(); return true;
}
}
return false;
}
void GraphWidget::feedback(xtc_result result, const char *success)
{
char text[256];
if (result == XTC_OK) std::snprintf(text, sizeof(text), "%s", success);
else if (result == XTC_ERR_CAPACITY)
std::snprintf(text, sizeof(text), "Graph unchanged: capacity exceeded (node limit or pending edit). Retry after the next audio block.");
else std::snprintf(text, sizeof(text), "Graph unchanged: %s.", xtc_result_string(result));
feedback_->copy_label(text);
}
xtc_result GraphWidget::add_node(xtc_node_type type)
{
xtc_graph_edit edit{};
edit.kind = XTC_EDIT_ADD_NODE;
edit.data.add.type = type;
edit.data.add.value = type == XTC_NODE_OSCILLATOR ? 440.0f : type == XTC_NODE_GAIN ? 0.1f : 0.0f;
size_t added = SIZE_MAX;
xtc_result result = handler_(context_, edit, &added);
if (result == XTC_OK) select_node(added);
feedback(result, "Node added. Drag its title to move; drag an output port to an input to connect.");
return result;
}
xtc_result GraphWidget::connect_ports(const xtc_connection_info &connection)
{
xtc_graph_edit edit{};
edit.kind = XTC_EDIT_CONNECT;
edit.data.connection = connection;
xtc_result result = handler_(context_, edit, nullptr);
feedback(result, "Connection added. Click a wire and press Delete to disconnect.");
return result;
}
xtc_result GraphWidget::delete_selected()
{
xtc_graph_edit edit{};
if (edge_selected_) { edit.kind = XTC_EDIT_DISCONNECT; edit.data.connection = selected_edge_; }
else if (selected_node_ != SIZE_MAX) { edit.kind = XTC_EDIT_REMOVE_NODE; edit.data.node_id = selected_node_; }
else { feedback_->copy_label("Select a node or wire first."); return XTC_ERR_INVALID_STATE; }
xtc_result result = handler_(context_, edit, nullptr);
feedback(result, "Selection deleted.");
return result;
}
bool GraphWidget::port_position(size_t id, size_t port, int &left, int &top) const
{
xtc_port_info info{};
if (xtc_graph_get_port(graph_, id, port, &info) != XTC_OK || !node_position(id, left, top)) return false;
left += info.direction == XTC_PORT_OUTPUT ? node_width : 0;
top += 48;
return true;
}
bool GraphWidget::hit_port(int left, int top, size_t &node, size_t &port) const
{
for (size_t i = count_; i > 0U; --i) for (size_t p = 0U; p < 2U; ++p) {
int px, py;
if (port_position(positions_[i - 1U].id, p, px, py) &&
std::abs(px - left) <= 9 && std::abs(py - top) <= 9) {
node = positions_[i - 1U].id; port = p; return true;
}
}
return false;
}
void GraphWidget::draw()
{
fl_push_clip(x(), y(), w(), h());
fl_color(fl_rgb_color(25, 28, 33)); fl_rectf(x(), y(), w(), h());
size_t nodes = 0U, edges = 0U;
(void)xtc_graph_get_counts(graph_, &nodes, &edges);
for (size_t i = 0U; i < edges; ++i) {
xtc_connection_info edge{};
int sx, sy, dx, dy;
if (xtc_graph_get_connection(graph_, i, &edge) != XTC_OK ||
!port_position(edge.source_node, 1U, sx, sy) || !port_position(edge.destination_node, 0U, dx, dy)) continue;
bool selected = edge_selected_ && selected_edge_.source_node == edge.source_node &&
selected_edge_.destination_node == edge.destination_node;
fl_color(selected ? FL_YELLOW : fl_rgb_color(100, 200, 190));
fl_line_style(FL_SOLID, selected ? 3 : 2);
fl_line(x() + sx, y() + sy, x() + dx, y() + dy);
}
fl_line_style(0);
for (size_t i = 0U; i < count_; ++i) {
xtc_node_info node{};
if (xtc_graph_get_node(graph_, i, &node) != XTC_OK) continue;
int left = x() + positions_[i].left, top = y() + positions_[i].top;
fl_color(fl_rgb_color(49, 54, 62)); fl_rectf(left, top, node_width, node_height);
fl_color(selected_node_ == node.id ? FL_YELLOW : fl_rgb_color(115, 130, 145));
fl_rect(left, top, node_width, node_height);
fl_color(FL_WHITE); fl_font(FL_HELVETICA_BOLD, 12);
char text[96];
std::snprintf(text, sizeof(text), "%s #%zu", node_name(node.type), node.id);
fl_draw(text, left + 10, top + 20);
fl_font(FL_HELVETICA, 12);
if (node.type == XTC_NODE_OSCILLATOR) std::snprintf(text, sizeof(text), "%.0f Hz", static_cast<double>(node.value));
else if (node.type == XTC_NODE_GAIN) std::snprintf(text, sizeof(text), "x %.2f", static_cast<double>(node.value));
else std::snprintf(text, sizeof(text), "%s", node.type == XTC_NODE_METER ? "Peak pass-through" : "Mono audio output");
fl_draw(text, left + 10, top + 37);
for (size_t port = 0U; port < 2U; ++port) {
int px, py;
if (!port_position(node.id, port, px, py)) continue;
fl_color(fl_rgb_color(110, 220, 185)); fl_pie(x() + px - 5, y() + py - 5, 10, 10, 0, 360);
fl_color(FL_WHITE); fl_draw(port == 0U ? "in" : "out", left + (port == 0U ? 10 : 118), top + 62);
}
}
if (wiring_) {
int px, py;
if (port_position(wire_node_, wire_port_, px, py)) {
fl_color(FL_YELLOW); fl_line(x() + px, y() + py, x() + mouse_x_, y() + mouse_y_);
}
}
fl_pop_clip();
}
int GraphWidget::handle(int event)
{
const int left = Fl::event_x() - x(), top = Fl::event_y() - y();
switch (event) {
case FL_FOCUS: case FL_UNFOCUS: return 1;
case FL_PUSH: {
if (Fl::event_button() != FL_LEFT_MOUSE) return 0;
take_focus(); mouse_x_ = left; mouse_y_ = top;
dragging_ = false; wiring_ = false;
if (hit_port(left, top, wire_node_, wire_port_)) { wiring_ = true; redraw(); return 1; }
for (size_t i = count_; i > 0U; --i) {
const Position &pos = positions_[i - 1U];
if (left >= pos.left && left <= pos.left + node_width && top >= pos.top && top <= pos.top + node_height) {
select_node(pos.id); dragging_ = true; drag_x_ = left - pos.left; drag_y_ = top - pos.top; return 1;
}
}
size_t nodes, edges;
(void)xtc_graph_get_counts(graph_, &nodes, &edges);
for (size_t i = 0U; i < edges; ++i) {
xtc_connection_info edge{};
int sx, sy, dx, dy;
(void)xtc_graph_get_connection(graph_, i, &edge);
if (!port_position(edge.source_node, 1U, sx, sy) || !port_position(edge.destination_node, 0U, dx, dy)) continue;
double vx = dx - sx, vy = dy - sy;
double length = vx * vx + vy * vy;
double t = length == 0.0 ? 0.0 : std::max(0.0, std::min(1.0, ((left - sx) * vx + (top - sy) * vy) / length));
if (std::hypot(left - sx - t * vx, top - sy - t * vy) <= 6.0) {
select_connection(edge.source_node, edge.destination_node); return 1;
}
}
selected_node_ = SIZE_MAX; edge_selected_ = false; redraw(); return 1;
}
case FL_DRAG:
mouse_x_ = left; mouse_y_ = top;
if (dragging_) move_node(selected_node_, left - drag_x_, top - drag_y_);
if (wiring_) redraw();
return 1;
case FL_RELEASE:
dragging_ = false;
if (wiring_) {
size_t node, port;
wiring_ = false;
if (hit_port(left, top, node, port)) connect_ports({wire_node_, wire_port_, node, port});
else feedback_->copy_label("Connection cancelled. Drag an output port to an input port.");
redraw();
}
return 1;
case FL_KEYDOWN:
if (Fl::event_key() == FL_Delete || Fl::event_key() == FL_BackSpace) { delete_selected(); return 1; }
if (Fl::event_key() == FL_Escape) { wiring_ = dragging_ = false; redraw(); return 1; }
break;
default: break;
}
return Fl_Widget::handle(event);
}

44
src/gui/graph_widget.hpp Normal file
View file

@ -0,0 +1,44 @@
#ifndef XTC_GRAPH_WIDGET_HPP
#define XTC_GRAPH_WIDGET_HPP
#include <FL/Fl_Widget.H>
#include <array>
#include "xtc/xtc_engine.h"
class Fl_Box;
/* GUI positions/selection only. Domain snapshots and edits stay in the C API. */
class GraphWidget : public Fl_Widget {
public:
using EditHandler = xtc_result (*)(void *, const xtc_graph_edit &, size_t *);
GraphWidget(int x, int y, int width, int height, const xtc_graph *graph,
EditHandler handler, void *context, Fl_Box *feedback);
void set_graph(const xtc_graph *graph);
const xtc_graph *graph() const { return graph_; } // Borrowed, GUI thread only.
xtc_result add_node(xtc_node_type type);
xtc_result connect_ports(const xtc_connection_info &connection);
xtc_result delete_selected();
bool select_node(size_t id);
bool select_connection(size_t source, size_t destination);
bool move_node(size_t id, int left, int top);
bool node_position(size_t id, int &left, int &top) const;
int handle(int event) override;
protected:
void draw() override;
private:
struct Position { size_t id = SIZE_MAX; int left = 0, top = 0; };
std::array<Position, 256> positions_{};
size_t count_ = 0U;
const xtc_graph *graph_;
EditHandler handler_;
void *context_;
Fl_Box *feedback_;
size_t selected_node_ = SIZE_MAX;
xtc_connection_info selected_edge_{};
bool edge_selected_ = false;
bool dragging_ = false, wiring_ = false;
size_t wire_node_ = SIZE_MAX, wire_port_ = 0U;
int drag_x_ = 0, drag_y_ = 0, mouse_x_ = 0, mouse_y_ = 0;
void feedback(xtc_result result, const char *success);
bool hit_port(int left, int top, size_t &node, size_t &port) const;
bool port_position(size_t id, size_t port, int &left, int &top) const;
};
#endif

View file

@ -1,15 +1,18 @@
#include "main_window.hpp"
#include "graph_widget.hpp"
#include "xtc/xtc_version.h"
#include <FL/Fl.H>
#include <FL/Fl_Box.H>
#include <FL/Fl_Button.H>
#include <FL/Fl_Group.H>
#include <FL/Fl_Scroll.H>
#include <FL/Fl_Menu_Bar.H>
#include <FL/Fl_Tabs.H>
#include <FL/fl_ask.H>
#include <cstdio>
#include <cstring>
#include <stdexcept>
namespace {
void set_label(Fl_Box *box, const char *text)
@ -22,6 +25,12 @@ void set_label(Fl_Box *box, const char *text)
MainWindow::MainWindow()
: Fl_Double_Window(1000, 680, XTC_PROJECT_NAME " — modular audio workstation")
{
xtc_graph *initial_graph = nullptr;
xtc_result graph_result = xtc_graph_create(64U, &initial_graph);
if (graph_result != XTC_OK) throw std::runtime_error(xtc_result_string(graph_result));
graph_.reset(initial_graph);
graph_result = xtc_graph_require_output_nodes(graph_.get(), 1);
if (graph_result != XTC_OK) throw std::runtime_error(xtc_result_string(graph_result));
begin();
auto *menu = new Fl_Menu_Bar(0, 0, 1000, 28);
menu->add("File/New (not implemented)", 0, nullptr, nullptr, FL_MENU_INACTIVE);
@ -45,7 +54,7 @@ MainWindow::MainWindow()
indicator_->align(FL_ALIGN_LEFT | FL_ALIGN_INSIDE);
indicator_->labelfont(FL_BOLD);
auto *hint = new Fl_Box(12, 78, 976, 24,
"Engine controls only. Playback content and recording are not implemented; no ports are connected automatically.");
"Edit signal flow in Modular Graph. No hardware ports are connected automatically; sequencer and recording are not implemented.");
hint->align(FL_ALIGN_LEFT | FL_ALIGN_INSIDE);
hint->labelsize(12);
@ -61,7 +70,37 @@ MainWindow::MainWindow()
mixer->end();
auto *graph = new Fl_Group(8, 140, 984, 444, "Modular Graph");
graph->begin();
new Fl_Box(24, 164, 940, 40, "Modular graph placeholder — visual graph editing is not implemented yet.");
graph_state_ = new Fl_Box(20, 182, 956, 24, "Graph: offline");
graph_state_->align(FL_ALIGN_LEFT | FL_ALIGN_INSIDE);
graph_state_->labelsize(12);
auto *feedback = new Fl_Box(20, 548, 956, 28,
"Add nodes. Drag output to input to connect; click a node or wire then Delete. Scroll to pan.");
feedback->align(FL_ALIGN_LEFT | FL_ALIGN_INSIDE | FL_ALIGN_WRAP);
feedback->labelsize(12);
auto *scroll = new Fl_Scroll(20, 212, 956, 328);
graph_widget_ = new GraphWidget(20, 212, 2000, 4000, graph_.get(), edit_graph, this, feedback);
scroll->end();
auto *oscillator = new Fl_Button(20, 150, 130, 26, "Add oscillator");
oscillator->callback([](Fl_Widget *, void *context) {
(void)static_cast<GraphWidget *>(context)->add_node(XTC_NODE_OSCILLATOR);
}, graph_widget_);
auto *gain = new Fl_Button(158, 150, 110, 26, "Add gain");
gain->callback([](Fl_Widget *, void *context) {
(void)static_cast<GraphWidget *>(context)->add_node(XTC_NODE_GAIN);
}, graph_widget_);
auto *meter = new Fl_Button(276, 150, 110, 26, "Add meter");
meter->callback([](Fl_Widget *, void *context) {
(void)static_cast<GraphWidget *>(context)->add_node(XTC_NODE_METER);
}, graph_widget_);
auto *output = new Fl_Button(394, 150, 110, 26, "Add output");
output->callback([](Fl_Widget *, void *context) {
(void)static_cast<GraphWidget *>(context)->add_node(XTC_NODE_OUTPUT);
}, graph_widget_);
auto *remove = new Fl_Button(512, 150, 130, 26, "Delete selection");
remove->callback([](Fl_Widget *, void *context) {
(void)static_cast<GraphWidget *>(context)->delete_selected();
}, graph_widget_);
graph->resizable(scroll);
graph->end();
tabs_->end();
tabs_->value(sequencer);
@ -104,6 +143,22 @@ void MainWindow::report_error(const char *operation, xtc_result result)
set_label(error_, text);
}
xtc_result MainWindow::edit_graph(void *context, const xtc_graph_edit &edit, size_t *out_added_id)
{
auto *window = static_cast<MainWindow *>(context);
xtc_graph *candidate = nullptr;
size_t added_id = SIZE_MAX;
xtc_result result = xtc_graph_apply_edit(window->graph_.get(), &edit, &candidate, &added_id);
if (result != XTC_OK) return result;
if (window->ready_) result = xtc_backend_submit(window->backend_, candidate);
if (result != XTC_OK) { xtc_graph_destroy(candidate); return result; }
window->graph_.reset(candidate);
window->graph_widget_->set_graph(candidate);
if (out_added_id != nullptr && edit.kind == XTC_EDIT_ADD_NODE) *out_added_id = added_id;
window->refresh_status();
return XTC_OK;
}
void MainWindow::open_backend()
{
xtc_backend_config config = {XTC_BACKEND_JACK, XTC_PROJECT_NAME, 65536U, 1U};
@ -111,7 +166,13 @@ void MainWindow::open_backend()
ready_ = result == XTC_OK;
running_ = false;
if (result != XTC_OK) report_error("Open JACK failed", result);
else set_label(error_, "");
else {
result = xtc_backend_submit(backend_, graph_.get());
if (result != XTC_OK) {
ready_ = false; // Handle is retained for close/reconnect; graph was not installed.
report_error("Submit graph failed", result);
} else set_label(error_, "");
}
refresh_status();
}
@ -157,12 +218,14 @@ void MainWindow::refresh_status()
set_label(indicator_, "Audio: disconnected");
set_label(status_, "JACK: not connected — use Reconnect after starting the server");
indicator_->labelcolor(fl_rgb_color(255, 155, 120));
set_label(graph_state_, "Graph: offline — edits stored locally; Reconnect to play");
} else {
xtc_backend_status status = {};
xtc_result result = xtc_backend_get_status(backend_, &status);
if (result != XTC_OK) {
report_error("Read engine status failed", result);
set_label(indicator_, "Audio: status unavailable");
set_label(graph_state_, "Graph: engine status unavailable");
set_label(status_, "Unable to read JACK status — use Reconnect");
indicator_->labelcolor(fl_rgb_color(255, 155, 120));
} else {
@ -172,7 +235,7 @@ void MainWindow::refresh_status()
const char *state = status.server_lost ? "Audio: server lost — Reconnect required" :
status.sample_rate_changed ? "Audio: sample rate changed — Reconnect required" :
status.process_result != XTC_OK ? "Audio: processing error" :
running_ ? "Audio: running (silence — no playback content)" : "Audio: ready / stopped";
running_ ? "Audio: running — modular graph" : "Audio: ready / stopped";
set_label(indicator_, state);
indicator_->labelcolor(fault ? fl_rgb_color(255, 155, 120) :
running_ ? fl_rgb_color(125, 225, 160) : fl_rgb_color(215, 215, 215));
@ -181,6 +244,11 @@ void MainWindow::refresh_status()
status.sample_rate, status.buffer_frames, status.processed_blocks, status.xruns,
xtc_result_string(status.process_result));
set_label(status_, text);
if (fault) set_label(graph_state_, "Graph: engine unavailable — Reconnect required");
else if (status.submitted_revision != status.active_revision)
set_label(graph_state_, running_ ? "Graph: queued — engine still processing previous graph" :
"Graph: prepared — starts on Play");
else set_label(graph_state_, running_ ? "Graph: active in engine" : "Graph: stopped");
}
xtc_backend_collect(backend_);
}
@ -229,7 +297,7 @@ void MainWindow::view_callback(Fl_Widget *widget, void *view)
}
void MainWindow::about_callback(Fl_Widget *, void *)
{
fl_message("%s %s\nModular audio workstation\nPhase 3: JACK engine controls; editors remain placeholders.",
fl_message("%s %s\nModular audio workstation\nPhase 4: modular graph editor; sequencer and mixer remain placeholders.",
XTC_PROJECT_NAME, XTC_VERSION_STRING);
}
void MainWindow::timer_callback(void *context)

View file

@ -2,8 +2,10 @@
#define XTC_MAIN_WINDOW_HPP
#include <FL/Fl_Double_Window.H>
#include <memory>
#include "xtc/xtc_backend.h"
class GraphWidget;
class Fl_Box;
class Fl_Button;
class Fl_Tabs;
@ -18,6 +20,9 @@ public:
int handle(int event) override;
private:
std::unique_ptr<xtc_graph, decltype(&xtc_graph_destroy)> graph_{nullptr, xtc_graph_destroy};
GraphWidget *graph_widget_ = nullptr;
Fl_Box *graph_state_ = nullptr;
xtc_backend *backend_ = nullptr;
bool ready_ = false;
bool running_ = false;
@ -29,6 +34,7 @@ private:
Fl_Box *error_ = nullptr;
Fl_Tabs *tabs_ = nullptr;
static xtc_result edit_graph(void *context, const xtc_graph_edit &edit, size_t *out_added_id);
void open_backend();
void start_engine();
void stop_engine();