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 #include "wx/evtloop.h"
30 #include "wx/thread.h"
31 #include "wx/module.h"
32 #include "wx/generic/private/timer.h"
33 #include "wx/unix/private/epolldispatcher.h"
34 #include "wx/private/selectdispatcher.h"
36 #define TRACE_EVENTS _T("events")
38 // ===========================================================================
39 // wxEventLoop::PipeIOHandler implementation
40 // ===========================================================================
42 // ----------------------------------------------------------------------------
44 // ----------------------------------------------------------------------------
46 bool wxConsoleEventLoop::PipeIOHandler::Create()
48 if ( !m_pipe
.Create() )
50 wxLogError(_("Failed to create wake up pipe used by event loop."));
54 const int fdRead
= GetReadFd();
56 int flags
= fcntl(fdRead
, F_GETFL
, 0);
57 if ( flags
== -1 || fcntl(fdRead
, F_SETFL
, flags
| O_NONBLOCK
) == -1 )
59 wxLogSysError(_("Failed to switch wake up pipe to non-blocking mode"));
63 wxLogTrace(TRACE_EVENTS
, wxT("Wake up pipe (%d, %d) created"),
64 fdRead
, m_pipe
[wxPipe::Write
]);
69 // ----------------------------------------------------------------------------
71 // ----------------------------------------------------------------------------
73 void wxConsoleEventLoop::PipeIOHandler::WakeUp()
75 if ( write(m_pipe
[wxPipe::Write
], "s", 1) != 1 )
77 // don't use wxLog here, we can be in another thread and this could
78 // result in dead locks
79 perror("write(wake up pipe)");
83 void wxConsoleEventLoop::PipeIOHandler::OnReadWaiting()
85 // got wakeup from child thread: read all data available in pipe just to
86 // make it empty (evevn though we write one byte at a time from WakeUp(),
87 // it could have been called several times)
91 const int size
= read(GetReadFd(), buf
, WXSIZEOF(buf
));
93 if ( size
== 0 || (size
== -1 && errno
== EAGAIN
) )
95 // nothing left in the pipe (EAGAIN is expected for an FD with
102 wxLogSysError(_("Failed to read from wake-up pipe"));
108 wxTheApp
->ProcessPendingEvents();
111 // ===========================================================================
112 // wxEventLoop implementation
113 // ===========================================================================
115 //-----------------------------------------------------------------------------
117 //-----------------------------------------------------------------------------
119 wxConsoleEventLoop::wxConsoleEventLoop()
121 if ( !m_wakeupPipe
.Create() )
127 #ifdef HAVE_SYS_EPOLL_H
128 m_dispatcher
= wxEpollDispatcher::Get();
130 #endif // HAVE_SYS_EPOLL_H
132 m_dispatcher
= wxSelectDispatcher::Get();
135 wxCHECK_RET( m_dispatcher
, _T("failed to create IO dispatcher") );
137 m_dispatcher
->RegisterFD
139 m_wakeupPipe
.GetReadFd(),
145 //-----------------------------------------------------------------------------
146 // events dispatch and loop handling
147 //-----------------------------------------------------------------------------
149 bool wxConsoleEventLoop::Pending() const
151 return wxTheApp
->HasPendingEvents();
154 bool wxConsoleEventLoop::Dispatch()
156 wxTheApp
->ProcessPendingEvents();
160 void wxConsoleEventLoop::WakeUp()
162 m_wakeupPipe
.WakeUp();
165 void wxConsoleEventLoop::OnNextIteration()
167 // calculate the timeout until the next timer expiration
171 wxUsecClock_t nextTimer
;
172 if ( wxTimerScheduler::Get().GetNext(&nextTimer
) )
175 timeout
= (nextTimer
/ 1000).ToLong();
177 else // no timers, we can block forever
178 #endif // wxUSE_TIMER
180 timeout
= wxFDIODispatcher::TIMEOUT_INFINITE
;
183 m_dispatcher
->RunLoop(timeout
);
186 wxTimerScheduler::Get().NotifyExpired();
189 // call the signal handlers for any signals we caught recently
190 wxTheApp
->CheckSignal();