blob: 8f341df5a3ec49393996c4f948cd452c9ca05d13 [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 =
49 0xFFFFFF10, EncodingRichCursor = 0xFFFFFF11, EncodingLastRect =
50 0xFFFFFF20, EncodingNewFBSize = 0xFFFFFF21;
Constantin Kaplinsky1215b992008-04-18 09:51:44 +000051
Constantin Kaplinsky72e47ef2008-04-18 17:48:16 +000052 final int HextileRaw = (1 << 0);
53 final int HextileBackgroundSpecified = (1 << 1);
54 final int HextileForegroundSpecified = (1 << 2);
55 final int HextileAnySubrects = (1 << 3);
56 final int HextileSubrectsColoured = (1 << 4);
Constantin Kaplinsky1215b992008-04-18 09:51:44 +000057
Constantin Kaplinsky72e47ef2008-04-18 17:48:16 +000058 final static int TightExplicitFilter = 0x04;
59 final static int TightFill = 0x08;
60 final static int TightJpeg = 0x09;
61 final static int TightMaxSubencoding = 0x09;
62 final static int TightFilterCopy = 0x00;
63 final static int TightFilterPalette = 0x01;
64 final static int TightFilterGradient = 0x02;
Constantin Kaplinsky1215b992008-04-18 09:51:44 +000065
Constantin Kaplinsky72e47ef2008-04-18 17:48:16 +000066 final static int TightMinToCompress = 12;
Constantin Kaplinsky1215b992008-04-18 09:51:44 +000067
Constantin Kaplinsky37cc43e2002-05-30 17:30:11 +000068 FbsInputStream fbs;
Constantin Kaplinsky1215b992008-04-18 09:51:44 +000069 DataInputStream is;
Constantin Kaplinsky1215b992008-04-18 09:51:44 +000070
71
72 //
Constantin Kaplinsky903009e2002-05-20 10:55:47 +000073 // Constructor.
Constantin Kaplinsky1215b992008-04-18 09:51:44 +000074 //
Constantin Kaplinsky37cc43e2002-05-30 17:30:11 +000075 RfbProto(URL url) throws Exception {
76 fbs = null;
77 newSession(url);
Constantin Kaplinsky1215b992008-04-18 09:51:44 +000078 }
79
Constantin Kaplinskyc833e012002-05-30 17:59:22 +000080 //
81 // Open new session URL.
82 //
Constantin Kaplinsky37cc43e2002-05-30 17:30:11 +000083 public void newSession(URL url) throws Exception {
84 if (fbs != null)
85 fbs.close();
86 fbs = new FbsInputStream(url.openStream());
87 is = new DataInputStream(fbs);
Constantin Kaplinsky1215b992008-04-18 09:51:44 +000088
Constantin Kaplinsky903009e2002-05-20 10:55:47 +000089 readVersionMsg();
90 if (readAuthScheme() != NoAuth) {
Constantin Kaplinskyf392f442002-05-20 13:05:42 +000091 throw new Exception("Wrong authentication type in the session file");
Constantin Kaplinsky1215b992008-04-18 09:51:44 +000092 }
Constantin Kaplinsky903009e2002-05-20 10:55:47 +000093 readServerInit();
Constantin Kaplinsky1215b992008-04-18 09:51:44 +000094 }
95
96 //
Constantin Kaplinskyc833e012002-05-30 17:59:22 +000097 // Read server's protocol version message.
Constantin Kaplinsky1215b992008-04-18 09:51:44 +000098 //
Constantin Kaplinsky1215b992008-04-18 09:51:44 +000099 int serverMajor, serverMinor;
100
101 void readVersionMsg() throws IOException {
102
103 byte[] b = new byte[12];
104
wimba.comc23aeb02004-09-16 00:00:00 +0000105 for (int i = 0; i < b.length; i++)
106 b[i] = (byte)'0';
107
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000108 is.readFully(b);
109
Constantin Kaplinsky72e47ef2008-04-18 17:48:16 +0000110 if ((b[0] != 'R') || (b[1] != 'F') || (b[2] != 'B') || (b[3] != ' ') ||
111 (b[4] < '0') || (b[4] > '9') || (b[5] < '0') || (b[5] > '9') || (b[6] <
112 '0') || (b[6] > '9') || (b[7] != '.') || (b[8] < '0') || (b[8] > '9') ||
113 (b[9] < '0') || (b[9] > '9') || (b[10] < '0') || (b[10] > '9') ||
114 (b[11] != '\n')) {
Constantin Kaplinsky903009e2002-05-20 10:55:47 +0000115 throw new IOException("Incorrect protocol version");
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000116 }
117
118 serverMajor = (b[4] - '0') * 100 + (b[5] - '0') * 10 + (b[6] - '0');
119 serverMinor = (b[8] - '0') * 100 + (b[9] - '0') * 10 + (b[10] - '0');
120 }
121
122
123 //
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000124 // Find out the authentication scheme.
125 //
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000126 int readAuthScheme() throws IOException {
127 int authScheme = is.readInt();
128
129 switch (authScheme) {
130
131 case ConnFailed:
132 int reasonLen = is.readInt();
133 byte[] reason = new byte[reasonLen];
134 is.readFully(reason);
135 throw new IOException(new String(reason));
136
137 case NoAuth:
138 case VncAuth:
139 return authScheme;
140
141 default:
Constantin Kaplinsky903009e2002-05-20 10:55:47 +0000142 throw new IOException("Unknown authentication scheme " + authScheme);
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000143
144 }
145 }
146
147
148 //
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000149 // Read the server initialisation message
150 //
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000151 String desktopName;
152 int framebufferWidth, framebufferHeight;
153 int bitsPerPixel, depth;
154 boolean bigEndian, trueColour;
155 int redMax, greenMax, blueMax, redShift, greenShift, blueShift;
156
Constantin Kaplinskyf392f442002-05-20 13:05:42 +0000157 void readServerInit() throws Exception {
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000158 framebufferWidth = is.readUnsignedShort();
159 framebufferHeight = is.readUnsignedShort();
160 bitsPerPixel = is.readUnsignedByte();
161 depth = is.readUnsignedByte();
162 bigEndian = (is.readUnsignedByte() != 0);
163 trueColour = (is.readUnsignedByte() != 0);
164 redMax = is.readUnsignedShort();
165 greenMax = is.readUnsignedShort();
166 blueMax = is.readUnsignedShort();
167 redShift = is.readUnsignedByte();
168 greenShift = is.readUnsignedByte();
169 blueShift = is.readUnsignedByte();
170 byte[] pad = new byte[3];
171 is.readFully(pad);
172 int nameLength = is.readInt();
173 byte[] name = new byte[nameLength];
174 is.readFully(name);
175 desktopName = new String(name);
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000176 }
177
178
179 //
180 // Set new framebuffer size
181 //
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000182 void setFramebufferSize(int width, int height) {
183 framebufferWidth = width;
184 framebufferHeight = height;
185 }
186
187
188 //
189 // Read the server message type
190 //
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000191 int readServerMessageType() throws IOException {
192 return is.readUnsignedByte();
193 }
194
195
196 //
197 // Read a FramebufferUpdate message
198 //
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000199 int updateNRects;
200
201 void readFramebufferUpdate() throws IOException {
202 is.readByte();
203 updateNRects = is.readUnsignedShort();
204 }
205
206 // Read a FramebufferUpdate rectangle header
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000207 int updateRectX, updateRectY, updateRectW, updateRectH, updateRectEncoding;
208
209 void readFramebufferUpdateRectHdr() throws IOException {
210 updateRectX = is.readUnsignedShort();
211 updateRectY = is.readUnsignedShort();
212 updateRectW = is.readUnsignedShort();
213 updateRectH = is.readUnsignedShort();
214 updateRectEncoding = is.readInt();
215
216 if ((updateRectEncoding == EncodingLastRect) ||
Constantin Kaplinsky72e47ef2008-04-18 17:48:16 +0000217 (updateRectEncoding == EncodingNewFBSize))
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000218 return;
219
220 if ((updateRectX + updateRectW > framebufferWidth) ||
Constantin Kaplinsky72e47ef2008-04-18 17:48:16 +0000221 (updateRectY + updateRectH > framebufferHeight)) {
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000222 throw new IOException("Framebuffer update rectangle too large: " +
Constantin Kaplinsky72e47ef2008-04-18 17:48:16 +0000223 updateRectW + "x" + updateRectH + " at (" +
224 updateRectX + "," + updateRectY + ")");
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000225 }
226 }
227
228 // Read CopyRect source X and Y.
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000229 int copyRectSrcX, copyRectSrcY;
230
231 void readCopyRect() throws IOException {
232 copyRectSrcX = is.readUnsignedShort();
233 copyRectSrcY = is.readUnsignedShort();
234 }
235
236
237 //
238 // Read a ServerCutText message
239 //
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000240 String readServerCutText() throws IOException {
241 byte[] pad = new byte[3];
242 is.readFully(pad);
243 int len = is.readInt();
244 byte[] text = new byte[len];
245 is.readFully(text);
246 return new String(text);
247 }
248
249
250 //
251 // Read integer in compact representation
252 //
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000253 int readCompactLen() throws IOException {
254 int portion = is.readUnsignedByte();
255 int len = portion & 0x7F;
256 if ((portion & 0x80) != 0) {
257 portion = is.readUnsignedByte();
258 len |= (portion & 0x7F) << 7;
259 if ((portion & 0x80) != 0) {
Constantin Kaplinsky72e47ef2008-04-18 17:48:16 +0000260 portion = is.readUnsignedByte();
261 len |= (portion & 0xFF) << 14;
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000262 }
263 }
264 return len;
265 }
266
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000267}