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