2015-01-27 14:56:52 -08:00
|
|
|
|
using System;
|
2015-07-28 02:48:27 -07:00
|
|
|
|
using System.Collections.Generic;
|
2015-07-23 07:55:44 -07:00
|
|
|
|
using System.Diagnostics;
|
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
|
|
|
|
|
{
|
|
|
|
|
private readonly Client _connectClient;
|
|
|
|
|
private bool _enableMouseInput;
|
2015-07-23 07:55:44 -07:00
|
|
|
|
private bool _started;
|
|
|
|
|
private FrameCounter _frameCounter;
|
|
|
|
|
private Stopwatch _sWatch;
|
2015-07-28 02:48:27 -07:00
|
|
|
|
private int _screenWidth;
|
|
|
|
|
private int _screenHeight;
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
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-04-21 11:27:52 -07:00
|
|
|
|
panelTop.Left = (this.Width/2) - (panelTop.Width/2);
|
2015-01-27 14:56:52 -08:00
|
|
|
|
|
2015-04-21 11:27:52 -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-04-21 11:27:52 -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)
|
|
|
|
|
{
|
|
|
|
|
while (true)
|
|
|
|
|
{
|
|
|
|
|
GetDesktopResponse packet;
|
|
|
|
|
lock (ProcessingScreensQueue)
|
|
|
|
|
{
|
|
|
|
|
if (ProcessingScreensQueue.Count == 0)
|
|
|
|
|
{
|
|
|
|
|
lock (ProcessingScreensLock)
|
|
|
|
|
{
|
|
|
|
|
ProcessingScreens = false;
|
|
|
|
|
}
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
packet = ProcessingScreensQueue.Dequeue();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (_connectClient.Value.StreamCodec == null)
|
|
|
|
|
_connectClient.Value.StreamCodec = new UnsafeStreamCodec(packet.Quality, packet.Monitor, packet.Resolution);
|
|
|
|
|
|
|
|
|
|
if (_connectClient.Value.StreamCodec.ImageQuality != packet.Quality || _connectClient.Value.StreamCodec.Monitor != packet.Monitor
|
|
|
|
|
|| _connectClient.Value.StreamCodec.Resolution != packet.Resolution)
|
|
|
|
|
{
|
|
|
|
|
if (_connectClient.Value.StreamCodec != null)
|
|
|
|
|
_connectClient.Value.StreamCodec.Dispose();
|
|
|
|
|
|
|
|
|
|
_connectClient.Value.StreamCodec = new UnsafeStreamCodec(packet.Quality, packet.Monitor, packet.Resolution);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
using (MemoryStream ms = new MemoryStream(packet.Image))
|
|
|
|
|
{
|
|
|
|
|
if (_connectClient.Value.FrmRdp != null)
|
|
|
|
|
_connectClient.Value.FrmRdp.UpdateImage(_connectClient.Value.StreamCodec.DecodeData(ms), true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
packet.Image = null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-05-29 14:34:57 -07:00
|
|
|
|
public void AddMonitors(int montiors)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
cbMonitors.Invoke((MethodInvoker) delegate
|
|
|
|
|
{
|
|
|
|
|
for (int i = 0; i < montiors; i++)
|
|
|
|
|
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-23 07:55:44 -07:00
|
|
|
|
private void CountFps()
|
|
|
|
|
{
|
|
|
|
|
var deltaTime = (float)_sWatch.Elapsed.TotalSeconds;
|
|
|
|
|
_sWatch = Stopwatch.StartNew();
|
|
|
|
|
|
|
|
|
|
_frameCounter.Update(deltaTime);
|
|
|
|
|
|
|
|
|
|
UpdateFps(_frameCounter.AverageFramesPerSecond);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void UpdateFps(float fps)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
this.Invoke((MethodInvoker)delegate
|
|
|
|
|
{
|
2015-07-25 15:10:59 -07:00
|
|
|
|
this.Text = string.Format("{0} - FPS: {1}", WindowHelper.GetWindowTitle("Remote Desktop", _connectClient), fps.ToString("0.00"));
|
2015-07-23 07:55:44 -07:00
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
catch (InvalidOperationException)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-07-28 02:48:27 -07:00
|
|
|
|
private void UpdateScreenResolution(int width, int height)
|
|
|
|
|
{
|
|
|
|
|
_screenWidth = width;
|
|
|
|
|
_screenHeight = height;
|
|
|
|
|
}
|
|
|
|
|
|
2015-06-20 09:50:02 -07:00
|
|
|
|
public void UpdateImage(Bitmap bmp, bool cloneBitmap = false)
|
2015-05-29 14:34:57 -07:00
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2015-07-23 07:55:44 -07:00
|
|
|
|
CountFps();
|
2015-07-28 02:48:27 -07:00
|
|
|
|
UpdateScreenResolution(bmp.Width, bmp.Height);
|
2015-05-29 14:34:57 -07:00
|
|
|
|
picDesktop.Invoke((MethodInvoker) delegate
|
|
|
|
|
{
|
2015-07-26 06:27:38 -07:00
|
|
|
|
// get old image to dispose it correctly
|
|
|
|
|
var oldImage = picDesktop.Image;
|
|
|
|
|
|
2015-07-27 14:42:26 -07:00
|
|
|
|
picDesktop.SuspendLayout();
|
2015-07-28 02:48:27 -07:00
|
|
|
|
picDesktop.Image = cloneBitmap ? new Bitmap(bmp, picDesktop.Width, picDesktop.Height) /*resize bitmap*/ : bmp;
|
2015-07-27 14:42:26 -07:00
|
|
|
|
picDesktop.ResumeLayout();
|
2015-07-26 06:27:38 -07:00
|
|
|
|
|
|
|
|
|
if (oldImage != null)
|
|
|
|
|
oldImage.Dispose();
|
2015-05-29 14:34:57 -07:00
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
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-23 07:55:44 -07:00
|
|
|
|
private void ToggleControls(bool t)
|
|
|
|
|
{
|
|
|
|
|
_started = !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)
|
|
|
|
|
{
|
2015-07-23 07:55:44 -07:00
|
|
|
|
if (_started)
|
|
|
|
|
new Core.Packets.ServerPackets.GetDesktop(0, 0, RemoteDesktopAction.Stop).Execute(_connectClient);
|
|
|
|
|
if (_sWatch != null)
|
|
|
|
|
_sWatch.Stop();
|
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-04-21 11:27:52 -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
|
|
|
|
_frameCounter = new FrameCounter();
|
|
|
|
|
_sWatch = Stopwatch.StartNew();
|
|
|
|
|
|
|
|
|
|
ToggleControls(false);
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
_sWatch.Stop();
|
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)
|
|
|
|
|
{
|
|
|
|
|
return localX * _screenWidth / picDesktop.Width;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private int GetRemoteHeight(int localY)
|
|
|
|
|
{
|
|
|
|
|
return localY * _screenHeight / picDesktop.Height;
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
|
}
|