| 1 | /////////////////////////////////////////////////////////////////////////////// |
| 2 | // Name: src/unix/fdiounix.cpp |
| 3 | // Purpose: wxFDIOManager implementation for console Unix applications |
| 4 | // Author: Vadim Zeitlin |
| 5 | // Created: 2009-08-17 |
| 6 | // RCS-ID: $Id$ |
| 7 | // Copyright: (c) 2009 Vadim Zeitlin <vadim@wxwidgets.org> |
| 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 | #ifdef __BORLANDC__ |
| 23 | #pragma hdrstop |
| 24 | #endif |
| 25 | |
| 26 | #if wxUSE_SOCKETS |
| 27 | |
| 28 | #include "wx/apptrait.h" |
| 29 | #include "wx/log.h" |
| 30 | #include "wx/private/fdiodispatcher.h" |
| 31 | #include "wx/unix/private/fdiounix.h" |
| 32 | |
| 33 | // ============================================================================ |
| 34 | // wxFDIOManagerUnix implementation |
| 35 | // ============================================================================ |
| 36 | |
| 37 | int wxFDIOManagerUnix::AddInput(wxFDIOHandler *handler, int fd, Direction d) |
| 38 | { |
| 39 | wxFDIODispatcher * const dispatcher = wxFDIODispatcher::Get(); |
| 40 | wxCHECK_MSG( dispatcher, -1, "can't monitor FDs without FD IO dispatcher" ); |
| 41 | |
| 42 | // translate our direction to dispatcher flags |
| 43 | const int flag = d == INPUT ? wxFDIO_INPUT : wxFDIO_OUTPUT; |
| 44 | |
| 45 | // we need to either register this FD with the dispatcher or update an |
| 46 | // existing registration depending on whether it had been previously |
| 47 | // registered for anything or not |
| 48 | bool ok; |
| 49 | const int regmask = handler->GetRegisteredEvents(); |
| 50 | if ( !regmask ) |
| 51 | { |
| 52 | ok = dispatcher->RegisterFD(fd, handler, flag); |
| 53 | } |
| 54 | else |
| 55 | { |
| 56 | ok = dispatcher->ModifyFD(fd, handler, regmask | flag); |
| 57 | } |
| 58 | |
| 59 | if ( !ok ) |
| 60 | return -1; |
| 61 | |
| 62 | // update the stored mask of registered events |
| 63 | handler->SetRegisteredEvent(flag); |
| 64 | |
| 65 | return fd; |
| 66 | } |
| 67 | |
| 68 | void wxFDIOManagerUnix::RemoveInput(wxFDIOHandler *handler, int fd, Direction d) |
| 69 | { |
| 70 | wxFDIODispatcher * const dispatcher = wxFDIODispatcher::Get(); |
| 71 | if ( !dispatcher ) |
| 72 | return; |
| 73 | |
| 74 | const int flag = d == INPUT ? wxFDIO_INPUT : wxFDIO_OUTPUT; |
| 75 | |
| 76 | // just as in AddInput() above we may need to either just modify the FD or |
| 77 | // remove it completely if we don't need to monitor it any more |
| 78 | bool ok; |
| 79 | const int regmask = handler->GetRegisteredEvents(); |
| 80 | if ( regmask == flag ) |
| 81 | { |
| 82 | ok = dispatcher->UnregisterFD(fd); |
| 83 | } |
| 84 | else |
| 85 | { |
| 86 | ok = dispatcher->ModifyFD(fd, handler, regmask & ~flag); |
| 87 | } |
| 88 | |
| 89 | if ( !ok ) |
| 90 | { |
| 91 | wxLogDebug("Failed to unregister %d in direction %d", fd, d); |
| 92 | } |
| 93 | |
| 94 | // do this even after a failure to unregister it, we still tried... |
| 95 | handler->ClearRegisteredEvent(flag); |
| 96 | } |
| 97 | |
| 98 | wxFDIOManager *wxAppTraits::GetFDIOManager() |
| 99 | { |
| 100 | static wxFDIOManagerUnix s_manager; |
| 101 | return &s_manager; |
| 102 | } |
| 103 | |
| 104 | #endif // wxUSE_SOCKETS |