x_connective.c 58 KB
Newer Older
Miller Puckette's avatar
Miller Puckette committed
1
2
3
4
5
6
7
8
9
10
/* 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.  */

/* connective objects */

#include "m_pd.h"

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

Miller Puckette's avatar
Miller Puckette committed
12
13
14
extern t_pd *newest;

/* -------------------------- int ------------------------------ */
15
static t_class *pdint_class;
Miller Puckette's avatar
Miller Puckette committed
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51

typedef struct _pdint
{
    t_object x_obj;
    t_float x_f;
} t_pdint;

static void *pdint_new(t_floatarg f)
{
    t_pdint *x = (t_pdint *)pd_new(pdint_class);
    x->x_f = f;
    outlet_new(&x->x_obj, &s_float);
    floatinlet_new(&x->x_obj, &x->x_f);
    return (x);
}

static void pdint_bang(t_pdint *x)
{
    outlet_float(x->x_obj.ob_outlet, (t_float)(int)(x->x_f));
}

static void pdint_float(t_pdint *x, t_float f)
{
    outlet_float(x->x_obj.ob_outlet, (t_float)(int)(x->x_f = f));
}

void pdint_setup(void)
{
    pdint_class = class_new(gensym("int"), (t_newmethod)pdint_new, 0,
        sizeof(t_pdint), 0, A_DEFFLOAT, 0);
    class_addcreator((t_newmethod)pdint_new, gensym("i"), A_DEFFLOAT, 0);
    class_addbang(pdint_class, pdint_bang);
    class_addfloat(pdint_class, pdint_float);
}

/* -------------------------- float ------------------------------ */
52
static t_class *pdfloat_class;
Miller Puckette's avatar
Miller Puckette committed
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88

typedef struct _pdfloat
{
    t_object x_obj;
    t_float x_f;
} t_pdfloat;

    /* "float," "symbol," and "bang" are special because
    they're created by short-circuited messages to the "new"
    object which are handled specially in pd_typedmess(). */

static void *pdfloat_new(t_pd *dummy, t_float f)
{
    t_pdfloat *x = (t_pdfloat *)pd_new(pdfloat_class);
    x->x_f = f;
    outlet_new(&x->x_obj, &s_float);
    floatinlet_new(&x->x_obj, &x->x_f);
    newest = &x->x_obj.ob_pd;
    return (x);
}

static void *pdfloat_new2(t_floatarg f)
{
    return (pdfloat_new(0, f));
}

static void pdfloat_bang(t_pdfloat *x)
{
    outlet_float(x->x_obj.ob_outlet, x->x_f);
}

static void pdfloat_float(t_pdfloat *x, t_float f)
{
    outlet_float(x->x_obj.ob_outlet, x->x_f = f);
}

89
90
91
92
93
94
95
96
97
98
99
100
101
int symbol_can_float(t_symbol *s, t_float *f)
{
        char c;
        if (!s || s == &s_) return 0;
        c = s->s_name[0];
        if (c != '-' && c != '+' && c < 48 && c > 57) return 0;
        char *str_end = NULL;
        *f = strtod(s->s_name, &str_end);
        /* Add error checking here like in cxc/hex2dec */
        if (*f == 0 && s->s_name == str_end)
            return 0;
        return 1;
}
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118

char *type_hint(t_symbol *s, int argc, t_atom *argv);

static void pdfloat_symbol(t_pdfloat *x, t_symbol *s)
{
        t_float f;
        if (symbol_can_float(s, &f))
            outlet_float(x->x_obj.ob_outlet, x->x_f = f);
        else
        {
            t_atom at;
            SETSYMBOL(&at, s);
            pd_error(x, "couldn't convert %s to float%s",
                s->s_name, type_hint(&s_symbol, 1, &at));
        }
}

Miller Puckette's avatar
Miller Puckette committed
119
120
121
122
123
124
125
void pdfloat_setup(void)
{
    pdfloat_class = class_new(gensym("float"), (t_newmethod)pdfloat_new, 0,
        sizeof(t_pdfloat), 0, A_FLOAT, 0);
    class_addcreator((t_newmethod)pdfloat_new2, gensym("f"), A_DEFFLOAT, 0);
    class_addbang(pdfloat_class, pdfloat_bang);
    class_addfloat(pdfloat_class, (t_method)pdfloat_float);
126
    class_addsymbol(pdfloat_class, (t_method)pdfloat_symbol);
Miller Puckette's avatar
Miller Puckette committed
127
128
129
}

/* -------------------------- symbol ------------------------------ */
130
static t_class *pdsymbol_class;
Miller Puckette's avatar
Miller Puckette committed
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

typedef struct _pdsymbol
{
    t_object x_obj;
    t_symbol *x_s;
} t_pdsymbol;

static void *pdsymbol_new(t_pd *dummy, t_symbol *s)
{
    t_pdsymbol *x = (t_pdsymbol *)pd_new(pdsymbol_class);
    x->x_s = s;
    outlet_new(&x->x_obj, &s_symbol);
    symbolinlet_new(&x->x_obj, &x->x_s);
    newest = &x->x_obj.ob_pd;
    return (x);
}

static void pdsymbol_bang(t_pdsymbol *x)
{
    outlet_symbol(x->x_obj.ob_outlet, x->x_s);
}

static void pdsymbol_symbol(t_pdsymbol *x, t_symbol *s)
{
    outlet_symbol(x->x_obj.ob_outlet, x->x_s = s);
}

static void pdsymbol_anything(t_pdsymbol *x, t_symbol *s, int ac, t_atom *av)
{
    outlet_symbol(x->x_obj.ob_outlet, x->x_s = s);
}

    /* For "list" message don't just output "list"; if empty, we want to
    bang the symbol and if it starts with a symbol, we output that.
    Otherwise it's not clear what we should do so we just go for the
    "anything" method.  LATER figure out if there are other places where
    empty lists aren't equivalent to "bang"???  Should Pd's message passer
    always check and call the more specific method, or should it be the 
    object's responsibility?  Dunno... */
Jonathan Wilkes's avatar
Jonathan Wilkes committed
170
171
    /* Currently this function isn't used... */
/*
Miller Puckette's avatar
Miller Puckette committed
172
173
174
175
176
177
178
179
static void pdsymbol_list(t_pdsymbol *x, t_symbol *s, int ac, t_atom *av)
{
    if (!ac)
        pdsymbol_bang(x);
    else if (av->a_type == A_SYMBOL)
        pdsymbol_symbol(x, av->a_w.w_symbol);
    else pdsymbol_anything(x, s, ac, av);
}
Jonathan Wilkes's avatar
Jonathan Wilkes committed
180
*/
Miller Puckette's avatar
Miller Puckette committed
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360

void pdsymbol_setup(void)
{
    pdsymbol_class = class_new(gensym("symbol"), (t_newmethod)pdsymbol_new, 0,
        sizeof(t_pdsymbol), 0, A_SYMBOL, 0);
    class_addbang(pdsymbol_class, pdsymbol_bang);
    class_addsymbol(pdsymbol_class, pdsymbol_symbol);
    class_addanything(pdsymbol_class, pdsymbol_anything);
}

/* -------------------------- bang ------------------------------ */
static t_class *bang_class;

typedef struct _bang
{
    t_object x_obj;
} t_bang;

static void *bang_new(t_pd *dummy)
{
    t_bang *x = (t_bang *)pd_new(bang_class);
    outlet_new(&x->x_obj, &s_bang);
    newest = &x->x_obj.ob_pd;
    return (x);
}

static void *bang_new2(t_bang f)
{
    return (bang_new(0));
}

static void bang_bang(t_bang *x)
{
    outlet_bang(x->x_obj.ob_outlet);
}

void bang_setup(void)
{
    bang_class = class_new(gensym("bang"), (t_newmethod)bang_new, 0,
        sizeof(t_bang), 0, 0);
    class_addcreator((t_newmethod)bang_new2, gensym("b"), 0);
    class_addbang(bang_class, bang_bang);
    class_addfloat(bang_class, bang_bang);
    class_addsymbol(bang_class, bang_bang);
    class_addlist(bang_class, bang_bang);
    class_addanything(bang_class, bang_bang);
}

