Skip to content

Commit

Permalink
implemented NamedPipe
Browse files Browse the repository at this point in the history
  • Loading branch information
Valkirie committed Nov 12, 2021
1 parent 70d86cb commit 233f024
Show file tree
Hide file tree
Showing 22 changed files with 663 additions and 325 deletions.
53 changes: 53 additions & 0 deletions ControllerHelper/ControllerHelper.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

87 changes: 87 additions & 0 deletions ControllerHelper/ControllerHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
using ControllerService;
using Microsoft.Toolkit.Uwp.Notifications;
using System;
using System.Runtime.InteropServices;
using System.Timers;
using System.Windows.Forms;
using Timer = System.Timers.Timer;

namespace ControllerHelper
{
public partial class ControllerHelper : Form
{
#region imports
[DllImport("User32.dll")]
static extern IntPtr GetForegroundWindow();
[DllImport("user32.dll", SetLastError = true)]
static extern uint GetWindowThreadProcessId(IntPtr hWnd, out IntPtr lpdwProcessId);
#endregion

private PipeClient PipeClient;
private Timer MonitorTimer;
private IntPtr CurrentProcess;

private MouseHook m_Hook;

public ControllerHelper()
{
InitializeComponent();
this.Hide();
}

private void ControllerHelper_Load(object sender, EventArgs e)
{
// start the pipe client
PipeClient = new PipeClient("ControllerService", this);
PipeClient.Start();

// start mouse hook
m_Hook = new MouseHook(PipeClient);
m_Hook.Start();

// monitors processes
MonitorTimer = new Timer(1000) { Enabled = true, AutoReset = true };
MonitorTimer.Elapsed += MonitorHelper;
}

private void ControllerHelper_Closed(object sender, System.Windows.Forms.FormClosedEventArgs e)
{
PipeClient.Stop();
}

public void Kill()
{
Application.Exit();
}

private void MonitorHelper(object sender, ElapsedEventArgs e)
{
IntPtr hWnd = GetForegroundWindow();
IntPtr processId;

if (GetWindowThreadProcessId(hWnd, out processId) == 0)
return;

if (processId != CurrentProcess)
{
PipeMessage message = new PipeMessage { Code = PipeCode.CODE_PROCESS, args = new string[] { processId.ToString() } };
PipeClient.SendMessage(message);

CurrentProcess = processId;
}
}

public void SendToast(string title, string content)
{
string url = "file:///" + AppDomain.CurrentDomain.BaseDirectory + "Toast.png";
var uri = new Uri(url);

new ToastContentBuilder()
.AddText(title)
.AddText(content)
.AddAppLogoOverride(uri, ToastGenericAppLogoCrop.Circle)
.SetToastDuration(ToastDuration.Short)
.Show();
}
}
}
37 changes: 37 additions & 0 deletions ControllerHelper/ControllerHelper.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>net5.0-windows10.0.19041.0</TargetFramework>
<AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>
<AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
<UseWindowsForms>true</UseWindowsForms>
<AssemblyTitle>ControllerHelper</AssemblyTitle>
<Product>ControllerHelper</Product>
<Copyright>Copyright © 2021</Copyright>
<OutputPath>$(SolutionDir)bin\</OutputPath>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugType>full</DebugType>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Toolkit.Uwp.Notifications" Version="7.1.1" />
<PackageReference Include="MouseKeyHook" Version="5.6.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\ControllerService\ControllerService.csproj" />
</ItemGroup>

<ItemGroup>
<None Update="Toast.png">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>

