2017-01-16 20:59:50 +01:00
|
|
|
|
/* Copyright (c) Citrix Systems, Inc.
|
2013-06-24 13:41:48 +02:00
|
|
|
|
* All rights reserved.
|
|
|
|
|
*
|
|
|
|
|
* Redistribution and use in source and binary forms,
|
|
|
|
|
* with or without modification, are permitted provided
|
|
|
|
|
* that the following conditions are met:
|
|
|
|
|
*
|
|
|
|
|
* * Redistributions of source code must retain the above
|
|
|
|
|
* copyright notice, this list of conditions and the
|
|
|
|
|
* following disclaimer.
|
|
|
|
|
* * Redistributions in binary form must reproduce the above
|
|
|
|
|
* copyright notice, this list of conditions and the
|
|
|
|
|
* following disclaimer in the documentation and/or other
|
|
|
|
|
* materials provided with the distribution.
|
|
|
|
|
*
|
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
|
|
|
|
|
* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
|
|
|
|
|
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
|
|
|
|
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
|
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
|
|
|
|
|
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
|
|
|
|
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
|
|
|
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
|
|
|
|
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
|
|
|
|
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
using System;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Drawing;
|
|
|
|
|
using System.Drawing.Imaging;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Text.RegularExpressions;
|
|
|
|
|
using System.Runtime.InteropServices;
|
|
|
|
|
using System.Security.Cryptography;
|
2017-11-17 02:04:45 +01:00
|
|
|
|
using XenCenterLib;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
|
|
|
|
namespace DotNetVnc
|
|
|
|
|
{
|
|
|
|
|
public class VNCStream
|
|
|
|
|
{
|
|
|
|
|
private const int RAW_ENCODING = 0;
|
|
|
|
|
private const int COPY_RECTANGLE_ENCODING = 1;
|
|
|
|
|
private const int RRE_ENCODING = 2;
|
|
|
|
|
private const int CORRE_ENCODING = 4;
|
|
|
|
|
private const int HEXTILE_ENCODING = 5;
|
|
|
|
|
private const int CURSOR_PSEUDO_ENCODING = -239;
|
|
|
|
|
private const int DESKTOP_SIZE_PSEUDO_ENCODING = -223;
|
|
|
|
|
private const int XENCENTER_ENCODING = -254;
|
2017-05-02 18:17:30 +02:00
|
|
|
|
private const int QEMU_EXT_KEY_ENCODING = -258;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
|
|
|
|
private const int SET_PIXEL_FORMAT = 0;
|
|
|
|
|
private const int SET_ENCODINGS = 2;
|
|
|
|
|
private const int FRAMEBUFFER_UPDATE_REQUEST = 3;
|
|
|
|
|
private const int KEY_EVENT = 4;
|
|
|
|
|
private const int KEY_SCAN_EVENT = 254;
|
2017-05-02 18:17:30 +02:00
|
|
|
|
private const int QEMU_MSG = 255;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
private const int POINTER_EVENT = 5;
|
|
|
|
|
private const int CLIENT_CUT_TEXT = 6;
|
|
|
|
|
|
|
|
|
|
private const int RAW_SUBENCODING = 1;
|
|
|
|
|
private const int BACKGROUND_SPECIFIED_SUBENCODING = 2;
|
|
|
|
|
private const int FOREGROUND_SPECIFIED_SUBENCODING = 4;
|
|
|
|
|
private const int ANY_SUBRECTS_SUBENCODING = 8;
|
|
|
|
|
private const int SUBRECTS_COLORED_SUBENCODING = 16;
|
|
|
|
|
|
|
|
|
|
private const int FRAME_BUFFER_UPDATE = 0;
|
|
|
|
|
private const int BELL = 2;
|
|
|
|
|
private const int SERVER_CUT_TEXT = 3;
|
|
|
|
|
|
2017-05-02 18:17:30 +02:00
|
|
|
|
private const int QEMU_EXT_KEY_EVENT = 0;
|
|
|
|
|
|
2013-06-24 13:41:48 +02:00
|
|
|
|
private static readonly log4net.ILog Log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
|
|
|
|
|
|
2022-04-07 14:51:09 +02:00
|
|
|
|
private Thread thread;
|
|
|
|
|
|
|
|
|
|
#region Current color properties
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
|
|
|
|
private int bitsPerPixel;
|
|
|
|
|
private int bytesPerPixel;
|
|
|
|
|
private int depth;
|
|
|
|
|
private bool bigEndian;
|
|
|
|
|
private bool trueColor;
|
|
|
|
|
private int redMax;
|
|
|
|
|
private int greenMax;
|
|
|
|
|
private int blueMax;
|
|
|
|
|
private int redMaxPlus1;
|
|
|
|
|
private int greenMaxPlus1;
|
|
|
|
|
private int blueMaxPlus1;
|
|
|
|
|
private int redMaxOver2;
|
|
|
|
|
private int greenMaxOver2;
|
|
|
|
|
private int blueMaxOver2;
|
|
|
|
|
private int redShift;
|
|
|
|
|
private int greenShift;
|
|
|
|
|
private int blueShift;
|
|
|
|
|
private bool rgb565;
|
|
|
|
|
|
2022-04-07 14:51:09 +02:00
|
|
|
|
#endregion
|
|
|
|
|
|
2013-06-24 13:41:48 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// This event will be fired when an error occurs. The helper thread is guaranteed to be
|
|
|
|
|
/// closing down at this point.
|
|
|
|
|
/// </summary>
|
2022-04-07 14:51:09 +02:00
|
|
|
|
public event Action<object,Exception> ErrorOccurred;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
2022-04-07 14:51:09 +02:00
|
|
|
|
public event EventHandler ConnectionSuccess;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
2022-04-07 14:51:09 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// The encodings used. Note that these are ordered: preferred encoding first.
|
|
|
|
|
/// </summary>
|
|
|
|
|
private static readonly int[] encodings = {
|
2013-06-24 13:41:48 +02:00
|
|
|
|
CORRE_ENCODING,
|
|
|
|
|
RRE_ENCODING,
|
|
|
|
|
COPY_RECTANGLE_ENCODING,
|
|
|
|
|
RAW_ENCODING,
|
|
|
|
|
CURSOR_PSEUDO_ENCODING,
|
|
|
|
|
DESKTOP_SIZE_PSEUDO_ENCODING,
|
2017-05-02 18:17:30 +02:00
|
|
|
|
XENCENTER_ENCODING,
|
|
|
|
|
QEMU_EXT_KEY_ENCODING
|
2013-06-24 13:41:48 +02:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
private readonly IVNCGraphicsClient client;
|
|
|
|
|
|
|
|
|
|
private readonly MyStream stream;
|
|
|
|
|
|
2022-04-07 14:51:09 +02:00
|
|
|
|
private readonly object writeLock = new object();
|
|
|
|
|
private readonly object pauseMonitor = new object();
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
|
|
|
|
private volatile bool running = true;
|
2022-04-07 14:51:09 +02:00
|
|
|
|
private bool paused;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
2022-04-07 14:51:09 +02:00
|
|
|
|
private int _width;
|
|
|
|
|
private int _height;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
2022-04-07 14:51:09 +02:00
|
|
|
|
private bool _incremental;
|
|
|
|
|
private bool qemu_ext_key_encoding;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
|
|
|
|
private PixelFormat pixelFormat;
|
|
|
|
|
private PixelFormat pixelFormatCursor;
|
|
|
|
|
|
2022-04-07 14:51:09 +02:00
|
|
|
|
private byte[] _data = new byte[1228800]; //640*480*32bpp
|
|
|
|
|
private byte[] data_8bpp;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
|
|
|
|
private readonly long imageUpdateThreshold;
|
|
|
|
|
|
2022-04-07 14:51:09 +02:00
|
|
|
|
public readonly object updateMonitor = new object();
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
|
|
|
|
public VNCStream(IVNCGraphicsClient client, Stream stream, bool startPaused)
|
|
|
|
|
{
|
|
|
|
|
this.client = client;
|
|
|
|
|
this.stream = new MyStream(stream);
|
2022-04-07 14:51:09 +02:00
|
|
|
|
paused = startPaused;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
2022-04-07 14:51:09 +02:00
|
|
|
|
if (!Win32.QueryPerformanceFrequency(out var freq))
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
|
|
|
|
System.Diagnostics.Trace.Assert(false);
|
|
|
|
|
}
|
|
|
|
|
imageUpdateThreshold = freq / 3;
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-07 14:51:09 +02:00
|
|
|
|
public void Connect(char[] password)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
|
|
|
|
System.Diagnostics.Trace.Assert(thread == null);
|
|
|
|
|
|
2022-04-07 14:51:09 +02:00
|
|
|
|
thread = new Thread(Run)
|
|
|
|
|
{
|
|
|
|
|
Name = $"VNC connection to {client.VmName} - {client.UUID}",
|
|
|
|
|
IsBackground = true
|
|
|
|
|
};
|
2013-06-24 13:41:48 +02:00
|
|
|
|
thread.Start(password);
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-19 04:59:31 +02:00
|
|
|
|
private void CheckProtocolVersion()
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
|
|
|
|
byte[] buffer = new byte[12];
|
2022-04-07 14:51:09 +02:00
|
|
|
|
stream.readFully(buffer, 0, 12);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
char[] chars = new char[12];
|
|
|
|
|
Encoding.ASCII.GetDecoder().GetChars(buffer, 0, 12, chars, 0);
|
|
|
|
|
String s = new String(chars);
|
|
|
|
|
Regex regex = new Regex("RFB ([0-9]{3})\\.([0-9]{3})\n");
|
|
|
|
|
Match match = regex.Match(s);
|
|
|
|
|
if (!match.Success)
|
2020-04-19 04:59:31 +02:00
|
|
|
|
throw new VNCException("Unexpected protocol version " + s);
|
|
|
|
|
|
|
|
|
|
int major = Int32.Parse(match.Groups[1].Value);
|
|
|
|
|
int minor = Int32.Parse(match.Groups[2].Value);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
2020-04-19 04:59:31 +02:00
|
|
|
|
if (major < 3)
|
|
|
|
|
throw new VNCException($"Unsupported protocol version {major}.{minor}");
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
|
2022-04-07 14:51:09 +02:00
|
|
|
|
private void SendProtocolVersion()
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
2022-04-07 14:51:09 +02:00
|
|
|
|
lock (writeLock)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
|
|
|
|
byte[] bytes = Encoding.ASCII.GetBytes("RFB 003.003\n");
|
2022-04-07 14:51:09 +02:00
|
|
|
|
stream.Write(bytes, 0, bytes.Length);
|
|
|
|
|
stream.Flush();
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-07 14:51:09 +02:00
|
|
|
|
private void ReadPixelFormat()
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
2022-04-07 14:51:09 +02:00
|
|
|
|
bitsPerPixel = stream.readCard8();
|
|
|
|
|
depth = stream.readCard8();
|
|
|
|
|
bigEndian = stream.readFlag();
|
|
|
|
|
trueColor = stream.readFlag();
|
|
|
|
|
redMax = stream.readCard16();
|
|
|
|
|
greenMax = stream.readCard16();
|
|
|
|
|
blueMax = stream.readCard16();
|
|
|
|
|
redShift = stream.readCard8();
|
|
|
|
|
greenShift = stream.readCard8();
|
|
|
|
|
blueShift = stream.readCard8();
|
|
|
|
|
stream.readPadding(3);
|
|
|
|
|
Log.Debug("readPixelFormat " + bitsPerPixel + " " + depth);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
|
2022-04-07 14:51:09 +02:00
|
|
|
|
private void WritePixelFormat()
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
2022-04-07 14:51:09 +02:00
|
|
|
|
Log.Debug("writePixelFormat " + bitsPerPixel + " " + depth);
|
|
|
|
|
stream.writeInt8(SET_PIXEL_FORMAT);
|
|
|
|
|
stream.writePadding(3);
|
|
|
|
|
|
|
|
|
|
stream.writeInt8(bitsPerPixel);
|
|
|
|
|
stream.writeInt8(depth);
|
|
|
|
|
stream.writeFlag(bigEndian);
|
|
|
|
|
stream.writeFlag(trueColor);
|
|
|
|
|
stream.writeInt16(redMax);
|
|
|
|
|
stream.writeInt16(greenMax);
|
|
|
|
|
stream.writeInt16(blueMax);
|
|
|
|
|
stream.writeInt8(redShift);
|
|
|
|
|
stream.writeInt8(greenShift);
|
|
|
|
|
stream.writeInt8(blueShift);
|
|
|
|
|
|
|
|
|
|
stream.writePadding(3);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
|
2022-04-07 14:51:09 +02:00
|
|
|
|
private void Force32bpp()
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
|
|
|
|
Log.Debug("force32bpp()");
|
|
|
|
|
|
2022-04-07 14:51:09 +02:00
|
|
|
|
bitsPerPixel = 32;
|
|
|
|
|
depth = 24;
|
|
|
|
|
trueColor = true;
|
|
|
|
|
redMax = 255;
|
|
|
|
|
greenMax = 255;
|
|
|
|
|
blueMax = 255;
|
|
|
|
|
redShift = 16;
|
|
|
|
|
greenShift = 8;
|
|
|
|
|
blueShift = 0;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
|
|
|
|
// Note that we keep the endian value from the server.
|
|
|
|
|
|
2022-04-07 14:51:09 +02:00
|
|
|
|
SetupPixelFormat();
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
2022-04-07 14:51:09 +02:00
|
|
|
|
lock (writeLock)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
2022-04-07 14:51:09 +02:00
|
|
|
|
WritePixelFormat();
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2022-04-07 14:51:09 +02:00
|
|
|
|
private void SetupPixelFormat()
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
|
|
|
|
Log.Debug("setupPixelFormat(" + bitsPerPixel + ")");
|
|
|
|
|
|
|
|
|
|
bytesPerPixel = bitsPerPixel >> 3;
|
|
|
|
|
|
|
|
|
|
redMaxPlus1 = redMax + 1;
|
|
|
|
|
greenMaxPlus1 = greenMax + 1;
|
|
|
|
|
blueMaxPlus1 = blueMax + 1;
|
|
|
|
|
|
|
|
|
|
redMaxOver2 = redMax >> 1;
|
|
|
|
|
greenMaxOver2 = greenMax >> 1;
|
|
|
|
|
blueMaxOver2 = blueMax >> 1;
|
|
|
|
|
|
|
|
|
|
if (bitsPerPixel == 32 || bitsPerPixel == 8)
|
|
|
|
|
{
|
|
|
|
|
pixelFormat = PixelFormat.Format32bppRgb;
|
|
|
|
|
pixelFormatCursor = PixelFormat.Format32bppArgb;
|
|
|
|
|
}
|
|
|
|
|
else if (bitsPerPixel == 16)
|
|
|
|
|
{
|
|
|
|
|
rgb565 = redShift == 11;
|
|
|
|
|
pixelFormat =
|
|
|
|
|
rgb565 ?
|
|
|
|
|
PixelFormat.Format16bppRgb565 :
|
|
|
|
|
PixelFormat.Format16bppRgb555;
|
|
|
|
|
pixelFormatCursor = PixelFormat.Format16bppArgb1555;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
throw new IOException("unexpected bits per pixel: " + bitsPerPixel);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-07 14:51:09 +02:00
|
|
|
|
private void WriteSetEncodings()
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
|
|
|
|
Log.Debug("writeSetEncodings");
|
2022-04-07 14:51:09 +02:00
|
|
|
|
stream.writeInt8(SET_ENCODINGS);
|
|
|
|
|
stream.writePadding(1);
|
|
|
|
|
stream.writeInt16(encodings.Length);
|
|
|
|
|
foreach (var encoding in encodings)
|
|
|
|
|
stream.writeInt32(encoding);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
|
2022-04-07 14:51:09 +02:00
|
|
|
|
private void WriteFramebufferUpdateRequest(int x, int y, int width, int height, bool incremental)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
2022-04-07 14:51:09 +02:00
|
|
|
|
stream.writeInt8(FRAMEBUFFER_UPDATE_REQUEST);
|
|
|
|
|
stream.writeFlag(incremental);
|
|
|
|
|
stream.writeInt16(x);
|
|
|
|
|
stream.writeInt16(y);
|
|
|
|
|
stream.writeInt16(width);
|
|
|
|
|
stream.writeInt16(height);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
|
2022-04-07 14:51:09 +02:00
|
|
|
|
private void AuthenticationExchange(char[] password)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
|
|
|
|
Log.Debug("authenticationExchange");
|
|
|
|
|
|
2022-04-07 14:51:09 +02:00
|
|
|
|
int scheme = stream.readCard32();
|
|
|
|
|
|
|
|
|
|
switch (scheme)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
2022-04-07 14:51:09 +02:00
|
|
|
|
case 0:
|
|
|
|
|
var reason = stream.readString();
|
|
|
|
|
throw new VNCException("connection failed: " + reason);
|
|
|
|
|
case 1:
|
|
|
|
|
// no authentication needed
|
|
|
|
|
break;
|
|
|
|
|
case 2:
|
|
|
|
|
PasswordAuthentication(password);
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
throw new VNCException("unexpected authentication scheme: " + scheme);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void PasswordAuthentication(char[] password)
|
|
|
|
|
{
|
|
|
|
|
byte[] keyBytes = new byte[8];
|
2022-04-07 14:51:09 +02:00
|
|
|
|
for (int i = 0; i < 8 && i < password.Length; ++i)
|
|
|
|
|
keyBytes[i] = Reverse((byte)password[i]);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
2022-04-07 14:51:09 +02:00
|
|
|
|
DESCryptoServiceProvider des = new DESCryptoServiceProvider
|
|
|
|
|
{
|
|
|
|
|
Padding = PaddingMode.None,
|
|
|
|
|
Mode = CipherMode.ECB
|
|
|
|
|
};
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
2022-04-07 14:51:09 +02:00
|
|
|
|
ICryptoTransform cipher = des.CreateEncryptor(keyBytes, null);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
|
|
|
|
byte[] challenge = new byte[16];
|
2022-04-07 14:51:09 +02:00
|
|
|
|
stream.readFully(challenge, 0, 16);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
2022-04-07 14:51:09 +02:00
|
|
|
|
byte[] response = cipher.TransformFinalBlock(challenge, 0, 16);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
2022-04-07 14:51:09 +02:00
|
|
|
|
stream.Write(response, 0, 16);
|
|
|
|
|
stream.Flush();
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
2022-04-07 14:51:09 +02:00
|
|
|
|
int status = stream.readCard32();
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
2022-04-07 14:51:09 +02:00
|
|
|
|
switch (status)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
2022-04-07 14:51:09 +02:00
|
|
|
|
case 0:
|
|
|
|
|
break;
|
|
|
|
|
case 1:
|
|
|
|
|
case 2:
|
|
|
|
|
throw new VNCAuthenticationException();
|
|
|
|
|
default:
|
|
|
|
|
throw new VNCException("Bad Authentication Response");
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-07 14:51:09 +02:00
|
|
|
|
private static byte Reverse(byte v)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
|
|
|
|
byte r = 0;
|
|
|
|
|
if ((v & 0x01) != 0) r |= 0x80;
|
|
|
|
|
if ((v & 0x02) != 0) r |= 0x40;
|
|
|
|
|
if ((v & 0x04) != 0) r |= 0x20;
|
|
|
|
|
if ((v & 0x08) != 0) r |= 0x10;
|
|
|
|
|
if ((v & 0x10) != 0) r |= 0x08;
|
|
|
|
|
if ((v & 0x20) != 0) r |= 0x04;
|
|
|
|
|
if ((v & 0x40) != 0) r |= 0x02;
|
|
|
|
|
if ((v & 0x80) != 0) r |= 0x01;
|
|
|
|
|
return r;
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-07 14:51:09 +02:00
|
|
|
|
private void InitializeClient()
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
|
|
|
|
Log.Debug("clientInitialisation");
|
2022-04-07 14:51:09 +02:00
|
|
|
|
lock (writeLock)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
2022-04-07 14:51:09 +02:00
|
|
|
|
stream.writeFlag(true); // shared
|
|
|
|
|
stream.Flush();
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-07 14:51:09 +02:00
|
|
|
|
private void InitializateServer()
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
|
|
|
|
Log.Debug("serverInitialisation");
|
2022-04-07 14:51:09 +02:00
|
|
|
|
int width = stream.readCard16();
|
|
|
|
|
int height = stream.readCard16();
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
2022-04-07 14:51:09 +02:00
|
|
|
|
ReadPixelFormat();
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
|
|
|
|
stream.readString(); /* The desktop name -- we don't care. */
|
|
|
|
|
|
|
|
|
|
if (trueColor)
|
|
|
|
|
{
|
2022-04-07 14:51:09 +02:00
|
|
|
|
SetupPixelFormat();
|
|
|
|
|
|
2013-06-24 13:41:48 +02:00
|
|
|
|
lock (writeLock)
|
|
|
|
|
{
|
2022-04-07 14:51:09 +02:00
|
|
|
|
WritePixelFormat();
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2022-04-07 14:51:09 +02:00
|
|
|
|
Force32bpp();
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
|
2022-04-07 14:51:09 +02:00
|
|
|
|
DesktopSize(width, height);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
2022-04-07 14:51:09 +02:00
|
|
|
|
lock (writeLock)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
2022-04-07 14:51:09 +02:00
|
|
|
|
WriteSetEncodings();
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Expects to be lock on writeLock.
|
|
|
|
|
*/
|
2022-04-07 14:51:09 +02:00
|
|
|
|
private void WriteKey(int command, bool down, int key)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
2022-04-07 14:51:09 +02:00
|
|
|
|
stream.writeInt8(command); //Send Scancodes
|
|
|
|
|
stream.writeFlag(down);
|
|
|
|
|
stream.writePadding(2);
|
|
|
|
|
stream.writeInt32(key);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
|
2022-04-07 14:51:09 +02:00
|
|
|
|
private void WriteQemuExtKey(int command, bool down, int key, int sym)
|
2017-05-02 18:17:30 +02:00
|
|
|
|
{
|
2022-04-07 14:51:09 +02:00
|
|
|
|
stream.writeInt8(command);
|
|
|
|
|
stream.writeInt8(QEMU_EXT_KEY_EVENT);
|
|
|
|
|
stream.writePadding(1);
|
|
|
|
|
stream.writeFlag(down);
|
|
|
|
|
stream.writeInt32(sym);
|
|
|
|
|
stream.writeInt32(key);
|
2017-05-02 18:17:30 +02:00
|
|
|
|
}
|
|
|
|
|
|
2017-06-06 17:32:25 +02:00
|
|
|
|
/**
|
|
|
|
|
* use_qemu_ext_key_encoding: Dictates if we want to use QEMU_EXT_KEY encoding.
|
|
|
|
|
*
|
|
|
|
|
* XS6.2 doesn't properly support QEMU_EXT_KEY and XS6.5 supports QEMU_EXT_KEY encoding
|
|
|
|
|
* only if XS65ESP1051 is applied, so restrict QEMU_EXT_KEY encoding to Inverness and above.
|
|
|
|
|
*/
|
|
|
|
|
public void keyScanEvent(bool down, int key, int sym, bool use_qemu_ext_key_encoding)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
2022-04-07 14:51:09 +02:00
|
|
|
|
lock (writeLock)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2017-06-06 17:32:25 +02:00
|
|
|
|
if (qemu_ext_key_encoding && use_qemu_ext_key_encoding)
|
2017-05-02 18:17:30 +02:00
|
|
|
|
{
|
2022-04-07 14:51:09 +02:00
|
|
|
|
WriteQemuExtKey(QEMU_MSG, down, key, sym);
|
2017-05-02 18:17:30 +02:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2022-04-07 14:51:09 +02:00
|
|
|
|
WriteKey(KEY_SCAN_EVENT, down, key);
|
2017-05-02 18:17:30 +02:00
|
|
|
|
}
|
2022-04-07 14:51:09 +02:00
|
|
|
|
stream.Flush();
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
catch (IOException e)
|
|
|
|
|
{
|
|
|
|
|
Log.Warn(e, e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void keyCodeEvent(bool down, int key)
|
|
|
|
|
{
|
2022-04-07 14:51:09 +02:00
|
|
|
|
lock (writeLock)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2022-04-07 14:51:09 +02:00
|
|
|
|
WriteKey(KEY_EVENT, down, key);
|
|
|
|
|
stream.Flush();
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
catch (IOException e)
|
|
|
|
|
{
|
|
|
|
|
Log.Warn(e, e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-07 14:51:09 +02:00
|
|
|
|
public void PointerEvent(int buttonMask, int x, int y)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
|
|
|
|
if (x < 0)
|
|
|
|
|
{
|
|
|
|
|
x = 0;
|
|
|
|
|
}
|
2022-04-07 14:51:09 +02:00
|
|
|
|
else if (x >= _width)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
2022-04-07 14:51:09 +02:00
|
|
|
|
x = _width - 1;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (y < 0)
|
|
|
|
|
{
|
|
|
|
|
y = 0;
|
|
|
|
|
}
|
2022-04-07 14:51:09 +02:00
|
|
|
|
else if (y >= _height)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
2022-04-07 14:51:09 +02:00
|
|
|
|
y = _height - 1;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
|
2022-04-07 14:51:09 +02:00
|
|
|
|
lock (writeLock)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2022-04-07 14:51:09 +02:00
|
|
|
|
PointerEvent_(buttonMask, x, y);
|
|
|
|
|
stream.Flush();
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
catch (IOException e)
|
|
|
|
|
{
|
|
|
|
|
Log.Warn(e, e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-07 14:51:09 +02:00
|
|
|
|
public void PointerWheelEvent(int x, int y, int r)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
2022-04-07 14:51:09 +02:00
|
|
|
|
lock (writeLock)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
/*
|
|
|
|
|
The RFB protocol specifies a down-up pair for each
|
|
|
|
|
scroll of the wheel, on button 4 for scrolling up, and
|
|
|
|
|
button 5 for scrolling down.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
int m;
|
|
|
|
|
|
|
|
|
|
if (r < 0)
|
|
|
|
|
{
|
|
|
|
|
r = -r;
|
|
|
|
|
m = 8;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
m = 16;
|
|
|
|
|
}
|
|
|
|
|
for (int i = 0; i < r; i++)
|
|
|
|
|
{
|
2022-04-07 14:51:09 +02:00
|
|
|
|
PointerEvent_(m, x, y);
|
|
|
|
|
PointerEvent_(0, x, y);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
|
2022-04-07 14:51:09 +02:00
|
|
|
|
stream.Flush();
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
catch (IOException e)
|
|
|
|
|
{
|
|
|
|
|
Log.Warn(e, e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-07 14:51:09 +02:00
|
|
|
|
private void PointerEvent_(int buttonMask, int x, int y)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
2022-04-07 14:51:09 +02:00
|
|
|
|
stream.writeInt8(POINTER_EVENT);
|
|
|
|
|
stream.writeInt8(buttonMask);
|
|
|
|
|
stream.writeInt16(x);
|
|
|
|
|
stream.writeInt16(y);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
|
2022-04-07 14:51:09 +02:00
|
|
|
|
public void ClientCutText(string text)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
|
|
|
|
Log.Debug("cutEvent");
|
|
|
|
|
|
2022-04-07 14:51:09 +02:00
|
|
|
|
lock (writeLock)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2022-04-07 14:51:09 +02:00
|
|
|
|
stream.writeInt8(CLIENT_CUT_TEXT);
|
|
|
|
|
stream.writePadding(3);
|
|
|
|
|
stream.writeString(text);
|
|
|
|
|
stream.Flush();
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
catch (IOException e)
|
|
|
|
|
{
|
|
|
|
|
Log.Warn(e, e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Creates an image in place in this.data. It expects the pixel data to already be in this.data.
|
|
|
|
|
/// </summary>
|
2022-04-07 14:51:09 +02:00
|
|
|
|
/// <param name="width"></param>
|
|
|
|
|
/// <param name="height"></param>
|
|
|
|
|
/// <param name="x"></param>
|
|
|
|
|
/// <param name="y"></param>
|
2013-06-24 13:41:48 +02:00
|
|
|
|
/// <param name="start">the start of image data in this.data</param>
|
|
|
|
|
/// <param name="length">the length of image data in this data</param>
|
2022-04-07 14:51:09 +02:00
|
|
|
|
/// <param name="maskLength">length of cursor mask (after the image in this.data), as specified by
|
2013-06-24 13:41:48 +02:00
|
|
|
|
/// the RFB protocol specification for the Cursor pseudo-encoding (1-bpp, packed). If 0,
|
|
|
|
|
/// the mask is assumed to be totally opaque (as used by normal "raw" packets). Masks are not
|
|
|
|
|
/// supported for 8-bpp images.</param>
|
2022-04-07 14:51:09 +02:00
|
|
|
|
/// <param name="cursor"></param>
|
|
|
|
|
private void CreateImage(int width, int height, int x, int y, int start, int length, int maskLength, bool cursor)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
|
|
|
|
if (width == 0 || height == 0)
|
|
|
|
|
return;
|
|
|
|
|
|
2022-04-07 14:51:09 +02:00
|
|
|
|
byte[] dataToRender;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
int stride;
|
|
|
|
|
|
|
|
|
|
if (bitsPerPixel == 32)
|
|
|
|
|
{
|
|
|
|
|
stride = width * 4;
|
2022-04-07 14:51:09 +02:00
|
|
|
|
dataToRender = _data;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
|
|
|
|
System.Diagnostics.Debug.Assert(length == height * stride);
|
|
|
|
|
|
|
|
|
|
if (cursor)
|
|
|
|
|
{
|
|
|
|
|
// for mask
|
|
|
|
|
int j = 0; // bit within the current byte (k)
|
|
|
|
|
int k = start + length; //byte
|
|
|
|
|
int m = 0; // bit within the current row
|
|
|
|
|
|
|
|
|
|
for (int i = start; i < start + length; i += 4)
|
|
|
|
|
{
|
2022-04-07 14:51:09 +02:00
|
|
|
|
bool mask = (_data[k] & (1 << (7 - j))) == 0;
|
|
|
|
|
_data[i + 3] = (byte)(mask ? 0 : 0xff);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
|
|
|
|
j++;
|
|
|
|
|
m++;
|
|
|
|
|
|
|
|
|
|
if (m == width)
|
|
|
|
|
{
|
|
|
|
|
j = 0;
|
|
|
|
|
m = 0;
|
|
|
|
|
k++;
|
|
|
|
|
}
|
|
|
|
|
else if (j > 7)
|
|
|
|
|
{
|
|
|
|
|
j = 0;
|
|
|
|
|
k++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else if (bitsPerPixel == 16)
|
|
|
|
|
{
|
|
|
|
|
// Bitmap requires that stride is a multiple of 4, so we
|
|
|
|
|
// will have to expand the data if width is odd.
|
|
|
|
|
bool expand_data = width % 2 == 1;
|
|
|
|
|
int stride_correction = expand_data ? 2 : 0;
|
|
|
|
|
stride = width * 2 + stride_correction;
|
2022-04-07 14:51:09 +02:00
|
|
|
|
dataToRender = expand_data ? new byte[stride * height] : _data;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
|
|
|
|
System.Diagnostics.Debug.Assert(length == height * width * 2);
|
|
|
|
|
|
|
|
|
|
if (cursor)
|
|
|
|
|
{
|
|
|
|
|
int p = 0; // Byte within the destination data_to_render.
|
|
|
|
|
// for mask
|
|
|
|
|
int j = 0; // bit within the current byte (k)
|
|
|
|
|
int k = start + length; //byte
|
|
|
|
|
int m = 0; // bit within the current row
|
|
|
|
|
|
|
|
|
|
for (int i = start; i < start + length; i += 2)
|
|
|
|
|
{
|
2022-04-07 14:51:09 +02:00
|
|
|
|
bool mask = (_data[k] & (1 << (7 - j))) == 0;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
byte mask_bit = (byte)(mask ? 0 : 0x80);
|
|
|
|
|
|
|
|
|
|
if (rgb565)
|
|
|
|
|
{
|
|
|
|
|
// Convert the 565 data into 1555.
|
2022-04-07 14:51:09 +02:00
|
|
|
|
dataToRender[p] = (byte)((_data[i] & 0x1f) | ((_data[i] & 0xe0) >> 1));
|
|
|
|
|
dataToRender[p + 1] = (byte)(((_data[i + 1] & 0x7) >> 1) | (_data[i + 1] & 0x78) | mask_bit);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
// Add the mask bit -- everything else is OK because it's already 555.
|
2022-04-07 14:51:09 +02:00
|
|
|
|
dataToRender[p] = _data[i];
|
|
|
|
|
dataToRender[p + 1] = (byte)(_data[i + 1] | mask_bit);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
j++;
|
|
|
|
|
m++;
|
|
|
|
|
p += 2;
|
|
|
|
|
|
|
|
|
|
if (m == width)
|
|
|
|
|
{
|
|
|
|
|
j = 0;
|
|
|
|
|
m = 0;
|
|
|
|
|
k++;
|
|
|
|
|
p += stride_correction;
|
|
|
|
|
}
|
|
|
|
|
else if (j > 7)
|
|
|
|
|
{
|
|
|
|
|
j = 0;
|
|
|
|
|
k++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else if (expand_data)
|
|
|
|
|
{
|
|
|
|
|
int w2 = width * 2;
|
|
|
|
|
int i = start; // Byte within the source data.
|
|
|
|
|
int p = 0; // Byte within the destination data_to_render.
|
|
|
|
|
|
|
|
|
|
for (int m = 0; m < height; m++)
|
|
|
|
|
{
|
2022-04-07 14:51:09 +02:00
|
|
|
|
Array.Copy(_data, i, dataToRender, p, w2);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
i += w2;
|
|
|
|
|
p += stride;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else if (bitsPerPixel == 8)
|
|
|
|
|
{
|
|
|
|
|
stride = width * 4;
|
2022-04-07 14:51:09 +02:00
|
|
|
|
dataToRender = data_8bpp;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
|
|
|
|
System.Diagnostics.Debug.Assert(length == width * height);
|
|
|
|
|
|
|
|
|
|
// for mask
|
|
|
|
|
int j = 0; // bit within the current byte (k)
|
|
|
|
|
int k = start + length; //byte
|
|
|
|
|
int m = 0; // bit within the current row
|
|
|
|
|
|
|
|
|
|
for (int i = start, n = 0; i < start + length; i++, n += 4)
|
|
|
|
|
{
|
2022-04-07 14:51:09 +02:00
|
|
|
|
data_8bpp[n + 2] = (byte)(((((_data[i] >> redShift) & redMax) << 8) + redMaxOver2) / redMaxPlus1);
|
|
|
|
|
data_8bpp[n + 1] = (byte)(((((_data[i] >> greenShift) & greenMax) << 8) + greenMaxOver2) / greenMaxPlus1);
|
|
|
|
|
data_8bpp[n] = (byte)(((((_data[i] >> blueShift) & blueMax) << 8) + blueMaxOver2) / blueMaxPlus1);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
if (cursor)
|
|
|
|
|
{
|
2022-04-07 14:51:09 +02:00
|
|
|
|
bool mask = (_data[k] & (1 << (7 - j))) == 0;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
data_8bpp[n + 3] = (byte)(mask ? 0 : 0xff);
|
|
|
|
|
|
|
|
|
|
j++;
|
|
|
|
|
m++;
|
|
|
|
|
|
|
|
|
|
if (m == width)
|
|
|
|
|
{
|
|
|
|
|
j = 0;
|
|
|
|
|
m = 0;
|
|
|
|
|
k++;
|
|
|
|
|
}
|
|
|
|
|
else if (j > 7)
|
|
|
|
|
{
|
|
|
|
|
j = 0;
|
|
|
|
|
k++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
data_8bpp[n + 3] = 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
throw new Exception("unexpected bits per pixel");
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-07 14:51:09 +02:00
|
|
|
|
BitmapToClient(width, height, x, y, start, stride, cursor, dataToRender);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void BitmapToClient(int width, int height, int x, int y, int start, int stride, bool cursor, byte [] img)
|
|
|
|
|
{
|
|
|
|
|
GCHandle handle = GCHandle.Alloc(img, GCHandleType.Pinned);
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
IntPtr pointer = Marshal.UnsafeAddrOfPinnedArrayElement(img, start);
|
|
|
|
|
using (Bitmap bitmap = new Bitmap(width, height, stride, cursor ? pixelFormatCursor : pixelFormat, pointer))
|
|
|
|
|
{
|
|
|
|
|
if (cursor)
|
|
|
|
|
{
|
|
|
|
|
client.ClientSetCursor(bitmap, x, y, width, height);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
client.ClientDrawImage(bitmap, x, y, width, height);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (ArgumentException exn)
|
|
|
|
|
{
|
|
|
|
|
Log.Error(exn, exn);
|
|
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
|
|
|
|
handle.Free();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-07 14:51:09 +02:00
|
|
|
|
private void ReadRawEncoding(int x, int y, int width, int height)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
2022-04-07 14:51:09 +02:00
|
|
|
|
ReadRawEncoding_(0, x, y, width, height, false);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param mask If true, read a mask after the raw data, as used by the
|
|
|
|
|
* Cursor pseudo-encoding.
|
|
|
|
|
* @param start The position in this.data to start using
|
|
|
|
|
*/
|
2022-04-07 14:51:09 +02:00
|
|
|
|
private void ReadRawEncoding_(int start, int x, int y, int width, int height, bool cursor)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
|
|
|
|
if (width < 0 || height < 0)
|
|
|
|
|
{
|
|
|
|
|
throw new VNCException("Invalid size: " + width + " x " +
|
|
|
|
|
height);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int length = width * height * bytesPerPixel;
|
2022-04-07 14:51:09 +02:00
|
|
|
|
if (_data.Length < start + length)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
throw new VNCException("Server error: received rectangle bigger than desktop!");
|
2022-04-07 14:51:09 +02:00
|
|
|
|
stream.readFully(_data, start, length);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
2022-04-07 14:51:09 +02:00
|
|
|
|
int maskLength = 0;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
if (cursor)
|
|
|
|
|
{
|
|
|
|
|
// 1 bit mask.
|
|
|
|
|
int scanline = (width + 7) >> 3;
|
2022-04-07 14:51:09 +02:00
|
|
|
|
maskLength = scanline * height;
|
|
|
|
|
System.Diagnostics.Trace.Assert(_data.Length >= start + length + maskLength);
|
|
|
|
|
stream.readFully(_data, start + length, maskLength);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
|
2022-04-07 14:51:09 +02:00
|
|
|
|
CreateImage(width, height, x, y, start, length, maskLength, cursor);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
|
2022-04-07 14:51:09 +02:00
|
|
|
|
private void ReadCopyRectangleEncoding(int dx, int dy, int width, int height)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
2022-04-07 14:51:09 +02:00
|
|
|
|
int x = stream.readCard16();
|
|
|
|
|
int y = stream.readCard16();
|
2013-06-24 13:41:48 +02:00
|
|
|
|
client.ClientCopyRectangle(x, y, width, height, dx, dy);
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-07 14:51:09 +02:00
|
|
|
|
private Color ReadColor()
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
2022-04-07 14:51:09 +02:00
|
|
|
|
byte[] color = ReadColorBytes();
|
2013-06-24 13:41:48 +02:00
|
|
|
|
return Color.FromArgb(color[2], color[1], color[0]);
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-07 14:51:09 +02:00
|
|
|
|
private byte[] ReadColorBytes(byte[] color, int start)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
2022-04-07 14:51:09 +02:00
|
|
|
|
uint pixel;
|
|
|
|
|
switch (bitsPerPixel)
|
|
|
|
|
{
|
|
|
|
|
case 32:
|
|
|
|
|
pixel = (uint)(color[start] |
|
|
|
|
|
color[start + 1] << 8 |
|
|
|
|
|
color[start + 2] << 16 |
|
|
|
|
|
color[start + 3] << 24);
|
|
|
|
|
break;
|
|
|
|
|
case 16:
|
|
|
|
|
pixel = (uint)(color[start] |
|
|
|
|
|
color[start + 1] << 8);
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
pixel = color[start];
|
|
|
|
|
break;
|
|
|
|
|
}
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
|
|
|
|
byte[] newColor = new byte[4];
|
|
|
|
|
|
|
|
|
|
//ARGB Encoding
|
|
|
|
|
newColor[3] = 0xFF;
|
|
|
|
|
newColor[2] = (byte)(((((pixel >> redShift) & redMax) << 8) + redMaxOver2) / redMaxPlus1);
|
|
|
|
|
newColor[1] = (byte)(((((pixel >> greenShift) & greenMax) << 8) + greenMaxOver2) / greenMaxPlus1);
|
|
|
|
|
newColor[0] = (byte)(((((pixel >> blueShift) & blueMax) << 8) + blueMaxOver2) / blueMaxPlus1);
|
|
|
|
|
|
|
|
|
|
return newColor;
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-07 14:51:09 +02:00
|
|
|
|
private byte[] ReadColorBytes()
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
|
|
|
|
int n = bitsPerPixel >> 3;
|
|
|
|
|
byte[] color = new byte[n];
|
|
|
|
|
|
2022-04-07 14:51:09 +02:00
|
|
|
|
stream.readFully(color, 0, n);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
2022-04-07 14:51:09 +02:00
|
|
|
|
return ReadColorBytes(color, 0);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
|
2022-04-07 14:51:09 +02:00
|
|
|
|
private void ReadRREEncoding(int x, int y, int width, int height)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
2022-04-07 14:51:09 +02:00
|
|
|
|
int n = stream.readCard32();
|
|
|
|
|
Color background = ReadColor();
|
2013-06-24 13:41:48 +02:00
|
|
|
|
client.ClientFillRectangle(x, y, width, height, background);
|
|
|
|
|
for (int i = 0; i < n; ++i)
|
|
|
|
|
{
|
2022-04-07 14:51:09 +02:00
|
|
|
|
Color foreground = ReadColor();
|
|
|
|
|
int rx = stream.readCard16();
|
|
|
|
|
int ry = stream.readCard16();
|
|
|
|
|
int rw = stream.readCard16();
|
|
|
|
|
int rh = stream.readCard16();
|
2013-06-24 13:41:48 +02:00
|
|
|
|
client.ClientFillRectangle(x + rx, y + ry, rw, rh, foreground);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-07 14:51:09 +02:00
|
|
|
|
private void ReadCoRREEncoding(int x, int y, int width, int height)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
2022-04-07 14:51:09 +02:00
|
|
|
|
int n = stream.readCard32();
|
|
|
|
|
Color background = ReadColor();
|
2013-06-24 13:41:48 +02:00
|
|
|
|
client.ClientFillRectangle(x, y, width, height, background);
|
|
|
|
|
for (int i = 0; i < n; ++i)
|
|
|
|
|
{
|
2022-04-07 14:51:09 +02:00
|
|
|
|
Color foreground = ReadColor();
|
|
|
|
|
int rx = stream.readCard8();
|
|
|
|
|
int ry = stream.readCard8();
|
|
|
|
|
int rw = stream.readCard8();
|
|
|
|
|
int rh = stream.readCard8();
|
2013-06-24 13:41:48 +02:00
|
|
|
|
client.ClientFillRectangle(x + rx, y + ry, rw, rh, foreground);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-07 14:51:09 +02:00
|
|
|
|
private void ReadFillRectangles(int rx, int ry, int n)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
2022-04-07 14:51:09 +02:00
|
|
|
|
int pixelSize = (bitsPerPixel + 7) >> 3;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
int length = n * (pixelSize + 2);
|
2022-04-07 14:51:09 +02:00
|
|
|
|
stream.readFully(_data, 0, length);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
int index = 0;
|
|
|
|
|
for (int i = 0; i < n; ++i)
|
|
|
|
|
{
|
2022-04-07 14:51:09 +02:00
|
|
|
|
Color foreground = ReadColor();
|
|
|
|
|
int sxy = _data[index++] & 0xff;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
int sx = sxy >> 4;
|
|
|
|
|
int sy = sxy & 0xf;
|
2022-04-07 14:51:09 +02:00
|
|
|
|
int swh = _data[index++] & 0xff;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
int sw = (swh >> 4) + 1;
|
|
|
|
|
int sh = (swh & 0xf) + 1;
|
|
|
|
|
client.ClientFillRectangle(
|
|
|
|
|
rx + sx, ry + sy, sw, sh, foreground
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-07 14:51:09 +02:00
|
|
|
|
private void ReadRectangles(int rx, int ry, int n, Color foreground)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
|
|
|
|
for (int i = 0; i < n; ++i)
|
|
|
|
|
{
|
2022-04-07 14:51:09 +02:00
|
|
|
|
int sxy = stream.readCard8();
|
2013-06-24 13:41:48 +02:00
|
|
|
|
int sx = sxy >> 4;
|
|
|
|
|
int sy = sxy & 0xf;
|
2022-04-07 14:51:09 +02:00
|
|
|
|
int swh = stream.readCard8();
|
2013-06-24 13:41:48 +02:00
|
|
|
|
int sw = (swh >> 4) + 1;
|
|
|
|
|
int sh = (swh & 0xf) + 1;
|
|
|
|
|
client.ClientFillRectangle(
|
|
|
|
|
rx + sx, ry + sy, sw, sh, foreground
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-07 14:51:09 +02:00
|
|
|
|
private void FillRectBytes(byte[] data, int stride, int x, int y, int width, int height, byte[] color)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
|
|
|
|
// Fast path thin rectangles
|
|
|
|
|
|
|
|
|
|
int p = (y * stride) + (x * 4);
|
|
|
|
|
int skip;
|
|
|
|
|
|
|
|
|
|
if (width == 1 && height == 1)
|
|
|
|
|
{
|
|
|
|
|
data[p + 0] = color[0];
|
|
|
|
|
data[p + 1] = color[1];
|
|
|
|
|
data[p + 2] = color[2];
|
|
|
|
|
data[p + 3] = color[3];
|
|
|
|
|
}
|
|
|
|
|
else if (width == 1)
|
|
|
|
|
{
|
|
|
|
|
skip = stride - 4;
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < height; i++)
|
|
|
|
|
{
|
|
|
|
|
data[p + 0] = color[0];
|
|
|
|
|
data[p + 1] = color[1];
|
|
|
|
|
data[p + 2] = color[2];
|
|
|
|
|
data[p + 3] = color[3];
|
|
|
|
|
|
|
|
|
|
p += skip;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else if (height == 1)
|
|
|
|
|
{
|
|
|
|
|
for (int i = 0; i < width; i++)
|
|
|
|
|
{
|
|
|
|
|
data[p + 0] = color[0];
|
|
|
|
|
data[p + 1] = color[1];
|
|
|
|
|
data[p + 2] = color[2];
|
|
|
|
|
data[p + 3] = color[3];
|
|
|
|
|
|
|
|
|
|
p += 4;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
skip = stride - (width * 4);
|
|
|
|
|
|
|
|
|
|
for (int j = 0; j < height; j++)
|
|
|
|
|
{
|
|
|
|
|
for (int i = 0; i < width; i++)
|
|
|
|
|
{
|
|
|
|
|
data[p + 0] = color[0];
|
|
|
|
|
data[p + 1] = color[1];
|
|
|
|
|
data[p + 2] = color[2];
|
|
|
|
|
data[p + 3] = color[3];
|
|
|
|
|
|
|
|
|
|
p += 4;
|
|
|
|
|
}
|
|
|
|
|
p += skip;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-07 14:51:09 +02:00
|
|
|
|
private void ReadHextileEncoding(int x, int y, int width, int height)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
|
|
|
|
/*
|
|
|
|
|
* Basically, we have two ways of doing this.
|
|
|
|
|
* 1. Draw the hextile to a byte buffer, convert to image
|
|
|
|
|
* and draw to client
|
|
|
|
|
* 2. Draw the individual rectangles of the hex encoding
|
|
|
|
|
* directly to the client
|
|
|
|
|
*
|
|
|
|
|
* I have found its faster to do 1) when size is > 64
|
|
|
|
|
*/
|
|
|
|
|
//GraphicsUtils.startTime();
|
|
|
|
|
if (width * height > 64)
|
|
|
|
|
{
|
|
|
|
|
byte[] background = { 0, 0, 0, 0xFF }; // Black
|
|
|
|
|
byte[] foreground = { 0xFF, 0xFF, 0xFF, 0xFF }; // White
|
|
|
|
|
|
|
|
|
|
byte[] buff = new byte[width * height * 4]; //assume 32 bpp
|
|
|
|
|
int stride = width * 4;
|
|
|
|
|
|
|
|
|
|
for (int sy = 0; sy < height; sy += 16)
|
|
|
|
|
{
|
|
|
|
|
int sheight = Math.Min(16, height - sy);
|
|
|
|
|
|
|
|
|
|
for (int sx = 0; sx < width; sx += 16)
|
|
|
|
|
{
|
|
|
|
|
int swidth = Math.Min(16, width - sx);
|
|
|
|
|
|
2022-04-07 14:51:09 +02:00
|
|
|
|
int mask = stream.readCard8();
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
|
|
|
|
if ((mask & RAW_SUBENCODING) != 0)
|
|
|
|
|
{
|
|
|
|
|
int length = swidth * sheight * 4;
|
2022-04-07 14:51:09 +02:00
|
|
|
|
stream.readFully(_data, 0, length);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
|
|
|
|
int index = 0;
|
|
|
|
|
int skip = stride - (swidth * 4);
|
|
|
|
|
int p = (sy * stride) + (sx * 4);
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < sheight; i++)
|
|
|
|
|
{
|
|
|
|
|
for (int j = 0; j < swidth; j++)
|
|
|
|
|
{
|
2022-04-07 14:51:09 +02:00
|
|
|
|
byte[] color = ReadColorBytes(_data, index);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
|
|
|
|
index += 4; //assumed 32bpp here
|
|
|
|
|
|
|
|
|
|
buff[p + 3] = color[3];
|
|
|
|
|
buff[p + 2] = color[2];
|
|
|
|
|
buff[p + 1] = color[1];
|
|
|
|
|
buff[p + 0] = color[0];
|
|
|
|
|
|
|
|
|
|
p += 4;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
p += skip;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if ((mask & BACKGROUND_SPECIFIED_SUBENCODING) != 0)
|
|
|
|
|
{
|
2022-04-07 14:51:09 +02:00
|
|
|
|
background = ReadColorBytes();
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
|
2022-04-07 14:51:09 +02:00
|
|
|
|
FillRectBytes(buff, stride, sx, sy, swidth, sheight, background);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
|
|
|
|
if ((mask & FOREGROUND_SPECIFIED_SUBENCODING) != 0)
|
|
|
|
|
{
|
2022-04-07 14:51:09 +02:00
|
|
|
|
foreground = ReadColorBytes();
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ((mask & ANY_SUBRECTS_SUBENCODING) != 0)
|
|
|
|
|
{
|
2022-04-07 14:51:09 +02:00
|
|
|
|
int n = stream.readCard8();
|
2013-06-24 13:41:48 +02:00
|
|
|
|
if ((mask & SUBRECTS_COLORED_SUBENCODING) != 0)
|
|
|
|
|
{
|
|
|
|
|
int length = n * 6; //assume 32bpp
|
2022-04-07 14:51:09 +02:00
|
|
|
|
stream.readFully(_data, 0, length);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
int index = 0;
|
|
|
|
|
for (int i = 0; i < n; ++i)
|
|
|
|
|
{
|
|
|
|
|
byte[] color = new byte[4];
|
2022-04-07 14:51:09 +02:00
|
|
|
|
uint pixel = (uint)(_data[index + 0] & 0xFF |
|
|
|
|
|
_data[index + 1] << 8 |
|
|
|
|
|
_data[index + 2] << 16 |
|
|
|
|
|
_data[index + 3] << 24);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
|
|
|
|
//ARGB Encoding
|
|
|
|
|
color[3] = 0xFF;
|
2022-04-07 14:51:09 +02:00
|
|
|
|
color[2] = (byte)((pixel >> redShift) & redMax);
|
|
|
|
|
color[1] = (byte)((pixel >> greenShift) & greenMax);
|
|
|
|
|
color[0] = (byte)((pixel >> blueShift) & blueMax);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
|
|
|
|
index += 4;
|
2022-04-07 14:51:09 +02:00
|
|
|
|
int txy = _data[index++] & 0xff;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
int tx = txy >> 4;
|
|
|
|
|
int ty = txy & 0xf;
|
2022-04-07 14:51:09 +02:00
|
|
|
|
int twh = _data[index++] & 0xff;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
int tw = (twh >> 4) + 1;
|
|
|
|
|
int th = (twh & 0xf) + 1;
|
|
|
|
|
|
2022-04-07 14:51:09 +02:00
|
|
|
|
FillRectBytes(buff, stride, sx + tx, sy + ty, tw, th, color);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
for (int i = 0; i < n; ++i)
|
|
|
|
|
{
|
2022-04-07 14:51:09 +02:00
|
|
|
|
int txy = stream.readCard8();
|
2013-06-24 13:41:48 +02:00
|
|
|
|
int tx = txy >> 4;
|
|
|
|
|
int ty = txy & 0xf;
|
2022-04-07 14:51:09 +02:00
|
|
|
|
int twh = stream.readCard8();
|
2013-06-24 13:41:48 +02:00
|
|
|
|
int tw = (twh >> 4) + 1;
|
|
|
|
|
int th = (twh & 0xf) + 1;
|
|
|
|
|
|
2022-04-07 14:51:09 +02:00
|
|
|
|
FillRectBytes(buff, stride, sx + tx, sy + ty, tw, th, foreground);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Now convert to image and write to screen
|
|
|
|
|
GCHandle handle = GCHandle.Alloc(buff, GCHandleType.Pinned);
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
IntPtr pointer = Marshal.UnsafeAddrOfPinnedArrayElement(buff, 0);
|
|
|
|
|
using (Bitmap bitmap = new Bitmap(width, height, stride, PixelFormat.Format32bppArgb, pointer))
|
|
|
|
|
{
|
|
|
|
|
client.ClientDrawImage(bitmap, x, y, width, height);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
|
|
|
|
handle.Free();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Color foreground = Color.White;
|
|
|
|
|
Color background = Color.Black;
|
|
|
|
|
|
|
|
|
|
int xCount = (width + 15) >> 4;
|
|
|
|
|
int yCount = (height + 15) >> 4;
|
|
|
|
|
for (int yi = 0; yi < yCount; ++yi)
|
|
|
|
|
{
|
|
|
|
|
int ry = y + (yi << 4);
|
|
|
|
|
int rh = (yi == (yCount - 1)) ? height & 0xf : 16;
|
|
|
|
|
if (rh == 0)
|
|
|
|
|
{
|
|
|
|
|
rh = 16;
|
|
|
|
|
}
|
|
|
|
|
for (int xi = 0; xi < xCount; ++xi)
|
|
|
|
|
{
|
|
|
|
|
int rx = x + (xi << 4);
|
|
|
|
|
int rw = (xi == (xCount - 1)) ? width & 0xf : 16;
|
|
|
|
|
if (rw == 0)
|
|
|
|
|
{
|
|
|
|
|
rw = 16;
|
|
|
|
|
}
|
2022-04-07 14:51:09 +02:00
|
|
|
|
int mask = stream.readCard8();
|
2013-06-24 13:41:48 +02:00
|
|
|
|
if ((mask & RAW_SUBENCODING) != 0)
|
|
|
|
|
{
|
2022-04-07 14:51:09 +02:00
|
|
|
|
ReadRawEncoding(rx, ry, rw, rh);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if ((mask & BACKGROUND_SPECIFIED_SUBENCODING) != 0)
|
|
|
|
|
{
|
2022-04-07 14:51:09 +02:00
|
|
|
|
background = ReadColor();
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
client.ClientFillRectangle(rx, ry, rw, rh, background);
|
|
|
|
|
if ((mask & FOREGROUND_SPECIFIED_SUBENCODING) != 0)
|
|
|
|
|
{
|
2022-04-07 14:51:09 +02:00
|
|
|
|
foreground = ReadColor();
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
if ((mask & ANY_SUBRECTS_SUBENCODING) != 0)
|
|
|
|
|
{
|
2022-04-07 14:51:09 +02:00
|
|
|
|
int n = stream.readCard8();
|
2013-06-24 13:41:48 +02:00
|
|
|
|
if ((mask & SUBRECTS_COLORED_SUBENCODING) != 0)
|
|
|
|
|
{
|
2022-04-07 14:51:09 +02:00
|
|
|
|
ReadFillRectangles(rx, ry, n);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2022-04-07 14:51:09 +02:00
|
|
|
|
ReadRectangles(rx, ry, n, foreground);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
/*double time = GraphicsUtils.endTime("hextile");
|
|
|
|
|
int size = width * height;
|
|
|
|
|
StatsEntry entry = new StatsEntry();
|
|
|
|
|
entry.time = time;
|
|
|
|
|
entry.size = size;
|
|
|
|
|
|
|
|
|
|
this.client.stats.Add(entry);*/
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-07 14:51:09 +02:00
|
|
|
|
private void ReadCursorPseudoEncoding(int x, int y, int width, int height)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
2022-04-07 14:51:09 +02:00
|
|
|
|
ReadRawEncoding_(0, x, y, width, height, true);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
|
2022-04-07 14:51:09 +02:00
|
|
|
|
private void ReadFrameBufferUpdate()
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
2022-04-07 14:51:09 +02:00
|
|
|
|
stream.readPadding(1);
|
|
|
|
|
int n = stream.readCard16();
|
2013-06-24 13:41:48 +02:00
|
|
|
|
Log.Debug("reading " + n + " rectangles");
|
|
|
|
|
bool fb_updated = false;
|
2022-04-07 14:51:09 +02:00
|
|
|
|
|
|
|
|
|
Win32.QueryPerformanceCounter(out var start);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
for (int i = 0; i < n; ++i)
|
|
|
|
|
{
|
2022-04-07 14:51:09 +02:00
|
|
|
|
int x = stream.readCard16();
|
|
|
|
|
int y = stream.readCard16();
|
|
|
|
|
int width = stream.readCard16();
|
|
|
|
|
int height = stream.readCard16();
|
|
|
|
|
int encoding = stream.readCard32();
|
|
|
|
|
Log.Debug("read " + x + " " + y + " " + width + " " + height + " " + encoding);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
switch (encoding)
|
|
|
|
|
{
|
|
|
|
|
case RAW_ENCODING:
|
2022-04-07 14:51:09 +02:00
|
|
|
|
ReadRawEncoding(x, y, width, height);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
break;
|
|
|
|
|
case RRE_ENCODING:
|
2022-04-07 14:51:09 +02:00
|
|
|
|
ReadRREEncoding(x, y, width, height);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
break;
|
|
|
|
|
case CORRE_ENCODING:
|
2022-04-07 14:51:09 +02:00
|
|
|
|
ReadCoRREEncoding(x, y, width, height);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
break;
|
|
|
|
|
case COPY_RECTANGLE_ENCODING:
|
2022-04-07 14:51:09 +02:00
|
|
|
|
ReadCopyRectangleEncoding(x, y, width, height);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
break;
|
|
|
|
|
case HEXTILE_ENCODING:
|
2022-04-07 14:51:09 +02:00
|
|
|
|
ReadHextileEncoding(x, y, width, height);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case CURSOR_PSEUDO_ENCODING:
|
2022-04-07 14:51:09 +02:00
|
|
|
|
ReadCursorPseudoEncoding(x, y, width, height);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case DESKTOP_SIZE_PSEUDO_ENCODING:
|
2022-04-07 14:51:09 +02:00
|
|
|
|
DesktopSize(width, height);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
// Since the desktop size has changed, we want a full buffer update next time
|
2022-04-07 14:51:09 +02:00
|
|
|
|
_incremental = false;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
break;
|
|
|
|
|
|
2017-05-08 18:37:35 +02:00
|
|
|
|
case QEMU_EXT_KEY_ENCODING:
|
|
|
|
|
qemu_ext_key_encoding = true;
|
|
|
|
|
break;
|
|
|
|
|
|
2013-06-24 13:41:48 +02:00
|
|
|
|
default:
|
|
|
|
|
throw new VNCException("unimplemented encoding: " + encoding);
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-07 14:51:09 +02:00
|
|
|
|
Win32.QueryPerformanceCounter(out var end);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
if (end - start > imageUpdateThreshold)
|
|
|
|
|
{
|
|
|
|
|
client.ClientFrameBufferUpdate();
|
|
|
|
|
start = end;
|
|
|
|
|
fb_updated = true;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
fb_updated = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!fb_updated)
|
|
|
|
|
client.ClientFrameBufferUpdate();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2022-04-07 14:51:09 +02:00
|
|
|
|
private void DesktopSize(int width, int height)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
2022-04-07 14:51:09 +02:00
|
|
|
|
_width = width;
|
|
|
|
|
_height = height;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
int neededBytes = width * height * bytesPerPixel;
|
2022-04-07 14:51:09 +02:00
|
|
|
|
if (neededBytes > _data.Length)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
2022-04-07 14:51:09 +02:00
|
|
|
|
_data = new byte[neededBytes];
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
if (bitsPerPixel == 8 && (data_8bpp == null || neededBytes * 4 > data_8bpp.Length))
|
|
|
|
|
{
|
|
|
|
|
data_8bpp = new byte[neededBytes * 4];
|
|
|
|
|
}
|
|
|
|
|
client.ClientDesktopSize(width, height);
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-07 14:51:09 +02:00
|
|
|
|
private void ReadServerCutText()
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
2022-04-07 14:51:09 +02:00
|
|
|
|
stream.readPadding(3);
|
|
|
|
|
String text = stream.readString();
|
2013-06-24 13:41:48 +02:00
|
|
|
|
client.ClientCutText(text);
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-07 14:51:09 +02:00
|
|
|
|
private void ReadServerMessage()
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
|
|
|
|
Log.Debug("readServerMessage");
|
|
|
|
|
|
2022-04-07 14:51:09 +02:00
|
|
|
|
int type = stream.readCard8();
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
|
|
|
|
switch (type)
|
|
|
|
|
{
|
|
|
|
|
case FRAME_BUFFER_UPDATE:
|
|
|
|
|
Log.Debug("Update");
|
2022-04-07 14:51:09 +02:00
|
|
|
|
ReadFrameBufferUpdate();
|
2013-06-24 13:41:48 +02:00
|
|
|
|
break;
|
|
|
|
|
case BELL:
|
|
|
|
|
Log.Debug("Bell");
|
|
|
|
|
client.ClientBell();
|
|
|
|
|
break;
|
|
|
|
|
case SERVER_CUT_TEXT:
|
|
|
|
|
Log.Debug("Cut text");
|
2022-04-07 14:51:09 +02:00
|
|
|
|
ReadServerCutText();
|
2013-06-24 13:41:48 +02:00
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
throw new VNCException("unknown server message: " + type);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-07 14:51:09 +02:00
|
|
|
|
private void Run(object o)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
|
|
|
|
char[] password = (char[])o;
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
2020-04-19 04:59:31 +02:00
|
|
|
|
CheckProtocolVersion();
|
2022-04-07 14:51:09 +02:00
|
|
|
|
SendProtocolVersion();
|
|
|
|
|
AuthenticationExchange(password);
|
|
|
|
|
InitializeClient();
|
|
|
|
|
InitializateServer();
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
|
|
|
|
if (ConnectionSuccess != null)
|
|
|
|
|
ConnectionSuccess(this, null);
|
|
|
|
|
|
|
|
|
|
// Request a full framebuffer update the first time
|
2022-04-07 14:51:09 +02:00
|
|
|
|
_incremental = false;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
|
|
|
|
while (running)
|
|
|
|
|
{
|
2022-04-07 14:51:09 +02:00
|
|
|
|
lock (writeLock)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
2022-04-07 14:51:09 +02:00
|
|
|
|
WriteFramebufferUpdateRequest(0, 0, _width, _height, _incremental);
|
|
|
|
|
stream.Flush();
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
|
2022-04-07 14:51:09 +02:00
|
|
|
|
_incremental = true;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
2022-04-07 14:51:09 +02:00
|
|
|
|
ReadServerMessage();
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
|
|
|
|
lock (pauseMonitor)
|
|
|
|
|
{
|
|
|
|
|
lock(updateMonitor)
|
|
|
|
|
Monitor.PulseAll(updateMonitor);
|
|
|
|
|
|
|
|
|
|
if (paused)
|
|
|
|
|
Monitor.Wait(pauseMonitor);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
2022-04-07 14:51:09 +02:00
|
|
|
|
if (running && ErrorOccurred != null)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
ErrorOccurred(this, e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Close()
|
|
|
|
|
{
|
|
|
|
|
if (!running)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
running = false;
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
stream.Close();
|
|
|
|
|
lock (pauseMonitor)
|
2022-04-07 14:51:09 +02:00
|
|
|
|
Monitor.PulseAll(pauseMonitor);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
thread.Interrupt();
|
|
|
|
|
}
|
|
|
|
|
catch
|
|
|
|
|
{
|
2021-09-10 15:19:08 +02:00
|
|
|
|
// ignored
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Pause()
|
|
|
|
|
{
|
|
|
|
|
paused = true;
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-07 14:51:09 +02:00
|
|
|
|
public void UnPause(bool fullupdate = false)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
2022-04-07 14:51:09 +02:00
|
|
|
|
_incremental = !fullupdate;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
paused = false;
|
2022-04-07 14:51:09 +02:00
|
|
|
|
lock (pauseMonitor)
|
|
|
|
|
Monitor.PulseAll(pauseMonitor);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|