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