]>
Commit | Line | Data |
---|---|---|
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 | |
6 | // RCS-ID: $Id$ | |
7 | // Copyright: (c) 2006 Zork Lukasz Michalski | |
8 | // Licence: wxWindows licence | |
9 | ///////////////////////////////////////////////////////////////////////////// | |
10 | ||
11 | // =========================================================================== | |
12 | // declarations | |
13 | // =========================================================================== | |
14 | ||
15 | // --------------------------------------------------------------------------- | |
16 | // headers | |
17 | // --------------------------------------------------------------------------- | |
18 | ||
19 | // For compilers that support precompilation, includes "wx.h". | |
20 | #include "wx/wxprec.h" | |
21 | ||
a1873279 | 22 | #if wxUSE_CONSOLE_EVENTLOOP |
b46b1d59 | 23 | |
1e04d2bf PC |
24 | #include "wx/evtloop.h" |
25 | ||
b46b1d59 VZ |
26 | #ifndef WX_PRECOMP |
27 | #include "wx/app.h" | |
28 | #include "wx/log.h" | |
29 | #endif | |
30 | ||
31 | #include <errno.h> | |
37ab9399 | 32 | #include "wx/apptrait.h" |
b46b1d59 VZ |
33 | #include "wx/thread.h" |
34 | #include "wx/module.h" | |
3f8cdda4 | 35 | #include "wx/unix/pipe.h" |
ba752031 | 36 | #include "wx/unix/private/timer.h" |
b46b1d59 VZ |
37 | #include "wx/unix/private/epolldispatcher.h" |
38 | #include "wx/private/selectdispatcher.h" | |
39 | ||
9a83f860 | 40 | #define TRACE_EVENTS wxT("events") |
b46b1d59 VZ |
41 | |
42 | // =========================================================================== | |
43 | // wxEventLoop::PipeIOHandler implementation | |
44 | // =========================================================================== | |
45 | ||
3f8cdda4 VS |
46 | namespace wxPrivate |
47 | { | |
48 | ||
49 | // pipe used for wake up messages: when a child thread wants to wake up | |
50 | // the event loop in the main thread it writes to this pipe | |
51 | class PipeIOHandler : public wxFDIOHandler | |
52 | { | |
53 | public: | |
54 | // default ctor does nothing, call Create() to really initialize the | |
55 | // object | |
56 | PipeIOHandler() { } | |
57 | ||
58 | bool Create(); | |
59 | ||
60 | // this method can be, and normally is, called from another thread | |
61 | void WakeUp(); | |
62 | ||
63 | int GetReadFd() { return m_pipe[wxPipe::Read]; } | |
64 | ||
65 | // implement wxFDIOHandler pure virtual methods | |
66 | virtual void OnReadWaiting(); | |
67 | virtual void OnWriteWaiting() { } | |
68 | virtual void OnExceptionWaiting() { } | |
69 | ||
70 | private: | |
71 | wxPipe m_pipe; | |
72 | }; | |
73 | ||
b46b1d59 VZ |
74 | // ---------------------------------------------------------------------------- |
75 | // initialization | |
76 | // ---------------------------------------------------------------------------- | |
77 | ||
3f8cdda4 | 78 | bool PipeIOHandler::Create() |
b46b1d59 VZ |
79 | { |
80 | if ( !m_pipe.Create() ) | |
81 | { | |
82 | wxLogError(_("Failed to create wake up pipe used by event loop.")); | |
83 | return false; | |
84 | } | |
85 | ||
86 | const int fdRead = GetReadFd(); | |
87 | ||
88 | int flags = fcntl(fdRead, F_GETFL, 0); | |
89 | if ( flags == -1 || fcntl(fdRead, F_SETFL, flags | O_NONBLOCK) == -1 ) | |
90 | { | |
91 | wxLogSysError(_("Failed to switch wake up pipe to non-blocking mode")); | |
92 | return false; | |
93 | } | |
94 | ||
95 | wxLogTrace(TRACE_EVENTS, wxT("Wake up pipe (%d, %d) created"), | |
96 | fdRead, m_pipe[wxPipe::Write]); | |
97 | ||
98 | return true; | |
99 | } | |
100 | ||
101 | // ---------------------------------------------------------------------------- | |
102 | // wakeup handling | |
103 | // ---------------------------------------------------------------------------- | |
104 | ||
3f8cdda4 | 105 | void PipeIOHandler::WakeUp() |
b46b1d59 VZ |
106 | { |
107 | if ( write(m_pipe[wxPipe::Write], "s", 1) != 1 ) | |
108 | { | |
109 | // don't use wxLog here, we can be in another thread and this could | |
110 | // result in dead locks | |
111 | perror("write(wake up pipe)"); | |
112 | } | |
113 | } | |
114 | ||
3f8cdda4 | 115 | void PipeIOHandler::OnReadWaiting() |
b46b1d59 VZ |
116 | { |
117 | // got wakeup from child thread: read all data available in pipe just to | |
a12698ab | 118 | // make it empty (even though we write one byte at a time from WakeUp(), |
b46b1d59 VZ |
119 | // it could have been called several times) |
120 | char buf[4]; | |
121 | for ( ;; ) | |
122 | { | |
123 | const int size = read(GetReadFd(), buf, WXSIZEOF(buf)); | |
124 | ||
fada8bef | 125 | if ( size == 0 || (size == -1 && (errno == EAGAIN || errno == EINTR)) ) |
b46b1d59 VZ |
126 | { |
127 | // nothing left in the pipe (EAGAIN is expected for an FD with | |
128 | // O_NONBLOCK) | |
129 | break; | |
130 | } | |
131 | ||
132 | if ( size == -1 ) | |
133 | { | |
134 | wxLogSysError(_("Failed to read from wake-up pipe")); | |
135 | ||
136 | break; | |
137 | } | |
138 | } | |
139 | ||
a12698ab VZ |
140 | // writing to the wake up pipe will make wxConsoleEventLoop return from |
141 | // wxFDIODispatcher::Dispatch() it might be currently blocking in, nothing | |
142 | // else needs to be done | |
b46b1d59 VZ |
143 | } |
144 | ||
3f8cdda4 VS |
145 | } // namespace wxPrivate |
146 | ||
b46b1d59 VZ |
147 | // =========================================================================== |
148 | // wxEventLoop implementation | |
149 | // =========================================================================== | |
150 | ||
151 | //----------------------------------------------------------------------------- | |
152 | // initialization | |
153 | //----------------------------------------------------------------------------- | |
154 | ||
155 | wxConsoleEventLoop::wxConsoleEventLoop() | |
156 | { | |
3f8cdda4 VS |
157 | m_wakeupPipe = new wxPrivate::PipeIOHandler(); |
158 | if ( !m_wakeupPipe->Create() ) | |
b46b1d59 | 159 | { |
3f8cdda4 | 160 | wxDELETE(m_wakeupPipe); |
b46b1d59 VZ |
161 | m_dispatcher = NULL; |
162 | return; | |
163 | } | |
164 | ||
5e1eac14 | 165 | m_dispatcher = wxFDIODispatcher::Get(); |
b46b1d59 | 166 | if ( !m_dispatcher ) |
5e1eac14 | 167 | return; |
b46b1d59 VZ |
168 | |
169 | m_dispatcher->RegisterFD | |
170 | ( | |
3f8cdda4 VS |
171 | m_wakeupPipe->GetReadFd(), |
172 | m_wakeupPipe, | |
b46b1d59 VZ |
173 | wxFDIO_INPUT |
174 | ); | |
3ef595d5 | 175 | } |
b46b1d59 | 176 | |
3f8cdda4 VS |
177 | wxConsoleEventLoop::~wxConsoleEventLoop() |
178 | { | |
c8299fa8 VZ |
179 | if ( m_wakeupPipe ) |
180 | { | |
181 | if ( m_dispatcher ) | |
182 | { | |
183 | m_dispatcher->UnregisterFD(m_wakeupPipe->GetReadFd()); | |
184 | } | |
185 | ||
186 | delete m_wakeupPipe; | |
187 | } | |
3f8cdda4 VS |
188 | } |
189 | ||
b46b1d59 VZ |
190 | //----------------------------------------------------------------------------- |
191 | // events dispatch and loop handling | |
192 | //----------------------------------------------------------------------------- | |
193 | ||
194 | bool wxConsoleEventLoop::Pending() const | |
195 | { | |
a12698ab VZ |
196 | if ( m_dispatcher->HasPending() ) |
197 | return true; | |
198 | ||
199 | #if wxUSE_TIMER | |
200 | wxUsecClock_t nextTimer; | |
201 | if ( wxTimerScheduler::Get().GetNext(&nextTimer) && | |
202 | !wxMilliClockToLong(nextTimer) ) | |
203 | return true; | |
204 | #endif // wxUSE_TIMER | |
205 | ||
206 | return false; | |
b46b1d59 VZ |
207 | } |
208 | ||
209 | bool wxConsoleEventLoop::Dispatch() | |
b46b1d59 | 210 | { |
c22ace4d VZ |
211 | DispatchTimeout(static_cast<unsigned long>( |
212 | wxFDIODispatcher::TIMEOUT_INFINITE)); | |
b46b1d59 | 213 | |
9af42efd VZ |
214 | return true; |
215 | } | |
216 | ||
217 | int wxConsoleEventLoop::DispatchTimeout(unsigned long timeout) | |
218 | { | |
b46b1d59 | 219 | #if wxUSE_TIMER |
9af42efd | 220 | // check if we need to decrease the timeout to account for a timer |
b46b1d59 VZ |
221 | wxUsecClock_t nextTimer; |
222 | if ( wxTimerScheduler::Get().GetNext(&nextTimer) ) | |
223 | { | |
9af42efd VZ |
224 | unsigned long timeUntilNextTimer = wxMilliClockToLong(nextTimer / 1000); |
225 | if ( timeUntilNextTimer < timeout ) | |
226 | timeout = timeUntilNextTimer; | |
b46b1d59 | 227 | } |
b46b1d59 | 228 | #endif // wxUSE_TIMER |
b46b1d59 | 229 | |
a12698ab | 230 | bool hadEvent = m_dispatcher->Dispatch(timeout) > 0; |
b46b1d59 VZ |
231 | |
232 | #if wxUSE_TIMER | |
9af42efd VZ |
233 | if ( wxTimerScheduler::Get().NotifyExpired() ) |
234 | hadEvent = true; | |
235 | #endif // wxUSE_TIMER | |
b46b1d59 | 236 | |
9af42efd | 237 | return hadEvent ? 1 : -1; |
438febca VZ |
238 | } |
239 | ||
240 | void wxConsoleEventLoop::WakeUp() | |
241 | { | |
3f8cdda4 | 242 | m_wakeupPipe->WakeUp(); |
438febca VZ |
243 | } |
244 | ||
245 | void wxConsoleEventLoop::OnNextIteration() | |
246 | { | |
b46b1d59 VZ |
247 | // call the signal handlers for any signals we caught recently |
248 | wxTheApp->CheckSignal(); | |
249 | } | |
250 | ||
37ab9399 SN |
251 | |
252 | wxEventLoopBase *wxConsoleAppTraits::CreateEventLoop() | |
253 | { | |
254 | return new wxEventLoop(); | |
255 | } | |
256 | ||
a1873279 | 257 | #endif // wxUSE_CONSOLE_EVENTLOOP |