-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDisplay.js
2192 lines (1913 loc) · 67.4 KB
/
Display.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 || {};
/**
* The Guacamole display. The display does not deal with the Guacamole
* protocol, and instead implements a set of graphical operations which
* embody the set of operations present in the protocol. The order operations
* are executed is guaranteed to be in the same order as their corresponding
* functions are called.
*
* @constructor
*/
Guacamole.Display = function() {
/**
* Reference to this Guacamole.Display.
* @private
*/
var guac_display = this;
var displayWidth = 0;
var displayHeight = 0;
var displayScale = 1;
// Create display
var display = document.createElement("div");
display.style.position = "relative";
display.style.width = displayWidth + "px";
display.style.height = displayHeight + "px";
// Ensure transformations on display originate at 0,0
display.style.transformOrigin =
display.style.webkitTransformOrigin =
display.style.MozTransformOrigin =
display.style.OTransformOrigin =
display.style.msTransformOrigin =
"0 0";
// Create default layer
var default_layer = new Guacamole.Display.VisibleLayer(displayWidth, displayHeight);
// Create cursor layer
var cursor = new Guacamole.Display.VisibleLayer(0, 0);
cursor.setChannelMask(Guacamole.Layer.SRC);
// Add default layer and cursor to display
display.appendChild(default_layer.getElement());
display.appendChild(cursor.getElement());
// Create bounding div
var bounds = document.createElement("div");
bounds.style.position = "relative";
bounds.style.width = (displayWidth*displayScale) + "px";
bounds.style.height = (displayHeight*displayScale) + "px";
// Add display to bounds
bounds.appendChild(display);
/**
* The X coordinate of the hotspot of the mouse cursor. The hotspot is
* the relative location within the image of the mouse cursor at which
* each click occurs.
*
* @type {!number}
*/
this.cursorHotspotX = 0;
/**
* The Y coordinate of the hotspot of the mouse cursor. The hotspot is
* the relative location within the image of the mouse cursor at which
* each click occurs.
*
* @type {!number}
*/
this.cursorHotspotY = 0;
/**
* The current X coordinate of the local mouse cursor. This is not
* necessarily the location of the actual mouse - it refers only to
* the location of the cursor image within the Guacamole display, as
* last set by moveCursor().
*
* @type {!number}
*/
this.cursorX = 0;
/**
* The current X coordinate of the local mouse cursor. This is not
* necessarily the location of the actual mouse - it refers only to
* the location of the cursor image within the Guacamole display, as
* last set by moveCursor().
*
* @type {!number}
*/
this.cursorY = 0;
/**
* The number of milliseconds over which display rendering statistics
* should be gathered, dispatching {@link #onstatistics} events as those
* statistics are available. If set to zero, no statistics will be
* gathered.
*
* @default 0
* @type {!number}
*/
this.statisticWindow = 0;
/**
* Fired when the default layer (and thus the entire Guacamole display)
* is resized.
*
* @event
* @param {!number} width
* The new width of the Guacamole display.
*
* @param {!number} height
* The new height of the Guacamole display.
*/
this.onresize = null;
/**
* Fired whenever the local cursor image is changed. This can be used to
* implement special handling of the client-side cursor, or to override
* the default use of a software cursor layer.
*
* @event
* @param {!HTMLCanvasElement} canvas
* The cursor image.
*
* @param {!number} x
* The X-coordinate of the cursor hotspot.
*
* @param {!number} y
* The Y-coordinate of the cursor hotspot.
*/
this.oncursor = null;
/**
* Fired whenever performance statistics are available for recently-
* rendered frames. This event will fire only if {@link #statisticWindow}
* is non-zero.
*
* @event
* @param {!Guacamole.Display.Statistics} stats
* An object containing general rendering performance statistics for
* the remote desktop, Guacamole server, and Guacamole client.
*/
this.onstatistics = null;
/**
* The queue of all pending Tasks. Tasks will be run in order, with new
* tasks added at the end of the queue and old tasks removed from the
* front of the queue (FIFO). These tasks will eventually be grouped
* into a Frame.
*
* @private
* @type {!Task[]}
*/
var tasks = [];
/**
* The queue of all frames. Each frame is a pairing of an array of tasks
* and a callback which must be called when the frame is rendered.
*
* @private
* @type {!Frame[]}
*/
var frames = [];
/**
* The ID of the animation frame request returned by the last call to
* requestAnimationFrame(). This value will only be set if the browser
* supports requestAnimationFrame(), if a frame render is currently
* pending, and if the current browser tab is currently focused (likely to
* handle requests for animation frames). In all other cases, this will be
* null.
*
* @private
* @type {number}
*/
var inProgressFrame = null;
/**
* Flushes all pending frames synchronously. This function will block until
* all pending frames have rendered. If a frame is currently blocked by an
* asynchronous operation like an image load, this function will return
* after reaching that operation and the flush operation will
* automamtically resume after that operation completes.
*
* @private
*/
var syncFlush = function syncFlush() {
var localTimestamp = 0;
var remoteTimestamp = 0;
var renderedLogicalFrames = 0;
var rendered_frames = 0;
// Draw all pending frames, if ready
while (rendered_frames < frames.length) {
var frame = frames[rendered_frames];
if (!frame.isReady())
break;
frame.flush();
localTimestamp = frame.localTimestamp;
remoteTimestamp = frame.remoteTimestamp;
renderedLogicalFrames += frame.logicalFrames;
rendered_frames++;
}
// Remove rendered frames from array
frames.splice(0, rendered_frames);
if (rendered_frames)
notifyFlushed(localTimestamp, remoteTimestamp, renderedLogicalFrames);
};
/**
* Flushes all pending frames asynchronously. This function returns
* immediately, relying on requestAnimationFrame() to dictate when each
* frame should be flushed.
*
* @private
*/
var asyncFlush = function asyncFlush() {
var continueFlush = function continueFlush() {
// We're no longer waiting to render a frame
inProgressFrame = null;
// Nothing to do if there are no frames remaining
if (!frames.length)
return;
// Flush the next frame only if it is ready (not awaiting
// completion of some asynchronous operation like an image load)
if (frames[0].isReady()) {
var frame = frames.shift();
frame.flush();
notifyFlushed(frame.localTimestamp, frame.remoteTimestamp, frame.logicalFrames);
}
// Request yet another animation frame if frames remain to be
// flushed
if (frames.length)
inProgressFrame = window.requestAnimationFrame(continueFlush);
};
// Begin flushing frames if not already waiting to render a frame
if (!inProgressFrame)
inProgressFrame = window.requestAnimationFrame(continueFlush);
};
/**
* Recently-gathered display render statistics, as made available by calls
* to notifyFlushed(). The contents of this array will be trimmed to
* contain only up to {@link #statisticWindow} milliseconds of statistics.
*
* @private
* @type {Guacamole.Display.Statistics[]}
*/
var statistics = [];
/**
* Notifies that one or more frames have been successfully rendered
* (flushed) to the display.
*
* @private
* @param {!number} localTimestamp
* The local timestamp of the point in time at which the most recent,
* flushed frame was received by the display, in milliseconds since the
* Unix Epoch.
*
* @param {!number} remoteTimestamp
* The remote timestamp of sync instruction associated with the most
* recent, flushed frame received by the display. This timestamp is in
* milliseconds, but is arbitrary, having meaning only relative to
* other timestamps in the same connection.
*
* @param {!number} logicalFrames
* The number of remote desktop frames that were flushed.
*/
var notifyFlushed = function notifyFlushed(localTimestamp, remoteTimestamp, logicalFrames) {
// Ignore if statistics are not being gathered
if (!guac_display.statisticWindow)
return;
var current = new Date().getTime();
// Find the first statistic that is still within the configured time
// window
for (var first = 0; first < statistics.length; first++) {
if (current - statistics[first].timestamp <= guac_display.statisticWindow)
break;
}
// Remove all statistics except those within the time window
statistics.splice(0, first - 1);
// Record statistics for latest frame
statistics.push({
localTimestamp : localTimestamp,
remoteTimestamp : remoteTimestamp,
timestamp : current,
frames : logicalFrames
});
// Determine the actual time interval of the available statistics (this
// will not perfectly match the configured interval, which is an upper
// bound)
var statDuration = (statistics[statistics.length - 1].timestamp - statistics[0].timestamp) / 1000;
// Determine the amount of time that elapsed remotely (within the
// remote desktop)
var remoteDuration = (statistics[statistics.length - 1].remoteTimestamp - statistics[0].remoteTimestamp) / 1000;
// Calculate the number of frames that have been rendered locally
// within the configured time interval
var localFrames = statistics.length;
// Calculate the number of frames actually received from the remote
// desktop by the Guacamole server
var remoteFrames = statistics.reduce(function sumFrames(prev, stat) {
return prev + stat.frames;
}, 0);
// Calculate the number of frames that the Guacamole server had to
// drop or combine with other frames
var drops = statistics.reduce(function sumDrops(prev, stat) {
return prev + Math.max(0, stat.frames - 1);
}, 0);
// Produce lag and FPS statistics from above raw measurements
var stats = new Guacamole.Display.Statistics({
processingLag : current - localTimestamp,
desktopFps : (remoteDuration && remoteFrames) ? remoteFrames / remoteDuration : null,
clientFps : statDuration ? localFrames / statDuration : null,
serverFps : remoteDuration ? localFrames / remoteDuration : null,
dropRate : remoteDuration ? drops / remoteDuration : null
});
// Notify of availability of new statistics
if (guac_display.onstatistics)
guac_display.onstatistics(stats);
};
// Switch from asynchronous frame handling to synchronous frame handling if
// requestAnimationFrame() is unlikely to be usable (browsers may not
// invoke the animation frame callback if the relevant tab is not focused)
window.addEventListener('blur', function switchToSyncFlush() {
if (inProgressFrame && !document.hasFocus()) {
// Cancel pending asynchronous processing of frame ...
window.cancelAnimationFrame(inProgressFrame);
inProgressFrame = null;
// ... and instead process it synchronously
syncFlush();
}
}, true);
/**
* Flushes all pending frames.
* @private
*/
function __flush_frames() {
if (window.requestAnimationFrame && document.hasFocus())
asyncFlush();
else
syncFlush();
}
/**
* An ordered list of tasks which must be executed atomically. Once
* executed, an associated (and optional) callback will be called.
*
* @private
* @constructor
* @param {function} [callback]
* The function to call when this frame is rendered.
*
* @param {!Task[]} tasks
* The set of tasks which must be executed to render this frame.
*
* @param {number} [timestamp]
* The remote timestamp of sync instruction associated with this frame.
* This timestamp is in milliseconds, but is arbitrary, having meaning
* only relative to other remote timestamps in the same connection. If
* omitted, a compatible but local timestamp will be used instead.
*
* @param {number} [logicalFrames=0]
* The number of remote desktop frames that were combined to produce
* this frame, or zero if this value is unknown or inapplicable.
*/
var Frame = function Frame(callback, tasks, timestamp, logicalFrames) {
/**
* The local timestamp of the point in time at which this frame was
* received by the display, in milliseconds since the Unix Epoch.
*
* @type {!number}
*/
this.localTimestamp = new Date().getTime();
/**
* The remote timestamp of sync instruction associated with this frame.
* This timestamp is in milliseconds, but is arbitrary, having meaning
* only relative to other remote timestamps in the same connection.
*
* @type {!number}
*/
this.remoteTimestamp = timestamp || this.localTimestamp;
/**
* The number of remote desktop frames that were combined to produce
* this frame. If unknown or not applicable, this will be zero.
*
* @type {!number}
*/
this.logicalFrames = logicalFrames || 0;
/**
* Cancels rendering of this frame and all associated tasks. The
* callback provided at construction time, if any, is not invoked.
*/
this.cancel = function cancel() {
callback = null;
tasks.forEach(function cancelTask(task) {
task.cancel();
});
tasks = [];
};
/**
* Returns whether this frame is ready to be rendered. This function
* returns true if and only if ALL underlying tasks are unblocked.
*
* @returns {!boolean}
* true if all underlying tasks are unblocked, false otherwise.
*/
this.isReady = function() {
// Search for blocked tasks
for (var i=0; i < tasks.length; i++) {
if (tasks[i].blocked)
return false;
}
// If no blocked tasks, the frame is ready
return true;
};
/**
* Renders this frame, calling the associated callback, if any, after
* the frame is complete. This function MUST only be called when no
* blocked tasks exist. Calling this function with blocked tasks
* will result in undefined behavior.
*/
this.flush = function() {
// Draw all pending tasks.
for (var i=0; i < tasks.length; i++)
tasks[i].execute();
// Call callback
if (callback) callback();
};
};
/**
* A container for an task handler. Each operation which must be ordered
* is associated with a Task that goes into a task queue. Tasks in this
* queue are executed in order once their handlers are set, while Tasks
* without handlers block themselves and any following Tasks from running.
*
* @constructor
* @private
* @param {function} [taskHandler]
* The function to call when this task runs, if any.
*
* @param {boolean} [blocked]
* Whether this task should start blocked.
*/
function Task(taskHandler, blocked) {
/**
* Reference to this Task.
*
* @private
* @type {!Guacamole.Display.Task}
*/
var task = this;
/**
* Whether this Task is blocked.
*
* @type {boolean}
*/
this.blocked = blocked;
/**
* Cancels this task such that it will not run. The task handler
* provided at construction time, if any, is not invoked. Calling
* execute() after calling this function has no effect.
*/
this.cancel = function cancel() {
task.blocked = false;
taskHandler = null;
};
/**
* Unblocks this Task, allowing it to run.
*/
this.unblock = function() {
if (task.blocked) {
task.blocked = false;
__flush_frames();
}
};
/**
* Calls the handler associated with this task IMMEDIATELY. This
* function does not track whether this task is marked as blocked.
* Enforcing the blocked status of tasks is up to the caller.
*/
this.execute = function() {
if (taskHandler) taskHandler();
};
}
/**
* Schedules a task for future execution. The given handler will execute
* immediately after all previous tasks upon frame flush, unless this
* task is blocked. If any tasks is blocked, the entire frame will not
* render (and no tasks within will execute) until all tasks are unblocked.
*
* @private
* @param {function} [handler]
* The function to call when possible, if any.
*
* @param {boolean} [blocked]
* Whether the task should start blocked.
*
* @returns {!Task}
* The Task created and added to the queue for future running.
*/
function scheduleTask(handler, blocked) {
var task = new Task(handler, blocked);
tasks.push(task);
return task;
}
/**
* Returns the element which contains the Guacamole display.
*
* @return {!Element}
* The element containing the Guacamole display.
*/
this.getElement = function() {
return bounds;
};
/**
* Returns the width of this display.
*
* @return {!number}
* The width of this display;
*/
this.getWidth = function() {
return displayWidth;
};
/**
* Returns the height of this display.
*
* @return {!number}
* The height of this display;
*/
this.getHeight = function() {
return displayHeight;
};
/**
* Returns the default layer of this display. Each Guacamole display always
* has at least one layer. Other layers can optionally be created within
* this layer, but the default layer cannot be removed and is the absolute
* ancestor of all other layers.
*
* @return {!Guacamole.Display.VisibleLayer}
* The default layer.
*/
this.getDefaultLayer = function() {
return default_layer;
};
/**
* Returns the cursor layer of this display. Each Guacamole display contains
* a layer for the image of the mouse cursor. This layer is a special case
* and exists above all other layers, similar to the hardware mouse cursor.
*
* @return {!Guacamole.Display.VisibleLayer}
* The cursor layer.
*/
this.getCursorLayer = function() {
return cursor;
};
/**
* Creates a new layer. The new layer will be a direct child of the default
* layer, but can be moved to be a child of any other layer. Layers returned
* by this function are visible.
*
* @return {!Guacamole.Display.VisibleLayer}
* The newly-created layer.
*/
this.createLayer = function() {
var layer = new Guacamole.Display.VisibleLayer(displayWidth, displayHeight);
layer.move(default_layer, 0, 0, 0);
return layer;
};
/**
* Creates a new buffer. Buffers are invisible, off-screen surfaces. They
* are implemented in the same manner as layers, but do not provide the
* same nesting semantics.
*
* @return {!Guacamole.Layer}
* The newly-created buffer.
*/
this.createBuffer = function() {
var buffer = new Guacamole.Layer(0, 0);
buffer.autosize = 1;
return buffer;
};
/**
* Flush all pending draw tasks, if possible, as a new frame. If the entire
* frame is not ready, the flush will wait until all required tasks are
* unblocked.
*
* @param {function} [callback]
* The function to call when this frame is flushed. This may happen
* immediately, or later when blocked tasks become unblocked.
*
* @param {number} timestamp
* The remote timestamp of sync instruction associated with this frame.
* This timestamp is in milliseconds, but is arbitrary, having meaning
* only relative to other remote timestamps in the same connection.
*
* @param {number} logicalFrames
* The number of remote desktop frames that were combined to produce
* this frame.
*/
this.flush = function(callback, timestamp, logicalFrames) {
// Add frame, reset tasks
frames.push(new Frame(callback, tasks, timestamp, logicalFrames));
tasks = [];
// Attempt flush
__flush_frames();
};
/**
* Cancels rendering of all pending frames and associated rendering
* operations. The callbacks provided to outstanding past calls to flush(),
* if any, are not invoked.
*/
this.cancel = function cancel() {
frames.forEach(function cancelFrame(frame) {
frame.cancel();
});
frames = [];
tasks.forEach(function cancelTask(task) {
task.cancel();
});
tasks = [];
};
/**
* Sets the hotspot and image of the mouse cursor displayed within the
* Guacamole display.
*
* @param {!number} hotspotX
* The X coordinate of the cursor hotspot.
*
* @param {!number} hotspotY
* The Y coordinate of the cursor hotspot.
*
* @param {!Guacamole.Layer} layer
* The source layer containing the data which should be used as the
* mouse cursor image.
*
* @param {!number} srcx
* The X coordinate of the upper-left corner of the rectangle within
* the source layer's coordinate space to copy data from.
*
* @param {!number} srcy
* The Y coordinate of the upper-left corner of the rectangle within
* the source layer's coordinate space to copy data from.
*
* @param {!number} srcw
* The width of the rectangle within the source layer's coordinate
* space to copy data from.
*
* @param {!number} srch
* The height of the rectangle within the source layer's coordinate
* space to copy data from.
*/
this.setCursor = function(hotspotX, hotspotY, layer, srcx, srcy, srcw, srch) {
scheduleTask(function __display_set_cursor() {
// Set hotspot
guac_display.cursorHotspotX = hotspotX;
guac_display.cursorHotspotY = hotspotY;
// Reset cursor size
cursor.resize(srcw, srch);
// Draw cursor to cursor layer
cursor.copy(layer, srcx, srcy, srcw, srch, 0, 0);
guac_display.moveCursor(guac_display.cursorX, guac_display.cursorY);
// Fire cursor change event
if (guac_display.oncursor)
guac_display.oncursor(cursor.toCanvas(), hotspotX, hotspotY);
});
};
/**
* Sets whether the software-rendered cursor is shown. This cursor differs
* from the hardware cursor in that it is built into the Guacamole.Display,
* and relies on its own Guacamole layer to render.
*
* @param {boolean} [shown=true]
* Whether to show the software cursor.
*/
this.showCursor = function(shown) {
var element = cursor.getElement();
var parent = element.parentNode;
// Remove from DOM if hidden
if (shown === false) {
if (parent)
parent.removeChild(element);
}
// Otherwise, ensure cursor is child of display
else if (parent !== display)
display.appendChild(element);
};
/**
* Sets the location of the local cursor to the given coordinates. For the
* sake of responsiveness, this function performs its action immediately.
* Cursor motion is not maintained within atomic frames.
*
* @param {!number} x
* The X coordinate to move the cursor to.
*
* @param {!number} y
* The Y coordinate to move the cursor to.
*/
this.moveCursor = function(x, y) {
// Move cursor layer
cursor.translate(x - guac_display.cursorHotspotX,
y - guac_display.cursorHotspotY);
// Update stored position
guac_display.cursorX = x;
guac_display.cursorY = y;
};
/**
* Changes the size of the given Layer to the given width and height.
* Resizing is only attempted if the new size provided is actually different
* from the current size.
*
* @param {!Guacamole.Layer} layer
* The layer to resize.
*
* @param {!number} width
* The new width.
*
* @param {!number} height
* The new height.
*/
this.resize = function(layer, width, height) {
scheduleTask(function __display_resize() {
layer.resize(width, height);
// Resize display if default layer is resized
if (layer === default_layer) {
// Update (set) display size
displayWidth = width;
displayHeight = height;
display.style.width = displayWidth + "px";
display.style.height = displayHeight + "px";
// Update bounds size
bounds.style.width = (displayWidth*displayScale) + "px";
bounds.style.height = (displayHeight*displayScale) + "px";
// Notify of resize
if (guac_display.onresize)
guac_display.onresize(width, height);
}
});
};
/**
* Draws the specified image at the given coordinates. The image specified
* must already be loaded.
*
* @param {!Guacamole.Layer} layer
* The layer to draw upon.
*
* @param {!number} x
* The destination X coordinate.
*
* @param {!number} y
* The destination Y coordinate.
*
* @param {!CanvasImageSource} image
* The image to draw. Note that this not a URL.
*/
this.drawImage = function(layer, x, y, image) {
scheduleTask(function __display_drawImage() {
layer.drawImage(x, y, image);
});
};
/**
* Draws the image contained within the specified Blob at the given
* coordinates. The Blob specified must already be populated with image
* data.
*
* @param {!Guacamole.Layer} layer
* The layer to draw upon.
*
* @param {!number} x
* The destination X coordinate.
*
* @param {!number} y
* The destination Y coordinate.
*
* @param {!Blob} blob
* The Blob containing the image data to draw.
*/
this.drawBlob = function(layer, x, y, blob) {
var task;
// Prefer createImageBitmap() over blob URLs if available
if (window.createImageBitmap) {
var bitmap;
// Draw image once loaded
task = scheduleTask(function drawImageBitmap() {
layer.drawImage(x, y, bitmap);
}, true);
// Load image from provided blob
window.createImageBitmap(blob).then(function bitmapLoaded(decoded) {
bitmap = decoded;
task.unblock();
});
}
// Use blob URLs and the Image object if createImageBitmap() is
// unavailable
else {
// Create URL for blob
var url = URL.createObjectURL(blob);
// Draw and free blob URL when ready
task = scheduleTask(function __display_drawBlob() {
// Draw the image only if it loaded without errors
if (image.width && image.height)
layer.drawImage(x, y, image);
// Blob URL no longer needed
URL.revokeObjectURL(url);
}, true);
// Load image from URL
var image = new Image();
image.onload = task.unblock;
image.onerror = task.unblock;
image.src = url;
}
};
/**
* Draws the image within the given stream at the given coordinates. The
* image will be loaded automatically, and this and any future operations
* will wait for the image to finish loading. This function will
* automatically choose an appropriate method for reading and decoding the
* given image stream, and should be preferred for received streams except
* where manual decoding of the stream is unavoidable.
*
* @param {!Guacamole.Layer} layer
* The layer to draw upon.
*
* @param {!number} x
* The destination X coordinate.
*
* @param {!number} y
* The destination Y coordinate.
*
* @param {!Guacamole.InputStream} stream
* The stream along which image data will be received.
*
* @param {!string} mimetype
* The mimetype of the image within the stream.
*/
this.drawStream = function drawStream(layer, x, y, stream, mimetype) {
// If createImageBitmap() is available, load the image as a blob so
// that function can be used
if (window.createImageBitmap) {
var reader = new Guacamole.BlobReader(stream, mimetype);
reader.onend = function drawImageBlob() {
guac_display.drawBlob(layer, x, y, reader.getBlob());
};
}
// Lacking createImageBitmap(), fall back to data URIs and the Image
// object
else {
var reader = new Guacamole.DataURIReader(stream, mimetype);
reader.onend = function drawImageDataURI() {
guac_display.draw(layer, x, y, reader.getURI());
};
}
};