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