]> git.saurik.com Git - wxWidgets.git/blame - src/common/process.cpp
avoiding nesting dcs on the same window concurrently
[wxWidgets.git] / src / common / process.cpp
CommitLineData
ca7731b7
GL
1/////////////////////////////////////////////////////////////////////////////
2// Name: process.cpp
3// Purpose: Process termination classes
4// Author: Guilhem Lavaux
7e84f02d 5// Modified by: Vadim Zeitlin to check error codes, added Detach() method
ca7731b7
GL
6// Created: 24/06/98
7// RCS-ID: $Id$
8// Copyright: (c) Guilhem Lavaux
65571936 9// Licence: wxWindows licence
ca7731b7
GL
10/////////////////////////////////////////////////////////////////////////////
11
50567b69
VZ
12// ============================================================================
13// declarations
14// ============================================================================
15
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
19
ca7731b7
GL
20// For compilers that support precompilation, includes "wx.h".
21#include "wx/wxprec.h"
22
23#ifdef __BORLANDC__
5336ece4 24 #pragma hdrstop
ca7731b7
GL
25#endif
26
ca7731b7
GL
27#include "wx/process.h"
28
50567b69
VZ
29// ----------------------------------------------------------------------------
30// event tables and such
31// ----------------------------------------------------------------------------
32
2e4df4bf
VZ
33DEFINE_EVENT_TYPE(wxEVT_END_PROCESS)
34
eebb848a
KB
35IMPLEMENT_DYNAMIC_CLASS(wxProcess, wxEvtHandler)
36IMPLEMENT_DYNAMIC_CLASS(wxProcessEvent, wxEvent)
ca7731b7 37
50567b69
VZ
38// ============================================================================
39// wxProcess implementation
40// ============================================================================
41
42// ----------------------------------------------------------------------------
43// wxProcess creation
44// ----------------------------------------------------------------------------
45
da00a8bb 46void wxProcess::Init(wxEvtHandler *parent, int id, int flags)
ca7731b7 47{
cd6ce4a9 48 if ( parent )
7e84f02d 49 SetNextHandler(parent);
ca7731b7 50
8b33ae2d 51 m_id = id;
da00a8bb 52 m_redirect = (flags & wxPROCESS_REDIRECT) != 0;
f6bcfd97
BP
53
54#if wxUSE_STREAMS
cd6ce4a9 55 m_inputStream = NULL;
f6bcfd97 56 m_errorStream = NULL;
cd6ce4a9 57 m_outputStream = NULL;
f6bcfd97 58#endif // wxUSE_STREAMS
8b33ae2d
GL
59}
60
da00a8bb 61/* static */
e626d7c7 62wxProcess *wxProcess::Open(const wxString& cmd, int flags)
da00a8bb 63{
feb6cde4 64 wxASSERT_MSG( !(flags & wxEXEC_SYNC), wxT("wxEXEC_SYNC should not be used." ));
da00a8bb 65 wxProcess *process = new wxProcess(wxPROCESS_REDIRECT);
e626d7c7 66 if ( !wxExecute(cmd, flags, process) )
da00a8bb
VZ
67 {
68 // couldn't launch the process
69 delete process;
70 return NULL;
71 }
72
73 return process;
74}
75
50567b69
VZ
76// ----------------------------------------------------------------------------
77// wxProcess termination
78// ----------------------------------------------------------------------------
79
8b33ae2d
GL
80wxProcess::~wxProcess()
81{
f6bcfd97 82#if wxUSE_STREAMS
cd6ce4a9 83 delete m_inputStream;
f6bcfd97 84 delete m_errorStream;
cd6ce4a9 85 delete m_outputStream;
f6bcfd97 86#endif // wxUSE_STREAMS
ca7731b7
GL
87}
88
5336ece4 89void wxProcess::OnTerminate(int pid, int status)
81d66cf3 90{
7e84f02d 91 wxProcessEvent event(m_id, pid, status);
ca7731b7 92
7e84f02d
VZ
93 if ( !ProcessEvent(event) )
94 delete this;
95 //else: the object which processed the event is responsible for deleting
96 // us!
97}
98
99void wxProcess::Detach()
100{
101 SetNextHandler(NULL);
ca7731b7 102}
8b33ae2d 103
50567b69
VZ
104// ----------------------------------------------------------------------------
105// process IO redirection
106// ----------------------------------------------------------------------------
107
f6bcfd97
BP
108#if wxUSE_STREAMS
109
110void wxProcess::SetPipeStreams(wxInputStream *inputSstream,
111 wxOutputStream *outputStream,
112 wxInputStream *errorStream)
8b33ae2d 113{
f6bcfd97
BP
114 m_inputStream = inputSstream;
115 m_errorStream = errorStream;
116 m_outputStream = outputStream;
8b33ae2d
GL
117}
118
411165f3
VZ
119bool wxProcess::IsInputOpened() const
120{
2b5f62a0 121 return m_inputStream && m_inputStream->GetLastError() != wxSTREAM_EOF;
411165f3
VZ
122}
123
124bool wxProcess::IsInputAvailable() const
125{
2b5f62a0 126 return m_inputStream && m_inputStream->CanRead();
411165f3
VZ
127}
128
129bool wxProcess::IsErrorAvailable() const
130{
2b5f62a0 131 return m_errorStream && m_errorStream->CanRead();
411165f3
VZ
132}
133
f6bcfd97 134#endif // wxUSE_STREAMS
50567b69
VZ
135
136// ----------------------------------------------------------------------------
137// process killing
138// ----------------------------------------------------------------------------
139
140/* static */
e0f6b731 141wxKillError wxProcess::Kill(int pid, wxSignal sig, int flags)
50567b69
VZ
142{
143 wxKillError rc;
e0f6b731 144 (void)wxKill(pid, sig, &rc, flags);
50567b69
VZ
145
146 return rc;
147}
148
149/* static */
150bool wxProcess::Exists(int pid)
151{
15b663b4 152 switch ( Kill(pid, wxSIGNONE) )
50567b69
VZ
153 {
154 case wxKILL_OK:
155 case wxKILL_ACCESS_DENIED:
7e548f6b 156 return true;
50567b69
VZ
157
158 default:
159 case wxKILL_ERROR:
160 case wxKILL_BAD_SIGNAL:
161 wxFAIL_MSG( _T("unexpected wxProcess::Kill() return code") );
162 // fall through
163
164 case wxKILL_NO_PROCESS:
7e548f6b 165 return false;
50567b69
VZ
166 }
167}
168