1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/x11/evtloop.cpp
3 // Purpose: implements wxEventLoop for X11
4 // Author: Julian Smart
8 // Copyright: (c) 2002 Julian Smart
9 // License: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 // for compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
23 #include "wx/evtloop.h"
28 #include "wx/window.h"
30 #include "wx/module.h"
33 #include "wx/private/selectdispatcher.h"
34 #include "wx/unix/private.h"
35 #include "wx/x11/private.h"
39 #include "wx/thread.h"
45 #ifdef HAVE_SYS_SELECT_H
46 # include <sys/select.h>
49 // ----------------------------------------------------------------------------
51 // ----------------------------------------------------------------------------
53 class WXDLLEXPORT wxEventLoopImpl
57 wxEventLoopImpl() { SetExitCode(0); m_keepGoing
= false; }
59 // process an XEvent, return true if it was processed
60 bool ProcessEvent(XEvent
* event
);
62 // generate an idle message, return true if more idle time requested
65 // set/get the exit code
66 void SetExitCode(int exitcode
) { m_exitcode
= exitcode
; }
67 int GetExitCode() const { return m_exitcode
; }
70 // preprocess an event, return true if processed (i.e. no further
71 // dispatching required)
72 bool PreProcessEvent(XEvent
* event
);
74 // the exit code of the event loop
80 // ============================================================================
81 // wxEventLoopImpl implementation
82 // ============================================================================
84 // ----------------------------------------------------------------------------
85 // wxEventLoopImpl message processing
86 // ----------------------------------------------------------------------------
88 bool wxEventLoopImpl::ProcessEvent(XEvent
*event
)
90 // give us the chance to preprocess the message first
91 if ( PreProcessEvent(event
) )
94 // if it wasn't done, dispatch it to the corresponding window
96 return wxTheApp
->ProcessXEvent((WXEvent
*) event
);
101 bool wxEventLoopImpl::PreProcessEvent(XEvent
*event
)
105 HWND hWnd
= msg
->hwnd
;
106 wxWindow
*wndThis
= wxGetWindowFromHWND((WXHWND
)hWnd
);
109 // try translations first; find the youngest window with a translation
112 for ( wnd
= wndThis
; wnd
; wnd
= wnd
->GetParent() )
114 if ( wnd
->MSWTranslateMessage((WXMSG
*)msg
) )
118 // Anyone for a non-translation message? Try youngest descendants first.
119 for ( wnd
= wndThis
; wnd
; wnd
= wnd
->GetParent() )
121 if ( wnd
->MSWProcessMessage((WXMSG
*)msg
) )
129 // ----------------------------------------------------------------------------
130 // wxEventLoopImpl idle event processing
131 // ----------------------------------------------------------------------------
133 bool wxEventLoopImpl::SendIdleEvent()
135 return wxTheApp
->ProcessIdle();
138 // ============================================================================
139 // wxEventLoop implementation
140 // ============================================================================
142 // ----------------------------------------------------------------------------
143 // wxEventLoop running and exiting
144 // ----------------------------------------------------------------------------
146 wxEventLoop::~wxEventLoop()
148 wxASSERT_MSG( !m_impl
, _T("should have been deleted in Run()") );
151 int wxEventLoop::Run()
153 // event loops are not recursive, you need to create another loop!
154 wxCHECK_MSG( !IsRunning(), -1, _T("can't reenter a message loop") );
156 m_impl
= new wxEventLoopImpl
;
158 wxEventLoopActivator
activate(this);
160 m_impl
->m_keepGoing
= true;
161 while ( m_impl
->m_keepGoing
)
163 // generate and process idle events for as long as we don't have
164 // anything else to do
165 while ( ! Pending() )
168 wxTimer::NotifyTimers(); // TODO: is this the correct place for it?
170 if (!m_impl
->SendIdleEvent())
172 // Break out of while loop
177 // a message came or no more idle processing to do, sit in Dispatch()
178 // waiting for the next message
187 int exitcode
= m_impl
->GetExitCode();
194 void wxEventLoop::Exit(int rc
)
196 wxCHECK_RET( IsRunning(), _T("can't call Exit() if not running") );
198 m_impl
->SetExitCode(rc
);
199 m_impl
->m_keepGoing
= false;
202 // ----------------------------------------------------------------------------
203 // wxEventLoop message processing dispatching
204 // ----------------------------------------------------------------------------
206 bool wxEventLoop::Pending() const
208 XFlush( wxGlobalDisplay() );
209 return (XPending( wxGlobalDisplay() ) > 0);
212 bool wxEventLoop::Dispatch()
216 // Start off by checking if any of our child processes have finished.
217 wxCheckForFinishedChildren();
219 // TODO allowing for threads, as per e.g. wxMSW
221 // This now waits until either an X event is received,
222 // or the select times out. So we should now process
223 // wxTimers in a reasonably timely fashion. However it
224 // does also mean that idle processing will happen more
225 // often, so we should probably limit idle processing to
226 // not be repeated more than every N milliseconds.
228 if (XPending( wxGlobalDisplay() ) == 0)
231 GR_TIMEOUT timeout
= 10; // Milliseconds
232 // Wait for next event, or timeout
233 GrGetNextEventTimeout(& event
, timeout
);
235 // Fall through to ProcessEvent.
236 // we'll assume that ProcessEvent will just ignore
237 // the event if there was a timeout and no event.
242 tv
.tv_usec
=10000; // TODO make this configurable
243 int fd
= ConnectionNumber( wxGlobalDisplay() );
248 wxFD_ZERO(&writeset
);
249 wxFD_SET(fd
, &readset
);
251 if (select( fd
+1, &readset
, &writeset
, NULL
, &tv
) != 0)
253 // An X11 event was pending, get it
254 if (wxFD_ISSET( fd
, &readset
))
255 XNextEvent( wxGlobalDisplay(), &event
);
261 XNextEvent( wxGlobalDisplay(), &event
);
265 // handle any pending socket events:
266 wxSelectDispatcher::Get().RunLoop(0);
269 (void) m_impl
->ProcessEvent( &event
);