aboutsummaryrefslogtreecommitdiff
path: root/toxy/plustot.var.c
blob: a5f36a73d16622ae1760d7a6068ae3b10a7e84bb (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
/* Copyright (c) 2003 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 "m_pd.h"
#include "common/loud.h"
#include "toxy/plusbob.h"
#include "plustot.h"

typedef struct _plusproxy_var
{
    t_pd        pp_pd;
    t_plusvar  *pp_var;
} t_plusproxy_var;

typedef struct _plustot_var
{
    t_object          x_ob;
    t_glist          *x_glist;
    t_plusvar        *x_var;
    t_plusproxy_var  *x_proxy;
} t_plustot_var;

static t_class *plusproxy_var_class;
static t_class *plustot_var_class;

static t_plusproxy_var *plusproxy_var_new(t_pd *master)
{
    t_plusproxy_var *pp = (t_plusproxy_var *)pd_new(plusproxy_var_class);
    pp->pp_var = ((t_plustot_var *)master)->x_var;
    return (pp);
}

static void plusproxy_var_float(t_plusproxy_var *pp, t_float f)
{
    plusvar_setfloat(pp->pp_var, f, 1);
}

static void plusproxy_var_symbol(t_plusproxy_var *pp, t_symbol *s)
{
    plusvar_setsymbol(pp->pp_var, s, 1);
}

static void plusproxy_var_list(t_plusproxy_var *pp,
			       t_symbol *s, int ac, t_atom *av)
{
    plusvar_setlist(pp->pp_var, ac, av, 1);
}

static void plustot_var_bang(t_plustot_var *x)
{
    if (plusvar_pull(x->x_var))
	outlet_plusbob(((t_object *)x)->ob_outlet, (t_plusbob *)x->x_var);
}

static void plustot_var_float(t_plustot_var *x, t_float f)
{
    if (plusvar_setfloat(x->x_var, f, 1))
	outlet_plusbob(((t_object *)x)->ob_outlet, (t_plusbob *)x->x_var);
}

static void plustot_var_symbol(t_plustot_var *x, t_symbol *s)
{
    if (plusvar_setsymbol(x->x_var, s, 1))
	outlet_plusbob(((t_object *)x)->ob_outlet, (t_plusbob *)x->x_var);
}

static void plustot_var_list(t_plustot_var *x, t_symbol *s, int ac, t_atom *av)
{
    if (plusvar_setlist(x->x_var, ac, av, 1))
	outlet_plusbob(((t_object *)x)->ob_outlet, (t_plusbob *)x->x_var);
}

static void plustot_var_free(t_plustot_var *x)
{
    plusbob_release((t_plusbob *)x->x_var);
    if (x->x_proxy) pd_free((t_pd *)x->x_proxy);
}

void *plustot_var_new(t_symbol *s, int ac, t_atom *av)
{
    t_plustot_var *x = 0;
    t_glist *glist = canvas_getcurrent();
    t_plustin *tin = 0;
    t_plusvar *var = 0;
    if (ac && av->a_type == A_SYMBOL &&
	(tin = plustin_glistprovide(glist, PLUSTIN_GLIST_ANY, 0)) &&
	(var = plusvar_new(av->a_w.w_symbol->s_name, 0, tin)))
    {
	x = (t_plustot_var *)pd_new(plustot_var_class);
	plusbob_preserve((t_plusbob *)var);
	plusbob_setowner((t_plusbob *)var, (t_pd *)x);
	plusvar_setlist(var, ac - 1, av + 1, 1);
	x->x_glist = glist;
	x->x_var = var;
	x->x_proxy = plusproxy_var_new((t_pd *)x);
	inlet_new((t_object *)x, (t_pd *)x->x_proxy, 0, 0);
	outlet_new((t_object *)x, &s_symbol);
    }
    else
    {
	if (!ac || av->a_type != A_SYMBOL)
	    loud_error(0, "+var: missing name of a variable");
	else
	    loud_error(0, "+var: cannot initialize");
	if (tin)
	{
	    plusbob_preserve((t_plusbob *)tin);
	    plusbob_release((t_plusbob *)tin);
	}
    }
    return (x);
}

void plustot_var_setup(void)
{
    plustot_var_class = class_new(gensym("+var"), 0,
				  (t_method)plustot_var_free,
				  sizeof(t_plustot_var), 0, 0);
    class_addbang(plustot_var_class, plustot_var_bang);
    class_addfloat(plustot_var_class, plustot_var_float);
    class_addsymbol(plustot_var_class, plustot_var_symbol);
    class_addlist(plustot_var_class, plustot_var_list);

    plusproxy_var_class = class_new(gensym("+var proxy"), 0, 0,
				    sizeof(t_plusproxy_var), CLASS_PD, 0);
    class_addfloat(plusproxy_var_class, plusproxy_var_float);
    class_addsymbol(plusproxy_var_class, plusproxy_var_symbol);
    class_addlist(plusproxy_var_class, plusproxy_var_list);
}