Migrate to minify-maven-plugin and Google Closure Compiler.

This commit is contained in:
Michael Jumper
2014-01-15 19:40:42 -08:00
parent 25248ede66
commit 6c00d8428c
14 changed files with 29 additions and 21 deletions

View File

@@ -0,0 +1,23 @@
/*! (C) 2014 Glyptodon LLC - glyptodon.org/MIT-LICENSE */
/*
* Copyright (C) 2013 Glyptodon LLC
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

View File

@@ -0,0 +1,234 @@
/*
* Copyright (C) 2013 Glyptodon LLC
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
var Guacamole = Guacamole || {};
/**
* Abstract audio channel which queues and plays arbitrary audio data.
* @constructor
*/
Guacamole.AudioChannel = function() {
/**
* Reference to this AudioChannel.
* @private
*/
var channel = this;
/**
* When the next packet should play.
* @private
*/
var next_packet_time = 0;
/**
* Queues up the given data for playing by this channel once all previously
* queued data has been played. If no data has been queued, the data will
* play immediately.
*
* @param {String} mimetype The mimetype of the data provided.
* @param {Number} duration The duration of the data provided, in
* milliseconds.
* @param {Blob} data The blob data to play.
*/
this.play = function(mimetype, duration, data) {
var packet =
new Guacamole.AudioChannel.Packet(mimetype, data);
var now = Guacamole.AudioChannel.getTimestamp();
// If underflow is detected, reschedule new packets relative to now.
if (next_packet_time < now)
next_packet_time = now;
// Schedule next packet
packet.play(next_packet_time);
next_packet_time += duration;
};
};
// Define context if available
if (window.webkitAudioContext) {
Guacamole.AudioChannel.context = new webkitAudioContext();
}
/**
* Returns a base timestamp which can be used for scheduling future audio
* playback. Scheduling playback for the value returned by this function plus
* N will cause the associated audio to be played back N milliseconds after
* the function is called.
*
* @return {Number} An arbitrary channel-relative timestamp, in milliseconds.
*/
Guacamole.AudioChannel.getTimestamp = function() {
// If we have an audio context, use its timestamp
if (Guacamole.AudioChannel.context)
return Guacamole.AudioChannel.context.currentTime * 1000;
// If we have high-resolution timers, use those
if (window.performance) {
if (window.performance.now)
return window.performance.now();
if (window.performance.webkitNow)
return window.performance.webkitNow();
}
// Fallback to millisecond-resolution system time
return new Date().getTime();
};
/**
* Abstract representation of an audio packet.
*
* @constructor
*
* @param {String} mimetype The mimetype of the data contained by this packet.
* @param {Blob} data The blob of sound data contained by this packet.
*/
Guacamole.AudioChannel.Packet = function(mimetype, data) {
/**
* Schedules this packet for playback at the given time.
*
* @function
* @param {Number} when The time this packet should be played, in
* milliseconds.
*/
this.play = undefined; // Defined conditionally depending on support
// If audio API available, use it.
if (Guacamole.AudioChannel.context) {
var readyBuffer = null;
// By default, when decoding finishes, store buffer for future
// playback
var handleReady = function(buffer) {
readyBuffer = buffer;
};
// Read data and start decoding
var reader = new FileReader();
reader.onload = function() {
Guacamole.AudioChannel.context.decodeAudioData(
reader.result,
function(buffer) { handleReady(buffer); }
);
};
reader.readAsArrayBuffer(data);
// Set up buffer source
var source = Guacamole.AudioChannel.context.createBufferSource();
source.connect(Guacamole.AudioChannel.context.destination);
var play_when;
function playDelayed(buffer) {
source.buffer = buffer;
source.noteOn(play_when / 1000);
}
/** @ignore */
this.play = function(when) {
play_when = when;
// If buffer available, play it NOW
if (readyBuffer)
playDelayed(readyBuffer);
// Otherwise, play when decoded
else
handleReady = playDelayed;
};
}
else {
var play_on_load = false;
// Create audio element to house and play the data
var audio = new Audio();
// Read data and start decoding
var reader = new FileReader();
reader.onload = function() {
var binary = "";
var bytes = new Uint8Array(reader.result);
// Produce binary string from bytes in buffer
for (var i=0; i<bytes.byteLength; i++)
binary += String.fromCharCode(bytes[i]);
// Convert to data URI
audio.src = "data:" + mimetype + ";base64," + window.btoa(binary);
// Play if play was attempted but packet wasn't loaded yet
if (play_on_load)
audio.play();
};
reader.readAsArrayBuffer(data);
function play() {
// If audio data is ready, play now
if (audio.src)
audio.play();
// Otherwise, play when loaded
else
play_on_load = true;
}
/** @ignore */
this.play = function(when) {
// Calculate time until play
var now = Guacamole.AudioChannel.getTimestamp();
var delay = when - now;
// Play now if too late
if (delay < 0)
play();
// Otherwise, schedule later playback
else
window.setTimeout(play, delay);
};
}
};

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,135 @@
/*
* Copyright (C) 2013 Glyptodon LLC
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
var Guacamole = Guacamole || {};
/**
* An input stream abstraction used by the Guacamole client to facilitate
* transfer of files or other binary data.
*
* @constructor
* @param {String} mimetype The mimetype of the data this stream will receive.
*/
Guacamole.InputStream = function(mimetype) {
/**
* Reference to this Guacamole.InputStream.
* @private
*/
var guac_stream = this;
/**
* The length of this Guacamole.InputStream in bytes.
* @private
*/
var length = 0;
/**
* The mimetype of the data contained within this blob.
*/
this.mimetype = mimetype;
// Get blob builder
var blob_builder;
if (window.BlobBuilder) blob_builder = new BlobBuilder();
else if (window.WebKitBlobBuilder) blob_builder = new WebKitBlobBuilder();
else if (window.MozBlobBuilder) blob_builder = new MozBlobBuilder();
else
blob_builder = new (function() {
var blobs = [];
/** @ignore */
this.append = function(data) {
blobs.push(new Blob([data], {"type": mimetype}));
};
/** @ignore */
this.getBlob = function() {
return new Blob(blobs, {"type": mimetype});
};
})();
/**
* Receives the given ArrayBuffer, storing its data within this
* Guacamole.InputStream.
*
* @param {ArrayBuffer} buffer An ArrayBuffer containing the data to be
* received.
*/
this.receive = function(buffer) {
blob_builder.append(buffer);
length += buffer.byteLength;
// Call handler, if present
if (guac_stream.onreceive)
guac_stream.onreceive(buffer.byteLength);
};
/**
* Closes this Guacamole.InputStream such that no further data will be
* written.
*/
this.close = function() {
// Call handler, if present
if (guac_stream.onclose)
guac_stream.onclose();
// NOTE: Currently not enforced.
};
/**
* Returns the current length of this Guacamole.InputStream, in bytes.
* @return {Number} The current length of this Guacamole.InputStream.
*/
this.getLength = function() {
return length;
};
/**
* Returns the contents of this Guacamole.InputStream as a Blob.
* @return {Blob} The contents of this Guacamole.InputStream.
*/
this.getBlob = function() {
return blob_builder.getBlob();
};
/**
* Fired once for every blob of data received.
*
* @event
* @param {Number} length The number of bytes received.
*/
this.onreceive = null;
/**
* Fired once this stream is finished and no further data will be written.
* @event
*/
this.onclose = null;
};

View File

@@ -0,0 +1,75 @@
/*
* Copyright (C) 2013 Glyptodon LLC
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
var Guacamole = Guacamole || {};
/**
* Integer pool which returns consistently increasing integers while integers
* are in use, and previously-used integers when possible.
* @constructor
*/
Guacamole.IntegerPool = function() {
/**
* Reference to this integer pool.
*/
var guac_pool = this;
/**
* Array of available integers.
* @type Number[]
*/
var pool = [];
/**
* The next integer to return if no more integers remain.
* @type Number
*/
this.next_int = 0;
/**
* Returns the next available integer in the pool. If possible, a previously
* used integer will be returned.
*
* @return {Number} The next available integer.
*/
this.next = function() {
// If free'd integers exist, return one of those
if (pool.length > 0)
return pool.shift();
// Otherwise, return a new integer
return guac_pool.next_int++;
};
/**
* Frees the given integer, allowing it to be reused.
*
* @param {Number} integer The integer to free.
*/
this.free = function(integer) {
pool.push(integer);
};
};

View File

