blob: c60314856b5426794a3276f5883a81df8090aec7 [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();
99 fbs = new FbsInputStream(url.openStream());
100 is = new DataInputStream(fbs);
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000101
Constantin Kaplinsky903009e2002-05-20 10:55:47 +0000102 readVersionMsg();
103 if (readAuthScheme() != NoAuth) {
Constantin Kaplinskyf392f442002-05-20 13:05:42 +0000104 throw new Exception("Wrong authentication type in the session file");
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000105 }
Constantin Kaplinsky903009e2002-05-20 10:55:47 +0000106 readServerInit();
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000107 }
108
109 //
Constantin Kaplinskyc833e012002-05-30 17:59:22 +0000110 // Read server's protocol version message.
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000111 //
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000112 int serverMajor, serverMinor;
113
114 void readVersionMsg() throws IOException {
115
116 byte[] b = new byte[12];
117
wimba.comc23aeb02004-09-16 00:00:00 +0000118 for (int i = 0; i < b.length; i++)
119 b[i] = (byte)'0';
120
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000121 is.readFully(b);
122
Constantin Kaplinsky72e47ef2008-04-18 17:48:16 +0000123 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 Kaplinsky903009e2002-05-20 10:55:47 +0000128 throw new IOException("Incorrect protocol version");
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000129 }
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 Kaplinsky1215b992008-04-18 09:51:44 +0000137 // Find out the authentication scheme.
138 //
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000139 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 Kaplinsky903009e2002-05-20 10:55:47 +0000155 throw new IOException("Unknown authentication scheme " + authScheme);
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000156
157 }
158 }
159
160
161 //
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000162 // Read the server initialisation message
163 //
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000164 String desktopName;
165 int framebufferWidth, framebufferHeight;
166 int bitsPerPixel, depth;
167 boolean bigEndian, trueColour;
168 int redMax, greenMax, blueMax, redShift, greenShift, blueShift;
169
Constantin Kaplinskyf392f442002-05-20 13:05:42 +0000170 void readServerInit() throws Exception {
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000171 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 Kaplinsky1215b992008-04-18 09:51:44 +0000189 }
190
191
192 //
193 // Set new framebuffer size
194 //
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000195 void setFramebufferSize(int width, int height) {
196 framebufferWidth = width;
197 framebufferHeight = height;
198 }
199
200
201 //
202 // Read the server message type
203 //
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000204 int readServerMessageType() throws IOException {
205 return is.readUnsignedByte();
206 }
207
208
209 //
210 // Read a FramebufferUpdate message
211 //
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000212 int updateNRects;
213
214 void readFramebufferUpdate() throws IOException {
215 is.readByte();
216 updateNRects = is.readUnsignedShort();
217 }
218
219 // Read a FramebufferUpdate rectangle header
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000220 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.com93cc0db2004-10-13 16:19:45 +0000229 if (updateRectEncoding < 0 || updateRectEncoding > MaxNormalEncoding)
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000230 return;
231
232 if ((updateRectX + updateRectW > framebufferWidth) ||
Constantin Kaplinsky72e47ef2008-04-18 17:48:16 +0000233 (updateRectY + updateRectH > framebufferHeight)) {
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000234 throw new IOException("Framebuffer update rectangle too large: " +
Constantin Kaplinsky72e47ef2008-04-18 17:48:16 +0000235 updateRectW + "x" + updateRectH + " at (" +
236 updateRectX + "," + updateRectY + ")");
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000237 }
238 }
239
240 // Read CopyRect source X and Y.
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000241 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 Kaplinsky1215b992008-04-18 09:51:44 +0000252 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 Kaplinsky1215b992008-04-18 09:51:44 +0000265 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 Kaplinsky72e47ef2008-04-18 17:48:16 +0000272 portion = is.readUnsignedByte();
273 len |= (portion & 0xFF) << 14;
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000274 }
275 }
276 return len;
277 }
278
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000279}