]>
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/evtloop.h"
31 #include "wx/thread.h"
32 #include "wx/module.h"
33 #include "wx/unix/private/timer.h"
34 #include "wx/unix/private/epolldispatcher.h"
35 #include "wx/private/selectdispatcher.h"
37 #define TRACE_EVENTS _T("events")
39 // ===========================================================================
40 // wxEventLoop::PipeIOHandler implementation
41 // ===========================================================================
43 // ----------------------------------------------------------------------------
45 // ----------------------------------------------------------------------------
47 bool wxConsoleEventLoop::PipeIOHandler::Create()
49 if ( !m_pipe
.Create() )
51 wxLogError(_("Failed to create wake up pipe used by event loop."));
55 const int fdRead
= GetReadFd();
57 int flags
= fcntl(fdRead
, F_GETFL
, 0);
58 if ( flags
== -1 || fcntl(fdRead
, F_SETFL
, flags
| O_NONBLOCK
) == -1 )
60 wxLogSysError(_("Failed to switch wake up pipe to non-blocking mode"));
64 wxLogTrace(TRACE_EVENTS
, wxT("Wake up pipe (%d, %d) created"),
65 fdRead
, m_pipe
[wxPipe::Write
]);
70 // ----------------------------------------------------------------------------
72 // ----------------------------------------------------------------------------
74 void wxConsoleEventLoop::PipeIOHandler::WakeUp()
76 if ( write(m_pipe
[wxPipe::Write
], "s", 1) != 1 )
78 // don't use wxLog here, we can be in another thread and this could
79 // result in dead locks
80 perror("write(wake up pipe)");
84 void wxConsoleEventLoop::PipeIOHandler::OnReadWaiting()
86 // got wakeup from child thread: read all data available in pipe just to
87 // make it empty (evevn though we write one byte at a time from WakeUp(),
88 // it could have been called several times)
92 const int size
= read(GetReadFd(), buf
, WXSIZEOF(buf
));
94 if ( size
== 0 || (size
== -1 && errno
== EAGAIN
) )
96 // nothing left in the pipe (EAGAIN is expected for an FD with
103 wxLogSysError(_("Failed to read from wake-up pipe"));
109 wxTheApp
->ProcessPendingEvents();
112 // ===========================================================================
113 // wxEventLoop implementation
114 // ===========================================================================
116 //-----------------------------------------------------------------------------
118 //-----------------------------------------------------------------------------
120 wxConsoleEventLoop::wxConsoleEventLoop()
122 if ( !m_wakeupPipe
.Create() )
128 m_dispatcher
= wxFDIODispatcher::Get();
132 m_dispatcher
->RegisterFD
134 m_wakeupPipe
.GetReadFd(),
140 //-----------------------------------------------------------------------------
141 // events dispatch and loop handling
142 //-----------------------------------------------------------------------------
144 bool wxConsoleEventLoop::Pending() const
146 return wxTheApp
->HasPendingEvents();
149 bool wxConsoleEventLoop::Dispatch()
151 wxTheApp
->ProcessPendingEvents();
155 void wxConsoleEventLoop::WakeUp()
157 m_wakeupPipe
.WakeUp();
160 void wxConsoleEventLoop::OnNextIteration()
162 // calculate the timeout until the next timer expiration
166 wxUsecClock_t nextTimer
;
167 if ( wxTimerScheduler::Get().GetNext(&nextTimer
) )
170 timeout
= (nextTimer
/ 1000).ToLong();
172 else // no timers, we can block forever
173 #endif // wxUSE_TIMER
175 timeout
= wxFDIODispatcher::TIMEOUT_INFINITE
;
178 m_dispatcher
->Dispatch(timeout
);
181 wxTimerScheduler::Get().NotifyExpired();
184 // call the signal handlers for any signals we caught recently
185 wxTheApp
->CheckSignal();
188 #endif // wxUSE_CONSOLE_EVENTLOOP