forked from AndersMalmgren/FreePIE
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MousePlugin.cs
324 lines (271 loc) · 9.08 KB
/
MousePlugin.cs
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
321
322
323
324
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Runtime.InteropServices;
using FreePIE.Core.Contracts;
using FreePIE.Core.Plugins.Strategies;
using SlimDX.DirectInput;
using SlimDX.RawInput;
namespace FreePIE.Core.Plugins
{
[GlobalType(Type = typeof(MouseGlobal))]
public class MousePlugin : Plugin
{
// Mouse position state variables
private double deltaXOut;
private double deltaYOut;
private int wheel;
public const int WheelMax = 120;
private DirectInput directInputInstance = new DirectInput();
private Mouse mouseDevice;
private MouseState currentMouseState;
private bool leftPressed;
private bool rightPressed;
private bool middlePressed;
private GetPressedStrategy<int> getButtonPressedStrategy;
private SetPressedStrategy setButtonPressedStrategy;
public override object CreateGlobal()
{
return new MouseGlobal(this);
}
public override Action Start()
{
IntPtr handle = Process.GetCurrentProcess().MainWindowHandle;
mouseDevice = new Mouse(directInputInstance);
if (mouseDevice == null)
throw new Exception("Failed to create mouse device");
mouseDevice.SetCooperativeLevel(handle, CooperativeLevel.Background | CooperativeLevel.Nonexclusive);
mouseDevice.Properties.AxisMode = DeviceAxisMode.Relative; // Get delta values
mouseDevice.Acquire();
getButtonPressedStrategy = new GetPressedStrategy<int>(IsButtonDown);
setButtonPressedStrategy = new SetPressedStrategy(SetButtonDown, SetButtonUp);
OnStarted(this, new EventArgs());
return null;
}
public override void Stop()
{
if (mouseDevice != null)
{
mouseDevice.Unacquire();
mouseDevice.Dispose();
mouseDevice = null;
}
if (directInputInstance != null)
{
directInputInstance.Dispose();
directInputInstance = null;
}
}
public override string FriendlyName
{
get { return "Mouse"; }
}
static private MouseKeyIO.MOUSEINPUT MouseInput(int x, int y, uint data, uint t, uint flag)
{
var mi = new MouseKeyIO.MOUSEINPUT {dx = x, dy = y, mouseData = data, time = t, dwFlags = flag};
return mi;
}
public override void DoBeforeNextExecute()
{
// If a mouse command was given in the script, issue it all at once right here
if ((int)deltaXOut != 0 || (int)deltaYOut != 0 || wheel != 0)
{
var input = new MouseKeyIO.INPUT[1];
input[0].type = MouseKeyIO.INPUT_MOUSE;
input[0].mi = MouseInput((int)deltaXOut, (int)deltaYOut, (uint)wheel, 0, MouseKeyIO.MOUSEEVENTF_MOVE | MouseKeyIO.MOUSEEVENTF_WHEEL);
MouseKeyIO.SendInput(1, input, Marshal.SizeOf(input[0].GetType()));
// Reset the mouse values
if ((int)deltaXOut != 0)
{
deltaXOut = deltaXOut - (int)deltaXOut;
}
if ((int)deltaYOut != 0)
{
deltaYOut = deltaYOut - (int)deltaYOut;
}
wheel = 0;
}
currentMouseState = null; // flush the mouse state
setButtonPressedStrategy.Do();
}
public double DeltaX
{
set
{
deltaXOut = deltaXOut + value;
}
get { return CurrentMouseState.X; }
}
public double DeltaY
{
set
{
deltaYOut = deltaYOut + value;
}
get { return CurrentMouseState.Y; }
}
public int Wheel
{
get { return CurrentMouseState.Z; }
set { wheel = value; }
}
private MouseState CurrentMouseState
{
get
{
if (currentMouseState == null)
currentMouseState = mouseDevice.GetCurrentState();
return currentMouseState;
}
}
public bool IsButtonDown(int index)
{
return CurrentMouseState.IsPressed(index);
}
public bool IsButtonPressed(int button)
{
return getButtonPressedStrategy.IsPressed(button);
}
private void SetButtonDown(int button)
{
SetButtonPressed(button, true);
}
private void SetButtonUp(int button)
{
SetButtonPressed(button, false);
}
public void SetButtonPressed(int index, bool pressed)
{
uint btn_flag = 0;
if (index == 0)
{
if (pressed)
{
if (!leftPressed)
btn_flag = MouseKeyIO.MOUSEEVENTF_LEFTDOWN;
}
else
{
if (leftPressed)
btn_flag = MouseKeyIO.MOUSEEVENTF_LEFTUP;
}
leftPressed = pressed;
}
else if (index == 1)
{
if (pressed)
{
if (!rightPressed)
btn_flag = MouseKeyIO.MOUSEEVENTF_RIGHTDOWN;
}
else
{
if (rightPressed)
btn_flag = MouseKeyIO.MOUSEEVENTF_RIGHTUP;
}
rightPressed = pressed;
}
else
{
if (pressed)
{
if (!middlePressed)
btn_flag = MouseKeyIO.MOUSEEVENTF_MIDDLEDOWN;
}
else
{
if (middlePressed)
btn_flag = MouseKeyIO.MOUSEEVENTF_MIDDLEUP;
}
middlePressed = pressed;
}
if (btn_flag != 0) {
var input = new MouseKeyIO.INPUT[1];
input[0].type = MouseKeyIO.INPUT_MOUSE;
input[0].mi = MouseInput(0, 0, 0, 0, btn_flag);
MouseKeyIO.SendInput(1, input, Marshal.SizeOf(input[0].GetType()));
}
}
public void PressAndRelease(int button)
{
setButtonPressedStrategy.Add(button);
}
public void PressAndRelease(int button, bool state)
{
setButtonPressedStrategy.Add(button, state);
}
}
[Global(Name = "mouse")]
public class MouseGlobal
{
private readonly MousePlugin plugin;
public MouseGlobal(MousePlugin plugin)
{
this.plugin = plugin;
}
public int wheelMax
{
get { return MousePlugin.WheelMax; }
}
public double deltaX
{
get { return plugin.DeltaX; }
set { plugin.DeltaX = value; }
}
public double deltaY
{
get { return plugin.DeltaY; }
set { plugin.DeltaY = value; }
}
public int wheel
{
get { return plugin.Wheel; }
set { plugin.Wheel = value; }
}
public bool wheelUp
{
get { return plugin.Wheel == wheelMax; }
set { plugin.Wheel = value ? wheelMax : 0; }
}
public bool wheelDown
{
get { return plugin.Wheel == -wheelMax; }
set { plugin.Wheel = value ? -wheelMax : 0; }
}
public bool leftButton
{
get { return plugin.IsButtonDown(0); }
set { plugin.SetButtonPressed(0, value); }
}
public bool middleButton
{
get { return plugin.IsButtonDown(2); }
set { plugin.SetButtonPressed(2, value); }
}
public bool rightButton
{
get { return plugin.IsButtonDown(1); }
set { plugin.SetButtonPressed(1, value); }
}
public bool getButton(int button)
{
return plugin.IsButtonDown(button);
}
public void setButton(int button, bool pressed)
{
plugin.SetButtonPressed(button, pressed);
}
public bool getPressed(int button)
{
return plugin.IsButtonPressed(button);
}
public void setPressed(int button)
{
plugin.PressAndRelease(button);
}
public void setPressed(int button, bool state)
{
plugin.PressAndRelease(button, state);
}
}
}