1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/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/window.h"
34 #include "wx/evtloop.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::~wxEventLoop()
101 wxASSERT_MSG( !m_impl
, _T("should have been deleted in Run()") );
104 int wxEventLoop::Run()
106 // event loops are not recursive, you need to create another loop!
107 wxCHECK_MSG( !IsRunning(), -1, _T("can't reenter a message loop") );
109 wxEventLoopActivator
activate(this);
111 m_impl
= new wxEventLoopImpl
;
112 m_impl
->SetKeepGoing( true );
116 if( !wxDoEventLoopIteration( *this ) )
120 int exitcode
= m_impl
->GetExitCode();
127 void wxEventLoop::Exit(int rc
)
129 wxCHECK_RET( IsRunning(), _T("can't call Exit() if not running") );
131 m_impl
->SetExitCode(rc
);
132 m_impl
->SetKeepGoing( false );
137 // ----------------------------------------------------------------------------
138 // wxEventLoop message processing dispatching
139 // ----------------------------------------------------------------------------
141 bool wxEventLoop::Pending() const
143 return XtAppPending( (XtAppContext
)wxTheApp
->GetAppContext() ) != 0;
146 bool wxEventLoop::Dispatch()
149 XtAppContext context
= (XtAppContext
)wxTheApp
->GetAppContext();
151 if( XtAppPeekEvent( context
, &event
) != 0 )
153 XtAppNextEvent( context
, &event
);
154 ProcessXEvent( &event
);
158 XtAppProcessEvent( context
, XtIMTimer
| XtIMAlternateInput
165 return m_impl
? m_impl
->GetKeepGoing() : true;
168 // ----------------------------------------------------------------------------
169 // Static functions (originally in app.cpp)
170 // ----------------------------------------------------------------------------
172 void ProcessXEvent(XEvent
* event
)
174 if (event
->type
== KeyPress
)
176 if (CheckForAccelerator(event
))
178 // Do nothing! We intercepted and processed the event as an
182 // It seemed before that this hack was redundant and
183 // key down events were being generated by wxCanvasInputEvent.
184 // But no longer - why ???
186 else if (CheckForKeyDown(event
))
188 // We intercepted and processed the key down event
193 XtDispatchEvent(event
);
197 else if (event
->type
== KeyRelease
)
199 // TODO: work out why we still need this ! -michael
201 if (::CheckForKeyUp(event
))
203 // We intercepted and processed the key up event
208 XtDispatchEvent(event
);
212 else if (event
->type
== PropertyNotify
)
214 wxTheApp
->HandlePropertyChange(event
);
217 else if (event
->type
== ResizeRequest
)
219 /* Terry Gitnick <terryg@scientech.com> - 1/21/98
220 * If resize event, don't resize until the last resize event for this
221 * window is recieved. Prevents flicker as windows are resized.
224 Display
*disp
= event
->xany
.display
;
225 Window win
= event
->xany
.window
;
230 while( XCheckTypedWindowEvent (disp
, win
, ResizeRequest
, &report
));
232 // TODO: when implementing refresh optimization, we can use
233 // XtAddExposureToRegion to expand the window's paint region.
235 XtDispatchEvent(event
);
239 XtDispatchEvent(event
);
243 // Returns true if an accelerator has been processed
244 bool CheckForAccelerator(XEvent
* event
)
246 if (event
->xany
.type
== KeyPress
)
248 // Find a wxWindow for this window
249 // TODO: should get display for the window, not the current display
250 Widget widget
= XtWindowToWidget(event
->xany
.display
,
252 wxWindow
* win
= NULL
;
254 // Find the first wxWindow that corresponds to this event window
255 while (widget
&& ((win
= wxGetWindowFromTable(widget
))!=NULL
))
256 widget
= XtParent(widget
);
261 wxKeyEvent
keyEvent(wxEVT_CHAR
);
262 wxTranslateKeyEvent(keyEvent
, win
, (Widget
) 0, event
);
264 // Now we have a wxKeyEvent and we have a wxWindow.
265 // Go up the hierarchy until we find a matching accelerator,
266 // or we get to the top.
269 if (win
->ProcessAccelerator(keyEvent
))
271 win
= win
->GetParent();
278 // bool wxApp::CheckForKeyDown(WXEvent* event) { wxFAIL; return false; }
280 bool CheckForKeyDown(XEvent
* event
)
282 if (event
->xany
.type
== KeyPress
)
284 Widget widget
= XtWindowToWidget(event
->xany
.display
,
286 wxWindow
* win
= NULL
;
288 // Find the first wxWindow that corresponds to this event window
289 while (widget
&& ((win
= wxGetWindowFromTable(widget
))!=NULL
))
290 widget
= XtParent(widget
);
295 wxKeyEvent
keyEvent(wxEVT_KEY_DOWN
);
296 wxTranslateKeyEvent(keyEvent
, win
, (Widget
) 0, event
);
298 return win
->GetEventHandler()->ProcessEvent( keyEvent
);
304 // bool wxApp::CheckForKeyUp(WXEvent* event) { wxFAIL; return false; }
306 bool CheckForKeyUp(XEvent
* event
)
308 if (event
->xany
.type
== KeyRelease
)
310 Widget widget
= XtWindowToWidget(event
->xany
.display
,
312 wxWindow
* win
= NULL
;
314 // Find the first wxWindow that corresponds to this event window
315 while (widget
&& ((win
= wxGetWindowFromTable(widget
))!=NULL
))
316 widget
= XtParent(widget
);
321 wxKeyEvent
keyEvent(wxEVT_KEY_UP
);
322 wxTranslateKeyEvent(keyEvent
, win
, (Widget
) 0, event
);
324 return win
->GetEventHandler()->ProcessEvent( keyEvent
);
330 // ----------------------------------------------------------------------------
331 // executes one main loop iteration (declared in include/wx/motif/private.h)
332 // ----------------------------------------------------------------------------
334 bool wxDoEventLoopIteration( wxEventLoop
& evtLoop
)
336 bool moreRequested
, pendingEvents
;
340 pendingEvents
= evtLoop
.Pending();
341 if( pendingEvents
) break;
342 moreRequested
= ::SendIdleMessage();
343 if( !moreRequested
) break;
347 if( !pendingEvents
&& !moreRequested
)
349 // leave the main loop to give other threads a chance to
350 // perform their GUI work
357 if( !evtLoop
.Dispatch() )
363 // ----------------------------------------------------------------------------
364 // ::wxWakeUpIdle implementation
365 // ----------------------------------------------------------------------------
367 // As per Vadim's suggestion, we open a pipe, and XtAppAddInputSource it;
368 // writing a single byte to the pipe will cause wxEventLoop::Pending
369 // to return true, and wxEventLoop::Dispatch to dispatch an input handler
370 // that simply removes the byte(s), and to return, which will cause
371 // an idle event to be sent
373 // also wxEventLoop::Exit is implemented that way, so that exiting an
374 // event loop won't require an event being in the queue
376 #include "wx/module.h"
378 #include <sys/types.h>
379 #include <sys/time.h>
382 static int idleFds
[2] = { -1, -1 };
384 class wxIdlePipeModule
: public wxModule
387 wxIdlePipeModule() {};
389 virtual bool OnInit()
391 // Must be done before modules are initialized
393 if( pipe(idleFds
) != 0 )
399 virtual void OnExit()
405 DECLARE_DYNAMIC_CLASS(wxIdlePipeModule
)
408 IMPLEMENT_DYNAMIC_CLASS(wxIdlePipeModule
, wxModule
)
410 static void wxInputCallback( XtPointer
, int* fd
, XtInputId
* )
414 // wxWakeUpIdle may have been called more than once
418 struct timeval timeout
;
424 wxFD_SET( *fd
, &in
);
426 if( select( *fd
+ 1, &in
, NULL
, NULL
, &timeout
) <= 0 )
428 if( read( *fd
, buffer
, sizeof(buffer
) - 1 ) != sizeof(buffer
) - 1 )
433 static void wxBreakDispatch()
435 char dummy
= 0; // for valgrind
437 // check if wxWakeUpIdle has already been called
439 struct timeval timeout
;
445 wxFD_SET( idleFds
[0], &in
);
447 if( select( idleFds
[0] + 1, &in
, NULL
, NULL
, &timeout
) > 0 )
450 // write a single byte
451 write( idleFds
[1], &dummy
, 1 );
454 void wxApp::WakeUpIdle()
461 if( pipe(idleFds
) != 0 )
466 bool wxAddIdleCallback()
468 if (!wxInitIdleFds())
471 // install input handler for wxWakeUpIdle
472 XtAppAddInput((XtAppContext
) wxTheApp
->GetAppContext(),
474 (XtPointer
)XtInputReadMask
,