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