Cleanly separate GUI socket-related code from net library.
[wxWidgets.git] / src / unix / fdiounix.cpp
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: wxhead.cpp,v 1.10 2009-06-29 10:23:04 zeitlin Exp $
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 #include "wx/apptrait.h"
27 #include "wx/private/fdiodispatcher.h"
28 #include "wx/unix/private/fdiounix.h"
29
30 // ============================================================================
31 // wxFDIOManagerUnix implementation
32 // ============================================================================
33
34 int wxFDIOManagerUnix::AddInput(wxFDIOHandler *handler, int fd, Direction d)
35 {
36 wxFDIODispatcher * const dispatcher = wxFDIODispatcher::Get();
37 wxCHECK_MSG( dispatcher, -1, "can't monitor FDs without FD IO dispatcher" );
38
39 // translate our direction to dispatcher flags
40 const int flag = d == INPUT ? wxFDIO_INPUT : wxFDIO_OUTPUT;
41
42 // we need to either register this FD with the dispatcher or update an
43 // existing registration depending on whether it had been previously
44 // registered for anything or not
45 bool ok;
46 const int regmask = handler->GetRegisteredEvents();
47 if ( !regmask )
48 {
49 ok = dispatcher->RegisterFD(fd, handler, flag);
50 }
51 else
52 {
53 ok = dispatcher->ModifyFD(fd, handler, regmask | flag);
54 }
55
56 if ( !ok )
57 return -1;
58
59 // update the stored mask of registered events
60 handler->SetRegisteredEvent(flag);
61
62 return fd;
63 }
64
65 void wxFDIOManagerUnix::RemoveInput(wxFDIOHandler *handler, int fd, Direction d)
66 {
67 wxFDIODispatcher * const dispatcher = wxFDIODispatcher::Get();
68 if ( !dispatcher )
69 return;
70
71 const int flag = d == INPUT ? wxFDIO_INPUT : wxFDIO_OUTPUT;
72
73 // just as in AddInput() above we may need to either just modify the FD or
74 // remove it completely if we don't need to monitor it any more
75 bool ok;
76 const int regmask = handler->GetRegisteredEvents();
77 if ( regmask == flag )
78 {
79 ok = dispatcher->UnregisterFD(fd);
80 }
81 else
82 {
83 ok = dispatcher->ModifyFD(fd, handler, regmask & ~flag);
84 }
85
86 if ( !ok )
87 {
88 wxLogDebug("Failed to unregister %d in direction %d", fd, d);
89 }
90
91 // do this even after a failure to unregister it, we still tried...
92 handler->ClearRegisteredEvent(flag);
93 }
94
95 wxFDIOManager *wxAppTraits::GetFDIOManager()
96 {
97 static wxFDIOManagerUnix s_manager;
98 return &s_manager;
99 }
100