From 487ce934ff2099674d2a74f16e6e5a3f8fba5651 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?IOhannes=20m=20zm=C3=B6lnig?= Date: Tue, 25 Nov 2008 09:00:26 +0000 Subject: a new loader that automagically creates abstractions based on a template (which can be user-supplied) if the user tries to create a yet-unknown object. svn path=/trunk/externals/iem/iemguts/; revision=10380 --- src/Makefile | 12 ++-- src/autoabstraction.c | 155 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 161 insertions(+), 6 deletions(-) create mode 100644 src/autoabstraction.c diff --git a/src/Makefile b/src/Makefile index 02b3420..eeaaaa6 100644 --- a/src/Makefile +++ b/src/Makefile @@ -62,7 +62,7 @@ TARGETS=$(SOURCES:.c=.$(EXTENSION)) l_ia64 l_i386 pd_linux: $(TARGETS) -LINUXCFLAGS = -DPD -g -O2 -funroll-loops -fomit-frame-pointer -fPIC \ +LINUXCFLAGS = -DPD -DMISSING_LOADER_T -g -O2 -funroll-loops -fomit-frame-pointer -fPIC \ -Wall -W -Wshadow -Wstrict-prototypes \ -Wno-unused -Wno-parentheses -Wno-switch @@ -82,7 +82,7 @@ STRIP?=strip pd_darwin: $(TARGETS) -DARWINCFLAGS = -DPD -O2 -Wall -W -Wshadow -Wstrict-prototypes \ +DARWINCFLAGS = -DPD -DMISSING_LOADER_T -O2 -Wall -W -Wshadow -Wstrict-prototypes \ -Wno-unused -Wno-parentheses -Wno-switch DARWININCLUDE = -I$(PDSRCDIR) @@ -96,7 +96,7 @@ DARWINLDFLAGS = -bundle -undefined suppress -flat_namespace # ----------------------- IRIX 5.x ----------------------- pd_irix5: $(TARGETS) -SGICFLAGS5 = -o32 -DPD -DSGI -O2 +SGICFLAGS5 = -o32 -DPD -DMISSING_LOADER_T -DSGI -O2 SGIINCLUDE = -I$(PDSRCDIR) @@ -111,7 +111,7 @@ SGILDFLAGS = -elf -shared -rdata_shared # ----------------------- IRIX 6.x ----------------------- pd_irix6: $(TARGETS) -SGICFLAGS6 = -DPD -DSGI -n32 \ +SGICFLAGS6 = -DPD -DMISSING_LOADER_T -DSGI -n32 \ -OPT:roundoff=3 -OPT:IEEE_arithmetic=3 -OPT:cray_ivdep=true \ -Ofast=ip32 @@ -124,7 +124,7 @@ SGICFLAGS6 = -DPD -DSGI -n32 \ # ----------------------- NT ----------------------- dll: $(TARGETS) -PDNTCFLAGS = /W3 /WX /DPD /DNT /D__WIN32__ /DMSW /nologo +PDNTCFLAGS = /W3 /WX /DPD /DNT /D__WIN32__ /DMSW /DMISSING_LOADER_T /nologo VC="C:\Programme\Microsoft Visual Studio\Vc98" @@ -144,7 +144,7 @@ PDNTLIB = $(PDNTLDIR)\libc.lib \ pd_nt: $(TARGETS) -MINGWCFLAGS = -DPD -O2 -funroll-loops -fomit-frame-pointer \ +MINGWCFLAGS = -DPD -DMISSING_LOADER_T -O2 -funroll-loops -fomit-frame-pointer \ -Wall -W -Wshadow -Wstrict-prototypes \ -Wno-unused -Wno-parentheses -Wno-switch -mms-bitfields diff --git a/src/autoabstraction.c b/src/autoabstraction.c new file mode 100644 index 0000000..37e7a1c --- /dev/null +++ b/src/autoabstraction.c @@ -0,0 +1,155 @@ +/* Copyright (c) 2008 IOhannes m zmölnig @ IEM + * For information on usage and redistribution, and for a DISCLAIMER OF ALL + * WARRANTIES, see the file, "LICENSE.txt," that comes with Pd. + */ + +/* + * this code adds an external "loader" to Miller S. Puckette's "pure data", + * which allows the on-the-fly creation of abstractions by just creating an + * object which is of a yet unknown file and for which no abstraction-file exists + * + * the new abstraction should be minimal and eventually be loaded from a template + * + * possible default abstraction: '#N canvas 0 0 450 300 10; #X vis 1;' + * + */ + + +#ifdef __WIN32__ +# define MSW +#endif + +#include "m_pd.h" + + +typedef struct _autoabstraction +{ + t_object x_obj; +} t_autoabstraction; +static t_class *autoabstraction_class; + +static char *version = "$Revision: 0.1 $"; + +/* this is the name of the filename that get's loaded as a default template for new abstractions */ +static char*templatefilename="autoabstraction.template"; +/* if the loading above fails, we resort to a simple default abstraction that automatically opens up */ +static char*templatestring="#N canvas 0 0 450 300 10; #X vis 1;"; + + +#if (PD_MINOR_VERSION >= 40) + +#include "s_stuff.h" +#include "g_canvas.h" +#include +#include +#include + +#ifdef UNISTD +# include +# include +#endif +#ifdef _WIN32 +# include +# include +#endif + +#ifdef MISSING_LOADER_T +/* definitions taken from s_loader.c, since they weren't in header orignally */ +typedef int (*loader_t)(t_canvas *canvas, char *classname); +void sys_register_loader(loader_t loader); +void class_set_extern_dir(t_symbol *s); +#endif + + +static t_binbuf*aa_bb=0; + + +static void autoabstraction_save(t_canvas*canvas, char*classname) { + t_binbuf*bb=0; + + if(!bb) + bb=aa_bb; + + if(bb) { + char name[MAXPDSTRING]; + snprintf(name, MAXPDSTRING, "%s.pd", classname); + + binbuf_write(bb, name, "", 0); + } else { + verbose(1, "[autoabstraction]: no template"); + } +} + + +/** + * the loader + * + * @param canvas the context of the object to be created + * @param classname the name of the object (external, library) to be created + * @return 1 on success, 0 on failure + */ +static int autoabstraction_loader(t_canvas *canvas, char *classname) +{ + /* check whether there is an abstraction with the given within the scope of */ + int fd=0; + char dirbuf[MAXPDSTRING], *nameptr; + + if((fd=canvas_open(canvas, classname, ".pd", dirbuf, &nameptr, MAXPDSTRING, 0)) >= 0 || + (fd=canvas_open(canvas, classname, ".pat", dirbuf, &nameptr, MAXPDSTRING, 0)) >= 0) + { + /* oops, there's already an abstraction of the given */ + close(fd); + return(0); + } + + + autoabstraction_save(canvas, classname); + + + /* we always fail, because we want Pd to do the real opening of abstractions */ + return 0; +} + +static void autoabstraction_initialize(void) +{ + aa_bb=binbuf_new(); + + /* try to read a template file */ + if(binbuf_read(aa_bb, templatefilename, "", 0)) { + /* if this fails, use the default template */ + size_t length=strlen(templatestring); + + binbuf_text(aa_bb, templatestring, length); + } +} + + + +#endif /* PD_MINOR_VERSION >= 40 */ + + + +static void*autoabstraction_new(t_symbol *s, int argc, t_atom *argv) +{ + t_autoabstraction*x = (t_autoabstraction*)pd_new(autoabstraction_class); + return (x); +} + +void autoabstraction_setup(void) +{ + /* relies on t.grill's loader functionality, fully added in 0.40 */ + post("automatic abstraction creator %s",version); + post("\twritten by IOhannes m zmoelnig, IEM "); + post("\tcompiled on "__DATE__" at "__TIME__ " "); + post("\tcompiled against Pd version %d.%d.%d.%s", PD_MAJOR_VERSION, PD_MINOR_VERSION, PD_BUGFIX_VERSION, PD_TEST_VERSION); + +#if (PD_MINOR_VERSION >= 40) + autoabstraction_initialize(); + sys_register_loader(autoabstraction_loader); +#else + error("to function, this needs to be compiled against Pd 0.40 or higher,\n"); + error("\tor a version that has sys_register_loader()"); +#endif + + autoabstraction_class = class_new(gensym("autoabstraction"), (t_newmethod)autoabstraction_new, 0, sizeof(t_autoabstraction), CLASS_NOINLET, A_GIMME, 0); +} -- cgit v1.2.1