m_class.c 30.3 KB
Newer Older
Miller Puckette's avatar
Miller Puckette committed
1
2
3
4
/* Copyright (c) 1997-1999 Miller Puckette.
* For information on usage and redistribution, and for a DISCLAIMER OF ALL
* WARRANTIES, see the file, "LICENSE.txt," in this distribution.  */

Hans-Christoph Steiner's avatar
Hans-Christoph Steiner committed
5
6
#include "config.h"

Miller Puckette's avatar
Miller Puckette committed
7
8
9
10
11
#define PD_CLASS_DEF
#include "m_pd.h"
#include "m_imp.h"
#include "s_stuff.h"
#include <stdlib.h>
Hans-Christoph Steiner's avatar
Hans-Christoph Steiner committed
12
13

#ifdef HAVE_UNISTD_H
Miller Puckette's avatar
Miller Puckette committed
14
15
#include <unistd.h>
#endif
Hans-Christoph Steiner's avatar
Hans-Christoph Steiner committed
16
17

#ifdef HAVE_IO_H
Miller Puckette's avatar
Miller Puckette committed
18
19
20
21
22
23
24
#include <io.h>
#endif

#include <stdarg.h>
#include <string.h>
#include <stdio.h>

25
26
27
28
#ifdef _MSC_VER  /* This is only for Microsoft's compiler, not cygwin, e.g. */
#define snprintf sprintf_s
#endif

29
t_symbol *class_loadsym;     /* name under which an extern is invoked */
Miller Puckette's avatar
Miller Puckette committed
30
static void pd_defaultfloat(t_pd *x, t_float f);
Hans-Christoph Steiner's avatar
Hans-Christoph Steiner committed
31
static void pd_defaultblob(t_pd *x, t_blob *st); /* MP20061226 blob type */
Miller Puckette's avatar
Miller Puckette committed
32
33
34
35
static void pd_defaultlist(t_pd *x, t_symbol *s, int argc, t_atom *argv);
t_pd pd_objectmaker;    /* factory for creating "object" boxes */
t_pd pd_canvasmaker;    /* factory for creating canvases */

36
37
38
39
40
41
42
43
typedef struct _classtable
{
    t_class *ct_class;
    struct _classtable *ct_next;
} t_classtable;

t_classtable *ct;

Miller Puckette's avatar
Miller Puckette committed
44
45
static t_symbol *class_extern_dir = &s_;

46
int symbol_can_float(t_symbol *s, t_float *f);
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68

char *type_hint(t_symbol *s, int argc, t_atom *argv)
{
    static char hint[MAXPDSTRING];
    t_float f;
    if (!s)
        sprintf(hint, " (null selector detected)");
    else if (s == &s_)
        sprintf(hint, " (empty symbol selector)");
    else if (s && s == &s_symbol && argc && argv->a_type == A_SYMBOL
            && argv->a_w.w_symbol == &s_)
        sprintf(hint, " (empty symbol message detected)");
    else if (symbol_can_float(s, &f))
        sprintf(hint, " (%s is actually a symbol atom here)", s->s_name);
    else if (symbol_can_float(atom_getsymbolarg(0, argc, argv), &f))
        sprintf(hint, " (symbol message with floatlike payload detected."
                      " Did you mean 'float %s'?)", argv->a_w.w_symbol->s_name);
    else
        hint[0] = '\0';
    return hint;
}

Miller Puckette's avatar
Miller Puckette committed
69
70
static void pd_defaultanything(t_pd *x, t_symbol *s, int argc, t_atom *argv)
{
71
72
    pd_error(x, "%s: no method for '%s'%s",
        (*x)->c_name->s_name, s->s_name, type_hint(s, argc, argv));
Miller Puckette's avatar
Miller Puckette committed
73
74
75
76
77
78
79
80
81
}

static void pd_defaultbang(t_pd *x)
{
    if (*(*x)->c_listmethod != pd_defaultlist)
        (*(*x)->c_listmethod)(x, 0, 0, 0);
    else (*(*x)->c_anymethod)(x, &s_bang, 0, 0);
}

Hans-Christoph Steiner's avatar
Hans-Christoph Steiner committed
82
83
84
85
86
static void pd_defaultblob(t_pd *x, t_blob *st) /* MP 20061226 blob type */
{ /* for now just reject it, later convert to symbol/float/list */
    pd_error(x, "%s: no method for blob so far...", (*x)->c_name->s_name);
}

Miller Puckette's avatar
Miller Puckette committed
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
static void pd_defaultpointer(t_pd *x, t_gpointer *gp)
{
    if (*(*x)->c_listmethod != pd_defaultlist)
    {
        t_atom at;
        SETPOINTER(&at, gp);
        (*(*x)->c_listmethod)(x, 0, 1, &at);
    }
    else
    {
        t_atom at;
        SETPOINTER(&at, gp);
        (*(*x)->c_anymethod)(x, &s_pointer, 1, &at);
    }
}

static void pd_defaultfloat(t_pd *x, t_float f)
{
    if (*(*x)->c_listmethod != pd_defaultlist)
    {
        t_atom at;
        SETFLOAT(&at, f);
        (*(*x)->c_listmethod)(x, 0, 1, &at);
    }
    else
    {
        t_atom at;
        SETFLOAT(&at, f);
        (*(*x)->c_anymethod)(x, &s_float, 1, &at);
    }
}

