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