summaryrefslogtreecommitdiff
path: root/apps/plugins/pdbox/PDa/src/m_fixed.c
blob: 7beaa8077a69b6829e3350c45cb9dc0dc2a00f45 (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

#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <netdb.h>
#include <stdio.h>

#include "m_pd.h"
#include "m_imp.h"

static t_class *ipod_class = 0;

typedef struct _ipod
{
    t_object x_obj;
    t_symbol* x_what;
} t_ipod;

static t_ipod* ipod;
static  t_int x_fd = -1;



static void ipod_connect()
{
    struct sockaddr_in server;
    struct hostent *hp;
    int sockfd;
    int portno = 3334;
    char hostname[] = "127.0.0.1";
    int intarg;
    if (x_fd >= 0)
    {
    	error("ipod_connect: already connected");
    	return;
    }

    	/* create a socket */
    sockfd = socket(AF_INET, SOCK_DGRAM, 0);

    if (sockfd < 0)
    {
    	sys_sockerror("socket");
    	return;
    }

    /* connect socket using hostname provided in command line */

    server.sin_family = AF_INET;
    hp = gethostbyname(hostname);
    if (hp == 0)
    {
	post("bad host?\n");
	return;
    }

    memcpy((char *)&server.sin_addr, (char *)hp->h_addr, hp->h_length);

    server.sin_port = htons((u_short)portno);
    if (connect(sockfd, (struct sockaddr *) &server, sizeof (server)) < 0)
    {
    	sys_sockerror("connecting stream socket");
    	sys_closesocket(sockfd);
    	return;
    }
    post("connected %s %d",hostname,portno);
    x_fd = sockfd;
}



static void ipod_bang(t_ipod *x)
{
    static char sendme[200];
    sprintf(sendme,"%s bang;\n",x->x_what->s_name);
    send(x_fd,sendme,strlen(sendme),0);

//    if (x->x_sym->s_thing) pd_bang(x->x_sym->s_thing);
}

static void ipod_float(t_ipod *x, t_float f)
{
    static char sendme[200];

    sprintf(sendme,"%s %f;\n",x->x_what->s_name,f);
    send(x_fd,sendme,strlen(sendme),0);

//    post("forwarding float %s",x->x_what->s_name);
//    if (x->x_sym->s_thing) pd_float(x->x_sym->s_thing, f);
}

static void *ipod_new(t_symbol* what)
{
    t_ipod *x = (t_ipod *)pd_new(ipod_class);
    post("new ipod %s",what->s_name);
    x->x_what = what;
    return (x);
}

static void ipod_setup(void)
{
    ipod_class = class_new(gensym("ipod"), (t_newmethod)ipod_new, 0,
    	sizeof(t_ipod), 0, A_DEFSYM, 0);
    class_addbang(ipod_class, ipod_bang);
    class_addfloat(ipod_class, ipod_float);
    ipod_connect();
}

void pd_checkgui(t_pd *x, t_symbol *s)
{
    if (!strncmp(s->s_name,"pod_",4))
        if (!strcmp((*x)->c_name->s_name,"gatom") ||
            !strcmp((*x)->c_name->s_name,"vsl") ||
            !strcmp((*x)->c_name->s_name,"hsl") ||
            !strcmp((*x)->c_name->s_name,"bng") ||
            !strcmp((*x)->c_name->s_name,"vradio") ||
            !strcmp((*x)->c_name->s_name,"hradio")) {
            
            post("binding %s to %s",s->s_name,(*x)->c_name->s_name);
            if (!ipod_class) ipod_setup();
            ipod = ipod_new(s);
            pd_bind(&ipod->x_obj.ob_pd,s);
        }
}