-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDevice.cs
More file actions
320 lines (260 loc) · 12.9 KB
/
Device.cs
File metadata and controls
320 lines (260 loc) · 12.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ODIF;
using ODIF.Extensions;
using System.Threading;
using System.Drawing;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Windows;
using System.Collections.ObjectModel;
using System.Windows.Controls;
namespace HIDMouse
{
[PluginInfo(
PluginName = "Mouse HID Input",
PluginDescription = "Allows for mouse connectivity in InputMapper.",
PluginID = 35,
PluginAuthorName = "InputMapper",
PluginAuthorEmail = "jhebbel@gmail.com",
PluginAuthorURL = "http://inputmapper.com",
PluginIconPath = @"pack://application:,,,/HIDMouse;component/Resources/Mouse.ico"
)]
public class iMouse_Plugin : InputDevicePlugin, IMPlugin
{
//public bool PluginActive { get { return true; } set { } }
//public Dictionary<string, InputDevice> Devices { get; set; }
//public AsyncObservableCollection<InputDevice> Devices { get; set; }
public iMouse_Plugin()
{
//Devices = new AsyncObservableCollection<InputDevice>();
iMouse_Device Mouse = new iMouse_Device();
Devices.Add(Mouse);
}
public void Dispose()
{
foreach (InputDevice Device in Devices.ToList())
{
Device.Dispose();
Devices.Remove(Device);
}
}
}
public static class mouseExtensions
{
public static iMouse_Device instance {get;set;}
}
internal class MouseDevice
{
public InputChannelTypes.JoyAxis mouseX { get; set; }
public InputChannelTypes.JoyAxis mouseY { get; set; }
public InputChannelTypes.JoyAxis mouseDx { get; set; }
public InputChannelTypes.JoyAxis mouseDy { get; set; }
public InputChannelTypes.Button mouseB1 { get; set; }
public InputChannelTypes.Button mouseB2 { get; set; }
public OutputChannelTypes.Toggle trapPosition { get; set; }
public OutputChannelTypes.Toggle trapPositionToggle { get; set; }
public MouseDevice()
{
mouseX = new InputChannelTypes.JoyAxis("Mouse X","This is the X position relitive to Windows' defined work area", Properties.Resources.Keyboard_White_Mouse_x.ToImageSource());
mouseY = new InputChannelTypes.JoyAxis("Mouse Y", "This is the Y position relitive to Windows' defined work area", Properties.Resources.Keyboard_White_Mouse_y.ToImageSource());
mouseDx = new InputChannelTypes.JoyAxis("Mouse Delta X" , "This is the speed at which the mouse is moving across the X axis", Properties.Resources.Keyboard_White_Mouse_dX.ToImageSource());
mouseDy = new InputChannelTypes.JoyAxis("Mouse Delta Y", "This is the speed at which the mouse is moving across the Y axis", Properties.Resources.Keyboard_White_Mouse_dY.ToImageSource());
mouseB1 = new InputChannelTypes.Button("Mouse Button 1", "Left mouse button",Properties.Resources.Keyboard_White_Mouse_Left.ToImageSource());
mouseB2 = new InputChannelTypes.Button("Mouse Button 2", "Right mouse button",Properties.Resources.Keyboard_White_Mouse_Right.ToImageSource());
trapPosition = new OutputChannelTypes.Toggle("Trap Mouse Position", "Use this when emulating mouse to controller stick to keep mouse for reaching screen edge");
trapPositionToggle = new OutputChannelTypes.Toggle("Trap Mouse Toggle", "Toggles on/off the 'Trap Mouse' setting");
trapPositionToggle.PropertyChanged += (s, e) => {
if ((s as DeviceChannel).Value == true)
{
trapPosition.Value = !(bool)trapPosition.Value;
}
};
}
}
public class iMouse_Device : InputDevice
{
public MappingWindow mappingWindow { get; }
public enum ThreadStatus { None, Starting, Running, RequestStop, Stoped, Error };
public ThreadStatus Status { get; set; }
private Thread DeltaListenerThread;
public ConnectionTypes DeviceConnectionType { get { return ConnectionTypes.USB | ConnectionTypes.BT; } }
private MouseDevice mouseDevice { get; set; }
private Timer checkDeltaTimer;
protected override void Dispose(bool disposing)
{
checkDeltaTimer.Dispose();
CallbackTimestamp.Stop();
UnhookWindowsHookEx(_hookID);
mouseExtensions.instance = null;
base.Dispose(disposing);
}
public iMouse_Device()
{
mouseDevice = new MouseDevice();
//InputChannels = new ObservableCollection<InputChannel>();
//OutputChannels = new ObservableCollection<OutputChannel>();
this.StatusIcon = new BitmapImage(new Uri("pack://application:,,,/HIDMouse;component/Resources/Mouse.ico"));
this.DeviceName = "Generic HID Mouse";
this.mappingWindow = new MouseMapping(this);
//mappingWindow = new test();
InputChannels.Add(mouseDevice.mouseX);
InputChannels.Add(mouseDevice.mouseY);
InputChannels.Add(mouseDevice.mouseDx);
InputChannels.Add(mouseDevice.mouseDy);
InputChannels.Add(mouseDevice.mouseB1);
InputChannels.Add(mouseDevice.mouseB2);
OutputChannels.Add(mouseDevice.trapPosition);
OutputChannels.Add(mouseDevice.trapPositionToggle);
mouseExtensions.instance = this;
CallbackTimestamp.Start();
_hookID = SetHook(_proc);
checkDeltaTimer = new Timer(checkDeltaCallback, null, 2, 2);
}
private double safeStickThrow(double value)
{
value = Math.Min(value,1);
value = Math.Max(value, -1);
return value;
}
private void checkDeltaCallback(object state)
{
if (mouseDevice.trapPosition.Value == true)
{
int centerX = Convert.ToInt32(SystemParameters.WorkArea.Left + SystemParameters.WorkArea.Width / 2);
int centerY = Convert.ToInt32(SystemParameters.WorkArea.Top + SystemParameters.WorkArea.Height / 2);
int timeDif = (int)CallbackTimestamp.ElapsedMilliseconds;
if (timeDif > 0)
{
int xDif = CurrentHook.pt.x - centerX;
int yDif = CurrentHook.pt.y - centerY;
double xDelta = safeStickThrow(((double)xDif / (double)timeDif) * .05);
double yDelta = safeStickThrow(((double)yDif / (double)timeDif) * .05);
xDeltaH.Add(xDelta);
if (xDeltaH.Count > 5)
xDeltaH.RemoveAt(0);
yDeltaH.Add(yDelta);
if (yDeltaH.Count > 5)
yDeltaH.RemoveAt(0);
mouseExtensions.instance.mouseDevice.mouseDx.Value = xDeltaH.ToArray().Average();
mouseExtensions.instance.mouseDevice.mouseDy.Value = yDeltaH.ToArray().Average();
//if (Global.Hashes.ContainsKey("MouseHIDInput.GenericHIDMouse.MouseDeltaX"))
// Console.WriteLine(Global.Hashes["MouseHIDInput.GenericHIDMouse.MouseDeltaX"].Value);
LastHook = CurrentHook;
CallbackTimestamp.Restart();
NativeMethods.SetCursorPos(centerX, centerY);
}
}
else {
int timeDif = (int)CallbackTimestamp.ElapsedMilliseconds;
if (timeDif > 0)
{
int xDif = CurrentHook.pt.x - LastHook.pt.x;
int yDif = CurrentHook.pt.y - LastHook.pt.y;
double xDelta = safeStickThrow(((double)xDif / (double)timeDif) * .05);
double yDelta = safeStickThrow(((double)yDif / (double)timeDif) * -.05);
xDeltaH.Add(xDelta);
if (xDeltaH.Count > 5)
xDeltaH.RemoveAt(0);
yDeltaH.Add(yDelta);
if (yDeltaH.Count > 5)
yDeltaH.RemoveAt(0);
mouseExtensions.instance.mouseDevice.mouseDx.Value = xDeltaH.ToArray().Average();
mouseExtensions.instance.mouseDevice.mouseDy.Value = yDeltaH.ToArray().Average();
//if (Global.Hashes.ContainsKey("MouseHIDInput.GenericHIDMouse.MouseDeltaX"))
// Console.WriteLine(Global.Hashes["MouseHIDInput.GenericHIDMouse.MouseDeltaX"].Value);
LastHook = CurrentHook;
CallbackTimestamp.Restart();
}
//System.Threading.Thread.Sleep(2);
}
}
private static LowLevelMouseProc _proc = HookCallback;
private static IntPtr _hookID = IntPtr.Zero;
private static IntPtr SetHook(LowLevelMouseProc proc)
{
using (Process curProcess = Process.GetCurrentProcess())
using (ProcessModule curModule = curProcess.MainModule)
{
return SetWindowsHookEx(WH_MOUSE_LL, proc,
GetModuleHandle(curModule.ModuleName), 0);
}
}
private delegate IntPtr LowLevelMouseProc(int nCode, IntPtr wParam, IntPtr lParam);
private static Stopwatch CallbackTimestamp = new Stopwatch();
private static MSLLHOOKSTRUCT LastHook = new MSLLHOOKSTRUCT();
private static MSLLHOOKSTRUCT CurrentHook = new MSLLHOOKSTRUCT();
private static List<double> xDeltaH = new List<double>();
private static List<double> yDeltaH = new List<double>();
private static IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam)
{
if (nCode >= 0 && MouseMessages.WM_MOUSEMOVE == (MouseMessages)wParam)
{
MSLLHOOKSTRUCT hookStruct = (MSLLHOOKSTRUCT)Marshal.PtrToStructure(lParam, typeof(MSLLHOOKSTRUCT));
mouseExtensions.instance.mouseDevice.mouseX.Value = (hookStruct.pt.x ) / SystemParameters.VirtualScreenWidth;
mouseExtensions.instance.mouseDevice.mouseY.Value = (hookStruct.pt.y ) / SystemParameters.VirtualScreenHeight;
//LastHook = CurrentHook;
CurrentHook = hookStruct;
}
lock (mouseExtensions.instance)
{
if (nCode >= 0 && ((MouseMessages)wParam).HasFlag(MouseMessages.WM_LBUTTONDOWN))
mouseExtensions.instance.mouseDevice.mouseB1.Value = true;
if (nCode >= 0 && ((MouseMessages)wParam).HasFlag(MouseMessages.WM_LBUTTONUP))
mouseExtensions.instance.mouseDevice.mouseB1.Value = false;
if (nCode >= 0 && ((MouseMessages)wParam).HasFlag(MouseMessages.WM_RBUTTONDOWN))
mouseExtensions.instance.mouseDevice.mouseB2.Value = true;
if (nCode >= 0 && ((MouseMessages)wParam).HasFlag(MouseMessages.WM_RBUTTONUP))
mouseExtensions.instance.mouseDevice.mouseB2.Value = false;
}
return CallNextHookEx(_hookID, nCode, wParam, lParam);
}
private const int WH_MOUSE_LL = 14;
private enum MouseMessages
{
WM_LBUTTONDOWN = 0x0201,
WM_LBUTTONUP = 0x0202,
WM_LBUTTONDBLCLK = 0x0203,
WM_MOUSEMOVE = 0x0200,
WM_MOUSEWHEEL = 0x020A,
WM_RBUTTONDOWN = 0x0204,
WM_RBUTTONUP = 0x0205,
WM_RBUTTONDBLCLK = 0x0206,
WM_MBUTTONDOWN = 0x0207,
WM_MBUTTONUP = 0x0208,
WM_MBUTTONDBLCLK = 0x0209,
}
[StructLayout(LayoutKind.Sequential)]
private struct POINT
{
public int x;
public int y;
}
[StructLayout(LayoutKind.Sequential)]
private struct MSLLHOOKSTRUCT
{
public POINT pt;
public uint mouseData;
public uint flags;
public uint time;
public IntPtr dwExtraInfo;
}
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern IntPtr SetWindowsHookEx(int idHook,
LowLevelMouseProc lpfn, IntPtr hMod, uint dwThreadId);
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool UnhookWindowsHookEx(IntPtr hhk);
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern IntPtr CallNextHookEx(IntPtr hhk, int nCode,
IntPtr wParam, IntPtr lParam);
[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern IntPtr GetModuleHandle(string lpModuleName);
}
}