blob: 092597525067dc1ddc197b1b6be763fbbe35be17 [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
98 // open the connection, making sure that it does not use
99 // a cached version of the archive
100 URLConnection connection = url.openConnection();
101 connection.setUseCaches(false);
102
103 fbs = new FbsInputStream(connection.getInputStream());
Constantin Kaplinsky37cc43e2002-05-30 17:30:11 +0000104 is = new DataInputStream(fbs);
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000105
Constantin Kaplinsky903009e2002-05-20 10:55:47 +0000106 readVersionMsg();
107 if (readAuthScheme() != NoAuth) {
Constantin Kaplinskyf392f442002-05-20 13:05:42 +0000108 throw new Exception("Wrong authentication type in the session file");
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000109 }
Constantin Kaplinsky903009e2002-05-20 10:55:47 +0000110 readServerInit();
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000111 }
112
113 //
Constantin Kaplinskyc833e012002-05-30 17:59:22 +0000114 // Read server's protocol version message.
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000115 //
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000116 int serverMajor, serverMinor;
117
118 void readVersionMsg() throws IOException {
119
120 byte[] b = new byte[12];
121
wimba.comc23aeb02004-09-16 00:00:00 +0000122 for (int i = 0; i < b.length; i++)
123 b[i] = (byte)'0';
124
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000125 is.readFully(b);
126
Constantin Kaplinsky72e47ef2008-04-18 17:48:16 +0000127 if ((b[0] != 'R') || (b[1] != 'F') || (b[2] != 'B') || (b[3] != ' ') ||
128 (b[4] < '0') || (b[4] > '9') || (b[5] < '0') || (b[5] > '9') || (b[6] <
129 '0') || (b[6] > '9') || (b[7] != '.') || (b[8] < '0') || (b[8] > '9') ||
130 (b[9] < '0') || (b[9] > '9') || (b[10] < '0') || (b[10] > '9') ||
131 (b[11] != '\n')) {
Constantin Kaplinsky903009e2002-05-20 10:55:47 +0000132 throw new IOException("Incorrect protocol version");
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000133 }
134
135 serverMajor = (b[4] - '0') * 100 + (b[5] - '0') * 10 + (b[6] - '0');
136 serverMinor = (b[8] - '0') * 100 + (b[9] - '0') * 10 + (b[10] - '0');
137 }
138
139
140 //
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000141 // Find out the authentication scheme.
142 //
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000143 int readAuthScheme() throws IOException {
144 int authScheme = is.readInt();
145
146 switch (authScheme) {
147
148 case ConnFailed:
149 int reasonLen = is.readInt();
150 byte[] reason = new byte[reasonLen];
151 is.readFully(reason);
152 throw new IOException(new String(reason));
153
154 case NoAuth:
155 case VncAuth:
156 return authScheme;
157
158 default:
Constantin Kaplinsky903009e2002-05-20 10:55:47 +0000159 throw new IOException("Unknown authentication scheme " + authScheme);
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000160
161 }
162 }
163
164
165 //
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000166 // Read the server initialisation message
167 //
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000168 String desktopName;
169 int framebufferWidth, framebufferHeight;
170 int bitsPerPixel, depth;
171 boolean bigEndian, trueColour;
172 int redMax, greenMax, blueMax, redShift, greenShift, blueShift;
173
Constantin Kaplinskyf392f442002-05-20 13:05:42 +0000174 void readServerInit() throws Exception {
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000175 framebufferWidth = is.readUnsignedShort();
176 framebufferHeight = is.readUnsignedShort();
177 bitsPerPixel = is.readUnsignedByte();
178 depth = is.readUnsignedByte();
179 bigEndian = (is.readUnsignedByte() != 0);
180 trueColour = (is.readUnsignedByte() != 0);
181 redMax = is.readUnsignedShort();
182 greenMax = is.readUnsignedShort();
183 blueMax = is.readUnsignedShort();
184 redShift = is.readUnsignedByte();
185 greenShift = is.readUnsignedByte();
186 blueShift = is.readUnsignedByte();
187 byte[] pad = new byte[3];
188 is.readFully(pad);
189 int nameLength = is.readInt();
190 byte[] name = new byte[nameLength];
191 is.readFully(name);
192 desktopName = new String(name);
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000193 }
194
195
196 //
197 // Set new framebuffer size
198 //
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000199 void setFramebufferSize(int width, int height) {
200 framebufferWidth = width;
201 framebufferHeight = height;
202 }
203
204
205 //
206 // Read the server message type
207 //
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000208 int readServerMessageType() throws IOException {
209 return is.readUnsignedByte();
210 }
211
212
213 //
214 // Read a FramebufferUpdate message
215 //
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000216 int updateNRects;
217
218 void readFramebufferUpdate() throws IOException {
219 is.readByte();
220 updateNRects = is.readUnsignedShort();
221 }
222
223 // Read a FramebufferUpdate rectangle header
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000224 int updateRectX, updateRectY, updateRectW, updateRectH, updateRectEncoding;
225
226 void readFramebufferUpdateRectHdr() throws IOException {
227 updateRectX = is.readUnsignedShort();
228 updateRectY = is.readUnsignedShort();
229 updateRectW = is.readUnsignedShort();
230 updateRectH = is.readUnsignedShort();
231 updateRectEncoding = is.readInt();
232
wimba.com93cc0db2004-10-13 16:19:45 +0000233 if (updateRectEncoding < 0 || updateRectEncoding > MaxNormalEncoding)
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000234 return;
235
236 if ((updateRectX + updateRectW > framebufferWidth) ||
Constantin Kaplinsky72e47ef2008-04-18 17:48:16 +0000237 (updateRectY + updateRectH > framebufferHeight)) {
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000238 throw new IOException("Framebuffer update rectangle too large: " +
Constantin Kaplinsky72e47ef2008-04-18 17:48:16 +0000239 updateRectW + "x" + updateRectH + " at (" +
240 updateRectX + "," + updateRectY + ")");
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000241 }
242 }
243
244 // Read CopyRect source X and Y.
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000245 int copyRectSrcX, copyRectSrcY;
246
247 void readCopyRect() throws IOException {
248 copyRectSrcX = is.readUnsignedShort();
249 copyRectSrcY = is.readUnsignedShort();
250 }
251
252
253 //
254 // Read a ServerCutText message
255 //
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000256 String readServerCutText() throws IOException {
257 byte[] pad = new byte[3];
258 is.readFully(pad);
259 int len = is.readInt();
260 byte[] text = new byte[len];
261 is.readFully(text);
262 return new String(text);
263 }
264
265
266 //
267 // Read integer in compact representation
268 //
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000269 int readCompactLen() throws IOException {
270 int portion = is.readUnsignedByte();
271 int len = portion & 0x7F;
272 if ((portion & 0x80) != 0) {
273 portion = is.readUnsignedByte();
274 len |= (portion & 0x7F) << 7;
275 if ((portion & 0x80) != 0) {
Constantin Kaplinsky72e47ef2008-04-18 17:48:16 +0000276 portion = is.readUnsignedByte();
277 len |= (portion & 0xFF) << 14;
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000278 }
279 }
280 return len;
281 }
282
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000283}