/* -------------------- send ------------------------------ */

static t_class *send_class;

typedef struct _send
{
    t_object x_obj;
    t_symbol *x_sym;
} t_send;

static void send_bang(t_send *x)
{
    if (x->x_sym->s_thing) pd_bang(x->x_sym->s_thing);
}

static void send_float(t_send *x, t_float f)
{
    if (x->x_sym->s_thing) pd_float(x->x_sym->s_thing, f);
}

static void send_symbol(t_send *x, t_symbol *s)
{
    if (x->x_sym->s_thing) pd_symbol(x->x_sym->s_thing, s);
}

static void send_pointer(t_send *x, t_gpointer *gp)
{
    if (x->x_sym->s_thing) pd_pointer(x->x_sym->s_thing, gp);
}

static void send_list(t_send *x, t_symbol *s, int argc, t_atom *argv)
{
    if (x->x_sym->s_thing) pd_list(x->x_sym->s_thing, s, argc, argv);
}

static void send_anything(t_send *x, t_symbol *s, int argc, t_atom *argv)
{
    if (x->x_sym->s_thing) typedmess(x->x_sym->s_thing, s, argc, argv);
}

static void *send_new(t_symbol *s)
{
    t_send *x = (t_send *)pd_new(send_class);
    if (!*s->s_name)
        symbolinlet_new(&x->x_obj, &x->x_sym);
    x->x_sym = s;
    return (x);
}

static void send_setup(void)
{
    send_class = class_new(gensym("send"), (t_newmethod)send_new, 0,
        sizeof(t_send), 0, A_DEFSYM, 0);
    class_addcreator((t_newmethod)send_new, gensym("s"), A_DEFSYM, 0);
    class_addbang(send_class, send_bang);
    class_addfloat(send_class, send_float);
    class_addsymbol(send_class, send_symbol);
    class_addpointer(send_class, send_pointer);
    class_addlist(send_class, send_list);
    class_addanything(send_class, send_anything);
}
/* -------------------- receive ------------------------------ */

static t_class *receive_class;

typedef struct _receive
{
    t_object x_obj;
    t_symbol *x_sym;
} t_receive;

static void receive_bang(t_receive *x)
{
    outlet_bang(x->x_obj.ob_outlet);
}

static void receive_float(t_receive *x, t_float f)
{
    outlet_float(x->x_obj.ob_outlet, f);
}

static void receive_symbol(t_receive *x, t_symbol *s)
{
    outlet_symbol(x->x_obj.ob_outlet, s);
}

static void receive_pointer(t_receive *x, t_gpointer *gp)
{
    outlet_pointer(x->x_obj.ob_outlet, gp);
}

static void receive_list(t_receive *x, t_symbol *s, int argc, t_atom *argv)
{
    outlet_list(x->x_obj.ob_outlet, s, argc, argv);
}

static void receive_anything(t_receive *x, t_symbol *s, int argc, t_atom *argv)
{
    outlet_anything(x->x_obj.ob_outlet, s, argc, argv);
}

static void *receive_new(t_symbol *s)
{
    t_receive *x = (t_receive *)pd_new(receive_class);
    x->x_sym = s;
    pd_bind(&x->x_obj.ob_pd, s);
    outlet_new(&x->x_obj, 0);
    return (x);
}

static void receive_free(t_receive *x)
{
    pd_unbind(&x->x_obj.ob_pd, x->x_sym);
}

static void receive_setup(void)
{
    receive_class = class_new(gensym("receive"), (t_newmethod)receive_new, 
        (t_method)receive_free, sizeof(t_receive), CLASS_NOINLET, A_DEFSYM, 0);
    class_addcreator((t_newmethod)receive_new, gensym("r"), A_DEFSYM, 0);
    class_addbang(receive_class, receive_bang);
    class_addfloat(receive_class, (t_method)receive_float);
    class_addsymbol(receive_class, receive_symbol);
    class_addpointer(receive_class, receive_pointer);
    class_addlist(receive_class, receive_list);
    class_addanything(receive_class, receive_anything);
}

/* -------------------------- select ------------------------------ */

static t_class *sel1_class;

361
362
363
364
365
366
367
368
static t_class *sel1_proxy_class;

typedef struct _sel1_proxy
{
    t_pd l_pd;
    void *parent;
} t_sel1_proxy;

Miller Puckette's avatar
Miller Puckette committed
369
370
371
372
373
374
typedef struct _sel1
{
    t_object x_obj;
    t_atom x_atom;
    t_outlet *x_outlet1;
    t_outlet *x_outlet2;
375
    t_sel1_proxy x_pxy;
Miller Puckette's avatar
Miller Puckette committed
376
377
} t_sel1;

378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
static void sel1_proxy_init(t_sel1_proxy *x, t_sel1 *p)
{
    x->l_pd = sel1_proxy_class;
    x->parent = (void *)p;
}

static void sel1_proxy_float(t_sel1_proxy *x, t_float f)
{
    t_sel1 *p = (t_sel1 *)x->parent;
    SETFLOAT(&p->x_atom, f);
}

static void sel1_proxy_symbol(t_sel1_proxy *x, t_symbol *s)
{
    t_sel1 *p = (t_sel1 *)x->parent;
    SETSYMBOL(&p->x_atom, s);
}

Miller Puckette's avatar
Miller Puckette committed
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
static void sel1_float(t_sel1 *x, t_float f)
{
    if (x->x_atom.a_type == A_FLOAT && f == x->x_atom.a_w.w_float)
        outlet_bang(x->x_outlet1);
    else outlet_float(x->x_outlet2, f);
}

static void sel1_symbol(t_sel1 *x, t_symbol *s)
{
    if (x->x_atom.a_type == A_SYMBOL && s == x->x_atom.a_w.w_symbol)
        outlet_bang(x->x_outlet1);
    else outlet_symbol(x->x_outlet2, s);
}

static t_class *sel2_class;

typedef struct _selectelement
{
414
    t_atom e_atom;
Miller Puckette's avatar
Miller Puckette committed
415
416
417
418
419
420
421
    t_outlet *e_outlet;
} t_selectelement;

typedef struct _sel2
{
    t_object x_obj;
    t_atomtype x_type;
422
    t_int x_mixed;
Miller Puckette's avatar
Miller Puckette committed
423
424
425
426
427
428
429
430
431
    t_int x_nelement;
    t_selectelement *x_vec;
    t_outlet *x_rejectout;
} t_sel2;

static void sel2_float(t_sel2 *x, t_float f)
{
    t_selectelement *e;
    int nelement;
432
    if (x->x_type == A_FLOAT || x->x_mixed)
Miller Puckette's avatar
Miller Puckette committed
433
434
    {
        for (nelement = x->x_nelement, e = x->x_vec; nelement--; e++)
435
            if (e->e_atom.a_type == A_FLOAT && e->e_atom.a_w.w_float == f)
436
437
438
439
            {
                outlet_bang(e->e_outlet);
                return;
            }
Miller Puckette's avatar
Miller Puckette committed
440
441
442
443
444
445
446
447
    }
    outlet_float(x->x_rejectout, f);
}

static void sel2_symbol(t_sel2 *x, t_symbol *s)
{
    t_selectelement *e;
    int nelement;
448
    if (x->x_type == A_SYMBOL || x->x_mixed)
Miller Puckette's avatar
Miller Puckette committed
449
450
    {
        for (nelement = x->x_nelement, e = x->x_vec; nelement--; e++)
451
            if (e->e_atom.a_type == A_SYMBOL && e->e_atom.a_w.w_symbol == s)
452
453
454
455
            {
                outlet_bang(e->e_outlet);
                return;
            }
Miller Puckette's avatar
Miller Puckette committed
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
    }
    outlet_symbol(x->x_rejectout, s);
}

static void sel2_free(t_sel2 *x)
{
    freebytes(x->x_vec, x->x_nelement * sizeof(*x->x_vec));
}