@@ -0,0 +1,661 @@
/*
* Copyright (C) 2013 Glyptodon LLC
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
var Guacamole = Guacamole || {};
/**
* Provides cross-browser and cross-keyboard keyboard for a specific element.
* Browser and keyboard layout variation is abstracted away, providing events
* which represent keys as their corresponding X11 keysym.
*
* @constructor
* @param {Element} element The Element to use to provide keyboard events.
*/
Guacamole.Keyboard = function(element) {
/**
* Reference to this Guacamole.Keyboard.
* @private
*/
var guac_keyboard = this;
/**
* Fired whenever the user presses a key with the element associated
* with this Guacamole.Keyboard in focus.
*
* @event
* @param {Number} keysym The keysym of the key being pressed.
*/
this.onkeydown = null;
/**
* Fired whenever the user releases a key with the element associated
* with this Guacamole.Keyboard in focus.
*
* @event
* @param {Number} keysym The keysym of the key being released.
*/
this.onkeyup = null;
/**
* Map of known JavaScript keycodes which do not map to typable characters
* to their unshifted X11 keysym equivalents.
* @private
*/
var unshiftedKeysym = {
8: [0xFF08], // backspace
9: [0xFF09], // tab
13: [0xFF0D], // enter
16: [0xFFE1, 0xFFE1, 0xFFE2], // shift
17: [0xFFE3, 0xFFE3, 0xFFE4], // ctrl
18: [0xFFE9, 0xFFE9, 0xFFEA], // alt
19: [0xFF13], // pause/break
20: [0xFFE5], // caps lock
27: [0xFF1B], // escape
32: [0x0020], // space
33: [0xFF55], // page up
34: [0xFF56], // page down
35: [0xFF57], // end
36: [0xFF50], // home
37: [0xFF51], // left arrow
38: [0xFF52], // up arrow
39: [0xFF53], // right arrow
40: [0xFF54], // down arrow
45: [0xFF63], // insert
46: [0xFFFF], // delete
91: [0xFFEB], // left window key (hyper_l)
92: [0xFF67], // right window key (menu key?)
93: null, // select key
112: [0xFFBE], // f1
113: [0xFFBF], // f2
114: [0xFFC0], // f3
115: [0xFFC1], // f4
116: [0xFFC2], // f5
117: [0xFFC3], // f6
118: [0xFFC4], // f7
119: [0xFFC5], // f8
120: [0xFFC6], // f9
121: [0xFFC7], // f10
122: [0xFFC8], // f11
123: [0xFFC9], // f12
144: [0xFF7F], // num lock
145: [0xFF14] // scroll lock
};
/**
* Map of known JavaScript keyidentifiers which do not map to typable
* characters to their unshifted X11 keysym equivalents.
* @private
*/
var keyidentifier_keysym = {
"Again": [0xFF66],
"AllCandidates": [0xFF3D],
"Alphanumeric": [0xFF30],
"Alt": [0xFFE9, 0xFFE9, 0xFFEA],
"Attn": [0xFD0E],
"AltGraph": [0xFFEA],
"ArrowDown": [0xFF54],
"ArrowLeft": [0xFF51],
"ArrowRight": [0xFF53],
"ArrowUp": [0xFF52],
"Backspace": [0xFF08],
"CapsLock": [0xFFE5],
"Cancel": [0xFF69],
"Clear": [0xFF0B],
"Convert": [0xFF21],
"Copy": [0xFD15],
"Crsel": [0xFD1C],
"CrSel": [0xFD1C],
"CodeInput": [0xFF37],
"Compose": [0xFF20],
"Control": [0xFFE3, 0xFFE3, 0xFFE4],
"ContextMenu": [0xFF67],
"Delete": [0xFFFF],
"Down": [0xFF54],
"End": [0xFF57],
"Enter": [0xFF0D],
"EraseEof": [0xFD06],
"Escape": [0xFF1B],
"Execute": [0xFF62],
"Exsel": [0xFD1D],
"ExSel": [0xFD1D],
"F1": [0xFFBE],
"F2": [0xFFBF],
"F3": [0xFFC0],
"F4": [0xFFC1],
"F5": [0xFFC2],
"F6": [0xFFC3],
"F7": [0xFFC4],
"F8": [0xFFC5],
"F9": [0xFFC6],
"F10": [0xFFC7],
"F11": [0xFFC8],
"F12": [0xFFC9],
"F13": [0xFFCA],
"F14": [0xFFCB],
"F15": [0xFFCC],
"F16": [0xFFCD],
"F17": [0xFFCE],
"F18": [0xFFCF],
"F19": [0xFFD0],
"F20": [0xFFD1],
"F21": [0xFFD2],
"F22": [0xFFD3],
"F23": [0xFFD4],
"F24": [0xFFD5],
"Find": [0xFF68],
"GroupFirst": [0xFE0C],
"GroupLast": [0xFE0E],
"GroupNext": [0xFE08],
"GroupPrevious": [0xFE0A],
"FullWidth": null,
"HalfWidth": null,
"HangulMode": [0xFF31],
"Hankaku": [0xFF29],
"HanjaMode": [0xFF34],
"Help": [0xFF6A],
"Hiragana": [0xFF25],
"HiraganaKatakana": [0xFF27],
"Home": [0xFF50],
"Hyper": [0xFFED, 0xFFED, 0xFFEE],
"Insert": [0xFF63],
"JapaneseHiragana": [0xFF25],
"JapaneseKatakana": [0xFF26],
"JapaneseRomaji": [0xFF24],
"JunjaMode": [0xFF38],
"KanaMode": [0xFF2D],
"KanjiMode": [0xFF21],
"Katakana": [0xFF26],
"Left": [0xFF51],
"Meta": [0xFFE7],
"ModeChange": [0xFF7E],
"NumLock": [0xFF7F],
"PageDown": [0xFF55],
"PageUp": [0xFF56],
"Pause": [0xFF13],
"Play": [0xFD16],
"PreviousCandidate": [0xFF3E],
"PrintScreen": [0xFD1D],
"Redo": [0xFF66],
"Right": [0xFF53],
"RomanCharacters": null,
"Scroll": [0xFF14],
"Select": [0xFF60],
"Separator": [0xFFAC],
"Shift": [0xFFE1, 0xFFE1, 0xFFE2],
"SingleCandidate": [0xFF3C],
"Super": [0xFFEB, 0xFFEB, 0xFFEC],
"Tab": [0xFF09],
"Up": [0xFF52],
"Undo": [0xFF65],
"Win": [0xFFEB],
"Zenkaku": [0xFF28],
"ZenkakuHankaku": [0xFF2A]
};
/**
* Map of known JavaScript keycodes which do not map to typable characters
* to their shifted X11 keysym equivalents. Keycodes must only be listed
* here if their shifted X11 keysym equivalents differ from their unshifted
* equivalents.
* @private
*/
var shiftedKeysym = {
18: [0xFFE7, 0xFFE7, 0xFFEA] // alt
};
/**
* All keysyms which should not repeat when held down.
* @private
*/
var no_repeat = {
0xFFE1: true, // Left shift
0xFFE2: true, // Right shift
0xFFE3: true, // Left ctrl
0xFFE4: true, // Right ctrl
0xFFE7: true, // Left meta
0xFFE8: true, // Right meta
0xFFE9: true, // Left alt
0xFFEA: true, // Right alt (or AltGr)
0xFFEB: true, // Left hyper
0xFFEC: true // Right hyper
};
/**
* All modifiers and their states.
*/
this.modifiers = new Guacamole.Keyboard.ModifierState();
/**
* The state of every key, indexed by keysym. If a particular key is
* pressed, the value of pressed for that keysym will be true. If a key
* is not currently pressed, it will not be defined.
*/
this.pressed = {};
/**
* The keysym associated with a given keycode when keydown fired.
* @private
*/
var keydownChar = [];
/**
* Timeout before key repeat starts.
* @private
*/
var key_repeat_timeout = null;
/**
* Interval which presses and releases the last key pressed while that
* key is still being held down.
* @private
*/
var key_repeat_interval = null;
/**
* Given an array of keysyms indexed by location, returns the keysym
* for the given location, or the keysym for the standard location if
* undefined.
*
* @param {Array} keysyms An array of keysyms, where the index of the
* keysym in the array is the location value.
* @param {Number} location The location on the keyboard corresponding to
* the key pressed, as defined at:
* http://www.w3.org/TR/DOM-Level-3-Events/#events-KeyboardEvent
*/
function get_keysym(keysyms, location) {
if (!keysyms)
return null;
return keysyms[location] || keysyms[0];
}
function keysym_from_key_identifier(shifted, identifier, location) {
var typedCharacter;
// If identifier is U+xxxx, decode Unicode character
var unicodePrefixLocation = identifier.indexOf("U+");
if (unicodePrefixLocation >= 0) {
var hex = identifier.substring(unicodePrefixLocation+2);
typedCharacter = String.fromCharCode(parseInt(hex, 16));
}
// If single character, use that as typed character
else if (identifier.length === 1)
typedCharacter = identifier;
// Otherwise, look up corresponding keysym
else
return get_keysym(keyidentifier_keysym[identifier], location);
// Convert case if shifted
if (shifted)
typedCharacter = typedCharacter.toUpperCase();
else
typedCharacter = typedCharacter.toLowerCase();
// Get codepoint
var codepoint = typedCharacter.charCodeAt(0);
return keysym_from_charcode(codepoint);
}
function isControlCharacter(codepoint) {
return codepoint <= 0x1F || (codepoint >= 0x7F && codepoint <= 0x9F);
}
function keysym_from_charcode(codepoint) {
// Keysyms for control characters
if (isControlCharacter(codepoint)) return 0xFF00 | codepoint;
// Keysyms for ASCII chars
if (codepoint >= 0x0000 && codepoint <= 0x00FF)
return codepoint;
// Keysyms for Unicode
if (codepoint >= 0x0100 && codepoint <= 0x10FFFF)
return 0x01000000 | codepoint;
return null;
}
function keysym_from_keycode(keyCode, location) {
var keysyms;
// If not shifted, just return unshifted keysym
if (!guac_keyboard.modifiers.shift)
keysyms = unshiftedKeysym[keyCode];
// Otherwise, return shifted keysym, if defined
else
keysyms = shiftedKeysym[keyCode] || unshiftedKeysym[keyCode];
return get_keysym(keysyms, location);
}
/**
* Marks a key as pressed, firing the keydown event if registered. Key
* repeat for the pressed key will start after a delay if that key is
* not a modifier.
*
* @private
* @param keysym The keysym of the key to press.
*/
function press_key(keysym) {
// Don't bother with pressing the key if the key is unknown
if (keysym === null) return;
// Only press if released
if (!guac_keyboard.pressed[keysym]) {
// Mark key as pressed
guac_keyboard.pressed[keysym] = true;
// Send key event
if (guac_keyboard.onkeydown) {
guac_keyboard.onkeydown(keysym);
// Stop any current repeat
window.clearTimeout(key_repeat_timeout);
window.clearInterval(key_repeat_interval);
// Repeat after a delay as long as pressed
if (!no_repeat[keysym])
key_repeat_timeout = window.setTimeout(function() {
key_repeat_interval = window.setInterval(function() {
guac_keyboard.onkeyup(keysym);
guac_keyboard.onkeydown(keysym);
}, 50);
}, 500);
}
}
}
/**
* Marks a key as released, firing the keyup event if registered.
*
* @private
* @param keysym The keysym of the key to release.
*/
function release_key(keysym) {
// Only release if pressed
if (guac_keyboard.pressed[keysym]) {
// Mark key as released
delete guac_keyboard.pressed[keysym];
// Stop repeat
window.clearTimeout(key_repeat_timeout);
window.clearInterval(key_repeat_interval);
// Send key event
if (keysym !== null && guac_keyboard.onkeyup)
guac_keyboard.onkeyup(keysym);
}
}
/**
* Given a keyboard event, updates the local modifier state and remote
* key state based on the modifier flags within the event. This function
* pays no attention to keycodes.
*
* @param {KeyboardEvent} e The keyboard event containing the flags to update.
*/
function update_modifier_state(e) {
// Get state
var state = Guacamole.Keyboard.ModifierState.fromKeyboardEvent(e);
// Release alt if implicitly released
if (guac_keyboard.modifiers.alt && state.alt === false) {
release_key(0xFFE9); // Left alt
release_key(0xFFEA); // Right alt (or AltGr)
}
// Release shift if implicitly released
if (guac_keyboard.modifiers.shift && state.shift === false) {
release_key(0xFFE1); // Left shift
release_key(0xFFE2); // Right shift
}
// Release ctrl if implicitly released
if (guac_keyboard.modifiers.ctrl && state.ctrl === false) {
release_key(0xFFE3); // Left ctrl
release_key(0xFFE4); // Right ctrl
}
// Release meta if implicitly released
if (guac_keyboard.modifiers.meta && state.meta === false) {
release_key(0xFFE7); // Left meta
release_key(0xFFE8); // Right meta
}
// Release hyper if implicitly released
if (guac_keyboard.modifiers.hyper && state.hyper === false) {
release_key(0xFFEB); // Left hyper
release_key(0xFFEC); // Right hyper
}
// Update state
guac_keyboard.modifiers = state;
}
// When key pressed
element.addEventListener("keydown", function(e) {
// Only intercept if handler set
if (!guac_keyboard.onkeydown) return;
var keynum;
if (window.event) keynum = window.event.keyCode;
else if (e.which) keynum = e.which;
// Get key location
var location = e.location || e.keyLocation || 0;
// Ignore any unknown key events
if (!keynum && !identifier) {
e.preventDefault();
return;
}
// Fix modifier states
update_modifier_state(e);
// Try to get keysym from keycode
var keysym = keysym_from_keycode(keynum, location);
// Also try to get get keysym from e.key
if (e.key)
keysym = keysym || keysym_from_key_identifier(
guac_keyboard.modifiers.shift, e.key, location);
// If no e.key, use e.keyIdentifier if absolutely necessary (can be buggy)
else {
var keypress_unlikely = guac_keyboard.modifiers.ctrl
|| guac_keyboard.modifiers.alt
|| guac_keyboard.modifiers.meta
|| guac_keyboard.modifiers.hyper;
if (keypress_unlikely && e.keyIdentifier)
keysym = keysym || keysym_from_key_identifier(
guac_keyboard.modifiers.shift, e.keyIdentifier, location);
}
// Press key if known
if (keysym !== null) {
e.preventDefault();
keydownChar[keynum] = keysym;
press_key(keysym);
// If a key is pressed while meta is held down, the keyup will
// never be sent in Chrome, so send it now. (bug #108404)
if (guac_keyboard.modifiers.meta && keysym !== 0xFFE7 && keysym !== 0xFFE8)
release_key(keysym);
}
}, true);
// When key pressed
element.addEventListener("keypress", function(e) {
// Only intercept if handler set
if (!guac_keyboard.onkeydown && !guac_keyboard.onkeyup) return;
e.preventDefault();
var keynum;
if (window.event) keynum = window.event.keyCode;
else if (e.which) keynum = e.which;
var keysym = keysym_from_charcode(keynum);
// Fix modifier states
update_modifier_state(e);
// If event identified as a typable character, and we're holding Ctrl+Alt,
// assume Ctrl+Alt is actually AltGr, and release both.
if (!isControlCharacter(keynum) && guac_keyboard.modifiers.ctrl && guac_keyboard.modifiers.alt) {
release_key(0xFFE3); // Left ctrl
release_key(0xFFE4); // Right ctrl
release_key(0xFFE9); // Left alt
release_key(0xFFEA); // Right alt
}
// Send press + release if keysym known
if (keysym !== null) {
press_key(keysym);
release_key(keysym);
}
}, true);
// When key released
element.addEventListener("keyup", function(e) {
// Only intercept if handler set
if (!guac_keyboard.onkeyup) return;
e.preventDefault();
var keynum;
if (window.event) keynum = window.event.keyCode;
else if (e.which) keynum = e.which;
// Fix modifier states
update_modifier_state(e);
// Send release event if original key known
var keysym = keydownChar[keynum];
if (keysym !== null)
release_key(keysym);
// Clear character record
keydownChar[keynum] = null;
}, true);
};
/**
* The state of all supported keyboard modifiers.
* @constructor
*/
Guacamole.Keyboard.ModifierState = function() {
/**
* Whether shift is currently pressed.
* @type Boolean
*/
this.shift = false;
/**
* Whether ctrl is currently pressed.
* @type Boolean
*/
this.ctrl = false;
/**
* Whether alt is currently pressed.
* @type Boolean
*/
this.alt = false;
/**
* Whether meta (apple key) is currently pressed.
* @type Boolean
*/
this.meta = false;
/**
* Whether hyper (windows key) is currently pressed.
* @type Boolean
*/
this.hyper = false;
};
/**
* Returns the modifier state applicable to the keyboard event given.
*
* @param {KeyboardEvent} e The keyboard event to read.
* @returns {Guacamole.Keyboard.ModifierState} The current state of keyboard
* modifiers.
*/
Guacamole.Keyboard.ModifierState.fromKeyboardEvent = function(e) {
var state = new Guacamole.Keyboard.ModifierState();
// Assign states from old flags
state.shift = e.shiftKey;
state.ctrl = e.ctrlKey;
state.alt = e.altKey;
state.meta = e.metaKey;
// Use DOM3 getModifierState() for others
if (e.getModifierState) {
state.hyper = e.getModifierState("OS")
|| e.getModifierState("Super")
|| e.getModifierState("Hyper")
|| e.getModifierState("Win");
}
return state;
};

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,815 @@
/*
* Copyright (C) 2013 Glyptodon LLC
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
var Guacamole = Guacamole || {};
/**
* Provides cross-browser mouse events for a given element. The events of
* the given element are automatically populated with handlers that translate
* mouse events into a non-browser-specific event provided by the
* Guacamole.Mouse instance.
*
* @constructor
* @param {Element} element The Element to use to provide mouse events.
*/
Guacamole.Mouse = function(element) {
/**
* Reference to this Guacamole.Mouse.
* @private
*/
var guac_mouse = this;
/**
* The number of mousemove events to require before re-enabling mouse
* event handling after receiving a touch event.
*/
this.touchMouseThreshold = 3;
/**
* The minimum amount of pixels scrolled required for a single scroll button
* click.
*/
this.scrollThreshold = 120;
/**
* The number of pixels to scroll per line.
*/
this.PIXELS_PER_LINE = 40;
/**
* The number of pixels to scroll per page.
*/
this.PIXELS_PER_PAGE = 640;
/**
* The current mouse state. The properties of this state are updated when
* mouse events fire. This state object is also passed in as a parameter to
* the handler of any mouse events.
*
* @type Guacamole.Mouse.State
*/
this.currentState = new Guacamole.Mouse.State(
0, 0,
false, false, false, false, false
);
/**
* Fired whenever the user presses a mouse button down over the element
* associated with this Guacamole.Mouse.
*
* @event
* @param {Guacamole.Mouse.State} state The current mouse state.
*/
this.onmousedown = null;
/**
* Fired whenever the user releases a mouse button down over the element
* associated with this Guacamole.Mouse.
*
* @event
* @param {Guacamole.Mouse.State} state The current mouse state.
*/
this.onmouseup = null;
/**
* Fired whenever the user moves the mouse over the element associated with
* this Guacamole.Mouse.
*
* @event
* @param {Guacamole.Mouse.State} state The current mouse state.
*/
this.onmousemove = null;
/**
* Counter of mouse events to ignore. This decremented by mousemove, and
* while non-zero, mouse events will have no effect.
* @private
*/
var ignore_mouse = 0;
/**
* Cumulative scroll delta amount. This value is accumulated through scroll
* events and results in scroll button clicks if it exceeds a certain
* threshold.
*/
var scroll_delta = 0;
function cancelEvent(e) {
e.stopPropagation();
if (e.preventDefault) e.preventDefault();
e.returnValue = false;
}
// Block context menu so right-click gets sent properly
element.addEventListener("contextmenu", function(e) {
cancelEvent(e);
}, false);
element.addEventListener("mousemove", function(e) {
cancelEvent(e);
// If ignoring events, decrement counter
if (ignore_mouse) {
ignore_mouse--;
return;
}
guac_mouse.currentState.fromClientPosition(element, e.clientX, e.clientY);
if (guac_mouse.onmousemove)
guac_mouse.onmousemove(guac_mouse.currentState);
}, false);
element.addEventListener("mousedown", function(e) {
cancelEvent(e);
// Do not handle if ignoring events
if (ignore_mouse)
return;
switch (e.button) {
case 0:
guac_mouse.currentState.left = true;
break;
case 1:
guac_mouse.currentState.middle = true;
break;
case 2:
guac_mouse.currentState.right = true;
break;
}
if (guac_mouse.onmousedown)
guac_mouse.onmousedown(guac_mouse.currentState);
}, false);
element.addEventListener("mouseup", function(e) {
cancelEvent(e);
// Do not handle if ignoring events
if (ignore_mouse)
return;
switch (e.button) {
case 0:
guac_mouse.currentState.left = false;
break;
case 1:
guac_mouse.currentState.middle = false;
break;
case 2:
guac_mouse.currentState.right = false;
break;
}
if (guac_mouse.onmouseup)
guac_mouse.onmouseup(guac_mouse.currentState);
}, false);
element.addEventListener("mouseout", function(e) {
// Get parent of the element the mouse pointer is leaving
if (!e) e = window.event;
// Check that mouseout is due to actually LEAVING the element
var target = e.relatedTarget || e.toElement;
while (target != null) {
if (target === element)
return;
target = target.parentNode;
}
cancelEvent(e);
// Release all buttons
if (guac_mouse.currentState.left
|| guac_mouse.currentState.middle
|| guac_mouse.currentState.right) {
guac_mouse.currentState.left = false;
guac_mouse.currentState.middle = false;
guac_mouse.currentState.right = false;
if (guac_mouse.onmouseup)
guac_mouse.onmouseup(guac_mouse.currentState);
}
}, false);
// Override selection on mouse event element.
element.addEventListener("selectstart", function(e) {
cancelEvent(e);
}, false);
// Ignore all pending mouse events when touch events are the apparent source
function ignorePendingMouseEvents() { ignore_mouse = guac_mouse.touchMouseThreshold; }
element.addEventListener("touchmove", ignorePendingMouseEvents, false);
element.addEventListener("touchstart", ignorePendingMouseEvents, false);
element.addEventListener("touchend", ignorePendingMouseEvents, false);
// Scroll wheel support
function mousewheel_handler(e) {
// Determine approximate scroll amount (in pixels)
var delta = e.deltaY || -e.wheelDeltaY || -e.wheelDelta;
// If successfully retrieved scroll amount, convert to pixels if not
// already in pixels
if (delta) {
// Convert to pixels if delta was lines
if (e.deltaMode === 1)
delta = e.deltaY * guac_mouse.PIXELS_PER_LINE;
// Convert to pixels if delta was pages
else if (e.deltaMode === 2)
delta = e.deltaY * guac_mouse.PIXELS_PER_PAGE;
}
// Otherwise, assume legacy mousewheel event and line scrolling
else
delta = e.detail * guac_mouse.PIXELS_PER_LINE;
// Update overall delta
scroll_delta += delta;
// Up
while (scroll_delta <= -guac_mouse.scrollThreshold) {
if (guac_mouse.onmousedown) {
guac_mouse.currentState.up = true;
guac_mouse.onmousedown(guac_mouse.currentState);
}
if (guac_mouse.onmouseup) {
guac_mouse.currentState.up = false;
guac_mouse.onmouseup(guac_mouse.currentState);
}
scroll_delta += guac_mouse.scrollThreshold;
}
// Down
while (scroll_delta >= guac_mouse.scrollThreshold) {
if (guac_mouse.onmousedown) {
guac_mouse.currentState.down = true;
guac_mouse.onmousedown(guac_mouse.currentState);
}
if (guac_mouse.onmouseup) {
guac_mouse.currentState.down = false;
guac_mouse.onmouseup(guac_mouse.currentState);
}
scroll_delta -= guac_mouse.scrollThreshold;
}
cancelEvent(e);
}
element.addEventListener('DOMMouseScroll', mousewheel_handler, false);
element.addEventListener('mousewheel', mousewheel_handler, false);
element.addEventListener('wheel', mousewheel_handler, false);
};
/**
* Simple container for properties describing the state of a mouse.
*
* @constructor
* @param {Number} x The X position of the mouse pointer in pixels.
* @param {Number} y The Y position of the mouse pointer in pixels.
* @param {Boolean} left Whether the left mouse button is pressed.
* @param {Boolean} middle Whether the middle mouse button is pressed.
* @param {Boolean} right Whether the right mouse button is pressed.
* @param {Boolean} up Whether the up mouse button is pressed (the fourth
* button, usually part of a scroll wheel).
* @param {Boolean} down Whether the down mouse button is pressed (the fifth
* button, usually part of a scroll wheel).
*/
Guacamole.Mouse.State = function(x, y, left, middle, right, up, down) {
/**
* Reference to this Guacamole.Mouse.State.
* @private
*/
var guac_state = this;
/**
* The current X position of the mouse pointer.
* @type Number
*/
this.x = x;
/**
* The current Y position of the mouse pointer.
* @type Number
*/
this.y = y;
/**
* Whether the left mouse button is currently pressed.
* @type Boolean
*/
this.left = left;
/**
* Whether the middle mouse button is currently pressed.
* @type Boolean
*/
this.middle = middle
/**
* Whether the right mouse button is currently pressed.
* @type Boolean
*/
this.right = right;
/**
* Whether the up mouse button is currently pressed. This is the fourth
* mouse button, associated with upward scrolling of the mouse scroll
* wheel.
* @type Boolean
*/
this.up = up;
/**
* Whether the down mouse button is currently pressed. This is the fifth
* mouse button, associated with downward scrolling of the mouse scroll
* wheel.
* @type Boolean
*/
this.down = down;
/**
* Updates the position represented within this state object by the given
* element and clientX/clientY coordinates (commonly available within event
* objects). Position is translated from clientX/clientY (relative to
* viewport) to element-relative coordinates.
*
* @param {Element} element The element the coordinates should be relative
* to.
* @param {Number} clientX The X coordinate to translate, viewport-relative.
* @param {Number} clientY The Y coordinate to translate, viewport-relative.
*/
this.fromClientPosition = function(element, clientX, clientY) {
guac_state.x = clientX - element.offsetLeft;
guac_state.y = clientY - element.offsetTop;
// This is all JUST so we can get the mouse position within the element
var parent = element.offsetParent;
while (parent && !(parent === document.body)) {
guac_state.x -= parent.offsetLeft - parent.scrollLeft;
guac_state.y -= parent.offsetTop - parent.scrollTop;
parent = parent.offsetParent;
}
// Element ultimately depends on positioning within document body,
// take document scroll into account.
if (parent) {
var documentScrollLeft = document.body.scrollLeft || document.documentElement.scrollLeft;
var documentScrollTop = document.body.scrollTop || document.documentElement.scrollTop;
guac_state.x -= parent.offsetLeft - documentScrollLeft;
guac_state.y -= parent.offsetTop - documentScrollTop;
}
};
};
/**
* Provides cross-browser relative touch event translation for a given element.
*
* Touch events are translated into mouse events as if the touches occurred
* on a touchpad (drag to push the mouse pointer, tap to click).
*
* @constructor
* @param {Element} element The Element to use to provide touch events.
*/
Guacamole.Mouse.Touchpad = function(element) {
/**
* Reference to this Guacamole.Mouse.Touchpad.
* @private
*/
var guac_touchpad = this;
/**
* The distance a two-finger touch must move per scrollwheel event, in
* pixels.
*/
this.scrollThreshold = 20 * (window.devicePixelRatio || 1);
/**
* The maximum number of milliseconds to wait for a touch to end for the
* gesture to be considered a click.
*/
this.clickTimingThreshold = 250;
/**
* The maximum number of pixels to allow a touch to move for the gesture to
* be considered a click.
*/
this.clickMoveThreshold = 10 * (window.devicePixelRatio || 1);
/**
* The current mouse state. The properties of this state are updated when
* mouse events fire. This state object is also passed in as a parameter to
* the handler of any mouse events.
*
* @type Guacamole.Mouse.State
*/
this.currentState = new Guacamole.Mouse.State(
0, 0,
false, false, false, false, false
);
/**
* Fired whenever a mouse button is effectively pressed. This can happen
* as part of a "click" gesture initiated by the user by tapping one
* or more fingers over the touchpad element, as part of a "scroll"
* gesture initiated by dragging two fingers up or down, etc.
*
* @event
* @param {Guacamole.Mouse.State} state The current mouse state.
*/
this.onmousedown = null;
/**
* Fired whenever a mouse button is effectively released. This can happen
* as part of a "click" gesture initiated by the user by tapping one
* or more fingers over the touchpad element, as part of a "scroll"
* gesture initiated by dragging two fingers up or down, etc.
*
* @event
* @param {Guacamole.Mouse.State} state The current mouse state.
*/
this.onmouseup = null;
/**
* Fired whenever the user moves the mouse by dragging their finger over
* the touchpad element.
*
* @event
* @param {Guacamole.Mouse.State} state The current mouse state.
*/
this.onmousemove = null;
var touch_count = 0;
var last_touch_x = 0;
var last_touch_y = 0;
var last_touch_time = 0;
var pixels_moved = 0;
var touch_buttons = {
1: "left",
2: "right",
3: "middle"
};
var gesture_in_progress = false;
var click_release_timeout = null;
element.addEventListener("touchend", function(e) {
e.stopPropagation();
e.preventDefault();
// If we're handling a gesture AND this is the last touch
if (gesture_in_progress && e.touches.length == 0) {
var time = new Date().getTime();
// Get corresponding mouse button
var button = touch_buttons[touch_count];
// If mouse already down, release anad clear timeout
if (guac_touchpad.currentState[button]) {
// Fire button up event
guac_touchpad.currentState[button] = false;
if (guac_touchpad.onmouseup)
guac_touchpad.onmouseup(guac_touchpad.currentState);
// Clear timeout, if set
if (click_release_timeout) {
window.clearTimeout(click_release_timeout);
click_release_timeout = null;
}
}
// If single tap detected (based on time and distance)
if (time - last_touch_time <= guac_touchpad.clickTimingThreshold
&& pixels_moved < guac_touchpad.clickMoveThreshold) {
// Fire button down event
guac_touchpad.currentState[button] = true;
if (guac_touchpad.onmousedown)
guac_touchpad.onmousedown(guac_touchpad.currentState);
// Delay mouse up - mouse up should be canceled if
// touchstart within timeout.
click_release_timeout = window.setTimeout(function() {
// Fire button up event
guac_touchpad.currentState[button] = false;
if (guac_touchpad.onmouseup)
guac_touchpad.onmouseup(guac_touchpad.currentState);
// Gesture now over
gesture_in_progress = false;
}, guac_touchpad.clickTimingThreshold);
}
// If we're not waiting to see if this is a click, stop gesture
if (!click_release_timeout)
gesture_in_progress = false;
}
}, false);
element.addEventListener("touchstart", function(e) {
e.stopPropagation();
e.preventDefault();
// Track number of touches, but no more than three
touch_count = Math.min(e.touches.length, 3);
// Clear timeout, if set
if (click_release_timeout) {
window.clearTimeout(click_release_timeout);
click_release_timeout = null;
}
// Record initial touch location and time for touch movement
// and tap gestures
if (!gesture_in_progress) {
// Stop mouse events while touching
gesture_in_progress = true;
// Record touch location and time
var starting_touch = e.touches[0];
last_touch_x = starting_touch.clientX;
last_touch_y = starting_touch.clientY;
last_touch_time = new Date().getTime();
pixels_moved = 0;
}
}, false);
element.addEventListener("touchmove", function(e) {
e.stopPropagation();
e.preventDefault();
// Get change in touch location
var touch = e.touches[0];
var delta_x = touch.clientX - last_touch_x;
var delta_y = touch.clientY - last_touch_y;
// Track pixels moved
pixels_moved += Math.abs(delta_x) + Math.abs(delta_y);
// If only one touch involved, this is mouse move
if (touch_count == 1) {
// Calculate average velocity in Manhatten pixels per millisecond
var velocity = pixels_moved / (new Date().getTime() - last_touch_time);
// Scale mouse movement relative to velocity
var scale = 1 + velocity;
// Update mouse location
guac_touchpad.currentState.x += delta_x*scale;
guac_touchpad.currentState.y += delta_y*scale;
// Prevent mouse from leaving screen
if (guac_touchpad.currentState.x < 0)
guac_touchpad.currentState.x = 0;
else if (guac_touchpad.currentState.x >= element.offsetWidth)
guac_touchpad.currentState.x = element.offsetWidth - 1;
if (guac_touchpad.currentState.y < 0)
guac_touchpad.currentState.y = 0;
else if (guac_touchpad.currentState.y >= element.offsetHeight)
guac_touchpad.currentState.y = element.offsetHeight - 1;
// Fire movement event, if defined
if (guac_touchpad.onmousemove)
guac_touchpad.onmousemove(guac_touchpad.currentState);
// Update touch location
last_touch_x = touch.clientX;
last_touch_y = touch.clientY;
}
// Interpret two-finger swipe as scrollwheel
else if (touch_count == 2) {
// If change in location passes threshold for scroll
if (Math.abs(delta_y) >= guac_touchpad.scrollThreshold) {
// Decide button based on Y movement direction
var button;
if (delta_y > 0) button = "down";
else button = "up";
// Fire button down event
guac_touchpad.currentState[button] = true;
if (guac_touchpad.onmousedown)
guac_touchpad.onmousedown(guac_touchpad.currentState);
// Fire button up event
guac_touchpad.currentState[button] = false;
if (guac_touchpad.onmouseup)
guac_touchpad.onmouseup(guac_touchpad.currentState);
// Only update touch location after a scroll has been
// detected
last_touch_x = touch.clientX;
last_touch_y = touch.clientY;
}
}
}, false);
};
/**
* Provides cross-browser absolute touch event translation for a given element.
*
* Touch events are translated into mouse events as if the touches occurred
* on a touchscreen (tapping anywhere on the screen clicks at that point,
* long-press to right-click).
*
* @constructor
* @param {Element} element The Element to use to provide touch events.
*/
Guacamole.Mouse.Touchscreen = function(element) {
/**
* Reference to this Guacamole.Mouse.Touchscreen.
* @private
*/
var guac_touchscreen = this;
/**
* The distance a two-finger touch must move per scrollwheel event, in
* pixels.
*/
this.scrollThreshold = 20 * (window.devicePixelRatio || 1);
/**
* The current mouse state. The properties of this state are updated when
* mouse events fire. This state object is also passed in as a parameter to
* the handler of any mouse events.
*
* @type Guacamole.Mouse.State
*/
this.currentState = new Guacamole.Mouse.State(
0, 0,
false, false, false, false, false
);
/**
* Fired whenever a mouse button is effectively pressed. This can happen
* as part of a "mousedown" gesture initiated by the user by pressing one
* finger over the touchscreen element, as part of a "scroll" gesture
* initiated by dragging two fingers up or down, etc.
*
* @event
* @param {Guacamole.Mouse.State} state The current mouse state.
*/
this.onmousedown = null;
/**
* Fired whenever a mouse button is effectively released. This can happen
* as part of a "mouseup" gesture initiated by the user by removing the
* finger pressed against the touchscreen element, or as part of a "scroll"
* gesture initiated by dragging two fingers up or down, etc.
*
* @event
* @param {Guacamole.Mouse.State} state The current mouse state.
*/
this.onmouseup = null;
/**
* Fired whenever the user moves the mouse by dragging their finger over
* the touchscreen element. Note that unlike Guacamole.Mouse.Touchpad,
* dragging a finger over the touchscreen element will always cause
* the mouse button to be effectively down, as if clicking-and-dragging.
*
* @event
* @param {Guacamole.Mouse.State} state The current mouse state.
*/
this.onmousemove = null;
element.addEventListener("touchend", function(e) {
// Ignore if more than one touch
if (e.touches.length + e.changedTouches.length != 1)
return;
e.stopPropagation();
e.preventDefault();
// Release button
guac_touchscreen.currentState.left = false;
// Fire release event when the last touch is released, if event defined
if (e.touches.length == 0 && guac_touchscreen.onmouseup)
guac_touchscreen.onmouseup(guac_touchscreen.currentState);
}, false);
element.addEventListener("touchstart", function(e) {
// Ignore if more than one touch
if (e.touches.length != 1)
return;
e.stopPropagation();
e.preventDefault();
// Get touch
var touch = e.touches[0];
// Update state
guac_touchscreen.currentState.left = true;
guac_touchscreen.currentState.fromClientPosition(element, touch.clientX, touch.clientY);
// Fire press event, if defined
if (guac_touchscreen.onmousedown)
guac_touchscreen.onmousedown(guac_touchscreen.currentState);
}, false);
element.addEventListener("touchmove", function(e) {
// Ignore if more than one touch
if (e.touches.length != 1)
return;
e.stopPropagation();
e.preventDefault();
// Get touch
var touch = e.touches[0];
// Update state
guac_touchscreen.currentState.fromClientPosition(element, touch.clientX, touch.clientY);
// Fire movement event, if defined
if (guac_touchscreen.onmousemove)
guac_touchscreen.onmousemove(guac_touchscreen.currentState);
}, false);
};

