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();
|
93
|
384
|
|
385 break;
|
|
386 case MyRfbProto.WriteJpegData:
|
|
387 byte[] b = rfb.readJpegData();
|
|
388 drawBufferedImage(b);
|
60
|
389 break;
|
|
390 case RfbProto.FramebufferUpdate:
|
|
391
|
|
392 if (statNumUpdates == viewer.debugStatsExcludeUpdates
|
|
393 && !statsRestarted) {
|
|
394 resetStats();
|
|
395 statsRestarted = true;
|
|
396 } else if (statNumUpdates == viewer.debugStatsMeasureUpdates
|
|
397 && statsRestarted) {
|
|
398 viewer.disconnect();
|
|
399 }
|
|
400
|
|
401 rfb.readFramebufferUpdate();
|
|
402 statNumUpdates++;
|
|
403
|
|
404 boolean cursorPosReceived = false;
|
|
405
|
|
406 for (int i = 0; i < rfb.updateNRects; i++) {
|
|
407
|
|
408 rfb.readFramebufferUpdateRectHdr();
|
|
409 statNumTotalRects++;
|
|
410 int rx = rfb.updateRectX, ry = rfb.updateRectY;
|
|
411 int rw = rfb.updateRectW, rh = rfb.updateRectH;
|
|
412
|
|
413 if (rfb.updateRectEncoding == rfb.EncodingLastRect)
|
|
414 break;
|
|
415
|
|
416 if (rfb.updateRectEncoding == rfb.EncodingNewFBSize) {
|
|
417 rfb.setFramebufferSize(rw, rh);
|
|
418 updateFramebufferSize();
|
|
419 break;
|
|
420 }
|
|
421
|
|
422 if (rfb.updateRectEncoding == rfb.EncodingXCursor
|
|
423 || rfb.updateRectEncoding == rfb.EncodingRichCursor) {
|
|
424 handleCursorShapeUpdate(rfb.updateRectEncoding, rx, ry,
|
|
425 rw, rh);
|
|
426 continue;
|
|
427 }
|
|
428
|
|
429 if (rfb.updateRectEncoding == rfb.EncodingPointerPos) {
|
|
430 softCursorMove(rx, ry);
|
|
431 cursorPosReceived = true;
|
|
432 continue;
|
|
433 }
|
|
434
|
|
435 long numBytesReadBefore = rfb.getNumBytesRead();
|
|
436
|
|
437 rfb.startTiming();
|
|
438
|
|
439 switch (rfb.updateRectEncoding) {
|
|
440 case RfbProto.EncodingRaw:
|
|
441 statNumRectsRaw++;
|
|
442 handleRawRect(rx, ry, rw, rh);
|
|
443 break;
|
|
444 case RfbProto.EncodingCopyRect:
|
|
445 statNumRectsCopy++;
|
|
446 handleCopyRect(rx, ry, rw, rh);
|
|
447 break;
|
|
448 case RfbProto.EncodingRRE:
|
|
449 handleRRERect(rx, ry, rw, rh);
|
|
450 break;
|
|
451 case RfbProto.EncodingCoRRE:
|
|
452 handleCoRRERect(rx, ry, rw, rh);
|
|
453 break;
|
|
454 case RfbProto.EncodingHextile:
|
|
455 statNumRectsHextile++;
|
|
456 handleHextileRect(rx, ry, rw, rh);
|
|
457 break;
|
|
458 case RfbProto.EncodingZRLE:
|
|
459 statNumRectsZRLE++;
|
|
460 handleZRLERect(rx, ry, rw, rh);
|
|
461 break;
|
78
|
462 case RfbProto.EncodingZRLEE:
|
|
463 statNumRectsZRLE++;
|
|
464 handleZRLERect(rx, ry, rw, rh);
|
|
465 break;
|
60
|
466 case RfbProto.EncodingZlib:
|
|
467 handleZlibRect(rx, ry, rw, rh);
|
|
468 break;
|
|
469 case RfbProto.EncodingTight:
|
|
470 statNumRectsTight++;
|
|
471 handleTightRect(rx, ry, rw, rh);
|
|
472 break;
|
|
473 default:
|
|
474 throw new Exception("Unknown RFB rectangle encoding "
|
|
475 + rfb.updateRectEncoding);
|
|
476 }
|
|
477
|
|
478 rfb.stopTiming();
|
65
|
479
|
|
480 long kbitsPerSecond = rfb.kbitsPerSecond();
|
67
|
481 // System.out.println("kbitsPerSecond = " + kbitsPerSecond);
|
60
|
482
|
|
483 statNumPixelRects++;
|
|
484 statNumBytesDecoded += rw * rh * bytesPixel;
|
|
485 statNumBytesEncoded += (int) (rfb.getNumBytesRead() - numBytesReadBefore);
|
|
486 }
|
|
487
|
|
488 boolean fullUpdateNeeded = false;
|
|
489
|
|
490 // Start/stop session recording if necessary. Request full
|
|
491 // update if a new session file was opened.
|
|
492 if (viewer.checkRecordingStatus())
|
|
493 fullUpdateNeeded = true;
|
|
494
|
|
495 // Defer framebuffer update request if necessary. But wake up
|
|
496 // immediately on keyboard or mouse event. Also, don't sleep
|
|
497 // if there is some data to receive, or if the last update
|
|
498 // included a PointerPos message.
|
|
499 if (viewer.deferUpdateRequests > 0 && rfb.available() == 0
|
|
500 && !cursorPosReceived) {
|
|
501 synchronized (rfb) {
|
|
502 try {
|
|
503 rfb.wait(viewer.deferUpdateRequests);
|
|
504 } catch (InterruptedException e) {
|
|
505 }
|
|
506 }
|
|
507 }
|
|
508
|
|
509 viewer.autoSelectEncodings();
|
|
510
|
|
511 // Before requesting framebuffer update, check if the pixel
|
|
512 // format should be changed.
|
|
513 /*
|
|
514 if (viewer.options.eightBitColors != (bytesPixel == 1)) {
|
|
515 // Pixel format should be changed.
|
|
516 setPixelFormat();
|
|
517 fullUpdateNeeded = true;
|
|
518 }
|
|
519 */
|
|
520 // Request framebuffer update if needed.
|
|
521 int w = rfb.framebufferWidth;
|
|
522 int h = rfb.framebufferHeight;
|
|
523 rfb.writeFramebufferUpdateRequest(0, 0, w, h, !fullUpdateNeeded);
|
|
524
|
|
525 break;
|
|
526
|
|
527 case RfbProto.SetColourMapEntries:
|
|
528 throw new Exception("Can't handle SetColourMapEntries message");
|
|
529
|
|
530 case RfbProto.Bell:
|
|
531 Toolkit.getDefaultToolkit().beep();
|
|
532 break;
|
|
533
|
|
534 case RfbProto.ServerCutText:
|
|
535 String s = rfb.readServerCutText();
|
|
536 viewer.clipboard.setCutText(s);
|
|
537 break;
|
|
538
|
|
539 default:
|
|
540 throw new Exception("Unknown RFB message type " + msgType);
|
|
541 }
|
|
542
|
|
543 int bufSize = (int)(rfb.getNumBytesRead() - numBytesRead);
|
|
544 // System.out.println("bufSize="+bufSize);
|
|
545 // rfb.bufResetSend(bufSize);
|
|
546
|
|
547
|
|
548
|
|
549 if(rfb.createBimgFlag){
|
|
550 // bimg = createBufferedImage(rawPixelsImage);
|
|
551 bimg = createBufferedImage(memImage);
|
|
552 //bimg(BufferedImage) -> rfb.pngBytes(byte[])
|
|
553 rfb.createPngBytes(bimg);
|
|
554 rfb.sendPngImage();
|
|
555 rfb.createBimgFlag = false;
|
|
556 }
|
|
557
|
|
558
|
|
559 /*
|
|
560 boolean result = false;
|
|
561 try{
|
|
562 result = ImageIO.write(bimg, "png", new File("sample.png"));
|
|
563 }catch(Exception e){
|
|
564 e.printStackTrace();
|
|
565 result = false;
|
|
566 }
|
|
567 */
|
|
568 }
|
|
569 }
|
|
570
|
|
571 //
|
|
572 // Handle a raw rectangle. The second form with paint==false is used
|
|
573 // by the Hextile decoder for raw-encoded tiles.
|
|
574 //
|
|
575
|
|
576 void handleRawRect(int x, int y, int w, int h) throws IOException {
|
|
577 handleRawRect(x, y, w, h, true);
|
|
578 }
|
|
579
|
|
580 void handleRawRect(int x, int y, int w, int h, boolean paint)
|
|
581 throws IOException {
|
|
582
|
|
583 if (bytesPixel == 1) {
|
|
584 for (int dy = y; dy < y + h; dy++) {
|
|
585 rfb.readFully(pixels8, dy * rfb.framebufferWidth + x, w);
|
|
586 if (rfb.rec != null) {
|
|
587 rfb.rec.write(pixels8, dy * rfb.framebufferWidth + x, w);
|
|
588 }
|
|
589 }
|
|
590 } else {
|
|
591 byte[] buf = new byte[w * 4];
|
|
592 int i, offset;
|
|
593 for (int dy = y; dy < y + h; dy++) {
|
|
594 rfb.readFully(buf);
|
|
595 if (rfb.rec != null) {
|
|
596 rfb.rec.write(buf);
|
|
597 }
|
|
598
|
|
599 offset = dy * rfb.framebufferWidth + x;
|
|
600 for (i = 0; i < w; i++) {
|
|
601 pixels24[offset + i] = (buf[i * 4 + 2] & 0xFF) << 16 |
|
|
602 (buf[i * 4 + 1] & 0xFF) << 8 |
|
|
603 (buf[i * 4] & 0xFF);
|
|
604 }
|
|
605
|
|
606 }
|
|
607 }
|
|
608
|
|
609 handleUpdatedPixels(x, y, w, h);
|
|
610 // if (paint) scheduleRepaint(x, y, w, h);
|
|
611
|
|
612 }
|
|
613
|
|
614 //
|
|
615 // Handle a CopyRect rectangle.
|
|
616 //
|
|
617
|
|
618 void handleCopyRect(int x, int y, int w, int h) throws IOException {
|
|
619
|
|
620 rfb.readCopyRect();
|
|
621 memGraphics.copyArea(rfb.copyRectSrcX, rfb.copyRectSrcY, w, h, x
|
|
622 - rfb.copyRectSrcX, y - rfb.copyRectSrcY);
|
|
623
|
|
624 scheduleRepaint(x, y, w, h);
|
|
625 }
|
|
626
|
|
627 //
|
|
628 // Handle an RRE-encoded rectangle.
|
|
629 //
|
|
630
|
|
631 void handleRRERect(int x, int y, int w, int h) throws IOException {
|
|
632
|
|
633 int nSubrects = rfb.readU32();
|
|
634
|
|
635 byte[] bg_buf = new byte[bytesPixel];
|
|
636 rfb.readFully(bg_buf);
|
|
637 Color pixel;
|
|
638 if (bytesPixel == 1) {
|
|
639 pixel = colors[bg_buf[0] & 0xFF];
|
|
640 } else {
|
|
641 pixel = new Color(bg_buf[2] & 0xFF, bg_buf[1] & 0xFF,
|
|
642 bg_buf[0] & 0xFF);
|
|
643 }
|
|
644 memGraphics.setColor(pixel);
|
|
645 memGraphics.fillRect(x, y, w, h);
|
|
646
|
|
647 byte[] buf = new byte[nSubrects * (bytesPixel + 8)];
|
|
648 rfb.readFully(buf);
|
|
649 DataInputStream ds = new DataInputStream(new ByteArrayInputStream(buf));
|
|
650
|
|
651 if (rfb.rec != null) {
|
|
652 rfb.rec.writeIntBE(nSubrects);
|
|
653 rfb.rec.write(bg_buf);
|
|
654 rfb.rec.write(buf);
|
|
655 }
|
|
656
|
|
657 int sx, sy, sw, sh;
|
|
658
|
|
659 for (int j = 0; j < nSubrects; j++) {
|
|
660 if (bytesPixel == 1) {
|
|
661 pixel = colors[ds.readUnsignedByte()];
|
|
662 } else {
|
|
663 ds.skip(4);
|
|
664 pixel = new Color(buf[j * 12 + 2] & 0xFF,
|
|
665 buf[j * 12 + 1] & 0xFF, buf[j * 12] & 0xFF);
|
|
666 }
|
|
667 sx = x + ds.readUnsignedShort();
|
|
668 sy = y + ds.readUnsignedShort();
|
|
669 sw = ds.readUnsignedShort();
|
|
670 sh = ds.readUnsignedShort();
|
|
671
|
|
672 memGraphics.setColor(pixel);
|
|
673 memGraphics.fillRect(sx, sy, sw, sh);
|
|
674 }
|
|
675
|
|
676 scheduleRepaint(x, y, w, h);
|
|
677 }
|
|
678
|
|
679 //
|
|
680 // Handle a CoRRE-encoded rectangle.
|
|
681 //
|
|
682
|
|
683 void handleCoRRERect(int x, int y, int w, int h) throws IOException {
|
|
684 int nSubrects = rfb.readU32();
|
|
685
|
|
686 byte[] bg_buf = new byte[bytesPixel];
|
|
687 rfb.readFully(bg_buf);
|
|
688 Color pixel;
|
|
689 if (bytesPixel == 1) {
|
|
690 pixel = colors[bg_buf[0] & 0xFF];
|
|
691 } else {
|
|
692 pixel = new Color(bg_buf[2] & 0xFF, bg_buf[1] & 0xFF,
|
|
693 bg_buf[0] & 0xFF);
|
|
694 }
|
|
695 memGraphics.setColor(pixel);
|
|
696 memGraphics.fillRect(x, y, w, h);
|
|
697
|
|
698 byte[] buf = new byte[nSubrects * (bytesPixel + 4)];
|
|
699 rfb.readFully(buf);
|
|
700
|
|
701 if (rfb.rec != null) {
|
|
702 rfb.rec.writeIntBE(nSubrects);
|
|
703 rfb.rec.write(bg_buf);
|
|
704 rfb.rec.write(buf);
|
|
705 }
|
|
706
|
|
707 int sx, sy, sw, sh;
|
|
708 int i = 0;
|
|
709
|
|
710 for (int j = 0; j < nSubrects; j++) {
|
|
711 if (bytesPixel == 1) {
|
|
712 pixel = colors[buf[i++] & 0xFF];
|
|
713 } else {
|
|
714 pixel = new Color(buf[i + 2] & 0xFF, buf[i + 1] & 0xFF,
|
|
715 buf[i] & 0xFF);
|
|
716 i += 4;
|
|
717 }
|
|
718 sx = x + (buf[i++] & 0xFF);
|
|
719 sy = y + (buf[i++] & 0xFF);
|
|
720 sw = buf[i++] & 0xFF;
|
|
721 sh = buf[i++] & 0xFF;
|
|
722
|
|
723 memGraphics.setColor(pixel);
|
|
724 memGraphics.fillRect(sx, sy, sw, sh);
|
|
725 }
|
|
726
|
|
727 scheduleRepaint(x, y, w, h);
|
|
728 }
|
|
729
|
|
730 //
|
|
731 // Handle a Hextile-encoded rectangle.
|
|
732 //
|
|
733
|
|
734 // These colors should be kept between handleHextileSubrect() calls.
|
|
735 private Color hextile_bg, hextile_fg;
|
|
736
|
|
737 void handleHextileRect(int x, int y, int w, int h) throws IOException {
|
|
738
|
|
739 hextile_bg = new Color(0);
|
|
740 hextile_fg = new Color(0);
|
|
741
|
|
742 for (int ty = y; ty < y + h; ty += 16) {
|
|
743 int th = 16;
|
|
744 if (y + h - ty < 16)
|
|
745 th = y + h - ty;
|
|
746
|
|
747 for (int tx = x; tx < x + w; tx += 16) {
|
|
748 int tw = 16;
|
|
749 if (x + w - tx < 16)
|
|
750 tw = x + w - tx;
|
|
751
|
|
752 handleHextileSubrect(tx, ty, tw, th);
|
|
753 }
|
|
754
|
|
755 // Finished with a row of tiles, now let's show it.
|
|
756 scheduleRepaint(x, y, w, h);
|
|
757 }
|
|
758 }
|
|
759
|
|
760 //
|
|
761 // Handle one tile in the Hextile-encoded data.
|
|
762 //
|
|
763
|
|
764 void handleHextileSubrect(int tx, int ty, int tw, int th)
|
|
765 throws IOException {
|
|
766
|
|
767 int subencoding = rfb.readU8();
|
|
768 if (rfb.rec != null) {
|
|
769 rfb.rec.writeByte(subencoding);
|
|
770 }
|
|
771
|
|
772 // Is it a raw-encoded sub-rectangle?
|
|
773 if ((subencoding & rfb.HextileRaw) != 0) {
|
|
774 handleRawRect(tx, ty, tw, th, false);
|
|
775 return;
|
|
776 }
|
|
777
|
|
778 // Read and draw the background if specified.
|
|
779 byte[] cbuf = new byte[bytesPixel];
|
|
780 if ((subencoding & rfb.HextileBackgroundSpecified) != 0) {
|
|
781 rfb.readFully(cbuf);
|
|
782 if (bytesPixel == 1) {
|
|
783 hextile_bg = colors[cbuf[0] & 0xFF];
|
|
784 } else {
|
|
785 hextile_bg = new Color(cbuf[2] & 0xFF, cbuf[1] & 0xFF,
|
|
786 cbuf[0] & 0xFF);
|
|
787 }
|
|
788 if (rfb.rec != null) {
|
|
789 rfb.rec.write(cbuf);
|
|
790 }
|
|
791 }
|
|
792 memGraphics.setColor(hextile_bg);
|
|
793 memGraphics.fillRect(tx, ty, tw, th);
|
|
794
|
|
795 // Read the foreground color if specified.
|
|
796 if ((subencoding & rfb.HextileForegroundSpecified) != 0) {
|
|
797 rfb.readFully(cbuf);
|
|
798 if (bytesPixel == 1) {
|
|
799 hextile_fg = colors[cbuf[0] & 0xFF];
|
|
800 } else {
|
|
801 hextile_fg = new Color(cbuf[2] & 0xFF, cbuf[1] & 0xFF,
|
|
802 cbuf[0] & 0xFF);
|
|
803 }
|
|
804 if (rfb.rec != null) {
|
|
805 rfb.rec.write(cbuf);
|
|
806 }
|
|
807 }
|
|
808
|
|
809 // Done with this tile if there is no sub-rectangles.
|
|
810 if ((subencoding & rfb.HextileAnySubrects) == 0)
|
|
811 return;
|
|
812
|
|
813 int nSubrects = rfb.readU8();
|
|
814 int bufsize = nSubrects * 2;
|
|
815 if ((subencoding & rfb.HextileSubrectsColoured) != 0) {
|
|
816 bufsize += nSubrects * bytesPixel;
|
|
817 }
|
|
818 byte[] buf = new byte[bufsize];
|
|
819 rfb.readFully(buf);
|
|
820 if (rfb.rec != null) {
|
|
821 rfb.rec.writeByte(nSubrects);
|
|
822 rfb.rec.write(buf);
|
|
823 }
|
|
824
|
|
825 int b1, b2, sx, sy, sw, sh;
|
|
826 int i = 0;
|
|
827
|
|
828 if ((subencoding & rfb.HextileSubrectsColoured) == 0) {
|
|
829
|
|
830 // Sub-rectangles are all of the same color.
|
|
831 memGraphics.setColor(hextile_fg);
|
|
832 for (int j = 0; j < nSubrects; j++) {
|
|
833 b1 = buf[i++] & 0xFF;
|
|
834 b2 = buf[i++] & 0xFF;
|
|
835 sx = tx + (b1 >> 4);
|
|
836 sy = ty + (b1 & 0xf);
|
|
837 sw = (b2 >> 4) + 1;
|
|
838 sh = (b2 & 0xf) + 1;
|
|
839 memGraphics.fillRect(sx, sy, sw, sh);
|
|
840 }
|
|
841 } else if (bytesPixel == 1) {
|
|
842
|
|
843 // BGR233 (8-bit color) version for colored sub-rectangles.
|
|
844 for (int j = 0; j < nSubrects; j++) {
|
|
845 hextile_fg = colors[buf[i++] & 0xFF];
|
|
846 b1 = buf[i++] & 0xFF;
|
|
847 b2 = buf[i++] & 0xFF;
|
|
848 sx = tx + (b1 >> 4);
|
|
849 sy = ty + (b1 & 0xf);
|
|
850 sw = (b2 >> 4) + 1;
|
|
851 sh = (b2 & 0xf) + 1;
|
|
852 memGraphics.setColor(hextile_fg);
|
|
853 memGraphics.fillRect(sx, sy, sw, sh);
|
|
854 }
|
|
855
|
|
856 } else {
|
|
857
|
|
858 // Full-color (24-bit) version for colored sub-rectangles.
|
|
859 for (int j = 0; j < nSubrects; j++) {
|
|
860 hextile_fg = new Color(buf[i + 2] & 0xFF, buf[i + 1] & 0xFF,
|
|
861 buf[i] & 0xFF);
|
|
862 i += 4;
|
|
863 b1 = buf[i++] & 0xFF;
|
|
864 b2 = buf[i++] & 0xFF;
|
|
865 sx = tx + (b1 >> 4);
|
|
866 sy = ty + (b1 & 0xf);
|
|
867 sw = (b2 >> 4) + 1;
|
|
868 sh = (b2 & 0xf) + 1;
|
|
869 memGraphics.setColor(hextile_fg);
|
|
870 memGraphics.fillRect(sx, sy, sw, sh);
|
|
871 }
|
|
872
|
|
873 }
|
|
874 }
|
|
875
|
|
876 //
|
|
877 // Handle a ZRLE-encoded rectangle.
|
|
878 //
|
|
879 // FIXME: Currently, session recording is not fully supported for ZRLE.
|
|
880 //
|
|
881
|
|
882 void handleZRLERect(int x, int y, int w, int h) throws Exception {
|
78
|
883 if (rfb.updateRectEncoding==RfbProto.EncodingZRLEE) zrleInStream = null;
|
60
|
884
|
|
885 if (zrleInStream == null)
|
|
886 zrleInStream = new ZlibInStream();
|
|
887
|
|
888 int nBytes = rfb.readU32();
|
|
889 if (nBytes > 64 * 1024 * 1024)
|
|
890 throw new Exception("ZRLE decoder: illegal compressed data size");
|
|
891
|
|
892 if (zrleBuf == null || zrleBufLen < nBytes) {
|
|
893 zrleBufLen = nBytes + 4096;
|
|
894 zrleBuf = new byte[zrleBufLen];
|
|
895 }
|
|
896
|
|
897 // FIXME: Do not wait for all the data before decompression.
|
|
898 rfb.readFully(zrleBuf, 0, nBytes);
|
|
899
|
|
900 if (rfb.rec != null) {
|
|
901 if (rfb.recordFromBeginning) {
|
|
902 rfb.rec.writeIntBE(nBytes);
|
|
903 rfb.rec.write(zrleBuf, 0, nBytes);
|
|
904 } else if (!zrleRecWarningShown) {
|
|
905 System.out.println("Warning: ZRLE session can be recorded"
|
|
906 + " only from the beginning");
|
|
907 System.out.println("Warning: Recorded file may be corrupted");
|
|
908 zrleRecWarningShown = true;
|
|
909 }
|
|
910
|
|
911 }
|
|
912
|
|
913 zrleInStream.setUnderlying(new MemInStream(zrleBuf, 0, nBytes), nBytes);
|
|
914
|
|
915 for (int ty = y; ty < y + h; ty += 64) {
|
|
916
|
|
917 int th = Math.min(y + h - ty, 64);
|
|
918
|
|
919 for (int tx = x; tx < x + w; tx += 64) {
|
|
920
|
|
921 int tw = Math.min(x + w - tx, 64);
|
|
922
|
|
923 int mode = zrleInStream.readU8();
|
|
924 boolean rle = (mode & 128) != 0;
|
|
925 int palSize = mode & 127;
|
|
926 int[] palette = new int[128];
|
|
927
|
|
928 readZrlePalette(palette, palSize);
|
|
929
|
|
930 if (palSize == 1) {
|
|
931 int pix = palette[0];
|
|
932 Color c = (bytesPixel == 1) ? colors[pix] : new Color(
|
|
933 0xFF000000 | pix);
|
|
934 memGraphics.setColor(c);
|
|
935 memGraphics.fillRect(tx, ty, tw, th);
|
|
936 continue;
|
|
937 }
|
|
938
|
|
939 if (!rle) {
|
|
940 if (palSize == 0) {
|
|
941 readZrleRawPixels(tw, th);
|
|
942 } else {
|
|
943 readZrlePackedPixels(tw, th, palette, palSize);
|
|
944 }
|
|
945 } else {
|
|
946 if (palSize == 0) {
|
|
947 readZrlePlainRLEPixels(tw, th);
|
|
948 } else {
|
|
949 readZrlePackedRLEPixels(tw, th, palette);
|
|
950 }
|
|
951 }
|
|
952 handleUpdatedZrleTile(tx, ty, tw, th);
|
|
953 }
|
|
954 }
|
|
955
|
|
956 zrleInStream.reset();
|
|
957
|
|
958 scheduleRepaint(x, y, w, h);
|
|
959 }
|
|
960
|
|
961 int readPixel(InStream is) throws Exception {
|
|
962 int pix;
|
|
963
|
|
964 if (bytesPixel == 1) {
|
|
965
|
|
966 pix = is.readU8();
|
|
967 } else {
|
|
968 int p1 = is.readU8();
|
|
969 int p2 = is.readU8();
|
|
970 int p3 = is.readU8();
|
|
971 pix = (p3 & 0xFF) << 16 | (p2 & 0xFF) << 8 | (p1 & 0xFF);
|
|
972 }
|
|
973 return pix;
|
|
974 }
|
|
975
|
|
976 void readPixels(InStream is, int[] dst, int count) throws Exception {
|
|
977 int pix;
|
|
978 if (bytesPixel == 1) {
|
|
979 byte[] buf = new byte[count];
|
|
980 is.readBytes(buf, 0, count);
|
|
981 for (int i = 0; i < count; i++) {
|
|
982 dst[i] = (int) buf[i] & 0xFF;
|
|
983 }
|
|
984 } else {
|
|
985 byte[] buf = new byte[count * 3];
|
|
986 is.readBytes(buf, 0, count * 3);
|
|
987 for (int i = 0; i < count; i++) {
|
|
988 dst[i] = ((buf[i * 3 + 2] & 0xFF) << 16
|
|
989 | (buf[i * 3 + 1] & 0xFF) << 8 | (buf[i * 3] & 0xFF));
|
|
990 }
|
|
991 }
|
|
992 }
|
|
993
|
|
994 void readZrlePalette(int[] palette, int palSize) throws Exception {
|
|
995 readPixels(zrleInStream, palette, palSize);
|
|
996 }
|
|
997
|
|
998 void readZrleRawPixels(int tw, int th) throws Exception {
|
|
999 if (bytesPixel == 1) {
|
|
1000 zrleInStream.readBytes(zrleTilePixels8, 0, tw * th);
|
|
1001 } else {
|
|
1002 readPixels(zrleInStream, zrleTilePixels24, tw * th); // /
|
|
1003 }
|
|
1004 }
|
|
1005
|
|
1006 void readZrlePackedPixels(int tw, int th, int[] palette, int palSize)
|
|
1007 throws Exception {
|
|
1008
|
|
1009 int bppp = ((palSize > 16) ? 8 : ((palSize > 4) ? 4
|
|
1010 : ((palSize > 2) ? 2 : 1)));
|
|
1011 int ptr = 0;
|
|
1012
|
|
1013 for (int i = 0; i < th; i++) {
|
|
1014 int eol = ptr + tw;
|
|
1015 int b = 0;
|
|
1016 int nbits = 0;
|
|
1017
|
|
1018 while (ptr < eol) {
|
|
1019 if (nbits == 0) {
|
|
1020 b = zrleInStream.readU8();
|
|
1021 nbits = 8;
|
|
1022 }
|
|
1023 nbits -= bppp;
|
|
1024 int index = (b >> nbits) & ((1 << bppp) - 1) & 127;
|
|
1025 if (bytesPixel == 1) {
|
|
1026 zrleTilePixels8[ptr++] = (byte) palette[index];
|
|
1027 } else {
|
|
1028 zrleTilePixels24[ptr++] = palette[index];
|
|
1029 }
|
|
1030 }
|
|
1031 }
|
|
1032 }
|
|
1033
|
|
1034 void readZrlePlainRLEPixels(int tw, int th) throws Exception {
|
|
1035 int ptr = 0;
|
|
1036 int end = ptr + tw * th;
|
|
1037 while (ptr < end) {
|
|
1038 int pix = readPixel(zrleInStream);
|
|
1039 int len = 1;
|
|
1040 int b;
|
|
1041 do {
|
|
1042 b = zrleInStream.readU8();
|
|
1043 len += b;
|
|
1044 } while (b == 255);
|
|
1045
|
|
1046 if (!(len <= end - ptr))
|
|
1047 throw new Exception("ZRLE decoder: assertion failed"
|
|
1048 + " (len <= end-ptr)");
|
|
1049
|
|
1050 if (bytesPixel == 1) {
|
|
1051 while (len-- > 0)
|
|
1052 zrleTilePixels8[ptr++] = (byte) pix;
|
|
1053 } else {
|
|
1054 while (len-- > 0)
|
|
1055 zrleTilePixels24[ptr++] = pix;
|
|
1056 }
|
|
1057 }
|
|
1058 }
|
|
1059
|
|
1060 void readZrlePackedRLEPixels(int tw, int th, int[] palette)
|
|
1061 throws Exception {
|
|
1062
|
|
1063 int ptr = 0;
|
|
1064 int end = ptr + tw * th;
|
|
1065 while (ptr < end) {
|
|
1066 int index = zrleInStream.readU8();
|
|
1067 int len = 1;
|
|
1068 if ((index & 128) != 0) {
|
|
1069 int b;
|
|
1070 do {
|
|
1071 b = zrleInStream.readU8();
|
|
1072 len += b;
|
|
1073 } while (b == 255);
|
|
1074
|
|
1075 if (!(len <= end - ptr))
|
|
1076 throw new Exception("ZRLE decoder: assertion failed"
|
|
1077 + " (len <= end - ptr)");
|
|
1078 }
|
|
1079
|
|
1080 index &= 127;
|
|
1081 int pix = palette[index];
|
|
1082
|
|
1083 if (bytesPixel == 1) {
|
|
1084 while (len-- > 0)
|
|
1085 zrleTilePixels8[ptr++] = (byte) pix;
|
|
1086 } else {
|
|
1087 while (len-- > 0)
|
|
1088 zrleTilePixels24[ptr++] = pix;
|
|
1089 }
|
|
1090 }
|
|
1091 }
|
|
1092
|
|
1093 //
|
|
1094 // Copy pixels from zrleTilePixels8 or zrleTilePixels24, then update.
|
|
1095 //
|
|
1096
|
|
1097 void handleUpdatedZrleTile(int x, int y, int w, int h) {
|
|
1098 Object src, dst;
|
|
1099 if (bytesPixel == 1) {
|
|
1100 src = zrleTilePixels8;
|
|
1101 dst = pixels8;
|
|
1102 } else {
|
|
1103 src = zrleTilePixels24;
|
|
1104 dst = pixels24;
|
|
1105 }
|
|
1106 int offsetSrc = 0;
|
|
1107 int offsetDst = (y * rfb.framebufferWidth + x);
|
|
1108 for (int j = 0; j < h; j++) {
|
|
1109 System.arraycopy(src, offsetSrc, dst, offsetDst, w);
|
|
1110 offsetSrc += w;
|
|
1111 offsetDst += rfb.framebufferWidth;
|
|
1112 }
|
|
1113 handleUpdatedPixels(x, y, w, h);
|
|
1114 }
|
|
1115
|
|
1116 //
|
|
1117 // Handle a Zlib-encoded rectangle.
|
|
1118 //
|
|
1119
|
|
1120 void handleZlibRect(int x, int y, int w, int h) throws Exception {
|
|
1121
|
|
1122 int nBytes = rfb.readU32();
|
|
1123
|
|
1124 if (zlibBuf == null || zlibBufLen < nBytes) {
|
|
1125 zlibBufLen = nBytes * 2;
|
|
1126 zlibBuf = new byte[zlibBufLen];
|
|
1127 }
|
|
1128
|
|
1129 rfb.readFully(zlibBuf, 0, nBytes);
|
|
1130
|
|
1131 if (rfb.rec != null && rfb.recordFromBeginning) {
|
|
1132 rfb.rec.writeIntBE(nBytes);
|
|
1133 rfb.rec.write(zlibBuf, 0, nBytes);
|
|
1134 }
|
|
1135
|
|
1136 if (zlibInflater == null) {
|
|
1137 zlibInflater = new Inflater();
|
|
1138 }
|
|
1139 zlibInflater.setInput(zlibBuf, 0, nBytes);
|
|
1140
|
|
1141 if (bytesPixel == 1) {
|
|
1142 for (int dy = y; dy < y + h; dy++) {
|
|
1143 zlibInflater.inflate(pixels8, dy * rfb.framebufferWidth + x, w);
|
|
1144 if (rfb.rec != null && !rfb.recordFromBeginning)
|
|
1145 rfb.rec.write(pixels8, dy * rfb.framebufferWidth + x, w);
|
|
1146 }
|
|
1147 } else {
|
|
1148 byte[] buf = new byte[w * 4];
|
|
1149 int i, offset;
|
|
1150 for (int dy = y; dy < y + h; dy++) {
|
|
1151 zlibInflater.inflate(buf);
|
|
1152 offset = dy * rfb.framebufferWidth + x;
|
|
1153 for (i = 0; i < w; i++) {
|
|
1154 pixels24[offset + i] = (buf[i * 4 + 2] & 0xFF) << 16
|
|
1155 | (buf[i * 4 + 1] & 0xFF) << 8
|
|
1156 | (buf[i * 4] & 0xFF);
|
|
1157 }
|
|
1158 if (rfb.rec != null && !rfb.recordFromBeginning)
|
|
1159 rfb.rec.write(buf);
|
|
1160 }
|
|
1161 }
|
|
1162
|
|
1163 handleUpdatedPixels(x, y, w, h);
|
|
1164 scheduleRepaint(x, y, w, h);
|
|
1165 }
|
|
1166
|
|
1167 //
|
|
1168 // Handle a Tight-encoded rectangle.
|
|
1169 //
|
|
1170
|
|
1171 void handleTightRect(int x, int y, int w, int h) throws Exception {
|
|
1172
|
|
1173 int comp_ctl = rfb.readU8();
|
|
1174 if (rfb.rec != null) {
|
|
1175 if (rfb.recordFromBeginning || comp_ctl == (rfb.TightFill << 4)
|
|
1176 || comp_ctl == (rfb.TightJpeg << 4)) {
|
|
1177 // Send data exactly as received.
|
|
1178 rfb.rec.writeByte(comp_ctl);
|
|
1179 } else {
|
|
1180 // Tell the decoder to flush each of the four zlib streams.
|
|
1181 rfb.rec.writeByte(comp_ctl | 0x0F);
|
|
1182 }
|
|
1183 }
|
|
1184
|
|
1185 // Flush zlib streams if we are told by the server to do so.
|
|
1186 for (int stream_id = 0; stream_id < 4; stream_id++) {
|
|
1187 if ((comp_ctl & 1) != 0 && tightInflaters[stream_id] != null) {
|
|
1188 tightInflaters[stream_id] = null;
|
|
1189 }
|
|
1190 comp_ctl >>= 1;
|
|
1191 }
|
|
1192
|
|
1193 // Check correctness of subencoding value.
|
|
1194 if (comp_ctl > rfb.TightMaxSubencoding) {
|
|
1195 throw new Exception("Incorrect tight subencoding: " + comp_ctl);
|
|
1196 }
|
|
1197
|
|
1198 // Handle solid-color rectangles.
|
|
1199 if (comp_ctl == rfb.TightFill) {
|
|
1200
|
|
1201 if (bytesPixel == 1) {
|
|
1202 int idx = rfb.readU8();
|
|
1203 memGraphics.setColor(colors[idx]);
|
|
1204 if (rfb.rec != null) {
|
|
1205 rfb.rec.writeByte(idx);
|
|
1206 }
|
|
1207 } else {
|
|
1208 byte[] buf = new byte[3];
|
|
1209 rfb.readFully(buf);
|
|
1210 if (rfb.rec != null) {
|
|
1211 rfb.rec.write(buf);
|
|
1212 }
|
|
1213 Color bg = new Color(0xFF000000 | (buf[0] & 0xFF) << 16
|
|
1214 | (buf[1] & 0xFF) << 8 | (buf[2] & 0xFF));
|
|
1215 memGraphics.setColor(bg);
|
|
1216 }
|
|
1217 memGraphics.fillRect(x, y, w, h);
|
|
1218 scheduleRepaint(x, y, w, h);
|
|
1219 return;
|
|
1220
|
|
1221 }
|
|
1222
|
|
1223 if (comp_ctl == rfb.TightJpeg) {
|
|
1224
|
|
1225 statNumRectsTightJPEG++;
|
|
1226
|
|
1227 // Read JPEG data.
|
|
1228 byte[] jpegData = new byte[rfb.readCompactLen()];
|
|
1229 rfb.readFully(jpegData);
|
|
1230 if (rfb.rec != null) {
|
|
1231 if (!rfb.recordFromBeginning) {
|
|
1232 rfb.recordCompactLen(jpegData.length);
|
|
1233 }
|
|
1234 rfb.rec.write(jpegData);
|
|
1235 }
|
|
1236
|
|
1237 // Create an Image object from the JPEG data.
|
|
1238 Image jpegImage = Toolkit.getDefaultToolkit().createImage(jpegData);
|
|
1239
|
|
1240 // Remember the rectangle where the image should be drawn.
|
|
1241 jpegRect = new Rectangle(x, y, w, h);
|
|
1242
|
|
1243 // Let the imageUpdate() method do the actual drawing, here just
|
|
1244 // wait until the image is fully loaded and drawn.
|
|
1245 synchronized (jpegRect) {
|
|
1246 Toolkit.getDefaultToolkit().prepareImage(jpegImage, -1, -1,
|
|
1247 this);
|
|
1248 try {
|
|
1249 // Wait no longer than three seconds.
|
|
1250 jpegRect.wait(3000);
|
|
1251 } catch (InterruptedException e) {
|
|
1252 throw new Exception("Interrupted while decoding JPEG image");
|
|
1253 }
|
|
1254 }
|
|
1255
|
|
1256 // Done, jpegRect is not needed any more.
|
|
1257 jpegRect = null;
|
|
1258 return;
|
|
1259
|
|
1260 }
|
|
1261
|
|
1262 // Read filter id and parameters.
|
|
1263 int numColors = 0, rowSize = w;
|
|
1264 byte[] palette8 = new byte[2];
|
|
1265 int[] palette24 = new int[256];
|
|
1266 boolean useGradient = false;
|
|
1267 if ((comp_ctl & rfb.TightExplicitFilter) != 0) {
|
|
1268 int filter_id = rfb.readU8();
|
|
1269 if (rfb.rec != null) {
|
|
1270 rfb.rec.writeByte(filter_id);
|
|
1271 }
|
|
1272 if (filter_id == rfb.TightFilterPalette) {
|
|
1273 numColors = rfb.readU8() + 1;
|
|
1274 if (rfb.rec != null) {
|
|
1275 rfb.rec.writeByte(numColors - 1);
|
|
1276 }
|
|
1277 if (bytesPixel == 1) {
|
|
1278 if (numColors != 2) {
|
|
1279 throw new Exception("Incorrect tight palette size: "
|
|
1280 + numColors);
|
|
1281 }
|
|
1282 rfb.readFully(palette8);
|
|
1283 if (rfb.rec != null) {
|
|
1284 rfb.rec.write(palette8);
|
|
1285 }
|
|
1286 } else {
|
|
1287 byte[] buf = new byte[numColors * 3];
|
|
1288 rfb.readFully(buf);
|
|
1289 if (rfb.rec != null) {
|
|
1290 rfb.rec.write(buf);
|
|
1291 }
|
|
1292 for (int i = 0; i < numColors; i++) {
|
|
1293 palette24[i] = ((buf[i * 3] & 0xFF) << 16
|
|
1294 | (buf[i * 3 + 1] & 0xFF) << 8 | (buf[i * 3 + 2] & 0xFF));
|
|
1295 }
|
|
1296 }
|
|
1297 if (numColors == 2)
|
|
1298 rowSize = (w + 7) / 8;
|
|
1299 } else if (filter_id == rfb.TightFilterGradient) {
|
|
1300 useGradient = true;
|
|
1301 } else if (filter_id != rfb.TightFilterCopy) {
|
|
1302 throw new Exception("Incorrect tight filter id: " + filter_id);
|
|
1303 }
|
|
1304 }
|
|
1305 if (numColors == 0 && bytesPixel == 4)
|
|
1306 rowSize *= 3;
|
|
1307
|
|
1308 // Read, optionally uncompress and decode data.
|
|
1309 int dataSize = h * rowSize;
|
|
1310 if (dataSize < rfb.TightMinToCompress) {
|
|
1311 // Data size is small - not compressed with zlib.
|
|
1312 if (numColors != 0) {
|
|
1313 // Indexed colors.
|
|
1314 byte[] indexedData = new byte[dataSize];
|
|
1315 rfb.readFully(indexedData);
|
|
1316 if (rfb.rec != null) {
|
|
1317 rfb.rec.write(indexedData);
|
|
1318 }
|
|
1319 if (numColors == 2) {
|
|
1320 // Two colors.
|
|
1321 if (bytesPixel == 1) {
|
|
1322 decodeMonoData(x, y, w, h, indexedData, palette8);
|
|
1323 } else {
|
|
1324 decodeMonoData(x, y, w, h, indexedData, palette24);
|
|
1325 }
|
|
1326 } else {
|
|
1327 // 3..255 colors (assuming bytesPixel == 4).
|
|
1328 int i = 0;
|
|
1329 for (int dy = y; dy < y + h; dy++) {
|
|
1330 for (int dx = x; dx < x + w; dx++) {
|
|
1331 pixels24[dy * rfb.framebufferWidth + dx] = palette24[indexedData[i++] & 0xFF];
|
|
1332 }
|
|
1333 }
|
|
1334 }
|
|
1335 } else if (useGradient) {
|
|
1336 // "Gradient"-processed data
|
|
1337 byte[] buf = new byte[w * h * 3];
|
|
1338 rfb.readFully(buf);
|
|
1339 if (rfb.rec != null) {
|
|
1340 rfb.rec.write(buf);
|
|
1341 }
|
|
1342 decodeGradientData(x, y, w, h, buf);
|
|
1343 } else {
|
|
1344 // Raw truecolor data.
|
|
1345 if (bytesPixel == 1) {
|
|
1346 for (int dy = y; dy < y + h; dy++) {
|
|
1347 rfb.readFully(pixels8, dy * rfb.framebufferWidth + x, w);
|
|
1348 if (rfb.rec != null) {
|
|
1349 rfb.rec.write(pixels8, dy * rfb.framebufferWidth
|
|
1350 + x, w);
|
|
1351 }
|
|
1352 }
|
|
1353 } else {
|
|
1354 byte[] buf = new byte[w * 3];
|
|
1355 int i, offset;
|
|
1356 for (int dy = y; dy < y + h; dy++) {
|
|
1357 rfb.readFully(buf);
|
|
1358 if (rfb.rec != null) {
|
|
1359 rfb.rec.write(buf);
|
|
1360 }
|
|
1361 offset = dy * rfb.framebufferWidth + x;
|
|
1362 for (i = 0; i < w; i++) {
|
|
1363 pixels24[offset + i] = (buf[i * 3] & 0xFF) << 16
|
|
1364 | (buf[i * 3 + 1] & 0xFF) << 8
|
|
1365 | (buf[i * 3 + 2] & 0xFF);
|
|
1366 }
|
|
1367 }
|
|
1368 }
|
|
1369 }
|
|
1370 } else {
|
|
1371 // Data was compressed with zlib.
|
|
1372 int zlibDataLen = rfb.readCompactLen();
|
|
1373 byte[] zlibData = new byte[zlibDataLen];
|
|
1374 rfb.readFully(zlibData);
|
|
1375 if (rfb.rec != null && rfb.recordFromBeginning) {
|
|
1376 rfb.rec.write(zlibData);
|
|
1377 }
|
|
1378 int stream_id = comp_ctl & 0x03;
|
|
1379 if (tightInflaters[stream_id] == null) {
|
|
1380 tightInflaters[stream_id] = new Inflater();
|
|
1381 }
|
|
1382 Inflater myInflater = tightInflaters[stream_id];
|
|
1383 myInflater.setInput(zlibData);
|
|
1384 byte[] buf = new byte[dataSize];
|
|
1385 myInflater.inflate(buf);
|
|
1386 if (rfb.rec != null && !rfb.recordFromBeginning) {
|
|
1387 rfb.recordCompressedData(buf);
|
|
1388 }
|
|
1389
|
|
1390 if (numColors != 0) {
|
|
1391 // Indexed colors.
|
|
1392 if (numColors == 2) {
|
|
1393 // Two colors.
|
|
1394 if (bytesPixel == 1) {
|
|
1395 decodeMonoData(x, y, w, h, buf, palette8);
|
|
1396 } else {
|
|
1397 decodeMonoData(x, y, w, h, buf, palette24);
|
|
1398 }
|
|
1399 } else {
|
|
1400 // More than two colors (assuming bytesPixel == 4).
|
|
1401 int i = 0;
|
|
1402 for (int dy = y; dy < y + h; dy++) {
|
|
1403 for (int dx = x; dx < x + w; dx++) {
|
|
1404 pixels24[dy * rfb.framebufferWidth + dx] = palette24[buf[i++] & 0xFF];
|
|
1405 }
|
|
1406 }
|
|
1407 }
|
|
1408 } else if (useGradient) {
|
|
1409 // Compressed "Gradient"-filtered data (assuming bytesPixel ==
|
|
1410 // 4).
|
|
1411 decodeGradientData(x, y, w, h, buf);
|
|
1412 } else {
|
|
1413 // Compressed truecolor data.
|
|
1414 if (bytesPixel == 1) {
|
|
1415 int destOffset = y * rfb.framebufferWidth + x;
|
|
1416 for (int dy = 0; dy < h; dy++) {
|
|
1417 System.arraycopy(buf, dy * w, pixels8, destOffset, w);
|
|
1418 destOffset += rfb.framebufferWidth;
|
|
1419 }
|
|
1420 } else {
|
|
1421 int srcOffset = 0;
|
|
1422 int destOffset, i;
|
|
1423 for (int dy = 0; dy < h; dy++) {
|
|
1424 myInflater.inflate(buf);
|
|
1425 destOffset = (y + dy) * rfb.framebufferWidth + x;
|
|
1426 for (i = 0; i < w; i++) {
|
|
1427 pixels24[destOffset + i] = (buf[srcOffset] & 0xFF) << 16
|
|
1428 | (buf[srcOffset + 1] & 0xFF) << 8
|
|
1429 | (buf[srcOffset + 2] & 0xFF);
|
|
1430 srcOffset += 3;
|
|
1431 }
|
|
1432 }
|
|
1433 }
|
|
1434 }
|
|
1435 }
|
|
1436
|
|
1437 handleUpdatedPixels(x, y, w, h);
|
|
1438 scheduleRepaint(x, y, w, h);
|
|
1439 }
|
|
1440
|
|
1441 //
|
|
1442 // Decode 1bpp-encoded bi-color rectangle (8-bit and 24-bit versions).
|
|
1443 //
|
|
1444
|
|
1445 void decodeMonoData(int x, int y, int w, int h, byte[] src, byte[] palette) {
|
|
1446
|
|
1447 int dx, dy, n;
|
|
1448 int i = y * rfb.framebufferWidth + x;
|
|
1449 int rowBytes = (w + 7) / 8;
|
|
1450 byte b;
|
|
1451
|
|
1452 for (dy = 0; dy < h; dy++) {
|
|
1453 for (dx = 0; dx < w / 8; dx++) {
|
|
1454 b = src[dy * rowBytes + dx];
|
|
1455 for (n = 7; n >= 0; n--)
|
|
1456 pixels8[i++] = palette[b >> n & 1];
|
|
1457 }
|
|
1458 for (n = 7; n >= 8 - w % 8; n--) {
|
|
1459 pixels8[i++] = palette[src[dy * rowBytes + dx] >> n & 1];
|
|
1460 }
|
|
1461 i += (rfb.framebufferWidth - w);
|
|
1462 }
|
|
1463 }
|
|
1464
|
|
1465 void decodeMonoData(int x, int y, int w, int h, byte[] src, int[] palette) {
|
|
1466
|
|
1467 int dx, dy, n;
|
|
1468 int i = y * rfb.framebufferWidth + x;
|
|
1469 int rowBytes = (w + 7) / 8;
|
|
1470 byte b;
|
|
1471
|
|
1472 for (dy = 0; dy < h; dy++) {
|
|
1473 for (dx = 0; dx < w / 8; dx++) {
|
|
1474 b = src[dy * rowBytes + dx];
|
|
1475 for (n = 7; n >= 0; n--)
|
|
1476 pixels24[i++] = palette[b >> n & 1];
|
|
1477 }
|
|
1478 for (n = 7; n >= 8 - w % 8; n--) {
|
|
1479 pixels24[i++] = palette[src[dy * rowBytes + dx] >> n & 1];
|
|
1480 }
|
|
1481 i += (rfb.framebufferWidth - w);
|
|
1482 }
|
|
1483 }
|
|
1484
|
|
1485 //
|
|
1486 // Decode data processed with the "Gradient" filter.
|
|
1487 //
|
|
1488
|
|
1489 void decodeGradientData(int x, int y, int w, int h, byte[] buf) {
|
|
1490
|
|
1491 int dx, dy, c;
|
|
1492 byte[] prevRow = new byte[w * 3];
|
|
1493 byte[] thisRow = new byte[w * 3];
|
|
1494 byte[] pix = new byte[3];
|
|
1495 int[] est = new int[3];
|
|
1496
|
|
1497 int offset = y * rfb.framebufferWidth + x;
|
|
1498
|
|
1499 for (dy = 0; dy < h; dy++) {
|
|
1500
|
|
1501 /* First pixel in a row */
|
|
1502 for (c = 0; c < 3; c++) {
|
|
1503 pix[c] = (byte) (prevRow[c] + buf[dy * w * 3 + c]);
|
|
1504 thisRow[c] = pix[c];
|
|
1505 }
|
|
1506 pixels24[offset++] = (pix[0] & 0xFF) << 16 | (pix[1] & 0xFF) << 8
|
|
1507 | (pix[2] & 0xFF);
|
|
1508
|
|
1509 /* Remaining pixels of a row */
|
|
1510 for (dx = 1; dx < w; dx++) {
|
|
1511 for (c = 0; c < 3; c++) {
|
|
1512 est[c] = ((prevRow[dx * 3 + c] & 0xFF) + (pix[c] & 0xFF) - (prevRow[(dx - 1)
|
|
1513 * 3 + c] & 0xFF));
|
|
1514 if (est[c] > 0xFF) {
|
|
1515 est[c] = 0xFF;
|
|
1516 } else if (est[c] < 0x00) {
|
|
1517 est[c] = 0x00;
|
|
1518 }
|
|
1519 pix[c] = (byte) (est[c] + buf[(dy * w + dx) * 3 + c]);
|
|
1520 thisRow[dx * 3 + c] = pix[c];
|
|
1521 }
|
|
1522 pixels24[offset++] = (pix[0] & 0xFF) << 16
|
|
1523 | (pix[1] & 0xFF) << 8 | (pix[2] & 0xFF);
|
|
1524 }
|
|
1525
|
|
1526 System.arraycopy(thisRow, 0, prevRow, 0, w * 3);
|
|
1527 offset += (rfb.framebufferWidth - w);
|
|
1528 }
|
|
1529 }
|
|
1530
|
|
1531 //
|
|
1532 // Display newly updated area of pixels.
|
|
1533 //
|
|
1534
|
|
1535 void handleUpdatedPixels(int x, int y, int w, int h) {
|
|
1536
|
|
1537 // Draw updated pixels of the off-screen image.
|
|
1538
|
|
1539 pixelsSource.newPixels(x, y, w, h);
|
|
1540 memGraphics.setClip(x, y, w, h);
|
|
1541 memGraphics.drawImage(rawPixelsImage, 0, 0, null);
|
|
1542 memGraphics.setClip(0, 0, rfb.framebufferWidth, rfb.framebufferHeight);
|
|
1543
|
|
1544 }
|
|
1545
|
|
1546 //
|
|
1547 // Tell JVM to repaint specified desktop area.
|
|
1548 //
|
|
1549
|
|
1550 void scheduleRepaint(int x, int y, int w, int h) {
|
|
1551 // Request repaint, deferred if necessary.
|
|
1552 if (rfb.framebufferWidth == scaledWidth) {
|
|
1553 repaint(viewer.deferScreenUpdates, x, y, w, h);
|
|
1554 } else {
|
|
1555 int sx = x * scalingFactor / 100;
|
|
1556 int sy = y * scalingFactor / 100;
|
|
1557 int sw = ((x + w) * scalingFactor + 49) / 100 - sx + 1;
|
|
1558 int sh = ((y + h) * scalingFactor + 49) / 100 - sy + 1;
|
|
1559 repaint(viewer.deferScreenUpdates, sx, sy, sw, sh);
|
|
1560 }
|
|
1561 }
|
|
1562
|
|
1563 //
|
|
1564 // Handle events.
|
|
1565 //
|
|
1566
|
|
1567 public void keyPressed(KeyEvent evt) {
|
|
1568 processLocalKeyEvent(evt);
|
|
1569 }
|
|
1570
|
|
1571 public void keyReleased(KeyEvent evt) {
|
|
1572 processLocalKeyEvent(evt);
|
|
1573 }
|
|
1574
|
|
1575 public void keyTyped(KeyEvent evt) {
|
|
1576 evt.consume();
|
|
1577 }
|
|
1578
|
|
1579 public void mousePressed(MouseEvent evt) {
|
|
1580 processLocalMouseEvent(evt, false);
|
|
1581 }
|
|
1582
|
|
1583 public void mouseReleased(MouseEvent evt) {
|
|
1584 processLocalMouseEvent(evt, false);
|
|
1585 }
|
|
1586
|
|
1587 public void mouseMoved(MouseEvent evt) {
|
|
1588 processLocalMouseEvent(evt, true);
|
|
1589 }
|
|
1590
|
|
1591 public void mouseDragged(MouseEvent evt) {
|
|
1592 processLocalMouseEvent(evt, true);
|
|
1593 }
|
|
1594
|
|
1595 public void processLocalKeyEvent(KeyEvent evt) {
|
|
1596 if (viewer.rfb != null && rfb.inNormalProtocol) {
|
|
1597 if (!inputEnabled) {
|
|
1598 if ((evt.getKeyChar() == 'r' || evt.getKeyChar() == 'R')
|
|
1599 && evt.getID() == KeyEvent.KEY_PRESSED) {
|
|
1600 // Request screen update.
|
|
1601 try {
|
|
1602 rfb.writeFramebufferUpdateRequest(0, 0,
|
|
1603 rfb.framebufferWidth, rfb.framebufferHeight,
|
|
1604 false);
|
|
1605 } catch (IOException e) {
|
|
1606 e.printStackTrace();
|
|
1607 }
|
|
1608 }
|
|
1609 } else {
|
|
1610 // Input enabled.
|
|
1611 synchronized (rfb) {
|
|
1612 try {
|
|
1613 rfb.writeKeyEvent(evt);
|
|
1614 } catch (Exception e) {
|
|
1615 e.printStackTrace();
|
|
1616 }
|
|
1617 rfb.notify();
|
|
1618 }
|
|
1619 }
|
|
1620 }
|
|
1621 // Don't ever pass keyboard events to AWT for default processing.
|
|
1622 // Otherwise, pressing Tab would switch focus to ButtonPanel etc.
|
|
1623 evt.consume();
|
|
1624 }
|
|
1625
|
|
1626 public void processLocalMouseEvent(MouseEvent evt, boolean moved) {
|
|
1627 if (viewer.rfb != null && rfb.inNormalProtocol) {
|
|
1628 if (moved) {
|
|
1629 softCursorMove(evt.getX(), evt.getY());
|
|
1630 }
|
|
1631 if (rfb.framebufferWidth != scaledWidth) {
|
|
1632 int sx = (evt.getX() * 100 + scalingFactor / 2) / scalingFactor;
|
|
1633 int sy = (evt.getY() * 100 + scalingFactor / 2) / scalingFactor;
|
|
1634 evt.translatePoint(sx - evt.getX(), sy - evt.getY());
|
|
1635 }
|
|
1636 synchronized (rfb) {
|
|
1637 try {
|
|
1638 rfb.writePointerEvent(evt);
|
|
1639 } catch (Exception e) {
|
|
1640 e.printStackTrace();
|
|
1641 }
|
|
1642 rfb.notify();
|
|
1643 }
|
|
1644 }
|
|
1645 }
|
|
1646
|
|
1647 //
|
|
1648 // Ignored events.
|
|
1649 //
|
|
1650
|
|
1651 public void mouseClicked(MouseEvent evt) {
|
|
1652 }
|
|
1653
|
|
1654 public void mouseEntered(MouseEvent evt) {
|
|
1655 }
|
|
1656
|
|
1657 public void mouseExited(MouseEvent evt) {
|
|
1658 }
|
|
1659
|
|
1660 //
|
|
1661 // Reset update statistics.
|
|
1662 //
|
|
1663
|
|
1664 void resetStats() {
|
|
1665 statStartTime = System.currentTimeMillis();
|
|
1666 statNumUpdates = 0;
|
|
1667 statNumTotalRects = 0;
|
|
1668 statNumPixelRects = 0;
|
|
1669 statNumRectsTight = 0;
|
|
1670 statNumRectsTightJPEG = 0;
|
|
1671 statNumRectsZRLE = 0;
|
|
1672 statNumRectsHextile = 0;
|
|
1673 statNumRectsRaw = 0;
|
|
1674 statNumRectsCopy = 0;
|
|
1675 statNumBytesEncoded = 0;
|
|
1676 statNumBytesDecoded = 0;
|
|
1677 }
|
|
1678
|
|
1679 // ////////////////////////////////////////////////////////////////
|
|
1680 //
|
|
1681 // Handle cursor shape updates (XCursor and RichCursor encodings).
|
|
1682 //
|
|
1683
|
|
1684 boolean showSoftCursor = false;
|
|
1685
|
|
1686 MemoryImageSource softCursorSource;
|
|
1687 Image softCursor;
|
|
1688
|
|
1689 int cursorX = 0, cursorY = 0;
|
|
1690 int cursorWidth, cursorHeight;
|
|
1691 int origCursorWidth, origCursorHeight;
|
|
1692 int hotX, hotY;
|
|
1693 int origHotX, origHotY;
|
|
1694
|
|
1695 //
|
|
1696 // Handle cursor shape update (XCursor and RichCursor encodings).
|
|
1697 //
|
|
1698
|
|
1699 synchronized void handleCursorShapeUpdate(int encodingType, int xhot,
|
|
1700 int yhot, int width, int height) throws IOException {
|
|
1701
|
|
1702 softCursorFree();
|
|
1703
|
|
1704 if (width * height == 0)
|
|
1705 return;
|
|
1706
|
|
1707 // Ignore cursor shape data if requested by user.
|
|
1708 if (viewer.options.ignoreCursorUpdates) {
|
|
1709 int bytesPerRow = (width + 7) / 8;
|
|
1710 int bytesMaskData = bytesPerRow * height;
|
|
1711
|
|
1712 if (encodingType == rfb.EncodingXCursor) {
|
|
1713 rfb.skipBytes(6 + bytesMaskData * 2);
|
|
1714 } else {
|
|
1715 // rfb.EncodingRichCursor
|
|
1716 rfb.skipBytes(width * height * bytesPixel + bytesMaskData);
|
|
1717 }
|
|
1718 return;
|
|
1719 }
|
|
1720
|
|
1721 // Decode cursor pixel data.
|
|
1722 softCursorSource = decodeCursorShape(encodingType, width, height);
|
|
1723
|
|
1724 // Set original (non-scaled) cursor dimensions.
|
|
1725 origCursorWidth = width;
|
|
1726 origCursorHeight = height;
|
|
1727 origHotX = xhot;
|
|
1728 origHotY = yhot;
|
|
1729
|
|
1730 // Create off-screen cursor image.
|
|
1731 createSoftCursor();
|
|
1732
|
|
1733 // Show the cursor.
|
|
1734 showSoftCursor = true;
|
|
1735 repaint(viewer.deferCursorUpdates, cursorX - hotX, cursorY - hotY,
|
|
1736 cursorWidth, cursorHeight);
|
|
1737 }
|
|
1738
|
|
1739 //
|
|
1740 // decodeCursorShape(). Decode cursor pixel data and return
|
|
1741 // corresponding MemoryImageSource instance.
|
|
1742 //
|
|
1743
|
|
1744 synchronized MemoryImageSource decodeCursorShape(int encodingType,
|
|
1745 int width, int height) throws IOException {
|
|
1746
|
|
1747 int bytesPerRow = (width + 7) / 8;
|
|
1748 int bytesMaskData = bytesPerRow * height;
|
|
1749
|
|
1750 int[] softCursorPixels = new int[width * height];
|
|
1751
|
|
1752 if (encodingType == rfb.EncodingXCursor) {
|
|
1753
|
|
1754 // Read foreground and background colors of the cursor.
|
|
1755 byte[] rgb = new byte[6];
|
|
1756 rfb.readFully(rgb);
|
|
1757 int[] colors = {
|
|
1758 (0xFF000000 | (rgb[3] & 0xFF) << 16 | (rgb[4] & 0xFF) << 8 | (rgb[5] & 0xFF)),
|
|
1759 (0xFF000000 | (rgb[0] & 0xFF) << 16 | (rgb[1] & 0xFF) << 8 | (rgb[2] & 0xFF)) };
|
|
1760
|
|
1761 // Read pixel and mask data.
|
|
1762 byte[] pixBuf = new byte[bytesMaskData];
|
|
1763 rfb.readFully(pixBuf);
|
|
1764 byte[] maskBuf = new byte[bytesMaskData];
|
|
1765 rfb.readFully(maskBuf);
|
|
1766
|
|
1767 // Decode pixel data into softCursorPixels[].
|
|
1768 byte pixByte, maskByte;
|
|
1769 int x, y, n, result;
|
|
1770 int i = 0;
|
|
1771 for (y = 0; y < height; y++) {
|
|
1772 for (x = 0; x < width / 8; x++) {
|
|
1773 pixByte = pixBuf[y * bytesPerRow + x];
|
|
1774 maskByte = maskBuf[y * bytesPerRow + x];
|
|
1775 for (n = 7; n >= 0; n--) {
|
|
1776 if ((maskByte >> n & 1) != 0) {
|
|
1777 result = colors[pixByte >> n & 1];
|
|
1778 } else {
|
|
1779 result = 0; // Transparent pixel
|
|
1780 }
|
|
1781 softCursorPixels[i++] = result;
|
|
1782 }
|
|
1783 }
|
|
1784 for (n = 7; n >= 8 - width % 8; n--) {
|
|
1785 if ((maskBuf[y * bytesPerRow + x] >> n & 1) != 0) {
|
|
1786 result = colors[pixBuf[y * bytesPerRow + x] >> n & 1];
|
|
1787 } else {
|
|
1788 result = 0; // Transparent pixel
|
|
1789 }
|
|
1790 softCursorPixels[i++] = result;
|
|
1791 }
|
|
1792 }
|
|
1793
|
|
1794 } else {
|
|
1795 // encodingType == rfb.EncodingRichCursor
|
|
1796
|
|
1797 // Read pixel and mask data.
|
|
1798 byte[] pixBuf = new byte[width * height * bytesPixel];
|
|
1799 rfb.readFully(pixBuf);
|
|
1800 byte[] maskBuf = new byte[bytesMaskData];
|
|
1801 rfb.readFully(maskBuf);
|
|
1802
|
|
1803 // Decode pixel data into softCursorPixels[].
|
|
1804 byte pixByte, maskByte;
|
|
1805 int x, y, n, result;
|
|
1806 int i = 0;
|
|
1807 for (y = 0; y < height; y++) {
|
|
1808 for (x = 0; x < width / 8; x++) {
|
|
1809 maskByte = maskBuf[y * bytesPerRow + x];
|
|
1810 for (n = 7; n >= 0; n--) {
|
|
1811 if ((maskByte >> n & 1) != 0) {
|
|
1812 if (bytesPixel == 1) {
|
|
1813 result = cm8.getRGB(pixBuf[i]);
|
|
1814 } else {
|
|
1815 result = 0xFF000000
|
|
1816 | (pixBuf[i * 4 + 2] & 0xFF) << 16
|
|
1817 | (pixBuf[i * 4 + 1] & 0xFF) << 8
|
|
1818 | (pixBuf[i * 4] & 0xFF);
|
|
1819 }
|
|
1820 } else {
|
|
1821 result = 0; // Transparent pixel
|
|
1822 }
|
|
1823 softCursorPixels[i++] = result;
|
|
1824 }
|
|
1825 }
|
|
1826 for (n = 7; n >= 8 - width % 8; n--) {
|
|
1827 if ((maskBuf[y * bytesPerRow + x] >> n & 1) != 0) {
|
|
1828 if (bytesPixel == 1) {
|
|
1829 result = cm8.getRGB(pixBuf[i]);
|
|
1830 } else {
|
|
1831 result = 0xFF000000
|
|
1832 | (pixBuf[i * 4 + 2] & 0xFF) << 16
|
|
1833 | (pixBuf[i * 4 + 1] & 0xFF) << 8
|
|
1834 | (pixBuf[i * 4] & 0xFF);
|
|
1835 }
|
|
1836 } else {
|
|
1837 result = 0; // Transparent pixel
|
|
1838 }
|
|
1839 softCursorPixels[i++] = result;
|
|
1840 }
|
|
1841 }
|
|
1842
|
|
1843 }
|
|
1844
|
|
1845 return new MemoryImageSource(width, height, softCursorPixels, 0, width);
|
|
1846 }
|
|
1847
|
|
1848 //
|
|
1849 // createSoftCursor(). Assign softCursor new Image (scaled if necessary).
|
|
1850 // Uses softCursorSource as a source for new cursor image.
|
|
1851 //
|
|
1852
|
|
1853 synchronized void createSoftCursor() {
|
|
1854
|
|
1855 if (softCursorSource == null)
|
|
1856 return;
|
|
1857
|
|
1858 int scaleCursor = viewer.options.scaleCursor;
|
|
1859 if (scaleCursor == 0 || !inputEnabled)
|
|
1860 scaleCursor = 100;
|
|
1861
|
|
1862 // Save original cursor coordinates.
|
|
1863 int x = cursorX - hotX;
|
|
1864 int y = cursorY - hotY;
|
|
1865 int w = cursorWidth;
|
|
1866 int h = cursorHeight;
|
|
1867
|
|
1868 cursorWidth = (origCursorWidth * scaleCursor + 50) / 100;
|
|
1869 cursorHeight = (origCursorHeight * scaleCursor + 50) / 100;
|
|
1870 hotX = (origHotX * scaleCursor + 50) / 100;
|
|
1871 hotY = (origHotY * scaleCursor + 50) / 100;
|
|
1872 softCursor = Toolkit.getDefaultToolkit().createImage(softCursorSource);
|
|
1873
|
|
1874 if (scaleCursor != 100) {
|
|
1875 softCursor = softCursor.getScaledInstance(cursorWidth,
|
|
1876 cursorHeight, Image.SCALE_SMOOTH);
|
|
1877 }
|
|
1878
|
|
1879 if (showSoftCursor) {
|
|
1880 // Compute screen area to update.
|
|
1881 x = Math.min(x, cursorX - hotX);
|
|
1882 y = Math.min(y, cursorY - hotY);
|
|
1883 w = Math.max(w, cursorWidth);
|
|
1884 h = Math.max(h, cursorHeight);
|
|
1885
|
|
1886 repaint(viewer.deferCursorUpdates, x, y, w, h);
|
|
1887 }
|
|
1888 }
|
|
1889
|
|
1890 //
|
|
1891 // softCursorMove(). Moves soft cursor into a particular location.
|
|
1892 //
|
|
1893
|
|
1894 synchronized void softCursorMove(int x, int y) {
|
|
1895 int oldX = cursorX;
|
|
1896 int oldY = cursorY;
|
|
1897 cursorX = x;
|
|
1898 cursorY = y;
|
|
1899 if (showSoftCursor) {
|
|
1900 repaint(viewer.deferCursorUpdates, oldX - hotX, oldY - hotY,
|
|
1901 cursorWidth, cursorHeight);
|
|
1902 repaint(viewer.deferCursorUpdates, cursorX - hotX, cursorY - hotY,
|
|
1903 cursorWidth, cursorHeight);
|
|
1904 }
|
|
1905 }
|
|
1906
|
|
1907 //
|
|
1908 // softCursorFree(). Remove soft cursor, dispose resources.
|
|
1909 //
|
|
1910
|
|
1911 synchronized void softCursorFree() {
|
|
1912 if (showSoftCursor) {
|
|
1913 showSoftCursor = false;
|
|
1914 softCursor = null;
|
|
1915 softCursorSource = null;
|
|
1916
|
|
1917 repaint(viewer.deferCursorUpdates, cursorX - hotX, cursorY - hotY,
|
|
1918 cursorWidth, cursorHeight);
|
|
1919 }
|
|
1920 }
|
|
1921
|
|
1922 BufferedImage createBufferedImage(Image img){
|
|
1923 BufferedImage bimg = new BufferedImage(img.getWidth(null), img.getHeight(null), BufferedImage.TYPE_INT_RGB );
|
|
1924
|
|
1925 Graphics g = bimg.getGraphics();
|
|
1926 g.drawImage(img, 0, 0, null);
|
|
1927 g.dispose();
|
|
1928 return bimg;
|
|
1929 }
|
|
1930
|
|
1931 byte[] getBytes(BufferedImage img)throws IOException {
|
|
1932 byte[] b = getImageBytes(img, "raw");
|
|
1933 return b;
|
|
1934 }
|
|
1935
|
|
1936 byte[] getImageBytes(BufferedImage image, String imageFormat) throws IOException {
|
|
1937 ByteArrayOutputStream bos = new ByteArrayOutputStream();
|
|
1938 BufferedOutputStream os = new BufferedOutputStream(bos);
|
|
1939 image.flush();
|
|
1940 ImageIO.write(image, imageFormat, os);
|
|
1941 os.flush();
|
|
1942 os.close();
|
|
1943 return bos.toByteArray();
|
|
1944 }
|
|
1945
|
93
|
1946 void drawBufferedImage(byte[] b) throws IOException{
|
|
1947 BufferedImage bimg = ImageIO.read(new ByteArrayInputStream(b));
|
|
1948 // ImageIO.write(bimg, "jpeg", new File("sample.jpeg"));
|
|
1949 memGraphics.setClip(0,0, rfb.framebufferWidth, rfb.framebufferHeight );
|
|
1950 memGraphics.drawImage( bimg, 0,0, null);
|
|
1951 scheduleRepaint(0, 0, rfb.framebufferWidth, rfb.framebufferHeight );
|
|
1952 }
|
|
1953
|
60
|
1954
|
|
1955 }
|