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