View File

@@ -0,0 +1,632 @@
/*
* Copyright (C) 2013 Glyptodon LLC
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
var Guacamole = Guacamole || {};
/**
* Dynamic on-screen keyboard. Given the URL to an XML keyboard layout file,
* this object will download and use the XML to construct a clickable on-screen
* keyboard with its own key events.
*
* @constructor
* @param {String} url The URL of an XML keyboard layout file.
*/
Guacamole.OnScreenKeyboard = function(url) {
var on_screen_keyboard = this;
/**
* State of all modifiers. This is the bitwise OR of all active modifier
* values.
*
* @private
*/
var modifiers = 0;
var scaledElements = [];
var modifier_masks = {};
var next_mask = 1;
/**
* Adds a class to an element.
*
* @private
* @function
* @param {Element} element The element to add a class to.
* @param {String} classname The name of the class to add.
*/
var addClass;
/**
* Removes a class from an element.
*
* @private
* @function
* @param {Element} element The element to remove a class from.
* @param {String} classname The name of the class to remove.
*/
var removeClass;
/**
* The number of mousemove events to require before re-enabling mouse
* event handling after receiving a touch event.
*/
this.touchMouseThreshold = 3;
/**
* Counter of mouse events to ignore. This decremented by mousemove, and
* while non-zero, mouse events will have no effect.
* @private
*/
var ignore_mouse = 0;
// Ignore all pending mouse events when touch events are the apparent source
function ignorePendingMouseEvents() { ignore_mouse = on_screen_keyboard.touchMouseThreshold; }
// If Node.classList is supported, implement addClass/removeClass using that
if (Node.classList) {
/** @ignore */
addClass = function(element, classname) {
element.classList.add(classname);
};
/** @ignore */
removeClass = function(element, classname) {
element.classList.remove(classname);
};
}
// Otherwise, implement own
else {
/** @ignore */
addClass = function(element, classname) {
// Simply add new class
element.className += " " + classname;
};
/** @ignore */
removeClass = function(element, classname) {
// Filter out classes with given name
element.className = element.className.replace(/([^ ]+)[ ]*/g,
function(match, testClassname, spaces, offset, string) {
// If same class, remove
if (testClassname == classname)
return "";
// Otherwise, allow
return match;
}
);
};
}
// Returns a unique power-of-two value for the modifier with the
// given name. The same value will be returned for the same modifier.
function getModifierMask(name) {
var value = modifier_masks[name];
if (!value) {
// Get current modifier, advance to next
value = next_mask;
next_mask <<= 1;
// Store value of this modifier
modifier_masks[name] = value;
}
return value;
}
function ScaledElement(element, width, height, scaleFont) {
this.width = width;
this.height = height;
this.scale = function(pixels) {
element.style.width = (width * pixels) + "px";
element.style.height = (height * pixels) + "px";
if (scaleFont) {
element.style.lineHeight = (height * pixels) + "px";
element.style.fontSize = pixels + "px";
}
}
}
// For each child of element, call handler defined in next
function parseChildren(element, next) {
var children = element.childNodes;
for (var i=0; i<children.length; i++) {
// Get child node
var child = children[i];
// Do not parse text nodes
if (!child.tagName)
continue;
// Get handler for node
var handler = next[child.tagName];
// Call handler if defined
if (handler)
handler(child);
// Throw exception if no handler
else
throw new Error(
"Unexpected " + child.tagName
+ " within " + element.tagName
);
}
}
// Create keyboard
var keyboard = document.createElement("div");
keyboard.className = "guac-keyboard";
// Retrieve keyboard XML
var xmlhttprequest = new XMLHttpRequest();
xmlhttprequest.open("GET", url, false);
xmlhttprequest.send(null);
var xml = xmlhttprequest.responseXML;
if (xml) {
function parse_row(e) {
var row = document.createElement("div");
row.className = "guac-keyboard-row";
parseChildren(e, {
"column": function(e) {
row.appendChild(parse_column(e));
},
"gap": function parse_gap(e) {
// Create element
var gap = document.createElement("div");
gap.className = "guac-keyboard-gap";
// Set gap size
var gap_units = 1;
if (e.getAttribute("size"))
gap_units = parseFloat(e.getAttribute("size"));
scaledElements.push(new ScaledElement(gap, gap_units, gap_units));
row.appendChild(gap);
},
"key": function parse_key(e) {
// Create element
var key_element = document.createElement("div");
key_element.className = "guac-keyboard-key";
// Append class if specified
if (e.getAttribute("class"))
key_element.className += " " + e.getAttribute("class");
// Position keys using container div
var key_container_element = document.createElement("div");
key_container_element.className = "guac-keyboard-key-container";
key_container_element.appendChild(key_element);
// Create key
var key = new Guacamole.OnScreenKeyboard.Key();
// Set key size
var key_units = 1;
if (e.getAttribute("size"))
key_units = parseFloat(e.getAttribute("size"));
key.size = key_units;
parseChildren(e, {
"cap": function parse_cap(e) {
// TODO: Handle "sticky" attribute
// Get content of key cap
var content = e.textContent || e.text;
// If read as blank, assume cap is a single space.
if (content.length == 0)
content = " ";
// Get keysym
var real_keysym = null;
if (e.getAttribute("keysym"))
real_keysym = parseInt(e.getAttribute("keysym"));
// If no keysym specified, try to get from key content
else if (content.length == 1) {
var charCode = content.charCodeAt(0);
if (charCode >= 0x0000 && charCode <= 0x00FF)
real_keysym = charCode;
else if (charCode >= 0x0100 && charCode <= 0x10FFFF)
real_keysym = 0x01000000 | charCode;
}
// Create cap
var cap = new Guacamole.OnScreenKeyboard.Cap(content, real_keysym);
if (e.getAttribute("modifier"))
cap.modifier = e.getAttribute("modifier");
// Create cap element
var cap_element = document.createElement("div");
cap_element.className = "guac-keyboard-cap";
cap_element.textContent = content;
key_element.appendChild(cap_element);
// Append class if specified
if (e.getAttribute("class"))
cap_element.className += " " + e.getAttribute("class");
// Get modifier value
var modifierValue = 0;
if (e.getAttribute("if")) {
// Get modifier value for specified comma-delimited
// list of required modifiers.
var requirements = e.getAttribute("if").split(",");
for (var i=0; i<requirements.length; i++) {
modifierValue |= getModifierMask(requirements[i]);
addClass(cap_element, "guac-keyboard-requires-" + requirements[i]);
addClass(key_element, "guac-keyboard-uses-" + requirements[i]);
}
}
// Store cap
key.modifierMask |= modifierValue;
key.caps[modifierValue] = cap;
}
});
scaledElements.push(new ScaledElement(key_container_element, key_units, 1, true));
row.appendChild(key_container_element);
// Set up click handler for key
function press() {
// Press key if not yet pressed
if (!key.pressed) {
addClass(key_element, "guac-keyboard-pressed");
// Get current cap based on modifier state
var cap = key.getCap(modifiers);
// Update modifier state
if (cap.modifier) {
// Construct classname for modifier
var modifierClass = "guac-keyboard-modifier-" + cap.modifier;
var modifierMask = getModifierMask(cap.modifier);
// Toggle modifier state
modifiers ^= modifierMask;
// Activate modifier if pressed
if (modifiers & modifierMask) {
addClass(keyboard, modifierClass);
// Send key event
if (on_screen_keyboard.onkeydown && cap.keysym)
on_screen_keyboard.onkeydown(cap.keysym);
}
// Deactivate if not pressed
else {
removeClass(keyboard, modifierClass);
// Send key event
if (on_screen_keyboard.onkeyup && cap.keysym)
on_screen_keyboard.onkeyup(cap.keysym);
}
}
// If not modifier, send key event now
else if (on_screen_keyboard.onkeydown && cap.keysym)
on_screen_keyboard.onkeydown(cap.keysym);
// Mark key as pressed
key.pressed = true;
}
}
function release() {
// Release key if currently pressed
if (key.pressed) {
// Get current cap based on modifier state
var cap = key.getCap(modifiers);
removeClass(key_element, "guac-keyboard-pressed");
// Send key event if not a modifier key
if (!cap.modifier && on_screen_keyboard.onkeyup && cap.keysym)
on_screen_keyboard.onkeyup(cap.keysym);
// Mark key as released
key.pressed = false;
}
}
function touchPress(e) {
e.preventDefault();
ignore_mouse = on_screen_keyboard.touchMouseThreshold;
press();
}
function touchRelease(e) {
e.preventDefault();
ignore_mouse = on_screen_keyboard.touchMouseThreshold;
release();
}
function mousePress(e) {
e.preventDefault();
if (ignore_mouse == 0)
press();
}
function mouseRelease(e) {
e.preventDefault();
if (ignore_mouse == 0)
release();
}
key_element.addEventListener("touchstart", touchPress, true);
key_element.addEventListener("touchend", touchRelease, true);
key_element.addEventListener("mousedown", mousePress, true);
key_element.addEventListener("mouseup", mouseRelease, true);
key_element.addEventListener("mouseout", mouseRelease, true);
}
});
return row;
}
function parse_column(e) {
var col = document.createElement("div");
col.className = "guac-keyboard-column";
if (col.getAttribute("align"))
col.style.textAlign = col.getAttribute("align");
// Columns can only contain rows
parseChildren(e, {
"row": function(e) {
col.appendChild(parse_row(e));
}
});
return col;
}
// Parse document
var keyboard_element = xml.documentElement;
if (keyboard_element.tagName != "keyboard")
throw new Error("Root element must be keyboard");
// Get attributes
if (!keyboard_element.getAttribute("size"))
throw new Error("size attribute is required for keyboard");
var keyboard_size = parseFloat(keyboard_element.getAttribute("size"));
parseChildren(keyboard_element, {
"row": function(e) {
keyboard.appendChild(parse_row(e));
},
"column": function(e) {
keyboard.appendChild(parse_column(e));
}
});
}
// Do not allow selection or mouse movement to propagate/register.
keyboard.onselectstart =
keyboard.onmousemove =
keyboard.onmouseup =
keyboard.onmousedown =
function(e) {
// If ignoring events, decrement counter
if (ignore_mouse)
ignore_mouse--;
e.stopPropagation();
return false;
};
/**
* Fired whenever the user presses a key on this Guacamole.OnScreenKeyboard.
*
* @event
* @param {Number} keysym The keysym of the key being pressed.
*/
this.onkeydown = null;
/**
* Fired whenever the user releases a key on this Guacamole.OnScreenKeyboard.
*
* @event
* @param {Number} keysym The keysym of the key being released.
*/
this.onkeyup = null;
/**
* Returns the element containing the entire on-screen keyboard.
* @returns {Element} The element containing the entire on-screen keyboard.
*/
this.getElement = function() {
return keyboard;
};
/**
* Resizes all elements within this Guacamole.OnScreenKeyboard such that
* the width is close to but does not exceed the specified width. The
* height of the keyboard is determined based on the width.
*
* @param {Number} width The width to resize this Guacamole.OnScreenKeyboard
* to, in pixels.
*/
this.resize = function(width) {
// Get pixel size of a unit
var unit = Math.floor(width * 10 / keyboard_size) / 10;
// Resize all scaled elements
for (var i=0; i<scaledElements.length; i++) {
var scaledElement = scaledElements[i];
scaledElement.scale(unit)
}
};
};
/**
* Basic representation of a single key of a keyboard. Each key has a set of
* caps associated with tuples of modifiers. The cap determins what happens
* when a key is pressed, while it is the state of modifier keys that determines
* what cap is in effect on any particular key.
*
* @constructor
*/
Guacamole.OnScreenKeyboard.Key = function() {
var key = this;
/**
* Whether this key is currently pressed.
*/
this.pressed = false;
/**
* Width of the key, relative to the size of the keyboard.
*/
this.size = 1;
/**
* An associative map of all caps by modifier.
*/
this.caps = {};
/**
* Bit mask with all modifiers that affect this key set.
*/
this.modifierMask = 0;
/**
* Given the bitwise OR of all active modifiers, returns the key cap
* which applies.
*/
this.getCap = function(modifier) {
return key.caps[modifier & key.modifierMask];
};
};
/**
* Basic representation of a cap of a key. The cap is the visible part of a key
* and determines the active behavior of a key when pressed. The state of all
* modifiers on the keyboard determines the active cap for all keys, thus
* each cap is associated with a set of modifiers.
*
* @constructor
* @param {String} text The text to be displayed within this cap.
* @param {Number} keysym The keysym this cap sends when its associated key is
* pressed or released.
* @param {String} modifier The modifier represented by this cap.
*/
Guacamole.OnScreenKeyboard.Cap = function(text, keysym, modifier) {
/**
* Modifier represented by this keycap
*/
this.modifier = null;
/**
* The text to be displayed within this keycap
*/
this.text = text;
/**
* The keysym this cap sends when its associated key is pressed/released
*/
this.keysym = keysym;
// Set modifier if provided
if (modifier) this.modifier = modifier;
};

