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/tooltip.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>
50 // ----------------------------------------------------------------------------
52 // ----------------------------------------------------------------------------
54 typedef void (*wxSocketCallback
) (int fd
, void* data
);
56 class wxSocketTableEntry
: public wxObject
61 m_fdInput
= -1; m_fdOutput
= -1;
62 m_callbackInput
= NULL
; m_callbackOutput
= NULL
;
63 m_dataInput
= NULL
; m_dataOutput
= NULL
;
68 wxSocketCallback m_callbackInput
;
69 wxSocketCallback m_callbackOutput
;
75 { wxSocketTableInput
, wxSocketTableOutput
} wxSocketTableType
;
77 class wxSocketTable
: public wxHashTable
80 wxSocketTable(): wxHashTable(wxKEY_INTEGER
)
83 virtual ~wxSocketTable()
85 WX_CLEAR_HASH_TABLE(*this)
88 wxSocketTableEntry
* FindEntry(int fd
);
90 void RegisterCallback(int fd
, wxSocketTableType socketType
, wxSocketCallback callback
, void* data
);
92 void UnregisterCallback(int fd
, wxSocketTableType socketType
);
94 bool CallCallback(int fd
, wxSocketTableType socketType
);
96 void FillSets(fd_set
* readset
, fd_set
* writeset
, int* highest
);
98 void ProcessEvents(fd_set
* readset
, fd_set
* writeset
);
101 wxSocketTableEntry
* wxSocketTable::FindEntry(int fd
)
103 wxSocketTableEntry
* entry
= (wxSocketTableEntry
*) Get(fd
);
107 void wxSocketTable::RegisterCallback(int fd
, wxSocketTableType socketType
, wxSocketCallback callback
, void* data
)
109 wxSocketTableEntry
* entry
= FindEntry(fd
);
112 entry
= new wxSocketTableEntry();
116 if (socketType
== wxSocketTableInput
)
118 entry
->m_fdInput
= fd
;
119 entry
->m_dataInput
= data
;
120 entry
->m_callbackInput
= callback
;
124 entry
->m_fdOutput
= fd
;
125 entry
->m_dataOutput
= data
;
126 entry
->m_callbackOutput
= callback
;
130 void wxSocketTable::UnregisterCallback(int fd
, wxSocketTableType socketType
)
132 wxSocketTableEntry
* entry
= FindEntry(fd
);
135 if (socketType
== wxSocketTableInput
)
137 entry
->m_fdInput
= -1;
138 entry
->m_dataInput
= NULL
;
139 entry
->m_callbackInput
= NULL
;
143 entry
->m_fdOutput
= -1;
144 entry
->m_dataOutput
= NULL
;
145 entry
->m_callbackOutput
= NULL
;
147 if (entry
->m_fdInput
== -1 && entry
->m_fdOutput
== -1)
155 bool wxSocketTable::CallCallback(int fd
, wxSocketTableType socketType
)
157 wxSocketTableEntry
* entry
= FindEntry(fd
);
160 if (socketType
== wxSocketTableInput
)
162 if (entry
->m_fdInput
!= -1 && entry
->m_callbackInput
)
164 (entry
->m_callbackInput
) (entry
->m_fdInput
, entry
->m_dataInput
);
169 if (entry
->m_fdOutput
!= -1 && entry
->m_callbackOutput
)
171 (entry
->m_callbackOutput
) (entry
->m_fdOutput
, entry
->m_dataOutput
);
180 void wxSocketTable::FillSets(fd_set
* readset
, fd_set
* writeset
, int* highest
)
183 wxHashTable::compatibility_iterator node
= Next();
186 wxSocketTableEntry
* entry
= (wxSocketTableEntry
*) node
->GetData();
188 if (entry
->m_fdInput
!= -1)
190 wxFD_SET(entry
->m_fdInput
, readset
);
191 if (entry
->m_fdInput
> *highest
)
192 * highest
= entry
->m_fdInput
;
195 if (entry
->m_fdOutput
!= -1)
197 wxFD_SET(entry
->m_fdOutput
, writeset
);
198 if (entry
->m_fdOutput
> *highest
)
199 * highest
= entry
->m_fdOutput
;
206 void wxSocketTable::ProcessEvents(fd_set
* readset
, fd_set
* writeset
)
209 wxHashTable::compatibility_iterator node
= Next();
212 wxSocketTableEntry
* entry
= (wxSocketTableEntry
*) node
->GetData();
214 if (entry
->m_fdInput
!= -1 && wxFD_ISSET(entry
->m_fdInput
, readset
))
216 (entry
->m_callbackInput
) (entry
->m_fdInput
, entry
->m_dataInput
);
219 if (entry
->m_fdOutput
!= -1 && wxFD_ISSET(entry
->m_fdOutput
, writeset
))
221 (entry
->m_callbackOutput
) (entry
->m_fdOutput
, entry
->m_dataOutput
);
228 wxSocketTable
* wxTheSocketTable
= NULL
;
230 class wxSocketTableModule
: public wxModule
232 DECLARE_DYNAMIC_CLASS(wxSocketTableModule
)
234 wxSocketTableModule() {}
235 bool OnInit() { wxTheSocketTable
= new wxSocketTable
; return true; };
236 void OnExit() { delete wxTheSocketTable
; wxTheSocketTable
= NULL
; };
239 IMPLEMENT_DYNAMIC_CLASS(wxSocketTableModule
, wxModule
)
241 // Implement registration functions as C functions so they
242 // can be called from gsock11.c
244 extern "C" void wxRegisterSocketCallback(int fd
, wxSocketTableType socketType
, wxSocketCallback callback
, void* data
)
246 if (wxTheSocketTable
)
248 wxTheSocketTable
->RegisterCallback(fd
, socketType
, callback
, data
);
252 extern "C" void wxUnregisterSocketCallback(int fd
, wxSocketTableType socketType
)
254 if (wxTheSocketTable
)
256 wxTheSocketTable
->UnregisterCallback(fd
, socketType
);
261 // ----------------------------------------------------------------------------
263 // ----------------------------------------------------------------------------
265 class WXDLLEXPORT wxEventLoopImpl
269 wxEventLoopImpl() { SetExitCode(0); m_keepGoing
= false; }
271 // process an XEvent, return true if it was processed
272 bool ProcessEvent(XEvent
* event
);
274 // generate an idle message, return true if more idle time requested
275 bool SendIdleEvent();
277 // set/get the exit code
278 void SetExitCode(int exitcode
) { m_exitcode
= exitcode
; }
279 int GetExitCode() const { return m_exitcode
; }
282 // preprocess an event, return true if processed (i.e. no further
283 // dispatching required)
284 bool PreProcessEvent(XEvent
* event
);
286 // the exit code of the event loop
292 // ============================================================================
293 // wxEventLoopImpl implementation
294 // ============================================================================
296 // ----------------------------------------------------------------------------
297 // wxEventLoopImpl message processing
298 // ----------------------------------------------------------------------------
300 bool wxEventLoopImpl::ProcessEvent(XEvent
*event
)
302 // give us the chance to preprocess the message first
303 if ( PreProcessEvent(event
) )
306 // if it wasn't done, dispatch it to the corresponding window
308 return wxTheApp
->ProcessXEvent((WXEvent
*) event
);
313 bool wxEventLoopImpl::PreProcessEvent(XEvent
*event
)
317 HWND hWnd
= msg
->hwnd
;
318 wxWindow
*wndThis
= wxGetWindowFromHWND((WXHWND
)hWnd
);
321 // try translations first; find the youngest window with a translation
324 for ( wnd
= wndThis
; wnd
; wnd
= wnd
->GetParent() )
326 if ( wnd
->MSWTranslateMessage((WXMSG
*)msg
) )
330 // Anyone for a non-translation message? Try youngest descendants first.
331 for ( wnd
= wndThis
; wnd
; wnd
= wnd
->GetParent() )
333 if ( wnd
->MSWProcessMessage((WXMSG
*)msg
) )
341 // ----------------------------------------------------------------------------
342 // wxEventLoopImpl idle event processing
343 // ----------------------------------------------------------------------------
345 bool wxEventLoopImpl::SendIdleEvent()
347 return wxTheApp
->ProcessIdle();
350 // ============================================================================
351 // wxEventLoop implementation
352 // ============================================================================
354 // ----------------------------------------------------------------------------
355 // wxEventLoop running and exiting
356 // ----------------------------------------------------------------------------
358 wxEventLoop::~wxEventLoop()
360 wxASSERT_MSG( !m_impl
, _T("should have been deleted in Run()") );
363 int wxEventLoop::Run()
365 // event loops are not recursive, you need to create another loop!
366 wxCHECK_MSG( !IsRunning(), -1, _T("can't reenter a message loop") );
368 m_impl
= new wxEventLoopImpl
;
370 wxEventLoopActivator
activate(this);
372 m_impl
->m_keepGoing
= true;
373 while ( m_impl
->m_keepGoing
)
375 #if 0 // wxUSE_THREADS
376 wxMutexGuiLeaveOrEnter();
377 #endif // wxUSE_THREADS
379 // generate and process idle events for as long as we don't have
380 // anything else to do
381 while ( ! Pending() )
384 wxTimer::NotifyTimers(); // TODO: is this the correct place for it?
386 if (!m_impl
->SendIdleEvent())
388 #if 0 // wxUSE_THREADS
389 // leave the main loop to give other threads a chance to
390 // perform their GUI work
395 // Break out of while loop
400 // a message came or no more idle processing to do, sit in Dispatch()
401 // waiting for the next message
408 int exitcode
= m_impl
->GetExitCode();
415 void wxEventLoop::Exit(int rc
)
417 wxCHECK_RET( IsRunning(), _T("can't call Exit() if not running") );
419 m_impl
->SetExitCode(rc
);
420 m_impl
->m_keepGoing
= false;
423 // ----------------------------------------------------------------------------
424 // wxEventLoop message processing dispatching
425 // ----------------------------------------------------------------------------
427 bool wxEventLoop::Pending() const
429 XFlush( wxGlobalDisplay() );
430 return (XPending( wxGlobalDisplay() ) > 0);
433 bool wxEventLoop::Dispatch()
437 // Start off by checking if any of our child processes have finished.
438 wxCheckForFinishedChildren();
440 // TODO allowing for threads, as per e.g. wxMSW
442 // This now waits until either an X event is received,
443 // or the select times out. So we should now process
444 // wxTimers in a reasonably timely fashion. However it
445 // does also mean that idle processing will happen more
446 // often, so we should probably limit idle processing to
447 // not be repeated more than every N milliseconds.
449 if (XPending( wxGlobalDisplay() ) == 0)
452 GR_TIMEOUT timeout
= 10; // Milliseconds
453 // Wait for next event, or timeout
454 GrGetNextEventTimeout(& event
, timeout
);
456 // Fall through to ProcessEvent.
457 // we'll assume that ProcessEvent will just ignore
458 // the event if there was a timeout and no event.
463 tv
.tv_usec
=10000; // TODO make this configurable
464 int fd
= ConnectionNumber( wxGlobalDisplay() );
470 wxFD_ZERO(&writeset
);
472 wxFD_SET(fd
, &readset
);
475 if (wxTheSocketTable
)
476 wxTheSocketTable
->FillSets( &readset
, &writeset
, &highest
);
479 if (select( highest
+1, &readset
, &writeset
, NULL
, &tv
) == 0)
481 // Timed out, so no event to process
486 // An X11 event was pending, so get it
487 if (wxFD_ISSET( fd
, &readset
))
488 XNextEvent( wxGlobalDisplay(), &event
);
491 // Check if any socket events were pending,
492 // and if so, call their callbacks
493 if (wxTheSocketTable
)
494 wxTheSocketTable
->ProcessEvents( &readset
, &writeset
);
501 XNextEvent( wxGlobalDisplay(), &event
);
505 (void) m_impl
->ProcessEvent( &event
);