/*
* noVNC: HTML5 VNC client
* Copyright (C) 2012 Joel Martin
* Copyright (C) 2013 Samuel Mannehed for Cendio AB
* Licensed under MPL 2.0 or any later version (see LICENSE.txt)
*/
var Keyboard,Mouse;!function(){"use strict";Keyboard=function(e){this._keyDownList=[],Util.set_defaults(this,e,{target:document,focused:!0}),this._handler=new KeyEventDecoder(kbdUtil.ModifierSync(),VerifyCharModifier(TrackKeyState(EscapeModifiers(this._handleRfbEvent.bind(this))))),this._eventHandlers={keyup:this._handleKeyUp.bind(this),keydown:this._handleKeyDown.bind(this),keypress:this._handleKeyPress.bind(this),blur:this._allKeysUp.bind(this)}},Keyboard.prototype={_handleRfbEvent:function(e){this._onKeyPress&&(Util.Debug("onKeyPress "+("keydown"==e.type?"down":"up")+", keysym: "+e.keysym.keysym+"("+e.keysym.keyname+")"),this._onKeyPress(e.keysym.keysym,"keydown"==e.type))},_handleKeyDown:function(e){return!this._focused||(!this._handler.keydown(e)||(Util.stopEvent(e),!1))},_handleKeyPress:function(e){return!this._focused||(!this._handler.keypress(e)||(Util.stopEvent(e),!1))},_handleKeyUp:function(e){return!this._focused||(!this._handler.keyup(e)||(Util.stopEvent(e),!1))},_allKeysUp:function(){Util.Debug(">> Keyboard.allKeysUp"),this._handler.releaseAll(),Util.Debug("<< Keyboard.allKeysUp")},grab:function(){var e=this._target;Util.addEvent(e,"keydown",this._eventHandlers.keydown),Util.addEvent(e,"keyup",this._eventHandlers.keyup),Util.addEvent(e,"keypress",this._eventHandlers.keypress),Util.addEvent(window,"blur",this._eventHandlers.blur)},ungrab:function(){var e=this._target;Util.removeEvent(e,"keydown",this._eventHandlers.keydown),Util.removeEvent(e,"keyup",this._eventHandlers.keyup),Util.removeEvent(e,"keypress",this._eventHandlers.keypress),Util.removeEvent(window,"blur",this._eventHandlers.blur),this._allKeysUp()},sync:function(e){this._handler.syncModifiers(e)}},Util.make_properties(Keyboard,[["target","wo","dom"],["focused","rw","bool"],["onKeyPress","rw","func"]]),Mouse=function(e){this._mouseCaptured=!1,this._doubleClickTimer=null,this._lastTouchPos=null,Util.set_defaults(this,e,{target:document,focused:!0,scale:1,touchButton:1}),this._eventHandlers={mousedown:this._handleMouseDown.bind(this),mouseup:this._handleMouseUp.bind(this),mousemove:this._handleMouseMove.bind(this),mousewheel:this._handleMouseWheel.bind(this),mousedisable:this._handleMouseDisable.bind(this)}},Mouse.prototype={_captureMouse:function(){this._target.setCapture&&this._target.setCapture(),this._mouseCaptured=!0},_releaseMouse:function(){this._target.releaseCapture&&this._target.releaseCapture(),this._mouseCaptured=!1},_resetDoubleClickTimer:function(){this._doubleClickTimer=null},_handleMouseButton:function(e,t){if(!this._focused)return!0;this._notify&&this._notify(e);var s,n=e||window.event,o=Util.getEventPosition(e,this._target,this._scale);if(e.touches||e.changedTouches){if(1==t){if(null===this._doubleClickTimer)this._lastTouchPos=o;else{clearTimeout(this._doubleClickTimer);var i=this._lastTouchPos.x-o.x,u=this._lastTouchPos.y-o.y;Math.sqrt(i*i+u*u)<20*(window.devicePixelRatio||1)&&(o=this._lastTouchPos)}this._doubleClickTimer=setTimeout(this._resetDoubleClickTimer.bind(this),500)}s=this._touchButton}else s=n.which?1<0?8:16,this._onMouseButton&&(this._onMouseButton(n.x,n.y,1,t),this._onMouseButton(n.x,n.y,0,t)),Util.stopEvent(e),!1},_handleMouseMove:function(e){if(!this._focused)return!0;this._notify&&this._notify(e);var t=(e||window.event,Util.getEventPosition(e,this._target,this._scale));return this._onMouseMove&&this._onMouseMove(t.x,t.y),Util.stopEvent(e),!1},_handleMouseDisable:function(e){return!this._focused||((e||window.event).target!=this._target||(Util.stopEvent(e),!1))},grab:function(){var e=this._target;"ontouchstart"in document.documentElement&&(Util.addEvent(e,"touchstart",this._eventHandlers.mousedown),Util.addEvent(window,"touchend",this._eventHandlers.mouseup),Util.addEvent(e,"touchend",this._eventHandlers.mouseup),Util.addEvent(e,"touchmove",this._eventHandlers.mousemove)),Util.addEvent(e,"mousedown",this._eventHandlers.mousedown),Util.addEvent(window,"mouseup",this._eventHandlers.mouseup),Util.addEvent(e,"mouseup",this._eventHandlers.mouseup),Util.addEvent(e,"mousemove",this._eventHandlers.mousemove),Util.addEvent(e,Util.Engine.gecko?"DOMMouseScroll":"mousewheel",this._eventHandlers.mousewheel),Util.addEvent(document,"click",this._eventHandlers.mousedisable),Util.addEvent(document.body,"contextmenu",this._eventHandlers.mousedisable)},ungrab:function(){var e=this._target;"ontouchstart"in document.documentElement&&(Util.removeEvent(e,"touchstart",this._eventHandlers.mousedown),Util.removeEvent(window,"touchend",this._eventHandlers.mouseup),Util.removeEvent(e,"touchend",this._eventHandlers.mouseup),Util.removeEvent(e,"touchmove",this._eventHandlers.mousemove)),Util.removeEvent(e,"mousedown",this._eventHandlers.mousedown),Util.removeEvent(window,"mouseup",this._eventHandlers.mouseup),Util.removeEvent(e,"mouseup",this._eventHandlers.mouseup),Util.removeEvent(e,"mousemove",this._eventHandlers.mousemove),Util.removeEvent(e,Util.Engine.gecko?"DOMMouseScroll":"mousewheel",this._eventHandlers.mousewheel),Util.removeEvent(document,"click",this._eventHandlers.mousedisable),Util.removeEvent(document.body,"contextmenu",this._eventHandlers.mousedisable)}},Util.make_properties(Mouse,[["target","ro","dom"],["notify","ro","func"],["focused","rw","bool"],["scale","rw","float"],["onMouseButton","rw","func"],["onMouseMove","rw","func"],["touchButton","rw","int"]])}();