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