1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: 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 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
21 #pragma implementation "evtloop.h"
25 #define XtParent XTPARENT
26 #define XtDisplay XTDISPLAY
29 // For compilers that support precompilation, includes "wx.h".
30 #include "wx/wxprec.h"
35 #include "wx/evtloop.h"
38 #include "wx/window.h"
41 #pragma message disable nosimpint
46 #pragma message enable nosimpint
49 #include "wx/motif/private.h"
51 static bool CheckForKeyUp(XEvent
* event
);
52 static bool CheckForKeyDown(XEvent
* event
);
53 static bool CheckForAccelerator(XEvent
* event
);
54 static void ProcessXEvent(XEvent
* event
);
55 static void wxBreakDispatch();
57 // ----------------------------------------------------------------------------
59 // ----------------------------------------------------------------------------
61 class WXDLLEXPORT wxEventLoopImpl
65 wxEventLoopImpl() { SetExitCode(0); }
67 // set/get the exit code
68 void SetExitCode(int exitcode
) { m_exitcode
= exitcode
; }
69 int GetExitCode() const { return m_exitcode
; }
71 bool SendIdleMessage();
72 bool GetKeepGoing() const { return m_keepGoing
; }
73 void SetKeepGoing(bool keepGoing
) { m_keepGoing
= keepGoing
; }
75 // the exit code of the event loop
80 // ----------------------------------------------------------------------------
81 // wxEventLoopImpl idle event processing
82 // ----------------------------------------------------------------------------
84 static bool SendIdleMessage()
88 return wxTheApp
->ProcessEvent(event
) && event
.MoreRequested();
91 bool wxEventLoopImpl::SendIdleMessage()
93 return ::SendIdleMessage();
96 // ============================================================================
97 // wxEventLoop implementation
98 // ============================================================================
100 // ----------------------------------------------------------------------------
101 // wxEventLoop running and exiting
102 // ----------------------------------------------------------------------------
104 wxEventLoop
*wxEventLoop::ms_activeLoop
= NULL
;
106 wxEventLoop::~wxEventLoop()
108 wxASSERT_MSG( !m_impl
, _T("should have been deleted in Run()") );
111 bool wxEventLoop::IsRunning() const
113 return m_impl
!= NULL
;
116 int wxEventLoop::Run()
118 // event loops are not recursive, you need to create another loop!
119 wxCHECK_MSG( !IsRunning(), -1, _T("can't reenter a message loop") );
121 wxEventLoop
*oldLoop
= ms_activeLoop
;
122 ms_activeLoop
= this;
124 m_impl
= new wxEventLoopImpl
;
125 m_impl
->SetKeepGoing( true );
129 if( !wxDoEventLoopIteration( *this ) )
133 int exitcode
= m_impl
->GetExitCode();
137 ms_activeLoop
= oldLoop
;
142 void wxEventLoop::Exit(int rc
)
144 wxCHECK_RET( IsRunning(), _T("can't call Exit() if not running") );
146 m_impl
->SetExitCode(rc
);
147 m_impl
->SetKeepGoing( false );
152 // ----------------------------------------------------------------------------
153 // wxEventLoop message processing dispatching
154 // ----------------------------------------------------------------------------
156 bool wxEventLoop::Pending() const
158 return XtAppPending( (XtAppContext
)wxTheApp
->GetAppContext() ) != 0;
161 bool wxEventLoop::Dispatch()
164 XtAppContext context
= (XtAppContext
)wxTheApp
->GetAppContext();
166 if( XtAppPeekEvent( context
, &event
) != 0 )
168 XtAppNextEvent( context
, &event
);
169 ProcessXEvent( &event
);
173 XtAppProcessEvent( context
, XtIMTimer
|XtIMAlternateInput
);
175 XtAppProcessEvent( context
, XtIMTimer
|XtIMAlternateInput
|XtIMSignal
);
178 return m_impl
? m_impl
->GetKeepGoing() : true;
181 // ----------------------------------------------------------------------------
182 // Static functions (originally in app.cpp)
183 // ----------------------------------------------------------------------------
185 void ProcessXEvent(XEvent
* event
)
187 if (event
->type
== KeyPress
)
189 if (CheckForAccelerator(event
))
191 // Do nothing! We intercepted and processed the event as an
195 // It seemed before that this hack was redundant and
196 // key down events were being generated by wxCanvasInputEvent.
197 // But no longer - why ???
199 else if (CheckForKeyDown(event
))
201 // We intercepted and processed the key down event
206 XtDispatchEvent(event
);
210 else if (event
->type
== KeyRelease
)
212 // TODO: work out why we still need this ! -michael
214 if (::CheckForKeyUp(event
))
216 // We intercepted and processed the key up event
221 XtDispatchEvent(event
);
225 else if (event
->type
== PropertyNotify
)
227 wxTheApp
->HandlePropertyChange(event
);
230 else if (event
->type
== ResizeRequest
)
232 /* Terry Gitnick <terryg@scientech.com> - 1/21/98
233 * If resize event, don't resize until the last resize event for this
234 * window is recieved. Prevents flicker as windows are resized.
237 Display
*disp
= event
->xany
.display
;
238 Window win
= event
->xany
.window
;
243 while( XCheckTypedWindowEvent (disp
, win
, ResizeRequest
, &report
));
245 // TODO: when implementing refresh optimization, we can use
246 // XtAddExposureToRegion to expand the window's paint region.
248 XtDispatchEvent(event
);
252 XtDispatchEvent(event
);
256 // Returns true if an accelerator has been processed
257 bool CheckForAccelerator(XEvent
* event
)
259 if (event
->xany
.type
== KeyPress
)
261 // Find a wxWindow for this window
262 // TODO: should get display for the window, not the current display
263 Widget widget
= XtWindowToWidget(event
->xany
.display
,
265 wxWindow
* win
= NULL
;
267 // Find the first wxWindow that corresponds to this event window
268 while (widget
&& !(win
= wxGetWindowFromTable(widget
)))
269 widget
= XtParent(widget
);
274 wxKeyEvent
keyEvent(wxEVT_CHAR
);
275 wxTranslateKeyEvent(keyEvent
, win
, (Widget
) 0, event
);
277 // Now we have a wxKeyEvent and we have a wxWindow.
278 // Go up the hierarchy until we find a matching accelerator,
279 // or we get to the top.
282 if (win
->ProcessAccelerator(keyEvent
))
284 win
= win
->GetParent();
291 // bool wxApp::CheckForKeyDown(WXEvent* event) { wxFAIL; return false; }
293 bool CheckForKeyDown(XEvent
* event
)
295 if (event
->xany
.type
== KeyPress
)
297 Widget widget
= XtWindowToWidget(event
->xany
.display
,
299 wxWindow
* win
= NULL
;
301 // Find the first wxWindow that corresponds to this event window
302 while (widget
&& !(win
= wxGetWindowFromTable(widget
)))
303 widget
= XtParent(widget
);
308 wxKeyEvent
keyEvent(wxEVT_KEY_DOWN
);
309 wxTranslateKeyEvent(keyEvent
, win
, (Widget
) 0, event
);
311 return win
->GetEventHandler()->ProcessEvent( keyEvent
);
317 // bool wxApp::CheckForKeyUp(WXEvent* event) { wxFAIL; return false; }
319 bool CheckForKeyUp(XEvent
* event
)
321 if (event
->xany
.type
== KeyRelease
)
323 Widget widget
= XtWindowToWidget(event
->xany
.display
,
325 wxWindow
* win
= NULL
;
327 // Find the first wxWindow that corresponds to this event window
328 while (widget
&& !(win
= wxGetWindowFromTable(widget
)))
329 widget
= XtParent(widget
);
334 wxKeyEvent
keyEvent(wxEVT_KEY_UP
);
335 wxTranslateKeyEvent(keyEvent
, win
, (Widget
) 0, event
);
337 return win
->GetEventHandler()->ProcessEvent( keyEvent
);
343 // ----------------------------------------------------------------------------
344 // executes one main loop iteration (declared in include/wx/motif/private.h)
345 // ----------------------------------------------------------------------------
347 bool wxDoEventLoopIteration( wxEventLoop
& evtLoop
)
349 bool moreRequested
, pendingEvents
;
353 pendingEvents
= evtLoop
.Pending();
354 if( pendingEvents
) break;
355 moreRequested
= ::SendIdleMessage();
356 if( !moreRequested
) break;
360 if( !pendingEvents
&& !moreRequested
)
362 // leave the main loop to give other threads a chance to
363 // perform their GUI work
370 if( !evtLoop
.Dispatch() )
376 // ----------------------------------------------------------------------------
377 // ::wxWakeUpIdle implementation
378 // ----------------------------------------------------------------------------
380 // As per Vadim's suggestion, we open a pipe, and XtAppAddInputSource it;
381 // writing a single byte to the pipe will cause wxEventLoop::Pending
382 // to return true, and wxEventLoop::Dispatch to dispatch an input handler
383 // that simply removes the byte(s), and to return, which will cause
384 // an idle event to be sent
386 // also wxEventLoop::Exit is implemented that way, so that exiting an
387 // event loop won't require an event being in the queue
389 #include "wx/module.h"
391 #include <sys/types.h>
392 #include <sys/time.h>
395 static XtInputId inputId
;
396 static int idleFds
[2] = { -1, -1 };
398 class wxIdlePipeModule
: public wxModule
401 wxIdlePipeModule() {};
403 virtual bool OnInit()
405 // Must be done before modules are initialized
407 if( pipe(idleFds
) != 0 )
413 virtual void OnExit()
419 DECLARE_DYNAMIC_CLASS(wxIdlePipeModule
);
422 IMPLEMENT_DYNAMIC_CLASS(wxIdlePipeModule
, wxModule
);
424 static void wxInputCallback( XtPointer
, int* fd
, XtInputId
* )
428 // wxWakeUpIdle may have been called more than once
432 struct timeval timeout
;
440 if( select( *fd
+ 1, &in
, NULL
, NULL
, &timeout
) <= 0 )
442 if( read( *fd
, buffer
, sizeof(buffer
) - 1 ) != sizeof(buffer
) - 1 )
447 static void wxBreakDispatch()
449 char dummy
= 0; // for valgrind
451 // check if wxWakeUpIdle has already been called
453 struct timeval timeout
;
459 FD_SET( idleFds
[0], &in
);
461 if( select( idleFds
[0] + 1, &in
, NULL
, NULL
, &timeout
) > 0 )
464 // write a single byte
465 write( idleFds
[1], &dummy
, 1 );
468 void wxApp::WakeUpIdle()
475 if( pipe(idleFds
) != 0 )
480 bool wxAddIdleCallback()
482 if (!wxInitIdleFds())
485 // install input handler for wxWakeUpIdle
486 inputId
= XtAppAddInput( (XtAppContext
) wxTheApp
->GetAppContext(),
488 (XtPointer
)XtInputReadMask
,