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