/src/wxwidgets/include/wx/process.h
Line | Count | Source |
1 | | ///////////////////////////////////////////////////////////////////////////// |
2 | | // Name: wx/process.h |
3 | | // Purpose: wxProcess class |
4 | | // Author: Guilhem Lavaux |
5 | | // Modified by: Vadim Zeitlin to check error codes, added Detach() method |
6 | | // Created: 24/06/98 |
7 | | // Copyright: (c) 1998 Guilhem Lavaux |
8 | | // Licence: wxWindows licence |
9 | | ///////////////////////////////////////////////////////////////////////////// |
10 | | |
11 | | #ifndef _WX_PROCESSH__ |
12 | | #define _WX_PROCESSH__ |
13 | | |
14 | | #include "wx/event.h" |
15 | | |
16 | | #if wxUSE_STREAMS |
17 | | #include "wx/stream.h" |
18 | | #endif |
19 | | |
20 | | #include "wx/utils.h" // for wxSignal |
21 | | |
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 | | |
32 | | // ---------------------------------------------------------------------------- |
33 | | // A wxProcess object should be passed to wxExecute - than its OnTerminate() |
34 | | // function will be called when the process terminates. |
35 | | // ---------------------------------------------------------------------------- |
36 | | |
37 | | class WXDLLIMPEXP_BASE wxProcess : public wxEvtHandler |
38 | | { |
39 | | public: |
40 | | // kill the process with the given PID |
41 | | static wxKillError Kill(int pid, wxSignal sig = wxSIGTERM, int flags = wxKILL_NOCHILDREN); |
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 nullptr 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 |
53 | | static wxProcess *Open(const wxString& cmd, int flags = wxEXEC_ASYNC); |
54 | | |
55 | | |
56 | | // ctors |
57 | | wxProcess(wxEvtHandler *parent = nullptr, int nId = wxID_ANY) |
58 | 0 | { Init(parent, nId, wxPROCESS_DEFAULT); } |
59 | | |
60 | 0 | wxProcess(int flags) { Init(nullptr, wxID_ANY, flags); } |
61 | | |
62 | | virtual ~wxProcess(); |
63 | | |
64 | | // get the process ID of the process executed by Open() |
65 | 0 | long GetPid() const { return m_pid; } |
66 | | |
67 | | // may be overridden to be notified about process termination |
68 | | virtual void OnTerminate(int pid, int status); |
69 | | |
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 |
73 | 0 | void Redirect() { m_redirect = true; } |
74 | 0 | bool IsRedirected() const { return m_redirect; } |
75 | | |
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 | | |
80 | | // Activates a GUI process by bringing its (main) window to the front. |
81 | | // |
82 | | // Currently only implemented in wxMSW, simply returns false under the |
83 | | // other platforms. |
84 | | bool Activate() const; |
85 | | |
86 | | #if wxUSE_STREAMS |
87 | | // Pipe handling |
88 | 0 | wxInputStream *GetInputStream() const { return m_inputStream; } |
89 | 0 | wxInputStream *GetErrorStream() const { return m_errorStream; } |
90 | 0 | wxOutputStream *GetOutputStream() const { return m_outputStream; } |
91 | | |
92 | | // close the output stream indicating that nothing more will be written |
93 | 0 | void CloseOutput() { delete m_outputStream; m_outputStream = nullptr; } |
94 | | |
95 | | // return true if the child process stdout is not closed |
96 | | bool IsInputOpened() const; |
97 | | |
98 | | // return true if any input is available on the child process stdout/err |
99 | | bool IsInputAvailable() const; |
100 | | bool IsErrorAvailable() const; |
101 | | |
102 | | // implementation only (for wxExecute) |
103 | | // |
104 | | // NB: the streams passed here should correspond to the child process |
105 | | // stdout, stdin and stderr and here the normal naming convention is |
106 | | // used unlike elsewhere in this class |
107 | | void SetPipeStreams(wxInputStream *outStream, |
108 | | wxOutputStream *inStream, |
109 | | wxInputStream *errStream); |
110 | | #endif // wxUSE_STREAMS |
111 | | |
112 | | // priority |
113 | | // Sets the priority to the given value: see wxPRIORITY_XXX constants. |
114 | | // |
115 | | // NB: the priority can only be set before the process is created |
116 | | void SetPriority(unsigned priority); |
117 | | |
118 | | // Get the current priority. |
119 | 0 | unsigned GetPriority() const { return m_priority; } |
120 | | |
121 | | // implementation only - don't use! |
122 | | // -------------------------------- |
123 | | |
124 | | // needs to be public since it needs to be used from wxExecute() global func |
125 | 0 | void SetPid(long pid) { m_pid = pid; } |
126 | | |
127 | | protected: |
128 | | void Init(wxEvtHandler *parent, int id, int flags); |
129 | | |
130 | | int m_id; |
131 | | long m_pid; |
132 | | |
133 | | unsigned m_priority; |
134 | | |
135 | | #if wxUSE_STREAMS |
136 | | // these streams are connected to stdout, stderr and stdin of the child |
137 | | // process respectively (yes, m_inputStream corresponds to stdout -- very |
138 | | // confusing but too late to change now) |
139 | | wxInputStream *m_inputStream, |
140 | | *m_errorStream; |
141 | | wxOutputStream *m_outputStream; |
142 | | #endif // wxUSE_STREAMS |
143 | | |
144 | | bool m_redirect; |
145 | | |
146 | | wxDECLARE_DYNAMIC_CLASS(wxProcess); |
147 | | wxDECLARE_NO_COPY_CLASS(wxProcess); |
148 | | }; |
149 | | |
150 | | // ---------------------------------------------------------------------------- |
151 | | // wxProcess events |
152 | | // ---------------------------------------------------------------------------- |
153 | | |
154 | | class WXDLLIMPEXP_FWD_BASE wxProcessEvent; |
155 | | |
156 | | wxDECLARE_EXPORTED_EVENT( WXDLLIMPEXP_BASE, wxEVT_END_PROCESS, wxProcessEvent ); |
157 | | |
158 | | class WXDLLIMPEXP_BASE wxProcessEvent : public wxEvent |
159 | | { |
160 | | public: |
161 | 0 | wxProcessEvent(int nId = 0, int pid = 0, int exitcode = 0) : wxEvent(nId) |
162 | 0 | { |
163 | 0 | m_eventType = wxEVT_END_PROCESS; |
164 | 0 | m_pid = pid; |
165 | 0 | m_exitcode = exitcode; |
166 | 0 | } |
167 | | |
168 | | // accessors |
169 | | // PID of process which terminated |
170 | 0 | int GetPid() const { return m_pid; } |
171 | | |
172 | | // the exit code |
173 | 0 | int GetExitCode() const { return m_exitcode; } |
174 | | |
175 | | // implement the base class pure virtual |
176 | 0 | wxNODISCARD virtual wxEvent *Clone() const override { return new wxProcessEvent(*this); } |
177 | | |
178 | | public: |
179 | | int m_pid, |
180 | | m_exitcode; |
181 | | |
182 | | wxDECLARE_DYNAMIC_CLASS_NO_ASSIGN_DEF_COPY(wxProcessEvent); |
183 | | }; |
184 | | |
185 | | typedef void (wxEvtHandler::*wxProcessEventFunction)(wxProcessEvent&); |
186 | | |
187 | | #define wxProcessEventHandler(func) \ |
188 | | wxEVENT_HANDLER_CAST(wxProcessEventFunction, func) |
189 | | |
190 | | #define EVT_END_PROCESS(id, func) \ |
191 | | wx__DECLARE_EVT1(wxEVT_END_PROCESS, id, wxProcessEventHandler(func)) |
192 | | |
193 | | #endif // _WX_PROCESSH__ |