]> git.saurik.com Git - wxWidgets.git/blame - src/unix/evtloopunix.cpp
s/wxUsleep/wxMilliSleep
[wxWidgets.git] / src / unix / evtloopunix.cpp
CommitLineData
b46b1d59
VZ
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
a1873279 22#if wxUSE_CONSOLE_EVENTLOOP
b46b1d59
VZ
23
24#ifndef WX_PRECOMP
25 #include "wx/app.h"
26 #include "wx/log.h"
27#endif
28
29#include <errno.h>
37ab9399 30#include "wx/apptrait.h"
a1873279 31#include "wx/evtloop.h"
b46b1d59
VZ
32#include "wx/thread.h"
33#include "wx/module.h"
ba752031 34#include "wx/unix/private/timer.h"
b46b1d59
VZ
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
48bool 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
75void 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
85void wxConsoleEventLoop::PipeIOHandler::OnReadWaiting()
86{
87 // got wakeup from child thread: read all data available in pipe just to
a12698ab 88 // make it empty (even though we write one byte at a time from WakeUp(),
b46b1d59
VZ
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
a12698ab
VZ
110 // writing to the wake up pipe will make wxConsoleEventLoop return from
111 // wxFDIODispatcher::Dispatch() it might be currently blocking in, nothing
112 // else needs to be done
b46b1d59
VZ
113}
114
115// ===========================================================================
116// wxEventLoop implementation
117// ===========================================================================
118
119//-----------------------------------------------------------------------------
120// initialization
121//-----------------------------------------------------------------------------
122
123wxConsoleEventLoop::wxConsoleEventLoop()
124{
125 if ( !m_wakeupPipe.Create() )
126 {
127 m_dispatcher = NULL;
128 return;
129 }
130
5e1eac14 131 m_dispatcher = wxFDIODispatcher::Get();
b46b1d59 132 if ( !m_dispatcher )
5e1eac14 133 return;
b46b1d59
VZ
134
135 m_dispatcher->RegisterFD
136 (
137 m_wakeupPipe.GetReadFd(),
138 &m_wakeupPipe,
139 wxFDIO_INPUT
140 );
3ef595d5 141}
b46b1d59
VZ
142
143//-----------------------------------------------------------------------------
144// events dispatch and loop handling
145//-----------------------------------------------------------------------------
146
147bool wxConsoleEventLoop::Pending() const
148{
a12698ab
VZ
149 if ( m_dispatcher->HasPending() )
150 return true;
151
152#if wxUSE_TIMER
153 wxUsecClock_t nextTimer;
154 if ( wxTimerScheduler::Get().GetNext(&nextTimer) &&
155 !wxMilliClockToLong(nextTimer) )
156 return true;
157#endif // wxUSE_TIMER
158
159 return false;
b46b1d59
VZ
160}
161
162bool wxConsoleEventLoop::Dispatch()
b46b1d59 163{
9af42efd 164 DispatchTimeout(wxFDIODispatcher::TIMEOUT_INFINITE);
b46b1d59 165
9af42efd
VZ
166 return true;
167}
168
169int wxConsoleEventLoop::DispatchTimeout(unsigned long timeout)
170{
b46b1d59 171#if wxUSE_TIMER
9af42efd 172 // check if we need to decrease the timeout to account for a timer
b46b1d59
VZ
173 wxUsecClock_t nextTimer;
174 if ( wxTimerScheduler::Get().GetNext(&nextTimer) )
175 {
9af42efd
VZ
176 unsigned long timeUntilNextTimer = wxMilliClockToLong(nextTimer / 1000);
177 if ( timeUntilNextTimer < timeout )
178 timeout = timeUntilNextTimer;
b46b1d59 179 }
b46b1d59 180#endif // wxUSE_TIMER
b46b1d59 181
a12698ab 182 bool hadEvent = m_dispatcher->Dispatch(timeout) > 0;
b46b1d59
VZ
183
184#if wxUSE_TIMER
9af42efd
VZ
185 if ( wxTimerScheduler::Get().NotifyExpired() )
186 hadEvent = true;
187#endif // wxUSE_TIMER
b46b1d59 188
9af42efd 189 return hadEvent ? 1 : -1;
438febca
VZ
190}
191
192void wxConsoleEventLoop::WakeUp()
193{
194 m_wakeupPipe.WakeUp();
195}
196
197void wxConsoleEventLoop::OnNextIteration()
198{
b46b1d59
VZ
199 // call the signal handlers for any signals we caught recently
200 wxTheApp->CheckSignal();
201}
202
37ab9399
SN
203
204wxEventLoopBase *wxConsoleAppTraits::CreateEventLoop()
205{
206 return new wxEventLoop();
207}
208
a1873279 209#endif // wxUSE_CONSOLE_EVENTLOOP