]>
Commit | Line | Data |
---|---|---|
1 | ///////////////////////////////////////////////////////////////////////////// | |
2 | // Name: 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 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) 1998 Guilhem Lavaux | |
9 | // Licence: wxWindows licence | |
10 | ///////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | #ifndef _WX_PROCESSH__ | |
13 | #define _WX_PROCESSH__ | |
14 | ||
15 | #include "wx/event.h" | |
16 | ||
17 | #if wxUSE_STREAMS | |
18 | #include "wx/stream.h" | |
19 | #endif | |
20 | ||
21 | #include "wx/utils.h" // for wxSignal | |
22 | ||
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 | ||
33 | // ---------------------------------------------------------------------------- | |
34 | // A wxProcess object should be passed to wxExecute - than its OnTerminate() | |
35 | // function will be called when the process terminates. | |
36 | // ---------------------------------------------------------------------------- | |
37 | ||
38 | class WXDLLIMPEXP_BASE wxProcess : public wxEvtHandler | |
39 | { | |
40 | public: | |
41 | // kill the process with the given PID | |
42 | static wxKillError Kill(int pid, wxSignal sig = wxSIGTERM, int flags = wxKILL_NOCHILDREN); | |
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 | |
54 | static wxProcess *Open(const wxString& cmd, int flags = wxEXEC_ASYNC); | |
55 | ||
56 | ||
57 | // ctors | |
58 | wxProcess(wxEvtHandler *parent = (wxEvtHandler *) NULL, int nId = wxID_ANY) | |
59 | { Init(parent, nId, wxPROCESS_DEFAULT); } | |
60 | ||
61 | wxProcess(int flags) { Init(NULL, wxID_ANY, flags); } | |
62 | ||
63 | virtual ~wxProcess(); | |
64 | ||
65 | // may be overridden to be notified about process termination | |
66 | virtual void OnTerminate(int pid, int status); | |
67 | ||
68 | // call this before passing the object to wxExecute() to redirect the | |
69 | // launched process stdin/stdout, then use GetInputStream() and | |
70 | // GetOutputStream() to get access to them | |
71 | void Redirect() { m_redirect = true; } | |
72 | bool IsRedirected() const { return m_redirect; } | |
73 | ||
74 | // detach from the parent - should be called by the parent if it's deleted | |
75 | // before the process it started terminates | |
76 | void Detach(); | |
77 | ||
78 | #if wxUSE_STREAMS | |
79 | // Pipe handling | |
80 | wxInputStream *GetInputStream() const { return m_inputStream; } | |
81 | wxInputStream *GetErrorStream() const { return m_errorStream; } | |
82 | wxOutputStream *GetOutputStream() const { return m_outputStream; } | |
83 | ||
84 | // close the output stream indicating that nothing more will be written | |
85 | void CloseOutput() { delete m_outputStream; m_outputStream = NULL; } | |
86 | ||
87 | // return true if the child process stdout is not closed | |
88 | bool IsInputOpened() const; | |
89 | ||
90 | // return true if any input is available on the child process stdout/err | |
91 | bool IsInputAvailable() const; | |
92 | bool IsErrorAvailable() const; | |
93 | ||
94 | // implementation only (for wxExecute) | |
95 | // | |
96 | // NB: the streams passed here should correspond to the child process | |
97 | // stdout, stdin and stderr and here the normal naming convention is | |
98 | // used unlike elsewhere in this class | |
99 | void SetPipeStreams(wxInputStream *outStream, | |
100 | wxOutputStream *inStream, | |
101 | wxInputStream *errStream); | |
102 | #endif // wxUSE_STREAMS | |
103 | ||
104 | // for backwards compatibility only, don't use | |
105 | #if WXWIN_COMPATIBILITY_2_2 | |
106 | wxDEPRECATED( wxProcess(wxEvtHandler *parent, bool redirect) ); | |
107 | #endif // WXWIN_COMPATIBILITY_2_2 | |
108 | ||
109 | protected: | |
110 | void Init(wxEvtHandler *parent, int id, int flags); | |
111 | ||
112 | int m_id; | |
113 | ||
114 | #if wxUSE_STREAMS | |
115 | // these streams are connected to stdout, stderr and stdin of the child | |
116 | // process respectively (yes, m_inputStream corresponds to stdout -- very | |
117 | // confusing but too late to change now) | |
118 | wxInputStream *m_inputStream, | |
119 | *m_errorStream; | |
120 | wxOutputStream *m_outputStream; | |
121 | #endif // wxUSE_STREAMS | |
122 | ||
123 | bool m_redirect; | |
124 | ||
125 | DECLARE_DYNAMIC_CLASS(wxProcess) | |
126 | DECLARE_NO_COPY_CLASS(wxProcess) | |
127 | }; | |
128 | ||
129 | // ---------------------------------------------------------------------------- | |
130 | // wxProcess events | |
131 | // ---------------------------------------------------------------------------- | |
132 | ||
133 | BEGIN_DECLARE_EVENT_TYPES() | |
134 | DECLARE_EXPORTED_EVENT_TYPE(WXDLLIMPEXP_BASE, wxEVT_END_PROCESS, 440) | |
135 | END_DECLARE_EVENT_TYPES() | |
136 | ||
137 | class WXDLLIMPEXP_BASE wxProcessEvent : public wxEvent | |
138 | { | |
139 | public: | |
140 | wxProcessEvent(int nId = 0, int pid = 0, int exitcode = 0) : wxEvent(nId) | |
141 | { | |
142 | m_eventType = wxEVT_END_PROCESS; | |
143 | m_pid = pid; | |
144 | m_exitcode = exitcode; | |
145 | } | |
146 | ||
147 | // accessors | |
148 | // PID of process which terminated | |
149 | int GetPid() { return m_pid; } | |
150 | ||
151 | // the exit code | |
152 | int GetExitCode() { return m_exitcode; } | |
153 | ||
154 | // implement the base class pure virtual | |
155 | virtual wxEvent *Clone() const { return new wxProcessEvent(*this); } | |
156 | ||
157 | public: | |
158 | int m_pid, | |
159 | m_exitcode; | |
160 | ||
161 | DECLARE_DYNAMIC_CLASS_NO_ASSIGN(wxProcessEvent) | |
162 | }; | |
163 | ||
164 | typedef void (wxEvtHandler::*wxProcessEventFunction)(wxProcessEvent&); | |
165 | ||
166 | #define wxProcessEventHandler(func) \ | |
167 | (wxObjectEventFunction)(wxEventFunction)wxStaticCastEvent(wxProcessEventFunction, &func) | |
168 | ||
169 | #define EVT_END_PROCESS(id, func) \ | |
170 | wx__DECLARE_EVT1(wxEVT_END_PROCESS, id, wxProcessEventHandler(func)) | |
171 | ||
172 | #endif // _WX_PROCESSH__ |