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/motif/private.h"
47 static bool CheckForKeyUp(XEvent
* event
);
48 static bool CheckForKeyDown(XEvent
* event
);
49 static bool CheckForAccelerator(XEvent
* event
);
50 static void ProcessXEvent(XEvent
* event
);
51 static void wxBreakDispatch();
53 // ----------------------------------------------------------------------------
55 // ----------------------------------------------------------------------------
57 class WXDLLEXPORT wxEventLoopImpl
61 wxEventLoopImpl() { SetExitCode(0); }
63 // set/get the exit code
64 void SetExitCode(int exitcode
) { m_exitcode
= exitcode
; }
65 int GetExitCode() const { return m_exitcode
; }
67 bool SendIdleMessage();
68 bool GetKeepGoing() const { return m_keepGoing
; }
69 void SetKeepGoing(bool keepGoing
) { m_keepGoing
= keepGoing
; }
71 // the exit code of the event loop
76 // ----------------------------------------------------------------------------
77 // wxEventLoopImpl idle event processing
78 // ----------------------------------------------------------------------------
80 static bool SendIdleMessage()
82 return wxTheApp
->ProcessIdle();
85 bool wxEventLoopImpl::SendIdleMessage()
87 return ::SendIdleMessage();
90 // ============================================================================
91 // wxEventLoop implementation
92 // ============================================================================
94 // ----------------------------------------------------------------------------
95 // wxEventLoop running and exiting
96 // ----------------------------------------------------------------------------
98 wxEventLoop
*wxEventLoopBase::ms_activeLoop
= NULL
;
100 wxEventLoop::~wxEventLoop()
102 wxASSERT_MSG( !m_impl
, _T("should have been deleted in Run()") );
105 int wxEventLoop::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 wxEventLoop
*oldLoop
= ms_activeLoop
;
111 ms_activeLoop
= this;
113 m_impl
= new wxEventLoopImpl
;
114 m_impl
->SetKeepGoing( true );
118 if( !wxDoEventLoopIteration( *this ) )
122 int exitcode
= m_impl
->GetExitCode();
126 ms_activeLoop
= oldLoop
;
131 void wxEventLoop::Exit(int rc
)
133 wxCHECK_RET( IsRunning(), _T("can't call Exit() if not running") );
135 m_impl
->SetExitCode(rc
);
136 m_impl
->SetKeepGoing( false );
141 // ----------------------------------------------------------------------------
142 // wxEventLoop message processing dispatching
143 // ----------------------------------------------------------------------------
145 bool wxEventLoop::Pending() const
147 return XtAppPending( (XtAppContext
)wxTheApp
->GetAppContext() ) != 0;
150 bool wxEventLoop::Dispatch()
153 XtAppContext context
= (XtAppContext
)wxTheApp
->GetAppContext();
155 if( XtAppPeekEvent( context
, &event
) != 0 )
157 XtAppNextEvent( context
, &event
);
158 ProcessXEvent( &event
);
162 XtAppProcessEvent( context
, XtIMTimer
| XtIMAlternateInput
169 return m_impl
? m_impl
->GetKeepGoing() : true;
172 // ----------------------------------------------------------------------------
173 // Static functions (originally in app.cpp)
174 // ----------------------------------------------------------------------------
176 void ProcessXEvent(XEvent
* event
)
178 if (event
->type
== KeyPress
)
180 if (CheckForAccelerator(event
))
182 // Do nothing! We intercepted and processed the event as an
186 // It seemed before that this hack was redundant and
187 // key down events were being generated by wxCanvasInputEvent.
188 // But no longer - why ???
190 else if (CheckForKeyDown(event
))
192 // We intercepted and processed the key down event
197 XtDispatchEvent(event
);
201 else if (event
->type
== KeyRelease
)
203 // TODO: work out why we still need this ! -michael
205 if (::CheckForKeyUp(event
))
207 // We intercepted and processed the key up event
212 XtDispatchEvent(event
);
216 else if (event
->type
== PropertyNotify
)
218 wxTheApp
->HandlePropertyChange(event
);
221 else if (event
->type
== ResizeRequest
)
223 /* Terry Gitnick <terryg@scientech.com> - 1/21/98
224 * If resize event, don't resize until the last resize event for this
225 * window is recieved. Prevents flicker as windows are resized.
228 Display
*disp
= event
->xany
.display
;
229 Window win
= event
->xany
.window
;
234 while( XCheckTypedWindowEvent (disp
, win
, ResizeRequest
, &report
));
236 // TODO: when implementing refresh optimization, we can use
237 // XtAddExposureToRegion to expand the window's paint region.
239 XtDispatchEvent(event
);
243 XtDispatchEvent(event
);
247 // Returns true if an accelerator has been processed
248 bool CheckForAccelerator(XEvent
* event
)
250 if (event
->xany
.type
== KeyPress
)
252 // Find a wxWindow for this window
253 // TODO: should get display for the window, not the current display
254 Widget widget
= XtWindowToWidget(event
->xany
.display
,
256 wxWindow
* win
= NULL
;
258 // Find the first wxWindow that corresponds to this event window
259 while (widget
&& !(win
= wxGetWindowFromTable(widget
)))
260 widget
= XtParent(widget
);
265 wxKeyEvent
keyEvent(wxEVT_CHAR
);
266 wxTranslateKeyEvent(keyEvent
, win
, (Widget
) 0, event
);
268 // Now we have a wxKeyEvent and we have a wxWindow.
269 // Go up the hierarchy until we find a matching accelerator,
270 // or we get to the top.
273 if (win
->ProcessAccelerator(keyEvent
))
275 win
= win
->GetParent();
282 // bool wxApp::CheckForKeyDown(WXEvent* event) { wxFAIL; return false; }
284 bool CheckForKeyDown(XEvent
* event
)
286 if (event
->xany
.type
== KeyPress
)
288 Widget widget
= XtWindowToWidget(event
->xany
.display
,
290 wxWindow
* win
= NULL
;
292 // Find the first wxWindow that corresponds to this event window
293 while (widget
&& !(win
= wxGetWindowFromTable(widget
)))
294 widget
= XtParent(widget
);
299 wxKeyEvent
keyEvent(wxEVT_KEY_DOWN
);
300 wxTranslateKeyEvent(keyEvent
, win
, (Widget
) 0, event
);
302 return win
->GetEventHandler()->ProcessEvent( keyEvent
);
308 // bool wxApp::CheckForKeyUp(WXEvent* event) { wxFAIL; return false; }
310 bool CheckForKeyUp(XEvent
* event
)
312 if (event
->xany
.type
== KeyRelease
)
314 Widget widget
= XtWindowToWidget(event
->xany
.display
,
316 wxWindow
* win
= NULL
;
318 // Find the first wxWindow that corresponds to this event window
319 while (widget
&& !(win
= wxGetWindowFromTable(widget
)))
320 widget
= XtParent(widget
);
325 wxKeyEvent
keyEvent(wxEVT_KEY_UP
);
326 wxTranslateKeyEvent(keyEvent
, win
, (Widget
) 0, event
);
328 return win
->GetEventHandler()->ProcessEvent( keyEvent
);
334 // ----------------------------------------------------------------------------
335 // executes one main loop iteration (declared in include/wx/motif/private.h)
336 // ----------------------------------------------------------------------------
338 bool wxDoEventLoopIteration( wxEventLoop
& evtLoop
)
340 bool moreRequested
, pendingEvents
;
344 pendingEvents
= evtLoop
.Pending();
345 if( pendingEvents
) break;
346 moreRequested
= ::SendIdleMessage();
347 if( !moreRequested
) break;
351 if( !pendingEvents
&& !moreRequested
)
353 // leave the main loop to give other threads a chance to
354 // perform their GUI work
361 if( !evtLoop
.Dispatch() )
367 // ----------------------------------------------------------------------------
368 // ::wxWakeUpIdle implementation
369 // ----------------------------------------------------------------------------
371 // As per Vadim's suggestion, we open a pipe, and XtAppAddInputSource it;
372 // writing a single byte to the pipe will cause wxEventLoop::Pending
373 // to return true, and wxEventLoop::Dispatch to dispatch an input handler
374 // that simply removes the byte(s), and to return, which will cause
375 // an idle event to be sent
377 // also wxEventLoop::Exit is implemented that way, so that exiting an
378 // event loop won't require an event being in the queue
380 #include "wx/module.h"
382 #include <sys/types.h>
383 #include <sys/time.h>
386 static int idleFds
[2] = { -1, -1 };
388 class wxIdlePipeModule
: public wxModule
391 wxIdlePipeModule() {};
393 virtual bool OnInit()
395 // Must be done before modules are initialized
397 if( pipe(idleFds
) != 0 )
403 virtual void OnExit()
409 DECLARE_DYNAMIC_CLASS(wxIdlePipeModule
)
412 IMPLEMENT_DYNAMIC_CLASS(wxIdlePipeModule
, wxModule
);
414 static void wxInputCallback( XtPointer
, int* fd
, XtInputId
* )
418 // wxWakeUpIdle may have been called more than once
422 struct timeval timeout
;
430 if( select( *fd
+ 1, &in
, NULL
, NULL
, &timeout
) <= 0 )
432 if( read( *fd
, buffer
, sizeof(buffer
) - 1 ) != sizeof(buffer
) - 1 )
437 static void wxBreakDispatch()
439 char dummy
= 0; // for valgrind
441 // check if wxWakeUpIdle has already been called
443 struct timeval timeout
;
449 FD_SET( idleFds
[0], &in
);
451 if( select( idleFds
[0] + 1, &in
, NULL
, NULL
, &timeout
) > 0 )
454 // write a single byte
455 write( idleFds
[1], &dummy
, 1 );
458 void wxApp::WakeUpIdle()
465 if( pipe(idleFds
) != 0 )
470 bool wxAddIdleCallback()
472 if (!wxInitIdleFds())
475 // install input handler for wxWakeUpIdle
476 XtAppAddInput((XtAppContext
) wxTheApp
->GetAppContext(),
478 (XtPointer
)XtInputReadMask
,