blob: e776ef6c94115fff1f1f84a8fa45d2f94dd4eea7 [file] [log] [blame]
Constantin Kaplinsky1215b992008-04-18 09:51:44 +00001//
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.comc23aeb02004-09-16 00:00:00 +000027package com.HorizonLive.RfbPlayer;
28
Constantin Kaplinsky1215b992008-04-18 09:51:44 +000029import java.io.*;
30import java.awt.*;
31import java.awt.event.*;
Constantin Kaplinsky37cc43e2002-05-30 17:30:11 +000032import java.net.*;
Constantin Kaplinsky1215b992008-04-18 09:51:44 +000033
34class RfbProto {
35
36 final String versionMsg = "RFB 003.003\n";
Constantin Kaplinsky72e47ef2008-04-18 17:48:16 +000037 final static int ConnFailed = 0, NoAuth = 1, VncAuth = 2;
38 final static int VncAuthOK = 0, VncAuthFailed = 1, VncAuthTooMany = 2;
Constantin Kaplinsky1215b992008-04-18 09:51:44 +000039
Constantin Kaplinsky72e47ef2008-04-18 17:48:16 +000040 final static int FramebufferUpdate = 0, SetColourMapEntries = 1, Bell = 2, ServerCutText =
41 3;
Constantin Kaplinsky1215b992008-04-18 09:51:44 +000042
Constantin Kaplinsky72e47ef2008-04-18 17:48:16 +000043 final int SetPixelFormat = 0, FixColourMapEntries = 1, SetEncodings = 2, FramebufferUpdateRequest =
44 3, KeyboardEvent = 4, PointerEvent = 5, ClientCutText = 6;
Constantin Kaplinsky1215b992008-04-18 09:51:44 +000045
Constantin Kaplinsky72e47ef2008-04-18 17:48:16 +000046 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.coma27098c2004-09-21 15:22:02 +000049 0xFFFFFF10, EncodingRichCursor = 0xFFFFFF11, EncodingPointerPos =
50 0xFFFFFF18, EncodingLastRect = 0xFFFFFF20, EncodingNewFBSize =
51 0xFFFFFF21;
Constantin Kaplinsky1215b992008-04-18 09:51:44 +000052
Constantin Kaplinsky72e47ef2008-04-18 17:48:16 +000053 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 Kaplinsky1215b992008-04-18 09:51:44 +000058
Constantin Kaplinsky72e47ef2008-04-18 17:48:16 +000059 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 Kaplinsky1215b992008-04-18 09:51:44 +000066
Constantin Kaplinsky72e47ef2008-04-18 17:48:16 +000067 final static int TightMinToCompress = 12;
Constantin Kaplinsky1215b992008-04-18 09:51:44 +000068
Constantin Kaplinsky37cc43e2002-05-30 17:30:11 +000069 FbsInputStream fbs;
Constantin Kaplinsky1215b992008-04-18 09:51:44 +000070 DataInputStream is;
Constantin Kaplinsky1215b992008-04-18 09:51:44 +000071
72
73 //
Constantin Kaplinsky903009e2002-05-20 10:55:47 +000074 // Constructor.
Constantin Kaplinsky1215b992008-04-18 09:51:44 +000075 //
Constantin Kaplinsky37cc43e2002-05-30 17:30:11 +000076 RfbProto(URL url) throws Exception {
77 fbs = null;
78 newSession(url);
Constantin Kaplinsky1215b992008-04-18 09:51:44 +000079 }
80
Constantin Kaplinskyc833e012002-05-30 17:59:22 +000081 //
82 // Open new session URL.
83 //
Constantin Kaplinsky37cc43e2002-05-30 17:30:11 +000084 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 Kaplinsky1215b992008-04-18 09:51:44 +000089
Constantin Kaplinsky903009e2002-05-20 10:55:47 +000090 readVersionMsg();
91 if (readAuthScheme() != NoAuth) {
Constantin Kaplinskyf392f442002-05-20 13:05:42 +000092 throw new Exception("Wrong authentication type in the session file");
Constantin Kaplinsky1215b992008-04-18 09:51:44 +000093 }
Constantin Kaplinsky903009e2002-05-20 10:55:47 +000094 readServerInit();
Constantin Kaplinsky1215b992008-04-18 09:51:44 +000095 }
96
97 //
Constantin Kaplinskyc833e012002-05-30 17:59:22 +000098 // Read server's protocol version message.
Constantin Kaplinsky1215b992008-04-18 09:51:44 +000099 //
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000100 int serverMajor, serverMinor;
101
102 void readVersionMsg() throws IOException {
103
104 byte[] b = new byte[12];
105
wimba.comc23aeb02004-09-16 00:00:00 +0000106 for (int i = 0; i < b.length; i++)
107 b[i] = (byte)'0';
108
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000109 is.readFully(b);
110
Constantin Kaplinsky72e47ef2008-04-18 17:48:16 +0000111 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 Kaplinsky903009e2002-05-20 10:55:47 +0000116 throw new IOException("Incorrect protocol version");
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000117 }
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 Kaplinsky1215b992008-04-18 09:51:44 +0000125 // Find out the authentication scheme.
126 //
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000127 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 Kaplinsky903009e2002-05-20 10:55:47 +0000143 throw new IOException("Unknown authentication scheme " + authScheme);
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000144
145 }
146 }
147
148
149 //
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000150 // Read the server initialisation message
151 //
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000152 String desktopName;
153 int framebufferWidth, framebufferHeight;
154 int bitsPerPixel, depth;
155 boolean bigEndian, trueColour;
156 int redMax, greenMax, blueMax, redShift, greenShift, blueShift;
157
Constantin Kaplinskyf392f442002-05-20 13:05:42 +0000158 void readServerInit() throws Exception {
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000159 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 Kaplinsky1215b992008-04-18 09:51:44 +0000177 }
178
179
180 //
181 // Set new framebuffer size
182 //
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000183 void setFramebufferSize(int width, int height) {
184 framebufferWidth = width;
185 framebufferHeight = height;
186 }
187
188
189 //
190 // Read the server message type
191 //
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000192 int readServerMessageType() throws IOException {
193 return is.readUnsignedByte();
194 }
195
196
197 //
198 // Read a FramebufferUpdate message
199 //
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000200 int updateNRects;
201
202 void readFramebufferUpdate() throws IOException {
203 is.readByte();
204 updateNRects = is.readUnsignedShort();
205 }
206
207 // Read a FramebufferUpdate rectangle header
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000208 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 Kaplinsky72e47ef2008-04-18 17:48:16 +0000218 (updateRectEncoding == EncodingNewFBSize))
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000219 return;
220
221 if ((updateRectX + updateRectW > framebufferWidth) ||
Constantin Kaplinsky72e47ef2008-04-18 17:48:16 +0000222 (updateRectY + updateRectH > framebufferHeight)) {
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000223 throw new IOException("Framebuffer update rectangle too large: " +
Constantin Kaplinsky72e47ef2008-04-18 17:48:16 +0000224 updateRectW + "x" + updateRectH + " at (" +
225 updateRectX + "," + updateRectY + ")");
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000226 }
227 }
228
229 // Read CopyRect source X and Y.
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000230 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 Kaplinsky1215b992008-04-18 09:51:44 +0000241 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 Kaplinsky1215b992008-04-18 09:51:44 +0000254 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 Kaplinsky72e47ef2008-04-18 17:48:16 +0000261 portion = is.readUnsignedByte();
262 len |= (portion & 0xFF) << 14;
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000263 }
264 }
265 return len;
266 }
267
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000268}