[PD-dev] presets, a proposal

Krzysztof Czaja czaja at chopin.edu.pl
Tue Dec 2 10:26:20 CET 2003


hi all, hi Miller,

how about adding to the core the following, minimal interface, and
letting people define state-saving externals, like Guenter's
state, cyclone's preset, or anything else:

--- m_pd.h~     2003-10-01 06:05:28.000000000 +0200
+++ m_pd.h      2003-12-02 09:42:37.000000000 +0100
@@ -428,4 +428,8 @@
  EXTERN void class_setpropertiesfn(t_class *c, t_propertiesfn f);
  EXTERN t_propertiesfn class_getpropertiesfn(t_class *c);
+        /* prototype for functions to store Pd's state to a binbuf */
+typedef int (*t_statefn)(t_gobj *x, t_binbuf *b, int flags);
+EXTERN void class_setstatefn(t_class *c, t_statefn f);
+EXTERN t_statefn class_getstatefn(t_class *c);

  #ifndef PD_CLASS_DEF

--- m_imp.h~    2003-08-27 18:30:15.000000000 +0200
+++ m_imp.h     2003-12-02 09:33:23.000000000 +0100
@@ -47,4 +47,5 @@
      t_savefn c_savefn;                 /* function to call when saving */
      t_propertiesfn c_propertiesfn;     /* function to start prop dialog */
+    t_statefn c_statefn;               /* function to store a state */
      int c_floatsignalin;               /* onset to float for signal input */
      char c_gobj;                       /* true if is a gobj */

--- m_class.c~  2003-09-16 06:39:26.000000000 +0200
+++ m_class.c   2003-12-02 09:36:59.000000000 +0100
@@ -209,4 +209,5 @@
      c->c_externdir = class_extern_dir;
      c->c_savefn = (typeflag == CLASS_PATCHABLE ? text_save : class_nosavefn);
+    c->c_statefn = 0;
  #if 0
      post("class: %s", c->c_name->s_name);
@@ -446,4 +447,14 @@
  }

+void class_setstatefn(t_class *c, t_statefn f)
+{
+    c->c_statefn = f;
+}
+
+t_statefn class_getstatefn(t_class *c)
+{
+    return (c->c_statefn);
+}
+

That is all, really.  Any class with a state, would then define
something like the gatom below:

--- g_text.c~   2003-10-27 22:09:10.000000000 +0100
+++ g_text.c    2003-12-02 09:43:10.000000000 +0100
@@ -862,4 +862,14 @@
  }

+static int gatom_state(t_gobj *z, t_binbuf *bb, int flags)
+{
+    t_gatom *x = (t_gatom *)z;
+    t_atom at;
+    SETSYMBOL(&at, gensym("set"));
+    binbuf_add(bb, 1, &at);
+    binbuf_add(bb, 1, &x->a_atom);
+    binbuf_addsemi(bb);
+    return (0);
+}

  /* -------------------- widget behavior for text objects ------------ */
@@ -1308,4 +1318,5 @@
      class_setwidget(gatom_class, &gatom_widgetbehavior);
      class_setpropertiesfn(gatom_class, gatom_properties);
+    class_setstatefn(gatom_class, gatom_state);
  }

Storing a glist's state into a preset/state/whatever requires only
this:

t_gobj *g;
t_binbuf *bb = binbuf_new();
int id;
for (g = x->x_glist->gl_list, id = 0; g; g = g->g_next, id++)
{
     t_object *ob;
     t_statefn statefn;
     if ((ob = pd_checkobject((t_pd *)g)) &&
         (statefn = class_getstatefn(*(t_pd *)g)))
     {
         binbuf_addv(bb, "isii", id, gensym(class_getname(*(t_pd *)g)),
                     (int)ob->te_xpix, (int)ob->te_ypix);
         (*statefn)(g, bb, 0);
     }
}
/* do what you like with the binbuf here */
binbuf_free(bb);

Validation could be based on checking an id against a class name,
and, optionally, a position.

Krzysztof





More information about the Pd-dev mailing list