1 /* -------------------------------------------------------------------------
2 * Project: GSocket (Generic Socket) for WX
4 * Purpose: GSocket: Mac OS X mach-o part
6 * Mac code by Brian Victor, February 2002. Email comments to bhv1@psu.edu
7 * ------------------------------------------------------------------------- */
14 #include "wx/gsocket.h"
15 #include "wx/unix/gsockunx.h"
17 #include <CoreFoundation/CoreFoundation.h>
22 CFRunLoopSourceRef source
;
25 void Mac_Socket_Callback(CFSocketRef s
, CFSocketCallBackType callbackType
,
26 CFDataRef address
, const void* data
, void* info
)
28 GSocket
* socket
= (GSocket
*)info
;
29 struct MacGSocketData
* macdata
;
30 macdata
= (struct MacGSocketData
*)socket
->m_gui_dependent
;
34 case kCFSocketConnectCallBack
:
35 socket
->m_functions
->Detected_Read(socket
);
37 case kCFSocketReadCallBack
:
38 socket
->m_functions
->Detected_Read(socket
);
40 case kCFSocketWriteCallBack
:
41 socket
->m_functions
->Detected_Write(socket
);
44 break; /* We shouldn't get here. */
48 struct MacGSocketData
* _GSocket_Get_Mac_Socket(GSocket
*socket
)
50 /* If socket is already created, returns a pointer to the data */
51 /* Otherwise, creates socket and returns the pointer */
54 struct MacGSocketData
* data
= (struct MacGSocketData
*)socket
->m_gui_dependent
;
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
;
61 c
= kCFSocketReadCallBack
| kCFSocketWriteCallBack
;
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);
68 CFSocketSetSocketFlags(cf
, 0); /* Callbacks must be reenabled manually */
69 socket
->m_gui_dependent
= (char*)data
;
70 CFRunLoopAddSource(CFRunLoopGetCurrent(), source
, kCFRunLoopDefaultMode
);
72 /* Keep the source and the socket around. */
73 data
->source
= source
;
79 int _GSocket_GUI_Init(void)
84 void _GSocket_GUI_Cleanup(void)
88 int _GSocket_GUI_Init_Socket(GSocket
*socket
)
90 struct MacGSocketData
*data
= malloc(sizeof(struct MacGSocketData
));
93 socket
->m_gui_dependent
= (char*)data
;
101 void _GSocket_GUI_Destroy_Socket(GSocket
*socket
)
103 struct MacGSocketData
*data
= (struct MacGSocketData
*)(socket
->m_gui_dependent
);
106 CFRunLoopRemoveSource(CFRunLoopGetCurrent(), data
->source
, kCFRunLoopCommonModes
);
107 CFSocketInvalidate(data
->socket
);
112 void _GSocket_Install_Callback(GSocket
*socket
, GSocketEvent event
)
115 struct MacGSocketData
* data
= _GSocket_Get_Mac_Socket(socket
);
119 case GSOCK_CONNECTION
:
120 c
= kCFSocketReadCallBack
; /* This works, but I don't know why. */
123 c
= kCFSocketReadCallBack
;
126 c
= kCFSocketWriteCallBack
;
131 CFSocketEnableCallBacks(data
->socket
, c
);
134 void _GSocket_Uninstall_Callback(GSocket
*socket
, GSocketEvent event
)
137 struct MacGSocketData
* data
= _GSocket_Get_Mac_Socket(socket
);
141 case GSOCK_CONNECTION
:
142 c
= kCFSocketConnectCallBack
;
145 c
= kCFSocketReadCallBack
;
148 c
= kCFSocketWriteCallBack
;
153 CFSocketDisableCallBacks(data
->socket
, c
);
156 void _GSocket_Enable_Events(GSocket
*socket
)
158 struct MacGSocketData
* data
= _GSocket_Get_Mac_Socket(socket
);
160 CFSocketEnableCallBacks(data
->socket
, kCFSocketReadCallBack
| kCFSocketWriteCallBack
);
163 void _GSocket_Disable_Events(GSocket
*socket
)
165 struct MacGSocketData
* data
= _GSocket_Get_Mac_Socket(socket
);
167 CFSocketDisableCallBacks(data
->socket
, kCFSocketReadCallBack
| kCFSocketWriteCallBack
);
170 #endif // wxUSE_SOCKETS