1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/motif/evtloop.cpp
3 // Purpose: implements wxEventLoop for Motif
4 // Author: Mattia Barbon
8 // Copyright: (c) 2002 Mattia Barbon
9 // Licence: 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
, wxT("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, wxT("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();
129 void wxGUIEventLoop::Exit(int rc
)
131 wxCHECK_RET( IsRunning(), wxT("can't call Exit() if not running") );
133 m_impl
->SetExitCode(rc
);
134 m_impl
->SetKeepGoing( false );
139 bool wxGUIEventLoop::YieldFor(long eventsToProcess
)
141 m_isInsideYield
= true;
142 m_eventsToProcessInsideYield
= eventsToProcess
;
144 while (wxTheApp
&& wxTheApp
->Pending())
145 // TODO: implement event filtering using the eventsToProcess mask
146 wxTheApp
->Dispatch();
148 m_isInsideYield
= false;
153 // ----------------------------------------------------------------------------
154 // wxEventLoop message processing dispatching
155 // ----------------------------------------------------------------------------
157 bool wxGUIEventLoop::Pending() const
159 return XtAppPending( (XtAppContext
)wxTheApp
->GetAppContext() ) != 0;
162 bool wxGUIEventLoop::Dispatch()
165 XtAppContext context
= (XtAppContext
)wxTheApp
->GetAppContext();
167 if( XtAppPeekEvent( context
, &event
) != 0 )
169 XtAppNextEvent( context
, &event
);
170 ProcessXEvent( &event
);
174 XtAppProcessEvent( context
, XtIMTimer
| XtIMAlternateInput
181 return m_impl
? m_impl
->GetKeepGoing() : true;
184 // ----------------------------------------------------------------------------
185 // Static functions (originally in app.cpp)
186 // ----------------------------------------------------------------------------
188 void ProcessXEvent(XEvent
* event
)
190 if (event
->type
== KeyPress
)
192 if (CheckForAccelerator(event
))
194 // Do nothing! We intercepted and processed the event as an
198 // It seemed before that this hack was redundant and
199 // key down events were being generated by wxCanvasInputEvent.
200 // But no longer - why ???
202 else if (CheckForKeyDown(event
))
204 // We intercepted and processed the key down event
209 XtDispatchEvent(event
);
213 else if (event
->type
== KeyRelease
)
215 // TODO: work out why we still need this ! -michael
217 if (::CheckForKeyUp(event
))
219 // We intercepted and processed the key up event
224 XtDispatchEvent(event
);
228 else if (event
->type
== PropertyNotify
)
230 wxTheApp
->HandlePropertyChange(event
);
233 else if (event
->type
== ResizeRequest
)
235 /* Terry Gitnick <terryg@scientech.com> - 1/21/98
236 * If resize event, don't resize until the last resize event for this
237 * window is recieved. Prevents flicker as windows are resized.
240 Display
*disp
= event
->xany
.display
;
241 Window win
= event
->xany
.window
;
246 while( XCheckTypedWindowEvent (disp
, win
, ResizeRequest
, &report
));
248 // TODO: when implementing refresh optimization, we can use
249 // XtAddExposureToRegion to expand the window's paint region.
251 XtDispatchEvent(event
);
255 XtDispatchEvent(event
);
259 // Returns true if an accelerator has been processed
260 bool CheckForAccelerator(XEvent
* event
)
262 if (event
->xany
.type
== KeyPress
)
264 // Find a wxWindow for this window
265 // TODO: should get display for the window, not the current display
266 Widget widget
= XtWindowToWidget(event
->xany
.display
,
268 wxWindow
* win
= NULL
;
270 // Find the first wxWindow that corresponds to this event window
271 while (widget
&& ((win
= wxGetWindowFromTable(widget
))!=NULL
))
272 widget
= XtParent(widget
);
277 wxKeyEvent
keyEvent(wxEVT_CHAR
);
278 wxTranslateKeyEvent(keyEvent
, win
, (Widget
) 0, event
);
280 // Now we have a wxKeyEvent and we have a wxWindow.
281 // Go up the hierarchy until we find a matching accelerator,
282 // or we get to the top.
285 if (win
->ProcessAccelerator(keyEvent
))
287 win
= win
->GetParent();
294 // bool wxApp::CheckForKeyDown(WXEvent* event) { wxFAIL; return false; }
296 bool CheckForKeyDown(XEvent
* event
)
298 if (event
->xany
.type
== KeyPress
)
300 Widget widget
= XtWindowToWidget(event
->xany
.display
,
302 wxWindow
* win
= NULL
;
304 // Find the first wxWindow that corresponds to this event window
305 while (widget
&& ((win
= wxGetWindowFromTable(widget
))!=NULL
))
306 widget
= XtParent(widget
);
311 wxKeyEvent
keyEvent(wxEVT_KEY_DOWN
);
312 wxTranslateKeyEvent(keyEvent
, win
, (Widget
) 0, event
);
314 return win
->HandleWindowEvent( keyEvent
);
320 // bool wxApp::CheckForKeyUp(WXEvent* event) { wxFAIL; return false; }
322 bool CheckForKeyUp(XEvent
* event
)
324 if (event
->xany
.type
== KeyRelease
)
326 Widget widget
= XtWindowToWidget(event
->xany
.display
,
328 wxWindow
* win
= NULL
;
330 // Find the first wxWindow that corresponds to this event window
331 while (widget
&& ((win
= wxGetWindowFromTable(widget
))!=NULL
))
332 widget
= XtParent(widget
);
337 wxKeyEvent
keyEvent(wxEVT_KEY_UP
);
338 wxTranslateKeyEvent(keyEvent
, win
, (Widget
) 0, event
);
340 return win
->HandleWindowEvent( keyEvent
);
346 // ----------------------------------------------------------------------------
347 // executes one main loop iteration (declared in include/wx/motif/private.h)
348 // ----------------------------------------------------------------------------
350 bool wxDoEventLoopIteration( wxGUIEventLoop
& evtLoop
)
352 bool moreRequested
, pendingEvents
;
356 pendingEvents
= evtLoop
.Pending();
357 if( pendingEvents
) break;
358 moreRequested
= ::SendIdleMessage();
359 if( !moreRequested
) break;
363 if( !pendingEvents
&& !moreRequested
)
365 // leave the main loop to give other threads a chance to
366 // perform their GUI work
373 if( !evtLoop
.Dispatch() )
379 // ----------------------------------------------------------------------------
380 // ::wxWakeUpIdle implementation
381 // ----------------------------------------------------------------------------
383 // As per Vadim's suggestion, we open a pipe, and XtAppAddInputSource it;
384 // writing a single byte to the pipe will cause wxEventLoop::Pending
385 // to return true, and wxEventLoop::Dispatch to dispatch an input handler
386 // that simply removes the byte(s), and to return, which will cause
387 // an idle event to be sent
389 // also wxEventLoop::Exit is implemented that way, so that exiting an
390 // event loop won't require an event being in the queue
392 #include <sys/types.h>
393 #include <sys/time.h>
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
;
438 wxFD_SET( *fd
, &in
);
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 wxFD_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 XtAppAddInput((XtAppContext
) wxTheApp
->GetAppContext(),
488 (XtPointer
)XtInputReadMask
,