static void *select_new(t_symbol *s, int argc, t_atom *argv)
{
    t_atom a;
    if (argc == 0)
    {
        argc = 1;
        SETFLOAT(&a, 0);
        argv = &a;
    }
    if (argc == 1)
    {
        t_sel1 *x = (t_sel1 *)pd_new(sel1_class);
        x->x_atom = *argv;
        x->x_outlet1 = outlet_new(&x->x_obj, &s_bang);
479
480
        sel1_proxy_init(&x->x_pxy, x);
        inlet_new(&x->x_obj, &x->x_pxy.l_pd, 0, 0);
Miller Puckette's avatar
Miller Puckette committed
481
482
483
484
485
486
487
488
489
490
491
492
        if (argv->a_type == A_FLOAT)
            x->x_outlet2 = outlet_new(&x->x_obj, &s_float);
        else
            x->x_outlet2 = outlet_new(&x->x_obj, &s_symbol);
        return (x);
    }
    else
    {
        int n;
        t_selectelement *e;
        t_sel2 *x = (t_sel2 *)pd_new(sel2_class);
        x->x_nelement = argc;
493
494
        x->x_type = argv[0].a_type;
        t_int f, s = 0;
Miller Puckette's avatar
Miller Puckette committed
495
496
497
498
        x->x_vec = (t_selectelement *)getbytes(argc * sizeof(*x->x_vec));
        for (n = 0, e = x->x_vec; n < argc; n++, e++)
        {
            e->e_outlet = outlet_new(&x->x_obj, &s_bang);
499
            if (argv[0].a_type == A_FLOAT)
500
501
502
503
            {
                SETFLOAT(&e->e_atom, atom_getfloatarg(n, argc, argv));
                f = 1;
            }
504
            else
505
506
507
508
            {
                SETSYMBOL(&e->e_atom, atom_getsymbolarg(n, argc, argv));
                s = 1;
            }
Miller Puckette's avatar
Miller Puckette committed
509
        }
510
        x->x_mixed = f && s;
Miller Puckette's avatar
Miller Puckette committed
511
512
513
514
515
516
517
518
        x->x_rejectout = outlet_new(&x->x_obj, &s_float);
        return (x);
    }

}

void select_setup(void)
{
519
520
521
522
    sel1_proxy_class = class_new(gensym("select_inlet"),
        0, 0, sizeof(t_sel1_proxy), 0, 0);
    class_addfloat(sel1_proxy_class, (t_method)sel1_proxy_float);
    class_addsymbol(sel1_proxy_class, (t_method)sel1_proxy_symbol);
523

Miller Puckette's avatar
Miller Puckette committed
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
    sel1_class = class_new(gensym("select"), 0, 0,
        sizeof(t_sel1), 0, 0);
    class_addfloat(sel1_class, sel1_float);
    class_addsymbol(sel1_class, sel1_symbol);

    sel2_class = class_new(gensym("select"), 0, (t_method)sel2_free,
        sizeof(t_sel2), 0, 0);
    class_addfloat(sel2_class, sel2_float);
    class_addsymbol(sel2_class, sel2_symbol);

    class_addcreator((t_newmethod)select_new, gensym("select"),  A_GIMME, 0);
    class_addcreator((t_newmethod)select_new, gensym("sel"),  A_GIMME, 0);
}

/* -------------------------- route ------------------------------ */

static t_class *route_class;

542
543
static t_class *route_proxy_class;

Miller Puckette's avatar
Miller Puckette committed
544
545
546
547
548
549
typedef struct _routeelement
{
    t_word e_w;
    t_outlet *e_outlet;
} t_routeelement;

550
551
552
553
554
555
typedef struct _route_proxy
{
    t_pd l_pd;
    void *parent;
} t_route_proxy;

Miller Puckette's avatar
Miller Puckette committed
556
557
558
559
560
561
562
typedef struct _route
{
    t_object x_obj;
    t_atomtype x_type;
    t_int x_nelement;
    t_routeelement *x_vec;
    t_outlet *x_rejectout;
563
    t_route_proxy x_pxy;
564
    t_int x_mixed;
Miller Puckette's avatar
Miller Puckette committed
565
566
} t_route;

567
568
569
570
571
572
573
574
575
static void route_proxy_init(t_route_proxy *x, t_route *p)
{
    x->l_pd = route_proxy_class;
    x->parent = (void *)p;
}

static void route_proxy_float(t_route_proxy *x, t_float f)
{
    t_route *p = (t_route *)x->parent;
576
577
    p->x_type = A_FLOAT;
    p->x_vec->e_w.w_float = f;
578
579
580
581
582
}

static void route_proxy_symbol(t_route_proxy *x, t_symbol *s)
{
    t_route *p = (t_route *)x->parent;
583
584
    p->x_type = A_SYMBOL;
    p->x_vec->e_w.w_symbol = s;
585
586
}

Miller Puckette's avatar
Miller Puckette committed
587
588
589
590
static void route_anything(t_route *x, t_symbol *sel, int argc, t_atom *argv)
{
    t_routeelement *e;
    int nelement;
591
    if (x->x_type == A_SYMBOL || x->x_mixed) 
Miller Puckette's avatar
Miller Puckette committed
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
    {
        for (nelement = x->x_nelement, e = x->x_vec; nelement--; e++)
            if (e->e_w.w_symbol == sel)
        {
            if (argc > 0 && argv[0].a_type == A_SYMBOL)
                outlet_anything(e->e_outlet, argv[0].a_w.w_symbol,
                    argc-1, argv+1);
            else outlet_list(e->e_outlet, 0, argc, argv);
            return;
        }
    }
    outlet_anything(x->x_rejectout, sel, argc, argv);
}

static void route_list(t_route *x, t_symbol *sel, int argc, t_atom *argv)
{
608
    //fprintf(stderr,"route_list\n");
Miller Puckette's avatar
Miller Puckette committed
609
610
    t_routeelement *e;
    int nelement;
611
    if (x->x_type == A_FLOAT || x->x_mixed)
Miller Puckette's avatar
Miller Puckette committed
612
    {
613
        t_float f;
Miller Puckette's avatar
Miller Puckette committed
614
        if (!argc) return;
615
        if (argv->a_type != A_FLOAT)
616
            goto try_symbol;
Miller Puckette's avatar
Miller Puckette committed
617
618
619
620
621
622
623
        f = atom_getfloat(argv);
        for (nelement = x->x_nelement, e = x->x_vec; nelement--; e++)
            if (e->e_w.w_float == f)
        {
            if (argc > 1 && argv[1].a_type == A_SYMBOL)
                outlet_anything(e->e_outlet, argv[1].a_w.w_symbol,
                    argc-2, argv+2);
624
625
626
627
628
629
630
            else
            {
                if (argc > 1)
                    outlet_list(e->e_outlet, 0, argc-1, argv+1);
                else
                    outlet_bang(e->e_outlet);
            }
Miller Puckette's avatar
Miller Puckette committed
631
632
633
            return;
        }
    }
634
635
 try_symbol:
    if (x->x_type == A_SYMBOL || x->x_mixed)    /* symbol arguments */
Miller Puckette's avatar
Miller Puckette committed
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
    {
        if (argc > 1)       /* 2 or more args: treat as "list" */
        {
            for (nelement = x->x_nelement, e = x->x_vec; nelement--; e++)
            {
                if (e->e_w.w_symbol == &s_list)
                {
                    if (argc > 0 && argv[0].a_type == A_SYMBOL)
                        outlet_anything(e->e_outlet, argv[0].a_w.w_symbol,
                            argc-1, argv+1);
                    else outlet_list(e->e_outlet, 0, argc, argv);
                    return;
                }
            }
        }
        else if (argc == 0)         /* no args: treat as "bang" */
        {
            for (nelement = x->x_nelement, e = x->x_vec; nelement--; e++)
            {
                if (e->e_w.w_symbol == &s_bang)
                {
                    outlet_bang(e->e_outlet);
                    return;
                }
            }
        }
        else if (argv[0].a_type == A_FLOAT)     /* one float arg */
        {
            for (nelement = x->x_nelement, e = x->x_vec; nelement--; e++)
            {
                if (e->e_w.w_symbol == &s_float)
                {
668
                    outlet_float(e->e_outlet, argv[0].a_w.w_float);
Miller Puckette's avatar
Miller Puckette committed
669
670
671
672
                    return;
                }
            }
        }
673
        else if (argv[0].a_type == A_SYMBOL)   /* one symbol arg */
Miller Puckette's avatar
Miller Puckette committed
674
675
676
677
678
        {
            for (nelement = x->x_nelement, e = x->x_vec; nelement--; e++)
            {
                if (e->e_w.w_symbol == &s_symbol)
                {
679
                    outlet_symbol(e->e_outlet, argv[0].a_w.w_symbol);
Miller Puckette's avatar
Miller Puckette committed
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
                    return;
                }
            }
        }
    }
    outlet_list(x->x_rejectout, 0, argc, argv);
}


