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