x_connective.c 50 KB
Newer Older
Miller Puckette's avatar
Miller Puckette committed
1
2
3
4
5
6
7
8
9
10
11
12
13
/* 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>
extern t_pd *newest;

/* -------------------------- int ------------------------------ */
14
static t_class *pdint_class;
Miller Puckette's avatar
Miller Puckette committed
15
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

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 ------------------------------ */
51
static t_class *pdfloat_class;
Miller Puckette's avatar
Miller Puckette committed
52
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
89
90
91
92
93
94
95
96
97

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);
}

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);
}

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

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
138
139
    /* Currently this function isn't used... */
/*
Miller Puckette's avatar
Miller Puckette committed
140
141
142
143
144
145
146
147
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
148
*/
Miller Puckette's avatar
Miller Puckette committed
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
177
178
179
180
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

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;

329
330
331
332
333
334
335
336
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
337
338
339
340
341
342
typedef struct _sel1
{
    t_object x_obj;
    t_atom x_atom;
    t_outlet *x_outlet1;
    t_outlet *x_outlet2;
343
    t_sel1_proxy x_pxy;
Miller Puckette's avatar
Miller Puckette committed
344
345
} t_sel1;

346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
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);
}

364
365
366
367
368
369
370
static void sel1_proxy_anything(t_sel1_proxy *x, t_symbol *s, int argc,
    t_atom *argv)
{
    t_sel1 *parent = (t_sel1 *)x->parent;
    pd_error(parent, "select: no method for %s", s->s_name);
}

Miller Puckette's avatar
Miller Puckette committed
371
372
373
374
375
376
377
378
379
380
381
382
383
384
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);
}

385
386
static void sel1_bang(t_sel1 *x)
{
387
    sel1_symbol(x, gensym("bang"));
388
389
}

Miller Puckette's avatar
Miller Puckette committed
390
391
392
393
static t_class *sel2_class;

typedef struct _selectelement
{
394
    t_atom e_atom;
Miller Puckette's avatar
Miller Puckette committed
395
396
397
398
399
400
401
    t_outlet *e_outlet;
} t_selectelement;

typedef struct _sel2
{
    t_object x_obj;
    t_atomtype x_type;
402
    t_int x_mixed;
Miller Puckette's avatar
Miller Puckette committed
403
404
405
406
407
408
409
410
411
    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;
412
    if (x->x_type == A_FLOAT || x->x_mixed)
Miller Puckette's avatar
Miller Puckette committed
413
414
    {
        for (nelement = x->x_nelement, e = x->x_vec; nelement--; e++)
415
            if (e->e_atom.a_type == A_FLOAT && e->e_atom.a_w.w_float == f)
416
417
418
419
            {
                outlet_bang(e->e_outlet);
                return;
            }
Miller Puckette's avatar
Miller Puckette committed
420
421
422
423
424
425
426
427
    }
    outlet_float(x->x_rejectout, f);
}

static void sel2_symbol(t_sel2 *x, t_symbol *s)
{
    t_selectelement *e;
    int nelement;
428
    if (x->x_type == A_SYMBOL || x->x_mixed)
Miller Puckette's avatar
Miller Puckette committed
429
430
    {
        for (nelement = x->x_nelement, e = x->x_vec; nelement--; e++)
431
            if (e->e_atom.a_type == A_SYMBOL && e->e_atom.a_w.w_symbol == s)
432
433
434
435
            {
                outlet_bang(e->e_outlet);
                return;
            }
Miller Puckette's avatar
Miller Puckette committed
436
437
438
439
    }
    outlet_symbol(x->x_rejectout, s);
}

440
441
static void sel2_bang(t_sel2 *x)
{
442
    sel2_symbol(x, gensym("bang"));
443
444
}

Miller Puckette's avatar
Miller Puckette committed
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
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);
464
465
        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
466
467
468
469
470
471
472
473
474
475
476
477
        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;
478
479
        x->x_type = argv[0].a_type;
        t_int f, s = 0;