static void pd_defaultsymbol(t_pd *x, t_symbol *s)
{
    if (*(*x)->c_listmethod != pd_defaultlist)
    {
        t_atom at;
        SETSYMBOL(&at, s);
        (*(*x)->c_listmethod)(x, 0, 1, &at);
    }
    else
    {
        t_atom at;
        SETSYMBOL(&at, s);
        (*(*x)->c_anymethod)(x, &s_symbol, 1, &at);
    }
}

void obj_list(t_object *x, t_symbol *s, int argc, t_atom *argv);
static void class_nosavefn(t_gobj *z, t_binbuf *b);

    /* handle "list" messages to Pds without explicit list methods defined. */
static void pd_defaultlist(t_pd *x, t_symbol *s, int argc, t_atom *argv)
{
            /* a list with no elements is handled by the 'bang' method if
            one exists. */
    if (argc == 0 && *(*x)->c_bangmethod != pd_defaultbang)
    {
        (*(*x)->c_bangmethod)(x);
        return;
    }
            /* a list with one element which is a number can be handled by a
            "float" method if any is defined; same for "symbol", "pointer". */
    if (argc == 1)
    {
        if (argv->a_type == A_FLOAT &&
        *(*x)->c_floatmethod != pd_defaultfloat)
        {
            (*(*x)->c_floatmethod)(x, argv->a_w.w_float);
            return;
        }
        else if (argv->a_type == A_SYMBOL &&
            *(*x)->c_symbolmethod != pd_defaultsymbol)
        {
            (*(*x)->c_symbolmethod)(x, argv->a_w.w_symbol);
            return;
        }
        else if (argv->a_type == A_POINTER &&
            *(*x)->c_pointermethod != pd_defaultpointer)
        {
            (*(*x)->c_pointermethod)(x, argv->a_w.w_gpointer);
            return;
        }
    }
        /* Next try for an "anything" method */
    if ((*x)->c_anymethod != pd_defaultanything)
        (*(*x)->c_anymethod)(x, &s_list, argc, argv);

        /* if the object is patchable (i.e., can have proper inlets)
            send it on to obj_list which will unpack the list into the inlets */
177
    else if (argc>0 && (*x)->c_patchable)
Miller Puckette's avatar
Miller Puckette committed
178
        obj_list((t_object *)x, s, argc, argv);
179
            /* otherwise give up and complain. */
Miller Puckette's avatar
Miller Puckette committed
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
    else pd_defaultanything(x, &s_list, argc, argv);
}

    /* for now we assume that all "gobjs" are text unless explicitly
    overridden later by calling class_setbehavior().  I'm not sure
    how to deal with Pds that aren't gobjs; shouldn't there be a
    way to check that at run time?  Perhaps the presence of a "newmethod"
    should be our cue, or perhaps the "tiny" flag.  */

    /* another matter.  This routine does two unrelated things: it creates
    a Pd class, but also adds a "new" method to create an instance of it.
    These are combined for historical reasons and for brevity in writing
    objects.  To avoid adding a "new" method send a null function pointer.
    To add additional ones, use class_addcreator below.  Some "classes", like
    "select", are actually two classes of the same name, one for the single-
    argument form, one for the multiple one; see select_setup() to find out
    how this is handled.  */

extern void text_save(t_gobj *z, t_binbuf *b);

200
201
202
203
204
205
206
207
208
209
210
void classtable_register(t_class *c)
{
    t_classtable *t;
    for(t = ct; t; t = t->ct_next)
        if (t->ct_class == c) post("already registered %s", c->c_name->s_name);
    t = (t_classtable *)t_getbytes(sizeof(*t));
    t->ct_class = c;
    t->ct_next = ct;
    ct = t;
}

211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
int classtable_size(void) {
    t_classtable *t;
    int i;
    for(t = ct, i = 0; t; t = t->ct_next)
        i++;
    return i;
}

void classtable_tovec(int size, t_atom *vec)
{
    t_classtable *t;
    int i;
    for(t = ct, i = 0; t && i < size; t = t->ct_next, i++)
        if (!t->ct_class->c_name)
        {
            SETSYMBOL(vec+i, gensym("anonymous-class"));
        }
        else
            SETSYMBOL(vec+i, t->ct_class->c_name);
}

232
233
234
235
236
237
238
239
240
241
242
// todo-- make accessors so m_imp.h isn't needed by x_interface.c

t_class *classtable_findbyname(t_symbol *s)
{
    t_classtable *t;
    for (t = ct; t; t = t->ct_next)
        if (t->ct_class->c_name == s)
            return t->ct_class;
    return NULL;
}

