]> git.saurik.com Git - wxWidgets.git/blame_incremental - src/mac/corefoundation/utilsexc_cf.cpp
added wxWindow::IsVisible() method
[wxWidgets.git] / src / mac / corefoundation / utilsexc_cf.cpp
... / ...
CommitLineData
1/////////////////////////////////////////////////////////////////////////////
2// Name: src/mac/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/mac/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/apptrait.h"
21#include "wx/thread.h"
22#include "wx/process.h"
23
24#include <sys/wait.h>
25
26// Use polling instead of Mach ports, which doesn't work on Intel
27// due to task_for_pid security issues.
28
29// What's a better test for Intel vs PPC?
30#ifdef WORDS_BIGENDIAN
31#define USE_POLLING 0
32#else
33#define USE_POLLING 1
34#endif
35
36#if USE_POLLING
37
38class wxProcessTerminationEventHandler: public wxEvtHandler
39{
40 public:
41 wxProcessTerminationEventHandler(wxEndProcessData* data)
42 {
43 m_data = data;
44 Connect(-1, wxEVT_END_PROCESS, wxProcessEventHandler(wxProcessTerminationEventHandler::OnTerminate));
45 }
46
47 void OnTerminate(wxProcessEvent& event)
48 {
49 Disconnect(-1, wxEVT_END_PROCESS, wxProcessEventHandler(wxProcessTerminationEventHandler::OnTerminate));
50 wxHandleProcessTermination(m_data);
51 delete this;
52 }
53
54 wxEndProcessData* m_data;
55};
56
57class wxProcessTerminationThread: public wxThread
58{
59 public:
60 wxProcessTerminationThread(wxEndProcessData* data, wxProcessTerminationEventHandler* handler): wxThread(wxTHREAD_DETACHED)
61 {
62 m_data = data;
63 m_handler = handler;
64 }
65
66 virtual void* Entry();
67
68 wxProcessTerminationEventHandler* m_handler;
69 wxEndProcessData* m_data;
70};
71
72// The problem with this is that we may be examining the
73// process e.g. in OnIdle at the point this cleans up the process,
74// so we need to delay until it's safe.
75
76void* wxProcessTerminationThread::Entry()
77{
78 while (true)
79 {
80 usleep(100);
81 int status = 0;
82 int rc = waitpid(abs(m_data->pid), & status, WNOHANG);
83 if (rc != 0)
84 {
85 if ((rc != -1) && WIFEXITED(status))
86 m_data->exitcode = WEXITSTATUS(status);
87 else
88 m_data->exitcode = -1;
89
90 wxProcessEvent event;
91 wxPostEvent(m_handler, event);
92
93 break;
94 }
95 }
96
97 return NULL;
98}
99
100int wxAddProcessCallbackForPid(wxEndProcessData *proc_data, int pid)
101{
102 if (pid < 1)
103 return -1;
104
105 wxProcessTerminationEventHandler* handler = new wxProcessTerminationEventHandler(proc_data);
106 wxProcessTerminationThread* thread = new wxProcessTerminationThread(proc_data, handler);
107
108 if (thread->Create() != wxTHREAD_NO_ERROR)
109 {
110 wxLogDebug(wxT("Could not create termination detection thread."));
111 delete thread;
112 delete handler;
113 return -1;
114 }
115
116 thread->Run();
117
118 return 0;
119}
120
121#else // !USE_POLLING
122
123#include <CoreFoundation/CFMachPort.h>
124extern "C" {
125#include <mach/mach.h>
126}
127
128void wxMAC_MachPortEndProcessDetect(CFMachPortRef port, void *data)
129{
130 wxEndProcessData *proc_data = (wxEndProcessData*)data;
131 wxLogDebug(wxT("Process ended"));
132 int status = 0;
133 int rc = waitpid(abs(proc_data->pid), &status, WNOHANG);
134 if(!rc)
135 {
136 wxLogDebug(wxT("Mach port was invalidated, but process hasn't terminated!"));
137 return;
138 }
139 if((rc != -1) && WIFEXITED(status))
140 proc_data->exitcode = WEXITSTATUS(status);
141 else
142 proc_data->exitcode = -1;
143 wxHandleProcessTermination(proc_data);
144}
145
146int wxAddProcessCallbackForPid(wxEndProcessData *proc_data, int pid)
147{
148 if(pid < 1)
149 return -1;
150 kern_return_t kernResult;
151 mach_port_t taskOfOurProcess;
152 mach_port_t machPortForProcess;
153 taskOfOurProcess = mach_task_self();
154 if(taskOfOurProcess == MACH_PORT_NULL)
155 {
156 wxLogDebug(wxT("No mach_task_self()"));
157 return -1;
158 }
159 wxLogDebug(wxT("pid=%d"),pid);
160 kernResult = task_for_pid(taskOfOurProcess,pid, &machPortForProcess);
161 if(kernResult != KERN_SUCCESS)
162 {
163 wxLogDebug(wxT("no task_for_pid()"));
164 // try seeing if it is already dead or something
165 // FIXME: a better method would be to call the callback function
166 // from idle time until the process terminates. Of course, how
167 // likely is it that it will take more than 0.1 seconds for the
168 // mach terminate event to make its way to the BSD subsystem?
169 usleep(100); // sleep for 0.1 seconds
170 wxMAC_MachPortEndProcessDetect(NULL, (void*)proc_data);
171 return -1;
172 }
173 CFMachPortContext termcb_contextinfo;
174 termcb_contextinfo.version = 0;
175 termcb_contextinfo.info = (void*)proc_data;
176 termcb_contextinfo.retain = NULL;
177 termcb_contextinfo.release = NULL;
178 termcb_contextinfo.copyDescription = NULL;
179 CFMachPortRef CFMachPortForProcess;
180 Boolean ShouldFreePort;
181 CFMachPortForProcess = CFMachPortCreateWithPort(NULL, machPortForProcess, NULL, &termcb_contextinfo, &ShouldFreePort);
182 if(!CFMachPortForProcess)
183 {
184 wxLogDebug(wxT("No CFMachPortForProcess"));
185 mach_port_deallocate(taskOfOurProcess, machPortForProcess);
186 return -1;
187 }
188 if(ShouldFreePort)
189 {
190 kernResult = mach_port_deallocate(taskOfOurProcess, machPortForProcess);
191 if(kernResult!=KERN_SUCCESS)
192 {
193 wxLogDebug(wxT("Couldn't deallocate mach port"));
194 return -1;
195 }
196 }
197 CFMachPortSetInvalidationCallBack(CFMachPortForProcess, &wxMAC_MachPortEndProcessDetect);
198 CFRunLoopSourceRef runloopsource;
199 runloopsource = CFMachPortCreateRunLoopSource(NULL,CFMachPortForProcess, (CFIndex)0);
200 if(!runloopsource)
201 {
202 wxLogDebug(wxT("Couldn't create runloopsource"));
203 return -1;
204 }
205
206 CFRelease(CFMachPortForProcess);
207
208 CFRunLoopAddSource(CFRunLoopGetCurrent(),runloopsource,kCFRunLoopDefaultMode);
209 CFRelease(runloopsource);
210 wxLogDebug(wxT("Successfully added notification to the runloop"));
211 return 0;
212}
213
214#endif // USE_POLLING/!USE_POLLING
215
216// NOTE: This doesn't really belong here but this was a handy file to
217// put it in because it's already compiled for wxCocoa and wxMac GUI lib.
218#if wxUSE_GUI
219
220static wxStandardPathsCF gs_stdPaths;
221wxStandardPathsBase& wxGUIAppTraits::GetStandardPaths()
222{
223 return gs_stdPaths;
224}
225
226#endif // wxUSE_GUI
227