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