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