Miller Puckette's avatar
Miller Puckette committed
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
t_class *class_new(t_symbol *s, t_newmethod newmethod, t_method freemethod,
    size_t size, int flags, t_atomtype type1, ...)
{
    va_list ap;
    t_atomtype vec[MAXPDARG+1], *vp = vec;
    int count = 0;
    t_class *c;
    int typeflag = flags & CLASS_TYPEMASK;
    if (!typeflag) typeflag = CLASS_PATCHABLE;
    *vp = type1;

    va_start(ap, type1);
    while (*vp)
    {
        if (count == MAXPDARG)
        {
259
            error("class %s: sorry: only %d args typechecked; use A_GIMME",
Miller Puckette's avatar
Miller Puckette committed
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
                s->s_name, MAXPDARG);
            break;
        }
        vp++;
        count++;
        *vp = va_arg(ap, t_atomtype);
    }
    va_end(ap);
    if (pd_objectmaker && newmethod)
    {
            /* add a "new" method by the name specified by the object */
        class_addmethod(pd_objectmaker, (t_method)newmethod, s,
            vec[0], vec[1], vec[2], vec[3], vec[4], vec[5]);
        if (class_loadsym)
        {
                /* if we're loading an extern it might have been invoked by a
                longer file name; in this case, make this an admissible name
                too. */
278
279
            char *loadstring = class_loadsym->s_name;
            int l1 = strlen(s->s_name), l2 = strlen(loadstring);
Miller Puckette's avatar
Miller Puckette committed
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
            if (l2 > l1 && !strcmp(s->s_name, loadstring + (l2 - l1)))
                class_addmethod(pd_objectmaker, (t_method)newmethod,
                    class_loadsym,
                    vec[0], vec[1], vec[2], vec[3], vec[4], vec[5]);
        }
    }
    c = (t_class *)t_getbytes(sizeof(*c));
    c->c_name = c->c_helpname = s;
    c->c_size = size;
    c->c_methods = t_getbytes(0);
    c->c_nmethod = 0;
    c->c_freemethod = (t_method)freemethod;
    c->c_bangmethod = pd_defaultbang;
    c->c_pointermethod = pd_defaultpointer;
    c->c_floatmethod = pd_defaultfloat;
    c->c_symbolmethod = pd_defaultsymbol;
Hans-Christoph Steiner's avatar
Hans-Christoph Steiner committed
296
    c->c_blobmethod = pd_defaultblob; /* MP 20061226 blob type */
Miller Puckette's avatar
Miller Puckette committed
297
298
299
300
301
302
303
304
305
306
307
308
309
310
    c->c_listmethod = pd_defaultlist;
    c->c_anymethod = pd_defaultanything;
    c->c_wb = (typeflag == CLASS_PATCHABLE ? &text_widgetbehavior : 0);
    c->c_pwb = 0;
    c->c_firstin = ((flags & CLASS_NOINLET) == 0);
    c->c_patchable = (typeflag == CLASS_PATCHABLE);
    c->c_gobj = (typeflag >= CLASS_GOBJ);
    c->c_drawcommand = 0;
    c->c_floatsignalin = 0;
    c->c_externdir = class_extern_dir;
    c->c_savefn = (typeflag == CLASS_PATCHABLE ? text_save : class_nosavefn);
#if 0 
    post("class: %s", c->c_name->s_name);
#endif
311
312
    classtable_register(c);
//    post("class: %s", c->c_name->s_name);
Miller Puckette's avatar
Miller Puckette committed
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
    return (c);
}

    /* add a creation method, which is a function that returns a Pd object
    suitable for putting in an object box.  We presume you've got a class it
    can belong to, but this won't be used until the newmethod is actually
    called back (and the new method explicitly takes care of this.) */

void class_addcreator(t_newmethod newmethod, t_symbol *s, 
    t_atomtype type1, ...)
{
    va_list ap;
    t_atomtype vec[MAXPDARG+1], *vp = vec;
    int count = 0;
    *vp = type1;

    va_start(ap, type1);
    while (*vp)
    {
        if (count == MAXPDARG)
        {
            error("class %s: sorry: only %d creation args allowed",
                s->s_name, MAXPDARG);
            break;
        }
        vp++;
        count++;
        *vp = va_arg(ap, t_atomtype);
    } 
    va_end(ap);
    class_addmethod(pd_objectmaker, (t_method)newmethod, s,
        vec[0], vec[1], vec[2], vec[3], vec[4], vec[5]);
345
346
347
348
349
350
351
352
353
354
355
356
    if (class_loadsym)
    {
            /* if we're loading an extern it might have been invoked by a
            longer file name; in this case, make this an admissible name
            too. */
        char *loadstring = class_loadsym->s_name,
            l1 = strlen(s->s_name), l2 = strlen(loadstring);
        if (l2 > l1 && !strcmp(s->s_name, loadstring + (l2 - l1)))
            class_addmethod(pd_objectmaker, (t_method)newmethod,
                class_loadsym,
                vec[0], vec[1], vec[2], vec[3], vec[4], vec[5]);
    }
Miller Puckette's avatar
Miller Puckette committed
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
}

