]>
git.saurik.com Git - wxWidgets.git/blob - src/common/process.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/process.cpp
3 // Purpose: Process termination classes
4 // Author: Guilhem Lavaux
5 // Modified by: Vadim Zeitlin to check error codes, added Detach() method
8 // Copyright: (c) Guilhem Lavaux
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
27 #include "wx/process.h"
29 // ----------------------------------------------------------------------------
30 // event tables and such
31 // ----------------------------------------------------------------------------
33 wxDEFINE_EVENT( wxEVT_END_PROCESS
, wxProcessEvent
);
35 IMPLEMENT_DYNAMIC_CLASS(wxProcess
, wxEvtHandler
)
36 IMPLEMENT_DYNAMIC_CLASS(wxProcessEvent
, wxEvent
)
38 // ============================================================================
39 // wxProcess implementation
40 // ============================================================================
42 // ----------------------------------------------------------------------------
44 // ----------------------------------------------------------------------------
46 void wxProcess::Init(wxEvtHandler
*parent
, int id
, int flags
)
49 SetNextHandler(parent
);
53 m_priority
= wxPRIORITY_DEFAULT
;
54 m_redirect
= (flags
& wxPROCESS_REDIRECT
) != 0;
59 m_outputStream
= NULL
;
60 #endif // wxUSE_STREAMS
64 wxProcess
*wxProcess::Open(const wxString
& cmd
, int flags
)
66 wxASSERT_MSG( !(flags
& wxEXEC_SYNC
), wxT("wxEXEC_SYNC should not be used." ));
67 wxProcess
*process
= new wxProcess(wxPROCESS_REDIRECT
);
68 long pid
= wxExecute(cmd
, flags
, process
);
71 // couldn't launch the process
81 // ----------------------------------------------------------------------------
82 // wxProcess termination
83 // ----------------------------------------------------------------------------
85 wxProcess::~wxProcess()
90 delete m_outputStream
;
91 #endif // wxUSE_STREAMS
94 void wxProcess::OnTerminate(int pid
, int status
)
96 wxProcessEvent
event(m_id
, pid
, status
);
98 if ( !ProcessEvent(event
) )
100 //else: the object which processed the event is responsible for deleting
104 void wxProcess::Detach()
106 // we just detach from the next handler of the chain (i.e. our "parent" -- see ctor)
107 // not also from the previous handler like wxEvtHandler::Unlink() would do:
110 m_nextHandler
->SetPreviousHandler(m_previousHandler
);
112 m_nextHandler
= NULL
;
115 // ----------------------------------------------------------------------------
116 // process IO redirection
117 // ----------------------------------------------------------------------------
121 void wxProcess::SetPipeStreams(wxInputStream
*inputSstream
,
122 wxOutputStream
*outputStream
,
123 wxInputStream
*errorStream
)
125 m_inputStream
= inputSstream
;
126 m_errorStream
= errorStream
;
127 m_outputStream
= outputStream
;
130 bool wxProcess::IsInputOpened() const
132 return m_inputStream
&& m_inputStream
->GetLastError() != wxSTREAM_EOF
;
135 bool wxProcess::IsInputAvailable() const
137 return m_inputStream
&& m_inputStream
->CanRead();
140 bool wxProcess::IsErrorAvailable() const
142 return m_errorStream
&& m_errorStream
->CanRead();
145 #endif // wxUSE_STREAMS
147 // ----------------------------------------------------------------------------
149 // ----------------------------------------------------------------------------
152 wxKillError
wxProcess::Kill(int pid
, wxSignal sig
, int flags
)
155 (void)wxKill(pid
, sig
, &rc
, flags
);
161 bool wxProcess::Exists(int pid
)
163 switch ( Kill(pid
, wxSIGNONE
) )
166 case wxKILL_ACCESS_DENIED
:
171 case wxKILL_BAD_SIGNAL
:
172 wxFAIL_MSG( wxT("unexpected wxProcess::Kill() return code") );
175 case wxKILL_NO_PROCESS
:
180 void wxProcess::SetPriority(unsigned priority
)
182 wxCHECK_RET( priority
<= wxPRIORITY_MAX
,
183 wxS("Invalid process priority value.") );
185 m_priority
= priority
;