| 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 | // License: 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, _T("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, _T("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 | delete m_impl; |
| 125 | m_impl = NULL; |
| 126 | |
| 127 | return exitcode; |
| 128 | } |
| 129 | |
| 130 | void wxGUIEventLoop::Exit(int rc) |
| 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 | |
| 144 | bool wxGUIEventLoop::Pending() const |
| 145 | { |
| 146 | return XtAppPending( (XtAppContext)wxTheApp->GetAppContext() ) != 0; |
| 147 | } |
| 148 | |
| 149 | bool wxGUIEventLoop::Dispatch() |
| 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 |
| 160 | { |
| 161 | XtAppProcessEvent( context, XtIMTimer | XtIMAlternateInput |
| 162 | #ifdef XtIMSignal |
| 163 | | XtIMSignal |
| 164 | #endif |
| 165 | ); |
| 166 | } |
| 167 | |
| 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 | |
| 227 | Display *disp = event->xany.display; |
| 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 |
| 253 | Widget widget = XtWindowToWidget(event->xany.display, |
| 254 | event->xany.window); |
| 255 | wxWindow* win = NULL; |
| 256 | |
| 257 | // Find the first wxWindow that corresponds to this event window |
| 258 | while (widget && ((win = wxGetWindowFromTable(widget))!=NULL)) |
| 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 | { |
| 287 | Widget widget = XtWindowToWidget(event->xany.display, |
| 288 | event->xany.window); |
| 289 | wxWindow* win = NULL; |
| 290 | |
| 291 | // Find the first wxWindow that corresponds to this event window |
| 292 | while (widget && ((win = wxGetWindowFromTable(widget))!=NULL)) |
| 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 | |
| 301 | return win->HandleWindowEvent( keyEvent ); |
| 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 | { |
| 313 | Widget widget = XtWindowToWidget(event->xany.display, |
| 314 | event->xany.window); |
| 315 | wxWindow* win = NULL; |
| 316 | |
| 317 | // Find the first wxWindow that corresponds to this event window |
| 318 | while (widget && ((win = wxGetWindowFromTable(widget))!=NULL)) |
| 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 | |
| 327 | return win->HandleWindowEvent( keyEvent ); |
| 328 | } |
| 329 | |
| 330 | return false; |
| 331 | } |
| 332 | |
| 333 | // ---------------------------------------------------------------------------- |
| 334 | // executes one main loop iteration (declared in include/wx/motif/private.h) |
| 335 | // ---------------------------------------------------------------------------- |
| 336 | |
| 337 | bool wxDoEventLoopIteration( wxGUIEventLoop& evtLoop ) |
| 338 | { |
| 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 | |
| 349 | #if wxUSE_THREADS |
| 350 | if( !pendingEvents && !moreRequested ) |
| 351 | { |
| 352 | // leave the main loop to give other threads a chance to |
| 353 | // perform their GUI work |
| 354 | wxMutexGuiLeave(); |
| 355 | wxMilliSleep(20); |
| 356 | wxMutexGuiEnter(); |
| 357 | } |
| 358 | #endif |
| 359 | |
| 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 | |
| 379 | #include <sys/types.h> |
| 380 | #include <sys/time.h> |
| 381 | #include <unistd.h> |
| 382 | |
| 383 | static int idleFds[2] = { -1, -1 }; |
| 384 | |
| 385 | class wxIdlePipeModule : public wxModule |
| 386 | { |
| 387 | public: |
| 388 | wxIdlePipeModule() {}; |
| 389 | |
| 390 | virtual bool OnInit() |
| 391 | { |
| 392 | // Must be done before modules are initialized |
| 393 | #if 0 |
| 394 | if( pipe(idleFds) != 0 ) |
| 395 | return false; |
| 396 | #endif |
| 397 | return true; |
| 398 | } |
| 399 | |
| 400 | virtual void OnExit() |
| 401 | { |
| 402 | close( idleFds[0] ); |
| 403 | close( idleFds[1] ); |
| 404 | } |
| 405 | private: |
| 406 | DECLARE_DYNAMIC_CLASS(wxIdlePipeModule) |
| 407 | }; |
| 408 | |
| 409 | IMPLEMENT_DYNAMIC_CLASS(wxIdlePipeModule, wxModule) |
| 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 | |
| 424 | wxFD_ZERO( &in ); |
| 425 | wxFD_SET( *fd, &in ); |
| 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 | { |
| 436 | char dummy = 0; // for valgrind |
| 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 | |
| 445 | wxFD_ZERO( &in ); |
| 446 | wxFD_SET( idleFds[0], &in ); |
| 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 | |
| 455 | void wxApp::WakeUpIdle() |
| 456 | { |
| 457 | ::wxBreakDispatch(); |
| 458 | } |
| 459 | |
| 460 | bool wxInitIdleFds() |
| 461 | { |
| 462 | if( pipe(idleFds) != 0 ) |
| 463 | return false; |
| 464 | return true; |
| 465 | } |
| 466 | |
| 467 | bool wxAddIdleCallback() |
| 468 | { |
| 469 | if (!wxInitIdleFds()) |
| 470 | return false; |
| 471 | |
| 472 | // install input handler for wxWakeUpIdle |
| 473 | XtAppAddInput((XtAppContext) wxTheApp->GetAppContext(), |
| 474 | idleFds[0], |
| 475 | (XtPointer)XtInputReadMask, |
| 476 | wxInputCallback, |
| 477 | NULL); |
| 478 | |
| 479 | return true; |
| 480 | } |