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