void class_addmethod(t_class *c, t_method fn, t_symbol *sel,
    t_atomtype arg1, ...)
{
    va_list ap;
    t_methodentry *m;
    t_atomtype argtype = arg1;
    int nargs;
    
    va_start(ap, arg1);
        /* "signal" method specifies that we take audio signals but
        that we don't want automatic float to signal conversion.  This
        is obsolete; you should now use the CLASS_MAINSIGNALIN macro. */
    if (sel == &s_signal)
    {
        if (c->c_floatsignalin)
            post("warning: signal method overrides class_mainsignalin");
        c->c_floatsignalin = -1;
    }
        /* check for special cases.  "Pointer" is missing here so that
        pd_objectmaker's pointer method can be typechecked differently.  */
    if (sel == &s_bang)
    {
        if (argtype) goto phooey;
        class_addbang(c, fn);
    }
    else if (sel == &s_float)
    {
        if (argtype != A_FLOAT || va_arg(ap, t_atomtype)) goto phooey;
        class_doaddfloat(c, fn);
    }
    else if (sel == &s_symbol)
    {
        if (argtype != A_SYMBOL || va_arg(ap, t_atomtype)) goto phooey;
        class_addsymbol(c, fn);
    }
Hans-Christoph Steiner's avatar
Hans-Christoph Steiner committed
394
395
396
397
398
399
    else if (sel == &s_blob) /* MP 20070106 blob type */
    {
        post("class_addmethod: %p", fn);
        if (argtype != A_BLOB || va_arg(ap, t_atomtype)) goto phooey;
        class_addblob(c, fn);
    }
Miller Puckette's avatar
Miller Puckette committed
400
401
402
403
404
405
406
407
408
409
410
411
    else if (sel == &s_list)
    {
        if (argtype != A_GIMME) goto phooey;
        class_addlist(c, fn);
    }
    else if (sel == &s_anything)
    {
        if (argtype != A_GIMME) goto phooey;
        class_addanything(c, fn);
    }
    else
    {
Hans-Christoph Steiner's avatar
Hans-Christoph Steiner committed
412
        /* Pd-extended doesn't use the aliasing automagic
413
414
415
416
417
418
419
420
421
422
423
424
425
        int i;
        for (i = 0; i < c->c_nmethod; i++)
            if (c->c_methods[i].me_name == sel)
        {
            char nbuf[80];
            snprintf(nbuf, 80, "%s_aliased", sel->s_name);
            c->c_methods[i].me_name = gensym(nbuf);
            if (c == pd_objectmaker)
                post("warning: class '%s' overwritten; old one renamed '%s'",
                    sel->s_name, nbuf);
            else post("warning: old method '%s' for class '%s' renamed '%s'",
                sel->s_name, c->c_name->s_name, nbuf);
        }
Hans-Christoph Steiner's avatar
Hans-Christoph Steiner committed
426
        */
Miller Puckette's avatar
Miller Puckette committed
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
        c->c_methods = t_resizebytes(c->c_methods,
            c->c_nmethod * sizeof(*c->c_methods),
            (c->c_nmethod + 1) * sizeof(*c->c_methods));
        m = c->c_methods +  c->c_nmethod;
        c->c_nmethod++;
        m->me_name = sel;
        m->me_fun = (t_gotfn)fn;
        nargs = 0;
        while (argtype != A_NULL && nargs < MAXPDARG)
        {
            m->me_arg[nargs++] = argtype;
            argtype = va_arg(ap, t_atomtype);
        }
        if (argtype != A_NULL)
            error("%s_%s: only 5 arguments are typecheckable; use A_GIMME",
                c->c_name->s_name, sel->s_name);
        m->me_arg[nargs] = A_NULL;
    }
445
    va_end(ap);
Miller Puckette's avatar
Miller Puckette committed
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
    return;
phooey:
    bug("class_addmethod: %s_%s: bad argument types\n",
        c->c_name->s_name, sel->s_name);
}

    /* Instead of these, see the "class_addfloat", etc.,  macros in m_pd.h */
void class_addbang(t_class *c, t_method fn)
{
    c->c_bangmethod = (t_bangmethod)fn;
}

void class_addpointer(t_class *c, t_method fn)
{
    c->c_pointermethod = (t_pointermethod)fn;
}

void class_doaddfloat(t_class *c, t_method fn)
{
    c->c_floatmethod = (t_floatmethod)fn;
}

void class_addsymbol(t_class *c, t_method fn)
{
    c->c_symbolmethod = (t_symbolmethod)fn;
}

Hans-Christoph Steiner's avatar
Hans-Christoph Steiner committed
473
474
475
476
477
void class_addblob(t_class *c, t_method fn) /* MP 20061226 blob type */
{
    c->c_blobmethod = (t_blobmethod)fn;
}

Miller Puckette's avatar
Miller Puckette committed
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
void class_addlist(t_class *c, t_method fn)
{
    c->c_listmethod = (t_listmethod)fn;
}

void class_addanything(t_class *c, t_method fn)
{
    c->c_anymethod = (t_anymethod)fn;
}

void class_setwidget(t_class *c, t_widgetbehavior *w)
{
    c->c_wb = w;
}

void class_setparentwidget(t_class *c, t_parentwidgetbehavior *pw)
{
    c->c_pwb = pw;
}

char *class_getname(t_class *c)
{
    return (c->c_name->s_name);
}

char *class_gethelpname(t_class *c)
{
    return (c->c_helpname->s_name);
}

void class_sethelpsymbol(t_class *c, t_symbol *s)
{
    c->c_helpname = s;
}

t_parentwidgetbehavior *pd_getparentwidget(t_pd *x)
{
    return ((*x)->c_pwb);
}

void class_setdrawcommand(t_class *c)
{
    c->c_drawcommand = 1;
}

int class_isdrawcommand(t_class *c)
{
    return (c->c_drawcommand);
}

