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