X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/facd6764872eede45605ba7c9dfa0e1d0c708fa2..b9efe021b554fa3967d1442cf758435c5cd5ae8f:/src/mac/carbon/gsockosx.cpp?ds=sidebyside diff --git a/src/mac/carbon/gsockosx.cpp b/src/mac/carbon/gsockosx.cpp index b9df39196e..46495ac6fb 100644 --- a/src/mac/carbon/gsockosx.cpp +++ b/src/mac/carbon/gsockosx.cpp @@ -1,8 +1,10 @@ /* ------------------------------------------------------------------------- - * Project: GSocket (Generic Socket) for WX - * Name: gsockosx.c - * Purpose: GSocket: Mac OS X mach-o part - * CVSID: $Id$ + * Project: GSocket (Generic Socket) for WX + * Name: gsockosx.c + * Copyright: (c) Brian Victor + * Licence: wxWindows Licence + * Purpose: GSocket: Mac OS X mach-o part + * CVSID: $Id$ * Mac code by Brian Victor, February 2002. Email comments to bhv1@psu.edu * ------------------------------------------------------------------------- */ @@ -24,6 +26,10 @@ struct MacGSocketData CFRunLoopSourceRef source; }; +// Sockets must use the event loop on the main thread +// We will store the main loop's reference when Initialize is called +static CFRunLoopRef s_mainRunLoop = NULL; + void Mac_Socket_Callback(CFSocketRef s, CFSocketCallBackType callbackType, CFDataRef address, const void* data, void* info) { @@ -35,13 +41,13 @@ void Mac_Socket_Callback(CFSocketRef s, CFSocketCallBackType callbackType, { case kCFSocketConnectCallBack: assert(!socket->m_server); - socket->m_functions->Detected_Write(socket); + socket->Detected_Write(); break; case kCFSocketReadCallBack: - socket->m_functions->Detected_Read(socket); + socket->Detected_Read(); break; case kCFSocketWriteCallBack: - socket->m_functions->Detected_Write(socket); + socket->Detected_Write(); break; default: break; /* We shouldn't get here. */ @@ -76,16 +82,32 @@ struct MacGSocketData* _GSocket_Get_Mac_Socket(GSocket *socket) return data; } -int _GSocket_GUI_Init(void) +bool GSocketGUIFunctionsTableConcrete::CanUseEventLoop() +{ return true; } + +bool GSocketGUIFunctionsTableConcrete::OnInit(void) { - return 1; + // No need to store the main loop again + if (s_mainRunLoop != NULL) + return true; + + // Get the loop for the main thread so our events will actually fire. + // The common socket.cpp code will assert if initialize is called from a + // secondary thread, otherwise Mac would have the same problems as MSW + s_mainRunLoop = CFRunLoopGetCurrent(); + CFRetain(s_mainRunLoop); + + return true; } -void _GSocket_GUI_Cleanup(void) +void GSocketGUIFunctionsTableConcrete::OnExit(void) { + // Release the reference count, and set the reference back to NULL + CFRelease(s_mainRunLoop); + s_mainRunLoop = NULL; } -int _GSocket_GUI_Init_Socket(GSocket *socket) +bool GSocketGUIFunctionsTableConcrete::Init_Socket(GSocket *socket) { struct MacGSocketData *data = (struct MacGSocketData *)malloc(sizeof(struct MacGSocketData)); if (data) @@ -98,7 +120,7 @@ int _GSocket_GUI_Init_Socket(GSocket *socket) return 0; } -void _GSocket_GUI_Destroy_Socket(GSocket *socket) +void GSocketGUIFunctionsTableConcrete::Destroy_Socket(GSocket *socket) { struct MacGSocketData *data = (struct MacGSocketData*)(socket->m_gui_dependent); if (data) @@ -108,7 +130,7 @@ void _GSocket_GUI_Destroy_Socket(GSocket *socket) } } -void _GSocket_Install_Callback(GSocket *socket, GSocketEvent event) +void GSocketGUIFunctionsTableConcrete::Install_Callback(GSocket *socket, GSocketEvent event) { int c; struct MacGSocketData* data = _GSocket_Get_Mac_Socket(socket); @@ -134,7 +156,7 @@ void _GSocket_Install_Callback(GSocket *socket, GSocketEvent event) CFSocketEnableCallBacks(data->socket, c); } -void _GSocket_Uninstall_Callback(GSocket *socket, GSocketEvent event) +void GSocketGUIFunctionsTableConcrete::Uninstall_Callback(GSocket *socket, GSocketEvent event) { int c; struct MacGSocketData* data = _GSocket_Get_Mac_Socket(socket); @@ -160,21 +182,21 @@ void _GSocket_Uninstall_Callback(GSocket *socket, GSocketEvent event) CFSocketDisableCallBacks(data->socket, c); } -void _GSocket_Enable_Events(GSocket *socket) +void GSocketGUIFunctionsTableConcrete::Enable_Events(GSocket *socket) { struct MacGSocketData* data = _GSocket_Get_Mac_Socket(socket); if (!data) return; - CFRunLoopAddSource(CFRunLoopGetCurrent(), data->source, kCFRunLoopDefaultMode); + CFRunLoopAddSource(s_mainRunLoop, data->source, kCFRunLoopCommonModes); } -void _GSocket_Disable_Events(GSocket *socket) +void GSocketGUIFunctionsTableConcrete::Disable_Events(GSocket *socket) { struct MacGSocketData* data = _GSocket_Get_Mac_Socket(socket); if (!data) return; /* CFSocketInvalidate does CFRunLoopRemoveSource anyway */ - CFRunLoopRemoveSource(CFRunLoopGetCurrent(), data->source, kCFRunLoopCommonModes); + CFRunLoopRemoveSource(s_mainRunLoop, data->source, kCFRunLoopCommonModes); CFSocketInvalidate(data->socket); }