Constantin Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 1 | // |
| 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.com | c23aeb0 | 2004-09-16 00:00:00 +0000 | [diff] [blame] | 23 | package com.HorizonLive.RfbPlayer; |
| 24 | |
Constantin Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 25 | import java.awt.*; |
| 26 | import java.awt.event.*; |
| 27 | import java.awt.image.*; |
| 28 | import java.io.*; |
| 29 | import java.lang.*; |
Constantin Kaplinsky | 5a7133a | 2002-07-24 17:02:04 +0000 | [diff] [blame] | 30 | import java.util.*; |
Constantin Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 31 | import java.util.zip.*; |
| 32 | |
| 33 | |
| 34 | // |
| 35 | // VncCanvas is a subclass of Canvas which draws a VNC desktop on it. |
| 36 | // |
Constantin Kaplinsky | 5a7133a | 2002-07-24 17:02:04 +0000 | [diff] [blame] | 37 | class VncCanvas extends Canvas implements Observer { |
Constantin Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 38 | |
Constantin Kaplinsky | 903009e | 2002-05-20 10:55:47 +0000 | [diff] [blame] | 39 | RfbPlayer player; |
Constantin Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 40 | RfbProto rfb; |
Constantin Kaplinsky | 903009e | 2002-05-20 10:55:47 +0000 | [diff] [blame] | 41 | ColorModel cm24; |
Constantin Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 42 | |
| 43 | Image memImage; |
| 44 | Graphics memGraphics; |
| 45 | |
| 46 | Image rawPixelsImage; |
| 47 | MemoryImageSource pixelsSource; |
Constantin Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 48 | 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 Kaplinsky | 52c4824 | 2002-05-30 13:26:34 +0000 | [diff] [blame] | 65 | // 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.com | 1609272 | 2004-09-21 20:54:37 +0000 | [diff] [blame] | 70 | // Distance of mouse from viewer border to trigger automatic scrolling |
| 71 | final static int SCROLL_MARGIN = 50; |
| 72 | |
wimba.com | ccb67c3 | 2005-01-03 16:20:38 +0000 | [diff] [blame^] | 73 | // Color for border around small shared area |
| 74 | static final Color DARK_GRAY = new Color(132, 138, 156); |
| 75 | |
Constantin Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 76 | // |
| 77 | // The constructor. |
| 78 | // |
Constantin Kaplinsky | 903009e | 2002-05-20 10:55:47 +0000 | [diff] [blame] | 79 | VncCanvas(RfbPlayer player) throws IOException { |
| 80 | this.player = player; |
| 81 | rfb = player.rfb; |
Constantin Kaplinsky | 52c4824 | 2002-05-30 13:26:34 +0000 | [diff] [blame] | 82 | seekMode = false; |
Constantin Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 83 | |
Constantin Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 84 | cm24 = new DirectColorModel(24, 0xFF0000, 0x00FF00, 0x0000FF); |
| 85 | |
Constantin Kaplinsky | 903009e | 2002-05-20 10:55:47 +0000 | [diff] [blame] | 86 | updateFramebufferSize(); |
wimba.com | ccb67c3 | 2005-01-03 16:20:38 +0000 | [diff] [blame^] | 87 | |
| 88 | setBackground(Color.white); |
Constantin Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | // |
| 92 | // Callback methods to determine geometry of our Component. |
| 93 | // |
Constantin Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 94 | public Dimension getPreferredSize() { |
wimba.com | ccb67c3 | 2005-01-03 16:20:38 +0000 | [diff] [blame^] | 95 | 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 Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | public Dimension getMinimumSize() { |
wimba.com | ccb67c3 | 2005-01-03 16:20:38 +0000 | [diff] [blame^] | 105 | return getPreferredSize(); |
Constantin Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | public Dimension getMaximumSize() { |
wimba.com | ccb67c3 | 2005-01-03 16:20:38 +0000 | [diff] [blame^] | 109 | 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 Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | // |
| 123 | // All painting is performed here. |
| 124 | // |
Constantin Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 125 | public void update(Graphics g) { |
| 126 | paint(g); |
| 127 | } |
| 128 | |
wimba.com | c219ca8 | 2004-09-22 15:10:14 +0000 | [diff] [blame] | 129 | public synchronized void paint(Graphics g) { |
wimba.com | ccb67c3 | 2005-01-03 16:20:38 +0000 | [diff] [blame^] | 130 | Point o = getImageOrigin(); |
| 131 | Dimension d = getPreferredSize(); |
Constantin Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 132 | synchronized(memImage) { |
wimba.com | ccb67c3 | 2005-01-03 16:20:38 +0000 | [diff] [blame^] | 133 | 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 Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 151 | } |
wimba.com | a5a4f4f | 2004-09-21 15:25:05 +0000 | [diff] [blame] | 152 | if (showSoftCursor) { |
| 153 | int x0 = cursorX - hotX, y0 = cursorY - hotY; |
wimba.com | ccb67c3 | 2005-01-03 16:20:38 +0000 | [diff] [blame^] | 154 | x0 += o.x; |
| 155 | y0 += o.y; |
wimba.com | a5a4f4f | 2004-09-21 15:25:05 +0000 | [diff] [blame] | 156 | Rectangle r = new Rectangle(x0, y0, cursorWidth, cursorHeight); |
| 157 | if (r.intersects(g.getClipBounds())) { |
wimba.com | ccb67c3 | 2005-01-03 16:20:38 +0000 | [diff] [blame^] | 158 | Rectangle c = g.getClipBounds(); |
| 159 | g.setClip(o.x, o.y, rfb.framebufferWidth, rfb.framebufferHeight); |
wimba.com | a5a4f4f | 2004-09-21 15:25:05 +0000 | [diff] [blame] | 160 | g.drawImage(softCursor, x0, y0, null); |
wimba.com | ccb67c3 | 2005-01-03 16:20:38 +0000 | [diff] [blame^] | 161 | g.setClip(c.x, c.y, c.width, c.height); |
wimba.com | a5a4f4f | 2004-09-21 15:25:05 +0000 | [diff] [blame] | 162 | } |
| 163 | } |
Constantin Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | // |
| 167 | // Override the ImageObserver interface method to handle drawing of |
| 168 | // JPEG-encoded data. |
| 169 | // |
Constantin Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 170 | public boolean imageUpdate(Image img, int infoflags, |
Constantin Kaplinsky | 72e47ef | 2008-04-18 17:48:16 +0000 | [diff] [blame] | 171 | int x, int y, int width, int height) { |
Constantin Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 172 | 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 Kaplinsky | 72e47ef | 2008-04-18 17:48:16 +0000 | [diff] [blame] | 177 | 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 Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 185 | } |
| 186 | return false; // All image data was processed. |
| 187 | } |
| 188 | } |
| 189 | |
Constantin Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 190 | 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 Kaplinsky | 903009e | 2002-05-20 10:55:47 +0000 | [diff] [blame] | 200 | memImage = player.createImage(fbWidth, fbHeight); |
Constantin Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 201 | memGraphics = memImage.getGraphics(); |
| 202 | } else if (memImage.getWidth(null) != fbWidth || |
Constantin Kaplinsky | 72e47ef | 2008-04-18 17:48:16 +0000 | [diff] [blame] | 203 | memImage.getHeight(null) != fbHeight) { |
Constantin Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 204 | synchronized(memImage) { |
Constantin Kaplinsky | 72e47ef | 2008-04-18 17:48:16 +0000 | [diff] [blame] | 205 | memImage = player.createImage(fbWidth, fbHeight); |
| 206 | memGraphics = memImage.getGraphics(); |
Constantin Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 207 | } |
| 208 | } |
| 209 | |
| 210 | // Images with raw pixels should be re-allocated on every change |
| 211 | // of geometry or pixel format. |
Constantin Kaplinsky | 903009e | 2002-05-20 10:55:47 +0000 | [diff] [blame] | 212 | pixels24 = new int[fbWidth * fbHeight]; |
| 213 | pixelsSource = |
Constantin Kaplinsky | 72e47ef | 2008-04-18 17:48:16 +0000 | [diff] [blame] | 214 | new MemoryImageSource(fbWidth, fbHeight, cm24, pixels24, 0, fbWidth); |
Constantin Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 215 | pixelsSource.setAnimated(true); |
| 216 | rawPixelsImage = createImage(pixelsSource); |
| 217 | |
| 218 | // Update the size of desktop containers. |
Constantin Kaplinsky | 903009e | 2002-05-20 10:55:47 +0000 | [diff] [blame] | 219 | if (player.inSeparateFrame) { |
| 220 | if (player.desktopScrollPane != null) |
Constantin Kaplinsky | 72e47ef | 2008-04-18 17:48:16 +0000 | [diff] [blame] | 221 | resizeDesktopFrame(); |
Constantin Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 222 | } else { |
| 223 | setSize(fbWidth, fbHeight); |
| 224 | } |
| 225 | } |
| 226 | |
| 227 | void resizeDesktopFrame() { |
wimba.com | 252eb3b | 2004-09-21 21:27:54 +0000 | [diff] [blame] | 228 | // determine screen size |
Constantin Kaplinsky | 903009e | 2002-05-20 10:55:47 +0000 | [diff] [blame] | 229 | Dimension screenSize = player.vncFrame.getToolkit().getScreenSize(); |
wimba.com | 252eb3b | 2004-09-21 21:27:54 +0000 | [diff] [blame] | 230 | Dimension scrollSize = player.desktopScrollPane.getSize(); |
wimba.com | c23aeb0 | 2004-09-16 00:00:00 +0000 | [diff] [blame] | 231 | |
| 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.com | 252eb3b | 2004-09-21 21:27:54 +0000 | [diff] [blame] | 236 | // 2) Taxkbar on Windows (usually about 28 px on bottom) |
| 237 | // 3) Other obstructions. |
wimba.com | c23aeb0 | 2004-09-16 00:00:00 +0000 | [diff] [blame] | 238 | screenSize.height -= 30; |
| 239 | screenSize.width -= 30; |
| 240 | |
wimba.com | 252eb3b | 2004-09-21 21:27:54 +0000 | [diff] [blame] | 241 | // 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.com | c23aeb0 | 2004-09-16 00:00:00 +0000 | [diff] [blame] | 246 | |
wimba.com | 252eb3b | 2004-09-21 21:27:54 +0000 | [diff] [blame] | 247 | // 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 Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 256 | } |
wimba.com | 252eb3b | 2004-09-21 21:27:54 +0000 | [diff] [blame] | 257 | if (h > screenSize.height) { |
| 258 | h = screenSize.height; |
| 259 | needResize = true; |
Constantin Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 260 | } |
wimba.com | 252eb3b | 2004-09-21 21:27:54 +0000 | [diff] [blame] | 261 | if (needResize) |
| 262 | player.desktopScrollPane.setSize(w, h); |
Constantin Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 263 | |
wimba.com | 252eb3b | 2004-09-21 21:27:54 +0000 | [diff] [blame] | 264 | player.vncFrame.pack(); |
wimba.com | ccb67c3 | 2005-01-03 16:20:38 +0000 | [diff] [blame^] | 265 | |
| 266 | // size the canvas |
| 267 | setSize(getPreferredSize()); |
wimba.com | 252eb3b | 2004-09-21 21:27:54 +0000 | [diff] [blame] | 268 | } |
| 269 | |
| 270 | void resizeEmbeddedApplet() { |
wimba.com | 252eb3b | 2004-09-21 21:27:54 +0000 | [diff] [blame] | 271 | // resize scroll pane if necessary |
| 272 | Dimension scrollSize = player.desktopScrollPane.getSize(); |
| 273 | if (scrollSize.width != player.dispW || |
wimba.com | ccb67c3 | 2005-01-03 16:20:38 +0000 | [diff] [blame^] | 274 | scrollSize.height != player.dispH) { |
wimba.com | 252eb3b | 2004-09-21 21:27:54 +0000 | [diff] [blame] | 275 | player.desktopScrollPane.setSize(player.dispW, player.dispH); |
wimba.com | ccb67c3 | 2005-01-03 16:20:38 +0000 | [diff] [blame^] | 276 | player.desktopScrollPane.validate(); |
| 277 | } |
wimba.com | 252eb3b | 2004-09-21 21:27:54 +0000 | [diff] [blame] | 278 | |
wimba.com | ccb67c3 | 2005-01-03 16:20:38 +0000 | [diff] [blame^] | 279 | // size the canvas |
| 280 | setSize(getPreferredSize()); |
Constantin Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 281 | } |
| 282 | |
| 283 | // |
Constantin Kaplinsky | 99df0a7 | 2002-06-04 06:13:20 +0000 | [diff] [blame] | 284 | // processNormalProtocol() - executed by the rfbThread to deal with |
| 285 | // the RFB data. |
Constantin Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 286 | // |
Constantin Kaplinsky | 99df0a7 | 2002-06-04 06:13:20 +0000 | [diff] [blame] | 287 | public void processNormalProtocol() throws Exception { |
Constantin Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 288 | |
Constantin Kaplinsky | 903009e | 2002-05-20 10:55:47 +0000 | [diff] [blame] | 289 | zlibInflater = new Inflater(); |
| 290 | tightInflaters = new Inflater[4]; |
Constantin Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 291 | |
Constantin Kaplinsky | 5a7133a | 2002-07-24 17:02:04 +0000 | [diff] [blame] | 292 | // Show current time position in the control panel. |
Constantin Kaplinsky | fe07983 | 2002-05-29 00:52:32 +0000 | [diff] [blame] | 293 | player.updatePos(); |
| 294 | |
Constantin Kaplinsky | 5a7133a | 2002-07-24 17:02:04 +0000 | [diff] [blame] | 295 | // Tell our FbsInputStream object to notify us when it goes to the |
| 296 | // `paused' mode. |
| 297 | rfb.fbs.addObserver(this); |
| 298 | |
Constantin Kaplinsky | 903009e | 2002-05-20 10:55:47 +0000 | [diff] [blame] | 299 | // Main dispatch loop. |
Constantin Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 300 | |
| 301 | while (true) { |
Constantin Kaplinsky | 903009e | 2002-05-20 10:55:47 +0000 | [diff] [blame] | 302 | |
Constantin Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 303 | int msgType = rfb.readServerMessageType(); |
| 304 | |
| 305 | switch (msgType) { |
| 306 | case RfbProto.FramebufferUpdate: |
Constantin Kaplinsky | 72e47ef | 2008-04-18 17:48:16 +0000 | [diff] [blame] | 307 | rfb.readFramebufferUpdate(); |
Constantin Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 308 | |
Constantin Kaplinsky | 72e47ef | 2008-04-18 17:48:16 +0000 | [diff] [blame] | 309 | for (int i = 0; i < rfb.updateNRects; i++) { |
| 310 | rfb.readFramebufferUpdateRectHdr(); |
wimba.com | a5a4f4f | 2004-09-21 15:25:05 +0000 | [diff] [blame] | 311 | |
| 312 | boolean cursorPosReceived = false; |
| 313 | |
Constantin Kaplinsky | 72e47ef | 2008-04-18 17:48:16 +0000 | [diff] [blame] | 314 | int rx = rfb.updateRectX, ry = rfb.updateRectY; |
| 315 | int rw = rfb.updateRectW, rh = rfb.updateRectH; |
Constantin Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 316 | |
Constantin Kaplinsky | 72e47ef | 2008-04-18 17:48:16 +0000 | [diff] [blame] | 317 | if (rfb.updateRectEncoding == rfb.EncodingLastRect) |
| 318 | break; |
Constantin Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 319 | |
Constantin Kaplinsky | 72e47ef | 2008-04-18 17:48:16 +0000 | [diff] [blame] | 320 | if (rfb.updateRectEncoding == rfb.EncodingNewFBSize) { |
wimba.com | b7017b7 | 2004-09-16 16:11:55 +0000 | [diff] [blame] | 321 | if (rfb.updateRectW != 0 && rfb.updateRectH != 0) { |
| 322 | rfb.setFramebufferSize(rfb.updateRectW, rfb.updateRectH); |
| 323 | updateFramebufferSize(); |
| 324 | } |
Constantin Kaplinsky | 72e47ef | 2008-04-18 17:48:16 +0000 | [diff] [blame] | 325 | break; |
| 326 | } |
Constantin Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 327 | |
Constantin Kaplinsky | 72e47ef | 2008-04-18 17:48:16 +0000 | [diff] [blame] | 328 | if (rfb.updateRectEncoding == rfb.EncodingXCursor || |
| 329 | rfb.updateRectEncoding == rfb.EncodingRichCursor) { |
wimba.com | a5a4f4f | 2004-09-21 15:25:05 +0000 | [diff] [blame] | 330 | 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 Kaplinsky | 72e47ef | 2008-04-18 17:48:16 +0000 | [diff] [blame] | 343 | } |
Constantin Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 344 | |
Constantin Kaplinsky | 72e47ef | 2008-04-18 17:48:16 +0000 | [diff] [blame] | 345 | 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 Kaplinsky | 99df0a7 | 2002-06-04 06:13:20 +0000 | [diff] [blame] | 362 | handleZlibRect(rx, ry, rw, rh); |
Constantin Kaplinsky | 72e47ef | 2008-04-18 17:48:16 +0000 | [diff] [blame] | 363 | break; |
| 364 | case RfbProto.EncodingTight: |
| 365 | handleTightRect(rx, ry, rw, rh); |
| 366 | break; |
| 367 | default: |
| 368 | throw new Exception("Unknown RFB rectangle encoding " + |
wimba.com | a5a4f4f | 2004-09-21 15:25:05 +0000 | [diff] [blame] | 369 | Integer.toString(rfb.updateRectEncoding, 16)); |
Constantin Kaplinsky | 72e47ef | 2008-04-18 17:48:16 +0000 | [diff] [blame] | 370 | } |
| 371 | } |
| 372 | break; |
Constantin Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 373 | |
| 374 | case RfbProto.SetColourMapEntries: |
Constantin Kaplinsky | 72e47ef | 2008-04-18 17:48:16 +0000 | [diff] [blame] | 375 | throw new Exception("Can't handle SetColourMapEntries message"); |
Constantin Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 376 | |
| 377 | case RfbProto.Bell: |
| 378 | Toolkit.getDefaultToolkit().beep(); |
Constantin Kaplinsky | 72e47ef | 2008-04-18 17:48:16 +0000 | [diff] [blame] | 379 | break; |
Constantin Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 380 | |
| 381 | case RfbProto.ServerCutText: |
Constantin Kaplinsky | 72e47ef | 2008-04-18 17:48:16 +0000 | [diff] [blame] | 382 | String s = rfb.readServerCutText(); |
| 383 | break; |
Constantin Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 384 | |
| 385 | default: |
wimba.com | a5a4f4f | 2004-09-21 15:25:05 +0000 | [diff] [blame] | 386 | throw new Exception("Unknown RFB message type " + |
| 387 | Integer.toString(msgType, 16)); |
Constantin Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 388 | } |
Constantin Kaplinsky | 903009e | 2002-05-20 10:55:47 +0000 | [diff] [blame] | 389 | |
Constantin Kaplinsky | fe07983 | 2002-05-29 00:52:32 +0000 | [diff] [blame] | 390 | player.updatePos(); |
Constantin Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 391 | } |
| 392 | } |
| 393 | |
| 394 | |
| 395 | // |
| 396 | // Handle a raw rectangle. |
| 397 | // |
Constantin Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 398 | void handleRawRect(int x, int y, int w, int h) throws IOException { |
| 399 | |
Constantin Kaplinsky | 903009e | 2002-05-20 10:55:47 +0000 | [diff] [blame] | 400 | 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 Kaplinsky | 72e47ef | 2008-04-18 17:48:16 +0000 | [diff] [blame] | 406 | pixels24[offset + i] = |
| 407 | (buf[i * 4 + 2] & 0xFF) << 16 | |
| 408 | (buf[i * 4 + 1] & 0xFF) << 8 | |
| 409 | (buf[i * 4] & 0xFF); |
Constantin Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 410 | } |
| 411 | } |
| 412 | |
| 413 | handleUpdatedPixels(x, y, w, h); |
| 414 | scheduleRepaint(x, y, w, h); |
| 415 | } |
| 416 | |
| 417 | |
| 418 | // |
Constantin Kaplinsky | 99df0a7 | 2002-06-04 06:13:20 +0000 | [diff] [blame] | 419 | // Handle a CopyRect rectangle. |
| 420 | // |
Constantin Kaplinsky | 99df0a7 | 2002-06-04 06:13:20 +0000 | [diff] [blame] | 421 | 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 Kaplinsky | 72e47ef | 2008-04-18 17:48:16 +0000 | [diff] [blame] | 425 | x - rfb.copyRectSrcX, y - rfb.copyRectSrcY); |
Constantin Kaplinsky | 99df0a7 | 2002-06-04 06:13:20 +0000 | [diff] [blame] | 426 | |
| 427 | scheduleRepaint(x, y, w, h); |
| 428 | } |
| 429 | |
| 430 | // |
| 431 | // Handle an RRE-encoded rectangle. |
| 432 | // |
Constantin Kaplinsky | 99df0a7 | 2002-06-04 06:13:20 +0000 | [diff] [blame] | 433 | 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 Kaplinsky | 99df0a7 | 2002-06-04 06:13:20 +0000 | [diff] [blame] | 462 | 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 Kaplinsky | 72e47ef | 2008-04-18 17:48:16 +0000 | [diff] [blame] | 493 | private Color hextile_bg, hextile_fg; |
Constantin Kaplinsky | 99df0a7 | 2002-06-04 06:13:20 +0000 | [diff] [blame] | 494 | |
| 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 Kaplinsky | 72e47ef | 2008-04-18 17:48:16 +0000 | [diff] [blame] | 503 | th = y + h - ty; |
Constantin Kaplinsky | 99df0a7 | 2002-06-04 06:13:20 +0000 | [diff] [blame] | 504 | |
| 505 | for (int tx = x; tx < x + w; tx += 16) { |
Constantin Kaplinsky | 72e47ef | 2008-04-18 17:48:16 +0000 | [diff] [blame] | 506 | int tw = 16; |
| 507 | if (x + w - tx < 16) |
| 508 | tw = x + w - tx; |
Constantin Kaplinsky | 99df0a7 | 2002-06-04 06:13:20 +0000 | [diff] [blame] | 509 | |
Constantin Kaplinsky | 72e47ef | 2008-04-18 17:48:16 +0000 | [diff] [blame] | 510 | handleHextileSubrect(tx, ty, tw, th); |
Constantin Kaplinsky | 99df0a7 | 2002-06-04 06:13:20 +0000 | [diff] [blame] | 511 | } |
| 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 Kaplinsky | 99df0a7 | 2002-06-04 06:13:20 +0000 | [diff] [blame] | 521 | void handleHextileSubrect(int tx, int ty, int tw, int th) |
Constantin Kaplinsky | 72e47ef | 2008-04-18 17:48:16 +0000 | [diff] [blame] | 522 | throws IOException { |
Constantin Kaplinsky | 99df0a7 | 2002-06-04 06:13:20 +0000 | [diff] [blame] | 523 | |
| 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 Kaplinsky | 72e47ef | 2008-04-18 17:48:16 +0000 | [diff] [blame] | 532 | 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 Kaplinsky | 99df0a7 | 2002-06-04 06:13:20 +0000 | [diff] [blame] | 540 | } |
| 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 Kaplinsky | 72e47ef | 2008-04-18 17:48:16 +0000 | [diff] [blame] | 568 | 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 Kaplinsky | 99df0a7 | 2002-06-04 06:13:20 +0000 | [diff] [blame] | 578 | } |
| 579 | } else { |
| 580 | memGraphics.setColor(hextile_fg); |
| 581 | for (int j = 0; j < nSubrects; j++) { |
Constantin Kaplinsky | 72e47ef | 2008-04-18 17:48:16 +0000 | [diff] [blame] | 582 | 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 Kaplinsky | 99df0a7 | 2002-06-04 06:13:20 +0000 | [diff] [blame] | 589 | } |
| 590 | } |
| 591 | } |
| 592 | |
| 593 | // |
Constantin Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 594 | // Handle a Zlib-encoded rectangle. |
| 595 | // |
Constantin Kaplinsky | 99df0a7 | 2002-06-04 06:13:20 +0000 | [diff] [blame] | 596 | 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 Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 607 | |
| 608 | try { |
Constantin Kaplinsky | 903009e | 2002-05-20 10:55:47 +0000 | [diff] [blame] | 609 | byte[] buf = new byte[w * 4]; |
| 610 | int i, offset; |
| 611 | for (int dy = y; dy < y + h; dy++) { |
Constantin Kaplinsky | 72e47ef | 2008-04-18 17:48:16 +0000 | [diff] [blame] | 612 | 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 Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 620 | } |
Constantin Kaplinsky | 72e47ef | 2008-04-18 17:48:16 +0000 | [diff] [blame] | 621 | } catch (DataFormatException dfe) { |
Constantin Kaplinsky | 99df0a7 | 2002-06-04 06:13:20 +0000 | [diff] [blame] | 622 | throw new Exception(dfe.toString()); |
Constantin Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 623 | } |
| 624 | |
| 625 | handleUpdatedPixels(x, y, w, h); |
| 626 | scheduleRepaint(x, y, w, h); |
| 627 | } |
| 628 | |
Constantin Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 629 | // |
Constantin Kaplinsky | 99df0a7 | 2002-06-04 06:13:20 +0000 | [diff] [blame] | 630 | // Handle a Tight-encoded rectangle. |
Constantin Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 631 | // |
Constantin Kaplinsky | 99df0a7 | 2002-06-04 06:13:20 +0000 | [diff] [blame] | 632 | void handleTightRect(int x, int y, int w, int h) throws Exception { |
Constantin Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 633 | |
| 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 Kaplinsky | 72e47ef | 2008-04-18 17:48:16 +0000 | [diff] [blame] | 639 | tightInflaters[stream_id] = null; |
Constantin Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 640 | } |
| 641 | comp_ctl >>= 1; |
| 642 | } |
| 643 | |
| 644 | // Check correctness of subencoding value. |
| 645 | if (comp_ctl > rfb.TightMaxSubencoding) { |
Constantin Kaplinsky | 99df0a7 | 2002-06-04 06:13:20 +0000 | [diff] [blame] | 646 | throw new Exception("Incorrect tight subencoding: " + comp_ctl); |
Constantin Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 647 | } |
| 648 | |
| 649 | // Handle solid-color rectangles. |
| 650 | if (comp_ctl == rfb.TightFill) { |
Constantin Kaplinsky | 903009e | 2002-05-20 10:55:47 +0000 | [diff] [blame] | 651 | byte[] buf = new byte[3]; |
| 652 | rfb.is.readFully(buf); |
Constantin Kaplinsky | a628bf0 | 2002-05-20 13:33:46 +0000 | [diff] [blame] | 653 | Color bg = new Color(buf[0] & 0xFF, buf[1] & 0xFF, buf[2] & 0xFF); |
Constantin Kaplinsky | 903009e | 2002-05-20 10:55:47 +0000 | [diff] [blame] | 654 | memGraphics.setColor(bg); |
Constantin Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 655 | 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 Kaplinsky | 72e47ef | 2008-04-18 17:48:16 +0000 | [diff] [blame] | 675 | 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 Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 682 | } |
| 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 Kaplinsky | 72e47ef | 2008-04-18 17:48:16 +0000 | [diff] [blame] | 698 | 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 Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 708 | } else if (filter_id == rfb.TightFilterGradient) { |
Constantin Kaplinsky | 72e47ef | 2008-04-18 17:48:16 +0000 | [diff] [blame] | 709 | useGradient = true; |
Constantin Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 710 | } else if (filter_id != rfb.TightFilterCopy) { |
Constantin Kaplinsky | 72e47ef | 2008-04-18 17:48:16 +0000 | [diff] [blame] | 711 | throw new Exception("Incorrect tight filter id: " + filter_id); |
Constantin Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 712 | } |
| 713 | } |
Constantin Kaplinsky | 903009e | 2002-05-20 10:55:47 +0000 | [diff] [blame] | 714 | if (numColors == 0) |
Constantin Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 715 | 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 Kaplinsky | 72e47ef | 2008-04-18 17:48:16 +0000 | [diff] [blame] | 722 | // 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 Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 738 | } else if (useGradient) { |
Constantin Kaplinsky | 72e47ef | 2008-04-18 17:48:16 +0000 | [diff] [blame] | 739 | // "Gradient"-processed data |
| 740 | byte[] buf = new byte[w * h * 3]; |
| 741 | rfb.is.readFully(buf); |
| 742 | decodeGradientData(x, y, w, h, buf); |
Constantin Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 743 | } else { |
Constantin Kaplinsky | 72e47ef | 2008-04-18 17:48:16 +0000 | [diff] [blame] | 744 | // 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 Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 757 | } |
| 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 Kaplinsky | 72e47ef | 2008-04-18 17:48:16 +0000 | [diff] [blame] | 765 | tightInflaters[stream_id] = new Inflater(); |
Constantin Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 766 | } |
| 767 | Inflater myInflater = tightInflaters[stream_id]; |
| 768 | myInflater.setInput(zlibData); |
| 769 | try { |
Constantin Kaplinsky | 72e47ef | 2008-04-18 17:48:16 +0000 | [diff] [blame] | 770 | 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 Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 809 | } |
| 810 | } |
| 811 | |
| 812 | handleUpdatedPixels(x, y, w, h); |
| 813 | scheduleRepaint(x, y, w, h); |
| 814 | } |
| 815 | |
| 816 | // |
Constantin Kaplinsky | 903009e | 2002-05-20 10:55:47 +0000 | [diff] [blame] | 817 | // Decode 1bpp-encoded bi-color rectangle. |
Constantin Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 818 | // |
Constantin Kaplinsky | 99df0a7 | 2002-06-04 06:13:20 +0000 | [diff] [blame] | 819 | void decodeMonoData(int x, int y, int w, int h, byte[] src, int[] palette) { |
Constantin Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 820 | |
| 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 Kaplinsky | 72e47ef | 2008-04-18 17:48:16 +0000 | [diff] [blame] | 828 | b = src[dy * rowBytes + dx]; |
| 829 | for (n = 7; n >= 0; n--) |
| 830 | pixels24[i++] = palette[b >> n & 1]; |
Constantin Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 831 | } |
| 832 | for (n = 7; n >= 8 - w % 8; n--) { |
Constantin Kaplinsky | 72e47ef | 2008-04-18 17:48:16 +0000 | [diff] [blame] | 833 | pixels24[i++] = palette[src[dy * rowBytes + dx] >> n & 1]; |
Constantin Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 834 | } |
| 835 | i += (rfb.framebufferWidth - w); |
| 836 | } |
| 837 | } |
| 838 | |
| 839 | // |
| 840 | // Decode data processed with the "Gradient" filter. |
| 841 | // |
Constantin Kaplinsky | 72e47ef | 2008-04-18 17:48:16 +0000 | [diff] [blame] | 842 | void decodeGradientData(int x, int y, int w, int h, byte[] buf) { |
Constantin Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 843 | |
| 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 Kaplinsky | 72e47ef | 2008-04-18 17:48:16 +0000 | [diff] [blame] | 856 | pix[c] = (byte)(prevRow[c] + buf[dy * w * 3 + c]); |
| 857 | thisRow[c] = pix[c]; |
Constantin Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 858 | } |
| 859 | pixels24[offset++] = |
Constantin Kaplinsky | 72e47ef | 2008-04-18 17:48:16 +0000 | [diff] [blame] | 860 | (pix[0] & 0xFF) << 16 | (pix[1] & 0xFF) << 8 | (pix[2] & 0xFF); |
Constantin Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 861 | |
| 862 | /* Remaining pixels of a row */ |
| 863 | for (dx = 1; dx < w; dx++) { |
Constantin Kaplinsky | 72e47ef | 2008-04-18 17:48:16 +0000 | [diff] [blame] | 864 | 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 Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 877 | } |
| 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 Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 888 | void handleUpdatedPixels(int x, int y, int w, int h) { |
| 889 | |
| 890 | // Draw updated pixels of the off-screen image. |
Constantin Kaplinsky | 903009e | 2002-05-20 10:55:47 +0000 | [diff] [blame] | 891 | |
Constantin Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 892 | 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.com | a5a4f4f | 2004-09-21 15:25:05 +0000 | [diff] [blame] | 898 | ////////////////////////////////////////////////////////////////// |
| 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.com | a5a4f4f | 2004-09-21 15:25:05 +0000 | [diff] [blame] | 948 | |
| 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.com | a5a4f4f | 2004-09-21 15:25:05 +0000 | [diff] [blame] | 957 | |
| 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.com | a5a4f4f | 2004-09-21 15:25:05 +0000 | [diff] [blame] | 993 | |
| 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.com | ccb67c3 | 2005-01-03 16:20:38 +0000 | [diff] [blame^] | 1086 | Point o = getImageOrigin(); |
| 1087 | int oldX = cursorX + o.x; |
| 1088 | int oldY = cursorY + o.y; |
| 1089 | cursorX = x; |
| 1090 | cursorY = y; |
wimba.com | a5a4f4f | 2004-09-21 15:25:05 +0000 | [diff] [blame] | 1091 | if (showSoftCursor) { |
| 1092 | repaint(deferCursorUpdates, |
wimba.com | ccb67c3 | 2005-01-03 16:20:38 +0000 | [diff] [blame^] | 1093 | oldX - hotX, oldY - hotY, cursorWidth, cursorHeight); |
wimba.com | a5a4f4f | 2004-09-21 15:25:05 +0000 | [diff] [blame] | 1094 | repaint(deferCursorUpdates, |
wimba.com | ccb67c3 | 2005-01-03 16:20:38 +0000 | [diff] [blame^] | 1095 | cursorX - hotX + o.x, cursorY - hotY + o.y, cursorWidth, |
| 1096 | cursorHeight); |
wimba.com | a5a4f4f | 2004-09-21 15:25:05 +0000 | [diff] [blame] | 1097 | |
wimba.com | 1609272 | 2004-09-21 20:54:37 +0000 | [diff] [blame] | 1098 | // 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.com | a5a4f4f | 2004-09-21 15:25:05 +0000 | [diff] [blame] | 1125 | } |
| 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.com | ccb67c3 | 2005-01-03 16:20:38 +0000 | [diff] [blame^] | 1140 | Point o = getImageOrigin(); |
wimba.com | a5a4f4f | 2004-09-21 15:25:05 +0000 | [diff] [blame] | 1141 | repaint(deferCursorUpdates, |
wimba.com | ccb67c3 | 2005-01-03 16:20:38 +0000 | [diff] [blame^] | 1142 | cursorX - hotX + o.x, cursorY - hotY + o.y, cursorWidth, |
| 1143 | cursorHeight); |
wimba.com | a5a4f4f | 2004-09-21 15:25:05 +0000 | [diff] [blame] | 1144 | } |
| 1145 | } |
Constantin Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 1146 | // |
| 1147 | // Tell JVM to repaint specified desktop area. |
| 1148 | // |
Constantin Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 1149 | void scheduleRepaint(int x, int y, int w, int h) { |
Constantin Kaplinsky | 37cc43e | 2002-05-30 17:30:11 +0000 | [diff] [blame] | 1150 | if (rfb.fbs.isSeeking()) { |
Constantin Kaplinsky | 52c4824 | 2002-05-30 13:26:34 +0000 | [diff] [blame] | 1151 | // Do nothing, and remember we are seeking. |
| 1152 | seekMode = true; |
| 1153 | } else { |
| 1154 | if (seekMode) { |
Constantin Kaplinsky | 72e47ef | 2008-04-18 17:48:16 +0000 | [diff] [blame] | 1155 | // Immediate repaint of the whole desktop after seeking. |
| 1156 | repaint(); |
Constantin Kaplinsky | 52c4824 | 2002-05-30 13:26:34 +0000 | [diff] [blame] | 1157 | } else { |
Constantin Kaplinsky | 72e47ef | 2008-04-18 17:48:16 +0000 | [diff] [blame] | 1158 | // Usual incremental repaint. |
wimba.com | ccb67c3 | 2005-01-03 16:20:38 +0000 | [diff] [blame^] | 1159 | Point o = getImageOrigin(); |
| 1160 | repaint(player.deferScreenUpdates, o.x + x, o.y + y, w, h); |
Constantin Kaplinsky | 52c4824 | 2002-05-30 13:26:34 +0000 | [diff] [blame] | 1161 | } |
| 1162 | seekMode = false; |
| 1163 | } |
Constantin Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 1164 | } |
| 1165 | |
Constantin Kaplinsky | 5a7133a | 2002-07-24 17:02:04 +0000 | [diff] [blame] | 1166 | // |
| 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 Kaplinsky | 5a7133a | 2002-07-24 17:02:04 +0000 | [diff] [blame] | 1171 | 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 Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 1178 | } |