]> git.saurik.com Git - wxWidgets.git/blob - src/mac/gsockosx.c
* _GSocket_Enable_Events() and _GSocket_Disable_Events() now call
[wxWidgets.git] / src / mac / gsockosx.c
1 /* -------------------------------------------------------------------------
2 * Project: GSocket (Generic Socket) for WX
3 * Name: gsockosx.c
4 * Purpose: GSocket: Mac OS X mach-o part
5 * CVSID: $Id$
6 * Mac code by Brian Victor, February 2002. Email comments to bhv1@psu.edu
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 #include <CoreFoundation/CoreFoundation.h>
18
19 #define ALL_CALLBACK_TYPES (kCFSocketReadCallBack | kCFSocketWriteCallBack | kCFSocketConnectCallBack)
20
21 struct MacGSocketData
22 {
23 CFSocketRef socket;
24 CFRunLoopSourceRef source;
25 };
26
27 void Mac_Socket_Callback(CFSocketRef s, CFSocketCallBackType callbackType,
28 CFDataRef address, const void* data, void* info)
29 {
30 GSocket* socket = (GSocket*)info;
31 struct MacGSocketData* macdata;
32 macdata = (struct MacGSocketData*)socket->m_gui_dependent;
33 if (!macdata) return;
34 switch (callbackType)
35 {
36 case kCFSocketConnectCallBack:
37 assert(!socket->m_server);
38 socket->m_functions->Detected_Write(socket);
39 break;
40 case kCFSocketReadCallBack:
41 socket->m_functions->Detected_Read(socket);
42 break;
43 case kCFSocketWriteCallBack:
44 socket->m_functions->Detected_Write(socket);
45 break;
46 default:
47 break; /* We shouldn't get here. */
48 }
49 }
50
51 struct MacGSocketData* _GSocket_Get_Mac_Socket(GSocket *socket)
52 {
53 /* If socket is already created, returns a pointer to the data */
54 /* Otherwise, creates socket and returns the pointer */
55 CFSocketContext cont;
56 struct MacGSocketData* data = (struct MacGSocketData*)socket->m_gui_dependent;
57
58 if (data && data->source) return data;
59
60 /* CFSocket has not been created, create it: */
61 if (socket->m_fd < 0 || !data) return NULL;
62 cont.version = 0; cont.retain = NULL;
63 cont.release = NULL; cont.copyDescription = NULL;
64 cont.info = socket;
65
66 CFSocketRef cf = CFSocketCreateWithNative(NULL, socket->m_fd,
67 ALL_CALLBACK_TYPES, Mac_Socket_Callback, &cont);
68 CFRunLoopSourceRef source = CFSocketCreateRunLoopSource(NULL, cf, 0);
69 assert(source);
70 /* Turn off kCFSocketCloseOnInvalidate (NOTE: > 10.2 only!) */
71 /* Another default flag that we don't turn on here is for DataCallBack and
72 also AcceptCallback (which overlap in bits) which we don't use anyway */
73 /* FIXME: For < 10.2 compatibility fix GSocket to call a platform-dependent
74 function to close the socket so that we can just call invalidate and
75 avoid having to set any special flags at all. */
76 CFSocketSetSocketFlags(cf, kCFSocketAutomaticallyReenableReadCallBack | kCFSocketAutomaticallyReenableWriteCallBack);
77 socket->m_gui_dependent = (char*)data;
78
79 /* Keep the source and the socket around. */
80 data->source = source;
81 data->socket = cf;
82
83 return data;
84 }
85
86 int _GSocket_GUI_Init(void)
87 {
88 return 1;
89 }
90
91 void _GSocket_GUI_Cleanup(void)
92 {
93 }
94
95 int _GSocket_GUI_Init_Socket(GSocket *socket)
96 {
97 struct MacGSocketData *data = malloc(sizeof(struct MacGSocketData));
98 if (data)
99 {
100 socket->m_gui_dependent = (char*)data;
101 data->socket = NULL;
102 data->source = NULL;
103 return 1;
104 }
105 return 0;
106 }
107
108 void _GSocket_GUI_Destroy_Socket(GSocket *socket)
109 {
110 struct MacGSocketData *data = (struct MacGSocketData*)(socket->m_gui_dependent);
111 if (data)
112 {
113 CFSocketInvalidate(data->socket);
114 CFRelease(data->socket);
115 free(data);
116 }
117 }
118
119 void _GSocket_Install_Callback(GSocket *socket, GSocketEvent event)
120 {
121 int c;
122 struct MacGSocketData* data = _GSocket_Get_Mac_Socket(socket);
123 if (!data) return;
124 switch (event)
125 {
126 case GSOCK_CONNECTION:
127 if(socket->m_server)
128 c = kCFSocketReadCallBack;
129 else
130 c = kCFSocketConnectCallBack;
131 break;
132 case GSOCK_LOST:
133 case GSOCK_INPUT:
134 c = kCFSocketReadCallBack;
135 break;
136 case GSOCK_OUTPUT:
137 c = kCFSocketWriteCallBack;
138 break;
139 default:
140 c = 0;
141 }
142 CFSocketEnableCallBacks(data->socket, c);
143 }
144
145 void _GSocket_Uninstall_Callback(GSocket *socket, GSocketEvent event)
146 {
147 int c;
148 struct MacGSocketData* data = _GSocket_Get_Mac_Socket(socket);
149 if (!data) return;
150 switch (event)
151 {
152 case GSOCK_CONNECTION:
153 if(socket->m_server)
154 c = kCFSocketReadCallBack;
155 else
156 c = kCFSocketConnectCallBack;
157 break;
158 case GSOCK_LOST:
159 case GSOCK_INPUT:
160 c = kCFSocketReadCallBack;
161 break;
162 case GSOCK_OUTPUT:
163 c = kCFSocketWriteCallBack;
164 break;
165 default:
166 c = 0;
167 }
168 CFSocketDisableCallBacks(data->socket, c);
169 }
170
171 void _GSocket_Enable_Events(GSocket *socket)
172 {
173 struct MacGSocketData* data = _GSocket_Get_Mac_Socket(socket);
174 if (!data) return;
175
176 CFRunLoopAddSource(CFRunLoopGetCurrent(), data->source, kCFRunLoopDefaultMode);
177 }
178
179 void _GSocket_Disable_Events(GSocket *socket)
180 {
181 struct MacGSocketData* data = _GSocket_Get_Mac_Socket(socket);
182 if (!data) return;
183
184 CFRunLoopRemoveSource(CFRunLoopGetCurrent(), data->source, kCFRunLoopCommonModes);
185 }
186
187 #endif // wxUSE_SOCKETS