aboutsummaryrefslogtreecommitdiff
path: root/shared/common/vefl.c
blob: ef02bb70484f49e5f8990b7d2a87178657dbe523 (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
/* Copyright (c) 1997-2003 Miller Puckette, krzYszcz, and others.
 * For information on usage and redistribution, and for a DISCLAIMER OF ALL
 * WARRANTIES, see the file, "LICENSE.txt," in this distribution.  */

/* The simplest of garrays: vector of floats */

/* Array checking is done in three points:
   1. vefl_new(): never complains
   2. vefl_renew(): this should be called once per every message
   3. vefl_tick(): no template checking (only redraw is involved)
*/

/* LATER rethink indsp flag */

#include "m_pd.h"
#include "g_canvas.h"
#include "shared.h"
#include "unstable/fragile.h"
#include "common/loud.h"
#include "vefl.h"

#ifdef KRZYSZCZ
//#define VEFL_DEBUG
#endif
#define VEFL_VERBOSE

/* on failure *vszp is not modified */
t_float *vefl_get(t_symbol *name, int *vszp, int indsp, t_pd *complain)
{
    if (name && name != &s_)
    {
	t_garray *ap = (t_garray *)pd_findbyclass(name, garray_class);
	if (ap)
	{
	    int vsz;
	    t_float *vec;
	    if (garray_getfloatwords(ap, &vsz, &vec))
	    {
		if (indsp) garray_usedindsp(ap);
		if (vszp) *vszp = vsz;
		return (vec);
	    }
	    else loud_error(complain,  /* always complain */
			    "bad template of array '%s'", name->s_name);
	}
	else if (complain)
	    loud_error(complain, "no such array '%s'", name->s_name);
    }
    return (0);
}

static void vefl_tick(t_vefl *vp)
{
    if (vp->v_name && vp->v_name != &s_
	/* Check if an array has not been deleted
	   (e.g. during patch closing sequence). */
	&& (vp->v_garray =
	    (t_garray *)pd_findbyclass(vp->v_name, garray_class)))
    {
	vp->v_glist = fragile_garray_glist(vp->v_garray);
	garray_redraw(vp->v_garray);
    }
    vp->v_clockset = 0;
    vp->v_updtime = clock_getsystime();
}

t_vefl *vefl_placement_new(t_vefl *vp, t_symbol *name,
			   int writable, t_glist *gl, t_garray *arr)
{
    if (sizeof(t_word) != sizeof(t_float))
    {
	loudbug_bug("vefl_new: sizeof(t_word) != sizeof(t_float)");
	return (0);
    }
    if (!vp)
    {
	if (!(vp = getbytes(sizeof(*vp))))
	    return (0);
	vp->v_autoalloc = 1;
    }
    else vp->v_autoalloc = 0;
    vp->v_name = name;
    if (writable)
    {
	vp->v_updtime = clock_getsystime();
	vp->v_clock = clock_new(vp, (t_method)vefl_tick);
	vp->v_clockset = 0;
    }
    else vp->v_clock = 0;
    vp->v_glist = gl;
    vp->v_garray = arr;
    vp->v_size = 0;
    vp->v_data = 0;
    vp->v_type = &s_float;
    if (!arr && name && name != &s_)
    {
	vp->v_garray = (t_garray *)pd_findbyclass(name, garray_class);
	vp->v_glist = vp->v_garray ? fragile_garray_glist(vp->v_garray) : 0;
    }
    if (vp->v_garray
	&& !garray_getfloatwords(vp->v_garray, &vp->v_size, &vp->v_data))
    {
	vp->v_glist = 0;
	vp->v_garray = 0;
	vp->v_type = 0;  /* template mismatch */
    }
    return (vp);
}

t_vefl *vefl_new(t_symbol *name, int writable, t_glist *gl, t_garray *arr)
{
    return (vefl_placement_new(0, name, writable, gl, arr));    
}

void vefl_free(t_vefl *vp)
{
    if (vp->v_clock) clock_free(vp->v_clock);
    if (vp->v_autoalloc) freebytes(vp, sizeof(*vp));
}

/* LATER handle yonset */
int vefl_renew(t_vefl *vp, t_symbol *name, t_pd *complain)
{
    if (!name || name == &s_) name = vp->v_name;
    if (name && name != &s_)
    {
	vp->v_glist = 0;
	/* There are three possible ways: */
#if 0
	vp->v_name = 0;
#elif 1  /* , do nothing, and */
	vp->v_name = name;
#endif  /* LATER check all the cases and decide... */
	if (!(vp->v_garray = (t_garray *)pd_findbyclass(name, garray_class)))
	{
	    if (complain)
		loud_error(complain, "no such array '%s'", name->s_name);
	}
	else if (!garray_getfloatwords(vp->v_garray, &vp->v_size, &vp->v_data))
	{
	    vp->v_garray = 0;
	    loud_error(complain,  /* always complain */
		       "bad template of array '%s'", name->s_name);
	}
	else
	{
	    vp->v_glist = fragile_garray_glist(vp->v_garray);
	    vp->v_name = name;
	    return (1);
	}
    }
    return (0);
}

void vefl_redraw(t_vefl *vp, float suppresstime)
{
    if (vp->v_clock)  /* requests from readers are ignored */
    {
	if (suppresstime > 0)
	{
	    double timesince = clock_gettimesince(vp->v_updtime);
	    if (timesince > suppresstime)
	    {
		clock_unset(vp->v_clock);
		vefl_tick(vp);
	    }
	    else if (!vp->v_clockset)
	    {
		clock_delay(vp->v_clock, suppresstime - timesince);
		vp->v_clockset = 1;
	    }
	}
	else {
	    clock_unset(vp->v_clock);
	    vefl_tick(vp);
	}
    }
}

void vefl_redraw_stop(t_vefl *vp)
{
    if (vp->v_clock)  /* requests from readers are ignored */
    {
	clock_unset(vp->v_clock);
	vp->v_clockset = 0;
    }
}

/* Y-bounds flipped here */
void vefl_getbounds(t_vefl *vp, t_float *xminp, t_float *yminp,
		    t_float *xmaxp, t_float *ymaxp)
{
    t_glist *gl = vp->v_glist;
    if (gl)
    {
	*xminp = gl->gl_x1;
	*xmaxp = gl->gl_x2;
	*yminp = gl->gl_y2;
	*ymaxp = gl->gl_y1;
    }
}

/* Y-bounds flipped here */
void vefl_setbounds(t_vefl *vp, t_float xmin, t_float ymin,
		    t_float xmax, t_float ymax)
{
    vmess((t_pd *)vp->v_glist, gensym("bounds"), "ffff",
	  xmin, ymax, xmax, ymin);
}

void vefl_getrange(t_vefl *vp, t_float *yminp, t_float *ymaxp)
{
    int vsz = vp->v_size;
    t_float *vec = vp->v_data;
    if (vec && vsz)
    {
	t_float ymin = SHARED_FLT_MAX, ymax = -SHARED_FLT_MAX;
	while (vsz--)
	{
	    if (*vec > ymax)
	    {
		ymax = *vec;
		if (ymax < ymin) ymin = ymax;
	    }
	    else if (*vec < ymin) ymin = *vec;
	    vec++;
	}
	*yminp = ymin;
	*ymaxp = ymax;
    }
}