aboutsummaryrefslogtreecommitdiff
path: root/gui/state.c
blob: 40d35014e491de496fada834f5739a4cda09db53 (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
/* (C) Guenter Geiger <geiger@epy.co.at> */


#include <m_pd.h>
#include <stdio.h>
#include <string.h>
#ifdef NT
#pragma warning( disable : 4244 )
#pragma warning( disable : 4305 )
#endif



/* hacks .... this duplicates definitions in pd and must be 
 * adjusted if something changes there !!!! */

#define EMPTYSYMBOL "emptysymbol"

typedef struct _mygatom
{
    t_text a_text;
    t_atom a_atom;
} t_mygatom; /* this is not !! the real t_gatom ...*/

#define TATOM(a) (&((t_mygatom*)a)->a_atom)
#define ATOMVAL(a) a->a_w.w_float

/* glist's are not visible, but the only thing we need is to
   get the list link from them */

typedef struct _myglist
{
     t_object gl_gobj;
     t_gobj* g_list;
} t_myglist;

#define FIRSTOBJECT(a) (((t_myglist*)a)->g_list)


#ifndef vmess 
#define vmess pd_vmess
#endif


/* ------------------------ State ----------------------------- */

#include "envgen.h"


static t_class *state_class;


typedef struct _state
{
     t_object  x_obj;
     t_canvas* x_canvas;
     t_symbol* x_name;
     int       x_slot;
     t_symbol* x_symslot;
     int       x_save;
     int       x_loading;
     t_clock*  x_clock;
} t_state;




void state_dosave(t_state *x)
{
     char name[255];
     FILE* fp;
     t_text* a;
     t_symbol* dir;
     char    dirstr[255];

#ifdef NT
     dir = gensym("");
#else
     dir = canvas_getdir(x->x_canvas);
#endif

     strcpy(dirstr,dir->s_name);

#ifndef NT
     strcat(dirstr,"/");
#endif

     if (x->x_symslot)
	  sprintf(name,"%s%s.%s",dirstr,x->x_name->s_name,x->x_symslot->s_name);
     else
	  sprintf(name,"%s%s.%d",dirstr,x->x_name->s_name,x->x_slot);

     fp = fopen(name,"w");
     if (!fp) {
	  post("state: unable to open %s",name);
	  return;
     }

     a = (t_text*)FIRSTOBJECT(x->x_canvas);

     do {  
	  if (a->te_type == T_ATOM) {
	       if (TATOM(a)->a_type == A_SYMBOL) {
		    if (strlen(TATOM(a)->a_w.w_symbol->s_name))
			 fprintf(fp,"%s\n",atom_getsymbol(TATOM(a))->s_name);
		    else
			 fprintf(fp,EMPTYSYMBOL"\n");
	       }
	       else {
		    fprintf(fp,"%f\n",atom_getfloat(TATOM(a)));
	       }
	  }

	  /* slider should be an atom as well ... how to do it ? */

	  if (!strcmp(class_getname(a->te_pd),"slider")) {
		fprintf(fp,"%f\n",atom_getfloat(TATOM(a)));
	  }
	  if (!strcmp(class_getname(a->te_pd),"sliderh")) {
		fprintf(fp,"%f\n",atom_getfloat(TATOM(a)));
	  }
#if 0
	  if (!strcmp(class_getname(a->te_pd),"vsl")) {
	    /*	    float val = atom_getfloat(TATOM(a));*/
	    float val = *((float*) (((char*)a) + sizeof(t_iemgui) + sizeof(int)));
		fprintf(fp,"%f\n",val);
		fprintf(stderr,"vslider %f\n",val);
	  }
	  if (!strcmp(class_getname(a->te_pd),"hsl")) {
	    float val = *((float*) (((char*)a) + sizeof(t_iemgui) + sizeof(int)));
		fprintf(fp,"%f\n",val);
		fprintf(stderr,"hslider %f\n",val);
	  }
#endif
	  if (!strncmp(class_getname(a->te_pd),"envgen",6)) {
	       int i;
	       t_envgen* e = (t_envgen*) a;

	       fprintf(fp,"%d ",e->last_state);
	       fprintf(fp,"%f ",e->finalvalues[0]);
	       for (i=1;i <= e->last_state;i++)
		    fprintf(fp,"%f %f ",e->duration[i] - e->duration[i-1],e->finalvalues[i]);
	       fprintf(fp,"\n");
	  }



     } while ((a = (t_text*)((t_gobj*)a)->g_next));
     post("state saved to: %s",name);

     fclose(fp);

}

void state_save(t_state *x) 
{
     x->x_save = 1;
     clock_delay(x->x_clock,2000);
}


void state_saveoff(t_state *x) 
{
     x->x_save = 0;
}

void state_load(t_state *x)
{
     char name[255];
     FILE* fp;
     t_text* a;
     t_float  in;
     t_symbol* dir;
     char    dirstr[255];

#ifdef NT
     dir = gensym("");
#else
     dir = canvas_getdir(x->x_canvas);
#endif

     strcpy(dirstr,dir->s_name);

#ifndef NT
     strcat(dirstr,"/");
#endif


     if (x->x_symslot)
	  sprintf(name,"%s%s.%s",dirstr,x->x_name->s_name,x->x_symslot->s_name);
     else
	  sprintf(name,"%s%s.%d",dirstr,x->x_name->s_name,x->x_slot);

     fp = fopen(name,"r");
     if (!fp) {
	  post("state: unable to open %s",name);
	  return;
     }

     a = (t_text*) FIRSTOBJECT(x->x_canvas);

     x->x_loading = 1;
     post("state loading from: %s",name);
     *name = 0;
     do {
	  if (a->te_type == T_ATOM || 
	      !strcmp(class_getname(a->te_pd),"slider")
	/* ||
	     !strcmp(class_getname(a->te_pd),"vsl") ||
	      !strcmp(class_getname(a->te_pd),"hsl" ) */
	      ) {
	       if (TATOM(a)->a_type == A_SYMBOL) {
		    fscanf(fp,"%s",name);
		    if (strcmp(name,EMPTYSYMBOL))
			vmess((t_pd*)a,gensym("set"),"s",gensym(name));
	       }
	       else {
		    fscanf(fp,"%f",&in);
		    vmess((t_pd*)a,&s_float,"f",in);
	       }
	  }

	  if (!strncmp(class_getname(a->te_pd),"envgen",6)) {
	       int i;
	       int end;
	       float val;
	       float dur;
	       t_atom  ilist[255];

	       fscanf(fp,"%f",&in);
	       end = in;

	       fscanf(fp,"%f",&val);
	       SETFLOAT(ilist,val);
	       for (i=1 ;i <= end;i++) {
		    fscanf(fp,"%f",&dur);
		    fscanf(fp,"%f",&val);
		    SETFLOAT(ilist +2*i-1,dur);
		    SETFLOAT(ilist+2*i,val);
	       }
	       pd_typedmess((t_pd*)a,&s_list,2*end+1,ilist);
	       post("ok %d",end);
	  }


     } while ((a = (t_text*)((t_gobj*)a)->g_next) && !feof(fp));

     x->x_loading = 0;
     fclose(fp);

}

void state_float(t_state *x,t_floatarg f)
{
     if (x->x_loading) return;
     x->x_symslot = NULL;
     x->x_slot = f;
     if (x->x_save) {
	  x->x_save = 0;
	  state_dosave(x);
	  return;
     }
	  
     state_load(x);
}


void state_anything(t_state *x,t_symbol* s,t_int argc,t_atom* argv)
{
     x->x_symslot = s;
     if (x->x_save) {
	  x->x_save = 0;
	  state_dosave(x);
	  return;
     }

     state_load(x);
}



void state_bang(t_state *x)
{
     if (x->x_symslot) outlet_symbol(x->x_obj.ob_outlet,x->x_symslot);
     else
	  outlet_float(x->x_obj.ob_outlet,x->x_slot);
}


static void *state_new(t_symbol* name)
{
    t_state *x = (t_state *)pd_new(state_class);
    x->x_canvas = canvas_getcurrent();
    if (name != &s_)
	 x->x_name = name;
    else
	 x->x_name = gensym("state");

    x->x_clock = clock_new(x, (t_method)state_saveoff);
    x->x_slot = 0;
    x->x_symslot = NULL;
    x->x_loading = 0;
    x->x_save = 0;
    outlet_new(&x->x_obj, &s_float);
    return (x);
}



void state_setup(void)
{
    state_class = class_new(gensym("state"), (t_newmethod)state_new, 0,
				sizeof(t_state), 0,A_DEFSYM,0);
    class_addfloat(state_class,state_float);
    class_addmethod(state_class,(t_method) state_save, gensym("save"), 0);
    class_addanything(state_class,(t_method) state_anything);
    class_addbang(state_class,state_bang);
/*    class_addmethod(state_class, (t_method)state_load, gensym("load"), 0);*/
}