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 // ----------------------------------------------------------------------------
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
24 #define XtParent XTPARENT
25 #define XtDisplay XTDISPLAY
31 #include "wx/window.h"
32 #include "wx/module.h"
35 #include "wx/evtloop.h"
36 #include "wx/thread.h"
39 #pragma message disable nosimpint
44 #pragma message enable nosimpint
47 #include "wx/unix/private.h"
48 #include "wx/motif/private.h"
50 #ifdef HAVE_SYS_SELECT_H
51 # include <sys/select.h>
54 static bool CheckForKeyUp(XEvent
* event
);
55 static bool CheckForKeyDown(XEvent
* event
);
56 static bool CheckForAccelerator(XEvent
* event
);
57 static void ProcessXEvent(XEvent
* event
);
58 static void wxBreakDispatch();
60 // ----------------------------------------------------------------------------
62 // ----------------------------------------------------------------------------
64 class WXDLLEXPORT wxEventLoopImpl
68 wxEventLoopImpl() { SetExitCode(0); }
70 // set/get the exit code
71 void SetExitCode(int exitcode
) { m_exitcode
= exitcode
; }
72 int GetExitCode() const { return m_exitcode
; }
74 bool SendIdleMessage();
75 bool GetKeepGoing() const { return m_keepGoing
; }
76 void SetKeepGoing(bool keepGoing
) { m_keepGoing
= keepGoing
; }
78 // the exit code of the event loop
83 // ----------------------------------------------------------------------------
84 // wxEventLoopImpl idle event processing
85 // ----------------------------------------------------------------------------
87 static bool SendIdleMessage()
89 return wxTheApp
->ProcessIdle();
92 bool wxEventLoopImpl::SendIdleMessage()
94 return ::SendIdleMessage();
97 // ============================================================================
98 // wxEventLoop implementation
99 // ============================================================================
101 // ----------------------------------------------------------------------------
102 // wxEventLoop running and exiting
103 // ----------------------------------------------------------------------------
105 wxGUIEventLoop::~wxGUIEventLoop()
107 wxASSERT_MSG( !m_impl
, _T("should have been deleted in Run()") );
110 int wxGUIEventLoop::Run()
112 // event loops are not recursive, you need to create another loop!
113 wxCHECK_MSG( !IsRunning(), -1, _T("can't reenter a message loop") );
115 wxEventLoopActivator
activate(this);
117 m_impl
= new wxEventLoopImpl
;
118 m_impl
->SetKeepGoing( true );
122 if( !wxDoEventLoopIteration( *this ) )
128 int exitcode
= m_impl
->GetExitCode();
135 void wxGUIEventLoop::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 wxGUIEventLoop::Pending() const
151 return XtAppPending( (XtAppContext
)wxTheApp
->GetAppContext() ) != 0;
154 bool wxGUIEventLoop::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
173 return m_impl
? m_impl
->GetKeepGoing() : true;
176 // ----------------------------------------------------------------------------
177 // Static functions (originally in app.cpp)
178 // ----------------------------------------------------------------------------
180 void ProcessXEvent(XEvent
* event
)
182 if (event
->type
== KeyPress
)
184 if (CheckForAccelerator(event
))
186 // Do nothing! We intercepted and processed the event as an
190 // It seemed before that this hack was redundant and
191 // key down events were being generated by wxCanvasInputEvent.
192 // But no longer - why ???
194 else if (CheckForKeyDown(event
))
196 // We intercepted and processed the key down event
201 XtDispatchEvent(event
);
205 else if (event
->type
== KeyRelease
)
207 // TODO: work out why we still need this ! -michael
209 if (::CheckForKeyUp(event
))
211 // We intercepted and processed the key up event
216 XtDispatchEvent(event
);
220 else if (event
->type
== PropertyNotify
)
222 wxTheApp
->HandlePropertyChange(event
);
225 else if (event
->type
== ResizeRequest
)
227 /* Terry Gitnick <terryg@scientech.com> - 1/21/98
228 * If resize event, don't resize until the last resize event for this
229 * window is recieved. Prevents flicker as windows are resized.
232 Display
*disp
= event
->xany
.display
;
233 Window win
= event
->xany
.window
;
238 while( XCheckTypedWindowEvent (disp
, win
, ResizeRequest
, &report
));
240 // TODO: when implementing refresh optimization, we can use
241 // XtAddExposureToRegion to expand the window's paint region.
243 XtDispatchEvent(event
);
247 XtDispatchEvent(event
);
251 // Returns true if an accelerator has been processed
252 bool CheckForAccelerator(XEvent
* event
)
254 if (event
->xany
.type
== KeyPress
)
256 // Find a wxWindow for this window
257 // TODO: should get display for the window, not the current display
258 Widget widget
= XtWindowToWidget(event
->xany
.display
,
260 wxWindow
* win
= NULL
;
262 // Find the first wxWindow that corresponds to this event window
263 while (widget
&& ((win
= wxGetWindowFromTable(widget
))!=NULL
))
264 widget
= XtParent(widget
);
269 wxKeyEvent
keyEvent(wxEVT_CHAR
);
270 wxTranslateKeyEvent(keyEvent
, win
, (Widget
) 0, event
);
272 // Now we have a wxKeyEvent and we have a wxWindow.
273 // Go up the hierarchy until we find a matching accelerator,
274 // or we get to the top.
277 if (win
->ProcessAccelerator(keyEvent
))
279 win
= win
->GetParent();
286 // bool wxApp::CheckForKeyDown(WXEvent* event) { wxFAIL; return false; }
288 bool CheckForKeyDown(XEvent
* event
)
290 if (event
->xany
.type
== KeyPress
)
292 Widget widget
= XtWindowToWidget(event
->xany
.display
,
294 wxWindow
* win
= NULL
;
296 // Find the first wxWindow that corresponds to this event window
297 while (widget
&& ((win
= wxGetWindowFromTable(widget
))!=NULL
))
298 widget
= XtParent(widget
);
303 wxKeyEvent
keyEvent(wxEVT_KEY_DOWN
);
304 wxTranslateKeyEvent(keyEvent
, win
, (Widget
) 0, event
);
306 return win
->HandleWindowEvent( keyEvent
);
312 // bool wxApp::CheckForKeyUp(WXEvent* event) { wxFAIL; return false; }
314 bool CheckForKeyUp(XEvent
* event
)
316 if (event
->xany
.type
== KeyRelease
)
318 Widget widget
= XtWindowToWidget(event
->xany
.display
,
320 wxWindow
* win
= NULL
;
322 // Find the first wxWindow that corresponds to this event window
323 while (widget
&& ((win
= wxGetWindowFromTable(widget
))!=NULL
))
324 widget
= XtParent(widget
);
329 wxKeyEvent
keyEvent(wxEVT_KEY_UP
);
330 wxTranslateKeyEvent(keyEvent
, win
, (Widget
) 0, event
);
332 return win
->HandleWindowEvent( keyEvent
);
338 // ----------------------------------------------------------------------------
339 // executes one main loop iteration (declared in include/wx/motif/private.h)
340 // ----------------------------------------------------------------------------
342 bool wxDoEventLoopIteration( wxGUIEventLoop
& evtLoop
)
344 bool moreRequested
, pendingEvents
;
348 pendingEvents
= evtLoop
.Pending();
349 if( pendingEvents
) break;
350 moreRequested
= ::SendIdleMessage();
351 if( !moreRequested
) break;
355 if( !pendingEvents
&& !moreRequested
)
357 // leave the main loop to give other threads a chance to
358 // perform their GUI work
365 if( !evtLoop
.Dispatch() )
371 // ----------------------------------------------------------------------------
372 // ::wxWakeUpIdle implementation
373 // ----------------------------------------------------------------------------
375 // As per Vadim's suggestion, we open a pipe, and XtAppAddInputSource it;
376 // writing a single byte to the pipe will cause wxEventLoop::Pending
377 // to return true, and wxEventLoop::Dispatch to dispatch an input handler
378 // that simply removes the byte(s), and to return, which will cause
379 // an idle event to be sent
381 // also wxEventLoop::Exit is implemented that way, so that exiting an
382 // event loop won't require an event being in the queue
384 #include <sys/types.h>
385 #include <sys/time.h>
388 static int idleFds
[2] = { -1, -1 };
390 class wxIdlePipeModule
: public wxModule
393 wxIdlePipeModule() {};
395 virtual bool OnInit()
397 // Must be done before modules are initialized
399 if( pipe(idleFds
) != 0 )
405 virtual void OnExit()
411 DECLARE_DYNAMIC_CLASS(wxIdlePipeModule
)
414 IMPLEMENT_DYNAMIC_CLASS(wxIdlePipeModule
, wxModule
)
416 static void wxInputCallback( XtPointer
, int* fd
, XtInputId
* )
420 // wxWakeUpIdle may have been called more than once
424 struct timeval timeout
;
430 wxFD_SET( *fd
, &in
);
432 if( select( *fd
+ 1, &in
, NULL
, NULL
, &timeout
) <= 0 )
434 if( read( *fd
, buffer
, sizeof(buffer
) - 1 ) != sizeof(buffer
) - 1 )
439 static void wxBreakDispatch()
441 char dummy
= 0; // for valgrind
443 // check if wxWakeUpIdle has already been called
445 struct timeval timeout
;
451 wxFD_SET( idleFds
[0], &in
);
453 if( select( idleFds
[0] + 1, &in
, NULL
, NULL
, &timeout
) > 0 )
456 // write a single byte
457 write( idleFds
[1], &dummy
, 1 );
460 void wxApp::WakeUpIdle()
467 if( pipe(idleFds
) != 0 )
472 bool wxAddIdleCallback()
474 if (!wxInitIdleFds())
477 // install input handler for wxWakeUpIdle
478 XtAppAddInput((XtAppContext
) wxTheApp
->GetAppContext(),
480 (XtPointer
)XtInputReadMask
,