blob: 58d24dea7bc3185c5d7846e6495c4cd7316f9aa9 [file] [log] [blame]
Constantin Kaplinsky1215b992008-04-18 09:51:44 +00001//
2// Copyright (C) 2001,2002 HorizonLive.com, Inc. All Rights Reserved.
3// Copyright (C) 2001 Constantin Kaplinsky. All Rights Reserved.
4// Copyright (C) 2000 Tridia Corporation. All Rights Reserved.
5// Copyright (C) 1999 AT&T Laboratories Cambridge. All Rights Reserved.
6//
7// This is free software; you can redistribute it and/or modify
8// it under the terms of the GNU General Public License as published by
9// the Free Software Foundation; either version 2 of the License, or
10// (at your option) any later version.
11//
12// This software is distributed in the hope that it will be useful,
13// but WITHOUT ANY WARRANTY; without even the implied warranty of
14// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15// GNU General Public License for more details.
16//
17// You should have received a copy of the GNU General Public License
18// along with this software; if not, write to the Free Software
19// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
20// USA.
21//
22
23import java.awt.*;
24import java.awt.event.*;
25import java.awt.image.*;
26import java.io.*;
27import java.lang.*;
28import java.util.zip.*;
29
30
31//
32// VncCanvas is a subclass of Canvas which draws a VNC desktop on it.
33//
34
Constantin Kaplinsky903009e2002-05-20 10:55:47 +000035class VncCanvas extends Canvas {
Constantin Kaplinsky1215b992008-04-18 09:51:44 +000036
Constantin Kaplinsky903009e2002-05-20 10:55:47 +000037 RfbPlayer player;
Constantin Kaplinsky1215b992008-04-18 09:51:44 +000038 RfbProto rfb;
Constantin Kaplinsky903009e2002-05-20 10:55:47 +000039 ColorModel cm24;
Constantin Kaplinsky1215b992008-04-18 09:51:44 +000040
41 Image memImage;
42 Graphics memGraphics;
43
44 Image rawPixelsImage;
45 MemoryImageSource pixelsSource;
Constantin Kaplinsky1215b992008-04-18 09:51:44 +000046 int[] pixels24;
47
48 // Zlib encoder's data.
49 byte[] zlibBuf;
50 int zlibBufLen = 0;
51 Inflater zlibInflater;
52
53 // Tight encoder's data.
54 final static int tightZlibBufferSize = 512;
55 Inflater[] tightInflaters;
56
57 // Since JPEG images are loaded asynchronously, we have to remember
58 // their position in the framebuffer. Also, this jpegRect object is
59 // used for synchronization between the rfbThread and a JVM's thread
60 // which decodes and loads JPEG images.
61 Rectangle jpegRect;
62
Constantin Kaplinsky1215b992008-04-18 09:51:44 +000063 //
64 // The constructor.
65 //
66
Constantin Kaplinsky903009e2002-05-20 10:55:47 +000067 VncCanvas(RfbPlayer player) throws IOException {
68 this.player = player;
69 rfb = player.rfb;
Constantin Kaplinsky1215b992008-04-18 09:51:44 +000070
Constantin Kaplinsky1215b992008-04-18 09:51:44 +000071 cm24 = new DirectColorModel(24, 0xFF0000, 0x00FF00, 0x0000FF);
72
Constantin Kaplinsky903009e2002-05-20 10:55:47 +000073 updateFramebufferSize();
Constantin Kaplinsky1215b992008-04-18 09:51:44 +000074 }
75
76 //
77 // Callback methods to determine geometry of our Component.
78 //
79
80 public Dimension getPreferredSize() {
81 return new Dimension(rfb.framebufferWidth, rfb.framebufferHeight);
82 }
83
84 public Dimension getMinimumSize() {
85 return new Dimension(rfb.framebufferWidth, rfb.framebufferHeight);
86 }
87
88 public Dimension getMaximumSize() {
89 return new Dimension(rfb.framebufferWidth, rfb.framebufferHeight);
90 }
91
92 //
93 // All painting is performed here.
94 //
95
96 public void update(Graphics g) {
97 paint(g);
98 }
99
100 public void paint(Graphics g) {
101 synchronized(memImage) {
102 g.drawImage(memImage, 0, 0, null);
103 }
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000104 }
105
106 //
107 // Override the ImageObserver interface method to handle drawing of
108 // JPEG-encoded data.
109 //
110
111 public boolean imageUpdate(Image img, int infoflags,
112 int x, int y, int width, int height) {
113 if ((infoflags & (ALLBITS | ABORT)) == 0) {
114 return true; // We need more image data.
115 } else {
116 // If the whole image is available, draw it now.
117 if ((infoflags & ALLBITS) != 0) {
118 if (jpegRect != null) {
119 synchronized(jpegRect) {
120 memGraphics.drawImage(img, jpegRect.x, jpegRect.y, null);
121 scheduleRepaint(jpegRect.x, jpegRect.y,
122 jpegRect.width, jpegRect.height);
123 jpegRect.notify();
124 }
125 }
126 }
127 return false; // All image data was processed.
128 }
129 }
130
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000131 void updateFramebufferSize() {
132
133 // Useful shortcuts.
134 int fbWidth = rfb.framebufferWidth;
135 int fbHeight = rfb.framebufferHeight;
136
137 // Create new off-screen image either if it does not exist, or if
138 // its geometry should be changed. It's not necessary to replace
139 // existing image if only pixel format should be changed.
140 if (memImage == null) {
Constantin Kaplinsky903009e2002-05-20 10:55:47 +0000141 memImage = player.createImage(fbWidth, fbHeight);
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000142 memGraphics = memImage.getGraphics();
143 } else if (memImage.getWidth(null) != fbWidth ||
144 memImage.getHeight(null) != fbHeight) {
145 synchronized(memImage) {
Constantin Kaplinsky903009e2002-05-20 10:55:47 +0000146 memImage = player.createImage(fbWidth, fbHeight);
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000147 memGraphics = memImage.getGraphics();
148 }
149 }
150
151 // Images with raw pixels should be re-allocated on every change
152 // of geometry or pixel format.
Constantin Kaplinsky903009e2002-05-20 10:55:47 +0000153 pixels24 = new int[fbWidth * fbHeight];
154 pixelsSource =
155 new MemoryImageSource(fbWidth, fbHeight, cm24, pixels24, 0, fbWidth);
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000156 pixelsSource.setAnimated(true);
157 rawPixelsImage = createImage(pixelsSource);
158
159 // Update the size of desktop containers.
Constantin Kaplinsky903009e2002-05-20 10:55:47 +0000160 if (player.inSeparateFrame) {
161 if (player.desktopScrollPane != null)
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000162 resizeDesktopFrame();
163 } else {
164 setSize(fbWidth, fbHeight);
165 }
166 }
167
168 void resizeDesktopFrame() {
169 setSize(rfb.framebufferWidth, rfb.framebufferHeight);
170
171 // FIXME: Find a better way to determine correct size of a
172 // ScrollPane. -- const
Constantin Kaplinsky903009e2002-05-20 10:55:47 +0000173 Insets insets = player.desktopScrollPane.getInsets();
174 player.desktopScrollPane.setSize(rfb.framebufferWidth +
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000175 2 * Math.min(insets.left, insets.right),
176 rfb.framebufferHeight +
177 2 * Math.min(insets.top, insets.bottom));
178
Constantin Kaplinsky903009e2002-05-20 10:55:47 +0000179 player.vncFrame.pack();
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000180
181 // Try to limit the frame size to the screen size.
Constantin Kaplinsky903009e2002-05-20 10:55:47 +0000182 Dimension screenSize = player.vncFrame.getToolkit().getScreenSize();
183 Dimension frameSize = player.vncFrame.getSize();
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000184 Dimension newSize = frameSize;
185 boolean needToResizeFrame = false;
186 if (frameSize.height > screenSize.height) {
187 newSize.height = screenSize.height;
188 needToResizeFrame = true;
189 }
190 if (frameSize.width > screenSize.width) {
191 newSize.width = screenSize.width;
192 needToResizeFrame = true;
193 }
194 if (needToResizeFrame) {
Constantin Kaplinsky903009e2002-05-20 10:55:47 +0000195 player.vncFrame.setSize(newSize);
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000196 }
197
Constantin Kaplinsky903009e2002-05-20 10:55:47 +0000198 player.desktopScrollPane.doLayout();
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000199 }
200
201 //
202 // processNormalProtocol() - executed by the rfbThread to deal with the
203 // RFB socket.
204 //
205
206 public void processNormalProtocol() throws IOException {
207
Constantin Kaplinsky903009e2002-05-20 10:55:47 +0000208 zlibInflater = new Inflater();
209 tightInflaters = new Inflater[4];
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000210
Constantin Kaplinsky903009e2002-05-20 10:55:47 +0000211 // Main dispatch loop.
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000212
213 while (true) {
Constantin Kaplinsky903009e2002-05-20 10:55:47 +0000214
215 while (player.getMode() != player.MODE_PLAYBACK) {
216 synchronized(this) {
217 try {
218 wait();
219 } catch (InterruptedException e) {
220 }
221 }
222 if (player.getMode() == player.MODE_STOPPED) {
223 throw new EOFException("Playback stopped");
224 }
225 if (player.getMode() == player.MODE_PLAYBACK) {
226 player.fbsStream.resumeReading();
227 }
228 }
229
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000230 int msgType = rfb.readServerMessageType();
231
232 switch (msgType) {
233 case RfbProto.FramebufferUpdate:
234 rfb.readFramebufferUpdate();
235
236 for (int i = 0; i < rfb.updateNRects; i++) {
237 rfb.readFramebufferUpdateRectHdr();
238
239 if (rfb.updateRectEncoding == rfb.EncodingLastRect)
240 break;
241
242 if (rfb.updateRectEncoding == rfb.EncodingNewFBSize) {
243 rfb.setFramebufferSize(rfb.updateRectW, rfb.updateRectH);
244 updateFramebufferSize();
245 break;
246 }
247
248 if (rfb.updateRectEncoding == rfb.EncodingXCursor ||
249 rfb.updateRectEncoding == rfb.EncodingRichCursor) {
Constantin Kaplinsky903009e2002-05-20 10:55:47 +0000250 throw new IOException("Sorry, no support for" +
251 " cursor shape updates yet");
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000252 }
253
254 switch (rfb.updateRectEncoding) {
255
256 case RfbProto.EncodingRaw:
257 {
258 handleRawRect(rfb.updateRectX, rfb.updateRectY,
259 rfb.updateRectW, rfb.updateRectH);
260 break;
261 }
262
263 case RfbProto.EncodingCopyRect:
264 {
265 rfb.readCopyRect();
266
267 int sx = rfb.copyRectSrcX, sy = rfb.copyRectSrcY;
268 int rx = rfb.updateRectX, ry = rfb.updateRectY;
269 int rw = rfb.updateRectW, rh = rfb.updateRectH;
270
271 memGraphics.copyArea(sx, sy, rw, rh, rx - sx, ry - sy);
272
273 scheduleRepaint(rx, ry, rw, rh);
274 break;
275 }
276
277 case RfbProto.EncodingRRE:
278 {
Constantin Kaplinsky2258f122002-05-20 13:10:44 +0000279 byte[] buf = new byte[4];
280
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000281 int rx = rfb.updateRectX, ry = rfb.updateRectY;
282 int rw = rfb.updateRectW, rh = rfb.updateRectH;
283 int nSubrects = rfb.is.readInt();
284 int x, y, w, h;
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000285
Constantin Kaplinsky2258f122002-05-20 13:10:44 +0000286 rfb.is.readFully(buf);
Constantin Kaplinskya628bf02002-05-20 13:33:46 +0000287 Color pixel = new Color(buf[2] & 0xFF,
288 buf[1] & 0xFF,
289 buf[0] & 0xFF);
Constantin Kaplinsky903009e2002-05-20 10:55:47 +0000290 memGraphics.setColor(pixel);
291 memGraphics.fillRect(rx, ry, rw, rh);
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000292
Constantin Kaplinsky903009e2002-05-20 10:55:47 +0000293 for (int j = 0; j < nSubrects; j++) {
Constantin Kaplinsky2258f122002-05-20 13:10:44 +0000294 rfb.is.readFully(buf);
Constantin Kaplinskya628bf02002-05-20 13:33:46 +0000295 pixel = new Color(buf[2] & 0xFF, buf[1] & 0xFF, buf[0] & 0xFF);
Constantin Kaplinsky903009e2002-05-20 10:55:47 +0000296 x = rx + rfb.is.readUnsignedShort();
297 y = ry + rfb.is.readUnsignedShort();
298 w = rfb.is.readUnsignedShort();
299 h = rfb.is.readUnsignedShort();
300
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000301 memGraphics.setColor(pixel);
Constantin Kaplinsky903009e2002-05-20 10:55:47 +0000302 memGraphics.fillRect(x, y, w, h);
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000303 }
304
305 scheduleRepaint(rx, ry, rw, rh);
306 break;
307 }
308
309 case RfbProto.EncodingCoRRE:
310 {
Constantin Kaplinsky2258f122002-05-20 13:10:44 +0000311 byte[] buf = new byte[4];
312
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000313 int rx = rfb.updateRectX, ry = rfb.updateRectY;
314 int rw = rfb.updateRectW, rh = rfb.updateRectH;
315 int nSubrects = rfb.is.readInt();
316 int x, y, w, h;
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000317
Constantin Kaplinsky2258f122002-05-20 13:10:44 +0000318 rfb.is.readFully(buf);
Constantin Kaplinskya628bf02002-05-20 13:33:46 +0000319 Color pixel = new Color(buf[2] & 0xFF,
320 buf[1] & 0xFF,
321 buf[0] & 0xFF);
Constantin Kaplinsky903009e2002-05-20 10:55:47 +0000322 memGraphics.setColor(pixel);
323 memGraphics.fillRect(rx, ry, rw, rh);
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000324
Constantin Kaplinsky903009e2002-05-20 10:55:47 +0000325 for (int j = 0; j < nSubrects; j++) {
Constantin Kaplinsky2258f122002-05-20 13:10:44 +0000326 rfb.is.readFully(buf);
Constantin Kaplinskya628bf02002-05-20 13:33:46 +0000327 pixel = new Color(buf[2] & 0xFF, buf[1] & 0xFF, buf[0] & 0xFF);
Constantin Kaplinsky903009e2002-05-20 10:55:47 +0000328 x = rx + rfb.is.readUnsignedByte();
329 y = ry + rfb.is.readUnsignedByte();
330 w = rfb.is.readUnsignedByte();
331 h = rfb.is.readUnsignedByte();
332
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000333 memGraphics.setColor(pixel);
Constantin Kaplinsky903009e2002-05-20 10:55:47 +0000334 memGraphics.fillRect(x, y, w, h);
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000335 }
336
337 scheduleRepaint(rx, ry, rw, rh);
338 break;
339 }
340
341 case RfbProto.EncodingHextile:
342 {
Constantin Kaplinskya5fcd982002-05-20 13:06:33 +0000343 byte[] buf = new byte[256 * 4];
344
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000345 int rx = rfb.updateRectX, ry = rfb.updateRectY;
346 int rw = rfb.updateRectW, rh = rfb.updateRectH;
Constantin Kaplinskya628bf02002-05-20 13:33:46 +0000347 Color bg = new Color(0, 0, 0), fg = new Color(0, 0, 0);
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000348
349 for (int ty = ry; ty < ry + rh; ty += 16) {
350
351 int th = 16;
352 if (ry + rh - ty < 16)
353 th = ry + rh - ty;
354
355 for (int tx = rx; tx < rx + rw; tx += 16) {
356
357 int tw = 16;
358 if (rx + rw - tx < 16)
359 tw = rx + rw - tx;
360
361 int subencoding = rfb.is.readUnsignedByte();
362
363 // Is it a raw-encoded sub-rectangle?
364 if ((subencoding & rfb.HextileRaw) != 0) {
Constantin Kaplinsky903009e2002-05-20 10:55:47 +0000365 int count, offset;
366 for (int j = ty; j < ty + th; j++) {
Constantin Kaplinskya5fcd982002-05-20 13:06:33 +0000367 rfb.is.readFully(buf, 0, tw * 4);
Constantin Kaplinsky903009e2002-05-20 10:55:47 +0000368 offset = j * rfb.framebufferWidth + tx;
369 for (count = 0; count < tw; count++) {
370 pixels24[offset + count] =
Constantin Kaplinskya5fcd982002-05-20 13:06:33 +0000371 (buf[count * 4 + 2] & 0xFF) << 16 |
372 (buf[count * 4 + 1] & 0xFF) << 8 |
373 (buf[count * 4] & 0xFF);
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000374 }
375 }
376 handleUpdatedPixels(tx, ty, tw, th);
377 continue;
378 }
379
380 // Read and draw the background if specified.
381 if ((subencoding & rfb.HextileBackgroundSpecified) != 0) {
Constantin Kaplinskya5fcd982002-05-20 13:06:33 +0000382 rfb.is.readFully(buf, 0, 4);
Constantin Kaplinskya628bf02002-05-20 13:33:46 +0000383 bg = new Color(buf[2] & 0xFF, buf[1] & 0xFF, buf[0] & 0xFF);
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000384 }
385 memGraphics.setColor(bg);
386 memGraphics.fillRect(tx, ty, tw, th);
387
388 // Read the foreground color if specified.
389 if ((subencoding & rfb.HextileForegroundSpecified) != 0) {
Constantin Kaplinskya5fcd982002-05-20 13:06:33 +0000390 rfb.is.readFully(buf, 0, 4);
Constantin Kaplinskya628bf02002-05-20 13:33:46 +0000391 fg = new Color(buf[2] & 0xFF, buf[1] & 0xFF, buf[0] & 0xFF);
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000392 }
393
394 // Done with this tile if there is no sub-rectangles.
395 if ((subencoding & rfb.HextileAnySubrects) == 0)
396 continue;
397
398 int nSubrects = rfb.is.readUnsignedByte();
399
400 int b1, b2, sx, sy, sw, sh;
Constantin Kaplinsky903009e2002-05-20 10:55:47 +0000401 if ((subencoding & rfb.HextileSubrectsColoured) != 0) {
402 for (int j = 0; j < nSubrects; j++) {
Constantin Kaplinskya5fcd982002-05-20 13:06:33 +0000403 rfb.is.readFully(buf, 0, 4);
Constantin Kaplinskya628bf02002-05-20 13:33:46 +0000404 fg = new Color(buf[2] & 0xFF,
405 buf[1] & 0xFF,
406 buf[0] & 0xFF);
Constantin Kaplinsky903009e2002-05-20 10:55:47 +0000407 b1 = rfb.is.readUnsignedByte();
408 b2 = rfb.is.readUnsignedByte();
409 sx = tx + (b1 >> 4);
410 sy = ty + (b1 & 0xf);
411 sw = (b2 >> 4) + 1;
412 sh = (b2 & 0xf) + 1;
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000413 memGraphics.setColor(fg);
Constantin Kaplinsky903009e2002-05-20 10:55:47 +0000414 memGraphics.fillRect(sx, sy, sw, sh);
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000415 }
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000416 } else {
Constantin Kaplinsky903009e2002-05-20 10:55:47 +0000417 memGraphics.setColor(fg);
418 for (int j = 0; j < nSubrects; j++) {
419 b1 = rfb.is.readUnsignedByte();
420 b2 = rfb.is.readUnsignedByte();
421 sx = tx + (b1 >> 4);
422 sy = ty + (b1 & 0xf);
423 sw = (b2 >> 4) + 1;
424 sh = (b2 & 0xf) + 1;
425 memGraphics.fillRect(sx, sy, sw, sh);
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000426 }
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000427 }
428
429 }
430 // Finished with a row of tiles, now let's show it.
431 scheduleRepaint(rx, ty, rw, th);
432 }
433 break;
434 }
435
436 case RfbProto.EncodingZlib:
437 {
438 int nBytes = rfb.is.readInt();
439
440 if (zlibBuf == null || zlibBufLen < nBytes) {
441 zlibBufLen = nBytes * 2;
442 zlibBuf = new byte[zlibBufLen];
443 }
444
445 rfb.is.readFully(zlibBuf, 0, nBytes);
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000446 zlibInflater.setInput(zlibBuf, 0, nBytes);
447
448 handleZlibRect(rfb.updateRectX, rfb.updateRectY,
449 rfb.updateRectW, rfb.updateRectH);
450
451 break;
452 }
453
454 case RfbProto.EncodingTight:
455 {
456 handleTightRect(rfb.updateRectX, rfb.updateRectY,
457 rfb.updateRectW, rfb.updateRectH);
458
459 break;
460 }
461
462 default:
463 throw new IOException("Unknown RFB rectangle encoding " +
464 rfb.updateRectEncoding);
465 }
466
467 }
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000468 break;
469
470 case RfbProto.SetColourMapEntries:
471 throw new IOException("Can't handle SetColourMapEntries message");
472
473 case RfbProto.Bell:
474 Toolkit.getDefaultToolkit().beep();
475 break;
476
477 case RfbProto.ServerCutText:
478 String s = rfb.readServerCutText();
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000479 break;
480
481 default:
482 throw new IOException("Unknown RFB message type " + msgType);
483 }
Constantin Kaplinsky903009e2002-05-20 10:55:47 +0000484
485 if (player.getMode() == player.MODE_STOPPED) {
486 throw new EOFException("Playback stopped");
487 }
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000488 }
489 }
490
491
492 //
493 // Handle a raw rectangle.
494 //
495
496 void handleRawRect(int x, int y, int w, int h) throws IOException {
497
Constantin Kaplinsky903009e2002-05-20 10:55:47 +0000498 byte[] buf = new byte[w * 4];
499 int i, offset;
500 for (int dy = y; dy < y + h; dy++) {
501 rfb.is.readFully(buf);
502 offset = dy * rfb.framebufferWidth + x;
503 for (i = 0; i < w; i++) {
504 pixels24[offset + i] =
Constantin Kaplinskya5fcd982002-05-20 13:06:33 +0000505 (buf[i * 4 + 2] & 0xFF) << 16 |
506 (buf[i * 4 + 1] & 0xFF) << 8 |
507 (buf[i * 4] & 0xFF);
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000508 }
509 }
510
511 handleUpdatedPixels(x, y, w, h);
512 scheduleRepaint(x, y, w, h);
513 }
514
515
516 //
517 // Handle a Zlib-encoded rectangle.
518 //
519
520 void handleZlibRect(int x, int y, int w, int h)
521 throws IOException {
522
523 try {
Constantin Kaplinsky903009e2002-05-20 10:55:47 +0000524 byte[] buf = new byte[w * 4];
525 int i, offset;
526 for (int dy = y; dy < y + h; dy++) {
527 zlibInflater.inflate(buf);
528 offset = dy * rfb.framebufferWidth + x;
529 for (i = 0; i < w; i++) {
530 pixels24[offset + i] =
Constantin Kaplinskya5fcd982002-05-20 13:06:33 +0000531 (buf[i * 4 + 2] & 0xFF) << 16 |
532 (buf[i * 4 + 1] & 0xFF) << 8 |
533 (buf[i * 4] & 0xFF);
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000534 }
535 }
536 }
537 catch (DataFormatException dfe) {
538 throw new IOException(dfe.toString());
539 }
540
541 handleUpdatedPixels(x, y, w, h);
542 scheduleRepaint(x, y, w, h);
543 }
544
545
546 //
547 // Handle a tight rectangle.
548 //
549
550 void handleTightRect(int x, int y, int w, int h) throws IOException {
551
552 int comp_ctl = rfb.is.readUnsignedByte();
553
554 // Flush zlib streams if we are told by the server to do so.
555 for (int stream_id = 0; stream_id < 4; stream_id++) {
556 if ((comp_ctl & 1) != 0 && tightInflaters[stream_id] != null) {
557 tightInflaters[stream_id] = null;
558 }
559 comp_ctl >>= 1;
560 }
561
562 // Check correctness of subencoding value.
563 if (comp_ctl > rfb.TightMaxSubencoding) {
564 throw new IOException("Incorrect tight subencoding: " + comp_ctl);
565 }
566
567 // Handle solid-color rectangles.
568 if (comp_ctl == rfb.TightFill) {
Constantin Kaplinsky903009e2002-05-20 10:55:47 +0000569 byte[] buf = new byte[3];
570 rfb.is.readFully(buf);
Constantin Kaplinskya628bf02002-05-20 13:33:46 +0000571 Color bg = new Color(buf[0] & 0xFF, buf[1] & 0xFF, buf[2] & 0xFF);
Constantin Kaplinsky903009e2002-05-20 10:55:47 +0000572 memGraphics.setColor(bg);
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000573 memGraphics.fillRect(x, y, w, h);
574 scheduleRepaint(x, y, w, h);
575 return;
576 }
577
578 if (comp_ctl == rfb.TightJpeg) {
579
580 // Read JPEG data.
581 byte[] jpegData = new byte[rfb.readCompactLen()];
582 rfb.is.readFully(jpegData);
583
584 // Create an Image object from the JPEG data.
585 Image jpegImage = Toolkit.getDefaultToolkit().createImage(jpegData);
586
587 // Remember the rectangle where the image should be drawn.
588 jpegRect = new Rectangle(x, y, w, h);
589
590 // Let the imageUpdate() method do the actual drawing, here just
591 // wait until the image is fully loaded and drawn.
592 synchronized(jpegRect) {
593 Toolkit.getDefaultToolkit().prepareImage(jpegImage, -1, -1, this);
594 try {
595 // Wait no longer than three seconds.
596 jpegRect.wait(3000);
597 } catch (InterruptedException e) {
598 throw new IOException("Interrupted while decoding JPEG image");
599 }
600 }
601
602 // Done, jpegRect is not needed any more.
603 jpegRect = null;
604 return;
605
606 }
607
608 // Read filter id and parameters.
609 int numColors = 0, rowSize = w;
610 byte[] palette8 = new byte[2];
611 int[] palette24 = new int[256];
612 boolean useGradient = false;
613 if ((comp_ctl & rfb.TightExplicitFilter) != 0) {
614 int filter_id = rfb.is.readUnsignedByte();
615 if (filter_id == rfb.TightFilterPalette) {
616 numColors = rfb.is.readUnsignedByte() + 1;
Constantin Kaplinsky903009e2002-05-20 10:55:47 +0000617 byte[] buf = new byte[numColors * 3];
618 rfb.is.readFully(buf);
619 for (int i = 0; i < numColors; i++) {
620 palette24[i] = ((buf[i * 3] & 0xFF) << 16 |
621 (buf[i * 3 + 1] & 0xFF) << 8 |
622 (buf[i * 3 + 2] & 0xFF));
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000623 }
624 if (numColors == 2)
625 rowSize = (w + 7) / 8;
626 } else if (filter_id == rfb.TightFilterGradient) {
627 useGradient = true;
628 } else if (filter_id != rfb.TightFilterCopy) {
629 throw new IOException("Incorrect tight filter id: " + filter_id);
630 }
631 }
Constantin Kaplinsky903009e2002-05-20 10:55:47 +0000632 if (numColors == 0)
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000633 rowSize *= 3;
634
635 // Read, optionally uncompress and decode data.
636 int dataSize = h * rowSize;
637 if (dataSize < rfb.TightMinToCompress) {
638 // Data size is small - not compressed with zlib.
639 if (numColors != 0) {
640 // Indexed colors.
641 byte[] indexedData = new byte[dataSize];
642 rfb.is.readFully(indexedData);
643 if (numColors == 2) {
644 // Two colors.
Constantin Kaplinsky903009e2002-05-20 10:55:47 +0000645 decodeMonoData(x, y, w, h, indexedData, palette24);
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000646 } else {
Constantin Kaplinsky903009e2002-05-20 10:55:47 +0000647 // 3..255 colors.
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000648 int i = 0;
649 for (int dy = y; dy < y + h; dy++) {
650 for (int dx = x; dx < x + w; dx++) {
651 pixels24[dy * rfb.framebufferWidth + dx] =
652 palette24[indexedData[i++] & 0xFF];
653 }
654 }
655 }
656 } else if (useGradient) {
657 // "Gradient"-processed data
658 byte[] buf = new byte[w * h * 3];
659 rfb.is.readFully(buf);
660 decodeGradientData(x, y, w, h, buf);
661 } else {
662 // Raw truecolor data.
Constantin Kaplinsky903009e2002-05-20 10:55:47 +0000663 byte[] buf = new byte[w * 3];
664 int i, offset;
665 for (int dy = y; dy < y + h; dy++) {
666 rfb.is.readFully(buf);
667 offset = dy * rfb.framebufferWidth + x;
668 for (i = 0; i < w; i++) {
669 pixels24[offset + i] =
670 (buf[i * 3] & 0xFF) << 16 |
671 (buf[i * 3 + 1] & 0xFF) << 8 |
672 (buf[i * 3 + 2] & 0xFF);
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000673 }
674 }
675 }
676 } else {
677 // Data was compressed with zlib.
678 int zlibDataLen = rfb.readCompactLen();
679 byte[] zlibData = new byte[zlibDataLen];
680 rfb.is.readFully(zlibData);
681 int stream_id = comp_ctl & 0x03;
682 if (tightInflaters[stream_id] == null) {
683 tightInflaters[stream_id] = new Inflater();
684 }
685 Inflater myInflater = tightInflaters[stream_id];
686 myInflater.setInput(zlibData);
687 try {
688 if (numColors != 0) {
689 // Indexed colors.
690 byte[] indexedData = new byte[dataSize];
691 myInflater.inflate(indexedData);
692 if (numColors == 2) {
693 // Two colors.
Constantin Kaplinsky903009e2002-05-20 10:55:47 +0000694 decodeMonoData(x, y, w, h, indexedData, palette24);
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000695 } else {
Constantin Kaplinsky903009e2002-05-20 10:55:47 +0000696 // More than two colors.
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000697 int i = 0;
698 for (int dy = y; dy < y + h; dy++) {
699 for (int dx = x; dx < x + w; dx++) {
700 pixels24[dy * rfb.framebufferWidth + dx] =
701 palette24[indexedData[i++] & 0xFF];
702 }
703 }
704 }
705 } else if (useGradient) {
Constantin Kaplinsky903009e2002-05-20 10:55:47 +0000706 // Compressed "Gradient"-filtered data.
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000707 byte[] buf = new byte[w * h * 3];
708 myInflater.inflate(buf);
709 decodeGradientData(x, y, w, h, buf);
710 } else {
711 // Compressed truecolor data.
Constantin Kaplinsky903009e2002-05-20 10:55:47 +0000712 byte[] buf = new byte[w * 3];
713 int i, offset;
714 for (int dy = y; dy < y + h; dy++) {
715 myInflater.inflate(buf);
716 offset = dy * rfb.framebufferWidth + x;
717 for (i = 0; i < w; i++) {
718 pixels24[offset + i] =
719 (buf[i * 3] & 0xFF) << 16 |
720 (buf[i * 3 + 1] & 0xFF) << 8 |
721 (buf[i * 3 + 2] & 0xFF);
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000722 }
723 }
724 }
725 }
726 catch(DataFormatException dfe) {
727 throw new IOException(dfe.toString());
728 }
729 }
730
731 handleUpdatedPixels(x, y, w, h);
732 scheduleRepaint(x, y, w, h);
733 }
734
735 //
Constantin Kaplinsky903009e2002-05-20 10:55:47 +0000736 // Decode 1bpp-encoded bi-color rectangle.
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000737 //
738
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000739 void decodeMonoData(int x, int y, int w, int h, byte[] src, int[] palette)
740 throws IOException {
741
742 int dx, dy, n;
743 int i = y * rfb.framebufferWidth + x;
744 int rowBytes = (w + 7) / 8;
745 byte b;
746
747 for (dy = 0; dy < h; dy++) {
748 for (dx = 0; dx < w / 8; dx++) {
749 b = src[dy*rowBytes+dx];
750 for (n = 7; n >= 0; n--)
751 pixels24[i++] = palette[b >> n & 1];
752 }
753 for (n = 7; n >= 8 - w % 8; n--) {
754 pixels24[i++] = palette[src[dy*rowBytes+dx] >> n & 1];
755 }
756 i += (rfb.framebufferWidth - w);
757 }
758 }
759
760 //
761 // Decode data processed with the "Gradient" filter.
762 //
763
764 void decodeGradientData (int x, int y, int w, int h, byte[] buf)
765 throws IOException {
766
767 int dx, dy, c;
768 byte[] prevRow = new byte[w * 3];
769 byte[] thisRow = new byte[w * 3];
770 byte[] pix = new byte[3];
771 int[] est = new int[3];
772
773 int offset = y * rfb.framebufferWidth + x;
774
775 for (dy = 0; dy < h; dy++) {
776
777 /* First pixel in a row */
778 for (c = 0; c < 3; c++) {
779 pix[c] = (byte)(prevRow[c] + buf[dy * w * 3 + c]);
780 thisRow[c] = pix[c];
781 }
782 pixels24[offset++] =
783 (pix[0] & 0xFF) << 16 | (pix[1] & 0xFF) << 8 | (pix[2] & 0xFF);
784
785 /* Remaining pixels of a row */
786 for (dx = 1; dx < w; dx++) {
787 for (c = 0; c < 3; c++) {
788 est[c] = ((prevRow[dx * 3 + c] & 0xFF) + (pix[c] & 0xFF) -
789 (prevRow[(dx-1) * 3 + c] & 0xFF));
790 if (est[c] > 0xFF) {
791 est[c] = 0xFF;
792 } else if (est[c] < 0x00) {
793 est[c] = 0x00;
794 }
795 pix[c] = (byte)(est[c] + buf[(dy * w + dx) * 3 + c]);
796 thisRow[dx * 3 + c] = pix[c];
797 }
798 pixels24[offset++] =
799 (pix[0] & 0xFF) << 16 | (pix[1] & 0xFF) << 8 | (pix[2] & 0xFF);
800 }
801
802 System.arraycopy(thisRow, 0, prevRow, 0, w * 3);
803 offset += (rfb.framebufferWidth - w);
804 }
805 }
806
807
808 //
809 // Display newly updated area of pixels.
810 //
811
812 void handleUpdatedPixels(int x, int y, int w, int h) {
813
814 // Draw updated pixels of the off-screen image.
Constantin Kaplinsky903009e2002-05-20 10:55:47 +0000815
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000816 pixelsSource.newPixels(x, y, w, h);
817 memGraphics.setClip(x, y, w, h);
818 memGraphics.drawImage(rawPixelsImage, 0, 0, null);
819 memGraphics.setClip(0, 0, rfb.framebufferWidth, rfb.framebufferHeight);
820 }
821
822 //
823 // Tell JVM to repaint specified desktop area.
824 //
825
826 void scheduleRepaint(int x, int y, int w, int h) {
827 // Request repaint, deferred if necessary.
Constantin Kaplinsky903009e2002-05-20 10:55:47 +0000828 repaint(player.deferScreenUpdates, x, y, w, h);
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000829 }
830
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000831}