2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
15 * The Original Code is guac-common-js.
17 * The Initial Developer of the Original Code is
19 * Portions created by the Initial Developer are Copyright (C) 2010
20 * the Initial Developer. All Rights Reserved.
24 * Alternatively, the contents of this file may be used under the terms of
25 * either the GNU General Public License Version 2 or later (the "GPL"), or
26 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 * in which case the provisions of the GPL or the LGPL are applicable instead
28 * of those above. If you wish to allow use of your version of this file only
29 * under the terms of either the GPL or the LGPL, and not to allow others to
30 * use your version of this file under the terms of the MPL, indicate your
31 * decision by deleting the provisions above and replace them with the notice
32 * and other provisions required by the GPL or the LGPL. If you do not delete
33 * the provisions above, a recipient may use your version of this file under
34 * the terms of any one of the MPL, the GPL or the LGPL.
36 * ***** END LICENSE BLOCK ***** */
38 // Guacamole namespace
39 var Guacamole = Guacamole || {};
42 * Dynamic on-screen keyboard. Given the URL to an XML keyboard layout file,
43 * this object will download and use the XML to construct a clickable on-screen
44 * keyboard with its own key events.
47 * @param {String} url The URL of an XML keyboard layout file.
49 Guacamole.OnScreenKeyboard = function(url) {
51 var on_screen_keyboard = this;
53 var scaledElements = [];
56 var currentModifier = 1;
58 // Function for adding a class to an element
61 // Function for removing a class from an element
64 // If Node.classList is supported, implement addClass/removeClass using that
67 addClass = function(element, classname) {
68 element.classList.add(classname);
71 removeClass = function(element, classname) {
72 element.classList.remove(classname);
77 // Otherwise, implement own
80 addClass = function(element, classname) {
82 // Simply add new class
83 element.className += " " + classname;
87 removeClass = function(element, classname) {
89 // Filter out classes with given name
90 element.className = element.className.replace(/([^ ]+)[ ]*/g,
91 function(match, testClassname, spaces, offset, string) {
93 // If same class, remove
94 if (testClassname == classname)
107 // Returns a unique power-of-two value for the modifier with the
108 // given name. The same value will be returned for the same modifier.
109 function getModifier(name) {
111 var value = modifiers[name];
114 // Get current modifier, advance to next
115 value = currentModifier;
116 currentModifier <<= 1;
118 // Store value of this modifier
119 modifiers[name] = value;
127 function ScaledElement(element, width, height, scaleFont) {
130 this.height = height;
132 this.scale = function(pixels) {
133 element.style.width = (width * pixels) + "px";
134 element.style.height = (height * pixels) + "px";
137 element.style.lineHeight = (height * pixels) + "px";
138 element.style.fontSize = pixels + "px";
144 // For each child of element, call handler defined in next
145 function parseChildren(element, next) {
147 var children = element.childNodes;
148 for (var i=0; i<children.length; i++) {
151 var child = children[i];
153 // Do not parse text nodes
157 // Get handler for node
158 var handler = next[child.tagName];
160 // Call handler if defined
164 // Throw exception if no handler
167 "Unexpected " + child.tagName
168 + " within " + element.tagName
176 var keyboard = document.createElement("div");
177 keyboard.className = "guac-keyboard";
179 // Retrieve keyboard XML
180 var xmlhttprequest = new XMLHttpRequest();
181 xmlhttprequest.open("GET", url, false);
182 xmlhttprequest.send(null);
184 var xml = xmlhttprequest.responseXML;
188 function parse_row(e) {
190 var row = document.createElement("div");
191 row.className = "guac-keyboard-row";
195 "column": function(e) {
196 row.appendChild(parse_column(e));
199 "gap": function parse_gap(e) {
202 var gap = document.createElement("div");
203 gap.className = "guac-keyboard-gap";
207 if (e.getAttribute("size"))
208 gap_units = parseFloat(e.getAttribute("size"));
210 scaledElements.push(new ScaledElement(gap, gap_units, gap_units));
211 row.appendChild(gap);
215 "key": function parse_key(e) {
218 var key_element = document.createElement("div");
219 key_element.className = "guac-keyboard-key";
221 // Append class if specified
222 if (e.getAttribute("class"))
223 key_element.className += " " + e.getAttribute("class");
225 // Position keys using container div
226 var key_container_element = document.createElement("div");
227 key_container_element.className = "guac-keyboard-key-container";
228 key_container_element.appendChild(key_element);
231 var key = new Guacamole.OnScreenKeyboard.Key();
235 if (e.getAttribute("size"))
236 key_units = parseFloat(e.getAttribute("size"));
238 key.size = key_units;
241 "cap": function parse_cap(e) {
243 // TODO: Handle "sticky" attribute
245 // Get content of key cap
246 var content = e.textContent || e.text;
249 var real_keysym = null;
250 if (e.getAttribute("keysym"))
251 real_keysym = parseInt(e.getAttribute("keysym"));
253 // If no keysym specified, try to get from key content
254 else if (content.length == 1) {
256 var charCode = content.charCodeAt(0);
257 if (charCode >= 0x0000 && charCode <= 0x00FF)
258 real_keysym = charCode;
259 else if (charCode >= 0x0100 && charCode <= 0x10FFFF)
260 real_keysym = 0x01000000 | charCode;
265 var cap = new Guacamole.OnScreenKeyboard.Cap(content, real_keysym);
267 if (e.getAttribute("modifier"))
268 cap.modifier = e.getAttribute("modifier");
270 // Create cap element
271 var cap_element = document.createElement("div");
272 cap_element.className = "guac-keyboard-cap";
273 cap_element.textContent = content;
274 key_element.appendChild(cap_element);
276 // Append class if specified
277 if (e.getAttribute("class"))
278 cap_element.className += " " + e.getAttribute("class");
280 // Get modifier value
281 var modifierValue = 0;
282 if (e.getAttribute("if")) {
284 // Get modifier value for specified comma-delimited
285 // list of required modifiers.
286 var requirements = e.getAttribute("if").split(",");
287 for (var i=0; i<requirements.length; i++) {
288 modifierValue |= getModifier(requirements[i]);
289 addClass(cap_element, "guac-keyboard-requires-" + requirements[i]);
290 addClass(key_element, "guac-keyboard-uses-" + requirements[i]);
296 key.modifierMask |= modifierValue;
297 key.caps[modifierValue] = cap;
302 scaledElements.push(new ScaledElement(key_container_element, key_units, 1, true));
303 row.appendChild(key_container_element);
305 // Set up click handler for key
308 // Press key if not yet pressed
311 addClass(key_element, "guac-keyboard-pressed");
313 // Get current cap based on modifier state
314 var cap = key.getCap(on_screen_keyboard.modifiers);
316 // Update modifier state
319 // Construct classname for modifier
320 var modifierClass = "guac-keyboard-modifier-" + cap.modifier;
321 var modifierFlag = getModifier(cap.modifier);
323 // Toggle modifier state
324 on_screen_keyboard.modifiers ^= modifierFlag;
326 // Activate modifier if pressed
327 if (on_screen_keyboard.modifiers & modifierFlag) {
329 addClass(keyboard, modifierClass);
332 if (on_screen_keyboard.onkeydown && cap.keysym)
333 on_screen_keyboard.onkeydown(cap.keysym);
337 // Deactivate if not pressed
340 removeClass(keyboard, modifierClass);
343 if (on_screen_keyboard.onkeyup && cap.keysym)
344 on_screen_keyboard.onkeyup(cap.keysym);
350 // If not modifier, send key event now
351 else if (on_screen_keyboard.onkeydown && cap.keysym)
352 on_screen_keyboard.onkeydown(cap.keysym);
354 // Mark key as pressed
363 function release(e) {
365 // Release key if currently pressed
368 // Get current cap based on modifier state
369 var cap = key.getCap(on_screen_keyboard.modifiers);
371 removeClass(key_element, "guac-keyboard-pressed");
373 // Send key event if not a modifier key
374 if (!cap.modifier && on_screen_keyboard.onkeyup && cap.keysym)
375 on_screen_keyboard.onkeyup(cap.keysym);
377 // Mark key as released
386 key_element.addEventListener("mousedown", press, true);
387 key_element.addEventListener("touchstart", press, true);
389 key_element.addEventListener("mouseup", release, true);
390 key_element.addEventListener("mouseout", release, true);
391 key_element.addEventListener("touchend", release, true);
401 function parse_column(e) {
403 var col = document.createElement("div");
404 col.className = "guac-keyboard-column";
406 if (col.getAttribute("align"))
407 col.style.textAlign = col.getAttribute("align");
409 // Columns can only contain rows
412 col.appendChild(parse_row(e));
422 var keyboard_element = xml.documentElement;
423 if (keyboard_element.tagName != "keyboard")
424 throw new Error("Root element must be keyboard");
427 if (!keyboard_element.getAttribute("size"))
428 throw new Error("size attribute is required for keyboard");
430 var keyboard_size = parseFloat(keyboard_element.getAttribute("size"));
432 parseChildren(keyboard_element, {
435 keyboard.appendChild(parse_row(e));
438 "column": function(e) {
439 keyboard.appendChild(parse_column(e));
446 // Do not allow selection or mouse movement to propagate/register.
447 keyboard.onselectstart =
448 keyboard.onmousemove =
450 keyboard.onmousedown =
457 * State of all modifiers.
461 this.onkeydown = null;
464 this.getElement = function() {
468 this.resize = function(width) {
470 // Get pixel size of a unit
471 var unit = Math.floor(width * 10 / keyboard_size) / 10;
473 // Resize all scaled elements
474 for (var i=0; i<scaledElements.length; i++) {
475 var scaledElement = scaledElements[i];
476 scaledElement.scale(unit)
483 Guacamole.OnScreenKeyboard.Key = function() {
488 * Whether this key is currently pressed.
490 this.pressed = false;
493 * Width of the key, relative to the size of the keyboard.
498 * An associative map of all caps by modifier.
503 * Bit mask with all modifiers that affect this key set.
505 this.modifierMask = 0;
508 * Given the bitwise OR of all active modifiers, returns the key cap
511 this.getCap = function(modifier) {
512 return key.caps[modifier & key.modifierMask];
517 Guacamole.OnScreenKeyboard.Cap = function(text, keysym, modifier) {
520 * Modifier represented by this keycap
522 this.modifier = null;
525 * The text to be displayed within this keycap
530 * The keysym this cap sends when its associated key is pressed/released
532 this.keysym = keysym;
534 // Set modifier if provided
535 if (modifier) this.modifier = modifier;