]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/gsockosx.cpp
wxTextPos for all GetLastPosition with constants for special cases. Make it virtual...
[wxWidgets.git] / src / mac / carbon / gsockosx.cpp
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 // Sockets must use the event loop on the main thread
28 // We will store the main loop's reference when Initialize is called
29 static CFRunLoopRef s_mainRunLoop = NULL;
30
31 void Mac_Socket_Callback(CFSocketRef s, CFSocketCallBackType callbackType,
32 CFDataRef address, const void* data, void* info)
33 {
34 GSocket* socket = (GSocket*)info;
35 struct MacGSocketData* macdata;
36 macdata = (struct MacGSocketData*)socket->m_gui_dependent;
37 if (!macdata) return;
38 switch (callbackType)
39 {
40 case kCFSocketConnectCallBack:
41 assert(!socket->m_server);
42 socket->Detected_Write();
43 break;
44 case kCFSocketReadCallBack:
45 socket->Detected_Read();
46 break;
47 case kCFSocketWriteCallBack:
48 socket->Detected_Write();
49 break;
50 default:
51 break; /* We shouldn't get here. */
52 }
53 }
54
55 struct MacGSocketData* _GSocket_Get_Mac_Socket(GSocket *socket)
56 {
57 /* If socket is already created, returns a pointer to the data */
58 /* Otherwise, creates socket and returns the pointer */
59 CFSocketContext cont;
60 struct MacGSocketData* data = (struct MacGSocketData*)socket->m_gui_dependent;
61
62 if (data && data->source) return data;
63
64 /* CFSocket has not been created, create it: */
65 if (socket->m_fd < 0 || !data) return NULL;
66 cont.version = 0; cont.retain = NULL;
67 cont.release = NULL; cont.copyDescription = NULL;
68 cont.info = socket;
69
70 CFSocketRef cf = CFSocketCreateWithNative(NULL, socket->m_fd,
71 ALL_CALLBACK_TYPES, Mac_Socket_Callback, &cont);
72 CFRunLoopSourceRef source = CFSocketCreateRunLoopSource(NULL, cf, 0);
73 assert(source);
74 socket->m_gui_dependent = (char*)data;
75
76 /* Keep the source and the socket around. */
77 data->source = source;
78 data->socket = cf;
79
80 return data;
81 }
82
83 bool GSocketGUIFunctionsTableConcrete::CanUseEventLoop()
84 { return true; }
85
86 bool GSocketGUIFunctionsTableConcrete::OnInit(void)
87 {
88 // No need to store the main loop again
89 if (s_mainRunLoop != NULL)
90 return true;
91
92 // Get the loop for the main thread so our events will actually fire.
93 // The common socket.cpp code will assert if initialize is called from a
94 // secondary thread, otherwise Mac would have the same problems as MSW
95 s_mainRunLoop = CFRunLoopGetCurrent();
96 CFRetain(s_mainRunLoop);
97
98 return true;
99 }
100
101 void GSocketGUIFunctionsTableConcrete::OnExit(void)
102 {
103 // Release the reference count, and set the reference back to NULL
104 CFRelease(s_mainRunLoop);
105 s_mainRunLoop = NULL;
106 }
107
108 bool GSocketGUIFunctionsTableConcrete::Init_Socket(GSocket *socket)
109 {
110 struct MacGSocketData *data = (struct MacGSocketData *)malloc(sizeof(struct MacGSocketData));
111 if (data)
112 {
113 socket->m_gui_dependent = (char*)data;
114 data->socket = NULL;
115 data->source = NULL;
116 return 1;
117 }
118 return 0;
119 }
120
121 void GSocketGUIFunctionsTableConcrete::Destroy_Socket(GSocket *socket)
122 {
123 struct MacGSocketData *data = (struct MacGSocketData*)(socket->m_gui_dependent);
124 if (data)
125 {
126 CFRelease(data->socket);
127 free(data);
128 }
129 }
130
131 void GSocketGUIFunctionsTableConcrete::Install_Callback(GSocket *socket, GSocketEvent event)
132 {
133 int c;
134 struct MacGSocketData* data = _GSocket_Get_Mac_Socket(socket);
135 if (!data) return;
136 switch (event)
137 {
138 case GSOCK_CONNECTION:
139 if(socket->m_server)
140 c = kCFSocketReadCallBack;
141 else
142 c = kCFSocketConnectCallBack;
143 break;
144 case GSOCK_LOST:
145 case GSOCK_INPUT:
146 c = kCFSocketReadCallBack;
147 break;
148 case GSOCK_OUTPUT:
149 c = kCFSocketWriteCallBack;
150 break;
151 default:
152 c = 0;
153 }
154 CFSocketEnableCallBacks(data->socket, c);
155 }
156
157 void GSocketGUIFunctionsTableConcrete::Uninstall_Callback(GSocket *socket, GSocketEvent event)
158 {
159 int c;
160 struct MacGSocketData* data = _GSocket_Get_Mac_Socket(socket);
161 if (!data) return;
162 switch (event)
163 {
164 case GSOCK_CONNECTION:
165 if(socket->m_server)
166 c = kCFSocketReadCallBack;
167 else
168 c = kCFSocketConnectCallBack;
169 break;
170 case GSOCK_LOST:
171 case GSOCK_INPUT:
172 c = kCFSocketReadCallBack;
173 break;
174 case GSOCK_OUTPUT:
175 c = kCFSocketWriteCallBack;
176 break;
177 default:
178 c = 0;
179 }
180 CFSocketDisableCallBacks(data->socket, c);
181 }
182
183 void GSocketGUIFunctionsTableConcrete::Enable_Events(GSocket *socket)
184 {
185 struct MacGSocketData* data = _GSocket_Get_Mac_Socket(socket);
186 if (!data) return;
187
188 CFRunLoopAddSource(s_mainRunLoop, data->source, kCFRunLoopCommonModes);
189 }
190
191 void GSocketGUIFunctionsTableConcrete::Disable_Events(GSocket *socket)
192 {
193 struct MacGSocketData* data = _GSocket_Get_Mac_Socket(socket);
194 if (!data) return;
195
196 /* CFSocketInvalidate does CFRunLoopRemoveSource anyway */
197 CFRunLoopRemoveSource(s_mainRunLoop, data->source, kCFRunLoopCommonModes);
198 CFSocketInvalidate(data->socket);
199 }
200
201 #endif // wxUSE_SOCKETS