blob: 2fe0f476335a5002b609bcbd7574646405387e64 [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);
287 Color pixel = new Color((buf[2] & 0xFF) << 16 |
288 (buf[1] & 0xFF) << 8 |
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);
295 pixel = new Color((buf[2] & 0xFF) << 16 |
296 (buf[1] & 0xFF) << 8 |
297 (buf[0] & 0xFF));
Constantin Kaplinsky903009e2002-05-20 10:55:47 +0000298 x = rx + rfb.is.readUnsignedShort();
299 y = ry + rfb.is.readUnsignedShort();
300 w = rfb.is.readUnsignedShort();
301 h = rfb.is.readUnsignedShort();
302
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000303 memGraphics.setColor(pixel);
Constantin Kaplinsky903009e2002-05-20 10:55:47 +0000304 memGraphics.fillRect(x, y, w, h);
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000305 }
306
307 scheduleRepaint(rx, ry, rw, rh);
308 break;
309 }
310
311 case RfbProto.EncodingCoRRE:
312 {
Constantin Kaplinsky2258f122002-05-20 13:10:44 +0000313 byte[] buf = new byte[4];
314
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000315 int rx = rfb.updateRectX, ry = rfb.updateRectY;
316 int rw = rfb.updateRectW, rh = rfb.updateRectH;
317 int nSubrects = rfb.is.readInt();
318 int x, y, w, h;
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000319
Constantin Kaplinsky2258f122002-05-20 13:10:44 +0000320 rfb.is.readFully(buf);
321 Color pixel = new Color((buf[2] & 0xFF) << 16 |
322 (buf[1] & 0xFF) << 8 |
323 (buf[0] & 0xFF));
Constantin Kaplinsky903009e2002-05-20 10:55:47 +0000324 memGraphics.setColor(pixel);
325 memGraphics.fillRect(rx, ry, rw, rh);
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000326
Constantin Kaplinsky903009e2002-05-20 10:55:47 +0000327 for (int j = 0; j < nSubrects; j++) {
Constantin Kaplinsky2258f122002-05-20 13:10:44 +0000328 rfb.is.readFully(buf);
329 pixel = new Color((buf[2] & 0xFF) << 16 |
330 (buf[1] & 0xFF) << 8 |
331 (buf[0] & 0xFF));
Constantin Kaplinsky903009e2002-05-20 10:55:47 +0000332 x = rx + rfb.is.readUnsignedByte();
333 y = ry + rfb.is.readUnsignedByte();
334 w = rfb.is.readUnsignedByte();
335 h = rfb.is.readUnsignedByte();
336
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000337 memGraphics.setColor(pixel);
Constantin Kaplinsky903009e2002-05-20 10:55:47 +0000338 memGraphics.fillRect(x, y, w, h);
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000339 }
340
341 scheduleRepaint(rx, ry, rw, rh);
342 break;
343 }
344
345 case RfbProto.EncodingHextile:
346 {
Constantin Kaplinskya5fcd982002-05-20 13:06:33 +0000347 byte[] buf = new byte[256 * 4];
348
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000349 int rx = rfb.updateRectX, ry = rfb.updateRectY;
350 int rw = rfb.updateRectW, rh = rfb.updateRectH;
351 Color bg = new Color(0), fg = new Color(0);
352
353 for (int ty = ry; ty < ry + rh; ty += 16) {
354
355 int th = 16;
356 if (ry + rh - ty < 16)
357 th = ry + rh - ty;
358
359 for (int tx = rx; tx < rx + rw; tx += 16) {
360
361 int tw = 16;
362 if (rx + rw - tx < 16)
363 tw = rx + rw - tx;
364
365 int subencoding = rfb.is.readUnsignedByte();
366
367 // Is it a raw-encoded sub-rectangle?
368 if ((subencoding & rfb.HextileRaw) != 0) {
Constantin Kaplinsky903009e2002-05-20 10:55:47 +0000369 int count, offset;
370 for (int j = ty; j < ty + th; j++) {
Constantin Kaplinskya5fcd982002-05-20 13:06:33 +0000371 rfb.is.readFully(buf, 0, tw * 4);
Constantin Kaplinsky903009e2002-05-20 10:55:47 +0000372 offset = j * rfb.framebufferWidth + tx;
373 for (count = 0; count < tw; count++) {
374 pixels24[offset + count] =
Constantin Kaplinskya5fcd982002-05-20 13:06:33 +0000375 (buf[count * 4 + 2] & 0xFF) << 16 |
376 (buf[count * 4 + 1] & 0xFF) << 8 |
377 (buf[count * 4] & 0xFF);
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000378 }
379 }
380 handleUpdatedPixels(tx, ty, tw, th);
381 continue;
382 }
383
384 // Read and draw the background if specified.
385 if ((subencoding & rfb.HextileBackgroundSpecified) != 0) {
Constantin Kaplinskya5fcd982002-05-20 13:06:33 +0000386 rfb.is.readFully(buf, 0, 4);
387 bg = new Color((buf[2] & 0xFF) << 16 |
388 (buf[1] & 0xFF) << 8 |
389 (buf[0] & 0xFF));
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000390 }
391 memGraphics.setColor(bg);
392 memGraphics.fillRect(tx, ty, tw, th);
393
394 // Read the foreground color if specified.
395 if ((subencoding & rfb.HextileForegroundSpecified) != 0) {
Constantin Kaplinskya5fcd982002-05-20 13:06:33 +0000396 rfb.is.readFully(buf, 0, 4);
397 fg = new Color((buf[2] & 0xFF) << 16 |
398 (buf[1] & 0xFF) << 8 |
399 (buf[0] & 0xFF));
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000400 }
401
402 // Done with this tile if there is no sub-rectangles.
403 if ((subencoding & rfb.HextileAnySubrects) == 0)
404 continue;
405
406 int nSubrects = rfb.is.readUnsignedByte();
407
408 int b1, b2, sx, sy, sw, sh;
Constantin Kaplinsky903009e2002-05-20 10:55:47 +0000409 if ((subencoding & rfb.HextileSubrectsColoured) != 0) {
410 for (int j = 0; j < nSubrects; j++) {
Constantin Kaplinskya5fcd982002-05-20 13:06:33 +0000411 rfb.is.readFully(buf, 0, 4);
412 fg = new Color((buf[2] & 0xFF) << 16 |
413 (buf[1] & 0xFF) << 8 |
414 (buf[0] & 0xFF));
Constantin Kaplinsky903009e2002-05-20 10:55:47 +0000415 b1 = rfb.is.readUnsignedByte();
416 b2 = rfb.is.readUnsignedByte();
417 sx = tx + (b1 >> 4);
418 sy = ty + (b1 & 0xf);
419 sw = (b2 >> 4) + 1;
420 sh = (b2 & 0xf) + 1;
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000421 memGraphics.setColor(fg);
Constantin Kaplinsky903009e2002-05-20 10:55:47 +0000422 memGraphics.fillRect(sx, sy, sw, sh);
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000423 }
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000424 } else {
Constantin Kaplinsky903009e2002-05-20 10:55:47 +0000425 memGraphics.setColor(fg);
426 for (int j = 0; j < nSubrects; j++) {
427 b1 = rfb.is.readUnsignedByte();
428 b2 = rfb.is.readUnsignedByte();
429 sx = tx + (b1 >> 4);
430 sy = ty + (b1 & 0xf);
431 sw = (b2 >> 4) + 1;
432 sh = (b2 & 0xf) + 1;
433 memGraphics.fillRect(sx, sy, sw, sh);
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000434 }
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000435 }
436
437 }
438 // Finished with a row of tiles, now let's show it.
439 scheduleRepaint(rx, ty, rw, th);
440 }
441 break;
442 }
443
444 case RfbProto.EncodingZlib:
445 {
446 int nBytes = rfb.is.readInt();
447
448 if (zlibBuf == null || zlibBufLen < nBytes) {
449 zlibBufLen = nBytes * 2;
450 zlibBuf = new byte[zlibBufLen];
451 }
452
453 rfb.is.readFully(zlibBuf, 0, nBytes);
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000454 zlibInflater.setInput(zlibBuf, 0, nBytes);
455
456 handleZlibRect(rfb.updateRectX, rfb.updateRectY,
457 rfb.updateRectW, rfb.updateRectH);
458
459 break;
460 }
461
462 case RfbProto.EncodingTight:
463 {
464 handleTightRect(rfb.updateRectX, rfb.updateRectY,
465 rfb.updateRectW, rfb.updateRectH);
466
467 break;
468 }
469
470 default:
471 throw new IOException("Unknown RFB rectangle encoding " +
472 rfb.updateRectEncoding);
473 }
474
475 }
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000476 break;
477
478 case RfbProto.SetColourMapEntries:
479 throw new IOException("Can't handle SetColourMapEntries message");
480
481 case RfbProto.Bell:
482 Toolkit.getDefaultToolkit().beep();
483 break;
484
485 case RfbProto.ServerCutText:
486 String s = rfb.readServerCutText();
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000487 break;
488
489 default:
490 throw new IOException("Unknown RFB message type " + msgType);
491 }
Constantin Kaplinsky903009e2002-05-20 10:55:47 +0000492
493 if (player.getMode() == player.MODE_STOPPED) {
494 throw new EOFException("Playback stopped");
495 }
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000496 }
497 }
498
499
500 //
501 // Handle a raw rectangle.
502 //
503
504 void handleRawRect(int x, int y, int w, int h) throws IOException {
505
Constantin Kaplinsky903009e2002-05-20 10:55:47 +0000506 byte[] buf = new byte[w * 4];
507 int i, offset;
508 for (int dy = y; dy < y + h; dy++) {
509 rfb.is.readFully(buf);
510 offset = dy * rfb.framebufferWidth + x;
511 for (i = 0; i < w; i++) {
512 pixels24[offset + i] =
Constantin Kaplinskya5fcd982002-05-20 13:06:33 +0000513 (buf[i * 4 + 2] & 0xFF) << 16 |
514 (buf[i * 4 + 1] & 0xFF) << 8 |
515 (buf[i * 4] & 0xFF);
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000516 }
517 }
518
519 handleUpdatedPixels(x, y, w, h);
520 scheduleRepaint(x, y, w, h);
521 }
522
523
524 //
525 // Handle a Zlib-encoded rectangle.
526 //
527
528 void handleZlibRect(int x, int y, int w, int h)
529 throws IOException {
530
531 try {
Constantin Kaplinsky903009e2002-05-20 10:55:47 +0000532 byte[] buf = new byte[w * 4];
533 int i, offset;
534 for (int dy = y; dy < y + h; dy++) {
535 zlibInflater.inflate(buf);
536 offset = dy * rfb.framebufferWidth + x;
537 for (i = 0; i < w; i++) {
538 pixels24[offset + i] =
Constantin Kaplinskya5fcd982002-05-20 13:06:33 +0000539 (buf[i * 4 + 2] & 0xFF) << 16 |
540 (buf[i * 4 + 1] & 0xFF) << 8 |
541 (buf[i * 4] & 0xFF);
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000542 }
543 }
544 }
545 catch (DataFormatException dfe) {
546 throw new IOException(dfe.toString());
547 }
548
549 handleUpdatedPixels(x, y, w, h);
550 scheduleRepaint(x, y, w, h);
551 }
552
553
554 //
555 // Handle a tight rectangle.
556 //
557
558 void handleTightRect(int x, int y, int w, int h) throws IOException {
559
560 int comp_ctl = rfb.is.readUnsignedByte();
561
562 // Flush zlib streams if we are told by the server to do so.
563 for (int stream_id = 0; stream_id < 4; stream_id++) {
564 if ((comp_ctl & 1) != 0 && tightInflaters[stream_id] != null) {
565 tightInflaters[stream_id] = null;
566 }
567 comp_ctl >>= 1;
568 }
569
570 // Check correctness of subencoding value.
571 if (comp_ctl > rfb.TightMaxSubencoding) {
572 throw new IOException("Incorrect tight subencoding: " + comp_ctl);
573 }
574
575 // Handle solid-color rectangles.
576 if (comp_ctl == rfb.TightFill) {
Constantin Kaplinsky903009e2002-05-20 10:55:47 +0000577 byte[] buf = new byte[3];
578 rfb.is.readFully(buf);
579 Color bg = new Color(0xFF000000 | (buf[0] & 0xFF) << 16 |
580 (buf[1] & 0xFF) << 8 | (buf[2] & 0xFF));
581 memGraphics.setColor(bg);
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000582 memGraphics.fillRect(x, y, w, h);
583 scheduleRepaint(x, y, w, h);
584 return;
585 }
586
587 if (comp_ctl == rfb.TightJpeg) {
588
589 // Read JPEG data.
590 byte[] jpegData = new byte[rfb.readCompactLen()];
591 rfb.is.readFully(jpegData);
592
593 // Create an Image object from the JPEG data.
594 Image jpegImage = Toolkit.getDefaultToolkit().createImage(jpegData);
595
596 // Remember the rectangle where the image should be drawn.
597 jpegRect = new Rectangle(x, y, w, h);
598
599 // Let the imageUpdate() method do the actual drawing, here just
600 // wait until the image is fully loaded and drawn.
601 synchronized(jpegRect) {
602 Toolkit.getDefaultToolkit().prepareImage(jpegImage, -1, -1, this);
603 try {
604 // Wait no longer than three seconds.
605 jpegRect.wait(3000);
606 } catch (InterruptedException e) {
607 throw new IOException("Interrupted while decoding JPEG image");
608 }
609 }
610
611 // Done, jpegRect is not needed any more.
612 jpegRect = null;
613 return;
614
615 }
616
617 // Read filter id and parameters.
618 int numColors = 0, rowSize = w;
619 byte[] palette8 = new byte[2];
620 int[] palette24 = new int[256];
621 boolean useGradient = false;
622 if ((comp_ctl & rfb.TightExplicitFilter) != 0) {
623 int filter_id = rfb.is.readUnsignedByte();
624 if (filter_id == rfb.TightFilterPalette) {
625 numColors = rfb.is.readUnsignedByte() + 1;
Constantin Kaplinsky903009e2002-05-20 10:55:47 +0000626 byte[] buf = new byte[numColors * 3];
627 rfb.is.readFully(buf);
628 for (int i = 0; i < numColors; i++) {
629 palette24[i] = ((buf[i * 3] & 0xFF) << 16 |
630 (buf[i * 3 + 1] & 0xFF) << 8 |
631 (buf[i * 3 + 2] & 0xFF));
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000632 }
633 if (numColors == 2)
634 rowSize = (w + 7) / 8;
635 } else if (filter_id == rfb.TightFilterGradient) {
636 useGradient = true;
637 } else if (filter_id != rfb.TightFilterCopy) {
638 throw new IOException("Incorrect tight filter id: " + filter_id);
639 }
640 }
Constantin Kaplinsky903009e2002-05-20 10:55:47 +0000641 if (numColors == 0)
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000642 rowSize *= 3;
643
644 // Read, optionally uncompress and decode data.
645 int dataSize = h * rowSize;
646 if (dataSize < rfb.TightMinToCompress) {
647 // Data size is small - not compressed with zlib.
648 if (numColors != 0) {
649 // Indexed colors.
650 byte[] indexedData = new byte[dataSize];
651 rfb.is.readFully(indexedData);
652 if (numColors == 2) {
653 // Two colors.
Constantin Kaplinsky903009e2002-05-20 10:55:47 +0000654 decodeMonoData(x, y, w, h, indexedData, palette24);
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000655 } else {
Constantin Kaplinsky903009e2002-05-20 10:55:47 +0000656 // 3..255 colors.
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000657 int i = 0;
658 for (int dy = y; dy < y + h; dy++) {
659 for (int dx = x; dx < x + w; dx++) {
660 pixels24[dy * rfb.framebufferWidth + dx] =
661 palette24[indexedData[i++] & 0xFF];
662 }
663 }
664 }
665 } else if (useGradient) {
666 // "Gradient"-processed data
667 byte[] buf = new byte[w * h * 3];
668 rfb.is.readFully(buf);
669 decodeGradientData(x, y, w, h, buf);
670 } else {
671 // Raw truecolor data.
Constantin Kaplinsky903009e2002-05-20 10:55:47 +0000672 byte[] buf = new byte[w * 3];
673 int i, offset;
674 for (int dy = y; dy < y + h; dy++) {
675 rfb.is.readFully(buf);
676 offset = dy * rfb.framebufferWidth + x;
677 for (i = 0; i < w; i++) {
678 pixels24[offset + i] =
679 (buf[i * 3] & 0xFF) << 16 |
680 (buf[i * 3 + 1] & 0xFF) << 8 |
681 (buf[i * 3 + 2] & 0xFF);
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000682 }
683 }
684 }
685 } else {
686 // Data was compressed with zlib.
687 int zlibDataLen = rfb.readCompactLen();
688 byte[] zlibData = new byte[zlibDataLen];
689 rfb.is.readFully(zlibData);
690 int stream_id = comp_ctl & 0x03;
691 if (tightInflaters[stream_id] == null) {
692 tightInflaters[stream_id] = new Inflater();
693 }
694 Inflater myInflater = tightInflaters[stream_id];
695 myInflater.setInput(zlibData);
696 try {
697 if (numColors != 0) {
698 // Indexed colors.
699 byte[] indexedData = new byte[dataSize];
700 myInflater.inflate(indexedData);
701 if (numColors == 2) {
702 // Two colors.
Constantin Kaplinsky903009e2002-05-20 10:55:47 +0000703 decodeMonoData(x, y, w, h, indexedData, palette24);
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000704 } else {
Constantin Kaplinsky903009e2002-05-20 10:55:47 +0000705 // More than two colors.
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000706 int i = 0;
707 for (int dy = y; dy < y + h; dy++) {
708 for (int dx = x; dx < x + w; dx++) {
709 pixels24[dy * rfb.framebufferWidth + dx] =
710 palette24[indexedData[i++] & 0xFF];
711 }
712 }
713 }
714 } else if (useGradient) {
Constantin Kaplinsky903009e2002-05-20 10:55:47 +0000715 // Compressed "Gradient"-filtered data.
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000716 byte[] buf = new byte[w * h * 3];
717 myInflater.inflate(buf);
718 decodeGradientData(x, y, w, h, buf);
719 } else {
720 // Compressed truecolor data.
Constantin Kaplinsky903009e2002-05-20 10:55:47 +0000721 byte[] buf = new byte[w * 3];
722 int i, offset;
723 for (int dy = y; dy < y + h; dy++) {
724 myInflater.inflate(buf);
725 offset = dy * rfb.framebufferWidth + x;
726 for (i = 0; i < w; i++) {
727 pixels24[offset + i] =
728 (buf[i * 3] & 0xFF) << 16 |
729 (buf[i * 3 + 1] & 0xFF) << 8 |
730 (buf[i * 3 + 2] & 0xFF);
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000731 }
732 }
733 }
734 }
735 catch(DataFormatException dfe) {
736 throw new IOException(dfe.toString());
737 }
738 }
739
740 handleUpdatedPixels(x, y, w, h);
741 scheduleRepaint(x, y, w, h);
742 }
743
744 //
Constantin Kaplinsky903009e2002-05-20 10:55:47 +0000745 // Decode 1bpp-encoded bi-color rectangle.
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000746 //
747
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000748 void decodeMonoData(int x, int y, int w, int h, byte[] src, int[] palette)
749 throws IOException {
750
751 int dx, dy, n;
752 int i = y * rfb.framebufferWidth + x;
753 int rowBytes = (w + 7) / 8;
754 byte b;
755
756 for (dy = 0; dy < h; dy++) {
757 for (dx = 0; dx < w / 8; dx++) {
758 b = src[dy*rowBytes+dx];
759 for (n = 7; n >= 0; n--)
760 pixels24[i++] = palette[b >> n & 1];
761 }
762 for (n = 7; n >= 8 - w % 8; n--) {
763 pixels24[i++] = palette[src[dy*rowBytes+dx] >> n & 1];
764 }
765 i += (rfb.framebufferWidth - w);
766 }
767 }
768
769 //
770 // Decode data processed with the "Gradient" filter.
771 //
772
773 void decodeGradientData (int x, int y, int w, int h, byte[] buf)
774 throws IOException {
775
776 int dx, dy, c;
777 byte[] prevRow = new byte[w * 3];
778 byte[] thisRow = new byte[w * 3];
779 byte[] pix = new byte[3];
780 int[] est = new int[3];
781
782 int offset = y * rfb.framebufferWidth + x;
783
784 for (dy = 0; dy < h; dy++) {
785
786 /* First pixel in a row */
787 for (c = 0; c < 3; c++) {
788 pix[c] = (byte)(prevRow[c] + buf[dy * w * 3 + c]);
789 thisRow[c] = pix[c];
790 }
791 pixels24[offset++] =
792 (pix[0] & 0xFF) << 16 | (pix[1] & 0xFF) << 8 | (pix[2] & 0xFF);
793
794 /* Remaining pixels of a row */
795 for (dx = 1; dx < w; dx++) {
796 for (c = 0; c < 3; c++) {
797 est[c] = ((prevRow[dx * 3 + c] & 0xFF) + (pix[c] & 0xFF) -
798 (prevRow[(dx-1) * 3 + c] & 0xFF));
799 if (est[c] > 0xFF) {
800 est[c] = 0xFF;
801 } else if (est[c] < 0x00) {
802 est[c] = 0x00;
803 }
804 pix[c] = (byte)(est[c] + buf[(dy * w + dx) * 3 + c]);
805 thisRow[dx * 3 + c] = pix[c];
806 }
807 pixels24[offset++] =
808 (pix[0] & 0xFF) << 16 | (pix[1] & 0xFF) << 8 | (pix[2] & 0xFF);
809 }
810
811 System.arraycopy(thisRow, 0, prevRow, 0, w * 3);
812 offset += (rfb.framebufferWidth - w);
813 }
814 }
815
816
817 //
818 // Display newly updated area of pixels.
819 //
820
821 void handleUpdatedPixels(int x, int y, int w, int h) {
822
823 // Draw updated pixels of the off-screen image.
Constantin Kaplinsky903009e2002-05-20 10:55:47 +0000824
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000825 pixelsSource.newPixels(x, y, w, h);
826 memGraphics.setClip(x, y, w, h);
827 memGraphics.drawImage(rawPixelsImage, 0, 0, null);
828 memGraphics.setClip(0, 0, rfb.framebufferWidth, rfb.framebufferHeight);
829 }
830
831 //
832 // Tell JVM to repaint specified desktop area.
833 //
834
835 void scheduleRepaint(int x, int y, int w, int h) {
836 // Request repaint, deferred if necessary.
Constantin Kaplinsky903009e2002-05-20 10:55:47 +0000837 repaint(player.deferScreenUpdates, x, y, w, h);
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000838 }
839
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000840}