]> git.saurik.com Git - wxWidgets.git/blame - src/unix/evtloopunix.cpp
Move code that does not need realized GdkWinow out of realize handler
[wxWidgets.git] / src / unix / evtloopunix.cpp
CommitLineData
b46b1d59
VZ
1/////////////////////////////////////////////////////////////////////////////
2// Name: src/unix/evtloopunix.cpp
3// Purpose: wxEventLoop implementation
4// Author: Lukasz Michalski (lm@zork.pl)
5// Created: 2007-05-07
b46b1d59 6// Copyright: (c) 2006 Zork Lukasz Michalski
821d856a
VZ
7// (c) 2009, 2013 Vadim Zeitlin <vadim@wxwidgets.org>
8// (c) 2013 Rob Bresalier
b46b1d59
VZ
9// Licence: wxWindows licence
10/////////////////////////////////////////////////////////////////////////////
11
12// ===========================================================================
13// declarations
14// ===========================================================================
15
16// ---------------------------------------------------------------------------
17// headers
18// ---------------------------------------------------------------------------
19
20// For compilers that support precompilation, includes "wx.h".
21#include "wx/wxprec.h"
22
a1873279 23#if wxUSE_CONSOLE_EVENTLOOP
b46b1d59 24
1e04d2bf
PC
25#include "wx/evtloop.h"
26
b46b1d59
VZ
27#ifndef WX_PRECOMP
28 #include "wx/app.h"
29 #include "wx/log.h"
30#endif
31
37ab9399 32#include "wx/apptrait.h"
5cd99866 33#include "wx/scopedptr.h"
b46b1d59
VZ
34#include "wx/thread.h"
35#include "wx/module.h"
ba752031 36#include "wx/unix/private/timer.h"
b46b1d59 37#include "wx/unix/private/epolldispatcher.h"
2ccfebab 38#include "wx/unix/private/wakeuppipe.h"
b46b1d59 39#include "wx/private/selectdispatcher.h"
71e9885b 40#include "wx/private/eventloopsourcesmanager.h"
5e382463 41#include "wx/private/fdioeventloopsourcehandler.h"
71e9885b 42#include "wx/private/eventloopsourcesmanager.h"
b46b1d59 43
5cd99866
VZ
44#if wxUSE_EVENTLOOP_SOURCE
45 #include "wx/evtloopsrc.h"
46#endif // wxUSE_EVENTLOOP_SOURCE
47
b46b1d59
VZ
48// ===========================================================================
49// wxEventLoop implementation
50// ===========================================================================
51
52//-----------------------------------------------------------------------------
53// initialization
54//-----------------------------------------------------------------------------
55
56wxConsoleEventLoop::wxConsoleEventLoop()
57{
39bc0168
VZ
58 // Be pessimistic initially and assume that we failed to initialize.
59 m_dispatcher = NULL;
60 m_wakeupPipe = NULL;
61 m_wakeupSource = NULL;
62
63 // Create the pipe.
64 wxScopedPtr<wxWakeUpPipeMT> wakeupPipe(new wxWakeUpPipeMT);
65 const int pipeFD = wakeupPipe->GetReadFd();
2ccfebab 66 if ( pipeFD == wxPipe::INVALID_FD )
b46b1d59 67 return;
b46b1d59 68
39bc0168
VZ
69 // And start monitoring it in our event loop.
70 m_wakeupSource = wxEventLoopBase::AddSourceForFD
71 (
72 pipeFD,
73 wakeupPipe.get(),
74 wxFDIO_INPUT
75 );
76
77 if ( !m_wakeupSource )
5e1eac14 78 return;
b46b1d59 79
39bc0168
VZ
80 // This is a bit ugly but we know that AddSourceForFD() used the currently
81 // active dispatcher to register this source, so use the same one for our
82 // other operations. Of course, currently the dispatcher returned by
83 // wxFDIODispatcher::Get() is always the same one anyhow so it doesn't
84 // really matter, but if we started returning different things later, it
85 // would.
86 m_dispatcher = wxFDIODispatcher::Get();
87
88 m_wakeupPipe = wakeupPipe.release();
3ef595d5 89}
b46b1d59 90
3f8cdda4
VS
91wxConsoleEventLoop::~wxConsoleEventLoop()
92{
c8299fa8
VZ
93 if ( m_wakeupPipe )
94 {
39bc0168 95 delete m_wakeupSource;
c8299fa8
VZ
96
97 delete m_wakeupPipe;
98 }
3f8cdda4
VS
99}
100
6b8ef0b3
VZ
101//-----------------------------------------------------------------------------
102// adding & removing sources
103//-----------------------------------------------------------------------------
104
105#if wxUSE_EVENTLOOP_SOURCE
106
71e9885b 107class wxConsoleEventLoopSourcesManager : public wxEventLoopSourcesManagerBase
6b8ef0b3 108{
71e9885b
VZ
109public:
110 wxEventLoopSource* AddSourceForFD( int fd,
111 wxEventLoopSourceHandler *handler,
112 int flags)
113 {
114 wxCHECK_MSG( fd != -1, NULL, "can't monitor invalid fd" );
6b8ef0b3 115
71e9885b
VZ
116 wxLogTrace(wxTRACE_EVT_SOURCE,
117 "Adding event loop source for fd=%d", fd);
118
119 // we need a bridge to wxFDIODispatcher
120 //
121 // TODO: refactor the code so that only wxEventLoopSourceHandler is used
122 wxScopedPtr<wxFDIOHandler>
123 fdioHandler(new wxFDIOEventLoopSourceHandler(handler));
6b8ef0b3 124
71e9885b
VZ
125 if ( !wxFDIODispatcher::Get()->RegisterFD(fd, fdioHandler.get(), flags) )
126 return NULL;
6b8ef0b3 127
71e9885b
VZ
128 return new wxUnixEventLoopSource(wxFDIODispatcher::Get(), fdioHandler.release(),
129 fd, handler, flags);
130 }
131};
132
133wxEventLoopSourcesManagerBase* wxAppTraits::GetEventLoopSourcesManager()
134{
135 static wxConsoleEventLoopSourcesManager s_eventLoopSourcesManager;
5cd99866 136
71e9885b 137 return &s_eventLoopSourcesManager;
6b8ef0b3
VZ
138}
139
5cd99866 140wxUnixEventLoopSource::~wxUnixEventLoopSource()
6b8ef0b3 141{
6b8ef0b3 142 wxLogTrace(wxTRACE_EVT_SOURCE,
5cd99866 143 "Removing event loop source for fd=%d", m_fd);
6b8ef0b3 144
5cd99866
VZ
145 m_dispatcher->UnregisterFD(m_fd);
146
147 delete m_fdioHandler;
6b8ef0b3 148}
5cd99866
VZ
149
150#endif // wxUSE_EVENTLOOP_SOURCE
6b8ef0b3 151
b46b1d59
VZ
152//-----------------------------------------------------------------------------
153// events dispatch and loop handling
154//-----------------------------------------------------------------------------
155
156bool wxConsoleEventLoop::Pending() const
157{
a12698ab
VZ
158 if ( m_dispatcher->HasPending() )
159 return true;
160
161#if wxUSE_TIMER
162 wxUsecClock_t nextTimer;
163 if ( wxTimerScheduler::Get().GetNext(&nextTimer) &&
164 !wxMilliClockToLong(nextTimer) )
165 return true;
166#endif // wxUSE_TIMER
167
168 return false;
b46b1d59
VZ
169}
170
171bool wxConsoleEventLoop::Dispatch()
b46b1d59 172{
c22ace4d
VZ
173 DispatchTimeout(static_cast<unsigned long>(
174 wxFDIODispatcher::TIMEOUT_INFINITE));
b46b1d59 175
9af42efd
VZ
176 return true;
177}
178
179int wxConsoleEventLoop::DispatchTimeout(unsigned long timeout)
180{
b46b1d59 181#if wxUSE_TIMER
9af42efd 182 // check if we need to decrease the timeout to account for a timer
b46b1d59
VZ
183 wxUsecClock_t nextTimer;
184 if ( wxTimerScheduler::Get().GetNext(&nextTimer) )
185 {
9af42efd
VZ
186 unsigned long timeUntilNextTimer = wxMilliClockToLong(nextTimer / 1000);
187 if ( timeUntilNextTimer < timeout )
188 timeout = timeUntilNextTimer;
b46b1d59 189 }
b46b1d59 190#endif // wxUSE_TIMER
b46b1d59 191
a12698ab 192 bool hadEvent = m_dispatcher->Dispatch(timeout) > 0;
b46b1d59
VZ
193
194#if wxUSE_TIMER
9af42efd
VZ
195 if ( wxTimerScheduler::Get().NotifyExpired() )
196 hadEvent = true;
197#endif // wxUSE_TIMER
b46b1d59 198
9af42efd 199 return hadEvent ? 1 : -1;
438febca
VZ
200}
201
202void wxConsoleEventLoop::WakeUp()
203{
3f8cdda4 204 m_wakeupPipe->WakeUp();
438febca
VZ
205}
206
207void wxConsoleEventLoop::OnNextIteration()
208{
b46b1d59
VZ
209 // call the signal handlers for any signals we caught recently
210 wxTheApp->CheckSignal();
211}
212
37ab9399
SN
213
214wxEventLoopBase *wxConsoleAppTraits::CreateEventLoop()
215{
216 return new wxEventLoop();
217}
218
a1873279 219#endif // wxUSE_CONSOLE_EVENTLOOP