aboutsummaryrefslogtreecommitdiff
path: root/control/unwonk.c
blob: 2c1fa9eb792c79ab684b5ec583d7954b7e48f918 (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
/* (C) Guenter Geiger <geiger@epy.co.at> */


#include <m_pd.h>


/* -------------------------- unwonk ------------------------------ */

static t_class *unwonk_class;

typedef struct unwonkout
{
    t_atomtype u_type;
    t_outlet *u_outlet;
} t_unwonkout;

typedef struct _unwonk
{
    t_object x_obj;
    t_int x_n;
    t_unwonkout *x_vec;
} t_unwonk;

static void *unwonk_new(t_symbol *s, int argc, t_atom *argv)
{
    t_unwonk *x = (t_unwonk *)pd_new(unwonk_class);
    t_atom defarg[2], *ap;
    t_unwonkout *u;
    int i;
    if (!argc)
    {
    	argv = defarg;
    	argc = 2;
    	SETFLOAT(&defarg[0], 0);
    	SETFLOAT(&defarg[1], 0);
    }

    x->x_n = argc + 1;
    x->x_vec = (t_unwonkout *)getbytes(x->x_n * sizeof(*x->x_vec));

    for (i = 0, ap = argv, u = x->x_vec; i < argc; u++, ap++, i++)
    {
    	t_atomtype type = ap->a_type;
    	if (type == A_SYMBOL)
    	{
    	    char c = *ap->a_w.w_symbol->s_name;
    	    if (c == 's')
    	    {
    	    	u->u_type = A_SYMBOL;
    	    	u->u_outlet = outlet_new(&x->x_obj, &s_symbol);
    	    }
    	    else if (c == 'p')
    	    {
    	    	u->u_type =  A_POINTER;
    	    	u->u_outlet = outlet_new(&x->x_obj, &s_pointer);
    	    }
    	    else
    	    {
    	    	if (c != 'f') error("unwonk: %s: bad type",
    	    	    ap->a_w.w_symbol->s_name);
    	    	u->u_type = A_FLOAT;
    	    	u->u_outlet = outlet_new(&x->x_obj, &s_float);
    	    }
    	}
    	else
    	{
    	    u->u_type =  A_FLOAT;
    	    u->u_outlet = outlet_new(&x->x_obj, &s_float);
    	}
    }

    u->u_type =  A_GIMME;
    u->u_outlet = outlet_new(&x->x_obj, &s_list);
    
    return (x);
}

static void unwonk_list(t_unwonk *x, t_symbol *s, int argc, t_atom *argv)
{
    t_atom *ap;
    t_unwonkout *u;
    int i;
    int margc = argc;


    if (argc > x->x_n - 1) margc = x->x_n - 1;

    if (argc - margc > 0) {
	 ap = argv + margc;
	 u = x->x_vec + margc;
	 outlet_list(u->u_outlet,0,argc - margc, ap);
    }

    for (i = margc, u = x->x_vec + i, ap = argv + i; u--, ap--, i--;)
    {
    	t_atomtype type = u->u_type;
    	if (type != ap->a_type)
    	    error("unwonk: type mismatch");
    	else if (type == A_FLOAT)
    	    outlet_float(u->u_outlet, ap->a_w.w_float);
    	else if (type == A_SYMBOL)
    	    outlet_symbol(u->u_outlet, ap->a_w.w_symbol);
    	else outlet_pointer(u->u_outlet, ap->a_w.w_gpointer);
    }
    

}

static void unwonk_free(t_unwonk *x)
{
    freebytes(x->x_vec, x->x_n * sizeof(*x->x_vec));
}

void unwonk_setup(void)
{
    unwonk_class = class_new(gensym("unwonk"), (t_newmethod)unwonk_new,
    	(t_method)unwonk_free, sizeof(t_unwonk), 0, A_GIMME, 0);
    class_addlist(unwonk_class, unwonk_list);
}