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 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
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"
41 // ----------------------------------------------------------------------------
43 // ----------------------------------------------------------------------------
45 typedef void (*wxSocketCallback
) (int fd
, void* data
);
47 class wxSocketTableEntry
: public wxObject
52 m_fdInput
= -1; m_fdOutput
= -1;
53 m_callbackInput
= NULL
; m_callbackOutput
= NULL
;
54 m_dataInput
= NULL
; m_dataOutput
= NULL
;
59 wxSocketCallback m_callbackInput
;
60 wxSocketCallback m_callbackOutput
;
66 { wxSocketTableInput
, wxSocketTableOutput
} wxSocketTableType
;
68 class wxSocketTable
: public wxHashTable
71 wxSocketTable(): wxHashTable(wxKEY_INTEGER
)
76 WX_CLEAR_HASH_TABLE(*this)
79 wxSocketTableEntry
* FindEntry(int fd
);
81 void RegisterCallback(int fd
, wxSocketTableType socketType
, wxSocketCallback callback
, void* data
);
83 void UnregisterCallback(int fd
, wxSocketTableType socketType
);
85 bool CallCallback(int fd
, wxSocketTableType socketType
);
87 void FillSets(fd_set
* readset
, fd_set
* writeset
, int* highest
);
89 void ProcessEvents(fd_set
* readset
, fd_set
* writeset
);
92 wxSocketTableEntry
* wxSocketTable::FindEntry(int fd
)
94 wxSocketTableEntry
* entry
= (wxSocketTableEntry
*) Get(fd
);
98 void wxSocketTable::RegisterCallback(int fd
, wxSocketTableType socketType
, wxSocketCallback callback
, void* data
)
100 wxSocketTableEntry
* entry
= FindEntry(fd
);
103 entry
= new wxSocketTableEntry();
107 if (socketType
== wxSocketTableInput
)
109 entry
->m_fdInput
= fd
;
110 entry
->m_dataInput
= data
;
111 entry
->m_callbackInput
= callback
;
115 entry
->m_fdOutput
= fd
;
116 entry
->m_dataOutput
= data
;
117 entry
->m_callbackOutput
= callback
;
121 void wxSocketTable::UnregisterCallback(int fd
, wxSocketTableType socketType
)
123 wxSocketTableEntry
* entry
= FindEntry(fd
);
126 if (socketType
== wxSocketTableInput
)
128 entry
->m_fdInput
= -1;
129 entry
->m_dataInput
= NULL
;
130 entry
->m_callbackInput
= NULL
;
134 entry
->m_fdOutput
= -1;
135 entry
->m_dataOutput
= NULL
;
136 entry
->m_callbackOutput
= NULL
;
138 if (entry
->m_fdInput
== -1 && entry
->m_fdOutput
== -1)
146 bool wxSocketTable::CallCallback(int fd
, wxSocketTableType socketType
)
148 wxSocketTableEntry
* entry
= FindEntry(fd
);
151 if (socketType
== wxSocketTableInput
)
153 if (entry
->m_fdInput
!= -1 && entry
->m_callbackInput
)
155 (entry
->m_callbackInput
) (entry
->m_fdInput
, entry
->m_dataInput
);
160 if (entry
->m_fdOutput
!= -1 && entry
->m_callbackOutput
)
162 (entry
->m_callbackOutput
) (entry
->m_fdOutput
, entry
->m_dataOutput
);
171 void wxSocketTable::FillSets(fd_set
* readset
, fd_set
* writeset
, int* highest
)
174 wxHashTable::compatibility_iterator node
= Next();
177 wxSocketTableEntry
* entry
= (wxSocketTableEntry
*) node
->GetData();
179 if (entry
->m_fdInput
!= -1)
181 FD_SET(entry
->m_fdInput
, readset
);
182 if (entry
->m_fdInput
> *highest
)
183 * highest
= entry
->m_fdInput
;
186 if (entry
->m_fdOutput
!= -1)
188 FD_SET(entry
->m_fdOutput
, writeset
);
189 if (entry
->m_fdOutput
> *highest
)
190 * highest
= entry
->m_fdOutput
;
197 void wxSocketTable::ProcessEvents(fd_set
* readset
, fd_set
* writeset
)
200 wxHashTable::compatibility_iterator node
= Next();
203 wxSocketTableEntry
* entry
= (wxSocketTableEntry
*) node
->GetData();
205 if (entry
->m_fdInput
!= -1 && FD_ISSET(entry
->m_fdInput
, readset
))
207 (entry
->m_callbackInput
) (entry
->m_fdInput
, entry
->m_dataInput
);
210 if (entry
->m_fdOutput
!= -1 && FD_ISSET(entry
->m_fdOutput
, writeset
))
212 (entry
->m_callbackOutput
) (entry
->m_fdOutput
, entry
->m_dataOutput
);
219 wxSocketTable
* wxTheSocketTable
= NULL
;
221 class wxSocketTableModule
: public wxModule
223 DECLARE_DYNAMIC_CLASS(wxSocketTableModule
)
225 wxSocketTableModule() {}
226 bool OnInit() { wxTheSocketTable
= new wxSocketTable
; return TRUE
; };
227 void OnExit() { delete wxTheSocketTable
; wxTheSocketTable
= NULL
; };
230 IMPLEMENT_DYNAMIC_CLASS(wxSocketTableModule
, wxModule
)
232 // Implement registration functions as C functions so they
233 // can be called from gsock11.c
235 extern "C" void wxRegisterSocketCallback(int fd
, wxSocketTableType socketType
, wxSocketCallback callback
, void* data
)
237 if (wxTheSocketTable
)
239 wxTheSocketTable
->RegisterCallback(fd
, socketType
, callback
, data
);
243 extern "C" void wxUnregisterSocketCallback(int fd
, wxSocketTableType socketType
)
245 if (wxTheSocketTable
)
247 wxTheSocketTable
->UnregisterCallback(fd
, socketType
);
252 // ----------------------------------------------------------------------------
254 // ----------------------------------------------------------------------------
256 class WXDLLEXPORT wxEventLoopImpl
260 wxEventLoopImpl() { SetExitCode(0); m_keepGoing
= FALSE
; }
262 // process an XEvent, return TRUE if it was processed
263 bool ProcessEvent(XEvent
* event
);
265 // generate an idle message, return TRUE if more idle time requested
266 bool SendIdleEvent();
268 // set/get the exit code
269 void SetExitCode(int exitcode
) { m_exitcode
= exitcode
; }
270 int GetExitCode() const { return m_exitcode
; }
273 // preprocess an event, return TRUE if processed (i.e. no further
274 // dispatching required)
275 bool PreProcessEvent(XEvent
* event
);
277 // the exit code of the event loop
283 // ============================================================================
284 // wxEventLoopImpl implementation
285 // ============================================================================
287 // ----------------------------------------------------------------------------
288 // wxEventLoopImpl message processing
289 // ----------------------------------------------------------------------------
291 bool wxEventLoopImpl::ProcessEvent(XEvent
*event
)
293 // give us the chance to preprocess the message first
294 if ( PreProcessEvent(event
) )
297 // if it wasn't done, dispatch it to the corresponding window
299 return wxTheApp
->ProcessXEvent((WXEvent
*) event
);
304 bool wxEventLoopImpl::PreProcessEvent(XEvent
*event
)
308 HWND hWnd
= msg
->hwnd
;
309 wxWindow
*wndThis
= wxGetWindowFromHWND((WXHWND
)hWnd
);
312 // try translations first; find the youngest window with a translation
315 for ( wnd
= wndThis
; wnd
; wnd
= wnd
->GetParent() )
317 if ( wnd
->MSWTranslateMessage((WXMSG
*)msg
) )
321 // Anyone for a non-translation message? Try youngest descendants first.
322 for ( wnd
= wndThis
; wnd
; wnd
= wnd
->GetParent() )
324 if ( wnd
->MSWProcessMessage((WXMSG
*)msg
) )
332 // ----------------------------------------------------------------------------
333 // wxEventLoopImpl idle event processing
334 // ----------------------------------------------------------------------------
336 bool wxEventLoopImpl::SendIdleEvent()
338 return wxTheApp
->ProcessIdle();
341 // ============================================================================
342 // wxEventLoop implementation
343 // ============================================================================
345 wxEventLoop
*wxEventLoopBase::ms_activeLoop
= NULL
;
347 // ----------------------------------------------------------------------------
348 // wxEventLoop running and exiting
349 // ----------------------------------------------------------------------------
351 wxEventLoop::~wxEventLoop()
353 wxASSERT_MSG( !m_impl
, _T("should have been deleted in Run()") );
356 int wxEventLoop::Run()
358 // event loops are not recursive, you need to create another loop!
359 wxCHECK_MSG( !IsRunning(), -1, _T("can't reenter a message loop") );
361 m_impl
= new wxEventLoopImpl
;
363 wxEventLoop
*oldLoop
= ms_activeLoop
;
364 ms_activeLoop
= this;
366 m_impl
->m_keepGoing
= TRUE
;
367 while ( m_impl
->m_keepGoing
)
369 #if 0 // wxUSE_THREADS
370 wxMutexGuiLeaveOrEnter();
371 #endif // wxUSE_THREADS
373 // generate and process idle events for as long as we don't have
374 // anything else to do
375 while ( ! Pending() )
378 wxTimer::NotifyTimers(); // TODO: is this the correct place for it?
380 if (!m_impl
->SendIdleEvent())
382 #if 0 // wxUSE_THREADS
383 // leave the main loop to give other threads a chance to
384 // perform their GUI work
389 // Break out of while loop
394 // a message came or no more idle processing to do, sit in Dispatch()
395 // waiting for the next message
402 int exitcode
= m_impl
->GetExitCode();
406 ms_activeLoop
= oldLoop
;
411 void wxEventLoop::Exit(int rc
)
413 wxCHECK_RET( IsRunning(), _T("can't call Exit() if not running") );
415 m_impl
->SetExitCode(rc
);
416 m_impl
->m_keepGoing
= FALSE
;
419 // ----------------------------------------------------------------------------
420 // wxEventLoop message processing dispatching
421 // ----------------------------------------------------------------------------
423 bool wxEventLoop::Pending() const
425 XFlush( wxGlobalDisplay() );
426 return (XPending( wxGlobalDisplay() ) > 0);
429 bool wxEventLoop::Dispatch()
433 // TODO allowing for threads, as per e.g. wxMSW
435 // This now waits until either an X event is received,
436 // or the select times out. So we should now process
437 // wxTimers in a reasonably timely fashion. However it
438 // does also mean that idle processing will happen more
439 // often, so we should probably limit idle processing to
440 // not be repeated more than every N milliseconds.
442 if (XPending( wxGlobalDisplay() ) == 0)
445 GR_TIMEOUT timeout
= 10; // Milliseconds
446 // Wait for next event, or timeout
447 GrGetNextEventTimeout(& event
, timeout
);
449 // Fall through to ProcessEvent.
450 // we'll assume that ProcessEvent will just ignore
451 // the event if there was a timeout and no event.
456 tv
.tv_usec
=10000; // TODO make this configurable
457 int fd
= ConnectionNumber( wxGlobalDisplay() );
465 FD_SET(fd
, &readset
);
468 if (wxTheSocketTable
)
469 wxTheSocketTable
->FillSets( &readset
, &writeset
, &highest
);
472 if (select( highest
+1, &readset
, &writeset
, NULL
, &tv
) == 0)
474 // Timed out, so no event to process
479 // An X11 event was pending, so get it
480 if (FD_ISSET( fd
, &readset
))
481 XNextEvent( wxGlobalDisplay(), &event
);
484 // Check if any socket events were pending,
485 // and if so, call their callbacks
486 if (wxTheSocketTable
)
487 wxTheSocketTable
->ProcessEvents( &readset
, &writeset
);
494 XNextEvent( wxGlobalDisplay(), &event
);
498 (void) m_impl
->ProcessEvent( &event
);