aboutsummaryrefslogtreecommitdiff
path: root/src/oreceive.c
blob: 7e0986ace1cf07872382cd707f58974fe6469dd8 (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

/******************************************************
 *
 * oreceive - implementation file
 *
 * copyleft (c) IOhannes m zmölnig
 *
 *   2307:forum::für::umläute:2008
 *
 *   institute of electronic music and acoustics (iem)
 *   unsiversity of music and dramatic arts graz (kug)
 *
 ******************************************************
 *
 * license: GNU General Public License v.2
 *
 ******************************************************/


#include "m_pd.h"

#if 0
# define debug_post post
#else
# define debug_post
#endif

static t_class *oreceive_class, *oreceive_proxy_class, *oreceive_guts_class;


/* ------------------------------------------------------------- */

/* 
 * [oreceive] : ordered receive
 *
 *
 * there come the guts: our own bind/unbind mechanism that
 * includes priorities
 *
 * the plan is as follows:
 *  [oreceive] tells us, which name it wants to bind to; 
 *   it doesn't _really_ get bound to this name!
 *
 *  we have an invisible permanent object, that keeps track of 
 *  sorted (by priority) lists of the [oreceive] objects
 *
 *  this object binds itself to the requested name
 *  and when it gets called, it in turn calls all the [oreceive] objects
 *
 * that's it
 */


typedef struct _bind_element {
  t_pd   *object;
  t_float priority;
  struct _bind_element*next;
} t_bind_element;

typedef struct _bind_list {
  t_object p_obj;

  t_symbol*key;
  t_bind_element*elements;
  struct _bind_list*next;
} t_oreceive_proxy;

static t_oreceive_proxy*priority_receiver=0;

static t_oreceive_proxy*guts_find_key(t_symbol*key)
{
  t_oreceive_proxy*binding=0;

  for(binding=priority_receiver; binding; binding=binding->next) {
    if(binding->key == key)
      return binding;
  }
  /* not found */
  return 0;
}


static t_oreceive_proxy*guts_add_key(t_symbol*key)
{
  t_oreceive_proxy*bind_list=0;
  bind_list=(t_oreceive_proxy*)pd_new(oreceive_proxy_class);
  bind_list->key=key;
  bind_list->elements=0;
  bind_list->next=0;

  debug_post("binding %x to %s", bind_list, key->s_name);
  pd_bind(&bind_list->p_obj.ob_pd, key);

  t_oreceive_proxy*last=priority_receiver;

  if(last) {
    while(last->next) {
      last=last->next;
    }
    last->next = bind_list;
  } else {
    priority_receiver = bind_list;
  }

  return bind_list;
}

static void guts_add_element(t_oreceive_proxy*bind_list, t_bind_element*element)
{
  /* insert the object according to it's priority 
   * this is already the right queue
   */
  t_float priority=element->priority;
  t_bind_element*elements=bind_list->elements, *last=0;
  debug_post("priority insert of %x:%g", element, priority);

  if(!elements || elements->priority >= priority) {
    bind_list->elements = element;
    element->next = elements;
    debug_post("inserting in the beginngin");
    return;
  }


  debug_post("trying %x:%g", elements, elements->priority);
  while(elements && elements->priority < priority) {
    debug_post("skipping %x:%g to %x", elements, elements->priority, elements->next);
    last=elements;
    elements=elements->next;
  }

  debug_post("inserting after %x:%g", last,     (last    ?    (last->priority):0));
  debug_post("inserting befor %x:%g", elements, (elements?(elements->priority):0));

  element->next=elements;    
  if(last) {
    last->next = element;
  } else {
    bug("\nlast object invalid when inserting prioritized receiver\n");
  }
}


static void pd_bind_priority(t_pd*x, t_symbol*key, t_float priority) {
  t_oreceive_proxy*bind_list=0;
  t_bind_element*element=0;
  debug_post("trying to bind '%s':%g via %x", key->s_name, priority, priority_receiver);

  bind_list=guts_find_key(key);
  if(!bind_list)
    bind_list=guts_add_key(key);
  if(!bind_list)return;

  element=(t_bind_element*)getbytes(sizeof(t_bind_element));
  element->object=x;
  element->priority=priority;
  element->next=0;

  guts_add_element(bind_list, element);
}


static void pd_unbind_priority(t_pd*x, t_symbol*key) {
  t_oreceive_proxy*list=0, *last=0;
  t_bind_element*elements=0, *lastlmn=0;

  debug_post("trying to unbind '%s' from %x", key->s_name, priority_receiver);

  for(list=priority_receiver; list && list->key!=key; list=list->next){
    last=list;
  }
  if(!list)return;

  for(elements=list->elements; elements && elements->object != x; elements=elements->next) {
    lastlmn=elements;
  }

  if(elements) {
    /* found it, now remove it */
    if(lastlmn) {
      lastlmn->next=elements->next;
    } else {
      list->elements=elements->next;
    }

    elements->object=0;
    elements->priority=0;
    elements->next=0;

    freebytes(elements, sizeof(elements));
  } else {
    // not here...
  }

  if(0==list->elements) {
    // should we unbind ??
    if(last) {
      last->next=list->next;
    } else {
      priority_receiver=list->next;
    }    

    pd_unbind(&list->p_obj.ob_pd, list->key);
    list->next=0;
    list->key=0;

    pd_free(&list->p_obj.ob_pd);
  }
}

/* -------------------- oreceive ------------------------------ */

typedef struct _oreceive
{
  t_object x_obj;
  t_symbol *x_sym;
  t_float   x_priority;

  t_inlet*x_symin,*x_fltin;
} t_oreceive;

static void oreceive_anything(t_oreceive *x, t_symbol *s, int argc, t_atom *argv);

static void oreceive_proxy_anything(t_oreceive_proxy*p, t_symbol*s, int argc, t_atom*argv)
{
  t_bind_element*elements=p->elements;

  debug_post("proxy anything: %x", p);

  while(elements) {
    t_pd*o=elements->object;
    elements=elements->next;
    if(o) {
      oreceive_anything((t_oreceive*)o, s, argc, argv);
    }
  }
}

/* ------------------------------------------------------------- */

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

static void oreceive_priority(t_oreceive *x, t_float p)
{
  x->x_priority=p;
  if(x->x_sym) {
    pd_unbind_priority(&x->x_obj.ob_pd, x->x_sym);
  }
  pd_bind_priority(&x->x_obj.ob_pd, x->x_sym, x->x_priority);
}

static void oreceive_name(t_oreceive *x, t_symbol*s)
{
  if(x->x_sym) {
    pd_unbind_priority(&x->x_obj.ob_pd, x->x_sym);
  }
  x->x_sym=s;
  pd_bind_priority(&x->x_obj.ob_pd, x->x_sym, x->x_priority);
}


static void *oreceive_new(t_symbol *s, t_float priority)
{
    t_oreceive *x = (t_oreceive *)pd_new(oreceive_class);

    x->x_symin = inlet_new(&x->x_obj, &x->x_obj.ob_pd, &s_symbol, &s_symbol);
    x->x_fltin = inlet_new(&x->x_obj, &x->x_obj.ob_pd, &s_float, &s_float);

    x->x_sym = s;
    x->x_priority = priority;
    pd_bind_priority(&x->x_obj.ob_pd, s, priority);
    outlet_new(&x->x_obj, 0);
    return (x);
}

static void oreceive_free(t_oreceive *x)
{
    pd_unbind_priority(&x->x_obj.ob_pd, x->x_sym);
}

void oreceive_setup(void)
{
    oreceive_class = class_new(gensym("oreceive"), (t_newmethod)oreceive_new, 
                               (t_method)oreceive_free, sizeof(t_oreceive), CLASS_NOINLET, A_DEFSYM, A_DEFFLOAT, 0);
    class_addcreator((t_newmethod)oreceive_new, gensym("r"), A_DEFSYM, A_DEFFLOAT, 0);
    class_addsymbol(oreceive_class, oreceive_name);
    class_addmethod(oreceive_class, (t_method)oreceive_priority, &s_float, A_FLOAT, 0);

    oreceive_proxy_class = class_new(0, 0, 0, sizeof(t_oreceive_proxy), CLASS_NOINLET | CLASS_PD, 0);
    class_addanything(oreceive_proxy_class, oreceive_proxy_anything);
}