aboutsummaryrefslogtreecommitdiff
path: root/puredata/pdp_comm.c
blob: 2674e36fece4bdeba7717012a7ff83611807827a (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
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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
/*
 *   Pure Data Packet system implementation.
 *   Copyright (c) by Tom Schouten <pdp@zzz.kotnet.org>
 *
 *   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.
 *
 */

/* this file contains misc communication (packet) methods for pd */


#include <stdio.h>
#include "pdp_pd.h"
#include "pdp_internals.h"
#include "pdp_packet.h"
#include "pdp_comm.h"
#include "pdp_type.h"
#include "pdp_control.h"
#include "pdp_mem.h"
#include "pdp_queue.h" // for notify drop: fix this (should be from pdp_control.h)
#include "pdp_debug.h"

/* all symbols are C style */
#ifdef __cplusplus
extern "C"
{
#endif


/* interface to pd system:
   pdp/dpd communication protocol in pd
   pd <-> pdp atom and list conversion */
   
    /* NOTE: when using the outlet_pdp methods, the packet
       is no longer read/write, but can be readonly */


    /* NOTE: since 0.13 the passing packet is no more.
       in order to limit copying. processors should always register ro,
       and replace with writable when packet needs to be written in the process method */


/* send a packet to an outlet */
void outlet_pdp_register(t_outlet *out, int packetid)
{
    t_atom atom[2];

    SETFLOAT(atom+1, (float)packetid);

    /* during the following phase, 
       objects can register a ro copy */

    SETSYMBOL(atom+0, S_REGISTER_RO);         
    outlet_anything(out, S_PDP, 2, atom);

    /* DEPRECIATED: objects can register a rw copy
       but this will always copy the packet. it is better
       to perform a pdp_packet_replace_with_writable operation during the process step */

    SETSYMBOL(atom+0, S_REGISTER_RW);         
    outlet_anything(out, S_PDP, 2, atom);    

}
/* send a packet to an outlet */
void outlet_pdp_process(t_outlet *out)
{
    t_atom atom[2];

    /* set a dummy invalid packet.
       this is for uniform pdp messages in pd, for ease of routing. */
    SETFLOAT(atom+1, (float)-1);

    /* during the process phase, objects can perform pdp_packet_replace_with_writable
       and process the packet data */
    SETSYMBOL(atom+0, S_PROCESS);
    outlet_anything(out, S_PDP, 2, atom);

}

/* for compat */
void outlet_pdp(t_outlet *out, int packetid)
{
    outlet_pdp_register(out, packetid);
    outlet_pdp_process(out);
}

/* send an accumulation packet to an outlet */
void outlet_dpd(t_outlet *out, int packetid)
{
    t_atom atom[2];

    SETFLOAT(atom+1, (float)packetid);

    SETSYMBOL(atom+0, S_INSPECT);
    outlet_anything(out, S_DPD, 2, atom);

    SETSYMBOL(atom+0, S_ACCUMULATE);
    outlet_anything(out, S_DPD, 2, atom);

}

/* unregister a packet and send it to an outlet */
void

pdp_packet_pass_if_valid(t_outlet *outlet, int *packet_ptr)
{


    t_pdp *header = pdp_packet_header(*packet_ptr);
    if (header){


	/* send register phase */
	outlet_pdp_register(outlet, *packet_ptr);


	/* unregister */
	pdp_packet_mark_unused(*packet_ptr);
	*packet_ptr = -1;

	/* send process phase */
	outlet_pdp_process(outlet);


    }

}

void
pdp_packet_replace_if_valid(int *dpacket, int *spacket)
{
    if (-1 != *spacket){
	pdp_packet_mark_unused(*dpacket);
	*dpacket = *spacket;
	*spacket = -1;
    }
    
}


int
pdp_packet_copy_ro_or_drop(int *dpacket, int spacket)
{
    int drop = 0;
    if (*dpacket == -1) *dpacket = pdp_packet_copy_ro(spacket);
    else {
	/* send a notification there is a dropped packet */
	pdp_control_notify_drop(spacket);
	drop = 1;
    }
    return drop;
}


int
pdp_packet_copy_rw_or_drop(int *dpacket, int spacket)
{
    int drop = 0;
    if (*dpacket == -1) *dpacket = pdp_packet_copy_rw(spacket);
    else {
	/* send a notification there is a dropped packet */
	pdp_control_notify_drop(spacket);
	drop = 1;
    }
    return drop;
}

int
pdp_packet_convert_ro_or_drop(int *dpacket, int spacket, t_pdp_symbol *template)
{
    int drop = 0;

    if (!template) return pdp_packet_copy_ro_or_drop(dpacket, spacket);

    if (*dpacket == -1) *dpacket = pdp_packet_convert_ro(spacket, template);
    else {
	/* send a notification there is a dropped packet */
	pdp_control_notify_drop(spacket);
	drop = 1;
    }
    return drop;
}


int
pdp_packet_convert_rw_or_drop(int *dpacket, int spacket, t_pdp_symbol *template)
{
    int drop = 0;

    if (!template) return pdp_packet_copy_rw_or_drop(dpacket, spacket);

    if (*dpacket == -1) *dpacket = pdp_packet_convert_rw(spacket, template);
    else {
	/* send a notification there is a dropped packet */
	pdp_control_notify_drop(spacket);
	drop = 1;
    }
    return drop;
}


/* send a pdp list to a pd outlet. packets are not copied but passed! */
void outlet_pdp_atom(t_outlet *out, t_pdp_atom *a)
{
    int packet = -1;
    if (!a) return;
    switch(a->t){
    case a_float:
	outlet_float(out, a->w.w_float);
	return;
    case a_int:
	outlet_float(out, (float)a->w.w_int);
	return;
    case a_symbol:
	outlet_symbol(out, gensym(a->w.w_symbol->s_name));
	return;
    case a_list:
	outlet_pdp_list(out, a->w.w_list);
	return;
    case a_packet:
	pdp_packet_pass_if_valid(out, &a->w.w_packet);
	return;
    default:
	return;
    }
}

void outlet_pdp_list(t_outlet *out, struct _pdp_list *l)
{
    int elements;
    t_atom *atomlist;
    t_pdp_atom *pdp_a;
    t_atom *pd_a;
    t_symbol *pd_selector;
    
    if (!l) return;
    switch(l->elements){
    case 0: /* bang */
	outlet_bang(out);
	return;
    case 1: /* atom */
	outlet_pdp_atom(out, l->first);
	return;
    default: /* proper list*/
	elements = l->elements;

	/* allocate list */
	atomlist = pdp_alloc(sizeof (t_atom) * l->elements);
	pd_a = atomlist;
	pdp_a = l->first;

	/* setup selector */
	if (pdp_a->t != a_symbol){
	    pd_selector = gensym("list");
	}
	else {
	    pd_selector = gensym(pdp_a->w.w_symbol->s_name);
	    elements--;
	    pdp_a = pdp_a->next;
	}

	/* setup atoms */
	while (pdp_a){
	    switch(pdp_a->t){
	    case a_float:
		SETFLOAT(pd_a, pdp_a->w.w_float);
		break;
	    case a_int:
		SETFLOAT(pd_a, (float)pdp_a->w.w_int);
		break;
	    case a_symbol:
		SETSYMBOL(pd_a, gensym(pdp_a->w.w_symbol->s_name));
		break;
	    default:
		SETSYMBOL(pd_a, gensym("invalid"));
		break;
	    }
			  
	    pdp_a = pdp_a->next;
	    pd_a++;
	}

	/* send out */
	outlet_anything(out, pd_selector, elements, atomlist);
	    


	/* clean up */
	pdp_dealloc(atomlist);
	
    }	
	
		
}


void pd_atom_to_pdp_atom(t_atom *pdatom, t_pdp_atom *pdpatom)
{
    switch (pdatom->a_type){
    case A_FLOAT:
	pdpatom->t = a_float;
	pdpatom->w.w_float = pdatom->a_w.w_float;
	break;
    case A_SYMBOL:
	pdpatom->t = a_symbol;
	pdpatom->w.w_symbol = pdp_gensym(pdatom->a_w.w_symbol->s_name);
	break;
    default:
	pdpatom->t = a_undef;
	break;
    }
}

#if PDP_SYMBOL_HACK

/* some "accelerated" pd symbols */
t_symbol s_pdp         = {"pdp", 0, 0};
t_symbol s_register_ro = {"register_ro", 0, 0};
t_symbol s_register_rw = {"register_rw", 0, 0};
t_symbol s_process     = {"process", 0, 0};
t_symbol s_dpd         = {"dpd", 0, 0};
t_symbol s_inspect     = {"inspect", 0, 0};
t_symbol s_accumulate  = {"accumulate", 0, 0};
t_symbol s_chanmask    = {"chanmask", 0, 0};



// internal pd method
t_symbol *dogensym(char *s, t_symbol *oldsym);
static void _addsym(t_symbol *s)
{

    /* don't kill me for this one..
       if the symbol is already defined and used, .. well, that's a problem
       but right now it seems a reasonable hack */

    t_symbol *sret = dogensym(s->s_name, s);
    if (s != sret){
	post("PDP INIT ERROR: pd symbol clash adding symbol %s: new=%08x old=%08x", s->s_name, s, sret);
	post("try loading pdp before other libraries");
    }
}


void
pdp_pdsym_setup(void)
{

    _addsym(&s_pdp);
    _addsym(&s_register_ro);
    _addsym(&s_register_rw);
    _addsym(&s_process);
    _addsym(&s_dpd);
    _addsym(&s_inspect);
    _addsym(&s_accumulate);
    _addsym(&s_chanmask);

}

#else

void pdp_pdsym_setup(void){
}

#endif


#ifdef __cplusplus
}
#endif