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