]>
Commit | Line | Data |
---|---|---|
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 | ||
22 | #if wxUSE_CONSOLE_EVENTLOOP | |
23 | ||
24 | #ifndef WX_PRECOMP | |
25 | #include "wx/app.h" | |
26 | #include "wx/log.h" | |
27 | #endif | |
28 | ||
29 | #include <errno.h> | |
30 | #include "wx/apptrait.h" | |
31 | #include "wx/evtloop.h" | |
32 | #include "wx/thread.h" | |
33 | #include "wx/module.h" | |
34 | #include "wx/unix/pipe.h" | |
35 | #include "wx/unix/private/timer.h" | |
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 | ||
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 | ||
73 | // ---------------------------------------------------------------------------- | |
74 | // initialization | |
75 | // ---------------------------------------------------------------------------- | |
76 | ||
77 | bool PipeIOHandler::Create() | |
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 | ||
104 | void PipeIOHandler::WakeUp() | |
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 | ||
114 | void PipeIOHandler::OnReadWaiting() | |
115 | { | |
116 | // got wakeup from child thread: read all data available in pipe just to | |
117 | // make it empty (even though we write one byte at a time from WakeUp(), | |
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 | ||
124 | if ( size == 0 || (size == -1 && (errno == EAGAIN || errno == EINTR)) ) | |
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 | ||
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 | |
142 | } | |
143 | ||
144 | } // namespace wxPrivate | |
145 | ||
146 | // =========================================================================== | |
147 | // wxEventLoop implementation | |
148 | // =========================================================================== | |
149 | ||
150 | //----------------------------------------------------------------------------- | |
151 | // initialization | |
152 | //----------------------------------------------------------------------------- | |
153 | ||
154 | wxConsoleEventLoop::wxConsoleEventLoop() | |
155 | { | |
156 | m_wakeupPipe = new wxPrivate::PipeIOHandler(); | |
157 | if ( !m_wakeupPipe->Create() ) | |
158 | { | |
159 | wxDELETE(m_wakeupPipe); | |
160 | m_dispatcher = NULL; | |
161 | return; | |
162 | } | |
163 | ||
164 | m_dispatcher = wxFDIODispatcher::Get(); | |
165 | if ( !m_dispatcher ) | |
166 | return; | |
167 | ||
168 | m_dispatcher->RegisterFD | |
169 | ( | |
170 | m_wakeupPipe->GetReadFd(), | |
171 | m_wakeupPipe, | |
172 | wxFDIO_INPUT | |
173 | ); | |
174 | } | |
175 | ||
176 | wxConsoleEventLoop::~wxConsoleEventLoop() | |
177 | { | |
178 | delete m_wakeupPipe; | |
179 | } | |
180 | ||
181 | //----------------------------------------------------------------------------- | |
182 | // events dispatch and loop handling | |
183 | //----------------------------------------------------------------------------- | |
184 | ||
185 | bool wxConsoleEventLoop::Pending() const | |
186 | { | |
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; | |
198 | } | |
199 | ||
200 | bool wxConsoleEventLoop::Dispatch() | |
201 | { | |
202 | DispatchTimeout(wxFDIODispatcher::TIMEOUT_INFINITE); | |
203 | ||
204 | return true; | |
205 | } | |
206 | ||
207 | int wxConsoleEventLoop::DispatchTimeout(unsigned long timeout) | |
208 | { | |
209 | #if wxUSE_TIMER | |
210 | // check if we need to decrease the timeout to account for a timer | |
211 | wxUsecClock_t nextTimer; | |
212 | if ( wxTimerScheduler::Get().GetNext(&nextTimer) ) | |
213 | { | |
214 | unsigned long timeUntilNextTimer = wxMilliClockToLong(nextTimer / 1000); | |
215 | if ( timeUntilNextTimer < timeout ) | |
216 | timeout = timeUntilNextTimer; | |
217 | } | |
218 | #endif // wxUSE_TIMER | |
219 | ||
220 | bool hadEvent = m_dispatcher->Dispatch(timeout) > 0; | |
221 | ||
222 | #if wxUSE_TIMER | |
223 | if ( wxTimerScheduler::Get().NotifyExpired() ) | |
224 | hadEvent = true; | |
225 | #endif // wxUSE_TIMER | |
226 | ||
227 | return hadEvent ? 1 : -1; | |
228 | } | |
229 | ||
230 | void wxConsoleEventLoop::WakeUp() | |
231 | { | |
232 | m_wakeupPipe->WakeUp(); | |
233 | } | |
234 | ||
235 | void wxConsoleEventLoop::OnNextIteration() | |
236 | { | |
237 | // call the signal handlers for any signals we caught recently | |
238 | wxTheApp->CheckSignal(); | |
239 | } | |
240 | ||
241 | ||
242 | wxEventLoopBase *wxConsoleAppTraits::CreateEventLoop() | |
243 | { | |
244 | return new wxEventLoop(); | |
245 | } | |
246 | ||
247 | #endif // wxUSE_CONSOLE_EVENTLOOP |