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