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