</Project>
60 changes: 60 additions & 0 deletions ControllerHelper/ControllerHelper.resx
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<root>
<xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xsd:import namespace="http://www.w3.org/XML/1998/namespace" />
<xsd:element name="root" msdata:IsDataSet="true">
<xsd:complexType>
<xsd:choice maxOccurs="unbounded">
<xsd:element name="metadata">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" />
</xsd:sequence>
<xsd:attribute name="name" use="required" type="xsd:string" />
<xsd:attribute name="type" type="xsd:string" />
<xsd:attribute name="mimetype" type="xsd:string" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="assembly">
<xsd:complexType>
<xsd:attribute name="alias" type="xsd:string" />
<xsd:attribute name="name" type="xsd:string" />
</xsd:complexType>
</xsd:element>
<xsd:element name="data">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
<xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" msdata:Ordinal="1" />
<xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" />
<xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="resheader">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" />
</xsd:complexType>
</xsd:element>
</xsd:choice>
</xsd:complexType>
</xsd:element>
</xsd:schema>
<resheader name="resmimetype">
<value>text/microsoft-resx</value>
</resheader>
<resheader name="version">
<value>2.0</value>
</resheader>
<resheader name="reader">
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
</root>
72 changes: 72 additions & 0 deletions ControllerHelper/MouseHook.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
using ControllerService;
using Gma.System.MouseKeyHook;
using System.Threading;
using System.Timers;
using System.Windows.Forms;
using Timer = System.Timers.Timer;

namespace ControllerHelper
{
public class MouseHook
{
private IKeyboardMouseEvents m_Events;
private Thread m_Hook;
private Timer m_Timer;

private PipeClient client;

public MouseHook(PipeClient client)
{
this.client = client;

// send MouseUp after 50ms interval
m_Timer = new Timer() { Enabled = false, Interval = 50, AutoReset = false };
m_Timer.Elapsed += SendMouseUp;

m_Hook = new Thread(Subscribe) { IsBackground = true };
}

public void Start()
{
m_Hook.Start();
}

private void Subscribe()
{
m_Events = Hook.GlobalEvents();
m_Events.MouseMoveExt += OnMouseMove;
m_Events.MouseDownExt += OnMouseDown;
m_Events.MouseUpExt += OnMouseUp;
Application.Run();
}

private void SendMouseUp(object sender, ElapsedEventArgs e)
{
client.SendMessage(new PipeMessage { Code = PipeCode.CODE_CURSOR_UP });
}

private void OnMouseDown(object sender, MouseEventExtArgs e)
{
m_Timer.Stop();
client.SendMessage(new PipeMessage { Code = PipeCode.CODE_CURSOR_DOWN });
}

private void OnMouseMove(object sender, MouseEventExtArgs e)
{
short TouchX = (short)(e.X);
short TouchY = (short)(e.Y);
client.SendMessage(new PipeMessage { Code = PipeCode.CODE_CURSOR_MOVE, args = new string[] { TouchX.ToString(), TouchY.ToString() } });
}

private void OnMouseUp(object sender, MouseEventExtArgs e)
{
m_Timer.Start();
}

internal void Stop()
{
m_Events.Dispose();
m_Hook = null;
}
}
}
68 changes: 68 additions & 0 deletions ControllerHelper/PipeClient.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
using ControllerService;
using NamedPipeWrapper;
using System;
using System.Windows.Forms;

namespace ControllerHelper
{
public class PipeClient
{
private readonly NamedPipeClient<PipeMessage> client;
private readonly ControllerHelper helper;

public PipeClient(string pipeName, ControllerHelper helper)
{
this.helper = helper;

client = new NamedPipeClient<PipeMessage>(pipeName);
client.AutoReconnect = true;

client.Disconnected += OnClientDisconnected;
client.ServerMessage += OnServerMessage;
client.Error += OnError;
}

private void OnClientDisconnected(NamedPipeConnection<PipeMessage, PipeMessage> connection)
{
client.Stop();
helper.Kill();
}

public void Start()
{
if (client == null)
return;

client.Start();
}

public void Stop()
{
if (client == null)
return;

client.Stop();
}

private void OnServerMessage(NamedPipeConnection<PipeMessage, PipeMessage> connection, PipeMessage message)
{
switch (message.Code)
{
case PipeCode.CODE_TOAST:
helper.SendToast(message.args[0], message.args[1]);
break;
}
}

private void OnError(Exception exception)
{
client.Stop();
helper.Kill();
}

public void SendMessage(PipeMessage message)
{
client.PushMessage(message);
}
}
}
Loading

0 comments on commit 233f024

Please sign in to comment.