annotate src/RfbProto.java @ 4:10dbc0e61adf

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