-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathMouseToJoystickHandler.cs
236 lines (192 loc) · 7.93 KB
/
MouseToJoystickHandler.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
using Gma.System.MouseKeyHook;
using System;
using System.Windows.Forms;
using vJoyInterfaceWrap;
namespace MouseToJoystick2
{
class MouseToJoystickHandler : IDisposable
{
readonly int invertX;
readonly int invertY;
readonly bool autoCenter;
readonly bool autoSize;
readonly int manualWidth;
readonly int manualHeight;
// MouseKeyHook stuff
private IKeyboardMouseEvents mouseEventHooker = null;
private int lastX;
private int lastY;
// vJoy stuff
private vJoy joystick = null;
private readonly uint id;
private readonly long AXIS_MAX;
private readonly long AXIS_MIN;
private readonly long AXIS_MID;
private const uint VJOY_BTN_1 = 1;
private const uint VJOY_BTN_2 = 2;
private const uint VJOY_BTN_3 = 3;
public MouseToJoystickHandler(uint vjoyDevId, bool invertX, bool invertY, bool autoCenter, bool autoSize, int manualWidth, int manualHeight)
{
this.id = vjoyDevId;
this.invertX = invertX ? -1 : 1;
this.invertY = invertY ? -1 : 1;
this.autoCenter = autoCenter;
this.autoSize = autoSize;
this.manualWidth = manualWidth;
this.manualHeight = manualHeight;
joystick = new vJoy();
// Make sure driver is enabled
if (!joystick.vJoyEnabled())
{
throw new InvalidOperationException("vJoy driver not enabled: Failed Getting vJoy attributes");
}
// Make sure we can get the joystick
VjdStat status = joystick.GetVJDStatus(id);
switch (status)
{
case VjdStat.VJD_STAT_OWN:
case VjdStat.VJD_STAT_FREE:
break;
case VjdStat.VJD_STAT_BUSY:
throw new InvalidOperationException("vJoy device is already owned by another feeder");
case VjdStat.VJD_STAT_MISS:
throw new InvalidOperationException("vJoy device is not installed or is disabled");
default:
throw new Exception("vJoy device general error");
};
if (!this.joystick.AcquireVJD(this.id))
{
throw new Exception("Failed to acquire vJoy device");
}
if (!this.joystick.ResetVJD(this.id))
{
throw new Exception("Failed to reset vJoy device");
}
if (!this.joystick.GetVJDAxisMax(this.id, HID_USAGES.HID_USAGE_X, ref this.AXIS_MAX))
{
throw new Exception("Failed to get vJoy axis max");
}
if (!this.joystick.GetVJDAxisMin(this.id, HID_USAGES.HID_USAGE_X, ref this.AXIS_MIN))
{
throw new Exception("Failed to get vJoy axis min");
}
this.AXIS_MID = AXIS_MAX - (AXIS_MAX - AXIS_MIN) / 2;
// Register for mouse events
mouseEventHooker = Hook.GlobalEvents();
mouseEventHooker.MouseMove += HandleMouseMove;
mouseEventHooker.MouseDown += HandleMouseDown;
mouseEventHooker.MouseUp += HandleMouseUp;
}
private void HandleMouseDown(object sender, MouseEventArgs e)
{
uint btnId;
switch (e.Button)
{
case MouseButtons.Left:
btnId = VJOY_BTN_1;
break;
case MouseButtons.Right:
btnId = VJOY_BTN_2;
break;
case MouseButtons.Middle:
btnId = VJOY_BTN_3;
break;
default:
return;
}
this.joystick.SetBtn(true, this.id, btnId);
}
private void HandleMouseUp(object sender, MouseEventArgs e)
{
uint btnId;
switch (e.Button)
{
case MouseButtons.Left:
btnId = VJOY_BTN_1;
break;
case MouseButtons.Right:
btnId = VJOY_BTN_2;
break;
case MouseButtons.Middle:
btnId = VJOY_BTN_3;
break;
default:
return;
}
this.joystick.SetBtn(false, this.id, btnId);
}
private void HandleMouseMoveFirst(object sender, MouseEventArgs e)
{
this.lastX = e.X;
this.lastY = e.Y;
mouseEventHooker.MouseMove -= HandleMouseMoveFirst;
mouseEventHooker.MouseMove += HandleMouseMove;
}
private void HandleMouseMove(object sender, MouseEventArgs e)
{
var bounds = Screen.PrimaryScreen.Bounds;
var minX = bounds.Left;
var maxX = this.autoSize ? bounds.Right : (bounds.Left + this.manualWidth);
var minY = bounds.Top;
var maxY = this.autoSize ? bounds.Bottom : (bounds.Top + this.manualHeight);
int deltaX = e.X - this.lastX;
int deltaY = e.Y - this.lastY;
this.lastX = this.autoCenter ? Clamp<int>(minX, e.X, maxX) : (minX + (maxX - minX) / 2);
this.lastY = this.autoCenter ? Clamp<int>(minY, e.Y, maxY) : (minY + (maxY - minY) / 2);
int xOut, yOut;
if (this.autoCenter)
{
xOut = Clamp<int>(Convert.ToInt32(AXIS_MIN), (int)Math.Round(AXIS_MID + invertX * (deltaX * (deltaX * -1.0 / 1.1 + 500))), Convert.ToInt32(AXIS_MAX));
yOut = Clamp<int>(Convert.ToInt32(AXIS_MIN), (int)Math.Round(AXIS_MID + invertY * (deltaY * (deltaY * -1.0 / 1.1 + 500))), Convert.ToInt32(AXIS_MAX));
}
else
{
int maxDeltaX = this.autoSize ? bounds.Width : this.manualWidth;
int maxDeltaY = this.autoSize ? bounds.Height : this.manualHeight;
long outputPerDeltaX = (AXIS_MAX - AXIS_MIN) / maxDeltaX;
long outputPerDeltaY = (AXIS_MAX - AXIS_MIN) / maxDeltaY;
xOut = Clamp<int>(Convert.ToInt32(AXIS_MIN), (int)(AXIS_MID + invertX * deltaX * outputPerDeltaX), Convert.ToInt32(AXIS_MAX));
yOut = Clamp<int>(Convert.ToInt32(AXIS_MIN), (int)(AXIS_MID + invertY * deltaY * outputPerDeltaY), Convert.ToInt32(AXIS_MAX));
}
//Console.WriteLine(String.Format("{0}, {1} -> {2}, {3}", lastX, lastY, xOut, yOut));
joystick.SetAxis(xOut, this.id, HID_USAGES.HID_USAGE_X);
joystick.SetAxis(yOut, this.id, HID_USAGES.HID_USAGE_Y);
}
private static T Clamp<T>(T min, T val, T max) where T : IComparable<T>
{
if (val.CompareTo(min) < 0) return min;
else if (val.CompareTo(max) > 0) return max;
else return val;
}
#region IDisposable Support
private bool disposedValue = false; // To detect redundant calls
protected virtual void Dispose(bool disposing)
{
if (!disposedValue)
{
if (disposing)
{
if (this.mouseEventHooker != null)
{
this.mouseEventHooker.Dispose();
this.mouseEventHooker = null;
}
// dispose managed state (managed objects).
if (this.joystick != null)
{
this.joystick.RelinquishVJD(this.id);
this.joystick = null;
}
}
disposedValue = true;
}
}
// This code added to correctly implement the disposable pattern.
public void Dispose()
{
// Do not change this code. Put cleanup code in Dispose(bool disposing) above.
Dispose(true);
}
#endregion
}
}