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