1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/osx/core/utilsexc_cf.cpp
3 // Purpose: Execution-related utilities for Darwin
4 // Author: David Elliott, Ryan Norton (wxMacExecute)
5 // Modified by: Stefan Csomor (added necessary wxT for unicode builds)
8 // Copyright: (c) David Elliott, Ryan Norton
9 // (c) 2013 Rob Bresalier
10 // Licence: wxWindows licence
11 // Notes: This code comes from src/osx/carbon/utilsexc.cpp,1.11
12 /////////////////////////////////////////////////////////////////////////////
14 #include "wx/wxprec.h"
18 #endif //ndef WX_PRECOMP
19 #include "wx/stdpaths.h"
21 #include "wx/apptrait.h"
22 #include "wx/thread.h"
23 #include "wx/process.h"
25 #include "wx/evtloop.h"
26 #include "wx/evtloopsrc.h"
27 #include "wx/private/eventloopsourcesmanager.h"
31 #include <CoreFoundation/CFSocket.h>
33 #if wxUSE_EVENTLOOP_SOURCE
40 wx_socket_callback(CFSocketRef
WXUNUSED(s
),
41 CFSocketCallBackType callbackType
,
42 CFDataRef
WXUNUSED(address
),
43 void const *WXUNUSED(data
),
46 wxLogTrace(wxTRACE_EVT_SOURCE
,
47 "CFSocket callback, type=%d", static_cast<int>(callbackType
));
49 wxCFEventLoopSource
* const
50 source
= static_cast<wxCFEventLoopSource
*>(ctxData
);
52 wxEventLoopSourceHandler
* const
53 handler
= source
->GetHandler();
55 switch ( callbackType
)
57 case kCFSocketReadCallBack
:
58 handler
->OnReadWaiting();
61 case kCFSocketWriteCallBack
:
62 handler
->OnWriteWaiting();
66 wxFAIL_MSG( "Unexpected callback type." );
70 } // anonymous namespace
72 class wxCFEventLoopSourcesManager
: public wxEventLoopSourcesManagerBase
76 AddSourceForFD(int fd
, wxEventLoopSourceHandler
*handler
, int flags
)
78 wxCHECK_MSG( fd
!= -1, NULL
, "can't monitor invalid fd" );
80 wxScopedPtr
<wxCFEventLoopSource
>
81 source(new wxCFEventLoopSource(handler
, flags
));
83 CFSocketContext context
= { 0, source
.get(), NULL
, NULL
, NULL
};
85 int callbackTypes
= 0;
86 if ( flags
& wxEVENT_SOURCE_INPUT
)
87 callbackTypes
|= kCFSocketReadCallBack
;
88 if ( flags
& wxEVENT_SOURCE_OUTPUT
)
89 callbackTypes
|= kCFSocketWriteCallBack
;
92 cfSocket(CFSocketCreateWithNative
103 wxLogError(wxS("Failed to create event loop source socket."));
107 // Adjust the socket options to suit our needs:
108 CFOptionFlags sockopt
= CFSocketGetSocketFlags(cfSocket
);
110 // First, by default, write callback is not called repeatedly when data
111 // can be written to the socket but we need this behaviour so request
113 if ( flags
& wxEVENT_SOURCE_OUTPUT
)
114 sockopt
|= kCFSocketAutomaticallyReenableWriteCallBack
;
116 // Second, we use the socket to monitor the FD but it doesn't own it,
117 // so prevent the FD from being closed when the socket is invalidated.
118 sockopt
&= ~kCFSocketCloseOnInvalidate
;
120 CFSocketSetSocketFlags(cfSocket
, sockopt
);
122 wxCFRef
<CFRunLoopSourceRef
>
123 runLoopSource(CFSocketCreateRunLoopSource
127 0 // Lowest index means highest priority
129 if ( !runLoopSource
)
131 wxLogError(wxS("Failed to create low level event loop source."));
132 CFSocketInvalidate(cfSocket
);
136 // Save the socket so that we can remove it later if asked to.
137 source
->InitSourceSocket(cfSocket
.release());
139 CFRunLoopAddSource(CFRunLoopGetCurrent(), runLoopSource
, kCFRunLoopCommonModes
);
141 return source
.release();
145 wxEventLoopSourcesManagerBase
* wxGUIAppTraits::GetEventLoopSourcesManager()
147 static wxCFEventLoopSourcesManager s_eventLoopSourcesManager
;
149 return &s_eventLoopSourcesManager
;
152 #endif // wxUSE_EVENTLOOP_SOURCE
154 /////////////////////////////////////////////////////////////////////////////
156 // NOTE: This doesn't really belong here but this was a handy file to
157 // put it in because it's already compiled for wxCocoa and wxMac GUI lib.
159 wxStandardPaths
& wxGUIAppTraits::GetStandardPaths()
161 // Derive a class just to be able to create it: wxStandardPaths ctor is
162 // protected to prevent its misuse, but it also means we can't create an
163 // object of this class directly.
164 class wxStandardPathsDefault
: public wxStandardPathsCF
167 wxStandardPathsDefault() { }
170 static wxStandardPathsDefault gs_stdPaths
;
178 // we need to implement this method in a file of the core library as it should
179 // only be used for the GUI applications but we can't use socket stuff from it
180 // directly as this would create unwanted dependencies of core on net library
182 // so we have this global pointer which is set from sockosx.cpp when it is
183 // linked in and we simply return it from here
184 extern WXDLLIMPEXP_BASE wxSocketManager
*wxOSXSocketManagerCF
;
185 wxSocketManager
*wxGUIAppTraits::GetSocketManager()
187 return wxOSXSocketManagerCF
? wxOSXSocketManagerCF
188 : wxGUIAppTraitsBase::GetSocketManager();
191 #endif // wxUSE_SOCKETS