canvas.js 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826
  1. /*
  2. * noVNC: HTML5 VNC client
  3. * Copyright (C) 2010 Joel Martin
  4. * Licensed under LGPL-3 (see LICENSE.txt)
  5. *
  6. * See README.md for usage and integration instructions.
  7. */
  8. "use strict";
  9. /*jslint browser: true, white: false, bitwise: false */
  10. /*global window, Util, Base64 */
  11. function Canvas(conf) {
  12. conf = conf || {}; // Configuration
  13. var that = {}, // Public API interface
  14. // Private Canvas namespace variables
  15. c_forceCanvas = false,
  16. c_width = 0,
  17. c_height = 0,
  18. c_prevStyle = "",
  19. c_keyPress = null,
  20. c_mouseButton = null,
  21. c_mouseMove = null,
  22. c_webkit_bug = false,
  23. c_flush_timer = null;
  24. // Configuration settings
  25. function cdef(v, type, defval, desc) {
  26. Util.conf_default(conf, that, v, type, defval, desc); }
  27. // Capability settings, default can be overridden
  28. cdef('prefer_js', 'raw', null, 'Prefer Javascript over canvas methods');
  29. cdef('cursor_uri', 'raw', null, 'Can we render cursor using data URI');
  30. cdef('target', 'dom', null, 'Canvas element for VNC viewport');
  31. cdef('focusContainer', 'dom', document, 'DOM element that traps keyboard input');
  32. cdef('true_color', 'bool', true, 'Request true color pixel data');
  33. cdef('focused', 'bool', true, 'Capture and send key strokes');
  34. cdef('colourMap', 'raw', [], 'Colour map array (not true color)');
  35. cdef('scale', 'float', 1, 'VNC viewport scale factor');
  36. cdef('render_mode', 'str', '', 'Canvas rendering mode (read-only)');
  37. // Override some specific getters/setters
  38. that.set_prefer_js = function(val) {
  39. if (val && c_forceCanvas) {
  40. Util.Warn("Preferring Javascript to Canvas ops is not supported");
  41. return false;
  42. }
  43. conf.prefer_js = val;
  44. return true;
  45. };
  46. that.get_colourMap = function(idx) {
  47. if (typeof idx === 'undefined') {
  48. return conf.colourMap;
  49. } else {
  50. return conf.colourMap[idx];
  51. }
  52. };
  53. that.set_colourMap = function(val, idx) {
  54. if (typeof idx === 'undefined') {
  55. conf.colourMap = val;
  56. } else {
  57. conf.colourMap[idx] = val;
  58. }
  59. };
  60. that.set_render_mode = function () { throw("render_mode is read-only"); };
  61. // Add some other getters/setters
  62. that.get_width = function() {
  63. return c_width;
  64. };
  65. that.get_height = function() {
  66. return c_height;
  67. };
  68. //
  69. // Private functions
  70. //
  71. // Create the public API interface
  72. function constructor() {
  73. Util.Debug(">> Canvas.init");
  74. var c, ctx, func, origfunc, imgTest, tval, i, curDat, curSave,
  75. has_imageData = false, UE = Util.Engine;
  76. if (! conf.target) { throw("target must be set"); }
  77. if (typeof conf.target === 'string') {
  78. throw("target must be a DOM element");
  79. }
  80. c = conf.target;
  81. if (! c.getContext) { throw("no getContext method"); }
  82. if (! conf.ctx) { conf.ctx = c.getContext('2d'); }
  83. ctx = conf.ctx;
  84. Util.Debug("User Agent: " + navigator.userAgent);
  85. if (UE.gecko) { Util.Debug("Browser: gecko " + UE.gecko); }
  86. if (UE.webkit) { Util.Debug("Browser: webkit " + UE.webkit); }
  87. if (UE.trident) { Util.Debug("Browser: trident " + UE.trident); }
  88. if (UE.presto) { Util.Debug("Browser: presto " + UE.presto); }
  89. that.clear();
  90. /*
  91. * Determine browser Canvas feature support
  92. * and select fastest rendering methods
  93. */
  94. tval = 0;
  95. try {
  96. imgTest = ctx.getImageData(0, 0, 1,1);
  97. imgTest.data[0] = 123;
  98. imgTest.data[3] = 255;
  99. ctx.putImageData(imgTest, 0, 0);
  100. tval = ctx.getImageData(0, 0, 1, 1).data[0];
  101. if (tval === 123) {
  102. has_imageData = true;
  103. }
  104. } catch (exc1) {}
  105. if (has_imageData) {
  106. Util.Info("Canvas supports imageData");
  107. c_forceCanvas = false;
  108. if (ctx.createImageData) {
  109. // If it's there, it's faster
  110. Util.Info("Using Canvas createImageData");
  111. conf.render_mode = "createImageData rendering";
  112. that.imageData = that.imageDataCreate;
  113. } else if (ctx.getImageData) {
  114. // I think this is mostly just Opera
  115. Util.Info("Using Canvas getImageData");
  116. conf.render_mode = "getImageData rendering";
  117. that.imageData = that.imageDataGet;
  118. }
  119. Util.Info("Prefering javascript operations");
  120. if (conf.prefer_js === null) {
  121. conf.prefer_js = true;
  122. }
  123. that.rgbxImage = that.rgbxImageData;
  124. that.cmapImage = that.cmapImageData;
  125. } else {
  126. Util.Warn("Canvas lacks imageData, using fillRect (slow)");
  127. conf.render_mode = "fillRect rendering (slow)";
  128. c_forceCanvas = true;
  129. conf.prefer_js = false;
  130. that.rgbxImage = that.rgbxImageFill;
  131. that.cmapImage = that.cmapImageFill;
  132. }
  133. if (UE.webkit && UE.webkit >= 534.7 && UE.webkit <= 534.9) {
  134. // Workaround WebKit canvas rendering bug #46319
  135. conf.render_mode += ", webkit bug workaround";
  136. Util.Debug("Working around WebKit bug #46319");
  137. c_webkit_bug = true;
  138. for (func in {"fillRect":1, "copyImage":1, "rgbxImage":1,
  139. "cmapImage":1, "blitStringImage":1}) {
  140. that[func] = (function() {
  141. var myfunc = that[func]; // Save original function
  142. //Util.Debug("Wrapping " + func);
  143. return function() {
  144. myfunc.apply(this, arguments);
  145. if (!c_flush_timer) {
  146. c_flush_timer = setTimeout(that.flush, 100);
  147. }
  148. };
  149. })();
  150. }
  151. }
  152. /*
  153. * Determine browser support for setting the cursor via data URI
  154. * scheme
  155. */
  156. curDat = [];
  157. for (i=0; i < 8 * 8 * 4; i += 1) {
  158. curDat.push(255);
  159. }
  160. try {
  161. curSave = c.style.cursor;
  162. changeCursor(conf.target, curDat, curDat, 2, 2, 8, 8);
  163. if (c.style.cursor) {
  164. if (conf.cursor_uri === null) {
  165. conf.cursor_uri = true;
  166. }
  167. Util.Info("Data URI scheme cursor supported");
  168. } else {
  169. if (conf.cursor_uri === null) {
  170. conf.cursor_uri = false;
  171. }
  172. Util.Warn("Data URI scheme cursor not supported");
  173. }
  174. c.style.cursor = curSave;
  175. } catch (exc2) {
  176. Util.Error("Data URI scheme cursor test exception: " + exc2);
  177. conf.cursor_uri = false;
  178. }
  179. conf.focused = true;
  180. Util.Debug("<< Canvas.init");
  181. return that ;
  182. }
  183. function onMouseButton(e, down) {
  184. var evt, pos, bmask;
  185. if (! conf.focused) {
  186. return true;
  187. }
  188. evt = (e ? e : window.event);
  189. pos = Util.getEventPosition(e, conf.target, conf.scale);
  190. bmask = 1 << evt.button;
  191. //Util.Debug('mouse ' + pos.x + "," + pos.y + " down: " + down + " bmask: " + bmask);
  192. if (c_mouseButton) {
  193. c_mouseButton(pos.x, pos.y, down, bmask);
  194. }
  195. Util.stopEvent(e);
  196. return false;
  197. }
  198. function onMouseDown(e) {
  199. onMouseButton(e, 1);
  200. }
  201. function onMouseUp(e) {
  202. onMouseButton(e, 0);
  203. }
  204. function onMouseWheel(e) {
  205. var evt, pos, bmask, wheelData;
  206. evt = (e ? e : window.event);
  207. pos = Util.getEventPosition(e, conf.target, conf.scale);
  208. wheelData = evt.detail ? evt.detail * -1 : evt.wheelDelta / 40;
  209. if (wheelData > 0) {
  210. bmask = 1 << 3;
  211. } else {
  212. bmask = 1 << 4;
  213. }
  214. //Util.Debug('mouse scroll by ' + wheelData + ':' + pos.x + "," + pos.y);
  215. if (c_mouseButton) {
  216. c_mouseButton(pos.x, pos.y, 1, bmask);
  217. c_mouseButton(pos.x, pos.y, 0, bmask);
  218. }
  219. Util.stopEvent(e);
  220. return false;
  221. }
  222. function onMouseMove(e) {
  223. var evt, pos;
  224. evt = (e ? e : window.event);
  225. pos = Util.getEventPosition(e, conf.target, conf.scale);
  226. //Util.Debug('mouse ' + evt.which + '/' + evt.button + ' up:' + pos.x + "," + pos.y);
  227. if (c_mouseMove) {
  228. c_mouseMove(pos.x, pos.y);
  229. }
  230. }
  231. function onKeyDown(e) {
  232. //Util.Debug("keydown: " + getKeysym(e));
  233. if (! conf.focused) {
  234. return true;
  235. }
  236. if (c_keyPress) {
  237. c_keyPress(getKeysym(e), 1, e.ctrlKey, e.shiftKey, e.altKey);
  238. }
  239. Util.stopEvent(e);
  240. return false;
  241. }
  242. function onKeyUp(e) {
  243. //Util.Debug("keyup: " + getKeysym(e));
  244. if (! conf.focused) {
  245. return true;
  246. }
  247. if (c_keyPress) {
  248. c_keyPress(getKeysym(e), 0, e.ctrlKey, e.shiftKey, e.altKey);
  249. }
  250. Util.stopEvent(e);
  251. return false;
  252. }
  253. function onMouseDisable(e) {
  254. var evt, pos;
  255. if (! conf.focused) {
  256. return true;
  257. }
  258. evt = (e ? e : window.event);
  259. pos = Util.getEventPosition(e, conf.target, conf.scale);
  260. /* Stop propagation if inside canvas area */
  261. if ((pos.x >= 0) && (pos.y >= 0) &&
  262. (pos.x < c_width) && (pos.y < c_height)) {
  263. //Util.Debug("mouse event disabled");
  264. Util.stopEvent(e);
  265. return false;
  266. }
  267. //Util.Debug("mouse event not disabled");
  268. return true;
  269. }
  270. //
  271. // Public API interface functions
  272. //
  273. that.getContext = function () {
  274. return conf.ctx;
  275. };
  276. that.start = function(keyPressFunc, mouseButtonFunc, mouseMoveFunc) {
  277. var c;
  278. Util.Debug(">> Canvas.start");
  279. c = conf.target;
  280. c_keyPress = keyPressFunc || null;
  281. c_mouseButton = mouseButtonFunc || null;
  282. c_mouseMove = mouseMoveFunc || null;
  283. Util.addEvent(conf.focusContainer, 'keydown', onKeyDown);
  284. Util.addEvent(conf.focusContainer, 'keyup', onKeyUp);
  285. Util.addEvent(c, 'mousedown', onMouseDown);
  286. Util.addEvent(c, 'mouseup', onMouseUp);
  287. Util.addEvent(c, 'mousemove', onMouseMove);
  288. Util.addEvent(c, (Util.Engine.gecko) ? 'DOMMouseScroll' : 'mousewheel',
  289. onMouseWheel);
  290. /* Work around right and middle click browser behaviors */
  291. Util.addEvent(conf.focusContainer, 'click', onMouseDisable);
  292. Util.addEvent(conf.focusContainer.body, 'contextmenu', onMouseDisable);
  293. Util.Debug("<< Canvas.start");
  294. };
  295. that.rescale = function(factor) {
  296. var c, tp, x, y,
  297. properties = ['transform', 'WebkitTransform', 'MozTransform', null];
  298. c = conf.target;
  299. tp = properties.shift();
  300. while (tp) {
  301. if (typeof c.style[tp] !== 'undefined') {
  302. break;
  303. }
  304. tp = properties.shift();
  305. }
  306. if (tp === null) {
  307. Util.Debug("No scaling support");
  308. return;
  309. }
  310. if (conf.scale === factor) {
  311. //Util.Debug("Canvas already scaled to '" + factor + "'");
  312. return;
  313. }
  314. conf.scale = factor;
  315. x = c.width - c.width * factor;
  316. y = c.height - c.height * factor;
  317. c.style[tp] = "scale(" + conf.scale + ") translate(-" + x + "px, -" + y + "px)";
  318. };
  319. that.resize = function(width, height, true_color) {
  320. var c = conf.target;
  321. if (typeof true_color !== "undefined") {
  322. conf.true_color = true_color;
  323. }
  324. c_prevStyle = "";
  325. c.width = width;
  326. c.height = height;
  327. c_width = c.offsetWidth;
  328. c_height = c.offsetHeight;
  329. that.rescale(conf.scale);
  330. };
  331. that.clear = function() {
  332. that.resize(640, 20);
  333. conf.ctx.clearRect(0, 0, c_width, c_height);
  334. // No benefit over default ("source-over") in Chrome and firefox
  335. //conf.ctx.globalCompositeOperation = "copy";
  336. };
  337. that.stop = function() {
  338. var c = conf.target;
  339. Util.removeEvent(conf.focusContainer, 'keydown', onKeyDown);
  340. Util.removeEvent(conf.focusContainer, 'keyup', onKeyUp);
  341. Util.removeEvent(c, 'mousedown', onMouseDown);
  342. Util.removeEvent(c, 'mouseup', onMouseUp);
  343. Util.removeEvent(c, 'mousemove', onMouseMove);
  344. Util.removeEvent(c, (Util.Engine.gecko) ? 'DOMMouseScroll' : 'mousewheel',
  345. onMouseWheel);
  346. /* Work around right and middle click browser behaviors */
  347. Util.removeEvent(conf.focusContainer, 'click', onMouseDisable);
  348. Util.removeEvent(conf.focusContainer.body, 'contextmenu', onMouseDisable);
  349. // Turn off cursor rendering
  350. if (conf.cursor_uri) {
  351. c.style.cursor = "default";
  352. }
  353. };
  354. that.flush = function() {
  355. var old_val;
  356. //Util.Debug(">> flush");
  357. // Force canvas redraw (for webkit bug #46319 workaround)
  358. old_val = conf.target.style.marginRight;
  359. conf.target.style.marginRight = "1px";
  360. c_flush_timer = null;
  361. setTimeout(function () {
  362. conf.target.style.marginRight = old_val;
  363. }, 1);
  364. };
  365. that.setFillColor = function(color) {
  366. var rgb, newStyle;
  367. if (conf.true_color) {
  368. rgb = color;
  369. } else {
  370. rgb = conf.colourMap[color[0]];
  371. }
  372. newStyle = "rgb(" + rgb[0] + "," + rgb[1] + "," + rgb[2] + ")";
  373. if (newStyle !== c_prevStyle) {
  374. conf.ctx.fillStyle = newStyle;
  375. c_prevStyle = newStyle;
  376. }
  377. };
  378. that.fillRect = function(x, y, width, height, color) {
  379. that.setFillColor(color);
  380. conf.ctx.fillRect(x, y, width, height);
  381. };
  382. that.copyImage = function(old_x, old_y, new_x, new_y, width, height) {
  383. conf.ctx.drawImage(conf.target, old_x, old_y, width, height,
  384. new_x, new_y, width, height);
  385. };
  386. /*
  387. * Tile rendering functions optimized for rendering engines.
  388. *
  389. * - In Chrome/webkit, Javascript image data array manipulations are
  390. * faster than direct Canvas fillStyle, fillRect rendering. In
  391. * gecko, Javascript array handling is much slower.
  392. */
  393. that.getTile = function(x, y, width, height, color) {
  394. var img, data = [], p, rgb, red, green, blue, j, i;
  395. img = {'x': x, 'y': y, 'width': width, 'height': height,
  396. 'data': data};
  397. if (conf.prefer_js) {
  398. if (conf.true_color) {
  399. rgb = color;
  400. } else {
  401. rgb = conf.colourMap[color[0]];
  402. }
  403. red = rgb[0];
  404. green = rgb[1];
  405. blue = rgb[2];
  406. for (i = 0; i < (width * height * 4); i+=4) {
  407. data[i ] = red;
  408. data[i + 1] = green;
  409. data[i + 2] = blue;
  410. }
  411. } else {
  412. that.fillRect(x, y, width, height, color);
  413. }
  414. return img;
  415. };
  416. that.setSubTile = function(img, x, y, w, h, color) {
  417. var data, p, rgb, red, green, blue, width, j, i, xend, yend;
  418. if (conf.prefer_js) {
  419. data = img.data;
  420. width = img.width;
  421. if (conf.true_color) {
  422. rgb = color;
  423. } else {
  424. rgb = conf.colourMap[color[0]];
  425. }
  426. red = rgb[0];
  427. green = rgb[1];
  428. blue = rgb[2];
  429. xend = x + w;
  430. yend = y + h;
  431. for (j = y; j < yend; j += 1) {
  432. for (i = x; i < xend; i += 1) {
  433. p = (i + (j * width) ) * 4;
  434. data[p ] = red;
  435. data[p + 1] = green;
  436. data[p + 2] = blue;
  437. }
  438. }
  439. } else {
  440. that.fillRect(img.x + x, img.y + y, w, h, color);
  441. }
  442. };
  443. that.putTile = function(img) {
  444. if (conf.prefer_js) {
  445. that.rgbxImage(img.x, img.y, img.width, img.height, img.data, 0);
  446. } else {
  447. // No-op, under gecko already done by setSubTile
  448. }
  449. };
  450. that.imageDataGet = function(width, height) {
  451. return conf.ctx.getImageData(0, 0, width, height);
  452. };
  453. that.imageDataCreate = function(width, height) {
  454. return conf.ctx.createImageData(width, height);
  455. };
  456. that.rgbxImageData = function(x, y, width, height, arr, offset) {
  457. var img, i, j, data;
  458. img = that.imageData(width, height);
  459. data = img.data;
  460. for (i=0, j=offset; i < (width * height * 4); i=i+4, j=j+4) {
  461. data[i + 0] = arr[j + 0];
  462. data[i + 1] = arr[j + 1];
  463. data[i + 2] = arr[j + 2];
  464. data[i + 3] = 255; // Set Alpha
  465. }
  466. conf.ctx.putImageData(img, x, y);
  467. };
  468. // really slow fallback if we don't have imageData
  469. that.rgbxImageFill = function(x, y, width, height, arr, offset) {
  470. var i, j, sx = 0, sy = 0;
  471. for (i=0, j=offset; i < (width * height); i+=1, j+=4) {
  472. that.fillRect(x+sx, y+sy, 1, 1, [arr[j+0], arr[j+1], arr[j+2]]);
  473. sx += 1;
  474. if ((sx % width) === 0) {
  475. sx = 0;
  476. sy += 1;
  477. }
  478. }
  479. };
  480. that.cmapImageData = function(x, y, width, height, arr, offset) {
  481. var img, i, j, data, rgb, cmap;
  482. img = that.imageData(width, height);
  483. data = img.data;
  484. cmap = conf.colourMap;
  485. for (i=0, j=offset; i < (width * height * 4); i+=4, j+=1) {
  486. rgb = cmap[arr[j]];
  487. data[i + 0] = rgb[0];
  488. data[i + 1] = rgb[1];
  489. data[i + 2] = rgb[2];
  490. data[i + 3] = 255; // Set Alpha
  491. }
  492. conf.ctx.putImageData(img, x, y);
  493. };
  494. that.cmapImageFill = function(x, y, width, height, arr, offset) {
  495. var i, j, sx = 0, sy = 0, cmap;
  496. cmap = conf.colourMap;
  497. for (i=0, j=offset; i < (width * height); i+=1, j+=1) {
  498. that.fillRect(x+sx, y+sy, 1, 1, [arr[j]]);
  499. sx += 1;
  500. if ((sx % width) === 0) {
  501. sx = 0;
  502. sy += 1;
  503. }
  504. }
  505. };
  506. that.blitImage = function(x, y, width, height, arr, offset) {
  507. if (conf.true_color) {
  508. that.rgbxImage(x, y, width, height, arr, offset);
  509. } else {
  510. that.cmapImage(x, y, width, height, arr, offset);
  511. }
  512. };
  513. that.blitStringImage = function(str, x, y) {
  514. var img = new Image();
  515. img.onload = function () { conf.ctx.drawImage(img, x, y); };
  516. img.src = str;
  517. };
  518. that.changeCursor = function(pixels, mask, hotx, hoty, w, h) {
  519. if (conf.cursor_uri === false) {
  520. Util.Warn("changeCursor called but no cursor data URI support");
  521. return;
  522. }
  523. if (conf.true_color) {
  524. changeCursor(conf.target, pixels, mask, hotx, hoty, w, h);
  525. } else {
  526. changeCursor(conf.target, pixels, mask, hotx, hoty, w, h, conf.colourMap);
  527. }
  528. }
  529. return constructor(); // Return the public API interface
  530. } // End of Canvas()
  531. /* Set CSS cursor property using data URI encoded cursor file */
  532. function changeCursor(target, pixels, mask, hotx, hoty, w, h, cmap) {
  533. var cur = [], rgb, IHDRsz, RGBsz, ANDsz, XORsz, url, idx, alpha, x, y;
  534. //Util.Debug(">> changeCursor, x: " + hotx + ", y: " + hoty + ", w: " + w + ", h: " + h);
  535. // Push multi-byte little-endian values
  536. cur.push16le = function (num) {
  537. this.push((num ) & 0xFF,
  538. (num >> 8) & 0xFF );
  539. };
  540. cur.push32le = function (num) {
  541. this.push((num ) & 0xFF,
  542. (num >> 8) & 0xFF,
  543. (num >> 16) & 0xFF,
  544. (num >> 24) & 0xFF );
  545. };
  546. IHDRsz = 40;
  547. RGBsz = w * h * 4;
  548. XORsz = Math.ceil( (w * h) / 8.0 );
  549. ANDsz = Math.ceil( (w * h) / 8.0 );
  550. // Main header
  551. cur.push16le(0); // 0: Reserved
  552. cur.push16le(2); // 2: .CUR type
  553. cur.push16le(1); // 4: Number of images, 1 for non-animated ico
  554. // Cursor #1 header (ICONDIRENTRY)
  555. cur.push(w); // 6: width
  556. cur.push(h); // 7: height
  557. cur.push(0); // 8: colors, 0 -> true-color
  558. cur.push(0); // 9: reserved
  559. cur.push16le(hotx); // 10: hotspot x coordinate
  560. cur.push16le(hoty); // 12: hotspot y coordinate
  561. cur.push32le(IHDRsz + RGBsz + XORsz + ANDsz);
  562. // 14: cursor data byte size
  563. cur.push32le(22); // 18: offset of cursor data in the file
  564. // Cursor #1 InfoHeader (ICONIMAGE/BITMAPINFO)
  565. cur.push32le(IHDRsz); // 22: Infoheader size
  566. cur.push32le(w); // 26: Cursor width
  567. cur.push32le(h*2); // 30: XOR+AND height
  568. cur.push16le(1); // 34: number of planes
  569. cur.push16le(32); // 36: bits per pixel
  570. cur.push32le(0); // 38: Type of compression
  571. cur.push32le(XORsz + ANDsz); // 43: Size of Image
  572. // Gimp leaves this as 0
  573. cur.push32le(0); // 46: reserved
  574. cur.push32le(0); // 50: reserved
  575. cur.push32le(0); // 54: reserved
  576. cur.push32le(0); // 58: reserved
  577. // 62: color data (RGBQUAD icColors[])
  578. for (y = h-1; y >= 0; y -= 1) {
  579. for (x = 0; x < w; x += 1) {
  580. idx = y * Math.ceil(w / 8) + Math.floor(x/8);
  581. alpha = (mask[idx] << (x % 8)) & 0x80 ? 255 : 0;
  582. if (cmap) {
  583. idx = (w * y) + x;
  584. rgb = cmap[pixels[idx]];
  585. cur.push(rgb[2]); // blue
  586. cur.push(rgb[1]); // green
  587. cur.push(rgb[0]); // red
  588. cur.push(alpha); // alpha
  589. } else {
  590. idx = ((w * y) + x) * 4;
  591. cur.push(pixels[idx + 2]); // blue
  592. cur.push(pixels[idx + 1]); // green
  593. cur.push(pixels[idx + 0]); // red
  594. cur.push(alpha); // alpha
  595. }
  596. }
  597. }
  598. // XOR/bitmask data (BYTE icXOR[])
  599. // (ignored, just needs to be right size)
  600. for (y = 0; y < h; y += 1) {
  601. for (x = 0; x < Math.ceil(w / 8); x += 1) {
  602. cur.push(0x00);
  603. }
  604. }
  605. // AND/bitmask data (BYTE icAND[])
  606. // (ignored, just needs to be right size)
  607. for (y = 0; y < h; y += 1) {
  608. for (x = 0; x < Math.ceil(w / 8); x += 1) {
  609. cur.push(0x00);
  610. }
  611. }
  612. url = "data:image/x-icon;base64," + Base64.encode(cur);
  613. Util.Info(url);
  614. target.style.cursor = "url(" + url + ") " + hotx + " " + hoty + ", default";
  615. //conf.target.style.cursor = "url(" + url + "), default";
  616. //Util.Debug("<< changeCursor, cur.length: " + cur.length);
  617. };
  618. /* Translate DOM key down/up event to keysym value */
  619. function getKeysym(e) {
  620. var evt, keysym;
  621. evt = (e ? e : window.event);
  622. /* Remap modifier and special keys */
  623. switch ( evt.keyCode ) {
  624. case 8 : keysym = 0xFF08; break; // BACKSPACE
  625. case 9 : keysym = 0xFF09; break; // TAB
  626. case 13 : keysym = 0xFF0D; break; // ENTER
  627. case 27 : keysym = 0xFF1B; break; // ESCAPE
  628. case 45 : keysym = 0xFF63; break; // INSERT
  629. case 46 : keysym = 0xFFFF; break; // DELETE
  630. case 36 : keysym = 0xFF50; break; // HOME
  631. case 35 : keysym = 0xFF57; break; // END
  632. case 33 : keysym = 0xFF55; break; // PAGE_UP
  633. case 34 : keysym = 0xFF56; break; // PAGE_DOWN
  634. case 37 : keysym = 0xFF51; break; // LEFT
  635. case 38 : keysym = 0xFF52; break; // UP
  636. case 39 : keysym = 0xFF53; break; // RIGHT
  637. case 40 : keysym = 0xFF54; break; // DOWN
  638. case 112 : keysym = 0xFFBE; break; // F1
  639. case 113 : keysym = 0xFFBF; break; // F2
  640. case 114 : keysym = 0xFFC0; break; // F3
  641. case 115 : keysym = 0xFFC1; break; // F4
  642. case 116 : keysym = 0xFFC2; break; // F5
  643. case 117 : keysym = 0xFFC3; break; // F6
  644. case 118 : keysym = 0xFFC4; break; // F7
  645. case 119 : keysym = 0xFFC5; break; // F8
  646. case 120 : keysym = 0xFFC6; break; // F9
  647. case 121 : keysym = 0xFFC7; break; // F10
  648. case 122 : keysym = 0xFFC8; break; // F11
  649. case 123 : keysym = 0xFFC9; break; // F12
  650. case 16 : keysym = 0xFFE1; break; // SHIFT
  651. case 17 : keysym = 0xFFE3; break; // CONTROL
  652. //case 18 : keysym = 0xFFE7; break; // Left Meta (Mac Option)
  653. case 18 : keysym = 0xFFE9; break; // Left ALT (Mac Command)
  654. default : keysym = evt.keyCode; break;
  655. }
  656. /* Remap symbols */
  657. switch (keysym) {
  658. case 186 : keysym = 59; break; // ; (IE)
  659. case 187 : keysym = 61; break; // = (IE)
  660. case 188 : keysym = 44; break; // , (Mozilla, IE)
  661. case 109 : // - (Mozilla)
  662. if (Util.Engine.gecko) {
  663. keysym = 45; }
  664. break;
  665. case 189 : keysym = 45; break; // - (IE)
  666. case 190 : keysym = 46; break; // . (Mozilla, IE)
  667. case 191 : keysym = 47; break; // / (Mozilla, IE)
  668. case 192 : keysym = 96; break; // ` (Mozilla, IE)
  669. case 219 : keysym = 91; break; // [ (Mozilla, IE)
  670. case 220 : keysym = 92; break; // \ (Mozilla, IE)
  671. case 221 : keysym = 93; break; // ] (Mozilla, IE)
  672. case 222 : keysym = 39; break; // ' (Mozilla, IE)
  673. }
  674. /* Remap shifted and unshifted keys */
  675. if (!!evt.shiftKey) {
  676. switch (keysym) {
  677. case 48 : keysym = 41 ; break; // ) (shifted 0)
  678. case 49 : keysym = 33 ; break; // ! (shifted 1)
  679. case 50 : keysym = 64 ; break; // @ (shifted 2)
  680. case 51 : keysym = 35 ; break; // # (shifted 3)
  681. case 52 : keysym = 36 ; break; // $ (shifted 4)
  682. case 53 : keysym = 37 ; break; // % (shifted 5)
  683. case 54 : keysym = 94 ; break; // ^ (shifted 6)
  684. case 55 : keysym = 38 ; break; // & (shifted 7)
  685. case 56 : keysym = 42 ; break; // * (shifted 8)
  686. case 57 : keysym = 40 ; break; // ( (shifted 9)
  687. case 59 : keysym = 58 ; break; // : (shifted `)
  688. case 61 : keysym = 43 ; break; // + (shifted ;)
  689. case 44 : keysym = 60 ; break; // < (shifted ,)
  690. case 45 : keysym = 95 ; break; // _ (shifted -)
  691. case 46 : keysym = 62 ; break; // > (shifted .)
  692. case 47 : keysym = 63 ; break; // ? (shifted /)
  693. case 96 : keysym = 126; break; // ~ (shifted `)
  694. case 91 : keysym = 123; break; // { (shifted [)
  695. case 92 : keysym = 124; break; // | (shifted \)
  696. case 93 : keysym = 125; break; // } (shifted ])
  697. case 39 : keysym = 34 ; break; // " (shifted ')
  698. }
  699. } else if ((keysym >= 65) && (keysym <=90)) {
  700. /* Remap unshifted A-Z */
  701. keysym += 32;
  702. } else if (evt.keyLocation === 3) {
  703. // numpad keys
  704. switch (keysym) {
  705. case 96 : keysym = 48; break; // 0
  706. case 97 : keysym = 49; break; // 1
  707. case 98 : keysym = 50; break; // 2
  708. case 99 : keysym = 51; break; // 3
  709. case 100: keysym = 52; break; // 4
  710. case 101: keysym = 53; break; // 5
  711. case 102: keysym = 54; break; // 6
  712. case 103: keysym = 55; break; // 7
  713. case 104: keysym = 56; break; // 8
  714. case 105: keysym = 57; break; // 9
  715. case 109: keysym = 45; break; // -
  716. case 110: keysym = 46; break; // .
  717. case 111: keysym = 47; break; // /
  718. }
  719. }
  720. return keysym;
  721. }