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