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