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