24
|
1 package myVncProxy;
|
0
|
2 //
|
|
3 // Copyright (C) 2001-2004 HorizonLive.com, Inc. All Rights Reserved.
|
|
4 // Copyright (C) 2001-2006 Constantin Kaplinsky. All Rights Reserved.
|
|
5 // Copyright (C) 2000 Tridia Corporation. All Rights Reserved.
|
|
6 // Copyright (C) 1999 AT&T Laboratories Cambridge. All Rights Reserved.
|
|
7 //
|
|
8 // This is free software; you can redistribute it and/or modify
|
|
9 // it under the terms of the GNU General Public License as published by
|
|
10 // the Free Software Foundation; either version 2 of the License, or
|
|
11 // (at your option) any later version.
|
|
12 //
|
|
13 // This software is distributed in the hope that it will be useful,
|
|
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
16 // GNU General Public License for more details.
|
|
17 //
|
|
18 // You should have received a copy of the GNU General Public License
|
|
19 // along with this software; if not, write to the Free Software
|
|
20 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
|
|
21 // USA.
|
|
22 //
|
|
23
|
|
24 //
|
|
25 // RfbProto.java
|
|
26 //
|
|
27
|
|
28 import java.io.*;
|
|
29 import java.awt.event.*;
|
|
30 import java.net.Socket;
|
|
31 import java.util.zip.*;
|
|
32
|
|
33 class RfbProto {
|
|
34
|
4
|
35 final static String versionMsg_3_3 = "RFB 003.003\n",
|
|
36 versionMsg_3_7 = "RFB 003.007\n", versionMsg_3_8 = "RFB 003.008\n";
|
|
37
|
43
|
38
|
4
|
39 // Vendor signatures: standard VNC/RealVNC, TridiaVNC, and TightVNC
|
|
40 final static String StandardVendor = "STDV", TridiaVncVendor = "TRDV",
|
|
41 TightVncVendor = "TGHT";
|
0
|
42
|
4
|
43 // Security types
|
|
44 final static int SecTypeInvalid = 0, SecTypeNone = 1, SecTypeVncAuth = 2,
|
|
45 SecTypeTight = 16;
|
|
46
|
|
47 // Supported tunneling types
|
|
48 final static int NoTunneling = 0;
|
|
49 final static String SigNoTunneling = "NOTUNNEL";
|
0
|
50
|
4
|
51 // Supported authentication types
|
|
52 final static int AuthNone = 1, AuthVNC = 2, AuthUnixLogin = 129;
|
|
53 final static String SigAuthNone = "NOAUTH__", SigAuthVNC = "VNCAUTH_",
|
|
54 SigAuthUnixLogin = "ULGNAUTH";
|
|
55
|
|
56 // VNC authentication results
|
|
57 final static int VncAuthOK = 0, VncAuthFailed = 1, VncAuthTooMany = 2;
|
0
|
58
|
4
|
59 // Standard server-to-client messages
|
|
60 final static int FramebufferUpdate = 0, SetColourMapEntries = 1, Bell = 2,
|
|
61 ServerCutText = 3;
|
0
|
62
|
4
|
63 // Non-standard server-to-client messages
|
|
64 final static int EndOfContinuousUpdates = 150;
|
|
65 final static String SigEndOfContinuousUpdates = "CUS_EOCU";
|
|
66
|
|
67 // Standard client-to-server messages
|
|
68 final static int SetPixelFormat = 0, FixColourMapEntries = 1,
|
|
69 SetEncodings = 2, FramebufferUpdateRequest = 3, KeyboardEvent = 4,
|
|
70 PointerEvent = 5, ClientCutText = 6;
|
0
|
71
|
4
|
72 // Non-standard client-to-server messages
|
|
73 final static int EnableContinuousUpdates = 150;
|
|
74 final static String SigEnableContinuousUpdates = "CUC_ENCU";
|
0
|
75
|
4
|
76 // Supported encodings and pseudo-encodings
|
|
77 final static int EncodingRaw = 0, EncodingCopyRect = 1, EncodingRRE = 2,
|
71
|
78 EncodingCoRRE = 4, EncodingHextile = 5, EncodingZlib = 6,
|
107
|
79 EncodingTight = 7, EncodingZRLEE = 15, EncodingZRLE = 16,
|
4
|
80 EncodingCompressLevel0 = 0xFFFFFF00,
|
|
81 EncodingQualityLevel0 = 0xFFFFFFE0, EncodingXCursor = 0xFFFFFF10,
|
|
82 EncodingRichCursor = 0xFFFFFF11, EncodingPointerPos = 0xFFFFFF18,
|
|
83 EncodingLastRect = 0xFFFFFF20, EncodingNewFBSize = 0xFFFFFF21;
|
|
84 final static String SigEncodingRaw = "RAW_____",
|
|
85 SigEncodingCopyRect = "COPYRECT", SigEncodingRRE = "RRE_____",
|
|
86 SigEncodingCoRRE = "CORRE___", SigEncodingHextile = "HEXTILE_",
|
|
87 SigEncodingZlib = "ZLIB____", SigEncodingTight = "TIGHT___",
|
107
|
88 SigEncodingZRLEE = "ZRLEE___",
|
4
|
89 SigEncodingZRLE = "ZRLE____",
|
|
90 SigEncodingCompressLevel0 = "COMPRLVL",
|
|
91 SigEncodingQualityLevel0 = "JPEGQLVL",
|
|
92 SigEncodingXCursor = "X11CURSR",
|
|
93 SigEncodingRichCursor = "RCHCURSR",
|
|
94 SigEncodingPointerPos = "POINTPOS",
|
|
95 SigEncodingLastRect = "LASTRECT",
|
|
96 SigEncodingNewFBSize = "NEWFBSIZ";
|
0
|
97
|
4
|
98 final static int MaxNormalEncoding = 255;
|
|
99
|
|
100 // Contstants used in the Hextile decoder
|
|
101 final static int HextileRaw = 1, HextileBackgroundSpecified = 2,
|
|
102 HextileForegroundSpecified = 4, HextileAnySubrects = 8,
|
|
103 HextileSubrectsColoured = 16;
|
0
|
104
|
4
|
105 // Contstants used in the Tight decoder
|
|
106 final static int TightMinToCompress = 12;
|
|
107 final static int TightExplicitFilter = 0x04, TightFill = 0x08,
|
|
108 TightJpeg = 0x09, TightMaxSubencoding = 0x09,
|
|
109 TightFilterCopy = 0x00, TightFilterPalette = 0x01,
|
|
110 TightFilterGradient = 0x02;
|
0
|
111
|
4
|
112 String host;
|
|
113 int port;
|
|
114 Socket sock;
|
|
115 OutputStream os;
|
|
116 SessionRecorder rec;
|
|
117 boolean inNormalProtocol = false;
|
|
118 VncViewer viewer;
|
|
119
|
|
120 // Input stream is declared private to make sure it can be accessed
|
|
121 // only via RfbProto methods. We have to do this because we want to
|
|
122 // count how many bytes were read.
|
7
|
123 // private DataInputStream is;
|
|
124 protected DataInputStream is;
|
|
125 // private long numBytesRead = 0;
|
|
126 protected long numBytesRead = 0;
|
4
|
127
|
|
128 public long getNumBytesRead() {
|
|
129 return numBytesRead;
|
|
130 }
|
0
|
131
|
|
132
|
4
|
133 // Java on UNIX does not call keyPressed() on some keys, for example
|
|
134 // swedish keys To prevent our workaround to produce duplicate
|
|
135 // keypresses on JVMs that actually works, keep track of if
|
|
136 // keyPressed() for a "broken" key was called or not.
|
|
137 boolean brokenKeyPressed = false;
|
|
138
|
|
139 // This will be set to true on the first framebuffer update
|
|
140 // containing Zlib-, ZRLE- or Tight-encoded data.
|
|
141 boolean wereZlibUpdates = false;
|
|
142
|
|
143 // This will be set to false if the startSession() was called after
|
|
144 // we have received at least one Zlib-, ZRLE- or Tight-encoded
|
|
145 // framebuffer update.
|
|
146 boolean recordFromBeginning = true;
|
0
|
147
|
4
|
148 // This fields are needed to show warnings about inefficiently saved
|
|
149 // sessions only once per each saved session file.
|
|
150 boolean zlibWarningShown;
|
|
151 boolean tightWarningShown;
|
0
|
152
|
4
|
153 // Before starting to record each saved session, we set this field
|
|
154 // to 0, and increment on each framebuffer update. We don't flush
|
|
155 // the SessionRecorder data into the file before the second update.
|
|
156 // This allows us to write initial framebuffer update with zero
|
|
157 // timestamp, to let the player show initial desktop before
|
|
158 // playback.
|
|
159 int numUpdatesInSession;
|
0
|
160
|
4
|
161 // Measuring network throughput.
|
|
162 boolean timing;
|
|
163 long timeWaitedIn100us;
|
|
164 long timedKbits;
|
0
|
165
|
4
|
166 // Protocol version and TightVNC-specific protocol options.
|
|
167 int serverMajor, serverMinor;
|
|
168 int clientMajor, clientMinor;
|
|
169 boolean protocolTightVNC;
|
|
170 CapsContainer tunnelCaps, authCaps;
|
|
171 CapsContainer serverMsgCaps, clientMsgCaps;
|
|
172 CapsContainer encodingCaps;
|
2
|
173
|
4
|
174 // If true, informs that the RFB socket was closed.
|
7
|
175 // private boolean closed;
|
|
176 protected boolean closed;
|
0
|
177
|
4
|
178 //
|
|
179 // Constructor. Make TCP connection to RFB server.
|
|
180 //
|
|
181 RfbProto(String h, int p, VncViewer v) throws IOException {
|
|
182 viewer = v;
|
|
183 host = h;
|
|
184 port = p;
|
0
|
185
|
4
|
186 if (viewer.socketFactory == null) {
|
|
187 sock = new Socket(host, port);
|
|
188 } else {
|
|
189 try {
|
|
190 Class factoryClass = Class.forName(viewer.socketFactory);
|
|
191 SocketFactory factory = (SocketFactory) factoryClass
|
|
192 .newInstance();
|
|
193 if (viewer.inAnApplet)
|
|
194 sock = factory.createSocket(host, port, viewer);
|
|
195 else
|
|
196 sock = factory.createSocket(host, port, viewer.mainArgs);
|
|
197 } catch (Exception e) {
|
|
198 e.printStackTrace();
|
|
199 throw new IOException(e.getMessage());
|
|
200 }
|
|
201 }
|
|
202 is = new DataInputStream(new BufferedInputStream(sock.getInputStream(),
|
|
203 16384));
|
|
204 os = sock.getOutputStream();
|
0
|
205
|
4
|
206 timing = false;
|
|
207 timeWaitedIn100us = 5;
|
|
208 timedKbits = 0;
|
|
209 }
|
13
|
210
|
|
211 RfbProto(String h, int p) throws IOException {
|
|
212 host = h;
|
|
213 port = p;
|
|
214
|
|
215 sock = new Socket(host, port);
|
|
216 is = new DataInputStream(new BufferedInputStream(sock.getInputStream(),
|
|
217 16384));
|
|
218 os = sock.getOutputStream();
|
|
219
|
|
220 timing = false;
|
|
221 timeWaitedIn100us = 5;
|
|
222 timedKbits = 0;
|
|
223 }
|
88
|
224
|
|
225 public RfbProto() {
|
|
226
|
|
227 }
|
51
|
228
|
|
229
|
0
|
230
|
4
|
231 synchronized void close() {
|
|
232 try {
|
|
233 sock.close();
|
|
234 closed = true;
|
|
235 System.out.println("RFB socket closed");
|
|
236 if (rec != null) {
|
|
237 rec.close();
|
|
238 rec = null;
|
|
239 }
|
|
240 } catch (Exception e) {
|
|
241 e.printStackTrace();
|
|
242 }
|
|
243 }
|
|
244
|
|
245 synchronized boolean closed() {
|
|
246 return closed;
|
|
247 }
|
|
248
|
|
249 //
|
|
250 // Read server's protocol version message
|
|
251 //
|
|
252
|
|
253 void readVersionMsg() throws Exception {
|
0
|
254
|
4
|
255 byte[] b = new byte[12];
|
|
256
|
|
257 readFully(b);
|
0
|
258
|
4
|
259 if ((b[0] != 'R') || (b[1] != 'F') || (b[2] != 'B') || (b[3] != ' ')
|
|
260 || (b[4] < '0') || (b[4] > '9') || (b[5] < '0') || (b[5] > '9')
|
|
261 || (b[6] < '0') || (b[6] > '9') || (b[7] != '.')
|
|
262 || (b[8] < '0') || (b[8] > '9') || (b[9] < '0') || (b[9] > '9')
|
|
263 || (b[10] < '0') || (b[10] > '9') || (b[11] != '\n')) {
|
|
264 throw new Exception("Host " + host + " port " + port
|
|
265 + " is not an RFB server");
|
|
266 }
|
0
|
267
|
4
|
268 serverMajor = (b[4] - '0') * 100 + (b[5] - '0') * 10 + (b[6] - '0');
|
|
269 serverMinor = (b[8] - '0') * 100 + (b[9] - '0') * 10 + (b[10] - '0');
|
0
|
270
|
4
|
271 if (serverMajor < 3) {
|
|
272 throw new Exception(
|
|
273 "RFB server does not support protocol version 3");
|
|
274 }
|
|
275 }
|
|
276
|
|
277 //
|
|
278 // Write our protocol version message
|
|
279 //
|
0
|
280
|
4
|
281 void writeVersionMsg() throws IOException {
|
|
282 clientMajor = 3;
|
|
283 if (serverMajor > 3 || serverMinor >= 8) {
|
|
284 clientMinor = 8;
|
|
285 os.write(versionMsg_3_8.getBytes());
|
|
286 } else if (serverMinor >= 7) {
|
|
287 clientMinor = 7;
|
|
288 os.write(versionMsg_3_7.getBytes());
|
|
289 } else {
|
|
290 clientMinor = 3;
|
|
291 os.write(versionMsg_3_3.getBytes());
|
|
292 }
|
|
293 protocolTightVNC = false;
|
|
294 initCapabilities();
|
|
295 }
|
0
|
296
|
4
|
297 //
|
|
298 // Negotiate the authentication scheme.
|
|
299 //
|
0
|
300
|
4
|
301 int negotiateSecurity() throws Exception {
|
|
302 return (clientMinor >= 7) ? selectSecurityType() : readSecurityType();
|
|
303 }
|
0
|
304
|
4
|
305 //
|
|
306 // Read security type from the server (protocol version 3.3).
|
|
307 //
|
|
308
|
|
309 int readSecurityType() throws Exception {
|
|
310 int secType = readU32();
|
0
|
311
|
4
|
312 switch (secType) {
|
|
313 case SecTypeInvalid:
|
|
314 readConnFailedReason();
|
|
315 return SecTypeInvalid; // should never be executed
|
|
316 case SecTypeNone:
|
|
317 case SecTypeVncAuth:
|
|
318 return secType;
|
|
319 default:
|
|
320 throw new Exception("Unknown security type from RFB server: "
|
|
321 + secType);
|
|
322 }
|
|
323 }
|
0
|
324
|
4
|
325 //
|
|
326 // Select security type from the server's list (protocol versions 3.7/3.8).
|
|
327 //
|
0
|
328
|
4
|
329 int selectSecurityType() throws Exception {
|
|
330 int secType = SecTypeInvalid;
|
0
|
331
|
4
|
332 // Read the list of secutiry types.
|
|
333 int nSecTypes = readU8();
|
|
334 if (nSecTypes == 0) {
|
|
335 readConnFailedReason();
|
|
336 return SecTypeInvalid; // should never be executed
|
|
337 }
|
|
338 byte[] secTypes = new byte[nSecTypes];
|
|
339 readFully(secTypes);
|
0
|
340
|
4
|
341 // Find out if the server supports TightVNC protocol extensions
|
|
342 for (int i = 0; i < nSecTypes; i++) {
|
|
343 if (secTypes[i] == SecTypeTight) {
|
|
344 protocolTightVNC = true;
|
|
345 os.write(SecTypeTight);
|
|
346 return SecTypeTight;
|
|
347 }
|
|
348 }
|
0
|
349
|
4
|
350 // Find first supported security type.
|
|
351 for (int i = 0; i < nSecTypes; i++) {
|
|
352 if (secTypes[i] == SecTypeNone || secTypes[i] == SecTypeVncAuth) {
|
|
353 secType = secTypes[i];
|
|
354 break;
|
|
355 }
|
|
356 }
|
0
|
357
|
4
|
358 if (secType == SecTypeInvalid) {
|
|
359 throw new Exception("Server did not offer supported security type");
|
|
360 } else {
|
|
361 os.write(secType);
|
|
362 }
|
0
|
363
|
4
|
364 return secType;
|
|
365 }
|
|
366
|
|
367 //
|
|
368 // Perform "no authentication".
|
|
369 //
|
0
|
370
|
4
|
371 void authenticateNone() throws Exception {
|
|
372 if (clientMinor >= 8)
|
|
373 readSecurityResult("No authentication");
|
|
374 }
|
0
|
375
|
4
|
376 //
|
|
377 // Perform standard VNC Authentication.
|
|
378 //
|
0
|
379
|
4
|
380 void authenticateVNC(String pw) throws Exception {
|
|
381 byte[] challenge = new byte[16];
|
|
382 readFully(challenge);
|
0
|
383
|
4
|
384 if (pw.length() > 8)
|
|
385 pw = pw.substring(0, 8); // Truncate to 8 chars
|
0
|
386
|
4
|
387 // Truncate password on the first zero byte.
|
|
388 int firstZero = pw.indexOf(0);
|
|
389 if (firstZero != -1)
|
|
390 pw = pw.substring(0, firstZero);
|
|
391
|
|
392 byte[] key = { 0, 0, 0, 0, 0, 0, 0, 0 };
|
|
393 System.arraycopy(pw.getBytes(), 0, key, 0, pw.length());
|
|
394
|
|
395 DesCipher des = new DesCipher(key);
|
0
|
396
|
4
|
397 des.encrypt(challenge, 0, challenge, 0);
|
|
398 des.encrypt(challenge, 8, challenge, 8);
|
|
399
|
|
400 os.write(challenge);
|
|
401
|
|
402 readSecurityResult("VNC authentication");
|
|
403 }
|
0
|
404
|
4
|
405 //
|
|
406 // Read security result.
|
|
407 // Throws an exception on authentication failure.
|
|
408 //
|
0
|
409
|
4
|
410 void readSecurityResult(String authType) throws Exception {
|
|
411 int securityResult = readU32();
|
0
|
412
|
4
|
413 switch (securityResult) {
|
|
414 case VncAuthOK:
|
|
415 System.out.println(authType + ": success");
|
|
416 break;
|
|
417 case VncAuthFailed:
|
|
418 if (clientMinor >= 8)
|
|
419 readConnFailedReason();
|
|
420 throw new Exception(authType + ": failed");
|
|
421 case VncAuthTooMany:
|
|
422 throw new Exception(authType + ": failed, too many tries");
|
|
423 default:
|
|
424 throw new Exception(authType + ": unknown result " + securityResult);
|
|
425 }
|
|
426 }
|
0
|
427
|
4
|
428 //
|
|
429 // Read the string describing the reason for a connection failure,
|
|
430 // and throw an exception.
|
|
431 //
|
0
|
432
|
4
|
433 void readConnFailedReason() throws Exception {
|
|
434 int reasonLen = readU32();
|
|
435 byte[] reason = new byte[reasonLen];
|
|
436 readFully(reason);
|
|
437 throw new Exception(new String(reason));
|
|
438 }
|
0
|
439
|
4
|
440 //
|
|
441 // Initialize capability lists (TightVNC protocol extensions).
|
|
442 //
|
0
|
443
|
4
|
444 void initCapabilities() {
|
|
445 tunnelCaps = new CapsContainer();
|
|
446 authCaps = new CapsContainer();
|
|
447 serverMsgCaps = new CapsContainer();
|
|
448 clientMsgCaps = new CapsContainer();
|
|
449 encodingCaps = new CapsContainer();
|
0
|
450
|
4
|
451 // Supported authentication methods
|
|
452 authCaps.add(AuthNone, StandardVendor, SigAuthNone, "No authentication");
|
|
453 authCaps.add(AuthVNC, StandardVendor, SigAuthVNC,
|
|
454 "Standard VNC password authentication");
|
0
|
455
|
4
|
456 // Supported non-standard server-to-client messages
|
|
457 // [NONE]
|
|
458
|
|
459 // Supported non-standard client-to-server messages
|
|
460 // [NONE]
|
0
|
461
|
4
|
462 // Supported encoding types
|
|
463 encodingCaps.add(EncodingCopyRect, StandardVendor, SigEncodingCopyRect,
|
|
464 "Standard CopyRect encoding");
|
|
465 encodingCaps.add(EncodingRRE, StandardVendor, SigEncodingRRE,
|
|
466 "Standard RRE encoding");
|
|
467 encodingCaps.add(EncodingCoRRE, StandardVendor, SigEncodingCoRRE,
|
|
468 "Standard CoRRE encoding");
|
|
469 encodingCaps.add(EncodingHextile, StandardVendor, SigEncodingHextile,
|
|
470 "Standard Hextile encoding");
|
|
471 encodingCaps.add(EncodingZRLE, StandardVendor, SigEncodingZRLE,
|
107
|
472 "Standard ZRLE encoding");
|
|
473 encodingCaps.add(EncodingZRLEE, StandardVendor, SigEncodingZRLEE,
|
|
474 "Standard ZRLE(E) encoding");
|
4
|
475 encodingCaps.add(EncodingZlib, TridiaVncVendor, SigEncodingZlib,
|
|
476 "Zlib encoding");
|
|
477 encodingCaps.add(EncodingTight, TightVncVendor, SigEncodingTight,
|
|
478 "Tight encoding");
|
0
|
479
|
4
|
480 // Supported pseudo-encoding types
|
78
|
481
|
4
|
482 encodingCaps.add(EncodingCompressLevel0, TightVncVendor,
|
|
483 SigEncodingCompressLevel0, "Compression level");
|
|
484 encodingCaps.add(EncodingQualityLevel0, TightVncVendor,
|
|
485 SigEncodingQualityLevel0, "JPEG quality level");
|
|
486 encodingCaps.add(EncodingXCursor, TightVncVendor, SigEncodingXCursor,
|
|
487 "X-style cursor shape update");
|
|
488 encodingCaps.add(EncodingRichCursor, TightVncVendor,
|
|
489 SigEncodingRichCursor, "Rich-color cursor shape update");
|
|
490 encodingCaps.add(EncodingPointerPos, TightVncVendor,
|
|
491 SigEncodingPointerPos, "Pointer position update");
|
|
492 encodingCaps.add(EncodingLastRect, TightVncVendor, SigEncodingLastRect,
|
|
493 "LastRect protocol extension");
|
|
494 encodingCaps.add(EncodingNewFBSize, TightVncVendor,
|
|
495 SigEncodingNewFBSize, "Framebuffer size change");
|
78
|
496
|
4
|
497 }
|
0
|
498
|
4
|
499 //
|
|
500 // Setup tunneling (TightVNC protocol extensions)
|
|
501 //
|
0
|
502
|
4
|
503 void setupTunneling() throws IOException {
|
|
504 int nTunnelTypes = readU32();
|
|
505 if (nTunnelTypes != 0) {
|
|
506 readCapabilityList(tunnelCaps, nTunnelTypes);
|
0
|
507
|
4
|
508 // We don't support tunneling yet.
|
|
509 writeInt(NoTunneling);
|
|
510 }
|
|
511 }
|
0
|
512
|
4
|
513 //
|
|
514 // Negotiate authentication scheme (TightVNC protocol extensions)
|
|
515 //
|
|
516
|
|
517 int negotiateAuthenticationTight() throws Exception {
|
|
518 int nAuthTypes = readU32();
|
|
519 if (nAuthTypes == 0)
|
|
520 return AuthNone;
|
0
|
521
|
4
|
522 readCapabilityList(authCaps, nAuthTypes);
|
|
523 for (int i = 0; i < authCaps.numEnabled(); i++) {
|
|
524 int authType = authCaps.getByOrder(i);
|
|
525 if (authType == AuthNone || authType == AuthVNC) {
|
|
526 writeInt(authType);
|
|
527 return authType;
|
|
528 }
|
|
529 }
|
|
530 throw new Exception("No suitable authentication scheme found");
|
|
531 }
|
|
532
|
|
533 //
|
|
534 // Read a capability list (TightVNC protocol extensions)
|
|
535 //
|
0
|
536
|
4
|
537 void readCapabilityList(CapsContainer caps, int count) throws IOException {
|
|
538 int code;
|
|
539 byte[] vendor = new byte[4];
|
|
540 byte[] name = new byte[8];
|
|
541 for (int i = 0; i < count; i++) {
|
|
542 code = readU32();
|
|
543 readFully(vendor);
|
|
544 readFully(name);
|
|
545 caps.enable(new CapabilityInfo(code, vendor, name));
|
|
546 }
|
|
547 }
|
0
|
548
|
4
|
549 //
|
|
550 // Write a 32-bit integer into the output stream.
|
|
551 //
|
0
|
552
|
4
|
553 void writeInt(int value) throws IOException {
|
|
554 byte[] b = new byte[4];
|
|
555 b[0] = (byte) ((value >> 24) & 0xff);
|
|
556 b[1] = (byte) ((value >> 16) & 0xff);
|
|
557 b[2] = (byte) ((value >> 8) & 0xff);
|
|
558 b[3] = (byte) (value & 0xff);
|
|
559 os.write(b);
|
|
560 }
|
0
|
561
|
4
|
562 //
|
|
563 // Write the client initialisation message
|
|
564 //
|
0
|
565
|
4
|
566 void writeClientInit() throws IOException {
|
13
|
567 /*
|
4
|
568 if (viewer.options.shareDesktop) {
|
43
|
569 */
|
66
|
570
|
|
571 /**
|
|
572 * shared flag
|
|
573 */
|
4
|
574 os.write(1);
|
43
|
575 // os.write(0);
|
13
|
576
|
|
577 // viewer.options.disableShareDesktop();
|
4
|
578 }
|
0
|
579
|
4
|
580 //
|
|
581 // Read the server initialisation message
|
|
582 //
|
|
583
|
|
584 String desktopName;
|
|
585 int framebufferWidth, framebufferHeight;
|
|
586 int bitsPerPixel, depth;
|
|
587 boolean bigEndian, trueColour;
|
|
588 int redMax, greenMax, blueMax, redShift, greenShift, blueShift;
|
0
|
589
|
4
|
590 void readServerInit() throws IOException {
|
8
|
591
|
4
|
592 framebufferWidth = readU16();
|
|
593 framebufferHeight = readU16();
|
|
594 bitsPerPixel = readU8();
|
|
595 depth = readU8();
|
|
596 bigEndian = (readU8() != 0);
|
|
597 trueColour = (readU8() != 0);
|
|
598 redMax = readU16();
|
|
599 greenMax = readU16();
|
|
600 blueMax = readU16();
|
|
601 redShift = readU8();
|
|
602 greenShift = readU8();
|
|
603 blueShift = readU8();
|
|
604 byte[] pad = new byte[3];
|
|
605 readFully(pad);
|
|
606 int nameLength = readU32();
|
|
607 byte[] name = new byte[nameLength];
|
|
608 readFully(name);
|
|
609 desktopName = new String(name);
|
0
|
610
|
4
|
611 // Read interaction capabilities (TightVNC protocol extensions)
|
|
612 if (protocolTightVNC) {
|
|
613 int nServerMessageTypes = readU16();
|
|
614 int nClientMessageTypes = readU16();
|
|
615 int nEncodingTypes = readU16();
|
|
616 readU16();
|
|
617 readCapabilityList(serverMsgCaps, nServerMessageTypes);
|
|
618 readCapabilityList(clientMsgCaps, nClientMessageTypes);
|
|
619 readCapabilityList(encodingCaps, nEncodingTypes);
|
|
620 }
|
0
|
621
|
4
|
622 inNormalProtocol = true;
|
|
623 }
|
0
|
624
|
4
|
625 //
|
|
626 // Create session file and write initial protocol messages into it.
|
|
627 //
|
0
|
628
|
4
|
629 void startSession(String fname) throws IOException {
|
|
630 rec = new SessionRecorder(fname);
|
|
631 rec.writeHeader();
|
|
632 rec.write(versionMsg_3_3.getBytes());
|
|
633 rec.writeIntBE(SecTypeNone);
|
|
634 rec.writeShortBE(framebufferWidth);
|
|
635 rec.writeShortBE(framebufferHeight);
|
|
636 byte[] fbsServerInitMsg = { 32, 24, 0, 1, 0, (byte) 0xFF, 0,
|
|
637 (byte) 0xFF, 0, (byte) 0xFF, 16, 8, 0, 0, 0, 0 };
|
|
638 rec.write(fbsServerInitMsg);
|
|
639 rec.writeIntBE(desktopName.length());
|
|
640 rec.write(desktopName.getBytes());
|
|
641 numUpdatesInSession = 0;
|
0
|
642
|
4
|
643 // FIXME: If there were e.g. ZRLE updates only, that should not
|
|
644 // affect recording of Zlib and Tight updates. So, actually
|
|
645 // we should maintain separate flags for Zlib, ZRLE and
|
|
646 // Tight, instead of one ``wereZlibUpdates'' variable.
|
|
647 //
|
|
648 if (wereZlibUpdates)
|
|
649 recordFromBeginning = false;
|
|
650
|
|
651 zlibWarningShown = false;
|
|
652 tightWarningShown = false;
|
|
653 }
|
|
654
|
|
655 //
|
|
656 // Close session file.
|
|
657 //
|
0
|
658
|
4
|
659 void closeSession() throws IOException {
|
|
660 if (rec != null) {
|
|
661 rec.close();
|
|
662 rec = null;
|
|
663 }
|
|
664 }
|
|
665
|
|
666 //
|
|
667 // Set new framebuffer size
|
|
668 //
|
0
|
669
|
4
|
670 void setFramebufferSize(int width, int height) {
|
|
671 framebufferWidth = width;
|
|
672 framebufferHeight = height;
|
|
673 }
|
0
|
674
|
4
|
675 //
|
|
676 // Read the server message type
|
|
677 //
|
|
678
|
|
679 int readServerMessageType() throws IOException {
|
|
680 int msgType = readU8();
|
0
|
681
|
4
|
682 // If the session is being recorded:
|
|
683 if (rec != null) {
|
|
684 if (msgType == Bell) { // Save Bell messages in session files.
|
|
685 rec.writeByte(msgType);
|
|
686 if (numUpdatesInSession > 0)
|
|
687 rec.flush();
|
|
688 }
|
|
689 }
|
0
|
690
|
4
|
691 return msgType;
|
|
692 }
|
|
693
|
|
694 //
|
|
695 // Read a FramebufferUpdate message
|
|
696 //
|
0
|
697
|
4
|
698 int updateNRects;
|
|
699
|
|
700 void readFramebufferUpdate() throws IOException {
|
|
701 skipBytes(1);
|
|
702 updateNRects = readU16();
|
|
703 // System.out.println(updateNRects);
|
0
|
704
|
4
|
705 // If the session is being recorded:
|
|
706 if (rec != null) {
|
|
707 rec.writeByte(FramebufferUpdate);
|
|
708 rec.writeByte(0);
|
|
709 rec.writeShortBE(updateNRects);
|
|
710 }
|
0
|
711
|
4
|
712 numUpdatesInSession++;
|
|
713 }
|
0
|
714
|
4
|
715 // Read a FramebufferUpdate rectangle header
|
0
|
716
|
4
|
717 int updateRectX, updateRectY, updateRectW, updateRectH, updateRectEncoding;
|
0
|
718
|
4
|
719 void readFramebufferUpdateRectHdr() throws Exception {
|
|
720 updateRectX = readU16();
|
|
721 updateRectY = readU16();
|
|
722 updateRectW = readU16();
|
|
723 updateRectH = readU16();
|
|
724 updateRectEncoding = readU32();
|
|
725 // System.out.println("readU16&32");
|
0
|
726
|
4
|
727 if (updateRectEncoding == EncodingZlib
|
|
728 || updateRectEncoding == EncodingZRLE
|
107
|
729 || updateRectEncoding == EncodingZRLEE
|
4
|
730 || updateRectEncoding == EncodingTight)
|
|
731 wereZlibUpdates = true;
|
0
|
732
|
4
|
733 // If the session is being recorded:
|
|
734 if (rec != null) {
|
|
735 if (numUpdatesInSession > 1)
|
|
736 rec.flush(); // Flush the output on each rectangle.
|
|
737 rec.writeShortBE(updateRectX);
|
|
738 rec.writeShortBE(updateRectY);
|
|
739 rec.writeShortBE(updateRectW);
|
|
740 rec.writeShortBE(updateRectH);
|
|
741 if (updateRectEncoding == EncodingZlib && !recordFromBeginning) {
|
|
742 // Here we cannot write Zlib-encoded rectangles because the
|
|
743 // decoder won't be able to reproduce zlib stream state.
|
|
744 if (!zlibWarningShown) {
|
|
745 System.out.println("Warning: Raw encoding will be used "
|
|
746 + "instead of Zlib in recorded session.");
|
|
747 zlibWarningShown = true;
|
|
748 }
|
|
749 rec.writeIntBE(EncodingRaw);
|
|
750 } else {
|
|
751 rec.writeIntBE(updateRectEncoding);
|
|
752 if (updateRectEncoding == EncodingTight && !recordFromBeginning
|
|
753 && !tightWarningShown) {
|
|
754 System.out.println("Warning: Re-compressing Tight-encoded "
|
|
755 + "updates for session recording.");
|
|
756 tightWarningShown = true;
|
|
757 }
|
|
758 }
|
|
759 }
|
0
|
760
|
4
|
761 if (updateRectEncoding < 0 || updateRectEncoding > MaxNormalEncoding)
|
|
762 return;
|
0
|
763
|
4
|
764 if (updateRectX + updateRectW > framebufferWidth
|
|
765 || updateRectY + updateRectH > framebufferHeight) {
|
|
766 throw new Exception("Framebuffer update rectangle too large: "
|
|
767 + updateRectW + "x" + updateRectH + " at (" + updateRectX
|
|
768 + "," + updateRectY + ")");
|
|
769 }
|
|
770 }
|
0
|
771
|
4
|
772 // Read CopyRect source X and Y.
|
|
773
|
|
774 int copyRectSrcX, copyRectSrcY;
|
|
775
|
|
776 void readCopyRect() throws IOException {
|
|
777 copyRectSrcX = readU16();
|
|
778 copyRectSrcY = readU16();
|
0
|
779
|
4
|
780 // If the session is being recorded:
|
|
781 if (rec != null) {
|
|
782 rec.writeShortBE(copyRectSrcX);
|
|
783 rec.writeShortBE(copyRectSrcY);
|
|
784 }
|
0
|
785 }
|
4
|
786
|
|
787 //
|
|
788 // Read a ServerCutText message
|
|
789 //
|
|
790
|
|
791 String readServerCutText() throws IOException {
|
|
792 skipBytes(3);
|
|
793 int len = readU32();
|
|
794 byte[] text = new byte[len];
|
|
795 readFully(text);
|
|
796 return new String(text);
|
0
|
797 }
|
4
|
798
|
|
799 //
|
|
800 // Read an integer in compact representation (1..3 bytes).
|
|
801 // Such format is used as a part of the Tight encoding.
|
|
802 // Also, this method records data if session recording is active and
|
|
803 // the viewer's recordFromBeginning variable is set to true.
|
|
804 //
|
0
|
805
|
4
|
806 int readCompactLen() throws IOException {
|
|
807 int[] portion = new int[3];
|
|
808 portion[0] = readU8();
|
|
809 int byteCount = 1;
|
|
810 int len = portion[0] & 0x7F;
|
|
811 if ((portion[0] & 0x80) != 0) {
|
|
812 portion[1] = readU8();
|
|
813 byteCount++;
|
|
814 len |= (portion[1] & 0x7F) << 7;
|
|
815 if ((portion[1] & 0x80) != 0) {
|
|
816 portion[2] = readU8();
|
|
817 byteCount++;
|
|
818 len |= (portion[2] & 0xFF) << 14;
|
|
819 }
|
|
820 }
|
0
|
821
|
4
|
822 if (rec != null && recordFromBeginning)
|
|
823 for (int i = 0; i < byteCount; i++)
|
|
824 rec.writeByte(portion[i]);
|
0
|
825
|
4
|
826 return len;
|
|
827 }
|
0
|
828
|
4
|
829 //
|
|
830 // Write a FramebufferUpdateRequest message
|
|
831 //
|
|
832
|
|
833 void writeFramebufferUpdateRequest(int x, int y, int w, int h,
|
|
834 boolean incremental) throws IOException {
|
|
835 byte[] b = new byte[10];
|
0
|
836
|
4
|
837 b[0] = (byte) FramebufferUpdateRequest;
|
|
838 b[1] = (byte) (incremental ? 1 : 0);
|
|
839 b[2] = (byte) ((x >> 8) & 0xff);
|
|
840 b[3] = (byte) (x & 0xff);
|
|
841 b[4] = (byte) ((y >> 8) & 0xff);
|
|
842 b[5] = (byte) (y & 0xff);
|
|
843 b[6] = (byte) ((w >> 8) & 0xff);
|
|
844 b[7] = (byte) (w & 0xff);
|
|
845 b[8] = (byte) ((h >> 8) & 0xff);
|
|
846 b[9] = (byte) (h & 0xff);
|
0
|
847
|
4
|
848 os.write(b);
|
|
849 }
|
|
850
|
|
851 //
|
|
852 // Write a SetPixelFormat message
|
|
853 //
|
|
854
|
|
855 void writeSetPixelFormat(int bitsPerPixel, int depth, boolean bigEndian,
|
|
856 boolean trueColour, int redMax, int greenMax, int blueMax,
|
|
857 int redShift, int greenShift, int blueShift) throws IOException {
|
|
858 byte[] b = new byte[20];
|
0
|
859
|
4
|
860 b[0] = (byte) SetPixelFormat;
|
|
861 b[4] = (byte) bitsPerPixel;
|
|
862 b[5] = (byte) depth;
|
|
863 b[6] = (byte) (bigEndian ? 1 : 0);
|
|
864 b[7] = (byte) (trueColour ? 1 : 0);
|
|
865 b[8] = (byte) ((redMax >> 8) & 0xff);
|
|
866 b[9] = (byte) (redMax & 0xff);
|
|
867 b[10] = (byte) ((greenMax >> 8) & 0xff);
|
|
868 b[11] = (byte) (greenMax & 0xff);
|
|
869 b[12] = (byte) ((blueMax >> 8) & 0xff);
|
|
870 b[13] = (byte) (blueMax & 0xff);
|
|
871 b[14] = (byte) redShift;
|
|
872 b[15] = (byte) greenShift;
|
|
873 b[16] = (byte) blueShift;
|
0
|
874
|
4
|
875 os.write(b);
|
|
876 }
|
0
|
877
|
4
|
878 //
|
|
879 // Write a FixColourMapEntries message. The values in the red, green and
|
|
880 // blue arrays are from 0 to 65535.
|
|
881 //
|
0
|
882
|
4
|
883 void writeFixColourMapEntries(int firstColour, int nColours, int[] red,
|
|
884 int[] green, int[] blue) throws IOException {
|
|
885 byte[] b = new byte[6 + nColours * 6];
|
0
|
886
|
4
|
887 b[0] = (byte) FixColourMapEntries;
|
|
888 b[2] = (byte) ((firstColour >> 8) & 0xff);
|
|
889 b[3] = (byte) (firstColour & 0xff);
|
|
890 b[4] = (byte) ((nColours >> 8) & 0xff);
|
|
891 b[5] = (byte) (nColours & 0xff);
|
0
|
892
|
4
|
893 for (int i = 0; i < nColours; i++) {
|
|
894 b[6 + i * 6] = (byte) ((red[i] >> 8) & 0xff);
|
|
895 b[6 + i * 6 + 1] = (byte) (red[i] & 0xff);
|
|
896 b[6 + i * 6 + 2] = (byte) ((green[i] >> 8) & 0xff);
|
|
897 b[6 + i * 6 + 3] = (byte) (green[i] & 0xff);
|
|
898 b[6 + i * 6 + 4] = (byte) ((blue[i] >> 8) & 0xff);
|
|
899 b[6 + i * 6 + 5] = (byte) (blue[i] & 0xff);
|
|
900 }
|
0
|
901
|
4
|
902 os.write(b);
|
|
903 }
|
0
|
904
|
4
|
905 //
|
|
906 // Write a SetEncodings message
|
|
907 //
|
|
908
|
|
909 void writeSetEncodings(int[] encs, int len) throws IOException {
|
|
910 byte[] b = new byte[4 + 4 * len];
|
0
|
911
|
4
|
912 b[0] = (byte) SetEncodings;
|
|
913 b[2] = (byte) ((len >> 8) & 0xff);
|
|
914 b[3] = (byte) (len & 0xff);
|
0
|
915
|
4
|
916 for (int i = 0; i < len; i++) {
|
|
917 b[4 + 4 * i] = (byte) ((encs[i] >> 24) & 0xff);
|
|
918 b[5 + 4 * i] = (byte) ((encs[i] >> 16) & 0xff);
|
|
919 b[6 + 4 * i] = (byte) ((encs[i] >> 8) & 0xff);
|
|
920 b[7 + 4 * i] = (byte) (encs[i] & 0xff);
|
|
921 }
|
|
922
|
|
923 os.write(b);
|
|
924 }
|
|
925
|
|
926 //
|
|
927 // Write a ClientCutText message
|
|
928 //
|
|
929
|
|
930 void writeClientCutText(String text) throws IOException {
|
|
931 byte[] b = new byte[8 + text.length()];
|
0
|
932
|
4
|
933 b[0] = (byte) ClientCutText;
|
|
934 b[4] = (byte) ((text.length() >> 24) & 0xff);
|
|
935 b[5] = (byte) ((text.length() >> 16) & 0xff);
|
|
936 b[6] = (byte) ((text.length() >> 8) & 0xff);
|
|
937 b[7] = (byte) (text.length() & 0xff);
|
0
|
938
|
4
|
939 System.arraycopy(text.getBytes(), 0, b, 8, text.length());
|
|
940
|
|
941 os.write(b);
|
|
942 }
|
0
|
943
|
4
|
944 //
|
|
945 // A buffer for putting pointer and keyboard events before being sent. This
|
|
946 // is to ensure that multiple RFB events generated from a single Java Event
|
|
947 // will all be sent in a single network packet. The maximum possible
|
|
948 // length is 4 modifier down events, a single key event followed by 4
|
|
949 // modifier up events i.e. 9 key events or 72 bytes.
|
|
950 //
|
0
|
951
|
4
|
952 byte[] eventBuf = new byte[72];
|
|
953 int eventBufLen;
|
0
|
954
|
4
|
955 // Useful shortcuts for modifier masks.
|
|
956
|
|
957 final static int CTRL_MASK = InputEvent.CTRL_MASK;
|
|
958 final static int SHIFT_MASK = InputEvent.SHIFT_MASK;
|
|
959 final static int META_MASK = InputEvent.META_MASK;
|
|
960 final static int ALT_MASK = InputEvent.ALT_MASK;
|
|
961
|
107
|
962
|
|
963
|
4
|
964 //
|
|
965 // Write a pointer event message. We may need to send modifier key events
|
|
966 // around it to set the correct modifier state.
|
|
967 //
|
0
|
968
|
4
|
969 int pointerMask = 0;
|
0
|
970
|
4
|
971 void writePointerEvent(MouseEvent evt) throws IOException {
|
|
972 int modifiers = evt.getModifiers();
|
0
|
973
|
4
|
974 int mask2 = 2;
|
|
975 int mask3 = 4;
|
|
976 if (viewer.options.reverseMouseButtons2And3) {
|
|
977 mask2 = 4;
|
|
978 mask3 = 2;
|
|
979 }
|
0
|
980
|
4
|
981 // Note: For some reason, AWT does not set BUTTON1_MASK on left
|
|
982 // button presses. Here we think that it was the left button if
|
|
983 // modifiers do not include BUTTON2_MASK or BUTTON3_MASK.
|
0
|
984
|
4
|
985 if (evt.getID() == MouseEvent.MOUSE_PRESSED) {
|
|
986 if ((modifiers & InputEvent.BUTTON2_MASK) != 0) {
|
|
987 pointerMask = mask2;
|
|
988 modifiers &= ~ALT_MASK;
|
|
989 } else if ((modifiers & InputEvent.BUTTON3_MASK) != 0) {
|
|
990 pointerMask = mask3;
|
|
991 modifiers &= ~META_MASK;
|
|
992 } else {
|
|
993 pointerMask = 1;
|
|
994 }
|
|
995 } else if (evt.getID() == MouseEvent.MOUSE_RELEASED) {
|
|
996 pointerMask = 0;
|
|
997 if ((modifiers & InputEvent.BUTTON2_MASK) != 0) {
|
|
998 modifiers &= ~ALT_MASK;
|
|
999 } else if ((modifiers & InputEvent.BUTTON3_MASK) != 0) {
|
|
1000 modifiers &= ~META_MASK;
|
|
1001 }
|
|
1002 }
|
0
|
1003
|
4
|
1004 eventBufLen = 0;
|
|
1005 writeModifierKeyEvents(modifiers);
|
|
1006
|
|
1007 int x = evt.getX();
|
|
1008 int y = evt.getY();
|
0
|
1009
|
4
|
1010 if (x < 0)
|
|
1011 x = 0;
|
|
1012 if (y < 0)
|
|
1013 y = 0;
|
0
|
1014
|
4
|
1015 eventBuf[eventBufLen++] = (byte) PointerEvent;
|
|
1016 eventBuf[eventBufLen++] = (byte) pointerMask;
|
|
1017 eventBuf[eventBufLen++] = (byte) ((x >> 8) & 0xff);
|
|
1018 eventBuf[eventBufLen++] = (byte) (x & 0xff);
|
|
1019 eventBuf[eventBufLen++] = (byte) ((y >> 8) & 0xff);
|
|
1020 eventBuf[eventBufLen++] = (byte) (y & 0xff);
|
0
|
1021
|
4
|
1022 //
|
|
1023 // Always release all modifiers after an "up" event
|
|
1024 //
|
0
|
1025
|
4
|
1026 if (pointerMask == 0) {
|
|
1027 writeModifierKeyEvents(0);
|
|
1028 }
|
0
|
1029
|
4
|
1030 os.write(eventBuf, 0, eventBufLen);
|
|
1031 }
|
0
|
1032
|
4
|
1033 //
|
|
1034 // Write a key event message. We may need to send modifier key events
|
|
1035 // around it to set the correct modifier state. Also we need to translate
|
|
1036 // from the Java key values to the X keysym values used by the RFB protocol.
|
|
1037 //
|
|
1038
|
|
1039 void writeKeyEvent(KeyEvent evt) throws IOException {
|
|
1040
|
|
1041 int keyChar = evt.getKeyChar();
|
0
|
1042
|
4
|
1043 //
|
|
1044 // Ignore event if only modifiers were pressed.
|
|
1045 //
|
|
1046
|
|
1047 // Some JVMs return 0 instead of CHAR_UNDEFINED in getKeyChar().
|
|
1048 if (keyChar == 0)
|
|
1049 keyChar = KeyEvent.CHAR_UNDEFINED;
|
0
|
1050
|
4
|
1051 if (keyChar == KeyEvent.CHAR_UNDEFINED) {
|
|
1052 int code = evt.getKeyCode();
|
|
1053 if (code == KeyEvent.VK_CONTROL || code == KeyEvent.VK_SHIFT
|
|
1054 || code == KeyEvent.VK_META || code == KeyEvent.VK_ALT)
|
|
1055 return;
|
|
1056 }
|
0
|
1057
|
4
|
1058 //
|
|
1059 // Key press or key release?
|
|
1060 //
|
|
1061
|
|
1062 boolean down = (evt.getID() == KeyEvent.KEY_PRESSED);
|
|
1063
|
|
1064 int key;
|
|
1065 if (evt.isActionKey()) {
|
0
|
1066
|
4
|
1067 //
|
|
1068 // An action key should be one of the following.
|
|
1069 // If not then just ignore the event.
|
|
1070 //
|
0
|
1071
|
4
|
1072 switch (evt.getKeyCode()) {
|
|
1073 case KeyEvent.VK_HOME:
|
|
1074 key = 0xff50;
|
|
1075 break;
|
|
1076 case KeyEvent.VK_LEFT:
|
|
1077 key = 0xff51;
|
|
1078 break;
|
|
1079 case KeyEvent.VK_UP:
|
|
1080 key = 0xff52;
|
|
1081 break;
|
|
1082 case KeyEvent.VK_RIGHT:
|
|
1083 key = 0xff53;
|
|
1084 break;
|
|
1085 case KeyEvent.VK_DOWN:
|
|
1086 key = 0xff54;
|
|
1087 break;
|
|
1088 case KeyEvent.VK_PAGE_UP:
|
|
1089 key = 0xff55;
|
|
1090 break;
|
|
1091 case KeyEvent.VK_PAGE_DOWN:
|
|
1092 key = 0xff56;
|
|
1093 break;
|
|
1094 case KeyEvent.VK_END:
|
|
1095 key = 0xff57;
|
|
1096 break;
|
|
1097 case KeyEvent.VK_INSERT:
|
|
1098 key = 0xff63;
|
|
1099 break;
|
|
1100 case KeyEvent.VK_F1:
|
|
1101 key = 0xffbe;
|
|
1102 break;
|
|
1103 case KeyEvent.VK_F2:
|
|
1104 key = 0xffbf;
|
|
1105 break;
|
|
1106 case KeyEvent.VK_F3:
|
|
1107 key = 0xffc0;
|
|
1108 break;
|
|
1109 case KeyEvent.VK_F4:
|
|
1110 key = 0xffc1;
|
|
1111 break;
|
|
1112 case KeyEvent.VK_F5:
|
|
1113 key = 0xffc2;
|
|
1114 break;
|
|
1115 case KeyEvent.VK_F6:
|
|
1116 key = 0xffc3;
|
|
1117 break;
|
|
1118 case KeyEvent.VK_F7:
|
|
1119 key = 0xffc4;
|
|
1120 break;
|
|
1121 case KeyEvent.VK_F8:
|
|
1122 key = 0xffc5;
|
|
1123 break;
|
|
1124 case KeyEvent.VK_F9:
|
|
1125 key = 0xffc6;
|
|
1126 break;
|
|
1127 case KeyEvent.VK_F10:
|
|
1128 key = 0xffc7;
|
|
1129 break;
|
|
1130 case KeyEvent.VK_F11:
|
|
1131 key = 0xffc8;
|
|
1132 break;
|
|
1133 case KeyEvent.VK_F12:
|
|
1134 key = 0xffc9;
|
|
1135 break;
|
|
1136 default:
|
|
1137 return;
|
|
1138 }
|
0
|
1139
|
4
|
1140 } else {
|
0
|
1141
|
4
|
1142 //
|
|
1143 // A "normal" key press. Ordinary ASCII characters go straight
|
|
1144 // through.
|
|
1145 // For CTRL-<letter>, CTRL is sent separately so just send <letter>.
|
|
1146 // Backspace, tab, return, escape and delete have special keysyms.
|
|
1147 // Anything else we ignore.
|
|
1148 //
|
0
|
1149
|
4
|
1150 key = keyChar;
|
0
|
1151
|
4
|
1152 if (key < 0x20) {
|
|
1153 if (evt.isControlDown()) {
|
|
1154 key += 0x60;
|
|
1155 } else {
|
|
1156 switch (key) {
|
|
1157 case KeyEvent.VK_BACK_SPACE:
|
|
1158 key = 0xff08;
|
|
1159 break;
|
|
1160 case KeyEvent.VK_TAB:
|
|
1161 key = 0xff09;
|
|
1162 break;
|
|
1163 case KeyEvent.VK_ENTER:
|
|
1164 key = 0xff0d;
|
|
1165 break;
|
|
1166 case KeyEvent.VK_ESCAPE:
|
|
1167 key = 0xff1b;
|
|
1168 break;
|
|
1169 }
|
|
1170 }
|
|
1171 } else if (key == 0x7f) {
|
|
1172 // Delete
|
|
1173 key = 0xffff;
|
|
1174 } else if (key > 0xff) {
|
|
1175 // JDK1.1 on X incorrectly passes some keysyms straight through,
|
|
1176 // so we do too. JDK1.1.4 seems to have fixed this.
|
|
1177 // The keysyms passed are 0xff00 .. XK_BackSpace .. XK_Delete
|
|
1178 // Also, we pass through foreign currency keysyms
|
|
1179 // (0x20a0..0x20af).
|
|
1180 if ((key < 0xff00 || key > 0xffff)
|
|
1181 && !(key >= 0x20a0 && key <= 0x20af))
|
|
1182 return;
|
|
1183 }
|
|
1184 }
|
0
|
1185
|
4
|
1186 // Fake keyPresses for keys that only generates keyRelease events
|
|
1187 if ((key == 0xe5) || (key == 0xc5) || // XK_aring / XK_Aring
|
|
1188 (key == 0xe4) || (key == 0xc4) || // XK_adiaeresis /
|
|
1189 // XK_Adiaeresis
|
|
1190 (key == 0xf6) || (key == 0xd6) || // XK_odiaeresis /
|
|
1191 // XK_Odiaeresis
|
|
1192 (key == 0xa7) || (key == 0xbd) || // XK_section / XK_onehalf
|
|
1193 (key == 0xa3)) { // XK_sterling
|
|
1194 // Make sure we do not send keypress events twice on platforms
|
|
1195 // with correct JVMs (those that actually report KeyPress for all
|
|
1196 // keys)
|
|
1197 if (down)
|
|
1198 brokenKeyPressed = true;
|
0
|
1199
|
4
|
1200 if (!down && !brokenKeyPressed) {
|
|
1201 // We've got a release event for this key, but haven't received
|
|
1202 // a press. Fake it.
|
|
1203 eventBufLen = 0;
|
|
1204 writeModifierKeyEvents(evt.getModifiers());
|
|
1205 writeKeyEvent(key, true);
|
|
1206 os.write(eventBuf, 0, eventBufLen);
|
|
1207 }
|
0
|
1208
|
4
|
1209 if (!down)
|
|
1210 brokenKeyPressed = false;
|
|
1211 }
|
0
|
1212
|
4
|
1213 eventBufLen = 0;
|
|
1214 writeModifierKeyEvents(evt.getModifiers());
|
|
1215 writeKeyEvent(key, down);
|
0
|
1216
|
4
|
1217 // Always release all modifiers after an "up" event
|
|
1218 if (!down)
|
|
1219 writeModifierKeyEvents(0);
|
0
|
1220
|
4
|
1221 os.write(eventBuf, 0, eventBufLen);
|
|
1222 }
|
|
1223
|
|
1224 //
|
|
1225 // Add a raw key event with the given X keysym to eventBuf.
|
|
1226 //
|
0
|
1227
|
4
|
1228 void writeKeyEvent(int keysym, boolean down) {
|
|
1229 eventBuf[eventBufLen++] = (byte) KeyboardEvent;
|
|
1230 eventBuf[eventBufLen++] = (byte) (down ? 1 : 0);
|
|
1231 eventBuf[eventBufLen++] = (byte) 0;
|
|
1232 eventBuf[eventBufLen++] = (byte) 0;
|
|
1233 eventBuf[eventBufLen++] = (byte) ((keysym >> 24) & 0xff);
|
|
1234 eventBuf[eventBufLen++] = (byte) ((keysym >> 16) & 0xff);
|
|
1235 eventBuf[eventBufLen++] = (byte) ((keysym >> 8) & 0xff);
|
|
1236 eventBuf[eventBufLen++] = (byte) (keysym & 0xff);
|
|
1237 }
|
0
|
1238
|
4
|
1239 //
|
|
1240 // Write key events to set the correct modifier state.
|
|
1241 //
|
0
|
1242
|
4
|
1243 int oldModifiers = 0;
|
0
|
1244
|
4
|
1245 void writeModifierKeyEvents(int newModifiers) {
|
|
1246 if ((newModifiers & CTRL_MASK) != (oldModifiers & CTRL_MASK))
|
|
1247 writeKeyEvent(0xffe3, (newModifiers & CTRL_MASK) != 0);
|
0
|
1248
|
4
|
1249 if ((newModifiers & SHIFT_MASK) != (oldModifiers & SHIFT_MASK))
|
|
1250 writeKeyEvent(0xffe1, (newModifiers & SHIFT_MASK) != 0);
|
0
|
1251
|
4
|
1252 if ((newModifiers & META_MASK) != (oldModifiers & META_MASK))
|
|
1253 writeKeyEvent(0xffe7, (newModifiers & META_MASK) != 0);
|
0
|
1254
|
4
|
1255 if ((newModifiers & ALT_MASK) != (oldModifiers & ALT_MASK))
|
|
1256 writeKeyEvent(0xffe9, (newModifiers & ALT_MASK) != 0);
|
0
|
1257
|
4
|
1258 oldModifiers = newModifiers;
|
|
1259 }
|
0
|
1260
|
4
|
1261 //
|
|
1262 // Compress and write the data into the recorded session file. This
|
|
1263 // method assumes the recording is on (rec != null).
|
|
1264 //
|
0
|
1265
|
4
|
1266 void recordCompressedData(byte[] data, int off, int len) throws IOException {
|
|
1267 Deflater deflater = new Deflater();
|
|
1268 deflater.setInput(data, off, len);
|
|
1269 int bufSize = len + len / 100 + 12;
|
|
1270 byte[] buf = new byte[bufSize];
|
|
1271 deflater.finish();
|
|
1272 int compressedSize = deflater.deflate(buf);
|
|
1273 recordCompactLen(compressedSize);
|
|
1274 rec.write(buf, 0, compressedSize);
|
|
1275 }
|
0
|
1276
|
4
|
1277 void recordCompressedData(byte[] data) throws IOException {
|
|
1278 recordCompressedData(data, 0, data.length);
|
|
1279 }
|
0
|
1280
|
4
|
1281 //
|
|
1282 // Write an integer in compact representation (1..3 bytes) into the
|
|
1283 // recorded session file. This method assumes the recording is on
|
|
1284 // (rec != null).
|
|
1285 //
|
0
|
1286
|
4
|
1287 void recordCompactLen(int len) throws IOException {
|
|
1288 byte[] buf = new byte[3];
|
|
1289 int bytes = 0;
|
|
1290 buf[bytes++] = (byte) (len & 0x7F);
|
|
1291 if (len > 0x7F) {
|
|
1292 buf[bytes - 1] |= 0x80;
|
|
1293 buf[bytes++] = (byte) (len >> 7 & 0x7F);
|
|
1294 if (len > 0x3FFF) {
|
|
1295 buf[bytes - 1] |= 0x80;
|
|
1296 buf[bytes++] = (byte) (len >> 14 & 0xFF);
|
|
1297 }
|
|
1298 }
|
|
1299 rec.write(buf, 0, bytes);
|
|
1300 }
|
0
|
1301
|
4
|
1302 public void startTiming() {
|
|
1303 timing = true;
|
0
|
1304
|
4
|
1305 // Carry over up to 1s worth of previous rate for smoothing.
|
0
|
1306
|
4
|
1307 if (timeWaitedIn100us > 10000) {
|
|
1308 timedKbits = timedKbits * 10000 / timeWaitedIn100us;
|
|
1309 timeWaitedIn100us = 10000;
|
|
1310 }
|
|
1311 }
|
0
|
1312
|
4
|
1313 public void stopTiming() {
|
|
1314 timing = false;
|
|
1315 if (timeWaitedIn100us < timedKbits / 2)
|
|
1316 timeWaitedIn100us = timedKbits / 2; // upper limit 20Mbit/s
|
|
1317 }
|
0
|
1318
|
4
|
1319 public long kbitsPerSecond() {
|
|
1320 return timedKbits * 10000 / timeWaitedIn100us;
|
|
1321 }
|
0
|
1322
|
4
|
1323 public long timeWaited() {
|
|
1324 return timeWaitedIn100us;
|
|
1325 }
|
0
|
1326
|
4
|
1327 //
|
|
1328 // Methods for reading data via our DataInputStream member variable (is).
|
|
1329 //
|
|
1330 // In addition to reading data, the readFully() methods updates variables
|
|
1331 // used to estimate data throughput.
|
|
1332 //
|
0
|
1333
|
4
|
1334 public void readFully(byte b[]) throws IOException {
|
|
1335 readFully(b, 0, b.length);
|
|
1336 }
|
0
|
1337
|
4
|
1338 public void readFully(byte b[], int off, int len) throws IOException {
|
|
1339 long before = 0;
|
|
1340 if (timing)
|
|
1341 before = System.currentTimeMillis();
|
|
1342
|
|
1343 is.readFully(b, off, len);
|
0
|
1344
|
4
|
1345 if (timing) {
|
|
1346 long after = System.currentTimeMillis();
|
|
1347 long newTimeWaited = (after - before) * 10;
|
|
1348 int newKbits = len * 8 / 1000;
|
0
|
1349
|
4
|
1350 // limit rate to between 10kbit/s and 40Mbit/s
|
0
|
1351
|
4
|
1352 if (newTimeWaited > newKbits * 1000)
|
|
1353 newTimeWaited = newKbits * 1000;
|
|
1354 if (newTimeWaited < newKbits / 4)
|
|
1355 newTimeWaited = newKbits / 4;
|
0
|
1356
|
4
|
1357 timeWaitedIn100us += newTimeWaited;
|
|
1358 timedKbits += newKbits;
|
|
1359 }
|
0
|
1360
|
4
|
1361 numBytesRead += len;
|
|
1362 }
|
0
|
1363
|
4
|
1364 final int available() throws IOException {
|
|
1365 return is.available();
|
|
1366 }
|
0
|
1367
|
4
|
1368 // FIXME: DataInputStream::skipBytes() is not guaranteed to skip
|
|
1369 // exactly n bytes. Probably we don't want to use this method.
|
|
1370 final int skipBytes(int n) throws IOException {
|
|
1371 int r = is.skipBytes(n);
|
|
1372 numBytesRead += r;
|
|
1373 return r;
|
|
1374 }
|
0
|
1375
|
4
|
1376 final int readU8() throws IOException {
|
|
1377 int r = is.readUnsignedByte();
|
|
1378 numBytesRead++;
|
0
|
1379
|
4
|
1380 return r;
|
|
1381 }
|
0
|
1382
|
4
|
1383 final int readU16() throws IOException {
|
|
1384 int r = is.readUnsignedShort();
|
|
1385 numBytesRead += 2;
|
|
1386 return r;
|
|
1387 }
|
0
|
1388
|
4
|
1389 final int readU32() throws IOException {
|
|
1390 int r = is.readInt();
|
|
1391 numBytesRead += 4;
|
|
1392 return r;
|
|
1393 }
|
0
|
1394 }
|