]>
Commit | Line | Data |
---|---|---|
d3796c14 JM |
1 | /* |
2 | * noVNC: HTML5 VNC client | |
1d728ace | 3 | * Copyright (C) 2012 Joel Martin |
b2f1961a | 4 | * Copyright (C) 2013 Samuel Mannehed for Cendio AB |
1d728ace | 5 | * Licensed under MPL 2.0 or any later version (see LICENSE.txt) |
d3796c14 JM |
6 | */ |
7 | ||
d6e281ba | 8 | /*jslint browser: true, white: false */ |
d3796c14 JM |
9 | /*global window, Util */ |
10 | ||
d6e281ba SR |
11 | var Keyboard, Mouse; |
12 | ||
13 | (function () { | |
14 | "use strict"; | |
15 | ||
16 | // | |
17 | // Keyboard event handler | |
18 | // | |
19 | ||
20 | Keyboard = function (defaults) { | |
21 | this._keyDownList = []; // List of depressed keys | |
22 | // (even if they are happy) | |
23 | ||
24 | Util.set_defaults(this, defaults, { | |
25 | 'target': document, | |
26 | 'focused': true | |
27 | }); | |
28 | ||
29 | // create the keyboard handler | |
30 | this._handler = new KeyEventDecoder(kbdUtil.ModifierSync(), | |
31 | VerifyCharModifier( /* jshint newcap: false */ | |
32 | TrackKeyState( | |
33 | EscapeModifiers(this._handleRfbEvent.bind(this)) | |
34 | ) | |
35 | ) | |
36 | ); /* jshint newcap: true */ | |
37 | ||
38 | // keep these here so we can refer to them later | |
39 | this._eventHandlers = { | |
40 | 'keyup': this._handleKeyUp.bind(this), | |
41 | 'keydown': this._handleKeyDown.bind(this), | |
42 | 'keypress': this._handleKeyPress.bind(this), | |
43 | 'blur': this._allKeysUp.bind(this) | |
44 | }; | |
45 | }; | |
46 | ||
47 | Keyboard.prototype = { | |
48 | // private methods | |
49 | ||
50 | _handleRfbEvent: function (e) { | |
51 | if (this._onKeyPress) { | |
52 | Util.Debug("onKeyPress " + (e.type == 'keydown' ? "down" : "up") + | |
53 | ", keysym: " + e.keysym.keysym + "(" + e.keysym.keyname + ")"); | |
8f06673a | 54 | this._onKeyPress(e); |
d6e281ba SR |
55 | } |
56 | }, | |
d3796c14 | 57 | |
8f06673a DHB |
58 | setQEMUVNCKeyboardHandler: function () { |
59 | this._handler = new QEMUKeyEventDecoder(kbdUtil.ModifierSync(), | |
60 | TrackQEMUKeyState( | |
61 | this._handleRfbEvent.bind(this) | |
62 | ) | |
63 | ); | |
64 | }, | |
65 | ||
d6e281ba SR |
66 | _handleKeyDown: function (e) { |
67 | if (!this._focused) { return true; } | |
d3796c14 | 68 | |
d6e281ba SR |
69 | if (this._handler.keydown(e)) { |
70 | // Suppress bubbling/default actions | |
71 | Util.stopEvent(e); | |
72 | return false; | |
73 | } else { | |
74 | // Allow the event to bubble and become a keyPress event which | |
75 | // will have the character code translated | |
76 | return true; | |
77 | } | |
78 | }, | |
d3796c14 | 79 | |
d6e281ba SR |
80 | _handleKeyPress: function (e) { |
81 | if (!this._focused) { return true; } | |
c96f9003 | 82 | |
d6e281ba SR |
83 | if (this._handler.keypress(e)) { |
84 | // Suppress bubbling/default actions | |
85 | Util.stopEvent(e); | |
86 | return false; | |
87 | } else { | |
88 | // Allow the event to bubble and become a keyPress event which | |
89 | // will have the character code translated | |
90 | return true; | |
91 | } | |
92 | }, | |
d3796c14 | 93 | |
d6e281ba SR |
94 | _handleKeyUp: function (e) { |
95 | if (!this._focused) { return true; } | |
d3796c14 | 96 | |
d6e281ba SR |
97 | if (this._handler.keyup(e)) { |
98 | // Suppress bubbling/default actions | |
99 | Util.stopEvent(e); | |
100 | return false; | |
101 | } else { | |
102 | // Allow the event to bubble and become a keyPress event which | |
103 | // will have the character code translated | |
104 | return true; | |
105 | } | |
106 | }, | |
107 | ||
108 | _allKeysUp: function () { | |
109 | Util.Debug(">> Keyboard.allKeysUp"); | |
110 | this._handler.releaseAll(); | |
111 | Util.Debug("<< Keyboard.allKeysUp"); | |
112 | }, | |
113 | ||
114 | // Public methods | |
115 | ||
116 | grab: function () { | |
117 | //Util.Debug(">> Keyboard.grab"); | |
118 | var c = this._target; | |
119 | ||
120 | Util.addEvent(c, 'keydown', this._eventHandlers.keydown); | |
121 | Util.addEvent(c, 'keyup', this._eventHandlers.keyup); | |
122 | Util.addEvent(c, 'keypress', this._eventHandlers.keypress); | |
123 | ||
124 | // Release (key up) if window loses focus | |
125 | Util.addEvent(window, 'blur', this._eventHandlers.blur); | |
126 | ||
127 | //Util.Debug("<< Keyboard.grab"); | |
128 | }, | |
129 | ||
130 | ungrab: function () { | |
131 | //Util.Debug(">> Keyboard.ungrab"); | |
132 | var c = this._target; | |
133 | ||
134 | Util.removeEvent(c, 'keydown', this._eventHandlers.keydown); | |
135 | Util.removeEvent(c, 'keyup', this._eventHandlers.keyup); | |
136 | Util.removeEvent(c, 'keypress', this._eventHandlers.keypress); | |
137 | Util.removeEvent(window, 'blur', this._eventHandlers.blur); | |
d3796c14 | 138 | |
d6e281ba SR |
139 | // Release (key up) all keys that are in a down state |
140 | this._allKeysUp(); | |
d3796c14 | 141 | |
d6e281ba SR |
142 | //Util.Debug(">> Keyboard.ungrab"); |
143 | }, | |
144 | ||
145 | sync: function (e) { | |
146 | this._handler.syncModifiers(e); | |
147 | } | |
148 | }; | |
149 | ||
150 | Util.make_properties(Keyboard, [ | |
151 | ['target', 'wo', 'dom'], // DOM element that captures keyboard input | |
152 | ['focused', 'rw', 'bool'], // Capture and send key events | |
153 | ||
154 | ['onKeyPress', 'rw', 'func'] // Handler for key press/release | |
5210330a | 155 | ]); |
d3796c14 | 156 | |
d6e281ba SR |
157 | // |
158 | // Mouse event handler | |
159 | // | |
160 | ||
161 | Mouse = function (defaults) { | |
162 | this._mouseCaptured = false; | |
163 | ||
164 | this._doubleClickTimer = null; | |
165 | this._lastTouchPos = null; | |
166 | ||
167 | // Configuration attributes | |
168 | Util.set_defaults(this, defaults, { | |
169 | 'target': document, | |
170 | 'focused': true, | |
171 | 'scale': 1.0, | |
172 | 'touchButton': 1 | |
173 | }); | |
174 | ||
175 | this._eventHandlers = { | |
176 | 'mousedown': this._handleMouseDown.bind(this), | |
177 | 'mouseup': this._handleMouseUp.bind(this), | |
178 | 'mousemove': this._handleMouseMove.bind(this), | |
179 | 'mousewheel': this._handleMouseWheel.bind(this), | |
180 | 'mousedisable': this._handleMouseDisable.bind(this) | |
181 | }; | |
182 | }; | |
183 | ||
184 | Mouse.prototype = { | |
185 | // private methods | |
186 | _captureMouse: function () { | |
187 | // capturing the mouse ensures we get the mouseup event | |
188 | if (this._target.setCapture) { | |
189 | this._target.setCapture(); | |
190 | } | |
191 | ||
192 | // some browsers give us mouseup events regardless, | |
193 | // so if we never captured the mouse, we can disregard the event | |
194 | this._mouseCaptured = true; | |
195 | }, | |
196 | ||
197 | _releaseMouse: function () { | |
198 | if (this._target.releaseCapture) { | |
199 | this._target.releaseCapture(); | |
200 | } | |
201 | this._mouseCaptured = false; | |
202 | }, | |
203 | ||
204 | _resetDoubleClickTimer: function () { | |
205 | this._doubleClickTimer = null; | |
206 | }, | |
b2f1961a | 207 | |
d6e281ba SR |
208 | _handleMouseButton: function (e, down) { |
209 | if (!this._focused) { return true; } | |
cf19ad37 | 210 | |
d6e281ba SR |
211 | if (this._notify) { |
212 | this._notify(e); | |
213 | } | |
b2f1961a | 214 | |
d6e281ba SR |
215 | var evt = (e ? e : window.event); |
216 | var pos = Util.getEventPosition(e, this._target, this._scale); | |
217 | ||
218 | var bmask; | |
219 | if (e.touches || e.changedTouches) { | |
220 | // Touch device | |
221 | ||
222 | // When two touches occur within 500 ms of each other and are | |
f52105bc | 223 | // close enough together a double click is triggered. |
d6e281ba SR |
224 | if (down == 1) { |
225 | if (this._doubleClickTimer === null) { | |
226 | this._lastTouchPos = pos; | |
227 | } else { | |
228 | clearTimeout(this._doubleClickTimer); | |
229 | ||
230 | // When the distance between the two touches is small enough | |
231 | // force the position of the latter touch to the position of | |
232 | // the first. | |
233 | ||
234 | var xs = this._lastTouchPos.x - pos.x; | |
235 | var ys = this._lastTouchPos.y - pos.y; | |
236 | var d = Math.sqrt((xs * xs) + (ys * ys)); | |
237 | ||
238 | // The goal is to trigger on a certain physical width, the | |
239 | // devicePixelRatio brings us a bit closer but is not optimal. | |
f52105bc | 240 | var threshold = 20 * (window.devicePixelRatio || 1); |
241 | if (d < threshold) { | |
d6e281ba SR |
242 | pos = this._lastTouchPos; |
243 | } | |
244 | } | |
245 | this._doubleClickTimer = setTimeout(this._resetDoubleClickTimer.bind(this), 500); | |
a4ec2f5c | 246 | } |
d6e281ba SR |
247 | bmask = this._touchButton; |
248 | // If bmask is set | |
249 | } else if (evt.which) { | |
250 | /* everything except IE */ | |
251 | bmask = 1 << evt.button; | |
252 | } else { | |
253 | /* IE including 9 */ | |
254 | bmask = (evt.button & 0x1) + // Left | |
255 | (evt.button & 0x2) * 2 + // Right | |
256 | (evt.button & 0x4) / 2; // Middle | |
b2f1961a | 257 | } |
d6e281ba SR |
258 | |
259 | if (this._onMouseButton) { | |
260 | Util.Debug("onMouseButton " + (down ? "down" : "up") + | |
261 | ", x: " + pos.x + ", y: " + pos.y + ", bmask: " + bmask); | |
262 | this._onMouseButton(pos.x, pos.y, down, bmask); | |
263 | } | |
264 | Util.stopEvent(e); | |
265 | return false; | |
266 | }, | |
267 | ||
268 | _handleMouseDown: function (e) { | |
269 | this._captureMouse(); | |
270 | this._handleMouseButton(e, 1); | |
271 | }, | |
272 | ||
273 | _handleMouseUp: function (e) { | |
274 | if (!this._mouseCaptured) { return; } | |
275 | ||
276 | this._handleMouseButton(e, 0); | |
277 | this._releaseMouse(); | |
278 | }, | |
279 | ||
280 | _handleMouseWheel: function (e) { | |
281 | if (!this._focused) { return true; } | |
282 | ||
283 | if (this._notify) { | |
284 | this._notify(e); | |
285 | } | |
286 | ||
287 | var evt = (e ? e : window.event); | |
288 | var pos = Util.getEventPosition(e, this._target, this._scale); | |
289 | var wheelData = evt.detail ? evt.detail * -1 : evt.wheelDelta / 40; | |
290 | var bmask; | |
291 | if (wheelData > 0) { | |
292 | bmask = 1 << 3; | |
293 | } else { | |
294 | bmask = 1 << 4; | |
295 | } | |
296 | ||
297 | if (this._onMouseButton) { | |
298 | this._onMouseButton(pos.x, pos.y, 1, bmask); | |
299 | this._onMouseButton(pos.x, pos.y, 0, bmask); | |
300 | } | |
301 | Util.stopEvent(e); | |
302 | return false; | |
303 | }, | |
304 | ||
305 | _handleMouseMove: function (e) { | |
306 | if (! this._focused) { return true; } | |
307 | ||
308 | if (this._notify) { | |
309 | this._notify(e); | |
310 | } | |
311 | ||
312 | var evt = (e ? e : window.event); | |
313 | var pos = Util.getEventPosition(e, this._target, this._scale); | |
314 | if (this._onMouseMove) { | |
315 | this._onMouseMove(pos.x, pos.y); | |
316 | } | |
317 | Util.stopEvent(e); | |
318 | return false; | |
319 | }, | |
320 | ||
321 | _handleMouseDisable: function (e) { | |
322 | if (!this._focused) { return true; } | |
323 | ||
324 | var evt = (e ? e : window.event); | |
325 | var pos = Util.getEventPosition(e, this._target, this._scale); | |
326 | ||
327 | /* Stop propagation if inside canvas area */ | |
328 | if ((pos.realx >= 0) && (pos.realy >= 0) && | |
329 | (pos.realx < this._target.offsetWidth) && | |
330 | (pos.realy < this._target.offsetHeight)) { | |
331 | //Util.Debug("mouse event disabled"); | |
332 | Util.stopEvent(e); | |
333 | return false; | |
334 | } | |
335 | ||
336 | return true; | |
337 | }, | |
338 | ||
339 | ||
340 | // Public methods | |
341 | grab: function () { | |
342 | var c = this._target; | |
343 | ||
344 | if ('ontouchstart' in document.documentElement) { | |
345 | Util.addEvent(c, 'touchstart', this._eventHandlers.mousedown); | |
346 | Util.addEvent(window, 'touchend', this._eventHandlers.mouseup); | |
347 | Util.addEvent(c, 'touchend', this._eventHandlers.mouseup); | |
348 | Util.addEvent(c, 'touchmove', this._eventHandlers.mousemove); | |
349 | } else { | |
350 | Util.addEvent(c, 'mousedown', this._eventHandlers.mousedown); | |
351 | Util.addEvent(window, 'mouseup', this._eventHandlers.mouseup); | |
352 | Util.addEvent(c, 'mouseup', this._eventHandlers.mouseup); | |
353 | Util.addEvent(c, 'mousemove', this._eventHandlers.mousemove); | |
354 | Util.addEvent(c, (Util.Engine.gecko) ? 'DOMMouseScroll' : 'mousewheel', | |
355 | this._eventHandlers.mousewheel); | |
356 | } | |
357 | ||
358 | /* Work around right and middle click browser behaviors */ | |
359 | Util.addEvent(document, 'click', this._eventHandlers.mousedisable); | |
360 | Util.addEvent(document.body, 'contextmenu', this._eventHandlers.mousedisable); | |
361 | }, | |
362 | ||
363 | ungrab: function () { | |
364 | var c = this._target; | |
365 | ||
366 | if ('ontouchstart' in document.documentElement) { | |
367 | Util.removeEvent(c, 'touchstart', this._eventHandlers.mousedown); | |
368 | Util.removeEvent(window, 'touchend', this._eventHandlers.mouseup); | |
369 | Util.removeEvent(c, 'touchend', this._eventHandlers.mouseup); | |
370 | Util.removeEvent(c, 'touchmove', this._eventHandlers.mousemove); | |
371 | } else { | |
372 | Util.removeEvent(c, 'mousedown', this._eventHandlers.mousedown); | |
373 | Util.removeEvent(window, 'mouseup', this._eventHandlers.mouseup); | |
374 | Util.removeEvent(c, 'mouseup', this._eventHandlers.mouseup); | |
375 | Util.removeEvent(c, 'mousemove', this._eventHandlers.mousemove); | |
376 | Util.removeEvent(c, (Util.Engine.gecko) ? 'DOMMouseScroll' : 'mousewheel', | |
377 | this._eventHandlers.mousewheel); | |
378 | } | |
379 | ||
380 | /* Work around right and middle click browser behaviors */ | |
381 | Util.removeEvent(document, 'click', this._eventHandlers.mousedisable); | |
382 | Util.removeEvent(document.body, 'contextmenu', this._eventHandlers.mousedisable); | |
383 | ||
b2f1961a | 384 | } |
d6e281ba SR |
385 | }; |
386 | ||
387 | Util.make_properties(Mouse, [ | |
388 | ['target', 'ro', 'dom'], // DOM element that captures mouse input | |
389 | ['notify', 'ro', 'func'], // Function to call to notify whenever a mouse event is received | |
390 | ['focused', 'rw', 'bool'], // Capture and send mouse clicks/movement | |
391 | ['scale', 'rw', 'float'], // Viewport scale factor 0.0 - 1.0 | |
392 | ||
393 | ['onMouseButton', 'rw', 'func'], // Handler for mouse button click/release | |
394 | ['onMouseMove', 'rw', 'func'], // Handler for mouse movement | |
395 | ['touchButton', 'rw', 'int'] // Button mask (1, 2, 4) for touch devices (0 means ignore clicks) | |
396 | ]); | |
397 | })(); |