| 1 | ///////////////////////////////////////////////////////////////////////////// |
| 2 | // Name: src/unix/evtloopunix.cpp |
| 3 | // Purpose: wxEventLoop implementation |
| 4 | // Author: Lukasz Michalski (lm@zork.pl) |
| 5 | // Created: 2007-05-07 |
| 6 | // RCS-ID: $Id$ |
| 7 | // Copyright: (c) 2006 Zork Lukasz Michalski |
| 8 | // Licence: wxWindows licence |
| 9 | ///////////////////////////////////////////////////////////////////////////// |
| 10 | |
| 11 | // =========================================================================== |
| 12 | // declarations |
| 13 | // =========================================================================== |
| 14 | |
| 15 | // --------------------------------------------------------------------------- |
| 16 | // headers |
| 17 | // --------------------------------------------------------------------------- |
| 18 | |
| 19 | // For compilers that support precompilation, includes "wx.h". |
| 20 | #include "wx/wxprec.h" |
| 21 | |
| 22 | #include "wx/evtloop.h" |
| 23 | |
| 24 | #ifndef WX_PRECOMP |
| 25 | #include "wx/app.h" |
| 26 | #include "wx/log.h" |
| 27 | #endif |
| 28 | |
| 29 | #include <errno.h> |
| 30 | #include "wx/thread.h" |
| 31 | #include "wx/module.h" |
| 32 | #include "wx/generic/private/timer.h" |
| 33 | #include "wx/unix/private/epolldispatcher.h" |
| 34 | #include "wx/private/selectdispatcher.h" |
| 35 | |
| 36 | #define TRACE_EVENTS _T("events") |
| 37 | |
| 38 | //this code should not be compiled when GUI is defined |
| 39 | //(monolithic build issue) |
| 40 | #if !wxUSE_GUI |
| 41 | |
| 42 | // =========================================================================== |
| 43 | // wxEventLoop::PipeIOHandler implementation |
| 44 | // =========================================================================== |
| 45 | |
| 46 | // ---------------------------------------------------------------------------- |
| 47 | // initialization |
| 48 | // ---------------------------------------------------------------------------- |
| 49 | |
| 50 | bool wxConsoleEventLoop::PipeIOHandler::Create() |
| 51 | { |
| 52 | if ( !m_pipe.Create() ) |
| 53 | { |
| 54 | wxLogError(_("Failed to create wake up pipe used by event loop.")); |
| 55 | return false; |
| 56 | } |
| 57 | |
| 58 | const int fdRead = GetReadFd(); |
| 59 | |
| 60 | int flags = fcntl(fdRead, F_GETFL, 0); |
| 61 | if ( flags == -1 || fcntl(fdRead, F_SETFL, flags | O_NONBLOCK) == -1 ) |
| 62 | { |
| 63 | wxLogSysError(_("Failed to switch wake up pipe to non-blocking mode")); |
| 64 | return false; |
| 65 | } |
| 66 | |
| 67 | wxLogTrace(TRACE_EVENTS, wxT("Wake up pipe (%d, %d) created"), |
| 68 | fdRead, m_pipe[wxPipe::Write]); |
| 69 | |
| 70 | return true; |
| 71 | } |
| 72 | |
| 73 | // ---------------------------------------------------------------------------- |
| 74 | // wakeup handling |
| 75 | // ---------------------------------------------------------------------------- |
| 76 | |
| 77 | void wxConsoleEventLoop::PipeIOHandler::WakeUp() |
| 78 | { |
| 79 | if ( write(m_pipe[wxPipe::Write], "s", 1) != 1 ) |
| 80 | { |
| 81 | // don't use wxLog here, we can be in another thread and this could |
| 82 | // result in dead locks |
| 83 | perror("write(wake up pipe)"); |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | void wxConsoleEventLoop::PipeIOHandler::OnReadWaiting() |
| 88 | { |
| 89 | // got wakeup from child thread: read all data available in pipe just to |
| 90 | // make it empty (evevn though we write one byte at a time from WakeUp(), |
| 91 | // it could have been called several times) |
| 92 | char buf[4]; |
| 93 | for ( ;; ) |
| 94 | { |
| 95 | const int size = read(GetReadFd(), buf, WXSIZEOF(buf)); |
| 96 | |
| 97 | if ( size == 0 || (size == -1 && errno == EAGAIN) ) |
| 98 | { |
| 99 | // nothing left in the pipe (EAGAIN is expected for an FD with |
| 100 | // O_NONBLOCK) |
| 101 | break; |
| 102 | } |
| 103 | |
| 104 | if ( size == -1 ) |
| 105 | { |
| 106 | wxLogSysError(_("Failed to read from wake-up pipe")); |
| 107 | |
| 108 | break; |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | wxTheApp->ProcessPendingEvents(); |
| 113 | } |
| 114 | |
| 115 | // =========================================================================== |
| 116 | // wxEventLoop implementation |
| 117 | // =========================================================================== |
| 118 | |
| 119 | //----------------------------------------------------------------------------- |
| 120 | // initialization |
| 121 | //----------------------------------------------------------------------------- |
| 122 | |
| 123 | wxConsoleEventLoop::wxConsoleEventLoop() |
| 124 | { |
| 125 | if ( !m_wakeupPipe.Create() ) |
| 126 | { |
| 127 | m_dispatcher = NULL; |
| 128 | return; |
| 129 | } |
| 130 | |
| 131 | #ifdef HAVE_SYS_EPOLL_H |
| 132 | m_dispatcher = wxEpollDispatcher::Get(); |
| 133 | if ( !m_dispatcher ) |
| 134 | #endif // HAVE_SYS_EPOLL_H |
| 135 | { |
| 136 | m_dispatcher = wxSelectDispatcher::Get(); |
| 137 | } |
| 138 | |
| 139 | wxCHECK_RET( m_dispatcher, _T("failed to create IO dispatcher") ); |
| 140 | |
| 141 | m_dispatcher->RegisterFD |
| 142 | ( |
| 143 | m_wakeupPipe.GetReadFd(), |
| 144 | &m_wakeupPipe, |
| 145 | wxFDIO_INPUT |
| 146 | ); |
| 147 | }; |
| 148 | |
| 149 | //----------------------------------------------------------------------------- |
| 150 | // events dispatch and loop handling |
| 151 | //----------------------------------------------------------------------------- |
| 152 | |
| 153 | bool wxConsoleEventLoop::Pending() const |
| 154 | { |
| 155 | return wxTheApp->HasPendingEvents(); |
| 156 | } |
| 157 | |
| 158 | bool wxConsoleEventLoop::Dispatch() |
| 159 | { |
| 160 | wxTheApp->ProcessPendingEvents(); |
| 161 | return true; |
| 162 | } |
| 163 | |
| 164 | void wxConsoleEventLoop::WakeUp() |
| 165 | { |
| 166 | m_wakeupPipe.WakeUp(); |
| 167 | } |
| 168 | |
| 169 | void wxConsoleEventLoop::OnNextIteration() |
| 170 | { |
| 171 | // calculate the timeout until the next timer expiration |
| 172 | int timeout; |
| 173 | |
| 174 | #if wxUSE_TIMER |
| 175 | wxUsecClock_t nextTimer; |
| 176 | if ( wxTimerScheduler::Get().GetNext(&nextTimer) ) |
| 177 | { |
| 178 | // timeout is in ms |
| 179 | timeout = (nextTimer / 1000).ToLong(); |
| 180 | } |
| 181 | else // no timers, we can block forever |
| 182 | #endif // wxUSE_TIMER |
| 183 | { |
| 184 | timeout = wxFDIODispatcher::TIMEOUT_INFINITE; |
| 185 | } |
| 186 | |
| 187 | m_dispatcher->RunLoop(timeout); |
| 188 | |
| 189 | #if wxUSE_TIMER |
| 190 | wxTimerScheduler::Get().NotifyExpired(); |
| 191 | #endif |
| 192 | |
| 193 | // call the signal handlers for any signals we caught recently |
| 194 | wxTheApp->CheckSignal(); |
| 195 | } |
| 196 | |
| 197 | #endif // !wxUSE_GUI |