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