]> git.saurik.com Git - wxWidgets.git/blame - src/mac/carbon/gsockosx.cpp
Compile fixes.
[wxWidgets.git] / src / mac / carbon / gsockosx.cpp
CommitLineData
02dad85e
DE
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
21struct MacGSocketData
22{
23 CFSocketRef socket;
24 CFRunLoopSourceRef source;
25};
26
27void 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);
0a647691 38 socket->Detected_Write();
02dad85e
DE
39 break;
40 case kCFSocketReadCallBack:
0a647691 41 socket->Detected_Read();
02dad85e
DE
42 break;
43 case kCFSocketWriteCallBack:
0a647691 44 socket->Detected_Write();
02dad85e
DE
45 break;
46 default:
47 break; /* We shouldn't get here. */
48 }
49}
50
51struct 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 socket->m_gui_dependent = (char*)data;
71
72 /* Keep the source and the socket around. */
73 data->source = source;
74 data->socket = cf;
75
76 return data;
77}
78
0a647691
DE
79bool GSocketGUIFunctionsTableConcrete::CanUseEventLoop()
80{ return true; }
81
82bool GSocketGUIFunctionsTableConcrete::OnInit(void)
02dad85e 83{
0a647691 84 return true;
02dad85e
DE
85}
86
0a647691 87void GSocketGUIFunctionsTableConcrete::OnExit(void)
02dad85e
DE
88{
89}
90
0a647691 91bool GSocketGUIFunctionsTableConcrete::Init_Socket(GSocket *socket)
02dad85e 92{
facd6764 93 struct MacGSocketData *data = (struct MacGSocketData *)malloc(sizeof(struct MacGSocketData));
02dad85e
DE
94 if (data)
95 {
96 socket->m_gui_dependent = (char*)data;
97 data->socket = NULL;
98 data->source = NULL;
99 return 1;
100 }
101 return 0;
102}
103
0a647691 104void GSocketGUIFunctionsTableConcrete::Destroy_Socket(GSocket *socket)
02dad85e
DE
105{
106 struct MacGSocketData *data = (struct MacGSocketData*)(socket->m_gui_dependent);
107 if (data)
108 {
109 CFRelease(data->socket);
110 free(data);
111 }
112}
113
0a647691 114void GSocketGUIFunctionsTableConcrete::Install_Callback(GSocket *socket, GSocketEvent event)
02dad85e
DE
115{
116 int c;
117 struct MacGSocketData* data = _GSocket_Get_Mac_Socket(socket);
118 if (!data) return;
119 switch (event)
120 {
121 case GSOCK_CONNECTION:
122 if(socket->m_server)
123 c = kCFSocketReadCallBack;
124 else
125 c = kCFSocketConnectCallBack;
126 break;
127 case GSOCK_LOST:
128 case GSOCK_INPUT:
129 c = kCFSocketReadCallBack;
130 break;
131 case GSOCK_OUTPUT:
132 c = kCFSocketWriteCallBack;
133 break;
134 default:
135 c = 0;
136 }
137 CFSocketEnableCallBacks(data->socket, c);
138}
139
0a647691 140void GSocketGUIFunctionsTableConcrete::Uninstall_Callback(GSocket *socket, GSocketEvent event)
02dad85e
DE
141{
142 int c;
143 struct MacGSocketData* data = _GSocket_Get_Mac_Socket(socket);
144 if (!data) return;
145 switch (event)
146 {
147 case GSOCK_CONNECTION:
148 if(socket->m_server)
149 c = kCFSocketReadCallBack;
150 else
151 c = kCFSocketConnectCallBack;
152 break;
153 case GSOCK_LOST:
154 case GSOCK_INPUT:
155 c = kCFSocketReadCallBack;
156 break;
157 case GSOCK_OUTPUT:
158 c = kCFSocketWriteCallBack;
159 break;
160 default:
161 c = 0;
162 }
163 CFSocketDisableCallBacks(data->socket, c);
164}
165
0a647691 166void GSocketGUIFunctionsTableConcrete::Enable_Events(GSocket *socket)
02dad85e
DE
167{
168 struct MacGSocketData* data = _GSocket_Get_Mac_Socket(socket);
169 if (!data) return;
170
171 CFRunLoopAddSource(CFRunLoopGetCurrent(), data->source, kCFRunLoopDefaultMode);
172}
173
0a647691 174void GSocketGUIFunctionsTableConcrete::Disable_Events(GSocket *socket)
02dad85e
DE
175{
176 struct MacGSocketData* data = _GSocket_Get_Mac_Socket(socket);
177 if (!data) return;
178
179 /* CFSocketInvalidate does CFRunLoopRemoveSource anyway */
180 CFRunLoopRemoveSource(CFRunLoopGetCurrent(), data->source, kCFRunLoopCommonModes);
181 CFSocketInvalidate(data->socket);
182}
183
184#endif // wxUSE_SOCKETS