Miller Puckette's avatar
Miller Puckette committed
480
481
482
483
        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);
484
            if (argv[0].a_type == A_FLOAT)
485
486
487
488
            {
                SETFLOAT(&e->e_atom, atom_getfloatarg(n, argc, argv));
                f = 1;
            }
489
            else
490
491
492
493
            {
                SETSYMBOL(&e->e_atom, atom_getsymbolarg(n, argc, argv));
                s = 1;
            }
Miller Puckette's avatar
Miller Puckette committed
494
        }
495
        x->x_mixed = f && s;
Miller Puckette's avatar
Miller Puckette committed
496
497
498
499
500
501
502
503
        x->x_rejectout = outlet_new(&x->x_obj, &s_float);
        return (x);
    }

}

void select_setup(void)
{
504
505
506
507
    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);
508
    class_addanything(sel1_proxy_class, (t_method)sel1_proxy_anything);
Miller Puckette's avatar
Miller Puckette committed
509
510
511
512
    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);
513
    class_addbang(sel1_class, sel1_bang);
Miller Puckette's avatar
Miller Puckette committed
514
515
516
517
518

    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);
519
    class_addbang(sel2_class, sel2_bang);
Miller Puckette's avatar
Miller Puckette committed
520
521
522
523
524
525
526
527
528

    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;

529
530
static t_class *route_proxy_class;

Miller Puckette's avatar
Miller Puckette committed
531
532
533
534
535
536
typedef struct _routeelement
{
    t_word e_w;
    t_outlet *e_outlet;
} t_routeelement;

537
538
539
540
541
542
typedef struct _route_proxy
{
    t_pd l_pd;
    void *parent;
} t_route_proxy;

Miller Puckette's avatar
Miller Puckette committed
543
544
545
546
547
548
549
typedef struct _route
{
    t_object x_obj;
    t_atomtype x_type;
    t_int x_nelement;
    t_routeelement *x_vec;
    t_outlet *x_rejectout;
550
    t_route_proxy x_pxy;
551
    t_int x_mixed;
Miller Puckette's avatar
Miller Puckette committed
552
553
} t_route;

554
555
556
557
558
559
560
561
562
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;
563
564
    p->x_type = A_FLOAT;
    p->x_vec->e_w.w_float = f;
565
566
567
568
569
}

static void route_proxy_symbol(t_route_proxy *x, t_symbol *s)
{
    t_route *p = (t_route *)x->parent;
570
571
    p->x_type = A_SYMBOL;
    p->x_vec->e_w.w_symbol = s;
572
573
}

Miller Puckette's avatar
Miller Puckette committed
574
575
576
577
static void route_anything(t_route *x, t_symbol *sel, int argc, t_atom *argv)
{
    t_routeelement *e;
    int nelement;
578
    if (x->x_type == A_SYMBOL || x->x_mixed) 
Miller Puckette's avatar
Miller Puckette committed
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
    {
        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)
{
595
    //fprintf(stderr,"route_list\n");
Miller Puckette's avatar
Miller Puckette committed
596
597
    t_routeelement *e;
    int nelement;
598
    if (x->x_type == A_FLOAT || x->x_mixed)
Miller Puckette's avatar
Miller Puckette committed
599
    {
600
        t_float f;
Miller Puckette's avatar
Miller Puckette committed
601
        if (!argc) return;
602
        if (argv->a_type != A_FLOAT)
603
            goto try_symbol;
Miller Puckette's avatar
Miller Puckette committed
604
605
606
607
608
609
610
        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);
611
612
613
614
615
616
617
            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
618
619
620
            return;
        }
    }
621
622
 try_symbol:
    if (x->x_type == A_SYMBOL || x->x_mixed)    /* symbol arguments */
Miller Puckette's avatar
Miller Puckette committed
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
    {
        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)
                {
655
                    outlet_float(e->e_outlet, argv[0].a_w.w_float);
Miller Puckette's avatar
Miller Puckette committed
656
657
658
659
                    return;
                }
            }
        }
