-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathClient.js
2081 lines (1655 loc) · 62.9 KB
/
Client.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
var Guacamole = Guacamole || {};
/**
* Guacamole protocol client. Given a {@link Guacamole.Tunnel},
* automatically handles incoming and outgoing Guacamole instructions via the
* provided tunnel, updating its display using one or more canvas elements.
*
* @constructor
* @param {!Guacamole.Tunnel} tunnel
* The tunnel to use to send and receive Guacamole instructions.
*/
Guacamole.Client = function(tunnel) {
var guac_client = this;
var currentState = Guacamole.Client.State.IDLE;
var currentTimestamp = 0;
/**
* The rough number of milliseconds to wait between sending keep-alive
* pings. This may vary depending on how frequently the browser allows
* timers to run, as well as how frequently the client receives messages
* from the server.
*
* @private
* @constant
* @type {!number}
*/
var KEEP_ALIVE_FREQUENCY = 5000;
/**
* The current keep-alive ping timeout ID, if any. This will only be set
* upon connecting.
*
* @private
* @type {number}
*/
var keepAliveTimeout = null;
/**
* The timestamp of the point in time that the last keep-live ping was
* sent, in milliseconds elapsed since midnight of January 1, 1970 UTC.
*
* @private
* @type {!number}
*/
var lastSentKeepAlive = 0;
/**
* Translation from Guacamole protocol line caps to Layer line caps.
*
* @private
* @type {!Object.<number, string>}
*/
var lineCap = {
0: "butt",
1: "round",
2: "square"
};
/**
* Translation from Guacamole protocol line caps to Layer line caps.
*
* @private
* @type {!Object.<number, string>}
*/
var lineJoin = {
0: "bevel",
1: "miter",
2: "round"
};
/**
* The underlying Guacamole display.
*
* @private
* @type {!Guacamole.Display}
*/
var display = new Guacamole.Display();
/**
* All available layers and buffers
*
* @private
* @type {!Object.<number, (Guacamole.Display.VisibleLayer|Guacamole.Layer)>}
*/
var layers = {};
/**
* All audio players currently in use by the client. Initially, this will
* be empty, but audio players may be allocated by the server upon request.
*
* @private
* @type {!Object.<number, Guacamole.AudioPlayer>}
*/
var audioPlayers = {};
/**
* All video players currently in use by the client. Initially, this will
* be empty, but video players may be allocated by the server upon request.
*
* @private
* @type {!Object.<number, Guacamole.VideoPlayer>}
*/
var videoPlayers = {};
// No initial parsers
var parsers = [];
// No initial streams
var streams = [];
/**
* All current objects. The index of each object is dictated by the
* Guacamole server.
*
* @private
* @type {!Guacamole.Object[]}
*/
var objects = [];
// Pool of available stream indices
var stream_indices = new Guacamole.IntegerPool();
// Array of allocated output streams by index
var output_streams = [];
function setState(state) {
if (state != currentState) {
currentState = state;
if (guac_client.onstatechange)
guac_client.onstatechange(currentState);
}
}
function isConnected() {
return currentState == Guacamole.Client.State.CONNECTED
|| currentState == Guacamole.Client.State.WAITING;
}
/**
* Produces an opaque representation of Guacamole.Client state which can be
* later imported through a call to importState(). This object is
* effectively an independent, compressed snapshot of protocol and display
* state. Invoking this function implicitly flushes the display.
*
* @param {!function} callback
* Callback which should be invoked once the state object is ready. The
* state object will be passed to the callback as the sole parameter.
* This callback may be invoked immediately, or later as the display
* finishes rendering and becomes ready.
*/
this.exportState = function exportState(callback) {
// Start with empty state
var state = {
'currentState' : currentState,
'currentTimestamp' : currentTimestamp,
'layers' : {}
};
var layersSnapshot = {};
// Make a copy of all current layers (protocol state)
for (var key in layers) {
layersSnapshot[key] = layers[key];
}
// Populate layers once data is available (display state, requires flush)
display.flush(function populateLayers() {
// Export each defined layer/buffer
for (var key in layersSnapshot) {
var index = parseInt(key);
var layer = layersSnapshot[key];
// Store layer/buffer dimensions
var exportLayer = {
'width' : layer.width,
'height' : layer.height
};
// Store layer/buffer image data, if it can be generated
if (layer.width && layer.height) {
var canvas = layer.toCanvas();
exportLayer.url = canvas.toDataURL('image/png');
}
// Add layer properties if not a buffer nor the default layer
if (index > 0) {
exportLayer.x = layer.x;
exportLayer.y = layer.y;
exportLayer.z = layer.z;
exportLayer.alpha = layer.alpha;
exportLayer.matrix = layer.matrix;
exportLayer.parent = getLayerIndex(layer.parent);
}
// Store exported layer
state.layers[key] = exportLayer;
}
// Invoke callback now that the state is ready
callback(state);
});
};
/**
* Restores Guacamole.Client protocol and display state based on an opaque
* object from a prior call to exportState(). The Guacamole.Client instance
* used to export that state need not be the same as this instance.
*
* @param {!object} state
* An opaque representation of Guacamole.Client state from a prior call
* to exportState().
*
* @param {function} [callback]
* The function to invoke when state has finished being imported. This
* may happen immediately, or later as images within the provided state
* object are loaded.
*/
this.importState = function importState(state, callback) {
var key;
var index;
currentState = state.currentState;
currentTimestamp = state.currentTimestamp;
// Cancel any pending display operations/frames
display.cancel();
// Dispose of all layers
for (key in layers) {
index = parseInt(key);
if (index > 0)
layers[key].dispose();
}
layers = {};
// Import state of each layer/buffer
for (key in state.layers) {
index = parseInt(key);
var importLayer = state.layers[key];
var layer = getLayer(index);
// Reset layer size
display.resize(layer, importLayer.width, importLayer.height);
// Initialize new layer if it has associated data
if (importLayer.url) {
display.setChannelMask(layer, Guacamole.Layer.SRC);
display.draw(layer, 0, 0, importLayer.url);
}
// Set layer-specific properties if not a buffer nor the default layer
if (index > 0 && importLayer.parent >= 0) {
// Apply layer position and set parent
var parent = getLayer(importLayer.parent);
display.move(layer, parent, importLayer.x, importLayer.y, importLayer.z);
// Set layer transparency
display.shade(layer, importLayer.alpha);
// Apply matrix transform
var matrix = importLayer.matrix;
display.distort(layer,
matrix[0], matrix[1], matrix[2],
matrix[3], matrix[4], matrix[5]);
}
}
// Flush changes to display
display.flush(callback);
};
/**
* Returns the underlying display of this Guacamole.Client. The display
* contains an Element which can be added to the DOM, causing the
* display to become visible.
*
* @return {!Guacamole.Display}
* The underlying display of this Guacamole.Client.
*/
this.getDisplay = function() {
return display;
};
/**
* Sends the current size of the screen.
*
* @param {!number} width
* The width of the screen.
*
* @param {!number} height
* The height of the screen.
*/
this.sendSize = function(width, height) {
// Do not send requests if not connected
if (!isConnected())
return;
tunnel.sendMessage("size", width, height);
};
/**
* Sends a key event having the given properties as if the user
* pressed or released a key.
*
* @param {!boolean} pressed
* Whether the key is pressed (true) or released (false).
*
* @param {!number} keysym
* The keysym of the key being pressed or released.
*/
this.sendKeyEvent = function(pressed, keysym) {
// Do not send requests if not connected
if (!isConnected())
return;
tunnel.sendMessage("key", keysym, pressed);
};
/**
* Sends a mouse event having the properties provided by the given mouse
* state.
*
* @param {!Guacamole.Mouse.State} mouseState
* The state of the mouse to send in the mouse event.
*
* @param {boolean} [applyDisplayScale=false]
* Whether the provided mouse state uses local display units, rather
* than remote display units, and should be scaled to match the
* {@link Guacamole.Display}.
*/
this.sendMouseState = function sendMouseState(mouseState, applyDisplayScale) {
// Do not send requests if not connected
if (!isConnected())
return;
var x = mouseState.x;
var y = mouseState.y;
// Translate for display units if requested
if (applyDisplayScale) {
x /= display.getScale();
y /= display.getScale();
}
// Update client-side cursor
display.moveCursor(
Math.floor(x),
Math.floor(y)
);
// Build mask
var buttonMask = 0;
if (mouseState.left) buttonMask |= 1;
if (mouseState.middle) buttonMask |= 2;
if (mouseState.right) buttonMask |= 4;
if (mouseState.up) buttonMask |= 8;
if (mouseState.down) buttonMask |= 16;
// Send message
tunnel.sendMessage("mouse", Math.floor(x), Math.floor(y), buttonMask);
};
/**
* Sends a touch event having the properties provided by the given touch
* state.
*
* @param {!Guacamole.Touch.State} touchState
* The state of the touch contact to send in the touch event.
*
* @param {boolean} [applyDisplayScale=false]
* Whether the provided touch state uses local display units, rather
* than remote display units, and should be scaled to match the
* {@link Guacamole.Display}.
*/
this.sendTouchState = function sendTouchState(touchState, applyDisplayScale) {
// Do not send requests if not connected
if (!isConnected())
return;
var x = touchState.x;
var y = touchState.y;
// Translate for display units if requested
if (applyDisplayScale) {
x /= display.getScale();
y /= display.getScale();
}
tunnel.sendMessage('touch', touchState.id, Math.floor(x), Math.floor(y),
Math.floor(touchState.radiusX), Math.floor(touchState.radiusY),
touchState.angle, touchState.force);
};
/**
* Allocates an available stream index and creates a new
* Guacamole.OutputStream using that index, associating the resulting
* stream with this Guacamole.Client. Note that this stream will not yet
* exist as far as the other end of the Guacamole connection is concerned.
* Streams exist within the Guacamole protocol only when referenced by an
* instruction which creates the stream, such as a "clipboard", "file", or
* "pipe" instruction.
*
* @returns {!Guacamole.OutputStream}
* A new Guacamole.OutputStream with a newly-allocated index and
* associated with this Guacamole.Client.
*/
this.createOutputStream = function createOutputStream() {
// Allocate index
var index = stream_indices.next();
// Return new stream
var stream = output_streams[index] = new Guacamole.OutputStream(guac_client, index);
return stream;
};
/**
* Opens a new audio stream for writing, where audio data having the give
* mimetype will be sent along the returned stream. The instruction
* necessary to create this stream will automatically be sent.
*
* @param {!string} mimetype
* The mimetype of the audio data that will be sent along the returned
* stream.
*
* @return {!Guacamole.OutputStream}
* The created audio stream.
*/
this.createAudioStream = function(mimetype) {
// Allocate and associate stream with audio metadata
var stream = guac_client.createOutputStream();
tunnel.sendMessage("audio", stream.index, mimetype);
return stream;
};
/**
* Opens a new file for writing, having the given index, mimetype and
* filename. The instruction necessary to create this stream will
* automatically be sent.
*
* @param {!string} mimetype
* The mimetype of the file being sent.
*
* @param {!string} filename
* The filename of the file being sent.
*
* @return {!Guacamole.OutputStream}
* The created file stream.
*/
this.createFileStream = function(mimetype, filename) {
// Allocate and associate stream with file metadata
var stream = guac_client.createOutputStream();
tunnel.sendMessage("file", stream.index, mimetype, filename);
return stream;
};
/**
* Opens a new pipe for writing, having the given name and mimetype. The
* instruction necessary to create this stream will automatically be sent.
*
* @param {!string} mimetype
* The mimetype of the data being sent.
*
* @param {!string} name
* The name of the pipe.
*
* @return {!Guacamole.OutputStream}
* The created file stream.
*/
this.createPipeStream = function(mimetype, name) {
// Allocate and associate stream with pipe metadata
var stream = guac_client.createOutputStream();
tunnel.sendMessage("pipe", stream.index, mimetype, name);
return stream;
};
/**
* Opens a new clipboard object for writing, having the given mimetype. The
* instruction necessary to create this stream will automatically be sent.
*
* @param {!string} mimetype
* The mimetype of the data being sent.
*
* @param {!string} name
* The name of the pipe.
*
* @return {!Guacamole.OutputStream}
* The created file stream.
*/
this.createClipboardStream = function(mimetype) {
// Allocate and associate stream with clipboard metadata
var stream = guac_client.createOutputStream();
tunnel.sendMessage("clipboard", stream.index, mimetype);
return stream;
};
/**
* Opens a new argument value stream for writing, having the given
* parameter name and mimetype, requesting that the connection parameter
* with the given name be updated to the value described by the contents
* of the following stream. The instruction necessary to create this stream
* will automatically be sent.
*
* @param {!string} mimetype
* The mimetype of the data being sent.
*
* @param {!string} name
* The name of the connection parameter to attempt to update.
*
* @return {!Guacamole.OutputStream}
* The created argument value stream.
*/
this.createArgumentValueStream = function createArgumentValueStream(mimetype, name) {
// Allocate and associate stream with argument value metadata
var stream = guac_client.createOutputStream();
tunnel.sendMessage("argv", stream.index, mimetype, name);
return stream;
};
/**
* Creates a new output stream associated with the given object and having
* the given mimetype and name. The legality of a mimetype and name is
* dictated by the object itself. The instruction necessary to create this
* stream will automatically be sent.
*
* @param {!number} index
* The index of the object for which the output stream is being
* created.
*
* @param {!string} mimetype
* The mimetype of the data which will be sent to the output stream.
*
* @param {!string} name
* The defined name of an output stream within the given object.
*
* @returns {!Guacamole.OutputStream}
* An output stream which will write blobs to the named output stream
* of the given object.
*/
this.createObjectOutputStream = function createObjectOutputStream(index, mimetype, name) {
// Allocate and associate stream with object metadata
var stream = guac_client.createOutputStream();
tunnel.sendMessage("put", index, stream.index, mimetype, name);
return stream;
};
/**
* Requests read access to the input stream having the given name. If
* successful, a new input stream will be created.
*
* @param {!number} index
* The index of the object from which the input stream is being
* requested.
*
* @param {!string} name
* The name of the input stream to request.
*/
this.requestObjectInputStream = function requestObjectInputStream(index, name) {
// Do not send requests if not connected
if (!isConnected())
return;
tunnel.sendMessage("get", index, name);
};
/**
* Acknowledge receipt of a blob on the stream with the given index.
*
* @param {!number} index
* The index of the stream associated with the received blob.
*
* @param {!string} message
* A human-readable message describing the error or status.
*
* @param {!number} code
* The error code, if any, or 0 for success.
*/
this.sendAck = function(index, message, code) {
// Do not send requests if not connected
if (!isConnected())
return;
tunnel.sendMessage("ack", index, message, code);
};
/**
* Given the index of a file, writes a blob of data to that file.
*
* @param {!number} index
* The index of the file to write to.
*
* @param {!string} data
* Base64-encoded data to write to the file.
*/
this.sendBlob = function(index, data) {
// Do not send requests if not connected
if (!isConnected())
return;
tunnel.sendMessage("blob", index, data);
};
/**
* Marks a currently-open stream as complete. The other end of the
* Guacamole connection will be notified via an "end" instruction that the
* stream is closed, and the index will be made available for reuse in
* future streams.
*
* @param {!number} index
* The index of the stream to end.
*/
this.endStream = function(index) {
// Do not send requests if not connected
if (!isConnected())
return;
// Explicitly close stream by sending "end" instruction
tunnel.sendMessage("end", index);
// Free associated index and stream if they exist
if (output_streams[index]) {
stream_indices.free(index);
delete output_streams[index];
}
};
/**
* Fired whenever the state of this Guacamole.Client changes.
*
* @event
* @param {!number} state
* The new state of the client.
*/
this.onstatechange = null;
/**
* Fired when the remote client sends a name update.
*
* @event
* @param {!string} name
* The new name of this client.
*/
this.onname = null;
/**
* Fired when an error is reported by the remote client, and the connection
* is being closed.
*
* @event
* @param {!Guacamole.Status} status
* A status object which describes the error.
*/
this.onerror = null;
/**
* Fired when an arbitrary message is received from the tunnel that should
* be processed by the client. By default, additional message-specific
* events such as "onjoin" and "onleave" will fire for the received message
* after this event has been processed. An event handler for "onmsg" need
* not be supplied if "onjoin" and/or "onleave" will be used.
*
* @event
* @param {!number} msgcode
* A status code sent by the remote server that indicates the nature of
* the message that is being sent to the client.
*
* @param {string[]} args
* An array of arguments to be processed with the message sent to the
* client.
*
* @return {boolean}
* true if message-specific events such as "onjoin" and
* "onleave" should be fired for this message, false otherwise. If
* no value is returned, message-specific events will be allowed to
* fire.
*/
this.onmsg = null;
/**
* Fired when a user joins a shared connection.
*
* @event
* @param {!string} userID
* A unique value representing this specific user's connection to the
* shared connection. This value is generated by the server and is
* guaranteed to be unique relative to other users of the connection.
*
* @param {!string} name
* A human-readable name representing the user that joined, such as
* their username. This value is provided by the web application during
* the connection handshake and is not necessarily unique relative to
* other users of the connection.
*/
this.onjoin = null;
/**
* Fired when a user leaves a shared connection.
*
* @event
* @param {!string} userID
* A unique value representing this specific user's connection to the
* shared connection. This value is generated by the server and is
* guaranteed to be unique relative to other users of the connection.
*
* @param {!string} name
* A human-readable name representing the user that left, such as their
* username. This value is provided by the web application during the
* connection handshake and is not necessarily unique relative to other
* users of the connection.
*/
this.onleave = null;
/**
* Fired when a audio stream is created. The stream provided to this event
* handler will contain its own event handlers for received data.
*
* @event
* @param {!Guacamole.InputStream} stream
* The stream that will receive audio data from the server.
*
* @param {!string} mimetype
* The mimetype of the audio data which will be received.
*
* @return {Guacamole.AudioPlayer}
* An object which implements the Guacamole.AudioPlayer interface and
* has been initialized to play the data in the provided stream, or null
* if the built-in audio players of the Guacamole client should be
* used.
*/
this.onaudio = null;
/**
* Fired when a video stream is created. The stream provided to this event
* handler will contain its own event handlers for received data.
*
* @event
* @param {!Guacamole.InputStream} stream
* The stream that will receive video data from the server.
*
* @param {!Guacamole.Display.VisibleLayer} layer
* The destination layer on which the received video data should be
* played. It is the responsibility of the Guacamole.VideoPlayer
* implementation to play the received data within this layer.
*
* @param {!string} mimetype
* The mimetype of the video data which will be received.
*
* @return {Guacamole.VideoPlayer}
* An object which implements the Guacamole.VideoPlayer interface and
* has been initialized to play the data in the provided stream, or null
* if the built-in video players of the Guacamole client should be
* used.
*/
this.onvideo = null;
/**
* Fired when the remote client is explicitly declaring the level of
* multi-touch support provided by a particular display layer.
*
* @event
* @param {!Guacamole.Display.VisibleLayer} layer
* The layer whose multi-touch support level is being declared.
*
* @param {!number} touches
* The maximum number of simultaneous touches supported by the given
* layer, where 0 indicates that touch events are not supported at all.
*/
this.onmultitouch = null;
/**
* Fired when the current value of a connection parameter is being exposed
* by the server.
*
* @event
* @param {!Guacamole.InputStream} stream
* The stream that will receive connection parameter data from the
* server.
*
* @param {!string} mimetype
* The mimetype of the data which will be received.
*
* @param {!string} name
* The name of the connection parameter whose value is being exposed.
*/
this.onargv = null;
/**
* Fired when the clipboard of the remote client is changing.
*
* @event
* @param {!Guacamole.InputStream} stream
* The stream that will receive clipboard data from the server.
*
* @param {!string} mimetype
* The mimetype of the data which will be received.
*/
this.onclipboard = null;
/**
* Fired when a file stream is created. The stream provided to this event
* handler will contain its own event handlers for received data.
*
* @event
* @param {!Guacamole.InputStream} stream
* The stream that will receive data from the server.
*
* @param {!string} mimetype
* The mimetype of the file received.
*
* @param {!string} filename
* The name of the file received.
*/
this.onfile = null;
/**
* Fired when a filesystem object is created. The object provided to this
* event handler will contain its own event handlers and functions for
* requesting and handling data.
*
* @event
* @param {!Guacamole.Object} object
* The created filesystem object.
*
* @param {!string} name
* The name of the filesystem.
*/
this.onfilesystem = null;
/**
* Fired when a pipe stream is created. The stream provided to this event
* handler will contain its own event handlers for received data;
*
* @event
* @param {!Guacamole.InputStream} stream
* The stream that will receive data from the server.
*
* @param {!string} mimetype
* The mimetype of the data which will be received.
*
* @param {!string} name
* The name of the pipe.
*/
this.onpipe = null;
/**
* Fired when a "required" instruction is received. A required instruction
* indicates that additional parameters are required for the connection to
* continue, such as user credentials.
*
* @event
* @param {!string[]} parameters
* The names of the connection parameters that are required to be
* provided for the connection to continue.
*/
this.onrequired = null;
/**
* Fired whenever a sync instruction is received from the server, indicating
* that the server is finished processing any input from the client and
* has sent any results.
*
* @event
* @param {!number} timestamp
* The timestamp associated with the sync instruction.
*
* @param {!number} frames
* The number of frames that were considered or combined to produce the
* frame associated with this sync instruction, or zero if this value
* is not known or the remote desktop server provides no concept of
* frames.
*/
this.onsync = null;
/**
* Returns the layer with the given index, creating it if necessary.
* Positive indices refer to visible layers, an index of zero refers to
* the default layer, and negative indices refer to buffers.
*
* @private
* @param {!number} index
* The index of the layer to retrieve.
*
* @return {!(Guacamole.Display.VisibleLayer|Guacamole.Layer)}
* The layer having the given index.
*/
var getLayer = function getLayer(index) {
// Get layer, create if necessary
var layer = layers[index];
if (!layer) {
// Create layer based on index
if (index === 0)
layer = display.getDefaultLayer();
else if (index > 0)
layer = display.createLayer();
else
layer = display.createBuffer();
// Add new layer
layers[index] = layer;
}
return layer;
};
/**
* Returns the index passed to getLayer() when the given layer was created.
* Positive indices refer to visible layers, an index of zero refers to the
* default layer, and negative indices refer to buffers.
*
* @param {!(Guacamole.Display.VisibleLayer|Guacamole.Layer)} layer
* The layer whose index should be determined.
*
* @returns {number}
* The index of the given layer, or null if no such layer is associated
* with this client.
*/
var getLayerIndex = function getLayerIndex(layer) {
// Avoid searching if there clearly is no such layer
if (!layer)
return null;
// Search through each layer, returning the index of the given layer
// once found
for (var key in layers) {
if (layer === layers[key])
return parseInt(key);
}
// Otherwise, no such index
return null;
};
function getParser(index) {
var parser = parsers[index];