static void pd_floatforsignal(t_pd *x, t_float f)
{
    int offset = (*x)->c_floatsignalin;
    if (offset > 0)
532
        *(t_float *)(((char *)x) + offset) = f;
Miller Puckette's avatar
Miller Puckette committed
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
    else
        pd_error(x, "%s: float unexpected for signal input",
            (*x)->c_name->s_name);
}

void class_domainsignalin(t_class *c, int onset)
{
    if (onset <= 0) onset = -1;
    else
    {
        if (c->c_floatmethod != pd_defaultfloat)
            post("warning: %s: float method overwritten", c->c_name->s_name);
        c->c_floatmethod = (t_floatmethod)pd_floatforsignal;
    }
    c->c_floatsignalin = onset;
}

void class_set_extern_dir(t_symbol *s)
{
    class_extern_dir = s;
}

char *class_gethelpdir(t_class *c)
{
    return (c->c_externdir->s_name);
}

static void class_nosavefn(t_gobj *z, t_binbuf *b)
{
    bug("save function called but not defined");
}

void class_setsavefn(t_class *c, t_savefn f)
{
    c->c_savefn = f;
}

t_savefn class_getsavefn(t_class *c)
{
    return (c->c_savefn);
}

void class_setpropertiesfn(t_class *c, t_propertiesfn f)
{
    c->c_propertiesfn = f;
}

t_propertiesfn class_getpropertiesfn(t_class *c)
{
    return (c->c_propertiesfn);
}

/* ---------------- the symbol table ------------------------ */

#define HASHSIZE 1024

static t_symbol *symhash[HASHSIZE];

Miller Puckette's avatar
Miller Puckette committed
591
t_symbol *dogensym(const char *s, t_symbol *oldsym)
Miller Puckette's avatar
Miller Puckette committed
592
593
594
595
{
    t_symbol **sym1, *sym2;
    unsigned int hash1 = 0,  hash2 = 0;
    int length = 0;
Miller Puckette's avatar
Miller Puckette committed
596
    const char *s2 = s;
Miller Puckette's avatar
Miller Puckette committed
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
    while (*s2)
    {
        hash1 += *s2;
        hash2 += hash1;
        length++;
        s2++;
    }
    sym1 = symhash + (hash2 & (HASHSIZE-1));
    while (sym2 = *sym1)
    {
        if (!strcmp(sym2->s_name, s)) return(sym2);
        sym1 = &sym2->s_next;
    }
    if (oldsym) sym2 = oldsym;
    else
    {
        sym2 = (t_symbol *)t_getbytes(sizeof(*sym2));
        sym2->s_name = t_getbytes(length+1);
        sym2->s_next = 0;
        sym2->s_thing = 0;
        strcpy(sym2->s_name, s);
    }
    *sym1 = sym2;
    return (sym2);
}

Miller Puckette's avatar
Miller Puckette committed
623
t_symbol *gensym(const char *s)
Miller Puckette's avatar
Miller Puckette committed
624
625
626
627
{
    return(dogensym(s, 0));
}

628
#define MAXOBJDEPTH 1000
Miller Puckette's avatar
Miller Puckette committed
629
630
631
static int tryingalready;

void canvas_popabstraction(t_canvas *x);
Hans-Christoph Steiner's avatar
Hans-Christoph Steiner committed
632
633
void canvas_initbang(t_canvas *x);

Miller Puckette's avatar
Miller Puckette committed
634
635
636
637
638
639
640
641
642
extern t_pd *newest;

t_symbol* pathsearch(t_symbol *s,char* ext);
int pd_setloadingabstraction(t_symbol *sym);

    /* this routine is called when a new "object" is requested whose class Pd
    doesn't know.  Pd tries to load it as an extern, then as an abstraction. */
void new_anything(void *dummy, t_symbol *s, int argc, t_atom *argv)
{
643
644
645
646
    if (tryingalready>MAXOBJDEPTH){
      error("maximum object loading depth %d reached", MAXOBJDEPTH);
      return;
    }
Miller Puckette's avatar
Miller Puckette committed
647
648
649
650
    newest = 0;
    class_loadsym = s;
    if (sys_load_lib(canvas_getcurrent(), s->s_name))
    {
651
        tryingalready++;
Miller Puckette's avatar
Miller Puckette committed
652
        typedmess(dummy, s, argc, argv);
653
        tryingalready--;
Miller Puckette's avatar
Miller Puckette committed
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
        return;
    }
    class_loadsym = 0;
}

t_symbol  s_pointer =   {"pointer", 0, 0};
t_symbol  s_float =     {"float", 0, 0};
t_symbol  s_symbol =    {"symbol", 0, 0};
t_symbol  s_bang =      {"bang", 0, 0};
t_symbol  s_list =      {"list", 0, 0};
t_symbol  s_anything =  {"anything", 0, 0};
t_symbol  s_signal =    {"signal", 0, 0};
t_symbol  s__N =        {"#N", 0, 0};
t_symbol  s__X =        {"#X", 0, 0};
t_symbol  s_x =         {"x", 0, 0};
t_symbol  s_y =         {"y", 0, 0};
t_symbol  s_ =          {"", 0, 0};
671
t_symbol  s_blob =      {"blob", 0, 0}; /* MP 20061223 blob type */
Miller Puckette's avatar
Miller Puckette committed
672
673

