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