Remove Monitor class

feature/SubscribeAll
Clive Galway 5 years ago
parent 6cfa439bcb
commit 170392741d

@ -44,7 +44,6 @@
<Compile Include="Helpers\ManagedWrapper.cs" />
<Compile Include="Helpers\MultimediaTimer.cs" />
<Compile Include="Manager.cs" />
<Compile Include="Monitor.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />

@ -1,243 +0,0 @@
using System;
using System.Collections.Concurrent;
using System.Threading;
using AutoHotInterception.Helpers;
namespace AutoHotInterception
{
public class Monitor : IDisposable
{
private readonly IntPtr _deviceContext;
private readonly ConcurrentDictionary<int, bool> _filteredDevices = new ConcurrentDictionary<int, bool>();
private dynamic _keyboardCallback;
private dynamic _mouseCallback;
private readonly MultimediaTimer _timer;
private readonly int _pollRate = 1;
private volatile bool _pollThreadRunning;
#region Public
public Monitor()
{
_deviceContext = ManagedWrapper.CreateContext();
_timer = new MultimediaTimer() { Interval = _pollRate };
_timer.Elapsed += DoPoll;
SetThreadState(true);
}
public void Dispose()
{
SetFilterState(false);
SetThreadState(false);
}
public string OkCheck()
{
return "OK";
}
public void Subscribe(dynamic keyboardCallback, dynamic mouseCallback)
{
_keyboardCallback = keyboardCallback;
_mouseCallback = mouseCallback;
}
public bool SetDeviceFilterState(int device, bool state)
{
SetFilterState(false);
if (state)
_filteredDevices[device] = true;
else
_filteredDevices.TryRemove(device, out _);
if (_filteredDevices.Count > 0) SetFilterState(true);
return true;
}
public HelperFunctions.DeviceInfo[] GetDeviceList()
{
return HelperFunctions.GetDeviceList(_deviceContext);
}
public int GetKeyboardId(int vid, int pid, int instance = 1)
{
return GetDeviceId(false, vid, pid, instance);
}
public int GetMouseId(int vid, int pid, int instance = 1)
{
return GetDeviceId(true, vid, pid, instance);
}
public int GetKeyboardIdFromHandle(string handle, int instance = 1)
{
return HelperFunctions.GetDeviceIdFromHandle(_deviceContext, false, handle, instance);
}
public int GetMouseIdFromHandle(string handle, int instance = 1)
{
return HelperFunctions.GetDeviceIdFromHandle(_deviceContext, true, handle, instance);
}
public int GetDeviceIdFromHandle(bool isMouse, string handle, int instance = 1)
{
return HelperFunctions.GetDeviceIdFromHandle(_deviceContext, isMouse, handle, instance);
}
public int GetDeviceId(bool isMouse, int vid, int pid, int instance = 1)
{
return HelperFunctions.GetDeviceId(_deviceContext, isMouse, vid, pid, instance);
}
#endregion
#region Private
private void SetThreadState(bool state)
{
if (state && !_timer.IsRunning)
{
SetFilterState(true);
_timer.Start();
}
else if (!state && _timer.IsRunning)
{
SetFilterState(false);
_timer.Stop();
while (_pollThreadRunning) // Are we mid-poll?
{
Thread.Sleep(10); // Wait until poll ends
}
}
}
private int IsMonitoredDevice(int device)
{
return Convert.ToInt32(_filteredDevices.ContainsKey(device));
}
private void SetFilterState(bool state)
{
ManagedWrapper.SetFilter(_deviceContext, IsMonitoredDevice,
state ? ManagedWrapper.Filter.All : ManagedWrapper.Filter.None);
}
private void DoPoll(object sender, EventArgs e)
{
_pollThreadRunning = true;
var stroke = new ManagedWrapper.Stroke();
for (var i = 1; i < 11; i++)
{
while (ManagedWrapper.Receive(_deviceContext, i, ref stroke, 1) > 0)
{
ManagedWrapper.Send(_deviceContext, i, ref stroke, 1);
var processedState = HelperFunctions.KeyboardStrokeToKeyboardState(stroke);
if (processedState.Ignore)
FireKeyboardCallback(i, new KeyboardCallback
{
Id = i,
Code = stroke.key.code,
State = stroke.key.state,
Info = "Ignored - showing raw values"
});
else
FireKeyboardCallback(i, new KeyboardCallback
{
Id = i,
Code = processedState.Code,
State = processedState.State,
Info = stroke.key.code > 255 ? "Extended" : ""
});
}
}
for (var i = 11; i < 21; i++)
{
while (ManagedWrapper.Receive(_deviceContext, i, ref stroke, 1) > 0)
{
ManagedWrapper.Send(_deviceContext, i, ref stroke, 1);
if (stroke.mouse.state != 0)
{
// Mouse Button
var btnStates = HelperFunctions.MouseStrokeToButtonStates(stroke);
foreach (var btnState in btnStates)
{
FireMouseCallback(new MouseCallback
{
Id = i,
Code = btnState.Button,
State = btnState.State,
Info = "Mouse Button"
});
}
}
else if ((stroke.mouse.flags & (ushort)ManagedWrapper.MouseFlag.MouseMoveAbsolute) ==
(ushort)ManagedWrapper.MouseFlag.MouseMoveAbsolute)
{
// Absolute Mouse Move
FireMouseCallback(new MouseCallback
{
Id = i,
X = stroke.mouse.x,
Y = stroke.mouse.y,
Info = "Absolute Move"
});
}
else if ((stroke.mouse.flags & (ushort)ManagedWrapper.MouseFlag.MouseMoveRelative) ==
(ushort)ManagedWrapper.MouseFlag.MouseMoveRelative)
{
// Relative Mouse Move
FireMouseCallback(new MouseCallback
{
Id = i,
X = stroke.mouse.x,
Y = stroke.mouse.y,
Info = "Relative Move"
});
}
}
}
_pollThreadRunning = false;
}
private void FireKeyboardCallback(int id, KeyboardCallback data)
{
ThreadPool.QueueUserWorkItem(threadProc =>
_keyboardCallback(data.Id, data.Code, data.State, data.Info));
}
private void FireMouseCallback(MouseCallback data)
{
ThreadPool.QueueUserWorkItem(threadProc =>
_mouseCallback(data.Id, data.Code, data.State, data.X, data.Y, data.Info));
}
public class MouseCallback
{
public int Id { get; set; }
public int Code { get; set; }
public int State { get; set; }
public int X { get; set; }
public int Y { get; set; }
public string Info { get; set; } = "";
}
public class KeyboardCallback
{
public int Id { get; set; }
public int Code { get; set; }
public int State { get; set; }
public string Info { get; set; } = "";
}
#endregion
}
}

