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