]> git.saurik.com Git - wxWidgets.git/blame - include/wx/process.h
simpler implementation
[wxWidgets.git] / include / wx / process.h
CommitLineData
cf447356
GL
1/////////////////////////////////////////////////////////////////////////////
2// Name: process.h
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
24enum
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 38class WXDLLIMPEXP_BASE wxProcess : public wxEvtHandler
cf447356 39{
5336ece4 40public:
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
dcb68102
RN
58 wxProcess(wxEvtHandler *parent = (wxEvtHandler *) NULL, int nId = wxID_ANY)
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
65 // may be overridden to be notified about process termination
5336ece4 66 virtual void OnTerminate(int pid, int status);
cf447356 67
cd6ce4a9
VZ
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
7e548f6b 71 void Redirect() { m_redirect = true; }
cd6ce4a9
VZ
72 bool IsRedirected() const { return m_redirect; }
73
7e84f02d
VZ
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
f6bcfd97 78#if wxUSE_STREAMS
8b33ae2d 79 // Pipe handling
cd6ce4a9 80 wxInputStream *GetInputStream() const { return m_inputStream; }
f6bcfd97 81 wxInputStream *GetErrorStream() const { return m_errorStream; }
cd6ce4a9 82 wxOutputStream *GetOutputStream() const { return m_outputStream; }
8b33ae2d 83
f6bcfd97
BP
84 // close the output stream indicating that nothing more will be written
85 void CloseOutput() { delete m_outputStream; m_outputStream = NULL; }
86
7e548f6b 87 // return true if the child process stdout is not closed
411165f3
VZ
88 bool IsInputOpened() const;
89
7e548f6b 90 // return true if any input is available on the child process stdout/err
411165f3
VZ
91 bool IsInputAvailable() const;
92 bool IsErrorAvailable() const;
93
cd6ce4a9 94 // implementation only (for wxExecute)
da00a8bb
VZ
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,
f6bcfd97
BP
101 wxInputStream *errStream);
102#endif // wxUSE_STREAMS
8b33ae2d 103
5336ece4 104protected:
da00a8bb 105 void Init(wxEvtHandler *parent, int id, int flags);
cd6ce4a9 106
5336ece4 107 int m_id;
cd6ce4a9 108
f6bcfd97 109#if wxUSE_STREAMS
da00a8bb
VZ
110 // these streams are connected to stdout, stderr and stdin of the child
111 // process respectively (yes, m_inputStream corresponds to stdout -- very
112 // confusing but too late to change now)
f6bcfd97
BP
113 wxInputStream *m_inputStream,
114 *m_errorStream;
cd6ce4a9 115 wxOutputStream *m_outputStream;
f6bcfd97 116#endif // wxUSE_STREAMS
cd6ce4a9
VZ
117
118 bool m_redirect;
50567b69
VZ
119
120 DECLARE_DYNAMIC_CLASS(wxProcess)
22f3361e 121 DECLARE_NO_COPY_CLASS(wxProcess)
cf447356
GL
122};
123
2e4df4bf
VZ
124// ----------------------------------------------------------------------------
125// wxProcess events
126// ----------------------------------------------------------------------------
127
71b9ed15 128BEGIN_DECLARE_EVENT_TYPES()
4c3ebca9 129 DECLARE_EXPORTED_EVENT_TYPE(WXDLLIMPEXP_BASE, wxEVT_END_PROCESS, 440)
71b9ed15
VZ
130END_DECLARE_EVENT_TYPES()
131
bddd7a8d 132class WXDLLIMPEXP_BASE wxProcessEvent : public wxEvent
0c850713
VZ
133{
134public:
dcb68102 135 wxProcessEvent(int nId = 0, int pid = 0, int exitcode = 0) : wxEvent(nId)
0c850713
VZ
136 {
137 m_eventType = wxEVT_END_PROCESS;
138 m_pid = pid;
139 m_exitcode = exitcode;
140 }
141
142 // accessors
143 // PID of process which terminated
144 int GetPid() { return m_pid; }
145
146 // the exit code
147 int GetExitCode() { return m_exitcode; }
148
acd15a3f
VZ
149 // implement the base class pure virtual
150 virtual wxEvent *Clone() const { return new wxProcessEvent(*this); }
151
0c850713 152public:
acd15a3f
VZ
153 int m_pid,
154 m_exitcode;
0c850713 155
fc7a2a60 156 DECLARE_DYNAMIC_CLASS_NO_ASSIGN(wxProcessEvent)
0c850713
VZ
157};
158
afadf3bc 159typedef void (wxEvtHandler::*wxProcessEventFunction)(wxProcessEvent&);
cf447356 160
7fa03f04 161#define wxProcessEventHandler(func) \
d94c09cd 162 (wxObjectEventFunction)(wxEventFunction)wxStaticCastEvent(wxProcessEventFunction, &func)
7fa03f04 163
82a5f02c 164#define EVT_END_PROCESS(id, func) \
7fa03f04 165 wx__DECLARE_EVT1(wxEVT_END_PROCESS, id, wxProcessEventHandler(func))
cf447356 166
7fa03f04 167#endif // _WX_PROCESSH__