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"
33 #include "wx/module.h"
34 #include "wx/x11/private.h"
40 // ----------------------------------------------------------------------------
42 // ----------------------------------------------------------------------------
44 typedef void (*wxSocketCallback
) (int fd
, void* data
);
46 class wxSocketTableEntry
: public wxObject
51 m_fdInput
= -1; m_fdOutput
= -1;
52 m_callbackInput
= NULL
; m_callbackOutput
= NULL
;
53 m_dataInput
= NULL
; m_dataOutput
= NULL
;
58 wxSocketCallback m_callbackInput
;
59 wxSocketCallback m_callbackOutput
;
65 { wxSocketTableInput
, wxSocketTableOutput
} wxSocketTableType
;
67 class wxSocketTable
: public wxHashTable
70 wxSocketTable(): wxHashTable(wxKEY_INTEGER
)
78 wxSocketTableEntry
* FindEntry(int fd
);
80 void RegisterCallback(int fd
, wxSocketTableType socketType
, wxSocketCallback callback
, void* data
);
82 void UnregisterCallback(int fd
, wxSocketTableType socketType
);
84 bool CallCallback(int fd
, wxSocketTableType socketType
);
86 void FillSets(fd_set
* readset
, fd_set
* writeset
, int* highest
);
88 void ProcessEvents(fd_set
* readset
, fd_set
* writeset
);
91 wxSocketTableEntry
* wxSocketTable::FindEntry(int fd
)
93 wxSocketTableEntry
* entry
= (wxSocketTableEntry
*) Get(fd
);
97 void wxSocketTable::RegisterCallback(int fd
, wxSocketTableType socketType
, wxSocketCallback callback
, void* data
)
99 wxSocketTableEntry
* entry
= FindEntry(fd
);
102 entry
= new wxSocketTableEntry();
106 if (socketType
== wxSocketTableInput
)
108 entry
->m_fdInput
= fd
;
109 entry
->m_dataInput
= data
;
110 entry
->m_callbackInput
= callback
;
114 entry
->m_fdOutput
= fd
;
115 entry
->m_dataOutput
= data
;
116 entry
->m_callbackOutput
= callback
;
120 void wxSocketTable::UnregisterCallback(int fd
, wxSocketTableType socketType
)
122 wxSocketTableEntry
* entry
= FindEntry(fd
);
125 if (socketType
== wxSocketTableInput
)
127 entry
->m_fdInput
= -1;
128 entry
->m_dataInput
= NULL
;
129 entry
->m_callbackInput
= NULL
;
133 entry
->m_fdOutput
= -1;
134 entry
->m_dataOutput
= NULL
;
135 entry
->m_callbackOutput
= NULL
;
137 if (entry
->m_fdInput
== -1 && entry
->m_fdOutput
== -1)
145 bool wxSocketTable::CallCallback(int fd
, wxSocketTableType socketType
)
147 wxSocketTableEntry
* entry
= FindEntry(fd
);
150 if (socketType
== wxSocketTableInput
)
152 if (entry
->m_fdInput
!= -1 && entry
->m_callbackInput
)
154 (entry
->m_callbackInput
) (entry
->m_fdInput
, entry
->m_dataInput
);
159 if (entry
->m_fdOutput
!= -1 && entry
->m_callbackOutput
)
161 (entry
->m_callbackOutput
) (entry
->m_fdOutput
, entry
->m_dataOutput
);
170 void wxSocketTable::FillSets(fd_set
* readset
, fd_set
* writeset
, int* highest
)
173 wxNode
* node
= Next();
176 wxSocketTableEntry
* entry
= (wxSocketTableEntry
*) node
->Data();
178 if (entry
->m_fdInput
!= -1)
180 FD_SET(entry
->m_fdInput
, readset
);
181 if (entry
->m_fdInput
> *highest
)
182 * highest
= entry
->m_fdInput
;
185 if (entry
->m_fdOutput
!= -1)
187 FD_SET(entry
->m_fdOutput
, writeset
);
188 if (entry
->m_fdOutput
> *highest
)
189 * highest
= entry
->m_fdOutput
;
196 void wxSocketTable::ProcessEvents(fd_set
* readset
, fd_set
* writeset
)
199 wxNode
* node
= Next();
202 wxSocketTableEntry
* entry
= (wxSocketTableEntry
*) node
->Data();
204 if (entry
->m_fdInput
!= -1 && FD_ISSET(entry
->m_fdInput
, readset
))
206 (entry
->m_callbackInput
) (entry
->m_fdInput
, entry
->m_dataInput
);
209 if (entry
->m_fdOutput
!= -1 && FD_ISSET(entry
->m_fdOutput
, writeset
))
211 (entry
->m_callbackOutput
) (entry
->m_fdOutput
, entry
->m_dataOutput
);
218 wxSocketTable
* wxTheSocketTable
= NULL
;
220 class wxSocketTableModule
: public wxModule
222 DECLARE_DYNAMIC_CLASS(wxSocketTableModule
)
224 wxSocketTableModule() {}
225 bool OnInit() { wxTheSocketTable
= new wxSocketTable
; return TRUE
; };
226 void OnExit() { delete wxTheSocketTable
; wxTheSocketTable
= NULL
; };
229 IMPLEMENT_DYNAMIC_CLASS(wxSocketTableModule
, wxModule
)
231 // Implement registration functions as C functions so they
232 // can be called from gsock11.c
234 extern "C" void wxRegisterSocketCallback(int fd
, wxSocketTableType socketType
, wxSocketCallback callback
, void* data
)
236 if (wxTheSocketTable
)
238 wxTheSocketTable
->RegisterCallback(fd
, socketType
, callback
, data
);
242 extern "C" void wxUnregisterSocketCallback(int fd
, wxSocketTableType socketType
)
244 if (wxTheSocketTable
)
246 wxTheSocketTable
->UnregisterCallback(fd
, socketType
);
250 // ----------------------------------------------------------------------------
252 // ----------------------------------------------------------------------------
254 class WXDLLEXPORT wxEventLoopImpl
258 wxEventLoopImpl() { SetExitCode(0); m_keepGoing
= FALSE
; }
260 // process an XEvent, return TRUE if it was processed
261 bool ProcessEvent(XEvent
* event
);
263 // generate an idle message, return TRUE if more idle time requested
264 bool SendIdleEvent();
266 // set/get the exit code
267 void SetExitCode(int exitcode
) { m_exitcode
= exitcode
; }
268 int GetExitCode() const { return m_exitcode
; }
271 // preprocess an event, return TRUE if processed (i.e. no further
272 // dispatching required)
273 bool PreProcessEvent(XEvent
* event
);
275 // the exit code of the event loop
281 // ============================================================================
282 // wxEventLoopImpl implementation
283 // ============================================================================
285 // ----------------------------------------------------------------------------
286 // wxEventLoopImpl message processing
287 // ----------------------------------------------------------------------------
289 bool wxEventLoopImpl::ProcessEvent(XEvent
*event
)
291 // give us the chance to preprocess the message first
292 if ( PreProcessEvent(event
) )
295 // if it wasn't done, dispatch it to the corresponding window
297 return wxTheApp
->ProcessXEvent((WXEvent
*) event
);
302 bool wxEventLoopImpl::PreProcessEvent(XEvent
*event
)
306 HWND hWnd
= msg
->hwnd
;
307 wxWindow
*wndThis
= wxGetWindowFromHWND((WXHWND
)hWnd
);
310 // try translations first; find the youngest window with a translation
313 for ( wnd
= wndThis
; wnd
; wnd
= wnd
->GetParent() )
315 if ( wnd
->MSWTranslateMessage((WXMSG
*)msg
) )
319 // Anyone for a non-translation message? Try youngest descendants first.
320 for ( wnd
= wndThis
; wnd
; wnd
= wnd
->GetParent() )
322 if ( wnd
->MSWProcessMessage((WXMSG
*)msg
) )
330 // ----------------------------------------------------------------------------
331 // wxEventLoopImpl idle event processing
332 // ----------------------------------------------------------------------------
334 bool wxEventLoopImpl::SendIdleEvent()
337 event
.SetEventObject(wxTheApp
);
339 return wxTheApp
->ProcessEvent(event
) && event
.MoreRequested();
342 // ============================================================================
343 // wxEventLoop implementation
344 // ============================================================================
346 wxEventLoop
*wxEventLoop::ms_activeLoop
= NULL
;
348 // ----------------------------------------------------------------------------
349 // wxEventLoop running and exiting
350 // ----------------------------------------------------------------------------
352 wxEventLoop::~wxEventLoop()
354 wxASSERT_MSG( !m_impl
, _T("should have been deleted in Run()") );
357 bool wxEventLoop::IsRunning() const
359 return m_impl
!= NULL
;
362 int wxEventLoop::Run()
364 // event loops are not recursive, you need to create another loop!
365 wxCHECK_MSG( !IsRunning(), -1, _T("can't reenter a message loop") );
367 m_impl
= new wxEventLoopImpl
;
369 wxEventLoop
*oldLoop
= ms_activeLoop
;
370 ms_activeLoop
= 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())
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();
412 ms_activeLoop
= oldLoop
;
417 void wxEventLoop::Exit(int rc
)
419 wxCHECK_RET( IsRunning(), _T("can't call Exit() if not running") );
421 m_impl
->SetExitCode(rc
);
422 m_impl
->m_keepGoing
= FALSE
;
425 // ----------------------------------------------------------------------------
426 // wxEventLoop message processing dispatching
427 // ----------------------------------------------------------------------------
429 bool wxEventLoop::Pending() const
431 XFlush((Display
*) wxGetDisplay());
432 return (XPending((Display
*) wxGetDisplay()) > 0);
435 bool wxEventLoop::Dispatch()
439 // TODO allowing for threads, as per e.g. wxMSW
441 // This now waits until either an X event is received,
442 // or the select times out. So we should now process
443 // wxTimers in a reasonably timely fashion. However it
444 // does also mean that idle processing will happen more
445 // often, so we should probably limit idle processing to
446 // not be repeated more than every N milliseconds.
448 if (XPending((Display
*) wxGetDisplay()) == 0)
451 GR_TIMEOUT timeout
= 10; // Milliseconds
452 // Wait for next event, or timeout
453 GrGetNextEventTimeout(& event
, timeout
);
455 // Fall through to ProcessEvent.
456 // we'll assume that ProcessEvent will just ignore
457 // the event if there was a timeout and no event.
462 tv
.tv_usec
=10000; // TODO make this configurable
463 int fd
= ConnectionNumber((Display
*) wxGetDisplay());
470 FD_SET(fd
, &readset
);
472 if (wxTheSocketTable
)
473 wxTheSocketTable
->FillSets(& readset
, & writeset
, & highest
);
475 if (select(highest
+1, &readset
, &writeset
, NULL
, & tv
) == 0)
477 // Timed out, so no event to process
482 // An X11 event was pending, so get it
483 if (FD_ISSET(fd
, & readset
))
484 XNextEvent((Display
*) wxGetDisplay(), & event
);
486 // Check if any socket events were pending,
487 // and if so, call their callbacks
488 if (wxTheSocketTable
)
489 wxTheSocketTable
->ProcessEvents(& readset
, & writeset
);
494 XNextEvent((Display
*) wxGetDisplay(), & event
);
497 (void) m_impl
->ProcessEvent(& event
);