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