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