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
)
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 wxNode
* node
= Next();
177 wxSocketTableEntry
* entry
= (wxSocketTableEntry
*) node
->Data();
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 wxNode
* node
= Next();
203 wxSocketTableEntry
* entry
= (wxSocketTableEntry
*) node
->Data();
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()
339 event
.SetEventObject(wxTheApp
);
341 return wxTheApp
->ProcessEvent(event
) && event
.MoreRequested();
344 // ============================================================================
345 // wxEventLoop implementation
346 // ============================================================================
348 wxEventLoop
*wxEventLoop::ms_activeLoop
= NULL
;
350 // ----------------------------------------------------------------------------
351 // wxEventLoop running and exiting
352 // ----------------------------------------------------------------------------
354 wxEventLoop::~wxEventLoop()
356 wxASSERT_MSG( !m_impl
, _T("should have been deleted in Run()") );
359 bool wxEventLoop::IsRunning() const
361 return m_impl
!= NULL
;
364 int wxEventLoop::Run()
366 // event loops are not recursive, you need to create another loop!
367 wxCHECK_MSG( !IsRunning(), -1, _T("can't reenter a message loop") );
369 m_impl
= new wxEventLoopImpl
;
371 wxEventLoop
*oldLoop
= ms_activeLoop
;
372 ms_activeLoop
= this;
374 m_impl
->m_keepGoing
= TRUE
;
375 while ( m_impl
->m_keepGoing
)
377 #if 0 // wxUSE_THREADS
378 wxMutexGuiLeaveOrEnter();
379 #endif // wxUSE_THREADS
381 // generate and process idle events for as long as we don't have
382 // anything else to do
383 while ( ! Pending() )
386 wxTimer::NotifyTimers(); // TODO: is this the correct place for it?
388 if (!m_impl
->SendIdleEvent())
391 // leave the main loop to give other threads a chance to
392 // perform their GUI work
397 // Break out of while loop
402 // a message came or no more idle processing to do, sit in Dispatch()
403 // waiting for the next message
410 int exitcode
= m_impl
->GetExitCode();
414 ms_activeLoop
= oldLoop
;
419 void wxEventLoop::Exit(int rc
)
421 wxCHECK_RET( IsRunning(), _T("can't call Exit() if not running") );
423 m_impl
->SetExitCode(rc
);
424 m_impl
->m_keepGoing
= FALSE
;
427 // ----------------------------------------------------------------------------
428 // wxEventLoop message processing dispatching
429 // ----------------------------------------------------------------------------
431 bool wxEventLoop::Pending() const
433 XFlush((Display
*) wxGetDisplay());
434 return (XPending((Display
*) wxGetDisplay()) > 0);
437 bool wxEventLoop::Dispatch()
441 // TODO allowing for threads, as per e.g. wxMSW
443 // This now waits until either an X event is received,
444 // or the select times out. So we should now process
445 // wxTimers in a reasonably timely fashion. However it
446 // does also mean that idle processing will happen more
447 // often, so we should probably limit idle processing to
448 // not be repeated more than every N milliseconds.
450 if (XPending((Display
*) wxGetDisplay()) == 0)
453 GR_TIMEOUT timeout
= 10; // Milliseconds
454 // Wait for next event, or timeout
455 GrGetNextEventTimeout(& event
, timeout
);
457 // Fall through to ProcessEvent.
458 // we'll assume that ProcessEvent will just ignore
459 // the event if there was a timeout and no event.
464 tv
.tv_usec
=10000; // TODO make this configurable
465 int fd
= ConnectionNumber((Display
*) wxGetDisplay());
472 FD_SET(fd
, &readset
);
475 if (wxTheSocketTable
)
476 wxTheSocketTable
->FillSets(& readset
, & writeset
, & highest
);
479 if (select(highest
+1, &readset
, &writeset
, NULL
, & tv
) == 0)
481 // Timed out, so no event to process
486 // An X11 event was pending, so get it
487 if (FD_ISSET(fd
, & readset
))
488 XNextEvent((Display
*) wxGetDisplay(), & event
);
491 // Check if any socket events were pending,
492 // and if so, call their callbacks
493 if (wxTheSocketTable
)
494 wxTheSocketTable
->ProcessEvents(& readset
, & writeset
);
500 XNextEvent((Display
*) wxGetDisplay(), & event
);
503 (void) m_impl
->ProcessEvent(& event
);