(function(){var t,e,n,s;(t=class t extends this.OS.GUI.BasicDialog{constructor(){super("ConnectionDialog",t.scheme)}main(){var t;return super.main(),this.find("bbp").data=[{text:"16 bits",value:16},{text:"32 bits",value:32}],this.find("txtWVNC").value=this.parent.setting.wvnc,this.find("txtServer").value=this.parent.setting.server,t=0,32===this.parent.setting.bbc&&(t=1),this.find("bbp").selected=t,this.find("jq").value=this.parent.setting.quality,this.find("bt-ok").onbtclick=t=>{if(this.handle)return this.parent.setting.wvnc=this.find("txtWVNC").value,this.parent.setting.server=this.find("txtServer").value,this.parent.setting.bbc=this.find("bbp").selectedItem.data.value,this.parent.setting.quality=this.find("jq").value,this.handle(void 0),this.quit()},this.find("bt-cancel").onbtclick=t=>this.quit()}}).scheme='\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n\n',(e=class t extends this.OS.GUI.BasicDialog{constructor(){super("CredentialDialog",t.scheme)}main(){return this.find("bt-ok").onbtclick=()=>{var t;return this.handle?(t={username:this.find("txtUser").value,password:this.find("txtPass").value},this.handle(t),this.quit()):this.quit()},this.find("bt-cancel").onbtclick=()=>this.quit()}}).scheme='\n \n \n \n \n \n \n \n \n',n=class extends this.OS.application.BaseApplication{constructor(t){super("RemoteDesktop",t)}main(){var t,n;return this.canvas=this.find("screen"),this.container=this.find("container"),this.zoom=this.find("zoom"),this.btreset=this.find("btreset"),this.zoom.max=200,this.zoom.value=100,this.zoom.onvaluechange=t=>this.setScale(),this.switch=this.find("capture_mouse"),this.switch.onswchange=t=>this.client.mouseCapture=this.switch.swon,n="ws",window.location.protocol.startsWith("https")&&(n+="s"),t=""+window.location.host,window.location.port&&""!==window.location.port&&(t+=":"+window.location.port),this.setting.wvnc||(this.setting.wvnc=`${n}://${t}/wvnc`),this.setting.server||(this.setting.server="127.0.0.1:5900"),this.setting.bbp||(this.setting.bbp=16),this.setting.quality||(this.setting.quality=40),this.btreset.onbtclick=t=>{var e,n,s,i;return i=$(this.container).width(),e=$(this.container).height(),n=i/this.client.resolution.w,s=e/this.client.resolution.h,this.zoom.value=n>s?100*s:100*n,this.setScale()},this.client=new s({element:this.canvas}),this.bindKey("CTRL-SHIFT-V",t=>this.pasteText()),this.client.ondisconnect=()=>this.showConnectionDialog(),this.client.onresize=()=>this.setScale(),this.client.onpassword=()=>new Promise((t,e)=>this.openDialog("PromptDialog",{title:__("VNC password"),label:__("VNC password"),value:"password",type:"password"}).then((function(e){return t(e)}))),this.client.oncopy=t=>this._api.setClipboard(t),this.client.oncredential=()=>new Promise((t,n)=>this.openDialog(new e,{title:__("User credential")}).then((function(e){return t(e.username,e.password)}))),this.on("focus",t=>$(this.canvas).focus()),this.client.init().then(()=>this.showConnectionDialog())}pasteText(){var t;if(this.client)return t=t=>{if(t&&""!==t)return this.client.sendTextAsClipboard(t)},this._api.getClipboard().then(e=>t(e)).catch(e=>(this.error(__("Unable to paste"),e),this.openDialog("TextDialog",{title:"Paste text"}).then(e=>t(e)).catch(t=>this.error(t.toString(),t))))}setScale(){if(console.log("scale changed"),this.client&&this.client.resolution)return this.client.setScale(this.zoom.value/100),this.container.scrollLeft=0,this.container.scrollTop=0}menu(){return[{text:"__(Connection)",nodes:[{text:"__(New Connection)",dataid:this.name+"-new"},{text:"__(Disconnect)",dataid:this.name+"-close"}],onchildselect:t=>{if(this.client)return this.client.disconnect(!1)}}]}showConnectionDialog(){if(this.client)return this.openDialog(new t,{title:__("Connection"),data:this.setting}).then(t=>(this.client.ws=this.setting.wvnc,this.client.connect(this.setting.server,this.setting)))}cleanup(){return this.client&&this.client.disconnect(!0),this.client=void 0}},this.OS.register("RemoteDesktop",n),s=class{constructor(t){var e,n;this.socket=void 0,this.ws=void 0,this.canvas=void 0,n="pkg://RemoteDesktop/decoder_asm.js".asFileHandle().getlink(),this.scale=1,t.ws&&(this.ws=t.ws),this.canvas=t.element,"string"==typeof this.canvas&&(this.canvas=document.getElementById(this.canvas)),this.decoder=new Worker(n),this.enableEvent=!1,this.mouseCapture=!0,this.pingto=!1,e=this,this.mouseMask=0,this.decoder.onmessage=function(t){return e.process(t.data)}}init(){var t;return t=this,new Promise((function(e,n){return t.canvas?($(t.canvas).attr("tabindex","1"),$(t.canvas).on("focus",()=>t.resetModifierKeys()),t.initInputEvent(),e()):n("Canvas is not set")}))}initInputEvent(){var t,e,n,s;if(n=this,this.canvas&&(e=function(t){var e;return e=n.canvas.getBoundingClientRect(),{x:Math.floor((t.clientX-e.left)/n.scale),y:Math.floor((t.clientY-e.top)/n.scale)}},s=function(t){var s;if(n.enableEvent)return s=e(t),n.sendPointEvent(s.x,s.y,n.mouseMask)},n.canvas))return n.canvas.oncontextmenu=function(t){return t.preventDefault(),!1},n.canvas.onmousemove=function(t){if(n.mouseCapture)return s(t)},n.canvas.onmousedown=function(t){var e;if(n.mouseCapture)return e=1<this.disconnect(!0),window.addEventListener("unload",t),window.addEventListener("beforeunload",t)}initCanvas(t,e,n){return this.canvas.width=t,this.canvas.height=e,this.resolution={w:t,h:e},this.decoder.postMessage(this.resolution),this.setScale(this.scale)}process(t){var e,n,s;if(this.socket)return n=new Uint8Array(t.pixels),(s=(e=this.canvas.getContext("2d",{alpha:!1})).createImageData(t.w,t.h)).data.set(n),e.putImageData(s,t.x,t.y),this.socket.send(this.buildCommand(4,1));this.socket.send(this.buildCommand(4,1))}setScale(t){if(this.scale=t,this.canvas)return this.canvas.style.transformOrigin="0 0",this.canvas.style.transform="scale("+t+")"}connect(t,e){var n;if(n=this,this.disconnect(!1),this.ws)return this.socket=new WebSocket(this.ws),this.socket.binaryType="arraybuffer",this.socket.onopen=function(){return console.log("socket opened"),n.initConnection(t,e)},this.socket.onmessage=function(t){return n.consume(t)},this.socket.onerror=t=>n.onerror("Websocket error"),this.socket.onclose=function(){return n.socket=null,n.canvas.style.cursor="auto",n.canvas&&n.resolution&&n.canvas.getContext("2d").clearRect(0,0,n.resolution.w,n.resolution.h),n.pingto&&clearTimeout(n.pingto),n.pingto=void 0,n.ondisconnect(),console.log("socket closed")}}disconnect(t){return this.socket&&this.socket.close(),this.socket=void 0,t&&this.decoder.terminate(),this.enableEvent=!1}initConnection(t,e){var n;return(n=new Uint8Array(t.length+2))[0]=16,n[1]=50,e&&(e.bbp&&(n[0]=e.bbp),e.quality&&(n[1]=e.quality)),n.set((new TextEncoder).encode(t),2),this.socket.send(this.buildCommand(1,n))}resetModifierKeys(){if(this.socket&&this.enableEvent)return this.sendKeyEvent(65511,0),this.sendKeyEvent(65512,0),this.sendKeyEvent(65505,0),this.sendKeyEvent(65507,0),this.sendKeyEvent(65513,0)}sendPointEvent(t,e,n){var s;if(this.socket&&this.enableEvent)return(s=new Uint8Array(5))[0]=255&t,s[1]=t>>8,s[2]=255&e,s[3]=e>>8,s[4]=n,this.socket.send(this.buildCommand(5,s))}sendKeyEvent(t,e){var n;if(this.socket&&this.enableEvent)return(n=new Uint8Array(3))[0]=255&t,n[1]=t>>8&255,n[2]=e,this.socket.send(this.buildCommand(6,n))}sendPing(){if(this.socket)return this.socket.send(this.buildCommand(8,"PING WVNC"))}buildCommand(t,e){var n,s;switch(s=void 0,typeof e){case"string":s=(new TextEncoder).encode(e);break;case"number":s=new Uint8Array([e]);break;default:s=e}return(n=new Uint8Array(s.length+3))[0]=t,n[2]=s.length>>8,n[1]=15&s.length,n.set(s,3),n.buffer}oncopy(t){return console.log("Get clipboard text: "+t)}onpassword(){return new Promise((function(t,e){return e("onpassword is not implemented")}))}sendTextAsClipboard(t){var e;if(this.socket)return this.socket.send(this.buildCommand(7,t)),e="v".charCodeAt(0),this.sendKeyEvent(65507,1),this.sendKeyEvent(e,1),this.sendKeyEvent(e,0),this.sendKeyEvent(65507,0)}oncredential(){return new Promise((function(t,e){return e("oncredential is not implemented")}))}onerror(t){return console.log("Error",t)}onresize(){return console.log("resize")}ondisconnect(){return console.log("disconnect")}consume(t){var e,n,s,i,a,o,r;switch(e=(n=new Uint8Array(t.data))[0],o=this,e){case 254:return n=n.subarray(1,n.length-1),s=new TextDecoder("utf-8"),this.onerror(s.decode(n));case 129:return console.log("Request for password"),this.enableEvent=!1,this.onpassword().then((function(t){return o.socket.send(o.buildCommand(2,t)),o.enableEvent=!0}));case 130:return console.log("Request for login"),this.enableEvent=!1,this.oncredential().then((function(t,e){var n;return(n=new Uint8Array(t.length+e.length+1)).set((new TextEncoder).encode(t),0),n.set(["\0"],t.length),n.set((new TextEncoder).encode(e),t.length+1),o.socket.send(o.buildCommand(3,n)),o.enableEvent=!0}));case 131:if(r=n[1]|n[2]<<8,a=n[3]|n[4]<<8,this.initCanvas(r,a),this.socket.send(this.buildCommand(4,1)),this.enableEvent=!0,this.onresize(),this.pingto)return;return i=()=>(this.sendPing(),this.pingto=setTimeout(i,5e3)),this.pingto=setTimeout(i,5e3);case 132:return this.decoder.postMessage(n.buffer,[n.buffer]);case 133:return n=n.subarray(1),s=new TextDecoder("utf-8"),this.oncopy(s.decode(n)),this.socket.send(this.buildCommand(4,1));default:return console.log(e)}}},window.WVNC=s}).call(this);