blob: df2601995b08035013bedc461b04ad6ffc07a559 [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
wimba.com93cc0db2004-10-13 16:19:45 +000053 final static int MaxNormalEncoding = 7;
54
Constantin Kaplinsky72e47ef2008-04-18 17:48:16 +000055 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 Kaplinsky1215b992008-04-18 09:51:44 +000060
Constantin Kaplinsky72e47ef2008-04-18 17:48:16 +000061 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 Kaplinsky1215b992008-04-18 09:51:44 +000068
Constantin Kaplinsky72e47ef2008-04-18 17:48:16 +000069 final static int TightMinToCompress = 12;
Constantin Kaplinsky1215b992008-04-18 09:51:44 +000070
Constantin Kaplinsky37cc43e2002-05-30 17:30:11 +000071 FbsInputStream fbs;
Constantin Kaplinsky1215b992008-04-18 09:51:44 +000072 DataInputStream is;
Constantin Kaplinsky1215b992008-04-18 09:51:44 +000073
74
75 //
Constantin Kaplinsky903009e2002-05-20 10:55:47 +000076 // Constructor.
Constantin Kaplinsky1215b992008-04-18 09:51:44 +000077 //
Constantin Kaplinsky37cc43e2002-05-30 17:30:11 +000078 RfbProto(URL url) throws Exception {
79 fbs = null;
80 newSession(url);
Constantin Kaplinsky1215b992008-04-18 09:51:44 +000081 }
82
wimba.comd1f56df2004-11-01 16:18:54 +000083 // Force processing to quit
84 public void quit() {
85 fbs.quit();
86 try {
wimba.com30ff9ed2004-11-01 20:54:08 +000087 fbs.close();
wimba.comd1f56df2004-11-01 16:18:54 +000088 } catch (IOException e) {
89 System.out.println("IOException quitting RfbProto: " + e);
90 }
91 }
92
Constantin Kaplinskyc833e012002-05-30 17:59:22 +000093 //
94 // Open new session URL.
95 //
Constantin Kaplinsky37cc43e2002-05-30 17:30:11 +000096 public void newSession(URL url) throws Exception {
97 if (fbs != null)
98 fbs.close();
wimba.com9831b812007-11-14 22:25:34 +000099
100 // open the connection, making sure that it does not use
101 // a cached version of the archive
102 URLConnection connection = url.openConnection();
103 connection.setUseCaches(false);
104
105 fbs = new FbsInputStream(connection.getInputStream());
Constantin Kaplinsky37cc43e2002-05-30 17:30:11 +0000106 is = new DataInputStream(fbs);
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000107
Constantin Kaplinsky903009e2002-05-20 10:55:47 +0000108 readVersionMsg();
109 if (readAuthScheme() != NoAuth) {
Constantin Kaplinskyf392f442002-05-20 13:05:42 +0000110 throw new Exception("Wrong authentication type in the session file");
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000111 }
Constantin Kaplinsky903009e2002-05-20 10:55:47 +0000112 readServerInit();
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000113 }
114
115 //
Constantin Kaplinskyc833e012002-05-30 17:59:22 +0000116 // Read server's protocol version message.
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000117 //
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000118 int serverMajor, serverMinor;
119
120 void readVersionMsg() throws IOException {
121
122 byte[] b = new byte[12];
123
wimba.comc23aeb02004-09-16 00:00:00 +0000124 for (int i = 0; i < b.length; i++)
125 b[i] = (byte)'0';
126
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000127 is.readFully(b);
128
Constantin Kaplinsky72e47ef2008-04-18 17:48:16 +0000129 if ((b[0] != 'R') || (b[1] != 'F') || (b[2] != 'B') || (b[3] != ' ') ||
130 (b[4] < '0') || (b[4] > '9') || (b[5] < '0') || (b[5] > '9') || (b[6] <
131 '0') || (b[6] > '9') || (b[7] != '.') || (b[8] < '0') || (b[8] > '9') ||
132 (b[9] < '0') || (b[9] > '9') || (b[10] < '0') || (b[10] > '9') ||
133 (b[11] != '\n')) {
Constantin Kaplinsky903009e2002-05-20 10:55:47 +0000134 throw new IOException("Incorrect protocol version");
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000135 }
136
137 serverMajor = (b[4] - '0') * 100 + (b[5] - '0') * 10 + (b[6] - '0');
138 serverMinor = (b[8] - '0') * 100 + (b[9] - '0') * 10 + (b[10] - '0');
139 }
140
141
142 //
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000143 // Find out the authentication scheme.
144 //
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000145 int readAuthScheme() throws IOException {
146 int authScheme = is.readInt();
147
148 switch (authScheme) {
149
150 case ConnFailed:
151 int reasonLen = is.readInt();
152 byte[] reason = new byte[reasonLen];
153 is.readFully(reason);
154 throw new IOException(new String(reason));
155
156 case NoAuth:
157 case VncAuth:
158 return authScheme;
159
160 default:
Constantin Kaplinsky903009e2002-05-20 10:55:47 +0000161 throw new IOException("Unknown authentication scheme " + authScheme);
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000162
163 }
164 }
165
166
167 //
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000168 // Read the server initialisation message
169 //
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000170 String desktopName;
171 int framebufferWidth, framebufferHeight;
172 int bitsPerPixel, depth;
173 boolean bigEndian, trueColour;
174 int redMax, greenMax, blueMax, redShift, greenShift, blueShift;
175
Constantin Kaplinskyf392f442002-05-20 13:05:42 +0000176 void readServerInit() throws Exception {
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000177 framebufferWidth = is.readUnsignedShort();
178 framebufferHeight = is.readUnsignedShort();
179 bitsPerPixel = is.readUnsignedByte();
180 depth = is.readUnsignedByte();
181 bigEndian = (is.readUnsignedByte() != 0);
182 trueColour = (is.readUnsignedByte() != 0);
183 redMax = is.readUnsignedShort();
184 greenMax = is.readUnsignedShort();
185 blueMax = is.readUnsignedShort();
186 redShift = is.readUnsignedByte();
187 greenShift = is.readUnsignedByte();
188 blueShift = is.readUnsignedByte();
189 byte[] pad = new byte[3];
190 is.readFully(pad);
191 int nameLength = is.readInt();
192 byte[] name = new byte[nameLength];
193 is.readFully(name);
194 desktopName = new String(name);
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000195 }
196
197
198 //
199 // Set new framebuffer size
200 //
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000201 void setFramebufferSize(int width, int height) {
202 framebufferWidth = width;
203 framebufferHeight = height;
204 }
205
206
207 //
208 // Read the server message type
209 //
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000210 int readServerMessageType() throws IOException {
211 return is.readUnsignedByte();
212 }
213
214
215 //
216 // Read a FramebufferUpdate message
217 //
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000218 int updateNRects;
219
220 void readFramebufferUpdate() throws IOException {
221 is.readByte();
222 updateNRects = is.readUnsignedShort();
223 }
224
225 // Read a FramebufferUpdate rectangle header
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000226 int updateRectX, updateRectY, updateRectW, updateRectH, updateRectEncoding;
227
228 void readFramebufferUpdateRectHdr() throws IOException {
229 updateRectX = is.readUnsignedShort();
230 updateRectY = is.readUnsignedShort();
231 updateRectW = is.readUnsignedShort();
232 updateRectH = is.readUnsignedShort();
233 updateRectEncoding = is.readInt();
234
wimba.com93cc0db2004-10-13 16:19:45 +0000235 if (updateRectEncoding < 0 || updateRectEncoding > MaxNormalEncoding)
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000236 return;
237
238 if ((updateRectX + updateRectW > framebufferWidth) ||
Constantin Kaplinsky72e47ef2008-04-18 17:48:16 +0000239 (updateRectY + updateRectH > framebufferHeight)) {
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000240 throw new IOException("Framebuffer update rectangle too large: " +
Constantin Kaplinsky72e47ef2008-04-18 17:48:16 +0000241 updateRectW + "x" + updateRectH + " at (" +
242 updateRectX + "," + updateRectY + ")");
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000243 }
244 }
245
246 // Read CopyRect source X and Y.
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000247 int copyRectSrcX, copyRectSrcY;
248
249 void readCopyRect() throws IOException {
250 copyRectSrcX = is.readUnsignedShort();
251 copyRectSrcY = is.readUnsignedShort();
252 }
253
254
255 //
256 // Read a ServerCutText message
257 //
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000258 String readServerCutText() throws IOException {
259 byte[] pad = new byte[3];
260 is.readFully(pad);
261 int len = is.readInt();
262 byte[] text = new byte[len];
263 is.readFully(text);
264 return new String(text);
265 }
266
267
268 //
269 // Read integer in compact representation
270 //
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000271 int readCompactLen() throws IOException {
272 int portion = is.readUnsignedByte();
273 int len = portion & 0x7F;
274 if ((portion & 0x80) != 0) {
275 portion = is.readUnsignedByte();
276 len |= (portion & 0x7F) << 7;
277 if ((portion & 0x80) != 0) {
Constantin Kaplinsky72e47ef2008-04-18 17:48:16 +0000278 portion = is.readUnsignedByte();
279 len |= (portion & 0xFF) << 14;
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000280 }
281 }
282 return len;
283 }
284
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000285}