]>
git.saurik.com Git - wxWidgets.git/blob - src/unix/evtloopunix.cpp
784241c6631f468be6f845b29c573c66414f38da
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
24 #include "wx/evtloop.h"
31 #include "wx/apptrait.h"
32 #include "wx/scopedptr.h"
33 #include "wx/thread.h"
34 #include "wx/module.h"
35 #include "wx/unix/private/timer.h"
36 #include "wx/unix/private/epolldispatcher.h"
37 #include "wx/unix/private/wakeuppipe.h"
38 #include "wx/private/selectdispatcher.h"
39 #include "wx/private/fdioeventloopsourcehandler.h"
41 #if wxUSE_EVENTLOOP_SOURCE
42 #include "wx/evtloopsrc.h"
43 #endif // wxUSE_EVENTLOOP_SOURCE
45 // ===========================================================================
46 // wxEventLoop implementation
47 // ===========================================================================
49 //-----------------------------------------------------------------------------
51 //-----------------------------------------------------------------------------
53 wxConsoleEventLoop::wxConsoleEventLoop()
55 m_wakeupPipe
= new wxWakeUpPipeMT
;
56 const int pipeFD
= m_wakeupPipe
->GetReadFd();
57 if ( pipeFD
== wxPipe::INVALID_FD
)
59 wxDELETE(m_wakeupPipe
);
64 m_dispatcher
= wxFDIODispatcher::Get();
68 m_dispatcher
->RegisterFD(pipeFD
, m_wakeupPipe
, wxFDIO_INPUT
);
71 wxConsoleEventLoop::~wxConsoleEventLoop()
77 m_dispatcher
->UnregisterFD(m_wakeupPipe
->GetReadFd());
84 //-----------------------------------------------------------------------------
85 // adding & removing sources
86 //-----------------------------------------------------------------------------
88 #if wxUSE_EVENTLOOP_SOURCE
91 wxConsoleEventLoop::AddSourceForFD(int fd
,
92 wxEventLoopSourceHandler
*handler
,
95 wxCHECK_MSG( fd
!= -1, NULL
, "can't monitor invalid fd" );
97 wxLogTrace(wxTRACE_EVT_SOURCE
,
98 "Adding event loop source for fd=%d", fd
);
100 // we need a bridge to wxFDIODispatcher
102 // TODO: refactor the code so that only wxEventLoopSourceHandler is used
103 wxScopedPtr
<wxFDIOHandler
>
104 fdioHandler(new wxFDIOEventLoopSourceHandler(handler
));
106 if ( !m_dispatcher
->RegisterFD(fd
, fdioHandler
.get(), flags
) )
109 return new wxUnixEventLoopSource(m_dispatcher
, fdioHandler
.release(),
113 wxUnixEventLoopSource::~wxUnixEventLoopSource()
115 wxLogTrace(wxTRACE_EVT_SOURCE
,
116 "Removing event loop source for fd=%d", m_fd
);
118 m_dispatcher
->UnregisterFD(m_fd
);
120 delete m_fdioHandler
;
123 #endif // wxUSE_EVENTLOOP_SOURCE
125 //-----------------------------------------------------------------------------
126 // events dispatch and loop handling
127 //-----------------------------------------------------------------------------
129 bool wxConsoleEventLoop::Pending() const
131 if ( m_dispatcher
->HasPending() )
135 wxUsecClock_t nextTimer
;
136 if ( wxTimerScheduler::Get().GetNext(&nextTimer
) &&
137 !wxMilliClockToLong(nextTimer
) )
139 #endif // wxUSE_TIMER
144 bool wxConsoleEventLoop::Dispatch()
146 DispatchTimeout(static_cast<unsigned long>(
147 wxFDIODispatcher::TIMEOUT_INFINITE
));
152 int wxConsoleEventLoop::DispatchTimeout(unsigned long timeout
)
155 // check if we need to decrease the timeout to account for a timer
156 wxUsecClock_t nextTimer
;
157 if ( wxTimerScheduler::Get().GetNext(&nextTimer
) )
159 unsigned long timeUntilNextTimer
= wxMilliClockToLong(nextTimer
/ 1000);
160 if ( timeUntilNextTimer
< timeout
)
161 timeout
= timeUntilNextTimer
;
163 #endif // wxUSE_TIMER
165 bool hadEvent
= m_dispatcher
->Dispatch(timeout
) > 0;
168 if ( wxTimerScheduler::Get().NotifyExpired() )
170 #endif // wxUSE_TIMER
172 return hadEvent
? 1 : -1;
175 void wxConsoleEventLoop::WakeUp()
177 m_wakeupPipe
->WakeUp();
180 void wxConsoleEventLoop::OnNextIteration()
182 // call the signal handlers for any signals we caught recently
183 wxTheApp
->CheckSignal();
187 wxEventLoopBase
*wxConsoleAppTraits::CreateEventLoop()
189 return new wxEventLoop();
192 #endif // wxUSE_CONSOLE_EVENTLOOP