@ -1,31 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AutoHotInterception;
namespace TestApp
{
public class MonitorTester
{
public MonitorTester()
{
var mon = new Monitor();
mon.Subscribe(new Action<int, int, int, string>((id, code, value, info) =>
{
Console.WriteLine($"Keyboard: ID={id}, Code={code}, Value={value}, Info={info}");
}),
new Action<int, int, int, int, int, string>((id, code, value, x, y, info) =>
{
Console.WriteLine($"Mouse: ID={id}, Code={code}, Value={value}, X={x}, Y={y}, Info={info}");
})
);
var keyboardId = mon.GetKeyboardId(0x04F2, 0x0112);
mon.SetDeviceFilterState(keyboardId, true);
var mouseId = mon.GetMouseIdFromHandle(@"HID\VID_046D&PID_C00C&REV_0620");
mon.SetDeviceFilterState(mouseId, true);
}
}
}

@ -12,7 +12,6 @@ namespace TestApp
//var kt = new KeyboardTester();
//var kkt = new KeyboardKeyTester();
//var tt = new TabletTester();
//var mon = new MonitorTester();
Console.ReadLine();
}
}

@ -44,7 +44,6 @@
<ItemGroup>
<Compile Include="KeyboardTester.cs" />
<Compile Include="KeyboardKeyTester.cs" />
<Compile Include="MonitorTester.cs" />
<Compile Include="MouseButtonsTester.cs" />
<Compile Include="MouseTester.cs" />
<Compile Include="Program.cs" />

@ -3,7 +3,7 @@
class AutoHotInterception {
_contextManagers := {}
__New(cls := "Manager") {
__New() {
bitness := A_PtrSize == 8 ? "x64" : "x86"
dllName := "interception.dll"
dllFile := A_LineFile "\..\" bitness "\" dllName
@ -31,7 +31,7 @@ class AutoHotInterception {
asm := CLR_LoadLibrary(dllFile)
try {
this.Instance := asm.CreateInstance("AutoHotInterception." cls)
this.Instance := asm.CreateInstance("AutoHotInterception.Manager")
}
catch {
MsgBox % dllName " failed to load`n`n" hintMessage

Loading…
Cancel
Save