add missing beginEventEmissionTable
[wxWidgets.git] / interface / wx / process.h
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: process.h
3 // Purpose: interface of wxProcess
4 // Author: wxWidgets team
5 // RCS-ID: $Id$
6 // Licence: wxWindows license
7 /////////////////////////////////////////////////////////////////////////////
8
9 /**
10 Signal constants used by wxProcess.
11 */
12 enum wxSignal
13 {
14 wxSIGNONE = 0, //!< verify if the process exists under Unix
15 wxSIGHUP,
16 wxSIGINT,
17 wxSIGQUIT,
18 wxSIGILL,
19 wxSIGTRAP,
20 wxSIGABRT,
21 wxSIGEMT,
22 wxSIGFPE,
23 wxSIGKILL, //!< forcefully kill, dangerous!
24 wxSIGBUS,
25 wxSIGSEGV,
26 wxSIGSYS,
27 wxSIGPIPE,
28 wxSIGALRM,
29 wxSIGTERM //!< terminate the process gently
30 };
31
32 /**
33 Return values for wxProcess::Kill.
34 */
35 enum wxKillError
36 {
37 wxKILL_OK, //!< no error
38 wxKILL_BAD_SIGNAL, //!< no such signal
39 wxKILL_ACCESS_DENIED, //!< permission denied
40 wxKILL_NO_PROCESS, //!< no such process
41 wxKILL_ERROR //!< another, unspecified error
42 };
43
44
45 /**
46 @class wxProcess
47
48 The objects of this class are used in conjunction with the ::wxExecute() function.
49 When a wxProcess object is passed to ::wxExecute(), its OnTerminate() virtual method
50 is called when the process terminates. This allows the program to be (asynchronously)
51 notified about the process termination and also retrieve its exit status which is
52 unavailable from ::wxExecute() in the case of asynchronous execution.
53
54 @note If the process termination notification is processed by the
55 parent, it is responsible for deleting the wxProcess object which sent it.
56 However, if it is not processed, the object will delete itself and so the
57 library users should only delete those objects whose notifications have been
58 processed (and call wxProcess::Detach for others).
59
60 wxProcess also supports IO redirection of the child process. For this, you have
61 to call its Redirect() method before passing it to ::wxExecute().
62 If the child process was launched successfully, GetInputStream(), GetOutputStream()
63 and GetErrorStream() can then be used to retrieve the streams corresponding to the
64 child process standard output, input and error output respectively.
65
66 @beginEventEmissionTable{wxProcessEvent}
67 @event{EVT_END_PROCESS(id, func)}
68 Process a @c wxEVT_END_PROCESS event, sent by wxProcess::OnTerminate upon
69 the external process termination.
70 @endEventTable
71
72 @library{wxbase}
73 @category{appmanagement}
74
75 @see wxExecute(), @ref page_samples_exec
76 */
77 class wxProcess : public wxEvtHandler
78 {
79 public:
80 /**
81 Constructs a process object. @a id is only used in the case you want to
82 use wxWidgets events. It identifies this object, or another window that will
83 receive the event.
84
85 If the @a parent parameter is different from @NULL, it will receive
86 a @c wxEVT_END_PROCESS notification event (you should insert @c EVT_END_PROCESS
87 macro in the event table of the parent to handle it) with the given @a id.
88
89 @param parent
90 The event handler parent.
91 @param id
92 id of an event.
93 */
94 wxProcess(wxEvtHandler* parent = NULL, int id = -1);
95
96 /**
97 Creates an object without any associated parent (and hence no id neither)
98 but allows to specify the @a flags which can have the value of
99 @c wxPROCESS_DEFAULT or @c wxPROCESS_REDIRECT.
100
101 Specifying the former value has no particular effect while using the latter
102 one is equivalent to calling Redirect().
103 */
104 wxProcess(int flags);
105
106 /**
107 Destroys the wxProcess object.
108 */
109 virtual ~wxProcess();
110
111 /**
112 Closes the output stream (the one connected to the stdin of the child
113 process).
114
115 This function can be used to indicate to the child process that
116 there is no more data to be read - usually, a filter program will only
117 terminate when the input stream is closed.
118 */
119 void CloseOutput();
120
121 /**
122 Normally, a wxProcess object is deleted by its parent when it receives the
123 notification about the process termination. However, it might happen that the
124 parent object is destroyed before the external process is terminated (e.g. a
125 window from which this external process was launched is closed by the user)
126 and in this case it @b should not delete the wxProcess object, but
127 @b should call Detach() instead. After the wxProcess object is detached
128 from its parent, no notification events will be sent to the parent and the
129 object will delete itself upon reception of the process termination
130 notification.
131 */
132 void Detach();
133
134 /**
135 Returns @true if the given process exists in the system.
136
137 @see Kill(), @ref page_samples_exec "Exec sample"
138 */
139 static bool Exists(int pid);
140
141 /**
142 Returns an input stream which corresponds to the standard error output (stderr)
143 of the child process.
144 */
145 wxInputStream* GetErrorStream() const;
146
147 /**
148 It returns an input stream corresponding to the standard output stream of the
149 subprocess. If it is @NULL, you have not turned on the redirection.
150
151 @see Redirect().
152 */
153 wxInputStream* GetInputStream() const;
154
155 /**
156 It returns an output stream correspoding to the input stream of the subprocess.
157 If it is @NULL, you have not turned on the redirection.
158
159 @see Redirect().
160 */
161 wxOutputStream* GetOutputStream() const;
162
163 /**
164 Returns the process ID of the process launched by Open().
165 */
166 long GetPid() const;
167
168 /**
169 Returns @true if there is data to be read on the child process standard
170 error stream.
171
172 @see IsInputAvailable()
173 */
174 bool IsErrorAvailable() const;
175
176 /**
177 Returns @true if there is data to be read on the child process standard
178 output stream.
179
180 This allows to write simple (and extremely inefficient) polling-based code
181 waiting for a better mechanism in future wxWidgets versions.
182 See the @ref page_samples_exec "exec sample" for an example of using this
183 function.
184
185 @see IsInputOpened()
186 */
187 bool IsInputAvailable() const;
188
189 /**
190 Returns @true if the child process standard output stream is opened.
191 */
192 bool IsInputOpened() const;
193
194 /**
195 Send the specified signal to the given process. Possible signal values
196 can be one of the ::wxSignal enumeration values.
197
198 @c wxSIGNONE, @c wxSIGKILL and @c wxSIGTERM have the same meaning
199 under both Unix and Windows but all the other signals are equivalent to
200 @c wxSIGTERM under Windows.
201
202 The @a flags parameter can be @c wxKILL_NOCHILDREN (the default),
203 or @c wxKILL_CHILDREN, in which case the child processes of this
204 process will be killed too. Note that under Unix, for @c wxKILL_CHILDREN
205 to work you should have created the process passing @c wxEXEC_MAKE_GROUP_LEADER.
206
207 Returns the element of ::wxKillError enum.
208
209 @see Exists(), wxKill(), @ref page_samples_exec "Exec sample"
210 */
211 static wxKillError Kill(int pid, wxSignal sig = wxSIGTERM,
212 int flags = wxKILL_NOCHILDREN);
213
214 /**
215 It is called when the process with the pid @a pid finishes.
216 It raises a wxWidgets event when it isn't overridden.
217
218 @param pid
219 The pid of the process which has just terminated.
220 @param status
221 The exit code of the process.
222 */
223 virtual void OnTerminate(int pid, int status);
224
225 /**
226 This static method replaces the standard @c popen() function: it launches
227 the process specified by the @a cmd parameter and returns the wxProcess
228 object which can be used to retrieve the streams connected to the standard
229 input, output and error output of the child process.
230
231 If the process couldn't be launched, @NULL is returned.
232
233 @remarks
234 In any case the returned pointer should @b not be deleted, rather the process
235 object will be destroyed automatically when the child process terminates. This
236 does mean that the child process should be told to quit before the main program
237 exits to avoid memory leaks.
238
239 @param cmd
240 The command to execute, including optional arguments.
241 @param flags
242 The flags to pass to ::wxExecute().
243 Note: @c wxEXEC_SYNC should not be used.
244
245 @return A pointer to new wxProcess object or @NULL on error.
246
247 @see ::wxExecute()
248 */
249 static wxProcess* Open(const wxString& cmd,
250 int flags = wxEXEC_ASYNC);
251
252 /**
253 Turns on redirection.
254
255 ::wxExecute() will try to open a couple of pipes to catch the subprocess stdio.
256 The caught input stream is returned by GetOutputStream() as a non-seekable stream.
257 The caught output stream is returned by GetInputStream() as a non-seekable stream.
258 */
259 void Redirect();
260 };
261
262
263
264 /**
265 @class wxProcessEvent
266
267 A process event is sent to the wxEvtHandler specified to wxProcess
268 when a process is terminated.
269
270 @beginEventTable{wxProcessEvent}
271 @event{EVT_END_PROCESS(id, func)}
272 Process a @c wxEVT_END_PROCESS event. @a id is the identifier of the process
273 object (the id passed to the wxProcess constructor) or a window to receive
274 the event.
275 @endEventTable
276
277 @library{wxbase}
278 @category{events}
279
280 @see wxProcess, @ref overview_events
281 */
282 class wxProcessEvent : public wxEvent
283 {
284 public:
285 /**
286 Constructor.
287
288 Takes a wxProcessObject or window id, a process id and an exit status.
289 */
290 wxProcessEvent(int id = 0, int pid = 0, int exitcode = 0);
291
292 /**
293 Returns the exist status.
294 */
295 int GetExitCode();
296
297 /**
298 Returns the process id.
299 */
300 int GetPid();
301 };
302