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