1 /////////////////////////////////////////////////////////////////////////////
2 // Name: mac/corefoundation/utilsexc_base.cpp
3 // Purpose: wxMacLaunch
8 // Copyright: (c) Ryan Norton
9 // Licence: wxWindows licence
10 // Notes: Source was originally in utilsexc_cf.cpp,1.6 then moved
11 // to totally unrelated hid.cpp,1.8.
12 /////////////////////////////////////////////////////////////////////////////
14 //===========================================================================
16 //===========================================================================
18 //---------------------------------------------------------------------------
19 // Pre-compiled header stuff
20 //---------------------------------------------------------------------------
22 // For compilers that support precompilation, includes "wx.h".
23 #include "wx/wxprec.h"
27 #include "wx/string.h"
35 #include <CoreFoundation/CoreFoundation.h>
36 #ifndef __WXOSX_IPHONE__
37 #include <ApplicationServices/ApplicationServices.h>
41 #include "wx/filename.h"
42 #include "wx/osx/core/cfstring.h"
43 #include "wx/osx/core/private.h"
46 #define kDefaultPathStyle kCFURLPOSIXPathStyle
49 // global pointer which lives in the base library, set from the net one (see
50 // sockosx.cpp) and used from the GUI code (see utilsexc_cf.cpp) -- ugly but
51 // needed hack, see the above-mentioned files for more information
52 class wxSocketManager
;
53 extern WXDLLIMPEXP_BASE wxSocketManager
*wxOSXSocketManagerCF
;
54 wxSocketManager
*wxOSXSocketManagerCF
= NULL
;
55 #endif // wxUSE_SOCKETS
57 extern bool WXDLLEXPORT
wxIsDebuggerRunning()
59 // TODO : try to find out ...
63 #if wxOSX_USE_COCOA_OR_CARBON
65 // have a fast version for mac code that returns the version as a return value
67 long UMAGetSystemVersion()
69 static SInt32 sUMASystemVersion
= 0 ;
70 if ( sUMASystemVersion
== 0 )
72 verify_noerr(Gestalt(gestaltSystemVersion
, &sUMASystemVersion
));
74 return sUMASystemVersion
;
77 // our OS version is the same in non GUI and GUI cases
78 wxOperatingSystemId
wxGetOsVersion(int *majorVsn
, int *minorVsn
)
81 Gestalt(gestaltSystemVersion
, &theSystem
);
83 if ( majorVsn
!= NULL
)
84 *majorVsn
= (theSystem
>> 8);
86 if ( minorVsn
!= NULL
)
87 *minorVsn
= (theSystem
& 0xFF);
89 return wxOS_MAC_OSX_DARWIN
;
92 #include <sys/utsname.h>
94 wxString
wxGetOsDescription()
98 return wxString::Format(wxT("Mac OS X (%s %s %s)"),
99 wxString::FromAscii(name
.sysname
).c_str(),
100 wxString::FromAscii(name
.release
).c_str(),
101 wxString::FromAscii(name
.machine
).c_str());
104 #endif // wxOSX_USE_COCOA_OR_CARBON
106 //===========================================================================
108 //===========================================================================
110 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
114 // argv is the command line split up, with the application path first
115 // flags are the flags from wxExecute
116 // process is the process passed from wxExecute for pipe streams etc.
117 // returns -1 on error for wxEXEC_SYNC and 0 on error for wxEXEC_ASYNC
118 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
119 bool wxMacLaunch(char **argv
)
121 // Obtains the number of arguments for determining the size of
122 // the CFArray used to hold them
123 CFIndex cfiCount
= 0;
124 for(char** argvcopy
= argv
; *argvcopy
!= NULL
; ++argvcopy
)
129 // If there is not a single argument then there is no application
133 wxLogDebug(wxT("wxMacLaunch No file to launch!"));
138 wxString path
= *argv
++;
140 // Create a CFURL for the application path
141 // Created this way because we are opening a bundle which is a directory
143 CFURLCreateWithFileSystemPath(
147 true); //false == not a directory
149 // Check for error from the CFURL
152 wxLogDebug(wxT("wxMacLaunch Can't open path: %s"), path
.c_str());
156 // Create a CFBundle from the CFURL created earlier
157 CFBundleRef cfbApp
= CFBundleCreate(kCFAllocatorDefault
, cfurlApp
);
159 // Check to see if CFBundleCreate returned an error,
160 // and if it did this was an invalid bundle or not a bundle
161 // at all (maybe a simple directory etc.)
164 wxLogDebug(wxT("wxMacLaunch Bad bundle: %s"), path
.c_str());
169 // Get the bundle type and make sure its an 'APPL' bundle
170 // Otherwise we're dealing with something else here...
171 UInt32 dwBundleType
, dwBundleCreator
;
172 CFBundleGetPackageInfo(cfbApp
, &dwBundleType
, &dwBundleCreator
);
173 if(dwBundleType
!= 'APPL')
175 wxLogDebug(wxT("wxMacLaunch Not an APPL bundle: %s"), path
.c_str());
181 // Create a CFArray for dealing with the command line
182 // arguments to the bundle
183 CFMutableArrayRef cfaFiles
= CFArrayCreateMutable(kCFAllocatorDefault
,
184 cfiCount
-1, &kCFTypeArrayCallBacks
);
185 if(!cfaFiles
) //This should never happen
187 wxLogDebug(wxT("wxMacLaunch Could not create CFMutableArray"));
193 // Loop through command line arguments to the bundle,
194 // turn them into CFURLs and then put them in cfaFiles
195 // For use to launch services call
196 for( ; *argv
!= NULL
; ++argv
)
198 // Check for '<' as this will ring true for
199 // CFURLCreateWithString but is generally not considered
200 // typical on mac but is usually passed here from wxExecute
201 if (wxStrcmp(*argv
, wxT("<")) == 0)
205 CFURLRef cfurlCurrentFile
; // CFURL to hold file path
206 wxFileName
argfn(*argv
); // Filename for path
208 if(argfn
.DirExists())
210 // First, try creating as a directory
211 cfurlCurrentFile
= CFURLCreateWithFileSystemPath(
213 wxCFStringRef(*argv
),
215 true); //true == directory
217 else if(argfn
.FileExists())
219 // And if it isn't a directory try creating it
221 cfurlCurrentFile
= CFURLCreateWithFileSystemPath(
223 wxCFStringRef(*argv
),
225 false); //false == regular file
229 // Argument did not refer to
230 // an entry in the local filesystem,
231 // so try creating it through CFURLCreateWithString
232 cfurlCurrentFile
= CFURLCreateWithString(
234 wxCFStringRef(*argv
),
238 // Continue in the loop if the CFURL could not be created
239 if(!cfurlCurrentFile
)
242 wxT("wxMacLaunch Could not create CFURL for argument:%s"),
247 // Add the valid CFURL to the argument array and then
248 // release it as the CFArray adds a ref count to it
253 CFRelease(cfurlCurrentFile
); // array has retained it
256 // Create a LSLaunchURLSpec for use with LSOpenFromURLSpec
257 // Note that there are several flag options (launchFlags) such
258 // as kLSLaunchDontSwitch etc. and maybe we could be more
259 // picky about the flags we choose
260 LSLaunchURLSpec launchspec
;
261 launchspec
.appURL
= cfurlApp
;
262 launchspec
.itemURLs
= cfaFiles
;
263 launchspec
.passThruParams
= NULL
; //AEDesc*
264 launchspec
.launchFlags
= kLSLaunchDefaults
;
265 launchspec
.asyncRefCon
= NULL
;
267 // Finally, call LSOpenFromURL spec with our arguments
268 // 2nd parameter is a pointer to a CFURL that gets
269 // the actual path launched by the function
270 OSStatus status
= LSOpenFromURLSpec(&launchspec
, NULL
);
272 // Cleanup corefoundation references
277 // Check for error from LSOpenFromURLSpec
280 wxLogDebug(wxT("wxMacLaunch LSOpenFromURLSpec Error: %d"),
285 // No error from LSOpenFromURLSpec, so app was launched