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 extern bool WXDLLEXPORT
wxIsDebuggerRunning()
59 // TODO : try to find out ...
63 #if ( !wxUSE_GUI && !wxOSX_USE_IPHONE ) || 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
)
80 // This returns 10 and 6 for OS X 10.6, consistent with behaviour on
83 Gestalt(gestaltSystemVersionMajor
, &maj
);
84 Gestalt(gestaltSystemVersionMinor
, &min
);
86 if ( majorVsn
!= NULL
)
89 if ( minorVsn
!= NULL
)
94 Gestalt(gestaltSystemVersion
, &theSystem
);
96 if ( majorVsn
!= NULL
)
97 *majorVsn
= (theSystem
>> 8);
99 if ( minorVsn
!= NULL
)
100 *minorVsn
= (theSystem
& 0xFF);
102 return wxOS_MAC_OSX_DARWIN
;
105 #include <sys/utsname.h>
107 wxString
wxGetOsDescription()
111 return wxString::Format(wxT("Mac OS X (%s %s %s)"),
112 wxString::FromAscii(name
.sysname
).c_str(),
113 wxString::FromAscii(name
.release
).c_str(),
114 wxString::FromAscii(name
.machine
).c_str());
117 //===========================================================================
119 //===========================================================================
121 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
125 // argv is the command line split up, with the application path first
126 // flags are the flags from wxExecute
127 // process is the process passed from wxExecute for pipe streams etc.
128 // returns -1 on error for wxEXEC_SYNC and 0 on error for wxEXEC_ASYNC
129 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
130 bool wxMacLaunch(char **argv
)
132 // Obtains the number of arguments for determining the size of
133 // the CFArray used to hold them
134 CFIndex cfiCount
= 0;
135 for(char** argvcopy
= argv
; *argvcopy
!= NULL
; ++argvcopy
)
140 // If there is not a single argument then there is no application
144 wxLogDebug(wxT("wxMacLaunch No file to launch!"));
149 wxString path
= *argv
++;
151 // Create a CFURL for the application path
152 // Created this way because we are opening a bundle which is a directory
154 CFURLCreateWithFileSystemPath(
158 true); //false == not a directory
160 // Check for error from the CFURL
163 wxLogDebug(wxT("wxMacLaunch Can't open path: %s"), path
.c_str());
167 // Create a CFBundle from the CFURL created earlier
168 CFBundleRef cfbApp
= CFBundleCreate(kCFAllocatorDefault
, cfurlApp
);
170 // Check to see if CFBundleCreate returned an error,
171 // and if it did this was an invalid bundle or not a bundle
172 // at all (maybe a simple directory etc.)
175 wxLogDebug(wxT("wxMacLaunch Bad bundle: %s"), path
.c_str());
180 // Get the bundle type and make sure its an 'APPL' bundle
181 // Otherwise we're dealing with something else here...
182 UInt32 dwBundleType
, dwBundleCreator
;
183 CFBundleGetPackageInfo(cfbApp
, &dwBundleType
, &dwBundleCreator
);
184 if(dwBundleType
!= 'APPL')
186 wxLogDebug(wxT("wxMacLaunch Not an APPL bundle: %s"), path
.c_str());
192 // Create a CFArray for dealing with the command line
193 // arguments to the bundle
194 CFMutableArrayRef cfaFiles
= CFArrayCreateMutable(kCFAllocatorDefault
,
195 cfiCount
-1, &kCFTypeArrayCallBacks
);
196 if(!cfaFiles
) //This should never happen
198 wxLogDebug(wxT("wxMacLaunch Could not create CFMutableArray"));
204 // Loop through command line arguments to the bundle,
205 // turn them into CFURLs and then put them in cfaFiles
206 // For use to launch services call
207 for( ; *argv
!= NULL
; ++argv
)
209 // Check for '<' as this will ring true for
210 // CFURLCreateWithString but is generally not considered
211 // typical on mac but is usually passed here from wxExecute
212 if (wxStrcmp(*argv
, wxT("<")) == 0)
216 CFURLRef cfurlCurrentFile
; // CFURL to hold file path
217 wxFileName
argfn(*argv
); // Filename for path
219 if(argfn
.DirExists())
221 // First, try creating as a directory
222 cfurlCurrentFile
= CFURLCreateWithFileSystemPath(
224 wxCFStringRef(*argv
),
226 true); //true == directory
228 else if(argfn
.FileExists())
230 // And if it isn't a directory try creating it
232 cfurlCurrentFile
= CFURLCreateWithFileSystemPath(
234 wxCFStringRef(*argv
),
236 false); //false == regular file
240 // Argument did not refer to
241 // an entry in the local filesystem,
242 // so try creating it through CFURLCreateWithString
243 cfurlCurrentFile
= CFURLCreateWithString(
245 wxCFStringRef(*argv
),
249 // Continue in the loop if the CFURL could not be created
250 if(!cfurlCurrentFile
)
253 wxT("wxMacLaunch Could not create CFURL for argument:%s"),
258 // Add the valid CFURL to the argument array and then
259 // release it as the CFArray adds a ref count to it
264 CFRelease(cfurlCurrentFile
); // array has retained it
267 // Create a LSLaunchURLSpec for use with LSOpenFromURLSpec
268 // Note that there are several flag options (launchFlags) such
269 // as kLSLaunchDontSwitch etc. and maybe we could be more
270 // picky about the flags we choose
271 LSLaunchURLSpec launchspec
;
272 launchspec
.appURL
= cfurlApp
;
273 launchspec
.itemURLs
= cfaFiles
;
274 launchspec
.passThruParams
= NULL
; //AEDesc*
275 launchspec
.launchFlags
= kLSLaunchDefaults
;
276 launchspec
.asyncRefCon
= NULL
;
278 // Finally, call LSOpenFromURL spec with our arguments
279 // 2nd parameter is a pointer to a CFURL that gets
280 // the actual path launched by the function
281 OSStatus status
= LSOpenFromURLSpec(&launchspec
, NULL
);
283 // Cleanup corefoundation references
288 // Check for error from LSOpenFromURLSpec
291 wxLogDebug(wxT("wxMacLaunch LSOpenFromURLSpec Error: %d"),
296 // No error from LSOpenFromURLSpec, so app was launched
300 #endif // wxOSX_USE_COCOA_OR_CARBON