Console.as 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. /*
  2. ** ADOBE SYSTEMS INCORPORATED
  3. ** Copyright 2012 Adobe Systems Incorporated. All Rights Reserved.
  4. **
  5. ** NOTICE: Adobe permits you to use, modify, and distribute this file in
  6. ** accordance with the terms of the Adobe license agreement accompanying it.
  7. ** If you have received this file from a source other than Adobe, then your use,
  8. ** modification, or distribution of it requires the prior written permission of Adobe.
  9. */
  10. package com.adobe.flascc
  11. {
  12. import flash.display.DisplayObjectContainer;
  13. import flash.display.Sprite;
  14. import flash.display.StageScaleMode;
  15. import flash.events.Event;
  16. import flash.net.LocalConnection;
  17. import flash.net.URLRequest;
  18. import flash.text.TextField;
  19. import flash.utils.ByteArray;
  20. import flash.display.*;
  21. import flash.display3D.*;
  22. import flash.events.*;
  23. import GLS3D.*;
  24. import com.adobe.flascc.vfs.ISpecialFile;
  25. /**
  26. * A basic implementation of a console for FlasCC apps.
  27. * The PlayerKernel class delegates to this for things like read/write,
  28. * so that console output can be displayed in a TextField on the Stage.
  29. */
  30. public class Console extends Sprite implements ISpecialFile
  31. {
  32. private var enableConsole:Boolean = true;
  33. private var _tf:TextField
  34. private var inputContainer:DisplayObjectContainer
  35. /**
  36. * To Support the preloader case you might want to have the Console
  37. * act as a child of some other DisplayObjectContainer.
  38. */
  39. public function Console(container:DisplayObjectContainer = null)
  40. {
  41. CModule.rootSprite = container ? container.root : this
  42. if(CModule.runningAsWorker()) {
  43. return;
  44. }
  45. if(container) {
  46. container.addChild(this)
  47. init(null)
  48. } else {
  49. addEventListener(Event.ADDED_TO_STAGE, init)
  50. }
  51. }
  52. protected function context_error(e:Event):void {
  53. trace("Context error!");
  54. };
  55. protected function context_created(e:Event):void {
  56. var s3d:Stage3D = stage.stage3Ds[0];
  57. var ctx3d:Context3D = s3d.context3D;
  58. ctx3d.configureBackBuffer(stage.stageWidth, stage.stageHeight, 2, true /*enableDepthAndStencil*/ );
  59. trace("Stage3D context: " + ctx3d.driverInfo);
  60. GLAPI.init(ctx3d, null, stage);
  61. GLAPI.instance.context.clear(0.0, 0.0, 0.0);
  62. GLAPI.instance.context.present();
  63. // change to false to prevent running main in the background
  64. // when Workers are supported
  65. const runMainBg:Boolean = false
  66. try
  67. {
  68. // PlayerKernel will delegate read/write requests to the "/dev/tty"
  69. // file to the object specified with this API.
  70. CModule.vfs.console = this
  71. // By default we run "main" on a background worker so that
  72. // console updates show up in real time. Otherwise "startAsync"
  73. // causes main to run on the UI worker
  74. if(runMainBg && CModule.canUseWorkers) // start in bg if we have workers
  75. CModule.startBackground(this, new <String>[], new <String>[])
  76. else
  77. CModule.startAsync(this)
  78. }
  79. catch(e:*)
  80. {
  81. // If main gives any exceptions make sure we get a full stack trace
  82. // in our console
  83. consoleWrite(e.toString() + "\n" + e.getStackTrace().toString())
  84. throw e
  85. }
  86. };
  87. /**
  88. * All of the real FlasCC init happens in this method,
  89. * which is either run on startup or once the SWF has
  90. * been added to the stage.
  91. */
  92. protected function init(e:Event):void
  93. {
  94. inputContainer = new Sprite()
  95. addChild(inputContainer)
  96. addEventListener(Event.ENTER_FRAME, enterFrame)
  97. stage.frameRate = 60
  98. stage.scaleMode = StageScaleMode.NO_SCALE
  99. if(enableConsole) {
  100. _tf = new TextField
  101. _tf.multiline = true
  102. _tf.width = stage.stageWidth
  103. _tf.height = stage.stageHeight
  104. inputContainer.addChild(_tf)
  105. }
  106. var s3d:Stage3D = stage.stage3Ds[0];
  107. s3d.addEventListener(Event.CONTEXT3D_CREATE, context_created);
  108. s3d.addEventListener(ErrorEvent.ERROR, context_error);
  109. s3d.requestContext3D(Context3DRenderMode.AUTO);
  110. }
  111. /**
  112. * The callback to call when FlasCC code calls the <code>posix exit()</code> function. Leave null to exit silently.
  113. * @private
  114. */
  115. public var exitHook:Function;
  116. /**
  117. * The PlayerKernel implementation will use this function to handle
  118. * C process exit requests
  119. */
  120. public function exit(code:int):Boolean
  121. {
  122. // default to unhandled
  123. return exitHook ? exitHook(code) : false;
  124. }
  125. /**
  126. * The PlayerKernel implementation uses this function to handle
  127. * C IO write requests to the file "/dev/tty" (for example, output from
  128. * printf will pass through this function). See the ISpecialFile
  129. * documentation for more information about the arguments and return value.
  130. */
  131. public function write(fd:int, bufPtr:int, nbyte:int, errnoPtr:int):int
  132. {
  133. var str:String = CModule.readString(bufPtr, nbyte)
  134. consoleWrite(str)
  135. return nbyte
  136. }
  137. /**
  138. * The PlayerKernel implementation uses this function to handle
  139. * C IO read requests to the file "/dev/tty" (for example, reads from stdin
  140. * will expect this function to provide the data). See the ISpecialFile
  141. * documentation for more information about the arguments and return value.
  142. */
  143. public function read(fd:int, bufPtr:int, nbyte:int, errnoPtr:int):int
  144. {
  145. return 0
  146. }
  147. /**
  148. * The PlayerKernel implementation uses this function to handle
  149. * C fcntl requests to the file "/dev/tty."
  150. * See the ISpecialFile documentation for more information about the
  151. * arguments and return value.
  152. */
  153. public function fcntl(fd:int, com:int, data:int, errnoPtr:int):int
  154. {
  155. return 0
  156. }
  157. /**
  158. * The PlayerKernel implementation uses this function to handle
  159. * C ioctl requests to the file "/dev/tty."
  160. * See the ISpecialFile documentation for more information about the
  161. * arguments and return value.
  162. */
  163. public function ioctl(fd:int, com:int, data:int, errnoPtr:int):int
  164. {
  165. return CModule.callI(CModule.getPublicSymbol("vglttyioctl"), new <int>[fd, com, data, errnoPtr]);
  166. }
  167. /**
  168. * Helper function that traces to the flashlog text file and also
  169. * displays output in the on-screen textfield console.
  170. */
  171. protected function consoleWrite(s:String):void
  172. {
  173. trace(s)
  174. if(enableConsole) {
  175. _tf.appendText(s)
  176. _tf.scrollV = _tf.maxScrollV
  177. }
  178. }
  179. /**
  180. * The enterFrame callback is run once every frame. UI thunk requests should be handled
  181. * here by calling <code>CModule.serviceUIRequests()</code> (see CModule ASdocs for more information on the UI thunking functionality).
  182. */
  183. protected function enterFrame(e:Event):void
  184. {
  185. CModule.serviceUIRequests();
  186. }
  187. /**
  188. * Provide a way to get the TextField's text.
  189. */
  190. public function get consoleText():String
  191. {
  192. var txt:String = null;
  193. if(_tf != null){
  194. txt = _tf.text;
  195. }
  196. return txt;
  197. }
  198. }
  199. }