aboutsummaryrefslogtreecommitdiff
path: root/src/iemlib2/iem_receive.c
blob: 309512bb4c11cbec6fb8c091657015e430184038 (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
/* For information on usage and redistribution, and for a DISCLAIMER OF ALL
* WARRANTIES, see the file, "LICENSE.txt," in this distribution.

iemlib2 written by Thomas Musil, Copyright (c) IEM KUG Graz Austria 2000 - 2005 */

#ifdef _MSC_VER
#pragma warning( disable : 4244 )
#pragma warning( disable : 4305 )
#endif

#include "m_pd.h"
#include "iemlib.h"
#include <stdlib.h>
#include <string.h>
#include <stdio.h>

/* ------------------------ iem_receive ---------------------------- */
/* -------- like millers r, but with setable receive label --------- */

struct _iem_receive_proxy;

static t_class *iem_receive_class;
static t_class *iem_receive_proxy_class;

typedef struct _iem_receive
{
  t_object                   x_obj;
  struct _iem_receive_proxy  *x_proxy_receiver;
  t_symbol                   *x_receive_label_sym;
} t_iem_receive;

typedef struct _iem_receive_proxy
{
  t_object       p_obj;
  t_iem_receive  *p_owner;
} t_iem_receive_proxy;

static void iem_receive_clear(t_iem_receive *x, t_symbol *s, int ac, t_atom *av)
{
	t_iem_receive_proxy *p=x->x_proxy_receiver;

  if(x->x_receive_label_sym)
    pd_unbind(&p->p_obj.ob_pd, x->x_receive_label_sym);
  x->x_receive_label_sym = 0;
}

static void iem_receive_set(t_iem_receive *x, t_symbol *s, int ac, t_atom *av)
{
	t_iem_receive_proxy *p=x->x_proxy_receiver;

	if(ac > 0)
  {
    if(IS_A_SYMBOL(av,0))
		{
			if(x->x_receive_label_sym)
        pd_unbind(&p->p_obj.ob_pd, x->x_receive_label_sym);
      x->x_receive_label_sym = atom_getsymbol(av);
			pd_bind(&p->p_obj.ob_pd, x->x_receive_label_sym);
		}
    else if(IS_A_FLOAT(av,0))
    {
      char str[32];
      
			if(x->x_receive_label_sym)
        pd_unbind(&p->p_obj.ob_pd, x->x_receive_label_sym);
      sprintf(str, "%g", atom_getfloat(av));
      x->x_receive_label_sym = gensym(str);
			pd_bind(&p->p_obj.ob_pd, x->x_receive_label_sym);
    }
  }
}

/* begin of proxy methods (anything inlets) */

static void iem_receive_proxy_bang(t_iem_receive_proxy *p)
{
	t_iem_receive *x = p->p_owner;

  outlet_bang(x->x_obj.ob_outlet);
}

static void iem_receive_proxy_float(t_iem_receive_proxy *p, t_float f)
{
	t_iem_receive *x = p->p_owner;

  outlet_float(x->x_obj.ob_outlet, f);
}

static void iem_receive_proxy_symbol(t_iem_receive_proxy *p, t_symbol *s)
{
	t_iem_receive *x = p->p_owner;

  outlet_symbol(x->x_obj.ob_outlet, s);
}

static void iem_receive_proxy_pointer(t_iem_receive_proxy *p, t_gpointer *gp)
{
	t_iem_receive *x = p->p_owner;

  outlet_pointer(x->x_obj.ob_outlet, gp);
}

static void iem_receive_proxy_list(t_iem_receive_proxy *p, t_symbol *s, int argc, t_atom *argv)
{
	t_iem_receive *x = p->p_owner;

  outlet_list(x->x_obj.ob_outlet, &s_list, argc, argv);
}

static void iem_receive_proxy_anything(t_iem_receive_proxy *p, t_symbol *s, int argc, t_atom *argv)
{
	t_iem_receive *x = p->p_owner;

  outlet_anything(x->x_obj.ob_outlet, s, argc, argv);
}

/* end of proxy methods (anything inlets) */

static void iem_receive_free(t_iem_receive *x)
{
	t_iem_receive_proxy *p=x->x_proxy_receiver;

  if(x->x_receive_label_sym)
    pd_unbind(&p->p_obj.ob_pd, x->x_receive_label_sym);
	if(x->x_proxy_receiver)
    pd_free((t_pd *)x->x_proxy_receiver);
}

static void *iem_receive_new(t_symbol *s, int ac, t_atom *av)
{
  t_iem_receive *x = (t_iem_receive *)pd_new(iem_receive_class);
	t_iem_receive_proxy *p = (t_iem_receive_proxy *)pd_new(iem_receive_proxy_class);

  x->x_proxy_receiver = p;
  p->p_owner = x;
  
  if(ac > 0)
  {
    if(IS_A_SYMBOL(av,0))
    {
      x->x_receive_label_sym = atom_getsymbol(av);
      pd_bind(&p->p_obj.ob_pd, x->x_receive_label_sym);
    }
    else if(IS_A_FLOAT(av,0))
    {
      char str[100];
      
      sprintf(str, "%g", atom_getfloat(av));
      x->x_receive_label_sym = gensym(str);
      pd_bind(&p->p_obj.ob_pd, x->x_receive_label_sym);
    }
    else
      x->x_receive_label_sym = 0;
  }
  else
    x->x_receive_label_sym = 0;
  
  outlet_new(&x->x_obj, &s_list);
  return (x);
}

void iem_receive_setup(void)
{
  iem_receive_class = class_new(gensym("iem_receive"), (t_newmethod)iem_receive_new, 
    (t_method)iem_receive_free, sizeof(t_iem_receive), 0, A_GIMME, 0);
	class_addcreator((t_newmethod)iem_receive_new, gensym("iem_r"), A_GIMME, 0);
	class_addmethod(iem_receive_class, (t_method)iem_receive_clear, gensym("clear"), A_GIMME, 0);
  class_addmethod(iem_receive_class, (t_method)iem_receive_set, gensym("set"), A_GIMME, 0);
  class_sethelpsymbol(iem_receive_class, gensym("iemhelp/help-iem_receive"));

	iem_receive_proxy_class = class_new(gensym("_iem_receive_proxy"),
    0, 0, sizeof(t_iem_receive_proxy), CLASS_PD | CLASS_NOINLET, 0);
  class_addbang(iem_receive_proxy_class, iem_receive_proxy_bang);
  class_addfloat(iem_receive_proxy_class, iem_receive_proxy_float);
	class_addsymbol(iem_receive_proxy_class, iem_receive_proxy_symbol);
	class_addpointer(iem_receive_proxy_class, iem_receive_proxy_pointer);
  class_addlist(iem_receive_proxy_class, iem_receive_proxy_list);
  class_addanything(iem_receive_proxy_class, iem_receive_proxy_anything);
}