static void route_free(t_route *x)
{
    freebytes(x->x_vec, x->x_nelement * sizeof(*x->x_vec));
}

static void *route_new(t_symbol *s, int argc, t_atom *argv)
{
696
    int n, flt = 0, sym = 0;
Miller Puckette's avatar
Miller Puckette committed
697
698
699
700
701
702
703
704
705
    t_routeelement *e;
    t_route *x = (t_route *)pd_new(route_class);
    t_atom a;
    if (argc == 0)
    {
        argc = 1;
        SETFLOAT(&a, 0);
        argv = &a;
    }
706
    x->x_type = argv[0].a_type;
Miller Puckette's avatar
Miller Puckette committed
707
708
709
710
711
    x->x_nelement = argc;
    x->x_vec = (t_routeelement *)getbytes(argc * sizeof(*x->x_vec));
    for (n = 0, e = x->x_vec; n < argc; n++, e++)
    {
        e->e_outlet = outlet_new(&x->x_obj, &s_list);
712
713
        if (argv[n].a_type == A_FLOAT)
        {
Miller Puckette's avatar
Miller Puckette committed
714
            e->e_w.w_float = atom_getfloatarg(n, argc, argv);
715
716
717
718
719
720
721
            flt = 1;
        }
        else
        {
            e->e_w.w_symbol = atom_getsymbolarg(n, argc, argv);
            sym = 1;
        }
Miller Puckette's avatar
Miller Puckette committed
722
    }
723
724
    if (argc == 1)
    {
725
726
        route_proxy_init(&x->x_pxy, x);
        inlet_new(&x->x_obj, &x->x_pxy.l_pd, 0, 0);
727
        /*if (argv->a_type == A_FLOAT)
728
            floatinlet_new(&x->x_obj, &x->x_vec->e_w.w_float);
729
730
        else
            symbolinlet_new(&x->x_obj, &x->x_vec->e_w.w_symbol);*/
731
    }
732
    x->x_mixed = flt * sym;
Miller Puckette's avatar
Miller Puckette committed
733
734
735
736
737
738
    x->x_rejectout = outlet_new(&x->x_obj, &s_list);
    return (x);
}

void route_setup(void)
{
739
740
741
742
    route_proxy_class = class_new(gensym("route_inlet"),
        0, 0, sizeof(t_route_proxy), 0, 0);
    class_addfloat(route_proxy_class, (t_method)route_proxy_float);
    class_addsymbol(route_proxy_class, (t_method)route_proxy_symbol);
Miller Puckette's avatar
Miller Puckette committed
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
    route_class = class_new(gensym("route"), (t_newmethod)route_new,
        (t_method)route_free, sizeof(t_route), 0, A_GIMME, 0);
    class_addlist(route_class, route_list);
    class_addanything(route_class, route_anything);
}

/* -------------------------- pack ------------------------------ */

static t_class *pack_class;

typedef struct _pack
{
    t_object x_obj;
    t_int x_n;              /* number of args */
    t_atom *x_vec;          /* input values */
    t_int x_nptr;           /* number of pointers */
    t_gpointer *x_gpointer; /* the pointers */
    t_atom *x_outvec;       /* space for output values */
} t_pack;

static void *pack_new(t_symbol *s, int argc, t_atom *argv)
{
    t_pack *x = (t_pack *)pd_new(pack_class);
Mathieu L Bouchard's avatar
Mathieu L Bouchard committed
766
    t_atom defarg[2], *ap, *vp;
Miller Puckette's avatar
Miller Puckette committed
767
768
769
770
771
772
773
774
775
776
777
778
    t_gpointer *gp;
    int nptr = 0;
    int i;
    if (!argc)
    {
        argv = defarg;
        argc = 2;
        SETFLOAT(&defarg[0], 0);
        SETFLOAT(&defarg[1], 0);
    }

    x->x_n = argc;
Mathieu L Bouchard's avatar
Mathieu L Bouchard committed
779
    x->x_vec = (t_atom *)getbytes(argc * sizeof(*x->x_vec));
Miller Puckette's avatar
Miller Puckette committed
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
    x->x_outvec = (t_atom *)getbytes(argc * sizeof(*x->x_outvec));

    for (i = argc, ap = argv; i--; ap++)
        if (ap->a_type == A_SYMBOL && *ap->a_w.w_symbol->s_name == 'p')
            nptr++;

    gp = x->x_gpointer = (t_gpointer *)t_getbytes(nptr * sizeof (*gp));
    x->x_nptr = nptr;

    for (i = 0, vp = x->x_vec, ap = argv; i < argc; i++, ap++, vp++)
    {
        if (ap->a_type == A_FLOAT)
        {
            *vp = *ap;
            if (i) floatinlet_new(&x->x_obj, &vp->a_w.w_float);
        }
        else if (ap->a_type == A_SYMBOL)
        {
            char c = *ap->a_w.w_symbol->s_name;
            if (c == 's')
            {
                SETSYMBOL(vp, &s_symbol);
                if (i) symbolinlet_new(&x->x_obj, &vp->a_w.w_symbol);
            }
            else if (c == 'p')
            {
                vp->a_type = A_POINTER;
                vp->a_w.w_gpointer = gp;
                gpointer_init(gp);
                if (i) pointerinlet_new(&x->x_obj, gp);
                gp++;
            }
            else
            {
                if (c != 'f') pd_error(x, "pack: %s: bad type",
                    ap->a_w.w_symbol->s_name);
                SETFLOAT(vp, 0);
                if (i) floatinlet_new(&x->x_obj, &vp->a_w.w_float);
            }
        }
    }
    outlet_new(&x->x_obj, &s_list);
    return (x);
}

static void pack_bang(t_pack *x)
{
    int i, reentered = 0, size = x->x_n * sizeof (t_atom);
    t_gpointer *gp;
    t_atom *outvec;
    for (i = x->x_nptr, gp = x->x_gpointer; i--; gp++)
        if (!gpointer_check(gp, 1))
    {
        pd_error(x, "pack: stale pointer");
        return;
    }
        /* reentrancy protection.  The first time through use the pre-allocated
        x_outvec; if we're reentered we have to allocate new memory. */
    if (!x->x_outvec)
    {
            /* LATER figure out how to deal with reentrancy and pointers... */
        if (x->x_nptr)
            post("pack_bang: warning: reentry with pointers unprotected");
        outvec = t_getbytes(size);
        reentered = 1;
    }
    else
    {
        outvec = x->x_outvec;
        x->x_outvec = 0;
    }
    memcpy(outvec, x->x_vec, size);
    outlet_list(x->x_obj.ob_outlet, &s_list, x->x_n, outvec);
    if (reentered)
        t_freebytes(outvec, size);
    else x->x_outvec = outvec;
}

static void pack_pointer(t_pack *x, t_gpointer *gp)
{
    if (x->x_vec->a_type == A_POINTER)
    {
        gpointer_unset(x->x_gpointer);
        *x->x_gpointer = *gp;
        if (gp->gp_stub) gp->gp_stub->gs_refcount++;
        pack_bang(x);
    }
    else pd_error(x, "pack_pointer: wrong type");
}

static void pack_float(t_pack *x, t_float f)
{
    if (x->x_vec->a_type == A_FLOAT)
    {
        x->x_vec->a_w.w_float = f;
        pack_bang(x);
    }
    else pd_error(x, "pack_float: wrong type");
}

