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 | |
| 23 | // |
| 24 | // RfbProto.java |
| 25 | // |
| 26 | |
wimba.com | 8091a2f | 2007-11-19 19:05:01 +0000 | [diff] [blame] | 27 | package com.wimba.RfbPlayer; |
wimba.com | c23aeb0 | 2004-09-16 00:00:00 +0000 | [diff] [blame] | 28 | |
Constantin Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 29 | import java.io.*; |
Constantin Kaplinsky | 37cc43e | 2002-05-30 17:30:11 +0000 | [diff] [blame] | 30 | import java.net.*; |
Constantin Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 31 | |
| 32 | class RfbProto { |
| 33 | |
| 34 | final String versionMsg = "RFB 003.003\n"; |
Constantin Kaplinsky | 72e47ef | 2008-04-18 17:48:16 +0000 | [diff] [blame] | 35 | final static int ConnFailed = 0, NoAuth = 1, VncAuth = 2; |
| 36 | final static int VncAuthOK = 0, VncAuthFailed = 1, VncAuthTooMany = 2; |
Constantin Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 37 | |
Constantin Kaplinsky | 72e47ef | 2008-04-18 17:48:16 +0000 | [diff] [blame] | 38 | final static int FramebufferUpdate = 0, SetColourMapEntries = 1, Bell = 2, ServerCutText = |
| 39 | 3; |
Constantin Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 40 | |
Constantin Kaplinsky | 72e47ef | 2008-04-18 17:48:16 +0000 | [diff] [blame] | 41 | final int SetPixelFormat = 0, FixColourMapEntries = 1, SetEncodings = 2, FramebufferUpdateRequest = |
| 42 | 3, KeyboardEvent = 4, PointerEvent = 5, ClientCutText = 6; |
Constantin Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 43 | |
Constantin Kaplinsky | 72e47ef | 2008-04-18 17:48:16 +0000 | [diff] [blame] | 44 | final static int EncodingRaw = 0, EncodingCopyRect = 1, EncodingRRE = 2, EncodingCoRRE = |
| 45 | 4, EncodingHextile = 5, EncodingZlib = 6, EncodingTight = 7, EncodingCompressLevel0 = |
| 46 | 0xFFFFFF00, EncodingQualityLevel0 = 0xFFFFFFE0, EncodingXCursor = |
wimba.com | a27098c | 2004-09-21 15:22:02 +0000 | [diff] [blame] | 47 | 0xFFFFFF10, EncodingRichCursor = 0xFFFFFF11, EncodingPointerPos = |
| 48 | 0xFFFFFF18, EncodingLastRect = 0xFFFFFF20, EncodingNewFBSize = |
| 49 | 0xFFFFFF21; |
Constantin Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 50 | |
wimba.com | 93cc0db | 2004-10-13 16:19:45 +0000 | [diff] [blame] | 51 | final static int MaxNormalEncoding = 7; |
| 52 | |
Constantin Kaplinsky | 72e47ef | 2008-04-18 17:48:16 +0000 | [diff] [blame] | 53 | final int HextileRaw = (1 << 0); |
| 54 | final int HextileBackgroundSpecified = (1 << 1); |
| 55 | final int HextileForegroundSpecified = (1 << 2); |
| 56 | final int HextileAnySubrects = (1 << 3); |
| 57 | final int HextileSubrectsColoured = (1 << 4); |
Constantin Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 58 | |
Constantin Kaplinsky | 72e47ef | 2008-04-18 17:48:16 +0000 | [diff] [blame] | 59 | final static int TightExplicitFilter = 0x04; |
| 60 | final static int TightFill = 0x08; |
| 61 | final static int TightJpeg = 0x09; |
| 62 | final static int TightMaxSubencoding = 0x09; |
| 63 | final static int TightFilterCopy = 0x00; |
| 64 | final static int TightFilterPalette = 0x01; |
| 65 | final static int TightFilterGradient = 0x02; |
Constantin Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 66 | |
Constantin Kaplinsky | 72e47ef | 2008-04-18 17:48:16 +0000 | [diff] [blame] | 67 | final static int TightMinToCompress = 12; |
Constantin Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 68 | |
Constantin Kaplinsky | 37cc43e | 2002-05-30 17:30:11 +0000 | [diff] [blame] | 69 | FbsInputStream fbs; |
Constantin Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 70 | DataInputStream is; |
Constantin Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 71 | |
| 72 | |
| 73 | // |
Constantin Kaplinsky | 903009e | 2002-05-20 10:55:47 +0000 | [diff] [blame] | 74 | // Constructor. |
Constantin Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 75 | // |
Constantin Kaplinsky | 37cc43e | 2002-05-30 17:30:11 +0000 | [diff] [blame] | 76 | RfbProto(URL url) throws Exception { |
| 77 | fbs = null; |
| 78 | newSession(url); |
Constantin Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 79 | } |
| 80 | |
wimba.com | d1f56df | 2004-11-01 16:18:54 +0000 | [diff] [blame] | 81 | // Force processing to quit |
| 82 | public void quit() { |
| 83 | fbs.quit(); |
| 84 | try { |
wimba.com | 30ff9ed | 2004-11-01 20:54:08 +0000 | [diff] [blame] | 85 | fbs.close(); |
wimba.com | d1f56df | 2004-11-01 16:18:54 +0000 | [diff] [blame] | 86 | } catch (IOException e) { |
| 87 | System.out.println("IOException quitting RfbProto: " + e); |
| 88 | } |
| 89 | } |
| 90 | |
Constantin Kaplinsky | c833e01 | 2002-05-30 17:59:22 +0000 | [diff] [blame] | 91 | // |
| 92 | // Open new session URL. |
| 93 | // |
Constantin Kaplinsky | 37cc43e | 2002-05-30 17:30:11 +0000 | [diff] [blame] | 94 | public void newSession(URL url) throws Exception { |
| 95 | if (fbs != null) |
| 96 | fbs.close(); |
wimba.com | 9831b81 | 2007-11-14 22:25:34 +0000 | [diff] [blame] | 97 | |
wimba.com | 5cf6b2a | 2008-01-02 21:50:38 +0000 | [diff] [blame^] | 98 | // open the connection, and use caching |
wimba.com | 9831b81 | 2007-11-14 22:25:34 +0000 | [diff] [blame] | 99 | URLConnection connection = url.openConnection(); |
wimba.com | 5cf6b2a | 2008-01-02 21:50:38 +0000 | [diff] [blame^] | 100 | connection.setUseCaches(true); |
wimba.com | 9831b81 | 2007-11-14 22:25:34 +0000 | [diff] [blame] | 101 | |
| 102 | fbs = new FbsInputStream(connection.getInputStream()); |
Constantin Kaplinsky | 37cc43e | 2002-05-30 17:30:11 +0000 | [diff] [blame] | 103 | is = new DataInputStream(fbs); |
Constantin Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 104 | |
Constantin Kaplinsky | 903009e | 2002-05-20 10:55:47 +0000 | [diff] [blame] | 105 | readVersionMsg(); |
| 106 | if (readAuthScheme() != NoAuth) { |
Constantin Kaplinsky | f392f44 | 2002-05-20 13:05:42 +0000 | [diff] [blame] | 107 | throw new Exception("Wrong authentication type in the session file"); |
Constantin Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 108 | } |
Constantin Kaplinsky | 903009e | 2002-05-20 10:55:47 +0000 | [diff] [blame] | 109 | readServerInit(); |
Constantin Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | // |
Constantin Kaplinsky | c833e01 | 2002-05-30 17:59:22 +0000 | [diff] [blame] | 113 | // Read server's protocol version message. |
Constantin Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 114 | // |
Constantin Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 115 | int serverMajor, serverMinor; |
| 116 | |
| 117 | void readVersionMsg() throws IOException { |
| 118 | |
| 119 | byte[] b = new byte[12]; |
| 120 | |
wimba.com | c23aeb0 | 2004-09-16 00:00:00 +0000 | [diff] [blame] | 121 | for (int i = 0; i < b.length; i++) |
| 122 | b[i] = (byte)'0'; |
| 123 | |
Constantin Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 124 | is.readFully(b); |
| 125 | |
Constantin Kaplinsky | 72e47ef | 2008-04-18 17:48:16 +0000 | [diff] [blame] | 126 | if ((b[0] != 'R') || (b[1] != 'F') || (b[2] != 'B') || (b[3] != ' ') || |
| 127 | (b[4] < '0') || (b[4] > '9') || (b[5] < '0') || (b[5] > '9') || (b[6] < |
| 128 | '0') || (b[6] > '9') || (b[7] != '.') || (b[8] < '0') || (b[8] > '9') || |
| 129 | (b[9] < '0') || (b[9] > '9') || (b[10] < '0') || (b[10] > '9') || |
| 130 | (b[11] != '\n')) { |
Constantin Kaplinsky | 903009e | 2002-05-20 10:55:47 +0000 | [diff] [blame] | 131 | throw new IOException("Incorrect protocol version"); |
Constantin Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | serverMajor = (b[4] - '0') * 100 + (b[5] - '0') * 10 + (b[6] - '0'); |
| 135 | serverMinor = (b[8] - '0') * 100 + (b[9] - '0') * 10 + (b[10] - '0'); |
| 136 | } |
| 137 | |
| 138 | |
| 139 | // |
Constantin Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 140 | // Find out the authentication scheme. |
| 141 | // |
Constantin Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 142 | int readAuthScheme() throws IOException { |
| 143 | int authScheme = is.readInt(); |
| 144 | |
| 145 | switch (authScheme) { |
| 146 | |
| 147 | case ConnFailed: |
| 148 | int reasonLen = is.readInt(); |
| 149 | byte[] reason = new byte[reasonLen]; |
| 150 | is.readFully(reason); |
| 151 | throw new IOException(new String(reason)); |
| 152 | |
| 153 | case NoAuth: |
| 154 | case VncAuth: |
| 155 | return authScheme; |
| 156 | |
| 157 | default: |
Constantin Kaplinsky | 903009e | 2002-05-20 10:55:47 +0000 | [diff] [blame] | 158 | throw new IOException("Unknown authentication scheme " + authScheme); |
Constantin Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 159 | |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | |
| 164 | // |
Constantin Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 165 | // Read the server initialisation message |
| 166 | // |
Constantin Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 167 | String desktopName; |
| 168 | int framebufferWidth, framebufferHeight; |
| 169 | int bitsPerPixel, depth; |
| 170 | boolean bigEndian, trueColour; |
| 171 | int redMax, greenMax, blueMax, redShift, greenShift, blueShift; |
| 172 | |
Constantin Kaplinsky | f392f44 | 2002-05-20 13:05:42 +0000 | [diff] [blame] | 173 | void readServerInit() throws Exception { |
Constantin Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 174 | framebufferWidth = is.readUnsignedShort(); |
| 175 | framebufferHeight = is.readUnsignedShort(); |
| 176 | bitsPerPixel = is.readUnsignedByte(); |
| 177 | depth = is.readUnsignedByte(); |
| 178 | bigEndian = (is.readUnsignedByte() != 0); |
| 179 | trueColour = (is.readUnsignedByte() != 0); |
| 180 | redMax = is.readUnsignedShort(); |
| 181 | greenMax = is.readUnsignedShort(); |
| 182 | blueMax = is.readUnsignedShort(); |
| 183 | redShift = is.readUnsignedByte(); |
| 184 | greenShift = is.readUnsignedByte(); |
| 185 | blueShift = is.readUnsignedByte(); |
| 186 | byte[] pad = new byte[3]; |
| 187 | is.readFully(pad); |
| 188 | int nameLength = is.readInt(); |
| 189 | byte[] name = new byte[nameLength]; |
| 190 | is.readFully(name); |
| 191 | desktopName = new String(name); |
Constantin Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | |
| 195 | // |
| 196 | // Set new framebuffer size |
| 197 | // |
Constantin Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 198 | void setFramebufferSize(int width, int height) { |
| 199 | framebufferWidth = width; |
| 200 | framebufferHeight = height; |
| 201 | } |
| 202 | |
| 203 | |
| 204 | // |
| 205 | // Read the server message type |
| 206 | // |
Constantin Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 207 | int readServerMessageType() throws IOException { |
| 208 | return is.readUnsignedByte(); |
| 209 | } |
| 210 | |
| 211 | |
| 212 | // |
| 213 | // Read a FramebufferUpdate message |
| 214 | // |
Constantin Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 215 | int updateNRects; |
| 216 | |
| 217 | void readFramebufferUpdate() throws IOException { |
| 218 | is.readByte(); |
| 219 | updateNRects = is.readUnsignedShort(); |
| 220 | } |
| 221 | |
| 222 | // Read a FramebufferUpdate rectangle header |
Constantin Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 223 | int updateRectX, updateRectY, updateRectW, updateRectH, updateRectEncoding; |
| 224 | |
| 225 | void readFramebufferUpdateRectHdr() throws IOException { |
| 226 | updateRectX = is.readUnsignedShort(); |
| 227 | updateRectY = is.readUnsignedShort(); |
| 228 | updateRectW = is.readUnsignedShort(); |
| 229 | updateRectH = is.readUnsignedShort(); |
| 230 | updateRectEncoding = is.readInt(); |
| 231 | |
wimba.com | 93cc0db | 2004-10-13 16:19:45 +0000 | [diff] [blame] | 232 | if (updateRectEncoding < 0 || updateRectEncoding > MaxNormalEncoding) |
Constantin Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 233 | return; |
| 234 | |
| 235 | if ((updateRectX + updateRectW > framebufferWidth) || |
Constantin Kaplinsky | 72e47ef | 2008-04-18 17:48:16 +0000 | [diff] [blame] | 236 | (updateRectY + updateRectH > framebufferHeight)) { |
Constantin Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 237 | throw new IOException("Framebuffer update rectangle too large: " + |
Constantin Kaplinsky | 72e47ef | 2008-04-18 17:48:16 +0000 | [diff] [blame] | 238 | updateRectW + "x" + updateRectH + " at (" + |
| 239 | updateRectX + "," + updateRectY + ")"); |
Constantin Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 240 | } |
| 241 | } |
| 242 | |
| 243 | // Read CopyRect source X and Y. |
Constantin Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 244 | int copyRectSrcX, copyRectSrcY; |
| 245 | |
| 246 | void readCopyRect() throws IOException { |
| 247 | copyRectSrcX = is.readUnsignedShort(); |
| 248 | copyRectSrcY = is.readUnsignedShort(); |
| 249 | } |
| 250 | |
| 251 | |
| 252 | // |
| 253 | // Read a ServerCutText message |
| 254 | // |
Constantin Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 255 | String readServerCutText() throws IOException { |
| 256 | byte[] pad = new byte[3]; |
| 257 | is.readFully(pad); |
| 258 | int len = is.readInt(); |
| 259 | byte[] text = new byte[len]; |
| 260 | is.readFully(text); |
| 261 | return new String(text); |
| 262 | } |
| 263 | |
| 264 | |
| 265 | // |
| 266 | // Read integer in compact representation |
| 267 | // |
Constantin Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 268 | int readCompactLen() throws IOException { |
| 269 | int portion = is.readUnsignedByte(); |
| 270 | int len = portion & 0x7F; |
| 271 | if ((portion & 0x80) != 0) { |
| 272 | portion = is.readUnsignedByte(); |
| 273 | len |= (portion & 0x7F) << 7; |
| 274 | if ((portion & 0x80) != 0) { |
Constantin Kaplinsky | 72e47ef | 2008-04-18 17:48:16 +0000 | [diff] [blame] | 275 | portion = is.readUnsignedByte(); |
| 276 | len |= (portion & 0xFF) << 14; |
Constantin Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 277 | } |
| 278 | } |
| 279 | return len; |
| 280 | } |
| 281 | |
Constantin Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 282 | } |