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 // ----------------------------------------------------------------------------
21 #define XtParent XTPARENT
22 #define XtDisplay XTDISPLAY
25 // For compilers that support precompilation, includes "wx.h".
26 #include "wx/wxprec.h"
31 #include "wx/evtloop.h"
34 #include "wx/window.h"
37 #pragma message disable nosimpint
42 #pragma message enable nosimpint
45 #include "wx/unix/private.h"
46 #include "wx/motif/private.h"
48 static bool CheckForKeyUp(XEvent
* event
);
49 static bool CheckForKeyDown(XEvent
* event
);
50 static bool CheckForAccelerator(XEvent
* event
);
51 static void ProcessXEvent(XEvent
* event
);
52 static void wxBreakDispatch();
54 // ----------------------------------------------------------------------------
56 // ----------------------------------------------------------------------------
58 class WXDLLEXPORT wxEventLoopImpl
62 wxEventLoopImpl() { SetExitCode(0); }
64 // set/get the exit code
65 void SetExitCode(int exitcode
) { m_exitcode
= exitcode
; }
66 int GetExitCode() const { return m_exitcode
; }
68 bool SendIdleMessage();
69 bool GetKeepGoing() const { return m_keepGoing
; }
70 void SetKeepGoing(bool keepGoing
) { m_keepGoing
= keepGoing
; }
72 // the exit code of the event loop
77 // ----------------------------------------------------------------------------
78 // wxEventLoopImpl idle event processing
79 // ----------------------------------------------------------------------------
81 static bool SendIdleMessage()
83 return wxTheApp
->ProcessIdle();
86 bool wxEventLoopImpl::SendIdleMessage()
88 return ::SendIdleMessage();
91 // ============================================================================
92 // wxEventLoop implementation
93 // ============================================================================
95 // ----------------------------------------------------------------------------
96 // wxEventLoop running and exiting
97 // ----------------------------------------------------------------------------
99 wxEventLoop
*wxEventLoopBase::ms_activeLoop
= NULL
;
101 wxEventLoop::~wxEventLoop()
103 wxASSERT_MSG( !m_impl
, _T("should have been deleted in Run()") );
106 int wxEventLoop::Run()
108 // event loops are not recursive, you need to create another loop!
109 wxCHECK_MSG( !IsRunning(), -1, _T("can't reenter a message loop") );
111 wxEventLoop
*oldLoop
= ms_activeLoop
;
112 ms_activeLoop
= this;
114 m_impl
= new wxEventLoopImpl
;
115 m_impl
->SetKeepGoing( true );
119 if( !wxDoEventLoopIteration( *this ) )
123 int exitcode
= m_impl
->GetExitCode();
127 ms_activeLoop
= oldLoop
;
132 void wxEventLoop::Exit(int rc
)
134 wxCHECK_RET( IsRunning(), _T("can't call Exit() if not running") );
136 m_impl
->SetExitCode(rc
);
137 m_impl
->SetKeepGoing( false );
142 // ----------------------------------------------------------------------------
143 // wxEventLoop message processing dispatching
144 // ----------------------------------------------------------------------------
146 bool wxEventLoop::Pending() const
148 return XtAppPending( (XtAppContext
)wxTheApp
->GetAppContext() ) != 0;
151 bool wxEventLoop::Dispatch()
154 XtAppContext context
= (XtAppContext
)wxTheApp
->GetAppContext();
156 if( XtAppPeekEvent( context
, &event
) != 0 )
158 XtAppNextEvent( context
, &event
);
159 ProcessXEvent( &event
);
163 XtAppProcessEvent( context
, XtIMTimer
| XtIMAlternateInput
170 return m_impl
? m_impl
->GetKeepGoing() : true;
173 // ----------------------------------------------------------------------------
174 // Static functions (originally in app.cpp)
175 // ----------------------------------------------------------------------------
177 void ProcessXEvent(XEvent
* event
)
179 if (event
->type
== KeyPress
)
181 if (CheckForAccelerator(event
))
183 // Do nothing! We intercepted and processed the event as an
187 // It seemed before that this hack was redundant and
188 // key down events were being generated by wxCanvasInputEvent.
189 // But no longer - why ???
191 else if (CheckForKeyDown(event
))
193 // We intercepted and processed the key down event
198 XtDispatchEvent(event
);
202 else if (event
->type
== KeyRelease
)
204 // TODO: work out why we still need this ! -michael
206 if (::CheckForKeyUp(event
))
208 // We intercepted and processed the key up event
213 XtDispatchEvent(event
);
217 else if (event
->type
== PropertyNotify
)
219 wxTheApp
->HandlePropertyChange(event
);
222 else if (event
->type
== ResizeRequest
)
224 /* Terry Gitnick <terryg@scientech.com> - 1/21/98
225 * If resize event, don't resize until the last resize event for this
226 * window is recieved. Prevents flicker as windows are resized.
229 Display
*disp
= event
->xany
.display
;
230 Window win
= event
->xany
.window
;
235 while( XCheckTypedWindowEvent (disp
, win
, ResizeRequest
, &report
));
237 // TODO: when implementing refresh optimization, we can use
238 // XtAddExposureToRegion to expand the window's paint region.
240 XtDispatchEvent(event
);
244 XtDispatchEvent(event
);
248 // Returns true if an accelerator has been processed
249 bool CheckForAccelerator(XEvent
* event
)
251 if (event
->xany
.type
== KeyPress
)
253 // Find a wxWindow for this window
254 // TODO: should get display for the window, not the current display
255 Widget widget
= XtWindowToWidget(event
->xany
.display
,
257 wxWindow
* win
= NULL
;
259 // Find the first wxWindow that corresponds to this event window
260 while (widget
&& !(win
= wxGetWindowFromTable(widget
)))
261 widget
= XtParent(widget
);
266 wxKeyEvent
keyEvent(wxEVT_CHAR
);
267 wxTranslateKeyEvent(keyEvent
, win
, (Widget
) 0, event
);
269 // Now we have a wxKeyEvent and we have a wxWindow.
270 // Go up the hierarchy until we find a matching accelerator,
271 // or we get to the top.
274 if (win
->ProcessAccelerator(keyEvent
))
276 win
= win
->GetParent();
283 // bool wxApp::CheckForKeyDown(WXEvent* event) { wxFAIL; return false; }
285 bool CheckForKeyDown(XEvent
* event
)
287 if (event
->xany
.type
== KeyPress
)
289 Widget widget
= XtWindowToWidget(event
->xany
.display
,
291 wxWindow
* win
= NULL
;
293 // Find the first wxWindow that corresponds to this event window
294 while (widget
&& !(win
= wxGetWindowFromTable(widget
)))
295 widget
= XtParent(widget
);
300 wxKeyEvent
keyEvent(wxEVT_KEY_DOWN
);
301 wxTranslateKeyEvent(keyEvent
, win
, (Widget
) 0, event
);
303 return win
->GetEventHandler()->ProcessEvent( keyEvent
);
309 // bool wxApp::CheckForKeyUp(WXEvent* event) { wxFAIL; return false; }
311 bool CheckForKeyUp(XEvent
* event
)
313 if (event
->xany
.type
== KeyRelease
)
315 Widget widget
= XtWindowToWidget(event
->xany
.display
,
317 wxWindow
* win
= NULL
;
319 // Find the first wxWindow that corresponds to this event window
320 while (widget
&& !(win
= wxGetWindowFromTable(widget
)))
321 widget
= XtParent(widget
);
326 wxKeyEvent
keyEvent(wxEVT_KEY_UP
);
327 wxTranslateKeyEvent(keyEvent
, win
, (Widget
) 0, event
);
329 return win
->GetEventHandler()->ProcessEvent( keyEvent
);
335 // ----------------------------------------------------------------------------
336 // executes one main loop iteration (declared in include/wx/motif/private.h)
337 // ----------------------------------------------------------------------------
339 bool wxDoEventLoopIteration( wxEventLoop
& evtLoop
)
341 bool moreRequested
, pendingEvents
;
345 pendingEvents
= evtLoop
.Pending();
346 if( pendingEvents
) break;
347 moreRequested
= ::SendIdleMessage();
348 if( !moreRequested
) break;
352 if( !pendingEvents
&& !moreRequested
)
354 // leave the main loop to give other threads a chance to
355 // perform their GUI work
362 if( !evtLoop
.Dispatch() )
368 // ----------------------------------------------------------------------------
369 // ::wxWakeUpIdle implementation
370 // ----------------------------------------------------------------------------
372 // As per Vadim's suggestion, we open a pipe, and XtAppAddInputSource it;
373 // writing a single byte to the pipe will cause wxEventLoop::Pending
374 // to return true, and wxEventLoop::Dispatch to dispatch an input handler
375 // that simply removes the byte(s), and to return, which will cause
376 // an idle event to be sent
378 // also wxEventLoop::Exit is implemented that way, so that exiting an
379 // event loop won't require an event being in the queue
381 #include "wx/module.h"
383 #include <sys/types.h>
384 #include <sys/time.h>
387 static int idleFds
[2] = { -1, -1 };
389 class wxIdlePipeModule
: public wxModule
392 wxIdlePipeModule() {};
394 virtual bool OnInit()
396 // Must be done before modules are initialized
398 if( pipe(idleFds
) != 0 )
404 virtual void OnExit()
410 DECLARE_DYNAMIC_CLASS(wxIdlePipeModule
)
413 IMPLEMENT_DYNAMIC_CLASS(wxIdlePipeModule
, wxModule
);
415 static void wxInputCallback( XtPointer
, int* fd
, XtInputId
* )
419 // wxWakeUpIdle may have been called more than once
423 struct timeval timeout
;
429 wxFD_SET( *fd
, &in
);
431 if( select( *fd
+ 1, &in
, NULL
, NULL
, &timeout
) <= 0 )
433 if( read( *fd
, buffer
, sizeof(buffer
) - 1 ) != sizeof(buffer
) - 1 )
438 static void wxBreakDispatch()
440 char dummy
= 0; // for valgrind
442 // check if wxWakeUpIdle has already been called
444 struct timeval timeout
;
450 wxFD_SET( idleFds
[0], &in
);
452 if( select( idleFds
[0] + 1, &in
, NULL
, NULL
, &timeout
) > 0 )
455 // write a single byte
456 write( idleFds
[1], &dummy
, 1 );
459 void wxApp::WakeUpIdle()
466 if( pipe(idleFds
) != 0 )
471 bool wxAddIdleCallback()
473 if (!wxInitIdleFds())
476 // install input handler for wxWakeUpIdle
477 XtAppAddInput((XtAppContext
) wxTheApp
->GetAppContext(),
479 (XtPointer
)XtInputReadMask
,