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