]>
Commit | Line | Data |
---|---|---|
cf447356 | 1 | ///////////////////////////////////////////////////////////////////////////// |
ce7208d4 | 2 | // Name: wx/process.h |
cf447356 GL |
3 | // Purpose: wxProcess class |
4 | // Author: Guilhem Lavaux | |
7e84f02d | 5 | // Modified by: Vadim Zeitlin to check error codes, added Detach() method |
cf447356 GL |
6 | // Created: 24/06/98 |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) 1998 Guilhem Lavaux | |
65571936 | 9 | // Licence: wxWindows licence |
cf447356 GL |
10 | ///////////////////////////////////////////////////////////////////////////// |
11 | ||
34138703 JS |
12 | #ifndef _WX_PROCESSH__ |
13 | #define _WX_PROCESSH__ | |
cf447356 | 14 | |
cf447356 | 15 | #include "wx/event.h" |
f6bcfd97 BP |
16 | |
17 | #if wxUSE_STREAMS | |
0c850713 | 18 | #include "wx/stream.h" |
f6bcfd97 | 19 | #endif |
cf447356 | 20 | |
50567b69 VZ |
21 | #include "wx/utils.h" // for wxSignal |
22 | ||
da00a8bb VZ |
23 | // the wxProcess creation flags |
24 | enum | |
25 | { | |
26 | // no redirection | |
27 | wxPROCESS_DEFAULT = 0, | |
28 | ||
29 | // redirect the IO of the child process | |
30 | wxPROCESS_REDIRECT = 1 | |
31 | }; | |
32 | ||
2e4df4bf | 33 | // ---------------------------------------------------------------------------- |
5336ece4 VZ |
34 | // A wxProcess object should be passed to wxExecute - than its OnTerminate() |
35 | // function will be called when the process terminates. | |
2e4df4bf VZ |
36 | // ---------------------------------------------------------------------------- |
37 | ||
bddd7a8d | 38 | class WXDLLIMPEXP_BASE wxProcess : public wxEvtHandler |
cf447356 | 39 | { |
5336ece4 | 40 | public: |
da00a8bb | 41 | // kill the process with the given PID |
e0f6b731 | 42 | static wxKillError Kill(int pid, wxSignal sig = wxSIGTERM, int flags = wxKILL_NOCHILDREN); |
da00a8bb VZ |
43 | |
44 | // test if the given process exists | |
45 | static bool Exists(int pid); | |
46 | ||
47 | // this function replaces the standard popen() one: it launches a process | |
48 | // asynchronously and allows the caller to get the streams connected to its | |
49 | // std{in|out|err} | |
50 | // | |
51 | // on error NULL is returned, in any case the process object will be | |
52 | // deleted automatically when the process terminates and should *not* be | |
53 | // deleted by the caller | |
e626d7c7 | 54 | static wxProcess *Open(const wxString& cmd, int flags = wxEXEC_ASYNC); |
da00a8bb VZ |
55 | |
56 | ||
57 | // ctors | |
d3b9f782 | 58 | wxProcess(wxEvtHandler *parent = NULL, int nId = wxID_ANY) |
dcb68102 | 59 | { Init(parent, nId, wxPROCESS_DEFAULT); } |
da00a8bb | 60 | |
7e548f6b | 61 | wxProcess(int flags) { Init(NULL, wxID_ANY, flags); } |
cf447356 | 62 | |
cd6ce4a9 VZ |
63 | virtual ~wxProcess(); |
64 | ||
a387938f JS |
65 | // get the process ID of the process executed by Open() |
66 | long GetPid() const { return m_pid; } | |
67 | ||
cd6ce4a9 | 68 | // may be overridden to be notified about process termination |
5336ece4 | 69 | virtual void OnTerminate(int pid, int status); |
cf447356 | 70 | |
cd6ce4a9 VZ |
71 | // call this before passing the object to wxExecute() to redirect the |
72 | // launched process stdin/stdout, then use GetInputStream() and | |
73 | // GetOutputStream() to get access to them | |
7e548f6b | 74 | void Redirect() { m_redirect = true; } |
cd6ce4a9 VZ |
75 | bool IsRedirected() const { return m_redirect; } |
76 | ||
7e84f02d VZ |
77 | // detach from the parent - should be called by the parent if it's deleted |
78 | // before the process it started terminates | |
79 | void Detach(); | |
80 | ||
f6bcfd97 | 81 | #if wxUSE_STREAMS |
8b33ae2d | 82 | // Pipe handling |
cd6ce4a9 | 83 | wxInputStream *GetInputStream() const { return m_inputStream; } |
f6bcfd97 | 84 | wxInputStream *GetErrorStream() const { return m_errorStream; } |
cd6ce4a9 | 85 | wxOutputStream *GetOutputStream() const { return m_outputStream; } |
8b33ae2d | 86 | |
f6bcfd97 BP |
87 | // close the output stream indicating that nothing more will be written |
88 | void CloseOutput() { delete m_outputStream; m_outputStream = NULL; } | |
89 | ||
7e548f6b | 90 | // return true if the child process stdout is not closed |
411165f3 VZ |
91 | bool IsInputOpened() const; |
92 | ||
7e548f6b | 93 | // return true if any input is available on the child process stdout/err |
411165f3 VZ |
94 | bool IsInputAvailable() const; |
95 | bool IsErrorAvailable() const; | |
96 | ||
cd6ce4a9 | 97 | // implementation only (for wxExecute) |
da00a8bb VZ |
98 | // |
99 | // NB: the streams passed here should correspond to the child process | |
100 | // stdout, stdin and stderr and here the normal naming convention is | |
101 | // used unlike elsewhere in this class | |
102 | void SetPipeStreams(wxInputStream *outStream, | |
103 | wxOutputStream *inStream, | |
f6bcfd97 BP |
104 | wxInputStream *errStream); |
105 | #endif // wxUSE_STREAMS | |
8b33ae2d | 106 | |
eaf4bde6 VZ |
107 | // priority |
108 | // Sets the priority to the given value: see wxPRIORITY_XXX constants. | |
109 | // | |
110 | // NB: the priority can only be set before the process is created | |
111 | void SetPriority(unsigned priority); | |
112 | ||
113 | // Get the current priority. | |
114 | unsigned GetPriority() const { return m_priority; } | |
115 | ||
ca5016d4 FM |
116 | // implementation only - don't use! |
117 | // -------------------------------- | |
03647350 | 118 | |
ca5016d4 FM |
119 | // needs to be public since it needs to be used from wxExecute() global func |
120 | void SetPid(long pid) { m_pid = pid; } | |
121 | ||
5336ece4 | 122 | protected: |
da00a8bb | 123 | void Init(wxEvtHandler *parent, int id, int flags); |
cd6ce4a9 | 124 | |
5336ece4 | 125 | int m_id; |
a387938f | 126 | long m_pid; |
cd6ce4a9 | 127 | |
eaf4bde6 VZ |
128 | unsigned m_priority; |
129 | ||
f6bcfd97 | 130 | #if wxUSE_STREAMS |
da00a8bb VZ |
131 | // these streams are connected to stdout, stderr and stdin of the child |
132 | // process respectively (yes, m_inputStream corresponds to stdout -- very | |
133 | // confusing but too late to change now) | |
f6bcfd97 BP |
134 | wxInputStream *m_inputStream, |
135 | *m_errorStream; | |
cd6ce4a9 | 136 | wxOutputStream *m_outputStream; |
f6bcfd97 | 137 | #endif // wxUSE_STREAMS |
cd6ce4a9 VZ |
138 | |
139 | bool m_redirect; | |
50567b69 VZ |
140 | |
141 | DECLARE_DYNAMIC_CLASS(wxProcess) | |
c0c133e1 | 142 | wxDECLARE_NO_COPY_CLASS(wxProcess); |
cf447356 GL |
143 | }; |
144 | ||
2e4df4bf VZ |
145 | // ---------------------------------------------------------------------------- |
146 | // wxProcess events | |
147 | // ---------------------------------------------------------------------------- | |
148 | ||
3c778901 VZ |
149 | class WXDLLIMPEXP_FWD_BASE wxProcessEvent; |
150 | ||
9b11752c | 151 | wxDECLARE_EXPORTED_EVENT( WXDLLIMPEXP_BASE, wxEVT_END_PROCESS, wxProcessEvent ); |
71b9ed15 | 152 | |
bddd7a8d | 153 | class WXDLLIMPEXP_BASE wxProcessEvent : public wxEvent |
0c850713 VZ |
154 | { |
155 | public: | |
dcb68102 | 156 | wxProcessEvent(int nId = 0, int pid = 0, int exitcode = 0) : wxEvent(nId) |
0c850713 VZ |
157 | { |
158 | m_eventType = wxEVT_END_PROCESS; | |
159 | m_pid = pid; | |
160 | m_exitcode = exitcode; | |
161 | } | |
162 | ||
163 | // accessors | |
164 | // PID of process which terminated | |
165 | int GetPid() { return m_pid; } | |
166 | ||
167 | // the exit code | |
168 | int GetExitCode() { return m_exitcode; } | |
169 | ||
acd15a3f VZ |
170 | // implement the base class pure virtual |
171 | virtual wxEvent *Clone() const { return new wxProcessEvent(*this); } | |
172 | ||
0c850713 | 173 | public: |
acd15a3f VZ |
174 | int m_pid, |
175 | m_exitcode; | |
0c850713 | 176 | |
fc7a2a60 | 177 | DECLARE_DYNAMIC_CLASS_NO_ASSIGN(wxProcessEvent) |
0c850713 VZ |
178 | }; |
179 | ||
afadf3bc | 180 | typedef void (wxEvtHandler::*wxProcessEventFunction)(wxProcessEvent&); |
cf447356 | 181 | |
7fa03f04 | 182 | #define wxProcessEventHandler(func) \ |
3c778901 | 183 | wxEVENT_HANDLER_CAST(wxProcessEventFunction, func) |
7fa03f04 | 184 | |
82a5f02c | 185 | #define EVT_END_PROCESS(id, func) \ |
7fa03f04 | 186 | wx__DECLARE_EVT1(wxEVT_END_PROCESS, id, wxProcessEventHandler(func)) |
cf447356 | 187 | |
7fa03f04 | 188 | #endif // _WX_PROCESSH__ |