]>
git.saurik.com Git - wxWidgets.git/blob - src/unix/evtloopunix.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/unix/evtloopunix.cpp
3 // Purpose: wxEventLoop implementation
4 // Author: Lukasz Michalski (lm@zork.pl)
7 // Copyright: (c) 2006 Zork Lukasz Michalski
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 // ===========================================================================
13 // ===========================================================================
15 // ---------------------------------------------------------------------------
17 // ---------------------------------------------------------------------------
19 // For compilers that support precompilation, includes "wx.h".
20 #include "wx/wxprec.h"
22 #if wxUSE_CONSOLE_EVENTLOOP
30 #include "wx/apptrait.h"
31 #include "wx/evtloop.h"
32 #include "wx/thread.h"
33 #include "wx/module.h"
34 #include "wx/unix/private/timer.h"
35 #include "wx/unix/private/epolldispatcher.h"
36 #include "wx/private/selectdispatcher.h"
38 #define TRACE_EVENTS _T("events")
40 // ===========================================================================
41 // wxEventLoop::PipeIOHandler implementation
42 // ===========================================================================
44 // ----------------------------------------------------------------------------
46 // ----------------------------------------------------------------------------
48 bool wxConsoleEventLoop::PipeIOHandler::Create()
50 if ( !m_pipe
.Create() )
52 wxLogError(_("Failed to create wake up pipe used by event loop."));
56 const int fdRead
= GetReadFd();
58 int flags
= fcntl(fdRead
, F_GETFL
, 0);
59 if ( flags
== -1 || fcntl(fdRead
, F_SETFL
, flags
| O_NONBLOCK
) == -1 )
61 wxLogSysError(_("Failed to switch wake up pipe to non-blocking mode"));
65 wxLogTrace(TRACE_EVENTS
, wxT("Wake up pipe (%d, %d) created"),
66 fdRead
, m_pipe
[wxPipe::Write
]);
71 // ----------------------------------------------------------------------------
73 // ----------------------------------------------------------------------------
75 void wxConsoleEventLoop::PipeIOHandler::WakeUp()
77 if ( write(m_pipe
[wxPipe::Write
], "s", 1) != 1 )
79 // don't use wxLog here, we can be in another thread and this could
80 // result in dead locks
81 perror("write(wake up pipe)");
85 void wxConsoleEventLoop::PipeIOHandler::OnReadWaiting()
87 // got wakeup from child thread: read all data available in pipe just to
88 // make it empty (even though we write one byte at a time from WakeUp(),
89 // it could have been called several times)
93 const int size
= read(GetReadFd(), buf
, WXSIZEOF(buf
));
95 if ( size
== 0 || (size
== -1 && errno
== EAGAIN
) )
97 // nothing left in the pipe (EAGAIN is expected for an FD with
104 wxLogSysError(_("Failed to read from wake-up pipe"));
110 // writing to the wake up pipe will make wxConsoleEventLoop return from
111 // wxFDIODispatcher::Dispatch() it might be currently blocking in, nothing
112 // else needs to be done
115 // ===========================================================================
116 // wxEventLoop implementation
117 // ===========================================================================
119 //-----------------------------------------------------------------------------
121 //-----------------------------------------------------------------------------
123 wxConsoleEventLoop::wxConsoleEventLoop()
125 if ( !m_wakeupPipe
.Create() )
131 m_dispatcher
= wxFDIODispatcher::Get();
135 m_dispatcher
->RegisterFD
137 m_wakeupPipe
.GetReadFd(),
143 //-----------------------------------------------------------------------------
144 // events dispatch and loop handling
145 //-----------------------------------------------------------------------------
147 bool wxConsoleEventLoop::Pending() const
149 if ( m_dispatcher
->HasPending() )
153 wxUsecClock_t nextTimer
;
154 if ( wxTimerScheduler::Get().GetNext(&nextTimer
) &&
155 !wxMilliClockToLong(nextTimer
) )
157 #endif // wxUSE_TIMER
162 bool wxConsoleEventLoop::Dispatch()
164 DispatchTimeout(wxFDIODispatcher::TIMEOUT_INFINITE
);
169 int wxConsoleEventLoop::DispatchTimeout(unsigned long timeout
)
172 // check if we need to decrease the timeout to account for a timer
173 wxUsecClock_t nextTimer
;
174 if ( wxTimerScheduler::Get().GetNext(&nextTimer
) )
176 unsigned long timeUntilNextTimer
= wxMilliClockToLong(nextTimer
/ 1000);
177 if ( timeUntilNextTimer
< timeout
)
178 timeout
= timeUntilNextTimer
;
180 #endif // wxUSE_TIMER
182 bool hadEvent
= m_dispatcher
->Dispatch(timeout
) > 0;
185 if ( wxTimerScheduler::Get().NotifyExpired() )
187 #endif // wxUSE_TIMER
189 return hadEvent
? 1 : -1;
192 void wxConsoleEventLoop::WakeUp()
194 m_wakeupPipe
.WakeUp();
197 void wxConsoleEventLoop::OnNextIteration()
199 // call the signal handlers for any signals we caught recently
200 wxTheApp
->CheckSignal();
204 wxEventLoopBase
*wxConsoleAppTraits::CreateEventLoop()
206 return new wxEventLoop();
209 #endif // wxUSE_CONSOLE_EVENTLOOP