660
        else if (argv[0].a_type == A_SYMBOL)   /* one symbol arg */
Miller Puckette's avatar
Miller Puckette committed
661
662
663
664
665
        {
            for (nelement = x->x_nelement, e = x->x_vec; nelement--; e++)
            {
                if (e->e_w.w_symbol == &s_symbol)
                {
666
                    outlet_symbol(e->e_outlet, argv[0].a_w.w_symbol);
Miller Puckette's avatar
Miller Puckette committed
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
                    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)
{
683
    int n, flt = 0, sym = 0;
Miller Puckette's avatar
Miller Puckette committed
684
685
686
687
688
689
690
691
692
    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;
    }
693
    x->x_type = argv[0].a_type;
Miller Puckette's avatar
Miller Puckette committed
694
695
696
697
698
    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);
699
700
        if (argv[n].a_type == A_FLOAT)
        {
Miller Puckette's avatar
Miller Puckette committed
701
            e->e_w.w_float = atom_getfloatarg(n, argc, argv);
702
703
704
705
706
707
708
            flt = 1;
        }
        else
        {
            e->e_w.w_symbol = atom_getsymbolarg(n, argc, argv);
            sym = 1;
        }
Miller Puckette's avatar
Miller Puckette committed
709
    }
710
711
    if (argc == 1)
    {
712
713
        route_proxy_init(&x->x_pxy, x);
        inlet_new(&x->x_obj, &x->x_pxy.l_pd, 0, 0);
714
        /*if (argv->a_type == A_FLOAT)
715
            floatinlet_new(&x->x_obj, &x->x_vec->e_w.w_float);
716
717
        else
            symbolinlet_new(&x->x_obj, &x->x_vec->e_w.w_symbol);*/
718
    }
719
    x->x_mixed = flt * sym;
Miller Puckette's avatar
Miller Puckette committed
720
721
722
723
724
725
    x->x_rejectout = outlet_new(&x->x_obj, &s_list);
    return (x);
}

void route_setup(void)
{
726
727
728
729
    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
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
    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
753
    t_atom defarg[2], *ap, *vp;
Miller Puckette's avatar
Miller Puckette committed
754
755
756
757
758
759
760
761
762
763
764
765
    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
766
    x->x_vec = (t_atom *)getbytes(argc * sizeof(*x->x_vec));
Miller Puckette's avatar
Miller Puckette committed
767
768
769
770
771
772
773
774
775
776
777
778
779
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
    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)
{
879
880
881
882
    if (ac==0)
        pack_bang(x);
    else
        obj_list(&x->x_obj, 0, ac, av);
Miller Puckette's avatar
Miller Puckette committed
883
884
885
886
887
888
889
890
891
892
893
894
895
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
}

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

1037
1038
1039
#define TR_STATIC_FLOAT 6
#define TR_STATIC_SYMBOL 7

Miller Puckette's avatar
Miller Puckette committed
1040
1041
1042
typedef struct triggerout
{
    int u_type;         /* outlet type from above */
1043
    t_symbol *u_sym;     /* static value */
1044
    t_float u_float;    /* static value */
Miller Puckette's avatar
Miller Puckette committed
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
    t_outlet *u_outlet;
} t_triggerout;

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

1055
1056
1057
// forward declaration
static void trigger_symbol(t_trigger *x, t_symbol *s);

Miller Puckette's avatar
Miller Puckette committed
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
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;
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
        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';
        }
1095
        else if (thistype == TR_FLOAT)
1096
            c = 'F';
Miller Puckette's avatar
Miller Puckette committed
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
        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);
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
        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
1123
            u->u_sym = gensym(ap->a_w.w_symbol->s_name);
1124
            u->u_type = TR_STATIC_SYMBOL;
1125
1126
1127
1128
            u->u_outlet = outlet_new(&x->x_obj, &s_symbol);
        }
        else
        {
Miller Puckette's avatar
Miller Puckette committed
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
            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,
1148
                (argc ? atom_getsymbol(argv) : (s != NULL ? s : &s_symbol)));
