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