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