]> git.saurik.com Git - wxWidgets.git/blob - src/unix/evtloopunix.cpp
b082984019b700bf980ce856672b7defb442d972
[wxWidgets.git] / src / unix / evtloopunix.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/unix/evtloopunix.cpp
3 // Purpose: wxEventLoop implementation
4 // Author: Lukasz Michalski (lm@zork.pl)
5 // Created: 2007-05-07
6 // RCS-ID: $Id$
7 // Copyright: (c) 2006 Zork Lukasz Michalski
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
10
11 // ===========================================================================
12 // declarations
13 // ===========================================================================
14
15 // ---------------------------------------------------------------------------
16 // headers
17 // ---------------------------------------------------------------------------
18
19 // For compilers that support precompilation, includes "wx.h".
20 #include "wx/wxprec.h"
21
22 #if wxUSE_CONSOLE_EVENTLOOP
23
24 #include "wx/evtloop.h"
25
26 #ifndef WX_PRECOMP
27 #include "wx/app.h"
28 #include "wx/log.h"
29 #endif
30
31 #include <errno.h>
32 #include "wx/apptrait.h"
33 #include "wx/thread.h"
34 #include "wx/module.h"
35 #include "wx/unix/pipe.h"
36 #include "wx/unix/private/timer.h"
37 #include "wx/unix/private/epolldispatcher.h"
38 #include "wx/private/selectdispatcher.h"
39
40 #define TRACE_EVENTS wxT("events")
41
42 // ===========================================================================
43 // wxEventLoop::PipeIOHandler implementation
44 // ===========================================================================
45
46 namespace wxPrivate
47 {
48
49 // pipe used for wake up messages: when a child thread wants to wake up
50 // the event loop in the main thread it writes to this pipe
51 class PipeIOHandler : public wxFDIOHandler
52 {
53 public:
54 // default ctor does nothing, call Create() to really initialize the
55 // object
56 PipeIOHandler() { }
57
58 bool Create();
59
60 // this method can be, and normally is, called from another thread
61 void WakeUp();
62
63 int GetReadFd() { return m_pipe[wxPipe::Read]; }
64
65 // implement wxFDIOHandler pure virtual methods
66 virtual void OnReadWaiting();
67 virtual void OnWriteWaiting() { }
68 virtual void OnExceptionWaiting() { }
69
70 private:
71 wxPipe m_pipe;
72 };
73
74 // ----------------------------------------------------------------------------
75 // initialization
76 // ----------------------------------------------------------------------------
77
78 bool PipeIOHandler::Create()
79 {
80 if ( !m_pipe.Create() )
81 {
82 wxLogError(_("Failed to create wake up pipe used by event loop."));
83 return false;
84 }
85
86 const int fdRead = GetReadFd();
87
88 int flags = fcntl(fdRead, F_GETFL, 0);
89 if ( flags == -1 || fcntl(fdRead, F_SETFL, flags | O_NONBLOCK) == -1 )
90 {
91 wxLogSysError(_("Failed to switch wake up pipe to non-blocking mode"));
92 return false;
93 }
94
95 wxLogTrace(TRACE_EVENTS, wxT("Wake up pipe (%d, %d) created"),
96 fdRead, m_pipe[wxPipe::Write]);
97
98 return true;
99 }
100
101 // ----------------------------------------------------------------------------
102 // wakeup handling
103 // ----------------------------------------------------------------------------
104
105 void PipeIOHandler::WakeUp()
106 {
107 if ( write(m_pipe[wxPipe::Write], "s", 1) != 1 )
108 {
109 // don't use wxLog here, we can be in another thread and this could
110 // result in dead locks
111 perror("write(wake up pipe)");
112 }
113 }
114
115 void PipeIOHandler::OnReadWaiting()
116 {
117 // got wakeup from child thread: read all data available in pipe just to
118 // make it empty (even though we write one byte at a time from WakeUp(),
119 // it could have been called several times)
120 char buf[4];
121 for ( ;; )
122 {
123 const int size = read(GetReadFd(), buf, WXSIZEOF(buf));
124
125 if ( size == 0 || (size == -1 && (errno == EAGAIN || errno == EINTR)) )
126 {
127 // nothing left in the pipe (EAGAIN is expected for an FD with
128 // O_NONBLOCK)
129 break;
130 }
131
132 if ( size == -1 )
133 {
134 wxLogSysError(_("Failed to read from wake-up pipe"));
135
136 break;
137 }
138 }
139
140 // writing to the wake up pipe will make wxConsoleEventLoop return from
141 // wxFDIODispatcher::Dispatch() it might be currently blocking in, nothing
142 // else needs to be done
143 }
144
145 } // namespace wxPrivate
146
147 // ===========================================================================
148 // wxEventLoop implementation
149 // ===========================================================================
150
151 //-----------------------------------------------------------------------------
152 // initialization
153 //-----------------------------------------------------------------------------
154
155 wxConsoleEventLoop::wxConsoleEventLoop()
156 {
157 m_wakeupPipe = new wxPrivate::PipeIOHandler();
158 if ( !m_wakeupPipe->Create() )
159 {
160 wxDELETE(m_wakeupPipe);
161 m_dispatcher = NULL;
162 return;
163 }
164
165 m_dispatcher = wxFDIODispatcher::Get();
166 if ( !m_dispatcher )
167 return;
168
169 m_dispatcher->RegisterFD
170 (
171 m_wakeupPipe->GetReadFd(),
172 m_wakeupPipe,
173 wxFDIO_INPUT
174 );
175 }
176
177 wxConsoleEventLoop::~wxConsoleEventLoop()
178 {
179 if ( m_wakeupPipe )
180 {
181 if ( m_dispatcher )
182 {
183 m_dispatcher->UnregisterFD(m_wakeupPipe->GetReadFd());
184 }
185
186 delete m_wakeupPipe;
187 }
188 }
189
190 //-----------------------------------------------------------------------------
191 // adding & removing sources
192 //-----------------------------------------------------------------------------
193
194 #if wxUSE_EVENTLOOP_SOURCE
195
196 // This class is a temporary bridge between event loop sources and
197 // FDIODispatcher. It is going to be removed soon, when all subject interfaces
198 // are modified
199 class wxFDIOEventLoopSourceHandler : public wxFDIOHandler
200 {
201 public:
202 wxFDIOEventLoopSourceHandler(wxEventLoopSourceHandler* handler) :
203 m_impl(handler) { }
204
205 virtual void OnReadWaiting()
206 {
207 m_impl->OnReadWaiting();
208 }
209 virtual void OnWriteWaiting()
210 {
211 m_impl->OnWriteWaiting();
212 }
213
214 virtual void OnExceptionWaiting()
215 {
216 m_impl->OnExceptionWaiting();
217 }
218
219 protected:
220 wxEventLoopSourceHandler* m_impl;
221 };
222
223 bool wxConsoleEventLoop::DoAddSource(wxAbstractEventLoopSource* src)
224 {
225 Source* source = dynamic_cast<Source*>(src);
226 wxCHECK_MSG( source, false, "Invalid source type" );
227
228 wxLogTrace(wxTRACE_EVT_SOURCE,
229 "wxConsoleEventLoop::AddSource() source=%d",
230 source->GetResource());
231
232 // translating into wxFDIOHandler
233 // XXX this is a memory leak of course, but this is really temporary, so
234 // we are not creating another map of handlers
235 wxFDIOHandler* h = new wxFDIOEventLoopSourceHandler(source->GetHandler());
236
237 return m_dispatcher->RegisterFD(source->GetResource(), h,
238 source->GetFlags());
239 }
240
241 bool wxConsoleEventLoop::DoRemoveSource(wxAbstractEventLoopSource* src)
242 {
243 Source* source = dynamic_cast<Source*>(src);
244 wxCHECK_MSG( source, false, "Invalid source type" );
245
246 wxLogTrace(wxTRACE_EVT_SOURCE,
247 "wxConsoleEventLoop::RemoveSource() source=%d",
248 source->GetResource());
249
250 return m_dispatcher->UnregisterFD(source->GetResource());
251 }
252 #endif
253
254 //-----------------------------------------------------------------------------
255 // events dispatch and loop handling
256 //-----------------------------------------------------------------------------
257
258 bool wxConsoleEventLoop::Pending() const
259 {
260 if ( m_dispatcher->HasPending() )
261 return true;
262
263 #if wxUSE_TIMER
264 wxUsecClock_t nextTimer;
265 if ( wxTimerScheduler::Get().GetNext(&nextTimer) &&
266 !wxMilliClockToLong(nextTimer) )
267 return true;
268 #endif // wxUSE_TIMER
269
270 return false;
271 }
272
273 bool wxConsoleEventLoop::Dispatch()
274 {
275 DispatchTimeout(static_cast<unsigned long>(
276 wxFDIODispatcher::TIMEOUT_INFINITE));
277
278 return true;
279 }
280
281 int wxConsoleEventLoop::DispatchTimeout(unsigned long timeout)
282 {
283 #if wxUSE_TIMER
284 // check if we need to decrease the timeout to account for a timer
285 wxUsecClock_t nextTimer;
286 if ( wxTimerScheduler::Get().GetNext(&nextTimer) )
287 {
288 unsigned long timeUntilNextTimer = wxMilliClockToLong(nextTimer / 1000);
289 if ( timeUntilNextTimer < timeout )
290 timeout = timeUntilNextTimer;
291 }
292 #endif // wxUSE_TIMER
293
294 bool hadEvent = m_dispatcher->Dispatch(timeout) > 0;
295
296 #if wxUSE_TIMER
297 if ( wxTimerScheduler::Get().NotifyExpired() )
298 hadEvent = true;
299 #endif // wxUSE_TIMER
300
301 return hadEvent ? 1 : -1;
302 }
303
304 void wxConsoleEventLoop::WakeUp()
305 {
306 m_wakeupPipe->WakeUp();
307 }
308
309 void wxConsoleEventLoop::OnNextIteration()
310 {
311 // call the signal handlers for any signals we caught recently
312 wxTheApp->CheckSignal();
313 }
314
315
316 wxEventLoopBase *wxConsoleAppTraits::CreateEventLoop()
317 {
318 return new wxEventLoop();
319 }
320
321 #endif // wxUSE_CONSOLE_EVENTLOOP