]> git.saurik.com Git - wxWidgets.git/blob - src/common/process.cpp
Don't cache HDC used by wxPaintDCEx in wxMSW.
[wxWidgets.git] / 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
6 // Created: 24/06/98
7 // RCS-ID: $Id$
8 // Copyright: (c) Guilhem Lavaux
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
22
23 #ifdef __BORLANDC__
24 #pragma hdrstop
25 #endif
26
27 #include "wx/process.h"
28
29 // ----------------------------------------------------------------------------
30 // event tables and such
31 // ----------------------------------------------------------------------------
32
33 wxDEFINE_EVENT( wxEVT_END_PROCESS, wxProcessEvent );
34
35 IMPLEMENT_DYNAMIC_CLASS(wxProcess, wxEvtHandler)
36 IMPLEMENT_DYNAMIC_CLASS(wxProcessEvent, wxEvent)
37
38 // ============================================================================
39 // wxProcess implementation
40 // ============================================================================
41
42 // ----------------------------------------------------------------------------
43 // wxProcess creation
44 // ----------------------------------------------------------------------------
45
46 void wxProcess::Init(wxEvtHandler *parent, int id, int flags)
47 {
48 if ( parent )
49 SetNextHandler(parent);
50
51 m_id = id;
52 m_pid = 0;
53 m_priority = wxPRIORITY_DEFAULT;
54 m_redirect = (flags & wxPROCESS_REDIRECT) != 0;
55
56 #if wxUSE_STREAMS
57 m_inputStream = NULL;
58 m_errorStream = NULL;
59 m_outputStream = NULL;
60 #endif // wxUSE_STREAMS
61 }
62
63 /* static */
64 wxProcess *wxProcess::Open(const wxString& cmd, int flags)
65 {
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);
69 if( !pid )
70 {
71 // couldn't launch the process
72 delete process;
73 return NULL;
74 }
75
76 process->SetPid(pid);
77
78 return process;
79 }
80
81 // ----------------------------------------------------------------------------
82 // wxProcess termination
83 // ----------------------------------------------------------------------------
84
85 wxProcess::~wxProcess()
86 {
87 #if wxUSE_STREAMS
88 delete m_inputStream;
89 delete m_errorStream;
90 delete m_outputStream;
91 #endif // wxUSE_STREAMS
92 }
93
94 void wxProcess::OnTerminate(int pid, int status)
95 {
96 wxProcessEvent event(m_id, pid, status);
97
98 if ( !ProcessEvent(event) )
99 delete this;
100 //else: the object which processed the event is responsible for deleting
101 // us!
102 }
103
104 void wxProcess::Detach()
105 {
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:
108
109 if (m_nextHandler)
110 m_nextHandler->SetPreviousHandler(m_previousHandler);
111
112 m_nextHandler = NULL;
113 }
114
115 // ----------------------------------------------------------------------------
116 // process IO redirection
117 // ----------------------------------------------------------------------------
118
119 #if wxUSE_STREAMS
120
121 void wxProcess::SetPipeStreams(wxInputStream *inputSstream,
122 wxOutputStream *outputStream,
123 wxInputStream *errorStream)
124 {
125 m_inputStream = inputSstream;
126 m_errorStream = errorStream;
127 m_outputStream = outputStream;
128 }
129
130 bool wxProcess::IsInputOpened() const
131 {
132 return m_inputStream && m_inputStream->GetLastError() != wxSTREAM_EOF;
133 }
134
135 bool wxProcess::IsInputAvailable() const
136 {
137 return m_inputStream && m_inputStream->CanRead();
138 }
139
140 bool wxProcess::IsErrorAvailable() const
141 {
142 return m_errorStream && m_errorStream->CanRead();
143 }
144
145 #endif // wxUSE_STREAMS
146
147 // ----------------------------------------------------------------------------
148 // process killing
149 // ----------------------------------------------------------------------------
150
151 /* static */
152 wxKillError wxProcess::Kill(int pid, wxSignal sig, int flags)
153 {
154 wxKillError rc;
155 (void)wxKill(pid, sig, &rc, flags);
156
157 return rc;
158 }
159
160 /* static */
161 bool wxProcess::Exists(int pid)
162 {
163 switch ( Kill(pid, wxSIGNONE) )
164 {
165 case wxKILL_OK:
166 case wxKILL_ACCESS_DENIED:
167 return true;
168
169 default:
170 case wxKILL_ERROR:
171 case wxKILL_BAD_SIGNAL:
172 wxFAIL_MSG( wxT("unexpected wxProcess::Kill() return code") );
173 // fall through
174
175 case wxKILL_NO_PROCESS:
176 return false;
177 }
178 }
179
180 void wxProcess::SetPriority(unsigned priority)
181 {
182 wxCHECK_RET( priority <= wxPRIORITY_MAX,
183 wxS("Invalid process priority value.") );
184
185 m_priority = priority;
186 }