]>
Commit | Line | Data |
---|---|---|
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 | @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 | @see wxExecute(), @ref overview_sampleexec "exec sample" | |
43 | */ | |
44 | class wxProcess : public wxEvtHandler | |
45 | { | |
46 | public: | |
47 | //@{ | |
48 | /** | |
49 | Constructs a process object. @a id is only used in the case you want to | |
50 | use wxWidgets events. It identifies this object, or another window that will | |
51 | receive the event. | |
52 | If the @a parent parameter is different from @NULL, it will receive | |
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. | |
55 | The second constructor creates an object without any associated parent (and | |
56 | hence no id neither) but allows to specify the @a flags which can have the | |
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 | ||
61 | @param parent | |
62 | The event handler parent. | |
63 | @param id | |
64 | id of an event. | |
65 | @param flags | |
66 | either wxPROCESS_DEFAULT or wxPROCESS_REDIRECT | |
67 | */ | |
68 | wxProcess(wxEvtHandler* parent = NULL, int id = -1); | |
69 | wxProcess(int flags); | |
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 | ||
101 | @see Kill(), @ref overview_sampleexec "Exec sample" | |
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 | */ | |
109 | wxInputStream* GetErrorStream() const; | |
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 | */ | |
116 | wxInputStream* GetInputStream() const; | |
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 | */ | |
123 | wxOutputStream* GetOutputStream() const; | |
124 | ||
125 | /** | |
126 | Returns the process ID of the process launched by Open(). | |
127 | */ | |
128 | long GetPid() const; | |
129 | ||
130 | /** | |
131 | Returns @true if there is data to be read on the child process standard | |
132 | error stream. | |
133 | ||
134 | @see IsInputAvailable() | |
135 | */ | |
136 | bool IsErrorAvailable() const; | |
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. | |
142 | See the @ref overview_sampleexec "exec sample" for an example of using this | |
143 | function. | |
144 | ||
145 | @see IsInputOpened() | |
146 | */ | |
147 | bool IsInputAvailable() const; | |
148 | ||
149 | /** | |
150 | Returns @true if the child process standard output stream is opened. | |
151 | */ | |
152 | bool IsInputOpened() const; | |
153 | ||
154 | /** | |
155 | Send the specified signal to the given process. Possible signal values are: | |
156 | ||
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. | |
160 | The @a flags parameter can be wxKILL_NOCHILDREN (the default), | |
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. | |
164 | Returns the element of @c wxKillError enum: | |
165 | ||
166 | @see Exists(), wxKill(), @ref overview_sampleexec "Exec sample" | |
167 | */ | |
168 | static wxKillError Kill(int pid, wxSignal signal = wxSIGNONE, | |
169 | int flags = wxKILL_NOCHILDREN); | |
170 | ||
171 | /** | |
172 | It is called when the process with the pid | |
173 | ||
174 | @param pid finishes. | |
175 | It raises a wxWidgets event when it isn't overridden. | |
176 | ||
177 | pid | |
178 | The pid of the process which has just terminated. | |
179 | @param status | |
180 | The exit code of the process. | |
181 | */ | |
182 | void OnTerminate(int pid, int status); | |
183 | ||
184 | /** | |
185 | This static method replaces the standard @c popen() function: it launches | |
186 | the process specified by the @a cmd parameter and returns the wxProcess | |
187 | object which can be used to retrieve the streams connected to the standard | |
188 | input, output and error output of the child process. | |
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 | ||
195 | @param cmd | |
196 | The command to execute, including optional arguments. | |
197 | @param flags | |
198 | The flags to pass to wxExecute. | |
199 | NOTE: wxEXEC_SYNC should not be used. | |
200 | ||
201 | @returns A pointer to new wxProcess object or @NULL on error. | |
202 | ||
203 | @see wxExecute() | |
204 | */ | |
205 | static wxProcess* Open(const wxString& cmd, | |
206 | int flags = wxEXEC_ASYNC); | |
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 | ||
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 | @see wxProcess, @ref overview_eventhandlingoverview | |
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 | */ | |
247 | int GetPid() const; | |
248 | }; | |
249 |