aboutsummaryrefslogtreecommitdiff
path: root/src/iemlib2/init.c
blob: 8e0164d7d0591ac470d39ed37d8af735d0cd701a (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
/* 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 */

#include "m_pd.h"
#include "iemlib.h"


/* ------------------------ init ---------------------------- */
/* -------- a combination of loadbang and any --------------- */

static t_class *init_class;

typedef struct _init
{
  t_object   x_obj;
  int        x_n;
  int        x_ac;
  t_atom     *x_at;
  t_symbol   *x_sym;
  t_atomtype x_type;
} t_init;

static void init_bang(t_init *x)
{
  if(x->x_type == A_FLOAT)
    outlet_float(x->x_obj.ob_outlet, atom_getfloat(x->x_at));
  else if(x->x_type == A_SYMBOL)
    outlet_symbol(x->x_obj.ob_outlet, atom_getsymbol(x->x_at));
  else if(x->x_type == A_NULL)
    outlet_bang(x->x_obj.ob_outlet);
  else if(x->x_type == A_COMMA)
    outlet_anything(x->x_obj.ob_outlet, x->x_sym, x->x_ac, x->x_at);
  else if(x->x_type == A_GIMME)
    outlet_list(x->x_obj.ob_outlet, &s_list, x->x_ac, x->x_at);
  else if(x->x_type == A_POINTER)
    outlet_pointer(x->x_obj.ob_outlet, (t_gpointer *)x->x_at->a_w.w_gpointer);
}

static void init_loadbang(t_init *x)
{
  if(!sys_noloadbang)
    init_bang(x);
}

static void init_float(t_init *x, t_floatarg f)
{
  x->x_ac = 1;
  SETFLOAT(x->x_at, f);
  x->x_sym = &s_float;
  x->x_type = A_FLOAT;
  outlet_float(x->x_obj.ob_outlet, f);
}

static void init_symbol(t_init *x, t_symbol *s)
{
  x->x_ac = 1;
  SETSYMBOL(x->x_at, s);
  x->x_sym = &s_symbol;
  x->x_type = A_SYMBOL;
  outlet_symbol(x->x_obj.ob_outlet, s);
}

static void init_pointer(t_init *x, t_gpointer *gp)
{
  x->x_ac = 1;
  SETPOINTER(x->x_at, gp);
  x->x_sym = &s_pointer;
  x->x_type = A_POINTER;
  outlet_pointer(x->x_obj.ob_outlet, gp);
}

static void init_list(t_init *x, t_symbol *s, int ac, t_atom *av)
{
  t_atom *at;
  
  if(ac > x->x_n)
  {
    if(x->x_at)
      freebytes(x->x_at, x->x_n * sizeof(t_atom));
    x->x_n = ac;
    x->x_at = (t_atom *)getbytes(x->x_n * sizeof(t_atom));
  }
  x->x_ac = ac;
  x->x_sym = &s_list;
  at = x->x_at;
  while(ac--)
    *at++ = *av++;
  x->x_type = A_GIMME;
  outlet_list(x->x_obj.ob_outlet, &s_list, x->x_ac, x->x_at);
}

static void init_anything(t_init *x, t_symbol *s, int ac, t_atom *av)
{
  t_atom *at;
  
  if(ac > x->x_n)
  {
    if(x->x_at)
      freebytes(x->x_at, x->x_n * sizeof(t_atom));
    x->x_n = ac;
    x->x_at = (t_atom *)getbytes(x->x_n * sizeof(t_atom));
  }
  x->x_ac = ac;
  x->x_sym = s;
  at = x->x_at;
  while(ac--)
    *at++ = *av++;
  x->x_type = A_COMMA;
  outlet_anything(x->x_obj.ob_outlet, x->x_sym, x->x_ac, x->x_at);
}

static void init_free(t_init *x)
{
  if(x->x_at)
    freebytes(x->x_at, x->x_n * sizeof(t_atom));
}

static void *init_new(t_symbol *s, int ac, t_atom *av)
{
  t_init *x = (t_init *)pd_new(init_class);
  int i;
  
  x->x_type = A_NULL;
  if(!ac)
  {
    x->x_type = A_NULL;
    x->x_sym = &s_bang;
    x->x_n = 1;
    x->x_ac = 0;
    x->x_at = (t_atom *)getbytes(x->x_n * sizeof(t_atom));
  }
  else if(ac == 1)
  {
    if(IS_A_SYMBOL(av,0))
    {
      x->x_type = A_COMMA;
      x->x_sym = atom_getsymbol(av);
      x->x_n = 1;
      x->x_ac = 0;
      x->x_at = (t_atom *)getbytes(x->x_n * sizeof(t_atom));
    }
    else
    {
      if(IS_A_FLOAT(av,0))
      {
        x->x_type = A_FLOAT;
        x->x_sym = &s_float;
      }
      else if(IS_A_POINTER(av,0))
      {
        x->x_type = A_POINTER;
        x->x_sym = &s_pointer;
      }
      x->x_n = x->x_ac = 1;
      x->x_at = (t_atom *)getbytes(x->x_n * sizeof(t_atom));
      x->x_at[0] = *av;
    }
  }
  else
  {
    if(IS_A_SYMBOL(av,0))
    {
      x->x_type = A_COMMA;/*for anything*/
      x->x_sym = atom_getsymbol(av++);
      ac--;
    }
    else
    {
      x->x_type = A_GIMME;
      x->x_sym = &s_list;
    }
    x->x_n = x->x_ac = ac;
    x->x_at = (t_atom *)getbytes(x->x_n * sizeof(t_atom));
    for(i=0; i<ac; i++)
      x->x_at[i] = *av++;
  }
  outlet_new(&x->x_obj, &s_list);
  return (x);
}

void init_setup(void)
{
  init_class = class_new(gensym("init"), (t_newmethod)init_new,
    (t_method)init_free, sizeof(t_init), 0, A_GIMME, 0);
  class_addcreator((t_newmethod)init_new, gensym("ii"), A_GIMME, 0);
  class_addmethod(init_class, (t_method)init_loadbang, gensym("loadbang"), 0);
  class_addbang(init_class, (t_method)init_bang);
  class_addanything(init_class, init_anything);
  class_addlist(init_class, init_list);
  class_addpointer(init_class, init_pointer);
  class_addfloat(init_class, (t_method)init_float);
  class_addsymbol(init_class, init_symbol);
  class_sethelpsymbol(init_class, gensym("iemhelp/help-init"));
}