blob: 2faf66c0e8967f324d6893daf7c85477142b4f86 [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
27import java.io.*;
28import java.awt.*;
29import java.awt.event.*;
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";
35 final static int ConnFailed = 0, NoAuth = 1, VncAuth = 2;
36 final static int VncAuthOK = 0, VncAuthFailed = 1, VncAuthTooMany = 2;
37
38 final static int FramebufferUpdate = 0, SetColourMapEntries = 1, Bell = 2,
39 ServerCutText = 3;
40
41 final int SetPixelFormat = 0, FixColourMapEntries = 1, SetEncodings = 2,
42 FramebufferUpdateRequest = 3, KeyboardEvent = 4, PointerEvent = 5,
43 ClientCutText = 6;
44
45 final static int
46 EncodingRaw = 0,
47 EncodingCopyRect = 1,
48 EncodingRRE = 2,
49 EncodingCoRRE = 4,
50 EncodingHextile = 5,
51 EncodingZlib = 6,
52 EncodingTight = 7,
53 EncodingCompressLevel0 = 0xFFFFFF00,
54 EncodingQualityLevel0 = 0xFFFFFFE0,
55 EncodingXCursor = 0xFFFFFF10,
56 EncodingRichCursor = 0xFFFFFF11,
57 EncodingLastRect = 0xFFFFFF20,
58 EncodingNewFBSize = 0xFFFFFF21;
59
60 final int HextileRaw = (1 << 0);
61 final int HextileBackgroundSpecified = (1 << 1);
62 final int HextileForegroundSpecified = (1 << 2);
63 final int HextileAnySubrects = (1 << 3);
64 final int HextileSubrectsColoured = (1 << 4);
65
66 final static int TightExplicitFilter = 0x04;
67 final static int TightFill = 0x08;
68 final static int TightJpeg = 0x09;
69 final static int TightMaxSubencoding = 0x09;
70 final static int TightFilterCopy = 0x00;
71 final static int TightFilterPalette = 0x01;
72 final static int TightFilterGradient = 0x02;
73
74 final static int TightMinToCompress = 12;
75
Constantin Kaplinsky37cc43e2002-05-30 17:30:11 +000076 FbsInputStream fbs;
Constantin Kaplinsky1215b992008-04-18 09:51:44 +000077 DataInputStream is;
Constantin Kaplinsky1215b992008-04-18 09:51:44 +000078
79
80 //
Constantin Kaplinsky903009e2002-05-20 10:55:47 +000081 // Constructor.
Constantin Kaplinsky1215b992008-04-18 09:51:44 +000082 //
83
Constantin Kaplinsky37cc43e2002-05-30 17:30:11 +000084 RfbProto(URL url) throws Exception {
85 fbs = null;
86 newSession(url);
Constantin Kaplinsky1215b992008-04-18 09:51:44 +000087 }
88
Constantin Kaplinsky37cc43e2002-05-30 17:30:11 +000089 public void newSession(URL url) throws Exception {
90 if (fbs != null)
91 fbs.close();
92 fbs = new FbsInputStream(url.openStream());
93 is = new DataInputStream(fbs);
Constantin Kaplinsky1215b992008-04-18 09:51:44 +000094
Constantin Kaplinsky903009e2002-05-20 10:55:47 +000095 readVersionMsg();
96 if (readAuthScheme() != NoAuth) {
Constantin Kaplinskyf392f442002-05-20 13:05:42 +000097 throw new Exception("Wrong authentication type in the session file");
Constantin Kaplinsky1215b992008-04-18 09:51:44 +000098 }
Constantin Kaplinsky903009e2002-05-20 10:55:47 +000099 readServerInit();
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000100 }
101
102 //
103 // Read server's protocol version message
104 //
105
106 int serverMajor, serverMinor;
107
108 void readVersionMsg() throws IOException {
109
110 byte[] b = new byte[12];
111
112 is.readFully(b);
113
114 if ((b[0] != 'R') || (b[1] != 'F') || (b[2] != 'B') || (b[3] != ' ')
115 || (b[4] < '0') || (b[4] > '9') || (b[5] < '0') || (b[5] > '9')
116 || (b[6] < '0') || (b[6] > '9') || (b[7] != '.')
117 || (b[8] < '0') || (b[8] > '9') || (b[9] < '0') || (b[9] > '9')
118 || (b[10] < '0') || (b[10] > '9') || (b[11] != '\n'))
119 {
Constantin Kaplinsky903009e2002-05-20 10:55:47 +0000120 throw new IOException("Incorrect protocol version");
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000121 }
122
123 serverMajor = (b[4] - '0') * 100 + (b[5] - '0') * 10 + (b[6] - '0');
124 serverMinor = (b[8] - '0') * 100 + (b[9] - '0') * 10 + (b[10] - '0');
125 }
126
127
128 //
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000129 // Find out the authentication scheme.
130 //
131
132 int readAuthScheme() throws IOException {
133 int authScheme = is.readInt();
134
135 switch (authScheme) {
136
137 case ConnFailed:
138 int reasonLen = is.readInt();
139 byte[] reason = new byte[reasonLen];
140 is.readFully(reason);
141 throw new IOException(new String(reason));
142
143 case NoAuth:
144 case VncAuth:
145 return authScheme;
146
147 default:
Constantin Kaplinsky903009e2002-05-20 10:55:47 +0000148 throw new IOException("Unknown authentication scheme " + authScheme);
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000149
150 }
151 }
152
153
154 //
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000155 // Read the server initialisation message
156 //
157
158 String desktopName;
159 int framebufferWidth, framebufferHeight;
160 int bitsPerPixel, depth;
161 boolean bigEndian, trueColour;
162 int redMax, greenMax, blueMax, redShift, greenShift, blueShift;
163
Constantin Kaplinskyf392f442002-05-20 13:05:42 +0000164 void readServerInit() throws Exception {
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000165 framebufferWidth = is.readUnsignedShort();
166 framebufferHeight = is.readUnsignedShort();
167 bitsPerPixel = is.readUnsignedByte();
168 depth = is.readUnsignedByte();
169 bigEndian = (is.readUnsignedByte() != 0);
170 trueColour = (is.readUnsignedByte() != 0);
171 redMax = is.readUnsignedShort();
172 greenMax = is.readUnsignedShort();
173 blueMax = is.readUnsignedShort();
174 redShift = is.readUnsignedByte();
175 greenShift = is.readUnsignedByte();
176 blueShift = is.readUnsignedByte();
177 byte[] pad = new byte[3];
178 is.readFully(pad);
179 int nameLength = is.readInt();
180 byte[] name = new byte[nameLength];
181 is.readFully(name);
182 desktopName = new String(name);
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000183 }
184
185
186 //
187 // Set new framebuffer size
188 //
189
190 void setFramebufferSize(int width, int height) {
191 framebufferWidth = width;
192 framebufferHeight = height;
193 }
194
195
196 //
197 // Read the server message type
198 //
199
200 int readServerMessageType() throws IOException {
201 return is.readUnsignedByte();
202 }
203
204
205 //
206 // Read a FramebufferUpdate message
207 //
208
209 int updateNRects;
210
211 void readFramebufferUpdate() throws IOException {
212 is.readByte();
213 updateNRects = is.readUnsignedShort();
214 }
215
216 // Read a FramebufferUpdate rectangle header
217
218 int updateRectX, updateRectY, updateRectW, updateRectH, updateRectEncoding;
219
220 void readFramebufferUpdateRectHdr() throws IOException {
221 updateRectX = is.readUnsignedShort();
222 updateRectY = is.readUnsignedShort();
223 updateRectW = is.readUnsignedShort();
224 updateRectH = is.readUnsignedShort();
225 updateRectEncoding = is.readInt();
226
227 if ((updateRectEncoding == EncodingLastRect) ||
228 (updateRectEncoding == EncodingNewFBSize))
229 return;
230
231 if ((updateRectX + updateRectW > framebufferWidth) ||
232 (updateRectY + updateRectH > framebufferHeight)) {
233 throw new IOException("Framebuffer update rectangle too large: " +
234 updateRectW + "x" + updateRectH + " at (" +
235 updateRectX + "," + updateRectY + ")");
236 }
237 }
238
239 // Read CopyRect source X and Y.
240
241 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 //
252
253 String readServerCutText() throws IOException {
254 byte[] pad = new byte[3];
255 is.readFully(pad);
256 int len = is.readInt();
257 byte[] text = new byte[len];
258 is.readFully(text);
259 return new String(text);
260 }
261
262
263 //
264 // Read integer in compact representation
265 //
266
267 int readCompactLen() throws IOException {
268 int portion = is.readUnsignedByte();
269 int len = portion & 0x7F;
270 if ((portion & 0x80) != 0) {
271 portion = is.readUnsignedByte();
272 len |= (portion & 0x7F) << 7;
273 if ((portion & 0x80) != 0) {
274 portion = is.readUnsignedByte();
275 len |= (portion & 0xFF) << 14;
276 }
277 }
278 return len;
279 }
280
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000281}