]> git.saurik.com Git - wxWidgets.git/blame - src/x11/evtloop.cpp
first stab at supporting custom renderers
[wxWidgets.git] / src / x11 / evtloop.cpp
CommitLineData
1b0fb34b 1///////////////////////////////////////////////////////////////////////////////
32d4c30a 2// Name: src/x11/evtloop.cpp
1b0fb34b
JS
3// Purpose: implements wxEventLoop for X11
4// Author: Julian Smart
5// Modified by:
6// Created: 01.06.01
7// RCS-ID: $Id$
8// Copyright: (c) 2002 Julian Smart
526954c5 9// Licence: wxWindows licence
1b0fb34b
JS
10///////////////////////////////////////////////////////////////////////////////
11
12// ============================================================================
13// declarations
14// ============================================================================
15
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
19
32d4c30a
WS
20// for compilers that support precompilation, includes "wx.h".
21#include "wx/wxprec.h"
22
670f9935
WS
23#include "wx/evtloop.h"
24
32d4c30a
WS
25#ifndef WX_PRECOMP
26 #include "wx/hash.h"
670f9935 27 #include "wx/app.h"
cdccdfab 28 #include "wx/window.h"
02761f6c 29 #include "wx/module.h"
32d4c30a
WS
30#endif
31
4eba3923 32#include "wx/private/fdiodispatcher.h"
17a1ebd1 33#include "wx/unix/private.h"
1b0fb34b 34#include "wx/x11/private.h"
c2ca375c 35#include "wx/generic/private/timer.h"
1b0fb34b 36
32d4c30a
WS
37#if wxUSE_THREADS
38 #include "wx/thread.h"
39#endif
40
8193abb6 41#include <X11/Xlib.h>
1016f0de
JS
42#include <sys/time.h>
43#include <unistd.h>
44
bc023abb
MW
45#ifdef HAVE_SYS_SELECT_H
46# include <sys/select.h>
47#endif
48
1b0fb34b
JS
49// ----------------------------------------------------------------------------
50// wxEventLoopImpl
51// ----------------------------------------------------------------------------
52
53class WXDLLEXPORT wxEventLoopImpl
54{
55public:
56 // ctor
32d4c30a 57 wxEventLoopImpl() { SetExitCode(0); m_keepGoing = false; }
1b0fb34b 58
32d4c30a 59 // process an XEvent, return true if it was processed
086fd560 60 bool ProcessEvent(XEvent* event);
1b0fb34b 61
32d4c30a 62 // generate an idle message, return true if more idle time requested
1b0fb34b
JS
63 bool SendIdleEvent();
64
65 // set/get the exit code
66 void SetExitCode(int exitcode) { m_exitcode = exitcode; }
67 int GetExitCode() const { return m_exitcode; }
68
69public:
32d4c30a 70 // preprocess an event, return true if processed (i.e. no further
1b0fb34b 71 // dispatching required)
7266b672 72 bool PreProcessEvent(XEvent* event);
1b0fb34b
JS
73
74 // the exit code of the event loop
75 int m_exitcode;
76
77 bool m_keepGoing;
78};
79
80// ============================================================================
81// wxEventLoopImpl implementation
82// ============================================================================
83
84// ----------------------------------------------------------------------------
85// wxEventLoopImpl message processing
86// ----------------------------------------------------------------------------
87
086fd560 88bool wxEventLoopImpl::ProcessEvent(XEvent *event)
1b0fb34b
JS
89{
90 // give us the chance to preprocess the message first
086fd560 91 if ( PreProcessEvent(event) )
32d4c30a 92 return true;
3754265e 93
086fd560
JS
94 // if it wasn't done, dispatch it to the corresponding window
95 if (wxTheApp)
96 return wxTheApp->ProcessXEvent((WXEvent*) event);
97
32d4c30a 98 return false;
1b0fb34b
JS
99}
100
89954433 101bool wxEventLoopImpl::PreProcessEvent(XEvent *WXUNUSED(event))
1b0fb34b 102{
32d4c30a 103 return false;
1b0fb34b
JS
104}
105
106// ----------------------------------------------------------------------------
107// wxEventLoopImpl idle event processing
108// ----------------------------------------------------------------------------
109
110bool wxEventLoopImpl::SendIdleEvent()
111{
e39af974 112 return wxTheApp->ProcessIdle();
1b0fb34b
JS
113}
114
115// ============================================================================
116// wxEventLoop implementation
117// ============================================================================
118
1b0fb34b
JS
119// ----------------------------------------------------------------------------
120// wxEventLoop running and exiting
121// ----------------------------------------------------------------------------
122
b46b1d59 123wxGUIEventLoop::~wxGUIEventLoop()
1b0fb34b 124{
9a83f860 125 wxASSERT_MSG( !m_impl, wxT("should have been deleted in Run()") );
1b0fb34b
JS
126}
127
b46b1d59 128int wxGUIEventLoop::Run()
1b0fb34b
JS
129{
130 // event loops are not recursive, you need to create another loop!
2c032a5d 131 wxCHECK_MSG( !m_impl, -1, wxT("can't reenter a message loop") );
1b0fb34b
JS
132
133 m_impl = new wxEventLoopImpl;
3754265e 134
77fb1a02 135 wxEventLoopActivator activate(this);
1b0fb34b 136
32d4c30a 137 m_impl->m_keepGoing = true;
1b0fb34b
JS
138 while ( m_impl->m_keepGoing )
139 {
1b0fb34b
JS
140 // generate and process idle events for as long as we don't have
141 // anything else to do
142 while ( ! Pending() )
143 {
b555c37c 144#if wxUSE_TIMER
c2ca375c 145 wxGenericTimerImpl::NotifyTimers(); // TODO: is this the correct place for it?
b555c37c 146#endif
1b0fb34b
JS
147 if (!m_impl->SendIdleEvent())
148 {
1b0fb34b
JS
149 // Break out of while loop
150 break;
151 }
152 }
153
154 // a message came or no more idle processing to do, sit in Dispatch()
155 // waiting for the next message
156 if ( !Dispatch() )
157 {
158 break;
159 }
160 }
161
16d17da6
VZ
162 OnExit();
163
1b0fb34b 164 int exitcode = m_impl->GetExitCode();
5276b0a5 165 wxDELETE(m_impl);
1b0fb34b 166
1b0fb34b
JS
167 return exitcode;
168}
169
b46b1d59 170void wxGUIEventLoop::Exit(int rc)
1b0fb34b 171{
2c032a5d
VZ
172 if ( m_impl )
173 {
174 m_impl->SetExitCode(rc);
175 m_impl->m_keepGoing = false;
176 }
1b0fb34b
JS
177}
178
179// ----------------------------------------------------------------------------
180// wxEventLoop message processing dispatching
181// ----------------------------------------------------------------------------
182
b46b1d59 183bool wxGUIEventLoop::Pending() const
1b0fb34b 184{
2b5f62a0
VZ
185 XFlush( wxGlobalDisplay() );
186 return (XPending( wxGlobalDisplay() ) > 0);
1b0fb34b
JS
187}
188
b46b1d59 189bool wxGUIEventLoop::Dispatch()
1b0fb34b 190{
1c991946
VZ
191 // see comment in wxEventLoopManual::ProcessEvents()
192 if ( wxTheApp )
193 wxTheApp->ProcessPendingEvents();
194
1b0fb34b
JS
195 XEvent event;
196
197 // TODO allowing for threads, as per e.g. wxMSW
198
1016f0de
JS
199 // This now waits until either an X event is received,
200 // or the select times out. So we should now process
201 // wxTimers in a reasonably timely fashion. However it
202 // does also mean that idle processing will happen more
203 // often, so we should probably limit idle processing to
204 // not be repeated more than every N milliseconds.
3754265e 205
2b5f62a0 206 if (XPending( wxGlobalDisplay() ) == 0)
1016f0de 207 {
868741e9
JS
208#if wxUSE_NANOX
209 GR_TIMEOUT timeout = 10; // Milliseconds
210 // Wait for next event, or timeout
211 GrGetNextEventTimeout(& event, timeout);
212
213 // Fall through to ProcessEvent.
214 // we'll assume that ProcessEvent will just ignore
215 // the event if there was a timeout and no event.
3754265e 216
868741e9 217#else
1016f0de
JS
218 struct timeval tv;
219 tv.tv_sec=0;
220 tv.tv_usec=10000; // TODO make this configurable
2b5f62a0 221 int fd = ConnectionNumber( wxGlobalDisplay() );
3754265e 222
1016f0de 223 fd_set readset;
52127426 224 fd_set writeset;
17a1ebd1
VZ
225 wxFD_ZERO(&readset);
226 wxFD_ZERO(&writeset);
17a1ebd1 227 wxFD_SET(fd, &readset);
52127426 228
6b44a335 229 if (select( fd+1, &readset, &writeset, NULL, &tv ) != 0)
1016f0de 230 {
6b44a335 231 // An X11 event was pending, get it
17a1ebd1 232 if (wxFD_ISSET( fd, &readset ))
2b5f62a0 233 XNextEvent( wxGlobalDisplay(), &event );
b46b1d59 234 }
868741e9 235#endif
3754265e 236 }
2b5f62a0 237 else
1016f0de 238 {
2b5f62a0 239 XNextEvent( wxGlobalDisplay(), &event );
1016f0de 240 }
3754265e 241
6b44a335
VS
242#if wxUSE_SOCKETS
243 // handle any pending socket events:
4eba3923 244 wxFDIODispatcher::DispatchPending();
6b44a335 245#endif
3754265e 246
2b5f62a0 247 (void) m_impl->ProcessEvent( &event );
32d4c30a 248 return true;
1b0fb34b 249}
dde19c21
FM
250
251bool wxGUIEventLoop::YieldFor(long eventsToProcess)
252{
253 // Sometimes only 2 yields seem
254 // to do the trick, e.g. in the
255 // progress dialog
256 int i;
257 for (i = 0; i < 2; i++)
258 {
259 m_isInsideYield = true;
260 m_eventsToProcessInsideYield = eventsToProcess;
261
262 // Call dispatch at least once so that sockets
263 // can be tested
264 wxTheApp->Dispatch();
265
266 // TODO: implement event filtering using the eventsToProcess mask
267 while (wxTheApp && wxTheApp->Pending())
268 wxTheApp->Dispatch();
269
270#if wxUSE_TIMER
271 wxGenericTimerImpl::NotifyTimers();
272#endif
273 ProcessIdle();
274
275 m_isInsideYield = false;
276 }
277
278 return true;
279}