Mercurial > hg > Members > nobuyasu > tightVNCProxy
annotate src/myVncProxy/ProxyVncCanvas.java @ 130:cb00150c223e
implement sendEchoPort
author | e085711 |
---|---|
date | Tue, 30 Aug 2011 05:39:12 +0900 |
parents | e3dddee7b934 |
children | 2a90459a05f0 |
rev | line source |
---|---|
24 | 1 package myVncProxy; |
13 | 2 import java.awt.*; |
3 import java.awt.event.*; | |
4 import java.awt.image.*; | |
5 import java.io.*; | |
6 import java.util.zip.*; | |
7 | |
25 | 8 import javax.imageio.ImageIO; |
9 | |
13 | 10 // |
11 //VncCanvas is a subclass of Canvas which draws a VNC desktop on it. | |
12 // | |
13 | |
14 class ProxyVncCanvas extends Canvas implements KeyListener, MouseListener, | |
15 | 15 MouseMotionListener { |
13 | 16 |
15 | 17 VncProxyService viewer; |
18 MyRfbProto rfb; | |
19 ColorModel cm8, cm24; | |
20 Color[] colors; | |
21 int bytesPixel; | |
13 | 22 |
15 | 23 int maxWidth = 0, maxHeight = 0; |
24 int scalingFactor; | |
25 int scaledWidth, scaledHeight; | |
13 | 26 |
26 | 27 // Image memImage; |
28 BufferedImage memImage; | |
15 | 29 Graphics memGraphics; |
13 | 30 |
26 | 31 Image rawPixelsImage; |
32 // BufferedImage rawPixelsImage; | |
33 BufferedImage bimg; | |
34 | |
15 | 35 MemoryImageSource pixelsSource; |
36 byte[] pixels8; | |
37 int[] pixels24; | |
13 | 38 |
15 | 39 // Update statistics. |
40 long statStartTime; // time on first framebufferUpdateRequest | |
41 int statNumUpdates; // counter for FramebufferUpdate messages | |
42 int statNumTotalRects; // rectangles in FramebufferUpdate messages | |
43 int statNumPixelRects; // the same, but excluding pseudo-rectangles | |
44 int statNumRectsTight; // Tight-encoded rectangles (including JPEG) | |
45 int statNumRectsTightJPEG; // JPEG-compressed Tight-encoded rectangles | |
46 int statNumRectsZRLE; // ZRLE-encoded rectangles | |
47 int statNumRectsHextile; // Hextile-encoded rectangles | |
48 int statNumRectsRaw; // Raw-encoded rectangles | |
49 int statNumRectsCopy; // CopyRect rectangles | |
50 int statNumBytesEncoded; // number of bytes in updates, as received | |
51 int statNumBytesDecoded; // number of bytes, as if Raw encoding was used | |
52 | |
53 // ZRLE encoder's data. | |
54 byte[] zrleBuf; | |
55 int zrleBufLen = 0; | |
56 byte[] zrleTilePixels8; | |
57 int[] zrleTilePixels24; | |
58 ZlibInStream zrleInStream; | |
59 boolean zrleRecWarningShown = false; | |
13 | 60 |
15 | 61 // Zlib encoder's data. |
62 byte[] zlibBuf; | |
63 int zlibBufLen = 0; | |
64 Inflater zlibInflater; | |
13 | 65 |
15 | 66 // Tight encoder's data. |
67 final static int tightZlibBufferSize = 512; | |
68 Inflater[] tightInflaters; | |
13 | 69 |
15 | 70 // Since JPEG images are loaded asynchronously, we have to remember |
71 // their position in the framebuffer. Also, this jpegRect object is | |
72 // used for synchronization between the rfbThread and a JVM's thread | |
73 // which decodes and loads JPEG images. | |
74 Rectangle jpegRect; | |
13 | 75 |
15 | 76 // True if we process keyboard and mouse events. |
77 boolean inputEnabled; | |
96
f0790bcf000d
fix concurrent modification
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
80
diff
changeset
|
78 private int b = 0; |
27 | 79 |
13 | 80 |
15 | 81 |
82 // | |
83 // The constructors. | |
84 // | |
13 | 85 |
15 | 86 public ProxyVncCanvas(VncProxyService v, int maxWidth_, int maxHeight_) |
87 throws IOException { | |
13 | 88 |
15 | 89 viewer = v; |
90 maxWidth = maxWidth_; | |
91 maxHeight = maxHeight_; | |
13 | 92 |
15 | 93 rfb = viewer.rfb; |
27 | 94 |
15 | 95 tightInflaters = new Inflater[4]; |
13 | 96 |
15 | 97 cm8 = new DirectColorModel(8, 7, (7 << 3), (3 << 6)); |
98 cm24 = new DirectColorModel(24, 0xFF0000, 0x00FF00, 0x0000FF); | |
13 | 99 |
15 | 100 colors = new Color[256]; |
101 for (int i = 0; i < 256; i++) | |
102 colors[i] = new Color(cm8.getRGB(i)); | |
13 | 103 |
130 | 104 setPixelFormat(); |
13 | 105 |
15 | 106 inputEnabled = false; |
107 // Keyboard listener is enabled even in view-only mode, to catch | |
108 // 'r' or 'R' key presses used to request screen update. | |
109 addKeyListener(this); | |
110 } | |
13 | 111 |
15 | 112 public ProxyVncCanvas(VncProxyService v) throws IOException { |
113 this(v, 0, 0); | |
114 } | |
115 | |
116 // | |
117 // Callback methods to determine geometry of our Component. | |
118 // | |
13 | 119 |
15 | 120 public Dimension getPreferredSize() { |
121 return new Dimension(scaledWidth, scaledHeight); | |
122 } | |
13 | 123 |
15 | 124 public Dimension getMinimumSize() { |
125 return new Dimension(scaledWidth, scaledHeight); | |
126 } | |
13 | 127 |
15 | 128 public Dimension getMaximumSize() { |
129 return new Dimension(scaledWidth, scaledHeight); | |
130 } | |
13 | 131 |
15 | 132 // |
133 // All painting is performed here. | |
134 // | |
13 | 135 |
15 | 136 public void update(Graphics g) { |
137 paint(g); | |
138 } | |
13 | 139 |
15 | 140 public void paint(Graphics g) { |
141 synchronized (memImage) { | |
142 if (rfb.framebufferWidth == scaledWidth) { | |
143 g.drawImage(memImage, 0, 0, null); | |
144 } else { | |
145 paintScaledFrameBuffer(g); | |
146 } | |
147 } | |
148 if (showSoftCursor) { | |
149 int x0 = cursorX - hotX, y0 = cursorY - hotY; | |
150 Rectangle r = new Rectangle(x0, y0, cursorWidth, cursorHeight); | |
151 if (r.intersects(g.getClipBounds())) { | |
152 g.drawImage(softCursor, x0, y0, null); | |
153 } | |
13 | 154 } |
155 } | |
15 | 156 |
157 public void paintScaledFrameBuffer(Graphics g) { | |
158 g.drawImage(memImage, 0, 0, scaledWidth, scaledHeight, null); | |
13 | 159 } |
160 | |
15 | 161 // |
162 // Override the ImageObserver interface method to handle drawing of | |
163 // JPEG-encoded data. | |
164 // | |
13 | 165 |
15 | 166 public boolean imageUpdate(Image img, int infoflags, int x, int y, |
167 int width, int height) { | |
168 if ((infoflags & (ALLBITS | ABORT)) == 0) { | |
169 return true; // We need more image data. | |
170 } else { | |
171 // If the whole image is available, draw it now. | |
172 if ((infoflags & ALLBITS) != 0) { | |
173 if (jpegRect != null) { | |
174 synchronized (jpegRect) { | |
175 memGraphics | |
176 .drawImage(img, jpegRect.x, jpegRect.y, null); | |
177 scheduleRepaint(jpegRect.x, jpegRect.y, jpegRect.width, | |
178 jpegRect.height); | |
179 jpegRect.notify(); | |
180 } | |
13 | 181 } |
182 } | |
15 | 183 return false; // All image data was processed. |
13 | 184 } |
185 } | |
186 | |
15 | 187 // |
188 // Start/stop receiving mouse events. Keyboard events are received | |
189 // even in view-only mode, because we want to map the 'r' key to the | |
190 // screen refreshing function. | |
191 // | |
13 | 192 |
15 | 193 public synchronized void enableInput(boolean enable) { |
194 if (enable && !inputEnabled) { | |
195 inputEnabled = true; | |
196 addMouseListener(this); | |
197 addMouseMotionListener(this); | |
198 if (viewer.showControls) { | |
199 viewer.buttonPanel.enableRemoteAccessControls(true); | |
200 } | |
201 createSoftCursor(); // scaled cursor | |
202 } else if (!enable && inputEnabled) { | |
203 inputEnabled = false; | |
204 removeMouseListener(this); | |
205 removeMouseMotionListener(this); | |
206 if (viewer.showControls) { | |
207 viewer.buttonPanel.enableRemoteAccessControls(false); | |
208 } | |
209 createSoftCursor(); // non-scaled cursor | |
210 } | |
211 } | |
13 | 212 |
15 | 213 public void setPixelFormat() throws IOException { |
130 | 214 |
15 | 215 if (viewer.options.eightBitColors) { |
216 rfb.writeSetPixelFormat(8, 8, false, true, 7, 7, 3, 0, 3, 6); | |
217 bytesPixel = 1; | |
218 } else { | |
219 rfb.writeSetPixelFormat(32, 24, false, true, 255, 255, 255, 16, 8, | |
220 0); | |
221 bytesPixel = 4; | |
222 } | |
130 | 223 |
15 | 224 updateFramebufferSize(); |
225 } | |
226 | |
227 void updateFramebufferSize() { | |
228 | |
229 // Useful shortcuts. | |
230 int fbWidth = rfb.framebufferWidth; | |
231 int fbHeight = rfb.framebufferHeight; | |
13 | 232 |
15 | 233 // Calculate scaling factor for auto scaling. |
234 if (maxWidth > 0 && maxHeight > 0) { | |
235 int f1 = maxWidth * 100 / fbWidth; | |
236 int f2 = maxHeight * 100 / fbHeight; | |
237 scalingFactor = Math.min(f1, f2); | |
238 if (scalingFactor > 100) | |
239 scalingFactor = 100; | |
240 System.out.println("Scaling desktop at " + scalingFactor + "%"); | |
241 } | |
242 | |
243 // Update scaled framebuffer geometry. | |
244 scaledWidth = (fbWidth * scalingFactor + 50) / 100; | |
245 scaledHeight = (fbHeight * scalingFactor + 50) / 100; | |
13 | 246 |
15 | 247 // Create new off-screen image either if it does not exist, or if |
248 // its geometry should be changed. It's not necessary to replace | |
249 // existing image if only pixel format should be changed. | |
26 | 250 /* |
15 | 251 if (memImage == null) { |
252 memImage = viewer.vncContainer.createImage(fbWidth, fbHeight); | |
253 memGraphics = memImage.getGraphics(); | |
254 } else if (memImage.getWidth(null) != fbWidth | |
255 || memImage.getHeight(null) != fbHeight) { | |
256 synchronized (memImage) { | |
257 memImage = viewer.vncContainer.createImage(fbWidth, fbHeight); | |
258 memGraphics = memImage.getGraphics(); | |
259 } | |
260 } | |
26 | 261 */ |
262 memImage = new BufferedImage(rfb.framebufferWidth, rfb.framebufferHeight, BufferedImage.TYPE_INT_RGB ); | |
263 memGraphics = memImage.getGraphics(); | |
264 | |
15 | 265 // Images with raw pixels should be re-allocated on every change |
266 // of geometry or pixel format. | |
267 if (bytesPixel == 1) { | |
268 | |
269 pixels24 = null; | |
270 pixels8 = new byte[fbWidth * fbHeight]; | |
271 | |
272 pixelsSource = new MemoryImageSource(fbWidth, fbHeight, cm8, | |
273 pixels8, 0, fbWidth); | |
274 | |
275 zrleTilePixels24 = null; | |
276 zrleTilePixels8 = new byte[64 * 64]; | |
13 | 277 |
15 | 278 } else { |
279 | |
280 pixels8 = null; | |
281 pixels24 = new int[fbWidth * fbHeight]; | |
13 | 282 |
15 | 283 pixelsSource = new MemoryImageSource(fbWidth, fbHeight, cm24, |
284 pixels24, 0, fbWidth); | |
285 | |
286 zrleTilePixels8 = null; | |
287 zrleTilePixels24 = new int[64 * 64]; | |
288 | |
289 } | |
290 pixelsSource.setAnimated(true); | |
26 | 291 rawPixelsImage = Toolkit.getDefaultToolkit().createImage(pixelsSource); |
292 // rawPixelsImage = (BufferedImage) Toolkit.getDefaultToolkit().createImage(pixelsSource); | |
293 | |
13 | 294 } |
295 | |
15 | 296 void resizeDesktopFrame() { |
297 setSize(scaledWidth, scaledHeight); | |
13 | 298 |
15 | 299 // FIXME: Find a better way to determine correct size of a |
300 // ScrollPane. -- const | |
301 Insets insets = viewer.desktopScrollPane.getInsets(); | |
302 viewer.desktopScrollPane.setSize( | |
303 scaledWidth + 2 * Math.min(insets.left, insets.right), | |
304 scaledHeight + 2 * Math.min(insets.top, insets.bottom)); | |
13 | 305 |
15 | 306 viewer.vncFrame.pack(); |
307 | |
308 // Try to limit the frame size to the screen size. | |
13 | 309 |
15 | 310 Dimension screenSize = viewer.vncFrame.getToolkit().getScreenSize(); |
311 Dimension frameSize = viewer.vncFrame.getSize(); | |
312 Dimension newSize = frameSize; | |
13 | 313 |
15 | 314 // Reduce Screen Size by 30 pixels in each direction; |
315 // This is a (poor) attempt to account for | |
316 // 1) Menu bar on Macintosh (should really also account for | |
317 // Dock on OSX). Usually 22px on top of screen. | |
318 // 2) Taxkbar on Windows (usually about 28 px on bottom) | |
319 // 3) Other obstructions. | |
13 | 320 |
15 | 321 screenSize.height -= 30; |
322 screenSize.width -= 30; | |
13 | 323 |
15 | 324 boolean needToResizeFrame = false; |
325 if (frameSize.height > screenSize.height) { | |
326 newSize.height = screenSize.height; | |
327 needToResizeFrame = true; | |
328 } | |
329 if (frameSize.width > screenSize.width) { | |
330 newSize.width = screenSize.width; | |
331 needToResizeFrame = true; | |
332 } | |
333 if (needToResizeFrame) { | |
334 viewer.vncFrame.setSize(newSize); | |
335 } | |
13 | 336 |
15 | 337 viewer.desktopScrollPane.doLayout(); |
13 | 338 } |
339 | |
340 // | |
15 | 341 // processNormalProtocol() - executed by the rfbThread to deal with the |
342 // RFB socket. | |
13 | 343 // |
344 | |
15 | 345 public void processNormalProtocol() throws Exception { |
346 | |
347 // Start/stop session recording if necessary. | |
348 viewer.checkRecordingStatus(); | |
349 | |
350 rfb.writeFramebufferUpdateRequest(0, 0, rfb.framebufferWidth, | |
351 rfb.framebufferHeight, false); | |
352 | |
353 resetStats(); | |
354 boolean statsRestarted = false; | |
355 | |
356 // | |
357 // main dispatch loop | |
358 // | |
66 | 359 |
26 | 360 long count = 0; |
15 | 361 while (true) { |
113
8424f64dd736
time out and discarding. kill time out thread after client death.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
108
diff
changeset
|
362 // System.out.println("\ncount=" + count); |
78 | 363 |
15 | 364 count++; |
365 | |
65 | 366 /** |
61 | 367 * read Data from parents and send Data to Client. |
368 */ | |
78 | 369 rfb.sendDataToClient(); |
130 | 370 |
371 if(rfb.returnMsgtype() == RfbProto.FramebufferUpdate ) { | |
372 boolean fullUpdateNeeded = false; | |
373 int w = rfb.framebufferWidth; | |
374 int h = rfb.framebufferHeight; | |
375 rfb.writeFramebufferUpdateRequest(0, 0, w, h, !fullUpdateNeeded); | |
376 continue; | |
377 } | |
78 | 378 |
80 | 379 long numBytesRead = rfb.getNumBytesRead(); |
15 | 380 |
381 // Read message type from the server. | |
382 int msgType = rfb.readServerMessageType(); | |
383 | |
384 // Process the message depending on its type. | |
385 switch (msgType) { | |
77 | 386 case MyRfbProto.SpeedCheckMillis: |
387 rfb.readSpeedCheck(); | |
65 | 388 break; |
15 | 389 case RfbProto.FramebufferUpdate: |
130 | 390 if(msgType == RfbProto.FramebufferUpdate){ |
391 rfb.is.reset(); | |
392 break; | |
393 } | |
394 | |
15 | 395 if (statNumUpdates == viewer.debugStatsExcludeUpdates |
396 && !statsRestarted) { | |
397 resetStats(); | |
398 statsRestarted = true; | |
399 } else if (statNumUpdates == viewer.debugStatsMeasureUpdates | |
400 && statsRestarted) { | |
401 viewer.disconnect(); | |
402 } | |
403 | |
404 rfb.readFramebufferUpdate(); | |
405 statNumUpdates++; | |
406 | |
407 boolean cursorPosReceived = false; | |
408 | |
409 for (int i = 0; i < rfb.updateNRects; i++) { | |
13 | 410 |
15 | 411 rfb.readFramebufferUpdateRectHdr(); |
412 statNumTotalRects++; | |
413 int rx = rfb.updateRectX, ry = rfb.updateRectY; | |
414 int rw = rfb.updateRectW, rh = rfb.updateRectH; | |
415 | |
416 if (rfb.updateRectEncoding == rfb.EncodingLastRect) | |
417 break; | |
418 | |
419 if (rfb.updateRectEncoding == rfb.EncodingNewFBSize) { | |
420 rfb.setFramebufferSize(rw, rh); | |
421 updateFramebufferSize(); | |
422 break; | |
423 } | |
424 | |
425 if (rfb.updateRectEncoding == rfb.EncodingXCursor | |
426 || rfb.updateRectEncoding == rfb.EncodingRichCursor) { | |
427 handleCursorShapeUpdate(rfb.updateRectEncoding, rx, ry, | |
428 rw, rh); | |
429 continue; | |
430 } | |
13 | 431 |
15 | 432 if (rfb.updateRectEncoding == rfb.EncodingPointerPos) { |
433 softCursorMove(rx, ry); | |
434 cursorPosReceived = true; | |
435 continue; | |
436 } | |
13 | 437 |
15 | 438 long numBytesReadBefore = rfb.getNumBytesRead(); |
439 | |
440 rfb.startTiming(); | |
13 | 441 |
15 | 442 switch (rfb.updateRectEncoding) { |
443 case RfbProto.EncodingRaw: | |
444 statNumRectsRaw++; | |
445 handleRawRect(rx, ry, rw, rh); | |
446 break; | |
447 case RfbProto.EncodingCopyRect: | |
448 statNumRectsCopy++; | |
449 handleCopyRect(rx, ry, rw, rh); | |
450 break; | |
451 case RfbProto.EncodingRRE: | |
452 handleRRERect(rx, ry, rw, rh); | |
453 break; | |
454 case RfbProto.EncodingCoRRE: | |
455 handleCoRRERect(rx, ry, rw, rh); | |
456 break; | |
457 case RfbProto.EncodingHextile: | |
458 statNumRectsHextile++; | |
459 handleHextileRect(rx, ry, rw, rh); | |
460 break; | |
461 case RfbProto.EncodingZRLE: | |
108 | 462 case RfbProto.EncodingZRLEE: |
15 | 463 statNumRectsZRLE++; |
464 handleZRLERect(rx, ry, rw, rh); | |
465 break; | |
466 case RfbProto.EncodingZlib: | |
467 handleZlibRect(rx, ry, rw, rh); | |
468 break; | |
469 case RfbProto.EncodingTight: | |
470 statNumRectsTight++; | |
471 handleTightRect(rx, ry, rw, rh); | |
472 break; | |
473 default: | |
474 throw new Exception("Unknown RFB rectangle encoding " | |
475 + rfb.updateRectEncoding); | |
476 } | |
13 | 477 |
15 | 478 rfb.stopTiming(); |
479 | |
480 statNumPixelRects++; | |
481 statNumBytesDecoded += rw * rh * bytesPixel; | |
482 statNumBytesEncoded += (int) (rfb.getNumBytesRead() - numBytesReadBefore); | |
483 } | |
484 | |
485 boolean fullUpdateNeeded = false; | |
13 | 486 |
15 | 487 // Start/stop session recording if necessary. Request full |
488 // update if a new session file was opened. | |
489 if (viewer.checkRecordingStatus()) | |
490 fullUpdateNeeded = true; | |
13 | 491 |
15 | 492 // Defer framebuffer update request if necessary. But wake up |
493 // immediately on keyboard or mouse event. Also, don't sleep | |
494 // if there is some data to receive, or if the last update | |
495 // included a PointerPos message. | |
496 if (viewer.deferUpdateRequests > 0 && rfb.available() == 0 | |
497 && !cursorPosReceived) { | |
498 synchronized (rfb) { | |
499 try { | |
500 rfb.wait(viewer.deferUpdateRequests); | |
501 } catch (InterruptedException e) { | |
502 } | |
13 | 503 } |
504 } | |
15 | 505 |
506 viewer.autoSelectEncodings(); | |
507 | |
508 // Before requesting framebuffer update, check if the pixel | |
509 // format should be changed. | |
510 /* | |
511 if (viewer.options.eightBitColors != (bytesPixel == 1)) { | |
512 // Pixel format should be changed. | |
513 setPixelFormat(); | |
514 fullUpdateNeeded = true; | |
515 } | |
65 | 516 */ |
15 | 517 // Request framebuffer update if needed. |
518 int w = rfb.framebufferWidth; | |
519 int h = rfb.framebufferHeight; | |
520 rfb.writeFramebufferUpdateRequest(0, 0, w, h, !fullUpdateNeeded); | |
521 | |
522 break; | |
523 | |
524 case RfbProto.SetColourMapEntries: | |
525 throw new Exception("Can't handle SetColourMapEntries message"); | |
526 | |
527 case RfbProto.Bell: | |
528 Toolkit.getDefaultToolkit().beep(); | |
529 break; | |
530 | |
531 case RfbProto.ServerCutText: | |
532 String s = rfb.readServerCutText(); | |
533 viewer.clipboard.setCutText(s); | |
534 break; | |
535 | |
536 default: | |
537 throw new Exception("Unknown RFB message type " + msgType); | |
538 } | |
130 | 539 |
540 | |
26 | 541 |
80 | 542 int bufSize = (int)(rfb.getNumBytesRead() - numBytesRead); |
130 | 543 System.out.println("bufSize="+bufSize); |
39 | 544 // rfb.bufResetSend(bufSize); |
25 | 545 |
42 | 546 |
547 | |
27 | 548 if(rfb.createBimgFlag){ |
30 | 549 // bimg = createBufferedImage(rawPixelsImage); |
550 bimg = createBufferedImage(memImage); | |
27 | 551 //bimg(BufferedImage) -> rfb.pngBytes(byte[]) |
552 rfb.createPngBytes(bimg); | |
553 rfb.sendPngImage(); | |
554 rfb.createBimgFlag = false; | |
555 } | |
42 | 556 |
27 | 557 |
26 | 558 /* |
559 boolean result = false; | |
560 try{ | |
561 result = ImageIO.write(bimg, "png", new File("sample.png")); | |
562 }catch(Exception e){ | |
563 e.printStackTrace(); | |
564 result = false; | |
565 } | |
566 */ | |
15 | 567 } |
568 } | |
569 | |
570 // | |
571 // Handle a raw rectangle. The second form with paint==false is used | |
572 // by the Hextile decoder for raw-encoded tiles. | |
573 // | |
574 | |
575 void handleRawRect(int x, int y, int w, int h) throws IOException { | |
576 handleRawRect(x, y, w, h, true); | |
577 } | |
578 | |
579 void handleRawRect(int x, int y, int w, int h, boolean paint) | |
580 throws IOException { | |
581 | |
582 if (bytesPixel == 1) { | |
583 for (int dy = y; dy < y + h; dy++) { | |
584 rfb.readFully(pixels8, dy * rfb.framebufferWidth + x, w); | |
585 if (rfb.rec != null) { | |
586 rfb.rec.write(pixels8, dy * rfb.framebufferWidth + x, w); | |
587 } | |
588 } | |
589 } else { | |
590 byte[] buf = new byte[w * 4]; | |
591 int i, offset; | |
592 for (int dy = y; dy < y + h; dy++) { | |
593 rfb.readFully(buf); | |
594 if (rfb.rec != null) { | |
595 rfb.rec.write(buf); | |
596 } | |
25 | 597 |
598 offset = dy * rfb.framebufferWidth + x; | |
599 for (i = 0; i < w; i++) { | |
600 pixels24[offset + i] = (buf[i * 4 + 2] & 0xFF) << 16 | | |
601 (buf[i * 4 + 1] & 0xFF) << 8 | | |
602 (buf[i * 4] & 0xFF); | |
603 } | |
604 | |
13 | 605 } |
606 } | |
26 | 607 |
25 | 608 handleUpdatedPixels(x, y, w, h); |
26 | 609 // if (paint) scheduleRepaint(x, y, w, h); |
610 | |
15 | 611 } |
612 | |
613 // | |
614 // Handle a CopyRect rectangle. | |
615 // | |
616 | |
617 void handleCopyRect(int x, int y, int w, int h) throws IOException { | |
618 | |
619 rfb.readCopyRect(); | |
620 memGraphics.copyArea(rfb.copyRectSrcX, rfb.copyRectSrcY, w, h, x | |
621 - rfb.copyRectSrcX, y - rfb.copyRectSrcY); | |
622 | |
623 scheduleRepaint(x, y, w, h); | |
624 } | |
625 | |
626 // | |
627 // Handle an RRE-encoded rectangle. | |
628 // | |
629 | |
630 void handleRRERect(int x, int y, int w, int h) throws IOException { | |
631 | |
632 int nSubrects = rfb.readU32(); | |
13 | 633 |
15 | 634 byte[] bg_buf = new byte[bytesPixel]; |
635 rfb.readFully(bg_buf); | |
636 Color pixel; | |
637 if (bytesPixel == 1) { | |
638 pixel = colors[bg_buf[0] & 0xFF]; | |
639 } else { | |
640 pixel = new Color(bg_buf[2] & 0xFF, bg_buf[1] & 0xFF, | |
641 bg_buf[0] & 0xFF); | |
642 } | |
643 memGraphics.setColor(pixel); | |
644 memGraphics.fillRect(x, y, w, h); | |
645 | |
646 byte[] buf = new byte[nSubrects * (bytesPixel + 8)]; | |
647 rfb.readFully(buf); | |
648 DataInputStream ds = new DataInputStream(new ByteArrayInputStream(buf)); | |
649 | |
650 if (rfb.rec != null) { | |
651 rfb.rec.writeIntBE(nSubrects); | |
652 rfb.rec.write(bg_buf); | |
653 rfb.rec.write(buf); | |
654 } | |
655 | |
656 int sx, sy, sw, sh; | |
657 | |
658 for (int j = 0; j < nSubrects; j++) { | |
659 if (bytesPixel == 1) { | |
660 pixel = colors[ds.readUnsignedByte()]; | |
661 } else { | |
662 ds.skip(4); | |
663 pixel = new Color(buf[j * 12 + 2] & 0xFF, | |
664 buf[j * 12 + 1] & 0xFF, buf[j * 12] & 0xFF); | |
665 } | |
666 sx = x + ds.readUnsignedShort(); | |
667 sy = y + ds.readUnsignedShort(); | |
668 sw = ds.readUnsignedShort(); | |
669 sh = ds.readUnsignedShort(); | |
670 | |
671 memGraphics.setColor(pixel); | |
672 memGraphics.fillRect(sx, sy, sw, sh); | |
673 } | |
674 | |
675 scheduleRepaint(x, y, w, h); | |
676 } | |
13 | 677 |
15 | 678 // |
679 // Handle a CoRRE-encoded rectangle. | |
680 // | |
681 | |
682 void handleCoRRERect(int x, int y, int w, int h) throws IOException { | |
683 int nSubrects = rfb.readU32(); | |
684 | |
685 byte[] bg_buf = new byte[bytesPixel]; | |
686 rfb.readFully(bg_buf); | |
687 Color pixel; | |
688 if (bytesPixel == 1) { | |
689 pixel = colors[bg_buf[0] & 0xFF]; | |
690 } else { | |
691 pixel = new Color(bg_buf[2] & 0xFF, bg_buf[1] & 0xFF, | |
692 bg_buf[0] & 0xFF); | |
693 } | |
694 memGraphics.setColor(pixel); | |
695 memGraphics.fillRect(x, y, w, h); | |
696 | |
697 byte[] buf = new byte[nSubrects * (bytesPixel + 4)]; | |
698 rfb.readFully(buf); | |
699 | |
700 if (rfb.rec != null) { | |
701 rfb.rec.writeIntBE(nSubrects); | |
702 rfb.rec.write(bg_buf); | |
703 rfb.rec.write(buf); | |
704 } | |
705 | |
706 int sx, sy, sw, sh; | |
707 int i = 0; | |
13 | 708 |
15 | 709 for (int j = 0; j < nSubrects; j++) { |
710 if (bytesPixel == 1) { | |
711 pixel = colors[buf[i++] & 0xFF]; | |
712 } else { | |
713 pixel = new Color(buf[i + 2] & 0xFF, buf[i + 1] & 0xFF, | |
714 buf[i] & 0xFF); | |
715 i += 4; | |
716 } | |
717 sx = x + (buf[i++] & 0xFF); | |
718 sy = y + (buf[i++] & 0xFF); | |
719 sw = buf[i++] & 0xFF; | |
720 sh = buf[i++] & 0xFF; | |
721 | |
722 memGraphics.setColor(pixel); | |
723 memGraphics.fillRect(sx, sy, sw, sh); | |
724 } | |
725 | |
726 scheduleRepaint(x, y, w, h); | |
727 } | |
728 | |
729 // | |
730 // Handle a Hextile-encoded rectangle. | |
731 // | |
732 | |
733 // These colors should be kept between handleHextileSubrect() calls. | |
734 private Color hextile_bg, hextile_fg; | |
735 | |
736 void handleHextileRect(int x, int y, int w, int h) throws IOException { | |
737 | |
738 hextile_bg = new Color(0); | |
739 hextile_fg = new Color(0); | |
740 | |
741 for (int ty = y; ty < y + h; ty += 16) { | |
742 int th = 16; | |
743 if (y + h - ty < 16) | |
744 th = y + h - ty; | |
745 | |
746 for (int tx = x; tx < x + w; tx += 16) { | |
747 int tw = 16; | |
748 if (x + w - tx < 16) | |
749 tw = x + w - tx; | |
750 | |
751 handleHextileSubrect(tx, ty, tw, th); | |
13 | 752 } |
753 | |
15 | 754 // Finished with a row of tiles, now let's show it. |
755 scheduleRepaint(x, y, w, h); | |
756 } | |
757 } | |
758 | |
759 // | |
760 // Handle one tile in the Hextile-encoded data. | |
761 // | |
762 | |
763 void handleHextileSubrect(int tx, int ty, int tw, int th) | |
764 throws IOException { | |
765 | |
766 int subencoding = rfb.readU8(); | |
767 if (rfb.rec != null) { | |
768 rfb.rec.writeByte(subencoding); | |
769 } | |
13 | 770 |
15 | 771 // Is it a raw-encoded sub-rectangle? |
772 if ((subencoding & rfb.HextileRaw) != 0) { | |
773 handleRawRect(tx, ty, tw, th, false); | |
774 return; | |
775 } | |
13 | 776 |
15 | 777 // Read and draw the background if specified. |
778 byte[] cbuf = new byte[bytesPixel]; | |
779 if ((subencoding & rfb.HextileBackgroundSpecified) != 0) { | |
780 rfb.readFully(cbuf); | |
781 if (bytesPixel == 1) { | |
782 hextile_bg = colors[cbuf[0] & 0xFF]; | |
783 } else { | |
784 hextile_bg = new Color(cbuf[2] & 0xFF, cbuf[1] & 0xFF, | |
785 cbuf[0] & 0xFF); | |
786 } | |
787 if (rfb.rec != null) { | |
788 rfb.rec.write(cbuf); | |
789 } | |
790 } | |
791 memGraphics.setColor(hextile_bg); | |
792 memGraphics.fillRect(tx, ty, tw, th); | |
13 | 793 |
15 | 794 // Read the foreground color if specified. |
795 if ((subencoding & rfb.HextileForegroundSpecified) != 0) { | |
796 rfb.readFully(cbuf); | |
797 if (bytesPixel == 1) { | |
798 hextile_fg = colors[cbuf[0] & 0xFF]; | |
799 } else { | |
800 hextile_fg = new Color(cbuf[2] & 0xFF, cbuf[1] & 0xFF, | |
801 cbuf[0] & 0xFF); | |
802 } | |
803 if (rfb.rec != null) { | |
804 rfb.rec.write(cbuf); | |
805 } | |
806 } | |
807 | |
808 // Done with this tile if there is no sub-rectangles. | |
809 if ((subencoding & rfb.HextileAnySubrects) == 0) | |
810 return; | |
13 | 811 |
15 | 812 int nSubrects = rfb.readU8(); |
813 int bufsize = nSubrects * 2; | |
814 if ((subencoding & rfb.HextileSubrectsColoured) != 0) { | |
815 bufsize += nSubrects * bytesPixel; | |
816 } | |
817 byte[] buf = new byte[bufsize]; | |
818 rfb.readFully(buf); | |
819 if (rfb.rec != null) { | |
820 rfb.rec.writeByte(nSubrects); | |
821 rfb.rec.write(buf); | |
822 } | |
823 | |
824 int b1, b2, sx, sy, sw, sh; | |
825 int i = 0; | |
826 | |
827 if ((subencoding & rfb.HextileSubrectsColoured) == 0) { | |
828 | |
829 // Sub-rectangles are all of the same color. | |
830 memGraphics.setColor(hextile_fg); | |
831 for (int j = 0; j < nSubrects; j++) { | |
832 b1 = buf[i++] & 0xFF; | |
833 b2 = buf[i++] & 0xFF; | |
834 sx = tx + (b1 >> 4); | |
835 sy = ty + (b1 & 0xf); | |
836 sw = (b2 >> 4) + 1; | |
837 sh = (b2 & 0xf) + 1; | |
838 memGraphics.fillRect(sx, sy, sw, sh); | |
839 } | |
840 } else if (bytesPixel == 1) { | |
13 | 841 |
15 | 842 // BGR233 (8-bit color) version for colored sub-rectangles. |
843 for (int j = 0; j < nSubrects; j++) { | |
844 hextile_fg = colors[buf[i++] & 0xFF]; | |
845 b1 = buf[i++] & 0xFF; | |
846 b2 = buf[i++] & 0xFF; | |
847 sx = tx + (b1 >> 4); | |
848 sy = ty + (b1 & 0xf); | |
849 sw = (b2 >> 4) + 1; | |
850 sh = (b2 & 0xf) + 1; | |
851 memGraphics.setColor(hextile_fg); | |
852 memGraphics.fillRect(sx, sy, sw, sh); | |
853 } | |
854 | |
855 } else { | |
856 | |
857 // Full-color (24-bit) version for colored sub-rectangles. | |
858 for (int j = 0; j < nSubrects; j++) { | |
859 hextile_fg = new Color(buf[i + 2] & 0xFF, buf[i + 1] & 0xFF, | |
860 buf[i] & 0xFF); | |
861 i += 4; | |
862 b1 = buf[i++] & 0xFF; | |
863 b2 = buf[i++] & 0xFF; | |
864 sx = tx + (b1 >> 4); | |
865 sy = ty + (b1 & 0xf); | |
866 sw = (b2 >> 4) + 1; | |
867 sh = (b2 & 0xf) + 1; | |
868 memGraphics.setColor(hextile_fg); | |
869 memGraphics.fillRect(sx, sy, sw, sh); | |
870 } | |
871 | |
872 } | |
873 } | |
874 | |
875 // | |
876 // Handle a ZRLE-encoded rectangle. | |
877 // | |
878 // FIXME: Currently, session recording is not fully supported for ZRLE. | |
879 // | |
880 | |
881 void handleZRLERect(int x, int y, int w, int h) throws Exception { | |
882 | |
883 if (zrleInStream == null) | |
884 zrleInStream = new ZlibInStream(); | |
13 | 885 |
15 | 886 int nBytes = rfb.readU32(); |
887 if (nBytes > 64 * 1024 * 1024) | |
888 throw new Exception("ZRLE decoder: illegal compressed data size"); | |
889 | |
890 if (zrleBuf == null || zrleBufLen < nBytes) { | |
891 zrleBufLen = nBytes + 4096; | |
892 zrleBuf = new byte[zrleBufLen]; | |
893 } | |
894 | |
895 // FIXME: Do not wait for all the data before decompression. | |
896 rfb.readFully(zrleBuf, 0, nBytes); | |
897 | |
898 if (rfb.rec != null) { | |
899 if (rfb.recordFromBeginning) { | |
900 rfb.rec.writeIntBE(nBytes); | |
901 rfb.rec.write(zrleBuf, 0, nBytes); | |
902 } else if (!zrleRecWarningShown) { | |
903 System.out.println("Warning: ZRLE session can be recorded" | |
904 + " only from the beginning"); | |
905 System.out.println("Warning: Recorded file may be corrupted"); | |
906 zrleRecWarningShown = true; | |
907 } | |
13 | 908 |
15 | 909 } |
910 | |
911 zrleInStream.setUnderlying(new MemInStream(zrleBuf, 0, nBytes), nBytes); | |
912 | |
913 for (int ty = y; ty < y + h; ty += 64) { | |
914 | |
915 int th = Math.min(y + h - ty, 64); | |
916 | |
917 for (int tx = x; tx < x + w; tx += 64) { | |
918 | |
919 int tw = Math.min(x + w - tx, 64); | |
920 | |
921 int mode = zrleInStream.readU8(); | |
922 boolean rle = (mode & 128) != 0; | |
923 int palSize = mode & 127; | |
924 int[] palette = new int[128]; | |
925 | |
926 readZrlePalette(palette, palSize); | |
927 | |
928 if (palSize == 1) { | |
929 int pix = palette[0]; | |
930 Color c = (bytesPixel == 1) ? colors[pix] : new Color( | |
931 0xFF000000 | pix); | |
932 memGraphics.setColor(c); | |
933 memGraphics.fillRect(tx, ty, tw, th); | |
13 | 934 continue; |
935 } | |
936 | |
15 | 937 if (!rle) { |
938 if (palSize == 0) { | |
939 readZrleRawPixels(tw, th); | |
940 } else { | |
941 readZrlePackedPixels(tw, th, palette, palSize); | |
942 } | |
943 } else { | |
944 if (palSize == 0) { | |
945 readZrlePlainRLEPixels(tw, th); | |
946 } else { | |
947 readZrlePackedRLEPixels(tw, th, palette); | |
13 | 948 } |
949 } | |
15 | 950 handleUpdatedZrleTile(tx, ty, tw, th); |
13 | 951 } |
952 } | |
953 | |
15 | 954 zrleInStream.reset(); |
13 | 955 |
15 | 956 scheduleRepaint(x, y, w, h); |
13 | 957 } |
958 | |
15 | 959 int readPixel(InStream is) throws Exception { |
960 int pix; | |
13 | 961 |
15 | 962 if (bytesPixel == 1) { |
13 | 963 |
15 | 964 pix = is.readU8(); |
13 | 965 } else { |
15 | 966 int p1 = is.readU8(); |
967 int p2 = is.readU8(); | |
968 int p3 = is.readU8(); | |
969 pix = (p3 & 0xFF) << 16 | (p2 & 0xFF) << 8 | (p1 & 0xFF); | |
13 | 970 } |
15 | 971 return pix; |
13 | 972 } |
973 | |
15 | 974 void readPixels(InStream is, int[] dst, int count) throws Exception { |
975 int pix; | |
13 | 976 if (bytesPixel == 1) { |
15 | 977 byte[] buf = new byte[count]; |
978 is.readBytes(buf, 0, count); | |
979 for (int i = 0; i < count; i++) { | |
980 dst[i] = (int) buf[i] & 0xFF; | |
981 } | |
13 | 982 } else { |
15 | 983 byte[] buf = new byte[count * 3]; |
984 is.readBytes(buf, 0, count * 3); | |
985 for (int i = 0; i < count; i++) { | |
986 dst[i] = ((buf[i * 3 + 2] & 0xFF) << 16 | |
987 | (buf[i * 3 + 1] & 0xFF) << 8 | (buf[i * 3] & 0xFF)); | |
988 } | |
13 | 989 } |
990 } | |
991 | |
15 | 992 void readZrlePalette(int[] palette, int palSize) throws Exception { |
993 readPixels(zrleInStream, palette, palSize); | |
13 | 994 } |
995 | |
15 | 996 void readZrleRawPixels(int tw, int th) throws Exception { |
997 if (bytesPixel == 1) { | |
998 zrleInStream.readBytes(zrleTilePixels8, 0, tw * th); | |
999 } else { | |
1000 readPixels(zrleInStream, zrleTilePixels24, tw * th); // / | |
13 | 1001 } |
1002 } | |
1003 | |
15 | 1004 void readZrlePackedPixels(int tw, int th, int[] palette, int palSize) |
1005 throws Exception { | |
13 | 1006 |
15 | 1007 int bppp = ((palSize > 16) ? 8 : ((palSize > 4) ? 4 |
1008 : ((palSize > 2) ? 2 : 1))); | |
1009 int ptr = 0; | |
1010 | |
1011 for (int i = 0; i < th; i++) { | |
1012 int eol = ptr + tw; | |
1013 int b = 0; | |
1014 int nbits = 0; | |
13 | 1015 |
15 | 1016 while (ptr < eol) { |
1017 if (nbits == 0) { | |
1018 b = zrleInStream.readU8(); | |
1019 nbits = 8; | |
1020 } | |
1021 nbits -= bppp; | |
1022 int index = (b >> nbits) & ((1 << bppp) - 1) & 127; | |
1023 if (bytesPixel == 1) { | |
1024 zrleTilePixels8[ptr++] = (byte) palette[index]; | |
1025 } else { | |
1026 zrleTilePixels24[ptr++] = palette[index]; | |
1027 } | |
13 | 1028 } |
1029 } | |
1030 } | |
1031 | |
15 | 1032 void readZrlePlainRLEPixels(int tw, int th) throws Exception { |
1033 int ptr = 0; | |
1034 int end = ptr + tw * th; | |
1035 while (ptr < end) { | |
1036 int pix = readPixel(zrleInStream); | |
1037 int len = 1; | |
13 | 1038 int b; |
1039 do { | |
1040 b = zrleInStream.readU8(); | |
1041 len += b; | |
1042 } while (b == 255); | |
1043 | |
1044 if (!(len <= end - ptr)) | |
1045 throw new Exception("ZRLE decoder: assertion failed" | |
15 | 1046 + " (len <= end-ptr)"); |
13 | 1047 |
15 | 1048 if (bytesPixel == 1) { |
1049 while (len-- > 0) | |
1050 zrleTilePixels8[ptr++] = (byte) pix; | |
1051 } else { | |
1052 while (len-- > 0) | |
1053 zrleTilePixels24[ptr++] = pix; | |
1054 } | |
13 | 1055 } |
1056 } | |
1057 | |
15 | 1058 void readZrlePackedRLEPixels(int tw, int th, int[] palette) |
1059 throws Exception { | |
13 | 1060 |
15 | 1061 int ptr = 0; |
1062 int end = ptr + tw * th; | |
1063 while (ptr < end) { | |
1064 int index = zrleInStream.readU8(); | |
1065 int len = 1; | |
1066 if ((index & 128) != 0) { | |
1067 int b; | |
1068 do { | |
1069 b = zrleInStream.readU8(); | |
1070 len += b; | |
1071 } while (b == 255); | |
13 | 1072 |
15 | 1073 if (!(len <= end - ptr)) |
1074 throw new Exception("ZRLE decoder: assertion failed" | |
1075 + " (len <= end - ptr)"); | |
1076 } | |
13 | 1077 |
15 | 1078 index &= 127; |
1079 int pix = palette[index]; | |
13 | 1080 |
15 | 1081 if (bytesPixel == 1) { |
1082 while (len-- > 0) | |
1083 zrleTilePixels8[ptr++] = (byte) pix; | |
1084 } else { | |
1085 while (len-- > 0) | |
1086 zrleTilePixels24[ptr++] = pix; | |
13 | 1087 } |
1088 } | |
1089 } | |
1090 | |
15 | 1091 // |
1092 // Copy pixels from zrleTilePixels8 or zrleTilePixels24, then update. | |
1093 // | |
13 | 1094 |
15 | 1095 void handleUpdatedZrleTile(int x, int y, int w, int h) { |
1096 Object src, dst; | |
1097 if (bytesPixel == 1) { | |
1098 src = zrleTilePixels8; | |
1099 dst = pixels8; | |
13 | 1100 } else { |
15 | 1101 src = zrleTilePixels24; |
1102 dst = pixels24; | |
13 | 1103 } |
15 | 1104 int offsetSrc = 0; |
1105 int offsetDst = (y * rfb.framebufferWidth + x); | |
1106 for (int j = 0; j < h; j++) { | |
1107 System.arraycopy(src, offsetSrc, dst, offsetDst, w); | |
1108 offsetSrc += w; | |
1109 offsetDst += rfb.framebufferWidth; | |
1110 } | |
1111 handleUpdatedPixels(x, y, w, h); | |
13 | 1112 } |
1113 | |
15 | 1114 // |
1115 // Handle a Zlib-encoded rectangle. | |
1116 // | |
1117 | |
1118 void handleZlibRect(int x, int y, int w, int h) throws Exception { | |
1119 | |
1120 int nBytes = rfb.readU32(); | |
13 | 1121 |
15 | 1122 if (zlibBuf == null || zlibBufLen < nBytes) { |
1123 zlibBufLen = nBytes * 2; | |
1124 zlibBuf = new byte[zlibBufLen]; | |
1125 } | |
1126 | |
1127 rfb.readFully(zlibBuf, 0, nBytes); | |
13 | 1128 |
15 | 1129 if (rfb.rec != null && rfb.recordFromBeginning) { |
1130 rfb.rec.writeIntBE(nBytes); | |
1131 rfb.rec.write(zlibBuf, 0, nBytes); | |
1132 } | |
1133 | |
1134 if (zlibInflater == null) { | |
1135 zlibInflater = new Inflater(); | |
1136 } | |
1137 zlibInflater.setInput(zlibBuf, 0, nBytes); | |
13 | 1138 |
1139 if (bytesPixel == 1) { | |
15 | 1140 for (int dy = y; dy < y + h; dy++) { |
1141 zlibInflater.inflate(pixels8, dy * rfb.framebufferWidth + x, w); | |
1142 if (rfb.rec != null && !rfb.recordFromBeginning) | |
1143 rfb.rec.write(pixels8, dy * rfb.framebufferWidth + x, w); | |
13 | 1144 } |
1145 } else { | |
15 | 1146 byte[] buf = new byte[w * 4]; |
1147 int i, offset; | |
1148 for (int dy = y; dy < y + h; dy++) { | |
1149 zlibInflater.inflate(buf); | |
1150 offset = dy * rfb.framebufferWidth + x; | |
1151 for (i = 0; i < w; i++) { | |
1152 pixels24[offset + i] = (buf[i * 4 + 2] & 0xFF) << 16 | |
1153 | (buf[i * 4 + 1] & 0xFF) << 8 | |
1154 | (buf[i * 4] & 0xFF); | |
1155 } | |
1156 if (rfb.rec != null && !rfb.recordFromBeginning) | |
1157 rfb.rec.write(buf); | |
13 | 1158 } |
1159 } | |
15 | 1160 |
1161 handleUpdatedPixels(x, y, w, h); | |
13 | 1162 scheduleRepaint(x, y, w, h); |
1163 } | |
1164 | |
15 | 1165 // |
1166 // Handle a Tight-encoded rectangle. | |
1167 // | |
13 | 1168 |
15 | 1169 void handleTightRect(int x, int y, int w, int h) throws Exception { |
13 | 1170 |
15 | 1171 int comp_ctl = rfb.readU8(); |
1172 if (rfb.rec != null) { | |
1173 if (rfb.recordFromBeginning || comp_ctl == (rfb.TightFill << 4) | |
1174 || comp_ctl == (rfb.TightJpeg << 4)) { | |
1175 // Send data exactly as received. | |
1176 rfb.rec.writeByte(comp_ctl); | |
1177 } else { | |
1178 // Tell the decoder to flush each of the four zlib streams. | |
1179 rfb.rec.writeByte(comp_ctl | 0x0F); | |
13 | 1180 } |
1181 } | |
1182 | |
15 | 1183 // Flush zlib streams if we are told by the server to do so. |
1184 for (int stream_id = 0; stream_id < 4; stream_id++) { | |
1185 if ((comp_ctl & 1) != 0 && tightInflaters[stream_id] != null) { | |
1186 tightInflaters[stream_id] = null; | |
1187 } | |
1188 comp_ctl >>= 1; | |
1189 } | |
13 | 1190 |
15 | 1191 // Check correctness of subencoding value. |
1192 if (comp_ctl > rfb.TightMaxSubencoding) { | |
1193 throw new Exception("Incorrect tight subencoding: " + comp_ctl); | |
13 | 1194 } |
15 | 1195 |
1196 // Handle solid-color rectangles. | |
1197 if (comp_ctl == rfb.TightFill) { | |
1198 | |
13 | 1199 if (bytesPixel == 1) { |
15 | 1200 int idx = rfb.readU8(); |
1201 memGraphics.setColor(colors[idx]); | |
13 | 1202 if (rfb.rec != null) { |
15 | 1203 rfb.rec.writeByte(idx); |
13 | 1204 } |
1205 } else { | |
15 | 1206 byte[] buf = new byte[3]; |
1207 rfb.readFully(buf); | |
1208 if (rfb.rec != null) { | |
1209 rfb.rec.write(buf); | |
1210 } | |
1211 Color bg = new Color(0xFF000000 | (buf[0] & 0xFF) << 16 | |
1212 | (buf[1] & 0xFF) << 8 | (buf[2] & 0xFF)); | |
1213 memGraphics.setColor(bg); | |
1214 } | |
1215 memGraphics.fillRect(x, y, w, h); | |
1216 scheduleRepaint(x, y, w, h); | |
1217 return; | |
1218 | |
1219 } | |
1220 | |
1221 if (comp_ctl == rfb.TightJpeg) { | |
1222 | |
1223 statNumRectsTightJPEG++; | |
1224 | |
1225 // Read JPEG data. | |
1226 byte[] jpegData = new byte[rfb.readCompactLen()]; | |
1227 rfb.readFully(jpegData); | |
1228 if (rfb.rec != null) { | |
1229 if (!rfb.recordFromBeginning) { | |
1230 rfb.recordCompactLen(jpegData.length); | |
1231 } | |
1232 rfb.rec.write(jpegData); | |
1233 } | |
1234 | |
1235 // Create an Image object from the JPEG data. | |
1236 Image jpegImage = Toolkit.getDefaultToolkit().createImage(jpegData); | |
1237 | |
1238 // Remember the rectangle where the image should be drawn. | |
1239 jpegRect = new Rectangle(x, y, w, h); | |
1240 | |
1241 // Let the imageUpdate() method do the actual drawing, here just | |
1242 // wait until the image is fully loaded and drawn. | |
1243 synchronized (jpegRect) { | |
1244 Toolkit.getDefaultToolkit().prepareImage(jpegImage, -1, -1, | |
1245 this); | |
1246 try { | |
1247 // Wait no longer than three seconds. | |
1248 jpegRect.wait(3000); | |
1249 } catch (InterruptedException e) { | |
1250 throw new Exception("Interrupted while decoding JPEG image"); | |
1251 } | |
1252 } | |
1253 | |
1254 // Done, jpegRect is not needed any more. | |
1255 jpegRect = null; | |
1256 return; | |
1257 | |
1258 } | |
1259 | |
1260 // Read filter id and parameters. | |
1261 int numColors = 0, rowSize = w; | |
1262 byte[] palette8 = new byte[2]; | |
1263 int[] palette24 = new int[256]; | |
1264 boolean useGradient = false; | |
1265 if ((comp_ctl & rfb.TightExplicitFilter) != 0) { | |
1266 int filter_id = rfb.readU8(); | |
1267 if (rfb.rec != null) { | |
1268 rfb.rec.writeByte(filter_id); | |
1269 } | |
1270 if (filter_id == rfb.TightFilterPalette) { | |
1271 numColors = rfb.readU8() + 1; | |
1272 if (rfb.rec != null) { | |
1273 rfb.rec.writeByte(numColors - 1); | |
1274 } | |
1275 if (bytesPixel == 1) { | |
1276 if (numColors != 2) { | |
1277 throw new Exception("Incorrect tight palette size: " | |
1278 + numColors); | |
1279 } | |
1280 rfb.readFully(palette8); | |
1281 if (rfb.rec != null) { | |
1282 rfb.rec.write(palette8); | |
1283 } | |
1284 } else { | |
1285 byte[] buf = new byte[numColors * 3]; | |
1286 rfb.readFully(buf); | |
1287 if (rfb.rec != null) { | |
1288 rfb.rec.write(buf); | |
1289 } | |
1290 for (int i = 0; i < numColors; i++) { | |
1291 palette24[i] = ((buf[i * 3] & 0xFF) << 16 | |
1292 | (buf[i * 3 + 1] & 0xFF) << 8 | (buf[i * 3 + 2] & 0xFF)); | |
1293 } | |
1294 } | |
1295 if (numColors == 2) | |
1296 rowSize = (w + 7) / 8; | |
1297 } else if (filter_id == rfb.TightFilterGradient) { | |
1298 useGradient = true; | |
1299 } else if (filter_id != rfb.TightFilterCopy) { | |
1300 throw new Exception("Incorrect tight filter id: " + filter_id); | |
1301 } | |
1302 } | |
1303 if (numColors == 0 && bytesPixel == 4) | |
1304 rowSize *= 3; | |
1305 | |
1306 // Read, optionally uncompress and decode data. | |
1307 int dataSize = h * rowSize; | |
1308 if (dataSize < rfb.TightMinToCompress) { | |
1309 // Data size is small - not compressed with zlib. | |
1310 if (numColors != 0) { | |
1311 // Indexed colors. | |
1312 byte[] indexedData = new byte[dataSize]; | |
1313 rfb.readFully(indexedData); | |
1314 if (rfb.rec != null) { | |
1315 rfb.rec.write(indexedData); | |
1316 } | |
1317 if (numColors == 2) { | |
1318 // Two colors. | |
1319 if (bytesPixel == 1) { | |
1320 decodeMonoData(x, y, w, h, indexedData, palette8); | |
1321 } else { | |
1322 decodeMonoData(x, y, w, h, indexedData, palette24); | |
1323 } | |
1324 } else { | |
1325 // 3..255 colors (assuming bytesPixel == 4). | |
1326 int i = 0; | |
1327 for (int dy = y; dy < y + h; dy++) { | |
1328 for (int dx = x; dx < x + w; dx++) { | |
1329 pixels24[dy * rfb.framebufferWidth + dx] = palette24[indexedData[i++] & 0xFF]; | |
1330 } | |
1331 } | |
1332 } | |
1333 } else if (useGradient) { | |
1334 // "Gradient"-processed data | |
1335 byte[] buf = new byte[w * h * 3]; | |
13 | 1336 rfb.readFully(buf); |
1337 if (rfb.rec != null) { | |
1338 rfb.rec.write(buf); | |
1339 } | |
15 | 1340 decodeGradientData(x, y, w, h, buf); |
1341 } else { | |
1342 // Raw truecolor data. | |
13 | 1343 if (bytesPixel == 1) { |
15 | 1344 for (int dy = y; dy < y + h; dy++) { |
1345 rfb.readFully(pixels8, dy * rfb.framebufferWidth + x, w); | |
1346 if (rfb.rec != null) { | |
1347 rfb.rec.write(pixels8, dy * rfb.framebufferWidth | |
1348 + x, w); | |
1349 } | |
1350 } | |
13 | 1351 } else { |
15 | 1352 byte[] buf = new byte[w * 3]; |
1353 int i, offset; | |
1354 for (int dy = y; dy < y + h; dy++) { | |
1355 rfb.readFully(buf); | |
1356 if (rfb.rec != null) { | |
1357 rfb.rec.write(buf); | |
1358 } | |
1359 offset = dy * rfb.framebufferWidth + x; | |
1360 for (i = 0; i < w; i++) { | |
1361 pixels24[offset + i] = (buf[i * 3] & 0xFF) << 16 | |
1362 | (buf[i * 3 + 1] & 0xFF) << 8 | |
1363 | (buf[i * 3 + 2] & 0xFF); | |
1364 } | |
13 | 1365 } |
1366 } | |
1367 } | |
15 | 1368 } else { |
1369 // Data was compressed with zlib. | |
1370 int zlibDataLen = rfb.readCompactLen(); | |
1371 byte[] zlibData = new byte[zlibDataLen]; | |
1372 rfb.readFully(zlibData); | |
1373 if (rfb.rec != null && rfb.recordFromBeginning) { | |
1374 rfb.rec.write(zlibData); | |
1375 } | |
1376 int stream_id = comp_ctl & 0x03; | |
1377 if (tightInflaters[stream_id] == null) { | |
1378 tightInflaters[stream_id] = new Inflater(); | |
13 | 1379 } |
15 | 1380 Inflater myInflater = tightInflaters[stream_id]; |
1381 myInflater.setInput(zlibData); | |
1382 byte[] buf = new byte[dataSize]; | |
1383 myInflater.inflate(buf); | |
1384 if (rfb.rec != null && !rfb.recordFromBeginning) { | |
1385 rfb.recordCompressedData(buf); | |
1386 } | |
1387 | |
1388 if (numColors != 0) { | |
1389 // Indexed colors. | |
1390 if (numColors == 2) { | |
1391 // Two colors. | |
1392 if (bytesPixel == 1) { | |
1393 decodeMonoData(x, y, w, h, buf, palette8); | |
1394 } else { | |
1395 decodeMonoData(x, y, w, h, buf, palette24); | |
13 | 1396 } |
15 | 1397 } else { |
1398 // More than two colors (assuming bytesPixel == 4). | |
1399 int i = 0; | |
1400 for (int dy = y; dy < y + h; dy++) { | |
1401 for (int dx = x; dx < x + w; dx++) { | |
1402 pixels24[dy * rfb.framebufferWidth + dx] = palette24[buf[i++] & 0xFF]; | |
1403 } | |
13 | 1404 } |
1405 } | |
15 | 1406 } else if (useGradient) { |
1407 // Compressed "Gradient"-filtered data (assuming bytesPixel == | |
1408 // 4). | |
1409 decodeGradientData(x, y, w, h, buf); | |
13 | 1410 } else { |
15 | 1411 // Compressed truecolor data. |
1412 if (bytesPixel == 1) { | |
1413 int destOffset = y * rfb.framebufferWidth + x; | |
1414 for (int dy = 0; dy < h; dy++) { | |
1415 System.arraycopy(buf, dy * w, pixels8, destOffset, w); | |
1416 destOffset += rfb.framebufferWidth; | |
13 | 1417 } |
15 | 1418 } else { |
1419 int srcOffset = 0; | |
1420 int destOffset, i; | |
1421 for (int dy = 0; dy < h; dy++) { | |
1422 myInflater.inflate(buf); | |
1423 destOffset = (y + dy) * rfb.framebufferWidth + x; | |
1424 for (i = 0; i < w; i++) { | |
1425 pixels24[destOffset + i] = (buf[srcOffset] & 0xFF) << 16 | |
1426 | (buf[srcOffset + 1] & 0xFF) << 8 | |
1427 | (buf[srcOffset + 2] & 0xFF); | |
1428 srcOffset += 3; | |
1429 } | |
13 | 1430 } |
1431 } | |
1432 } | |
1433 } | |
15 | 1434 |
1435 handleUpdatedPixels(x, y, w, h); | |
1436 scheduleRepaint(x, y, w, h); | |
1437 } | |
1438 | |
1439 // | |
1440 // Decode 1bpp-encoded bi-color rectangle (8-bit and 24-bit versions). | |
1441 // | |
1442 | |
1443 void decodeMonoData(int x, int y, int w, int h, byte[] src, byte[] palette) { | |
1444 | |
1445 int dx, dy, n; | |
1446 int i = y * rfb.framebufferWidth + x; | |
1447 int rowBytes = (w + 7) / 8; | |
1448 byte b; | |
1449 | |
1450 for (dy = 0; dy < h; dy++) { | |
1451 for (dx = 0; dx < w / 8; dx++) { | |
1452 b = src[dy * rowBytes + dx]; | |
1453 for (n = 7; n >= 0; n--) | |
1454 pixels8[i++] = palette[b >> n & 1]; | |
1455 } | |
1456 for (n = 7; n >= 8 - w % 8; n--) { | |
1457 pixels8[i++] = palette[src[dy * rowBytes + dx] >> n & 1]; | |
1458 } | |
1459 i += (rfb.framebufferWidth - w); | |
1460 } | |
1461 } | |
1462 | |
1463 void decodeMonoData(int x, int y, int w, int h, byte[] src, int[] palette) { | |
1464 | |
1465 int dx, dy, n; | |
1466 int i = y * rfb.framebufferWidth + x; | |
1467 int rowBytes = (w + 7) / 8; | |
1468 byte b; | |
1469 | |
1470 for (dy = 0; dy < h; dy++) { | |
1471 for (dx = 0; dx < w / 8; dx++) { | |
1472 b = src[dy * rowBytes + dx]; | |
1473 for (n = 7; n >= 0; n--) | |
1474 pixels24[i++] = palette[b >> n & 1]; | |
1475 } | |
1476 for (n = 7; n >= 8 - w % 8; n--) { | |
1477 pixels24[i++] = palette[src[dy * rowBytes + dx] >> n & 1]; | |
1478 } | |
1479 i += (rfb.framebufferWidth - w); | |
1480 } | |
1481 } | |
1482 | |
1483 // | |
1484 // Decode data processed with the "Gradient" filter. | |
1485 // | |
1486 | |
1487 void decodeGradientData(int x, int y, int w, int h, byte[] buf) { | |
1488 | |
1489 int dx, dy, c; | |
1490 byte[] prevRow = new byte[w * 3]; | |
1491 byte[] thisRow = new byte[w * 3]; | |
1492 byte[] pix = new byte[3]; | |
1493 int[] est = new int[3]; | |
1494 | |
1495 int offset = y * rfb.framebufferWidth + x; | |
1496 | |
1497 for (dy = 0; dy < h; dy++) { | |
1498 | |
1499 /* First pixel in a row */ | |
1500 for (c = 0; c < 3; c++) { | |
1501 pix[c] = (byte) (prevRow[c] + buf[dy * w * 3 + c]); | |
1502 thisRow[c] = pix[c]; | |
1503 } | |
1504 pixels24[offset++] = (pix[0] & 0xFF) << 16 | (pix[1] & 0xFF) << 8 | |
1505 | (pix[2] & 0xFF); | |
1506 | |
1507 /* Remaining pixels of a row */ | |
1508 for (dx = 1; dx < w; dx++) { | |
1509 for (c = 0; c < 3; c++) { | |
1510 est[c] = ((prevRow[dx * 3 + c] & 0xFF) + (pix[c] & 0xFF) - (prevRow[(dx - 1) | |
1511 * 3 + c] & 0xFF)); | |
1512 if (est[c] > 0xFF) { | |
1513 est[c] = 0xFF; | |
1514 } else if (est[c] < 0x00) { | |
1515 est[c] = 0x00; | |
1516 } | |
1517 pix[c] = (byte) (est[c] + buf[(dy * w + dx) * 3 + c]); | |
1518 thisRow[dx * 3 + c] = pix[c]; | |
1519 } | |
1520 pixels24[offset++] = (pix[0] & 0xFF) << 16 | |
1521 | (pix[1] & 0xFF) << 8 | (pix[2] & 0xFF); | |
1522 } | |
1523 | |
1524 System.arraycopy(thisRow, 0, prevRow, 0, w * 3); | |
1525 offset += (rfb.framebufferWidth - w); | |
1526 } | |
13 | 1527 } |
1528 | |
15 | 1529 // |
1530 // Display newly updated area of pixels. | |
1531 // | |
1532 | |
1533 void handleUpdatedPixels(int x, int y, int w, int h) { | |
13 | 1534 |
15 | 1535 // Draw updated pixels of the off-screen image. |
25 | 1536 |
15 | 1537 pixelsSource.newPixels(x, y, w, h); |
1538 memGraphics.setClip(x, y, w, h); | |
1539 memGraphics.drawImage(rawPixelsImage, 0, 0, null); | |
1540 memGraphics.setClip(0, 0, rfb.framebufferWidth, rfb.framebufferHeight); | |
25 | 1541 |
15 | 1542 } |
13 | 1543 |
15 | 1544 // |
1545 // Tell JVM to repaint specified desktop area. | |
1546 // | |
13 | 1547 |
15 | 1548 void scheduleRepaint(int x, int y, int w, int h) { |
1549 // Request repaint, deferred if necessary. | |
1550 if (rfb.framebufferWidth == scaledWidth) { | |
1551 repaint(viewer.deferScreenUpdates, x, y, w, h); | |
1552 } else { | |
1553 int sx = x * scalingFactor / 100; | |
1554 int sy = y * scalingFactor / 100; | |
1555 int sw = ((x + w) * scalingFactor + 49) / 100 - sx + 1; | |
1556 int sh = ((y + h) * scalingFactor + 49) / 100 - sy + 1; | |
1557 repaint(viewer.deferScreenUpdates, sx, sy, sw, sh); | |
13 | 1558 } |
1559 } | |
15 | 1560 |
1561 // | |
1562 // Handle events. | |
1563 // | |
1564 | |
1565 public void keyPressed(KeyEvent evt) { | |
1566 processLocalKeyEvent(evt); | |
1567 } | |
13 | 1568 |
15 | 1569 public void keyReleased(KeyEvent evt) { |
1570 processLocalKeyEvent(evt); | |
1571 } | |
13 | 1572 |
15 | 1573 public void keyTyped(KeyEvent evt) { |
1574 evt.consume(); | |
1575 } | |
1576 | |
1577 public void mousePressed(MouseEvent evt) { | |
1578 processLocalMouseEvent(evt, false); | |
1579 } | |
13 | 1580 |
15 | 1581 public void mouseReleased(MouseEvent evt) { |
1582 processLocalMouseEvent(evt, false); | |
1583 } | |
13 | 1584 |
15 | 1585 public void mouseMoved(MouseEvent evt) { |
1586 processLocalMouseEvent(evt, true); | |
1587 } | |
1588 | |
1589 public void mouseDragged(MouseEvent evt) { | |
1590 processLocalMouseEvent(evt, true); | |
1591 } | |
13 | 1592 |
15 | 1593 public void processLocalKeyEvent(KeyEvent evt) { |
1594 if (viewer.rfb != null && rfb.inNormalProtocol) { | |
1595 if (!inputEnabled) { | |
1596 if ((evt.getKeyChar() == 'r' || evt.getKeyChar() == 'R') | |
1597 && evt.getID() == KeyEvent.KEY_PRESSED) { | |
1598 // Request screen update. | |
1599 try { | |
1600 rfb.writeFramebufferUpdateRequest(0, 0, | |
1601 rfb.framebufferWidth, rfb.framebufferHeight, | |
1602 false); | |
1603 } catch (IOException e) { | |
1604 e.printStackTrace(); | |
1605 } | |
13 | 1606 } |
15 | 1607 } else { |
1608 // Input enabled. | |
1609 synchronized (rfb) { | |
1610 try { | |
1611 rfb.writeKeyEvent(evt); | |
1612 } catch (Exception e) { | |
1613 e.printStackTrace(); | |
1614 } | |
1615 rfb.notify(); | |
13 | 1616 } |
1617 } | |
15 | 1618 } |
1619 // Don't ever pass keyboard events to AWT for default processing. | |
1620 // Otherwise, pressing Tab would switch focus to ButtonPanel etc. | |
1621 evt.consume(); | |
1622 } | |
1623 | |
1624 public void processLocalMouseEvent(MouseEvent evt, boolean moved) { | |
1625 if (viewer.rfb != null && rfb.inNormalProtocol) { | |
1626 if (moved) { | |
1627 softCursorMove(evt.getX(), evt.getY()); | |
1628 } | |
1629 if (rfb.framebufferWidth != scaledWidth) { | |
1630 int sx = (evt.getX() * 100 + scalingFactor / 2) / scalingFactor; | |
1631 int sy = (evt.getY() * 100 + scalingFactor / 2) / scalingFactor; | |
1632 evt.translatePoint(sx - evt.getX(), sy - evt.getY()); | |
1633 } | |
13 | 1634 synchronized (rfb) { |
1635 try { | |
15 | 1636 rfb.writePointerEvent(evt); |
13 | 1637 } catch (Exception e) { |
1638 e.printStackTrace(); | |
1639 } | |
1640 rfb.notify(); | |
1641 } | |
1642 } | |
1643 } | |
15 | 1644 |
1645 // | |
1646 // Ignored events. | |
1647 // | |
1648 | |
1649 public void mouseClicked(MouseEvent evt) { | |
1650 } | |
1651 | |
1652 public void mouseEntered(MouseEvent evt) { | |
1653 } | |
1654 | |
1655 public void mouseExited(MouseEvent evt) { | |
1656 } | |
1657 | |
1658 // | |
1659 // Reset update statistics. | |
1660 // | |
13 | 1661 |
15 | 1662 void resetStats() { |
1663 statStartTime = System.currentTimeMillis(); | |
1664 statNumUpdates = 0; | |
1665 statNumTotalRects = 0; | |
1666 statNumPixelRects = 0; | |
1667 statNumRectsTight = 0; | |
1668 statNumRectsTightJPEG = 0; | |
1669 statNumRectsZRLE = 0; | |
1670 statNumRectsHextile = 0; | |
1671 statNumRectsRaw = 0; | |
1672 statNumRectsCopy = 0; | |
1673 statNumBytesEncoded = 0; | |
1674 statNumBytesDecoded = 0; | |
13 | 1675 } |
1676 | |
15 | 1677 // //////////////////////////////////////////////////////////////// |
1678 // | |
1679 // Handle cursor shape updates (XCursor and RichCursor encodings). | |
1680 // | |
13 | 1681 |
15 | 1682 boolean showSoftCursor = false; |
13 | 1683 |
15 | 1684 MemoryImageSource softCursorSource; |
1685 Image softCursor; | |
13 | 1686 |
15 | 1687 int cursorX = 0, cursorY = 0; |
1688 int cursorWidth, cursorHeight; | |
1689 int origCursorWidth, origCursorHeight; | |
1690 int hotX, hotY; | |
1691 int origHotX, origHotY; | |
13 | 1692 |
15 | 1693 // |
1694 // Handle cursor shape update (XCursor and RichCursor encodings). | |
1695 // | |
1696 | |
1697 synchronized void handleCursorShapeUpdate(int encodingType, int xhot, | |
1698 int yhot, int width, int height) throws IOException { | |
1699 | |
1700 softCursorFree(); | |
1701 | |
1702 if (width * height == 0) | |
1703 return; | |
13 | 1704 |
15 | 1705 // Ignore cursor shape data if requested by user. |
1706 if (viewer.options.ignoreCursorUpdates) { | |
1707 int bytesPerRow = (width + 7) / 8; | |
1708 int bytesMaskData = bytesPerRow * height; | |
13 | 1709 |
15 | 1710 if (encodingType == rfb.EncodingXCursor) { |
1711 rfb.skipBytes(6 + bytesMaskData * 2); | |
1712 } else { | |
1713 // rfb.EncodingRichCursor | |
1714 rfb.skipBytes(width * height * bytesPixel + bytesMaskData); | |
1715 } | |
1716 return; | |
1717 } | |
13 | 1718 |
15 | 1719 // Decode cursor pixel data. |
1720 softCursorSource = decodeCursorShape(encodingType, width, height); | |
13 | 1721 |
15 | 1722 // Set original (non-scaled) cursor dimensions. |
1723 origCursorWidth = width; | |
1724 origCursorHeight = height; | |
1725 origHotX = xhot; | |
1726 origHotY = yhot; | |
1727 | |
1728 // Create off-screen cursor image. | |
1729 createSoftCursor(); | |
13 | 1730 |
15 | 1731 // Show the cursor. |
1732 showSoftCursor = true; | |
1733 repaint(viewer.deferCursorUpdates, cursorX - hotX, cursorY - hotY, | |
1734 cursorWidth, cursorHeight); | |
1735 } | |
13 | 1736 |
15 | 1737 // |
1738 // decodeCursorShape(). Decode cursor pixel data and return | |
1739 // corresponding MemoryImageSource instance. | |
1740 // | |
13 | 1741 |
15 | 1742 synchronized MemoryImageSource decodeCursorShape(int encodingType, |
1743 int width, int height) throws IOException { | |
13 | 1744 |
1745 int bytesPerRow = (width + 7) / 8; | |
1746 int bytesMaskData = bytesPerRow * height; | |
1747 | |
15 | 1748 int[] softCursorPixels = new int[width * height]; |
1749 | |
13 | 1750 if (encodingType == rfb.EncodingXCursor) { |
1751 | |
15 | 1752 // Read foreground and background colors of the cursor. |
1753 byte[] rgb = new byte[6]; | |
1754 rfb.readFully(rgb); | |
1755 int[] colors = { | |
1756 (0xFF000000 | (rgb[3] & 0xFF) << 16 | (rgb[4] & 0xFF) << 8 | (rgb[5] & 0xFF)), | |
1757 (0xFF000000 | (rgb[0] & 0xFF) << 16 | (rgb[1] & 0xFF) << 8 | (rgb[2] & 0xFF)) }; | |
13 | 1758 |
15 | 1759 // Read pixel and mask data. |
1760 byte[] pixBuf = new byte[bytesMaskData]; | |
1761 rfb.readFully(pixBuf); | |
1762 byte[] maskBuf = new byte[bytesMaskData]; | |
1763 rfb.readFully(maskBuf); | |
13 | 1764 |
15 | 1765 // Decode pixel data into softCursorPixels[]. |
1766 byte pixByte, maskByte; | |
1767 int x, y, n, result; | |
1768 int i = 0; | |
1769 for (y = 0; y < height; y++) { | |
1770 for (x = 0; x < width / 8; x++) { | |
1771 pixByte = pixBuf[y * bytesPerRow + x]; | |
1772 maskByte = maskBuf[y * bytesPerRow + x]; | |
1773 for (n = 7; n >= 0; n--) { | |
1774 if ((maskByte >> n & 1) != 0) { | |
1775 result = colors[pixByte >> n & 1]; | |
1776 } else { | |
1777 result = 0; // Transparent pixel | |
1778 } | |
1779 softCursorPixels[i++] = result; | |
1780 } | |
1781 } | |
1782 for (n = 7; n >= 8 - width % 8; n--) { | |
1783 if ((maskBuf[y * bytesPerRow + x] >> n & 1) != 0) { | |
1784 result = colors[pixBuf[y * bytesPerRow + x] >> n & 1]; | |
13 | 1785 } else { |
1786 result = 0; // Transparent pixel | |
1787 } | |
1788 softCursorPixels[i++] = result; | |
1789 } | |
1790 } | |
15 | 1791 |
1792 } else { | |
1793 // encodingType == rfb.EncodingRichCursor | |
13 | 1794 |
15 | 1795 // Read pixel and mask data. |
1796 byte[] pixBuf = new byte[width * height * bytesPixel]; | |
1797 rfb.readFully(pixBuf); | |
1798 byte[] maskBuf = new byte[bytesMaskData]; | |
1799 rfb.readFully(maskBuf); | |
13 | 1800 |
15 | 1801 // Decode pixel data into softCursorPixels[]. |
1802 byte pixByte, maskByte; | |
1803 int x, y, n, result; | |
1804 int i = 0; | |
1805 for (y = 0; y < height; y++) { | |
1806 for (x = 0; x < width / 8; x++) { | |
1807 maskByte = maskBuf[y * bytesPerRow + x]; | |
1808 for (n = 7; n >= 0; n--) { | |
1809 if ((maskByte >> n & 1) != 0) { | |
1810 if (bytesPixel == 1) { | |
1811 result = cm8.getRGB(pixBuf[i]); | |
1812 } else { | |
1813 result = 0xFF000000 | |
1814 | (pixBuf[i * 4 + 2] & 0xFF) << 16 | |
1815 | (pixBuf[i * 4 + 1] & 0xFF) << 8 | |
1816 | (pixBuf[i * 4] & 0xFF); | |
1817 } | |
1818 } else { | |
1819 result = 0; // Transparent pixel | |
1820 } | |
1821 softCursorPixels[i++] = result; | |
1822 } | |
1823 } | |
1824 for (n = 7; n >= 8 - width % 8; n--) { | |
1825 if ((maskBuf[y * bytesPerRow + x] >> n & 1) != 0) { | |
13 | 1826 if (bytesPixel == 1) { |
1827 result = cm8.getRGB(pixBuf[i]); | |
1828 } else { | |
1829 result = 0xFF000000 | |
1830 | (pixBuf[i * 4 + 2] & 0xFF) << 16 | |
1831 | (pixBuf[i * 4 + 1] & 0xFF) << 8 | |
1832 | (pixBuf[i * 4] & 0xFF); | |
1833 } | |
1834 } else { | |
1835 result = 0; // Transparent pixel | |
1836 } | |
1837 softCursorPixels[i++] = result; | |
1838 } | |
1839 } | |
15 | 1840 |
13 | 1841 } |
1842 | |
15 | 1843 return new MemoryImageSource(width, height, softCursorPixels, 0, width); |
13 | 1844 } |
1845 | |
15 | 1846 // |
1847 // createSoftCursor(). Assign softCursor new Image (scaled if necessary). | |
1848 // Uses softCursorSource as a source for new cursor image. | |
1849 // | |
1850 | |
1851 synchronized void createSoftCursor() { | |
1852 | |
1853 if (softCursorSource == null) | |
1854 return; | |
13 | 1855 |
15 | 1856 int scaleCursor = viewer.options.scaleCursor; |
1857 if (scaleCursor == 0 || !inputEnabled) | |
1858 scaleCursor = 100; | |
13 | 1859 |
15 | 1860 // Save original cursor coordinates. |
1861 int x = cursorX - hotX; | |
1862 int y = cursorY - hotY; | |
1863 int w = cursorWidth; | |
1864 int h = cursorHeight; | |
13 | 1865 |
15 | 1866 cursorWidth = (origCursorWidth * scaleCursor + 50) / 100; |
1867 cursorHeight = (origCursorHeight * scaleCursor + 50) / 100; | |
1868 hotX = (origHotX * scaleCursor + 50) / 100; | |
1869 hotY = (origHotY * scaleCursor + 50) / 100; | |
1870 softCursor = Toolkit.getDefaultToolkit().createImage(softCursorSource); | |
13 | 1871 |
15 | 1872 if (scaleCursor != 100) { |
1873 softCursor = softCursor.getScaledInstance(cursorWidth, | |
1874 cursorHeight, Image.SCALE_SMOOTH); | |
1875 } | |
13 | 1876 |
15 | 1877 if (showSoftCursor) { |
1878 // Compute screen area to update. | |
1879 x = Math.min(x, cursorX - hotX); | |
1880 y = Math.min(y, cursorY - hotY); | |
1881 w = Math.max(w, cursorWidth); | |
1882 h = Math.max(h, cursorHeight); | |
1883 | |
1884 repaint(viewer.deferCursorUpdates, x, y, w, h); | |
1885 } | |
13 | 1886 } |
1887 | |
15 | 1888 // |
1889 // softCursorMove(). Moves soft cursor into a particular location. | |
1890 // | |
13 | 1891 |
15 | 1892 synchronized void softCursorMove(int x, int y) { |
1893 int oldX = cursorX; | |
1894 int oldY = cursorY; | |
1895 cursorX = x; | |
1896 cursorY = y; | |
1897 if (showSoftCursor) { | |
1898 repaint(viewer.deferCursorUpdates, oldX - hotX, oldY - hotY, | |
1899 cursorWidth, cursorHeight); | |
1900 repaint(viewer.deferCursorUpdates, cursorX - hotX, cursorY - hotY, | |
1901 cursorWidth, cursorHeight); | |
1902 } | |
1903 } | |
1904 | |
1905 // | |
1906 // softCursorFree(). Remove soft cursor, dispose resources. | |
1907 // | |
1908 | |
1909 synchronized void softCursorFree() { | |
1910 if (showSoftCursor) { | |
1911 showSoftCursor = false; | |
1912 softCursor = null; | |
1913 softCursorSource = null; | |
1914 | |
1915 repaint(viewer.deferCursorUpdates, cursorX - hotX, cursorY - hotY, | |
1916 cursorWidth, cursorHeight); | |
1917 } | |
13 | 1918 } |
25 | 1919 |
1920 BufferedImage createBufferedImage(Image img){ | |
1921 BufferedImage bimg = new BufferedImage(img.getWidth(null), img.getHeight(null), BufferedImage.TYPE_INT_RGB ); | |
1922 | |
1923 Graphics g = bimg.getGraphics(); | |
1924 g.drawImage(img, 0, 0, null); | |
1925 g.dispose(); | |
1926 return bimg; | |
1927 } | |
1928 | |
1929 byte[] getBytes(BufferedImage img)throws IOException { | |
1930 byte[] b = getImageBytes(img, "raw"); | |
1931 return b; | |
1932 } | |
1933 | |
1934 byte[] getImageBytes(BufferedImage image, String imageFormat) throws IOException { | |
1935 ByteArrayOutputStream bos = new ByteArrayOutputStream(); | |
1936 BufferedOutputStream os = new BufferedOutputStream(bos); | |
1937 image.flush(); | |
1938 ImageIO.write(image, imageFormat, os); | |
1939 os.flush(); | |
1940 os.close(); | |
1941 return bos.toByteArray(); | |
1942 } | |
1943 | |
1944 | |
13 | 1945 } |