Quasar/Server/Forms/FrmRemoteDesktop.cs

333 lines
11 KiB
C#
Raw Normal View History

2015-01-27 14:56:52 -08:00
using System;
using System.Drawing;
2015-01-27 14:56:52 -08:00
using System.Windows.Forms;
using xServer.Core.Helper;
using xServer.Core.Networking;
using xServer.Core.Utilities;
using xServer.Core.MouseKeyHook;
using xServer.Enums;
2015-01-27 14:56:52 -08:00
namespace xServer.Forms
{
public partial class FrmRemoteDesktop : Form
{
public bool IsStarted { get; private set; }
2015-07-28 14:48:39 -07:00
private readonly Client _connectClient;
private bool _enableMouseInput;
2015-07-29 06:11:07 -07:00
private bool _enableKeyboardInput;
private IKeyboardMouseEvents _mEvents;
2015-07-28 14:48:39 -07:00
2015-01-27 14:56:52 -08:00
public FrmRemoteDesktop(Client c)
{
_connectClient = c;
_connectClient.Value.FrmRdp = this;
Subscribe(Hook.GlobalEvents());
2015-01-27 14:56:52 -08:00
InitializeComponent();
}
private void FrmRemoteDesktop_Load(object sender, EventArgs e)
{
this.Text = WindowHelper.GetWindowTitle("Remote Desktop", _connectClient);
2015-01-27 14:56:52 -08:00
panelTop.Left = (this.Width / 2) - (panelTop.Width / 2);
2015-01-27 14:56:52 -08:00
btnHide.Left = (panelTop.Width / 2) - (btnHide.Width / 2);
2015-01-27 14:56:52 -08:00
btnShow.Location = new Point(377, 0);
btnShow.Left = (this.Width / 2) - (btnShow.Width / 2);
2015-01-27 14:56:52 -08:00
2015-07-29 06:11:07 -07:00
_enableKeyboardInput = false;
2015-01-27 14:56:52 -08:00
if (_connectClient.Value != null)
2015-07-14 10:00:31 -07:00
new Core.Packets.ServerPackets.GetMonitors().Execute(_connectClient);
2015-01-27 14:56:52 -08:00
}
2015-07-29 06:11:07 -07:00
private void Subscribe(IKeyboardMouseEvents events)
{
_mEvents = events;
_mEvents.MouseWheel += MouseWheelEvent;
2015-07-29 06:11:07 -07:00
_mEvents.KeyDown += OnKeyDown;
2015-07-29 07:21:49 -07:00
_mEvents.KeyUp += OnKeyUp;
}
private void Unsubscribe()
{
if (_mEvents == null) return;
_mEvents.MouseWheel -= MouseWheelEvent;
2015-07-29 06:11:07 -07:00
_mEvents.KeyDown -= OnKeyDown;
2015-07-29 07:21:49 -07:00
_mEvents.KeyUp -= OnKeyUp;
}
2015-07-28 12:12:18 -07:00
public void AddMonitors(int monitors)
{
try
{
cbMonitors.Invoke((MethodInvoker)delegate
{
2015-07-28 12:12:18 -07:00
for (int i = 0; i < monitors; i++)
cbMonitors.Items.Add(string.Format("Monitor {0}", i + 1));
cbMonitors.SelectedIndex = 0;
});
}
catch (InvalidOperationException)
{
}
}
public void UpdateImage(Bitmap bmp, bool cloneBitmap = false)
{
picDesktop.UpdateImage(bmp, cloneBitmap);
}
private void _frameCounter_FrameUpdated(FrameUpdatedEventArgs e)
{
2015-07-28 15:12:31 -07:00
try
{
this.Invoke((MethodInvoker)delegate
{
this.Text = string.Format("{0} - FPS: {1}", WindowHelper.GetWindowTitle("Remote Desktop", _connectClient), e.CurrentFramesPerSecond.ToString("0.00"));
});
}
catch (InvalidOperationException)
{
2015-07-28 15:12:31 -07:00
}
}
private void ToggleControls(bool t)
{
IsStarted = !t;
try
{
this.Invoke((MethodInvoker)delegate
{
btnStart.Enabled = t;
btnStop.Enabled = !t;
barQuality.Enabled = t;
});
}
catch (InvalidOperationException)
{
}
}
2015-01-27 14:56:52 -08:00
private void FrmRemoteDesktop_FormClosing(object sender, FormClosingEventArgs e)
{
if (!picDesktop.IsDisposed && !picDesktop.Disposing)
picDesktop.Dispose();
2015-01-27 14:56:52 -08:00
if (_connectClient.Value != null)
_connectClient.Value.FrmRdp = null;
Unsubscribe();
2015-01-27 14:56:52 -08:00
}
private void FrmRemoteDesktop_Resize(object sender, EventArgs e)
{
2015-07-28 14:48:39 -07:00
panelTop.Left = (this.Width/2) - (panelTop.Width/2);
btnShow.Left = (this.Width/2) - (btnShow.Width/2);
2015-01-27 14:56:52 -08:00
}
private void btnStart_Click(object sender, EventArgs e)
{
if (cbMonitors.Items.Count == 0)
{
MessageBox.Show("No monitor detected.\nPlease wait till the client sends a list with available monitors.",
"Starting failed", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
ToggleControls(false);
picDesktop.Start();
// Subscribe to the new frame counter.
2015-07-28 14:48:39 -07:00
picDesktop.SetFrameUpdatedEvent(_frameCounter_FrameUpdated);
2015-07-29 06:11:07 -07:00
this.ActiveControl = picDesktop;
new Core.Packets.ServerPackets.GetDesktop(barQuality.Value, cbMonitors.SelectedIndex).Execute(_connectClient);
2015-01-27 14:56:52 -08:00
}
private void btnStop_Click(object sender, EventArgs e)
{
ToggleControls(true);
picDesktop.Stop();
// Unsubscribe from the frame counter. It will be re-created when starting again.
2015-07-28 14:48:39 -07:00
picDesktop.UnsetFrameUpdatedEvent(_frameCounter_FrameUpdated);
2015-07-29 06:11:07 -07:00
this.ActiveControl = picDesktop;
2015-01-27 14:56:52 -08:00
}
private void barQuality_Scroll(object sender, EventArgs e)
{
int value = barQuality.Value;
lblQualityShow.Text = value.ToString();
if (value < 25)
lblQualityShow.Text += " (low)";
else if (value >= 85)
lblQualityShow.Text += " (best)";
else if (value >= 75)
lblQualityShow.Text += " (high)";
else if (value >= 25)
lblQualityShow.Text += " (mid)";
2015-07-29 06:11:07 -07:00
this.ActiveControl = picDesktop;
2015-01-27 14:56:52 -08:00
}
private void btnMouse_Click(object sender, EventArgs e)
{
if (_enableMouseInput)
{
this.picDesktop.Cursor = Cursors.Default;
btnMouse.Image = Properties.Resources.mouse_delete;
_enableMouseInput = false;
}
else
{
this.picDesktop.Cursor = Cursors.Hand;
btnMouse.Image = Properties.Resources.mouse_add;
_enableMouseInput = true;
}
2015-07-29 06:11:07 -07:00
this.ActiveControl = picDesktop;
}
private void btnKeyboard_Click(object sender, EventArgs e)
{
_enableKeyboardInput = !_enableKeyboardInput;
this.ActiveControl = picDesktop;
2015-01-27 14:56:52 -08:00
}
private int GetRemoteWidth(int localX)
{
2015-07-28 14:48:39 -07:00
return localX * picDesktop.ScreenWidth / picDesktop.Width;
}
private int GetRemoteHeight(int localY)
{
2015-07-28 14:48:39 -07:00
return localY * picDesktop.ScreenHeight / picDesktop.Height;
}
private void picDesktop_MouseDown(object sender, MouseEventArgs e)
2015-01-27 14:56:52 -08:00
{
2015-07-29 10:17:19 -07:00
if (picDesktop.Image != null && _enableMouseInput && IsStarted && this.ContainsFocus)
2015-01-27 14:56:52 -08:00
{
int local_x = e.X;
int local_y = e.Y;
int remote_x = GetRemoteWidth(local_x);
int remote_y = GetRemoteHeight(local_y);
2015-01-27 14:56:52 -08:00
2015-07-27 08:36:18 -07:00
MouseAction action = MouseAction.None;
if (e.Button == MouseButtons.Left)
2015-07-27 08:36:18 -07:00
action = MouseAction.LeftDown;
if (e.Button == MouseButtons.Right)
2015-07-27 08:36:18 -07:00
action = MouseAction.RightDown;
2015-01-27 14:56:52 -08:00
int selectedMonitorIndex = cbMonitors.SelectedIndex;
2015-01-27 14:56:52 -08:00
if (_connectClient != null)
new Core.Packets.ServerPackets.DoMouseEvent(action, true, remote_x, remote_y, selectedMonitorIndex).Execute(_connectClient);
2015-01-27 14:56:52 -08:00
}
}
private void picDesktop_MouseUp(object sender, MouseEventArgs e)
2015-01-27 14:56:52 -08:00
{
2015-07-29 10:17:19 -07:00
if (picDesktop.Image != null && _enableMouseInput && IsStarted && this.ContainsFocus)
2015-01-27 14:56:52 -08:00
{
int local_x = e.X;
int local_y = e.Y;
int remote_x = GetRemoteWidth(local_x);
int remote_y = GetRemoteHeight(local_y);
2015-01-27 14:56:52 -08:00
2015-07-27 08:36:18 -07:00
MouseAction action = MouseAction.None;
if (e.Button == MouseButtons.Left)
2015-07-27 08:36:18 -07:00
action = MouseAction.LeftDown;
if (e.Button == MouseButtons.Right)
2015-07-27 08:36:18 -07:00
action = MouseAction.RightDown;
int selectedMonitorIndex = cbMonitors.SelectedIndex;
if (_connectClient != null)
new Core.Packets.ServerPackets.DoMouseEvent(action, false, remote_x, remote_y, selectedMonitorIndex).Execute(_connectClient);
}
}
private void picDesktop_MouseMove(object sender, MouseEventArgs e)
{
2015-07-29 10:17:19 -07:00
if (picDesktop.Image != null && _enableMouseInput && IsStarted && this.ContainsFocus)
{
int local_x = e.X;
int local_y = e.Y;
int remote_x = GetRemoteWidth(local_x);
int remote_y = GetRemoteHeight(local_y);
2015-01-27 14:56:52 -08:00
int selectedMonitorIndex = cbMonitors.SelectedIndex;
2015-01-27 14:56:52 -08:00
if (_connectClient != null)
2015-07-27 08:36:18 -07:00
new Core.Packets.ServerPackets.DoMouseEvent(MouseAction.MoveCursor, false, remote_x, remote_y, selectedMonitorIndex).Execute(_connectClient);
2015-01-27 14:56:52 -08:00
}
}
private void MouseWheelEvent(object sender, MouseEventArgs e)
{
2015-07-29 10:17:19 -07:00
if (picDesktop.Image != null && _enableMouseInput && IsStarted && this.ContainsFocus)
{
if (_connectClient != null)
new Core.Packets.ServerPackets.DoMouseEvent(e.Delta == 120 ? MouseAction.ScrollUp : MouseAction.ScrollDown, false, 0, 0, cbMonitors.SelectedIndex).Execute(_connectClient);
}
}
2015-07-29 06:11:07 -07:00
private void OnKeyDown(object sender, KeyEventArgs e)
{
2015-07-29 10:17:19 -07:00
if (picDesktop.Image != null && _enableKeyboardInput && IsStarted && this.ContainsFocus)
2015-07-29 06:11:07 -07:00
{
if (_connectClient != null)
2015-07-29 07:21:49 -07:00
new Core.Packets.ServerPackets.DoKeyboardEvent((byte)e.KeyCode, true).Execute(_connectClient);
}
}
private void OnKeyUp(object sender, KeyEventArgs e)
{
2015-07-29 10:17:19 -07:00
if (picDesktop.Image != null && _enableKeyboardInput && IsStarted && this.ContainsFocus)
2015-07-29 07:21:49 -07:00
{
if (_connectClient != null)
new Core.Packets.ServerPackets.DoKeyboardEvent((byte)e.KeyCode, false).Execute(_connectClient);
2015-07-29 06:11:07 -07:00
}
}
2015-01-27 14:56:52 -08:00
private void btnHide_Click(object sender, EventArgs e)
{
panelTop.Visible = false;
btnShow.Visible = true;
btnHide.Visible = false;
2015-07-29 06:11:07 -07:00
this.ActiveControl = picDesktop;
2015-01-27 14:56:52 -08:00
}
private void btnShow_Click(object sender, EventArgs e)
{
panelTop.Visible = true;
btnShow.Visible = false;
btnHide.Visible = true;
2015-07-29 06:11:07 -07:00
this.ActiveControl = picDesktop;
2015-01-27 14:56:52 -08:00
}
2015-07-29 10:17:19 -07:00
private void chkForwardCommandKeys_CheckedChanged(object sender, EventArgs e)
{
if (chkForwardCommandKeys.Checked)
{
//register hot keys for command keys such as Windows key and ALT + key combinations
}
else
{
}
}
2015-01-27 14:56:52 -08:00
}
}