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"
32 #include "wx/tooltip.h"
33 #include "wx/module.h"
34 #include "wx/unix/private.h"
35 #include "wx/x11/private.h"
39 #include "wx/thread.h"
46 // ----------------------------------------------------------------------------
48 // ----------------------------------------------------------------------------
50 typedef void (*wxSocketCallback
) (int fd
, void* data
);
52 class wxSocketTableEntry
: public wxObject
57 m_fdInput
= -1; m_fdOutput
= -1;
58 m_callbackInput
= NULL
; m_callbackOutput
= NULL
;
59 m_dataInput
= NULL
; m_dataOutput
= NULL
;
64 wxSocketCallback m_callbackInput
;
65 wxSocketCallback m_callbackOutput
;
71 { wxSocketTableInput
, wxSocketTableOutput
} wxSocketTableType
;
73 class wxSocketTable
: public wxHashTable
76 wxSocketTable(): wxHashTable(wxKEY_INTEGER
)
81 WX_CLEAR_HASH_TABLE(*this)
84 wxSocketTableEntry
* FindEntry(int fd
);
86 void RegisterCallback(int fd
, wxSocketTableType socketType
, wxSocketCallback callback
, void* data
);
88 void UnregisterCallback(int fd
, wxSocketTableType socketType
);
90 bool CallCallback(int fd
, wxSocketTableType socketType
);
92 void FillSets(fd_set
* readset
, fd_set
* writeset
, int* highest
);
94 void ProcessEvents(fd_set
* readset
, fd_set
* writeset
);
97 wxSocketTableEntry
* wxSocketTable::FindEntry(int fd
)
99 wxSocketTableEntry
* entry
= (wxSocketTableEntry
*) Get(fd
);
103 void wxSocketTable::RegisterCallback(int fd
, wxSocketTableType socketType
, wxSocketCallback callback
, void* data
)
105 wxSocketTableEntry
* entry
= FindEntry(fd
);
108 entry
= new wxSocketTableEntry();
112 if (socketType
== wxSocketTableInput
)
114 entry
->m_fdInput
= fd
;
115 entry
->m_dataInput
= data
;
116 entry
->m_callbackInput
= callback
;
120 entry
->m_fdOutput
= fd
;
121 entry
->m_dataOutput
= data
;
122 entry
->m_callbackOutput
= callback
;
126 void wxSocketTable::UnregisterCallback(int fd
, wxSocketTableType socketType
)
128 wxSocketTableEntry
* entry
= FindEntry(fd
);
131 if (socketType
== wxSocketTableInput
)
133 entry
->m_fdInput
= -1;
134 entry
->m_dataInput
= NULL
;
135 entry
->m_callbackInput
= NULL
;
139 entry
->m_fdOutput
= -1;
140 entry
->m_dataOutput
= NULL
;
141 entry
->m_callbackOutput
= NULL
;
143 if (entry
->m_fdInput
== -1 && entry
->m_fdOutput
== -1)
151 bool wxSocketTable::CallCallback(int fd
, wxSocketTableType socketType
)
153 wxSocketTableEntry
* entry
= FindEntry(fd
);
156 if (socketType
== wxSocketTableInput
)
158 if (entry
->m_fdInput
!= -1 && entry
->m_callbackInput
)
160 (entry
->m_callbackInput
) (entry
->m_fdInput
, entry
->m_dataInput
);
165 if (entry
->m_fdOutput
!= -1 && entry
->m_callbackOutput
)
167 (entry
->m_callbackOutput
) (entry
->m_fdOutput
, entry
->m_dataOutput
);
176 void wxSocketTable::FillSets(fd_set
* readset
, fd_set
* writeset
, int* highest
)
179 wxHashTable::compatibility_iterator node
= Next();
182 wxSocketTableEntry
* entry
= (wxSocketTableEntry
*) node
->GetData();
184 if (entry
->m_fdInput
!= -1)
186 wxFD_SET(entry
->m_fdInput
, readset
);
187 if (entry
->m_fdInput
> *highest
)
188 * highest
= entry
->m_fdInput
;
191 if (entry
->m_fdOutput
!= -1)
193 wxFD_SET(entry
->m_fdOutput
, writeset
);
194 if (entry
->m_fdOutput
> *highest
)
195 * highest
= entry
->m_fdOutput
;
202 void wxSocketTable::ProcessEvents(fd_set
* readset
, fd_set
* writeset
)
205 wxHashTable::compatibility_iterator node
= Next();
208 wxSocketTableEntry
* entry
= (wxSocketTableEntry
*) node
->GetData();
210 if (entry
->m_fdInput
!= -1 && wxFD_ISSET(entry
->m_fdInput
, readset
))
212 (entry
->m_callbackInput
) (entry
->m_fdInput
, entry
->m_dataInput
);
215 if (entry
->m_fdOutput
!= -1 && wxFD_ISSET(entry
->m_fdOutput
, writeset
))
217 (entry
->m_callbackOutput
) (entry
->m_fdOutput
, entry
->m_dataOutput
);
224 wxSocketTable
* wxTheSocketTable
= NULL
;
226 class wxSocketTableModule
: public wxModule
228 DECLARE_DYNAMIC_CLASS(wxSocketTableModule
)
230 wxSocketTableModule() {}
231 bool OnInit() { wxTheSocketTable
= new wxSocketTable
; return true; };
232 void OnExit() { delete wxTheSocketTable
; wxTheSocketTable
= NULL
; };
235 IMPLEMENT_DYNAMIC_CLASS(wxSocketTableModule
, wxModule
)
237 // Implement registration functions as C functions so they
238 // can be called from gsock11.c
240 extern "C" void wxRegisterSocketCallback(int fd
, wxSocketTableType socketType
, wxSocketCallback callback
, void* data
)
242 if (wxTheSocketTable
)
244 wxTheSocketTable
->RegisterCallback(fd
, socketType
, callback
, data
);
248 extern "C" void wxUnregisterSocketCallback(int fd
, wxSocketTableType socketType
)
250 if (wxTheSocketTable
)
252 wxTheSocketTable
->UnregisterCallback(fd
, socketType
);
257 // ----------------------------------------------------------------------------
259 // ----------------------------------------------------------------------------
261 class WXDLLEXPORT wxEventLoopImpl
265 wxEventLoopImpl() { SetExitCode(0); m_keepGoing
= false; }
267 // process an XEvent, return true if it was processed
268 bool ProcessEvent(XEvent
* event
);
270 // generate an idle message, return true if more idle time requested
271 bool SendIdleEvent();
273 // set/get the exit code
274 void SetExitCode(int exitcode
) { m_exitcode
= exitcode
; }
275 int GetExitCode() const { return m_exitcode
; }
278 // preprocess an event, return true if processed (i.e. no further
279 // dispatching required)
280 bool PreProcessEvent(XEvent
* event
);
282 // the exit code of the event loop
288 // ============================================================================
289 // wxEventLoopImpl implementation
290 // ============================================================================
292 // ----------------------------------------------------------------------------
293 // wxEventLoopImpl message processing
294 // ----------------------------------------------------------------------------
296 bool wxEventLoopImpl::ProcessEvent(XEvent
*event
)
298 // give us the chance to preprocess the message first
299 if ( PreProcessEvent(event
) )
302 // if it wasn't done, dispatch it to the corresponding window
304 return wxTheApp
->ProcessXEvent((WXEvent
*) event
);
309 bool wxEventLoopImpl::PreProcessEvent(XEvent
*event
)
313 HWND hWnd
= msg
->hwnd
;
314 wxWindow
*wndThis
= wxGetWindowFromHWND((WXHWND
)hWnd
);
317 // try translations first; find the youngest window with a translation
320 for ( wnd
= wndThis
; wnd
; wnd
= wnd
->GetParent() )
322 if ( wnd
->MSWTranslateMessage((WXMSG
*)msg
) )
326 // Anyone for a non-translation message? Try youngest descendants first.
327 for ( wnd
= wndThis
; wnd
; wnd
= wnd
->GetParent() )
329 if ( wnd
->MSWProcessMessage((WXMSG
*)msg
) )
337 // ----------------------------------------------------------------------------
338 // wxEventLoopImpl idle event processing
339 // ----------------------------------------------------------------------------
341 bool wxEventLoopImpl::SendIdleEvent()
343 return wxTheApp
->ProcessIdle();
346 // ============================================================================
347 // wxEventLoop implementation
348 // ============================================================================
350 // ----------------------------------------------------------------------------
351 // wxEventLoop running and exiting
352 // ----------------------------------------------------------------------------
354 wxEventLoop::~wxEventLoop()
356 wxASSERT_MSG( !m_impl
, _T("should have been deleted in Run()") );
359 int wxEventLoop::Run()
361 // event loops are not recursive, you need to create another loop!
362 wxCHECK_MSG( !IsRunning(), -1, _T("can't reenter a message loop") );
364 m_impl
= new wxEventLoopImpl
;
366 wxEventLoopActivator
activate(this);
368 m_impl
->m_keepGoing
= true;
369 while ( m_impl
->m_keepGoing
)
371 #if 0 // wxUSE_THREADS
372 wxMutexGuiLeaveOrEnter();
373 #endif // wxUSE_THREADS
375 // generate and process idle events for as long as we don't have
376 // anything else to do
377 while ( ! Pending() )
380 wxTimer::NotifyTimers(); // TODO: is this the correct place for it?
382 if (!m_impl
->SendIdleEvent())
384 #if 0 // wxUSE_THREADS
385 // leave the main loop to give other threads a chance to
386 // perform their GUI work
391 // Break out of while loop
396 // a message came or no more idle processing to do, sit in Dispatch()
397 // waiting for the next message
404 int exitcode
= m_impl
->GetExitCode();
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 // Start off by checking if any of our child processes have finished.
434 wxCheckForFinishedChildren();
436 // TODO allowing for threads, as per e.g. wxMSW
438 // This now waits until either an X event is received,
439 // or the select times out. So we should now process
440 // wxTimers in a reasonably timely fashion. However it
441 // does also mean that idle processing will happen more
442 // often, so we should probably limit idle processing to
443 // not be repeated more than every N milliseconds.
445 if (XPending( wxGlobalDisplay() ) == 0)
448 GR_TIMEOUT timeout
= 10; // Milliseconds
449 // Wait for next event, or timeout
450 GrGetNextEventTimeout(& event
, timeout
);
452 // Fall through to ProcessEvent.
453 // we'll assume that ProcessEvent will just ignore
454 // the event if there was a timeout and no event.
459 tv
.tv_usec
=10000; // TODO make this configurable
460 int fd
= ConnectionNumber( wxGlobalDisplay() );
466 wxFD_ZERO(&writeset
);
468 wxFD_SET(fd
, &readset
);
471 if (wxTheSocketTable
)
472 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 (wxFD_ISSET( fd
, &readset
))
484 XNextEvent( wxGlobalDisplay(), &event
);
487 // Check if any socket events were pending,
488 // and if so, call their callbacks
489 if (wxTheSocketTable
)
490 wxTheSocketTable
->ProcessEvents( &readset
, &writeset
);
497 XNextEvent( wxGlobalDisplay(), &event
);
501 (void) m_impl
->ProcessEvent( &event
);