-
Notifications
You must be signed in to change notification settings - Fork 1
/
NativeProcessTest.as
353 lines (284 loc) · 11.4 KB
/
NativeProcessTest.as
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
package
{
import flash.desktop.NativeApplication;
import flash.desktop.NativeProcess;
import flash.desktop.NativeProcessStartupInfo;
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.NativeWindow;
import flash.display.NativeWindowDisplayState;
import flash.display.Sprite;
import flash.display.StageAlign;
import flash.display.StageScaleMode;
import flash.events.*;
import flash.filesystem.File;
import flash.geom.Point;
import flash.geom.Rectangle;
import flash.system.Capabilities;
import flash.text.*;
import flash.utils.ByteArray;
import flash.utils.Endian;
import flash.utils.setTimeout;
[SWF(frameRate="60", backgroundColor="#ffffff")]
public class NativeProcessTest extends Sprite
{
private static const PROCESS_ENDIANNESS:String = Endian.LITTLE_ENDIAN; // .NET process uses little-endian
private static const PROCESS_PATH:String = "ScreenCapturer.exe";
private static const PROCESS_GETIMAGECOMMAND:String = "GET";
private static const PROCESS_STARTCODE:uint = 9999999;
private static const PROCESS_ENDCODE:uint = 6666666;
private var _process:NativeProcess;
private var _imageData:ByteArray;
private var _requestedWidth:int = 640;
private var _requestedHeight:int = 480;
private var _requestedOffsetX:int = 100;
private var _requestedOffsetY:int = 100;
private var _expectedImageLength:Number;
private var _imageWidth:int;
private var _imageHeight:int;
private var _isReceivingData:Boolean;
private var _startReceiveTimestamp:Number;
private var _startRequestTimestamp:Number;
private var _mouseDownMousePos:Point;
private var _mouseDownOffsetPos:Point;
private var _timestamps:Array = [];
private var _image:Bitmap;
private var _tf1:TextField;
private var _tf2:TextField;
public function NativeProcessTest()
{
this.stage.frameRate = 30;
this.stage.align = StageAlign.TOP_LEFT;
this.stage.scaleMode = StageScaleMode.NO_SCALE;
this.stage.nativeWindow.x = Capabilities.screenResolutionX - _requestedWidth - 100;
this.stage.nativeWindow.y = 50;
this.stage.nativeWindow.visible = true;
initUi();
var success:Boolean = initProcess();
if (! success) return;
setTimeout(requestImage, 333, _requestedWidth, _requestedHeight, _requestedOffsetX, _requestedOffsetY); // .. for good measure
}
private function initUi():void
{
var s:Sprite = new Sprite();
s.buttonMode = true;
this.addChild(s);
_image = new Bitmap();
s.addChild(_image);
_tf1 = new TextField();
_tf1.width = 640;
_tf1.selectable = false;
_tf1.defaultTextFormat = new TextFormat("_sans", 16, 0xffffff, true);
_tf1.htmlText = "SELECT IMAGE DIMENSIONS: <u><a href='event:640_480'>640x480</a></u> <u><a href='event:800_600'>800x600</a></u> <u><a href='event:1024_768'>1024x768</a></u> <u><a href='event:1280_1024'>1280x1024</a></u>";
_tf1.addEventListener(TextEvent.LINK, onTextLink);
this.addChild(_tf1);
_tf2 = new TextField();
_tf2.autoSize = TextFieldAutoSize.LEFT;
_tf2.defaultTextFormat = new TextFormat("_sans", 16, 0xffffff, true);
this.addChild(_tf2);
_tf1.height = _tf2.height = 25;
_tf1.background = _tf2.background = true;
_tf1.backgroundColor = _tf2.backgroundColor = 0x00;
this.stage.nativeWindow.addEventListener(NativeWindowDisplayStateEvent.DISPLAY_STATE_CHANGE, onDisplayStateChange);
this.stage.addEventListener(Event.MOUSE_LEAVE, onLeave);
this.addEventListener(MouseEvent.MOUSE_DOWN, onDragStart);
}
public function initProcess():Boolean
{
if (Capabilities.os.toLowerCase().indexOf("win") == -1) {
_tf1.htmlText = "<font size='18'>Sorry, Windows only.</font>";
return false;
}
if (! NativeProcess.isSupported) {
_tf1.htmlText = "<font size='18'>NativeProcess not supported.</font>";
return false;
}
var np:Class = NativeProcess;
var file:File = File.applicationDirectory;
file = file.resolvePath(PROCESS_PATH);
var nativeProcessStartupInfo:NativeProcessStartupInfo = new NativeProcessStartupInfo();
try {
nativeProcessStartupInfo.executable = file;
}
catch (e:Error) {
_tf1.htmlText = "<font size='18'>Couldn't launch executable:<br/>" + e.message + "</font>";
return false;
}
_process = new NativeProcess();
_process.addEventListener(ProgressEvent.STANDARD_OUTPUT_DATA, onStandardOutput);
_process.addEventListener(ProgressEvent.STANDARD_ERROR_DATA, onStandardError);
_process.addEventListener(ProgressEvent.STANDARD_INPUT_PROGRESS, onStandardInputProgress);
_process.standardOutput.endian = PROCESS_ENDIANNESS;
_process.start(nativeProcessStartupInfo);
NativeApplication.nativeApplication.addEventListener(Event.EXITING, onAppExiting);
_process.running
return true;
}
//
private function onDisplayStateChange($e:NativeWindowDisplayStateEvent):void
{
if ($e.afterDisplayState == NativeWindowDisplayState.NORMAL)
requestImage(_requestedWidth, _requestedHeight, _requestedOffsetX, _requestedOffsetY);
}
private function onLeave(e:*):void
{
_tf1.visible = false;
this.stage.addEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);
}
private function onMouseMove(e:*):void
{
_tf1.visible = true;
this.stage.removeEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);
}
private function onTextLink($e:TextEvent):void
{
trace("onTextLink");
var a:Array = $e.text.split("_");
_requestedWidth = parseInt(a[0]);
_requestedHeight = parseInt(a[1]);
}
private function onDragStart(e:*):void
{
_mouseDownMousePos = new Point(this.mouseX, this.mouseY);
_mouseDownOffsetPos = new Point(_requestedOffsetX, _requestedOffsetY);
this.addEventListener(Event.ENTER_FRAME, onDragging);
this.stage.addEventListener(MouseEvent.MOUSE_UP, onDragEnd);
this.stage.addEventListener(Event.MOUSE_LEAVE, onDragEnd);
}
private function onDragging(e:*):void
{
var dx:int = this.mouseX - _mouseDownMousePos.x;
var dy:int = this.mouseY - _mouseDownMousePos.y;
_requestedOffsetX = _mouseDownOffsetPos.x - dx;
_requestedOffsetY = _mouseDownOffsetPos.y - dy;
}
private function onDragEnd(e:*):void
{
this.removeEventListener(Event.ENTER_FRAME, onDragging);
this.stage.removeEventListener(MouseEvent.MOUSE_UP, onDragEnd);
this.stage.removeEventListener(Event.MOUSE_LEAVE, onDragEnd);
}
private function onStandardInputProgress(event:ProgressEvent):void
{
// Do nothing
// _process.closeInput();
}
private function onStandardOutput(event:ProgressEvent):void
{
processStandardOut();
}
public function onStandardError(event:ProgressEvent):void
{
trace("Error: " + _process.standardError.readUTFBytes(_process.standardError.bytesAvailable));
}
private function onAppExiting(e:*):void
{
if (_process && _process.running)
{
_process.removeEventListener(ProgressEvent.STANDARD_OUTPUT_DATA, onStandardOutput);
_process.removeEventListener(ProgressEvent.STANDARD_ERROR_DATA, onStandardError);
_process.removeEventListener(ProgressEvent.STANDARD_INPUT_PROGRESS, onStandardInputProgress);
_process.exit(true);
}
}
//
private function requestImage($width:int, $height:int, $offsetX:int, $offsetY:int):Boolean
{
if (! _process || ! _process.running) return false;
if (_isReceivingData) {
trace('Skipping - already trying to receive an image');
return false;
}
benchmarkCalc();
// bounds check
_requestedOffsetX = Math.min(_requestedOffsetX, Capabilities.screenResolutionX - _requestedWidth);
_requestedOffsetY = Math.min(_requestedOffsetY, Capabilities.screenResolutionY - _requestedHeight);
_requestedOffsetX = Math.max(_requestedOffsetX, 0);
_requestedOffsetY = Math.max(_requestedOffsetY, 0);
var s:String = PROCESS_GETIMAGECOMMAND + " " +
_requestedWidth.toString() + " " +
_requestedHeight.toString() + " " +
_requestedOffsetX.toString() + " " +
_requestedOffsetY.toString() + "\n";
_process.standardInput.writeUTFBytes(s);
_startRequestTimestamp = new Date().getTime();
_isReceivingData = true;
return true;
}
private function processStandardOut():void
{
// Check first 4 bytes for start code
var first4:int = _process.standardOutput.readUnsignedInt();
if (first4 == PROCESS_STARTCODE)
{
// error check
if (_process.standardOutput.bytesAvailable < 8) {
trace('Header appears to be split between packets. Ignoring rather than writing extra logic.');
return;
}
_startReceiveTimestamp = new Date().getTime();
// trace('Time waiting for response: ', _startReceiveTimestamp - _startRequestTimestamp);
_imageWidth = _process.standardOutput.readInt();
_imageHeight = _process.standardOutput.readInt();
_expectedImageLength = _imageWidth * _imageHeight * 4;
_imageData = new ByteArray();
_imageData.endian = Endian.LITTLE_ENDIAN;
if (_process.standardOutput.bytesAvailable == 0) return; // (expected behavior)
}
else // ... that first longword is part of the real image data, so write it to our ByteArray
{
_imageData.writeInt(first4);
}
// Write the remainder of the stdout data to the ByteArray
_process.standardOutput.readBytes(_imageData, _imageData.length);
// Check last 4 bytes for termination code
_imageData.position = _imageData.length - 4;
var last4:int = _imageData.readUnsignedInt();
if (last4 == PROCESS_ENDCODE)
{
_imageData.length = _imageData.length - 4; // .. remove endcode from bytearray
_isReceivingData = false;
if (_imageData.length != _expectedImageLength) trace("Image not of expected length: ", _imageData.length, "versus", _expectedImageLength);
// trace('Time taken to transfer data: ', new Date().getTime() - _startReceiveTimestamp);
drawImage(_imageData);
}
}
private function drawImage($b:ByteArray):void
{
// Make new bitmapdata if necessary
if (! _image.bitmapData || _image.bitmapData.width != _imageWidth || _image.bitmapData.height != _imageHeight) {
if (_image.bitmapData) _image.bitmapData.dispose();
_image.bitmapData = new BitmapData(_imageWidth,_imageHeight,false);
}
// Apply image
$b.position = 0;
_image.bitmapData.setPixels(new Rectangle(0,0,_imageWidth,_imageHeight), $b);
// *** This is where you might use or manipulate the BitmapData to have the app do something actually useful...
// If window is minimized, return
if (this.stage.nativeWindow.displayState == NativeWindowDisplayState.MINIMIZED) return;
// Resize window if necessary
var px:Number = this.stage.nativeWindow.width - this.stage.stageWidth;
var py:Number = this.stage.nativeWindow.height - this.stage.stageHeight;
if (this.stage.nativeWindow.width != _imageWidth + px) {
this.stage.nativeWindow.width = _imageWidth + px;
_tf1.width = _imageWidth;
_tf2.x = _imageWidth - 58;
}
if (this.stage.nativeWindow.height != _imageHeight + py) {
this.stage.nativeWindow.height = _imageHeight + py;
}
// Immediately request new image
requestImage(_requestedWidth, _requestedHeight, _requestedOffsetX, _requestedOffsetY);
}
private function benchmarkCalc():void
{
var now:Number = new Date().getTime();
_timestamps.push(now);
if (_timestamps.length > 10) {
var msPerFrame:Number = (now - _timestamps.shift()) / 10;
var framesPerSecond:Number = 1000 / msPerFrame;
_tf2.text = Math.round(framesPerSecond).toString() + " FPS";
}
}
}
}