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