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