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