display.js 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757
  1. /*
  2. * noVNC: HTML5 VNC client
  3. * Copyright (C) 2012 Joel Martin
  4. * Licensed under LGPL-3 (see LICENSE.txt)
  5. *
  6. * See README.md for usage and integration instructions.
  7. */
  8. /*jslint browser: true, white: false, bitwise: false */
  9. /*global Util, Base64, changeCursor */
  10. function Display(defaults) {
  11. "use strict";
  12. var that = {}, // Public API methods
  13. conf = {}, // Configuration attributes
  14. // Private Display namespace variables
  15. c_ctx = null,
  16. c_forceCanvas = false,
  17. // Queued drawing actions for in-order rendering
  18. renderQ = [],
  19. // Predefine function variables (jslint)
  20. imageDataGet, rgbImageData, bgrxImageData, cmapImageData,
  21. setFillColor, rescale, scan_renderQ,
  22. // The full frame buffer (logical canvas) size
  23. fb_width = 0,
  24. fb_height = 0,
  25. // The visible "physical canvas" viewport
  26. viewport = {'x': 0, 'y': 0, 'w' : 0, 'h' : 0 },
  27. cleanRect = {'x1': 0, 'y1': 0, 'x2': -1, 'y2': -1},
  28. c_prevStyle = "",
  29. tile = null,
  30. tile16x16 = null,
  31. tile_x = 0,
  32. tile_y = 0;
  33. // Configuration attributes
  34. Util.conf_defaults(conf, that, defaults, [
  35. ['target', 'wo', 'dom', null, 'Canvas element for rendering'],
  36. ['context', 'ro', 'raw', null, 'Canvas 2D context for rendering (read-only)'],
  37. ['logo', 'rw', 'raw', null, 'Logo to display when cleared: {"width": width, "height": height, "data": data}'],
  38. ['true_color', 'rw', 'bool', true, 'Use true-color pixel data'],
  39. ['colourMap', 'rw', 'arr', [], 'Colour map array (when not true-color)'],
  40. ['scale', 'rw', 'float', 1.0, 'Display area scale factor 0.0 - 1.0'],
  41. ['viewport', 'rw', 'bool', false, 'Use a viewport set with viewportChange()'],
  42. ['width', 'rw', 'int', null, 'Display area width'],
  43. ['height', 'rw', 'int', null, 'Display area height'],
  44. ['render_mode', 'ro', 'str', '', 'Canvas rendering mode (read-only)'],
  45. ['prefer_js', 'rw', 'str', null, 'Prefer Javascript over canvas methods'],
  46. ['cursor_uri', 'rw', 'raw', null, 'Can we render cursor using data URI']
  47. ]);
  48. // Override some specific getters/setters
  49. that.get_context = function () { return c_ctx; };
  50. that.set_scale = function(scale) { rescale(scale); };
  51. that.set_width = function (val) { that.resize(val, fb_height); };
  52. that.get_width = function() { return fb_width; };
  53. that.set_height = function (val) { that.resize(fb_width, val); };
  54. that.get_height = function() { return fb_height; };
  55. //
  56. // Private functions
  57. //
  58. // Create the public API interface
  59. function constructor() {
  60. Util.Debug(">> Display.constructor");
  61. var c, func, i, curDat, curSave,
  62. has_imageData = false, UE = Util.Engine;
  63. if (! conf.target) { throw("target must be set"); }
  64. if (typeof conf.target === 'string') {
  65. throw("target must be a DOM element");
  66. }
  67. c = conf.target;
  68. if (! c.getContext) { throw("no getContext method"); }
  69. if (! c_ctx) { c_ctx = c.getContext('2d'); }
  70. Util.Debug("User Agent: " + navigator.userAgent);
  71. if (UE.gecko) { Util.Debug("Browser: gecko " + UE.gecko); }
  72. if (UE.webkit) { Util.Debug("Browser: webkit " + UE.webkit); }
  73. if (UE.trident) { Util.Debug("Browser: trident " + UE.trident); }
  74. if (UE.presto) { Util.Debug("Browser: presto " + UE.presto); }
  75. that.clear();
  76. // Check canvas features
  77. if ('createImageData' in c_ctx) {
  78. conf.render_mode = "canvas rendering";
  79. } else {
  80. throw("Canvas does not support createImageData");
  81. }
  82. if (conf.prefer_js === null) {
  83. Util.Info("Prefering javascript operations");
  84. conf.prefer_js = true;
  85. }
  86. // Initialize cached tile imageData
  87. tile16x16 = c_ctx.createImageData(16, 16);
  88. /*
  89. * Determine browser support for setting the cursor via data URI
  90. * scheme
  91. */
  92. curDat = [];
  93. for (i=0; i < 8 * 8 * 4; i += 1) {
  94. curDat.push(255);
  95. }
  96. try {
  97. curSave = c.style.cursor;
  98. changeCursor(conf.target, curDat, curDat, 2, 2, 8, 8);
  99. if (c.style.cursor) {
  100. if (conf.cursor_uri === null) {
  101. conf.cursor_uri = true;
  102. }
  103. Util.Info("Data URI scheme cursor supported");
  104. } else {
  105. if (conf.cursor_uri === null) {
  106. conf.cursor_uri = false;
  107. }
  108. Util.Warn("Data URI scheme cursor not supported");
  109. }
  110. c.style.cursor = curSave;
  111. } catch (exc2) {
  112. Util.Error("Data URI scheme cursor test exception: " + exc2);
  113. conf.cursor_uri = false;
  114. }
  115. Util.Debug("<< Display.constructor");
  116. return that ;
  117. }
  118. rescale = function(factor) {
  119. var c, tp, x, y,
  120. properties = ['transform', 'WebkitTransform', 'MozTransform', null];
  121. c = conf.target;
  122. tp = properties.shift();
  123. while (tp) {
  124. if (typeof c.style[tp] !== 'undefined') {
  125. break;
  126. }
  127. tp = properties.shift();
  128. }
  129. if (tp === null) {
  130. Util.Debug("No scaling support");
  131. return;
  132. }
  133. if (typeof(factor) === "undefined") {
  134. factor = conf.scale;
  135. } else if (factor > 1.0) {
  136. factor = 1.0;
  137. } else if (factor < 0.1) {
  138. factor = 0.1;
  139. }
  140. if (conf.scale === factor) {
  141. //Util.Debug("Display already scaled to '" + factor + "'");
  142. return;
  143. }
  144. conf.scale = factor;
  145. x = c.width - c.width * factor;
  146. y = c.height - c.height * factor;
  147. c.style[tp] = "scale(" + conf.scale + ") translate(-" + x + "px, -" + y + "px)";
  148. };
  149. setFillColor = function(color) {
  150. var bgr, newStyle;
  151. if (conf.true_color) {
  152. bgr = color;
  153. } else {
  154. bgr = conf.colourMap[color[0]];
  155. }
  156. newStyle = "rgb(" + bgr[2] + "," + bgr[1] + "," + bgr[0] + ")";
  157. if (newStyle !== c_prevStyle) {
  158. c_ctx.fillStyle = newStyle;
  159. c_prevStyle = newStyle;
  160. }
  161. };
  162. //
  163. // Public API interface functions
  164. //
  165. // Shift and/or resize the visible viewport
  166. that.viewportChange = function(deltaX, deltaY, width, height) {
  167. var c = conf.target, v = viewport, cr = cleanRect,
  168. saveImg = null, saveStyle, x1, y1, vx2, vy2, w, h;
  169. if (!conf.viewport) {
  170. Util.Debug("Setting viewport to full display region");
  171. deltaX = -v.w; // Clamped later if out of bounds
  172. deltaY = -v.h; // Clamped later if out of bounds
  173. width = fb_width;
  174. height = fb_height;
  175. }
  176. if (typeof(deltaX) === "undefined") { deltaX = 0; }
  177. if (typeof(deltaY) === "undefined") { deltaY = 0; }
  178. if (typeof(width) === "undefined") { width = v.w; }
  179. if (typeof(height) === "undefined") { height = v.h; }
  180. // Size change
  181. if (width > fb_width) { width = fb_width; }
  182. if (height > fb_height) { height = fb_height; }
  183. if ((v.w !== width) || (v.h !== height)) {
  184. // Change width
  185. if ((width < v.w) && (cr.x2 > v.x + width -1)) {
  186. cr.x2 = v.x + width - 1;
  187. }
  188. v.w = width;
  189. // Change height
  190. if ((height < v.h) && (cr.y2 > v.y + height -1)) {
  191. cr.y2 = v.y + height - 1;
  192. }
  193. v.h = height;
  194. if (v.w > 0 && v.h > 0 && c.width > 0 && c.height > 0) {
  195. saveImg = c_ctx.getImageData(0, 0,
  196. (c.width < v.w) ? c.width : v.w,
  197. (c.height < v.h) ? c.height : v.h);
  198. }
  199. c.width = v.w;
  200. c.height = v.h;
  201. if (saveImg) {
  202. c_ctx.putImageData(saveImg, 0, 0);
  203. }
  204. }
  205. vx2 = v.x + v.w - 1;
  206. vy2 = v.y + v.h - 1;
  207. // Position change
  208. if ((deltaX < 0) && ((v.x + deltaX) < 0)) {
  209. deltaX = - v.x;
  210. }
  211. if ((vx2 + deltaX) >= fb_width) {
  212. deltaX -= ((vx2 + deltaX) - fb_width + 1);
  213. }
  214. if ((v.y + deltaY) < 0) {
  215. deltaY = - v.y;
  216. }
  217. if ((vy2 + deltaY) >= fb_height) {
  218. deltaY -= ((vy2 + deltaY) - fb_height + 1);
  219. }
  220. if ((deltaX === 0) && (deltaY === 0)) {
  221. //Util.Debug("skipping viewport change");
  222. return;
  223. }
  224. Util.Debug("viewportChange deltaX: " + deltaX + ", deltaY: " + deltaY);
  225. v.x += deltaX;
  226. vx2 += deltaX;
  227. v.y += deltaY;
  228. vy2 += deltaY;
  229. // Update the clean rectangle
  230. if (v.x > cr.x1) {
  231. cr.x1 = v.x;
  232. }
  233. if (vx2 < cr.x2) {
  234. cr.x2 = vx2;
  235. }
  236. if (v.y > cr.y1) {
  237. cr.y1 = v.y;
  238. }
  239. if (vy2 < cr.y2) {
  240. cr.y2 = vy2;
  241. }
  242. if (deltaX < 0) {
  243. // Shift viewport left, redraw left section
  244. x1 = 0;
  245. w = - deltaX;
  246. } else {
  247. // Shift viewport right, redraw right section
  248. x1 = v.w - deltaX;
  249. w = deltaX;
  250. }
  251. if (deltaY < 0) {
  252. // Shift viewport up, redraw top section
  253. y1 = 0;
  254. h = - deltaY;
  255. } else {
  256. // Shift viewport down, redraw bottom section
  257. y1 = v.h - deltaY;
  258. h = deltaY;
  259. }
  260. // Copy the valid part of the viewport to the shifted location
  261. saveStyle = c_ctx.fillStyle;
  262. c_ctx.fillStyle = "rgb(255,255,255)";
  263. if (deltaX !== 0) {
  264. //that.copyImage(0, 0, -deltaX, 0, v.w, v.h);
  265. //that.fillRect(x1, 0, w, v.h, [255,255,255]);
  266. c_ctx.drawImage(c, 0, 0, v.w, v.h, -deltaX, 0, v.w, v.h);
  267. c_ctx.fillRect(x1, 0, w, v.h);
  268. }
  269. if (deltaY !== 0) {
  270. //that.copyImage(0, 0, 0, -deltaY, v.w, v.h);
  271. //that.fillRect(0, y1, v.w, h, [255,255,255]);
  272. c_ctx.drawImage(c, 0, 0, v.w, v.h, 0, -deltaY, v.w, v.h);
  273. c_ctx.fillRect(0, y1, v.w, h);
  274. }
  275. c_ctx.fillStyle = saveStyle;
  276. };
  277. // Return a map of clean and dirty areas of the viewport and reset the
  278. // tracking of clean and dirty areas.
  279. //
  280. // Returns: {'cleanBox': {'x': x, 'y': y, 'w': w, 'h': h},
  281. // 'dirtyBoxes': [{'x': x, 'y': y, 'w': w, 'h': h}, ...]}
  282. that.getCleanDirtyReset = function() {
  283. var v = viewport, c = cleanRect, cleanBox, dirtyBoxes = [],
  284. vx2 = v.x + v.w - 1, vy2 = v.y + v.h - 1;
  285. // Copy the cleanRect
  286. cleanBox = {'x': c.x1, 'y': c.y1,
  287. 'w': c.x2 - c.x1 + 1, 'h': c.y2 - c.y1 + 1};
  288. if ((c.x1 >= c.x2) || (c.y1 >= c.y2)) {
  289. // Whole viewport is dirty
  290. dirtyBoxes.push({'x': v.x, 'y': v.y, 'w': v.w, 'h': v.h});
  291. } else {
  292. // Redraw dirty regions
  293. if (v.x < c.x1) {
  294. // left side dirty region
  295. dirtyBoxes.push({'x': v.x, 'y': v.y,
  296. 'w': c.x1 - v.x + 1, 'h': v.h});
  297. }
  298. if (vx2 > c.x2) {
  299. // right side dirty region
  300. dirtyBoxes.push({'x': c.x2 + 1, 'y': v.y,
  301. 'w': vx2 - c.x2, 'h': v.h});
  302. }
  303. if (v.y < c.y1) {
  304. // top/middle dirty region
  305. dirtyBoxes.push({'x': c.x1, 'y': v.y,
  306. 'w': c.x2 - c.x1 + 1, 'h': c.y1 - v.y});
  307. }
  308. if (vy2 > c.y2) {
  309. // bottom/middle dirty region
  310. dirtyBoxes.push({'x': c.x1, 'y': c.y2 + 1,
  311. 'w': c.x2 - c.x1 + 1, 'h': vy2 - c.y2});
  312. }
  313. }
  314. // Reset the cleanRect to the whole viewport
  315. cleanRect = {'x1': v.x, 'y1': v.y,
  316. 'x2': v.x + v.w - 1, 'y2': v.y + v.h - 1};
  317. return {'cleanBox': cleanBox, 'dirtyBoxes': dirtyBoxes};
  318. };
  319. // Translate viewport coordinates to absolute coordinates
  320. that.absX = function(x) {
  321. return x + viewport.x;
  322. };
  323. that.absY = function(y) {
  324. return y + viewport.y;
  325. };
  326. that.resize = function(width, height) {
  327. c_prevStyle = "";
  328. fb_width = width;
  329. fb_height = height;
  330. rescale(conf.scale);
  331. that.viewportChange();
  332. };
  333. that.clear = function() {
  334. if (conf.logo) {
  335. that.resize(conf.logo.width, conf.logo.height);
  336. that.blitStringImage(conf.logo.data, 0, 0);
  337. } else {
  338. that.resize(640, 20);
  339. c_ctx.clearRect(0, 0, viewport.w, viewport.h);
  340. }
  341. renderQ = [];
  342. // No benefit over default ("source-over") in Chrome and firefox
  343. //c_ctx.globalCompositeOperation = "copy";
  344. };
  345. that.fillRect = function(x, y, width, height, color) {
  346. setFillColor(color);
  347. c_ctx.fillRect(x - viewport.x, y - viewport.y, width, height);
  348. };
  349. that.copyImage = function(old_x, old_y, new_x, new_y, w, h) {
  350. var x1 = old_x - viewport.x, y1 = old_y - viewport.y,
  351. x2 = new_x - viewport.x, y2 = new_y - viewport.y;
  352. c_ctx.drawImage(conf.target, x1, y1, w, h, x2, y2, w, h);
  353. };
  354. // Start updating a tile
  355. that.startTile = function(x, y, width, height, color) {
  356. var data, bgr, red, green, blue, i;
  357. tile_x = x;
  358. tile_y = y;
  359. if ((width === 16) && (height === 16)) {
  360. tile = tile16x16;
  361. } else {
  362. tile = c_ctx.createImageData(width, height);
  363. }
  364. data = tile.data;
  365. if (conf.prefer_js) {
  366. if (conf.true_color) {
  367. bgr = color;
  368. } else {
  369. bgr = conf.colourMap[color[0]];
  370. }
  371. red = bgr[2];
  372. green = bgr[1];
  373. blue = bgr[0];
  374. for (i = 0; i < (width * height * 4); i+=4) {
  375. data[i ] = red;
  376. data[i + 1] = green;
  377. data[i + 2] = blue;
  378. data[i + 3] = 255;
  379. }
  380. } else {
  381. that.fillRect(x, y, width, height, color);
  382. }
  383. };
  384. // Update sub-rectangle of the current tile
  385. that.subTile = function(x, y, w, h, color) {
  386. var data, p, bgr, red, green, blue, width, j, i, xend, yend;
  387. if (conf.prefer_js) {
  388. data = tile.data;
  389. width = tile.width;
  390. if (conf.true_color) {
  391. bgr = color;
  392. } else {
  393. bgr = conf.colourMap[color[0]];
  394. }
  395. red = bgr[2];
  396. green = bgr[1];
  397. blue = bgr[0];
  398. xend = x + w;
  399. yend = y + h;
  400. for (j = y; j < yend; j += 1) {
  401. for (i = x; i < xend; i += 1) {
  402. p = (i + (j * width) ) * 4;
  403. data[p ] = red;
  404. data[p + 1] = green;
  405. data[p + 2] = blue;
  406. data[p + 3] = 255;
  407. }
  408. }
  409. } else {
  410. that.fillRect(tile_x + x, tile_y + y, w, h, color);
  411. }
  412. };
  413. // Draw the current tile to the screen
  414. that.finishTile = function() {
  415. if (conf.prefer_js) {
  416. c_ctx.putImageData(tile, tile_x - viewport.x, tile_y - viewport.y);
  417. }
  418. // else: No-op, if not prefer_js then already done by setSubTile
  419. };
  420. rgbImageData = function(x, y, width, height, arr, offset) {
  421. var img, i, j, data, v = viewport;
  422. /*
  423. if ((x - v.x >= v.w) || (y - v.y >= v.h) ||
  424. (x - v.x + width < 0) || (y - v.y + height < 0)) {
  425. // Skipping because outside of viewport
  426. return;
  427. }
  428. */
  429. img = c_ctx.createImageData(width, height);
  430. data = img.data;
  431. for (i=0, j=offset; i < (width * height * 4); i=i+4, j=j+3) {
  432. data[i ] = arr[j ];
  433. data[i + 1] = arr[j + 1];
  434. data[i + 2] = arr[j + 2];
  435. data[i + 3] = 255; // Set Alpha
  436. }
  437. c_ctx.putImageData(img, x - v.x, y - v.y);
  438. };
  439. bgrxImageData = function(x, y, width, height, arr, offset) {
  440. var img, i, j, data, v = viewport;
  441. /*
  442. if ((x - v.x >= v.w) || (y - v.y >= v.h) ||
  443. (x - v.x + width < 0) || (y - v.y + height < 0)) {
  444. // Skipping because outside of viewport
  445. return;
  446. }
  447. */
  448. img = c_ctx.createImageData(width, height);
  449. data = img.data;
  450. for (i=0, j=offset; i < (width * height * 4); i=i+4, j=j+4) {
  451. data[i ] = arr[j + 2];
  452. data[i + 1] = arr[j + 1];
  453. data[i + 2] = arr[j ];
  454. data[i + 3] = 255; // Set Alpha
  455. }
  456. c_ctx.putImageData(img, x - v.x, y - v.y);
  457. };
  458. cmapImageData = function(x, y, width, height, arr, offset) {
  459. var img, i, j, data, bgr, cmap;
  460. img = c_ctx.createImageData(width, height);
  461. data = img.data;
  462. cmap = conf.colourMap;
  463. for (i=0, j=offset; i < (width * height * 4); i+=4, j+=1) {
  464. bgr = cmap[arr[j]];
  465. data[i ] = bgr[2];
  466. data[i + 1] = bgr[1];
  467. data[i + 2] = bgr[0];
  468. data[i + 3] = 255; // Set Alpha
  469. }
  470. c_ctx.putImageData(img, x - viewport.x, y - viewport.y);
  471. };
  472. that.blitImage = function(x, y, width, height, arr, offset) {
  473. if (conf.true_color) {
  474. bgrxImageData(x, y, width, height, arr, offset);
  475. } else {
  476. cmapImageData(x, y, width, height, arr, offset);
  477. }
  478. };
  479. that.blitRgbImage = function(x, y, width, height, arr, offset) {
  480. if (conf.true_color) {
  481. rgbImageData(x, y, width, height, arr, offset);
  482. } else {
  483. // prolly wrong...
  484. cmapImageData(x, y, width, height, arr, offset);
  485. }
  486. };
  487. that.blitStringImage = function(str, x, y) {
  488. var img = new Image();
  489. img.onload = function () {
  490. c_ctx.drawImage(img, x - viewport.x, y - viewport.y);
  491. };
  492. img.src = str;
  493. };
  494. // Wrap ctx.drawImage but relative to viewport
  495. that.drawImage = function(img, x, y) {
  496. c_ctx.drawImage(img, x - viewport.x, y - viewport.y);
  497. };
  498. that.renderQ_push = function(action) {
  499. renderQ.push(action);
  500. if (renderQ.length === 1) {
  501. // If this can be rendered immediately it will be, otherwise
  502. // the scanner will start polling the queue (every
  503. // requestAnimationFrame interval)
  504. scan_renderQ();
  505. }
  506. };
  507. scan_renderQ = function() {
  508. var a, ready = true;
  509. while (ready && renderQ.length > 0) {
  510. a = renderQ[0];
  511. switch (a.type) {
  512. case 'copy':
  513. that.copyImage(a.old_x, a.old_y, a.x, a.y, a.width, a.height);
  514. break;
  515. case 'fill':
  516. that.fillRect(a.x, a.y, a.width, a.height, a.color);
  517. break;
  518. case 'blit':
  519. that.blitImage(a.x, a.y, a.width, a.height, a.data, 0);
  520. break;
  521. case 'blitRgb':
  522. that.blitRgbImage(a.x, a.y, a.width, a.height, a.data, 0);
  523. break;
  524. case 'img':
  525. if (a.img.complete) {
  526. that.drawImage(a.img, a.x, a.y);
  527. } else {
  528. // We need to wait for this image to 'load'
  529. // to keep things in-order
  530. ready = false;
  531. }
  532. break;
  533. }
  534. if (ready) {
  535. a = renderQ.shift();
  536. }
  537. }
  538. if (renderQ.length > 0) {
  539. requestAnimFrame(scan_renderQ);
  540. }
  541. };
  542. that.changeCursor = function(pixels, mask, hotx, hoty, w, h) {
  543. if (conf.cursor_uri === false) {
  544. Util.Warn("changeCursor called but no cursor data URI support");
  545. return;
  546. }
  547. if (conf.true_color) {
  548. changeCursor(conf.target, pixels, mask, hotx, hoty, w, h);
  549. } else {
  550. changeCursor(conf.target, pixels, mask, hotx, hoty, w, h, conf.colourMap);
  551. }
  552. };
  553. that.defaultCursor = function() {
  554. conf.target.style.cursor = "default";
  555. };
  556. return constructor(); // Return the public API interface
  557. } // End of Display()
  558. /* Set CSS cursor property using data URI encoded cursor file */
  559. function changeCursor(target, pixels, mask, hotx, hoty, w, h, cmap) {
  560. "use strict";
  561. var cur = [], rgb, IHDRsz, RGBsz, ANDsz, XORsz, url, idx, alpha, x, y;
  562. //Util.Debug(">> changeCursor, x: " + hotx + ", y: " + hoty + ", w: " + w + ", h: " + h);
  563. // Push multi-byte little-endian values
  564. cur.push16le = function (num) {
  565. this.push((num ) & 0xFF,
  566. (num >> 8) & 0xFF );
  567. };
  568. cur.push32le = function (num) {
  569. this.push((num ) & 0xFF,
  570. (num >> 8) & 0xFF,
  571. (num >> 16) & 0xFF,
  572. (num >> 24) & 0xFF );
  573. };
  574. IHDRsz = 40;
  575. RGBsz = w * h * 4;
  576. XORsz = Math.ceil( (w * h) / 8.0 );
  577. ANDsz = Math.ceil( (w * h) / 8.0 );
  578. // Main header
  579. cur.push16le(0); // 0: Reserved
  580. cur.push16le(2); // 2: .CUR type
  581. cur.push16le(1); // 4: Number of images, 1 for non-animated ico
  582. // Cursor #1 header (ICONDIRENTRY)
  583. cur.push(w); // 6: width
  584. cur.push(h); // 7: height
  585. cur.push(0); // 8: colors, 0 -> true-color
  586. cur.push(0); // 9: reserved
  587. cur.push16le(hotx); // 10: hotspot x coordinate
  588. cur.push16le(hoty); // 12: hotspot y coordinate
  589. cur.push32le(IHDRsz + RGBsz + XORsz + ANDsz);
  590. // 14: cursor data byte size
  591. cur.push32le(22); // 18: offset of cursor data in the file
  592. // Cursor #1 InfoHeader (ICONIMAGE/BITMAPINFO)
  593. cur.push32le(IHDRsz); // 22: Infoheader size
  594. cur.push32le(w); // 26: Cursor width
  595. cur.push32le(h*2); // 30: XOR+AND height
  596. cur.push16le(1); // 34: number of planes
  597. cur.push16le(32); // 36: bits per pixel
  598. cur.push32le(0); // 38: Type of compression
  599. cur.push32le(XORsz + ANDsz); // 43: Size of Image
  600. // Gimp leaves this as 0
  601. cur.push32le(0); // 46: reserved
  602. cur.push32le(0); // 50: reserved
  603. cur.push32le(0); // 54: reserved
  604. cur.push32le(0); // 58: reserved
  605. // 62: color data (RGBQUAD icColors[])
  606. for (y = h-1; y >= 0; y -= 1) {
  607. for (x = 0; x < w; x += 1) {
  608. idx = y * Math.ceil(w / 8) + Math.floor(x/8);
  609. alpha = (mask[idx] << (x % 8)) & 0x80 ? 255 : 0;
  610. if (cmap) {
  611. idx = (w * y) + x;
  612. rgb = cmap[pixels[idx]];
  613. cur.push(rgb[2]); // blue
  614. cur.push(rgb[1]); // green
  615. cur.push(rgb[0]); // red
  616. cur.push(alpha); // alpha
  617. } else {
  618. idx = ((w * y) + x) * 4;
  619. cur.push(pixels[idx + 2]); // blue
  620. cur.push(pixels[idx + 1]); // green
  621. cur.push(pixels[idx ]); // red
  622. cur.push(alpha); // alpha
  623. }
  624. }
  625. }
  626. // XOR/bitmask data (BYTE icXOR[])
  627. // (ignored, just needs to be right size)
  628. for (y = 0; y < h; y += 1) {
  629. for (x = 0; x < Math.ceil(w / 8); x += 1) {
  630. cur.push(0x00);
  631. }
  632. }
  633. // AND/bitmask data (BYTE icAND[])
  634. // (ignored, just needs to be right size)
  635. for (y = 0; y < h; y += 1) {
  636. for (x = 0; x < Math.ceil(w / 8); x += 1) {
  637. cur.push(0x00);
  638. }
  639. }
  640. url = "data:image/x-icon;base64," + Base64.encode(cur);
  641. target.style.cursor = "url(" + url + ") " + hotx + " " + hoty + ", default";
  642. //Util.Debug("<< changeCursor, cur.length: " + cur.length);
  643. }