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,
|
|
81 EncodingCoRRE = 4, EncodingHextile = 5, EncodingZlib = 6,
|
|
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 */
|
4
|
564 os.write(1);
|
43
|
565 // os.write(0);
|
13
|
566
|
|
567 // viewer.options.disableShareDesktop();
|
4
|
568 }
|
0
|
569
|
4
|
570 //
|
|
571 // Read the server initialisation message
|
|
572 //
|
|
573
|
|
574 String desktopName;
|
|
575 int framebufferWidth, framebufferHeight;
|
|
576 int bitsPerPixel, depth;
|
|
577 boolean bigEndian, trueColour;
|
|
578 int redMax, greenMax, blueMax, redShift, greenShift, blueShift;
|
0
|
579
|
4
|
580 void readServerInit() throws IOException {
|
8
|
581
|
4
|
582 framebufferWidth = readU16();
|
|
583 framebufferHeight = readU16();
|
|
584 bitsPerPixel = readU8();
|
|
585 depth = readU8();
|
|
586 bigEndian = (readU8() != 0);
|
|
587 trueColour = (readU8() != 0);
|
|
588 redMax = readU16();
|
|
589 greenMax = readU16();
|
|
590 blueMax = readU16();
|
|
591 redShift = readU8();
|
|
592 greenShift = readU8();
|
|
593 blueShift = readU8();
|
|
594 byte[] pad = new byte[3];
|
|
595 readFully(pad);
|
|
596 int nameLength = readU32();
|
|
597 byte[] name = new byte[nameLength];
|
|
598 readFully(name);
|
|
599 desktopName = new String(name);
|
0
|
600
|
4
|
601 // Read interaction capabilities (TightVNC protocol extensions)
|
|
602 if (protocolTightVNC) {
|
|
603 int nServerMessageTypes = readU16();
|
|
604 int nClientMessageTypes = readU16();
|
|
605 int nEncodingTypes = readU16();
|
|
606 readU16();
|
|
607 readCapabilityList(serverMsgCaps, nServerMessageTypes);
|
|
608 readCapabilityList(clientMsgCaps, nClientMessageTypes);
|
|
609 readCapabilityList(encodingCaps, nEncodingTypes);
|
|
610 }
|
0
|
611
|
4
|
612 inNormalProtocol = true;
|
|
613 }
|
0
|
614
|
4
|
615 //
|
|
616 // Create session file and write initial protocol messages into it.
|
|
617 //
|
0
|
618
|
4
|
619 void startSession(String fname) throws IOException {
|
|
620 rec = new SessionRecorder(fname);
|
|
621 rec.writeHeader();
|
|
622 rec.write(versionMsg_3_3.getBytes());
|
|
623 rec.writeIntBE(SecTypeNone);
|
|
624 rec.writeShortBE(framebufferWidth);
|
|
625 rec.writeShortBE(framebufferHeight);
|
|
626 byte[] fbsServerInitMsg = { 32, 24, 0, 1, 0, (byte) 0xFF, 0,
|
|
627 (byte) 0xFF, 0, (byte) 0xFF, 16, 8, 0, 0, 0, 0 };
|
|
628 rec.write(fbsServerInitMsg);
|
|
629 rec.writeIntBE(desktopName.length());
|
|
630 rec.write(desktopName.getBytes());
|
|
631 numUpdatesInSession = 0;
|
0
|
632
|
4
|
633 // FIXME: If there were e.g. ZRLE updates only, that should not
|
|
634 // affect recording of Zlib and Tight updates. So, actually
|
|
635 // we should maintain separate flags for Zlib, ZRLE and
|
|
636 // Tight, instead of one ``wereZlibUpdates'' variable.
|
|
637 //
|
|
638 if (wereZlibUpdates)
|
|
639 recordFromBeginning = false;
|
|
640
|
|
641 zlibWarningShown = false;
|
|
642 tightWarningShown = false;
|
|
643 }
|
|
644
|
|
645 //
|
|
646 // Close session file.
|
|
647 //
|
0
|
648
|
4
|
649 void closeSession() throws IOException {
|
|
650 if (rec != null) {
|
|
651 rec.close();
|
|
652 rec = null;
|
|
653 }
|
|
654 }
|
|
655
|
|
656 //
|
|
657 // Set new framebuffer size
|
|
658 //
|
0
|
659
|
4
|
660 void setFramebufferSize(int width, int height) {
|
|
661 framebufferWidth = width;
|
|
662 framebufferHeight = height;
|
|
663 }
|
0
|
664
|
4
|
665 //
|
|
666 // Read the server message type
|
|
667 //
|
|
668
|
|
669 int readServerMessageType() throws IOException {
|
|
670 int msgType = readU8();
|
0
|
671
|
4
|
672 // If the session is being recorded:
|
|
673 if (rec != null) {
|
|
674 if (msgType == Bell) { // Save Bell messages in session files.
|
|
675 rec.writeByte(msgType);
|
|
676 if (numUpdatesInSession > 0)
|
|
677 rec.flush();
|
|
678 }
|
|
679 }
|
0
|
680
|
4
|
681 return msgType;
|
|
682 }
|
|
683
|
|
684 //
|
|
685 // Read a FramebufferUpdate message
|
|
686 //
|
0
|
687
|
4
|
688 int updateNRects;
|
|
689
|
|
690 void readFramebufferUpdate() throws IOException {
|
|
691 skipBytes(1);
|
|
692 updateNRects = readU16();
|
|
693 // System.out.println(updateNRects);
|
0
|
694
|
4
|
695 // If the session is being recorded:
|
|
696 if (rec != null) {
|
|
697 rec.writeByte(FramebufferUpdate);
|
|
698 rec.writeByte(0);
|
|
699 rec.writeShortBE(updateNRects);
|
|
700 }
|
0
|
701
|
4
|
702 numUpdatesInSession++;
|
|
703 }
|
0
|
704
|
4
|
705 // Read a FramebufferUpdate rectangle header
|
0
|
706
|
4
|
707 int updateRectX, updateRectY, updateRectW, updateRectH, updateRectEncoding;
|
0
|
708
|
4
|
709 void readFramebufferUpdateRectHdr() throws Exception {
|
|
710 updateRectX = readU16();
|
|
711 updateRectY = readU16();
|
|
712 updateRectW = readU16();
|
|
713 updateRectH = readU16();
|
|
714 updateRectEncoding = readU32();
|
|
715 // System.out.println("readU16&32");
|
0
|
716
|
4
|
717 if (updateRectEncoding == EncodingZlib
|
|
718 || updateRectEncoding == EncodingZRLE
|
|
719 || updateRectEncoding == EncodingTight)
|
|
720 wereZlibUpdates = true;
|
0
|
721
|
4
|
722 // If the session is being recorded:
|
|
723 if (rec != null) {
|
|
724 if (numUpdatesInSession > 1)
|
|
725 rec.flush(); // Flush the output on each rectangle.
|
|
726 rec.writeShortBE(updateRectX);
|
|
727 rec.writeShortBE(updateRectY);
|
|
728 rec.writeShortBE(updateRectW);
|
|
729 rec.writeShortBE(updateRectH);
|
|
730 if (updateRectEncoding == EncodingZlib && !recordFromBeginning) {
|
|
731 // Here we cannot write Zlib-encoded rectangles because the
|
|
732 // decoder won't be able to reproduce zlib stream state.
|
|
733 if (!zlibWarningShown) {
|
|
734 System.out.println("Warning: Raw encoding will be used "
|
|
735 + "instead of Zlib in recorded session.");
|
|
736 zlibWarningShown = true;
|
|
737 }
|
|
738 rec.writeIntBE(EncodingRaw);
|
|
739 } else {
|
|
740 rec.writeIntBE(updateRectEncoding);
|
|
741 if (updateRectEncoding == EncodingTight && !recordFromBeginning
|
|
742 && !tightWarningShown) {
|
|
743 System.out.println("Warning: Re-compressing Tight-encoded "
|
|
744 + "updates for session recording.");
|
|
745 tightWarningShown = true;
|
|
746 }
|
|
747 }
|
|
748 }
|
0
|
749
|
4
|
750 if (updateRectEncoding < 0 || updateRectEncoding > MaxNormalEncoding)
|
|
751 return;
|
0
|
752
|
4
|
753 if (updateRectX + updateRectW > framebufferWidth
|
|
754 || updateRectY + updateRectH > framebufferHeight) {
|
|
755 throw new Exception("Framebuffer update rectangle too large: "
|
|
756 + updateRectW + "x" + updateRectH + " at (" + updateRectX
|
|
757 + "," + updateRectY + ")");
|
|
758 }
|
|
759 }
|
0
|
760
|
4
|
761 // Read CopyRect source X and Y.
|
|
762
|
|
763 int copyRectSrcX, copyRectSrcY;
|
|
764
|
|
765 void readCopyRect() throws IOException {
|
|
766 copyRectSrcX = readU16();
|
|
767 copyRectSrcY = readU16();
|
0
|
768
|
4
|
769 // If the session is being recorded:
|
|
770 if (rec != null) {
|
|
771 rec.writeShortBE(copyRectSrcX);
|
|
772 rec.writeShortBE(copyRectSrcY);
|
|
773 }
|
0
|
774 }
|
4
|
775
|
|
776 //
|
|
777 // Read a ServerCutText message
|
|
778 //
|
|
779
|
|
780 String readServerCutText() throws IOException {
|
|
781 skipBytes(3);
|
|
782 int len = readU32();
|
|
783 byte[] text = new byte[len];
|
|
784 readFully(text);
|
|
785 return new String(text);
|
0
|
786 }
|
4
|
787
|
|
788 //
|
|
789 // Read an integer in compact representation (1..3 bytes).
|
|
790 // Such format is used as a part of the Tight encoding.
|
|
791 // Also, this method records data if session recording is active and
|
|
792 // the viewer's recordFromBeginning variable is set to true.
|
|
793 //
|
0
|
794
|
4
|
795 int readCompactLen() throws IOException {
|
|
796 int[] portion = new int[3];
|
|
797 portion[0] = readU8();
|
|
798 int byteCount = 1;
|
|
799 int len = portion[0] & 0x7F;
|
|
800 if ((portion[0] & 0x80) != 0) {
|
|
801 portion[1] = readU8();
|
|
802 byteCount++;
|
|
803 len |= (portion[1] & 0x7F) << 7;
|
|
804 if ((portion[1] & 0x80) != 0) {
|
|
805 portion[2] = readU8();
|
|
806 byteCount++;
|
|
807 len |= (portion[2] & 0xFF) << 14;
|
|
808 }
|
|
809 }
|
0
|
810
|
4
|
811 if (rec != null && recordFromBeginning)
|
|
812 for (int i = 0; i < byteCount; i++)
|
|
813 rec.writeByte(portion[i]);
|
0
|
814
|
4
|
815 return len;
|
|
816 }
|
0
|
817
|
4
|
818 //
|
|
819 // Write a FramebufferUpdateRequest message
|
|
820 //
|
|
821
|
|
822 void writeFramebufferUpdateRequest(int x, int y, int w, int h,
|
|
823 boolean incremental) throws IOException {
|
|
824 byte[] b = new byte[10];
|
0
|
825
|
4
|
826 b[0] = (byte) FramebufferUpdateRequest;
|
|
827 b[1] = (byte) (incremental ? 1 : 0);
|
|
828 b[2] = (byte) ((x >> 8) & 0xff);
|
|
829 b[3] = (byte) (x & 0xff);
|
|
830 b[4] = (byte) ((y >> 8) & 0xff);
|
|
831 b[5] = (byte) (y & 0xff);
|
|
832 b[6] = (byte) ((w >> 8) & 0xff);
|
|
833 b[7] = (byte) (w & 0xff);
|
|
834 b[8] = (byte) ((h >> 8) & 0xff);
|
|
835 b[9] = (byte) (h & 0xff);
|
0
|
836
|
4
|
837 os.write(b);
|
|
838 }
|
|
839
|
|
840 //
|
|
841 // Write a SetPixelFormat message
|
|
842 //
|
|
843
|
|
844 void writeSetPixelFormat(int bitsPerPixel, int depth, boolean bigEndian,
|
|
845 boolean trueColour, int redMax, int greenMax, int blueMax,
|
|
846 int redShift, int greenShift, int blueShift) throws IOException {
|
|
847 byte[] b = new byte[20];
|
0
|
848
|
4
|
849 b[0] = (byte) SetPixelFormat;
|
|
850 b[4] = (byte) bitsPerPixel;
|
|
851 b[5] = (byte) depth;
|
|
852 b[6] = (byte) (bigEndian ? 1 : 0);
|
|
853 b[7] = (byte) (trueColour ? 1 : 0);
|
|
854 b[8] = (byte) ((redMax >> 8) & 0xff);
|
|
855 b[9] = (byte) (redMax & 0xff);
|
|
856 b[10] = (byte) ((greenMax >> 8) & 0xff);
|
|
857 b[11] = (byte) (greenMax & 0xff);
|
|
858 b[12] = (byte) ((blueMax >> 8) & 0xff);
|
|
859 b[13] = (byte) (blueMax & 0xff);
|
|
860 b[14] = (byte) redShift;
|
|
861 b[15] = (byte) greenShift;
|
|
862 b[16] = (byte) blueShift;
|
0
|
863
|
4
|
864 os.write(b);
|
|
865 }
|
0
|
866
|
4
|
867 //
|
|
868 // Write a FixColourMapEntries message. The values in the red, green and
|
|
869 // blue arrays are from 0 to 65535.
|
|
870 //
|
0
|
871
|
4
|
872 void writeFixColourMapEntries(int firstColour, int nColours, int[] red,
|
|
873 int[] green, int[] blue) throws IOException {
|
|
874 byte[] b = new byte[6 + nColours * 6];
|
0
|
875
|
4
|
876 b[0] = (byte) FixColourMapEntries;
|
|
877 b[2] = (byte) ((firstColour >> 8) & 0xff);
|
|
878 b[3] = (byte) (firstColour & 0xff);
|
|
879 b[4] = (byte) ((nColours >> 8) & 0xff);
|
|
880 b[5] = (byte) (nColours & 0xff);
|
0
|
881
|
4
|
882 for (int i = 0; i < nColours; i++) {
|
|
883 b[6 + i * 6] = (byte) ((red[i] >> 8) & 0xff);
|
|
884 b[6 + i * 6 + 1] = (byte) (red[i] & 0xff);
|
|
885 b[6 + i * 6 + 2] = (byte) ((green[i] >> 8) & 0xff);
|
|
886 b[6 + i * 6 + 3] = (byte) (green[i] & 0xff);
|
|
887 b[6 + i * 6 + 4] = (byte) ((blue[i] >> 8) & 0xff);
|
|
888 b[6 + i * 6 + 5] = (byte) (blue[i] & 0xff);
|
|
889 }
|
0
|
890
|
4
|
891 os.write(b);
|
|
892 }
|
0
|
893
|
4
|
894 //
|
|
895 // Write a SetEncodings message
|
|
896 //
|
|
897
|
|
898 void writeSetEncodings(int[] encs, int len) throws IOException {
|
|
899 byte[] b = new byte[4 + 4 * len];
|
0
|
900
|
4
|
901 b[0] = (byte) SetEncodings;
|
|
902 b[2] = (byte) ((len >> 8) & 0xff);
|
|
903 b[3] = (byte) (len & 0xff);
|
0
|
904
|
4
|
905 for (int i = 0; i < len; i++) {
|
|
906 b[4 + 4 * i] = (byte) ((encs[i] >> 24) & 0xff);
|
|
907 b[5 + 4 * i] = (byte) ((encs[i] >> 16) & 0xff);
|
|
908 b[6 + 4 * i] = (byte) ((encs[i] >> 8) & 0xff);
|
|
909 b[7 + 4 * i] = (byte) (encs[i] & 0xff);
|
|
910 }
|
|
911
|
|
912 os.write(b);
|
|
913 }
|
|
914
|
|
915 //
|
|
916 // Write a ClientCutText message
|
|
917 //
|
|
918
|
|
919 void writeClientCutText(String text) throws IOException {
|
|
920 byte[] b = new byte[8 + text.length()];
|
0
|
921
|
4
|
922 b[0] = (byte) ClientCutText;
|
|
923 b[4] = (byte) ((text.length() >> 24) & 0xff);
|
|
924 b[5] = (byte) ((text.length() >> 16) & 0xff);
|
|
925 b[6] = (byte) ((text.length() >> 8) & 0xff);
|
|
926 b[7] = (byte) (text.length() & 0xff);
|
0
|
927
|
4
|
928 System.arraycopy(text.getBytes(), 0, b, 8, text.length());
|
|
929
|
|
930 os.write(b);
|
|
931 }
|
0
|
932
|
4
|
933 //
|
|
934 // A buffer for putting pointer and keyboard events before being sent. This
|
|
935 // is to ensure that multiple RFB events generated from a single Java Event
|
|
936 // will all be sent in a single network packet. The maximum possible
|
|
937 // length is 4 modifier down events, a single key event followed by 4
|
|
938 // modifier up events i.e. 9 key events or 72 bytes.
|
|
939 //
|
0
|
940
|
4
|
941 byte[] eventBuf = new byte[72];
|
|
942 int eventBufLen;
|
0
|
943
|
4
|
944 // Useful shortcuts for modifier masks.
|
|
945
|
|
946 final static int CTRL_MASK = InputEvent.CTRL_MASK;
|
|
947 final static int SHIFT_MASK = InputEvent.SHIFT_MASK;
|
|
948 final static int META_MASK = InputEvent.META_MASK;
|
|
949 final static int ALT_MASK = InputEvent.ALT_MASK;
|
|
950
|
|
951 //
|
|
952 // Write a pointer event message. We may need to send modifier key events
|
|
953 // around it to set the correct modifier state.
|
|
954 //
|
0
|
955
|
4
|
956 int pointerMask = 0;
|
0
|
957
|
4
|
958 void writePointerEvent(MouseEvent evt) throws IOException {
|
|
959 int modifiers = evt.getModifiers();
|
0
|
960
|
4
|
961 int mask2 = 2;
|
|
962 int mask3 = 4;
|
|
963 if (viewer.options.reverseMouseButtons2And3) {
|
|
964 mask2 = 4;
|
|
965 mask3 = 2;
|
|
966 }
|
0
|
967
|
4
|
968 // Note: For some reason, AWT does not set BUTTON1_MASK on left
|
|
969 // button presses. Here we think that it was the left button if
|
|
970 // modifiers do not include BUTTON2_MASK or BUTTON3_MASK.
|
0
|
971
|
4
|
972 if (evt.getID() == MouseEvent.MOUSE_PRESSED) {
|
|
973 if ((modifiers & InputEvent.BUTTON2_MASK) != 0) {
|
|
974 pointerMask = mask2;
|
|
975 modifiers &= ~ALT_MASK;
|
|
976 } else if ((modifiers & InputEvent.BUTTON3_MASK) != 0) {
|
|
977 pointerMask = mask3;
|
|
978 modifiers &= ~META_MASK;
|
|
979 } else {
|
|
980 pointerMask = 1;
|
|
981 }
|
|
982 } else if (evt.getID() == MouseEvent.MOUSE_RELEASED) {
|
|
983 pointerMask = 0;
|
|
984 if ((modifiers & InputEvent.BUTTON2_MASK) != 0) {
|
|
985 modifiers &= ~ALT_MASK;
|
|
986 } else if ((modifiers & InputEvent.BUTTON3_MASK) != 0) {
|
|
987 modifiers &= ~META_MASK;
|
|
988 }
|
|
989 }
|
0
|
990
|
4
|
991 eventBufLen = 0;
|
|
992 writeModifierKeyEvents(modifiers);
|
|
993
|
|
994 int x = evt.getX();
|
|
995 int y = evt.getY();
|
0
|
996
|
4
|
997 if (x < 0)
|
|
998 x = 0;
|
|
999 if (y < 0)
|
|
1000 y = 0;
|
0
|
1001
|
4
|
1002 eventBuf[eventBufLen++] = (byte) PointerEvent;
|
|
1003 eventBuf[eventBufLen++] = (byte) pointerMask;
|
|
1004 eventBuf[eventBufLen++] = (byte) ((x >> 8) & 0xff);
|
|
1005 eventBuf[eventBufLen++] = (byte) (x & 0xff);
|
|
1006 eventBuf[eventBufLen++] = (byte) ((y >> 8) & 0xff);
|
|
1007 eventBuf[eventBufLen++] = (byte) (y & 0xff);
|
0
|
1008
|
4
|
1009 //
|
|
1010 // Always release all modifiers after an "up" event
|
|
1011 //
|
0
|
1012
|
4
|
1013 if (pointerMask == 0) {
|
|
1014 writeModifierKeyEvents(0);
|
|
1015 }
|
0
|
1016
|
4
|
1017 os.write(eventBuf, 0, eventBufLen);
|
|
1018 }
|
0
|
1019
|
4
|
1020 //
|
|
1021 // Write a key event message. We may need to send modifier key events
|
|
1022 // around it to set the correct modifier state. Also we need to translate
|
|
1023 // from the Java key values to the X keysym values used by the RFB protocol.
|
|
1024 //
|
|
1025
|
|
1026 void writeKeyEvent(KeyEvent evt) throws IOException {
|
|
1027
|
|
1028 int keyChar = evt.getKeyChar();
|
0
|
1029
|
4
|
1030 //
|
|
1031 // Ignore event if only modifiers were pressed.
|
|
1032 //
|
|
1033
|
|
1034 // Some JVMs return 0 instead of CHAR_UNDEFINED in getKeyChar().
|
|
1035 if (keyChar == 0)
|
|
1036 keyChar = KeyEvent.CHAR_UNDEFINED;
|
0
|
1037
|
4
|
1038 if (keyChar == KeyEvent.CHAR_UNDEFINED) {
|
|
1039 int code = evt.getKeyCode();
|
|
1040 if (code == KeyEvent.VK_CONTROL || code == KeyEvent.VK_SHIFT
|
|
1041 || code == KeyEvent.VK_META || code == KeyEvent.VK_ALT)
|
|
1042 return;
|
|
1043 }
|
0
|
1044
|
4
|
1045 //
|
|
1046 // Key press or key release?
|
|
1047 //
|
|
1048
|
|
1049 boolean down = (evt.getID() == KeyEvent.KEY_PRESSED);
|
|
1050
|
|
1051 int key;
|
|
1052 if (evt.isActionKey()) {
|
0
|
1053
|
4
|
1054 //
|
|
1055 // An action key should be one of the following.
|
|
1056 // If not then just ignore the event.
|
|
1057 //
|
0
|
1058
|
4
|
1059 switch (evt.getKeyCode()) {
|
|
1060 case KeyEvent.VK_HOME:
|
|
1061 key = 0xff50;
|
|
1062 break;
|
|
1063 case KeyEvent.VK_LEFT:
|
|
1064 key = 0xff51;
|
|
1065 break;
|
|
1066 case KeyEvent.VK_UP:
|
|
1067 key = 0xff52;
|
|
1068 break;
|
|
1069 case KeyEvent.VK_RIGHT:
|
|
1070 key = 0xff53;
|
|
1071 break;
|
|
1072 case KeyEvent.VK_DOWN:
|
|
1073 key = 0xff54;
|
|
1074 break;
|
|
1075 case KeyEvent.VK_PAGE_UP:
|
|
1076 key = 0xff55;
|
|
1077 break;
|
|
1078 case KeyEvent.VK_PAGE_DOWN:
|
|
1079 key = 0xff56;
|
|
1080 break;
|
|
1081 case KeyEvent.VK_END:
|
|
1082 key = 0xff57;
|
|
1083 break;
|
|
1084 case KeyEvent.VK_INSERT:
|
|
1085 key = 0xff63;
|
|
1086 break;
|
|
1087 case KeyEvent.VK_F1:
|
|
1088 key = 0xffbe;
|
|
1089 break;
|
|
1090 case KeyEvent.VK_F2:
|
|
1091 key = 0xffbf;
|
|
1092 break;
|
|
1093 case KeyEvent.VK_F3:
|
|
1094 key = 0xffc0;
|
|
1095 break;
|
|
1096 case KeyEvent.VK_F4:
|
|
1097 key = 0xffc1;
|
|
1098 break;
|
|
1099 case KeyEvent.VK_F5:
|
|
1100 key = 0xffc2;
|
|
1101 break;
|
|
1102 case KeyEvent.VK_F6:
|
|
1103 key = 0xffc3;
|
|
1104 break;
|
|
1105 case KeyEvent.VK_F7:
|
|
1106 key = 0xffc4;
|
|
1107 break;
|
|
1108 case KeyEvent.VK_F8:
|
|
1109 key = 0xffc5;
|
|
1110 break;
|
|
1111 case KeyEvent.VK_F9:
|
|
1112 key = 0xffc6;
|
|
1113 break;
|
|
1114 case KeyEvent.VK_F10:
|
|
1115 key = 0xffc7;
|
|
1116 break;
|
|
1117 case KeyEvent.VK_F11:
|
|
1118 key = 0xffc8;
|
|
1119 break;
|
|
1120 case KeyEvent.VK_F12:
|
|
1121 key = 0xffc9;
|
|
1122 break;
|
|
1123 default:
|
|
1124 return;
|
|
1125 }
|
0
|
1126
|
4
|
1127 } else {
|
0
|
1128
|
4
|
1129 //
|
|
1130 // A "normal" key press. Ordinary ASCII characters go straight
|
|
1131 // through.
|
|
1132 // For CTRL-<letter>, CTRL is sent separately so just send <letter>.
|
|
1133 // Backspace, tab, return, escape and delete have special keysyms.
|
|
1134 // Anything else we ignore.
|
|
1135 //
|
0
|
1136
|
4
|
1137 key = keyChar;
|
0
|
1138
|
4
|
1139 if (key < 0x20) {
|
|
1140 if (evt.isControlDown()) {
|
|
1141 key += 0x60;
|
|
1142 } else {
|
|
1143 switch (key) {
|
|
1144 case KeyEvent.VK_BACK_SPACE:
|
|
1145 key = 0xff08;
|
|
1146 break;
|
|
1147 case KeyEvent.VK_TAB:
|
|
1148 key = 0xff09;
|
|
1149 break;
|
|
1150 case KeyEvent.VK_ENTER:
|
|
1151 key = 0xff0d;
|
|
1152 break;
|
|
1153 case KeyEvent.VK_ESCAPE:
|
|
1154 key = 0xff1b;
|
|
1155 break;
|
|
1156 }
|
|
1157 }
|
|
1158 } else if (key == 0x7f) {
|
|
1159 // Delete
|
|
1160 key = 0xffff;
|
|
1161 } else if (key > 0xff) {
|
|
1162 // JDK1.1 on X incorrectly passes some keysyms straight through,
|
|
1163 // so we do too. JDK1.1.4 seems to have fixed this.
|
|
1164 // The keysyms passed are 0xff00 .. XK_BackSpace .. XK_Delete
|
|
1165 // Also, we pass through foreign currency keysyms
|
|
1166 // (0x20a0..0x20af).
|
|
1167 if ((key < 0xff00 || key > 0xffff)
|
|
1168 && !(key >= 0x20a0 && key <= 0x20af))
|
|
1169 return;
|
|
1170 }
|
|
1171 }
|
0
|
1172
|
4
|
1173 // Fake keyPresses for keys that only generates keyRelease events
|
|
1174 if ((key == 0xe5) || (key == 0xc5) || // XK_aring / XK_Aring
|
|
1175 (key == 0xe4) || (key == 0xc4) || // XK_adiaeresis /
|
|
1176 // XK_Adiaeresis
|
|
1177 (key == 0xf6) || (key == 0xd6) || // XK_odiaeresis /
|
|
1178 // XK_Odiaeresis
|
|
1179 (key == 0xa7) || (key == 0xbd) || // XK_section / XK_onehalf
|
|
1180 (key == 0xa3)) { // XK_sterling
|
|
1181 // Make sure we do not send keypress events twice on platforms
|
|
1182 // with correct JVMs (those that actually report KeyPress for all
|
|
1183 // keys)
|
|
1184 if (down)
|
|
1185 brokenKeyPressed = true;
|
0
|
1186
|
4
|
1187 if (!down && !brokenKeyPressed) {
|
|
1188 // We've got a release event for this key, but haven't received
|
|
1189 // a press. Fake it.
|
|
1190 eventBufLen = 0;
|
|
1191 writeModifierKeyEvents(evt.getModifiers());
|
|
1192 writeKeyEvent(key, true);
|
|
1193 os.write(eventBuf, 0, eventBufLen);
|
|
1194 }
|
0
|
1195
|
4
|
1196 if (!down)
|
|
1197 brokenKeyPressed = false;
|
|
1198 }
|
0
|
1199
|
4
|
1200 eventBufLen = 0;
|
|
1201 writeModifierKeyEvents(evt.getModifiers());
|
|
1202 writeKeyEvent(key, down);
|
0
|
1203
|
4
|
1204 // Always release all modifiers after an "up" event
|
|
1205 if (!down)
|
|
1206 writeModifierKeyEvents(0);
|
0
|
1207
|
4
|
1208 os.write(eventBuf, 0, eventBufLen);
|
|
1209 }
|
|
1210
|
|
1211 //
|
|
1212 // Add a raw key event with the given X keysym to eventBuf.
|
|
1213 //
|
0
|
1214
|
4
|
1215 void writeKeyEvent(int keysym, boolean down) {
|
|
1216 eventBuf[eventBufLen++] = (byte) KeyboardEvent;
|
|
1217 eventBuf[eventBufLen++] = (byte) (down ? 1 : 0);
|
|
1218 eventBuf[eventBufLen++] = (byte) 0;
|
|
1219 eventBuf[eventBufLen++] = (byte) 0;
|
|
1220 eventBuf[eventBufLen++] = (byte) ((keysym >> 24) & 0xff);
|
|
1221 eventBuf[eventBufLen++] = (byte) ((keysym >> 16) & 0xff);
|
|
1222 eventBuf[eventBufLen++] = (byte) ((keysym >> 8) & 0xff);
|
|
1223 eventBuf[eventBufLen++] = (byte) (keysym & 0xff);
|
|
1224 }
|
0
|
1225
|
4
|
1226 //
|
|
1227 // Write key events to set the correct modifier state.
|
|
1228 //
|
0
|
1229
|
4
|
1230 int oldModifiers = 0;
|
0
|
1231
|
4
|
1232 void writeModifierKeyEvents(int newModifiers) {
|
|
1233 if ((newModifiers & CTRL_MASK) != (oldModifiers & CTRL_MASK))
|
|
1234 writeKeyEvent(0xffe3, (newModifiers & CTRL_MASK) != 0);
|
0
|
1235
|
4
|
1236 if ((newModifiers & SHIFT_MASK) != (oldModifiers & SHIFT_MASK))
|
|
1237 writeKeyEvent(0xffe1, (newModifiers & SHIFT_MASK) != 0);
|
0
|
1238
|
4
|
1239 if ((newModifiers & META_MASK) != (oldModifiers & META_MASK))
|
|
1240 writeKeyEvent(0xffe7, (newModifiers & META_MASK) != 0);
|
0
|
1241
|
4
|
1242 if ((newModifiers & ALT_MASK) != (oldModifiers & ALT_MASK))
|
|
1243 writeKeyEvent(0xffe9, (newModifiers & ALT_MASK) != 0);
|
0
|
1244
|
4
|
1245 oldModifiers = newModifiers;
|
|
1246 }
|
0
|
1247
|
4
|
1248 //
|
|
1249 // Compress and write the data into the recorded session file. This
|
|
1250 // method assumes the recording is on (rec != null).
|
|
1251 //
|
0
|
1252
|
4
|
1253 void recordCompressedData(byte[] data, int off, int len) throws IOException {
|
|
1254 Deflater deflater = new Deflater();
|
|
1255 deflater.setInput(data, off, len);
|
|
1256 int bufSize = len + len / 100 + 12;
|
|
1257 byte[] buf = new byte[bufSize];
|
|
1258 deflater.finish();
|
|
1259 int compressedSize = deflater.deflate(buf);
|
|
1260 recordCompactLen(compressedSize);
|
|
1261 rec.write(buf, 0, compressedSize);
|
|
1262 }
|
0
|
1263
|
4
|
1264 void recordCompressedData(byte[] data) throws IOException {
|
|
1265 recordCompressedData(data, 0, data.length);
|
|
1266 }
|
0
|
1267
|
4
|
1268 //
|
|
1269 // Write an integer in compact representation (1..3 bytes) into the
|
|
1270 // recorded session file. This method assumes the recording is on
|
|
1271 // (rec != null).
|
|
1272 //
|
0
|
1273
|
4
|
1274 void recordCompactLen(int len) throws IOException {
|
|
1275 byte[] buf = new byte[3];
|
|
1276 int bytes = 0;
|
|
1277 buf[bytes++] = (byte) (len & 0x7F);
|
|
1278 if (len > 0x7F) {
|
|
1279 buf[bytes - 1] |= 0x80;
|
|
1280 buf[bytes++] = (byte) (len >> 7 & 0x7F);
|
|
1281 if (len > 0x3FFF) {
|
|
1282 buf[bytes - 1] |= 0x80;
|
|
1283 buf[bytes++] = (byte) (len >> 14 & 0xFF);
|
|
1284 }
|
|
1285 }
|
|
1286 rec.write(buf, 0, bytes);
|
|
1287 }
|
0
|
1288
|
4
|
1289 public void startTiming() {
|
|
1290 timing = true;
|
0
|
1291
|
4
|
1292 // Carry over up to 1s worth of previous rate for smoothing.
|
0
|
1293
|
4
|
1294 if (timeWaitedIn100us > 10000) {
|
|
1295 timedKbits = timedKbits * 10000 / timeWaitedIn100us;
|
|
1296 timeWaitedIn100us = 10000;
|
|
1297 }
|
|
1298 }
|
0
|
1299
|
4
|
1300 public void stopTiming() {
|
|
1301 timing = false;
|
|
1302 if (timeWaitedIn100us < timedKbits / 2)
|
|
1303 timeWaitedIn100us = timedKbits / 2; // upper limit 20Mbit/s
|
|
1304 }
|
0
|
1305
|
4
|
1306 public long kbitsPerSecond() {
|
|
1307 return timedKbits * 10000 / timeWaitedIn100us;
|
|
1308 }
|
0
|
1309
|
4
|
1310 public long timeWaited() {
|
|
1311 return timeWaitedIn100us;
|
|
1312 }
|
0
|
1313
|
4
|
1314 //
|
|
1315 // Methods for reading data via our DataInputStream member variable (is).
|
|
1316 //
|
|
1317 // In addition to reading data, the readFully() methods updates variables
|
|
1318 // used to estimate data throughput.
|
|
1319 //
|
0
|
1320
|
4
|
1321 public void readFully(byte b[]) throws IOException {
|
|
1322 readFully(b, 0, b.length);
|
|
1323 }
|
0
|
1324
|
4
|
1325 public void readFully(byte b[], int off, int len) throws IOException {
|
|
1326 long before = 0;
|
|
1327 if (timing)
|
|
1328 before = System.currentTimeMillis();
|
|
1329
|
|
1330 is.readFully(b, off, len);
|
0
|
1331
|
4
|
1332 if (timing) {
|
|
1333 long after = System.currentTimeMillis();
|
|
1334 long newTimeWaited = (after - before) * 10;
|
|
1335 int newKbits = len * 8 / 1000;
|
0
|
1336
|
4
|
1337 // limit rate to between 10kbit/s and 40Mbit/s
|
0
|
1338
|
4
|
1339 if (newTimeWaited > newKbits * 1000)
|
|
1340 newTimeWaited = newKbits * 1000;
|
|
1341 if (newTimeWaited < newKbits / 4)
|
|
1342 newTimeWaited = newKbits / 4;
|
0
|
1343
|
4
|
1344 timeWaitedIn100us += newTimeWaited;
|
|
1345 timedKbits += newKbits;
|
|
1346 }
|
0
|
1347
|
4
|
1348 numBytesRead += len;
|
|
1349 }
|
0
|
1350
|
4
|
1351 final int available() throws IOException {
|
|
1352 return is.available();
|
|
1353 }
|
0
|
1354
|
4
|
1355 // FIXME: DataInputStream::skipBytes() is not guaranteed to skip
|
|
1356 // exactly n bytes. Probably we don't want to use this method.
|
|
1357 final int skipBytes(int n) throws IOException {
|
|
1358 int r = is.skipBytes(n);
|
|
1359 numBytesRead += r;
|
|
1360 return r;
|
|
1361 }
|
0
|
1362
|
4
|
1363 final int readU8() throws IOException {
|
|
1364 int r = is.readUnsignedByte();
|
|
1365 numBytesRead++;
|
0
|
1366
|
4
|
1367 return r;
|
|
1368 }
|
0
|
1369
|
4
|
1370 final int readU16() throws IOException {
|
|
1371 int r = is.readUnsignedShort();
|
|
1372 numBytesRead += 2;
|
|
1373 return r;
|
|
1374 }
|
0
|
1375
|
4
|
1376 final int readU32() throws IOException {
|
|
1377 int r = is.readInt();
|
|
1378 numBytesRead += 4;
|
|
1379 return r;
|
|
1380 }
|
0
|
1381 }
|