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