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