1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/osx/corefoundation/utilsexec_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 // Licence: wxWindows licence
10 // Notes: This code comes from src/osx/carbon/utilsexc.cpp,1.11
11 /////////////////////////////////////////////////////////////////////////////
13 #include "wx/wxprec.h"
17 #endif //ndef WX_PRECOMP
18 #include "wx/unix/execute.h"
19 #include "wx/stdpaths.h"
21 #include "wx/apptrait.h"
22 #include "wx/thread.h"
23 #include "wx/process.h"
27 #include <CoreFoundation/CFSocket.h>
30 Called due to source signal detected by the CFRunLoop.
31 This is nearly identical to the wxGTK equivalent.
33 extern "C" void WXCF_EndProcessDetector(CFSocketRef s
,
34 CFSocketCallBackType
WXUNUSED(callbackType
),
35 CFDataRef
WXUNUSED(address
),
36 void const *WXUNUSED(data
),
40 Either our pipe was closed or the process ended successfully. Either way,
41 we're done. It's not if waitpid is going to magically succeed when
42 we get fired again. CFSocketInvalidate closes the fd for us and also
43 invalidates the run loop source for us which should cause it to
44 release the CFSocket (thus causing it to be deallocated) and remove
45 itself from the runloop which should release it and cause it to also
46 be deallocated. Of course, it's possible the RunLoop hangs onto
47 one or both of them by retaining/releasing them within its stack
48 frame. However, that shouldn't be depended on. Assume that s is
49 deallocated due to the following call.
51 CFSocketInvalidate(s
);
53 // Now tell wx that the process has ended.
54 wxHandleProcessTermination(static_cast<wxEndProcessData
*>(info
));
58 Implements the GUI-specific AddProcessCallback() for both wxMac and
59 wxCocoa using the CFSocket/CFRunLoop API which is available to both.
60 Takes advantage of the fact that sockets on UNIX are just regular
61 file descriptors and thus even a non-socket file descriptor can
62 apparently be used with CFSocket so long as you only tell CFSocket
63 to do things with it that would be valid for a non-socket fd.
65 int wxGUIAppTraits::AddProcessCallback(wxEndProcessData
*proc_data
, int fd
)
67 static int s_last_tag
= 0;
68 CFSocketContext context
=
70 , static_cast<void*>(proc_data
)
75 CFSocketRef cfSocket
= CFSocketCreateWithNative(kCFAllocatorDefault
,fd
,kCFSocketReadCallBack
,&WXCF_EndProcessDetector
,&context
);
78 wxLogError(wxT("Failed to create socket for end process detection"));
81 CFRunLoopSourceRef runLoopSource
= CFSocketCreateRunLoopSource(kCFAllocatorDefault
, cfSocket
, /*highest priority:*/0);
82 if(runLoopSource
== NULL
)
84 wxLogError(wxT("Failed to create CFRunLoopSource from CFSocket for end process detection"));
85 // closes the fd.. we can't really stop it, nor do we necessarily want to.
86 CFSocketInvalidate(cfSocket
);
90 // Now that the run loop source has the socket retained and we no longer
91 // need to refer to it within this method, we can release it.
94 CFRunLoopAddSource(CFRunLoopGetCurrent(), runLoopSource
, kCFRunLoopCommonModes
);
95 // Now that the run loop has the source retained we can release it.
96 CFRelease(runLoopSource
);
99 Feed wx some bullshit.. we don't use it since CFSocket helpfully passes
100 itself into our callback and that's enough to be able to
101 CFSocketInvalidate it which is all we need to do to get everything we
102 just created to be deallocated.
107 /////////////////////////////////////////////////////////////////////////////
109 // NOTE: This doesn't really belong here but this was a handy file to
110 // put it in because it's already compiled for wxCocoa and wxMac GUI lib.
112 static wxStandardPathsCF gs_stdPaths
;
113 wxStandardPaths
& wxGUIAppTraits::GetStandardPaths()
121 // we need to implement this method in a file of the core library as it should
122 // only be used for the GUI applications but we can't use socket stuff from it
123 // directly as this would create unwanted dependencies of core on net library
125 // so we have this global pointer which is set from sockosx.cpp when it is
126 // linked in and we simply return it from here
127 extern WXDLLIMPEXP_BASE wxSocketManager
*wxOSXSocketManagerCF
;
128 wxSocketManager
*wxGUIAppTraits::GetSocketManager()
130 return wxOSXSocketManagerCF
? wxOSXSocketManagerCF
131 : wxGUIAppTraitsBase::GetSocketManager();
134 #endif // wxUSE_SOCKETS