]> git.saurik.com Git - wxWidgets.git/blobdiff - src/mac/corefoundation/utilsexc_cf.cpp
don't create non-existing groups in HasEntry()
[wxWidgets.git] / src / mac / corefoundation / utilsexc_cf.cpp
index bb6e19d6ed6634495c95295aa8363abe827b6e94..5eadfff5706dff5dc58c01d905d325417daeebc7 100644 (file)
 #include "wx/unix/execute.h"
 #include "wx/stdpaths.h"
 #include "wx/apptrait.h"
+#include "wx/thread.h"
+#include "wx/process.h"
 
-#ifdef __WXCOCOA__
-#include <CoreFoundation/CoreFoundation.h>
-#include <ApplicationServices/ApplicationServices.h>
+// Use polling instead of Mach ports, which doesn't work on Intel
+// due to task_for_pid security issues.
+
+// What's a better test for Intel vs PPC?
+#ifdef WORDS_BIGENDIAN
+#define USE_POLLING 0
 #else
-#include "wx/mac/private.h"
-#include "LaunchServices.h"
+#define USE_POLLING 1
 #endif
 
-#include "wx/uri.h"
-#include "wx/mac/corefoundation/cfstring.h"
+#if USE_POLLING
 
-long wxMacExecute(wxChar **argv,
-               int flags,
-               wxProcess *process)
+class wxProcessTerminationEventHandler: public wxEvtHandler
 {
-    CFIndex cfiCount = 0;
-    //get count
-    for(wxChar** argvcopy = argv; *argvcopy != NULL ; ++argvcopy)
+  public:
+    wxProcessTerminationEventHandler(wxEndProcessData* data)
     {
-        ++cfiCount;
+        m_data = data;
+        Connect(-1, wxEVT_END_PROCESS, wxProcessEventHandler(wxProcessTerminationEventHandler::OnTerminate));
     }
 
-    if(cfiCount == 0) //no file to launch?
+    void OnTerminate(wxProcessEvent& event)
     {
-        wxLogDebug(wxT("wxMacExecute No file to launch!"));
-        return -1;
+        Disconnect(-1, wxEVT_END_PROCESS, wxProcessEventHandler(wxProcessTerminationEventHandler::OnTerminate));
+        wxHandleProcessTermination(m_data);
+        delete this;
     }
-    
-    CFURLRef cfurlApp = CFURLCreateWithString(
-            kCFAllocatorDefault,
-            wxMacCFStringHolder(*argv++, wxLocale::GetSystemEncoding()),
-            NULL);
-    wxASSERT(cfurlApp);
-
-    CFBundleRef cfbApp = CFBundleCreate(kCFAllocatorDefault, cfurlApp);
-    if(!cfbApp)
-    {
-        wxLogDebug(wxT("wxMacExecute Bad bundle"));
-        CFRelease(cfurlApp);
-        return -1;
-    }
-    
-    
-    UInt32 dwBundleType, dwBundleCreator;
-    CFBundleGetPackageInfo(cfbApp, &dwBundleType, &dwBundleCreator);
 
-    //Only call wxMacExecute for .app bundles - others could be actual unix programs
-    if(dwBundleType != 'APPL')
+    wxEndProcessData* m_data;
+};
+
+class wxProcessTerminationThread: public wxThread
+{
+  public:
+    wxProcessTerminationThread(wxEndProcessData* data, wxProcessTerminationEventHandler* handler): wxThread(wxTHREAD_DETACHED)
     {
-        CFRelease(cfurlApp);
-        return -1;
+        m_data = data;
+        m_handler = handler;
     }
-    
-    //
-    // We have a good bundle - so let's launch it!
-    //
-    
-    CFMutableArrayRef cfaFiles = CFArrayCreateMutable(kCFAllocatorDefault, cfiCount - 1, NULL);
-            
-    wxASSERT(cfaFiles);
-    
-    if(--cfiCount)
+
+    virtual void* Entry();
+
+    wxProcessTerminationEventHandler* m_handler;
+    wxEndProcessData* m_data;
+};
+
+// The problem with this is that we may be examining the
+// process e.g. in OnIdle at the point this cleans up the process,
+// so we need to delay until it's safe.
+
+void* wxProcessTerminationThread::Entry()
+{
+    while (true)
     {
-        for( ; *argv != NULL ; ++argv)
+        usleep(100);
+        int status = 0;
+        int rc = waitpid(abs(m_data->pid), & status, WNOHANG);
+        if (rc != 0)
         {
-//            wxLogDebug(*argv);
-            wxString sCurrentFile;
-            
-            if(wxURI(*argv).IsReference())
-                sCurrentFile = wxString(wxT("file://")) + *argv;
+            if ((rc != -1) && WIFEXITED(status))
+                m_data->exitcode = WEXITSTATUS(status);
             else
-                sCurrentFile = *argv;
-                
-            CFURLRef cfurlCurrentFile =   CFURLCreateWithString(
-                    kCFAllocatorDefault,
-                    wxMacCFStringHolder(sCurrentFile, wxLocale::GetSystemEncoding()),
-                    NULL);
-            wxASSERT(cfurlCurrentFile);
-
-            CFArrayAppendValue(
-                cfaFiles,
-                cfurlCurrentFile
-                            );
+                m_data->exitcode = -1;
+
+            wxProcessEvent event;
+            wxPostEvent(m_handler, event);
+
+            break;
         }
     }
     
-    LSLaunchURLSpec launchspec;
-    launchspec.appURL = cfurlApp;
-    launchspec.itemURLs = cfaFiles;
-    launchspec.passThruParams = NULL; //AEDesc* 
-    launchspec.launchFlags = kLSLaunchDefaults | kLSLaunchDontSwitch;  //TODO:  Possibly be smarter with flags
-    launchspec.asyncRefCon = NULL;
-    
-    OSStatus status = LSOpenFromURLSpec(&launchspec,
-                        NULL); //2nd is CFURLRef* really launched
+    return NULL;
+}
+
+int wxAddProcessCallbackForPid(wxEndProcessData *proc_data, int pid)
+{
+    if (pid < 1)
+        return -1;
 
-    //cleanup
-    CFRelease(cfurlApp);
-    CFRelease(cfaFiles);
+    wxProcessTerminationEventHandler* handler = new wxProcessTerminationEventHandler(proc_data);    
+    wxProcessTerminationThread* thread = new wxProcessTerminationThread(proc_data, handler);
     
-    //check for error
-    if(status != noErr)
+    if (thread->Create() != wxTHREAD_NO_ERROR)
     {
-        wxLogDebug(wxString::Format(wxT("wxMacExecute ERROR: %d")), (int)status);
+        wxLogDebug(wxT("Could not create termination detection thread."));
+        delete thread;
+        delete handler;
         return -1;
     }
-    return 0; //success
+
+    thread->Run();
+    
+    return 0;
 }
 
+#else
 
 #include <CoreFoundation/CFMachPort.h>
 #include <sys/wait.h>
@@ -221,6 +210,9 @@ int wxAddProcessCallbackForPid(wxEndProcessData *proc_data, int pid)
     return 0;
 }
 
+#endif
+  // USE_POLLING
+
 // NOTE: This doens't really belong here but this was a handy file to
 // put it in because it's already compiled for wxCocoa and wxMac GUI lib.
 static wxStandardPathsCF gs_stdPaths;