35123d4ccf2995d97aee096e23dacb6fe486f47a
[ardour.git] / gtk2_ardour / system_exec.h
1 /*
2     Copyright (C) 2010 Paul Davis
3     Author: Robin Gareus <robin@gareus.org>
4
5     This program is free software; you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation; either version 2 of the License, or
8     (at your option) any later version.
9
10     This program is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14
15     You should have received a copy of the GNU General Public License
16     along with this program; if not, write to the Free Software
17     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18
19 */
20 #ifdef WITH_VIDEOTIMELINE
21
22 #ifndef __ardour_system_exec_h__
23 #define __ardour_system_exec_h__
24
25 #ifndef STDIN_FILENO
26 #define STDIN_FILENO 0
27 #endif
28 #ifndef STDOUT_FILENO
29 #define STDOUT_FILENO 1
30 #endif
31 #ifndef STDERR_FILENO
32 #define STDERR_FILENO 2
33 #endif
34
35 #include <string>
36 #include <pthread.h>
37 #include <signal.h>
38 #ifdef NOPBD  /* outside ardour */
39 #include <sigc++/bind.h>
40 #include <sigc++/signal.h>
41 #else
42 #include <pbd/signals.h>
43 #endif
44
45 /** @class: SystemExec
46  *  @brief execute an external command
47  *
48  * This class allows launche an external command-line application
49  * opening a full-duplex connection to its standard I/O.
50  *
51  * In Ardour context it is used to launch xjadeo and ffmpeg.
52  *
53  * The \ref write_to_stdin function provides for injecting data into STDIN
54  * of the child-application while output of the program to STDOUT/STDERR is
55  * forwarded using the \ref ReadStdout signal.
56  * \ref Terminated is sent if the child application exits.
57  *
58  */
59 class SystemExec
60 {
61         public:
62                 /** prepare execution of a program with 'execve'
63                  *
64                  * This function takes over the existing environment variable and provides
65                  * an easy way to speciy command-line arguments for the new process.
66                  *
67                  * Note: The argument parser does not interpret quotation-marks and splits
68                  * arugments on whitespace. The argument string can be empty.
69                  * The alternative constructor below allows to specify quoted parameters
70                  * incl. whitespace.
71                  *
72                  * @param c program pathname that identifies the new process image file.
73                  * @param a string of commandline-arguments to be passed to the new program.
74                  */
75                 SystemExec (std::string c, std::string a = "");
76                 /** similar to \ref SystemExec but allows to specify custom arguments
77                  *
78                  * @param c program pathname that identifies the new process image file.
79                  * @param a array of argument strings passed to the new program as 'argv'.
80                  *          it must be terminated by a null pointer (see the 'evecve'
81                  *          POSIX-C documentation for more information)
82                  *          The array must be dynamically allocated using malloc or strdup.
83                  *          Unless they're NULL, the array itself and each of its content
84                  *          memory is freed() in the destructor.
85                  *
86                  */
87                 SystemExec (std::string c, char ** a);
88                 virtual ~SystemExec ();
89
90                 /** fork and execute the given program
91                  *
92                  * @param stderr_mode select what to do with program's standard error
93                  * output:
94                  * '0': keep STDERR; mix it with parent-process' STDERR
95                  * '1': ignore STDERR of child-program
96                  * '2': merge STDERR into STDOUT and send it with the
97                  *      ReadStdout signal.
98                  * @return If the process is already running or was launched successfully
99                  * the function returns zero (0). A negative number indicates an error.
100                  */
101                 int start (int stderr_mode = 1);
102                 /** kill running child-process
103                  *
104                  * if a child process exists trt to shut it down by closing its STDIN.
105                  * if the program dies not react try SIGTERM and eventually SIGKILL
106                  */
107                 void terminate ();
108                 /** check if the child programm is (still) running.
109                  *
110                  * This function calls waitpid(WNOHANG) to check the state of the
111                  * child-process.
112                  * @return true if the program is (still) running.
113                  */
114                 bool is_running ();
115                 /** call the waitpid system-call with the pid of the child-program
116                  *
117                  * Basically what \ref terminate uses internally.
118                  *
119                  * This function is only useful if you want to control application
120                  * termination yourself (eg timeouts or progress-dialog).
121                  * @param option flags - see waitpid manual
122                  * @return status info from waitpid call (not waitpid's return value)
123                  * or -1 if the child-program is not running.
124                  */
125                 int wait (int options=0);
126                 /** closes both STDIN and STDOUT connections to/from
127                  * the child-program.
128                  * With the output-interposer thread gone, the program
129                  * should terminate.
130                  * used by \ref terminate()
131                  */
132                 void close_stdin ();
133                 /** write into child-program's STDIN
134                  * @param d data to write
135                  * @param len length of data to write, if it is 0 (zero), d.length() is
136                  * used to determine the number of bytes to transmit.
137                  * @return number of bytes written.
138                  */
139                 int write_to_stdin (std::string d, size_t len=0);
140
141                 /** The ReadStdout signal is emitted when the application writes to STDOUT.
142                  * it passes the written data and its length in bytes as arguments to the bound
143                  * slot(s).
144                  */
145 #ifdef NOPBD  /* outside ardour */
146                 sigc::signal<void, std::string,size_t> ReadStdout;
147 #else
148                 PBD::Signal2<void, std::string,size_t> ReadStdout;
149 #endif
150
151                 /** The Terminated signal is emitted when application terminates. */
152 #ifdef NOPBD  /* outside ardour */
153                 sigc::signal<void> Terminated;
154 #else
155                 PBD::Signal0<void> Terminated;
156 #endif
157
158                 /** interposer to emit signal for writes to STDOUT/ERR.
159                  *
160                  * Thread that reads the stdout of the forked
161                  * process and signal-sends it to the main thread.
162                  * It also emits the Terminated() signal once
163                  * the the forked process closes it's stdout.
164                  *
165                  * Note: it's actually 'private' function but used
166                  * by the internal pthread, which only has a pointer
167                  * to this instance and thus can only access public fn.
168                  */
169                 void output_interposer ();
170
171         protected:
172                 std::string cmd; ///< path to command - set when creating the class
173                 int nicelevel; ///< process nice level - defaults to 0
174
175                 void make_argp(std::string);
176                 void make_envp();
177
178                 char **argp;
179                 char **envp;
180
181         private:
182 #ifdef __WIN32__
183                 PROCESS_INFORMATION *pid;
184                 HANDLE stdinP[2];
185                 HANDLE stdoutP[2];
186                 HANDLE stderrP[2];
187                 char *w_args;
188                 void make_wargs(char **);
189 #else
190                 pid_t pid;
191 #endif
192                 pthread_mutex_t write_lock;
193
194                 int fdin; ///< file-descriptor for writing to child's STDIN. This variable is identical to pin[1] but also used as status check if the stdin pipe is open: <0 means closed.
195                 int pok[2];
196                 int pin[2];
197                 int pout[2];
198
199                 pthread_t      thread_id_tt;
200                 bool           thread_active;
201 };
202
203 #endif /* __ardour_system_exec_h__ */
204 #endif /* WITH_VIDEOTIMELINE */