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 // ----------------------------------------------------------------------------
20 #include "wx/window.h"
22 #include "wx/evtloop.h"
23 #include "wx/tooltip.h"
25 #include "wx/thread.h"
29 #include "wx/module.h"
30 #include "wx/unix/private.h"
31 #include "wx/x11/private.h"
38 // ----------------------------------------------------------------------------
40 // ----------------------------------------------------------------------------
42 typedef void (*wxSocketCallback
) (int fd
, void* data
);
44 class wxSocketTableEntry
: public wxObject
49 m_fdInput
= -1; m_fdOutput
= -1;
50 m_callbackInput
= NULL
; m_callbackOutput
= NULL
;
51 m_dataInput
= NULL
; m_dataOutput
= NULL
;
56 wxSocketCallback m_callbackInput
;
57 wxSocketCallback m_callbackOutput
;
63 { wxSocketTableInput
, wxSocketTableOutput
} wxSocketTableType
;
65 class wxSocketTable
: public wxHashTable
68 wxSocketTable(): wxHashTable(wxKEY_INTEGER
)
73 WX_CLEAR_HASH_TABLE(*this)
76 wxSocketTableEntry
* FindEntry(int fd
);
78 void RegisterCallback(int fd
, wxSocketTableType socketType
, wxSocketCallback callback
, void* data
);
80 void UnregisterCallback(int fd
, wxSocketTableType socketType
);
82 bool CallCallback(int fd
, wxSocketTableType socketType
);
84 void FillSets(fd_set
* readset
, fd_set
* writeset
, int* highest
);
86 void ProcessEvents(fd_set
* readset
, fd_set
* writeset
);
89 wxSocketTableEntry
* wxSocketTable::FindEntry(int fd
)
91 wxSocketTableEntry
* entry
= (wxSocketTableEntry
*) Get(fd
);
95 void wxSocketTable::RegisterCallback(int fd
, wxSocketTableType socketType
, wxSocketCallback callback
, void* data
)
97 wxSocketTableEntry
* entry
= FindEntry(fd
);
100 entry
= new wxSocketTableEntry();
104 if (socketType
== wxSocketTableInput
)
106 entry
->m_fdInput
= fd
;
107 entry
->m_dataInput
= data
;
108 entry
->m_callbackInput
= callback
;
112 entry
->m_fdOutput
= fd
;
113 entry
->m_dataOutput
= data
;
114 entry
->m_callbackOutput
= callback
;
118 void wxSocketTable::UnregisterCallback(int fd
, wxSocketTableType socketType
)
120 wxSocketTableEntry
* entry
= FindEntry(fd
);
123 if (socketType
== wxSocketTableInput
)
125 entry
->m_fdInput
= -1;
126 entry
->m_dataInput
= NULL
;
127 entry
->m_callbackInput
= NULL
;
131 entry
->m_fdOutput
= -1;
132 entry
->m_dataOutput
= NULL
;
133 entry
->m_callbackOutput
= NULL
;
135 if (entry
->m_fdInput
== -1 && entry
->m_fdOutput
== -1)
143 bool wxSocketTable::CallCallback(int fd
, wxSocketTableType socketType
)
145 wxSocketTableEntry
* entry
= FindEntry(fd
);
148 if (socketType
== wxSocketTableInput
)
150 if (entry
->m_fdInput
!= -1 && entry
->m_callbackInput
)
152 (entry
->m_callbackInput
) (entry
->m_fdInput
, entry
->m_dataInput
);
157 if (entry
->m_fdOutput
!= -1 && entry
->m_callbackOutput
)
159 (entry
->m_callbackOutput
) (entry
->m_fdOutput
, entry
->m_dataOutput
);
168 void wxSocketTable::FillSets(fd_set
* readset
, fd_set
* writeset
, int* highest
)
171 wxHashTable::compatibility_iterator node
= Next();
174 wxSocketTableEntry
* entry
= (wxSocketTableEntry
*) node
->GetData();
176 if (entry
->m_fdInput
!= -1)
178 wxFD_SET(entry
->m_fdInput
, readset
);
179 if (entry
->m_fdInput
> *highest
)
180 * highest
= entry
->m_fdInput
;
183 if (entry
->m_fdOutput
!= -1)
185 wxFD_SET(entry
->m_fdOutput
, writeset
);
186 if (entry
->m_fdOutput
> *highest
)
187 * highest
= entry
->m_fdOutput
;
194 void wxSocketTable::ProcessEvents(fd_set
* readset
, fd_set
* writeset
)
197 wxHashTable::compatibility_iterator node
= Next();
200 wxSocketTableEntry
* entry
= (wxSocketTableEntry
*) node
->GetData();
202 if (entry
->m_fdInput
!= -1 && wxFD_ISSET(entry
->m_fdInput
, readset
))
204 (entry
->m_callbackInput
) (entry
->m_fdInput
, entry
->m_dataInput
);
207 if (entry
->m_fdOutput
!= -1 && wxFD_ISSET(entry
->m_fdOutput
, writeset
))
209 (entry
->m_callbackOutput
) (entry
->m_fdOutput
, entry
->m_dataOutput
);
216 wxSocketTable
* wxTheSocketTable
= NULL
;
218 class wxSocketTableModule
: public wxModule
220 DECLARE_DYNAMIC_CLASS(wxSocketTableModule
)
222 wxSocketTableModule() {}
223 bool OnInit() { wxTheSocketTable
= new wxSocketTable
; return TRUE
; };
224 void OnExit() { delete wxTheSocketTable
; wxTheSocketTable
= NULL
; };
227 IMPLEMENT_DYNAMIC_CLASS(wxSocketTableModule
, wxModule
)
229 // Implement registration functions as C functions so they
230 // can be called from gsock11.c
232 extern "C" void wxRegisterSocketCallback(int fd
, wxSocketTableType socketType
, wxSocketCallback callback
, void* data
)
234 if (wxTheSocketTable
)
236 wxTheSocketTable
->RegisterCallback(fd
, socketType
, callback
, data
);
240 extern "C" void wxUnregisterSocketCallback(int fd
, wxSocketTableType socketType
)
242 if (wxTheSocketTable
)
244 wxTheSocketTable
->UnregisterCallback(fd
, socketType
);
249 // ----------------------------------------------------------------------------
251 // ----------------------------------------------------------------------------
253 class WXDLLEXPORT wxEventLoopImpl
257 wxEventLoopImpl() { SetExitCode(0); m_keepGoing
= FALSE
; }
259 // process an XEvent, return TRUE if it was processed
260 bool ProcessEvent(XEvent
* event
);
262 // generate an idle message, return TRUE if more idle time requested
263 bool SendIdleEvent();
265 // set/get the exit code
266 void SetExitCode(int exitcode
) { m_exitcode
= exitcode
; }
267 int GetExitCode() const { return m_exitcode
; }
270 // preprocess an event, return TRUE if processed (i.e. no further
271 // dispatching required)
272 bool PreProcessEvent(XEvent
* event
);
274 // the exit code of the event loop
280 // ============================================================================
281 // wxEventLoopImpl implementation
282 // ============================================================================
284 // ----------------------------------------------------------------------------
285 // wxEventLoopImpl message processing
286 // ----------------------------------------------------------------------------
288 bool wxEventLoopImpl::ProcessEvent(XEvent
*event
)
290 // give us the chance to preprocess the message first
291 if ( PreProcessEvent(event
) )
294 // if it wasn't done, dispatch it to the corresponding window
296 return wxTheApp
->ProcessXEvent((WXEvent
*) event
);
301 bool wxEventLoopImpl::PreProcessEvent(XEvent
*event
)
305 HWND hWnd
= msg
->hwnd
;
306 wxWindow
*wndThis
= wxGetWindowFromHWND((WXHWND
)hWnd
);
309 // try translations first; find the youngest window with a translation
312 for ( wnd
= wndThis
; wnd
; wnd
= wnd
->GetParent() )
314 if ( wnd
->MSWTranslateMessage((WXMSG
*)msg
) )
318 // Anyone for a non-translation message? Try youngest descendants first.
319 for ( wnd
= wndThis
; wnd
; wnd
= wnd
->GetParent() )
321 if ( wnd
->MSWProcessMessage((WXMSG
*)msg
) )
329 // ----------------------------------------------------------------------------
330 // wxEventLoopImpl idle event processing
331 // ----------------------------------------------------------------------------
333 bool wxEventLoopImpl::SendIdleEvent()
335 return wxTheApp
->ProcessIdle();
338 // ============================================================================
339 // wxEventLoop implementation
340 // ============================================================================
342 wxEventLoop
*wxEventLoopBase::ms_activeLoop
= NULL
;
344 // ----------------------------------------------------------------------------
345 // wxEventLoop running and exiting
346 // ----------------------------------------------------------------------------
348 wxEventLoop::~wxEventLoop()
350 wxASSERT_MSG( !m_impl
, _T("should have been deleted in Run()") );
353 int wxEventLoop::Run()
355 // event loops are not recursive, you need to create another loop!
356 wxCHECK_MSG( !IsRunning(), -1, _T("can't reenter a message loop") );
358 m_impl
= new wxEventLoopImpl
;
360 wxEventLoopActivator
activate(this);
362 m_impl
->m_keepGoing
= TRUE
;
363 while ( m_impl
->m_keepGoing
)
365 #if 0 // wxUSE_THREADS
366 wxMutexGuiLeaveOrEnter();
367 #endif // wxUSE_THREADS
369 // generate and process idle events for as long as we don't have
370 // anything else to do
371 while ( ! Pending() )
374 wxTimer::NotifyTimers(); // TODO: is this the correct place for it?
376 if (!m_impl
->SendIdleEvent())
378 #if 0 // wxUSE_THREADS
379 // leave the main loop to give other threads a chance to
380 // perform their GUI work
385 // Break out of while loop
390 // a message came or no more idle processing to do, sit in Dispatch()
391 // waiting for the next message
398 int exitcode
= m_impl
->GetExitCode();
405 void wxEventLoop::Exit(int rc
)
407 wxCHECK_RET( IsRunning(), _T("can't call Exit() if not running") );
409 m_impl
->SetExitCode(rc
);
410 m_impl
->m_keepGoing
= FALSE
;
413 // ----------------------------------------------------------------------------
414 // wxEventLoop message processing dispatching
415 // ----------------------------------------------------------------------------
417 bool wxEventLoop::Pending() const
419 XFlush( wxGlobalDisplay() );
420 return (XPending( wxGlobalDisplay() ) > 0);
423 bool wxEventLoop::Dispatch()
427 // TODO allowing for threads, as per e.g. wxMSW
429 // This now waits until either an X event is received,
430 // or the select times out. So we should now process
431 // wxTimers in a reasonably timely fashion. However it
432 // does also mean that idle processing will happen more
433 // often, so we should probably limit idle processing to
434 // not be repeated more than every N milliseconds.
436 if (XPending( wxGlobalDisplay() ) == 0)
439 GR_TIMEOUT timeout
= 10; // Milliseconds
440 // Wait for next event, or timeout
441 GrGetNextEventTimeout(& event
, timeout
);
443 // Fall through to ProcessEvent.
444 // we'll assume that ProcessEvent will just ignore
445 // the event if there was a timeout and no event.
450 tv
.tv_usec
=10000; // TODO make this configurable
451 int fd
= ConnectionNumber( wxGlobalDisplay() );
457 wxFD_ZERO(&writeset
);
459 wxFD_SET(fd
, &readset
);
462 if (wxTheSocketTable
)
463 wxTheSocketTable
->FillSets( &readset
, &writeset
, &highest
);
466 if (select( highest
+1, &readset
, &writeset
, NULL
, &tv
) == 0)
468 // Timed out, so no event to process
473 // An X11 event was pending, so get it
474 if (wxFD_ISSET( fd
, &readset
))
475 XNextEvent( wxGlobalDisplay(), &event
);
478 // Check if any socket events were pending,
479 // and if so, call their callbacks
480 if (wxTheSocketTable
)
481 wxTheSocketTable
->ProcessEvents( &readset
, &writeset
);
488 XNextEvent( wxGlobalDisplay(), &event
);
492 (void) m_impl
->ProcessEvent( &event
);