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