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