View File

@@ -0,0 +1,81 @@
/*
* Copyright (C) 2013 Glyptodon LLC
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
var Guacamole = Guacamole || {};
/**
* Abstract stream which can receive data.
*
* @constructor
* @param {Guacamole.Client} client The client owning this stream.
* @param {Number} index The index of this stream.
*/
Guacamole.OutputStream = function(client, index) {
/**
* Reference to this stream.
* @private
*/
var guac_stream = this;
/**
* The index of this stream.
* @type Number
*/
this.index = index;
/**
* Fired when the stream is being closed due to an error.
*
* @param {String} reason A human-readable reason describing the error.
* @param {Number} code The error code associated with the error.
*/
this.onerror = null;
/**
* Fired whenever an acknowledgement is received from the server, indicating
* that a stream operation has completed.
*
* @event
* @param {String} message A human-readable status message related to the
* operation performed.
* @param {Number} code The error code associated with the operation.
*/
this.onack = null;
/**
* Writes the given base64-encoded data to this stream as a blob.
*
* @param {String} data The base64-encoded data to send.
*/
this.write = function(data) {
client.sendBlob(guac_stream.index, data);
};
/**
* Closes this stream.
*/
this.close = function() {
client.endStream(guac_stream.index);
};
};

View File

@@ -0,0 +1,159 @@
/*
* Copyright (C) 2013 Glyptodon LLC
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
var Guacamole = Guacamole || {};
/**
* Simple Guacamole protocol parser that invokes an oninstruction event when
* full instructions are available from data received via receive().
*
* @constructor
*/
Guacamole.Parser = function() {
/**
* Reference to this parser.
* @private
*/
var parser = this;
/**
* Current buffer of received data. This buffer grows until a full
* element is available. After a full element is available, that element
* is flushed into the element buffer.
*
* @private
*/
var buffer = "";
/**
* Buffer of all received, complete elements. After an entire instruction
* is read, this buffer is flushed, and a new instruction begins.
*
* @private
*/
var element_buffer = [];
// The location of the last element's terminator
var element_end = -1;
// Where to start the next length search or the next element
var start_index = 0;
/**
* Appends the given instruction data packet to the internal buffer of
* this Guacamole.Parser, executing all completed instructions at
* the beginning of this buffer, if any.
*
* @param {String} packet The instruction data to receive.
*/
this.receive = function(packet) {
// Truncate buffer as necessary
if (start_index > 4096 && element_end >= start_index) {
buffer = buffer.substring(start_index);
// Reset parse relative to truncation
element_end -= start_index;
start_index = 0;
}
// Append data to buffer
buffer += packet;
// While search is within currently received data
while (element_end < buffer.length) {
// If we are waiting for element data
if (element_end >= start_index) {
// We now have enough data for the element. Parse.
var element = buffer.substring(start_index, element_end);
var terminator = buffer.substring(element_end, element_end+1);
// Add element to array
element_buffer.push(element);
// If last element, handle instruction
if (terminator == ";") {
// Get opcode
var opcode = element_buffer.shift();
// Call instruction handler.
if (parser.oninstruction != null)
parser.oninstruction(opcode, element_buffer);
// Clear elements
element_buffer.length = 0;
}
else if (terminator != ',')
throw new Error("Illegal terminator.");
// Start searching for length at character after
// element terminator
start_index = element_end + 1;
}
// Search for end of length
var length_end = buffer.indexOf(".", start_index);
if (length_end != -1) {
// Parse length
var length = parseInt(buffer.substring(element_end+1, length_end));
if (length == NaN)
throw new Error("Non-numeric character in element length.");
// Calculate start of element
start_index = length_end + 1;
// Calculate location of element terminator
element_end = start_index + length;
}
// If no period yet, continue search when more data
// is received
else {
start_index = buffer.length;
break;
}
} // end parse loop
};
/**
* Fired once for every complete Guacamole instruction received, in order.
*
* @event
* @param {String} opcode The Guacamole instruction opcode.
* @param {Array} parameters The parameters provided for the instruction,
* if any.
*/
this.oninstruction = null;
};

