]> git.proxmox.com Git - mirror_novnc.git/blame - core/display.js
Wait for proper image load event
[mirror_novnc.git] / core / display.js
CommitLineData
c4164bda
JM
1/*
2 * noVNC: HTML5 VNC client
d58f8b51 3 * Copyright (C) 2012 Joel Martin
fdedbafb 4 * Copyright (C) 2015 Samuel Mannehed for Cendio AB
1d728ace 5 * Licensed under MPL 2.0 (see LICENSE.txt)
c4164bda
JM
6 *
7 * See README.md for usage and integration instructions.
8 */
c4164bda 9
1e13775b 10/*jslint browser: true, white: false */
d3796c14 11/*global Util, Base64, changeCursor */
c4164bda 12
ae510306
SR
13/* [module]
14 * import Util from "./util";
15 * import Base64 from "./base64";
16 */
17
18/* [module] export default */ function Display(defaults) {
19 this._drawCtx = null;
20 this._c_forceCanvas = false;
21
22 this._renderQ = []; // queue drawing actions for in-oder rendering
23
24 // the full frame buffer (logical canvas) size
25 this._fb_width = 0;
26 this._fb_height = 0;
27
28 // the size limit of the viewport (start disabled)
29 this._maxWidth = 0;
30 this._maxHeight = 0;
31
32 // the visible "physical canvas" viewport
33 this._viewportLoc = { 'x': 0, 'y': 0, 'w': 0, 'h': 0 };
34 this._cleanRect = { 'x1': 0, 'y1': 0, 'x2': -1, 'y2': -1 };
35
36 this._prevDrawStyle = "";
37 this._tile = null;
38 this._tile16x16 = null;
39 this._tile_x = 0;
40 this._tile_y = 0;
41
42 Util.set_defaults(this, defaults, {
43 'true_color': true,
44 'colourMap': [],
45 'scale': 1.0,
46 'viewport': false,
47 'render_mode': ''
48 });
49
50 Util.Debug(">> Display.constructor");
51
52 if (!this._target) {
53 throw new Error("Target must be set");
54 }
55
56 if (typeof this._target === 'string') {
57 throw new Error('target must be a DOM element');
58 }
59
60 if (!this._target.getContext) {
61 throw new Error("no getContext method");
62 }
63
64 if (!this._drawCtx) {
65 this._drawCtx = this._target.getContext('2d');
66 }
67
68 Util.Debug("User Agent: " + navigator.userAgent);
69 if (Util.Engine.gecko) { Util.Debug("Browser: gecko " + Util.Engine.gecko); }
70 if (Util.Engine.webkit) { Util.Debug("Browser: webkit " + Util.Engine.webkit); }
71 if (Util.Engine.trident) { Util.Debug("Browser: trident " + Util.Engine.trident); }
72 if (Util.Engine.presto) { Util.Debug("Browser: presto " + Util.Engine.presto); }
73
74 this.clear();
75
76 // Check canvas features
77 if ('createImageData' in this._drawCtx) {
78 this._render_mode = 'canvas rendering';
79 } else {
80 throw new Error("Canvas does not support createImageData");
81 }
82
83 if (this._prefer_js === null) {
84 Util.Info("Prefering javascript operations");
85 this._prefer_js = true;
86 }
87
88 // Determine browser support for setting the cursor via data URI scheme
89 if (this._cursor_uri || this._cursor_uri === null ||
90 this._cursor_uri === undefined) {
91 this._cursor_uri = Util.browserSupportsCursorURIs();
92 }
93
94 Util.Debug("<< Display.constructor");
95};
ff36b127 96
1e13775b
SR
97(function () {
98 "use strict";
e2e7c224 99
d1800d09
SR
100 var SUPPORTS_IMAGEDATA_CONSTRUCTOR = false;
101 try {
6521c6ac 102 new ImageData(new Uint8ClampedArray(4), 1, 1);
d1800d09
SR
103 SUPPORTS_IMAGEDATA_CONSTRUCTOR = true;
104 } catch (ex) {
105 // ignore failure
106 }
107
490d471c 108
1e13775b
SR
109 Display.prototype = {
110 // Public methods
636be753 111 viewportChangePos: function (deltaX, deltaY) {
1e13775b 112 var vp = this._viewportLoc;
a8255821 113 deltaX = Math.floor(deltaX);
114 deltaY = Math.floor(deltaY);
1e13775b
SR
115
116 if (!this._viewport) {
1e13775b
SR
117 deltaX = -vp.w; // clamped later of out of bounds
118 deltaY = -vp.h;
8db09746 119 }
54e7cbdf 120
1e13775b
SR
121 var vx2 = vp.x + vp.w - 1;
122 var vy2 = vp.y + vp.h - 1;
54e7cbdf 123
1e13775b 124 // Position change
54e7cbdf 125
1e13775b
SR
126 if (deltaX < 0 && vp.x + deltaX < 0) {
127 deltaX = -vp.x;
128 }
129 if (vx2 + deltaX >= this._fb_width) {
130 deltaX -= vx2 + deltaX - this._fb_width + 1;
131 }
54e7cbdf 132
1e13775b
SR
133 if (vp.y + deltaY < 0) {
134 deltaY = -vp.y;
135 }
136 if (vy2 + deltaY >= this._fb_height) {
137 deltaY -= (vy2 + deltaY - this._fb_height + 1);
138 }
54e7cbdf 139
1e13775b
SR
140 if (deltaX === 0 && deltaY === 0) {
141 return;
142 }
143 Util.Debug("viewportChange deltaX: " + deltaX + ", deltaY: " + deltaY);
144
145 vp.x += deltaX;
146 vx2 += deltaX;
147 vp.y += deltaY;
148 vy2 += deltaY;
149
150 // Update the clean rectangle
636be753 151 var cr = this._cleanRect;
1e13775b
SR
152 if (vp.x > cr.x1) {
153 cr.x1 = vp.x;
154 }
155 if (vx2 < cr.x2) {
156 cr.x2 = vx2;
157 }
158 if (vp.y > cr.y1) {
159 cr.y1 = vp.y;
160 }
161 if (vy2 < cr.y2) {
162 cr.y2 = vy2;
163 }
164
165 var x1, w;
166 if (deltaX < 0) {
167 // Shift viewport left, redraw left section
168 x1 = 0;
169 w = -deltaX;
170 } else {
171 // Shift viewport right, redraw right section
172 x1 = vp.w - deltaX;
173 w = deltaX;
174 }
175
176 var y1, h;
177 if (deltaY < 0) {
178 // Shift viewport up, redraw top section
179 y1 = 0;
180 h = -deltaY;
181 } else {
182 // Shift viewport down, redraw bottom section
183 y1 = vp.h - deltaY;
184 h = deltaY;
185 }
186
1e13775b 187 var saveStyle = this._drawCtx.fillStyle;
636be753 188 var canvas = this._target;
1e13775b 189 this._drawCtx.fillStyle = "rgb(255,255,255)";
a8255821 190
191 // Due to this bug among others [1] we need to disable the image-smoothing to
192 // avoid getting a blur effect when panning.
193 //
194 // 1. https://bugzilla.mozilla.org/show_bug.cgi?id=1194719
195 //
196 // We need to set these every time since all properties are reset
197 // when the the size is changed
198 if (this._drawCtx.mozImageSmoothingEnabled) {
199 this._drawCtx.mozImageSmoothingEnabled = false;
200 } else if (this._drawCtx.webkitImageSmoothingEnabled) {
201 this._drawCtx.webkitImageSmoothingEnabled = false;
202 } else if (this._drawCtx.msImageSmoothingEnabled) {
203 this._drawCtx.msImageSmoothingEnabled = false;
204 } else if (this._drawCtx.imageSmoothingEnabled) {
205 this._drawCtx.imageSmoothingEnabled = false;
206 }
207
208 // Copy the valid part of the viewport to the shifted location
209 this._drawCtx.drawImage(canvas, 0, 0, vp.w, vp.h, -deltaX, -deltaY, vp.w, vp.h);
210
1e13775b 211 if (deltaX !== 0) {
1e13775b
SR
212 this._drawCtx.fillRect(x1, 0, w, vp.h);
213 }
214 if (deltaY !== 0) {
1e13775b
SR
215 this._drawCtx.fillRect(0, y1, vp.w, h);
216 }
217 this._drawCtx.fillStyle = saveStyle;
218 },
219
636be753 220 viewportChangeSize: function(width, height) {
221
fdedbafb 222 if (typeof(width) === "undefined" || typeof(height) === "undefined") {
636be753 223
224 Util.Debug("Setting viewport to full display region");
225 width = this._fb_width;
226 height = this._fb_height;
227 }
228
229 var vp = this._viewportLoc;
230 if (vp.w !== width || vp.h !== height) {
231
fdedbafb 232 if (this._viewport) {
233 if (this._maxWidth !== 0 && width > this._maxWidth) {
234 width = this._maxWidth;
235 }
236 if (this._maxHeight !== 0 && height > this._maxHeight) {
237 height = this._maxHeight;
238 }
239 }
240
636be753 241 var cr = this._cleanRect;
242
243 if (width < vp.w && cr.x2 > vp.x + width - 1) {
244 cr.x2 = vp.x + width - 1;
245 }
636be753 246 if (height < vp.h && cr.y2 > vp.y + height - 1) {
247 cr.y2 = vp.y + height - 1;
248 }
249
fdedbafb 250 vp.w = width;
251 vp.h = height;
636be753 252
636be753 253 var canvas = this._target;
fdedbafb 254 if (canvas.width !== width || canvas.height !== height) {
255
256 // We have to save the canvas data since changing the size will clear it
257 var saveImg = null;
258 if (vp.w > 0 && vp.h > 0 && canvas.width > 0 && canvas.height > 0) {
259 var img_width = canvas.width < vp.w ? canvas.width : vp.w;
260 var img_height = canvas.height < vp.h ? canvas.height : vp.h;
261 saveImg = this._drawCtx.getImageData(0, 0, img_width, img_height);
262 }
263
6e296bfa 264 if (canvas.width !== width) {
265 canvas.width = width;
fdedbafb 266 canvas.style.width = width + 'px';
267 }
6e296bfa 268 if (canvas.height !== height) {
269 canvas.height = height;
270 canvas.style.height = height + 'px';
271 }
636be753 272
fdedbafb 273 if (saveImg) {
274 this._drawCtx.putImageData(saveImg, 0, 0);
275 }
636be753 276 }
277 }
278 },
279
1e13775b
SR
280 // Return a map of clean and dirty areas of the viewport and reset the
281 // tracking of clean and dirty areas
282 //
283 // Returns: { 'cleanBox': { 'x': x, 'y': y, 'w': w, 'h': h},
284 // 'dirtyBoxes': [{ 'x': x, 'y': y, 'w': w, 'h': h }, ...] }
285 getCleanDirtyReset: function () {
286 var vp = this._viewportLoc;
287 var cr = this._cleanRect;
288
289 var cleanBox = { 'x': cr.x1, 'y': cr.y1,
290 'w': cr.x2 - cr.x1 + 1, 'h': cr.y2 - cr.y1 + 1 };
291
292 var dirtyBoxes = [];
293 if (cr.x1 >= cr.x2 || cr.y1 >= cr.y2) {
294 // Whole viewport is dirty
295 dirtyBoxes.push({ 'x': vp.x, 'y': vp.y, 'w': vp.w, 'h': vp.h });
296 } else {
297 // Redraw dirty regions
298 var vx2 = vp.x + vp.w - 1;
299 var vy2 = vp.y + vp.h - 1;
300
301 if (vp.x < cr.x1) {
302 // left side dirty region
303 dirtyBoxes.push({'x': vp.x, 'y': vp.y,
304 'w': cr.x1 - vp.x + 1, 'h': vp.h});
305 }
306 if (vx2 > cr.x2) {
307 // right side dirty region
308 dirtyBoxes.push({'x': cr.x2 + 1, 'y': vp.y,
309 'w': vx2 - cr.x2, 'h': vp.h});
310 }
311 if(vp.y < cr.y1) {
312 // top/middle dirty region
313 dirtyBoxes.push({'x': cr.x1, 'y': vp.y,
314 'w': cr.x2 - cr.x1 + 1, 'h': cr.y1 - vp.y});
315 }
316 if (vy2 > cr.y2) {
317 // bottom/middle dirty region
318 dirtyBoxes.push({'x': cr.x1, 'y': cr.y2 + 1,
319 'w': cr.x2 - cr.x1 + 1, 'h': vy2 - cr.y2});
320 }
321 }
322
323 this._cleanRect = {'x1': vp.x, 'y1': vp.y,
324 'x2': vp.x + vp.w - 1, 'y2': vp.y + vp.h - 1};
325
326 return {'cleanBox': cleanBox, 'dirtyBoxes': dirtyBoxes};
327 },
328
329 absX: function (x) {
330 return x + this._viewportLoc.x;
331 },
332
333 absY: function (y) {
334 return y + this._viewportLoc.y;
335 },
336
337 resize: function (width, height) {
338 this._prevDrawStyle = "";
339
340 this._fb_width = width;
341 this._fb_height = height;
342
343 this._rescale(this._scale);
344
636be753 345 this.viewportChangeSize();
1e13775b
SR
346 },
347
348 clear: function () {
349 if (this._logo) {
350 this.resize(this._logo.width, this._logo.height);
351 this.blitStringImage(this._logo.data, 0, 0);
352 } else {
0b0b0433
SR
353 if (Util.Engine.trident === 6) {
354 // NB(directxman12): there's a bug in IE10 where we can fail to actually
355 // clear the canvas here because of the resize.
356 // Clearing the current viewport first fixes the issue
357 this._drawCtx.clearRect(0, 0, this._viewportLoc.w, this._viewportLoc.h);
358 }
795fca23 359 this.resize(240, 20);
1e13775b
SR
360 this._drawCtx.clearRect(0, 0, this._viewportLoc.w, this._viewportLoc.h);
361 }
362
363 this._renderQ = [];
364 },
365
f00193e0
SR
366 fillRect: function (x, y, width, height, color, from_queue) {
367 if (this._renderQ.length !== 0 && !from_queue) {
1578fa68 368 this._renderQ_push({
f00193e0
SR
369 'type': 'fill',
370 'x': x,
371 'y': y,
372 'width': width,
373 'height': height,
374 'color': color
375 });
376 } else {
377 this._setFillColor(color);
378 this._drawCtx.fillRect(x - this._viewportLoc.x, y - this._viewportLoc.y, width, height);
379 }
380 },
381
382 copyImage: function (old_x, old_y, new_x, new_y, w, h, from_queue) {
383 if (this._renderQ.length !== 0 && !from_queue) {
1578fa68 384 this._renderQ_push({
f00193e0
SR
385 'type': 'copy',
386 'old_x': old_x,
387 'old_y': old_y,
388 'x': new_x,
389 'y': new_y,
390 'width': w,
391 'height': h,
392 });
393 } else {
394 var x1 = old_x - this._viewportLoc.x;
395 var y1 = old_y - this._viewportLoc.y;
396 var x2 = new_x - this._viewportLoc.x;
397 var y2 = new_y - this._viewportLoc.y;
1e13775b 398
f00193e0
SR
399 this._drawCtx.drawImage(this._target, x1, y1, w, h, x2, y2, w, h);
400 }
1e13775b
SR
401 },
402
1578fa68
PO
403 imageRect: function(x, y, mime, arr) {
404 var img = new Image();
405 img.src = "data: " + mime + ";base64," + Base64.encode(arr);
406 this._renderQ_push({
407 'type': 'img',
408 'img': img,
409 'x': x,
410 'y': y
411 });
412 },
413
1e13775b
SR
414 // start updating a tile
415 startTile: function (x, y, width, height, color) {
416 this._tile_x = x;
417 this._tile_y = y;
418 if (width === 16 && height === 16) {
419 this._tile = this._tile16x16;
420 } else {
421 this._tile = this._drawCtx.createImageData(width, height);
422 }
423
424 if (this._prefer_js) {
425 var bgr;
426 if (this._true_color) {
427 bgr = color;
34d8b844 428 } else {
1e13775b 429 bgr = this._colourMap[color[0]];
34d8b844 430 }
1e13775b
SR
431 var red = bgr[2];
432 var green = bgr[1];
433 var blue = bgr[0];
434
435 var data = this._tile.data;
436 for (var i = 0; i < width * height * 4; i += 4) {
437 data[i] = red;
438 data[i + 1] = green;
439 data[i + 2] = blue;
440 data[i + 3] = 255;
441 }
442 } else {
f00193e0 443 this.fillRect(x, y, width, height, color, true);
1e13775b
SR
444 }
445 },
446
447 // update sub-rectangle of the current tile
448 subTile: function (x, y, w, h, color) {
449 if (this._prefer_js) {
450 var bgr;
451 if (this._true_color) {
452 bgr = color;
453 } else {
454 bgr = this._colourMap[color[0]];
455 }
456 var red = bgr[2];
457 var green = bgr[1];
458 var blue = bgr[0];
459 var xend = x + w;
460 var yend = y + h;
461
462 var data = this._tile.data;
463 var width = this._tile.width;
464 for (var j = y; j < yend; j++) {
465 for (var i = x; i < xend; i++) {
466 var p = (i + (j * width)) * 4;
467 data[p] = red;
468 data[p + 1] = green;
469 data[p + 2] = blue;
470 data[p + 3] = 255;
471 }
472 }
473 } else {
f00193e0 474 this.fillRect(this._tile_x + x, this._tile_y + y, w, h, color, true);
1e13775b
SR
475 }
476 },
34d8b844 477
1e13775b
SR
478 // draw the current tile to the screen
479 finishTile: function () {
480 if (this._prefer_js) {
481 this._drawCtx.putImageData(this._tile, this._tile_x - this._viewportLoc.x,
482 this._tile_y - this._viewportLoc.y);
483 }
484 // else: No-op -- already done by setSubTile
485 },
bc28395a 486
f00193e0
SR
487 blitImage: function (x, y, width, height, arr, offset, from_queue) {
488 if (this._renderQ.length !== 0 && !from_queue) {
7bc383e8
SR
489 // NB(directxman12): it's technically more performant here to use preallocated arrays,
490 // but it's a lot of extra work for not a lot of payoff -- if we're using the render queue,
491 // this probably isn't getting called *nearly* as much
492 var new_arr = new Uint8Array(width * height * 4);
493 new_arr.set(new Uint8Array(arr.buffer, 0, new_arr.length));
1578fa68 494 this._renderQ_push({
f00193e0 495 'type': 'blit',
7bc383e8 496 'data': new_arr,
f00193e0
SR
497 'x': x,
498 'y': y,
499 'width': width,
500 'height': height,
501 });
502 } else if (this._true_color) {
1e13775b
SR
503 this._bgrxImageData(x, y, this._viewportLoc.x, this._viewportLoc.y, width, height, arr, offset);
504 } else {
505 this._cmapImageData(x, y, this._viewportLoc.x, this._viewportLoc.y, width, height, arr, offset);
506 }
507 },
2c2b492c 508
f00193e0
SR
509 blitRgbImage: function (x, y , width, height, arr, offset, from_queue) {
510 if (this._renderQ.length !== 0 && !from_queue) {
7bc383e8
SR
511 // NB(directxman12): it's technically more performant here to use preallocated arrays,
512 // but it's a lot of extra work for not a lot of payoff -- if we're using the render queue,
513 // this probably isn't getting called *nearly* as much
6024677f 514 var new_arr = new Uint8Array(width * height * 3);
7bc383e8 515 new_arr.set(new Uint8Array(arr.buffer, 0, new_arr.length));
1578fa68 516 this._renderQ_push({
f00193e0 517 'type': 'blitRgb',
7bc383e8 518 'data': new_arr,
f00193e0
SR
519 'x': x,
520 'y': y,
521 'width': width,
522 'height': height,
523 });
524 } else if (this._true_color) {
1e13775b
SR
525 this._rgbImageData(x, y, this._viewportLoc.x, this._viewportLoc.y, width, height, arr, offset);
526 } else {
527 // probably wrong?
528 this._cmapImageData(x, y, this._viewportLoc.x, this._viewportLoc.y, width, height, arr, offset);
529 }
530 },
531
f00193e0
SR
532 blitRgbxImage: function (x, y, width, height, arr, offset, from_queue) {
533 if (this._renderQ.length !== 0 && !from_queue) {
7bc383e8 534 // NB(directxman12): it's technically more performant here to use preallocated arrays,
f00193e0
SR
535 // but it's a lot of extra work for not a lot of payoff -- if we're using the render queue,
536 // this probably isn't getting called *nearly* as much
537 var new_arr = new Uint8Array(width * height * 4);
538 new_arr.set(new Uint8Array(arr.buffer, 0, new_arr.length));
1578fa68 539 this._renderQ_push({
f00193e0
SR
540 'type': 'blitRgbx',
541 'data': new_arr,
542 'x': x,
543 'y': y,
544 'width': width,
545 'height': height,
546 });
547 } else {
548 this._rgbxImageData(x, y, this._viewportLoc.x, this._viewportLoc.y, width, height, arr, offset);
549 }
d1800d09
SR
550 },
551
1e13775b
SR
552 blitStringImage: function (str, x, y) {
553 var img = new Image();
554 img.onload = function () {
555 this._drawCtx.drawImage(img, x - this._viewportLoc.x, y - this._viewportLoc.y);
556 }.bind(this);
557 img.src = str;
558 return img; // for debugging purposes
559 },
560
561 // wrap ctx.drawImage but relative to viewport
562 drawImage: function (img, x, y) {
563 this._drawCtx.drawImage(img, x - this._viewportLoc.x, y - this._viewportLoc.y);
564 },
565
1e13775b
SR
566 changeCursor: function (pixels, mask, hotx, hoty, w, h) {
567 if (this._cursor_uri === false) {
568 Util.Warn("changeCursor called but no cursor data URI support");
569 return;
570 }
d3796c14 571
1e13775b
SR
572 if (this._true_color) {
573 Display.changeCursor(this._target, pixels, mask, hotx, hoty, w, h);
574 } else {
575 Display.changeCursor(this._target, pixels, mask, hotx, hoty, w, h, this._colourMap);
576 }
577 },
578
579 defaultCursor: function () {
580 this._target.style.cursor = "default";
581 },
582
b804b3e4 583 disableLocalCursor: function () {
584 this._target.style.cursor = "none";
585 },
586
fdedbafb 587 clippingDisplay: function () {
588 var vp = this._viewportLoc;
589
590 var fbClip = this._fb_width > vp.w || this._fb_height > vp.h;
591 var limitedVp = this._maxWidth !== 0 && this._maxHeight !== 0;
592 var clipping = false;
593
594 if (limitedVp) {
595 clipping = vp.w > this._maxWidth || vp.h > this._maxHeight;
596 }
597
598 return fbClip || (limitedVp && clipping);
636be753 599 },
600
1e13775b
SR
601 // Overridden getters/setters
602 get_context: function () {
603 return this._drawCtx;
604 },
605
606 set_scale: function (scale) {
607 this._rescale(scale);
608 },
609
610 set_width: function (w) {
636be753 611 this._fb_width = w;
1e13775b
SR
612 },
613 get_width: function () {
614 return this._fb_width;
615 },
616
617 set_height: function (h) {
636be753 618 this._fb_height = h;
1e13775b
SR
619 },
620 get_height: function () {
621 return this._fb_height;
622 },
623
72747869
SR
624 autoscale: function (containerWidth, containerHeight, downscaleOnly) {
625 var targetAspectRatio = containerWidth / containerHeight;
626 var fbAspectRatio = this._fb_width / this._fb_height;
9a23006e 627
72747869
SR
628 var scaleRatio;
629 if (fbAspectRatio >= targetAspectRatio) {
630 scaleRatio = containerWidth / this._fb_width;
631 } else {
632 scaleRatio = containerHeight / this._fb_height;
1e13775b 633 }
9a23006e 634
72747869
SR
635 var targetW, targetH;
636 if (scaleRatio > 1.0 && downscaleOnly) {
637 targetW = this._fb_width;
638 targetH = this._fb_height;
639 scaleRatio = 1.0;
640 } else if (fbAspectRatio >= targetAspectRatio) {
641 targetW = containerWidth;
642 targetH = Math.round(containerWidth / fbAspectRatio);
643 } else {
644 targetW = Math.round(containerHeight * fbAspectRatio);
645 targetH = containerHeight;
1e13775b 646 }
9a23006e 647
72747869
SR
648 // NB(directxman12): If you set the width directly, or set the
649 // style width to a number, the canvas is cleared.
650 // However, if you set the style width to a string
651 // ('NNNpx'), the canvas is scaled without clearing.
652 this._target.style.width = targetW + 'px';
653 this._target.style.height = targetH + 'px';
654
655 this._scale = scaleRatio;
656
657 return scaleRatio; // so that the mouse, etc scale can be set
658 },
1e13775b 659
72747869
SR
660 // Private Methods
661 _rescale: function (factor) {
1e13775b 662 this._scale = factor;
72747869 663
fdedbafb 664 var w;
665 var h;
666
667 if (this._viewport &&
668 this._maxWidth !== 0 && this._maxHeight !== 0) {
669 w = Math.min(this._fb_width, this._maxWidth);
670 h = Math.min(this._fb_height, this._maxHeight);
671 } else {
672 w = this._fb_width;
673 h = this._fb_height;
674 }
675
676 this._target.style.width = Math.round(factor * w) + 'px';
677 this._target.style.height = Math.round(factor * h) + 'px';
1e13775b 678 },
67b4e987 679
1e13775b
SR
680 _setFillColor: function (color) {
681 var bgr;
682 if (this._true_color) {
683 bgr = color;
9a23006e 684 } else {
a369a80c 685 bgr = this._colourMap[color];
1e13775b
SR
686 }
687
688 var newStyle = 'rgb(' + bgr[2] + ',' + bgr[1] + ',' + bgr[0] + ')';
689 if (newStyle !== this._prevDrawStyle) {
690 this._drawCtx.fillStyle = newStyle;
691 this._prevDrawStyle = newStyle;
692 }
693 },
694
695 _rgbImageData: function (x, y, vx, vy, width, height, arr, offset) {
696 var img = this._drawCtx.createImageData(width, height);
697 var data = img.data;
698 for (var i = 0, j = offset; i < width * height * 4; i += 4, j += 3) {
699 data[i] = arr[j];
700 data[i + 1] = arr[j + 1];
701 data[i + 2] = arr[j + 2];
702 data[i + 3] = 255; // Alpha
703 }
704 this._drawCtx.putImageData(img, x - vx, y - vy);
705 },
706
707 _bgrxImageData: function (x, y, vx, vy, width, height, arr, offset) {
708 var img = this._drawCtx.createImageData(width, height);
709 var data = img.data;
710 for (var i = 0, j = offset; i < width * height * 4; i += 4, j += 4) {
711 data[i] = arr[j + 2];
712 data[i + 1] = arr[j + 1];
713 data[i + 2] = arr[j];
714 data[i + 3] = 255; // Alpha
715 }
716 this._drawCtx.putImageData(img, x - vx, y - vy);
717 },
718
d1800d09
SR
719 _rgbxImageData: function (x, y, vx, vy, width, height, arr, offset) {
720 // NB(directxman12): arr must be an Type Array view
d1800d09
SR
721 var img;
722 if (SUPPORTS_IMAGEDATA_CONSTRUCTOR) {
f00193e0 723 img = new ImageData(new Uint8ClampedArray(arr.buffer, arr.byteOffset, width * height * 4), width, height);
d1800d09
SR
724 } else {
725 img = this._drawCtx.createImageData(width, height);
f00193e0 726 img.data.set(new Uint8ClampedArray(arr.buffer, arr.byteOffset, width * height * 4));
d1800d09
SR
727 }
728 this._drawCtx.putImageData(img, x - vx, y - vy);
729 },
730
1e13775b
SR
731 _cmapImageData: function (x, y, vx, vy, width, height, arr, offset) {
732 var img = this._drawCtx.createImageData(width, height);
733 var data = img.data;
734 var cmap = this._colourMap;
735 for (var i = 0, j = offset; i < width * height * 4; i += 4, j++) {
736 var bgr = cmap[arr[j]];
737 data[i] = bgr[2];
738 data[i + 1] = bgr[1];
739 data[i + 2] = bgr[0];
740 data[i + 3] = 255; // Alpha
741 }
742 this._drawCtx.putImageData(img, x - vx, y - vy);
743 },
744
1578fa68
PO
745 _renderQ_push: function (action) {
746 this._renderQ.push(action);
747 if (this._renderQ.length === 1) {
748 // If this can be rendered immediately it will be, otherwise
bb6965f2 749 // the scanner will wait for the relevant event
1578fa68
PO
750 this._scan_renderQ();
751 }
752 },
753
bb6965f2
PO
754 _resume_renderQ: function() {
755 // "this" is the object that is ready, not the
756 // display object
757 this.removeEventListener('load', this._noVNC_display._resume_renderQ);
758 this._noVNC_display._scan_renderQ();
759 },
760
1e13775b
SR
761 _scan_renderQ: function () {
762 var ready = true;
763 while (ready && this._renderQ.length > 0) {
764 var a = this._renderQ[0];
765 switch (a.type) {
766 case 'copy':
f00193e0 767 this.copyImage(a.old_x, a.old_y, a.x, a.y, a.width, a.height, true);
1e13775b
SR
768 break;
769 case 'fill':
f00193e0 770 this.fillRect(a.x, a.y, a.width, a.height, a.color, true);
1e13775b
SR
771 break;
772 case 'blit':
f00193e0 773 this.blitImage(a.x, a.y, a.width, a.height, a.data, 0, true);
1e13775b
SR
774 break;
775 case 'blitRgb':
f00193e0 776 this.blitRgbImage(a.x, a.y, a.width, a.height, a.data, 0, true);
1e13775b 777 break;
d1800d09 778 case 'blitRgbx':
f00193e0 779 this.blitRgbxImage(a.x, a.y, a.width, a.height, a.data, 0, true);
1e13775b
SR
780 break;
781 case 'img':
782 if (a.img.complete) {
783 this.drawImage(a.img, a.x, a.y);
784 } else {
bb6965f2
PO
785 a.img._noVNC_display = this;
786 a.img.addEventListener('load', this._resume_renderQ);
1e13775b
SR
787 // We need to wait for this image to 'load'
788 // to keep things in-order
789 ready = false;
790 }
791 break;
792 }
793
794 if (ready) {
795 this._renderQ.shift();
796 }
797 }
1e13775b
SR
798 },
799 };
800
801 Util.make_properties(Display, [
802 ['target', 'wo', 'dom'], // Canvas element for rendering
803 ['context', 'ro', 'raw'], // Canvas 2D context for rendering (read-only)
804 ['logo', 'rw', 'raw'], // Logo to display when cleared: {"width": w, "height": h, "data": data}
805 ['true_color', 'rw', 'bool'], // Use true-color pixel data
806 ['colourMap', 'rw', 'arr'], // Colour map array (when not true-color)
807 ['scale', 'rw', 'float'], // Display area scale factor 0.0 - 1.0
fdedbafb 808 ['viewport', 'rw', 'bool'], // Use viewport clipping
1e13775b
SR
809 ['width', 'rw', 'int'], // Display area width
810 ['height', 'rw', 'int'], // Display area height
fdedbafb 811 ['maxWidth', 'rw', 'int'], // Viewport max width (0 if disabled)
812 ['maxHeight', 'rw', 'int'], // Viewport max height (0 if disabled)
1e13775b
SR
813
814 ['render_mode', 'ro', 'str'], // Canvas rendering mode (read-only)
815
816 ['prefer_js', 'rw', 'str'], // Prefer Javascript over canvas methods
817 ['cursor_uri', 'rw', 'raw'] // Can we render cursor using data URI
818 ]);
819
820 // Class Methods
821 Display.changeCursor = function (target, pixels, mask, hotx, hoty, w0, h0, cmap) {
822 var w = w0;
823 var h = h0;
824 if (h < w) {
825 h = w; // increase h to make it square
826 } else {
827 w = h; // increase w to make it square
828 }
829
830 var cur = [];
831
832 // Push multi-byte little-endian values
833 cur.push16le = function (num) {
834 this.push(num & 0xFF, (num >> 8) & 0xFF);
835 };
836 cur.push32le = function (num) {
837 this.push(num & 0xFF,
838 (num >> 8) & 0xFF,
839 (num >> 16) & 0xFF,
840 (num >> 24) & 0xFF);
841 };
842
843 var IHDRsz = 40;
844 var RGBsz = w * h * 4;
845 var XORsz = Math.ceil((w * h) / 8.0);
846 var ANDsz = Math.ceil((w * h) / 8.0);
847
848 cur.push16le(0); // 0: Reserved
849 cur.push16le(2); // 2: .CUR type
850 cur.push16le(1); // 4: Number of images, 1 for non-animated ico
851
852 // Cursor #1 header (ICONDIRENTRY)
853 cur.push(w); // 6: width
854 cur.push(h); // 7: height
855 cur.push(0); // 8: colors, 0 -> true-color
856 cur.push(0); // 9: reserved
857 cur.push16le(hotx); // 10: hotspot x coordinate
858 cur.push16le(hoty); // 12: hotspot y coordinate
859 cur.push32le(IHDRsz + RGBsz + XORsz + ANDsz);
860 // 14: cursor data byte size
861 cur.push32le(22); // 18: offset of cursor data in the file
862
863 // Cursor #1 InfoHeader (ICONIMAGE/BITMAPINFO)
864 cur.push32le(IHDRsz); // 22: InfoHeader size
865 cur.push32le(w); // 26: Cursor width
866 cur.push32le(h * 2); // 30: XOR+AND height
867 cur.push16le(1); // 34: number of planes
868 cur.push16le(32); // 36: bits per pixel
869 cur.push32le(0); // 38: Type of compression
870
871 cur.push32le(XORsz + ANDsz);
872 // 42: Size of Image
873 cur.push32le(0); // 46: reserved
874 cur.push32le(0); // 50: reserved
875 cur.push32le(0); // 54: reserved
876 cur.push32le(0); // 58: reserved
877
878 // 62: color data (RGBQUAD icColors[])
879 var y, x;
880 for (y = h - 1; y >= 0; y--) {
881 for (x = 0; x < w; x++) {
882 if (x >= w0 || y >= h0) {
883 cur.push(0); // blue
884 cur.push(0); // green
885 cur.push(0); // red
886 cur.push(0); // alpha
6f4cbb3f 887 } else {
1e13775b
SR
888 var idx = y * Math.ceil(w0 / 8) + Math.floor(x / 8);
889 var alpha = (mask[idx] << (x % 8)) & 0x80 ? 255 : 0;
890 if (cmap) {
891 idx = (w0 * y) + x;
892 var rgb = cmap[pixels[idx]];
893 cur.push(rgb[2]); // blue
894 cur.push(rgb[1]); // green
895 cur.push(rgb[0]); // red
896 cur.push(alpha); // alpha
ec31f82e
SR
897 } else {
898 idx = ((w0 * y) + x) * 4;
899 cur.push(pixels[idx + 2]); // blue
900 cur.push(pixels[idx + 1]); // green
901 cur.push(pixels[idx]); // red
902 cur.push(alpha); // alpha
1e13775b 903 }
6f4cbb3f 904 }
2c2b492c
JM
905 }
906 }
2c2b492c 907
1e13775b
SR
908 // XOR/bitmask data (BYTE icXOR[])
909 // (ignored, just needs to be the right size)
910 for (y = 0; y < h; y++) {
911 for (x = 0; x < Math.ceil(w / 8); x++) {
912 cur.push(0);
913 }
9a23006e 914 }
9a23006e 915
1e13775b
SR
916 // AND/bitmask data (BYTE icAND[])
917 // (ignored, just needs to be the right size)
918 for (y = 0; y < h; y++) {
919 for (x = 0; x < Math.ceil(w / 8); x++) {
920 cur.push(0);
921 }
2c2b492c 922 }
2c2b492c 923
1e13775b
SR
924 var url = 'data:image/x-icon;base64,' + Base64.encode(cur);
925 target.style.cursor = 'url(' + url + ')' + hotx + ' ' + hoty + ', default';
926 };
927})();