1 /*
2     TiMidity++ -- MIDI to WAVE converter and player
3     Copyright (C) 1999-2002 Masanao Izumo <mo@goice.co.jp>
4     Copyright (C) 1995 Tuukka Toivonen <tt@cgs.fi>
5 
6     This program is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10 
11     This program is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15 
16     You should have received a copy of the GNU General Public License
17     along with this program; if not, write to the Free Software
18     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19 
20     motif_pipe.c: written by Vincent Pagel (pagel@loria.fr) 10/4/95
21 
22     pipe communication between motif interface and sound generator
23 
24     Copied from the motif_p source. - Glenn Trigg, 29 Oct 1998
25 
26     */
27 
28 #ifdef HAVE_CONFIG_H
29 #include "config.h"
30 #endif /* HAVE_CONFIG_H*/
31 
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <unistd.h>
35 #include <stdarg.h>
36 #include <errno.h>
37 #include <sys/ioctl.h>
38 #ifdef SOLARIS
39 #include <sys/filio.h>
40 #endif
41 #ifndef NO_STRING_H
42 #include <string.h>
43 #else
44 #include <strings.h>
45 #endif
46 #include <sys/time.h>
47 #include <sys/types.h>
48 
49 
50 #include "timidity.h"
51 #include "controls.h"
52 #include "gtk_h.h"
53 
54 int pipeAppli[2],pipeGtk[2]; /* Pipe for communication with Gtk+ process   */
55 int fpip_in, fpip_out;	/* in and out depends in which process we are */
56 int pid;	               /* Pid for child process */
57 
58 /* DATA VALIDITY CHECK */
59 #define INT_CODE 214
60 #define STRING_CODE 216
61 
62 #define DEBUGPIPE
63 
64 /***********************************************************************/
65 /* PIPE COMUNICATION                                                   */
66 /***********************************************************************/
67 
68 static void
pipe_error(char * st)69 pipe_error(char *st)
70 {
71     fprintf(stderr,"CONNECTION PROBLEM WITH Gtk+ PROCESS IN %s BECAUSE:%s\n",
72 	    st,
73 	    strerror(errno));
74     exit(1);
75 }
76 
77 
78 /*****************************************
79  *              INT                      *
80  *****************************************/
81 
82 void
gtk_pipe_int_write(int c)83 gtk_pipe_int_write(int c)
84 {
85     int len;
86     int code=INT_CODE;
87 
88 #ifdef DEBUGPIPE
89     len = write(fpip_out,&code,sizeof(code));
90     if (len!=sizeof(code))
91 	pipe_error("PIPE_INT_WRITE");
92 #endif
93 
94     len = write(fpip_out,&c,sizeof(c));
95     if (len!=sizeof(int))
96 	pipe_error("PIPE_INT_WRITE");
97 }
98 
99 void
gtk_pipe_int_read(int * c)100 gtk_pipe_int_read(int *c)
101 {
102     int len;
103 
104 #ifdef DEBUGPIPE
105     int code;
106 
107     len = read(fpip_in,&code,sizeof(code));
108     if (len!=sizeof(code))
109 	pipe_error("PIPE_INT_READ");
110     if (code!=INT_CODE)
111 	fprintf(stderr,"BUG ALERT ON INT PIPE %i\n",code);
112 #endif
113 
114     len = read(fpip_in,c, sizeof(int));
115     if (len!=sizeof(int)) pipe_error("PIPE_INT_READ");
116 }
117 
118 
119 
120 /*****************************************
121  *              STRINGS                  *
122  *****************************************/
123 
124 void
gtk_pipe_string_write(char * str)125 gtk_pipe_string_write(char *str)
126 {
127    int len, slen;
128 
129 #ifdef DEBUGPIPE
130    int code=STRING_CODE;
131 
132    len = write(fpip_out,&code,sizeof(code));
133    if (len!=sizeof(code))	pipe_error("PIPE_STRING_WRITE");
134 #endif
135 
136    slen=strlen(str);
137    len = write(fpip_out,&slen,sizeof(slen));
138    if (len!=sizeof(slen)) pipe_error("PIPE_STRING_WRITE");
139 
140    len = write(fpip_out,str,slen);
141    if (len!=slen) pipe_error("PIPE_STRING_WRITE on string part");
142 }
143 
144 void
gtk_pipe_string_read(char * str)145 gtk_pipe_string_read(char *str)
146 {
147     int len, slen;
148 
149 #ifdef DEBUGPIPE
150     int code;
151 
152     len = read(fpip_in,&code,sizeof(code));
153     if (len!=sizeof(code)) pipe_error("PIPE_STRING_READ");
154     if (code!=STRING_CODE) fprintf(stderr,"BUG ALERT ON STRING PIPE %i\n",code);
155 #endif
156 
157     len = read(fpip_in,&slen,sizeof(slen));
158     if (len!=sizeof(slen)) pipe_error("PIPE_STRING_READ");
159 
160     len = read(fpip_in,str,slen);
161     if (len!=slen) pipe_error("PIPE_STRING_READ on string part");
162     str[slen]='\0';		/* Append a terminal 0 */
163 }
164 
165 int
gtk_pipe_read_ready(void)166 gtk_pipe_read_ready(void)
167 {
168     fd_set fds;
169     int cnt;
170     struct timeval timeout;
171 
172     FD_ZERO(&fds);
173     FD_SET(fpip_in, &fds);
174     timeout.tv_sec = timeout.tv_usec = 0;
175     if((cnt = select(fpip_in + 1, &fds, NULL, NULL, &timeout)) < 0)
176     {
177 	perror("select");
178 	return -1;
179     }
180 
181     return cnt > 0 && FD_ISSET(fpip_in, &fds) != 0;
182 }
183 
184 void
gtk_pipe_open(void)185 gtk_pipe_open(void)
186 {
187     int res;
188 
189     res=pipe(pipeAppli);
190     if (res!=0) pipe_error("PIPE_APPLI CREATION");
191 
192     res=pipe(pipeGtk);
193     if (res!=0) pipe_error("PIPE_GTK CREATION");
194 
195     if ((pid=fork())==0) {   /*child*/
196 	close(pipeGtk[1]);
197 	close(pipeAppli[0]);
198 
199 	fpip_in=pipeGtk[0];
200 	fpip_out= pipeAppli[1];
201 
202 	Launch_Gtk_Process(fpip_in);
203 	/* Won't come back from here */
204 	fprintf(stderr,"WARNING: come back from Gtk+\n");
205 	exit(0);
206     }
207 
208     close(pipeGtk[0]);
209     close(pipeAppli[1]);
210 
211     fpip_in= pipeAppli[0];
212     fpip_out= pipeGtk[1];
213 }
214