]>
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 #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 //this code should not be compiled when GUI is defined
39 //(monolithic build issue)
42 // ===========================================================================
43 // wxEventLoop::PipeIOHandler implementation
44 // ===========================================================================
46 // ----------------------------------------------------------------------------
48 // ----------------------------------------------------------------------------
50 bool wxConsoleEventLoop::PipeIOHandler::Create()
52 if ( !m_pipe
.Create() )
54 wxLogError(_("Failed to create wake up pipe used by event loop."));
58 const int fdRead
= GetReadFd();
60 int flags
= fcntl(fdRead
, F_GETFL
, 0);
61 if ( flags
== -1 || fcntl(fdRead
, F_SETFL
, flags
| O_NONBLOCK
) == -1 )
63 wxLogSysError(_("Failed to switch wake up pipe to non-blocking mode"));
67 wxLogTrace(TRACE_EVENTS
, wxT("Wake up pipe (%d, %d) created"),
68 fdRead
, m_pipe
[wxPipe::Write
]);
73 // ----------------------------------------------------------------------------
75 // ----------------------------------------------------------------------------
77 void wxConsoleEventLoop::PipeIOHandler::WakeUp()
79 if ( write(m_pipe
[wxPipe::Write
], "s", 1) != 1 )
81 // don't use wxLog here, we can be in another thread and this could
82 // result in dead locks
83 perror("write(wake up pipe)");
87 void wxConsoleEventLoop::PipeIOHandler::OnReadWaiting()
89 // got wakeup from child thread: read all data available in pipe just to
90 // make it empty (evevn though we write one byte at a time from WakeUp(),
91 // it could have been called several times)
95 const int size
= read(GetReadFd(), buf
, WXSIZEOF(buf
));
97 if ( size
== 0 || (size
== -1 && errno
== EAGAIN
) )
99 // nothing left in the pipe (EAGAIN is expected for an FD with
106 wxLogSysError(_("Failed to read from wake-up pipe"));
112 wxTheApp
->ProcessPendingEvents();
115 // ===========================================================================
116 // wxEventLoop implementation
117 // ===========================================================================
119 //-----------------------------------------------------------------------------
121 //-----------------------------------------------------------------------------
123 wxConsoleEventLoop::wxConsoleEventLoop()
125 if ( !m_wakeupPipe
.Create() )
131 #ifdef HAVE_SYS_EPOLL_H
132 m_dispatcher
= wxEpollDispatcher::Get();
134 #endif // HAVE_SYS_EPOLL_H
136 m_dispatcher
= wxSelectDispatcher::Get();
139 wxCHECK_RET( m_dispatcher
, _T("failed to create IO dispatcher") );
141 m_dispatcher
->RegisterFD
143 m_wakeupPipe
.GetReadFd(),
149 //-----------------------------------------------------------------------------
150 // events dispatch and loop handling
151 //-----------------------------------------------------------------------------
153 bool wxConsoleEventLoop::Pending() const
155 return wxTheApp
->HasPendingEvents();
158 bool wxConsoleEventLoop::Dispatch()
160 wxTheApp
->ProcessPendingEvents();
164 void wxConsoleEventLoop::WakeUp()
166 m_wakeupPipe
.WakeUp();
169 void wxConsoleEventLoop::OnNextIteration()
171 // calculate the timeout until the next timer expiration
175 wxUsecClock_t nextTimer
;
176 if ( wxTimerScheduler::Get().GetNext(&nextTimer
) )
179 timeout
= (nextTimer
/ 1000).ToLong();
181 else // no timers, we can block forever
182 #endif // wxUSE_TIMER
184 timeout
= wxFDIODispatcher::TIMEOUT_INFINITE
;
187 m_dispatcher
->RunLoop(timeout
);
190 wxTimerScheduler::Get().NotifyExpired();
193 // call the signal handlers for any signals we caught recently
194 wxTheApp
->CheckSignal();