aboutsummaryrefslogtreecommitdiff
path: root/dist.c
blob: e98eebbf240d0afa733214016879406cbf48c496 (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
/* --------------------------  dist  ------------------------------------------ */
/*                                                                              */
/* Distributes incoming data to a changeable list of receive objects.           */
/* Written by Olaf Matthes (olaf.matthes@gmx.de)                                */
/* Get source at http://www.akustische-kunst.org/puredata/maxlib/               */
/*                                                                              */
/* 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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.  */
/*                                                                              */
/* Based on PureData by Miller Puckette and others.                             */
/*                                                                              */
/* ---------------------------------------------------------------------------- */
/*
   connect <symbol list>: store receive objects in list of receivers
   disconnect <symbol list>: remove objects from list of receivers
   clear: clear list of receivers
   send <anything>: send anything to all receives named in the list of receivers  */

#include "m_pd.h"

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

#define MAX_REC 64		            /* maximum number of receive objects */
#define MAX_ARG 32		            /* maximum number of arguments to pass on */

static char *version = "dist v0.1, written by Olaf Matthes <olaf.matthes@gmx.de>";

static t_class *dist_class;

typedef struct _dist
{
    t_object x_obj;
    t_symbol *x_sym[MAX_REC];       /* names of receiving objects */
	t_int    x_rec;                 /* current number of receiving objects */
	t_int    x_verbose;             /* set to 0 to turn off detailed output in Pd window */
} t_dist;

static void dist_bang(t_dist *x)
{
	int i;

	for(i = 0; i <= x->x_rec; i++)
	{
		if (x->x_sym[i]->s_thing) pd_bang(x->x_sym[i]->s_thing);
	}
}

static void dist_float(t_dist *x, t_float f)
{
	int i;

	for(i = 0; i <= x->x_rec; i++)
	{
		if (x->x_sym[i]->s_thing) pd_float(x->x_sym[i]->s_thing, f);
	}
}

static void dist_symbol(t_dist *x, t_symbol *s)
{
	int i;

	for(i = 0; i <= x->x_rec; i++)
	{
		if (x->x_sym[i]->s_thing) pd_symbol(x->x_sym[i]->s_thing, s);
	}
}

static void dist_pointer(t_dist *x, t_gpointer *gp)
{
	int i;

	for(i = 0; i <= x->x_rec; i++)
	{
		if (x->x_sym[i]->s_thing) pd_pointer(x->x_sym[i]->s_thing, gp);
	}
}

static void dist_list(t_dist *x, t_symbol *s, int argc, t_atom *argv)
{
	int i;

	for(i = 0; i <= x->x_rec; i++)
	{
		if (x->x_sym[i]->s_thing) pd_list(x->x_sym[i]->s_thing, s, argc, argv);
	}
}

static void dist_anything(t_dist *x, t_symbol *s, int argc, t_atom *argv)
{
	int i;

	for(i = 0; i <= x->x_rec; i++)
	{
		if (x->x_sym[i]->s_thing) typedmess(x->x_sym[i]->s_thing, s, argc, argv);
	}
}

	/* send 'anything' to receiver */
static void dist_send(t_dist *x, t_symbol *s, int argc, t_atom *argv)
{
	int i;
	t_atom av[MAX_ARG];		/* the 'new' t_atom without first element */
	t_int ac = argc - 1;    /* the 'new' number of arguments */

	if(ac > MAX_ARG)
	{
		post("dist: too many arguments!");
		return;
	}

	for(i = 1; i < argc; i++)
	{
		av[i - 1] = argv[i];	/* just copy, don't care about types */
	}
		/* send only argument-part to receivers */
	for(i = 0; i <= x->x_rec; i++)
	{
		if (x->x_sym[i]->s_thing) pd_forwardmess(x->x_sym[i]->s_thing, argc, argv);
	}
}

static void dist_connect(t_dist *x, t_symbol *s, int argc, t_atom *argv)
{
	int i, j;
	int exist;
	t_symbol *name;
		/* just append every new receive-name to end of list */
	for(i = 0; i < argc; i++)
	{
		exist = 0;
		if(x->x_rec == MAX_REC - 1)
		{
			post("dist: too many connections in use!");
			return;
		}
		name = atom_getsymbolarg(i, argc, argv);
		for(j = 0; j <= x->x_rec; j++)
		{	
				/* check if the name already exists */
			if(x->x_sym[j] == name)
			{
				post("dist: \"%s\" already exists in list of receivers", name->s_name);
				exist = 1;	/* indicate that it _does_ exist */
			}
		}	
			/* add it in case it's a new one */
		if(!exist)
		{
			x->x_rec++;
			x->x_sym[x->x_rec] = name;
			if(x->x_verbose)post("dist: \"%s\" added to list of receivers", x->x_sym[x->x_rec]->s_name);
		}
	}
}

static void dist_disconnect(t_dist *x, t_symbol *s, int argc, t_atom *argv)
{
		/* need to rearrange list in order to get rid of empty entries */
	int i, j, k;
	int done;
	t_symbol *name;

	for(i = 0; i < argc; i++)
	{
		name = atom_getsymbolarg(i, argc, argv);	/* the one we're going to remove */
		done = 0;                                   /* not yet removed */
		for(j = 0; j <= x->x_rec; j++)              /* search for it... */
		{
			if(x->x_sym[j] == name)
			{
				x->x_rec--;
				if(x->x_verbose)post("dist: \"%s\" removed from list of receivers", x->x_sym[j]->s_name);
				x->x_sym[j] = NULL;	/* delete entry */
					/* rearrange list now: move entries to close the gap */
				for(k = j; k <= x->x_rec; k++)
				{
					x->x_sym[k] = x->x_sym[k + 1];
				}
				done = 1;							/* removed successfully */
			}
		}
		if(!done)post("dist: \"%s\" not in list of receivers, ignored", name->s_name);
	}
}

static void dist_clear(t_dist *x)
{
	int i;

	for(i = 0; i < MAX_REC; i++)
	{
		x->x_sym[i] = NULL;
	}
	x->x_rec = -1;
}

static void dist_print(t_dist *x)
{
	int i;

	if(x->x_rec == 0)
	{
		post("dist: there is one object in receiver list:");
	} 
	else if(x->x_rec > 0)
	{
		post("dist: there are %d objects in receiver list:", x->x_rec + 1);
	} 
	else
	{
		post("dist: there are no objects in receiver list");
		return;
	}

	for(i = 0; i <= x->x_rec; i++)
	{
		post("      \"%s\"", x->x_sym[i]->s_name);
	}
}

static void *dist_new(t_symbol *s, int argc, t_atom *argv)
{
	int i;

    t_dist *x = (t_dist *)pd_new(dist_class);

	x->x_rec = -1;
	x->x_verbose = 1;	/* display info on connect/disconnect */
	for(i = 0; i < argc; i++)
	{
		x->x_sym[i] = atom_getsymbolarg(i, argc, argv);
		x->x_rec++;
	}
    return (x);
}

#ifndef MAXLIB
void dist_setup(void)
{
    dist_class = class_new(gensym("dist"), (t_newmethod)dist_new, 0,
    	sizeof(t_dist), 0, A_GIMME, 0);
#else
void maxlib_dist_setup(void)
{
    dist_class = class_new(gensym("maxlib_dist"), (t_newmethod)dist_new, 0,
    	sizeof(t_dist), 0, A_GIMME, 0);
#endif
    class_addcreator((t_newmethod)dist_new, gensym("d"), A_GIMME, 0);
    class_addbang(dist_class, dist_bang);
    class_addfloat(dist_class, dist_float);
    class_addsymbol(dist_class, dist_symbol);
    class_addpointer(dist_class, dist_pointer);
    class_addlist(dist_class, dist_list);
	class_addmethod(dist_class, (t_method)dist_connect, gensym("connect"), A_GIMME, 0);
	class_addmethod(dist_class, (t_method)dist_disconnect, gensym("disconnect"), A_GIMME, 0);
	class_addmethod(dist_class, (t_method)dist_clear, gensym("clear"), 0);
	class_addmethod(dist_class, (t_method)dist_print, gensym("print"), 0);
	class_addmethod(dist_class, (t_method)dist_send, gensym("send"), A_GIMME, 0);
    class_addanything(dist_class, dist_anything);
#ifndef MAXLIB
	
    logpost(NULL, 4, version);
#else
	class_addcreator((t_newmethod)dist_new, gensym("dist"), A_GIMME, 0);
	class_sethelpsymbol(dist_class, gensym("maxlib/dist-help.pd"));
#endif
}