2023-01-24 15:29:31 +01:00
|
|
|
|
/* Copyright (c) Cloud Software Group, Inc.
|
2013-06-24 13:41:48 +02:00
|
|
|
|
*
|
|
|
|
|
* 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.Drawing;
|
|
|
|
|
using System.Drawing.Drawing2D;
|
|
|
|
|
using System.Windows.Forms;
|
|
|
|
|
using System.Runtime.InteropServices;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Text.RegularExpressions;
|
|
|
|
|
using DotNetVnc;
|
|
|
|
|
using XenAdmin.Core;
|
|
|
|
|
using System.Linq;
|
2017-11-17 02:04:45 +01:00
|
|
|
|
using XenCenterLib;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
|
|
|
|
namespace XenAdmin.ConsoleView
|
|
|
|
|
{
|
|
|
|
|
public class VNCGraphicsClient : UserControl, IVNCGraphicsClient, IRemoteConsole
|
|
|
|
|
{
|
2017-02-27 19:00:46 +01:00
|
|
|
|
public const int BORDER_PADDING = 5;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
public const int BORDER_WIDTH = 1;
|
|
|
|
|
|
2022-04-07 14:51:09 +02:00
|
|
|
|
private const int MOUSE_EVENTS_BEFORE_UPDATE = 2;
|
|
|
|
|
private const int MOUSE_EVENTS_DROPPED = 5; // should this be proportional to bandwidth?
|
|
|
|
|
|
|
|
|
|
private const int WM_KEYDOWN = 0x100;
|
|
|
|
|
private const int WM_SYSKEYDOWN = 0x104;
|
|
|
|
|
|
2013-06-24 13:41:48 +02:00
|
|
|
|
private static readonly log4net.ILog Log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
|
|
|
|
|
|
2022-04-05 10:48:06 +02:00
|
|
|
|
public event Action<object, Exception> ErrorOccurred;
|
|
|
|
|
public event EventHandler ConnectionSuccess;
|
2022-04-07 14:51:09 +02:00
|
|
|
|
private VNCStream _vncStream;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// connected implies that vncStream is non-null and ready to be used.
|
|
|
|
|
/// </summary>
|
2022-04-07 14:51:09 +02:00
|
|
|
|
private volatile bool _connected;
|
|
|
|
|
public bool Connected => _connected;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// terminated means that we have been told to disconnect. connected starts off false, becomes
|
|
|
|
|
/// true when a connection is made, and then becomes false again when connection goes away for
|
|
|
|
|
/// whatever reason. In contrast, terminated may become true even before a connection has been
|
|
|
|
|
/// made.
|
|
|
|
|
/// </summary>
|
2022-04-07 14:51:09 +02:00
|
|
|
|
private volatile bool _terminated;
|
|
|
|
|
public bool Terminated => _terminated;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
2022-04-07 14:51:09 +02:00
|
|
|
|
private CustomCursor _remoteCursor;
|
|
|
|
|
private readonly CustomCursor _localCursor = new CustomCursor(Images.StaticImages.vnc_local_cursor, 2, 2);
|
|
|
|
|
private bool _cursorOver;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// This field is locked before any drawing is done through backGraphics or frontGraphics.
|
|
|
|
|
/// It is set in the constructor below, and then there is a delicate handover during desktop
|
|
|
|
|
/// resize to keep this safe.
|
|
|
|
|
/// </summary>
|
2022-04-07 14:51:09 +02:00
|
|
|
|
private Bitmap _backBuffer;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// The contents of the backbuffer are interesting if the backbuffer has been drawn to,
|
|
|
|
|
/// and we will present them to the user even after disconnect (if they are not interesting
|
|
|
|
|
/// we just show a blank rectangle instead).
|
|
|
|
|
/// </summary>
|
2022-04-07 14:51:09 +02:00
|
|
|
|
private volatile bool _backBufferInteresting;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// A Graphics object onto backBuffer. Access to this field must always be locked under backBuffer.
|
|
|
|
|
/// This field will be set to null in Dispose.
|
|
|
|
|
/// </summary>
|
2022-04-07 14:51:09 +02:00
|
|
|
|
private Graphics _backGraphics;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Graphics on actual screen. Access to this field must always be locked under backBuffer.
|
|
|
|
|
/// This field will be set to null in Dispose.
|
|
|
|
|
/// </summary>
|
2022-04-07 14:51:09 +02:00
|
|
|
|
private Graphics _frontGraphics;
|
|
|
|
|
|
|
|
|
|
private readonly object _mouseEventLock = new object();
|
|
|
|
|
|
|
|
|
|
private Rectangle _damage = Rectangle.Empty;
|
|
|
|
|
|
|
|
|
|
private float _scale;
|
|
|
|
|
private float _dx;
|
|
|
|
|
private float _dy;
|
|
|
|
|
private float _oldDx;
|
|
|
|
|
private float _oldDy;
|
|
|
|
|
private int _bump;
|
|
|
|
|
private bool _scaling;
|
|
|
|
|
private bool _sendScanCodes = true;
|
|
|
|
|
private bool _useSource;
|
|
|
|
|
|
|
|
|
|
private static bool _handlingChange;
|
|
|
|
|
private bool _updateClipboardOnFocus;
|
|
|
|
|
private string _clipboardStash = string.Empty;
|
|
|
|
|
private int _currentMouseState;
|
|
|
|
|
/*
|
|
|
|
|
* We're going to track how many moves we have between screen updates
|
|
|
|
|
* to stop the mouse running away with long updates.
|
|
|
|
|
* Unfortunately we sometimes seem to send moves and get no update
|
|
|
|
|
* back, so we cap the number of 'dropped' moves.
|
|
|
|
|
*/
|
|
|
|
|
private volatile int _mouseMoved;
|
|
|
|
|
private volatile int _mouseNotMoved;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
2022-04-07 14:51:09 +02:00
|
|
|
|
private MouseEventArgs _pending ;
|
|
|
|
|
private int _pendingState;
|
|
|
|
|
private int _lastState;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
2022-04-07 14:51:09 +02:00
|
|
|
|
private Set<Keys> _pressedKeys = new Set<Keys>();
|
|
|
|
|
private Set<int> _pressedScans = new Set<int>();
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
2022-04-07 14:51:09 +02:00
|
|
|
|
private bool _modifierKeyPressedAlone;
|
|
|
|
|
private bool _helperIsPaused = true;
|
|
|
|
|
private bool _displayBorder = true;
|
|
|
|
|
private bool _altGrReleaseSent;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
|
|
|
|
public bool UseSource
|
|
|
|
|
{
|
2022-04-07 14:51:09 +02:00
|
|
|
|
set => _useSource = value;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
|
2022-04-07 14:51:09 +02:00
|
|
|
|
private bool TalkingToVNCTerm => !_sendScanCodes && _useSource;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
2022-04-05 10:48:06 +02:00
|
|
|
|
public string UUID => SourceVM.uuid;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
2022-04-05 10:48:06 +02:00
|
|
|
|
public XenAPI.VM SourceVM { get; set; }
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
2022-04-05 10:48:06 +02:00
|
|
|
|
public string VmName => SourceVM.name_label;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
|
|
|
|
public object Console;
|
|
|
|
|
|
2022-04-05 10:48:06 +02:00
|
|
|
|
public event EventHandler DesktopResized;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
2017-06-08 10:38:09 +02:00
|
|
|
|
public bool UseQemuExtKeyEncoding { set; private get; }
|
|
|
|
|
|
2013-06-24 13:41:48 +02:00
|
|
|
|
public VNCGraphicsClient(ContainerControl parent)
|
|
|
|
|
{
|
|
|
|
|
Program.AssertOnEventThread();
|
|
|
|
|
|
|
|
|
|
this.SetStyle(ControlStyles.AllPaintingInWmPaint
|
|
|
|
|
| ControlStyles.UserPaint
|
|
|
|
|
| ControlStyles.ResizeRedraw, true);
|
|
|
|
|
SetStyle(ControlStyles.Opaque, false);
|
|
|
|
|
|
2022-04-07 14:51:09 +02:00
|
|
|
|
_frontGraphics = CreateGraphics();
|
|
|
|
|
SetupGraphicsOptions(_frontGraphics);
|
|
|
|
|
_backBuffer = new Bitmap(640, 480, _frontGraphics);
|
|
|
|
|
_backGraphics = Graphics.FromImage(_backBuffer);
|
|
|
|
|
SetupGraphicsOptions(_backGraphics);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
using (SolidBrush backBrush = new SolidBrush(BackColor))
|
|
|
|
|
{
|
2022-04-07 14:51:09 +02:00
|
|
|
|
_backGraphics.FillRectangle(backBrush, 0, 0, 640, 480);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
DesktopSize = new Size(640, 480);
|
|
|
|
|
|
|
|
|
|
#pragma warning disable 0219
|
|
|
|
|
IntPtr _ = Handle;
|
|
|
|
|
#pragma warning restore 0219
|
|
|
|
|
|
|
|
|
|
Clip.ClipboardChanged += ClipboardChanged;
|
|
|
|
|
parent.Controls.Add(this);
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-05 10:48:06 +02:00
|
|
|
|
private static void SetupGraphicsOptions(Graphics g)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
|
|
|
|
g.CompositingQuality = CompositingQuality.AssumeLinear;
|
|
|
|
|
g.InterpolationMode = InterpolationMode.Default;
|
|
|
|
|
g.SmoothingMode = SmoothingMode.Default;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override void Dispose(bool disposing)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
if (!disposing)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
Clip.ClipboardChanged -= ClipboardChanged;
|
|
|
|
|
|
|
|
|
|
Disconnect();
|
|
|
|
|
|
2022-04-07 14:51:09 +02:00
|
|
|
|
lock (_backBuffer)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
2022-04-07 14:51:09 +02:00
|
|
|
|
_frontGraphics.Dispose();
|
|
|
|
|
_backGraphics.Dispose();
|
|
|
|
|
_backBuffer.Dispose();
|
|
|
|
|
_backGraphics = null;
|
|
|
|
|
_frontGraphics = null;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
|
2022-04-07 14:51:09 +02:00
|
|
|
|
if (_remoteCursor != null)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
2022-04-07 14:51:09 +02:00
|
|
|
|
_remoteCursor.Dispose();
|
|
|
|
|
_remoteCursor = null;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
|
|
|
|
base.Dispose(disposing);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-05 10:48:06 +02:00
|
|
|
|
internal void Connect(Stream stream, char[] password)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
|
|
|
|
Program.AssertOnEventThread();
|
|
|
|
|
|
2022-04-07 14:51:09 +02:00
|
|
|
|
_vncStream = new VNCStream(this, stream, _helperIsPaused);
|
|
|
|
|
_vncStream.ErrorOccurred += OnError;
|
|
|
|
|
_vncStream.ConnectionSuccess += vncStream_ConnectionSuccess;
|
|
|
|
|
_connected = true;
|
|
|
|
|
_vncStream.Connect(password);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void vncStream_ConnectionSuccess(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
Program.AssertOffEventThread();
|
|
|
|
|
|
|
|
|
|
// Set the remote clipboard based on the current contents.
|
2015-04-16 15:17:02 +02:00
|
|
|
|
Program.Invoke(this, (EventHandler)ClipboardChanged, null, null);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
|
|
|
|
if (ConnectionSuccess != null)
|
2015-04-16 15:17:02 +02:00
|
|
|
|
Program.Invoke(this, ConnectionSuccess, sender, e);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void OnError(object sender, Exception e)
|
|
|
|
|
{
|
|
|
|
|
Program.AssertOffEventThread();
|
2022-04-07 14:51:09 +02:00
|
|
|
|
System.Diagnostics.Debug.Assert(sender == _vncStream); // Please see to CA-236844 if this assertion fails
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
2022-04-07 14:51:09 +02:00
|
|
|
|
if (sender != _vncStream)
|
2017-02-01 13:55:00 +01:00
|
|
|
|
return;
|
|
|
|
|
|
2022-04-07 14:51:09 +02:00
|
|
|
|
_connected = false;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
if (ErrorOccurred != null)
|
|
|
|
|
ErrorOccurred(this, e);
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-05 10:48:06 +02:00
|
|
|
|
private void Disconnect()
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
2022-04-07 14:51:09 +02:00
|
|
|
|
_connected = false;
|
|
|
|
|
_terminated = true;
|
|
|
|
|
if (_vncStream != null)
|
2017-02-01 13:55:00 +01:00
|
|
|
|
{
|
2022-04-07 14:51:09 +02:00
|
|
|
|
_vncStream.ErrorOccurred -= OnError;
|
|
|
|
|
_vncStream.ConnectionSuccess -= vncStream_ConnectionSuccess;
|
2017-02-01 13:55:00 +01:00
|
|
|
|
}
|
2022-04-07 14:51:09 +02:00
|
|
|
|
VNCStream s = _vncStream;
|
|
|
|
|
_vncStream = null;
|
2022-04-05 10:48:06 +02:00
|
|
|
|
s?.Close();
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private bool RedirectingClipboard()
|
|
|
|
|
{
|
2022-04-05 10:48:06 +02:00
|
|
|
|
return Properties.Settings.Default.ClipboardAndPrinterRedirection;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void ClipboardChanged(object sender, EventArgs args)
|
|
|
|
|
{
|
|
|
|
|
Program.AssertOnEventThread();
|
|
|
|
|
|
|
|
|
|
if (!RedirectingClipboard())
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
2022-04-07 14:51:09 +02:00
|
|
|
|
if (!_handlingChange && _connected)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
|
|
|
|
if (Focused)
|
2022-04-05 10:48:06 +02:00
|
|
|
|
SetConsoleClipboard();
|
2013-06-24 13:41:48 +02:00
|
|
|
|
else
|
2022-04-07 14:51:09 +02:00
|
|
|
|
_updateClipboardOnFocus = true;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
2022-04-05 10:48:06 +02:00
|
|
|
|
catch (IOException)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
|
|
|
|
// The server's gone away -- that's fine.
|
|
|
|
|
}
|
|
|
|
|
catch (Exception exn)
|
|
|
|
|
{
|
|
|
|
|
Log.Warn(exn, exn);
|
|
|
|
|
// Nothing more we can do with this.
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-05 10:48:06 +02:00
|
|
|
|
private void SetConsoleClipboard()
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2022-04-07 14:51:09 +02:00
|
|
|
|
_handlingChange = true;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
string text = Clip.ClipboardText;
|
2022-04-07 14:51:09 +02:00
|
|
|
|
if (TalkingToVNCTerm)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
text = text.Replace("\r\n", "\n");
|
2022-04-07 14:51:09 +02:00
|
|
|
|
_vncStream.ClientCutText(text);
|
|
|
|
|
_updateClipboardOnFocus = false;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
2022-04-07 14:51:09 +02:00
|
|
|
|
_handlingChange = false;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Records damage to the screen.
|
|
|
|
|
/// </summary>
|
|
|
|
|
private void Damage(int x, int y, int width, int height)
|
|
|
|
|
{
|
|
|
|
|
Program.AssertOffEventThread();
|
|
|
|
|
|
|
|
|
|
Rectangle r = new Rectangle(x, y, width, height);
|
|
|
|
|
|
2022-04-07 14:51:09 +02:00
|
|
|
|
if (_scaling)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
2022-04-07 14:51:09 +02:00
|
|
|
|
r.Inflate(_bump, _bump); // Fix for scaling issues
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
|
2022-04-07 14:51:09 +02:00
|
|
|
|
if (_damage.IsEmpty)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
2022-04-07 14:51:09 +02:00
|
|
|
|
_damage = r;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2022-04-07 14:51:09 +02:00
|
|
|
|
_damage = Rectangle.Union(_damage, r);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-07 14:51:09 +02:00
|
|
|
|
private void CheckAssertion(bool assertion, String message, params Object[] args)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
|
|
|
|
if (!assertion)
|
|
|
|
|
{
|
|
|
|
|
Log.Error("Bad VNC server message: " + String.Format(message, args));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#region Functions called by vncStream to update display
|
|
|
|
|
|
|
|
|
|
public void ClientDrawImage(Bitmap image, int x, int y, int width, int height)
|
|
|
|
|
{
|
|
|
|
|
Program.AssertOffEventThread();
|
|
|
|
|
|
|
|
|
|
Damage(x, y, width, height);
|
2022-04-07 14:51:09 +02:00
|
|
|
|
lock (_backBuffer)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2022-04-07 14:51:09 +02:00
|
|
|
|
if (_backGraphics != null)
|
|
|
|
|
_backGraphics.DrawImageUnscaled(image, x, y);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
2022-04-05 10:48:06 +02:00
|
|
|
|
// We seem to be very occasionally getting weird exception from this. These are probably due to
|
2013-06-24 13:41:48 +02:00
|
|
|
|
// bad server messages, so we can just log and ignore them
|
|
|
|
|
|
|
|
|
|
Log.Error("Error drawing image from server", e);
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
2022-04-07 14:51:09 +02:00
|
|
|
|
CheckAssertion(image.Width == width, "Width {0} != {1}", image.Width, width);
|
|
|
|
|
CheckAssertion(image.Height == height, "Height {0} != {1}", image.Height, height);
|
|
|
|
|
CheckAssertion(x < DesktopSize.Width, "x {0} >= {1}", x, DesktopSize.Width);
|
|
|
|
|
CheckAssertion(y < DesktopSize.Height, "y {0} >= {1}", y, DesktopSize.Height);
|
|
|
|
|
CheckAssertion(x + width <= DesktopSize.Width, "x + width {0} + {1} > {2}", x, width, DesktopSize.Width);
|
|
|
|
|
CheckAssertion(y + height <= DesktopSize.Height, "y + height {0} + {1} > {2}", y, height, DesktopSize.Height);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
catch
|
|
|
|
|
{
|
2021-09-10 15:19:08 +02:00
|
|
|
|
// ignored
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region Custom Cursor handling
|
|
|
|
|
|
|
|
|
|
// Taken from
|
|
|
|
|
// http://www.codeproject.com/cs/miscctrl/DragDropTreeview.asp?df=100&forumid=84437&exp=0&select=1838138#xx1838138xx
|
|
|
|
|
|
|
|
|
|
[StructLayout(LayoutKind.Sequential)]
|
2022-04-07 14:51:09 +02:00
|
|
|
|
public struct IconInfo
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
|
|
|
|
public bool fIcon;
|
|
|
|
|
public int xHotspot;
|
|
|
|
|
public int yHotspot;
|
|
|
|
|
public IntPtr hbmMask;
|
|
|
|
|
public IntPtr hbmColor;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[DllImport("user32.dll")]
|
2022-04-07 14:51:09 +02:00
|
|
|
|
public static extern IntPtr CreateIconIndirect(ref IconInfo iconinfo);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
|
|
|
|
[DllImport("user32.dll")]
|
|
|
|
|
public static extern bool DestroyIcon(IntPtr hIcon);
|
|
|
|
|
|
2022-04-07 14:51:09 +02:00
|
|
|
|
private class CustomCursor : IDisposable
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
2022-04-07 14:51:09 +02:00
|
|
|
|
private IntPtr _handle = IntPtr.Zero;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
|
|
|
|
internal CustomCursor(Bitmap bitmap, int x, int y)
|
|
|
|
|
{
|
2022-04-07 14:51:09 +02:00
|
|
|
|
var iconInfo = new IconInfo
|
|
|
|
|
{
|
|
|
|
|
fIcon = false,
|
|
|
|
|
xHotspot = x,
|
|
|
|
|
yHotspot = y,
|
|
|
|
|
hbmMask = bitmap.GetHbitmap(),
|
|
|
|
|
hbmColor = bitmap.GetHbitmap()
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
_handle = CreateIconIndirect(ref iconInfo);
|
|
|
|
|
Cursor = new Cursor(_handle);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
~CustomCursor()
|
|
|
|
|
{
|
|
|
|
|
Dispose(false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Dispose()
|
|
|
|
|
{
|
|
|
|
|
GC.SuppressFinalize(this);
|
|
|
|
|
Dispose(true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected virtual void Dispose(bool disposing)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2022-04-07 14:51:09 +02:00
|
|
|
|
if (_handle != IntPtr.Zero)
|
|
|
|
|
DestroyIcon(_handle);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
catch
|
|
|
|
|
{
|
2021-09-10 15:19:08 +02:00
|
|
|
|
// ignored
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
2022-04-07 14:51:09 +02:00
|
|
|
|
_handle = IntPtr.Zero;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-07 14:51:09 +02:00
|
|
|
|
public Cursor Cursor { get; }
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void ClientSetCursor(Bitmap image, int x, int y, int width, int height)
|
|
|
|
|
{
|
|
|
|
|
Program.AssertOffEventThread();
|
|
|
|
|
|
2022-04-07 14:51:09 +02:00
|
|
|
|
_remoteCursor?.Dispose();
|
|
|
|
|
_remoteCursor = new CustomCursor(image, x, y);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
2022-04-05 10:48:06 +02:00
|
|
|
|
Program.Invoke(this, () =>
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
2022-04-07 14:51:09 +02:00
|
|
|
|
if (_cursorOver)
|
|
|
|
|
Cursor = _remoteCursor.Cursor;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
public void ClientCopyRectangle(int x, int y, int width, int height, int dx, int dy)
|
|
|
|
|
{
|
|
|
|
|
Program.AssertOffEventThread();
|
|
|
|
|
|
|
|
|
|
Damage(dx, dy, width, height);
|
2022-04-07 14:51:09 +02:00
|
|
|
|
lock (_backBuffer)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
2022-04-07 14:51:09 +02:00
|
|
|
|
if (_backGraphics != null)
|
|
|
|
|
GraphicsUtils.copyRect(_backBuffer, x, y, width, height, _backGraphics, dx, dy);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void ClientFillRectangle(int x, int y, int width, int height, Color color)
|
|
|
|
|
{
|
|
|
|
|
Program.AssertOffEventThread();
|
|
|
|
|
|
|
|
|
|
Damage(x, y, width, height);
|
2022-04-07 14:51:09 +02:00
|
|
|
|
lock (_backBuffer)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
2022-04-07 14:51:09 +02:00
|
|
|
|
if (_backGraphics != null)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
|
|
|
|
using (SolidBrush backBrush = new SolidBrush(color))
|
|
|
|
|
{
|
2022-04-07 14:51:09 +02:00
|
|
|
|
_backGraphics.FillRectangle(backBrush, x, y, width, height);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void ClientFrameBufferUpdate()
|
|
|
|
|
{
|
|
|
|
|
Program.AssertOffEventThread();
|
|
|
|
|
|
2022-04-07 14:51:09 +02:00
|
|
|
|
lock (_backBuffer)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2022-04-07 14:51:09 +02:00
|
|
|
|
if (!_damage.IsEmpty)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
2022-04-07 14:51:09 +02:00
|
|
|
|
if (_frontGraphics != null)
|
|
|
|
|
_frontGraphics.DrawImage(_backBuffer, _damage, _damage, GraphicsUnit.Pixel);
|
|
|
|
|
_damage = Rectangle.Empty;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
|
2022-04-07 14:51:09 +02:00
|
|
|
|
_backBufferInteresting = true;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
catch
|
|
|
|
|
{
|
2021-09-10 15:19:08 +02:00
|
|
|
|
// ignored
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
2022-04-05 10:48:06 +02:00
|
|
|
|
* If there is a pending mouse event, send it.
|
2013-06-24 13:41:48 +02:00
|
|
|
|
* Also reset the mouse event counters
|
|
|
|
|
*/
|
2022-04-07 14:51:09 +02:00
|
|
|
|
lock (_mouseEventLock)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
2022-04-07 14:51:09 +02:00
|
|
|
|
if (_pending != null)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
2022-04-07 14:51:09 +02:00
|
|
|
|
MouseEvent(_pendingState, _pending.X, _pending.Y);
|
|
|
|
|
_pending = null;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
|
2022-04-07 14:51:09 +02:00
|
|
|
|
_mouseMoved = 0;
|
|
|
|
|
_mouseNotMoved = 0;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void ClientBell()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void ClientCutText(String text)
|
|
|
|
|
{
|
|
|
|
|
Program.AssertOffEventThread();
|
|
|
|
|
|
2022-04-07 14:51:09 +02:00
|
|
|
|
if (TalkingToVNCTerm)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
|
|
|
|
// Lets translate from unix line endings to windows ones...
|
2022-04-07 14:51:09 +02:00
|
|
|
|
_clipboardStash = Regex.Replace(text, "\r?\n", "\r\n");
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2015-04-22 10:46:42 +02:00
|
|
|
|
if (!RedirectingClipboard())
|
|
|
|
|
return;
|
2022-04-05 10:48:06 +02:00
|
|
|
|
Program.Invoke(this, () =>
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
|
|
|
|
if (Clipboard.ContainsText() && Clipboard.GetText() == text)
|
|
|
|
|
return;
|
2016-10-13 17:16:16 +02:00
|
|
|
|
Clip.SetClipboardText(text);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void ClientDesktopSize(int width, int height)
|
|
|
|
|
{
|
|
|
|
|
Program.AssertOffEventThread();
|
|
|
|
|
|
|
|
|
|
// Cannot do an invoke with a locked back buffer, as it may event thread
|
|
|
|
|
// (onPaint) tried to lock back buffer as well - therefore deadlock.
|
|
|
|
|
|
2022-04-05 10:48:06 +02:00
|
|
|
|
Program.Invoke(this, () =>
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
2022-04-05 10:48:06 +02:00
|
|
|
|
Bitmap oldBackBuffer;
|
2022-04-07 14:51:09 +02:00
|
|
|
|
lock (_backBuffer)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
|
|
|
|
if (width <= 0)
|
|
|
|
|
width = 1;
|
|
|
|
|
if (height <= 0)
|
|
|
|
|
height = 1;
|
|
|
|
|
|
|
|
|
|
if (width == DesktopSize.Width &&
|
|
|
|
|
height == DesktopSize.Height)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-07 14:51:09 +02:00
|
|
|
|
oldBackBuffer = _backBuffer;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
2022-04-07 14:51:09 +02:00
|
|
|
|
_backGraphics.Dispose();
|
|
|
|
|
_frontGraphics.Dispose();
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
2022-04-07 14:51:09 +02:00
|
|
|
|
_frontGraphics = CreateGraphics();
|
|
|
|
|
SetupGraphicsOptions(_frontGraphics);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
2022-04-07 14:51:09 +02:00
|
|
|
|
Bitmap newBackBuffer = new Bitmap(width, height, _frontGraphics);
|
|
|
|
|
_backGraphics = Graphics.FromImage(newBackBuffer);
|
|
|
|
|
SetupGraphicsOptions(_backGraphics);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
|
|
|
|
using (SolidBrush backBrush = new SolidBrush(BackColor))
|
|
|
|
|
{
|
2022-04-07 14:51:09 +02:00
|
|
|
|
_backGraphics.FillRectangle(backBrush, 0, 0, width, height);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
DesktopSize = new Size(width, height);
|
|
|
|
|
|
|
|
|
|
// Now that backGraphics is valid, we can switch backBuffer. We're relying on backBuffer
|
|
|
|
|
// as our lock.
|
2022-04-07 14:51:09 +02:00
|
|
|
|
_backBuffer = newBackBuffer;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
|
2022-04-07 14:51:09 +02:00
|
|
|
|
lock (_backBuffer)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
|
|
|
|
SetupScaling();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Invalidate();
|
|
|
|
|
Update();
|
|
|
|
|
|
2022-04-05 10:48:06 +02:00
|
|
|
|
oldBackBuffer.Dispose();
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
|
|
|
|
OnDesktopResized();
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void OnDesktopResized()
|
|
|
|
|
{
|
|
|
|
|
Program.AssertOnEventThread();
|
|
|
|
|
|
|
|
|
|
if (DesktopResized != null)
|
|
|
|
|
DesktopResized(this, null);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override void OnScroll(ScrollEventArgs se)
|
|
|
|
|
{
|
|
|
|
|
base.OnScroll(se);
|
|
|
|
|
|
2022-04-07 14:51:09 +02:00
|
|
|
|
if (!_scaling && se.OldValue != se.NewValue)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
|
|
|
|
if (se.ScrollOrientation == ScrollOrientation.HorizontalScroll)
|
|
|
|
|
{
|
2022-04-07 14:51:09 +02:00
|
|
|
|
_dx = -1 * se.NewValue + (_displayBorder ? BORDER_PADDING : 0);
|
|
|
|
|
lock (_backBuffer)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
2022-04-07 14:51:09 +02:00
|
|
|
|
if (_frontGraphics != null)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
2022-04-07 14:51:09 +02:00
|
|
|
|
_frontGraphics.ResetTransform();
|
|
|
|
|
_frontGraphics.TranslateTransform(_dx, _dy);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (se.ScrollOrientation == ScrollOrientation.VerticalScroll)
|
|
|
|
|
{
|
2022-04-07 14:51:09 +02:00
|
|
|
|
_dy = (-1 * se.NewValue) + (_displayBorder ? BORDER_PADDING : 0);
|
|
|
|
|
lock (_backBuffer)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
2022-04-07 14:51:09 +02:00
|
|
|
|
if (_frontGraphics != null)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
2022-04-07 14:51:09 +02:00
|
|
|
|
_frontGraphics.ResetTransform();
|
|
|
|
|
_frontGraphics.TranslateTransform(_dx, _dy);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this.Refresh();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void MyPaintBackground(Graphics g, int x, int y, int width, int height)
|
|
|
|
|
{
|
|
|
|
|
Rectangle r = new Rectangle(x, y, width, height);
|
|
|
|
|
|
|
|
|
|
// Hack pulled from Control.cs:PaintTransparentBackground
|
|
|
|
|
if (Application.RenderWithVisualStyles)
|
|
|
|
|
{
|
|
|
|
|
ButtonRenderer.DrawParentBackground(g, r, this);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
using (Brush backBrush = new SolidBrush(BackColor))
|
|
|
|
|
{
|
|
|
|
|
g.FillRectangle(backBrush, r);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override void OnPaintBackground(PaintEventArgs e)
|
|
|
|
|
{
|
2022-04-05 10:48:06 +02:00
|
|
|
|
//do nothing
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override void OnPaint(PaintEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
Program.AssertOnEventThread();
|
|
|
|
|
|
2022-04-07 14:51:09 +02:00
|
|
|
|
if (_connected || _backBufferInteresting)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
|
|
|
|
/*
|
|
|
|
|
* Draw the background by working out the surrounding bars
|
|
|
|
|
* as opposed to just one big black rect, to reduce flicker.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
// Draw two vertical bars at either end
|
|
|
|
|
|
2022-04-07 14:51:09 +02:00
|
|
|
|
int w = (int)_dx;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
|
|
|
|
Graphics g = e.Graphics;
|
|
|
|
|
|
2022-04-07 14:51:09 +02:00
|
|
|
|
lock (_backBuffer)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
2022-04-07 14:51:09 +02:00
|
|
|
|
if (_frontGraphics != null)
|
|
|
|
|
_frontGraphics.DrawImageUnscaled(_backBuffer, 0, 0);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (w > 0)
|
|
|
|
|
MyPaintBackground(g, 0, 0, w + 1, ClientSize.Height);
|
|
|
|
|
|
2022-04-07 14:51:09 +02:00
|
|
|
|
w = (int)(ClientSize.Width - _dx - (DesktopSize.Width * _scale)) + 1;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
|
|
|
|
if (w > 0)
|
|
|
|
|
MyPaintBackground(g, ClientSize.Width - w, 0, w, ClientSize.Height);
|
|
|
|
|
|
|
|
|
|
// Draw two horizontal bars at top and bottom
|
|
|
|
|
|
2022-04-07 14:51:09 +02:00
|
|
|
|
int h = (int)_dy;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
|
|
|
|
if (h > 0)
|
|
|
|
|
MyPaintBackground(g, 0, 0, Size.Width, h + 1);
|
|
|
|
|
|
2022-04-07 14:51:09 +02:00
|
|
|
|
h = (int)(ClientSize.Height - _dy - (DesktopSize.Height * _scale)) + 1;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
|
|
|
|
if (h > 0)
|
|
|
|
|
MyPaintBackground(g, 0, ClientSize.Height - h, Size.Width, h);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
base.OnPaintBackground(e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override void OnResize(EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
Program.AssertOnEventThread();
|
|
|
|
|
base.OnResize(e);
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Need to recreate the front graphics or we
|
|
|
|
|
* get weird clipping problems.
|
|
|
|
|
*/
|
2022-04-07 14:51:09 +02:00
|
|
|
|
lock (_backBuffer)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
2022-04-07 14:51:09 +02:00
|
|
|
|
if (_frontGraphics != null)
|
|
|
|
|
_frontGraphics.Dispose();
|
|
|
|
|
_frontGraphics = CreateGraphics();
|
|
|
|
|
SetupGraphicsOptions(_frontGraphics);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
SetupScaling();
|
|
|
|
|
|
|
|
|
|
// We don't need to redraw, as the window will repaint us
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-05 10:48:06 +02:00
|
|
|
|
private void MouseEvent(int state, int x, int y)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
2022-04-05 10:48:06 +02:00
|
|
|
|
DoIfConnected(() =>
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
2022-04-07 14:51:09 +02:00
|
|
|
|
if (TalkingToVNCTerm && _lastState == 4 && state == 0)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
|
|
|
|
ShowPopupMenu(x, y);
|
|
|
|
|
}
|
2022-04-07 14:51:09 +02:00
|
|
|
|
else if (_scaling)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
2022-04-07 14:51:09 +02:00
|
|
|
|
_vncStream.PointerEvent(state, (int)((x - _dx) / _scale), (int)((y - _dy) / _scale));
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2022-04-07 14:51:09 +02:00
|
|
|
|
_vncStream.PointerEvent(state, x - (int)_dx, y - (int)_dy);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
|
2022-04-07 14:51:09 +02:00
|
|
|
|
_lastState = state;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void ShowPopupMenu(int x, int y)
|
|
|
|
|
{
|
|
|
|
|
ToolStripDropDownMenu popupMenu = new ToolStripDropDownMenu();
|
|
|
|
|
|
|
|
|
|
ToolStripMenuItem copyItem = new ToolStripMenuItem(Messages.COPY);
|
2017-06-16 09:55:38 +02:00
|
|
|
|
|
2020-06-18 02:20:29 +02:00
|
|
|
|
copyItem.Image = Images.StaticImages.copy_16;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
copyItem.Click += copyItem_Click;
|
2017-06-16 09:55:38 +02:00
|
|
|
|
|
2013-06-24 13:41:48 +02:00
|
|
|
|
popupMenu.Items.Add(copyItem);
|
2022-04-05 10:48:06 +02:00
|
|
|
|
if (SourceVM != null && SourceVM.power_state == XenAPI.vm_power_state.Running)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
|
|
|
|
ToolStripMenuItem pasteItem = new ToolStripMenuItem(Messages.PASTE);
|
2020-06-18 02:20:29 +02:00
|
|
|
|
pasteItem.Image = Images.StaticImages.paste_16;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
pasteItem.Click += pasteItem_Click;
|
|
|
|
|
|
|
|
|
|
popupMenu.Items.Add(pasteItem);
|
|
|
|
|
}
|
|
|
|
|
popupMenu.Show(this, x, y);
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-05 10:48:06 +02:00
|
|
|
|
private void copyItem_Click(object sender, EventArgs e)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
2022-04-07 14:51:09 +02:00
|
|
|
|
System.Diagnostics.Trace.Assert(TalkingToVNCTerm);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
Program.AssertOnEventThread();
|
2022-04-07 14:51:09 +02:00
|
|
|
|
if (_clipboardStash != "")
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
2022-04-07 14:51:09 +02:00
|
|
|
|
Clip.SetClipboardText(_clipboardStash);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-05 10:48:06 +02:00
|
|
|
|
private void pasteItem_Click(object sender, EventArgs e)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
2022-04-07 14:51:09 +02:00
|
|
|
|
System.Diagnostics.Trace.Assert(TalkingToVNCTerm);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
Program.AssertOnEventThread();
|
|
|
|
|
|
|
|
|
|
if (Clipboard.ContainsText())
|
|
|
|
|
{
|
2022-04-07 14:51:09 +02:00
|
|
|
|
_vncStream.ClientCutText(Clipboard.GetText().Replace("\r\n", "\n"));
|
2022-04-05 10:48:06 +02:00
|
|
|
|
MouseEvent(2, 0, 0);
|
|
|
|
|
MouseEvent(0, 0, 0);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-05 10:48:06 +02:00
|
|
|
|
private void DisableMenuShortcuts()
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
2018-10-01 18:01:59 +02:00
|
|
|
|
Program.MainWindow.MenuShortcutsEnabled = false;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
|
2022-04-05 10:48:06 +02:00
|
|
|
|
private void EnableMenuShortcuts()
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
2018-10-01 18:01:59 +02:00
|
|
|
|
Program.MainWindow.MenuShortcutsEnabled = true;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override void OnGotFocus(EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
Program.AssertOnEventThread();
|
|
|
|
|
base.OnGotFocus(e);
|
|
|
|
|
|
|
|
|
|
DisableMenuShortcuts();
|
|
|
|
|
|
2022-04-07 14:51:09 +02:00
|
|
|
|
if (_sendScanCodes)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
2022-04-07 14:51:09 +02:00
|
|
|
|
InterceptKeys.grabKeys(KeyScan, false);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
|
2022-04-07 14:51:09 +02:00
|
|
|
|
if (_updateClipboardOnFocus)
|
2022-04-05 10:48:06 +02:00
|
|
|
|
SetConsoleClipboard();
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override void OnLostFocus(EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
Program.AssertOnEventThread();
|
|
|
|
|
base.OnLostFocus(e);
|
|
|
|
|
|
|
|
|
|
EnableMenuShortcuts();
|
|
|
|
|
|
|
|
|
|
InterceptKeys.releaseKeys();
|
|
|
|
|
|
2022-04-07 14:51:09 +02:00
|
|
|
|
_cursorOver = false;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
|
|
|
|
Cursor = Cursors.Default;
|
|
|
|
|
|
|
|
|
|
//Release any held keys
|
2022-04-05 10:48:06 +02:00
|
|
|
|
DoIfConnected(() =>
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
2022-04-07 14:51:09 +02:00
|
|
|
|
foreach (Keys key in _pressedKeys)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
|
|
|
|
// This won't release composite key events atm.
|
|
|
|
|
int sym = KeyMap.translateKey(ConsoleKeyHandler.GetSimpleKey(key));
|
|
|
|
|
if (sym > 0)
|
2022-04-07 14:51:09 +02:00
|
|
|
|
_vncStream.keyCodeEvent(false, sym);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
|
2022-04-07 14:51:09 +02:00
|
|
|
|
foreach (int key in _pressedScans)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
2022-04-07 14:51:09 +02:00
|
|
|
|
_vncStream.keyScanEvent(false, key, -1, UseQemuExtKeyEncoding);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2022-04-07 14:51:09 +02:00
|
|
|
|
_pressedKeys = new Set<Keys>();
|
|
|
|
|
_pressedScans = new Set<int>();
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override void OnMouseMove(MouseEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
// Only hide the mouse when over the actual screen,
|
|
|
|
|
// some parts of this control will be window blinds
|
|
|
|
|
|
|
|
|
|
if (Focused && Connected)
|
|
|
|
|
{
|
2022-04-07 14:51:09 +02:00
|
|
|
|
int top = (int)_dy;
|
|
|
|
|
int left = (int)_dx;
|
|
|
|
|
int bottom = (int)((DesktopSize.Height * _scale) + top);
|
|
|
|
|
int right = (int)((DesktopSize.Width * _scale) + left);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
|
|
|
|
if (e.X > left && e.X < right
|
|
|
|
|
&& e.Y > top && e.Y < bottom)
|
|
|
|
|
{
|
2022-04-07 14:51:09 +02:00
|
|
|
|
_cursorOver = true;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
2022-04-07 14:51:09 +02:00
|
|
|
|
if (_remoteCursor == null)
|
|
|
|
|
Cursor = _localCursor.Cursor;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
else
|
2022-04-07 14:51:09 +02:00
|
|
|
|
Cursor = _remoteCursor.Cursor;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
2022-04-07 14:51:09 +02:00
|
|
|
|
lock (_mouseEventLock)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
2022-04-07 14:51:09 +02:00
|
|
|
|
if (_mouseMoved < MOUSE_EVENTS_BEFORE_UPDATE)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
2022-04-07 14:51:09 +02:00
|
|
|
|
_mouseMoved++;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
2022-04-07 14:51:09 +02:00
|
|
|
|
MouseEvent(_currentMouseState, e.X, e.Y);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
2022-04-07 14:51:09 +02:00
|
|
|
|
else if (_mouseNotMoved > MOUSE_EVENTS_DROPPED)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
2022-04-07 14:51:09 +02:00
|
|
|
|
_mouseMoved = 0;
|
|
|
|
|
_mouseNotMoved = 0;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2022-04-07 14:51:09 +02:00
|
|
|
|
_mouseNotMoved++;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
2022-04-07 14:51:09 +02:00
|
|
|
|
_pendingState = _currentMouseState;
|
|
|
|
|
_pending = e;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2022-04-07 14:51:09 +02:00
|
|
|
|
_cursorOver = false;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
Cursor = Cursors.Default;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-05 10:48:06 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// We're not going to buffer any clicks etc.
|
|
|
|
|
/// Don't forget to clear any pending moves.
|
|
|
|
|
/// </summary>
|
2013-06-24 13:41:48 +02:00
|
|
|
|
protected override void OnMouseDown(MouseEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
//added this line to track mouse clicks for automatic switch to RDP
|
2022-04-05 10:48:06 +02:00
|
|
|
|
//if there are any issues with focus start looking here
|
2013-06-24 13:41:48 +02:00
|
|
|
|
base.OnMouseDown(e);
|
2022-04-05 10:48:06 +02:00
|
|
|
|
Focus();
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
2022-04-07 14:51:09 +02:00
|
|
|
|
_pending = null;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
switch (e.Button)
|
|
|
|
|
{
|
|
|
|
|
case MouseButtons.Left:
|
2022-04-07 14:51:09 +02:00
|
|
|
|
_currentMouseState |= 1;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
break;
|
|
|
|
|
case MouseButtons.Right:
|
2022-04-07 14:51:09 +02:00
|
|
|
|
_currentMouseState |= 4;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
break;
|
|
|
|
|
case MouseButtons.Middle:
|
2022-04-07 14:51:09 +02:00
|
|
|
|
_currentMouseState |= 2;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
break;
|
|
|
|
|
}
|
2022-04-07 14:51:09 +02:00
|
|
|
|
MouseEvent(_currentMouseState, e.X, e.Y);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override void OnMouseUp(MouseEventArgs e)
|
|
|
|
|
{
|
2022-04-07 14:51:09 +02:00
|
|
|
|
_pending = null;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
switch (e.Button)
|
|
|
|
|
{
|
|
|
|
|
case MouseButtons.Left:
|
2022-04-07 14:51:09 +02:00
|
|
|
|
_currentMouseState &= ~1;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
break;
|
|
|
|
|
case MouseButtons.Right:
|
2022-04-07 14:51:09 +02:00
|
|
|
|
_currentMouseState &= ~4;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
break;
|
|
|
|
|
case MouseButtons.Middle:
|
2022-04-07 14:51:09 +02:00
|
|
|
|
_currentMouseState &= ~2;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
break;
|
|
|
|
|
}
|
2022-04-07 14:51:09 +02:00
|
|
|
|
MouseEvent(_currentMouseState, e.X, e.Y);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override void OnMouseWheel(MouseEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
if (Focused)
|
|
|
|
|
{
|
2022-04-07 14:51:09 +02:00
|
|
|
|
DoIfConnected(() => _vncStream.PointerWheelEvent((int)((e.X - _dx) / _scale), (int)((e.Y - _dy) / _scale),
|
2022-04-05 10:48:06 +02:00
|
|
|
|
e.Delta * -SystemInformation.MouseWheelScrollLines / 120));
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override void OnMouseLeave(EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
Cursor = Cursors.Default;
|
2022-04-07 14:51:09 +02:00
|
|
|
|
_cursorOver = false;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
base.OnMouseLeave(e);
|
2022-04-07 14:51:09 +02:00
|
|
|
|
_currentMouseState = 0;
|
|
|
|
|
_mouseMoved = 0;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override bool ProcessTabKey(bool forward)
|
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void DoIfConnected(MethodInvoker methodInvoker)
|
|
|
|
|
{
|
2022-04-07 14:51:09 +02:00
|
|
|
|
if (_connected)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
methodInvoker();
|
|
|
|
|
}
|
2022-04-05 10:48:06 +02:00
|
|
|
|
catch (IOException)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
|
|
|
|
// The server's gone away -- that's fine.
|
|
|
|
|
}
|
|
|
|
|
catch (Exception exn)
|
|
|
|
|
{
|
|
|
|
|
Log.Warn(exn, exn);
|
|
|
|
|
// Nothing more we can do with this.
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
|
|
|
|
|
{
|
|
|
|
|
bool down = ((msg.Msg == WM_KEYDOWN) || (msg.Msg == WM_SYSKEYDOWN));
|
|
|
|
|
|
|
|
|
|
Keys key = keyData;
|
|
|
|
|
|
|
|
|
|
if ((key & Keys.Control) == Keys.Control)
|
2022-04-05 10:48:06 +02:00
|
|
|
|
key &= ~Keys.Control;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
|
|
|
|
if ((key & Keys.Alt) == Keys.Alt)
|
2022-04-05 10:48:06 +02:00
|
|
|
|
key &= ~Keys.Alt;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
|
|
|
|
if ((key & Keys.Shift) == Keys.Shift)
|
2022-04-05 10:48:06 +02:00
|
|
|
|
key &= ~Keys.Shift;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
|
|
|
|
// use TranslateKeyMessage to identify if Left or Right modifier keys have been pressed/released
|
|
|
|
|
Keys extKey = ConsoleKeyHandler.TranslateKeyMessage(msg);
|
|
|
|
|
|
2022-04-07 14:51:09 +02:00
|
|
|
|
return KeySym(down, key, extKey);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override void OnKeyUp(KeyEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
// we cannot identify if Left or Right modifier keys have been pressed
|
2022-04-07 14:51:09 +02:00
|
|
|
|
e.Handled = KeySym(false, e.KeyCode, e.KeyCode);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
|
2022-04-07 14:51:09 +02:00
|
|
|
|
private void SendAltGrKeySym(bool pressed)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
2022-04-07 14:51:09 +02:00
|
|
|
|
KeySym_(pressed, Keys.ControlKey);
|
|
|
|
|
KeySym_(pressed, Keys.Menu);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
|
2022-04-07 14:51:09 +02:00
|
|
|
|
private void HandleAltGrKeySym(bool pressed, Keys key)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
|
|
|
|
if (pressed)
|
|
|
|
|
{
|
2022-04-07 14:51:09 +02:00
|
|
|
|
if (_pressedKeys.Count > 2 && key != Keys.ControlKey && key != Keys.Menu)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
2022-04-07 14:51:09 +02:00
|
|
|
|
bool isAltGrPressed = _pressedKeys.Where(
|
2013-06-24 13:41:48 +02:00
|
|
|
|
extKey => ConsoleKeyHandler.GetSimpleKey(extKey) == Keys.ControlKey ||
|
|
|
|
|
ConsoleKeyHandler.GetSimpleKey(extKey) == Keys.Menu).ToList().Count == 2;
|
|
|
|
|
|
|
|
|
|
if (isAltGrPressed &&
|
2022-04-07 14:51:09 +02:00
|
|
|
|
(KeyMap.IsMapped(key) && _altGrReleaseSent || !KeyMap.IsMapped(key) && !_altGrReleaseSent))
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
2022-04-07 14:51:09 +02:00
|
|
|
|
SendAltGrKeySym(_altGrReleaseSent);
|
|
|
|
|
_altGrReleaseSent = !_altGrReleaseSent;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if (key == Keys.ControlKey || key == Keys.Menu)
|
2022-04-07 14:51:09 +02:00
|
|
|
|
_altGrReleaseSent = false;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-07 14:51:09 +02:00
|
|
|
|
private bool KeySym(bool pressed, Keys key, Keys extendedKey)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
2022-04-07 14:51:09 +02:00
|
|
|
|
if (_sendScanCodes)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
return true;
|
|
|
|
|
|
2022-04-07 14:51:09 +02:00
|
|
|
|
if (KeyHandler.handleExtras(pressed, _pressedKeys, KeyHandler.ExtraKeys, extendedKey, KeyHandler.ModifierKeys, ref _modifierKeyPressedAlone))
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
2022-04-07 14:51:09 +02:00
|
|
|
|
if (!pressed && _modifierKeyPressedAlone)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
|
|
|
|
// send key up anyway
|
2022-04-07 14:51:09 +02:00
|
|
|
|
_modifierKeyPressedAlone = false;
|
|
|
|
|
return KeySym_(false, key);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
2022-04-05 10:48:06 +02:00
|
|
|
|
Parent.Focus();
|
2013-06-24 13:41:48 +02:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-07 14:51:09 +02:00
|
|
|
|
HandleAltGrKeySym(pressed, key);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
|
|
|
|
// on keyup, try to remove extended keys (i.e. LControlKey, LControlKey, RShiftKey, LShiftKey, RMenu, LMenu)
|
|
|
|
|
// we need to do this here, because we cannot otherwise distinguish between Left and Right modifier keys on KeyUp
|
|
|
|
|
if (!pressed)
|
|
|
|
|
{
|
|
|
|
|
List<Keys> extendedKeys = ConsoleKeyHandler.GetExtendedKeys(key);
|
|
|
|
|
foreach (var k in extendedKeys)
|
|
|
|
|
{
|
2022-04-07 14:51:09 +02:00
|
|
|
|
_pressedKeys.Remove(k);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-07 14:51:09 +02:00
|
|
|
|
return KeySym_(pressed, key);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
|
2022-04-07 14:51:09 +02:00
|
|
|
|
private bool KeySym_(bool pressed, Keys key)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
|
|
|
|
int keysym = KeyMap.translateKey(key);
|
|
|
|
|
|
|
|
|
|
if (keysym > 0)
|
|
|
|
|
{
|
2022-04-07 14:51:09 +02:00
|
|
|
|
DoIfConnected(() => _vncStream.keyCodeEvent(pressed, keysym));
|
2013-06-24 13:41:48 +02:00
|
|
|
|
return true;
|
|
|
|
|
}
|
2022-04-05 10:48:06 +02:00
|
|
|
|
|
|
|
|
|
return false;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
|
2022-04-07 14:51:09 +02:00
|
|
|
|
private void KeyScan(bool pressed, int scanCode, int keySym)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
2022-04-07 14:51:09 +02:00
|
|
|
|
if (KeyHandler.handleExtras(pressed, _pressedScans, KeyHandler.ExtraScans, scanCode, KeyHandler.ModifierScans, ref _modifierKeyPressedAlone))
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
2022-04-07 14:51:09 +02:00
|
|
|
|
if (!pressed && _modifierKeyPressedAlone)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
|
|
|
|
// send key up anyway
|
2022-04-07 14:51:09 +02:00
|
|
|
|
_modifierKeyPressedAlone = false;
|
|
|
|
|
KeyScan_(false, scanCode, keySym);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
this.Focus();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-07 14:51:09 +02:00
|
|
|
|
KeyScan_(pressed, scanCode, keySym);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
|
2022-04-07 14:51:09 +02:00
|
|
|
|
private void KeyScan_(bool pressed, int scanCode, int keySym = -1)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
2022-04-07 14:51:09 +02:00
|
|
|
|
DoIfConnected(() => _vncStream.keyScanEvent(pressed, scanCode, keySym, UseQemuExtKeyEncoding));
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#region IRemoteConsole implementation
|
|
|
|
|
|
|
|
|
|
public ConsoleKeyHandler KeyHandler { get; set; }
|
|
|
|
|
|
2022-04-05 10:48:06 +02:00
|
|
|
|
public Control ConsoleControl => this;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
|
|
|
|
public void Activate()
|
|
|
|
|
{
|
2022-04-05 10:48:06 +02:00
|
|
|
|
Select();
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void DisconnectAndDispose()
|
|
|
|
|
{
|
|
|
|
|
Disconnect();
|
|
|
|
|
Dispose();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Pause()
|
|
|
|
|
{
|
2022-04-07 14:51:09 +02:00
|
|
|
|
_helperIsPaused = true;
|
|
|
|
|
_vncStream?.Pause();
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
|
2022-04-07 14:51:09 +02:00
|
|
|
|
public void UnPause()
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
2022-04-07 14:51:09 +02:00
|
|
|
|
_helperIsPaused = false;
|
|
|
|
|
_vncStream?.UnPause();
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void SendCAD()
|
|
|
|
|
{
|
2022-04-07 14:51:09 +02:00
|
|
|
|
if (_sendScanCodes)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
2022-04-07 14:51:09 +02:00
|
|
|
|
KeyScan_(true, ConsoleKeyHandler.CTRL_SCAN);
|
|
|
|
|
KeyScan_(true, ConsoleKeyHandler.ALT_SCAN);
|
|
|
|
|
KeyScan_(true, ConsoleKeyHandler.DEL_SCAN);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
2022-04-07 14:51:09 +02:00
|
|
|
|
KeyScan_(false, ConsoleKeyHandler.CTRL_SCAN);
|
|
|
|
|
KeyScan_(false, ConsoleKeyHandler.ALT_SCAN);
|
|
|
|
|
KeyScan_(false, ConsoleKeyHandler.DEL_SCAN);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2022-04-07 14:51:09 +02:00
|
|
|
|
KeySym_(true, Keys.ControlKey);
|
|
|
|
|
KeySym_(true, Keys.Menu);
|
|
|
|
|
KeySym_(true, Keys.Delete);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
2022-04-07 14:51:09 +02:00
|
|
|
|
KeySym_(false, Keys.ControlKey);
|
|
|
|
|
KeySym_(false, Keys.Menu);
|
|
|
|
|
KeySym_(false, Keys.Delete);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Image Snapshot()
|
|
|
|
|
{
|
2022-04-07 14:51:09 +02:00
|
|
|
|
while (_vncStream == null)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
Thread.Sleep(100);
|
|
|
|
|
|
2022-04-07 14:51:09 +02:00
|
|
|
|
_vncStream.UnPause(true); //request full update
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
2022-04-07 14:51:09 +02:00
|
|
|
|
lock (_vncStream.updateMonitor)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
2022-04-07 14:51:09 +02:00
|
|
|
|
Monitor.Wait(_vncStream.updateMonitor, 1000);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
|
2022-04-07 14:51:09 +02:00
|
|
|
|
lock (_backBuffer)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
2022-04-07 14:51:09 +02:00
|
|
|
|
Image image = new Bitmap(_backBuffer);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
return image;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool SendScanCodes
|
|
|
|
|
{
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
Program.AssertOnEventThread();
|
2022-04-07 14:51:09 +02:00
|
|
|
|
if (_sendScanCodes == value)
|
2021-09-09 11:24:39 +02:00
|
|
|
|
return;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
2021-09-09 11:24:39 +02:00
|
|
|
|
Log.InfoFormat("VNCGraphicsClient.SetSendScanCodes newSendScanCodes={0}", value);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
2021-09-09 11:24:39 +02:00
|
|
|
|
if (!value)
|
|
|
|
|
{
|
|
|
|
|
InterceptKeys.releaseKeys();
|
|
|
|
|
}
|
|
|
|
|
else if (Focused)
|
|
|
|
|
{
|
2022-04-07 14:51:09 +02:00
|
|
|
|
InterceptKeys.grabKeys(KeyScan, false);
|
2021-09-09 11:24:39 +02:00
|
|
|
|
}
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
2022-04-07 14:51:09 +02:00
|
|
|
|
_sendScanCodes = value;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool Scaling
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
Program.AssertOnEventThread();
|
2022-04-07 14:51:09 +02:00
|
|
|
|
return _scaling;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
Program.AssertOnEventThread();
|
|
|
|
|
|
2022-04-07 14:51:09 +02:00
|
|
|
|
if (_scaling == value)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
// it going to scaling, quickly save the old dx, dy values
|
|
|
|
|
if (value)
|
|
|
|
|
{
|
2022-04-07 14:51:09 +02:00
|
|
|
|
_oldDx = _dx;
|
|
|
|
|
_oldDy = _dy;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
|
2022-04-07 14:51:09 +02:00
|
|
|
|
lock (_backBuffer)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
2022-04-07 14:51:09 +02:00
|
|
|
|
_scaling = value;
|
2022-04-05 10:48:06 +02:00
|
|
|
|
SetupScaling();
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
2022-04-05 10:48:06 +02:00
|
|
|
|
Invalidate();
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Invoke holding backBuffer lock
|
|
|
|
|
/// </summary>
|
|
|
|
|
private void SetupScaling()
|
|
|
|
|
{
|
|
|
|
|
Program.AssertOnEventThread();
|
|
|
|
|
|
2022-04-07 14:51:09 +02:00
|
|
|
|
if (_frontGraphics == null)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
return;
|
|
|
|
|
|
2022-04-07 14:51:09 +02:00
|
|
|
|
if (_scaling)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
2022-04-05 10:48:06 +02:00
|
|
|
|
AutoScroll = false;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
2022-04-05 10:48:06 +02:00
|
|
|
|
float xScale = Size.Width /
|
2022-04-07 14:51:09 +02:00
|
|
|
|
(float)(_displayBorder ? DesktopSize.Width + BORDER_PADDING * 3 : DesktopSize.Width);
|
2022-04-05 10:48:06 +02:00
|
|
|
|
float yScale = Size.Height /
|
2022-04-07 14:51:09 +02:00
|
|
|
|
(float)(_displayBorder ? DesktopSize.Height + BORDER_PADDING * 3 : DesktopSize.Height);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
2022-04-07 14:51:09 +02:00
|
|
|
|
_scale = xScale > yScale ? yScale : xScale;
|
|
|
|
|
_scale = _scale > 0.01 ? _scale : (float)0.01;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
2022-04-07 14:51:09 +02:00
|
|
|
|
_bump = (int)Math.Ceiling(1 / _scale);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
|
|
|
|
// Now do the offset
|
|
|
|
|
|
2022-04-07 14:51:09 +02:00
|
|
|
|
_dx = (Size.Width - DesktopSize.Width * _scale) / 2;
|
|
|
|
|
_dy = (Size.Height - DesktopSize.Height * _scale) / 2;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
|
|
|
|
Matrix transform = new Matrix();
|
2022-04-07 14:51:09 +02:00
|
|
|
|
transform.Translate(_dx, _dy);
|
|
|
|
|
transform.Scale(_scale, _scale);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
2022-04-07 14:51:09 +02:00
|
|
|
|
_frontGraphics.Transform = transform;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2022-04-07 14:51:09 +02:00
|
|
|
|
_scale = 1;
|
|
|
|
|
_bump = 0;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
2022-04-07 14:51:09 +02:00
|
|
|
|
if (_connected)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
2022-04-05 10:48:06 +02:00
|
|
|
|
AutoScrollMinSize = new Size(
|
2022-04-07 14:51:09 +02:00
|
|
|
|
_displayBorder ? DesktopSize.Width + BORDER_PADDING + BORDER_PADDING : DesktopSize.Width,
|
|
|
|
|
_displayBorder ? DesktopSize.Height + BORDER_PADDING + BORDER_PADDING : DesktopSize.Height);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2022-04-05 10:48:06 +02:00
|
|
|
|
AutoScrollMinSize = new Size(0, 0);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
|
2022-04-05 10:48:06 +02:00
|
|
|
|
AutoScroll = true;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
2022-04-07 14:51:09 +02:00
|
|
|
|
if (Size.Height >= (_displayBorder ? DesktopSize.Height + BORDER_PADDING + BORDER_PADDING : DesktopSize.Height))
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
2022-04-07 14:51:09 +02:00
|
|
|
|
_dy = ((float) Size.Height - DesktopSize.Height) / 2;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2022-04-07 14:51:09 +02:00
|
|
|
|
if (_displayBorder)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
2022-04-07 14:51:09 +02:00
|
|
|
|
_dy = BORDER_PADDING;
|
|
|
|
|
AutoScrollPosition = new Point(BORDER_PADDING, (int)_oldDy);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2022-04-07 14:51:09 +02:00
|
|
|
|
_dy = 0;
|
|
|
|
|
AutoScrollPosition = new Point(0, (int)_oldDy);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-07 14:51:09 +02:00
|
|
|
|
if (Size.Width >= (_displayBorder ? DesktopSize.Width + BORDER_PADDING + BORDER_PADDING : DesktopSize.Width))
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
2022-04-07 14:51:09 +02:00
|
|
|
|
_dx = ((float)Size.Width - DesktopSize.Width) / 2;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2022-04-07 14:51:09 +02:00
|
|
|
|
if (_displayBorder)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
2022-04-07 14:51:09 +02:00
|
|
|
|
_dx = BORDER_PADDING;
|
|
|
|
|
AutoScrollPosition = new Point((int)_oldDx, BORDER_PADDING);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2022-04-07 14:51:09 +02:00
|
|
|
|
_dx = 0;
|
|
|
|
|
AutoScrollPosition = new Point((int)_oldDx, 0);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Matrix transform = new Matrix();
|
2022-04-07 14:51:09 +02:00
|
|
|
|
transform.Translate(_dx, _dy);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
2022-04-07 14:51:09 +02:00
|
|
|
|
_frontGraphics.Transform = transform;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Whether or not to display the blue rectangle around the control when it has focus.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public bool DisplayBorder
|
|
|
|
|
{
|
|
|
|
|
set
|
|
|
|
|
{
|
2022-04-07 14:51:09 +02:00
|
|
|
|
_displayBorder = value;
|
|
|
|
|
lock (_backBuffer)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
|
|
|
|
SetupScaling();
|
|
|
|
|
}
|
2022-04-05 10:48:06 +02:00
|
|
|
|
Invalidate();
|
|
|
|
|
Update();
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Size DesktopSize { get; set; }
|
|
|
|
|
|
2022-04-05 10:48:06 +02:00
|
|
|
|
public Rectangle ConsoleBounds =>
|
2022-04-07 14:51:09 +02:00
|
|
|
|
_scaling
|
|
|
|
|
? new Rectangle((int)_dx, (int)_dy, Size.Width - 2 * (int)_dx, Size.Height - 2 * (int)_dy)
|
|
|
|
|
: new Rectangle((int)_dx, (int)_dy, DesktopSize.Width, DesktopSize.Height);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
}
|
|
|
|
|
}
|