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