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