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 #ifdef HAVE_SYS_SELECT_H
49 # include <sys/select.h>
52 static bool CheckForKeyUp(XEvent
* event
);
53 static bool CheckForKeyDown(XEvent
* event
);
54 static bool CheckForAccelerator(XEvent
* event
);
55 static void ProcessXEvent(XEvent
* event
);
56 static void wxBreakDispatch();
58 // ----------------------------------------------------------------------------
60 // ----------------------------------------------------------------------------
62 class WXDLLEXPORT wxEventLoopImpl
66 wxEventLoopImpl() { SetExitCode(0); }
68 // set/get the exit code
69 void SetExitCode(int exitcode
) { m_exitcode
= exitcode
; }
70 int GetExitCode() const { return m_exitcode
; }
72 bool SendIdleMessage();
73 bool GetKeepGoing() const { return m_keepGoing
; }
74 void SetKeepGoing(bool keepGoing
) { m_keepGoing
= keepGoing
; }
76 // the exit code of the event loop
81 // ----------------------------------------------------------------------------
82 // wxEventLoopImpl idle event processing
83 // ----------------------------------------------------------------------------
85 static bool SendIdleMessage()
87 return wxTheApp
->ProcessIdle();
90 bool wxEventLoopImpl::SendIdleMessage()
92 return ::SendIdleMessage();
95 // ============================================================================
96 // wxEventLoop implementation
97 // ============================================================================
99 // ----------------------------------------------------------------------------
100 // wxEventLoop running and exiting
101 // ----------------------------------------------------------------------------
103 wxEventLoop::~wxEventLoop()
105 wxASSERT_MSG( !m_impl
, _T("should have been deleted in Run()") );
108 int wxEventLoop::Run()
110 // event loops are not recursive, you need to create another loop!
111 wxCHECK_MSG( !IsRunning(), -1, _T("can't reenter a message loop") );
113 wxEventLoopActivator
activate(this);
115 m_impl
= new wxEventLoopImpl
;
116 m_impl
->SetKeepGoing( true );
120 if( !wxDoEventLoopIteration( *this ) )
124 int exitcode
= m_impl
->GetExitCode();
131 void wxEventLoop::Exit(int rc
)
133 wxCHECK_RET( IsRunning(), _T("can't call Exit() if not running") );
135 m_impl
->SetExitCode(rc
);
136 m_impl
->SetKeepGoing( false );
141 // ----------------------------------------------------------------------------
142 // wxEventLoop message processing dispatching
143 // ----------------------------------------------------------------------------
145 bool wxEventLoop::Pending() const
147 return XtAppPending( (XtAppContext
)wxTheApp
->GetAppContext() ) != 0;
150 bool wxEventLoop::Dispatch()
153 XtAppContext context
= (XtAppContext
)wxTheApp
->GetAppContext();
155 if( XtAppPeekEvent( context
, &event
) != 0 )
157 XtAppNextEvent( context
, &event
);
158 ProcessXEvent( &event
);
162 XtAppProcessEvent( context
, XtIMTimer
| XtIMAlternateInput
169 return m_impl
? m_impl
->GetKeepGoing() : true;
172 // ----------------------------------------------------------------------------
173 // Static functions (originally in app.cpp)
174 // ----------------------------------------------------------------------------
176 void ProcessXEvent(XEvent
* event
)
178 if (event
->type
== KeyPress
)
180 if (CheckForAccelerator(event
))
182 // Do nothing! We intercepted and processed the event as an
186 // It seemed before that this hack was redundant and
187 // key down events were being generated by wxCanvasInputEvent.
188 // But no longer - why ???
190 else if (CheckForKeyDown(event
))
192 // We intercepted and processed the key down event
197 XtDispatchEvent(event
);
201 else if (event
->type
== KeyRelease
)
203 // TODO: work out why we still need this ! -michael
205 if (::CheckForKeyUp(event
))
207 // We intercepted and processed the key up event
212 XtDispatchEvent(event
);
216 else if (event
->type
== PropertyNotify
)
218 wxTheApp
->HandlePropertyChange(event
);
221 else if (event
->type
== ResizeRequest
)
223 /* Terry Gitnick <terryg@scientech.com> - 1/21/98
224 * If resize event, don't resize until the last resize event for this
225 * window is recieved. Prevents flicker as windows are resized.
228 Display
*disp
= event
->xany
.display
;
229 Window win
= event
->xany
.window
;
234 while( XCheckTypedWindowEvent (disp
, win
, ResizeRequest
, &report
));
236 // TODO: when implementing refresh optimization, we can use
237 // XtAddExposureToRegion to expand the window's paint region.
239 XtDispatchEvent(event
);
243 XtDispatchEvent(event
);
247 // Returns true if an accelerator has been processed
248 bool CheckForAccelerator(XEvent
* event
)
250 if (event
->xany
.type
== KeyPress
)
252 // Find a wxWindow for this window
253 // TODO: should get display for the window, not the current display
254 Widget widget
= XtWindowToWidget(event
->xany
.display
,
256 wxWindow
* win
= NULL
;
258 // Find the first wxWindow that corresponds to this event window
259 while (widget
&& ((win
= wxGetWindowFromTable(widget
))!=NULL
))
260 widget
= XtParent(widget
);
265 wxKeyEvent
keyEvent(wxEVT_CHAR
);
266 wxTranslateKeyEvent(keyEvent
, win
, (Widget
) 0, event
);
268 // Now we have a wxKeyEvent and we have a wxWindow.
269 // Go up the hierarchy until we find a matching accelerator,
270 // or we get to the top.
273 if (win
->ProcessAccelerator(keyEvent
))
275 win
= win
->GetParent();
282 // bool wxApp::CheckForKeyDown(WXEvent* event) { wxFAIL; return false; }
284 bool CheckForKeyDown(XEvent
* event
)
286 if (event
->xany
.type
== KeyPress
)
288 Widget widget
= XtWindowToWidget(event
->xany
.display
,
290 wxWindow
* win
= NULL
;
292 // Find the first wxWindow that corresponds to this event window
293 while (widget
&& ((win
= wxGetWindowFromTable(widget
))!=NULL
))
294 widget
= XtParent(widget
);
299 wxKeyEvent
keyEvent(wxEVT_KEY_DOWN
);
300 wxTranslateKeyEvent(keyEvent
, win
, (Widget
) 0, event
);
302 return win
->GetEventHandler()->ProcessEvent( keyEvent
);
308 // bool wxApp::CheckForKeyUp(WXEvent* event) { wxFAIL; return false; }
310 bool CheckForKeyUp(XEvent
* event
)
312 if (event
->xany
.type
== KeyRelease
)
314 Widget widget
= XtWindowToWidget(event
->xany
.display
,
316 wxWindow
* win
= NULL
;
318 // Find the first wxWindow that corresponds to this event window
319 while (widget
&& ((win
= wxGetWindowFromTable(widget
))!=NULL
))
320 widget
= XtParent(widget
);
325 wxKeyEvent
keyEvent(wxEVT_KEY_UP
);
326 wxTranslateKeyEvent(keyEvent
, win
, (Widget
) 0, event
);
328 return win
->GetEventHandler()->ProcessEvent( keyEvent
);
334 // ----------------------------------------------------------------------------
335 // executes one main loop iteration (declared in include/wx/motif/private.h)
336 // ----------------------------------------------------------------------------
338 bool wxDoEventLoopIteration( wxEventLoop
& evtLoop
)
340 bool moreRequested
, pendingEvents
;
344 pendingEvents
= evtLoop
.Pending();
345 if( pendingEvents
) break;
346 moreRequested
= ::SendIdleMessage();
347 if( !moreRequested
) break;
351 if( !pendingEvents
&& !moreRequested
)
353 // leave the main loop to give other threads a chance to
354 // perform their GUI work
361 if( !evtLoop
.Dispatch() )
367 // ----------------------------------------------------------------------------
368 // ::wxWakeUpIdle implementation
369 // ----------------------------------------------------------------------------
371 // As per Vadim's suggestion, we open a pipe, and XtAppAddInputSource it;
372 // writing a single byte to the pipe will cause wxEventLoop::Pending
373 // to return true, and wxEventLoop::Dispatch to dispatch an input handler
374 // that simply removes the byte(s), and to return, which will cause
375 // an idle event to be sent
377 // also wxEventLoop::Exit is implemented that way, so that exiting an
378 // event loop won't require an event being in the queue
380 #include "wx/module.h"
382 #include <sys/types.h>
383 #include <sys/time.h>
386 static int idleFds
[2] = { -1, -1 };
388 class wxIdlePipeModule
: public wxModule
391 wxIdlePipeModule() {};
393 virtual bool OnInit()
395 // Must be done before modules are initialized
397 if( pipe(idleFds
) != 0 )
403 virtual void OnExit()
409 DECLARE_DYNAMIC_CLASS(wxIdlePipeModule
)
412 IMPLEMENT_DYNAMIC_CLASS(wxIdlePipeModule
, wxModule
)
414 static void wxInputCallback( XtPointer
, int* fd
, XtInputId
* )
418 // wxWakeUpIdle may have been called more than once
422 struct timeval timeout
;
428 wxFD_SET( *fd
, &in
);
430 if( select( *fd
+ 1, &in
, NULL
, NULL
, &timeout
) <= 0 )
432 if( read( *fd
, buffer
, sizeof(buffer
) - 1 ) != sizeof(buffer
) - 1 )
437 static void wxBreakDispatch()
439 char dummy
= 0; // for valgrind
441 // check if wxWakeUpIdle has already been called
443 struct timeval timeout
;
449 wxFD_SET( idleFds
[0], &in
);
451 if( select( idleFds
[0] + 1, &in
, NULL
, NULL
, &timeout
) > 0 )
454 // write a single byte
455 write( idleFds
[1], &dummy
, 1 );
458 void wxApp::WakeUpIdle()
465 if( pipe(idleFds
) != 0 )
470 bool wxAddIdleCallback()
472 if (!wxInitIdleFds())
475 // install input handler for wxWakeUpIdle
476 XtAppAddInput((XtAppContext
) wxTheApp
->GetAppContext(),
478 (XtPointer
)XtInputReadMask
,