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