blob: 14186b6a6032a33a0072967fe385f691f96ce47f [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.com8091a2f2007-11-19 19:05:01 +000027package com.wimba.RfbPlayer;
wimba.comc23aeb02004-09-16 00:00:00 +000028
Constantin Kaplinsky1215b992008-04-18 09:51:44 +000029import java.io.*;
Constantin Kaplinsky37cc43e2002-05-30 17:30:11 +000030import java.net.*;
Constantin Kaplinsky1215b992008-04-18 09:51:44 +000031
32class RfbProto {
33
34 final String versionMsg = "RFB 003.003\n";
Constantin Kaplinsky72e47ef2008-04-18 17:48:16 +000035 final static int ConnFailed = 0, NoAuth = 1, VncAuth = 2;
36 final static int VncAuthOK = 0, VncAuthFailed = 1, VncAuthTooMany = 2;
Constantin Kaplinsky1215b992008-04-18 09:51:44 +000037
Constantin Kaplinsky72e47ef2008-04-18 17:48:16 +000038 final static int FramebufferUpdate = 0, SetColourMapEntries = 1, Bell = 2, ServerCutText =
39 3;
Constantin Kaplinsky1215b992008-04-18 09:51:44 +000040
Constantin Kaplinsky72e47ef2008-04-18 17:48:16 +000041 final int SetPixelFormat = 0, FixColourMapEntries = 1, SetEncodings = 2, FramebufferUpdateRequest =
42 3, KeyboardEvent = 4, PointerEvent = 5, ClientCutText = 6;
Constantin Kaplinsky1215b992008-04-18 09:51:44 +000043
Constantin Kaplinsky72e47ef2008-04-18 17:48:16 +000044 final static int EncodingRaw = 0, EncodingCopyRect = 1, EncodingRRE = 2, EncodingCoRRE =
45 4, EncodingHextile = 5, EncodingZlib = 6, EncodingTight = 7, EncodingCompressLevel0 =
46 0xFFFFFF00, EncodingQualityLevel0 = 0xFFFFFFE0, EncodingXCursor =
wimba.coma27098c2004-09-21 15:22:02 +000047 0xFFFFFF10, EncodingRichCursor = 0xFFFFFF11, EncodingPointerPos =
48 0xFFFFFF18, EncodingLastRect = 0xFFFFFF20, EncodingNewFBSize =
49 0xFFFFFF21;
Constantin Kaplinsky1215b992008-04-18 09:51:44 +000050
wimba.com93cc0db2004-10-13 16:19:45 +000051 final static int MaxNormalEncoding = 7;
52
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
wimba.comd1f56df2004-11-01 16:18:54 +000081 // Force processing to quit
82 public void quit() {
83 fbs.quit();
84 try {
wimba.com30ff9ed2004-11-01 20:54:08 +000085 fbs.close();
wimba.comd1f56df2004-11-01 16:18:54 +000086 } catch (IOException e) {
87 System.out.println("IOException quitting RfbProto: " + e);
88 }
89 }
90
Constantin Kaplinskyc833e012002-05-30 17:59:22 +000091 //
92 // Open new session URL.
93 //
Constantin Kaplinsky37cc43e2002-05-30 17:30:11 +000094 public void newSession(URL url) throws Exception {
95 if (fbs != null)
96 fbs.close();
wimba.com9831b812007-11-14 22:25:34 +000097
wimba.com5cf6b2a2008-01-02 21:50:38 +000098 // open the connection, and use caching
wimba.com9831b812007-11-14 22:25:34 +000099 URLConnection connection = url.openConnection();
wimba.com5cf6b2a2008-01-02 21:50:38 +0000100 connection.setUseCaches(true);
wimba.com9831b812007-11-14 22:25:34 +0000101
102 fbs = new FbsInputStream(connection.getInputStream());
Constantin Kaplinsky37cc43e2002-05-30 17:30:11 +0000103 is = new DataInputStream(fbs);
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000104
Constantin Kaplinsky903009e2002-05-20 10:55:47 +0000105 readVersionMsg();
106 if (readAuthScheme() != NoAuth) {
Constantin Kaplinskyf392f442002-05-20 13:05:42 +0000107 throw new Exception("Wrong authentication type in the session file");
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000108 }
Constantin Kaplinsky903009e2002-05-20 10:55:47 +0000109 readServerInit();
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000110 }
111
112 //
Constantin Kaplinskyc833e012002-05-30 17:59:22 +0000113 // Read server's protocol version message.
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000114 //
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000115 int serverMajor, serverMinor;
116
117 void readVersionMsg() throws IOException {
118
119 byte[] b = new byte[12];
120
wimba.comc23aeb02004-09-16 00:00:00 +0000121 for (int i = 0; i < b.length; i++)
122 b[i] = (byte)'0';
123
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000124 is.readFully(b);
125
Constantin Kaplinsky72e47ef2008-04-18 17:48:16 +0000126 if ((b[0] != 'R') || (b[1] != 'F') || (b[2] != 'B') || (b[3] != ' ') ||
127 (b[4] < '0') || (b[4] > '9') || (b[5] < '0') || (b[5] > '9') || (b[6] <
128 '0') || (b[6] > '9') || (b[7] != '.') || (b[8] < '0') || (b[8] > '9') ||
129 (b[9] < '0') || (b[9] > '9') || (b[10] < '0') || (b[10] > '9') ||
130 (b[11] != '\n')) {
Constantin Kaplinsky903009e2002-05-20 10:55:47 +0000131 throw new IOException("Incorrect protocol version");
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000132 }
133
134 serverMajor = (b[4] - '0') * 100 + (b[5] - '0') * 10 + (b[6] - '0');
135 serverMinor = (b[8] - '0') * 100 + (b[9] - '0') * 10 + (b[10] - '0');
136 }
137
138
139 //
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000140 // Find out the authentication scheme.
141 //
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000142 int readAuthScheme() throws IOException {
143 int authScheme = is.readInt();
144
145 switch (authScheme) {
146
147 case ConnFailed:
148 int reasonLen = is.readInt();
149 byte[] reason = new byte[reasonLen];
150 is.readFully(reason);
151 throw new IOException(new String(reason));
152
153 case NoAuth:
154 case VncAuth:
155 return authScheme;
156
157 default:
Constantin Kaplinsky903009e2002-05-20 10:55:47 +0000158 throw new IOException("Unknown authentication scheme " + authScheme);
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000159
160 }
161 }
162
163
164 //
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000165 // Read the server initialisation message
166 //
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000167 String desktopName;
168 int framebufferWidth, framebufferHeight;
169 int bitsPerPixel, depth;
170 boolean bigEndian, trueColour;
171 int redMax, greenMax, blueMax, redShift, greenShift, blueShift;
172
Constantin Kaplinskyf392f442002-05-20 13:05:42 +0000173 void readServerInit() throws Exception {
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000174 framebufferWidth = is.readUnsignedShort();
175 framebufferHeight = is.readUnsignedShort();
176 bitsPerPixel = is.readUnsignedByte();
177 depth = is.readUnsignedByte();
178 bigEndian = (is.readUnsignedByte() != 0);
179 trueColour = (is.readUnsignedByte() != 0);
180 redMax = is.readUnsignedShort();
181 greenMax = is.readUnsignedShort();
182 blueMax = is.readUnsignedShort();
183 redShift = is.readUnsignedByte();
184 greenShift = is.readUnsignedByte();
185 blueShift = is.readUnsignedByte();
186 byte[] pad = new byte[3];
187 is.readFully(pad);
188 int nameLength = is.readInt();
189 byte[] name = new byte[nameLength];
190 is.readFully(name);
191 desktopName = new String(name);
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000192 }
193
194
195 //
196 // Set new framebuffer size
197 //
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000198 void setFramebufferSize(int width, int height) {
199 framebufferWidth = width;
200 framebufferHeight = height;
201 }
202
203
204 //
205 // Read the server message type
206 //
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000207 int readServerMessageType() throws IOException {
208 return is.readUnsignedByte();
209 }
210
211
212 //
213 // Read a FramebufferUpdate message
214 //
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000215 int updateNRects;
216
217 void readFramebufferUpdate() throws IOException {
218 is.readByte();
219 updateNRects = is.readUnsignedShort();
220 }
221
222 // Read a FramebufferUpdate rectangle header
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000223 int updateRectX, updateRectY, updateRectW, updateRectH, updateRectEncoding;
224
225 void readFramebufferUpdateRectHdr() throws IOException {
226 updateRectX = is.readUnsignedShort();
227 updateRectY = is.readUnsignedShort();
228 updateRectW = is.readUnsignedShort();
229 updateRectH = is.readUnsignedShort();
230 updateRectEncoding = is.readInt();
231
wimba.com93cc0db2004-10-13 16:19:45 +0000232 if (updateRectEncoding < 0 || updateRectEncoding > MaxNormalEncoding)
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000233 return;
234
235 if ((updateRectX + updateRectW > framebufferWidth) ||
Constantin Kaplinsky72e47ef2008-04-18 17:48:16 +0000236 (updateRectY + updateRectH > framebufferHeight)) {
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000237 throw new IOException("Framebuffer update rectangle too large: " +
Constantin Kaplinsky72e47ef2008-04-18 17:48:16 +0000238 updateRectW + "x" + updateRectH + " at (" +
239 updateRectX + "," + updateRectY + ")");
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000240 }
241 }
242
243 // Read CopyRect source X and Y.
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000244 int copyRectSrcX, copyRectSrcY;
245
246 void readCopyRect() throws IOException {
247 copyRectSrcX = is.readUnsignedShort();
248 copyRectSrcY = is.readUnsignedShort();
249 }
250
251
252 //
253 // Read a ServerCutText message
254 //
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000255 String readServerCutText() throws IOException {
256 byte[] pad = new byte[3];
257 is.readFully(pad);
258 int len = is.readInt();
259 byte[] text = new byte[len];
260 is.readFully(text);
261 return new String(text);
262 }
263
264
265 //
266 // Read integer in compact representation
267 //
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000268 int readCompactLen() throws IOException {
269 int portion = is.readUnsignedByte();
270 int len = portion & 0x7F;
271 if ((portion & 0x80) != 0) {
272 portion = is.readUnsignedByte();
273 len |= (portion & 0x7F) << 7;
274 if ((portion & 0x80) != 0) {
Constantin Kaplinsky72e47ef2008-04-18 17:48:16 +0000275 portion = is.readUnsignedByte();
276 len |= (portion & 0xFF) << 14;
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000277 }
278 }
279 return len;
280 }
281
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000282}