View File

@@ -0,0 +1,809 @@
/*
* Copyright (C) 2013 Glyptodon LLC
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
var Guacamole = Guacamole || {};
/**
* Core object providing abstract communication for Guacamole. This object
* is a null implementation whose functions do nothing. Guacamole applications
* should use {@link Guacamole.HTTPTunnel} instead, or implement their own tunnel based
* on this one.
*
* @constructor
* @see Guacamole.HTTPTunnel
*/
Guacamole.Tunnel = function() {
/**
* Connect to the tunnel with the given optional data. This data is
* typically used for authentication. The format of data accepted is
* up to the tunnel implementation.
*
* @param {String} data The data to send to the tunnel when connecting.
*/
this.connect = function(data) {};
/**
* Disconnect from the tunnel.
*/
this.disconnect = function() {};
/**
* Send the given message through the tunnel to the service on the other
* side. All messages are guaranteed to be received in the order sent.
*
* @param {...} elements The elements of the message to send to the
* service on the other side of the tunnel.
*/
this.sendMessage = function(elements) {};
/**
* Fired whenever an error is encountered by the tunnel.
*
* @event
* @param {String} message A human-readable description of the error that
* occurred.
*/
this.onerror = null;
/**
* Fired once for every complete Guacamole instruction received, in order.
*
* @event
* @param {String} opcode The Guacamole instruction opcode.
* @param {Array} parameters The parameters provided for the instruction,
* if any.
*/
this.oninstruction = null;
};
/**
* Guacamole Tunnel implemented over HTTP via XMLHttpRequest.
*
* @constructor
* @augments Guacamole.Tunnel
* @param {String} tunnelURL The URL of the HTTP tunneling service.
*/
Guacamole.HTTPTunnel = function(tunnelURL) {
/**
* Reference to this HTTP tunnel.
* @private
*/
var tunnel = this;
var tunnel_uuid;
var TUNNEL_CONNECT = tunnelURL + "?connect";
var TUNNEL_READ = tunnelURL + "?read:";
var TUNNEL_WRITE = tunnelURL + "?write:";
var STATE_IDLE = 0;
var STATE_CONNECTED = 1;
var STATE_DISCONNECTED = 2;
var currentState = STATE_IDLE;
var POLLING_ENABLED = 1;
var POLLING_DISABLED = 0;
// Default to polling - will be turned off automatically if not needed
var pollingMode = POLLING_ENABLED;
var sendingMessages = false;
var outputMessageBuffer = "";
this.sendMessage = function() {
// Do not attempt to send messages if not connected
if (currentState != STATE_CONNECTED)
return;
// Do not attempt to send empty messages
if (arguments.length == 0)
return;
/**
* Converts the given value to a length/string pair for use as an
* element in a Guacamole instruction.
*
* @private
* @param value The value to convert.
* @return {String} The converted value.
*/
function getElement(value) {
var string = new String(value);
return string.length + "." + string;
}
// Initialized message with first element
var message = getElement(arguments[0]);
// Append remaining elements
for (var i=1; i<arguments.length; i++)
message += "," + getElement(arguments[i]);
// Final terminator
message += ";";
// Add message to buffer
outputMessageBuffer += message;
// Send if not currently sending
if (!sendingMessages)
sendPendingMessages();
};
function sendPendingMessages() {
if (outputMessageBuffer.length > 0) {
sendingMessages = true;
var message_xmlhttprequest = new XMLHttpRequest();
message_xmlhttprequest.open("POST", TUNNEL_WRITE + tunnel_uuid);
message_xmlhttprequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded; charset=UTF-8");
// Once response received, send next queued event.
message_xmlhttprequest.onreadystatechange = function() {
if (message_xmlhttprequest.readyState == 4) {
// If an error occurs during send, handle it
if (message_xmlhttprequest.status != 200)
handleHTTPTunnelError(message_xmlhttprequest);
// Otherwise, continue the send loop
else
sendPendingMessages();
}
}
message_xmlhttprequest.send(outputMessageBuffer);
outputMessageBuffer = ""; // Clear buffer
}
else
sendingMessages = false;
}
function getHTTPTunnelErrorMessage(xmlhttprequest) {
var status = xmlhttprequest.status;
// Special cases
if (status == 0) return "Disconnected";
if (status == 200) return "Success";
if (status == 403) return "Unauthorized";
if (status == 404) return "Connection closed"; /* While it may be more
* accurate to say the
* connection does not
* exist, it is confusing
* to the user.
*
* In general, this error
* will only happen when
* the tunnel does not
* exist, which happens
* after the connection
* is closed and the
* tunnel is detached.
*/
// Internal server errors
if (status >= 500 && status <= 599) return "Server error";
// Otherwise, unknown
return "Unknown error";
}
function handleHTTPTunnelError(xmlhttprequest) {
// Get error message
var message = getHTTPTunnelErrorMessage(xmlhttprequest);
// Call error handler
if (tunnel.onerror) tunnel.onerror(message);
// Finish
tunnel.disconnect();
}
function handleResponse(xmlhttprequest) {
var interval = null;
var nextRequest = null;
var dataUpdateEvents = 0;
// The location of the last element's terminator
var elementEnd = -1;
// Where to start the next length search or the next element
var startIndex = 0;
// Parsed elements
var elements = new Array();
function parseResponse() {
// Do not handle responses if not connected
if (currentState != STATE_CONNECTED) {
// Clean up interval if polling
if (interval != null)
clearInterval(interval);
return;
}
// Do not parse response yet if not ready
if (xmlhttprequest.readyState < 2) return;
// Attempt to read status
var status;
try { status = xmlhttprequest.status; }
// If status could not be read, assume successful.
catch (e) { status = 200; }
// Start next request as soon as possible IF request was successful
if (nextRequest == null && status == 200)
nextRequest = makeRequest();
// Parse stream when data is received and when complete.
if (xmlhttprequest.readyState == 3 ||
xmlhttprequest.readyState == 4) {
// Also poll every 30ms (some browsers don't repeatedly call onreadystatechange for new data)
if (pollingMode == POLLING_ENABLED) {
if (xmlhttprequest.readyState == 3 && interval == null)
interval = setInterval(parseResponse, 30);
else if (xmlhttprequest.readyState == 4 && interval != null)
clearInterval(interval);
}
// If canceled, stop transfer
if (xmlhttprequest.status == 0) {
tunnel.disconnect();
return;
}
// Halt on error during request
else if (xmlhttprequest.status != 200) {
handleHTTPTunnelError(xmlhttprequest);
return;
}
// Attempt to read in-progress data
var current;
try { current = xmlhttprequest.responseText; }
// Do not attempt to parse if data could not be read
catch (e) { return; }
// While search is within currently received data
while (elementEnd < current.length) {
// If we are waiting for element data
if (elementEnd >= startIndex) {
// We now have enough data for the element. Parse.
var element = current.substring(startIndex, elementEnd);
var terminator = current.substring(elementEnd, elementEnd+1);
// Add element to array
elements.push(element);
// If last element, handle instruction
if (terminator == ";") {
// Get opcode
var opcode = elements.shift();
// Call instruction handler.
if (tunnel.oninstruction != null)
tunnel.oninstruction(opcode, elements);
// Clear elements
elements.length = 0;
}
// Start searching for length at character after
// element terminator
startIndex = elementEnd + 1;
}
// Search for end of length
var lengthEnd = current.indexOf(".", startIndex);
if (lengthEnd != -1) {
// Parse length
var length = parseInt(current.substring(elementEnd+1, lengthEnd));
// If we're done parsing, handle the next response.
if (length == 0) {
// Clean up interval if polling
if (interval != null)
clearInterval(interval);
// Clean up object
xmlhttprequest.onreadystatechange = null;
xmlhttprequest.abort();
// Start handling next request
if (nextRequest)
handleResponse(nextRequest);
// Done parsing
break;
}
// Calculate start of element
startIndex = lengthEnd + 1;
// Calculate location of element terminator
elementEnd = startIndex + length;
}
// If no period yet, continue search when more data
// is received
else {
startIndex = current.length;
break;
}
} // end parse loop
}
}
// If response polling enabled, attempt to detect if still
// necessary (via wrapping parseResponse())
if (pollingMode == POLLING_ENABLED) {
xmlhttprequest.onreadystatechange = function() {
// If we receive two or more readyState==3 events,
// there is no need to poll.
if (xmlhttprequest.readyState == 3) {
dataUpdateEvents++;
if (dataUpdateEvents >= 2) {
pollingMode = POLLING_DISABLED;
xmlhttprequest.onreadystatechange = parseResponse;
}
}
parseResponse();
}
}
// Otherwise, just parse
else
xmlhttprequest.onreadystatechange = parseResponse;
parseResponse();
}
/**
* Arbitrary integer, unique for each tunnel read request.
* @private
*/
var request_id = 0;
function makeRequest() {
// Make request, increment request ID
var xmlhttprequest = new XMLHttpRequest();
xmlhttprequest.open("GET", TUNNEL_READ + tunnel_uuid + ":" + (request_id++));
xmlhttprequest.send(null);
return xmlhttprequest;
}
this.connect = function(data) {
// Start tunnel and connect synchronously
var connect_xmlhttprequest = new XMLHttpRequest();
connect_xmlhttprequest.open("POST", TUNNEL_CONNECT, false);
connect_xmlhttprequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded; charset=UTF-8");
connect_xmlhttprequest.send(data);
// If failure, throw error
if (connect_xmlhttprequest.status != 200) {
var message = getHTTPTunnelErrorMessage(connect_xmlhttprequest);
throw new Error(message);
}
// Get UUID from response
tunnel_uuid = connect_xmlhttprequest.responseText;
// Start reading data
currentState = STATE_CONNECTED;
handleResponse(makeRequest());
};
this.disconnect = function() {
currentState = STATE_DISCONNECTED;
};
};
Guacamole.HTTPTunnel.prototype = new Guacamole.Tunnel();
/**
* Guacamole Tunnel implemented over WebSocket via XMLHttpRequest.
*
* @constructor
* @augments Guacamole.Tunnel
* @param {String} tunnelURL The URL of the WebSocket tunneling service.
*/
Guacamole.WebSocketTunnel = function(tunnelURL) {
/**
* Reference to this WebSocket tunnel.
* @private
*/
var tunnel = this;
/**
* The WebSocket used by this tunnel.
* @private
*/
var socket = null;
/**
* The WebSocket protocol corresponding to the protocol used for the current
* location.
* @private
*/
var ws_protocol = {
"http:": "ws:",
"https:": "wss:"
};
var status_code = {
1000: "Connection closed normally.",
1001: "Connection shut down.",
1002: "Protocol error.",
1003: "Invalid data.",
1004: "[UNKNOWN, RESERVED]",
1005: "No status code present.",
1006: "Connection closed abnormally.",
1007: "Inconsistent data type.",
1008: "Policy violation.",
1009: "Message too large.",
1010: "Extension negotiation failed."
};
var STATE_IDLE = 0;
var STATE_CONNECTED = 1;
var STATE_DISCONNECTED = 2;
var currentState = STATE_IDLE;
// Transform current URL to WebSocket URL
// If not already a websocket URL
if ( tunnelURL.substring(0, 3) != "ws:"
&& tunnelURL.substring(0, 4) != "wss:") {
var protocol = ws_protocol[window.location.protocol];
// If absolute URL, convert to absolute WS URL
if (tunnelURL.substring(0, 1) == "/")
tunnelURL =
protocol
+ "//" + window.location.host
+ tunnelURL;
// Otherwise, construct absolute from relative URL
else {
// Get path from pathname
var slash = window.location.pathname.lastIndexOf("/");
var path = window.location.pathname.substring(0, slash + 1);
// Construct absolute URL
tunnelURL =
protocol
+ "//" + window.location.host
+ path
+ tunnelURL;
}
}
this.sendMessage = function(elements) {
// Do not attempt to send messages if not connected
if (currentState != STATE_CONNECTED)
return;
// Do not attempt to send empty messages
if (arguments.length == 0)
return;
/**
* Converts the given value to a length/string pair for use as an
* element in a Guacamole instruction.
*
* @private
* @param value The value to convert.
* @return {String} The converted value.
*/
function getElement(value) {
var string = new String(value);
return string.length + "." + string;
}
// Initialized message with first element
var message = getElement(arguments[0]);
// Append remaining elements
for (var i=1; i<arguments.length; i++)
message += "," + getElement(arguments[i]);
// Final terminator
message += ";";
socket.send(message);
};
this.connect = function(data) {
// Connect socket
socket = new WebSocket(tunnelURL + "?" + data, "guacamole");
socket.onopen = function(event) {
currentState = STATE_CONNECTED;
};
socket.onclose = function(event) {
// If connection closed abnormally, signal error.
if (event.code != 1000 && tunnel.onerror)
tunnel.onerror(status_code[event.code]);
};
socket.onerror = function(event) {
// Call error handler
if (tunnel.onerror) tunnel.onerror(event.data);
};
socket.onmessage = function(event) {
var message = event.data;
var startIndex = 0;
var elementEnd;
var elements = [];
do {
// Search for end of length
var lengthEnd = message.indexOf(".", startIndex);
if (lengthEnd != -1) {
// Parse length
var length = parseInt(message.substring(elementEnd+1, lengthEnd));
// Calculate start of element
startIndex = lengthEnd + 1;
// Calculate location of element terminator
elementEnd = startIndex + length;
}
// If no period, incomplete instruction.
else
throw new Error("Incomplete instruction.");
// We now have enough data for the element. Parse.
var element = message.substring(startIndex, elementEnd);
var terminator = message.substring(elementEnd, elementEnd+1);
// Add element to array
elements.push(element);
// If last element, handle instruction
if (terminator == ";") {
// Get opcode
var opcode = elements.shift();
// Call instruction handler.
if (tunnel.oninstruction != null)
tunnel.oninstruction(opcode, elements);
// Clear elements
elements.length = 0;
}
// Start searching for length at character after
// element terminator
startIndex = elementEnd + 1;
} while (startIndex < message.length);
};
};
this.disconnect = function() {
currentState = STATE_DISCONNECTED;
socket.close();
};
};
Guacamole.WebSocketTunnel.prototype = new Guacamole.Tunnel();
/**
* Guacamole Tunnel which cycles between all specified tunnels until
* no tunnels are left. Another tunnel is used if an error occurs but
* no instructions have been received. If an instruction has been
* received, or no tunnels remain, the error is passed directly out
* through the onerror handler (if defined).
*
* @constructor
* @augments Guacamole.Tunnel
* @param {...} tunnel_chain The tunnels to use, in order of priority.
*/
Guacamole.ChainedTunnel = function(tunnel_chain) {
/**
* Reference to this chained tunnel.
* @private
*/
var chained_tunnel = this;
/**
* The currently wrapped tunnel, if any.
* @private
*/
var current_tunnel = null;
/**
* Data passed in via connect(), to be used for
* wrapped calls to other tunnels' connect() functions.
* @private
*/
var connect_data;
/**
* Array of all tunnels passed to this ChainedTunnel through the
* constructor arguments.
* @private
*/
var tunnels = [];
// Load all tunnels into array
for (var i=0; i<arguments.length; i++)
tunnels.push(arguments[i]);
/**
* Sets the current tunnel.
*
* @private
* @param {Guacamole.Tunnel} tunnel The tunnel to set as the current tunnel.
*/
function attach(tunnel) {
// Clear handlers of current tunnel, if any
if (current_tunnel) {
current_tunnel.onerror = null;
current_tunnel.oninstruction = null;
}
// Set own functions to tunnel's functions
chained_tunnel.disconnect = tunnel.disconnect;
chained_tunnel.sendMessage = tunnel.sendMessage;
// Record current tunnel
current_tunnel = tunnel;
// Wrap own oninstruction within current tunnel
current_tunnel.oninstruction = function(opcode, elements) {
// Invoke handler
chained_tunnel.oninstruction(opcode, elements);
// Use handler permanently from now on
current_tunnel.oninstruction = chained_tunnel.oninstruction;
// Pass through errors (without trying other tunnels)
current_tunnel.onerror = chained_tunnel.onerror;
}
// Attach next tunnel on error
current_tunnel.onerror = function(message) {
// Get next tunnel
var next_tunnel = tunnels.shift();
// If there IS a next tunnel, try using it.
if (next_tunnel)
attach(next_tunnel);
// Otherwise, call error handler
else if (chained_tunnel.onerror)
chained_tunnel.onerror(message);
};
try {
// Attempt connection
current_tunnel.connect(connect_data);
}
catch (e) {
// Call error handler of current tunnel on error
current_tunnel.onerror(e.message);
}
}
this.connect = function(data) {
// Remember connect data
connect_data = data;
// Get first tunnel
var next_tunnel = tunnels.shift();
// Attach first tunnel
if (next_tunnel)
attach(next_tunnel);
// If there IS no first tunnel, error
else if (chained_tunnel.onerror)
chained_tunnel.onerror("No tunnels to try.");
};
};
Guacamole.ChainedTunnel.prototype = new Guacamole.Tunnel();