static void pack_symbol(t_pack *x, t_symbol *s)
{
    if (x->x_vec->a_type == A_SYMBOL)
    {
        x->x_vec->a_w.w_symbol = s;
        pack_bang(x);
    }
    else pd_error(x, "pack_symbol: wrong type");
}

static void pack_list(t_pack *x, t_symbol *s, int ac, t_atom *av)
{
892
893
894
895
    if (ac==0)
        pack_bang(x);
    else
        obj_list(&x->x_obj, 0, ac, av);
Miller Puckette's avatar
Miller Puckette committed
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
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
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
}

static void pack_anything(t_pack *x, t_symbol *s, int ac, t_atom *av)
{
    t_atom *av2 = (t_atom *)getbytes((ac + 1) * sizeof(t_atom));
    int i;
    for (i = 0; i < ac; i++)
        av2[i + 1] = av[i];
    SETSYMBOL(av2, s);
    obj_list(&x->x_obj, 0, ac+1, av2);
    freebytes(av2, (ac + 1) * sizeof(t_atom));
}

static void pack_free(t_pack *x)
{
    t_gpointer *gp;
    int i;
    for (gp = x->x_gpointer, i = x->x_nptr; i--; gp++)
        gpointer_unset(gp);
    freebytes(x->x_vec, x->x_n * sizeof(*x->x_vec));
    freebytes(x->x_outvec, x->x_n * sizeof(*x->x_outvec));
    freebytes(x->x_gpointer, x->x_nptr * sizeof(*x->x_gpointer));
}

static void pack_setup(void)
{
    pack_class = class_new(gensym("pack"), (t_newmethod)pack_new,
        (t_method)pack_free, sizeof(t_pack), 0, A_GIMME, 0);
    class_addbang(pack_class, pack_bang);
    class_addpointer(pack_class, pack_pointer);
    class_addfloat(pack_class, pack_float);
    class_addsymbol(pack_class, pack_symbol);
    class_addlist(pack_class, pack_list);
    class_addanything(pack_class, pack_anything);
}

/* -------------------------- unpack ------------------------------ */

static t_class *unpack_class;

typedef struct unpackout
{
    t_atomtype u_type;
    t_outlet *u_outlet;
} t_unpackout;

typedef struct _unpack
{
    t_object x_obj;
    t_int x_n;
    t_unpackout *x_vec;
} t_unpack;

static void *unpack_new(t_symbol *s, int argc, t_atom *argv)
{
    t_unpack *x = (t_unpack *)pd_new(unpack_class);
    t_atom defarg[2], *ap;
    t_unpackout *u;
    int i;
    if (!argc)
    {
        argv = defarg;
        argc = 2;
        SETFLOAT(&defarg[0], 0);
        SETFLOAT(&defarg[1], 0);
    }
    x->x_n = argc;
    x->x_vec = (t_unpackout *)getbytes(argc * sizeof(*x->x_vec));
    for (i = 0, ap = argv, u = x->x_vec; i < argc; u++, ap++, i++)
    {
        t_atomtype type = ap->a_type;
        if (type == A_SYMBOL)
        {
            char c = *ap->a_w.w_symbol->s_name;
            if (c == 's')
            {
                u->u_type = A_SYMBOL;
                u->u_outlet = outlet_new(&x->x_obj, &s_symbol);
            }
            else if (c == 'p')
            {
                u->u_type =  A_POINTER;
                u->u_outlet = outlet_new(&x->x_obj, &s_pointer);
            }
            else
            {
                if (c != 'f') pd_error(x, "unpack: %s: bad type",
                    ap->a_w.w_symbol->s_name);
                u->u_type = A_FLOAT;
                u->u_outlet = outlet_new(&x->x_obj, &s_float);
            }
        }
        else
        {
            u->u_type =  A_FLOAT;
            u->u_outlet = outlet_new(&x->x_obj, &s_float);
        }
    }
    return (x);
}

static void unpack_list(t_unpack *x, t_symbol *s, int argc, t_atom *argv)
{
    t_atom *ap;
    t_unpackout *u;
    int i;
    if (argc > x->x_n) argc = x->x_n;
    for (i = argc, u = x->x_vec + i, ap = argv + i; u--, ap--, i--;)
    {
        t_atomtype type = u->u_type;
        if (type != ap->a_type)
            pd_error(x, "unpack: type mismatch");
        else if (type == A_FLOAT)
            outlet_float(u->u_outlet, ap->a_w.w_float);
        else if (type == A_SYMBOL)
            outlet_symbol(u->u_outlet, ap->a_w.w_symbol);
        else outlet_pointer(u->u_outlet, ap->a_w.w_gpointer);
    }
}

static void unpack_anything(t_unpack *x, t_symbol *s, int ac, t_atom *av)
{
    t_atom *av2 = (t_atom *)getbytes((ac + 1) * sizeof(t_atom));
    int i;
    for (i = 0; i < ac; i++)
        av2[i + 1] = av[i];
    SETSYMBOL(av2, s);
    unpack_list(x, 0, ac+1, av2);
    freebytes(av2, (ac + 1) * sizeof(t_atom));
}

static void unpack_free(t_unpack *x)
{
    freebytes(x->x_vec, x->x_n * sizeof(*x->x_vec));
}

static void unpack_setup(void)
{
    unpack_class = class_new(gensym("unpack"), (t_newmethod)unpack_new,
        (t_method)unpack_free, sizeof(t_unpack), 0, A_GIMME, 0);
    class_addlist(unpack_class, unpack_list);
    class_addanything(unpack_class, unpack_anything);
}

/* -------------------------- trigger ------------------------------ */

static t_class *trigger_class;
#define TR_BANG 0
#define TR_FLOAT 1
#define TR_SYMBOL 2
#define TR_POINTER 3
#define TR_LIST 4
#define TR_ANYTHING 5

1050
1051
1052
#define TR_STATIC_FLOAT 6
#define TR_STATIC_SYMBOL 7

Miller Puckette's avatar
Miller Puckette committed
1053
1054
1055
typedef struct triggerout
{
    int u_type;         /* outlet type from above */
1056
    t_symbol *u_sym;     /* static value */
1057
    t_float u_float;    /* static value */
Miller Puckette's avatar
Miller Puckette committed
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
    t_outlet *u_outlet;
} t_triggerout;

typedef struct _trigger
{
    t_object x_obj;
    t_int x_n;
    t_triggerout *x_vec;
} t_trigger;

1068
1069
1070
// forward declaration
static void trigger_symbol(t_trigger *x, t_symbol *s);

