flatudp: the signal-row editor form with a per-row Vector as drop-down, its probe line, README and architecture notes

Multicast was verified on a loopback group (239.255.42.99 on 127.0.0.1); a real network was not exercised.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
Martino FerrariandClaude Opus 4.6 committed 2026-11-02 01:23:51 +01:00
1 parent e3bc1f72f3
commit d955baf612
6 files changed
+385 -20

No files matched your search

+191 -17
View File
@@ -6,10 +6,14 @@
#include <adwaita.h>
#include <algorithm>
#include <cctype>
#include <cstdint>
#include <cstdio>
#include <functional>
#include <memory>
#include <string>
#include <vector>
namespace udpscope {
namespace {
@@ -23,6 +27,13 @@ std::string num(double v) {
return b;
}
std::string trim(const std::string& s) {
std::size_t a = 0, b = s.size();
while (a < b && std::isspace(static_cast<unsigned char>(s[a]))) ++a;
while (b > a && std::isspace(static_cast<unsigned char>(s[b - 1]))) --b;
return s.substr(a, b - a);
}
GtkWidget* entry_row(const char* title) {
GtkWidget* r = adw_entry_row_new();
adw_preferences_row_set_title(ADW_PREFERENCES_ROW(r), title);
@@ -66,18 +77,70 @@ void mark(GtkWidget* w, bool bad) {
}
}
/// The form's widgets, shared by every signal closure and every `ConfigForm` closure.
struct Form {
GtkWidget *port, *bind, *multicast, *iface, *rate, *endian, *signals;
/// One signal's editing widgets; `row` is the `AdwEntryRow` that holds the others. `vec` ("Vector as":
/// Channels or Waveform) is shown only while `count` is above 1.
struct SigRow {
GtkWidget *row, *type, *count, *vec;
};
/// The form's widgets, shared by every signal closure and every `ConfigForm` closure. The raw
/// widget pointers are valid while the chooser keeps the rows.
struct Form : std::enable_shared_from_this<Form> {
GtkWidget *port, *bind, *multicast, *iface, *rate, *endian, *group;
std::vector<SigRow> sigs;
ChangeCb changed = std::make_shared<std::function<void()>>();
bool updating = false;
bool alive = true; ///< cleared when the chooser destroys the rows; handlers then touch nothing
/// A bad address is marked with the `error` style; the rest of the problem is `valid()`'s.
std::vector<Row> rows_now() const {
std::vector<Row> rows;
for (const SigRow& s : sigs) {
Row r;
r.name = trim(text_of(s.row));
r.type = static_cast<Type>(std::min<guint>(gtk_drop_down_get_selected(GTK_DROP_DOWN(s.type)), 9));
r.count = static_cast<std::uint32_t>(gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(s.count)));
r.wave = r.count > 1 && gtk_drop_down_get_selected(GTK_DROP_DOWN(s.vec)) == 1;
rows.push_back(r);
}
return rows;
}
/// The first `S<n>` that no row uses.
std::string next_name() const {
const std::vector<Row> rows = rows_now();
for (int n = 1;; ++n) {
const std::string name = "S" + std::to_string(n);
bool used = false;
for (const Row& r : rows) used = used || r.name == name;
if (!used) return name;
}
}
/// Bad addresses are marked with the `error` style; the group's subtitle carries the signal list's
/// own state, the first rule it breaks or the size it implies.
void refresh() {
mark(bind, !text_of(bind).empty() && !valid_ipv4(text_of(bind)));
mark(multicast, !text_of(multicast).empty() && !valid_multicast(text_of(multicast)));
mark(iface, !text_of(iface).empty() && !valid_ipv4(text_of(iface)));
// "Vector as" belongs to a vector row only; a row back at one element returns to Channels.
for (const SigRow& s : sigs) {
const bool vec = gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(s.count)) > 1;
gtk_widget_set_visible(s.vec, vec);
if (!vec && gtk_drop_down_get_selected(GTK_DROP_DOWN(s.vec)) != 0)
gtk_drop_down_set_selected(GTK_DROP_DOWN(s.vec), 0);
}
const std::vector<Row> rows = rows_now();
const std::string p = flatudp::problem(rows);
std::string sub = p;
if (p.empty()) {
sub = std::to_string(channel_count(rows)) + (channel_count(rows) == 1 ? " channel, " : " channels, ") +
std::to_string(datagram_size(rows)) + " bytes per datagram";
}
adw_expander_row_set_subtitle(ADW_EXPANDER_ROW(group), sub.c_str());
}
void fire() {
if (!updating && changed && *changed) (*changed)();
}
SourceConfig read() const {
@@ -88,10 +151,13 @@ struct Form {
if (!text_of(iface).empty()) c.set("iface", text_of(iface));
c.set("rate", num(value_of(rate)));
c.set("endian", sel_of(endian) == 1 ? "big" : "little");
c.set("signals", text_of(signals));
c.set("signals", format(rows_now()));
return c;
}
void add_row(const Row& r);
void remove_row(GtkWidget* row);
void write(const SourceConfig& cfg) {
const FlatUdpSettings s = FlatUdpSettings::from(cfg);
updating = true;
@@ -101,7 +167,12 @@ struct Form {
gtk_editable_set_text(GTK_EDITABLE(iface), s.iface.c_str());
if (s.rate > 0.0) adw_spin_row_set_value(ADW_SPIN_ROW(rate), s.rate);
adw_combo_row_set_selected(ADW_COMBO_ROW(endian), s.big_endian ? 1 : 0);
gtk_editable_set_text(GTK_EDITABLE(signals), cfg.get("signals").c_str());
while (!sigs.empty()) {
GtkWidget* row = sigs.back().row;
sigs.pop_back();
adw_expander_row_remove(ADW_EXPANDER_ROW(group), row);
}
for (const Row& r : s.rows) add_row(r);
updating = false;
refresh();
}
@@ -111,21 +182,53 @@ struct Form {
using FormPtr = std::shared_ptr<Form>;
/// A remove button's context: which form, which row.
struct RowCtx {
FormPtr form;
GtkWidget* row;
};
void on_notify(GObject*, GParamSpec*, gpointer user) {
Form& f = **static_cast<FormPtr*>(user);
if (!f.alive) return;
f.refresh();
if (!f.updating && f.changed && *f.changed) (*f.changed)();
f.fire();
}
void on_edited(GtkEditable*, gpointer user) {
Form& f = **static_cast<FormPtr*>(user);
if (!f.alive) return;
f.refresh();
if (!f.updating && f.changed && *f.changed) (*f.changed)();
f.fire();
}
void on_spin(GtkSpinButton*, gpointer user) {
Form& f = **static_cast<FormPtr*>(user);
if (!f.alive) return;
f.refresh();
f.fire();
}
void on_add(GtkButton*, gpointer user) {
FormPtr f = *static_cast<FormPtr*>(user);
if (!f->alive) return;
f->add_row(Row{f->next_name(), Type::F32, 1});
adw_expander_row_set_expanded(ADW_EXPANDER_ROW(f->group), TRUE);
f->refresh();
f->fire();
}
void on_remove(GtkButton*, gpointer user) {
// Copy first: removing the row finalizes this button, and with it this context.
RowCtx* c = static_cast<RowCtx*>(user);
FormPtr f = c->form;
GtkWidget* row = c->row;
if (!f->alive) return;
f->remove_row(row);
}
void free_form(gpointer d, GClosure*) { delete static_cast<FormPtr*>(d); }
void free_ctx(gpointer d, GClosure*) { delete static_cast<RowCtx*>(d); }
/// "destroy" on a row group: the siblings the handlers reach by raw pointer may already be gone.
void on_form_destroy(GtkWidget*, gpointer user) { (**static_cast<FormPtr*>(user)).alive = false; }
@@ -145,6 +248,70 @@ void hook_entry(GtkWidget* w, const FormPtr& f) {
static_cast<GConnectFlags>(0));
}
void Form::add_row(const Row& r) {
FormPtr self = shared_from_this();
GtkWidget* row = adw_entry_row_new();
adw_preferences_row_set_title(ADW_PREFERENCES_ROW(row), "Name");
gtk_editable_set_text(GTK_EDITABLE(row), r.name.c_str());
const char* names[11];
for (std::size_t i = 0; i < 10; ++i) names[i] = type_name(static_cast<Type>(i));
names[10] = nullptr;
GtkWidget* type = gtk_drop_down_new_from_strings(names);
gtk_drop_down_set_selected(GTK_DROP_DOWN(type), static_cast<guint>(r.type));
gtk_widget_set_valign(type, GTK_ALIGN_CENTER);
gtk_widget_set_name(type, "flatudp-type");
// The spin clamps to 1..4096, so a hand-edited session with a count outside it is repaired here.
GtkWidget* count = gtk_spin_button_new_with_range(1, kMaxCount, 1);
gtk_spin_button_set_value(GTK_SPIN_BUTTON(count), r.count);
gtk_widget_set_valign(count, GTK_ALIGN_CENTER);
gtk_widget_set_tooltip_text(count, "Elements: 1 is a scalar signal, more make a vector signal");
gtk_widget_set_name(count, "flatudp-count");
static const char* const kVec[] = {"Channels", "Waveform", nullptr};
GtkWidget* vec = gtk_drop_down_new_from_strings(kVec);
gtk_drop_down_set_selected(GTK_DROP_DOWN(vec), r.wave && r.count > 1 ? 1 : 0);
gtk_widget_set_valign(vec, GTK_ALIGN_CENTER);
gtk_widget_set_visible(vec, r.count > 1);
gtk_widget_set_tooltip_text(vec,
"Vector as: Channels makes one channel per element, one sample per cycle; "
"Waveform makes one channel of all the elements per cycle");
gtk_widget_set_name(vec, "flatudp-vector");
GtkWidget* rm = gtk_button_new_with_label("Remove");
gtk_widget_set_valign(rm, GTK_ALIGN_CENTER);
gtk_widget_set_name(rm, "flatudp-remove");
adw_entry_row_add_suffix(ADW_ENTRY_ROW(row), type);
adw_entry_row_add_suffix(ADW_ENTRY_ROW(row), count);
adw_entry_row_add_suffix(ADW_ENTRY_ROW(row), vec);
adw_entry_row_add_suffix(ADW_ENTRY_ROW(row), rm);
hook_entry(row, self);
hook_notify(type, "notify::selected", self);
hook_notify(vec, "notify::selected", self);
g_signal_connect_data(count, "value-changed", G_CALLBACK(on_spin), new FormPtr(self), free_form,
static_cast<GConnectFlags>(0));
g_signal_connect_data(rm, "clicked", G_CALLBACK(on_remove), new RowCtx{self, row}, free_ctx,
static_cast<GConnectFlags>(0));
adw_expander_row_add_row(ADW_EXPANDER_ROW(group), row);
sigs.push_back(SigRow{row, type, count, vec});
}
void Form::remove_row(GtkWidget* row) {
for (auto it = sigs.begin(); it != sigs.end(); ++it) {
if (it->row == row) {
sigs.erase(it);
adw_expander_row_remove(ADW_EXPANDER_ROW(group), row);
break;
}
}
refresh();
fire();
}
class FlatUdpKind final : public SourceKind {
public:
std::string id() const override { return "flatudp"; }
@@ -169,27 +336,34 @@ class FlatUdpKind final : public SourceKind {
f->iface = entry_row("Multicast _interface");
f->rate = spin_row("Cycle _rate (Hz)", 0.001, 1000000, 1, 1000, 3);
f->endian = combo_row("_Byte order", kEndian);
f->signals = entry_row("_Signals");
GtkWidget* net = expander("Network", {f->port, f->bind, f->multicast, f->iface});
GtkWidget* timing = expander("Timing and format", {f->rate, f->endian});
GtkWidget* sig = expander("Signals", {f->signals});
f->group = adw_expander_row_new();
adw_preferences_row_set_title(ADW_PREFERENCES_ROW(f->group), "Signals");
adw_expander_row_set_expanded(ADW_EXPANDER_ROW(net), TRUE);
adw_expander_row_set_expanded(ADW_EXPANDER_ROW(sig), TRUE);
adw_expander_row_set_expanded(ADW_EXPANDER_ROW(f->group), TRUE);
for (GtkWidget* w : {net, timing, sig}) hook_destroy(w, f);
for (GtkWidget* w : {f->bind, f->multicast, f->iface, f->signals}) hook_entry(w, f);
GtkWidget* add = gtk_button_new_with_label("Add signal");
gtk_widget_set_valign(add, GTK_ALIGN_CENTER);
gtk_widget_set_name(add, "flatudp-add");
adw_expander_row_add_suffix(ADW_EXPANDER_ROW(f->group), add);
g_signal_connect_data(add, "clicked", G_CALLBACK(on_add), new FormPtr(f), free_form,
static_cast<GConnectFlags>(0));
for (GtkWidget* w : {net, timing, f->group}) hook_destroy(w, f);
for (GtkWidget* w : {f->bind, f->multicast, f->iface}) hook_entry(w, f);
for (GtkWidget* w : {f->port, f->rate}) hook_notify(w, "notify::value", f);
hook_notify(f->endian, "notify::selected", f);
f->refresh();
ConfigForm out;
out.rows = {net, timing, sig};
out.rows = {net, timing, f->group};
out.description =
"The signal list must match the sender's exactly, in order: the datagrams carry no "
"description. Write name:type[count], comma separated, e.g. Trigger:uint8,Ia:float32,"
"Wave:float32[16]";
out.focus = f->signals;
"description, and a datagram of any other size is dropped. A vector signal is either "
"one channel per element or, as a waveform, one channel of all its elements";
out.focus = f->rate;
out.write = [f](const SourceConfig& c) { f->write(c); };
out.read = [f]() { return f->read(); };
out.valid = [f]() { return f->valid(); };