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"
32 #include "wx/module.h"
35 #include "wx/evtloop.h"
38 #pragma message disable nosimpint
43 #pragma message enable nosimpint
46 #include "wx/unix/private.h"
47 #include "wx/motif/private.h"
49 #ifdef HAVE_SYS_SELECT_H
50 # include <sys/select.h>
53 static bool CheckForKeyUp(XEvent
* event
);
54 static bool CheckForKeyDown(XEvent
* event
);
55 static bool CheckForAccelerator(XEvent
* event
);
56 static void ProcessXEvent(XEvent
* event
);
57 static void wxBreakDispatch();
59 // ----------------------------------------------------------------------------
61 // ----------------------------------------------------------------------------
63 class WXDLLEXPORT wxEventLoopImpl
67 wxEventLoopImpl() { SetExitCode(0); }
69 // set/get the exit code
70 void SetExitCode(int exitcode
) { m_exitcode
= exitcode
; }
71 int GetExitCode() const { return m_exitcode
; }
73 bool SendIdleMessage();
74 bool GetKeepGoing() const { return m_keepGoing
; }
75 void SetKeepGoing(bool keepGoing
) { m_keepGoing
= keepGoing
; }
77 // the exit code of the event loop
82 // ----------------------------------------------------------------------------
83 // wxEventLoopImpl idle event processing
84 // ----------------------------------------------------------------------------
86 static bool SendIdleMessage()
88 return wxTheApp
->ProcessIdle();
91 bool wxEventLoopImpl::SendIdleMessage()
93 return ::SendIdleMessage();
96 // ============================================================================
97 // wxEventLoop implementation
98 // ============================================================================
100 // ----------------------------------------------------------------------------
101 // wxEventLoop running and exiting
102 // ----------------------------------------------------------------------------
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 wxEventLoopActivator
activate(this);
116 m_impl
= new wxEventLoopImpl
;
117 m_impl
->SetKeepGoing( true );
121 if( !wxDoEventLoopIteration( *this ) )
125 int exitcode
= m_impl
->GetExitCode();
132 void wxEventLoop::Exit(int rc
)
134 wxCHECK_RET( IsRunning(), _T("can't call Exit() if not running") );
136 m_impl
->SetExitCode(rc
);
137 m_impl
->SetKeepGoing( false );
142 // ----------------------------------------------------------------------------
143 // wxEventLoop message processing dispatching
144 // ----------------------------------------------------------------------------
146 bool wxEventLoop::Pending() const
148 return XtAppPending( (XtAppContext
)wxTheApp
->GetAppContext() ) != 0;
151 bool wxEventLoop::Dispatch()
154 XtAppContext context
= (XtAppContext
)wxTheApp
->GetAppContext();
156 if( XtAppPeekEvent( context
, &event
) != 0 )
158 XtAppNextEvent( context
, &event
);
159 ProcessXEvent( &event
);
163 XtAppProcessEvent( context
, XtIMTimer
| XtIMAlternateInput
170 return m_impl
? m_impl
->GetKeepGoing() : true;
173 // ----------------------------------------------------------------------------
174 // Static functions (originally in app.cpp)
175 // ----------------------------------------------------------------------------
177 void ProcessXEvent(XEvent
* event
)
179 if (event
->type
== KeyPress
)
181 if (CheckForAccelerator(event
))
183 // Do nothing! We intercepted and processed the event as an
187 // It seemed before that this hack was redundant and
188 // key down events were being generated by wxCanvasInputEvent.
189 // But no longer - why ???
191 else if (CheckForKeyDown(event
))
193 // We intercepted and processed the key down event
198 XtDispatchEvent(event
);
202 else if (event
->type
== KeyRelease
)
204 // TODO: work out why we still need this ! -michael
206 if (::CheckForKeyUp(event
))
208 // We intercepted and processed the key up event
213 XtDispatchEvent(event
);
217 else if (event
->type
== PropertyNotify
)
219 wxTheApp
->HandlePropertyChange(event
);
222 else if (event
->type
== ResizeRequest
)
224 /* Terry Gitnick <terryg@scientech.com> - 1/21/98
225 * If resize event, don't resize until the last resize event for this
226 * window is recieved. Prevents flicker as windows are resized.
229 Display
*disp
= event
->xany
.display
;
230 Window win
= event
->xany
.window
;
235 while( XCheckTypedWindowEvent (disp
, win
, ResizeRequest
, &report
));
237 // TODO: when implementing refresh optimization, we can use
238 // XtAddExposureToRegion to expand the window's paint region.
240 XtDispatchEvent(event
);
244 XtDispatchEvent(event
);
248 // Returns true if an accelerator has been processed
249 bool CheckForAccelerator(XEvent
* event
)
251 if (event
->xany
.type
== KeyPress
)
253 // Find a wxWindow for this window
254 // TODO: should get display for the window, not the current display
255 Widget widget
= XtWindowToWidget(event
->xany
.display
,
257 wxWindow
* win
= NULL
;
259 // Find the first wxWindow that corresponds to this event window
260 while (widget
&& ((win
= wxGetWindowFromTable(widget
))!=NULL
))
261 widget
= XtParent(widget
);
266 wxKeyEvent
keyEvent(wxEVT_CHAR
);
267 wxTranslateKeyEvent(keyEvent
, win
, (Widget
) 0, event
);
269 // Now we have a wxKeyEvent and we have a wxWindow.
270 // Go up the hierarchy until we find a matching accelerator,
271 // or we get to the top.
274 if (win
->ProcessAccelerator(keyEvent
))
276 win
= win
->GetParent();
283 // bool wxApp::CheckForKeyDown(WXEvent* event) { wxFAIL; return false; }
285 bool CheckForKeyDown(XEvent
* event
)
287 if (event
->xany
.type
== KeyPress
)
289 Widget widget
= XtWindowToWidget(event
->xany
.display
,
291 wxWindow
* win
= NULL
;
293 // Find the first wxWindow that corresponds to this event window
294 while (widget
&& ((win
= wxGetWindowFromTable(widget
))!=NULL
))
295 widget
= XtParent(widget
);
300 wxKeyEvent
keyEvent(wxEVT_KEY_DOWN
);
301 wxTranslateKeyEvent(keyEvent
, win
, (Widget
) 0, event
);
303 return win
->GetEventHandler()->ProcessEvent( keyEvent
);
309 // bool wxApp::CheckForKeyUp(WXEvent* event) { wxFAIL; return false; }
311 bool CheckForKeyUp(XEvent
* event
)
313 if (event
->xany
.type
== KeyRelease
)
315 Widget widget
= XtWindowToWidget(event
->xany
.display
,
317 wxWindow
* win
= NULL
;
319 // Find the first wxWindow that corresponds to this event window
320 while (widget
&& ((win
= wxGetWindowFromTable(widget
))!=NULL
))
321 widget
= XtParent(widget
);
326 wxKeyEvent
keyEvent(wxEVT_KEY_UP
);
327 wxTranslateKeyEvent(keyEvent
, win
, (Widget
) 0, event
);
329 return win
->GetEventHandler()->ProcessEvent( keyEvent
);
335 // ----------------------------------------------------------------------------
336 // executes one main loop iteration (declared in include/wx/motif/private.h)
337 // ----------------------------------------------------------------------------
339 bool wxDoEventLoopIteration( wxEventLoop
& evtLoop
)
341 bool moreRequested
, pendingEvents
;
345 pendingEvents
= evtLoop
.Pending();
346 if( pendingEvents
) break;
347 moreRequested
= ::SendIdleMessage();
348 if( !moreRequested
) break;
352 if( !pendingEvents
&& !moreRequested
)
354 // leave the main loop to give other threads a chance to
355 // perform their GUI work
362 if( !evtLoop
.Dispatch() )
368 // ----------------------------------------------------------------------------
369 // ::wxWakeUpIdle implementation
370 // ----------------------------------------------------------------------------
372 // As per Vadim's suggestion, we open a pipe, and XtAppAddInputSource it;
373 // writing a single byte to the pipe will cause wxEventLoop::Pending
374 // to return true, and wxEventLoop::Dispatch to dispatch an input handler
375 // that simply removes the byte(s), and to return, which will cause
376 // an idle event to be sent
378 // also wxEventLoop::Exit is implemented that way, so that exiting an
379 // event loop won't require an event being in the queue
381 #include <sys/types.h>
382 #include <sys/time.h>
385 static int idleFds
[2] = { -1, -1 };
387 class wxIdlePipeModule
: public wxModule
390 wxIdlePipeModule() {};
392 virtual bool OnInit()
394 // Must be done before modules are initialized
396 if( pipe(idleFds
) != 0 )
402 virtual void OnExit()
408 DECLARE_DYNAMIC_CLASS(wxIdlePipeModule
)
411 IMPLEMENT_DYNAMIC_CLASS(wxIdlePipeModule
, wxModule
)
413 static void wxInputCallback( XtPointer
, int* fd
, XtInputId
* )
417 // wxWakeUpIdle may have been called more than once
421 struct timeval timeout
;
427 wxFD_SET( *fd
, &in
);
429 if( select( *fd
+ 1, &in
, NULL
, NULL
, &timeout
) <= 0 )
431 if( read( *fd
, buffer
, sizeof(buffer
) - 1 ) != sizeof(buffer
) - 1 )
436 static void wxBreakDispatch()
438 char dummy
= 0; // for valgrind
440 // check if wxWakeUpIdle has already been called
442 struct timeval timeout
;
448 wxFD_SET( idleFds
[0], &in
);
450 if( select( idleFds
[0] + 1, &in
, NULL
, NULL
, &timeout
) > 0 )
453 // write a single byte
454 write( idleFds
[1], &dummy
, 1 );
457 void wxApp::WakeUpIdle()
464 if( pipe(idleFds
) != 0 )
469 bool wxAddIdleCallback()
471 if (!wxInitIdleFds())
474 // install input handler for wxWakeUpIdle
475 XtAppAddInput((XtAppContext
) wxTheApp
->GetAppContext(),
477 (XtPointer
)XtInputReadMask
,