shithub: neindaw

Download patch

ref: 8e7cbb0dc376fc473f627250402fab3f20192e78
parent: bdd619996e8c81b02d0f51cb9602dc588d17e1bf
author: Sigrid Haflínudóttir <ftrvxmtrx@gmail.com>
date: Wed Mar 18 19:31:39 EDT 2020

provide a way to set custom readstr/writestr for UI elements

--- a/aux.h
+++ b/aux.h
@@ -41,8 +41,8 @@
 	float min;
 	float max;
 	float step;
-	char *(*readstr)(UI *ui, int type, char *s, int sz);
-	int (*write)(UI *ui, int type, char *s);
+	char *(*readstr)(UI *ui, int auxtype, char *s, int sz);
+	int (*writestr)(UI *ui, int auxtype, char *s);
 
 	Meta *meta;
 	int nummeta;
--- a/ay38910/ay38910.c
+++ b/ay38910/ay38910.c
@@ -341,7 +341,6 @@
 {
 	Aux *a, *o;
 	char b[256];
-	int st;
 
 	if (r->ifcall.count >= sizeof(b)) {
 		respond(r, "can't fit into buffer");
@@ -356,8 +355,12 @@
 	switch (a->type) {
 	case Xuictl:
 		o = auxtype2obj(&a->type);
-		st = o->ui->write != nil ? o->ui->write(o->ui, a->type, b) : -1;
-		respond(r, st == 0 ? nil : "write failed");
+		if (o->ui->writestr == nil)
+			respond(r, "not implemented");
+		else if (o->ui->writestr(o->ui, a->type, b) >= 0)
+			respond(r, nil);
+		else
+			responderror(r);
 		break;
 	case Xdspctl: /* FIXME changing sampling rate */
 		o = auxtype2obj(&a->type);
--- a/dsp/fs.c
+++ b/dsp/fs.c
@@ -223,7 +223,6 @@
 {
 	Aux *a, *o;
 	char b[256];
-	int st;
 
 	if (r->ifcall.count >= sizeof(b)) {
 		respond(r, "can't fit into buffer");
@@ -238,8 +237,12 @@
 	switch (a->type) {
 	case Xuictl:
 		o = auxtype2obj(&a->type);
-		st = o->ui->write != nil ? o->ui->write(o->ui, a->type, b) : -1;
-		respond(r, st == 0 ? nil : "write failed");
+		if (o->ui->writestr == nil)
+			respond(r, "not implemented");
+		else if (o->ui->writestr(o->ui, a->type, b) >= 0)
+			respond(r, nil);
+		else
+			responderror(r);
 		break;
 	case Xdspctl: /* FIXME changing sampling rate */
 		o = auxtype2obj(&a->type);
--- a/uiglue.c
+++ b/uiglue.c
@@ -13,13 +13,13 @@
 	int nummeta;
 }decl;
 
-static char *
-ui_readstr(UI *ui, int type, char *s, int sz)
+char *
+ui_readstr(UI *ui, int auxtype, char *s, int sz)
 {
 	char *x, *t;
 	int i;
 
-	if (type == Xuictl) {
+	if (auxtype == Xuictl) {
 		if (ui->type < 0 || ui->type >= UInum)
 			sysfatal("unknown ui type %d", ui->type);
 		t = uitypenames[ui->type];
@@ -45,7 +45,7 @@
 		default:
 			sysfatal("readstr not implemented for ui type %d", ui->type);
 		}
-	} else if (type == Xuimeta) {
+	} else if (auxtype == Xuimeta) {
 		x = s;
 		*x = 0;
 		for (i = 0; i < ui->nummeta; i++)
@@ -52,21 +52,21 @@
 			x = seprint(x, s+sz-1, "%s\t%s\n", ui->meta[i].k, ui->meta[i].v);
 		return s;
 	} else {
-		sysfatal("unknown ui file type %d", type);
+		sysfatal("unsupported ui aux %d", auxtype);
 	}
 
 	return nil;
 }
 
-static int
-ui_write(UI *ui, int type, char *s)
+int
+ui_writestr(UI *ui, int auxtype, char *s)
 {
 	char *e;
 	int failoor;
 	float v;
 
-	if (type != Xuictl)
-		sysfatal("unknown ui file %d", type);
+	if (auxtype != Xuictl)
+		sysfatal("unsupported ui aux %d", auxtype);
 
 	/* FIXME optional argument should specify at which frame to apply the change */
 
@@ -123,8 +123,8 @@
 	a->metadata = Xuimeta;
 	a->ui->zone = decl.zone;
 	a->ui->label = label;
-	a->ui->readstr = ui_readstr;
-	a->ui->write = ui_write;
+	a->ui->readstr = uiglue.readstr != nil ? uiglue.readstr : ui_readstr;
+	a->ui->writestr = uiglue.writestr != nil ? uiglue.writestr : ui_writestr;
 	if ((uiglue.f = createfile(f, label, nil, DMDIR|0775, a)) == nil)
 		sysfatal("failed to create ui: %r");
 	if ((f = createfile(uiglue.f, "ctl", nil, 0664, &a->ctl)) == nil)
--- a/uiglue.h
+++ b/uiglue.h
@@ -2,6 +2,7 @@
 typedef struct MetaGlue MetaGlue;
 
 struct File;
+struct UI;
 
 struct UIGlue {
 	union {
@@ -20,6 +21,9 @@
 	void (*addHorizontalBargraph)(void *uiInterface, const char *label, float *zone, float min, float max);
 	void (*addVerticalBargraph)(void *uiInterface, const char *label, float *zone, float min, float max);
 	void (*declare)(void *uiInterface, float *zone, const char *key, const char *value);
+
+	char *(*readstr)(struct UI *ui, int auxtype, char *s, int sz);
+	int (*writestr)(struct UI *ui, int auxtype, char *s);
 };
 
 struct MetaGlue {
@@ -28,3 +32,6 @@
 };
 
 extern UIGlue uiglue;
+
+char *ui_readstr(struct UI *ui, int type, char *s, int sz);
+int ui_writestr(struct UI *ui, int type, char *s);