1 ///////////////////////////////////////////////////////////////////////////////
2 // Name:        src/gtk/sockgtk.cpp
3 // Purpose:     implementation of wxGTK-specific socket event handling
4 // Author:      Guilhem Lavaux, Vadim Zeitlin
5 // Created:     1999
6 // Copyright:   (c) 1999, 2007 wxWidgets dev team
7 //              (c) 2009 Vadim Zeitlin
8 // Licence:     wxWindows licence
9 ///////////////////////////////////////////////////////////////////////////////
10 
11 // For compilers that support precompilation, includes "wx.h".
12 #include "wx/wxprec.h"
13 
14 #if wxUSE_SOCKETS && defined(__UNIX__)
15 
16 #include "wx/apptrait.h"
17 #include "wx/private/fdiomanager.h"
18 
19 #include <glib.h>
20 
21 extern "C" {
wxSocket_Input(GIOChannel *,GIOCondition condition,gpointer data)22 static gboolean wxSocket_Input(GIOChannel*, GIOCondition condition, gpointer data)
23 {
24     wxFDIOHandler * const handler = static_cast<wxFDIOHandler *>(data);
25 
26     if (condition & G_IO_IN)
27     {
28         handler->OnReadWaiting();
29 
30         // we could have lost connection while reading in which case we
31         // shouldn't call OnWriteWaiting() as the socket is now closed and it
32         // would assert
33         if ( !handler->IsOk() )
34             return true;
35     }
36 
37     if (condition & G_IO_OUT)
38         handler->OnWriteWaiting();
39 
40     return true;
41 }
42 }
43 
44 class GTKFDIOManager : public wxFDIOManager
45 {
46 public:
AddInput(wxFDIOHandler * handler,int fd,Direction d)47     virtual int AddInput(wxFDIOHandler *handler, int fd, Direction d) wxOVERRIDE
48     {
49         GIOChannel* channel = g_io_channel_unix_new(fd);
50         unsigned id = g_io_add_watch(
51             channel,
52             d == OUTPUT ? G_IO_OUT : G_IO_IN,
53             wxSocket_Input,
54             handler);
55         g_io_channel_unref(channel);
56         return id;
57     }
58 
59     virtual void
RemoveInput(wxFDIOHandler * WXUNUSED (handler),int fd,Direction WXUNUSED (d))60     RemoveInput(wxFDIOHandler* WXUNUSED(handler), int fd, Direction WXUNUSED(d)) wxOVERRIDE
61     {
62         g_source_remove(fd);
63     }
64 };
65 
GetFDIOManager()66 wxFDIOManager *wxGUIAppTraits::GetFDIOManager()
67 {
68     static GTKFDIOManager s_manager;
69     return &s_manager;
70 }
71 
72 #endif // wxUSE_SOCKETS && __UNIX__
73