import mx.managers.PopUpManager; import mx.core.*; import ParttyConnectionDialog; import org.partty.Telnet; private var _host:String; private var _port:int; [Bindable] private var _session:String; [Bindable] private var _password:String; private function init():void { } private function start():void { telnet.setPartnerOptionHandler(Telnet.OPT_NAWS, telnetPassThroughHandler); telnet.setSbHandler(Telnet.OPT_NAWS, terminalResizedHandler); terminal.addEventListener(KeyboardEvent.KEY_DOWN, keyboardHandler); for each(var l:UIComponent in terminalControlLink.getChildren()) { l.tabEnabled = false; } for each(var c:Container in terminalControl.getChildren()) { c.tabEnabled = false; for each(var cc:UIComponent in c.getChildren()) { // FIXME cc.tabEnabled = false; } } //resize_fit(); _host = Application.application.parameters.host; if(!_host) { _host = "www.partty.org"; } var port:String = Application.application.parameters.port; if(port) { _port = Number(port); } else { _port = 7776; } _session = Application.application.parameters.session; _password = Application.application.parameters.password; if(!_password) { _password = ""; } if(!_session) { popUpDialog("Connect to Partty! session."); } else { connect(); } } private function resize_fit():void { var limit:uint; var to_width:Number = this.width; var to_height:Number = this.height - controller.height - 20; if(to_width > terminal.width) { while(to_width > terminal.width) { terminal.col += 1; if( terminal.col >= 130 ) { break; } } terminal.col -= 4; // FIXME } while(to_width <= terminal.width) { terminal.col -= 1; if( terminal.col <= 40 ) { break; } } limit = 0; if(to_height > terminal.height) { while(to_height > terminal.height) { terminal.row += 1; if( terminal.row >= 60 ) { break; } } terminal.row -= 1; // FIXME } while(to_height <= terminal.height) { terminal.row -= 1; if( terminal.row <= 10 ) { break; } } } private function keyboardHandler(event:KeyboardEvent):void { switch(event.keyCode) { case Keyboard.UP: telnet.writeByte(0x1b); // \e telnet.writeByte(0x4f); // O telnet.writeByte(0x41); // A break; case Keyboard.DOWN: telnet.writeByte(0x1b); // \e telnet.writeByte(0x4f); // O telnet.writeByte(0x42); // B break; case Keyboard.RIGHT: telnet.writeByte(0x1b); // \e telnet.writeByte(0x4f); // O telnet.writeByte(0x43); // C break; case Keyboard.LEFT: telnet.writeByte(0x1b); // \e telnet.writeByte(0x4f); // O telnet.writeByte(0x44); // D break; default: var code:uint = event.charCode; if(code == 0) { return; } telnet.writeByte(code); break; } } private var hasCommandLineInput:Boolean = false; private function onCommandLineInput(event:TextEvent):void { hasCommandLineInput = true; } private function onCommandLineKeyUp(event:KeyboardEvent):void { if (event.keyCode == Keyboard.ENTER && hasCommandLineInput == false) { trace(commandLine.text); if(commandLine.text.length == 0) { // send enter key telnet.writeByte(Keyboard.ENTER); } else { // send text telnet.writeUTFBytes(commandLine.text); commandLine.text = ""; } } hasCommandLineInput = false; } private function popUpDialog(message:String):void { var dialog:ParttyConnectionDialog = PopUpManager.createPopUp(this, ParttyConnectionDialog, true) as ParttyConnectionDialog; dialog.addEventListener(ParttyConnectionDialog.CONNECT_START, dialogHandler); //dialog.x = (terminal.width - dialog.width) / 2; //dialog.y = (terminal.height - dialog.height) / 2; dialog.message.text = message; if(_session) { dialog.session.text = _session; } } private function dialogHandler(event:Event):void { _session = event.target.session.text; _password = event.target.password.text; PopUpManager.removePopUp(IFlexDisplayObject(event.target)); connect(); } private function connect():void { telnet.connect(_host, _port); telnet.addEventListener(Event.CONNECT, connectHandler); telnet.addEventListener(Event.CLOSE, closeHandler); telnet.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler); telnet.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler); terminal.setFocus(); } private function reconnect(session:String, password:String):void { _session = session; _password = password; telnet.close(); connect(); } private function connectHandler(event:Event):void { telnet.writeMultiByte(_session, "utf-8"); telnet.writeByte(0); telnet.writeMultiByte(_password, "utf-8"); telnet.writeByte(0); } private function closeHandler(event:Event):void { popUpDialog("Session closed."); } private function ioErrorHandler(event:IOErrorEvent):void { popUpDialog("IO Error: " + event.text); } private function securityErrorHandler(event:SecurityErrorEvent):void { popUpDialog("Security Error: " + event.text); } private function telnetPassThroughHandler(cmd:uint, sw:Boolean, telnet:Telnet):void { } private function terminalResizedHandler(cmd:uint, args:ByteArray, telnet:Telnet):void { if( args.length != 4 ) { return; } var cols:uint = args.readUnsignedShort(); var rows:uint = args.readUnsignedShort(); trace("terminal resized"); terminal.resize(cols, rows); }