]> git.saurik.com Git - wxWidgets.git/blob - src/cocoa/utilsexc.mm
No changes, just move mouse capturing bookkeeping data out of wxWindow.
[wxWidgets.git] / src / cocoa / utilsexc.mm
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/cocoa/utilsexec.mm
3 // Purpose: Execution-related utilities for wxCocoa
4 // Author: Ryan Norton
5 // Modified by:
6 // Created: 2004-10-05
7 // Copyright: (c) Ryan Norton
8 // Licence: wxWindows licence
9 // Notes: This code may be useful on platforms other than Darwin.
10 // On Darwin we share the CoreFoundation code with wxMac.
11 /////////////////////////////////////////////////////////////////////////////
12
13 #include "wx/wxprec.h"
14
15 #ifndef WX_PRECOMP
16 #include "wx/utils.h"
17 #endif
18
19 #if 0
20
21 #ifndef WX_PRECOMP
22 #if wxUSE_STREAMS
23 #include "wx/stream.h"
24 #endif // wxUSE_STREAMS
25 #endif //WX_PRECOMP
26
27 #include "wx/process.h"
28
29 #include "wx/cocoa/string.h"
30
31 #include "wx/cocoa/objc/objc_uniquifying.h"
32
33 #import <Foundation/Foundation.h>
34 #import <AppKit/NSWorkspace.h>
35
36 //
37 // RN: This is a prelimenary implementation - simple
38 // launching and process redirection works,
39 // but with the piping tests in the exec sample
40 // SIGPIPE is triggered...
41 //
42
43 class wxPipeInputStream : public wxInputStream
44 {
45 public:
46 wxPipeInputStream(NSPipe* thePipe) :
47 m_thePipe(thePipe),
48 m_theHandle([m_thePipe fileHandleForReading])
49 {
50 }
51
52 ~wxPipeInputStream()
53 {
54 [m_thePipe release];
55 }
56
57 protected:
58 virtual size_t OnSysRead(void *buffer, size_t size)
59 {
60 NSData* theData = [m_theHandle readDataOfLength:size];
61 memcpy(buffer, [theData bytes], [theData length]);
62 return [theData length];
63 }
64
65
66 NSPipe* m_thePipe;
67 NSFileHandle* m_theHandle;
68 };
69
70 class wxPipeOutputStream : public wxOutputStream
71 {
72 public:
73 wxPipeOutputStream(NSPipe* thePipe) :
74 m_thePipe(thePipe),
75 m_theHandle([m_thePipe fileHandleForWriting])
76 {
77 }
78
79 ~wxPipeOutputStream()
80 {
81 [m_thePipe release];
82 }
83
84 protected:
85
86 virtual size_t OnSysWrite(const void *buffer, size_t bufsize)
87 {
88 NSData* theData = [NSData dataWithBytesNoCopy:(void*)buffer
89 length:bufsize];
90 [m_theHandle writeData:theData];
91 return bufsize;
92 }
93
94 NSPipe* m_thePipe;
95 NSFileHandle* m_theHandle;
96 };
97
98 @interface wxTaskHandler : NSObject
99 {
100 long m_pid;
101 void* m_handle;
102 }
103 -(id)init:(void*)handle processIdentifier:(long)pid;
104 - (void)termHandler:(NSNotification *)aNotification;
105 @end
106 WX_DECLARE_GET_OBJC_CLASS(wxTaskHandler,NSObject)
107
108 @implementation wxTaskHandler : NSObject
109
110 -(id)init:(void*)handle processIdentifier:(long)pid
111 {
112 self = [super init];
113
114 m_handle = handle;
115 m_pid = pid;
116
117 [[NSNotificationCenter defaultCenter] addObserver:self
118 selector:@selector(termHandler:)
119 name:NSTaskDidTerminateNotification
120 object:nil];
121 return self;
122 }
123
124 - (void)termHandler:(NSNotification *)aNotification
125 {
126 NSTask* theTask = [aNotification object];
127
128 if ([theTask processIdentifier] == m_pid)
129 {
130 ((wxProcess*)m_handle)->OnTerminate([theTask processIdentifier],
131 [theTask terminationStatus]);
132
133 [self release];
134 }
135 }
136
137 @end
138 WX_IMPLEMENT_GET_OBJC_CLASS(wxTaskHandler,NSObject)
139
140 long wxExecute(const wxString& command,
141 int sync,
142 wxProcess *handle,
143 const wxExecuteEnv *env)
144 {
145 NSTask* theTask = [[NSTask alloc] init];
146
147 if (handle && handle->IsRedirected())
148 {
149 NSPipe* theStdinPipe = [[NSPipe alloc] init];
150 NSPipe* theStderrPipe = [[NSPipe alloc] init];
151 NSPipe* theStdoutPipe = [[NSPipe alloc] init];
152
153 [theTask setStandardInput:theStdinPipe];
154 [theTask setStandardError:theStderrPipe];
155 [theTask setStandardOutput:theStdoutPipe];
156
157 handle->SetPipeStreams(new wxPipeInputStream(theStdoutPipe),
158 new wxPipeOutputStream(theStdinPipe),
159 new wxPipeInputStream(theStderrPipe) );
160 }
161
162 NSArray* theQuoteArguments =
163 [wxNSStringWithWxString(command) componentsSeparatedByString:@"\""];
164
165 NSMutableArray* theSeparatedArguments =
166 [NSMutableArray arrayWithCapacity:10];
167
168 for (unsigned i = 0; i < [theQuoteArguments count]; ++i)
169 {
170 [theSeparatedArguments addObjectsFromArray:
171 [[theQuoteArguments objectAtIndex:i] componentsSeparatedByString:@" "]
172 ];
173
174 if(++i < [theQuoteArguments count])
175 [theSeparatedArguments addObject:[theQuoteArguments objectAtIndex:i]];
176 }
177
178 [theTask setLaunchPath:[theSeparatedArguments objectAtIndex:0]];
179 [theTask setArguments:theSeparatedArguments];
180 [theTask launch];
181
182 if(sync & wxEXEC_ASYNC)
183 {
184 [[WX_GET_OBJC_CLASS(wxTaskHandler) alloc]init:handle
185 processIdentifier:[theTask processIdentifier]];
186
187 return 0;
188 }
189 else
190 {
191 [theTask waitUntilExit];
192
193 return [theTask terminationStatus];
194 }
195 }
196 #endif //0