blob: 9c3a45f213bc6792b89c134ab3a3ee60f61a2541 [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
wimba.comc23aeb02004-09-16 00:00:00 +000023package com.HorizonLive.RfbPlayer;
24
Constantin Kaplinsky1215b992008-04-18 09:51:44 +000025import java.awt.*;
26import java.awt.event.*;
27import java.awt.image.*;
28import java.io.*;
29import java.lang.*;
Constantin Kaplinsky5a7133a2002-07-24 17:02:04 +000030import java.util.*;
Constantin Kaplinsky1215b992008-04-18 09:51:44 +000031import java.util.zip.*;
32
33
34//
35// VncCanvas is a subclass of Canvas which draws a VNC desktop on it.
36//
Constantin Kaplinsky5a7133a2002-07-24 17:02:04 +000037class VncCanvas extends Canvas implements Observer {
Constantin Kaplinsky1215b992008-04-18 09:51:44 +000038
Constantin Kaplinsky903009e2002-05-20 10:55:47 +000039 RfbPlayer player;
Constantin Kaplinsky1215b992008-04-18 09:51:44 +000040 RfbProto rfb;
Constantin Kaplinsky903009e2002-05-20 10:55:47 +000041 ColorModel cm24;
Constantin Kaplinsky1215b992008-04-18 09:51:44 +000042
43 Image memImage;
44 Graphics memGraphics;
45
46 Image rawPixelsImage;
47 MemoryImageSource pixelsSource;
Constantin Kaplinsky1215b992008-04-18 09:51:44 +000048 int[] pixels24;
49
50 // Zlib encoder's data.
51 byte[] zlibBuf;
52 int zlibBufLen = 0;
53 Inflater zlibInflater;
54
55 // Tight encoder's data.
56 final static int tightZlibBufferSize = 512;
57 Inflater[] tightInflaters;
58
59 // Since JPEG images are loaded asynchronously, we have to remember
60 // their position in the framebuffer. Also, this jpegRect object is
61 // used for synchronization between the rfbThread and a JVM's thread
62 // which decodes and loads JPEG images.
63 Rectangle jpegRect;
64
Constantin Kaplinsky52c48242002-05-30 13:26:34 +000065 // When we're in the seeking mode, we should not update the desktop.
66 // This variable helps us to remember that repainting the desktop at
67 // once is necessary when the seek operation is finished.
68 boolean seekMode;
69
wimba.com16092722004-09-21 20:54:37 +000070 // Distance of mouse from viewer border to trigger automatic scrolling
71 final static int SCROLL_MARGIN = 50;
72
wimba.comccb67c32005-01-03 16:20:38 +000073 // Color for border around small shared area
74 static final Color DARK_GRAY = new Color(132, 138, 156);
75
Constantin Kaplinsky1215b992008-04-18 09:51:44 +000076 //
77 // The constructor.
78 //
Constantin Kaplinsky903009e2002-05-20 10:55:47 +000079 VncCanvas(RfbPlayer player) throws IOException {
80 this.player = player;
81 rfb = player.rfb;
Constantin Kaplinsky52c48242002-05-30 13:26:34 +000082 seekMode = false;
Constantin Kaplinsky1215b992008-04-18 09:51:44 +000083
Constantin Kaplinsky1215b992008-04-18 09:51:44 +000084 cm24 = new DirectColorModel(24, 0xFF0000, 0x00FF00, 0x0000FF);
85
Constantin Kaplinsky903009e2002-05-20 10:55:47 +000086 updateFramebufferSize();
wimba.comccb67c32005-01-03 16:20:38 +000087
88 setBackground(Color.white);
Constantin Kaplinsky1215b992008-04-18 09:51:44 +000089 }
90
91 //
92 // Callback methods to determine geometry of our Component.
93 //
Constantin Kaplinsky1215b992008-04-18 09:51:44 +000094 public Dimension getPreferredSize() {
wimba.comccb67c32005-01-03 16:20:38 +000095 Dimension d = player.desktopScrollPane.getViewportSize();
96 Dimension sz = new Dimension(rfb.framebufferWidth, rfb.framebufferHeight);
97 if (d.width > sz.width)
98 sz.width = d.width;
99 if (d.height > sz.height)
100 sz.height = d.height;
101 return sz;
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000102 }
103
104 public Dimension getMinimumSize() {
wimba.comccb67c32005-01-03 16:20:38 +0000105 return getPreferredSize();
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000106 }
107
108 public Dimension getMaximumSize() {
wimba.comccb67c32005-01-03 16:20:38 +0000109 return getPreferredSize();
110 }
111
112 Point getImageOrigin() {
113 int x = 0, y = 0;
114 Dimension d = player.desktopScrollPane.getViewportSize();
115 if (rfb.framebufferWidth < d.width)
116 x = d.width / 2 - rfb.framebufferWidth / 2;
117 if (rfb.framebufferHeight < d.height)
118 y = d.height / 2 - rfb.framebufferHeight / 2;
119 return new Point(x, y);
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000120 }
121
122 //
123 // All painting is performed here.
124 //
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000125 public void update(Graphics g) {
126 paint(g);
127 }
128
wimba.comc219ca82004-09-22 15:10:14 +0000129 public synchronized void paint(Graphics g) {
wimba.comccb67c32005-01-03 16:20:38 +0000130 Point o = getImageOrigin();
131 Dimension d = getPreferredSize();
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000132 synchronized(memImage) {
wimba.comccb67c32005-01-03 16:20:38 +0000133 g.drawImage(memImage, o.x, o.y, null);
134
135 // fill in background
136 if (o.x > 0 || o.y > 0) {
137 // left, right, top, bottom
138 Color c = g.getColor();
139 g.setColor(Color.white);
140 g.fillRect(0, 0, o.x, d.height);
141 g.fillRect(o.x + rfb.framebufferWidth, 0, d.width - (o.x +
142 rfb.framebufferWidth), d.height);
143 g.fillRect(o.x, 0, rfb.framebufferWidth, o.y);
144 g.fillRect(o.x, o.y + rfb.framebufferHeight, rfb.framebufferWidth,
145 d.height - (o.y + rfb.framebufferHeight));
146 g.setColor(DARK_GRAY);
147 g.drawRect(o.x - 1, o.y - 1, rfb.framebufferWidth + 1,
148 rfb.framebufferHeight + 1);
149 g.setColor(c);
150 }
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000151 }
wimba.coma5a4f4f2004-09-21 15:25:05 +0000152 if (showSoftCursor) {
153 int x0 = cursorX - hotX, y0 = cursorY - hotY;
wimba.comccb67c32005-01-03 16:20:38 +0000154 x0 += o.x;
155 y0 += o.y;
wimba.coma5a4f4f2004-09-21 15:25:05 +0000156 Rectangle r = new Rectangle(x0, y0, cursorWidth, cursorHeight);
157 if (r.intersects(g.getClipBounds())) {
wimba.comccb67c32005-01-03 16:20:38 +0000158 Rectangle c = g.getClipBounds();
159 g.setClip(o.x, o.y, rfb.framebufferWidth, rfb.framebufferHeight);
wimba.coma5a4f4f2004-09-21 15:25:05 +0000160 g.drawImage(softCursor, x0, y0, null);
wimba.comccb67c32005-01-03 16:20:38 +0000161 g.setClip(c.x, c.y, c.width, c.height);
wimba.coma5a4f4f2004-09-21 15:25:05 +0000162 }
163 }
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000164 }
165
166 //
167 // Override the ImageObserver interface method to handle drawing of
168 // JPEG-encoded data.
169 //
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000170 public boolean imageUpdate(Image img, int infoflags,
Constantin Kaplinsky72e47ef2008-04-18 17:48:16 +0000171 int x, int y, int width, int height) {
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000172 if ((infoflags & (ALLBITS | ABORT)) == 0) {
173 return true; // We need more image data.
174 } else {
175 // If the whole image is available, draw it now.
176 if ((infoflags & ALLBITS) != 0) {
Constantin Kaplinsky72e47ef2008-04-18 17:48:16 +0000177 if (jpegRect != null) {
178 synchronized(jpegRect) {
179 memGraphics.drawImage(img, jpegRect.x, jpegRect.y, null);
180 scheduleRepaint(jpegRect.x, jpegRect.y,
181 jpegRect.width, jpegRect.height);
182 jpegRect.notify();
183 }
184 }
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000185 }
186 return false; // All image data was processed.
187 }
188 }
189
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000190 void updateFramebufferSize() {
191
192 // Useful shortcuts.
193 int fbWidth = rfb.framebufferWidth;
194 int fbHeight = rfb.framebufferHeight;
195
196 // Create new off-screen image either if it does not exist, or if
197 // its geometry should be changed. It's not necessary to replace
198 // existing image if only pixel format should be changed.
199 if (memImage == null) {
Constantin Kaplinsky903009e2002-05-20 10:55:47 +0000200 memImage = player.createImage(fbWidth, fbHeight);
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000201 memGraphics = memImage.getGraphics();
202 } else if (memImage.getWidth(null) != fbWidth ||
Constantin Kaplinsky72e47ef2008-04-18 17:48:16 +0000203 memImage.getHeight(null) != fbHeight) {
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000204 synchronized(memImage) {
Constantin Kaplinsky72e47ef2008-04-18 17:48:16 +0000205 memImage = player.createImage(fbWidth, fbHeight);
206 memGraphics = memImage.getGraphics();
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000207 }
208 }
209
210 // Images with raw pixels should be re-allocated on every change
211 // of geometry or pixel format.
Constantin Kaplinsky903009e2002-05-20 10:55:47 +0000212 pixels24 = new int[fbWidth * fbHeight];
213 pixelsSource =
Constantin Kaplinsky72e47ef2008-04-18 17:48:16 +0000214 new MemoryImageSource(fbWidth, fbHeight, cm24, pixels24, 0, fbWidth);
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000215 pixelsSource.setAnimated(true);
216 rawPixelsImage = createImage(pixelsSource);
217
218 // Update the size of desktop containers.
Constantin Kaplinsky903009e2002-05-20 10:55:47 +0000219 if (player.inSeparateFrame) {
220 if (player.desktopScrollPane != null)
Constantin Kaplinsky72e47ef2008-04-18 17:48:16 +0000221 resizeDesktopFrame();
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000222 } else {
223 setSize(fbWidth, fbHeight);
224 }
225 }
226
227 void resizeDesktopFrame() {
wimba.com252eb3b2004-09-21 21:27:54 +0000228 // determine screen size
Constantin Kaplinsky903009e2002-05-20 10:55:47 +0000229 Dimension screenSize = player.vncFrame.getToolkit().getScreenSize();
wimba.com252eb3b2004-09-21 21:27:54 +0000230 Dimension scrollSize = player.desktopScrollPane.getSize();
wimba.comc23aeb02004-09-16 00:00:00 +0000231
232 // Reduce Screen Size by 30 pixels in each direction;
233 // This is a (poor) attempt to account for
234 // 1) Menu bar on Macintosh (should really also account for
235 // Dock on OSX). Usually 22px on top of screen.
wimba.com252eb3b2004-09-21 21:27:54 +0000236 // 2) Taxkbar on Windows (usually about 28 px on bottom)
237 // 3) Other obstructions.
wimba.comc23aeb02004-09-16 00:00:00 +0000238 screenSize.height -= 30;
239 screenSize.width -= 30;
240
wimba.com252eb3b2004-09-21 21:27:54 +0000241 // Further reduce the screen size to account for the
242 // scroll pane's insets.
243 Insets insets = player.desktopScrollPane.getInsets();
244 screenSize.height -= insets.top + insets.bottom;
245 screenSize.width -= insets.left + insets.right;
wimba.comc23aeb02004-09-16 00:00:00 +0000246
wimba.com252eb3b2004-09-21 21:27:54 +0000247 // Limit pane size to fit on screen.
248 boolean needResize = false;
249 if (scrollSize.width != rfb.framebufferWidth ||
250 scrollSize.height != rfb.framebufferHeight)
251 needResize = true;
252 int w = rfb.framebufferWidth, h = rfb.framebufferHeight;
253 if (w > screenSize.width) {
254 w = screenSize.width;
255 needResize = true;
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000256 }
wimba.com252eb3b2004-09-21 21:27:54 +0000257 if (h > screenSize.height) {
258 h = screenSize.height;
259 needResize = true;
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000260 }
wimba.com252eb3b2004-09-21 21:27:54 +0000261 if (needResize)
262 player.desktopScrollPane.setSize(w, h);
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000263
wimba.com252eb3b2004-09-21 21:27:54 +0000264 player.vncFrame.pack();
wimba.comccb67c32005-01-03 16:20:38 +0000265
266 // size the canvas
267 setSize(getPreferredSize());
wimba.com252eb3b2004-09-21 21:27:54 +0000268 }
269
270 void resizeEmbeddedApplet() {
wimba.com252eb3b2004-09-21 21:27:54 +0000271 // resize scroll pane if necessary
272 Dimension scrollSize = player.desktopScrollPane.getSize();
273 if (scrollSize.width != player.dispW ||
wimba.comccb67c32005-01-03 16:20:38 +0000274 scrollSize.height != player.dispH) {
wimba.com252eb3b2004-09-21 21:27:54 +0000275 player.desktopScrollPane.setSize(player.dispW, player.dispH);
wimba.comccb67c32005-01-03 16:20:38 +0000276 player.desktopScrollPane.validate();
277 }
wimba.com252eb3b2004-09-21 21:27:54 +0000278
wimba.comccb67c32005-01-03 16:20:38 +0000279 // size the canvas
280 setSize(getPreferredSize());
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000281 }
282
283 //
Constantin Kaplinsky99df0a72002-06-04 06:13:20 +0000284 // processNormalProtocol() - executed by the rfbThread to deal with
285 // the RFB data.
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000286 //
Constantin Kaplinsky99df0a72002-06-04 06:13:20 +0000287 public void processNormalProtocol() throws Exception {
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000288
Constantin Kaplinsky903009e2002-05-20 10:55:47 +0000289 zlibInflater = new Inflater();
290 tightInflaters = new Inflater[4];
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000291
Constantin Kaplinsky5a7133a2002-07-24 17:02:04 +0000292 // Show current time position in the control panel.
Constantin Kaplinskyfe079832002-05-29 00:52:32 +0000293 player.updatePos();
294
Constantin Kaplinsky5a7133a2002-07-24 17:02:04 +0000295 // Tell our FbsInputStream object to notify us when it goes to the
296 // `paused' mode.
297 rfb.fbs.addObserver(this);
298
Constantin Kaplinsky903009e2002-05-20 10:55:47 +0000299 // Main dispatch loop.
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000300
301 while (true) {
Constantin Kaplinsky903009e2002-05-20 10:55:47 +0000302
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000303 int msgType = rfb.readServerMessageType();
304
305 switch (msgType) {
306 case RfbProto.FramebufferUpdate:
Constantin Kaplinsky72e47ef2008-04-18 17:48:16 +0000307 rfb.readFramebufferUpdate();
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000308
Constantin Kaplinsky72e47ef2008-04-18 17:48:16 +0000309 for (int i = 0; i < rfb.updateNRects; i++) {
310 rfb.readFramebufferUpdateRectHdr();
wimba.coma5a4f4f2004-09-21 15:25:05 +0000311
312 boolean cursorPosReceived = false;
313
Constantin Kaplinsky72e47ef2008-04-18 17:48:16 +0000314 int rx = rfb.updateRectX, ry = rfb.updateRectY;
315 int rw = rfb.updateRectW, rh = rfb.updateRectH;
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000316
Constantin Kaplinsky72e47ef2008-04-18 17:48:16 +0000317 if (rfb.updateRectEncoding == rfb.EncodingLastRect)
318 break;
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000319
Constantin Kaplinsky72e47ef2008-04-18 17:48:16 +0000320 if (rfb.updateRectEncoding == rfb.EncodingNewFBSize) {
wimba.comb7017b72004-09-16 16:11:55 +0000321 if (rfb.updateRectW != 0 && rfb.updateRectH != 0) {
322 rfb.setFramebufferSize(rfb.updateRectW, rfb.updateRectH);
323 updateFramebufferSize();
324 }
Constantin Kaplinsky72e47ef2008-04-18 17:48:16 +0000325 break;
326 }
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000327
Constantin Kaplinsky72e47ef2008-04-18 17:48:16 +0000328 if (rfb.updateRectEncoding == rfb.EncodingXCursor ||
329 rfb.updateRectEncoding == rfb.EncodingRichCursor) {
wimba.coma5a4f4f2004-09-21 15:25:05 +0000330 handleCursorShapeUpdate(rfb.updateRectEncoding, rx, ry, rw, rh);
331 continue;
332 }
333// if (rfb.updateRectEncoding == rfb.EncodingXCursor ||
334// rfb.updateRectEncoding == rfb.EncodingRichCursor) {
335// throw new Exception("Sorry, no support for" +
336// " cursor shape updates yet");
337// }
338
339 if (rfb.updateRectEncoding == rfb.EncodingPointerPos) {
340 softCursorMove(rx, ry);
341 cursorPosReceived = true;
342 continue;
Constantin Kaplinsky72e47ef2008-04-18 17:48:16 +0000343 }
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000344
Constantin Kaplinsky72e47ef2008-04-18 17:48:16 +0000345 switch (rfb.updateRectEncoding) {
346 case RfbProto.EncodingRaw:
347 handleRawRect(rx, ry, rw, rh);
348 break;
349 case RfbProto.EncodingCopyRect:
350 handleCopyRect(rx, ry, rw, rh);
351 break;
352 case RfbProto.EncodingRRE:
353 handleRRERect(rx, ry, rw, rh);
354 break;
355 case RfbProto.EncodingCoRRE:
356 handleCoRRERect(rx, ry, rw, rh);
357 break;
358 case RfbProto.EncodingHextile:
359 handleHextileRect(rx, ry, rw, rh);
360 break;
361 case RfbProto.EncodingZlib:
Constantin Kaplinsky99df0a72002-06-04 06:13:20 +0000362 handleZlibRect(rx, ry, rw, rh);
Constantin Kaplinsky72e47ef2008-04-18 17:48:16 +0000363 break;
364 case RfbProto.EncodingTight:
365 handleTightRect(rx, ry, rw, rh);
366 break;
367 default:
368 throw new Exception("Unknown RFB rectangle encoding " +
wimba.coma5a4f4f2004-09-21 15:25:05 +0000369 Integer.toString(rfb.updateRectEncoding, 16));
Constantin Kaplinsky72e47ef2008-04-18 17:48:16 +0000370 }
371 }
372 break;
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000373
374 case RfbProto.SetColourMapEntries:
Constantin Kaplinsky72e47ef2008-04-18 17:48:16 +0000375 throw new Exception("Can't handle SetColourMapEntries message");
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000376
377 case RfbProto.Bell:
378 Toolkit.getDefaultToolkit().beep();
Constantin Kaplinsky72e47ef2008-04-18 17:48:16 +0000379 break;
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000380
381 case RfbProto.ServerCutText:
Constantin Kaplinsky72e47ef2008-04-18 17:48:16 +0000382 String s = rfb.readServerCutText();
383 break;
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000384
385 default:
wimba.coma5a4f4f2004-09-21 15:25:05 +0000386 throw new Exception("Unknown RFB message type " +
387 Integer.toString(msgType, 16));
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000388 }
Constantin Kaplinsky903009e2002-05-20 10:55:47 +0000389
Constantin Kaplinskyfe079832002-05-29 00:52:32 +0000390 player.updatePos();
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000391 }
392 }
393
394
395 //
396 // Handle a raw rectangle.
397 //
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000398 void handleRawRect(int x, int y, int w, int h) throws IOException {
399
Constantin Kaplinsky903009e2002-05-20 10:55:47 +0000400 byte[] buf = new byte[w * 4];
401 int i, offset;
402 for (int dy = y; dy < y + h; dy++) {
403 rfb.is.readFully(buf);
404 offset = dy * rfb.framebufferWidth + x;
405 for (i = 0; i < w; i++) {
Constantin Kaplinsky72e47ef2008-04-18 17:48:16 +0000406 pixels24[offset + i] =
407 (buf[i * 4 + 2] & 0xFF) << 16 |
408 (buf[i * 4 + 1] & 0xFF) << 8 |
409 (buf[i * 4] & 0xFF);
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000410 }
411 }
412
413 handleUpdatedPixels(x, y, w, h);
414 scheduleRepaint(x, y, w, h);
415 }
416
417
418 //
Constantin Kaplinsky99df0a72002-06-04 06:13:20 +0000419 // Handle a CopyRect rectangle.
420 //
Constantin Kaplinsky99df0a72002-06-04 06:13:20 +0000421 void handleCopyRect(int x, int y, int w, int h) throws IOException {
422
423 rfb.readCopyRect();
424 memGraphics.copyArea(rfb.copyRectSrcX, rfb.copyRectSrcY, w, h,
Constantin Kaplinsky72e47ef2008-04-18 17:48:16 +0000425 x - rfb.copyRectSrcX, y - rfb.copyRectSrcY);
Constantin Kaplinsky99df0a72002-06-04 06:13:20 +0000426
427 scheduleRepaint(x, y, w, h);
428 }
429
430 //
431 // Handle an RRE-encoded rectangle.
432 //
Constantin Kaplinsky99df0a72002-06-04 06:13:20 +0000433 void handleRRERect(int x, int y, int w, int h) throws IOException {
434
435 int nSubrects = rfb.is.readInt();
436 int sx, sy, sw, sh;
437
438 byte[] buf = new byte[4];
439 rfb.is.readFully(buf);
440 Color pixel = new Color(buf[2] & 0xFF, buf[1] & 0xFF, buf[0] & 0xFF);
441 memGraphics.setColor(pixel);
442 memGraphics.fillRect(x, y, w, h);
443
444 for (int j = 0; j < nSubrects; j++) {
445 rfb.is.readFully(buf);
446 pixel = new Color(buf[2] & 0xFF, buf[1] & 0xFF, buf[0] & 0xFF);
447 sx = x + rfb.is.readUnsignedShort();
448 sy = y + rfb.is.readUnsignedShort();
449 sw = rfb.is.readUnsignedShort();
450 sh = rfb.is.readUnsignedShort();
451
452 memGraphics.setColor(pixel);
453 memGraphics.fillRect(sx, sy, sw, sh);
454 }
455
456 scheduleRepaint(x, y, w, h);
457 }
458
459 //
460 // Handle a CoRRE-encoded rectangle.
461 //
Constantin Kaplinsky99df0a72002-06-04 06:13:20 +0000462 void handleCoRRERect(int x, int y, int w, int h) throws IOException {
463
464 int nSubrects = rfb.is.readInt();
465 int sx, sy, sw, sh;
466
467 byte[] buf = new byte[4];
468 rfb.is.readFully(buf);
469 Color pixel = new Color(buf[2] & 0xFF, buf[1] & 0xFF, buf[0] & 0xFF);
470 memGraphics.setColor(pixel);
471 memGraphics.fillRect(x, y, w, h);
472
473 for (int j = 0; j < nSubrects; j++) {
474 rfb.is.readFully(buf);
475 pixel = new Color(buf[2] & 0xFF, buf[1] & 0xFF, buf[0] & 0xFF);
476 sx = x + rfb.is.readUnsignedByte();
477 sy = y + rfb.is.readUnsignedByte();
478 sw = rfb.is.readUnsignedByte();
479 sh = rfb.is.readUnsignedByte();
480
481 memGraphics.setColor(pixel);
482 memGraphics.fillRect(sx, sy, sw, sh);
483 }
484
485 scheduleRepaint(x, y, w, h);
486 }
487
488 //
489 // Handle a Hextile-encoded rectangle.
490 //
491
492 // These colors should be kept between handleHextileSubrect() calls.
Constantin Kaplinsky72e47ef2008-04-18 17:48:16 +0000493 private Color hextile_bg, hextile_fg;
Constantin Kaplinsky99df0a72002-06-04 06:13:20 +0000494
495 void handleHextileRect(int x, int y, int w, int h) throws IOException {
496
497 hextile_bg = new Color(0, 0, 0);
498 hextile_fg = new Color(0, 0, 0);
499
500 for (int ty = y; ty < y + h; ty += 16) {
501 int th = 16;
502 if (y + h - ty < 16)
Constantin Kaplinsky72e47ef2008-04-18 17:48:16 +0000503 th = y + h - ty;
Constantin Kaplinsky99df0a72002-06-04 06:13:20 +0000504
505 for (int tx = x; tx < x + w; tx += 16) {
Constantin Kaplinsky72e47ef2008-04-18 17:48:16 +0000506 int tw = 16;
507 if (x + w - tx < 16)
508 tw = x + w - tx;
Constantin Kaplinsky99df0a72002-06-04 06:13:20 +0000509
Constantin Kaplinsky72e47ef2008-04-18 17:48:16 +0000510 handleHextileSubrect(tx, ty, tw, th);
Constantin Kaplinsky99df0a72002-06-04 06:13:20 +0000511 }
512
513 // Finished with a row of tiles, now let's show it.
514 scheduleRepaint(x, y, w, h);
515 }
516 }
517
518 //
519 // Handle one tile in the Hextile-encoded data.
520 //
Constantin Kaplinsky99df0a72002-06-04 06:13:20 +0000521 void handleHextileSubrect(int tx, int ty, int tw, int th)
Constantin Kaplinsky72e47ef2008-04-18 17:48:16 +0000522 throws IOException {
Constantin Kaplinsky99df0a72002-06-04 06:13:20 +0000523
524 byte[] buf = new byte[256 * 4];
525
526 int subencoding = rfb.is.readUnsignedByte();
527
528 // Is it a raw-encoded sub-rectangle?
529 if ((subencoding & rfb.HextileRaw) != 0) {
530 int count, offset;
531 for (int j = ty; j < ty + th; j++) {
Constantin Kaplinsky72e47ef2008-04-18 17:48:16 +0000532 rfb.is.readFully(buf, 0, tw * 4);
533 offset = j * rfb.framebufferWidth + tx;
534 for (count = 0; count < tw; count++) {
535 pixels24[offset + count] =
536 (buf[count * 4 + 2] & 0xFF) << 16 |
537 (buf[count * 4 + 1] & 0xFF) << 8 |
538 (buf[count * 4] & 0xFF);
539 }
Constantin Kaplinsky99df0a72002-06-04 06:13:20 +0000540 }
541 handleUpdatedPixels(tx, ty, tw, th);
542 return;
543 }
544
545 // Read and draw the background if specified.
546 if ((subencoding & rfb.HextileBackgroundSpecified) != 0) {
547 rfb.is.readFully(buf, 0, 4);
548 hextile_bg = new Color(buf[2] & 0xFF, buf[1] & 0xFF, buf[0] & 0xFF);
549 }
550 memGraphics.setColor(hextile_bg);
551 memGraphics.fillRect(tx, ty, tw, th);
552
553 // Read the foreground color if specified.
554 if ((subencoding & rfb.HextileForegroundSpecified) != 0) {
555 rfb.is.readFully(buf, 0, 4);
556 hextile_fg = new Color(buf[2] & 0xFF, buf[1] & 0xFF, buf[0] & 0xFF);
557 }
558
559 // Done with this tile if there is no sub-rectangles.
560 if ((subencoding & rfb.HextileAnySubrects) == 0)
561 return;
562
563 int nSubrects = rfb.is.readUnsignedByte();
564
565 int b1, b2, sx, sy, sw, sh;
566 if ((subencoding & rfb.HextileSubrectsColoured) != 0) {
567 for (int j = 0; j < nSubrects; j++) {
Constantin Kaplinsky72e47ef2008-04-18 17:48:16 +0000568 rfb.is.readFully(buf, 0, 4);
569 hextile_fg = new Color(buf[2] & 0xFF, buf[1] & 0xFF, buf[0] & 0xFF);
570 b1 = rfb.is.readUnsignedByte();
571 b2 = rfb.is.readUnsignedByte();
572 sx = tx + (b1 >> 4);
573 sy = ty + (b1 & 0xf);
574 sw = (b2 >> 4) + 1;
575 sh = (b2 & 0xf) + 1;
576 memGraphics.setColor(hextile_fg);
577 memGraphics.fillRect(sx, sy, sw, sh);
Constantin Kaplinsky99df0a72002-06-04 06:13:20 +0000578 }
579 } else {
580 memGraphics.setColor(hextile_fg);
581 for (int j = 0; j < nSubrects; j++) {
Constantin Kaplinsky72e47ef2008-04-18 17:48:16 +0000582 b1 = rfb.is.readUnsignedByte();
583 b2 = rfb.is.readUnsignedByte();
584 sx = tx + (b1 >> 4);
585 sy = ty + (b1 & 0xf);
586 sw = (b2 >> 4) + 1;
587 sh = (b2 & 0xf) + 1;
588 memGraphics.fillRect(sx, sy, sw, sh);
Constantin Kaplinsky99df0a72002-06-04 06:13:20 +0000589 }
590 }
591 }
592
593 //
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000594 // Handle a Zlib-encoded rectangle.
595 //
Constantin Kaplinsky99df0a72002-06-04 06:13:20 +0000596 void handleZlibRect(int x, int y, int w, int h) throws Exception {
597
598 int nBytes = rfb.is.readInt();
599
600 if (zlibBuf == null || zlibBufLen < nBytes) {
601 zlibBufLen = nBytes * 2;
602 zlibBuf = new byte[zlibBufLen];
603 }
604
605 rfb.is.readFully(zlibBuf, 0, nBytes);
606 zlibInflater.setInput(zlibBuf, 0, nBytes);
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000607
608 try {
Constantin Kaplinsky903009e2002-05-20 10:55:47 +0000609 byte[] buf = new byte[w * 4];
610 int i, offset;
611 for (int dy = y; dy < y + h; dy++) {
Constantin Kaplinsky72e47ef2008-04-18 17:48:16 +0000612 zlibInflater.inflate(buf);
613 offset = dy * rfb.framebufferWidth + x;
614 for (i = 0; i < w; i++) {
615 pixels24[offset + i] =
616 (buf[i * 4 + 2] & 0xFF) << 16 |
617 (buf[i * 4 + 1] & 0xFF) << 8 |
618 (buf[i * 4] & 0xFF);
619 }
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000620 }
Constantin Kaplinsky72e47ef2008-04-18 17:48:16 +0000621 } catch (DataFormatException dfe) {
Constantin Kaplinsky99df0a72002-06-04 06:13:20 +0000622 throw new Exception(dfe.toString());
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000623 }
624
625 handleUpdatedPixels(x, y, w, h);
626 scheduleRepaint(x, y, w, h);
627 }
628
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000629 //
Constantin Kaplinsky99df0a72002-06-04 06:13:20 +0000630 // Handle a Tight-encoded rectangle.
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000631 //
Constantin Kaplinsky99df0a72002-06-04 06:13:20 +0000632 void handleTightRect(int x, int y, int w, int h) throws Exception {
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000633
634 int comp_ctl = rfb.is.readUnsignedByte();
635
636 // Flush zlib streams if we are told by the server to do so.
637 for (int stream_id = 0; stream_id < 4; stream_id++) {
638 if ((comp_ctl & 1) != 0 && tightInflaters[stream_id] != null) {
Constantin Kaplinsky72e47ef2008-04-18 17:48:16 +0000639 tightInflaters[stream_id] = null;
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000640 }
641 comp_ctl >>= 1;
642 }
643
644 // Check correctness of subencoding value.
645 if (comp_ctl > rfb.TightMaxSubencoding) {
Constantin Kaplinsky99df0a72002-06-04 06:13:20 +0000646 throw new Exception("Incorrect tight subencoding: " + comp_ctl);
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000647 }
648
649 // Handle solid-color rectangles.
650 if (comp_ctl == rfb.TightFill) {
Constantin Kaplinsky903009e2002-05-20 10:55:47 +0000651 byte[] buf = new byte[3];
652 rfb.is.readFully(buf);
Constantin Kaplinskya628bf02002-05-20 13:33:46 +0000653 Color bg = new Color(buf[0] & 0xFF, buf[1] & 0xFF, buf[2] & 0xFF);
Constantin Kaplinsky903009e2002-05-20 10:55:47 +0000654 memGraphics.setColor(bg);
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000655 memGraphics.fillRect(x, y, w, h);
656 scheduleRepaint(x, y, w, h);
657 return;
658 }
659
660 if (comp_ctl == rfb.TightJpeg) {
661
662 // Read JPEG data.
663 byte[] jpegData = new byte[rfb.readCompactLen()];
664 rfb.is.readFully(jpegData);
665
666 // Create an Image object from the JPEG data.
667 Image jpegImage = Toolkit.getDefaultToolkit().createImage(jpegData);
668
669 // Remember the rectangle where the image should be drawn.
670 jpegRect = new Rectangle(x, y, w, h);
671
672 // Let the imageUpdate() method do the actual drawing, here just
673 // wait until the image is fully loaded and drawn.
674 synchronized(jpegRect) {
Constantin Kaplinsky72e47ef2008-04-18 17:48:16 +0000675 Toolkit.getDefaultToolkit().prepareImage(jpegImage, -1, -1, this);
676 try {
677 // Wait no longer than three seconds.
678 jpegRect.wait(3000);
679 } catch (InterruptedException e) {
680 throw new Exception("Interrupted while decoding JPEG image");
681 }
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000682 }
683
684 // Done, jpegRect is not needed any more.
685 jpegRect = null;
686 return;
687
688 }
689
690 // Read filter id and parameters.
691 int numColors = 0, rowSize = w;
692 byte[] palette8 = new byte[2];
693 int[] palette24 = new int[256];
694 boolean useGradient = false;
695 if ((comp_ctl & rfb.TightExplicitFilter) != 0) {
696 int filter_id = rfb.is.readUnsignedByte();
697 if (filter_id == rfb.TightFilterPalette) {
Constantin Kaplinsky72e47ef2008-04-18 17:48:16 +0000698 numColors = rfb.is.readUnsignedByte() + 1;
699 byte[] buf = new byte[numColors * 3];
700 rfb.is.readFully(buf);
701 for (int i = 0; i < numColors; i++) {
702 palette24[i] = ((buf[i * 3] & 0xFF) << 16 |
703 (buf[i * 3 + 1] & 0xFF) << 8 |
704 (buf[i * 3 + 2] & 0xFF));
705 }
706 if (numColors == 2)
707 rowSize = (w + 7) / 8;
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000708 } else if (filter_id == rfb.TightFilterGradient) {
Constantin Kaplinsky72e47ef2008-04-18 17:48:16 +0000709 useGradient = true;
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000710 } else if (filter_id != rfb.TightFilterCopy) {
Constantin Kaplinsky72e47ef2008-04-18 17:48:16 +0000711 throw new Exception("Incorrect tight filter id: " + filter_id);
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000712 }
713 }
Constantin Kaplinsky903009e2002-05-20 10:55:47 +0000714 if (numColors == 0)
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000715 rowSize *= 3;
716
717 // Read, optionally uncompress and decode data.
718 int dataSize = h * rowSize;
719 if (dataSize < rfb.TightMinToCompress) {
720 // Data size is small - not compressed with zlib.
721 if (numColors != 0) {
Constantin Kaplinsky72e47ef2008-04-18 17:48:16 +0000722 // Indexed colors.
723 byte[] indexedData = new byte[dataSize];
724 rfb.is.readFully(indexedData);
725 if (numColors == 2) {
726 // Two colors.
727 decodeMonoData(x, y, w, h, indexedData, palette24);
728 } else {
729 // 3..255 colors.
730 int i = 0;
731 for (int dy = y; dy < y + h; dy++) {
732 for (int dx = x; dx < x + w; dx++) {
733 pixels24[dy * rfb.framebufferWidth + dx] =
734 palette24[indexedData[i++] & 0xFF];
735 }
736 }
737 }
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000738 } else if (useGradient) {
Constantin Kaplinsky72e47ef2008-04-18 17:48:16 +0000739 // "Gradient"-processed data
740 byte[] buf = new byte[w * h * 3];
741 rfb.is.readFully(buf);
742 decodeGradientData(x, y, w, h, buf);
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000743 } else {
Constantin Kaplinsky72e47ef2008-04-18 17:48:16 +0000744 // Raw truecolor data.
745 byte[] buf = new byte[w * 3];
746 int i, offset;
747 for (int dy = y; dy < y + h; dy++) {
748 rfb.is.readFully(buf);
749 offset = dy * rfb.framebufferWidth + x;
750 for (i = 0; i < w; i++) {
751 pixels24[offset + i] =
752 (buf[i * 3] & 0xFF) << 16 |
753 (buf[i * 3 + 1] & 0xFF) << 8 |
754 (buf[i * 3 + 2] & 0xFF);
755 }
756 }
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000757 }
758 } else {
759 // Data was compressed with zlib.
760 int zlibDataLen = rfb.readCompactLen();
761 byte[] zlibData = new byte[zlibDataLen];
762 rfb.is.readFully(zlibData);
763 int stream_id = comp_ctl & 0x03;
764 if (tightInflaters[stream_id] == null) {
Constantin Kaplinsky72e47ef2008-04-18 17:48:16 +0000765 tightInflaters[stream_id] = new Inflater();
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000766 }
767 Inflater myInflater = tightInflaters[stream_id];
768 myInflater.setInput(zlibData);
769 try {
Constantin Kaplinsky72e47ef2008-04-18 17:48:16 +0000770 if (numColors != 0) {
771 // Indexed colors.
772 byte[] indexedData = new byte[dataSize];
773 myInflater.inflate(indexedData);
774 if (numColors == 2) {
775 // Two colors.
776 decodeMonoData(x, y, w, h, indexedData, palette24);
777 } else {
778 // More than two colors.
779 int i = 0;
780 for (int dy = y; dy < y + h; dy++) {
781 for (int dx = x; dx < x + w; dx++) {
782 pixels24[dy * rfb.framebufferWidth + dx] =
783 palette24[indexedData[i++] & 0xFF];
784 }
785 }
786 }
787 } else if (useGradient) {
788 // Compressed "Gradient"-filtered data.
789 byte[] buf = new byte[w * h * 3];
790 myInflater.inflate(buf);
791 decodeGradientData(x, y, w, h, buf);
792 } else {
793 // Compressed truecolor data.
794 byte[] buf = new byte[w * 3];
795 int i, offset;
796 for (int dy = y; dy < y + h; dy++) {
797 myInflater.inflate(buf);
798 offset = dy * rfb.framebufferWidth + x;
799 for (i = 0; i < w; i++) {
800 pixels24[offset + i] =
801 (buf[i * 3] & 0xFF) << 16 |
802 (buf[i * 3 + 1] & 0xFF) << 8 |
803 (buf[i * 3 + 2] & 0xFF);
804 }
805 }
806 }
807 } catch (DataFormatException dfe) {
808 throw new Exception(dfe.toString());
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000809 }
810 }
811
812 handleUpdatedPixels(x, y, w, h);
813 scheduleRepaint(x, y, w, h);
814 }
815
816 //
Constantin Kaplinsky903009e2002-05-20 10:55:47 +0000817 // Decode 1bpp-encoded bi-color rectangle.
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000818 //
Constantin Kaplinsky99df0a72002-06-04 06:13:20 +0000819 void decodeMonoData(int x, int y, int w, int h, byte[] src, int[] palette) {
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000820
821 int dx, dy, n;
822 int i = y * rfb.framebufferWidth + x;
823 int rowBytes = (w + 7) / 8;
824 byte b;
825
826 for (dy = 0; dy < h; dy++) {
827 for (dx = 0; dx < w / 8; dx++) {
Constantin Kaplinsky72e47ef2008-04-18 17:48:16 +0000828 b = src[dy * rowBytes + dx];
829 for (n = 7; n >= 0; n--)
830 pixels24[i++] = palette[b >> n & 1];
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000831 }
832 for (n = 7; n >= 8 - w % 8; n--) {
Constantin Kaplinsky72e47ef2008-04-18 17:48:16 +0000833 pixels24[i++] = palette[src[dy * rowBytes + dx] >> n & 1];
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000834 }
835 i += (rfb.framebufferWidth - w);
836 }
837 }
838
839 //
840 // Decode data processed with the "Gradient" filter.
841 //
Constantin Kaplinsky72e47ef2008-04-18 17:48:16 +0000842 void decodeGradientData(int x, int y, int w, int h, byte[] buf) {
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000843
844 int dx, dy, c;
845 byte[] prevRow = new byte[w * 3];
846 byte[] thisRow = new byte[w * 3];
847 byte[] pix = new byte[3];
848 int[] est = new int[3];
849
850 int offset = y * rfb.framebufferWidth + x;
851
852 for (dy = 0; dy < h; dy++) {
853
854 /* First pixel in a row */
855 for (c = 0; c < 3; c++) {
Constantin Kaplinsky72e47ef2008-04-18 17:48:16 +0000856 pix[c] = (byte)(prevRow[c] + buf[dy * w * 3 + c]);
857 thisRow[c] = pix[c];
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000858 }
859 pixels24[offset++] =
Constantin Kaplinsky72e47ef2008-04-18 17:48:16 +0000860 (pix[0] & 0xFF) << 16 | (pix[1] & 0xFF) << 8 | (pix[2] & 0xFF);
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000861
862 /* Remaining pixels of a row */
863 for (dx = 1; dx < w; dx++) {
Constantin Kaplinsky72e47ef2008-04-18 17:48:16 +0000864 for (c = 0; c < 3; c++) {
865 est[c] = ((prevRow[dx * 3 + c] & 0xFF) + (pix[c] & 0xFF) -
866 (prevRow[(dx - 1) * 3 + c] & 0xFF));
867 if (est[c] > 0xFF) {
868 est[c] = 0xFF;
869 } else if (est[c] < 0x00) {
870 est[c] = 0x00;
871 }
872 pix[c] = (byte)(est[c] + buf[(dy * w + dx) * 3 + c]);
873 thisRow[dx * 3 + c] = pix[c];
874 }
875 pixels24[offset++] =
876 (pix[0] & 0xFF) << 16 | (pix[1] & 0xFF) << 8 | (pix[2] & 0xFF);
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000877 }
878
879 System.arraycopy(thisRow, 0, prevRow, 0, w * 3);
880 offset += (rfb.framebufferWidth - w);
881 }
882 }
883
884
885 //
886 // Display newly updated area of pixels.
887 //
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000888 void handleUpdatedPixels(int x, int y, int w, int h) {
889
890 // Draw updated pixels of the off-screen image.
Constantin Kaplinsky903009e2002-05-20 10:55:47 +0000891
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000892 pixelsSource.newPixels(x, y, w, h);
893 memGraphics.setClip(x, y, w, h);
894 memGraphics.drawImage(rawPixelsImage, 0, 0, null);
895 memGraphics.setClip(0, 0, rfb.framebufferWidth, rfb.framebufferHeight);
896 }
897
wimba.coma5a4f4f2004-09-21 15:25:05 +0000898 //////////////////////////////////////////////////////////////////
899 //
900 // Handle cursor shape updates (XCursor and RichCursor encodings).
901 //
902 boolean showSoftCursor = false;
903
904 int[] softCursorPixels;
905 MemoryImageSource softCursorSource;
906 Image softCursor;
907
908 int cursorX = 0, cursorY = 0;
909 int cursorWidth, cursorHeight;
910 int origCursorWidth, origCursorHeight;
911 int hotX, hotY;
912 int origHotX, origHotY;
913 int deferCursorUpdates = 10;
914
915 //
916 // Handle cursor shape update (XCursor and RichCursor encodings).
917 //
918 synchronized void handleCursorShapeUpdate(int encodingType,
919 int xhot, int yhot, int width,
920 int height)
921 throws IOException {
922
923 int bytesPerRow = (width + 7) / 8;
924 int bytesMaskData = bytesPerRow * height;
925
926 softCursorFree();
927
928 if (width * height == 0)
929 return;
930
931// // Ignore cursor shape data if requested by user.
932//
933// if (viewer.options.ignoreCursorUpdates) {
934// if (encodingType == rfb.EncodingXCursor) {
935// rfb.is.skipBytes(6 + bytesMaskData * 2);
936// } else {
937// // rfb.EncodingRichCursor
938// rfb.is.skipBytes(width * height + bytesMaskData);
939// }
940// return;
941// }
942
943 // Decode cursor pixel data.
944
945 softCursorPixels = new int[width * height];
946
947 if (encodingType == rfb.EncodingXCursor) {
wimba.coma5a4f4f2004-09-21 15:25:05 +0000948
949 // Read foreground and background colors of the cursor.
950 byte[] rgb = new byte[6];
951 rfb.is.readFully(rgb);
952 int[] colors = {(0xFF000000 | (rgb[3] & 0xFF) << 16 |
953 (rgb[4] & 0xFF) << 8 | (rgb[5] & 0xFF)),
954 (0xFF000000 | (rgb[0] & 0xFF) << 16 |
955 (rgb[1] & 0xFF) << 8 | (rgb[2] & 0xFF))
956 };
wimba.coma5a4f4f2004-09-21 15:25:05 +0000957
958 // Read pixel and mask data.
959 byte[] pixBuf = new byte[bytesMaskData];
960 rfb.is.readFully(pixBuf);
961 byte[] maskBuf = new byte[bytesMaskData];
962 rfb.is.readFully(maskBuf);
963
964 // Decode pixel data into softCursorPixels[].
965 byte pixByte, maskByte;
966 int x, y, n, result;
967 int i = 0;
968 for (y = 0; y < height; y++) {
969 for (x = 0; x < width / 8; x++) {
970 pixByte = pixBuf[y * bytesPerRow + x];
971 maskByte = maskBuf[y * bytesPerRow + x];
972 for (n = 7; n >= 0; n--) {
973 if ((maskByte >> n & 1) != 0) {
974 result = colors[pixByte >> n & 1];
975 } else {
976 result = 0; // Transparent pixel
977 }
978 softCursorPixels[i++] = result;
979 }
980 }
981 for (n = 7; n >= 8 - width % 8; n--) {
982 if ((maskBuf[y * bytesPerRow + x] >> n & 1) != 0) {
983 result = colors[pixBuf[y * bytesPerRow + x] >> n & 1];
984 } else {
985 result = 0; // Transparent pixel
986 }
987 softCursorPixels[i++] = result;
988 }
989 }
990
991 } else {
992 // encodingType == rfb.EncodingRichCursor
wimba.coma5a4f4f2004-09-21 15:25:05 +0000993
994 // Read pixel and mask data.
995 byte[] pixBuf = new byte[width * height * 4];
996 rfb.is.readFully(pixBuf);
997 byte[] maskBuf = new byte[bytesMaskData];
998 rfb.is.readFully(maskBuf);
999
1000 // Decode pixel data into softCursorPixels[].
1001 byte pixByte, maskByte;
1002 int x, y, n, result;
1003 int i = 0;
1004 for (y = 0; y < height; y++) {
1005 for (x = 0; x < width / 8; x++) {
1006 maskByte = maskBuf[y * bytesPerRow + x];
1007 for (n = 7; n >= 0; n--) {
1008 if ((maskByte >> n & 1) != 0) {
1009// if (bytesPerPixel == 1) {
1010// result = cm8.getRGB(pixBuf[i]);
1011// } else {
1012 result = (pixBuf[i * 4] & 0xFF) << 24 |
1013 (pixBuf[i * 4 + 1] & 0xFF) << 16 |
1014 (pixBuf[i * 4 + 2] & 0xFF) << 8 |
1015 (pixBuf[i * 4 + 3] & 0xFF);
1016 //result = 0xFF000000 |
1017 // (pixBuf[i * 4 + 1] & 0xFF) << 16 |
1018 // (pixBuf[i * 4 + 2] & 0xFF) << 8 |
1019 // (pixBuf[i * 4 + 3] & 0xFF);
1020// }
1021 } else {
1022 result = 0; // Transparent pixel
1023 }
1024 softCursorPixels[i++] = result;
1025 }
1026 }
1027
1028 for (n = 7; n >= 8 - width % 8; n--) {
1029 if ((maskBuf[y * bytesPerRow + x] >> n & 1) != 0) {
1030// if (bytesPerPixel == 1) {
1031// result = cm8.getRGB(pixBuf[i]);
1032// } else {
1033 result = 0xFF000000 |
1034 (pixBuf[i * 4 + 1] & 0xFF) << 16 |
1035 (pixBuf[i * 4 + 2] & 0xFF) << 8 |
1036 (pixBuf[i * 4 + 3] & 0xFF);
1037// }
1038 } else {
1039 result = 0; // Transparent pixel
1040 }
1041 softCursorPixels[i++] = result;
1042 }
1043 }
1044
1045 }
1046
1047 // Draw the cursor on an off-screen image.
1048
1049 softCursorSource =
1050 new MemoryImageSource(width, height, softCursorPixels, 0, width);
1051 softCursor = Toolkit.getDefaultToolkit().createImage(softCursorSource);
1052// if (inputEnabled && viewer.options.scaleCursor != 0) {
1053// int w = (width * viewer.options.scaleCursor) / 100;
1054// int h = (height * viewer.options.scaleCursor) / 100;
1055// Image newCursor = softCursor.getScaledInstance(w, h, Image.SCALE_SMOOTH);
1056// softCursor = newCursor;
1057// cursorWidth = w;
1058// cursorHeight = h;
1059// hotX = (xhot * viewer.options.scaleCursor) / 100;
1060// hotY = (yhot * viewer.options.scaleCursor) / 100;
1061// } else {
1062 cursorWidth = width;
1063 cursorHeight = height;
1064 hotX = xhot;
1065 hotY = yhot;
1066// }
1067
1068 // Set data associated with cursor.
1069 origCursorWidth = width;
1070 origCursorHeight = height;
1071 origHotX = xhot;
1072 origHotY = yhot;
1073
1074 showSoftCursor = true;
1075
1076 // Show the cursor.
1077
1078 repaint(deferCursorUpdates,
1079 cursorX - hotX, cursorY - hotY, cursorWidth, cursorHeight);
1080 }
1081
1082 //
1083 // softCursorMove(). Moves soft cursor into a particular location.
1084 //
1085 synchronized void softCursorMove(int x, int y) {
wimba.comccb67c32005-01-03 16:20:38 +00001086 Point o = getImageOrigin();
1087 int oldX = cursorX + o.x;
1088 int oldY = cursorY + o.y;
1089 cursorX = x;
1090 cursorY = y;
wimba.coma5a4f4f2004-09-21 15:25:05 +00001091 if (showSoftCursor) {
1092 repaint(deferCursorUpdates,
wimba.comccb67c32005-01-03 16:20:38 +00001093 oldX - hotX, oldY - hotY, cursorWidth, cursorHeight);
wimba.coma5a4f4f2004-09-21 15:25:05 +00001094 repaint(deferCursorUpdates,
wimba.comccb67c32005-01-03 16:20:38 +00001095 cursorX - hotX + o.x, cursorY - hotY + o.y, cursorWidth,
1096 cursorHeight);
wimba.coma5a4f4f2004-09-21 15:25:05 +00001097
wimba.com16092722004-09-21 20:54:37 +00001098 // Automatic viewport scrolling
1099 if (player.desktopScrollPane != null) {
1100 boolean needScroll = false;
1101 Dimension d = player.desktopScrollPane.getSize();
1102 Point topLeft = player.desktopScrollPane.getScrollPosition();
1103 Point botRight = new Point(topLeft.x + d.width, topLeft.y + d.height);
1104
1105 if (x < topLeft.x + SCROLL_MARGIN) {
1106 // shift left
1107 topLeft.x = x - SCROLL_MARGIN;
1108 needScroll = true;
1109 } else if (x > botRight.x - SCROLL_MARGIN) {
1110 // shift right
1111 topLeft.x = x - d.width + SCROLL_MARGIN;
1112 needScroll = true;
1113 }
1114 if (y < topLeft.y + SCROLL_MARGIN) {
1115 // shift up
1116 topLeft.y = y - SCROLL_MARGIN;
1117 needScroll = true;
1118 } else if (y > botRight.y - SCROLL_MARGIN) {
1119 // shift down
1120 topLeft.y = y - d.height + SCROLL_MARGIN;
1121 needScroll = true;
1122 }
1123 player.desktopScrollPane.setScrollPosition(topLeft.x, topLeft.y);
1124 }
wimba.coma5a4f4f2004-09-21 15:25:05 +00001125 }
1126
1127 cursorX = x;
1128 cursorY = y;
1129 }
1130 //
1131 // softCursorFree(). Remove soft cursor, dispose resources.
1132 //
1133 synchronized void softCursorFree() {
1134 if (showSoftCursor) {
1135 showSoftCursor = false;
1136 softCursor = null;
1137 softCursorSource = null;
1138 softCursorPixels = null;
1139
wimba.comccb67c32005-01-03 16:20:38 +00001140 Point o = getImageOrigin();
wimba.coma5a4f4f2004-09-21 15:25:05 +00001141 repaint(deferCursorUpdates,
wimba.comccb67c32005-01-03 16:20:38 +00001142 cursorX - hotX + o.x, cursorY - hotY + o.y, cursorWidth,
1143 cursorHeight);
wimba.coma5a4f4f2004-09-21 15:25:05 +00001144 }
1145 }
Constantin Kaplinsky1215b992008-04-18 09:51:44 +00001146 //
1147 // Tell JVM to repaint specified desktop area.
1148 //
Constantin Kaplinsky1215b992008-04-18 09:51:44 +00001149 void scheduleRepaint(int x, int y, int w, int h) {
Constantin Kaplinsky37cc43e2002-05-30 17:30:11 +00001150 if (rfb.fbs.isSeeking()) {
Constantin Kaplinsky52c48242002-05-30 13:26:34 +00001151 // Do nothing, and remember we are seeking.
1152 seekMode = true;
1153 } else {
1154 if (seekMode) {
Constantin Kaplinsky72e47ef2008-04-18 17:48:16 +00001155 // Immediate repaint of the whole desktop after seeking.
1156 repaint();
Constantin Kaplinsky52c48242002-05-30 13:26:34 +00001157 } else {
Constantin Kaplinsky72e47ef2008-04-18 17:48:16 +00001158 // Usual incremental repaint.
wimba.comccb67c32005-01-03 16:20:38 +00001159 Point o = getImageOrigin();
1160 repaint(player.deferScreenUpdates, o.x + x, o.y + y, w, h);
Constantin Kaplinsky52c48242002-05-30 13:26:34 +00001161 }
1162 seekMode = false;
1163 }
Constantin Kaplinsky1215b992008-04-18 09:51:44 +00001164 }
1165
Constantin Kaplinsky5a7133a2002-07-24 17:02:04 +00001166 //
1167 // We are observing our FbsInputStream object to get notified on
1168 // switching to the `paused' mode. In such cases we want to repaint
1169 // our desktop if we were seeking.
1170 //
Constantin Kaplinsky5a7133a2002-07-24 17:02:04 +00001171 public void update(Observable o, Object arg) {
1172 // Immediate repaint of the whole desktop after seeking.
1173 repaint();
1174 // Let next scheduleRepaint() call invoke incremental drawing.
1175 seekMode = false;
1176 }
1177
Constantin Kaplinsky1215b992008-04-18 09:51:44 +00001178}