1149
1150
        else if (u->u_type == TR_ANYTHING)
            outlet_anything(u->u_outlet, s, argc, argv);
Miller Puckette's avatar
Miller Puckette committed
1151
1152
1153
1154
1155
1156
        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);
        }
1157
1158
1159
1160
1161
1162
        else if (u->u_type == TR_STATIC_FLOAT)
        {
            outlet_float(u->u_outlet, u->u_float);    
        }
        else if (u->u_type == TR_STATIC_SYMBOL)
        {
1163
            outlet_symbol(u->u_outlet, u->u_sym);
1164
        }
Miller Puckette's avatar
Miller Puckette committed
1165
1166
1167
1168
1169
1170
        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)
{
1171
1172
    //fprintf(stderr,"trigger_anything %s %d\n", s->s_name, argc);
    t_atom *av2 = NULL;
Miller Puckette's avatar
Miller Puckette committed
1173
    t_triggerout *u;
1174
    int i, j = 0;
Miller Puckette's avatar
Miller Puckette committed
1175
1176
1177
1178
    for (i = x->x_n, u = x->x_vec + i; u--, i--;)
    {
        if (u->u_type == TR_BANG)
            outlet_bang(u->u_outlet);
1179
1180
1181
        else if (u->u_type == TR_ANYTHING)
        {
            //fprintf(stderr,"TR_ANYTHING\n");
Miller Puckette's avatar
Miller Puckette committed
1182
            outlet_anything(u->u_outlet, s, argc, argv);
1183
1184
1185
1186
1187
1188
1189
        }
        else if (u->u_type == TR_STATIC_FLOAT)
        {
            outlet_float(u->u_outlet, u->u_float);    
        }
        else if (u->u_type == TR_STATIC_SYMBOL)
        {
1190
            outlet_symbol(u->u_outlet, u->u_sym);
1191
        }        
1192
        //else trigger_symbol(x, s);
1193
1194
        else
        {
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
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
            // 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);
1233
                outlet_symbol(u->u_outlet, u->u_sym);
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
            }
            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));
            }
1255
        }
Miller Puckette's avatar
Miller Puckette committed
1256
1257
1258
1259
1260
    }
}

static void trigger_bang(t_trigger *x)
{
1261
    trigger_list(x, &s_bang, 0, 0);
Miller Puckette's avatar
Miller Puckette committed
1262
1263
1264
1265
1266
1267
}

static void trigger_pointer(t_trigger *x, t_gpointer *gp)
{
    t_atom at;
    SETPOINTER(&at, gp);
1268
    trigger_list(x, &s_list, 1, &at);
Miller Puckette's avatar
Miller Puckette committed
1269
1270
1271
1272
1273
1274
}

static void trigger_float(t_trigger *x, t_float f)
{
    t_atom at;
    SETFLOAT(&at, f);
1275
    trigger_list(x, &s_float, 1, &at);
Miller Puckette's avatar
Miller Puckette committed
1276
1277
1278
1279
1280
1281
}

static void trigger_symbol(t_trigger *x, t_symbol *s)
{
    t_atom at;
    SETSYMBOL(&at, s);
1282
    trigger_list(x, &s_symbol, 1, &at);
Miller Puckette's avatar
Miller Puckette committed
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
}

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;
1309
    t_float x_state;
Miller Puckette's avatar
Miller Puckette committed
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
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
} 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;
1370
    t_float x_y;
