2015-01-27 14:56:52 -08:00
|
|
|
|
using System;
|
2015-07-28 02:48:27 -07:00
|
|
|
|
using System.Collections.Generic;
|
2015-05-29 14:34:57 -07:00
|
|
|
|
using System.Drawing;
|
2015-07-28 02:48:27 -07:00
|
|
|
|
using System.IO;
|
2015-01-27 14:56:52 -08:00
|
|
|
|
using System.Windows.Forms;
|
2015-06-25 13:22:45 -07:00
|
|
|
|
using xServer.Core.Helper;
|
2015-06-05 14:07:37 -07:00
|
|
|
|
using xServer.Core.Networking;
|
2015-07-28 02:48:27 -07:00
|
|
|
|
using xServer.Core.Packets.ClientPackets;
|
2015-07-25 15:10:59 -07:00
|
|
|
|
using xServer.Core.Utilities;
|
|
|
|
|
using xServer.Enums;
|
2015-01-27 14:56:52 -08:00
|
|
|
|
|
|
|
|
|
namespace xServer.Forms
|
|
|
|
|
{
|
|
|
|
|
public partial class FrmRemoteDesktop : Form
|
|
|
|
|
{
|
2015-07-28 02:48:27 -07:00
|
|
|
|
public readonly Queue<GetDesktopResponse> ProcessingScreensQueue = new Queue<GetDesktopResponse>();
|
|
|
|
|
public readonly object ProcessingScreensLock = new object();
|
|
|
|
|
public bool ProcessingScreens;
|
2015-01-27 14:56:52 -08:00
|
|
|
|
|
2015-07-28 14:48:39 -07:00
|
|
|
|
private readonly Client _connectClient;
|
|
|
|
|
private bool _enableMouseInput;
|
|
|
|
|
private bool _started;
|
|
|
|
|
|
2015-01-27 14:56:52 -08:00
|
|
|
|
public FrmRemoteDesktop(Client c)
|
|
|
|
|
{
|
|
|
|
|
_connectClient = c;
|
|
|
|
|
_connectClient.Value.FrmRdp = this;
|
|
|
|
|
InitializeComponent();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void FrmRemoteDesktop_Load(object sender, EventArgs e)
|
|
|
|
|
{
|
2015-07-25 15:10:59 -07:00
|
|
|
|
this.Text = WindowHelper.GetWindowTitle("Remote Desktop", _connectClient);
|
2015-01-27 14:56:52 -08:00
|
|
|
|
|
2015-07-28 11:36:04 -07:00
|
|
|
|
panelTop.Left = (this.Width / 2) - (panelTop.Width / 2);
|
2015-01-27 14:56:52 -08:00
|
|
|
|
|
2015-07-28 11:36:04 -07:00
|
|
|
|
btnHide.Left = (panelTop.Width / 2) - (btnHide.Width / 2);
|
2015-01-27 14:56:52 -08:00
|
|
|
|
|
2015-06-25 13:22:45 -07:00
|
|
|
|
btnShow.Location = new Point(377, 0);
|
2015-07-28 11:36:04 -07:00
|
|
|
|
btnShow.Left = (this.Width / 2) - (btnShow.Width / 2);
|
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-28 02:48:27 -07:00
|
|
|
|
public void ProcessScreens(object state)
|
|
|
|
|
{
|
2015-07-28 11:36:04 -07:00
|
|
|
|
while (true && picDesktop != null && !picDesktop.IsDisposed && !picDesktop.Disposing)
|
2015-07-28 02:48:27 -07:00
|
|
|
|
{
|
|
|
|
|
GetDesktopResponse packet;
|
|
|
|
|
lock (ProcessingScreensQueue)
|
|
|
|
|
{
|
|
|
|
|
if (ProcessingScreensQueue.Count == 0)
|
|
|
|
|
{
|
|
|
|
|
lock (ProcessingScreensLock)
|
|
|
|
|
{
|
|
|
|
|
ProcessingScreens = false;
|
|
|
|
|
}
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
packet = ProcessingScreensQueue.Dequeue();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (_connectClient.Value.StreamCodec == null)
|
2015-07-28 11:36:04 -07:00
|
|
|
|
{
|
2015-07-28 02:48:27 -07:00
|
|
|
|
_connectClient.Value.StreamCodec = new UnsafeStreamCodec(packet.Quality, packet.Monitor, packet.Resolution);
|
2015-07-28 11:36:04 -07:00
|
|
|
|
}
|
|
|
|
|
else if (_connectClient.Value.StreamCodec.ImageQuality != packet.Quality || _connectClient.Value.StreamCodec.Monitor != packet.Monitor)
|
2015-07-28 02:48:27 -07:00
|
|
|
|
{
|
2015-07-28 11:36:04 -07:00
|
|
|
|
if (string.Compare(_connectClient.Value.StreamCodec.Resolution, packet.Resolution, StringComparison.InvariantCultureIgnoreCase) != 0)
|
|
|
|
|
{
|
2015-07-28 02:48:27 -07:00
|
|
|
|
_connectClient.Value.StreamCodec.Dispose();
|
2015-07-28 11:36:04 -07:00
|
|
|
|
}
|
2015-07-28 02:48:27 -07:00
|
|
|
|
|
|
|
|
|
_connectClient.Value.StreamCodec = new UnsafeStreamCodec(packet.Quality, packet.Monitor, packet.Resolution);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
using (MemoryStream ms = new MemoryStream(packet.Image))
|
|
|
|
|
{
|
2015-07-28 11:36:04 -07:00
|
|
|
|
try
|
|
|
|
|
{
|
2015-07-28 14:48:39 -07:00
|
|
|
|
// update the new image from the packet data
|
2015-07-28 11:36:04 -07:00
|
|
|
|
picDesktop.UpdateImage(_connectClient.Value.StreamCodec.DecodeData(ms), true);
|
|
|
|
|
|
|
|
|
|
this.Invoke((MethodInvoker)delegate
|
|
|
|
|
{
|
2015-07-28 11:52:07 -07:00
|
|
|
|
if (picDesktop != null && !picDesktop.IsDisposed)
|
2015-07-28 11:36:04 -07:00
|
|
|
|
{
|
|
|
|
|
picDesktop.Invalidate();
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
catch
|
2015-07-28 14:48:39 -07:00
|
|
|
|
{
|
|
|
|
|
}
|
2015-07-28 02:48:27 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
packet.Image = null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-07-28 12:12:18 -07:00
|
|
|
|
public void AddMonitors(int monitors)
|
2015-05-29 14:34:57 -07:00
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2015-07-28 11:36:04 -07:00
|
|
|
|
cbMonitors.Invoke((MethodInvoker)delegate
|
2015-05-29 14:34:57 -07:00
|
|
|
|
{
|
2015-07-28 12:12:18 -07:00
|
|
|
|
for (int i = 0; i < monitors; i++)
|
2015-05-29 14:34:57 -07:00
|
|
|
|
cbMonitors.Items.Add(string.Format("Monitor {0}", i + 1));
|
|
|
|
|
cbMonitors.SelectedIndex = 0;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
catch (InvalidOperationException)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
MessageBox.Show(
|
|
|
|
|
string.Format(
|
|
|
|
|
"An unexpected error occurred: {0}\n\nPlease report this as fast as possible here:\\https://github.com/MaxXor/xRAT/issues",
|
|
|
|
|
ex.Message), "", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-07-28 11:36:04 -07:00
|
|
|
|
// Update on frame change.
|
|
|
|
|
private void _frameCounter_FrameUpdated(FrameUpdatedEventArgs e)
|
2015-07-23 07:55:44 -07:00
|
|
|
|
{
|
2015-07-28 11:36:04 -07:00
|
|
|
|
this.Invoke((MethodInvoker)delegate
|
2015-07-23 07:55:44 -07:00
|
|
|
|
{
|
2015-07-28 11:36:04 -07:00
|
|
|
|
this.Text = string.Format("{0} - FPS: {1}", WindowHelper.GetWindowTitle("Remote Desktop", _connectClient), e.CurrentFramesPerSecond.ToString("0.00"));
|
|
|
|
|
});
|
2015-07-23 07:55:44 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void ToggleControls(bool t)
|
|
|
|
|
{
|
|
|
|
|
_started = !t;
|
|
|
|
|
try
|
|
|
|
|
{
|
2015-07-28 11:36:04 -07:00
|
|
|
|
this.Invoke((MethodInvoker)delegate
|
2015-07-23 07:55:44 -07:00
|
|
|
|
{
|
|
|
|
|
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)
|
|
|
|
|
{
|
2015-07-23 07:55:44 -07:00
|
|
|
|
if (_started)
|
|
|
|
|
new Core.Packets.ServerPackets.GetDesktop(0, 0, RemoteDesktopAction.Stop).Execute(_connectClient);
|
2015-07-28 11:36:04 -07:00
|
|
|
|
if (!picDesktop.IsDisposed && !picDesktop.Disposing)
|
|
|
|
|
picDesktop.Dispose();
|
2015-01-27 14:56:52 -08:00
|
|
|
|
if (_connectClient.Value != null)
|
|
|
|
|
_connectClient.Value.FrmRdp = null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
{
|
2015-05-15 01:10:53 -07:00
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2015-07-23 07:55:44 -07:00
|
|
|
|
ToggleControls(false);
|
|
|
|
|
|
2015-07-28 11:36:04 -07:00
|
|
|
|
picDesktop.Start();
|
|
|
|
|
|
|
|
|
|
// Subscribe to the new frame counter.
|
2015-07-28 14:48:39 -07:00
|
|
|
|
picDesktop.SetFrameUpdatedEvent(_frameCounter_FrameUpdated);
|
2015-07-28 11:36:04 -07:00
|
|
|
|
|
2015-07-23 07:55:44 -07:00
|
|
|
|
new Core.Packets.ServerPackets.GetDesktop(barQuality.Value, cbMonitors.SelectedIndex, RemoteDesktopAction.Start).Execute(_connectClient);
|
2015-01-27 14:56:52 -08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void btnStop_Click(object sender, EventArgs e)
|
|
|
|
|
{
|
2015-07-23 07:55:44 -07:00
|
|
|
|
new Core.Packets.ServerPackets.GetDesktop(0, 0, RemoteDesktopAction.Stop).Execute(_connectClient);
|
|
|
|
|
ToggleControls(true);
|
2015-07-28 11:36:04 -07:00
|
|
|
|
|
|
|
|
|
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-01-27 14:56:52 -08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void barQuality_Scroll(object sender, EventArgs e)
|
|
|
|
|
{
|
2015-04-02 01:26:57 -07:00
|
|
|
|
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-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-28 02:48:27 -07:00
|
|
|
|
private int GetRemoteWidth(int localX)
|
|
|
|
|
{
|
2015-07-28 14:48:39 -07:00
|
|
|
|
return localX * picDesktop.ScreenWidth / picDesktop.Width;
|
2015-07-28 02:48:27 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private int GetRemoteHeight(int localY)
|
|
|
|
|
{
|
2015-07-28 14:48:39 -07:00
|
|
|
|
return localY * picDesktop.ScreenHeight / picDesktop.Height;
|
2015-07-28 02:48:27 -07:00
|
|
|
|
}
|
|
|
|
|
|
2015-07-27 06:57:05 -07:00
|
|
|
|
private void picDesktop_MouseDown(object sender, MouseEventArgs e)
|
2015-01-27 14:56:52 -08:00
|
|
|
|
{
|
2015-07-27 07:20:55 -07:00
|
|
|
|
if (picDesktop.Image != null && _enableMouseInput && !btnStart.Enabled)
|
2015-01-27 14:56:52 -08:00
|
|
|
|
{
|
|
|
|
|
int local_x = e.X;
|
|
|
|
|
int local_y = e.Y;
|
|
|
|
|
|
2015-07-28 02:48:27 -07:00
|
|
|
|
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;
|
2015-07-27 06:57:05 -07:00
|
|
|
|
|
|
|
|
|
if (e.Button == MouseButtons.Left)
|
2015-07-27 08:36:18 -07:00
|
|
|
|
action = MouseAction.LeftDown;
|
2015-07-27 06:57:05 -07:00
|
|
|
|
if (e.Button == MouseButtons.Right)
|
2015-07-27 08:36:18 -07:00
|
|
|
|
action = MouseAction.RightDown;
|
2015-01-27 14:56:52 -08:00
|
|
|
|
|
2015-05-21 01:32:24 -07:00
|
|
|
|
int selectedMonitorIndex = cbMonitors.SelectedIndex;
|
|
|
|
|
|
2015-01-27 14:56:52 -08:00
|
|
|
|
if (_connectClient != null)
|
2015-07-27 06:57:05 -07:00
|
|
|
|
new Core.Packets.ServerPackets.DoMouseEvent(action, true, remote_x, remote_y, selectedMonitorIndex).Execute(_connectClient);
|
2015-01-27 14:56:52 -08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-07-27 06:57:05 -07:00
|
|
|
|
private void picDesktop_MouseUp(object sender, MouseEventArgs e)
|
2015-01-27 14:56:52 -08:00
|
|
|
|
{
|
2015-07-27 07:20:55 -07:00
|
|
|
|
if (picDesktop.Image != null && _enableMouseInput && !btnStart.Enabled)
|
2015-01-27 14:56:52 -08:00
|
|
|
|
{
|
|
|
|
|
int local_x = e.X;
|
|
|
|
|
int local_y = e.Y;
|
|
|
|
|
|
2015-07-28 02:48:27 -07:00
|
|
|
|
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;
|
2015-07-27 06:57:05 -07:00
|
|
|
|
|
|
|
|
|
if (e.Button == MouseButtons.Left)
|
2015-07-27 08:36:18 -07:00
|
|
|
|
action = MouseAction.LeftDown;
|
2015-07-27 06:57:05 -07:00
|
|
|
|
if (e.Button == MouseButtons.Right)
|
2015-07-27 08:36:18 -07:00
|
|
|
|
action = MouseAction.RightDown;
|
2015-07-27 06:57:05 -07:00
|
|
|
|
|
|
|
|
|
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-27 07:20:55 -07:00
|
|
|
|
if (picDesktop.Image != null && _enableMouseInput && !btnStart.Enabled)
|
2015-07-27 06:57:05 -07:00
|
|
|
|
{
|
|
|
|
|
int local_x = e.X;
|
|
|
|
|
int local_y = e.Y;
|
|
|
|
|
|
2015-07-28 02:48:27 -07:00
|
|
|
|
int remote_x = GetRemoteWidth(local_x);
|
|
|
|
|
int remote_y = GetRemoteHeight(local_y);
|
2015-01-27 14:56:52 -08:00
|
|
|
|
|
2015-05-21 01:32:24 -07: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 btnHide_Click(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
panelTop.Visible = false;
|
|
|
|
|
btnShow.Visible = true;
|
|
|
|
|
btnHide.Visible = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void btnShow_Click(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
panelTop.Visible = true;
|
|
|
|
|
btnShow.Visible = false;
|
|
|
|
|
btnHide.Visible = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
2015-04-21 11:27:52 -07:00
|
|
|
|
}
|