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