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()
86 return wxTheApp
->ProcessIdle();
89 bool wxEventLoopImpl::SendIdleMessage()
91 return ::SendIdleMessage();
94 // ============================================================================
95 // wxEventLoop implementation
96 // ============================================================================
98 // ----------------------------------------------------------------------------
99 // wxEventLoop running and exiting
100 // ----------------------------------------------------------------------------
102 wxEventLoop
*wxEventLoopBase::ms_activeLoop
= NULL
;
104 wxEventLoop::~wxEventLoop()
106 wxASSERT_MSG( !m_impl
, _T("should have been deleted in Run()") );
109 int wxEventLoop::Run()
111 // event loops are not recursive, you need to create another loop!
112 wxCHECK_MSG( !IsRunning(), -1, _T("can't reenter a message loop") );
114 wxEventLoop
*oldLoop
= ms_activeLoop
;
115 ms_activeLoop
= this;
117 m_impl
= new wxEventLoopImpl
;
118 m_impl
->SetKeepGoing( true );
122 if( !wxDoEventLoopIteration( *this ) )
126 int exitcode
= m_impl
->GetExitCode();
130 ms_activeLoop
= oldLoop
;
135 void wxEventLoop::Exit(int rc
)
137 wxCHECK_RET( IsRunning(), _T("can't call Exit() if not running") );
139 m_impl
->SetExitCode(rc
);
140 m_impl
->SetKeepGoing( false );
145 // ----------------------------------------------------------------------------
146 // wxEventLoop message processing dispatching
147 // ----------------------------------------------------------------------------
149 bool wxEventLoop::Pending() const
151 return XtAppPending( (XtAppContext
)wxTheApp
->GetAppContext() ) != 0;
154 bool wxEventLoop::Dispatch()
157 XtAppContext context
= (XtAppContext
)wxTheApp
->GetAppContext();
159 if( XtAppPeekEvent( context
, &event
) != 0 )
161 XtAppNextEvent( context
, &event
);
162 ProcessXEvent( &event
);
166 XtAppProcessEvent( context
, XtIMTimer
|XtIMAlternateInput
);
168 XtAppProcessEvent( context
, XtIMTimer
|XtIMAlternateInput
|XtIMSignal
);
171 return m_impl
? m_impl
->GetKeepGoing() : true;
174 // ----------------------------------------------------------------------------
175 // Static functions (originally in app.cpp)
176 // ----------------------------------------------------------------------------
178 void ProcessXEvent(XEvent
* event
)
180 if (event
->type
== KeyPress
)
182 if (CheckForAccelerator(event
))
184 // Do nothing! We intercepted and processed the event as an
188 // It seemed before that this hack was redundant and
189 // key down events were being generated by wxCanvasInputEvent.
190 // But no longer - why ???
192 else if (CheckForKeyDown(event
))
194 // We intercepted and processed the key down event
199 XtDispatchEvent(event
);
203 else if (event
->type
== KeyRelease
)
205 // TODO: work out why we still need this ! -michael
207 if (::CheckForKeyUp(event
))
209 // We intercepted and processed the key up event
214 XtDispatchEvent(event
);
218 else if (event
->type
== PropertyNotify
)
220 wxTheApp
->HandlePropertyChange(event
);
223 else if (event
->type
== ResizeRequest
)
225 /* Terry Gitnick <terryg@scientech.com> - 1/21/98
226 * If resize event, don't resize until the last resize event for this
227 * window is recieved. Prevents flicker as windows are resized.
230 Display
*disp
= event
->xany
.display
;
231 Window win
= event
->xany
.window
;
236 while( XCheckTypedWindowEvent (disp
, win
, ResizeRequest
, &report
));
238 // TODO: when implementing refresh optimization, we can use
239 // XtAddExposureToRegion to expand the window's paint region.
241 XtDispatchEvent(event
);
245 XtDispatchEvent(event
);
249 // Returns true if an accelerator has been processed
250 bool CheckForAccelerator(XEvent
* event
)
252 if (event
->xany
.type
== KeyPress
)
254 // Find a wxWindow for this window
255 // TODO: should get display for the window, not the current display
256 Widget widget
= XtWindowToWidget(event
->xany
.display
,
258 wxWindow
* win
= NULL
;
260 // Find the first wxWindow that corresponds to this event window
261 while (widget
&& !(win
= wxGetWindowFromTable(widget
)))
262 widget
= XtParent(widget
);
267 wxKeyEvent
keyEvent(wxEVT_CHAR
);
268 wxTranslateKeyEvent(keyEvent
, win
, (Widget
) 0, event
);
270 // Now we have a wxKeyEvent and we have a wxWindow.
271 // Go up the hierarchy until we find a matching accelerator,
272 // or we get to the top.
275 if (win
->ProcessAccelerator(keyEvent
))
277 win
= win
->GetParent();
284 // bool wxApp::CheckForKeyDown(WXEvent* event) { wxFAIL; return false; }
286 bool CheckForKeyDown(XEvent
* event
)
288 if (event
->xany
.type
== KeyPress
)
290 Widget widget
= XtWindowToWidget(event
->xany
.display
,
292 wxWindow
* win
= NULL
;
294 // Find the first wxWindow that corresponds to this event window
295 while (widget
&& !(win
= wxGetWindowFromTable(widget
)))
296 widget
= XtParent(widget
);
301 wxKeyEvent
keyEvent(wxEVT_KEY_DOWN
);
302 wxTranslateKeyEvent(keyEvent
, win
, (Widget
) 0, event
);
304 return win
->GetEventHandler()->ProcessEvent( keyEvent
);
310 // bool wxApp::CheckForKeyUp(WXEvent* event) { wxFAIL; return false; }
312 bool CheckForKeyUp(XEvent
* event
)
314 if (event
->xany
.type
== KeyRelease
)
316 Widget widget
= XtWindowToWidget(event
->xany
.display
,
318 wxWindow
* win
= NULL
;
320 // Find the first wxWindow that corresponds to this event window
321 while (widget
&& !(win
= wxGetWindowFromTable(widget
)))
322 widget
= XtParent(widget
);
327 wxKeyEvent
keyEvent(wxEVT_KEY_UP
);
328 wxTranslateKeyEvent(keyEvent
, win
, (Widget
) 0, event
);
330 return win
->GetEventHandler()->ProcessEvent( keyEvent
);
336 // ----------------------------------------------------------------------------
337 // executes one main loop iteration (declared in include/wx/motif/private.h)
338 // ----------------------------------------------------------------------------
340 bool wxDoEventLoopIteration( wxEventLoop
& evtLoop
)
342 bool moreRequested
, pendingEvents
;
346 pendingEvents
= evtLoop
.Pending();
347 if( pendingEvents
) break;
348 moreRequested
= ::SendIdleMessage();
349 if( !moreRequested
) break;
353 if( !pendingEvents
&& !moreRequested
)
355 // leave the main loop to give other threads a chance to
356 // perform their GUI work
363 if( !evtLoop
.Dispatch() )
369 // ----------------------------------------------------------------------------
370 // ::wxWakeUpIdle implementation
371 // ----------------------------------------------------------------------------
373 // As per Vadim's suggestion, we open a pipe, and XtAppAddInputSource it;
374 // writing a single byte to the pipe will cause wxEventLoop::Pending
375 // to return true, and wxEventLoop::Dispatch to dispatch an input handler
376 // that simply removes the byte(s), and to return, which will cause
377 // an idle event to be sent
379 // also wxEventLoop::Exit is implemented that way, so that exiting an
380 // event loop won't require an event being in the queue
382 #include "wx/module.h"
384 #include <sys/types.h>
385 #include <sys/time.h>
388 static XtInputId inputId
;
389 static int idleFds
[2] = { -1, -1 };
391 class wxIdlePipeModule
: public wxModule
394 wxIdlePipeModule() {};
396 virtual bool OnInit()
398 // Must be done before modules are initialized
400 if( pipe(idleFds
) != 0 )
406 virtual void OnExit()
412 DECLARE_DYNAMIC_CLASS(wxIdlePipeModule
)
415 IMPLEMENT_DYNAMIC_CLASS(wxIdlePipeModule
, wxModule
);
417 static void wxInputCallback( XtPointer
, int* fd
, XtInputId
* )
421 // wxWakeUpIdle may have been called more than once
425 struct timeval timeout
;
433 if( select( *fd
+ 1, &in
, NULL
, NULL
, &timeout
) <= 0 )
435 if( read( *fd
, buffer
, sizeof(buffer
) - 1 ) != sizeof(buffer
) - 1 )
440 static void wxBreakDispatch()
442 char dummy
= 0; // for valgrind
444 // check if wxWakeUpIdle has already been called
446 struct timeval timeout
;
452 FD_SET( idleFds
[0], &in
);
454 if( select( idleFds
[0] + 1, &in
, NULL
, NULL
, &timeout
) > 0 )
457 // write a single byte
458 write( idleFds
[1], &dummy
, 1 );
461 void wxApp::WakeUpIdle()
468 if( pipe(idleFds
) != 0 )
473 bool wxAddIdleCallback()
475 if (!wxInitIdleFds())
478 // install input handler for wxWakeUpIdle
479 inputId
= XtAppAddInput( (XtAppContext
) wxTheApp
->GetAppContext(),
481 (XtPointer
)XtInputReadMask
,