1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: 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 // ----------------------------------------------------------------------------
21 #pragma implementation "evtloop.h"
24 #include "wx/window.h"
26 #include "wx/evtloop.h"
27 #include "wx/tooltip.h"
29 #include "wx/thread.h"
32 #include "wx/x11/private.h"
38 // ----------------------------------------------------------------------------
40 // ----------------------------------------------------------------------------
42 class WXDLLEXPORT wxEventLoopImpl
46 wxEventLoopImpl() { SetExitCode(0); m_keepGoing
= FALSE
; }
48 // process an XEvent, return TRUE if it was processed
49 bool ProcessEvent(XEvent
* event
);
51 // generate an idle message, return TRUE if more idle time requested
54 // set/get the exit code
55 void SetExitCode(int exitcode
) { m_exitcode
= exitcode
; }
56 int GetExitCode() const { return m_exitcode
; }
59 // preprocess an event, return TRUE if processed (i.e. no further
60 // dispatching required)
61 bool PreProcessEvent(XEvent
* event
);
63 // the exit code of the event loop
69 // ============================================================================
70 // wxEventLoopImpl implementation
71 // ============================================================================
73 // ----------------------------------------------------------------------------
74 // wxEventLoopImpl message processing
75 // ----------------------------------------------------------------------------
77 bool wxEventLoopImpl::ProcessEvent(XEvent
*event
)
79 // give us the chance to preprocess the message first
80 if ( PreProcessEvent(event
) )
83 // if it wasn't done, dispatch it to the corresponding window
85 return wxTheApp
->ProcessXEvent((WXEvent
*) event
);
90 bool wxEventLoopImpl::PreProcessEvent(XEvent
*event
)
94 HWND hWnd
= msg
->hwnd
;
95 wxWindow
*wndThis
= wxGetWindowFromHWND((WXHWND
)hWnd
);
98 // try translations first; find the youngest window with a translation
101 for ( wnd
= wndThis
; wnd
; wnd
= wnd
->GetParent() )
103 if ( wnd
->MSWTranslateMessage((WXMSG
*)msg
) )
107 // Anyone for a non-translation message? Try youngest descendants first.
108 for ( wnd
= wndThis
; wnd
; wnd
= wnd
->GetParent() )
110 if ( wnd
->MSWProcessMessage((WXMSG
*)msg
) )
118 // ----------------------------------------------------------------------------
119 // wxEventLoopImpl idle event processing
120 // ----------------------------------------------------------------------------
122 bool wxEventLoopImpl::SendIdleEvent()
125 event
.SetEventObject(wxTheApp
);
127 return wxTheApp
->ProcessEvent(event
) && event
.MoreRequested();
130 // ============================================================================
131 // wxEventLoop implementation
132 // ============================================================================
134 wxEventLoop
*wxEventLoop::ms_activeLoop
= NULL
;
136 // ----------------------------------------------------------------------------
137 // wxEventLoop running and exiting
138 // ----------------------------------------------------------------------------
140 wxEventLoop::~wxEventLoop()
142 wxASSERT_MSG( !m_impl
, _T("should have been deleted in Run()") );
145 bool wxEventLoop::IsRunning() const
147 return m_impl
!= NULL
;
150 int wxEventLoop::Run()
152 // event loops are not recursive, you need to create another loop!
153 wxCHECK_MSG( !IsRunning(), -1, _T("can't reenter a message loop") );
155 m_impl
= new wxEventLoopImpl
;
157 wxEventLoop
*oldLoop
= ms_activeLoop
;
158 ms_activeLoop
= this;
160 m_impl
->m_keepGoing
= TRUE
;
161 while ( m_impl
->m_keepGoing
)
163 #if 0 // wxUSE_THREADS
164 wxMutexGuiLeaveOrEnter();
165 #endif // wxUSE_THREADS
167 // generate and process idle events for as long as we don't have
168 // anything else to do
169 while ( ! Pending() )
172 wxTimer::NotifyTimers(); // TODO: is this the correct place for it?
174 if (!m_impl
->SendIdleEvent())
177 // leave the main loop to give other threads a chance to
178 // perform their GUI work
183 // Break out of while loop
188 // a message came or no more idle processing to do, sit in Dispatch()
189 // waiting for the next message
196 int exitcode
= m_impl
->GetExitCode();
200 ms_activeLoop
= oldLoop
;
205 void wxEventLoop::Exit(int rc
)
207 wxCHECK_RET( IsRunning(), _T("can't call Exit() if not running") );
209 m_impl
->SetExitCode(rc
);
210 m_impl
->m_keepGoing
= FALSE
;
213 // ----------------------------------------------------------------------------
214 // wxEventLoop message processing dispatching
215 // ----------------------------------------------------------------------------
217 bool wxEventLoop::Pending() const
219 XFlush((Display
*) wxGetDisplay());
220 return (XPending((Display
*) wxGetDisplay()) > 0);
223 bool wxEventLoop::Dispatch()
227 // TODO allowing for threads, as per e.g. wxMSW
230 XNextEvent((Display
*) wxGetDisplay(), & event
);
233 // This now waits until either an X event is received,
234 // or the select times out. So we should now process
235 // wxTimers in a reasonably timely fashion. However it
236 // does also mean that idle processing will happen more
237 // often, so we should probably limit idle processing to
238 // not be repeated more than every N milliseconds.
240 if (XPending((Display
*) wxGetDisplay()) == 0)
243 GR_TIMEOUT timeout
= 10; // Milliseconds
244 // Wait for next event, or timeout
245 GrGetNextEventTimeout(& event
, timeout
);
247 // Fall through to ProcessEvent.
248 // we'll assume that ProcessEvent will just ignore
249 // the event if there was a timeout and no event.
254 tv
.tv_usec
=10000; // TODO make this configurable
255 int fd
= ConnectionNumber((Display
*) wxGetDisplay());
258 FD_SET(fd
, &readset
);
259 if (select(fd
+1, &readset
, NULL
, NULL
, & tv
) == 0)
261 // Timed out, so no event to process
266 // An event was pending, so get it
267 XNextEvent((Display
*) wxGetDisplay(), & event
);
272 XNextEvent((Display
*) wxGetDisplay(), & event
);
275 (void) m_impl
->ProcessEvent(& event
);