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