Miller Puckette's avatar
Miller Puckette committed
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
static void *trigger_new(t_symbol *s, int argc, t_atom *argv)
{
    t_trigger *x = (t_trigger *)pd_new(trigger_class);
    t_atom defarg[2], *ap;
    t_triggerout *u;
    int i;
    if (!argc)
    {
        argv = defarg;
        argc = 2;
        SETSYMBOL(&defarg[0], &s_bang);
        SETSYMBOL(&defarg[1], &s_bang);
    }
    x->x_n = argc;
    x->x_vec = (t_triggerout *)getbytes(argc * sizeof(*x->x_vec));
    for (i = 0, ap = argv, u = x->x_vec; i < argc; u++, ap++, i++)
    {
        t_atomtype thistype = ap->a_type;
        char c;
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
        if (thistype == TR_SYMBOL)
        {
            if (strlen(ap->a_w.w_symbol->s_name) == 1)
                c = ap->a_w.w_symbol->s_name[0];
            else if (strcmp(ap->a_w.w_symbol->s_name, "anything") == 0)
                c = 'a';
            else if (strcmp(ap->a_w.w_symbol->s_name, "bang") == 0)
                c = 'b';
            else if (strcmp(ap->a_w.w_symbol->s_name, "float") == 0)
                c = 'f';
            else if (strcmp(ap->a_w.w_symbol->s_name, "list") == 0)
                c = 'l';
            else if (strcmp(ap->a_w.w_symbol->s_name, "pointer") == 0)
                c = 'p';            
            else if (strcmp(ap->a_w.w_symbol->s_name, "symbol") == 0)
                c = 's';
            else c = 'S';
        }
1108
        else if (thistype == TR_FLOAT)
1109
            c = 'F';
Miller Puckette's avatar
Miller Puckette committed
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
        else c = 0;
        if (c == 'p')
            u->u_type = TR_POINTER,
                u->u_outlet = outlet_new(&x->x_obj, &s_pointer);
        else if (c == 'f')
            u->u_type = TR_FLOAT, u->u_outlet = outlet_new(&x->x_obj, &s_float);
        else if (c == 'b')
            u->u_type = TR_BANG, u->u_outlet = outlet_new(&x->x_obj, &s_bang);
        else if (c == 'l')
            u->u_type = TR_LIST, u->u_outlet = outlet_new(&x->x_obj, &s_list);
        else if (c == 's')
            u->u_type = TR_SYMBOL,
                u->u_outlet = outlet_new(&x->x_obj, &s_symbol);
        else if (c == 'a')
            u->u_type = TR_ANYTHING,
                u->u_outlet = outlet_new(&x->x_obj, &s_symbol);
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
        else if (c == 'F')
        {
            //static float
            u->u_float = ap->a_w.w_float;
            u->u_type = TR_STATIC_FLOAT;
            u->u_outlet = outlet_new(&x->x_obj, &s_float);
        }
        else if (c == 'S')
        {
            //static symbol
1136
            u->u_sym = gensym(ap->a_w.w_symbol->s_name);
1137
            u->u_type = TR_STATIC_SYMBOL;
1138
1139
1140
1141
            u->u_outlet = outlet_new(&x->x_obj, &s_symbol);
        }
        else
        {
Miller Puckette's avatar
Miller Puckette committed
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
            pd_error(x, "trigger: %s: bad type", ap->a_w.w_symbol->s_name);
            u->u_type = TR_FLOAT, u->u_outlet = outlet_new(&x->x_obj, &s_float);
        }
    }
    return (x);
}

static void trigger_list(t_trigger *x, t_symbol *s, int argc, t_atom *argv)
{
    t_triggerout *u;
    int i;
    for (i = x->x_n, u = x->x_vec + i; u--, i--;)
    {
        if (u->u_type == TR_FLOAT)
            outlet_float(u->u_outlet, (argc ? atom_getfloat(argv) : 0));
        else if (u->u_type == TR_BANG)
            outlet_bang(u->u_outlet);
        else if (u->u_type == TR_SYMBOL)
            outlet_symbol(u->u_outlet,
1161
                (argc ? atom_getsymbol(argv) : (s != NULL ? s : &s_symbol)));
1162
1163
        else if (u->u_type == TR_ANYTHING)
            outlet_anything(u->u_outlet, s, argc, argv);
Miller Puckette's avatar
Miller Puckette committed
1164
1165
1166
1167
1168
1169
        else if (u->u_type == TR_POINTER)
        {
            if (!argc || argv->a_type != TR_POINTER)
                pd_error(x, "unpack: bad pointer");
            else outlet_pointer(u->u_outlet, argv->a_w.w_gpointer);
        }
1170
1171
1172
1173
1174
1175
        else if (u->u_type == TR_STATIC_FLOAT)
        {
            outlet_float(u->u_outlet, u->u_float);    
        }
        else if (u->u_type == TR_STATIC_SYMBOL)
        {
1176
            outlet_symbol(u->u_outlet, u->u_sym);
1177
        }
Miller Puckette's avatar
Miller Puckette committed
1178
1179
1180
1181
1182
1183
        else outlet_list(u->u_outlet, &s_list, argc, argv);
    }
}

static void trigger_anything(t_trigger *x, t_symbol *s, int argc, t_atom *argv)
{
1184
1185
    //fprintf(stderr,"trigger_anything %s %d\n", s->s_name, argc);
    t_atom *av2 = NULL;
Miller Puckette's avatar
Miller Puckette committed
1186
    t_triggerout *u;
1187
    int i, j = 0;
Miller Puckette's avatar
Miller Puckette committed
1188
1189
1190
1191
    for (i = x->x_n, u = x->x_vec + i; u--, i--;)
    {
        if (u->u_type == TR_BANG)
            outlet_bang(u->u_outlet);
1192
1193
1194
        else if (u->u_type == TR_ANYTHING)
        {
            //fprintf(stderr,"TR_ANYTHING\n");
Miller Puckette's avatar
Miller Puckette committed
1195
            outlet_anything(u->u_outlet, s, argc, argv);
1196
1197
1198
1199
1200
1201
1202
        }
        else if (u->u_type == TR_STATIC_FLOAT)
        {
            outlet_float(u->u_outlet, u->u_float);    
        }
        else if (u->u_type == TR_STATIC_SYMBOL)
        {
1203
            outlet_symbol(u->u_outlet, u->u_sym);
1204
        }        
1205
        //else trigger_symbol(x, s);
1206
1207
        else
        {
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
            // copying trigger_list behavior except that here we keep
            // the outlet number and therefore avoid redundant printouts
            if (u->u_type == TR_FLOAT)
            {
                //fprintf(stderr,"trigger_anything -> TR_FLOAT %d\n", argc);
                outlet_float(u->u_outlet, (argc ? atom_getfloat(argv) : 0));
            }
            else if (u->u_type == TR_BANG)
            {
                //fprintf(stderr,"trigger_anything -> TR_BANG %d\n", argc);
                outlet_bang(u->u_outlet);
            }
            else if (u->u_type == TR_SYMBOL)
            {
                //fprintf(stderr,"trigger_anything -> TR_SYMBOL %d\n", argc);
                outlet_symbol(u->u_outlet,
                    (s != NULL ? s : (argc ? atom_getsymbol(argv) : &s_symbol)));
            }
            else if (u->u_type == TR_ANYTHING)
            {
                //fprintf(stderr,"trigger_anything -> TR_ANYTHING %d\n", argc);
                outlet_anything(u->u_outlet, s, argc, argv);
            }
            else if (u->u_type == TR_POINTER)
            {
                //fprintf(stderr,"trigger_anything -> TR_POINTER %d\n", argc);
                if (!argc || argv->a_type != TR_POINTER)
                    pd_error(x, "unpack: bad pointer");
                else outlet_pointer(u->u_outlet, argv->a_w.w_gpointer);
            }
            else if (u->u_type == TR_STATIC_FLOAT)
            {
                //fprintf(stderr,"trigger_anything -> TR_STATIC_FLOAT %d\n", argc);
                outlet_float(u->u_outlet, u->u_float);
            }
            else if (u->u_type == TR_STATIC_SYMBOL)
            {
                //fprintf(stderr,"trigger_anything -> TR_STATIC_SYMBOL %d\n", argc);
1246
                outlet_symbol(u->u_outlet, u->u_sym);
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
            }
            else
            {
                // Ico: don't have to worry about zero element case (AFAICT)
                av2 = (t_atom *)getbytes((argc + 1) * sizeof(t_atom));
                SETSYMBOL(av2, s);
                if (argc == 0)
                {
                    //fprintf(stderr,"trigger_anything -> symbol %d\n", argc);
                    outlet_list(u->u_outlet, &s_symbol, argc+1, av2);
                }
                else
                {
                    for (j = 0; j < argc; j++)
                        av2[j + 1] = argv[j];
                    //fprintf(stderr,"trigger_anything -> list %d\n", argc);
                    SETSYMBOL(av2, s);
                    outlet_list(u->u_outlet, &s_list, argc+1, av2);
                }
                freebytes(av2, (argc + 1) * sizeof(t_atom));
            }
1268
        }
Miller Puckette's avatar
Miller Puckette committed
1269
1270
1271
1272
1273
    }
}

static void trigger_bang(t_trigger *x)
{
1274
    trigger_list(x, &s_bang, 0, 0);
Miller Puckette's avatar
Miller Puckette committed
1275
1276
1277
1278
1279
1280
}

static void trigger_pointer(t_trigger *x, t_gpointer *gp)
{
    t_atom at;
    SETPOINTER(&at, gp);
1281
    trigger_list(x, &s_list, 1, &at);
Miller Puckette's avatar
Miller Puckette committed
1282
1283
1284
1285
1286
1287
}

