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

#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#include "m_pd.h"
#include "fitter.h"

#ifdef KRZYSZCZ
# include "loud.h"
# define FITTER_DEBUG
#else
# define loudbug_bug(msg)  fprintf(stderr, "BUG: %s\n", msg), bug(msg)
#endif

/* FIXME compatibility mode should be a standard Pd feature.  When it is,
   it will be possible to simplify the implementation.  Until then,
   we have to handle multiple copies of the 'fittermode_value' variable
   (coming from different externals), and the only way is multicasting
   through a symbol (#compatibility). */
static t_symbol *fittermode_value = 0;

typedef struct _fittermode_client
{
    t_class                    *fc_owner;
    t_symbol                  **fc_mirror;
    t_fittermode_callback       fc_callback;
    struct _fittermode_client  *fc_next;
} t_fittermode_client;

static t_fittermode_client *fittermode_clients = 0;
static t_class *fittermode_class = 0;
static t_pd *fittermode_target = 0;
static int fittermode_ready = 0;
static t_symbol *fitterps_hashcompatibility = 0;
static t_symbol *fitterps_max = 0;

/* read access (query), only from fittermode_dosetup() */
static void fittermode_bang(t_pd *x)
{
    if (fitterps_hashcompatibility)
    {
	if (fittermode_ready  /* do not reply to own request */
	    && fitterps_hashcompatibility->s_thing)
	    /* this proliferates for the third and subsequent
	       fittermode_dosetup() calls... */
	    pd_symbol(fitterps_hashcompatibility->s_thing,
		      fittermode_value);
    }
    else loudbug_bug("fittermode_bang");
}

/* read access (reply), only from fitter_dosetup() */
static void fittermode_symbol(t_pd *x, t_symbol *s)
{
    fittermode_value = s;
}

/* write access, only from fitter_setmode() */
static void fittermode_set(t_pd *x, t_symbol *s)
{
    t_fittermode_client *fc;
    fittermode_value = s;
    for (fc = fittermode_clients; fc; fc = fc->fc_next)
    {
	if (fc->fc_mirror)
	    *fc->fc_mirror = s;
	if (fc->fc_callback)
	    fc->fc_callback(s);
    }
}

static void fittermode_dosetup(int noquery)
{
    if (fittermode_class || fittermode_target)
	loudbug_bug("fittermode_dosetup");
    fitterps_hashcompatibility = gensym("#compatibility");
    fitterps_max = gensym("max");
    fittermode_class = class_new(fitterps_hashcompatibility,
					  0, 0, sizeof(t_pd),
					  CLASS_PD | CLASS_NOINLET, 0);
    class_addbang(fittermode_class, fittermode_bang);
    class_addsymbol(fittermode_class, fittermode_symbol);
    class_addmethod(fittermode_class,
		    (t_method)fittermode_set,
		    gensym("set"), A_SYMBOL, 0);
    fittermode_target = pd_new(fittermode_class);
    pd_bind(fittermode_target, fitterps_hashcompatibility);
    if (!noquery)
	pd_bang(fitterps_hashcompatibility->s_thing);
    fittermode_ready = 1;
}

void fitter_setup(t_class *owner, t_symbol **mirror,
		  t_fittermode_callback callback)
{
    if (!fittermode_class)
	fittermode_dosetup(0);
    if (mirror || callback)
    {
	t_fittermode_client *fc = getbytes(sizeof(*fc));
	fc->fc_owner = owner;
	fc->fc_mirror = mirror;
	fc->fc_callback = callback;
	fc->fc_next = fittermode_clients;
	fittermode_clients = fc;
	if (mirror)
	    *mirror = fittermode_value;
    }
}

void fitter_drop(t_class *owner)
{
    if (fittermode_class && fitterps_hashcompatibility->s_thing)
    {
	t_fittermode_client *fcp = 0,
	    *fc = fittermode_clients;
	while (fc)
	{
	    if (fc->fc_owner == owner)
	    {
		if (fcp)
		    fcp->fc_next = fc->fc_next;
		else
		    fittermode_clients = fc->fc_next;
		break;
	    }
	    fcp = fc;
	    fc = fc->fc_next;
	}
	if (fc)
	    freebytes(fc, sizeof(*fc));
	else
	    loudbug_bug("fitter_drop 1");
    }
    else loudbug_bug("fitter_drop 2");
}

void fitter_setmode(t_symbol *s)
{
    post("setting compatibility mode to '%s'", (s ? s->s_name : "none"));
    if (!fittermode_class)
	fittermode_dosetup(1);
    if (fitterps_hashcompatibility->s_thing)
    {
	t_atom at;
	SETSYMBOL(&at, s);
	typedmess(fitterps_hashcompatibility->s_thing, gensym("set"), 1, &at);
    }
    else loudbug_bug("fitter_setmode");
}

t_symbol *fitter_getmode(void)
{
    if (!fittermode_class)
	fittermode_dosetup(0);
    return (fittermode_value);
}

void fittermax_set(void)
{
    if (!fittermode_class)
	fittermode_dosetup(0);
    fitter_setmode(fitterps_max);
}

int fittermax_get(void)
{
    if (!fittermode_class)
	fittermode_dosetup(0);
    return (fittermode_value == fitterps_max);
}

void fittermax_warning(t_class *c, char *fmt, ...)
{
    if (!fittermode_class)
	fittermode_dosetup(0);
    if (fittermode_value == fitterps_max)
    {
	char buf[MAXPDSTRING];
	va_list ap;
	va_start(ap, fmt);
	vsprintf(buf, fmt, ap);
	post("'%s' class incompatibility warning:\n\t%s",
	     class_getname(c), buf);
	va_end(ap);
    }
}

void fittermax_rangewarning(t_class *c, int maxmax, char *what)
{
    fittermax_warning(c, "more than %d %s requested", maxmax, what);
}