Miller Puckette's avatar
Miller Puckette committed
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
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
} 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)
{
1426
1427
    if (f < 0)
        f = 0;
Miller Puckette's avatar
Miller Puckette committed
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
    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;

1452
1453
1454
1455
1456
1457
1458
1459
typedef enum {
    NONE = 0,
    INT,
    FLOAT,
    STRING,
    POINTER,
} t_printtype;

Miller Puckette's avatar
Miller Puckette committed
1460
1461
1462
1463
typedef struct _makefilename
{
    t_object x_obj;
    t_symbol *x_format;
1464
    t_printtype x_accept;
Miller Puckette's avatar
Miller Puckette committed
1465
1466
} t_makefilename;

1467
static const char* makefilename_doscanformat(const char *str, t_printtype *typ)
Miller Puckette's avatar
Miller Puckette committed
1468
{
Mathieu L Bouchard's avatar
Mathieu L Bouchard committed
1469
    int infmt=0;
1470
    for (; *str; str++)
1471
1472
1473
    {
        if (!infmt && *str=='%')
        {
Miller Puckette's avatar
Miller Puckette committed
1474
1475
1476
            infmt=1;
            continue;
        }
1477
1478
        if (infmt)
        {
1479
1480
1481
1482
1483
            if (*str=='%')
            {
                infmt=0;
                continue;
            }
Miller Puckette's avatar
Miller Puckette committed
1484
1485
            if (strchr("-.#0123456789",*str)!=0)
                continue;
1486
1487
            if (*str=='s')
            {
1488
1489
                *typ = STRING;
                return str;
Miller Puckette's avatar
Miller Puckette committed
1490
            }
1491
1492
            if (strchr("fgGeE",*str)!=0)
            {
1493
1494
                *typ = FLOAT;
                return str;
Miller Puckette's avatar
Miller Puckette committed
1495
            }
1496
1497
            if (strchr("xXdiouc",*str)!=0)
            {
1498
1499
1500
1501
1502
1503
1504
                *typ = INT;
                return str;
            }
            if (strchr("p",*str)!=0)
            {
                *typ = POINTER;
                return str;
Miller Puckette's avatar
Miller Puckette committed
1505
            }
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
        }
    }
    *typ = NONE;
    return str;
}

static void makefilename_scanformat(t_makefilename *x)
{
    const char *str;
    t_printtype typ;
    if (!x->x_format) return;
    str = x->x_format->s_name;
    str = makefilename_doscanformat(str, &typ);
    x->x_accept = typ;
    if (str && (NONE != typ))
    {
            /* try again, to see if there's another format specifier
               (which we forbid) */
        str = makefilename_doscanformat(str, &typ);
        if (NONE != typ)
        {
            pd_error(x, "makefilename: invalid format string '%s' "
                        "(too many format specifiers)", x->x_format->s_name);
            x->x_format = 0;
            return;
Miller Puckette's avatar
Miller Puckette committed
1531
1532
1533
1534
        }
    }
}

Miller Puckette's avatar
Miller Puckette committed
1535
1536
1537
static void *makefilename_new(t_symbol *s)
{
    t_makefilename *x = (t_makefilename *)pd_new(makefilename_class);
1538
1539
    if (!s || !*s->s_name)
        s = gensym("file.%d");
Miller Puckette's avatar
Miller Puckette committed
1540
1541
    outlet_new(&x->x_obj, &s_symbol);
    x->x_format = s;
1542
    x->x_accept = NONE;
Miller Puckette's avatar
Miller Puckette committed
1543
    makefilename_scanformat(x);
Miller Puckette's avatar
Miller Puckette committed
1544
1545
1546
    return (x);
}

1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
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);
    }
}

Miller Puckette's avatar
Miller Puckette committed
1564
1565
1566
static void makefilename_float(t_makefilename *x, t_floatarg f)
{
    char buf[MAXPDSTRING];
1567
    if (!x->x_format)
1568
    {
1569
1570
        pd_error(x, "makefilename: no format specifier given");
        return;
Miller Puckette's avatar
Miller Puckette committed
1571
    }
1572
    switch(x->x_accept)
1573
    {
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
    case NONE:
        makefilename_snprintf(x, buf, "%s",  x->x_format->s_name);
        break;
    case INT: case POINTER:
        makefilename_snprintf(x, buf, x->x_format->s_name, (int)f);
        break;
    case FLOAT:
        makefilename_snprintf(x, buf, x->x_format->s_name, f);
        break;
    case STRING: {
1584
        char buf2[MAXPDSTRING];
1585
1586
1587
1588
1589
1590
        makefilename_snprintf(x, buf2, "%g", f);
        makefilename_snprintf(x, buf, x->x_format->s_name, buf2);
        break;
    }
    default:
        makefilename_snprintf(x, buf, "%s", x->x_format->s_name);
1591
    }
Miller Puckette's avatar
Miller Puckette committed
1592
    if (buf[0]!=0)
1593
        outlet_symbol(x->x_obj.ob_outlet, gensym(buf));
Miller Puckette's avatar
Miller Puckette committed
1594
1595
1596
1597
1598
}