static t_symbol *symlist[] = { &s_pointer, &s_float, &s_symbol, &s_bang,
Hans-Christoph Steiner's avatar
Hans-Christoph Steiner committed
674
    &s_list, &s_anything, &s_signal, &s__N, &s__X, &s_x, &s_y, &s_, &s_blob}; /* MP 20061223 added s_blob */
Miller Puckette's avatar
Miller Puckette committed
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720

void mess_init(void)
{
    t_symbol **sp;
    int i;

    if (pd_objectmaker) return;    
    for (i = sizeof(symlist)/sizeof(*symlist), sp = symlist; i--; sp++)
        (void) dogensym((*sp)->s_name, *sp);
    pd_objectmaker = class_new(gensym("objectmaker"), 0, 0, sizeof(t_pd),
        CLASS_DEFAULT, A_NULL);
    pd_canvasmaker = class_new(gensym("classmaker"), 0, 0, sizeof(t_pd),
        CLASS_DEFAULT, A_NULL);
    pd_bind(&pd_canvasmaker, &s__N);
    class_addanything(pd_objectmaker, (t_method)new_anything);
}

t_pd *newest;

/* This is externally available, but note that it might later disappear; the
whole "newest" thing is a hack which needs to be redesigned. */
t_pd *pd_newest(void)
{
    return (newest);
}

    /* horribly, we need prototypes for each of the artificial function
    calls in typedmess(), to keep the compiler quiet. */
typedef t_pd *(*t_newgimme)(t_symbol *s, int argc, t_atom *argv);
typedef void(*t_messgimme)(t_pd *x, t_symbol *s, int argc, t_atom *argv);

typedef t_pd *(*t_fun0)(
    t_floatarg d1, t_floatarg d2, t_floatarg d3, t_floatarg d4, t_floatarg d5);
typedef t_pd *(*t_fun1)(t_int i1,
    t_floatarg d1, t_floatarg d2, t_floatarg d3, t_floatarg d4, t_floatarg d5);
typedef t_pd *(*t_fun2)(t_int i1, t_int i2,
    t_floatarg d1, t_floatarg d2, t_floatarg d3, t_floatarg d4, t_floatarg d5);
typedef t_pd *(*t_fun3)(t_int i1, t_int i2, t_int i3,
    t_floatarg d1, t_floatarg d2, t_floatarg d3, t_floatarg d4, t_floatarg d5);
typedef t_pd *(*t_fun4)(t_int i1, t_int i2, t_int i3, t_int i4,
    t_floatarg d1, t_floatarg d2, t_floatarg d3, t_floatarg d4, t_floatarg d5);
typedef t_pd *(*t_fun5)(t_int i1, t_int i2, t_int i3, t_int i4, t_int i5,
    t_floatarg d1, t_floatarg d2, t_floatarg d3, t_floatarg d4, t_floatarg d5);
typedef t_pd *(*t_fun6)(t_int i1, t_int i2, t_int i3, t_int i4, t_int i5, t_int i6,
    t_floatarg d1, t_floatarg d2, t_floatarg d3, t_floatarg d4, t_floatarg d5);

721
722
723
/* needed for proper error reporting */
extern t_pd *pd_mess_from_responder(t_pd *x);

724
725
726
727
728
729
730
731
/* This is a hack for the messages in Pd files that follow a comma, such as:
     #X restore..., f 12
   In that case the "f 12" would get sent to the subcanvas where we actually
   want it to apply to the parent.

   Pd Vanilla apparently made a special case to handle this-- instead of using
   the comma after the "restore" message it starts a new "#X f 12;" which
   ensures that the #X is bound to the correct canvas. But that means it fails
Jonathan Wilkes's avatar
Jonathan Wilkes committed
732
   for the old syntax, and there are patches in the wild that use it.
733
734
735
736
737
738
739

   So we support _both_ of these styles in Purr Data by doing the following:
   1. Checking if the last typedmess was "restore" selector.
   2. Checking if the last t_pd was a canvas that matches the current one.
      If so, we're dealing with the old "#X..., f 12;" syntax. If not, it's
      the new Pd Vanilla syntax.
*/
740
t_symbol *last_typedmess;
741
t_pd *last_typedmess_pd;
742

