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