static void makefilename_symbol(t_makefilename *x, t_symbol *s)
{
    char buf[MAXPDSTRING];
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
    if (!x->x_format)
    {
        pd_error(x, "makefilename: no format specifier given");
        return;
    }
    switch(x->x_accept)
    {
    case STRING: case POINTER:
        makefilename_snprintf(x, buf, x->x_format->s_name, s->s_name);
        break;
    case INT:
        makefilename_snprintf(x, buf, x->x_format->s_name, 0);
        break;
    case FLOAT:
        makefilename_snprintf(x, buf, x->x_format->s_name, 0.);
        break;
    case NONE:
        makefilename_snprintf(x, buf, "%s", x->x_format->s_name);
        break;
    default:
        makefilename_snprintf(x, buf, "%s", x->x_format->s_name);
    }
    if (buf[0]!=0)
        outlet_symbol(x->x_obj.ob_outlet, gensym(buf));
}

static void makefilename_bang(t_makefilename *x)
{
    char buf[MAXPDSTRING];
    if(!x->x_format)
    {
        pd_error(x, "makefilename: no format specifier given");
        return;
    }
    switch(x->x_accept)
    {
    case INT:
        makefilename_snprintf(x, buf, x->x_format->s_name, 0);
        break;
    case FLOAT:
        makefilename_snprintf(x, buf, x->x_format->s_name, 0.);
        break;
    case NONE:
        makefilename_snprintf(x, buf, "%s", x->x_format->s_name);
        break;
    default:
        makefilename_snprintf(x, buf, "%s", x->x_format->s_name);
    }
Miller Puckette's avatar
Miller Puckette committed
1647
    if (buf[0]!=0)
1648
        outlet_symbol(x->x_obj.ob_outlet, gensym(buf));
Miller Puckette's avatar
Miller Puckette committed
1649
1650
1651
1652
1653
}

static void makefilename_set(t_makefilename *x, t_symbol *s)
{
    x->x_format = s;
Miller Puckette's avatar
Miller Puckette committed
1654
    makefilename_scanformat(x);
Miller Puckette's avatar
Miller Puckette committed
1655
1656
1657
1658
1659
1660
1661
1662
1663
}

static void makefilename_setup(void)
{
    makefilename_class = class_new(gensym("makefilename"),
    (t_newmethod)makefilename_new, 0,
        sizeof(t_makefilename), 0, A_DEFSYM, 0);
    class_addfloat(makefilename_class, makefilename_float);
    class_addsymbol(makefilename_class, makefilename_symbol);
1664
    class_addbang(makefilename_class, makefilename_bang);
Miller Puckette's avatar
Miller Puckette committed
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
    class_addmethod(makefilename_class, (t_method)makefilename_set,
        gensym("set"), A_SYMBOL, 0);
}

/* -------------------------- swap ------------------------------ */
static t_class *swap_class;

typedef struct _swap
{
    t_object x_obj;
    t_outlet *x_out2;
    t_float x_f1;
    t_float x_f2;
} t_swap;

static void *swap_new(t_floatarg f)
{
    t_swap *x = (t_swap *)pd_new(swap_class);
    x->x_f2 = f;
    x->x_f1 = 0;
    outlet_new(&x->x_obj, &s_float);
    x->x_out2 = outlet_new(&x->x_obj, &s_float);
    floatinlet_new(&x->x_obj, &x->x_f2);
    return (x);
}

