-
Notifications
You must be signed in to change notification settings - Fork 145
/
JoystickPlugin.cs
173 lines (143 loc) · 4.63 KB
/
JoystickPlugin.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
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using FreePIE.Core.Contracts;
using FreePIE.Core.Plugins.Globals;
using FreePIE.Core.Plugins.Strategies;
using SlimDX.DirectInput;
namespace FreePIE.Core.Plugins
{
[GlobalType(Type = typeof(JoystickGlobal), IsIndexed = true)]
public class JoystickPlugin : Plugin
{
private List<Device> devices;
public override object CreateGlobal()
{
var directInput = new DirectInput();
var handle = Process.GetCurrentProcess().MainWindowHandle;
devices = new List<Device>();
var diDevices = directInput.GetDevices(DeviceClass.GameController, DeviceEnumerationFlags.AttachedOnly);
var creator = new Func<DeviceInstance, JoystickGlobal>(d =>
{
var controller = new Joystick(directInput, d.InstanceGuid);
controller.SetCooperativeLevel(handle, CooperativeLevel.Exclusive | CooperativeLevel.Background);
controller.Acquire();
var device = new Device(controller);
devices.Add(device);
return new JoystickGlobal(device);
});
return new GlobalIndexer<JoystickGlobal, int, string>(intIndex => creator(diDevices[intIndex]),(strIndex, idx) =>
{
var d = diDevices.Where(di => di.InstanceName == strIndex).ToArray();
return d.Length > 0 && d.Length > idx ? creator(d[idx]) : null;
});
}
public override void Stop()
{
devices.ForEach(d => d.Dispose());
}
public override void DoBeforeNextExecute()
{
devices.ForEach(d => d.Reset());
}
public override string FriendlyName
{
get { return "Joystick"; }
}
}
public class Device : IDisposable
{
private readonly Joystick joystick;
private JoystickState state;
private readonly GetPressedStrategy<int> getPressedStrategy;
public Device(Joystick joystick)
{
this.joystick = joystick;
SetRange(-1000, 1000);
getPressedStrategy = new GetPressedStrategy<int>(GetDown);
}
public void Dispose()
{
joystick.Dispose();
}
public JoystickState State
{
get { return state ?? (state = joystick.GetCurrentState()); }
}
public void Reset()
{
state = null;
}
public void SetRange(int lowerRange, int upperRange)
{
foreach (DeviceObjectInstance deviceObject in joystick.GetObjects())
{
if ((deviceObject.ObjectType & ObjectDeviceType.Axis) != 0)
joystick.GetObjectPropertiesById((int)deviceObject.ObjectType).SetRange(lowerRange, upperRange);
}
}
public bool GetPressed(int button)
{
return getPressedStrategy.IsPressed(button);
}
public bool GetDown(int button)
{
return State.IsPressed(button);
}
}
[Global(Name = "joystick")]
public class JoystickGlobal
{
private readonly Device device;
public JoystickGlobal(Device device)
{
this.device = device;
}
private JoystickState State { get { return device.State; } }
public void setRange(int lowerRange, int upperRange)
{
device.SetRange(lowerRange, upperRange);
}
public bool getPressed(int button)
{
return device.GetPressed(button);
}
public bool getDown(int button)
{
return device.GetDown(button);
}
public int x
{
get { return State.X; }
}
public int y
{
get { return State.Y; }
}
public int z
{
get { return State.Z; }
}
public int xRotation
{
get { return State.RotationX; }
}
public int yRotation
{
get { return State.RotationY; }
}
public int zRotation
{
get { return State.RotationZ; }
}
public int[] sliders
{
get { return State.GetSliders(); }
}
public int[] pov
{
get { return State.GetPointOfViewControllers(); }
}
}
}