Constantin Kaplinsky | 2844fd5 | 2008-04-14 08:02:25 +0000 | [diff] [blame] | 1 | // |
| 2 | // Copyright (C) 2004 Horizon Wimba. All Rights Reserved. |
| 3 | // Copyright (C) 2001-2003 HorizonLive.com, Inc. All Rights Reserved. |
| 4 | // Copyright (C) 2001,2002 Constantin Kaplinsky. All Rights Reserved. |
| 5 | // Copyright (C) 2000 Tridia Corporation. All Rights Reserved. |
| 6 | // Copyright (C) 1999 AT&T Laboratories Cambridge. All Rights Reserved. |
| 7 | // |
| 8 | // This is free software; you can redistribute it and/or modify |
| 9 | // it under the terms of the GNU General Public License as published by |
| 10 | // the Free Software Foundation; either version 2 of the License, or |
| 11 | // (at your option) any later version. |
| 12 | // |
| 13 | // This software is distributed in the hope that it will be useful, |
| 14 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | // GNU General Public License for more details. |
| 17 | // |
| 18 | // You should have received a copy of the GNU General Public License |
| 19 | // along with this software; if not, write to the Free Software |
| 20 | // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
| 21 | // USA. |
| 22 | // |
| 23 | |
Constantin Kaplinsky | 90d8a50 | 2008-04-14 09:45:50 +0000 | [diff] [blame] | 24 | package com.tightvnc.vncviewer; |
| 25 | |
enikey | c41ba1d | 2008-12-19 09:07:22 +0000 | [diff] [blame] | 26 | import com.tightvnc.decoder.CoRREDecoder; |
| 27 | import com.tightvnc.decoder.HextileDecoder; |
| 28 | import com.tightvnc.decoder.RREDecoder; |
| 29 | import com.tightvnc.decoder.RawDecoder; |
| 30 | import com.tightvnc.decoder.TightDecoder; |
| 31 | import com.tightvnc.decoder.ZRLEDecoder; |
| 32 | import com.tightvnc.decoder.ZlibDecoder; |
enikey | 0dbc153 | 2008-12-19 08:51:47 +0000 | [diff] [blame] | 33 | import com.tightvnc.decoder.common.Repaintable; |
Constantin Kaplinsky | 2844fd5 | 2008-04-14 08:02:25 +0000 | [diff] [blame] | 34 | import java.awt.*; |
| 35 | import java.awt.event.*; |
| 36 | import java.awt.image.*; |
| 37 | import java.io.*; |
| 38 | import java.lang.*; |
| 39 | import java.util.zip.*; |
| 40 | |
| 41 | |
| 42 | // |
| 43 | // VncCanvas is a subclass of Canvas which draws a VNC desktop on it. |
| 44 | // |
| 45 | |
| 46 | class VncCanvas extends Canvas |
enikey | 0dbc153 | 2008-12-19 08:51:47 +0000 | [diff] [blame] | 47 | implements KeyListener, MouseListener, MouseMotionListener, RecordInterface, |
| 48 | Repaintable { |
Constantin Kaplinsky | 2844fd5 | 2008-04-14 08:02:25 +0000 | [diff] [blame] | 49 | |
| 50 | VncViewer viewer; |
| 51 | RfbProto rfb; |
| 52 | ColorModel cm8, cm24; |
Constantin Kaplinsky | 2844fd5 | 2008-04-14 08:02:25 +0000 | [diff] [blame] | 53 | int bytesPixel; |
| 54 | |
| 55 | int maxWidth = 0, maxHeight = 0; |
| 56 | int scalingFactor; |
| 57 | int scaledWidth, scaledHeight; |
| 58 | |
| 59 | Image memImage; |
| 60 | Graphics memGraphics; |
| 61 | |
enikey | c41ba1d | 2008-12-19 09:07:22 +0000 | [diff] [blame] | 62 | // |
| 63 | // Decoders |
| 64 | // |
| 65 | |
| 66 | RawDecoder rawDecoder; |
| 67 | RREDecoder rreDecoder; |
| 68 | CoRREDecoder correDecoder; |
| 69 | ZlibDecoder zlibDecoder; |
| 70 | HextileDecoder hextileDecoder; |
| 71 | ZRLEDecoder zrleDecoder; |
| 72 | TightDecoder tightDecoder; |
| 73 | |
| 74 | // Base decoder decoders array |
| 75 | RawDecoder []decoders = null; |
| 76 | |
Constantin Kaplinsky | 2844fd5 | 2008-04-14 08:02:25 +0000 | [diff] [blame] | 77 | // Update statistics. |
| 78 | long statStartTime; // time on first framebufferUpdateRequest |
enikey | 191695b | 2008-12-24 09:09:31 +0000 | [diff] [blame^] | 79 | long statNumUpdates; // counter for FramebufferUpdate messages |
| 80 | long statNumTotalRects; // rectangles in FramebufferUpdate messages |
| 81 | long statNumPixelRects; // the same, but excluding pseudo-rectangles |
| 82 | long statNumRectsTight; // Tight-encoded rectangles (including JPEG) |
| 83 | long statNumRectsTightJPEG; // JPEG-compressed Tight-encoded rectangles |
| 84 | long statNumRectsZRLE; // ZRLE-encoded rectangles |
| 85 | long statNumRectsHextile; // Hextile-encoded rectangles |
| 86 | long statNumRectsRaw; // Raw-encoded rectangles |
| 87 | long statNumRectsCopy; // CopyRect rectangles |
| 88 | long statNumBytesEncoded; // number of bytes in updates, as received |
| 89 | long statNumBytesDecoded; // number of bytes, as if Raw encoding was used |
Constantin Kaplinsky | 2844fd5 | 2008-04-14 08:02:25 +0000 | [diff] [blame] | 90 | |
Constantin Kaplinsky | 2844fd5 | 2008-04-14 08:02:25 +0000 | [diff] [blame] | 91 | // True if we process keyboard and mouse events. |
| 92 | boolean inputEnabled; |
| 93 | |
enikey | c92c1d1 | 2008-12-19 09:58:31 +0000 | [diff] [blame] | 94 | // True if was no one auto resize of canvas |
| 95 | boolean isFirstSizeAutoUpdate = true; |
| 96 | |
Constantin Kaplinsky | 2844fd5 | 2008-04-14 08:02:25 +0000 | [diff] [blame] | 97 | // |
| 98 | // The constructors. |
| 99 | // |
| 100 | |
| 101 | public VncCanvas(VncViewer v, int maxWidth_, int maxHeight_) |
| 102 | throws IOException { |
| 103 | |
| 104 | viewer = v; |
| 105 | maxWidth = maxWidth_; |
| 106 | maxHeight = maxHeight_; |
| 107 | |
| 108 | rfb = viewer.rfb; |
| 109 | scalingFactor = viewer.options.scalingFactor; |
| 110 | |
Constantin Kaplinsky | 2844fd5 | 2008-04-14 08:02:25 +0000 | [diff] [blame] | 111 | cm8 = new DirectColorModel(8, 7, (7 << 3), (3 << 6)); |
| 112 | cm24 = new DirectColorModel(24, 0xFF0000, 0x00FF00, 0x0000FF); |
| 113 | |
enikey | c41ba1d | 2008-12-19 09:07:22 +0000 | [diff] [blame] | 114 | // |
| 115 | // Create decoders |
| 116 | // |
| 117 | |
| 118 | // Input stream for decoders |
| 119 | RfbInputStream rfbis = new RfbInputStream(rfb); |
enikey | 1622a3c | 2008-12-24 03:58:29 +0000 | [diff] [blame] | 120 | // Create output stream for session recording |
| 121 | RecordOutputStream ros = new RecordOutputStream(this); |
enikey | c41ba1d | 2008-12-19 09:07:22 +0000 | [diff] [blame] | 122 | |
| 123 | rawDecoder = new RawDecoder(memGraphics, rfbis); |
| 124 | rreDecoder = new RREDecoder(memGraphics, rfbis); |
| 125 | correDecoder = new CoRREDecoder(memGraphics, rfbis); |
| 126 | hextileDecoder = new HextileDecoder(memGraphics, rfbis); |
| 127 | tightDecoder = new TightDecoder(memGraphics, rfbis); |
enikey | 4582bab | 2008-12-19 09:19:59 +0000 | [diff] [blame] | 128 | zlibDecoder = new ZlibDecoder(memGraphics, rfbis); |
enikey | c41ba1d | 2008-12-19 09:07:22 +0000 | [diff] [blame] | 129 | zrleDecoder = new ZRLEDecoder(memGraphics, rfbis); |
| 130 | |
| 131 | // |
| 132 | // Set data for decoders that needs extra parameters |
| 133 | // |
| 134 | |
| 135 | hextileDecoder.setRepainableControl(this); |
| 136 | tightDecoder.setRepainableControl(this); |
| 137 | |
| 138 | // |
| 139 | // Create array that contains our decoders |
| 140 | // |
| 141 | |
| 142 | decoders = new RawDecoder[7]; |
| 143 | decoders[0] = rawDecoder; |
| 144 | decoders[1] = rreDecoder; |
| 145 | decoders[2] = correDecoder; |
| 146 | decoders[3] = hextileDecoder; |
| 147 | decoders[4] = zlibDecoder; |
| 148 | decoders[5] = tightDecoder; |
| 149 | decoders[6] = zrleDecoder; |
| 150 | |
| 151 | // |
| 152 | // Set session recorder for decoders |
| 153 | // |
| 154 | |
| 155 | for (int i = 0; i < decoders.length; i++) { |
| 156 | decoders[i].setSessionRecorder(this); |
enikey | 1622a3c | 2008-12-24 03:58:29 +0000 | [diff] [blame] | 157 | decoders[i].setDataOutputStream(ros); |
enikey | c41ba1d | 2008-12-19 09:07:22 +0000 | [diff] [blame] | 158 | } |
enikey | c92c1d1 | 2008-12-19 09:58:31 +0000 | [diff] [blame] | 159 | |
enikey | 2f7f46e | 2008-12-19 09:32:35 +0000 | [diff] [blame] | 160 | setPixelFormat(); |
| 161 | |
| 162 | resetSelection(); |
| 163 | |
| 164 | inputEnabled = false; |
| 165 | if (!viewer.options.viewOnly) |
| 166 | enableInput(true); |
enikey | c41ba1d | 2008-12-19 09:07:22 +0000 | [diff] [blame] | 167 | |
Constantin Kaplinsky | f7cb2bf | 2008-05-27 08:38:28 +0000 | [diff] [blame] | 168 | // Enable mouse and keyboard event listeners. |
Constantin Kaplinsky | 2844fd5 | 2008-04-14 08:02:25 +0000 | [diff] [blame] | 169 | addKeyListener(this); |
Constantin Kaplinsky | f7cb2bf | 2008-05-27 08:38:28 +0000 | [diff] [blame] | 170 | addMouseListener(this); |
| 171 | addMouseMotionListener(this); |
Constantin Kaplinsky | 2844fd5 | 2008-04-14 08:02:25 +0000 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | public VncCanvas(VncViewer v) throws IOException { |
| 175 | this(v, 0, 0); |
| 176 | } |
| 177 | |
| 178 | // |
| 179 | // Callback methods to determine geometry of our Component. |
| 180 | // |
| 181 | |
| 182 | public Dimension getPreferredSize() { |
| 183 | return new Dimension(scaledWidth, scaledHeight); |
| 184 | } |
| 185 | |
| 186 | public Dimension getMinimumSize() { |
| 187 | return new Dimension(scaledWidth, scaledHeight); |
| 188 | } |
| 189 | |
| 190 | public Dimension getMaximumSize() { |
| 191 | return new Dimension(scaledWidth, scaledHeight); |
| 192 | } |
| 193 | |
| 194 | // |
| 195 | // All painting is performed here. |
| 196 | // |
| 197 | |
| 198 | public void update(Graphics g) { |
| 199 | paint(g); |
| 200 | } |
| 201 | |
| 202 | public void paint(Graphics g) { |
| 203 | synchronized(memImage) { |
| 204 | if (rfb.framebufferWidth == scaledWidth) { |
| 205 | g.drawImage(memImage, 0, 0, null); |
| 206 | } else { |
| 207 | paintScaledFrameBuffer(g); |
| 208 | } |
| 209 | } |
| 210 | if (showSoftCursor) { |
| 211 | int x0 = cursorX - hotX, y0 = cursorY - hotY; |
| 212 | Rectangle r = new Rectangle(x0, y0, cursorWidth, cursorHeight); |
| 213 | if (r.intersects(g.getClipBounds())) { |
| 214 | g.drawImage(softCursor, x0, y0, null); |
| 215 | } |
| 216 | } |
Constantin Kaplinsky | f7cb2bf | 2008-05-27 08:38:28 +0000 | [diff] [blame] | 217 | if (isInSelectionMode()) { |
| 218 | Rectangle r = getSelection(true); |
| 219 | if (r.width > 0 && r.height > 0) { |
| 220 | // Don't forget to correct the coordinates for the right and bottom |
| 221 | // borders, so that the borders are the part of the selection. |
| 222 | r.width -= 1; |
| 223 | r.height -= 1; |
| 224 | g.setXORMode(Color.yellow); |
| 225 | g.drawRect(r.x, r.y, r.width, r.height); |
| 226 | } |
| 227 | } |
Constantin Kaplinsky | 2844fd5 | 2008-04-14 08:02:25 +0000 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | public void paintScaledFrameBuffer(Graphics g) { |
| 231 | g.drawImage(memImage, 0, 0, scaledWidth, scaledHeight, null); |
| 232 | } |
| 233 | |
| 234 | // |
Constantin Kaplinsky | 2844fd5 | 2008-04-14 08:02:25 +0000 | [diff] [blame] | 235 | // Start/stop receiving mouse events. Keyboard events are received |
| 236 | // even in view-only mode, because we want to map the 'r' key to the |
| 237 | // screen refreshing function. |
| 238 | // |
| 239 | |
| 240 | public synchronized void enableInput(boolean enable) { |
| 241 | if (enable && !inputEnabled) { |
| 242 | inputEnabled = true; |
Constantin Kaplinsky | 2844fd5 | 2008-04-14 08:02:25 +0000 | [diff] [blame] | 243 | if (viewer.showControls) { |
| 244 | viewer.buttonPanel.enableRemoteAccessControls(true); |
| 245 | } |
| 246 | createSoftCursor(); // scaled cursor |
| 247 | } else if (!enable && inputEnabled) { |
| 248 | inputEnabled = false; |
Constantin Kaplinsky | 2844fd5 | 2008-04-14 08:02:25 +0000 | [diff] [blame] | 249 | if (viewer.showControls) { |
| 250 | viewer.buttonPanel.enableRemoteAccessControls(false); |
| 251 | } |
| 252 | createSoftCursor(); // non-scaled cursor |
| 253 | } |
| 254 | } |
| 255 | |
| 256 | public void setPixelFormat() throws IOException { |
| 257 | if (viewer.options.eightBitColors) { |
| 258 | rfb.writeSetPixelFormat(8, 8, false, true, 7, 7, 3, 0, 3, 6); |
| 259 | bytesPixel = 1; |
| 260 | } else { |
| 261 | rfb.writeSetPixelFormat(32, 24, false, true, 255, 255, 255, 16, 8, 0); |
| 262 | bytesPixel = 4; |
| 263 | } |
| 264 | updateFramebufferSize(); |
| 265 | } |
| 266 | |
enikey | 45cfaa5 | 2008-12-03 06:52:09 +0000 | [diff] [blame] | 267 | void setScalingFactor(int sf) { |
| 268 | scalingFactor = sf; |
| 269 | updateFramebufferSize(); |
| 270 | invalidate(); |
| 271 | } |
| 272 | |
Constantin Kaplinsky | 2844fd5 | 2008-04-14 08:02:25 +0000 | [diff] [blame] | 273 | void updateFramebufferSize() { |
| 274 | |
| 275 | // Useful shortcuts. |
| 276 | int fbWidth = rfb.framebufferWidth; |
| 277 | int fbHeight = rfb.framebufferHeight; |
| 278 | |
enikey | 87647e9 | 2008-12-04 08:42:34 +0000 | [diff] [blame] | 279 | // FIXME: This part of code must be in VncViewer i think |
enikey | 7368320 | 2008-12-03 09:17:25 +0000 | [diff] [blame] | 280 | if (viewer.options.autoScale) { |
enikey | 87647e9 | 2008-12-04 08:42:34 +0000 | [diff] [blame] | 281 | if (viewer.inAnApplet) { |
| 282 | maxWidth = viewer.getWidth(); |
| 283 | maxHeight = viewer.getHeight(); |
| 284 | } else { |
| 285 | if (viewer.vncFrame != null) { |
| 286 | if (isFirstSizeAutoUpdate) { |
| 287 | isFirstSizeAutoUpdate = false; |
| 288 | Dimension screenSize = viewer.vncFrame.getToolkit().getScreenSize(); |
| 289 | maxWidth = (int)screenSize.getWidth() - 100; |
enikey | c41ba1d | 2008-12-19 09:07:22 +0000 | [diff] [blame] | 290 | maxHeight = (int)screenSize.getHeight() - 100; |
enikey | 87647e9 | 2008-12-04 08:42:34 +0000 | [diff] [blame] | 291 | viewer.vncFrame.setSize(maxWidth, maxHeight); |
| 292 | } else { |
| 293 | viewer.desktopScrollPane.doLayout(); |
| 294 | maxWidth = viewer.desktopScrollPane.getWidth(); |
| 295 | maxHeight = viewer.desktopScrollPane.getHeight(); |
| 296 | } |
| 297 | } else { |
| 298 | maxWidth = fbWidth; |
| 299 | maxHeight = fbHeight; |
| 300 | } |
enikey | 7368320 | 2008-12-03 09:17:25 +0000 | [diff] [blame] | 301 | } |
Constantin Kaplinsky | 2844fd5 | 2008-04-14 08:02:25 +0000 | [diff] [blame] | 302 | int f1 = maxWidth * 100 / fbWidth; |
| 303 | int f2 = maxHeight * 100 / fbHeight; |
| 304 | scalingFactor = Math.min(f1, f2); |
| 305 | if (scalingFactor > 100) |
| 306 | scalingFactor = 100; |
| 307 | System.out.println("Scaling desktop at " + scalingFactor + "%"); |
| 308 | } |
| 309 | |
| 310 | // Update scaled framebuffer geometry. |
| 311 | scaledWidth = (fbWidth * scalingFactor + 50) / 100; |
| 312 | scaledHeight = (fbHeight * scalingFactor + 50) / 100; |
| 313 | |
| 314 | // Create new off-screen image either if it does not exist, or if |
| 315 | // its geometry should be changed. It's not necessary to replace |
| 316 | // existing image if only pixel format should be changed. |
| 317 | if (memImage == null) { |
| 318 | memImage = viewer.vncContainer.createImage(fbWidth, fbHeight); |
| 319 | memGraphics = memImage.getGraphics(); |
| 320 | } else if (memImage.getWidth(null) != fbWidth || |
| 321 | memImage.getHeight(null) != fbHeight) { |
| 322 | synchronized(memImage) { |
| 323 | memImage = viewer.vncContainer.createImage(fbWidth, fbHeight); |
| 324 | memGraphics = memImage.getGraphics(); |
| 325 | } |
| 326 | } |
| 327 | |
enikey | 4582bab | 2008-12-19 09:19:59 +0000 | [diff] [blame] | 328 | // |
| 329 | // Update decoders |
| 330 | // |
| 331 | |
| 332 | // |
| 333 | // FIXME: Why decoders can be null here? |
| 334 | // |
| 335 | |
| 336 | if (decoders != null) { |
| 337 | for (int i = 0; i < decoders.length; i++) { |
| 338 | // |
| 339 | // Set changes to every decoder that we can use |
| 340 | // |
| 341 | |
| 342 | decoders[i].setBPP(bytesPixel); |
| 343 | decoders[i].setFrameBufferSize(fbWidth, fbHeight); |
| 344 | decoders[i].setGraphics(memGraphics); |
| 345 | |
| 346 | // |
| 347 | // Update decoder |
| 348 | // |
| 349 | |
| 350 | decoders[i].update(); |
| 351 | } |
| 352 | } |
| 353 | |
enikey | 87647e9 | 2008-12-04 08:42:34 +0000 | [diff] [blame] | 354 | // FIXME: This part of code must be in VncViewer i think |
Constantin Kaplinsky | 2844fd5 | 2008-04-14 08:02:25 +0000 | [diff] [blame] | 355 | // Update the size of desktop containers. |
| 356 | if (viewer.inSeparateFrame) { |
enikey | 87647e9 | 2008-12-04 08:42:34 +0000 | [diff] [blame] | 357 | if (viewer.desktopScrollPane != null) { |
| 358 | if (!viewer.options.autoScale) { |
| 359 | resizeDesktopFrame(); |
| 360 | } else { |
| 361 | setSize(scaledWidth, scaledHeight); |
| 362 | viewer.desktopScrollPane.setSize(maxWidth + 200, |
| 363 | maxHeight + 200); |
| 364 | } |
| 365 | } |
Constantin Kaplinsky | 2844fd5 | 2008-04-14 08:02:25 +0000 | [diff] [blame] | 366 | } else { |
| 367 | setSize(scaledWidth, scaledHeight); |
| 368 | } |
| 369 | viewer.moveFocusToDesktop(); |
| 370 | } |
| 371 | |
| 372 | void resizeDesktopFrame() { |
| 373 | setSize(scaledWidth, scaledHeight); |
| 374 | |
| 375 | // FIXME: Find a better way to determine correct size of a |
| 376 | // ScrollPane. -- const |
| 377 | Insets insets = viewer.desktopScrollPane.getInsets(); |
| 378 | viewer.desktopScrollPane.setSize(scaledWidth + |
| 379 | 2 * Math.min(insets.left, insets.right), |
| 380 | scaledHeight + |
| 381 | 2 * Math.min(insets.top, insets.bottom)); |
| 382 | |
| 383 | viewer.vncFrame.pack(); |
| 384 | |
| 385 | // Try to limit the frame size to the screen size. |
| 386 | |
| 387 | Dimension screenSize = viewer.vncFrame.getToolkit().getScreenSize(); |
| 388 | Dimension frameSize = viewer.vncFrame.getSize(); |
| 389 | Dimension newSize = frameSize; |
| 390 | |
| 391 | // Reduce Screen Size by 30 pixels in each direction; |
| 392 | // This is a (poor) attempt to account for |
| 393 | // 1) Menu bar on Macintosh (should really also account for |
| 394 | // Dock on OSX). Usually 22px on top of screen. |
| 395 | // 2) Taxkbar on Windows (usually about 28 px on bottom) |
| 396 | // 3) Other obstructions. |
| 397 | |
| 398 | screenSize.height -= 30; |
| 399 | screenSize.width -= 30; |
| 400 | |
| 401 | boolean needToResizeFrame = false; |
| 402 | if (frameSize.height > screenSize.height) { |
| 403 | newSize.height = screenSize.height; |
| 404 | needToResizeFrame = true; |
| 405 | } |
| 406 | if (frameSize.width > screenSize.width) { |
| 407 | newSize.width = screenSize.width; |
| 408 | needToResizeFrame = true; |
| 409 | } |
| 410 | if (needToResizeFrame) { |
| 411 | viewer.vncFrame.setSize(newSize); |
| 412 | } |
| 413 | |
| 414 | viewer.desktopScrollPane.doLayout(); |
| 415 | } |
| 416 | |
| 417 | // |
| 418 | // processNormalProtocol() - executed by the rfbThread to deal with the |
| 419 | // RFB socket. |
| 420 | // |
| 421 | |
| 422 | public void processNormalProtocol() throws Exception { |
| 423 | |
| 424 | // Start/stop session recording if necessary. |
| 425 | viewer.checkRecordingStatus(); |
| 426 | |
| 427 | rfb.writeFramebufferUpdateRequest(0, 0, rfb.framebufferWidth, |
| 428 | rfb.framebufferHeight, false); |
| 429 | |
| 430 | if (viewer.options.continuousUpdates) { |
| 431 | rfb.tryEnableContinuousUpdates(0, 0, rfb.framebufferWidth, |
| 432 | rfb.framebufferHeight); |
| 433 | } |
| 434 | |
| 435 | resetStats(); |
| 436 | boolean statsRestarted = false; |
| 437 | |
| 438 | // |
| 439 | // main dispatch loop |
| 440 | // |
| 441 | |
| 442 | while (true) { |
| 443 | |
| 444 | // Read message type from the server. |
| 445 | int msgType = rfb.readServerMessageType(); |
| 446 | |
| 447 | // Process the message depending on its type. |
| 448 | switch (msgType) { |
| 449 | case RfbProto.FramebufferUpdate: |
| 450 | |
| 451 | if (statNumUpdates == viewer.debugStatsExcludeUpdates && |
| 452 | !statsRestarted) { |
| 453 | resetStats(); |
| 454 | statsRestarted = true; |
| 455 | } else if (statNumUpdates == viewer.debugStatsMeasureUpdates && |
| 456 | statsRestarted) { |
| 457 | viewer.disconnect(); |
| 458 | } |
| 459 | |
| 460 | rfb.readFramebufferUpdate(); |
| 461 | statNumUpdates++; |
| 462 | |
| 463 | boolean cursorPosReceived = false; |
| 464 | |
| 465 | for (int i = 0; i < rfb.updateNRects; i++) { |
| 466 | |
| 467 | rfb.readFramebufferUpdateRectHdr(); |
| 468 | statNumTotalRects++; |
| 469 | int rx = rfb.updateRectX, ry = rfb.updateRectY; |
| 470 | int rw = rfb.updateRectW, rh = rfb.updateRectH; |
| 471 | |
| 472 | if (rfb.updateRectEncoding == rfb.EncodingLastRect) |
| 473 | break; |
| 474 | |
| 475 | if (rfb.updateRectEncoding == rfb.EncodingNewFBSize) { |
| 476 | rfb.setFramebufferSize(rw, rh); |
| 477 | updateFramebufferSize(); |
| 478 | break; |
| 479 | } |
| 480 | |
| 481 | if (rfb.updateRectEncoding == rfb.EncodingXCursor || |
| 482 | rfb.updateRectEncoding == rfb.EncodingRichCursor) { |
| 483 | handleCursorShapeUpdate(rfb.updateRectEncoding, rx, ry, rw, rh); |
| 484 | continue; |
| 485 | } |
| 486 | |
| 487 | if (rfb.updateRectEncoding == rfb.EncodingPointerPos) { |
| 488 | softCursorMove(rx, ry); |
| 489 | cursorPosReceived = true; |
| 490 | continue; |
| 491 | } |
| 492 | |
| 493 | long numBytesReadBefore = rfb.getNumBytesRead(); |
| 494 | |
| 495 | rfb.startTiming(); |
| 496 | |
| 497 | switch (rfb.updateRectEncoding) { |
| 498 | case RfbProto.EncodingRaw: |
| 499 | statNumRectsRaw++; |
| 500 | handleRawRect(rx, ry, rw, rh); |
| 501 | break; |
| 502 | case RfbProto.EncodingCopyRect: |
| 503 | statNumRectsCopy++; |
| 504 | handleCopyRect(rx, ry, rw, rh); |
| 505 | break; |
| 506 | case RfbProto.EncodingRRE: |
| 507 | handleRRERect(rx, ry, rw, rh); |
| 508 | break; |
| 509 | case RfbProto.EncodingCoRRE: |
| 510 | handleCoRRERect(rx, ry, rw, rh); |
| 511 | break; |
| 512 | case RfbProto.EncodingHextile: |
| 513 | statNumRectsHextile++; |
| 514 | handleHextileRect(rx, ry, rw, rh); |
| 515 | break; |
| 516 | case RfbProto.EncodingZRLE: |
| 517 | statNumRectsZRLE++; |
| 518 | handleZRLERect(rx, ry, rw, rh); |
| 519 | break; |
| 520 | case RfbProto.EncodingZlib: |
| 521 | handleZlibRect(rx, ry, rw, rh); |
| 522 | break; |
| 523 | case RfbProto.EncodingTight: |
enikey | 2f7f46e | 2008-12-19 09:32:35 +0000 | [diff] [blame] | 524 | if (tightDecoder != null) { |
enikey | 479b18c | 2008-12-19 09:39:40 +0000 | [diff] [blame] | 525 | statNumRectsTightJPEG = tightDecoder.getNumJPEGRects(); |
enikey | 1214ab1 | 2008-12-24 09:01:19 +0000 | [diff] [blame] | 526 | //statNumRectsTight = tightDecoder.getNumTightRects(); |
enikey | 2f7f46e | 2008-12-19 09:32:35 +0000 | [diff] [blame] | 527 | } |
enikey | 1214ab1 | 2008-12-24 09:01:19 +0000 | [diff] [blame] | 528 | statNumRectsTight++; |
Constantin Kaplinsky | 2844fd5 | 2008-04-14 08:02:25 +0000 | [diff] [blame] | 529 | handleTightRect(rx, ry, rw, rh); |
| 530 | break; |
| 531 | default: |
| 532 | throw new Exception("Unknown RFB rectangle encoding " + |
| 533 | rfb.updateRectEncoding); |
| 534 | } |
| 535 | |
| 536 | rfb.stopTiming(); |
| 537 | |
| 538 | statNumPixelRects++; |
| 539 | statNumBytesDecoded += rw * rh * bytesPixel; |
| 540 | statNumBytesEncoded += |
| 541 | (int)(rfb.getNumBytesRead() - numBytesReadBefore); |
| 542 | } |
| 543 | |
| 544 | boolean fullUpdateNeeded = false; |
| 545 | |
| 546 | // Start/stop session recording if necessary. Request full |
| 547 | // update if a new session file was opened. |
| 548 | if (viewer.checkRecordingStatus()) |
| 549 | fullUpdateNeeded = true; |
| 550 | |
| 551 | // Defer framebuffer update request if necessary. But wake up |
| 552 | // immediately on keyboard or mouse event. Also, don't sleep |
| 553 | // if there is some data to receive, or if the last update |
| 554 | // included a PointerPos message. |
| 555 | if (viewer.deferUpdateRequests > 0 && |
| 556 | rfb.available() == 0 && !cursorPosReceived) { |
| 557 | synchronized(rfb) { |
| 558 | try { |
| 559 | rfb.wait(viewer.deferUpdateRequests); |
| 560 | } catch (InterruptedException e) { |
| 561 | } |
| 562 | } |
| 563 | } |
| 564 | |
| 565 | viewer.autoSelectEncodings(); |
| 566 | |
| 567 | // Before requesting framebuffer update, check if the pixel |
| 568 | // format should be changed. |
| 569 | if (viewer.options.eightBitColors != (bytesPixel == 1)) { |
| 570 | // Pixel format should be changed. |
| 571 | if (!rfb.continuousUpdatesAreActive()) { |
| 572 | // Continuous updates are not used. In this case, we just |
| 573 | // set new pixel format and request full update. |
| 574 | setPixelFormat(); |
| 575 | fullUpdateNeeded = true; |
| 576 | } else { |
| 577 | // Otherwise, disable continuous updates first. Pixel |
| 578 | // format will be set later when we are sure that there |
| 579 | // will be no unsolicited framebuffer updates. |
| 580 | rfb.tryDisableContinuousUpdates(); |
| 581 | break; // skip the code below |
| 582 | } |
| 583 | } |
| 584 | |
| 585 | // Enable/disable continuous updates to reflect the GUI setting. |
| 586 | boolean enable = viewer.options.continuousUpdates; |
| 587 | if (enable != rfb.continuousUpdatesAreActive()) { |
| 588 | if (enable) { |
| 589 | rfb.tryEnableContinuousUpdates(0, 0, rfb.framebufferWidth, |
| 590 | rfb.framebufferHeight); |
| 591 | } else { |
| 592 | rfb.tryDisableContinuousUpdates(); |
| 593 | } |
| 594 | } |
| 595 | |
| 596 | // Finally, request framebuffer update if needed. |
| 597 | if (fullUpdateNeeded) { |
| 598 | rfb.writeFramebufferUpdateRequest(0, 0, rfb.framebufferWidth, |
| 599 | rfb.framebufferHeight, false); |
| 600 | } else if (!rfb.continuousUpdatesAreActive()) { |
| 601 | rfb.writeFramebufferUpdateRequest(0, 0, rfb.framebufferWidth, |
| 602 | rfb.framebufferHeight, true); |
| 603 | } |
| 604 | |
| 605 | break; |
| 606 | |
| 607 | case RfbProto.SetColourMapEntries: |
| 608 | throw new Exception("Can't handle SetColourMapEntries message"); |
| 609 | |
| 610 | case RfbProto.Bell: |
| 611 | Toolkit.getDefaultToolkit().beep(); |
| 612 | break; |
| 613 | |
| 614 | case RfbProto.ServerCutText: |
| 615 | String s = rfb.readServerCutText(); |
| 616 | viewer.clipboard.setCutText(s); |
| 617 | break; |
| 618 | |
| 619 | case RfbProto.EndOfContinuousUpdates: |
| 620 | if (rfb.continuousUpdatesAreActive()) { |
| 621 | rfb.endOfContinuousUpdates(); |
| 622 | |
| 623 | // Change pixel format if such change was pending. Note that we |
| 624 | // could not change pixel format while continuous updates were |
| 625 | // in effect. |
| 626 | boolean incremental = true; |
| 627 | if (viewer.options.eightBitColors != (bytesPixel == 1)) { |
| 628 | setPixelFormat(); |
| 629 | incremental = false; |
| 630 | } |
| 631 | // From this point, we ask for updates explicitly. |
| 632 | rfb.writeFramebufferUpdateRequest(0, 0, rfb.framebufferWidth, |
| 633 | rfb.framebufferHeight, |
| 634 | incremental); |
| 635 | } |
| 636 | break; |
| 637 | |
| 638 | default: |
| 639 | throw new Exception("Unknown RFB message type " + msgType); |
| 640 | } |
| 641 | } |
| 642 | } |
| 643 | |
Constantin Kaplinsky | 2844fd5 | 2008-04-14 08:02:25 +0000 | [diff] [blame] | 644 | // |
| 645 | // Handle a raw rectangle. The second form with paint==false is used |
| 646 | // by the Hextile decoder for raw-encoded tiles. |
| 647 | // |
| 648 | |
enikey | 6c72ce1 | 2008-12-19 09:47:14 +0000 | [diff] [blame] | 649 | void handleRawRect(int x, int y, int w, int h) throws IOException, Exception { |
Constantin Kaplinsky | 2844fd5 | 2008-04-14 08:02:25 +0000 | [diff] [blame] | 650 | handleRawRect(x, y, w, h, true); |
| 651 | } |
| 652 | |
| 653 | void handleRawRect(int x, int y, int w, int h, boolean paint) |
enikey | 6c72ce1 | 2008-12-19 09:47:14 +0000 | [diff] [blame] | 654 | throws IOException , Exception{ |
| 655 | rawDecoder.handleRect(x, y, w, h); |
Constantin Kaplinsky | 2844fd5 | 2008-04-14 08:02:25 +0000 | [diff] [blame] | 656 | if (paint) |
| 657 | scheduleRepaint(x, y, w, h); |
| 658 | } |
| 659 | |
| 660 | // |
| 661 | // Handle a CopyRect rectangle. |
| 662 | // |
| 663 | |
| 664 | void handleCopyRect(int x, int y, int w, int h) throws IOException { |
| 665 | |
| 666 | rfb.readCopyRect(); |
| 667 | memGraphics.copyArea(rfb.copyRectSrcX, rfb.copyRectSrcY, w, h, |
| 668 | x - rfb.copyRectSrcX, y - rfb.copyRectSrcY); |
| 669 | |
| 670 | scheduleRepaint(x, y, w, h); |
| 671 | } |
| 672 | |
| 673 | // |
| 674 | // Handle an RRE-encoded rectangle. |
| 675 | // |
| 676 | |
| 677 | void handleRRERect(int x, int y, int w, int h) throws IOException { |
enikey | 6c72ce1 | 2008-12-19 09:47:14 +0000 | [diff] [blame] | 678 | rreDecoder.handleRect(x, y, w, h); |
Constantin Kaplinsky | 2844fd5 | 2008-04-14 08:02:25 +0000 | [diff] [blame] | 679 | scheduleRepaint(x, y, w, h); |
| 680 | } |
| 681 | |
| 682 | // |
| 683 | // Handle a CoRRE-encoded rectangle. |
| 684 | // |
| 685 | |
| 686 | void handleCoRRERect(int x, int y, int w, int h) throws IOException { |
enikey | 6c72ce1 | 2008-12-19 09:47:14 +0000 | [diff] [blame] | 687 | correDecoder.handleRect(x, y, w, h); |
Constantin Kaplinsky | 2844fd5 | 2008-04-14 08:02:25 +0000 | [diff] [blame] | 688 | scheduleRepaint(x, y, w, h); |
| 689 | } |
| 690 | |
| 691 | // |
| 692 | // Handle a Hextile-encoded rectangle. |
| 693 | // |
| 694 | |
enikey | 6c72ce1 | 2008-12-19 09:47:14 +0000 | [diff] [blame] | 695 | void handleHextileRect(int x, int y, int w, int h) throws IOException, |
| 696 | Exception { |
| 697 | hextileDecoder.handleRect(x, y, w, h); |
Constantin Kaplinsky | 2844fd5 | 2008-04-14 08:02:25 +0000 | [diff] [blame] | 698 | } |
| 699 | |
| 700 | // |
Constantin Kaplinsky | 2844fd5 | 2008-04-14 08:02:25 +0000 | [diff] [blame] | 701 | // Handle a ZRLE-encoded rectangle. |
| 702 | // |
| 703 | // FIXME: Currently, session recording is not fully supported for ZRLE. |
| 704 | // |
| 705 | |
| 706 | void handleZRLERect(int x, int y, int w, int h) throws Exception { |
enikey | c92c1d1 | 2008-12-19 09:58:31 +0000 | [diff] [blame] | 707 | zrleDecoder.handleRect(x, y, w, h); |
Constantin Kaplinsky | 2844fd5 | 2008-04-14 08:02:25 +0000 | [diff] [blame] | 708 | scheduleRepaint(x, y, w, h); |
| 709 | } |
| 710 | |
Constantin Kaplinsky | 2844fd5 | 2008-04-14 08:02:25 +0000 | [diff] [blame] | 711 | // |
| 712 | // Handle a Zlib-encoded rectangle. |
| 713 | // |
| 714 | |
| 715 | void handleZlibRect(int x, int y, int w, int h) throws Exception { |
enikey | c92c1d1 | 2008-12-19 09:58:31 +0000 | [diff] [blame] | 716 | zlibDecoder.handleRect(x, y, w, h); |
Constantin Kaplinsky | 2844fd5 | 2008-04-14 08:02:25 +0000 | [diff] [blame] | 717 | scheduleRepaint(x, y, w, h); |
| 718 | } |
| 719 | |
| 720 | // |
| 721 | // Handle a Tight-encoded rectangle. |
| 722 | // |
| 723 | |
| 724 | void handleTightRect(int x, int y, int w, int h) throws Exception { |
enikey | 2f7f46e | 2008-12-19 09:32:35 +0000 | [diff] [blame] | 725 | tightDecoder.handleRect(x, y, w, h); |
Constantin Kaplinsky | 2844fd5 | 2008-04-14 08:02:25 +0000 | [diff] [blame] | 726 | scheduleRepaint(x, y, w, h); |
| 727 | } |
| 728 | |
| 729 | // |
Constantin Kaplinsky | 2844fd5 | 2008-04-14 08:02:25 +0000 | [diff] [blame] | 730 | // Tell JVM to repaint specified desktop area. |
| 731 | // |
| 732 | |
enikey | 0dbc153 | 2008-12-19 08:51:47 +0000 | [diff] [blame] | 733 | public void scheduleRepaint(int x, int y, int w, int h) { |
Constantin Kaplinsky | 2844fd5 | 2008-04-14 08:02:25 +0000 | [diff] [blame] | 734 | // Request repaint, deferred if necessary. |
| 735 | if (rfb.framebufferWidth == scaledWidth) { |
| 736 | repaint(viewer.deferScreenUpdates, x, y, w, h); |
| 737 | } else { |
| 738 | int sx = x * scalingFactor / 100; |
| 739 | int sy = y * scalingFactor / 100; |
| 740 | int sw = ((x + w) * scalingFactor + 49) / 100 - sx + 1; |
| 741 | int sh = ((y + h) * scalingFactor + 49) / 100 - sy + 1; |
| 742 | repaint(viewer.deferScreenUpdates, sx, sy, sw, sh); |
| 743 | } |
| 744 | } |
| 745 | |
| 746 | // |
| 747 | // Handle events. |
| 748 | // |
| 749 | |
| 750 | public void keyPressed(KeyEvent evt) { |
| 751 | processLocalKeyEvent(evt); |
| 752 | } |
| 753 | public void keyReleased(KeyEvent evt) { |
| 754 | processLocalKeyEvent(evt); |
| 755 | } |
| 756 | public void keyTyped(KeyEvent evt) { |
| 757 | evt.consume(); |
| 758 | } |
| 759 | |
| 760 | public void mousePressed(MouseEvent evt) { |
| 761 | processLocalMouseEvent(evt, false); |
| 762 | } |
| 763 | public void mouseReleased(MouseEvent evt) { |
| 764 | processLocalMouseEvent(evt, false); |
| 765 | } |
| 766 | public void mouseMoved(MouseEvent evt) { |
| 767 | processLocalMouseEvent(evt, true); |
| 768 | } |
| 769 | public void mouseDragged(MouseEvent evt) { |
| 770 | processLocalMouseEvent(evt, true); |
| 771 | } |
| 772 | |
Constantin Kaplinsky | f7cb2bf | 2008-05-27 08:38:28 +0000 | [diff] [blame] | 773 | // |
| 774 | // Ignored events. |
| 775 | // |
| 776 | |
| 777 | public void mouseClicked(MouseEvent evt) {} |
| 778 | public void mouseEntered(MouseEvent evt) {} |
| 779 | public void mouseExited(MouseEvent evt) {} |
| 780 | |
| 781 | // |
| 782 | // Actual event processing. |
| 783 | // |
| 784 | |
| 785 | private void processLocalKeyEvent(KeyEvent evt) { |
Constantin Kaplinsky | 2844fd5 | 2008-04-14 08:02:25 +0000 | [diff] [blame] | 786 | if (viewer.rfb != null && rfb.inNormalProtocol) { |
| 787 | if (!inputEnabled) { |
| 788 | if ((evt.getKeyChar() == 'r' || evt.getKeyChar() == 'R') && |
| 789 | evt.getID() == KeyEvent.KEY_PRESSED ) { |
| 790 | // Request screen update. |
| 791 | try { |
| 792 | rfb.writeFramebufferUpdateRequest(0, 0, rfb.framebufferWidth, |
| 793 | rfb.framebufferHeight, false); |
| 794 | } catch (IOException e) { |
| 795 | e.printStackTrace(); |
| 796 | } |
| 797 | } |
| 798 | } else { |
| 799 | // Input enabled. |
| 800 | synchronized(rfb) { |
| 801 | try { |
| 802 | rfb.writeKeyEvent(evt); |
| 803 | } catch (Exception e) { |
| 804 | e.printStackTrace(); |
| 805 | } |
| 806 | rfb.notify(); |
| 807 | } |
| 808 | } |
| 809 | } |
enikey | c41ba1d | 2008-12-19 09:07:22 +0000 | [diff] [blame] | 810 | // Don't ever pass keyboard events to AWT for default processing. |
Constantin Kaplinsky | 2844fd5 | 2008-04-14 08:02:25 +0000 | [diff] [blame] | 811 | // Otherwise, pressing Tab would switch focus to ButtonPanel etc. |
| 812 | evt.consume(); |
| 813 | } |
| 814 | |
Constantin Kaplinsky | f7cb2bf | 2008-05-27 08:38:28 +0000 | [diff] [blame] | 815 | private void processLocalMouseEvent(MouseEvent evt, boolean moved) { |
Constantin Kaplinsky | 2844fd5 | 2008-04-14 08:02:25 +0000 | [diff] [blame] | 816 | if (viewer.rfb != null && rfb.inNormalProtocol) { |
Constantin Kaplinsky | f7cb2bf | 2008-05-27 08:38:28 +0000 | [diff] [blame] | 817 | if (!inSelectionMode) { |
| 818 | if (inputEnabled) { |
| 819 | sendMouseEvent(evt, moved); |
| 820 | } |
| 821 | } else { |
| 822 | handleSelectionMouseEvent(evt); |
Constantin Kaplinsky | 2844fd5 | 2008-04-14 08:02:25 +0000 | [diff] [blame] | 823 | } |
| 824 | } |
| 825 | } |
| 826 | |
Constantin Kaplinsky | f7cb2bf | 2008-05-27 08:38:28 +0000 | [diff] [blame] | 827 | private void sendMouseEvent(MouseEvent evt, boolean moved) { |
| 828 | if (moved) { |
| 829 | softCursorMove(evt.getX(), evt.getY()); |
| 830 | } |
| 831 | if (rfb.framebufferWidth != scaledWidth) { |
| 832 | int sx = (evt.getX() * 100 + scalingFactor/2) / scalingFactor; |
| 833 | int sy = (evt.getY() * 100 + scalingFactor/2) / scalingFactor; |
| 834 | evt.translatePoint(sx - evt.getX(), sy - evt.getY()); |
| 835 | } |
| 836 | synchronized(rfb) { |
| 837 | try { |
| 838 | rfb.writePointerEvent(evt); |
| 839 | } catch (Exception e) { |
| 840 | e.printStackTrace(); |
| 841 | } |
| 842 | rfb.notify(); |
| 843 | } |
| 844 | } |
Constantin Kaplinsky | 2844fd5 | 2008-04-14 08:02:25 +0000 | [diff] [blame] | 845 | |
| 846 | // |
| 847 | // Reset update statistics. |
| 848 | // |
| 849 | |
| 850 | void resetStats() { |
| 851 | statStartTime = System.currentTimeMillis(); |
| 852 | statNumUpdates = 0; |
| 853 | statNumTotalRects = 0; |
| 854 | statNumPixelRects = 0; |
| 855 | statNumRectsTight = 0; |
| 856 | statNumRectsTightJPEG = 0; |
| 857 | statNumRectsZRLE = 0; |
| 858 | statNumRectsHextile = 0; |
| 859 | statNumRectsRaw = 0; |
| 860 | statNumRectsCopy = 0; |
| 861 | statNumBytesEncoded = 0; |
| 862 | statNumBytesDecoded = 0; |
enikey | 1214ab1 | 2008-12-24 09:01:19 +0000 | [diff] [blame] | 863 | if (tightDecoder != null) { |
enikey | 2f7f46e | 2008-12-19 09:32:35 +0000 | [diff] [blame] | 864 | tightDecoder.setNumJPEGRects(0); |
enikey | 1214ab1 | 2008-12-24 09:01:19 +0000 | [diff] [blame] | 865 | tightDecoder.setNumTightRects(0); |
| 866 | } |
Constantin Kaplinsky | 2844fd5 | 2008-04-14 08:02:25 +0000 | [diff] [blame] | 867 | } |
| 868 | |
| 869 | ////////////////////////////////////////////////////////////////// |
| 870 | // |
| 871 | // Handle cursor shape updates (XCursor and RichCursor encodings). |
| 872 | // |
| 873 | |
| 874 | boolean showSoftCursor = false; |
| 875 | |
| 876 | MemoryImageSource softCursorSource; |
| 877 | Image softCursor; |
| 878 | |
| 879 | int cursorX = 0, cursorY = 0; |
| 880 | int cursorWidth, cursorHeight; |
| 881 | int origCursorWidth, origCursorHeight; |
| 882 | int hotX, hotY; |
| 883 | int origHotX, origHotY; |
| 884 | |
| 885 | // |
| 886 | // Handle cursor shape update (XCursor and RichCursor encodings). |
| 887 | // |
| 888 | |
| 889 | synchronized void |
| 890 | handleCursorShapeUpdate(int encodingType, |
| 891 | int xhot, int yhot, int width, int height) |
| 892 | throws IOException { |
| 893 | |
| 894 | softCursorFree(); |
| 895 | |
| 896 | if (width * height == 0) |
| 897 | return; |
| 898 | |
| 899 | // Ignore cursor shape data if requested by user. |
| 900 | if (viewer.options.ignoreCursorUpdates) { |
| 901 | int bytesPerRow = (width + 7) / 8; |
| 902 | int bytesMaskData = bytesPerRow * height; |
| 903 | |
| 904 | if (encodingType == rfb.EncodingXCursor) { |
| 905 | rfb.skipBytes(6 + bytesMaskData * 2); |
| 906 | } else { |
| 907 | // rfb.EncodingRichCursor |
| 908 | rfb.skipBytes(width * height + bytesMaskData); |
| 909 | } |
| 910 | return; |
| 911 | } |
| 912 | |
| 913 | // Decode cursor pixel data. |
| 914 | softCursorSource = decodeCursorShape(encodingType, width, height); |
| 915 | |
| 916 | // Set original (non-scaled) cursor dimensions. |
| 917 | origCursorWidth = width; |
| 918 | origCursorHeight = height; |
| 919 | origHotX = xhot; |
| 920 | origHotY = yhot; |
| 921 | |
| 922 | // Create off-screen cursor image. |
| 923 | createSoftCursor(); |
| 924 | |
| 925 | // Show the cursor. |
| 926 | showSoftCursor = true; |
| 927 | repaint(viewer.deferCursorUpdates, |
| 928 | cursorX - hotX, cursorY - hotY, cursorWidth, cursorHeight); |
| 929 | } |
| 930 | |
| 931 | // |
| 932 | // decodeCursorShape(). Decode cursor pixel data and return |
| 933 | // corresponding MemoryImageSource instance. |
| 934 | // |
| 935 | |
| 936 | synchronized MemoryImageSource |
| 937 | decodeCursorShape(int encodingType, int width, int height) |
| 938 | throws IOException { |
| 939 | |
| 940 | int bytesPerRow = (width + 7) / 8; |
| 941 | int bytesMaskData = bytesPerRow * height; |
| 942 | |
| 943 | int[] softCursorPixels = new int[width * height]; |
| 944 | |
| 945 | if (encodingType == rfb.EncodingXCursor) { |
| 946 | |
| 947 | // Read foreground and background colors of the cursor. |
| 948 | byte[] rgb = new byte[6]; |
| 949 | rfb.readFully(rgb); |
| 950 | int[] colors = { (0xFF000000 | (rgb[3] & 0xFF) << 16 | |
| 951 | (rgb[4] & 0xFF) << 8 | (rgb[5] & 0xFF)), |
| 952 | (0xFF000000 | (rgb[0] & 0xFF) << 16 | |
| 953 | (rgb[1] & 0xFF) << 8 | (rgb[2] & 0xFF)) }; |
| 954 | |
| 955 | // Read pixel and mask data. |
| 956 | byte[] pixBuf = new byte[bytesMaskData]; |
| 957 | rfb.readFully(pixBuf); |
| 958 | byte[] maskBuf = new byte[bytesMaskData]; |
| 959 | rfb.readFully(maskBuf); |
| 960 | |
| 961 | // Decode pixel data into softCursorPixels[]. |
| 962 | byte pixByte, maskByte; |
| 963 | int x, y, n, result; |
| 964 | int i = 0; |
| 965 | for (y = 0; y < height; y++) { |
| 966 | for (x = 0; x < width / 8; x++) { |
| 967 | pixByte = pixBuf[y * bytesPerRow + x]; |
| 968 | maskByte = maskBuf[y * bytesPerRow + x]; |
| 969 | for (n = 7; n >= 0; n--) { |
| 970 | if ((maskByte >> n & 1) != 0) { |
| 971 | result = colors[pixByte >> n & 1]; |
| 972 | } else { |
| 973 | result = 0; // Transparent pixel |
| 974 | } |
| 975 | softCursorPixels[i++] = result; |
| 976 | } |
| 977 | } |
| 978 | for (n = 7; n >= 8 - width % 8; n--) { |
| 979 | if ((maskBuf[y * bytesPerRow + x] >> n & 1) != 0) { |
| 980 | result = colors[pixBuf[y * bytesPerRow + x] >> n & 1]; |
| 981 | } else { |
| 982 | result = 0; // Transparent pixel |
| 983 | } |
| 984 | softCursorPixels[i++] = result; |
| 985 | } |
| 986 | } |
| 987 | |
| 988 | } else { |
| 989 | // encodingType == rfb.EncodingRichCursor |
| 990 | |
| 991 | // Read pixel and mask data. |
| 992 | byte[] pixBuf = new byte[width * height * bytesPixel]; |
| 993 | rfb.readFully(pixBuf); |
| 994 | byte[] maskBuf = new byte[bytesMaskData]; |
| 995 | rfb.readFully(maskBuf); |
| 996 | |
| 997 | // Decode pixel data into softCursorPixels[]. |
| 998 | byte pixByte, maskByte; |
| 999 | int x, y, n, result; |
| 1000 | int i = 0; |
| 1001 | for (y = 0; y < height; y++) { |
| 1002 | for (x = 0; x < width / 8; x++) { |
| 1003 | maskByte = maskBuf[y * bytesPerRow + x]; |
| 1004 | for (n = 7; n >= 0; n--) { |
| 1005 | if ((maskByte >> n & 1) != 0) { |
| 1006 | if (bytesPixel == 1) { |
| 1007 | result = cm8.getRGB(pixBuf[i]); |
| 1008 | } else { |
| 1009 | result = 0xFF000000 | |
| 1010 | (pixBuf[i * 4 + 2] & 0xFF) << 16 | |
| 1011 | (pixBuf[i * 4 + 1] & 0xFF) << 8 | |
| 1012 | (pixBuf[i * 4] & 0xFF); |
| 1013 | } |
| 1014 | } else { |
| 1015 | result = 0; // Transparent pixel |
| 1016 | } |
| 1017 | softCursorPixels[i++] = result; |
| 1018 | } |
| 1019 | } |
| 1020 | for (n = 7; n >= 8 - width % 8; n--) { |
| 1021 | if ((maskBuf[y * bytesPerRow + x] >> n & 1) != 0) { |
| 1022 | if (bytesPixel == 1) { |
| 1023 | result = cm8.getRGB(pixBuf[i]); |
| 1024 | } else { |
| 1025 | result = 0xFF000000 | |
| 1026 | (pixBuf[i * 4 + 2] & 0xFF) << 16 | |
| 1027 | (pixBuf[i * 4 + 1] & 0xFF) << 8 | |
| 1028 | (pixBuf[i * 4] & 0xFF); |
| 1029 | } |
| 1030 | } else { |
| 1031 | result = 0; // Transparent pixel |
| 1032 | } |
| 1033 | softCursorPixels[i++] = result; |
| 1034 | } |
| 1035 | } |
| 1036 | |
| 1037 | } |
| 1038 | |
| 1039 | return new MemoryImageSource(width, height, softCursorPixels, 0, width); |
| 1040 | } |
| 1041 | |
| 1042 | // |
| 1043 | // createSoftCursor(). Assign softCursor new Image (scaled if necessary). |
| 1044 | // Uses softCursorSource as a source for new cursor image. |
| 1045 | // |
| 1046 | |
| 1047 | synchronized void |
| 1048 | createSoftCursor() { |
| 1049 | |
| 1050 | if (softCursorSource == null) |
| 1051 | return; |
| 1052 | |
| 1053 | int scaleCursor = viewer.options.scaleCursor; |
| 1054 | if (scaleCursor == 0 || !inputEnabled) |
| 1055 | scaleCursor = 100; |
| 1056 | |
| 1057 | // Save original cursor coordinates. |
| 1058 | int x = cursorX - hotX; |
| 1059 | int y = cursorY - hotY; |
| 1060 | int w = cursorWidth; |
| 1061 | int h = cursorHeight; |
| 1062 | |
| 1063 | cursorWidth = (origCursorWidth * scaleCursor + 50) / 100; |
| 1064 | cursorHeight = (origCursorHeight * scaleCursor + 50) / 100; |
| 1065 | hotX = (origHotX * scaleCursor + 50) / 100; |
| 1066 | hotY = (origHotY * scaleCursor + 50) / 100; |
| 1067 | softCursor = Toolkit.getDefaultToolkit().createImage(softCursorSource); |
| 1068 | |
| 1069 | if (scaleCursor != 100) { |
| 1070 | softCursor = softCursor.getScaledInstance(cursorWidth, cursorHeight, |
| 1071 | Image.SCALE_SMOOTH); |
| 1072 | } |
| 1073 | |
| 1074 | if (showSoftCursor) { |
| 1075 | // Compute screen area to update. |
| 1076 | x = Math.min(x, cursorX - hotX); |
| 1077 | y = Math.min(y, cursorY - hotY); |
| 1078 | w = Math.max(w, cursorWidth); |
| 1079 | h = Math.max(h, cursorHeight); |
| 1080 | |
| 1081 | repaint(viewer.deferCursorUpdates, x, y, w, h); |
| 1082 | } |
| 1083 | } |
| 1084 | |
| 1085 | // |
| 1086 | // softCursorMove(). Moves soft cursor into a particular location. |
| 1087 | // |
| 1088 | |
| 1089 | synchronized void softCursorMove(int x, int y) { |
| 1090 | int oldX = cursorX; |
| 1091 | int oldY = cursorY; |
| 1092 | cursorX = x; |
| 1093 | cursorY = y; |
| 1094 | if (showSoftCursor) { |
| 1095 | repaint(viewer.deferCursorUpdates, |
| 1096 | oldX - hotX, oldY - hotY, cursorWidth, cursorHeight); |
| 1097 | repaint(viewer.deferCursorUpdates, |
| 1098 | cursorX - hotX, cursorY - hotY, cursorWidth, cursorHeight); |
| 1099 | } |
| 1100 | } |
| 1101 | |
| 1102 | // |
| 1103 | // softCursorFree(). Remove soft cursor, dispose resources. |
| 1104 | // |
| 1105 | |
| 1106 | synchronized void softCursorFree() { |
| 1107 | if (showSoftCursor) { |
| 1108 | showSoftCursor = false; |
| 1109 | softCursor = null; |
| 1110 | softCursorSource = null; |
| 1111 | |
| 1112 | repaint(viewer.deferCursorUpdates, |
| 1113 | cursorX - hotX, cursorY - hotY, cursorWidth, cursorHeight); |
| 1114 | } |
| 1115 | } |
Constantin Kaplinsky | f7cb2bf | 2008-05-27 08:38:28 +0000 | [diff] [blame] | 1116 | |
| 1117 | ////////////////////////////////////////////////////////////////// |
| 1118 | // |
| 1119 | // Support for selecting a rectangular video area. |
| 1120 | // |
| 1121 | |
| 1122 | /** This flag is false in normal operation, and true in the selection mode. */ |
| 1123 | private boolean inSelectionMode; |
| 1124 | |
| 1125 | /** The point where the selection was started. */ |
| 1126 | private Point selectionStart; |
| 1127 | |
| 1128 | /** The second point of the selection. */ |
| 1129 | private Point selectionEnd; |
| 1130 | |
| 1131 | /** |
| 1132 | * We change cursor when enabling the selection mode. In this variable, we |
| 1133 | * save the original cursor so we can restore it on returning to the normal |
| 1134 | * mode. |
| 1135 | */ |
| 1136 | private Cursor savedCursor; |
| 1137 | |
| 1138 | /** |
| 1139 | * Initialize selection-related varibles. |
| 1140 | */ |
| 1141 | private synchronized void resetSelection() { |
| 1142 | inSelectionMode = false; |
| 1143 | selectionStart = new Point(0, 0); |
| 1144 | selectionEnd = new Point(0, 0); |
| 1145 | |
| 1146 | savedCursor = getCursor(); |
| 1147 | } |
| 1148 | |
| 1149 | /** |
| 1150 | * Check current state of the selection mode. |
| 1151 | * @return true in the selection mode, false otherwise. |
| 1152 | */ |
| 1153 | public boolean isInSelectionMode() { |
| 1154 | return inSelectionMode; |
| 1155 | } |
| 1156 | |
| 1157 | /** |
| 1158 | * Get current selection. |
| 1159 | * @param useScreenCoords use screen coordinates if true, or framebuffer |
| 1160 | * coordinates if false. This makes difference when scaling factor is not 100. |
| 1161 | * @return The selection as a {@link Rectangle}. |
| 1162 | */ |
| 1163 | private synchronized Rectangle getSelection(boolean useScreenCoords) { |
Constantin Kaplinsky | 4f374ff | 2008-09-03 04:51:28 +0000 | [diff] [blame] | 1164 | int x0 = selectionStart.x; |
| 1165 | int x1 = selectionEnd.x; |
| 1166 | int y0 = selectionStart.y; |
| 1167 | int y1 = selectionEnd.y; |
Constantin Kaplinsky | f7cb2bf | 2008-05-27 08:38:28 +0000 | [diff] [blame] | 1168 | // Make x and y point to the upper left corner of the selection. |
Constantin Kaplinsky | 4f374ff | 2008-09-03 04:51:28 +0000 | [diff] [blame] | 1169 | if (x1 < x0) { |
| 1170 | int t = x0; x0 = x1; x1 = t; |
Constantin Kaplinsky | f7cb2bf | 2008-05-27 08:38:28 +0000 | [diff] [blame] | 1171 | } |
Constantin Kaplinsky | 4f374ff | 2008-09-03 04:51:28 +0000 | [diff] [blame] | 1172 | if (y1 < y0) { |
| 1173 | int t = y0; y0 = y1; y1 = t; |
Constantin Kaplinsky | f7cb2bf | 2008-05-27 08:38:28 +0000 | [diff] [blame] | 1174 | } |
Constantin Kaplinsky | 4f374ff | 2008-09-03 04:51:28 +0000 | [diff] [blame] | 1175 | // Include the borders in the selection (unless it's empty). |
| 1176 | if (x0 != x1 && y0 != y1) { |
| 1177 | x1 += 1; |
| 1178 | y1 += 1; |
Constantin Kaplinsky | f7cb2bf | 2008-05-27 08:38:28 +0000 | [diff] [blame] | 1179 | } |
| 1180 | // Translate from screen coordinates to framebuffer coordinates. |
| 1181 | if (rfb.framebufferWidth != scaledWidth) { |
Constantin Kaplinsky | 4f374ff | 2008-09-03 04:51:28 +0000 | [diff] [blame] | 1182 | x0 = (x0 * 100 + scalingFactor/2) / scalingFactor; |
| 1183 | y0 = (y0 * 100 + scalingFactor/2) / scalingFactor; |
| 1184 | x1 = (x1 * 100 + scalingFactor/2) / scalingFactor; |
| 1185 | y1 = (y1 * 100 + scalingFactor/2) / scalingFactor; |
Constantin Kaplinsky | f7cb2bf | 2008-05-27 08:38:28 +0000 | [diff] [blame] | 1186 | } |
Constantin Kaplinsky | 10da44d | 2008-09-03 03:12:18 +0000 | [diff] [blame] | 1187 | // Clip the selection to framebuffer. |
Constantin Kaplinsky | 4f374ff | 2008-09-03 04:51:28 +0000 | [diff] [blame] | 1188 | if (x0 < 0) |
| 1189 | x0 = 0; |
| 1190 | if (y0 < 0) |
| 1191 | y0 = 0; |
| 1192 | if (x1 > rfb.framebufferWidth) |
| 1193 | x1 = rfb.framebufferWidth; |
| 1194 | if (y1 > rfb.framebufferHeight) |
| 1195 | y1 = rfb.framebufferHeight; |
Constantin Kaplinsky | f7cb2bf | 2008-05-27 08:38:28 +0000 | [diff] [blame] | 1196 | // Make width a multiple of 16. |
Constantin Kaplinsky | 4f374ff | 2008-09-03 04:51:28 +0000 | [diff] [blame] | 1197 | int widthBlocks = (x1 - x0 + 8) / 16; |
| 1198 | if (selectionStart.x <= selectionEnd.x) { |
| 1199 | x1 = x0 + widthBlocks * 16; |
| 1200 | if (x1 > rfb.framebufferWidth) { |
| 1201 | x1 -= 16; |
| 1202 | } |
| 1203 | } else { |
| 1204 | x0 = x1 - widthBlocks * 16; |
| 1205 | if (x0 < 0) { |
| 1206 | x0 += 16; |
| 1207 | } |
Constantin Kaplinsky | f7cb2bf | 2008-05-27 08:38:28 +0000 | [diff] [blame] | 1208 | } |
| 1209 | // Make height a multiple of 8. |
Constantin Kaplinsky | 4f374ff | 2008-09-03 04:51:28 +0000 | [diff] [blame] | 1210 | int heightBlocks = (y1 - y0 + 4) / 8; |
| 1211 | if (selectionStart.y <= selectionEnd.y) { |
| 1212 | y1 = y0 + heightBlocks * 8; |
| 1213 | if (y1 > rfb.framebufferHeight) { |
| 1214 | y1 -= 8; |
| 1215 | } |
| 1216 | } else { |
| 1217 | y0 = y1 - heightBlocks * 8; |
| 1218 | if (y0 < 0) { |
| 1219 | y0 += 8; |
| 1220 | } |
Constantin Kaplinsky | f7cb2bf | 2008-05-27 08:38:28 +0000 | [diff] [blame] | 1221 | } |
| 1222 | // Translate the selection back to screen coordinates if requested. |
Constantin Kaplinsky | f7cb2bf | 2008-05-27 08:38:28 +0000 | [diff] [blame] | 1223 | if (useScreenCoords && rfb.framebufferWidth != scaledWidth) { |
Constantin Kaplinsky | 4f374ff | 2008-09-03 04:51:28 +0000 | [diff] [blame] | 1224 | x0 = (x0 * scalingFactor + 50) / 100; |
| 1225 | y0 = (y0 * scalingFactor + 50) / 100; |
| 1226 | x1 = (x1 * scalingFactor + 50) / 100; |
| 1227 | y1 = (y1 * scalingFactor + 50) / 100; |
Constantin Kaplinsky | f7cb2bf | 2008-05-27 08:38:28 +0000 | [diff] [blame] | 1228 | } |
Constantin Kaplinsky | 4f374ff | 2008-09-03 04:51:28 +0000 | [diff] [blame] | 1229 | // Construct and return the result. |
| 1230 | return new Rectangle(x0, y0, x1 - x0, y1 - y0); |
Constantin Kaplinsky | f7cb2bf | 2008-05-27 08:38:28 +0000 | [diff] [blame] | 1231 | } |
| 1232 | |
| 1233 | /** |
| 1234 | * Enable or disable the selection mode. |
| 1235 | * @param enable enables the selection mode if true, disables if fasle. |
| 1236 | */ |
| 1237 | public synchronized void enableSelection(boolean enable) { |
| 1238 | if (enable && !inSelectionMode) { |
| 1239 | // Enter the selection mode. |
| 1240 | inSelectionMode = true; |
| 1241 | savedCursor = getCursor(); |
| 1242 | setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR)); |
| 1243 | repaint(); |
| 1244 | } else if (!enable && inSelectionMode) { |
| 1245 | // Leave the selection mode. |
| 1246 | inSelectionMode = false; |
| 1247 | setCursor(savedCursor); |
| 1248 | repaint(); |
| 1249 | } |
| 1250 | } |
| 1251 | |
| 1252 | /** |
| 1253 | * Process mouse events in the selection mode. |
enikey | c41ba1d | 2008-12-19 09:07:22 +0000 | [diff] [blame] | 1254 | * |
Constantin Kaplinsky | f7cb2bf | 2008-05-27 08:38:28 +0000 | [diff] [blame] | 1255 | * @param evt mouse event that was originally passed to |
| 1256 | * {@link MouseListener} or {@link MouseMotionListener}. |
| 1257 | */ |
| 1258 | private synchronized void handleSelectionMouseEvent(MouseEvent evt) { |
| 1259 | int id = evt.getID(); |
| 1260 | boolean button1 = (evt.getModifiers() & InputEvent.BUTTON1_MASK) != 0; |
| 1261 | |
| 1262 | if (id == MouseEvent.MOUSE_PRESSED && button1) { |
| 1263 | selectionStart = selectionEnd = evt.getPoint(); |
| 1264 | repaint(); |
| 1265 | } |
| 1266 | if (id == MouseEvent.MOUSE_DRAGGED && button1) { |
| 1267 | selectionEnd = evt.getPoint(); |
| 1268 | repaint(); |
| 1269 | } |
| 1270 | if (id == MouseEvent.MOUSE_RELEASED && button1) { |
| 1271 | try { |
| 1272 | rfb.trySendVideoSelection(getSelection(false)); |
| 1273 | } catch (IOException e) { |
| 1274 | e.printStackTrace(); |
| 1275 | } |
| 1276 | } |
| 1277 | } |
| 1278 | |
enikey | 418611f | 2008-12-19 04:37:09 +0000 | [diff] [blame] | 1279 | // |
| 1280 | // Override RecordInterface methods |
| 1281 | // |
| 1282 | |
| 1283 | public boolean isRecordFromBeginning() { |
| 1284 | return rfb.recordFromBeginning; |
| 1285 | } |
| 1286 | |
| 1287 | public boolean canWrite() { |
| 1288 | // We can record if rec is not null |
| 1289 | return rfb.rec != null; |
| 1290 | } |
| 1291 | |
| 1292 | public void write(byte b[]) throws IOException { |
| 1293 | rfb.rec.write(b); |
| 1294 | } |
| 1295 | |
| 1296 | public void write(byte b[], int off, int len) throws IOException { |
| 1297 | rfb.rec.write(b, off, len); |
| 1298 | } |
| 1299 | |
| 1300 | public void writeByte(byte b) throws IOException { |
| 1301 | rfb.rec.writeByte(b); |
| 1302 | } |
| 1303 | |
| 1304 | public void writeByte(int i) throws IOException { |
| 1305 | rfb.rec.writeByte(i); |
| 1306 | } |
| 1307 | |
| 1308 | public void writeIntBE(int v) throws IOException { |
| 1309 | rfb.rec.writeIntBE(v); |
| 1310 | } |
| 1311 | |
| 1312 | public void recordCompactLen(int len) throws IOException { |
| 1313 | rfb.recordCompactLen(len); |
| 1314 | } |
| 1315 | |
| 1316 | public void recordCompressedData(byte[] data) throws IOException { |
| 1317 | rfb.recordCompressedData(data); |
| 1318 | } |
Constantin Kaplinsky | 2844fd5 | 2008-04-14 08:02:25 +0000 | [diff] [blame] | 1319 | } |