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