Miller Puckette's avatar
Miller Puckette committed
743
744
745
746
747
748
749
750
751
752
753
void pd_typedmess(t_pd *x, t_symbol *s, int argc, t_atom *argv)
{
    t_class *c = *x;
    t_methodentry *m;
    t_atomtype *wp, wanttype;
    int i;
    t_int ai[MAXPDARG+1], *ap = ai;
    t_floatarg ad[MAXPDARG+1], *dp = ad;
    int narg = 0;
    t_pd *bonzo;
    
754
    //fprintf(stderr,"pd_typedmess: %s %d\n", s->s_name, c->c_nmethod);
755

Miller Puckette's avatar
Miller Puckette committed
756
757
758
759
760
761
762
763
764
        /* check for messages that are handled by fixed slots in the class
        structure.  We don't catch "pointer" though so that sending "pointer"
        to pd_objectmaker doesn't require that we supply a pointer value. */
    if (s == &s_float)
    {
        if (!argc) (*c->c_floatmethod)(x, 0.);
        else if (argv->a_type == A_FLOAT)
            (*c->c_floatmethod)(x, argv->a_w.w_float);
        else goto badarg;
765
        goto lastmess;
Miller Puckette's avatar
Miller Puckette committed
766
767
768
769
    }
    if (s == &s_bang)
    {
        (*c->c_bangmethod)(x);
770
        goto lastmess;
Miller Puckette's avatar
Miller Puckette committed
771
772
773
774
    }
    if (s == &s_list)
    {
        (*c->c_listmethod)(x, s, argc, argv);
775
        goto lastmess;
Miller Puckette's avatar
Miller Puckette committed
776
777
778
779
780
781
782
    }
    if (s == &s_symbol)
    {
        if (argc && argv->a_type == A_SYMBOL)
            (*c->c_symbolmethod)(x, argv->a_w.w_symbol);
        else
            (*c->c_symbolmethod)(x, &s_);
783
        goto lastmess;
Miller Puckette's avatar
Miller Puckette committed
784
    }
Hans-Christoph Steiner's avatar
Hans-Christoph Steiner committed
785
786
787
788
789
    if (s == &s_blob) /* MP 20061226 blob type */
    {
        /*post("pd_typedmess argc = %d\n", argc);*//* MP 20061226 debug */
        if (argc == 1) (*c->c_blobmethod)(x, argv->a_w.w_blob);
        else goto badarg;
790
        goto lastmess;
Hans-Christoph Steiner's avatar
Hans-Christoph Steiner committed
791
    }
Miller Puckette's avatar
Miller Puckette committed
792
793
    for (i = c->c_nmethod, m = c->c_methods; i--; m++)
    {
794
795
        //if (m->me_name == s)
        if (m && m->me_name == s)
Miller Puckette's avatar
Miller Puckette committed
796
        {
797
798
799
            //fprintf(stderr,"me_name %s\n", m->me_name);
            wp = m->me_arg;
            if (*wp == A_GIMME)
Miller Puckette's avatar
Miller Puckette committed
800
            {
801
802
803
                if (x == &pd_objectmaker)
                    newest = (*((t_newgimme)(m->me_fun)))(s, argc, argv);
                else (*((t_messgimme)(m->me_fun)))(x, s, argc, argv);
804
                goto lastmess;
805
806
807
808
809
810
            }
            if (argc > MAXPDARG) argc = MAXPDARG;
            if (x != &pd_objectmaker) *(ap++) = (t_int)x, narg++;
            while (wanttype = *wp++)
            {
                switch (wanttype)
Miller Puckette's avatar
Miller Puckette committed
811
                {
812
813
814
815
816
817
818
819
820
821
822
823
824
825
                case A_POINTER:
                    if (!argc) goto badarg;
                    else
                    {
                        if (argv->a_type == A_POINTER)
                            *ap = (t_int)(argv->a_w.w_gpointer);
                        else goto badarg;
                        argc--;
                        argv++;
                    }
                    narg++;
                    ap++;
                    break;
                case A_FLOAT:
826
                    if (!argc) goto badarg; /* falls through */
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
                case A_DEFFLOAT:
                    if (!argc) *dp = 0;
                    else
                    {
                        if (argv->a_type == A_FLOAT)
                            *dp = argv->a_w.w_float;
                        else goto badarg;
                        argc--;
                        argv++;
                    }
                    dp++;
                    break;
                case A_BLOB:/* MP 20070106 blob type */
                    /*post("pd_typedmess A_BLOB");*/
                    if (!argc) goto badarg;
                    if (argv->a_type == A_BLOB)
                    {
                        /*post("argv->a_type == A_BLOB, argc = %d, narg= %d",
                        //    argc, narg);*/
                        *ap = (t_int)(argv->a_w.w_blob);
                    }
Miller Puckette's avatar
Miller Puckette committed
848
849
                    argc--;
                    argv++;
850
851
852
853
                    narg++;
                    ap++;
                    break;
                case A_SYMBOL:
854
                    if (!argc) goto badarg; /* falls through */
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
                case A_DEFSYM:
                    if (!argc) *ap = (t_int)(&s_);
                    else
                    {
                        if (argv->a_type == A_SYMBOL)
                            *ap = (t_int)(argv->a_w.w_symbol);
                                /* if it's an unfilled "dollar" argument it
                                   appears as zero here; cheat and bash it
                                   to the null symbol.  Unfortunately, this
                                   lets real zeros pass as symbols too, which
                                   seems wrong... */
                        else if (x == &pd_objectmaker &&
                                 argv->a_type == A_FLOAT
                                 && argv->a_w.w_float == 0)
                            *ap = (t_int)(&s_);
                        else goto badarg;
                        argc--;
                        argv++;
                    }
                    narg++;
                    ap++;
876
877
878
                    break;
                default:
                    goto badarg;
Miller Puckette's avatar
Miller Puckette committed
879
880
                }
            }
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
            switch (narg)
            {
            case 0 : bonzo = (*(t_fun0)(m->me_fun))
                (ad[0], ad[1], ad[2], ad[3], ad[4]); break;
            case 1 : bonzo = (*(t_fun1)(m->me_fun))
                (ai[0], ad[0], ad[1], ad[2], ad[3], ad[4]); break;
            case 2 : bonzo = (*(t_fun2)(m->me_fun))
                (ai[0], ai[1], ad[0], ad[1], ad[2], ad[3], ad[4]); break;
            case 3 : bonzo = (*(t_fun3)(m->me_fun))
                (ai[0], ai[1], ai[2], ad[0], ad[1], ad[2], ad[3], ad[4]); break;
            case 4 : bonzo = (*(t_fun4)(m->me_fun))
                (ai[0], ai[1], ai[2], ai[3],
                    ad[0], ad[1], ad[2], ad[3], ad[4]); break;
            case 5 : bonzo = (*(t_fun5)(m->me_fun))
                (ai[0], ai[1], ai[2], ai[3], ai[4],
                    ad[0], ad[1], ad[2], ad[3], ad[4]); break;
            case 6 : bonzo = (*(t_fun6)(m->me_fun))
                (ai[0], ai[1], ai[2], ai[3], ai[4], ai[5],
                    ad[0], ad[1], ad[2], ad[3], ad[4]); break;
            default: bonzo = 0;
            }
            if (x == &pd_objectmaker)
                newest = bonzo;
904
            goto lastmess;
Miller Puckette's avatar
Miller Puckette committed
905
906
907
        }
    }
    (*c->c_anymethod)(x, s, argc, argv);
908
    goto lastmess;
Miller Puckette's avatar
Miller Puckette committed
909
badarg:
910
911
912
913
    /* if x is a messresponder class, tweak it to point to the
       message that contains it (so it can be selected when 'Find
       Error' is used). */
    x = pd_mess_from_responder(x);
Miller Puckette's avatar
Miller Puckette committed
914
915
    pd_error(x, "Bad arguments for message '%s' to object '%s'",
        s->s_name, c->c_name->s_name);
916
917
lastmess:
    last_typedmess = s;    
918
    last_typedmess_pd = x;
919
    return;
Miller Puckette's avatar
Miller Puckette committed
920
921
}

