]> git.saurik.com Git - wxWidgets.git/blob - src/x11/gsockx11.cpp
clean-up and fix
[wxWidgets.git] / src / x11 / gsockx11.cpp
1 /* -------------------------------------------------------------------------
2 * Project: GSocket (Generic Socket) for WX
3 * Name: src/x11/gsockx11.cpp
4 * Purpose: GSocket: X11 part
5 * Licence: The wxWindows licence
6 * CVSID: $Id$
7 * ------------------------------------------------------------------------- */
8
9 // for compilers that support precompilation, includes "wx.h".
10 #include "wx/wxprec.h"
11
12 #if defined(__BORLANDC__)
13 #pragma hdrstop
14 #endif
15
16 #if wxUSE_SOCKETS
17
18 #include <stdlib.h>
19 #include "wx/gsocket.h"
20 #include "wx/unix/gsockunx.h"
21
22 /*
23 * FIXME: have these in a common header instead of being repeated
24 * in evtloop.cpp and gsockx11.c
25 */
26
27 typedef void (*wxSocketCallback) (int fd, void* data);
28
29 typedef enum
30 { wxSocketTableInput, wxSocketTableOutput } wxSocketTableType ;
31
32 extern "C" void wxRegisterSocketCallback(int fd, wxSocketTableType socketType, wxSocketCallback cback, void* data);
33 extern "C" void wxUnregisterSocketCallback(int fd, wxSocketTableType socketType);
34
35
36 static void _GSocket_X11_Input(int *fid, void* data)
37 {
38 GSocket *socket = (GSocket *)data;
39
40 socket->Detected_Read();
41 }
42
43 static void _GSocket_X11_Output(int *fid, void* data)
44 {
45 GSocket *socket = (GSocket *)data;
46
47 socket->Detected_Write();
48 }
49
50 bool GSocketGUIFunctionsTableConcrete::CanUseEventLoop()
51 { return true; }
52
53 bool GSocketGUIFunctionsTableConcrete::OnInit(void)
54 {
55 return 1;
56 }
57
58 void GSocketGUIFunctionsTableConcrete::OnExit(void)
59 {
60 }
61
62 bool GSocketGUIFunctionsTableConcrete::Init_Socket(GSocket *socket)
63 {
64 int *m_id;
65
66 socket->m_gui_dependent = (char *)malloc(sizeof(int)*2);
67 m_id = (int *)(socket->m_gui_dependent);
68
69 m_id[0] = -1;
70 m_id[1] = -1;
71
72 return true;
73 }
74
75 void GSocketGUIFunctionsTableConcrete::Destroy_Socket(GSocket *socket)
76 {
77 free(socket->m_gui_dependent);
78 }
79
80 void GSocketGUIFunctionsTableConcrete::Install_Callback(GSocket *socket, GSocketEvent event)
81 {
82 int *m_id = (int *)(socket->m_gui_dependent);
83 int c;
84
85 if (socket->m_fd == -1)
86 return;
87
88 switch (event)
89 {
90 case GSOCK_LOST: /* fall-through */
91 case GSOCK_INPUT: c = 0; break;
92 case GSOCK_OUTPUT: c = 1; break;
93 case GSOCK_CONNECTION: c = ((socket->m_server) ? 0 : 1); break;
94 default: return;
95 }
96
97 #if 0
98 if (m_id[c] != -1)
99 XtRemoveInput(m_id[c]);
100 #endif /* 0 */
101
102 if (c == 0)
103 {
104 m_id[0] = socket->m_fd;
105
106 wxRegisterSocketCallback(socket->m_fd, wxSocketTableInput,
107 (wxSocketCallback) _GSocket_X11_Input, (void*) socket);
108 }
109 else
110 {
111 m_id[1] = socket->m_fd;
112
113 wxRegisterSocketCallback(socket->m_fd, wxSocketTableOutput,
114 (wxSocketCallback) _GSocket_X11_Output, (void*) socket);
115 }
116 }
117
118 void GSocketGUIFunctionsTableConcrete::Uninstall_Callback(GSocket *socket, GSocketEvent event)
119 {
120 int *m_id = (int *)(socket->m_gui_dependent);
121 int c;
122
123 switch (event)
124 {
125 case GSOCK_LOST: /* fall-through */
126 case GSOCK_INPUT: c = 0; break;
127 case GSOCK_OUTPUT: c = 1; break;
128 case GSOCK_CONNECTION: c = ((socket->m_server) ? 0 : 1); break;
129 default: return;
130 }
131
132 if (m_id[c] != -1)
133 {
134 if (c == 0)
135 wxUnregisterSocketCallback(m_id[c], wxSocketTableInput);
136 else
137 wxUnregisterSocketCallback(m_id[c], wxSocketTableOutput);
138 }
139
140 m_id[c] = -1;
141 }
142
143 void GSocketGUIFunctionsTableConcrete::Enable_Events(GSocket *socket)
144 {
145 Install_Callback(socket, GSOCK_INPUT);
146 Install_Callback(socket, GSOCK_OUTPUT);
147 }
148
149 void GSocketGUIFunctionsTableConcrete::Disable_Events(GSocket *socket)
150 {
151 Uninstall_Callback(socket, GSOCK_INPUT);
152 Uninstall_Callback(socket, GSOCK_OUTPUT);
153 }
154
155 #else /* !wxUSE_SOCKETS */
156
157 /* some compilers don't like having empty source files */
158 static int wxDummyGsockVar = 0;
159
160 #endif /* wxUSE_SOCKETS/!wxUSE_SOCKETS */