1 /*
2     remote.h:
3 
4     Copyright (C) 2006 by Barry Vercoe
5 
6     This file is not yet part of Csound.
7 
8     The Csound Library is free software; you can redistribute it
9     and/or modify it under the terms of the GNU Lesser General Public
10     License as published by the Free Software Foundation; either
11     version 2.1 of the License, or (at your option) any later version.
12 
13     Csound is distributed in the hope that it will be useful,
14     but WITHOUT ANY WARRANTY; without even the implied warranty of
15     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16     GNU Lesser General Public License for more details.
17 
18     You should have received a copy of the GNU Lesser General Public
19     License along with Csound; if not, write to the Free Software
20     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
21     02110-1301 USA
22 */
23 
24 #ifndef CSOUND_REMOTE_H
25 #define CSOUND_REMOTE_H
26 
27 #ifdef HAVE_SOCKETS
28   #if defined(WIN32) && !defined(__CYGWIN__)
29     #include <winsock2.h>
30     #ifndef SHUT_RDWR
31       #define SHUT_RD   0x00
32       #define SHUT_WR   0x01
33       #define SHUT_RDWR 0x02
34     #endif
35   #else
36     #include <sys/ioctl.h>
37     #ifdef __HAIKU__
38       #include <sys/sockio.h>
39     #endif
40     #include <sys/socket.h>
41     #include <netinet/in.h>
42     #ifdef MACOSX
43       #include <net/if.h>
44     #endif
45     #ifdef __FreeBSD__
46       #include <net/if.h>
47     #endif
48     #ifdef linux
49       #include <linux/if.h>
50     #endif
51     #include <arpa/inet.h>
52     #ifdef HAVE_UNISTD_H
53     #  include <unistd.h>
54     #endif
55   #endif
56 #endif /* HAVE_SOCKETS */
57 
58 #include <stdlib.h>
59 #include <stdio.h>
60 #ifdef HAVE_SYS_TYPES_H
61 #  include <sys/types.h>
62 #endif
63 #include <string.h>
64 #include <errno.h>
65 
66 
67 void m_chanmsg(CSOUND *csound, MEVENT *mep);   /* called from midirecv & musmon */
68 char remoteID(CSOUND *csound);
69 
70 #define REMOT_PORT 40002
71 
72 #define SCOR_EVT 1
73 #define MIDI_EVT 2
74 #define MIDI_MSG 3
75 #define MAXSEND (sizeof(EVTBLK) + 2*sizeof(int))
76 #define GLOBAL_REMOT -99
77 
78 typedef struct {                        /* Remote Communication buffer          */
79     int         len;                    /* lentot = len + type + data used      */
80     int         type;
81     char        data[MAXSEND];
82 } REMOT_BUF;
83 
84 #ifdef HAVE_SOCKETS
85 
86 
87 typedef struct {
88     char *adr;
89     int   rfd;
90 } SOCK;
91 
92 typedef struct {
93   SOCK *socksout; /* = NULL; */
94   int *socksin; /* = NULL; */
95   int *insrfd_list; /* = NULL; */
96   int *chnrfd_list; /* = NULL; */
97   int insrfd_count; /* = 0; */
98   int chnrfd_count; /* = 0; */
99   int  *insrfd; /* = NULL; */
100   int  *chnrfd; /* = NULL; */
101   char *ipadrs; /* = NULL; */
102   struct sockaddr_in to_addr;
103   struct sockaddr_in local_addr;
104   REMOT_BUF CLsendbuf;          /* rt evt output Communications buffer */
105   int   remote_port;            /* = 40002 default */
106 } REMOTE_GLOBALS;
107 
108 #endif /* HAVE_SOCKETS */
109 
110 typedef struct {                        /* structs for INSTR 0 opcodes */
111     OPDS    h;
112     MYFLT   *port;
113 } REMOTEPORT;
114 
115 typedef struct {                        /* structs for INSTR 0 opcodes */
116     OPDS    h;
117    STRINGDAT  *str1, *str2;
118    MYFLT *insno[64];
119 } INSREMOT;
120 
121 typedef struct {                                /* structs for INSTR 0 opcodes */
122     OPDS    h;
123     STRINGDAT *str1;
124     MYFLT  *insno[64];
125 } INSGLOBAL;
126 
127 typedef struct {
128     OPDS    h;
129   STRINGDAT   *str1, *str2;
130   MYFLT  *chnum[16];
131 } MIDREMOT;
132 
133 typedef struct {                                /* structs for INSTR 0 opcodes */
134     OPDS    h;
135   STRINGDAT   *str1;
136   MYFLT  *chnum[16];
137 } MIDGLOBAL;
138 
139 int CLsend(CSOUND *csound, int conn, void *data, int length);
140 int SVrecv(CSOUND *csound, int conn, void *data, int length);
141 
142 /* musmon:      divert a score insno event to a remote machine */
143 int insSendevt(CSOUND *p, EVTBLK *evt, int rfd);
144 
145 /* musmon:      send an event (funcs, reverbs) to all active remote machines */
146 int insGlobevt(CSOUND *p, EVTBLK *evt);
147 
148 /* musmon:      divert a MIDI channel event to a remote machine */
149 int MIDIsendevt(CSOUND *p, MEVENT *evt, int rfd);
150 
151 /* musmon:      send a MIDI channel event (ctrlrs, reverbs) to all
152    active remote machines */
153 int MIDIGlobevt(CSOUND *p, MEVENT *evt);
154 
155 /* midirecv:    divert a MIDI channel message to a remote machine */
156 int MIDIsend_msg(CSOUND *p, MEVENT *evt, int rfd);
157 
158 /* midirecv:    send a MIDI channel message (ctrlrs, reverbs) to all
159    active remote machines */
160 int MIDIGlob_msg(CSOUND *p, MEVENT *evt);
161 
162 /* musmon: returns the active input sockets # */
163 int* getRemoteSocksIn(CSOUND *csound);
164 
165 /* musmon: determine whether an instrument accepts remove events */
166 int getRemoteInsRfd(CSOUND *csound, int insno);
167 
168 /* musmon: determine how many instruments accept remove events */
169 int getRemoteInsRfdCount(CSOUND *csound);
170 
171 /* musmon: determine whether MIDI channel accepts remove events */
172 int getRemoteChnRfd(CSOUND *csound, int chan);
173 
174 #endif      /* CSOUND_REMOTE_H */
175