]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/gsockosx.c
GSocket (Generic Socket) Mac OS X mach-o part by Brian Victor
[wxWidgets.git] / src / mac / carbon / 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 struct MacGSocketData
20 {
21 CFSocketRef socket;
22 CFRunLoopSourceRef source;
23 };
24
25 void Mac_Socket_Callback(CFSocketRef s, CFSocketCallBackType callbackType,
26 CFDataRef address, const void* data, void* info)
27 {
28 GSocket* socket = (GSocket*)info;
29 struct MacGSocketData* macdata;
30 macdata = (struct MacGSocketData*)socket->m_gui_dependent;
31 if (!macdata) return;
32 switch (callbackType)
33 {
34 case kCFSocketConnectCallBack:
35 socket->m_functions->Detected_Read(socket);
36 break;
37 case kCFSocketReadCallBack:
38 socket->m_functions->Detected_Read(socket);
39 break;
40 case kCFSocketWriteCallBack:
41 socket->m_functions->Detected_Write(socket);
42 break;
43 default:
44 break; /* We shouldn't get here. */
45 }
46 }
47
48 struct MacGSocketData* _GSocket_Get_Mac_Socket(GSocket *socket)
49 {
50 /* If socket is already created, returns a pointer to the data */
51 /* Otherwise, creates socket and returns the pointer */
52 CFOptionFlags c;
53 CFSocketContext cont;
54 struct MacGSocketData* data = (struct MacGSocketData*)socket->m_gui_dependent;
55
56 if (data && data->source) return data;
57 if (socket->m_fd < 0 || !data) return NULL;
58 cont.version = 0; cont.retain = NULL;
59 cont.release = NULL; cont.copyDescription = NULL;
60 cont.info = socket;
61 c = kCFSocketReadCallBack | kCFSocketWriteCallBack;
62
63 CFSocketRef cf = CFSocketCreateWithNative(NULL, socket->m_fd, c,
64 Mac_Socket_Callback, &cont);
65 CFSocketDisableCallBacks(cf, kCFSocketReadCallBack | kCFSocketWriteCallBack);
66 CFRunLoopSourceRef source = CFSocketCreateRunLoopSource(NULL, cf, 0);
67 assert(source);
68 CFSocketSetSocketFlags(cf, 0); /* Callbacks must be reenabled manually */
69 socket->m_gui_dependent = (char*)data;
70 CFRunLoopAddSource(CFRunLoopGetCurrent(), source, kCFRunLoopDefaultMode);
71
72 /* Keep the source and the socket around. */
73 data->source = source;
74 data->socket = cf;
75
76 return data;
77 }
78
79 int _GSocket_GUI_Init(void)
80 {
81 return 1;
82 }
83
84 void _GSocket_GUI_Cleanup(void)
85 {
86 }
87
88 int _GSocket_GUI_Init_Socket(GSocket *socket)
89 {
90 struct MacGSocketData *data = malloc(sizeof(struct MacGSocketData));
91 if (data)
92 {
93 socket->m_gui_dependent = (char*)data;
94 data->socket = NULL;
95 data->source = NULL;
96 return 1;
97 }
98 return 0;
99 }
100
101 void _GSocket_GUI_Destroy_Socket(GSocket *socket)
102 {
103 struct MacGSocketData *data = (struct MacGSocketData*)(socket->m_gui_dependent);
104 if (data)
105 {
106 CFRunLoopRemoveSource(CFRunLoopGetCurrent(), data->source, kCFRunLoopCommonModes);
107 CFSocketInvalidate(data->socket);
108 free(data);
109 }
110 }
111
112 void _GSocket_Install_Callback(GSocket *socket, GSocketEvent event)
113 {
114 int c;
115 struct MacGSocketData* data = _GSocket_Get_Mac_Socket(socket);
116 if (!data) return;
117 switch (event)
118 {
119 case GSOCK_CONNECTION:
120 c = kCFSocketReadCallBack; /* This works, but I don't know why. */
121 break;
122 case GSOCK_INPUT:
123 c = kCFSocketReadCallBack;
124 break;
125 case GSOCK_OUTPUT:
126 c = kCFSocketWriteCallBack;
127 break;
128 default:
129 c = 0;
130 }
131 CFSocketEnableCallBacks(data->socket, c);
132 }
133
134 void _GSocket_Uninstall_Callback(GSocket *socket, GSocketEvent event)
135 {
136 int c;
137 struct MacGSocketData* data = _GSocket_Get_Mac_Socket(socket);
138 if (!data) return;
139 switch (event)
140 {
141 case GSOCK_CONNECTION:
142 c = kCFSocketConnectCallBack;
143 break;
144 case GSOCK_INPUT:
145 c = kCFSocketReadCallBack;
146 break;
147 case GSOCK_OUTPUT:
148 c = kCFSocketWriteCallBack;
149 break;
150 default:
151 c = 0;
152 }
153 CFSocketDisableCallBacks(data->socket, c);
154 }
155
156 void _GSocket_Enable_Events(GSocket *socket)
157 {
158 struct MacGSocketData* data = _GSocket_Get_Mac_Socket(socket);
159 if (!data) return;
160 CFSocketEnableCallBacks(data->socket, kCFSocketReadCallBack | kCFSocketWriteCallBack);
161 }
162
163 void _GSocket_Disable_Events(GSocket *socket)
164 {
165 struct MacGSocketData* data = _GSocket_Get_Mac_Socket(socket);
166 if (!data) return;
167 CFSocketDisableCallBacks(data->socket, kCFSocketReadCallBack | kCFSocketWriteCallBack);
168 }
169
170 #endif // wxUSE_SOCKETS