]>
Commit | Line | Data |
---|---|---|
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) | |
6 | // Created: 2004-11-04 | |
7 | // RCS-ID: $Id$ | |
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 | ///////////////////////////////////////////////////////////////////////////// | |
12 | ||
13 | #include "wx/wxprec.h" | |
14 | #ifndef WX_PRECOMP | |
15 | #include "wx/log.h" | |
16 | #include "wx/utils.h" | |
17 | #endif //ndef WX_PRECOMP | |
18 | #include "wx/unix/execute.h" | |
19 | #include "wx/stdpaths.h" | |
20 | #include "wx/app.h" | |
21 | #include "wx/apptrait.h" | |
22 | #include "wx/thread.h" | |
23 | #include "wx/process.h" | |
24 | ||
25 | #include <sys/wait.h> | |
26 | ||
27 | #include <CoreFoundation/CFSocket.h> | |
28 | ||
29 | /*! | |
30 | Called due to source signal detected by the CFRunLoop. | |
31 | This is nearly identical to the wxGTK equivalent. | |
32 | */ | |
33 | extern "C" void WXCF_EndProcessDetector(CFSocketRef s, | |
34 | CFSocketCallBackType WXUNUSED(callbackType), | |
35 | CFDataRef WXUNUSED(address), | |
36 | void const *WXUNUSED(data), | |
37 | void *info) | |
38 | { | |
39 | /* | |
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. | |
50 | */ | |
51 | CFSocketInvalidate(s); | |
52 | ||
53 | // Now tell wx that the process has ended. | |
54 | wxHandleProcessTermination(static_cast<wxEndProcessData *>(info)); | |
55 | } | |
56 | ||
57 | /*! | |
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. | |
64 | */ | |
65 | int wxGUIAppTraits::AddProcessCallback(wxEndProcessData *proc_data, int fd) | |
66 | { | |
67 | static int s_last_tag = 0; | |
68 | CFSocketContext context = | |
69 | { 0 | |
70 | , static_cast<void*>(proc_data) | |
71 | , NULL | |
72 | , NULL | |
73 | , NULL | |
74 | }; | |
75 | CFSocketRef cfSocket = CFSocketCreateWithNative(kCFAllocatorDefault,fd,kCFSocketReadCallBack,&WXCF_EndProcessDetector,&context); | |
76 | if(cfSocket == NULL) | |
77 | { | |
78 | wxLogError(wxT("Failed to create socket for end process detection")); | |
79 | return 0; | |
80 | } | |
81 | CFRunLoopSourceRef runLoopSource = CFSocketCreateRunLoopSource(kCFAllocatorDefault, cfSocket, /*highest priority:*/0); | |
82 | if(runLoopSource == NULL) | |
83 | { | |
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); | |
87 | CFRelease(cfSocket); | |
88 | return 0; | |
89 | } | |
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. | |
92 | CFRelease(cfSocket); | |
93 | ||
94 | CFRunLoopAddSource(CFRunLoopGetCurrent(), runLoopSource, kCFRunLoopCommonModes); | |
95 | // Now that the run loop has the source retained we can release it. | |
96 | CFRelease(runLoopSource); | |
97 | ||
98 | /* | |
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. | |
103 | */ | |
104 | return ++s_last_tag; | |
105 | } | |
106 | ||
107 | ///////////////////////////////////////////////////////////////////////////// | |
108 | ||
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. | |
111 | #if wxUSE_STDPATHS | |
112 | static wxStandardPathsCF gs_stdPaths; | |
113 | wxStandardPaths& wxGUIAppTraits::GetStandardPaths() | |
114 | { | |
115 | return gs_stdPaths; | |
116 | } | |
117 | #endif | |
118 | ||
119 | #if wxUSE_SOCKETS | |
120 | ||
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 | |
124 | // | |
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() | |
129 | { | |
130 | return wxOSXSocketManagerCF ? wxOSXSocketManagerCF | |
131 | : wxGUIAppTraitsBase::GetSocketManager(); | |
132 | } | |
133 | ||
134 | #endif // wxUSE_SOCKETS |