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 guacamole-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 scaledElements = [];
53 function ScaledElement(element, width, height, scaleFont) {
58 this.scale = function(pixels) {
59 element.style.width = width * pixels + "px";
60 element.style.height = height * pixels + "px";
63 element.style.lineHeight = height * pixels + "px";
64 element.style.fontSize = pixels + "px";
70 // For each child of element, call handler defined in next
71 function parseChildren(element, next) {
73 var children = element.childNodes;
74 for (var i=0; i<children.length; i++) {
77 var child = children[i];
79 // Do not parse text nodes
83 // Get handler for node
84 var handler = next[child.tagName];
86 // Call handler if defined
90 // Throw exception if no handler
93 "Unexpected " + child.tagName
94 + " within " + element.tagName
102 var keyboard = document.createElement("div");
103 keyboard.className = "guacamole-keyboard";
105 // Retrieve keyboard XML
106 var xmlhttprequest = new XMLHttpRequest();
107 xmlhttprequest.open("GET", url, false);
108 xmlhttprequest.send(null);
110 var xml = xmlhttprequest.responseXML;
114 function parse_row(e) {
116 var row = document.createElement("div");
117 row.className = "guacamole-keyboard-row";
121 "column": function(e) {
122 row.appendChild(parse_column(e));
125 "gap": function parse_gap(e) {
128 var gap_size = e.attributes["size"];
131 var gap = document.createElement("div");
132 gap.className = "guacamole-keyboard-gap";
137 gap_units = parseFloat(gap_size.value);
139 scaledElements.push(new ScaledElement(gap, gap_units, gap_units));
140 row.appendChild(gap);
144 "key": function parse_key(e) {
148 var key_size = e.attributes["size"];
151 var key_element = document.createElement("div");
152 key_element.className = "guacamole-keyboard-key";
155 var cap_element = document.createElement("div");
156 cap_element.className = "guacamole-keyboard-cap";
157 key_element.appendChild(cap_element);
162 key_units = parseFloat(key_size.value);
165 "cap": function cap(e) {
168 var required = e.attributes["if"];
169 var modifier = e.attributes["modifier"];
170 var keysym = e.attributes["keysym"];
171 var sticky = e.attributes["sticky"];
173 // Get content of key cap
174 var content = e.textContent;
176 // If no requirements, then show cap by default
178 cap_element.textContent = content;
184 scaledElements.push(new ScaledElement(key_element, key_units, 1, true));
185 row.appendChild(key_element);
195 function parse_column(e) {
197 var col = document.createElement("div");
198 col.className = "guacamole-keyboard-column";
200 var align = col.attributes["align"];
203 col.style.textAlign = align.value;
205 // Columns can only contain rows
208 col.appendChild(parse_row(e));
218 var keyboard_element = xml.documentElement;
219 if (keyboard_element.tagName != "keyboard")
220 throw new Error("Root element must be keyboard");
223 if (!keyboard_element.attributes["size"])
224 throw new Error("size attribute is required for keyboard");
226 var keyboard_size = parseFloat(keyboard_element.attributes["size"].value);
228 parseChildren(keyboard_element, {
231 keyboard.appendChild(parse_row(e));
234 "column": function(e) {
235 keyboard.appendChild(parse_column(e));
242 // Do not allow selection or mouse movement to propagate/register.
243 keyboard.onselectstart =
244 keyboard.onmousemove =
246 keyboard.onmousedown =
253 this.onkeypressed = null;
254 this.onkeyreleased = null;
256 this.getElement = function() {
260 this.resize = function(width) {
262 // Get pixel size of a unit
263 var unit = width / keyboard_size;
265 // Resize all scaled elements
266 for (var i=0; i<scaledElements.length; i++) {
267 var scaledElement = scaledElements[i];
268 scaledElement.scale(unit)
275 Guacamole.OnScreenKeyboard.Key = function() {
278 * Width of the key, relative to the size of the keyboard.
283 * Whether this key is currently pressed.
285 this.pressed = false;
288 * An associative map of all caps by modifier.
294 Guacamole.OnScreenKeyboard.Cap = function(text, keycode, modifier) {
297 * Modifier represented by this keycap
302 * The text to be displayed within this keycap
307 * The keycode this cap sends when its associated key is pressed/released
309 this.keycode = keycode;
311 // Set modifier if provided
312 if (modifier) this.modifier = modifier;