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