static void trigger_float(t_trigger *x, t_float f)
{
    t_atom at;
    SETFLOAT(&at, f);
1288
    trigger_list(x, &s_float, 1, &at);
Miller Puckette's avatar
Miller Puckette committed
1289
1290
1291
1292
1293
1294
}

static void trigger_symbol(t_trigger *x, t_symbol *s)
{
    t_atom at;
    SETSYMBOL(&at, s);
1295
    trigger_list(x, &s_symbol, 1, &at);
Miller Puckette's avatar
Miller Puckette committed
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
}

static void trigger_free(t_trigger *x)
{
    freebytes(x->x_vec, x->x_n * sizeof(*x->x_vec));
}

static void trigger_setup(void)
{
    trigger_class = class_new(gensym("trigger"), (t_newmethod)trigger_new,
        (t_method)trigger_free, sizeof(t_trigger), 0, A_GIMME, 0);
    class_addcreator((t_newmethod)trigger_new, gensym("t"), A_GIMME, 0);
    class_addlist(trigger_class, trigger_list);
    class_addbang(trigger_class, trigger_bang);
    class_addpointer(trigger_class, trigger_pointer);
    class_addfloat(trigger_class, (t_method)trigger_float);
    class_addsymbol(trigger_class, trigger_symbol);
    class_addanything(trigger_class, trigger_anything);
}

/* -------------------------- spigot ------------------------------ */
static t_class *spigot_class;

typedef struct _spigot
{
    t_object x_obj;
1322
    t_float x_state;
Miller Puckette's avatar
Miller Puckette committed
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
} t_spigot;

static void *spigot_new(t_floatarg f)
{
    t_spigot *x = (t_spigot *)pd_new(spigot_class);
    floatinlet_new(&x->x_obj, &x->x_state);
    outlet_new(&x->x_obj, 0);
    x->x_state = f;
    return (x);
}

static void spigot_bang(t_spigot *x)
{
    if (x->x_state != 0) outlet_bang(x->x_obj.ob_outlet);
}

static void spigot_pointer(t_spigot *x, t_gpointer *gp)
{
    if (x->x_state != 0) outlet_pointer(x->x_obj.ob_outlet, gp);
}

static void spigot_float(t_spigot *x, t_float f)
{
    if (x->x_state != 0) outlet_float(x->x_obj.ob_outlet, f);
}

static void spigot_symbol(t_spigot *x, t_symbol *s)
{
    if (x->x_state != 0) outlet_symbol(x->x_obj.ob_outlet, s);
}

static void spigot_list(t_spigot *x, t_symbol *s, int argc, t_atom *argv)
{
    if (x->x_state != 0) outlet_list(x->x_obj.ob_outlet, s, argc, argv);
}

static void spigot_anything(t_spigot *x, t_symbol *s, int argc, t_atom *argv)
{
    if (x->x_state != 0) outlet_anything(x->x_obj.ob_outlet, s, argc, argv);
}

static void spigot_setup(void)
{
    spigot_class = class_new(gensym("spigot"), (t_newmethod)spigot_new, 0,
        sizeof(t_spigot), 0, A_DEFFLOAT, 0);
    class_addbang(spigot_class, spigot_bang);
    class_addpointer(spigot_class, spigot_pointer);
    class_addfloat(spigot_class, spigot_float);
    class_addsymbol(spigot_class, spigot_symbol);
    class_addlist(spigot_class, spigot_list);
    class_addanything(spigot_class, spigot_anything);
}

/* --------------------------- moses ----------------------------- */
static t_class *moses_class;

typedef struct _moses
{
    t_object x_ob;
    t_outlet *x_out2;
1383
    t_float x_y;
Miller Puckette's avatar
Miller Puckette committed
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
} t_moses;

static void *moses_new(t_floatarg f)
{
    t_moses *x = (t_moses *)pd_new(moses_class);
    floatinlet_new(&x->x_ob, &x->x_y);
    outlet_new(&x->x_ob, &s_float);
    x->x_out2 = outlet_new(&x->x_ob, &s_float);
    x->x_y = f;
    return (x);
}

static void moses_float(t_moses *x, t_float f)
{
    if (f < x->x_y) outlet_float(x->x_ob.ob_outlet, f);
    else outlet_float(x->x_out2, f);
}

static void moses_setup(void)
{
    moses_class = class_new(gensym("moses"), (t_newmethod)moses_new, 0,
        sizeof(t_moses), 0, A_DEFFLOAT, 0);
    class_addfloat(moses_class, moses_float);
}

/* ----------------------- until --------------------- */

static t_class *until_class;

typedef struct _until
{
    t_object x_obj;
    int x_run;
    int x_count;
} t_until;

static void *until_new(void)
{
    t_until *x = (t_until *)pd_new(until_class);
    inlet_new(&x->x_obj, &x->x_obj.ob_pd, gensym("bang"), gensym("bang2"));
    outlet_new(&x->x_obj, &s_bang);
    x->x_run = 0;
    return (x);
}

static void until_bang(t_until *x)
{
    x->x_run = 1;
    x->x_count = -1;
    while (x->x_run && x->x_count)
        x->x_count--, outlet_bang(x->x_obj.ob_outlet);
}

static void until_float(t_until *x, t_float f)
{
1439
1440
    if (f < 0)
        f = 0;
Miller Puckette's avatar
Miller Puckette committed
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
    x->x_run = 1;
    x->x_count = f;
    while (x->x_run && x->x_count)
        x->x_count--, outlet_bang(x->x_obj.ob_outlet);
}

static void until_bang2(t_until *x)
{
    x->x_run = 0;
}

static void until_setup(void)
{
    until_class = class_new(gensym("until"), (t_newmethod)until_new, 0,
        sizeof(t_until), 0, 0);
    class_addbang(until_class, until_bang);
    class_addfloat(until_class, until_float);
    class_addmethod(until_class, (t_method)until_bang2, gensym("bang2"), 0);
}

/* ----------------------- makefilename --------------------- */

static t_class *makefilename_class;

1465
1466
1467
1468
1469
1470
1471
1472
typedef enum {
    NONE = 0,
    INT,
    FLOAT,
    STRING,
    POINTER,
} t_printtype;

Miller Puckette's avatar
Miller Puckette committed
1473
1474
1475
1476
typedef struct _makefilename
{
    t_object x_obj;
    t_symbol *x_format;
1477
    t_printtype x_accept;
Miller Puckette's avatar
Miller Puckette committed
1478
1479
} t_makefilename;

1480
1481
static const char* makefilename_doscanformat(const char *str, t_printtype *typ,
    char *errormsg)
