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