aboutsummaryrefslogtreecommitdiff
path: root/src/param.c
blob: fc8a152587476c2c85890b02cea7b4eb8155893f (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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
/*
 *      param.c
 *      
 *      Copyright 2009 Thomas O Fredericks <tom@hp>
 *      
 *      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., 51 Franklin Street, Fifth Floor, Boston,
 *      MA 02110-1301, USA.
 */

// Post a lot of debug messages.
//#define PARAMDEBUG

// Adds the root's $0 to the start of the paths.
//#define LOCAL

// Uses the symbol's s_thing to target the proper param
// Instead of searching through paramroots for a match.
// This is faster but can potentially target two params.
#define USEBINDINGS

#include "param.h"

// Global symbols
static t_symbol* s_vis;
static t_symbol* s_empty;
static t_symbol* s_clear;
static t_symbol* s_set;
static t_symbol* s_obj;
static t_symbol* s_nbx;
static t_symbol* s_bng;
static t_symbol* s_slider;
static t_symbol* s_hsl;
static t_symbol* s_knob;
static t_symbol* s_tgl;
static t_symbol* s_symbolatom;
static t_symbol* s_sym;
static t_symbol* s_text;
static t_symbol* s_cnv;


#include "paramCustom.h"
#include "paramDump.h"
#include "paramFile.h"
#include "paramPath.h"
#include "paramRoute.h"
#include "paramGui.h"

// For loadbang
extern int sys_noloadbang;

static t_class *param_class;
static t_class *param_inlet2_class;
struct _paramClass_inlet2;

typedef struct _paramClass {
  t_object                  	x_obj;
  struct _paramClass_inlet2     *inlet2;
  t_param*						param;
  int 							noloadbang;
  t_symbol*						selector;
  int							alloc;
  int 							ac;
  t_atom*						av;
  int							gac;  // gui options count
  t_atom*						gav;  // gui options
  t_outlet*						outlet;
  t_symbol*						receive;
  t_symbol*						send;
  t_symbol*						set_s;
  int							nowaitforbang;
  int							nopresets;
} t_paramClass;

typedef struct _paramClass_inlet2
{
  t_object       		 x_obj;
  t_paramClass  		*p_owner;
} t_paramClass_inlet2;



static void paramClass_bang(t_paramClass *x)
{
	outlet_anything(x->outlet, x->selector, x->ac, x->av);
	
	if(x->selector != &s_bang ) tof_send_anything_prepend(x->send,x->selector,x->ac,x->av,x->set_s );
   
}

static void paramClass_loadbang(t_paramClass *x)
{
    if (!sys_noloadbang && !x->noloadbang)
        paramClass_bang(x);
}

static void paramClass_anything(t_paramClass *x, t_symbol *s, int ac, t_atom *av)
{
 #ifdef PARAMDEBUG
  post("RECEIVING SOMETHING");
 #endif
 if ( s == &s_bang || ac == 0 ) {
		x->ac = 0;
		x->selector = s;
	} else {
		if(ac > x->alloc) {	
			x->av = resizebytes(x->av, x->alloc*sizeof(*(x->av)), 
				(10 + ac)*sizeof(*(x->av)));
			x->alloc = 10 + ac;
		}
		x->ac = ac;
		x->selector = s;
		tof_copy_atoms(av, x->av, ac);
   }
  
  if (x->nowaitforbang) paramClass_bang(x);
    
}




// SECOND INLET METHOD

static void paramClass_inlet2_anything(t_paramClass_inlet2 *p, t_symbol *s, int ac, t_atom *av)
{
  paramClass_anything(p->p_owner, s,ac,av);
}  

// DECONSTRUCTOR

static void paramClass_free(t_paramClass *x)
{
	
	if(x->inlet2) pd_free((t_pd *)x->inlet2);

    if (x->receive) pd_unbind(&x->x_obj.ob_pd, x->receive);

	if (x->param) param_unregister(x->param);
	
	freebytes(x->gav, x->gac * sizeof(*(x->gav)));
	
	freebytes(x->av, x->alloc * sizeof(*(x->av)));
    
}

// SPECIAL PARAM GET FUNCTION
static void paramClass_get(t_paramClass *x, t_symbol** s, int* ac, t_atom** av) {
	*s = x->selector;
	*ac = x->ac;
	*av = x->av;
}

// SPECIAL PARAM SAVE FUNCTION
static void paramClass_save(t_paramClass *x, t_binbuf* bb,int f) {
	
	//post("save:%i",f);
	
	
	// f = -1 for the main save file
	// f => 0 if it is a preset
	if ( f >= 0 && x->nopresets) return;
	
	//Put my data in binbuf
	if ((x->selector != &s_bang)) {
		int ac = x->ac + 2;
		t_atom *av = getbytes(ac*sizeof(*av));	
		tof_copy_atoms(x->av,av+2,x->ac);
		SETSYMBOL(av, x->param->path);
		SETSYMBOL(av+1, x->selector);
		binbuf_add(bb, ac, av);
		binbuf_addsemi(bb);
		freebytes(av, ac*sizeof(*av));
	}
}

// SPECIAL PARAM GUI FUNCTION
static void paramClass_GUI(t_paramClass *x, int* ac, t_atom** av, t_symbol** send,t_symbol** receive) {
	*ac = x->gac;
	*av = x->gav;
    *send = x->receive;
    *receive = x->send;
}

/*
// SPECIAL PARAM GUI FUNCTION
static void paramClass_GUIUpdate(t_paramClass *x) {
	if(x->selector != &s_bang ) tof_send_anything_prepend(x->send,x->selector,x->ac,x->av,x->set_s );
}
*/

// CONSTRUCTOR
static void* paramClass_new(t_symbol *s, int ac, t_atom *av)
{
	t_paramClass *x = (t_paramClass *)pd_new(param_class);
  
       
	// GET THE CURRENT CANVAS
	t_canvas* canvas=tof_get_canvas();
  
	// GET THE NAME
	t_symbol* name = param_get_name(ac,av);
  
	if (!name) return NULL;
	  
	t_symbol* path = param_get_path(canvas,name);
	t_symbol* root = tof_get_dollarzero(tof_get_root_canvas(canvas));
	  
	  
	 //FIND THE GUI OPTIONS: /g
	 int ac_temp = 0;
	 t_atom* av_temp = NULL;
	 
	 tof_find_tagged_argument('/',gensym("/g"), ac-1, av+1,&ac_temp,&av_temp);
	 x->gac = ac_temp;
	 x->gav = getbytes(x->gac * sizeof(*(x->gav)));
	 tof_copy_atoms(av_temp,x->gav,x->gac);	  
	  
	  // FIND THE NO LOADBANG TAG: /nlb
	x->noloadbang = tof_find_symbol(gensym("/nlb"), ac-1, av+1);
	  //post("nlb: %i",x->noloadbang);
	  
	  
	  
	  // FIND THE WAIT FOR BANG TAG: /wfb
	  x->nowaitforbang = !(tof_find_symbol(gensym("/wfb"), ac-1, av+1));
	    // FIND THE NO SAVE TAG: /ns
	int nosave = tof_find_symbol(gensym("/ns"), ac-1, av+1);
	  //post("ns: %i",nosave);
	  
	   // FIND THE NO PRESET TAG: /nps
	  x->nopresets = tof_find_symbol(gensym("/nps"), ac-1, av+1);
	  
	  
	  // REGISTER PARAM
	 t_paramSaveMethod paramSaveMethod = NULL;
	 t_paramGUIMethod paramGUIMethod = NULL;
	 
	 
	 //post("no save:%i",nosave);
	 
	if ( x->gac > 0 ) paramGUIMethod = (t_paramGUIMethod) paramClass_GUI;
	if ( nosave == 0 ) paramSaveMethod = (t_paramSaveMethod) paramClass_save;
		
	x->param = param_register(x,root,path, \
	(t_paramGetMethod) paramClass_get, \
	paramSaveMethod, \
	paramGUIMethod);
  
	if (!x->param) return NULL;
	 
	// FIND PARAM VALUE
	// A. In canvas' arguments
	// B. In object's arguments
	// C. Defaults to a bang

	int ac_p = 0;
	t_atom* av_p = NULL;

		  
	// A. In canvas' arguments
	int ac_c = 0;
	t_atom* av_c = NULL;

	t_canvas * before = tof_get_canvas_before_root(canvas);
	tof_get_canvas_arguments(before,&ac_c , &av_c);
	tof_find_tagged_argument('/',name, ac_c, av_c,&ac_p,&av_p);

	// B. I object's arguments
	if ( ac_p == 0  && ac > 1) {
	int ac_a = 0;
	t_atom* av_a = NULL;
	tof_find_tagged_argument('/',name, ac, av,&ac_p,&av_p);
	//tof_get_tagged_argument('/',ac,av,&start,&count);
	//if (count > 1) {
	//	ac_p = ac_a;
	//	av_p = av_a + 1;
	//}
	}
		  

	  
	  int l = strlen(path->s_name) + strlen(root->s_name) + 2;
	  char* receiver = getbytes( l * sizeof(*receiver));
	  receiver[0]='\0';
	  #ifdef LOCAL
	  strcat(receiver,root->s_name);
	  #endif
	  strcat(receiver,path->s_name);
	  x->receive = gensym(receiver);
	  strcat(receiver,"_");
	  x->send = gensym(receiver);
	  freebytes(receiver,  l * sizeof(*receiver));
	  #ifdef PARAMDEBUG
			post("receive:%s",x->receive->s_name);
			post("send:%s",x->send->s_name);
	  #endif
	  
	  
	  // BIND RECEIVER
  	  pd_bind(&x->x_obj.ob_pd, x->receive );
	  
	  
	  // Create memory space
	  t_symbol* selector;
	  param_set_selector(&selector,&ac_p,&av_p);
	  x->selector = selector;
	  x->alloc = ac_p + 10;
	  x->ac = ac_p;
	  x->av = getbytes(x->alloc * sizeof(*(x->av)));
	  tof_copy_atoms(av_p, x->av, x->ac);	  
	  
     
    
	x->set_s = gensym("set");
  
  // Set up second inlet proxy
  t_paramClass_inlet2 *p = (t_paramClass_inlet2 *)pd_new(param_inlet2_class);
  x->inlet2 = p;
  p->p_owner = x;
  
   // CREATE INLETS AND OUTLETS
      inlet_new((t_object *)x, (t_pd *)p, 0, 0);
      x->outlet = outlet_new(&x->x_obj, &s_list);
  
	//post("Selector: %s",x->selector->s_name);
  
  
  return (x);
}

static void* param_new(t_symbol *s, int argc, t_atom *argv) {
    
    //post("RUNNING COMMON NEW");
    
    t_pd* x = NULL;
    
    if ( !argc || argv[0].a_type == A_FLOAT ) {
        x = NULL;
    } else {
    
		t_symbol *s2 = argv[0].a_w.w_symbol;
    
		//post("Loading: %s",s2->s_name);
        if ( s2->s_name[0] == '/' )
			x = paramClass_new(s, argc, argv);
        else if (s2 == gensym("custom"))
            x = paramCustom_new(s, argc-1, argv+1);
        else if (s2 == gensym("dump"))
            x = paramDump_new(s, argc-1, argv+1);
         else if (s2 == gensym("file"))
            x = paramFile_new(s, argc-1, argv+1);
         else if (s2 == gensym("id") || s2 == gensym("path"))
            x = paramPath_new(s, argc-1, argv+1);
         else if (s2 == gensym("route"))
            x = paramRoute_new(s, argc-1, argv+1);
        else if (s2 == gensym("gui"))
            x = paramGui_new(s, argc-1, argv+1);
        else 
			post("Param is missing an argument. Possible values: custom, dump, file, path, route, gui or a /name.");
    }
    
    return (x);
    
}



void param_setup(void)
{
  
  // SETUP THE PARAM CLASS
  param_class = class_new(gensym("param /"),
    (t_newmethod)paramClass_new, (t_method)paramClass_free,
    sizeof(t_paramClass), 0, A_GIMME, 0);


  class_addanything(param_class, paramClass_anything);
  class_addbang(param_class, paramClass_bang);
  
  class_addmethod(param_class, (t_method)paramClass_loadbang, gensym("loadbang"), 0);
  
  param_inlet2_class = class_new(gensym("_param_inlet2"),
    0, 0, sizeof(t_paramClass_inlet2), CLASS_PD | CLASS_NOINLET, 0);
	
  class_addanything(param_inlet2_class, paramClass_inlet2_anything);
  
   class_sethelpsymbol(param_class,gensym("param"));
  
  // SETUP OTHER CLASSES
   paramCustom_setup();
   paramDump_setup();
   paramFile_setup();
   paramPath_setup();
   paramRoute_setup();
   paramGui_setup();
  
  
  // ADD THE COMMON CREATOR
  
   class_addcreator((t_newmethod)param_new, gensym("param"), A_GIMME, 0);
   class_addcreator((t_newmethod)param_new, gensym("tof/param"), A_GIMME, 0);
   
   // GENERATE THE SYMBOLS
   
   s_vis = gensym("vis");
  s_empty = gensym("empty");
  s_clear = gensym("clear");
  s_set = gensym("set");
  s_obj = gensym("obj");
  s_nbx = gensym("nbx");
  s_bng = gensym("bng");
  s_slider = gensym("slider");
  s_hsl=gensym("hsl");
  s_knob = gensym("knob");
  s_tgl = gensym("tgl");
  s_symbolatom = gensym("symbolatom");
  s_sym = gensym("sym");
  s_text = gensym("text");
  s_cnv = gensym("cnv");
   
   
  
}