]>
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/private/timer.h" | |
35 | #include "wx/unix/private/epolldispatcher.h" | |
36 | #include "wx/private/selectdispatcher.h" | |
37 | ||
38 | #define TRACE_EVENTS _T("events") | |
39 | ||
40 | // =========================================================================== | |
41 | // wxEventLoop::PipeIOHandler implementation | |
42 | // =========================================================================== | |
43 | ||
44 | // ---------------------------------------------------------------------------- | |
45 | // initialization | |
46 | // ---------------------------------------------------------------------------- | |
47 | ||
48 | bool wxConsoleEventLoop::PipeIOHandler::Create() | |
49 | { | |
50 | if ( !m_pipe.Create() ) | |
51 | { | |
52 | wxLogError(_("Failed to create wake up pipe used by event loop.")); | |
53 | return false; | |
54 | } | |
55 | ||
56 | const int fdRead = GetReadFd(); | |
57 | ||
58 | int flags = fcntl(fdRead, F_GETFL, 0); | |
59 | if ( flags == -1 || fcntl(fdRead, F_SETFL, flags | O_NONBLOCK) == -1 ) | |
60 | { | |
61 | wxLogSysError(_("Failed to switch wake up pipe to non-blocking mode")); | |
62 | return false; | |
63 | } | |
64 | ||
65 | wxLogTrace(TRACE_EVENTS, wxT("Wake up pipe (%d, %d) created"), | |
66 | fdRead, m_pipe[wxPipe::Write]); | |
67 | ||
68 | return true; | |
69 | } | |
70 | ||
71 | // ---------------------------------------------------------------------------- | |
72 | // wakeup handling | |
73 | // ---------------------------------------------------------------------------- | |
74 | ||
75 | void wxConsoleEventLoop::PipeIOHandler::WakeUp() | |
76 | { | |
77 | if ( write(m_pipe[wxPipe::Write], "s", 1) != 1 ) | |
78 | { | |
79 | // don't use wxLog here, we can be in another thread and this could | |
80 | // result in dead locks | |
81 | perror("write(wake up pipe)"); | |
82 | } | |
83 | } | |
84 | ||
85 | void wxConsoleEventLoop::PipeIOHandler::OnReadWaiting() | |
86 | { | |
87 | // got wakeup from child thread: read all data available in pipe just to | |
88 | // make it empty (evevn though we write one byte at a time from WakeUp(), | |
89 | // it could have been called several times) | |
90 | char buf[4]; | |
91 | for ( ;; ) | |
92 | { | |
93 | const int size = read(GetReadFd(), buf, WXSIZEOF(buf)); | |
94 | ||
95 | if ( size == 0 || (size == -1 && errno == EAGAIN) ) | |
96 | { | |
97 | // nothing left in the pipe (EAGAIN is expected for an FD with | |
98 | // O_NONBLOCK) | |
99 | break; | |
100 | } | |
101 | ||
102 | if ( size == -1 ) | |
103 | { | |
104 | wxLogSysError(_("Failed to read from wake-up pipe")); | |
105 | ||
106 | break; | |
107 | } | |
108 | } | |
109 | ||
110 | wxTheApp->ProcessPendingEvents(); | |
111 | } | |
112 | ||
113 | // =========================================================================== | |
114 | // wxEventLoop implementation | |
115 | // =========================================================================== | |
116 | ||
117 | //----------------------------------------------------------------------------- | |
118 | // initialization | |
119 | //----------------------------------------------------------------------------- | |
120 | ||
121 | wxConsoleEventLoop::wxConsoleEventLoop() | |
122 | { | |
123 | if ( !m_wakeupPipe.Create() ) | |
124 | { | |
125 | m_dispatcher = NULL; | |
126 | return; | |
127 | } | |
128 | ||
129 | m_dispatcher = wxFDIODispatcher::Get(); | |
130 | if ( !m_dispatcher ) | |
131 | return; | |
132 | ||
133 | m_dispatcher->RegisterFD | |
134 | ( | |
135 | m_wakeupPipe.GetReadFd(), | |
136 | &m_wakeupPipe, | |
137 | wxFDIO_INPUT | |
138 | ); | |
139 | } | |
140 | ||
141 | //----------------------------------------------------------------------------- | |
142 | // events dispatch and loop handling | |
143 | //----------------------------------------------------------------------------- | |
144 | ||
145 | bool wxConsoleEventLoop::Pending() const | |
146 | { | |
147 | return wxTheApp->HasPendingEvents(); | |
148 | } | |
149 | ||
150 | bool wxConsoleEventLoop::Dispatch() | |
151 | { | |
152 | // calculate the timeout until the next timer expiration | |
153 | int timeout; | |
154 | ||
155 | #if wxUSE_TIMER | |
156 | wxUsecClock_t nextTimer; | |
157 | if ( wxTimerScheduler::Get().GetNext(&nextTimer) ) | |
158 | { | |
159 | // timeout is in ms | |
160 | timeout = (nextTimer / 1000).ToLong(); | |
161 | } | |
162 | else // no timers, we can block forever | |
163 | #endif // wxUSE_TIMER | |
164 | { | |
165 | timeout = wxFDIODispatcher::TIMEOUT_INFINITE; | |
166 | } | |
167 | ||
168 | m_dispatcher->Dispatch(timeout); | |
169 | ||
170 | #if wxUSE_TIMER | |
171 | wxTimerScheduler::Get().NotifyExpired(); | |
172 | #endif | |
173 | ||
174 | wxTheApp->ProcessPendingEvents(); | |
175 | return true; | |
176 | } | |
177 | ||
178 | void wxConsoleEventLoop::WakeUp() | |
179 | { | |
180 | m_wakeupPipe.WakeUp(); | |
181 | } | |
182 | ||
183 | void wxConsoleEventLoop::OnNextIteration() | |
184 | { | |
185 | // call the signal handlers for any signals we caught recently | |
186 | wxTheApp->CheckSignal(); | |
187 | } | |
188 | ||
189 | ||
190 | wxEventLoopBase *wxConsoleAppTraits::CreateEventLoop() | |
191 | { | |
192 | return new wxEventLoop(); | |
193 | } | |
194 | ||
195 | #endif // wxUSE_CONSOLE_EVENTLOOP |