1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/motif/evtloop.cpp
3 // Purpose: implements wxEventLoop for Motif
4 // Author: Mattia Barbon
8 // Copyright: (c) 2002 Mattia Barbon
9 // License: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
26 #include "wx/window.h"
27 #include "wx/module.h"
30 #include "wx/evtloop.h"
31 #include "wx/thread.h"
34 #pragma message disable nosimpint
39 #pragma message enable nosimpint
42 #include "wx/unix/private.h"
43 #include "wx/motif/private.h"
45 #ifdef HAVE_SYS_SELECT_H
46 # include <sys/select.h>
49 static bool CheckForKeyUp(XEvent
* event
);
50 static bool CheckForKeyDown(XEvent
* event
);
51 static bool CheckForAccelerator(XEvent
* event
);
52 static void ProcessXEvent(XEvent
* event
);
53 static void wxBreakDispatch();
55 // ----------------------------------------------------------------------------
57 // ----------------------------------------------------------------------------
59 class WXDLLEXPORT wxEventLoopImpl
63 wxEventLoopImpl() { SetExitCode(0); }
65 // set/get the exit code
66 void SetExitCode(int exitcode
) { m_exitcode
= exitcode
; }
67 int GetExitCode() const { return m_exitcode
; }
69 bool SendIdleMessage();
70 bool GetKeepGoing() const { return m_keepGoing
; }
71 void SetKeepGoing(bool keepGoing
) { m_keepGoing
= keepGoing
; }
73 // the exit code of the event loop
78 // ----------------------------------------------------------------------------
79 // wxEventLoopImpl idle event processing
80 // ----------------------------------------------------------------------------
82 static bool SendIdleMessage()
84 return wxTheApp
->ProcessIdle();
87 bool wxEventLoopImpl::SendIdleMessage()
89 return ::SendIdleMessage();
92 // ============================================================================
93 // wxEventLoop implementation
94 // ============================================================================
96 // ----------------------------------------------------------------------------
97 // wxEventLoop running and exiting
98 // ----------------------------------------------------------------------------
100 wxGUIEventLoop::~wxGUIEventLoop()
102 wxASSERT_MSG( !m_impl
, _T("should have been deleted in Run()") );
105 int wxGUIEventLoop::Run()
107 // event loops are not recursive, you need to create another loop!
108 wxCHECK_MSG( !IsRunning(), -1, _T("can't reenter a message loop") );
110 wxEventLoopActivator
activate(this);
112 m_impl
= new wxEventLoopImpl
;
113 m_impl
->SetKeepGoing( true );
117 if( !wxDoEventLoopIteration( *this ) )
123 int exitcode
= m_impl
->GetExitCode();
130 void wxGUIEventLoop::Exit(int rc
)
132 wxCHECK_RET( IsRunning(), _T("can't call Exit() if not running") );
134 m_impl
->SetExitCode(rc
);
135 m_impl
->SetKeepGoing( false );
140 bool wxGUIEventLoop::YieldFor(long eventsToProcess
)
142 m_isInsideYield
= true;
143 m_eventsToProcessInsideYield
= eventsToProcess
;
145 while (wxTheApp
&& wxTheApp
->Pending())
146 // TODO: implement event filtering using the eventsToProcess mask
147 wxTheApp
->Dispatch();
149 m_isInsideYield
= false;
154 // ----------------------------------------------------------------------------
155 // wxEventLoop message processing dispatching
156 // ----------------------------------------------------------------------------
158 bool wxGUIEventLoop::Pending() const
160 return XtAppPending( (XtAppContext
)wxTheApp
->GetAppContext() ) != 0;
163 bool wxGUIEventLoop::Dispatch()
166 XtAppContext context
= (XtAppContext
)wxTheApp
->GetAppContext();
168 if( XtAppPeekEvent( context
, &event
) != 0 )
170 XtAppNextEvent( context
, &event
);
171 ProcessXEvent( &event
);
175 XtAppProcessEvent( context
, XtIMTimer
| XtIMAlternateInput
182 return m_impl
? m_impl
->GetKeepGoing() : true;
185 // ----------------------------------------------------------------------------
186 // Static functions (originally in app.cpp)
187 // ----------------------------------------------------------------------------
189 void ProcessXEvent(XEvent
* event
)
191 if (event
->type
== KeyPress
)
193 if (CheckForAccelerator(event
))
195 // Do nothing! We intercepted and processed the event as an
199 // It seemed before that this hack was redundant and
200 // key down events were being generated by wxCanvasInputEvent.
201 // But no longer - why ???
203 else if (CheckForKeyDown(event
))
205 // We intercepted and processed the key down event
210 XtDispatchEvent(event
);
214 else if (event
->type
== KeyRelease
)
216 // TODO: work out why we still need this ! -michael
218 if (::CheckForKeyUp(event
))
220 // We intercepted and processed the key up event
225 XtDispatchEvent(event
);
229 else if (event
->type
== PropertyNotify
)
231 wxTheApp
->HandlePropertyChange(event
);
234 else if (event
->type
== ResizeRequest
)
236 /* Terry Gitnick <terryg@scientech.com> - 1/21/98
237 * If resize event, don't resize until the last resize event for this
238 * window is recieved. Prevents flicker as windows are resized.
241 Display
*disp
= event
->xany
.display
;
242 Window win
= event
->xany
.window
;
247 while( XCheckTypedWindowEvent (disp
, win
, ResizeRequest
, &report
));
249 // TODO: when implementing refresh optimization, we can use
250 // XtAddExposureToRegion to expand the window's paint region.
252 XtDispatchEvent(event
);
256 XtDispatchEvent(event
);
260 // Returns true if an accelerator has been processed
261 bool CheckForAccelerator(XEvent
* event
)
263 if (event
->xany
.type
== KeyPress
)
265 // Find a wxWindow for this window
266 // TODO: should get display for the window, not the current display
267 Widget widget
= XtWindowToWidget(event
->xany
.display
,
269 wxWindow
* win
= NULL
;
271 // Find the first wxWindow that corresponds to this event window
272 while (widget
&& ((win
= wxGetWindowFromTable(widget
))!=NULL
))
273 widget
= XtParent(widget
);
278 wxKeyEvent
keyEvent(wxEVT_CHAR
);
279 wxTranslateKeyEvent(keyEvent
, win
, (Widget
) 0, event
);
281 // Now we have a wxKeyEvent and we have a wxWindow.
282 // Go up the hierarchy until we find a matching accelerator,
283 // or we get to the top.
286 if (win
->ProcessAccelerator(keyEvent
))
288 win
= win
->GetParent();
295 // bool wxApp::CheckForKeyDown(WXEvent* event) { wxFAIL; return false; }
297 bool CheckForKeyDown(XEvent
* event
)
299 if (event
->xany
.type
== KeyPress
)
301 Widget widget
= XtWindowToWidget(event
->xany
.display
,
303 wxWindow
* win
= NULL
;
305 // Find the first wxWindow that corresponds to this event window
306 while (widget
&& ((win
= wxGetWindowFromTable(widget
))!=NULL
))
307 widget
= XtParent(widget
);
312 wxKeyEvent
keyEvent(wxEVT_KEY_DOWN
);
313 wxTranslateKeyEvent(keyEvent
, win
, (Widget
) 0, event
);
315 return win
->HandleWindowEvent( keyEvent
);
321 // bool wxApp::CheckForKeyUp(WXEvent* event) { wxFAIL; return false; }
323 bool CheckForKeyUp(XEvent
* event
)
325 if (event
->xany
.type
== KeyRelease
)
327 Widget widget
= XtWindowToWidget(event
->xany
.display
,
329 wxWindow
* win
= NULL
;
331 // Find the first wxWindow that corresponds to this event window
332 while (widget
&& ((win
= wxGetWindowFromTable(widget
))!=NULL
))
333 widget
= XtParent(widget
);
338 wxKeyEvent
keyEvent(wxEVT_KEY_UP
);
339 wxTranslateKeyEvent(keyEvent
, win
, (Widget
) 0, event
);
341 return win
->HandleWindowEvent( keyEvent
);
347 // ----------------------------------------------------------------------------
348 // executes one main loop iteration (declared in include/wx/motif/private.h)
349 // ----------------------------------------------------------------------------
351 bool wxDoEventLoopIteration( wxGUIEventLoop
& evtLoop
)
353 bool moreRequested
, pendingEvents
;
357 pendingEvents
= evtLoop
.Pending();
358 if( pendingEvents
) break;
359 moreRequested
= ::SendIdleMessage();
360 if( !moreRequested
) break;
364 if( !pendingEvents
&& !moreRequested
)
366 // leave the main loop to give other threads a chance to
367 // perform their GUI work
374 if( !evtLoop
.Dispatch() )
380 // ----------------------------------------------------------------------------
381 // ::wxWakeUpIdle implementation
382 // ----------------------------------------------------------------------------
384 // As per Vadim's suggestion, we open a pipe, and XtAppAddInputSource it;
385 // writing a single byte to the pipe will cause wxEventLoop::Pending
386 // to return true, and wxEventLoop::Dispatch to dispatch an input handler
387 // that simply removes the byte(s), and to return, which will cause
388 // an idle event to be sent
390 // also wxEventLoop::Exit is implemented that way, so that exiting an
391 // event loop won't require an event being in the queue
393 #include <sys/types.h>
394 #include <sys/time.h>
397 static int idleFds
[2] = { -1, -1 };
399 class wxIdlePipeModule
: public wxModule
402 wxIdlePipeModule() {};
404 virtual bool OnInit()
406 // Must be done before modules are initialized
408 if( pipe(idleFds
) != 0 )
414 virtual void OnExit()
420 DECLARE_DYNAMIC_CLASS(wxIdlePipeModule
)
423 IMPLEMENT_DYNAMIC_CLASS(wxIdlePipeModule
, wxModule
)
425 static void wxInputCallback( XtPointer
, int* fd
, XtInputId
* )
429 // wxWakeUpIdle may have been called more than once
433 struct timeval timeout
;
439 wxFD_SET( *fd
, &in
);
441 if( select( *fd
+ 1, &in
, NULL
, NULL
, &timeout
) <= 0 )
443 if( read( *fd
, buffer
, sizeof(buffer
) - 1 ) != sizeof(buffer
) - 1 )
448 static void wxBreakDispatch()
450 char dummy
= 0; // for valgrind
452 // check if wxWakeUpIdle has already been called
454 struct timeval timeout
;
460 wxFD_SET( idleFds
[0], &in
);
462 if( select( idleFds
[0] + 1, &in
, NULL
, NULL
, &timeout
) > 0 )
465 // write a single byte
466 write( idleFds
[1], &dummy
, 1 );
469 void wxApp::WakeUpIdle()
476 if( pipe(idleFds
) != 0 )
481 bool wxAddIdleCallback()
483 if (!wxInitIdleFds())
486 // install input handler for wxWakeUpIdle
487 XtAppAddInput((XtAppContext
) wxTheApp
->GetAppContext(),
489 (XtPointer
)XtInputReadMask
,