922
923
924
    /* convenience routine giving a stdarg interface to typedmess().  Only
    ten args supported; it seems unlikely anyone will need more since
    longer messages are likely to be programmatically generated anyway. */
Miller Puckette's avatar
Miller Puckette committed
925
926
927
void pd_vmess(t_pd *x, t_symbol *sel, char *fmt, ...)
{
    va_list ap;
928
    t_atom arg[10], *at = arg;
Miller Puckette's avatar
Miller Puckette committed
929
930
931
932
933
934
    int nargs = 0;
    char *fp = fmt;

    va_start(ap, fmt);
    while (1)
    {
935
        if (nargs >= 10)
Miller Puckette's avatar
Miller Puckette committed
936
        {
937
            pd_error(x, "pd_vmess: only 10 allowed");
Miller Puckette's avatar
Miller Puckette committed
938
939
940
941
942
943
            break;
        }
        switch(*fp++)
        {
        case 'f': SETFLOAT(at, va_arg(ap, double)); break;
        case 's': SETSYMBOL(at, va_arg(ap, t_symbol *)); break;
Hans-Christoph Steiner's avatar
Hans-Christoph Steiner committed
944
945
946
947
        case 't':
            SETBLOB(at, va_arg(ap, t_blob *));
            /*post("pd_vmess: arg[0].a_w.w_blob = %p", arg[0].a_w.w_blob);*/
            break; /* MP 20061226 blob type */
Miller Puckette's avatar
Miller Puckette committed
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
        case 'i': SETFLOAT(at, va_arg(ap, t_int)); break;       
        case 'p': SETPOINTER(at, va_arg(ap, t_gpointer *)); break;
        default: goto done;
        }
        at++;
        nargs++;
    }
done:
    va_end(ap);
    typedmess(x, sel, nargs, arg);
}

void pd_forwardmess(t_pd *x, int argc, t_atom *argv)
{
    if (argc)
    {
        t_atomtype t = argv->a_type;
        if (t == A_SYMBOL) pd_typedmess(x, argv->a_w.w_symbol, argc-1, argv+1);
        else if (t == A_POINTER)
        {
            if (argc == 1) pd_pointer(x, argv->a_w.w_gpointer);
            else pd_list(x, &s_list, argc, argv);
        }
        else if (t == A_FLOAT)
        {
            if (argc == 1) pd_float(x, argv->a_w.w_float);
            else pd_list(x, &s_list, argc, argv);
        }
        else bug("pd_forwardmess");
    }

}

void nullfn(void) {}

t_gotfn getfn(t_pd *x, t_symbol *s)
{
    t_class *c = *x;
    t_methodentry *m;
    int i;

    for (i = c->c_nmethod, m = c->c_methods; i--; m++)
        if (m->me_name == s) return(m->me_fun);
    pd_error(x, "%s: no method for message '%s'", c->c_name->s_name, s->s_name);
    return((t_gotfn)nullfn);
}

t_gotfn zgetfn(t_pd *x, t_symbol *s)
{
    t_class *c = *x;
    t_methodentry *m;
    int i;

    for (i = c->c_nmethod, m = c->c_methods; i--; m++)
        if (m->me_name == s) return(m->me_fun);
    return(0);
}