]> git.saurik.com Git - wxWidgets.git/blame - src/mac/carbon/gsockosx.c
refresh had to offset the region by borders
[wxWidgets.git] / src / mac / carbon / gsockosx.c
CommitLineData
e1385476
GD
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
a38b65ab
DE
19#define ALL_CALLBACK_TYPES (kCFSocketReadCallBack | kCFSocketWriteCallBack | kCFSocketConnectCallBack)
20
e1385476
GD
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:
a38b65ab
DE
37 assert(!socket->m_server);
38 socket->m_functions->Detected_Write(socket);
e1385476
GD
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
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 */
e1385476 55 CFSocketContext cont;
db365433
DE
56 CFSocketRef cf;
57 CFRunLoopSourceRef source;
58
e1385476
GD
59 struct MacGSocketData* data = (struct MacGSocketData*)socket->m_gui_dependent;
60
61 if (data && data->source) return data;
a38b65ab
DE
62
63 /* CFSocket has not been created, create it: */
e1385476
GD
64 if (socket->m_fd < 0 || !data) return NULL;
65 cont.version = 0; cont.retain = NULL;
66 cont.release = NULL; cont.copyDescription = NULL;
67 cont.info = socket;
e1385476 68
db365433 69 cf = CFSocketCreateWithNative(NULL, socket->m_fd,
a38b65ab 70 ALL_CALLBACK_TYPES, Mac_Socket_Callback, &cont);
db365433 71 source = CFSocketCreateRunLoopSource(NULL, cf, 0);
e1385476 72 assert(source);
e1385476 73 socket->m_gui_dependent = (char*)data;
e1385476
GD
74
75 /* Keep the source and the socket around. */
76 data->source = source;
77 data->socket = cf;
78
79 return data;
80}
81
82int _GSocket_GUI_Init(void)
83{
84 return 1;
85}
86
87void _GSocket_GUI_Cleanup(void)
88{
89}
90
91int _GSocket_GUI_Init_Socket(GSocket *socket)
92{
93 struct MacGSocketData *data = malloc(sizeof(struct MacGSocketData));
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
104void _GSocket_GUI_Destroy_Socket(GSocket *socket)
105{
106 struct MacGSocketData *data = (struct MacGSocketData*)(socket->m_gui_dependent);
107 if (data)
108 {
a38b65ab 109 CFRelease(data->socket);
e1385476
GD
110 free(data);
111 }
112}
113
114void _GSocket_Install_Callback(GSocket *socket, GSocketEvent event)
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:
a38b65ab
DE
122 if(socket->m_server)
123 c = kCFSocketReadCallBack;
124 else
125 c = kCFSocketConnectCallBack;
e1385476 126 break;
a38b65ab 127 case GSOCK_LOST:
e1385476
GD
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
140void _GSocket_Uninstall_Callback(GSocket *socket, GSocketEvent event)
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:
a38b65ab
DE
148 if(socket->m_server)
149 c = kCFSocketReadCallBack;
150 else
151 c = kCFSocketConnectCallBack;
e1385476 152 break;
a38b65ab 153 case GSOCK_LOST:
e1385476
GD
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
166void _GSocket_Enable_Events(GSocket *socket)
167{
168 struct MacGSocketData* data = _GSocket_Get_Mac_Socket(socket);
169 if (!data) return;
a38b65ab 170
fda20787 171 CFRunLoopAddSource(CFRunLoopGetCurrent(), data->source, kCFRunLoopDefaultMode);
e1385476
GD
172}
173
174void _GSocket_Disable_Events(GSocket *socket)
175{
176 struct MacGSocketData* data = _GSocket_Get_Mac_Socket(socket);
177 if (!data) return;
fda20787 178
ffb649e4 179 /* CFSocketInvalidate does CFRunLoopRemoveSource anyway */
fda20787 180 CFRunLoopRemoveSource(CFRunLoopGetCurrent(), data->source, kCFRunLoopCommonModes);
ffb649e4 181 CFSocketInvalidate(data->socket);
e1385476
GD
182}
183
184#endif // wxUSE_SOCKETS