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