Improved placement and available classes.
[guacamole-common-js.git] / src / main / resources / oskeyboard.js
1
2 /* ***** BEGIN LICENSE BLOCK *****
3  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4  *
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/
9  *
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
13  * License.
14  *
15  * The Original Code is guacamole-common-js.
16  *
17  * The Initial Developer of the Original Code is
18  * Michael Jumper.
19  * Portions created by the Initial Developer are Copyright (C) 2010
20  * the Initial Developer. All Rights Reserved.
21  *
22  * Contributor(s):
23  *
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.
35  *
36  * ***** END LICENSE BLOCK ***** */
37
38 // Guacamole namespace
39 var Guacamole = Guacamole || {};
40
41 /**
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.
45  * 
46  * @constructor
47  * @param {String} url The URL of an XML keyboard layout file.
48  */
49 Guacamole.OnScreenKeyboard = function(url) {
50
51     // For each child of element, call handler defined in next
52     function parseChildren(element, next) {
53
54         var children = element.childNodes;
55         for (var i=0; i<children.length; i++) {
56
57             // Get child node
58             var child = children[i];
59
60             // Do not parse text nodes
61             if (!child.tagName)
62                 continue;
63
64             // Get handler for node
65             var handler = next[child.tagName];
66
67             // Call handler if defined
68             if (handler)
69                 handler(child);
70
71             // Throw exception if no handler
72             else
73                 throw new Error(
74                       "Unexpected " + child.tagName
75                     + " within " + element.tagName
76                 );
77
78         }
79
80     }
81
82     // Create keyboard
83     var keyboard = document.createElement("div");
84     keyboard.className = "guacamole-keyboard";
85
86     // Retrieve keyboard XML
87     var xmlhttprequest = new XMLHttpRequest();
88     xmlhttprequest.open("GET", url, false);
89     xmlhttprequest.send(null);
90
91     var xml = xmlhttprequest.responseXML;
92
93     if (xml) {
94
95         var width = 640;
96         var size = 20;
97         var unit = width / size;
98
99         function parse_row(e) {
100             
101             var row = document.createElement("div");
102             row.className = "guacamole-keyboard-row";
103
104             parseChildren(e, {
105                 
106                 "column": function(e) {
107                     row.appendChild(parse_column(e));
108                 },
109                 
110                 "gap": function parse_gap(e) {
111
112                     // Get attributes
113                     var gap_size = e.attributes["size"];
114
115                     // Create element
116                     var gap = document.createElement("div");
117                     gap.className = "guacamole-keyboard-gap";
118
119                     // Set gap size
120                     if (gap_size)
121                         gap.style.width = gap.style.height =
122                             parseFloat(gap_size.value)*unit + "px";
123
124                     row.appendChild(gap);
125
126                 },
127                 
128                 "key": function parse_key(e) {
129                     
130
131                     // Get attributes
132                     var key_size = e.attributes["size"];
133
134                     // Create element
135                     var key_element = document.createElement("div");
136                     key_element.className = "guacamole-keyboard-key";
137                     key_element.style.fontSize = unit + "px";
138                     key_element.style.height = unit + "px";
139                     key_element.style.lineHeight = unit + "px";
140                     
141                     // Create cap
142                     var cap_element = document.createElement("div");
143                     cap_element.className = "guacamole-keyboard-cap";
144                     key_element.appendChild(cap_element);
145
146                     // Set key size
147                     if (key_size)
148                         key_element.style.width = parseFloat(key_size.value)*unit + "px";
149                     else
150                         key_element.style.width = unit + "px";
151
152                     parseChildren(e, {
153                         "cap": function cap(e) {
154
155                             // Get attributes
156                             var required = e.attributes["if"];
157                             var modifier = e.attributes["modifier"];
158                             var keysym   = e.attributes["keysym"];
159                             var sticky   = e.attributes["sticky"];
160                             
161                             // Get content of key cap
162                             var content = e.textContent;
163                             
164                             // If no requirements, then show cap by default
165                             if (!required) {
166                                 cap_element.textContent = content;
167                             }
168
169                         }
170                     });
171
172                     row.appendChild(key_element);
173
174                 }
175                 
176             });
177
178             return row;
179
180         }
181
182         function parse_column(e) {
183             
184             var col = document.createElement("div");
185             col.className = "guacamole-keyboard-column";
186
187             var align = col.attributes["align"];
188
189             if (align)
190                 col.style.textAlign = align.value;
191
192             // Columns can only contain rows
193             parseChildren(e, {
194                 "row": function(e) {
195                     col.appendChild(parse_row(e));
196                 }
197             });
198
199             return col;
200
201         }
202
203
204         // Parse document
205         var keyboard_element = xml.documentElement;
206         if (keyboard_element.tagName != "keyboard")
207             throw new Error("Root element must be keyboard");
208
209         // Get attributes
210         var keyboard_size = keyboard_element.attributes["size"];
211         
212         parseChildren(keyboard_element, {
213             
214             "row": function(e) {
215                 keyboard.appendChild(parse_row(e));
216             },
217             
218             "column": function(e) {
219                 keyboard.appendChild(parse_column(e));
220             }
221             
222         });
223
224     }
225
226     // Do not allow selection or mouse movement to propagate/register.
227     keyboard.onselectstart =
228     keyboard.onmousemove   =
229     keyboard.onmouseup     =
230     keyboard.onmousedown   =
231     function(e) {
232         e.stopPropagation();
233         return false;
234     };
235
236
237     this.onkeypressed  = null;
238     this.onkeyreleased = null;
239
240     this.getElement = function() {
241         return keyboard;
242     };
243
244 };
245
246 Guacamole.OnScreenKeyboard.Key = function() {
247
248     /**
249      * Width of the key, relative to the size of the keyboard.
250      */
251     this.size = 1;
252
253     /**
254      * Whether this key is currently pressed.
255      */
256     this.pressed = false;
257
258     /**
259      * An associative map of all caps by modifier.
260      */
261     this.caps = {};
262
263 }
264
265 Guacamole.OnScreenKeyboard.Cap = function(text, keycode, modifier) {
266     
267     /**
268      * Modifier represented by this keycap
269      */
270     this.modifier = 0;
271     
272     /**
273      * The text to be displayed within this keycap
274      */
275     this.text = text;
276
277     /**
278      * The keycode this cap sends when its associated key is pressed/released
279      */
280     this.keycode = keycode;
281
282     // Set modifier if provided
283     if (modifier) this.modifier = modifier;
284     
285 }