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