]> git.saurik.com Git - wxWidgets.git/blob - src/motif/evtloop.cpp
No changes, just use wxRecursionGuard instead of manual boolean flag.
[wxWidgets.git] / src / motif / evtloop.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/motif/evtloop.cpp
3 // Purpose: implements wxEventLoop for Motif
4 // Author: Mattia Barbon
5 // Modified by:
6 // Created: 01.11.02
7 // Copyright: (c) 2002 Mattia Barbon
8 // Licence: wxWindows licence
9 ///////////////////////////////////////////////////////////////////////////////
10
11 // ============================================================================
12 // declarations
13 // ============================================================================
14
15 // ----------------------------------------------------------------------------
16 // headers
17 // ----------------------------------------------------------------------------
18
19 // For compilers that support precompilation, includes "wx.h".
20 #include "wx/wxprec.h"
21
22 #ifndef WX_PRECOMP
23 #include "wx/event.h"
24 #include "wx/app.h"
25 #include "wx/window.h"
26 #include "wx/module.h"
27 #endif //WX_PRECOMP
28
29 #include "wx/evtloop.h"
30 #include "wx/thread.h"
31
32 #ifdef __VMS__
33 #pragma message disable nosimpint
34 #endif
35 #include <Xm/Xm.h>
36 #include <X11/Xlib.h>
37 #ifdef __VMS__
38 #pragma message enable nosimpint
39 #endif
40
41 #include "wx/unix/private.h"
42 #include "wx/motif/private.h"
43
44 #ifdef HAVE_SYS_SELECT_H
45 # include <sys/select.h>
46 #endif
47
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();
53
54 // ----------------------------------------------------------------------------
55 // wxEventLoopImpl
56 // ----------------------------------------------------------------------------
57
58 class WXDLLEXPORT wxEventLoopImpl
59 {
60 public:
61 // ctor
62 wxEventLoopImpl() { SetExitCode(0); }
63
64 // set/get the exit code
65 void SetExitCode(int exitcode) { m_exitcode = exitcode; }
66 int GetExitCode() const { return m_exitcode; }
67
68 bool SendIdleMessage();
69 bool GetKeepGoing() const { return m_keepGoing; }
70 void SetKeepGoing(bool keepGoing) { m_keepGoing = keepGoing; }
71 private:
72 // the exit code of the event loop
73 int m_exitcode;
74 bool m_keepGoing;
75 };
76
77 // ----------------------------------------------------------------------------
78 // wxEventLoopImpl idle event processing
79 // ----------------------------------------------------------------------------
80
81 static bool SendIdleMessage()
82 {
83 return wxTheApp->ProcessIdle();
84 }
85
86 bool wxEventLoopImpl::SendIdleMessage()
87 {
88 return ::SendIdleMessage();
89 }
90
91 // ============================================================================
92 // wxEventLoop implementation
93 // ============================================================================
94
95 // ----------------------------------------------------------------------------
96 // wxEventLoop running and exiting
97 // ----------------------------------------------------------------------------
98
99 wxGUIEventLoop::~wxGUIEventLoop()
100 {
101 wxASSERT_MSG( !m_impl, wxT("should have been deleted in Run()") );
102 }
103
104 int wxGUIEventLoop::DoRun()
105 {
106 m_impl = new wxEventLoopImpl;
107 m_impl->SetKeepGoing( true );
108
109 for( ;; )
110 {
111 if( !wxDoEventLoopIteration( *this ) )
112 break;
113 }
114
115 OnExit();
116
117 int exitcode = m_impl->GetExitCode();
118 wxDELETE(m_impl);
119
120 return exitcode;
121 }
122
123 void wxGUIEventLoop::ScheduleExit(int rc)
124 {
125 wxCHECK_RET( IsInsideRun(), wxT("can't call ScheduleExit() if not started") );
126
127 m_impl->SetExitCode(rc);
128 m_impl->SetKeepGoing( false );
129
130 ::wxBreakDispatch();
131 }
132
133 bool wxGUIEventLoop::YieldFor(long eventsToProcess)
134 {
135 m_isInsideYield = true;
136 m_eventsToProcessInsideYield = eventsToProcess;
137
138 while (wxTheApp && wxTheApp->Pending())
139 // TODO: implement event filtering using the eventsToProcess mask
140 wxTheApp->Dispatch();
141
142 m_isInsideYield = false;
143
144 return true;
145 }
146
147 // ----------------------------------------------------------------------------
148 // wxEventLoop message processing dispatching
149 // ----------------------------------------------------------------------------
150
151 bool wxGUIEventLoop::Pending() const
152 {
153 return XtAppPending( (XtAppContext)wxTheApp->GetAppContext() ) != 0;
154 }
155
156 bool wxGUIEventLoop::Dispatch()
157 {
158 XEvent event;
159 XtAppContext context = (XtAppContext)wxTheApp->GetAppContext();
160
161 if( XtAppPeekEvent( context, &event ) != 0 )
162 {
163 XtAppNextEvent( context, &event );
164 ProcessXEvent( &event );
165 }
166 else
167 {
168 XtAppProcessEvent( context, XtIMTimer | XtIMAlternateInput
169 #ifdef XtIMSignal
170 | XtIMSignal
171 #endif
172 );
173 }
174
175 return m_impl ? m_impl->GetKeepGoing() : true;
176 }
177
178 // ----------------------------------------------------------------------------
179 // Static functions (originally in app.cpp)
180 // ----------------------------------------------------------------------------
181
182 void ProcessXEvent(XEvent* event)
183 {
184 if (event->type == KeyPress)
185 {
186 if (CheckForAccelerator(event))
187 {
188 // Do nothing! We intercepted and processed the event as an
189 // accelerator.
190 return;
191 }
192 // It seemed before that this hack was redundant and
193 // key down events were being generated by wxCanvasInputEvent.
194 // But no longer - why ???
195 //
196 else if (CheckForKeyDown(event))
197 {
198 // We intercepted and processed the key down event
199 return;
200 }
201 else
202 {
203 XtDispatchEvent(event);
204 return;
205 }
206 }
207 else if (event->type == KeyRelease)
208 {
209 // TODO: work out why we still need this ! -michael
210 //
211 if (::CheckForKeyUp(event))
212 {
213 // We intercepted and processed the key up event
214 return;
215 }
216 else
217 {
218 XtDispatchEvent(event);
219 return;
220 }
221 }
222 else if (event->type == PropertyNotify)
223 {
224 wxTheApp->HandlePropertyChange(event);
225 return;
226 }
227 else if (event->type == ResizeRequest)
228 {
229 /* Terry Gitnick <terryg@scientech.com> - 1/21/98
230 * If resize event, don't resize until the last resize event for this
231 * window is recieved. Prevents flicker as windows are resized.
232 */
233
234 Display *disp = event->xany.display;
235 Window win = event->xany.window;
236 XEvent report;
237
238 // to avoid flicker
239 report = * event;
240 while( XCheckTypedWindowEvent (disp, win, ResizeRequest, &report));
241
242 // TODO: when implementing refresh optimization, we can use
243 // XtAddExposureToRegion to expand the window's paint region.
244
245 XtDispatchEvent(event);
246 }
247 else
248 {
249 XtDispatchEvent(event);
250 }
251 }
252
253 // Returns true if an accelerator has been processed
254 bool CheckForAccelerator(XEvent* event)
255 {
256 if (event->xany.type == KeyPress)
257 {
258 // Find a wxWindow for this window
259 // TODO: should get display for the window, not the current display
260 Widget widget = XtWindowToWidget(event->xany.display,
261 event->xany.window);
262 wxWindow* win = NULL;
263
264 // Find the first wxWindow that corresponds to this event window
265 while (widget && ((win = wxGetWindowFromTable(widget))!=NULL))
266 widget = XtParent(widget);
267
268 if (!widget || !win)
269 return false;
270
271 wxKeyEvent keyEvent(wxEVT_CHAR);
272 wxTranslateKeyEvent(keyEvent, win, (Widget) 0, event);
273
274 // Now we have a wxKeyEvent and we have a wxWindow.
275 // Go up the hierarchy until we find a matching accelerator,
276 // or we get to the top.
277 while (win)
278 {
279 if (win->ProcessAccelerator(keyEvent))
280 return true;
281 win = win->GetParent();
282 }
283 return false;
284 }
285 return false;
286 }
287
288 // bool wxApp::CheckForKeyDown(WXEvent* event) { wxFAIL; return false; }
289
290 bool CheckForKeyDown(XEvent* event)
291 {
292 if (event->xany.type == KeyPress)
293 {
294 Widget widget = XtWindowToWidget(event->xany.display,
295 event->xany.window);
296 wxWindow* win = NULL;
297
298 // Find the first wxWindow that corresponds to this event window
299 while (widget && ((win = wxGetWindowFromTable(widget))!=NULL))
300 widget = XtParent(widget);
301
302 if (!widget || !win)
303 return false;
304
305 wxKeyEvent keyEvent(wxEVT_KEY_DOWN);
306 wxTranslateKeyEvent(keyEvent, win, (Widget) 0, event);
307
308 return win->HandleWindowEvent( keyEvent );
309 }
310
311 return false;
312 }
313
314 // bool wxApp::CheckForKeyUp(WXEvent* event) { wxFAIL; return false; }
315
316 bool CheckForKeyUp(XEvent* event)
317 {
318 if (event->xany.type == KeyRelease)
319 {
320 Widget widget = XtWindowToWidget(event->xany.display,
321 event->xany.window);
322 wxWindow* win = NULL;
323
324 // Find the first wxWindow that corresponds to this event window
325 while (widget && ((win = wxGetWindowFromTable(widget))!=NULL))
326 widget = XtParent(widget);
327
328 if (!widget || !win)
329 return false;
330
331 wxKeyEvent keyEvent(wxEVT_KEY_UP);
332 wxTranslateKeyEvent(keyEvent, win, (Widget) 0, event);
333
334 return win->HandleWindowEvent( keyEvent );
335 }
336
337 return false;
338 }
339
340 // ----------------------------------------------------------------------------
341 // executes one main loop iteration (declared in include/wx/motif/private.h)
342 // ----------------------------------------------------------------------------
343
344 bool wxDoEventLoopIteration( wxGUIEventLoop& evtLoop )
345 {
346 bool moreRequested, pendingEvents;
347
348 for(;;)
349 {
350 pendingEvents = evtLoop.Pending();
351 if( pendingEvents ) break;
352 moreRequested = ::SendIdleMessage();
353 if( !moreRequested ) break;
354 }
355
356 #if wxUSE_THREADS
357 if( !pendingEvents && !moreRequested )
358 {
359 // leave the main loop to give other threads a chance to
360 // perform their GUI work
361 wxMutexGuiLeave();
362 wxMilliSleep(20);
363 wxMutexGuiEnter();
364 }
365 #endif
366
367 if( !evtLoop.Dispatch() )
368 return false;
369
370 return true;
371 }
372
373 // ----------------------------------------------------------------------------
374 // ::wxWakeUpIdle implementation
375 // ----------------------------------------------------------------------------
376
377 // As per Vadim's suggestion, we open a pipe, and XtAppAddInputSource it;
378 // writing a single byte to the pipe will cause wxEventLoop::Pending
379 // to return true, and wxEventLoop::Dispatch to dispatch an input handler
380 // that simply removes the byte(s), and to return, which will cause
381 // an idle event to be sent
382
383 // also wxEventLoop::Exit is implemented that way, so that exiting an
384 // event loop won't require an event being in the queue
385
386 #include <sys/types.h>
387 #include <sys/time.h>
388 #include <unistd.h>
389
390 static int idleFds[2] = { -1, -1 };
391
392 class wxIdlePipeModule : public wxModule
393 {
394 public:
395 wxIdlePipeModule() {};
396
397 virtual bool OnInit()
398 {
399 // Must be done before modules are initialized
400 #if 0
401 if( pipe(idleFds) != 0 )
402 return false;
403 #endif
404 return true;
405 }
406
407 virtual void OnExit()
408 {
409 close( idleFds[0] );
410 close( idleFds[1] );
411 }
412 private:
413 DECLARE_DYNAMIC_CLASS(wxIdlePipeModule)
414 };
415
416 IMPLEMENT_DYNAMIC_CLASS(wxIdlePipeModule, wxModule)
417
418 static void wxInputCallback( XtPointer, int* fd, XtInputId* )
419 {
420 char buffer[128];
421
422 // wxWakeUpIdle may have been called more than once
423 for(;;)
424 {
425 fd_set in;
426 struct timeval timeout;
427
428 timeout.tv_sec = 0;
429 timeout.tv_usec = 0;
430
431 wxFD_ZERO( &in );
432 wxFD_SET( *fd, &in );
433
434 if( select( *fd + 1, &in, NULL, NULL, &timeout ) <= 0 )
435 break;
436 if( read( *fd, buffer, sizeof(buffer) - 1 ) != sizeof(buffer) - 1 )
437 break;
438 }
439 }
440
441 static void wxBreakDispatch()
442 {
443 char dummy = 0; // for valgrind
444
445 // check if wxWakeUpIdle has already been called
446 fd_set in;
447 struct timeval timeout;
448
449 timeout.tv_sec = 0;
450 timeout.tv_usec = 0;
451
452 wxFD_ZERO( &in );
453 wxFD_SET( idleFds[0], &in );
454
455 if( select( idleFds[0] + 1, &in, NULL, NULL, &timeout ) > 0 )
456 return;
457
458 // write a single byte
459 write( idleFds[1], &dummy, 1 );
460 }
461
462 void wxApp::WakeUpIdle()
463 {
464 ::wxBreakDispatch();
465 }
466
467 bool wxInitIdleFds()
468 {
469 if( pipe(idleFds) != 0 )
470 return false;
471 return true;
472 }
473
474 bool wxAddIdleCallback()
475 {
476 if (!wxInitIdleFds())
477 return false;
478
479 // install input handler for wxWakeUpIdle
480 XtAppAddInput((XtAppContext) wxTheApp->GetAppContext(),
481 idleFds[0],
482 (XtPointer)XtInputReadMask,
483 wxInputCallback,
484 NULL);
485
486 return true;
487 }