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