-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathForm1.cs
351 lines (318 loc) · 13.1 KB
/
Form1.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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Resources;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using Midi;
using vJoyInterfaceWrap;
using Control = System.Windows.Forms.Control;
namespace MidiFeeder
{
public partial class Form1 : Form
{
private bool _running = true;
private List<JoystickControl> _joystickControls = new List<JoystickControl>();
private int _numButtons;
private vJoy.JoystickState _state;
private Dictionary<HID_USAGES, long> _maxValues = new Dictionary<HID_USAGES, long>();
public Form1()
{
InitializeComponent();
this.FormClosing += Form1_FormClosing;
InitializeJoystick();
foreach (var installedDevice in InputDevice.InstalledDevices)
{
installedDevice.Open();
installedDevice.StartReceiving(null);
installedDevice.ControlChange += OnControlChange;
}
var t = new Thread(() =>
{
while (_running)
{
Program.Joystick.UpdateVJD(1, ref _state);
Thread.Sleep(10);
}
});
t.Start();
}
private void InitializeJoystick()
{
switch (Program.Joystick.GetVJDStatus(1))
{
case VjdStat.VJD_STAT_OWN:
MessageBox.Show("Virtual joystick already aquired. This shouldn't happen.");
break;
case VjdStat.VJD_STAT_FREE:
if (!Program.Joystick.AcquireVJD(1))
MessageBox.Show("Couldn't aquire joystick");
Program.Joystick.ResetVJD(1);
var values = Enum.GetValues(typeof(HID_USAGES));
foreach (HID_USAGES value in values)
{
long max = 0;
Program.Joystick.GetVJDAxisMax(1, value, ref max);
_maxValues.Add(value, max);
_joystickControls.Add(new JoystickControl
{
HidUsage = value
});
}
_numButtons = Program.Joystick.GetVJDButtonNumber(1);
for (int i = 0; i < _numButtons; i++)
{
_joystickControls.Add(new JoystickControl
{
IsButton = true,
ButtonNumber = i
});
}
break;
case VjdStat.VJD_STAT_BUSY:
MessageBox.Show("The virtual joystick is already receiving data from another application. Close the offending application or choose another virtual joystick.");
break;
case VjdStat.VJD_STAT_MISS:
MessageBox.Show("The vJoy driver doesn't seem to be installed. Make sure you download and install vJoy before running vJoy MIDI Mapper.");
break;
case VjdStat.VJD_STAT_UNKN:
MessageBox.Show("vJoy unknown generic joystick error. An unhandled error occured. Don't worry, make sure vJoy is running and the driver is properly installed.");
break;
default:
throw new ArgumentOutOfRangeException();
}
}
void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
_running = false;
foreach (var installedDevice in InputDevice.InstalledDevices)
{
if (installedDevice.IsOpen)
installedDevice.Close();
}
Program.Joystick.RelinquishVJD(1);
}
private void OnControlChange(ControlChangeMessage msg)
{
Invoke((Action<ControlChangeMessage>)delegate(ControlChangeMessage message)
{
Change(InputDevice.InstalledDevices.IndexOf((InputDevice)message.Device),
(int)message.Control, message.Value);
}, msg);
}
private int _currentDeviceId, _currentControlId;
private void Change(int deviceId, int controlOrPitch, int value)
{
// Redraw UI etc.
JoystickControl joystickControl = _joystickControls.SingleOrDefault(x => x.Mapping != null && (x.Mapping.DeviceId == deviceId && x.Mapping.ControlNumber == controlOrPitch));
if (_currentDeviceId != deviceId || _currentControlId != controlOrPitch)
{
_currentDeviceId = deviceId;
_currentControlId = controlOrPitch;
lblMidiDevice.Text = "MIDI Device: " + InputDevice.InstalledDevices[deviceId].Name;
lblMidiControlNumber.Text = "Control #: " + controlOrPitch;
if (joystickControl != null)
{
radioTypeButton.Checked = joystickControl.IsButton;
radioTypeAxis.Checked = !joystickControl.IsButton;
btnUnmap.Enabled = true;
}
else
{
radioTypeAxis.Checked = radioTypeButton.Checked = false;
btnUnmap.Enabled = false;
}
}
progessMidiData.Value = (value+1);
progessMidiData.Value = value;
if (joystickControl != null)
{
UpdateJoystick(joystickControl, value);
}
}
private void Form1_Resize(object sender, EventArgs e)
{
if (WindowState == FormWindowState.Minimized)
{
Visible = false;
}
}
private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
{
Visible = true;
WindowState = FormWindowState.Normal;
}
private void UpdateJoystick(JoystickControl joystickControl, int value)
{
if (joystickControl.IsButton == false)
{
var percentage = value / 127.0f;
long min = 0, max = 0;
var axisVal = (int)(percentage * _maxValues[joystickControl.HidUsage]);
switch (joystickControl.HidUsage)
{
case HID_USAGES.HID_USAGE_X:
_state.AxisX = axisVal;
break;
case HID_USAGES.HID_USAGE_Y:
_state.AxisY = axisVal;
break;
case HID_USAGES.HID_USAGE_Z:
_state.AxisZ = axisVal;
break;
case HID_USAGES.HID_USAGE_RX:
_state.AxisXRot = axisVal;
break;
case HID_USAGES.HID_USAGE_RY:
_state.AxisYRot = axisVal;
break;
case HID_USAGES.HID_USAGE_RZ:
_state.AxisZRot = axisVal;
break;
case HID_USAGES.HID_USAGE_SL0:
_state.Slider = axisVal;
break;
case HID_USAGES.HID_USAGE_SL1:
_state.Dial = axisVal;
break;
case HID_USAGES.HID_USAGE_WHL:
_state.Wheel = axisVal;
break;
default:
throw new ArgumentOutOfRangeException();
}
}
else
{
var pressed = value > 0;
switch (joystickControl.ButtonNumber / 32) { //Each button bank in vJoy supports 32 buttons, so divide the button number by 32 to figure out which bank it is
case 0: //Original bank, Buttons
if (pressed)
_state.Buttons |= (uint)(1 << (joystickControl.ButtonNumber % 32));
else
_state.Buttons &= ~(uint)(1 << (joystickControl.ButtonNumber % 32));
break;
case 1: //First extended bank, ButtonsEx1
if (pressed)
_state.ButtonsEx1 |= (uint)(1 << (joystickControl.ButtonNumber % 32));
else
_state.ButtonsEx1 &= ~(uint)(1 << (joystickControl.ButtonNumber % 32));
break;
case 2: //Second extended bank, ButtonsEx2
if (pressed)
_state.ButtonsEx2 |= (uint)(1 << (joystickControl.ButtonNumber % 32));
else
_state.ButtonsEx2 &= ~(uint)(1 << (joystickControl.ButtonNumber % 32));
break;
case 3: //Third extended bank, ButtonsEx3
if (pressed)
_state.ButtonsEx3 |= (uint)(1 << (joystickControl.ButtonNumber % 32));
else
_state.ButtonsEx3 &= ~(uint)(1 << (joystickControl.ButtonNumber % 32));
break;
default:
throw new ArgumentOutOfRangeException();
}
}
}
private JoystickControl GetCurrentMapping()
{
var currentMapping = _joystickControls.SingleOrDefault(x => x.Mapping != null && (x.Mapping.DeviceId == _currentDeviceId && x.Mapping.ControlNumber == _currentControlId));
return currentMapping;
}
private void Map(bool button)
{
var currentMapping = GetCurrentMapping();
if (currentMapping == null || currentMapping.IsButton == !button)
{
if (currentMapping != null)
currentMapping.Mapping = null;
var unmapped = _joystickControls.FirstOrDefault(x => x.IsButton == button && x.Mapping == null);
if (unmapped == null)
{
string errorString = "There are no unmapped axes left. Unmap another axis in order to map this control.";
if (button)
errorString = "There are no unmapped buttons left. Unmap another button in order to map this control.";
MessageBox.Show(errorString);
}
else
{
unmapped.Mapping = new Mapping
{
ControlNumber = _currentControlId,
DeviceId = _currentDeviceId
};
btnUnmap.Enabled = true;
}
}
}
private void radioTypeButton_CheckedChanged(object sender, EventArgs e)
{
if (!radioTypeButton.Checked)
return;
Map(true);
}
private void btnUnmap_Click(object sender, EventArgs e)
{
var currentMapping = GetCurrentMapping();
if (currentMapping == null)
return;
currentMapping.Mapping = null;
radioTypeAxis.Checked = radioTypeButton.Checked = false;
btnUnmap.Enabled = false;
}
private void radioTypeAxis_CheckedChanged(object sender, EventArgs e)
{
if (!radioTypeAxis.Checked)
return;
Map(false);
}
}
public class JoystickControl
{
public bool IsButton { get; set; }
public int ButtonNumber { get; set; }
public HID_USAGES HidUsage { get; set; }
public Mapping Mapping { get; set; }
public override string ToString()
{
if (IsButton)
return "Button " + (ButtonNumber + 1);
switch (HidUsage)
{
case HID_USAGES.HID_USAGE_X:
return "X Axis";
case HID_USAGES.HID_USAGE_Y:
return "Y Axis";
case HID_USAGES.HID_USAGE_Z:
return "Z Axis";
case HID_USAGES.HID_USAGE_RX:
return "X Axis Rotation";
case HID_USAGES.HID_USAGE_RY:
return "Y Axis Rotation";
case HID_USAGES.HID_USAGE_RZ:
return "Z Axis Rotation";
case HID_USAGES.HID_USAGE_SL0:
return "Slider 1";
case HID_USAGES.HID_USAGE_SL1:
return "Slider 2";
case HID_USAGES.HID_USAGE_WHL:
return "Wheel";
case HID_USAGES.HID_USAGE_POV:
return "POV Hat";
default:
throw new ArgumentOutOfRangeException();
}
}
}
public class Mapping
{
public int DeviceId { get; set; }
public int ControlNumber { get; set; }
}
}