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