1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/osx/core/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 #if ( !wxUSE_GUI && !wxOSX_USE_IPHONE ) || wxOSX_USE_COCOA_OR_CARBON
59 // have a fast version for mac code that returns the version as a return value
61 long UMAGetSystemVersion()
63 static SInt32 sUMASystemVersion
= 0 ;
64 if ( sUMASystemVersion
== 0 )
66 verify_noerr(Gestalt(gestaltSystemVersion
, &sUMASystemVersion
));
68 return sUMASystemVersion
;
71 // our OS version is the same in non GUI and GUI cases
72 wxOperatingSystemId
wxGetOsVersion(int *majorVsn
, int *minorVsn
)
74 // This returns 10 and 6 for OS X 10.6, consistent with behaviour on
77 Gestalt(gestaltSystemVersionMajor
, &maj
);
78 Gestalt(gestaltSystemVersionMinor
, &min
);
80 if ( majorVsn
!= NULL
)
83 if ( minorVsn
!= NULL
)
88 Gestalt(gestaltSystemVersion
, &theSystem
);
90 if ( majorVsn
!= NULL
)
91 *majorVsn
= (theSystem
>> 8);
93 if ( minorVsn
!= NULL
)
94 *minorVsn
= (theSystem
& 0xFF);
96 return wxOS_MAC_OSX_DARWIN
;
99 #include <sys/utsname.h>
101 wxString
wxGetOsDescription()
105 return wxString::Format(wxT("Mac OS X (%s %s %s)"),
106 wxString::FromAscii(name
.sysname
).c_str(),
107 wxString::FromAscii(name
.release
).c_str(),
108 wxString::FromAscii(name
.machine
).c_str());
111 //===========================================================================
113 //===========================================================================
115 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
119 // argv is the command line split up, with the application path first
120 // flags are the flags from wxExecute
121 // process is the process passed from wxExecute for pipe streams etc.
122 // returns -1 on error for wxEXEC_SYNC and 0 on error for wxEXEC_ASYNC
123 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
124 bool wxMacLaunch(char **argv
)
126 // Obtains the number of arguments for determining the size of
127 // the CFArray used to hold them
128 CFIndex cfiCount
= 0;
129 for(char** argvcopy
= argv
; *argvcopy
!= NULL
; ++argvcopy
)
134 // If there is not a single argument then there is no application
138 wxLogDebug(wxT("wxMacLaunch No file to launch!"));
143 wxString path
= *argv
++;
145 // Create a CFURL for the application path
146 // Created this way because we are opening a bundle which is a directory
148 CFURLCreateWithFileSystemPath(
152 true); //false == not a directory
154 // Check for error from the CFURL
157 wxLogDebug(wxT("wxMacLaunch Can't open path: %s"), path
.c_str());
161 // Create a CFBundle from the CFURL created earlier
162 CFBundleRef cfbApp
= CFBundleCreate(kCFAllocatorDefault
, cfurlApp
);
164 // Check to see if CFBundleCreate returned an error,
165 // and if it did this was an invalid bundle or not a bundle
166 // at all (maybe a simple directory etc.)
169 wxLogDebug(wxT("wxMacLaunch Bad bundle: %s"), path
.c_str());
174 // Get the bundle type and make sure its an 'APPL' bundle
175 // Otherwise we're dealing with something else here...
176 UInt32 dwBundleType
, dwBundleCreator
;
177 CFBundleGetPackageInfo(cfbApp
, &dwBundleType
, &dwBundleCreator
);
178 if(dwBundleType
!= 'APPL')
180 wxLogDebug(wxT("wxMacLaunch Not an APPL bundle: %s"), path
.c_str());
186 // Create a CFArray for dealing with the command line
187 // arguments to the bundle
188 CFMutableArrayRef cfaFiles
= CFArrayCreateMutable(kCFAllocatorDefault
,
189 cfiCount
-1, &kCFTypeArrayCallBacks
);
190 if(!cfaFiles
) //This should never happen
192 wxLogDebug(wxT("wxMacLaunch Could not create CFMutableArray"));
198 // Loop through command line arguments to the bundle,
199 // turn them into CFURLs and then put them in cfaFiles
200 // For use to launch services call
201 for( ; *argv
!= NULL
; ++argv
)
203 // Check for '<' as this will ring true for
204 // CFURLCreateWithString but is generally not considered
205 // typical on mac but is usually passed here from wxExecute
206 if (wxStrcmp(*argv
, wxT("<")) == 0)
210 CFURLRef cfurlCurrentFile
; // CFURL to hold file path
211 wxFileName
argfn(*argv
); // Filename for path
213 if(argfn
.DirExists())
215 // First, try creating as a directory
216 cfurlCurrentFile
= CFURLCreateWithFileSystemPath(
218 wxCFStringRef(*argv
),
220 true); //true == directory
222 else if(argfn
.FileExists())
224 // And if it isn't a directory try creating it
226 cfurlCurrentFile
= CFURLCreateWithFileSystemPath(
228 wxCFStringRef(*argv
),
230 false); //false == regular file
234 // Argument did not refer to
235 // an entry in the local filesystem,
236 // so try creating it through CFURLCreateWithString
237 cfurlCurrentFile
= CFURLCreateWithString(
239 wxCFStringRef(*argv
),
243 // Continue in the loop if the CFURL could not be created
244 if(!cfurlCurrentFile
)
247 wxT("wxMacLaunch Could not create CFURL for argument:%s"),
252 // Add the valid CFURL to the argument array and then
253 // release it as the CFArray adds a ref count to it
258 CFRelease(cfurlCurrentFile
); // array has retained it
261 // Create a LSLaunchURLSpec for use with LSOpenFromURLSpec
262 // Note that there are several flag options (launchFlags) such
263 // as kLSLaunchDontSwitch etc. and maybe we could be more
264 // picky about the flags we choose
265 LSLaunchURLSpec launchspec
;
266 launchspec
.appURL
= cfurlApp
;
267 launchspec
.itemURLs
= cfaFiles
;
268 launchspec
.passThruParams
= NULL
; //AEDesc*
269 launchspec
.launchFlags
= kLSLaunchDefaults
;
270 launchspec
.asyncRefCon
= NULL
;
272 // Finally, call LSOpenFromURL spec with our arguments
273 // 2nd parameter is a pointer to a CFURL that gets
274 // the actual path launched by the function
275 OSStatus status
= LSOpenFromURLSpec(&launchspec
, NULL
);
277 // Cleanup corefoundation references
282 // Check for error from LSOpenFromURLSpec
285 wxLogDebug(wxT("wxMacLaunch LSOpenFromURLSpec Error: %d"),
290 // No error from LSOpenFromURLSpec, so app was launched
294 #endif // wxOSX_USE_COCOA_OR_CARBON