static void swap_bang(t_swap *x)
{
    outlet_float(x->x_out2, x->x_f1);
    outlet_float(x->x_obj.ob_outlet, x->x_f2);
}

static void swap_float(t_swap *x, t_float f)
{
    x->x_f1 = f;
    swap_bang(x);
}

void swap_setup(void)
{
    swap_class = class_new(gensym("swap"), (t_newmethod)swap_new, 0,
        sizeof(t_swap), 0, A_DEFFLOAT, 0);
    class_addcreator((t_newmethod)swap_new, gensym("fswap"), A_DEFFLOAT, 0);
    class_addbang(swap_class, swap_bang);
    class_addfloat(swap_class, swap_float);
}

/* -------------------------- change ------------------------------ */
static t_class *change_class;

typedef struct _change
{
    t_object x_obj;
    t_float x_f;
} t_change;

static void *change_new(t_floatarg f)
{
    t_change *x = (t_change *)pd_new(change_class);
    x->x_f = f;
    outlet_new(&x->x_obj, &s_float);
    return (x);
}

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

static void change_float(t_change *x, t_float f)
{
    if (f != x->x_f)
    {
        x->x_f = f;
        outlet_float(x->x_obj.ob_outlet, x->x_f);
    }
}

static void change_set(t_change *x, t_float f)
{
    x->x_f = f;
}

void change_setup(void)
{
    change_class = class_new(gensym("change"), (t_newmethod)change_new, 0,
        sizeof(t_change), 0, A_DEFFLOAT, 0);
    class_addbang(change_class, change_bang);
    class_addfloat(change_class, change_float);
    class_addmethod(change_class, (t_method)change_set, gensym("set"),
        A_DEFFLOAT, 0);
}

/* -------------------- value ------------------------------ */

static t_class *value_class, *vcommon_class;

typedef struct vcommon
{
    t_pd c_pd;
    int c_refcount;
    t_float c_f;
} t_vcommon;

typedef struct _value
{
    t_object x_obj;
    t_symbol *x_sym;
    t_float *x_floatstar;
} t_value;

    /* get a pointer to a named floating-point variable.  The variable
    belongs to a "vcommon" object, which is created if necessary. */
t_float *value_get(t_symbol *s)
{
    t_vcommon *c = (t_vcommon *)pd_findbyclass(s, vcommon_class);
    if (!c)
    {
        c = (t_vcommon *)pd_new(vcommon_class);
        c->c_f = 0;
        c->c_refcount = 0;
        pd_bind(&c->c_pd, s);
    }
    c->c_refcount++;
    return (&c->c_f);
}

    /* release a variable.  This only frees the "vcommon" resource when the
    last interested party releases it. */
void value_release(t_symbol *s)
{
    t_vcommon *c = (t_vcommon *)pd_findbyclass(s, vcommon_class);
    if (c)
    {
        if (!--c->c_refcount)
        {
            pd_unbind(&c->c_pd, s);
            pd_free(&c->c_pd);
        }
    }
    else bug("value_release");
}

/*
 * value_getfloat -- obtain the float value of a "value" object 
 *                  return 0 on success, 1 otherwise
 */
int
value_getfloat(t_symbol *s, t_float *f) 
{
    t_vcommon *c = (t_vcommon *)pd_findbyclass(s, vcommon_class);
    if (!c)
        return (1);
    *f = c->c_f;
    return (0); 
}
 
/*
 * value_setfloat -- set the float value of a "value" object
 *                  return 0 on success, 1 otherwise
 */
int
value_setfloat(t_symbol *s, t_float f)
{
    t_vcommon *c = (t_vcommon *)pd_findbyclass(s, vcommon_class);
    if (!c)
        return (1);
    c->c_f = f; 
    return (0); 
}

1836
1837
1838
1839
1840
static void vcommon_float(t_vcommon *x, t_float f)
{
    x->c_f = f;
}

Miller Puckette's avatar
Miller Puckette committed
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
static void *value_new(t_symbol *s)
{
    t_value *x = (t_value *)pd_new(value_class);
    x->x_sym = s;