aboutsummaryrefslogtreecommitdiff
path: root/modules/generic/pdp_trigger.c
blob: 36a9f3d542e350ba1fbc63d57f52032189fd59b6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
51
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/*
 *   Pure Data Packet module.
 *   Copyright (c) by Tom Schouten <tom@zwizwa.be>
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 2 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program; if not, write to the Free Software
 *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 */



#include "pdp.h"
#include "pdp_internals.h"

/* adapted from the pd trigger object */

#define TR_BANG 0
#define TR_FLOAT 1
#define TR_SYMBOL 2
#define TR_POINTER 3
#define TR_LIST 4
#define TR_ANYTHING 5
#define TR_PDP 6

/*

$$$TODO: emplement so that it behaves like the standard trigger object

i.e. [trigger bang pdp pdp bang pdp]

register_ro and register_rw messages pass right trough,
since they're not action events, only configure events.
a bang is made equivalent to a process event.

*/

typedef struct triggerout
{
    int u_type;         /* outlet type from above */
    t_outlet *u_outlet;
} t_triggerout;


typedef struct pdp_trigger_struct
{
    t_object x_obj;
    t_float x_f;

    int x_n;
    t_triggerout *x_vec;

} t_pdp_trigger;




static void pdp_trigger_input_pdp(t_pdp_trigger *x, t_symbol *s, t_floatarg f)
{
    t_atom atom[2];
    t_symbol *pdp = S_PDP;
    t_symbol *prc = S_PROCESS;
    t_triggerout *u;
    int i;

    for (i = x->x_n, u = x->x_vec + i; u--, i--;){
	/* trigger bang outlet only when a process event is recieved */
	if ((u->u_type == TR_BANG) && (s == prc)){
	    outlet_bang(u->u_outlet);
	}
	/* just pass the message if it is a pdp outlet */
	if ((u->u_type) == TR_PDP){
	    SETSYMBOL(atom+0, s);
	    SETFLOAT(atom+1, f);
	    if (s == prc) outlet_anything(u->u_outlet, pdp, 1, atom);
	    else outlet_anything(u->u_outlet, pdp, 2, atom);
	    
	}
    }
    
}

static void pdp_trigger_input_dpd(t_pdp_trigger *x, t_symbol *s, t_floatarg f)
{
    t_atom atom[2];
    t_symbol *dpd = S_DPD;
    t_symbol *acc = S_ACCUMULATE;
    t_triggerout *u;
    int i;
    int p = (int)f;

    for (i = x->x_n, u = x->x_vec + i; u--, i--;){
	/* trigger outlet only when an accumulate event is recieved */
	if (s == acc){

	    /* output bang */
	    if (u->u_type == TR_BANG) outlet_bang(u->u_outlet);

	    /* output a complete dpd message if it is a pdp outlet */
	    if ((u->u_type) == TR_PDP){
		outlet_dpd(u->u_outlet, p);
	    }
	}
    }
    
}


static void pdp_trigger_free(t_pdp_trigger *x)
{
    pdp_dealloc(x->x_vec);
}

t_class *pdp_trigger_class;



static void *pdp_trigger_new(t_symbol *s, int argc, t_atom *argv)
{
    t_pdp_trigger *x = (t_pdp_trigger *)pd_new(pdp_trigger_class);
    t_atom defarg[2], *ap;
    t_triggerout *u;
    int i;


    if (!argc)
    {
        argv = defarg;
        argc = 2;
        SETSYMBOL(&defarg[0], gensym("pdp"));
        SETSYMBOL(&defarg[1], gensym("bang"));
    }

    x->x_n = argc;
    x->x_vec = pdp_alloc(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;
        if (thistype == TR_SYMBOL) c = ap->a_w.w_symbol->s_name[0];
        else c = 0;
        if (c == 'p')
            u->u_type = TR_PDP,
                u->u_outlet = outlet_new(&x->x_obj, &s_anything);
        else if (c == 'b')
            u->u_type = TR_BANG, u->u_outlet = outlet_new(&x->x_obj, &s_bang);
        else
        {
            pd_error(x, "pdp_trigger: %s: bad type", ap->a_w.w_symbol->s_name);
            u->u_type = TR_BANG, u->u_outlet = outlet_new(&x->x_obj, &s_bang);
        }
    }

    return (void *)x;
}


#ifdef __cplusplus
extern "C"
{
#endif


void pdp_trigger_setup(void)
{


    pdp_trigger_class = class_new(gensym("pdp_trigger"), (t_newmethod)pdp_trigger_new,
    	(t_method)pdp_trigger_free, sizeof(t_pdp_trigger), 0, A_GIMME, A_NULL);

    class_addcreator((t_newmethod)pdp_trigger_new, gensym("pdp_t"), A_GIMME, 0);
    
    class_addmethod(pdp_trigger_class, (t_method)pdp_trigger_input_pdp, gensym("pdp"),  A_SYMBOL, A_DEFFLOAT, A_NULL);
    class_addmethod(pdp_trigger_class, (t_method)pdp_trigger_input_dpd, gensym("dpd"),  A_SYMBOL, A_DEFFLOAT, A_NULL);


}

#ifdef __cplusplus
}
#endif