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"
27 #include "wx/window.h"
29 #include "wx/evtloop.h"
30 #include "wx/tooltip.h"
32 #include "wx/module.h"
33 #include "wx/unix/private.h"
34 #include "wx/x11/private.h"
38 #include "wx/thread.h"
45 // ----------------------------------------------------------------------------
47 // ----------------------------------------------------------------------------
49 typedef void (*wxSocketCallback
) (int fd
, void* data
);
51 class wxSocketTableEntry
: public wxObject
56 m_fdInput
= -1; m_fdOutput
= -1;
57 m_callbackInput
= NULL
; m_callbackOutput
= NULL
;
58 m_dataInput
= NULL
; m_dataOutput
= NULL
;
63 wxSocketCallback m_callbackInput
;
64 wxSocketCallback m_callbackOutput
;
70 { wxSocketTableInput
, wxSocketTableOutput
} wxSocketTableType
;
72 class wxSocketTable
: public wxHashTable
75 wxSocketTable(): wxHashTable(wxKEY_INTEGER
)
80 WX_CLEAR_HASH_TABLE(*this)
83 wxSocketTableEntry
* FindEntry(int fd
);
85 void RegisterCallback(int fd
, wxSocketTableType socketType
, wxSocketCallback callback
, void* data
);
87 void UnregisterCallback(int fd
, wxSocketTableType socketType
);
89 bool CallCallback(int fd
, wxSocketTableType socketType
);
91 void FillSets(fd_set
* readset
, fd_set
* writeset
, int* highest
);
93 void ProcessEvents(fd_set
* readset
, fd_set
* writeset
);
96 wxSocketTableEntry
* wxSocketTable::FindEntry(int fd
)
98 wxSocketTableEntry
* entry
= (wxSocketTableEntry
*) Get(fd
);
102 void wxSocketTable::RegisterCallback(int fd
, wxSocketTableType socketType
, wxSocketCallback callback
, void* data
)
104 wxSocketTableEntry
* entry
= FindEntry(fd
);
107 entry
= new wxSocketTableEntry();
111 if (socketType
== wxSocketTableInput
)
113 entry
->m_fdInput
= fd
;
114 entry
->m_dataInput
= data
;
115 entry
->m_callbackInput
= callback
;
119 entry
->m_fdOutput
= fd
;
120 entry
->m_dataOutput
= data
;
121 entry
->m_callbackOutput
= callback
;
125 void wxSocketTable::UnregisterCallback(int fd
, wxSocketTableType socketType
)
127 wxSocketTableEntry
* entry
= FindEntry(fd
);
130 if (socketType
== wxSocketTableInput
)
132 entry
->m_fdInput
= -1;
133 entry
->m_dataInput
= NULL
;
134 entry
->m_callbackInput
= NULL
;
138 entry
->m_fdOutput
= -1;
139 entry
->m_dataOutput
= NULL
;
140 entry
->m_callbackOutput
= NULL
;
142 if (entry
->m_fdInput
== -1 && entry
->m_fdOutput
== -1)
150 bool wxSocketTable::CallCallback(int fd
, wxSocketTableType socketType
)
152 wxSocketTableEntry
* entry
= FindEntry(fd
);
155 if (socketType
== wxSocketTableInput
)
157 if (entry
->m_fdInput
!= -1 && entry
->m_callbackInput
)
159 (entry
->m_callbackInput
) (entry
->m_fdInput
, entry
->m_dataInput
);
164 if (entry
->m_fdOutput
!= -1 && entry
->m_callbackOutput
)
166 (entry
->m_callbackOutput
) (entry
->m_fdOutput
, entry
->m_dataOutput
);
175 void wxSocketTable::FillSets(fd_set
* readset
, fd_set
* writeset
, int* highest
)
178 wxHashTable::compatibility_iterator node
= Next();
181 wxSocketTableEntry
* entry
= (wxSocketTableEntry
*) node
->GetData();
183 if (entry
->m_fdInput
!= -1)
185 wxFD_SET(entry
->m_fdInput
, readset
);
186 if (entry
->m_fdInput
> *highest
)
187 * highest
= entry
->m_fdInput
;
190 if (entry
->m_fdOutput
!= -1)
192 wxFD_SET(entry
->m_fdOutput
, writeset
);
193 if (entry
->m_fdOutput
> *highest
)
194 * highest
= entry
->m_fdOutput
;
201 void wxSocketTable::ProcessEvents(fd_set
* readset
, fd_set
* writeset
)
204 wxHashTable::compatibility_iterator node
= Next();
207 wxSocketTableEntry
* entry
= (wxSocketTableEntry
*) node
->GetData();
209 if (entry
->m_fdInput
!= -1 && wxFD_ISSET(entry
->m_fdInput
, readset
))
211 (entry
->m_callbackInput
) (entry
->m_fdInput
, entry
->m_dataInput
);
214 if (entry
->m_fdOutput
!= -1 && wxFD_ISSET(entry
->m_fdOutput
, writeset
))
216 (entry
->m_callbackOutput
) (entry
->m_fdOutput
, entry
->m_dataOutput
);
223 wxSocketTable
* wxTheSocketTable
= NULL
;
225 class wxSocketTableModule
: public wxModule
227 DECLARE_DYNAMIC_CLASS(wxSocketTableModule
)
229 wxSocketTableModule() {}
230 bool OnInit() { wxTheSocketTable
= new wxSocketTable
; return true; };
231 void OnExit() { delete wxTheSocketTable
; wxTheSocketTable
= NULL
; };
234 IMPLEMENT_DYNAMIC_CLASS(wxSocketTableModule
, wxModule
)
236 // Implement registration functions as C functions so they
237 // can be called from gsock11.c
239 extern "C" void wxRegisterSocketCallback(int fd
, wxSocketTableType socketType
, wxSocketCallback callback
, void* data
)
241 if (wxTheSocketTable
)
243 wxTheSocketTable
->RegisterCallback(fd
, socketType
, callback
, data
);
247 extern "C" void wxUnregisterSocketCallback(int fd
, wxSocketTableType socketType
)
249 if (wxTheSocketTable
)
251 wxTheSocketTable
->UnregisterCallback(fd
, socketType
);
256 // ----------------------------------------------------------------------------
258 // ----------------------------------------------------------------------------
260 class WXDLLEXPORT wxEventLoopImpl
264 wxEventLoopImpl() { SetExitCode(0); m_keepGoing
= false; }
266 // process an XEvent, return true if it was processed
267 bool ProcessEvent(XEvent
* event
);
269 // generate an idle message, return true if more idle time requested
270 bool SendIdleEvent();
272 // set/get the exit code
273 void SetExitCode(int exitcode
) { m_exitcode
= exitcode
; }
274 int GetExitCode() const { return m_exitcode
; }
277 // preprocess an event, return true if processed (i.e. no further
278 // dispatching required)
279 bool PreProcessEvent(XEvent
* event
);
281 // the exit code of the event loop
287 // ============================================================================
288 // wxEventLoopImpl implementation
289 // ============================================================================
291 // ----------------------------------------------------------------------------
292 // wxEventLoopImpl message processing
293 // ----------------------------------------------------------------------------
295 bool wxEventLoopImpl::ProcessEvent(XEvent
*event
)
297 // give us the chance to preprocess the message first
298 if ( PreProcessEvent(event
) )
301 // if it wasn't done, dispatch it to the corresponding window
303 return wxTheApp
->ProcessXEvent((WXEvent
*) event
);
308 bool wxEventLoopImpl::PreProcessEvent(XEvent
*event
)
312 HWND hWnd
= msg
->hwnd
;
313 wxWindow
*wndThis
= wxGetWindowFromHWND((WXHWND
)hWnd
);
316 // try translations first; find the youngest window with a translation
319 for ( wnd
= wndThis
; wnd
; wnd
= wnd
->GetParent() )
321 if ( wnd
->MSWTranslateMessage((WXMSG
*)msg
) )
325 // Anyone for a non-translation message? Try youngest descendants first.
326 for ( wnd
= wndThis
; wnd
; wnd
= wnd
->GetParent() )
328 if ( wnd
->MSWProcessMessage((WXMSG
*)msg
) )
336 // ----------------------------------------------------------------------------
337 // wxEventLoopImpl idle event processing
338 // ----------------------------------------------------------------------------
340 bool wxEventLoopImpl::SendIdleEvent()
342 return wxTheApp
->ProcessIdle();
345 // ============================================================================
346 // wxEventLoop implementation
347 // ============================================================================
349 // ----------------------------------------------------------------------------
350 // wxEventLoop running and exiting
351 // ----------------------------------------------------------------------------
353 wxEventLoop::~wxEventLoop()
355 wxASSERT_MSG( !m_impl
, _T("should have been deleted in Run()") );
358 int wxEventLoop::Run()
360 // event loops are not recursive, you need to create another loop!
361 wxCHECK_MSG( !IsRunning(), -1, _T("can't reenter a message loop") );
363 m_impl
= new wxEventLoopImpl
;
365 wxEventLoopActivator
activate(this);
367 m_impl
->m_keepGoing
= true;
368 while ( m_impl
->m_keepGoing
)
370 #if 0 // wxUSE_THREADS
371 wxMutexGuiLeaveOrEnter();
372 #endif // wxUSE_THREADS
374 // generate and process idle events for as long as we don't have
375 // anything else to do
376 while ( ! Pending() )
379 wxTimer::NotifyTimers(); // TODO: is this the correct place for it?
381 if (!m_impl
->SendIdleEvent())
383 #if 0 // wxUSE_THREADS
384 // leave the main loop to give other threads a chance to
385 // perform their GUI work
390 // Break out of while loop
395 // a message came or no more idle processing to do, sit in Dispatch()
396 // waiting for the next message
403 int exitcode
= m_impl
->GetExitCode();
410 void wxEventLoop::Exit(int rc
)
412 wxCHECK_RET( IsRunning(), _T("can't call Exit() if not running") );
414 m_impl
->SetExitCode(rc
);
415 m_impl
->m_keepGoing
= false;
418 // ----------------------------------------------------------------------------
419 // wxEventLoop message processing dispatching
420 // ----------------------------------------------------------------------------
422 bool wxEventLoop::Pending() const
424 XFlush( wxGlobalDisplay() );
425 return (XPending( wxGlobalDisplay() ) > 0);
428 bool wxEventLoop::Dispatch()
432 // TODO allowing for threads, as per e.g. wxMSW
434 // This now waits until either an X event is received,
435 // or the select times out. So we should now process
436 // wxTimers in a reasonably timely fashion. However it
437 // does also mean that idle processing will happen more
438 // often, so we should probably limit idle processing to
439 // not be repeated more than every N milliseconds.
441 if (XPending( wxGlobalDisplay() ) == 0)
444 GR_TIMEOUT timeout
= 10; // Milliseconds
445 // Wait for next event, or timeout
446 GrGetNextEventTimeout(& event
, timeout
);
448 // Fall through to ProcessEvent.
449 // we'll assume that ProcessEvent will just ignore
450 // the event if there was a timeout and no event.
455 tv
.tv_usec
=10000; // TODO make this configurable
456 int fd
= ConnectionNumber( wxGlobalDisplay() );
462 wxFD_ZERO(&writeset
);
464 wxFD_SET(fd
, &readset
);
467 if (wxTheSocketTable
)
468 wxTheSocketTable
->FillSets( &readset
, &writeset
, &highest
);
471 if (select( highest
+1, &readset
, &writeset
, NULL
, &tv
) == 0)
473 // Timed out, so no event to process
478 // An X11 event was pending, so get it
479 if (wxFD_ISSET( fd
, &readset
))
480 XNextEvent( wxGlobalDisplay(), &event
);
483 // Check if any socket events were pending,
484 // and if so, call their callbacks
485 if (wxTheSocketTable
)
486 wxTheSocketTable
->ProcessEvents( &readset
, &writeset
);
493 XNextEvent( wxGlobalDisplay(), &event
);
497 (void) m_impl
->ProcessEvent( &event
);