Miller Puckette's avatar
Miller Puckette committed
1482
{
1483
    int infmt = 0, i, hashflag = 0, zeroflag = 0;
1484
    for (; *str; str++)
1485
    {
1486
        if (!infmt && *str == '%')
1487
        {
1488
1489
            /* A single '%' character isn't a valid. It needs a conversion
               specifier ('g', 's', etc.) to follow it */
1490
1491
1492
            if (*(str+1) == '\0')
            {
                str++;
1493
                sprintf(errormsg, "specifier missing");
1494
1495
1496
1497
                *typ = NONE;
                return str;
            }
            infmt = 1;
Miller Puckette's avatar
Miller Puckette committed
1498
1499
            continue;
        }
1500
1501
        if (infmt)
        {
1502
1503
            /* 1) flags
               Check for "%%" which produces a literal '%' in the output */
1504
            if (*str == '%')
1505
            {
1506
1507
                /* Not in a format specifier after all, so let's reset
                   infmt and continue searching... */
1508
                infmt = 0;
1509
1510
                continue;
            }
1511
            for (i = 0; *str && strchr("-+#0", *str) != 0; str++, i++)
1512
            {
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
                /* Check for flags. While a space is a legal flag, Pd's
                   parser would split it off into a separate arg. And since
                   makefilename has always truncated extra args we don't
                   support spaces here. We also don't support the single
                   quote flag.
                   Some flag/specifier combinations can cause undefined
                   behavior so we need to track them. */

                if (*str == '#') hashflag++;
                if (*str == '0') zeroflag++;

1524
1525
                /* Since we're dealing with arbitrary input let's keep
                   the total number of flags sane. */
1526
                if (i > 15)
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
                {
                    sprintf(errormsg, "too many flags");
                    *typ = NONE;
                    return str;
                }
            }
            /* 2) width field
               Consecutive digits. Technically a width field may also be
               '*' to use a variable to set the value. But makefilename has
               never supported that, either generating a memory error or crash
               upon use. So we exclude it here. */
            if (*str == '*')
            {
                sprintf(errormsg, "variable width value not supported");
                *typ = NONE;
                return str;
            }
            int maxwidth = 3;
            for (i = 0; *str && strchr("0123456789", *str) != 0; str++, i++)
            {
                /* Limit width length to prevent out-of-memory errors. */
                if (i >= maxwidth)
                {
                    sprintf(errormsg, "width field cannot be greater than "
                        "%d digits", maxwidth);
                    *typ = NONE;
                    return str;
                }
            }
            /* 3) precision field
               A '.' followed by consecutive digits. Can also have a '*' for
               variable, so we need to check and exclude as with the width
               field above. */
            if (*str == '.')
            {
                str++;
                if (*str == '*')
                {
                    sprintf(errormsg, "variable precision field not supported");
                    *typ = NONE;
                    return str;
                }
                int maxwidth = 3;
                for (i = 0; *str && strchr("0123456789", *str) != 0; str++, i++)
                {
                    if (i >= maxwidth)
                    {
                        sprintf(errormsg, "precision field cannot be greater "
                            "than %d digits", maxwidth);
                        *typ = NONE;
                        return str;
                    }
                }
1580
1581
                /* precision isn't defined for all conversion specifiers. */
                if (*str && strchr("diouxXeEfFgGs", *str) == 0)
1582
                {
1583
1584
                    sprintf(errormsg, "precision field restricted to "
                                      "[diouxXeEfFgGs] specifiers");
1585
1586
1587
1588
1589
1590
1591
1592
                    *typ = NONE;
                    return str;
                }
            }
            /* 4) length field
               Fairly certain the length field doesn't make any sense
               here. At least I've never seen it used, so let's skip it. */

1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
            /* 5) conversion specifier
               The type of value we want to fill the slot with. */

            /* First, check our flags against the allowed specifiers */
            if (*str && hashflag && strchr("fFgGeExXo", *str) == 0)
            {
                sprintf(errormsg, "'#' flag restricted to [fFgGeExXo] "
                                  "specifiers");
                *typ = NONE;
                return str;
            }
            if (*str && zeroflag && strchr("diouxXeEfFgG", *str) == 0)
            {
                sprintf(errormsg, "'0' flag restricted to [diouxXeEfFgG] "
                                  "specifiers");
                *typ = NONE;
                return str;
            }
1611
1612
1613

            /* a C string */
            if (*str == 's')
1614
            {
1615
1616
                *typ = STRING;
                return str;
Miller Puckette's avatar
Miller Puckette committed
1617
            }
1618
1619
1620
1621

            /* a float. There's also 'a' and 'A' from C99, but let's stick
               to the old school ones for now... */
            if (*str && strchr("fFgGeE", *str) != 0)
1622
            {
1623
1624
                *typ = FLOAT;
                return str;
Miller Puckette's avatar
Miller Puckette committed
1625
            }
1626
1627
1628

            /* an int */
            if (*str && strchr("xXdiouc", *str) != 0)
1629
            {
1630
1631
1632
                *typ = INT;
                return str;
            }
1633

1634
1635
1636
            /* a pointer. We don't suppor this in Purr Data because of the
               possibility of both undefined and implementation-specific
               behavior. */
1637
            if (*str && strchr("p", *str) != 0)
1638
            {
1639
1640
1641
1642
                /* *typ = POINTER; */
                /* return str; */
                sprintf(errormsg, "p specifier not supported");
                *typ = NONE;
1643
                return str;
Miller Puckette's avatar
Miller Puckette committed
1644
            }
1645
1646
1647
1648
1649
1650
1651

            /* if we've gotten here it means we are missing a type field.
               Undefined behavior would result, so we will bail here */
            
            /* First, let's check for any remaining type fields which
               we don't support. That way we can give a better error message. */
            if (*str == 'a' || *str == 'A')
1652
                sprintf(errormsg, "hexfloat specifier not supported. If you "
1653
1654
1655
                                  "need this feature make a case on the "
                                  "mailing list and we'll consider adding it");
            else if (*str == 'n')
1656
                sprintf(errormsg, "n specifier not supported");
1657
            else if (*str == 'm')
1658
                sprintf(errormsg, "m specifier not supported");
1659
            else if (*str)
1660
                sprintf(errormsg, "bad specifier type '%c'", *str);
1661
            else
1662
                sprintf(errormsg, "specifier missing");
1663
1664
            *typ = NONE;
            return str;
1665
1666
1667
1668
1669
1670
1671
1672
        }
    }
    *typ = NONE;
    return str;
}

static void makefilename_scanformat(t_makefilename *x)
{
1673
1674
    char errorbuf[MAXPDSTRING];
    errorbuf[0] = '\0';
1675
1676
1677
1678
    const char *str;
    t_printtype typ;
    if (!x->x_format) return;
    str = x->x_format->s_name;
1679
1680
1681
1682
1683
1684
    /* First attempt at parsing the format string for the field type. */
    str = makefilename_doscanformat(str, &typ, errorbuf);
    /* If we got any errors we will have some content in our errorbuf... */
    if (*errorbuf)
    {
        pd_error(x, "makefilename: invalid format string '%s' "
1685
                    "(%s). Supressing output.",
1686
            x->x_format->s_name, errorbuf);
1687
        /* set the format string to zero. It would be great to refuse to
1688
1689
1690
1691
1692
1693
           create the object here, but we also have to deal with new format
           strings with the 'set' message. So for consistency we zero out
           the format string and make it a runtimer error. */
        x->x_format = 0;
        return;
    }
1694
    x->x_accept = typ;
1695
    if (str && (typ != NONE))
1696
    {
1697
1698
1699
1700
1701
1702
        /* try again, to see if there's another format specifier
           (which we forbid) */
        str = makefilename_doscanformat(str, &typ, errorbuf);
        /* If we've got a type other than none-- OR if we've got something
           in our errorbuf-- we've got a syntax error. */
        if (typ != NONE || *errorbuf)
1703
1704
        {
            pd_error(x, "makefilename: invalid format string '%s' "
1705
1706
                        "(too many specifiers). "
                        "Suppressing output.",
1707
                x->x_format->s_name);
1708
1709
            x->x_format = 0;
            return;
Miller Puckette's avatar
Miller Puckette committed
1710
1711
1712
1713
        }
    }
}

Miller Puckette's avatar
Miller Puckette committed
1714
1715
1716
static void *makefilename_new(t_symbol *s)
{
    t_makefilename *x = (t_makefilename *)pd_new(makefilename_class);
1717
1718
    if (!s || !*s->s_name)
        s = gensym("file.%d");
Miller Puckette's avatar
Miller Puckette committed
1719
1720
    outlet_new(&x->x_obj, &s_symbol);
    x->x_format = s;
1721
    x->x_accept = NONE;
Miller Puckette's avatar
Miller Puckette committed
1722
    makefilename_scanformat(x);
Miller Puckette's avatar
Miller Puckette committed
1723
1724
1725
    return (x);
}

1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
static void makefilename_snprintf(t_makefilename *x, char *buf, char *fmt, ...)
{
    int length_minus_null_terminator;
    va_list ap;
    va_start(ap, fmt);
    length_minus_null_terminator =
        vsnprintf(buf, MAXPDSTRING, fmt, ap);
    va_end(ap);
    if (length_minus_null_terminator >= MAXPDSTRING)
    {
        /* Just don't trust snprintf... */
        buf[MAXPDSTRING-1] = '\0';
        pd_error(x, "makefilename: output truncated to %d characters",
            MAXPDSTRING);
    }