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 allKeys = new Array();
52 var modifierState = new function() {};
54 function getKeySize(size) {
55 return (5*size) + "ex";
58 function getCapSize(size) {
59 return (5*size - 0.5) + "ex";
62 function clearModifiers() {
64 // Send key release events for all pressed modifiers
65 for (var k=0; k<allKeys.length; k++) {
68 var cap = key.getCap();
69 var modifier = cap.getModifier();
71 if (modifier && isModifierActive(modifier) && !cap.isSticky() && key.isPressed())
78 function setModifierReleased(modifier) {
79 if (isModifierActive(modifier))
80 modifierState[modifier]--;
83 function setModifierPressed(modifier) {
84 if (modifierState[modifier] == null)
85 modifierState[modifier] = 1;
87 modifierState[modifier]++;
90 function isModifierActive(modifier) {
91 if (modifierState[modifier] > 0)
97 function toggleModifierPressed(modifier) {
98 if (isModifierActive(modifier))
99 setModifierReleased(modifier);
101 setModifierPressed(modifier);
104 function refreshAllKeysState() {
105 for (var k=0; k<allKeys.length; k++)
106 allKeys[k].refreshState();
114 var displayText = cap.textContent;
118 if (cap.attributes["keysym"])
119 keysym = parseInt(cap.attributes["keysym"].value);
121 // If keysym not specified, get keysym from display text.
122 else if (displayText.length == 1) {
124 var charCode = displayText.charCodeAt(0);
126 if (charCode >= 0x0000 && charCode <= 0x00FF)
129 else if (charCode >= 0x0100 && charCode <= 0x10FFFF)
130 keysym = 0x01000000 | charCode;
133 // Required modifiers for this keycap
135 if (cap.attributes["if"])
136 reqMod = cap.attributes["if"].value.split(",");
139 // Modifier represented by this keycap
141 if (cap.attributes["modifier"])
142 modifier = cap.attributes["modifier"].value;
145 // Whether this key is sticky (toggles)
146 // Currently only valid for modifiers.
148 if (cap.attributes["sticky"] && cap.attributes["sticky"].value == "true")
151 this.getDisplayText = function() {
152 return cap.textContent;
155 this.getKeySym = function() {
159 this.getRequiredModifiers = function() {
163 this.getModifier = function() {
167 this.isSticky = function() {
174 if (key.attributes["size"])
175 size = parseFloat(key.attributes["size"].value);
177 var caps = key.getElementsByTagName("cap");
178 var keycaps = new Array();
179 for (var i=0; i<caps.length; i++)
180 keycaps.push(new Cap(caps[i]));
182 var rowKey = document.createElement("div");
183 rowKey.className = "key";
185 var keyCap = document.createElement("div");
186 keyCap.className = "cap";
187 rowKey.appendChild(keyCap);
190 var STATE_RELEASED = 0;
191 var STATE_PRESSED = 1;
192 var state = STATE_RELEASED;
194 rowKey.isPressed = function() {
195 return state == STATE_PRESSED;
198 var currentCap = null;
199 function refreshState(modifier) {
203 for (var j=0; j<keycaps.length; j++) {
205 var keycap = keycaps[j];
206 var required = keycap.getRequiredModifiers();
210 // If modifiers required, make sure all modifiers are active
213 for (var k=0; k<required.length; k++) {
214 if (!isModifierActive(required[k])) {
227 rowKey.className = "key";
229 if (currentCap.getModifier())
230 rowKey.className += " modifier";
232 if (currentCap.isSticky())
233 rowKey.className += " sticky";
235 if (isModifierActive(currentCap.getModifier()))
236 rowKey.className += " active";
238 if (state == STATE_PRESSED)
239 rowKey.className += " pressed";
241 keyCap.textContent = currentCap.getDisplayText();
243 rowKey.refreshState = refreshState;
245 rowKey.getCap = function() {
253 rowKey.style.width = getKeySize(size);
254 keyCap.style.width = getCapSize(size);
259 // Set pressed, if released
262 if (state == STATE_RELEASED) {
264 state = STATE_PRESSED;
266 var keysym = currentCap.getKeySym();
267 var modifier = currentCap.getModifier();
268 var sticky = currentCap.isSticky();
270 if (keyPressedHandler && keysym)
271 keyPressedHandler(keysym);
275 // If sticky modifier, toggle
277 toggleModifierPressed(modifier);
279 // Otherwise, just set on.
281 setModifierPressed(modifier);
283 refreshAllKeysState();
290 rowKey.press = press;
293 // Set released, if pressed
296 if (state == STATE_PRESSED) {
298 state = STATE_RELEASED;
300 var keysym = currentCap.getKeySym();
301 var modifier = currentCap.getModifier();
302 var sticky = currentCap.isSticky();
304 if (keyReleasedHandler && keysym)
305 keyReleasedHandler(keysym);
309 // If not sticky modifier, release modifier
311 setModifierReleased(modifier);
312 refreshAllKeysState();
321 // If not a modifier, also release all pressed modifiers
328 rowKey.release = release;
330 // Toggle press/release states
332 if (state == STATE_PRESSED)
339 // Send key press on mousedown
340 rowKey.onmousedown = function(e) {
344 var modifier = currentCap.getModifier();
345 var sticky = currentCap.isSticky();
347 // Toggle non-sticky modifiers
348 if (modifier && !sticky)
358 // Send key release on mouseup/out
361 rowKey.onmouseup = function(e) {
365 var modifier = currentCap.getModifier();
366 var sticky = currentCap.isSticky();
368 // Release non-modifiers and sticky modifiers
369 if (!modifier || sticky)
375 rowKey.onselectstart = function() { return false; };
383 var keyboardGap = document.createElement("div");
384 keyboardGap.className = "gap";
385 keyboardGap.textContent = " ";
388 if (gap.attributes["size"])
389 size = parseFloat(gap.attributes["size"].value);
392 keyboardGap.style.width = getKeySize(size);
393 keyboardGap.style.height = getKeySize(size);
402 var keyboardRow = document.createElement("div");
403 keyboardRow.className = "row";
405 var children = row.childNodes;
406 for (var j=0; j<children.length; j++) {
407 var child = children[j];
409 // <row> can contain <key> or <column>
410 if (child.tagName == "key") {
411 var key = new Key(child);
412 keyboardRow.appendChild(key);
415 else if (child.tagName == "gap") {
416 var gap = new Gap(child);
417 keyboardRow.appendChild(gap);
419 else if (child.tagName == "column") {
420 var col = new Column(child);
421 keyboardRow.appendChild(col);
430 function Column(col) {
432 var keyboardCol = document.createElement("div");
433 keyboardCol.className = "col";
436 if (col.attributes["align"])
437 align = col.attributes["align"].value;
439 var children = col.childNodes;
440 for (var j=0; j<children.length; j++) {
441 var child = children[j];
443 // <column> can only contain <row>
444 if (child.tagName == "row") {
445 var row = new Row(child);
446 keyboardCol.appendChild(row);
452 keyboardCol.style.textAlign = align;
461 var keyboard = document.createElement("div");
462 keyboard.className = "keyboard";
465 // Retrieve keyboard XML
466 var xmlhttprequest = new XMLHttpRequest();
467 xmlhttprequest.open("GET", url, false);
468 xmlhttprequest.send(null);
470 var xml = xmlhttprequest.responseXML;
475 var root = xml.documentElement;
478 var children = root.childNodes;
479 for (var i=0; i<children.length; i++) {
480 var child = children[i];
482 // <keyboard> can contain <row> or <column>
483 if (child.tagName == "row") {
484 keyboard.appendChild(new Row(child));
486 else if (child.tagName == "column") {
487 keyboard.appendChild(new Column(child));
496 var keyPressedHandler = null;
497 var keyReleasedHandler = null;
499 keyboard.setKeyPressedHandler = function(kh) { keyPressedHandler = kh; };
500 keyboard.setKeyReleasedHandler = function(kh) { keyReleasedHandler = kh; };
502 // Do not allow selection or mouse movement to propagate/register.
503 keyboard.onselectstart =
504 keyboard.onmousemove =
506 keyboard.onmousedown =