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"
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
*wxEventLoop::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 bool wxEventLoop::IsRunning() const
358 return m_impl
!= NULL
;
361 int wxEventLoop::Run()
363 // event loops are not recursive, you need to create another loop!
364 wxCHECK_MSG( !IsRunning(), -1, _T("can't reenter a message loop") );
366 m_impl
= new wxEventLoopImpl
;
368 wxEventLoop
*oldLoop
= ms_activeLoop
;
369 ms_activeLoop
= this;
371 m_impl
->m_keepGoing
= TRUE
;
372 while ( m_impl
->m_keepGoing
)
374 #if 0 // wxUSE_THREADS
375 wxMutexGuiLeaveOrEnter();
376 #endif // wxUSE_THREADS
378 // generate and process idle events for as long as we don't have
379 // anything else to do
380 while ( ! Pending() )
383 wxTimer::NotifyTimers(); // TODO: is this the correct place for it?
385 if (!m_impl
->SendIdleEvent())
387 #if 0 // wxUSE_THREADS
388 // leave the main loop to give other threads a chance to
389 // perform their GUI work
394 // Break out of while loop
399 // a message came or no more idle processing to do, sit in Dispatch()
400 // waiting for the next message
407 int exitcode
= m_impl
->GetExitCode();
411 ms_activeLoop
= oldLoop
;
416 void wxEventLoop::Exit(int rc
)
418 wxCHECK_RET( IsRunning(), _T("can't call Exit() if not running") );
420 m_impl
->SetExitCode(rc
);
421 m_impl
->m_keepGoing
= FALSE
;
424 // ----------------------------------------------------------------------------
425 // wxEventLoop message processing dispatching
426 // ----------------------------------------------------------------------------
428 bool wxEventLoop::Pending() const
430 XFlush( wxGlobalDisplay() );
431 return (XPending( wxGlobalDisplay() ) > 0);
434 bool wxEventLoop::Dispatch()
438 // TODO allowing for threads, as per e.g. wxMSW
440 // This now waits until either an X event is received,
441 // or the select times out. So we should now process
442 // wxTimers in a reasonably timely fashion. However it
443 // does also mean that idle processing will happen more
444 // often, so we should probably limit idle processing to
445 // not be repeated more than every N milliseconds.
447 if (XPending( wxGlobalDisplay() ) == 0)
450 GR_TIMEOUT timeout
= 10; // Milliseconds
451 // Wait for next event, or timeout
452 GrGetNextEventTimeout(& event
, timeout
);
454 // Fall through to ProcessEvent.
455 // we'll assume that ProcessEvent will just ignore
456 // the event if there was a timeout and no event.
461 tv
.tv_usec
=10000; // TODO make this configurable
462 int fd
= ConnectionNumber( wxGlobalDisplay() );
470 FD_SET(fd
, &readset
);
473 if (wxTheSocketTable
)
474 wxTheSocketTable
->FillSets( &readset
, &writeset
, &highest
);
477 if (select( highest
+1, &readset
, &writeset
, NULL
, &tv
) == 0)
479 // Timed out, so no event to process
484 // An X11 event was pending, so get it
485 if (FD_ISSET( fd
, &readset
))
486 XNextEvent( wxGlobalDisplay(), &event
);
489 // Check if any socket events were pending,
490 // and if so, call their callbacks
491 if (wxTheSocketTable
)
492 wxTheSocketTable
->ProcessEvents( &readset
, &writeset
);
499 XNextEvent( wxGlobalDisplay(), &event
);
503 (void) m_impl
->ProcessEvent( &event
);