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