-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClass1.cs
More file actions
287 lines (237 loc) · 11.2 KB
/
Class1.cs
File metadata and controls
287 lines (237 loc) · 11.2 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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ODIF;
using ODIF.Extensions;
using ODIF.Localization;
using System.Windows.Media;
using System.Windows.Controls;
using System.Windows.Media.Imaging;
using System.Collections.ObjectModel;
using System.Threading;
using System.Windows;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Diagnostics;
namespace SC360
{
public static class SC360Globals
{
public static X360Device scBus;
public static SC360_Plugin PluginRef;
}
[PluginInfo(
PluginName = "Scarlet Crush 360 Virtual Bus",
PluginDescription = "Creates a virtual bus for 360 controllers.",
PluginID = 39,
PluginAuthorName = "Scarlet Crush",
PluginAuthorEmail = "a",
PluginAuthorURL = "a",
PluginIconPath = @"pack://application:,,,/SC360;component/Resources/ScarletCrush.ico"
)]
public class SC360_Plugin : OutputDevicePlugin , pluginSettings, DynamicDeviceList
{
//public AsyncObservableCollection<OutputDevice> Devices { get; set; }
public SettingGroup settings { get; set; }
public SC360_Plugin()
{
settings = new SettingGroup("General Settings","");
Setting lowLatencyMode = new Setting("Low latency mode", "Extremely low latency and low system overhead, but does not support rumble.", SettingControl.Checkbox, SettingType.Bool, false);
lowLatencyMode.descriptionVisibility = DescriptionVisibility.SubText;
settings.settings.Add(lowLatencyMode);
settings.loadSettings();
ApplicationMenuItem mymenu = new ApplicationMenuItem("XInput Test");
mymenu.Clicked += (s, e) => { Process.Start(Global.PluginsDirectory +"\\sc360\\XInputTest.exe"); };
Global.MainMenu.FirstOrDefault(m => m.Text == "Tools").Items.Add(mymenu);
SC360Globals.scBus = new X360Device();
SC360Globals.PluginRef = this;
SC360Globals.scBus.Start();
}
public void RemoveDevice(Device device)
{
lock (Devices)
{
if (Devices.Count > 0)
{
device.Dispose();
Devices.Remove(device as ODIF.OutputDevice);
}
}
}
public void AddDevice()
{
lock (Devices)
{
Devices.Add(new SC360_Device_Plugin(Devices.Count + 1, settings));
}
}
protected override void Dispose(bool disposing)
{
settings.saveSettings();
foreach (SC360_Device_Plugin device in Devices)
{
device.Dispose();
}
SC360Globals.scBus.UnplugAll();
SC360Globals.scBus.Stop();
base.Dispose(disposing);
}
}
internal class x360device
{
public InputChannelTypes.JoyAxis LSx { get; set; }
public InputChannelTypes.JoyAxis LSy { get; set; }
public InputChannelTypes.JoyAxis RSx { get; set; }
public InputChannelTypes.JoyAxis RSy { get; set; }
public InputChannelTypes.Button LS { get; set; }
public InputChannelTypes.Button RS { get; set; }
public InputChannelTypes.JoyAxis LT { get; set; }
public InputChannelTypes.JoyAxis RT { get; set; }
public InputChannelTypes.Button LB { get; set; }
public InputChannelTypes.Button RB { get; set; }
public InputChannelTypes.Button DUp { get; set; }
public InputChannelTypes.Button DDown { get; set; }
public InputChannelTypes.Button DLeft { get; set; }
public InputChannelTypes.Button DRight { get; set; }
public InputChannelTypes.Button A { get; set; }
public InputChannelTypes.Button B { get; set; }
public InputChannelTypes.Button X { get; set; }
public InputChannelTypes.Button Y { get; set; }
public InputChannelTypes.Button Start { get; set; }
public InputChannelTypes.Button Back { get; set; }
public InputChannelTypes.Button Guide { get; set; }
public OutputChannelTypes.RumbleMotor BigRumble { get; set; }
public OutputChannelTypes.RumbleMotor SmallRumble { get; set; }
public x360device()
{
LSx = new InputChannelTypes.JoyAxis("Left Stick X","",Properties.Resources._24_360_Left_Stick.ToImageSource());
LSy = new InputChannelTypes.JoyAxis("Left Stick Y","", Properties.Resources._24_360_Left_Stick.ToImageSource());
RSx = new InputChannelTypes.JoyAxis("Right Stick X","", Properties.Resources._24_360_Right_Stick.ToImageSource());
RSy = new InputChannelTypes.JoyAxis("Right Stick Y","", Properties.Resources._24_360_Right_Stick.ToImageSource());
LS = new InputChannelTypes.Button("Left Stick","", Properties.Resources._24_360_Left_Stick.ToImageSource());
RS = new InputChannelTypes.Button("Right Stick","", Properties.Resources._24_360_Right_Stick.ToImageSource());
LT = new InputChannelTypes.JoyAxis("Left Trigger","", Properties.Resources._24_360_LT.ToImageSource()) { min_Value = 0 };
RT = new InputChannelTypes.JoyAxis("Right Trigger","", Properties.Resources._24_360_RT.ToImageSource()) { min_Value = 0 };
LB = new InputChannelTypes.Button("Left Bumper","", Properties.Resources._24_360_LB.ToImageSource());
RB = new InputChannelTypes.Button("Right Bumper","", Properties.Resources._24_360_RB.ToImageSource());
DUp = new InputChannelTypes.Button("DPad Up","", Properties.Resources._24_360_Dpad_Up.ToImageSource());
DDown = new InputChannelTypes.Button("DPad Down","", Properties.Resources._24_360_Dpad_Down.ToImageSource());
DLeft = new InputChannelTypes.Button("DPad Left","", Properties.Resources._24_360_Dpad_Left.ToImageSource());
DRight = new InputChannelTypes.Button("DPad Right","", Properties.Resources._24_360_Dpad_Right.ToImageSource());
A = new InputChannelTypes.Button("A","", Properties.Resources._24_360_A.ToImageSource());
B = new InputChannelTypes.Button("B","", Properties.Resources._24_360_B.ToImageSource());
X = new InputChannelTypes.Button("X","", Properties.Resources._24_360_X.ToImageSource());
Y = new InputChannelTypes.Button("Y","", Properties.Resources._24_360_Y.ToImageSource());
Start = new InputChannelTypes.Button("Start","", Properties.Resources._24_360_Start.ToImageSource());
Back = new InputChannelTypes.Button("Back","", Properties.Resources._24_360_Back.ToImageSource());
Guide = new InputChannelTypes.Button("Guide","", Properties.Resources._24_360_Guide.ToImageSource());
BigRumble = new OutputChannelTypes.RumbleMotor("Big Rumble", "");
SmallRumble = new OutputChannelTypes.RumbleMotor("Small Rumble", "");
}
}
public class SC360_Device_Plugin : OutputDevice
{
public System.Windows.Controls.Page MappingPage()
{
return new System.Windows.Controls.Page();
}
private SettingGroup settings { get; set; }
public ConnectionTypes DeviceConnectionType { get { return ConnectionTypes.Virtual; } }
private Thread ReportingThread;
private int ControllerID;
public SC360_Device_Plugin(int device, SettingGroup settings)
{
ControllerID = device;
this.settings = settings;
this.StatusIcon = new BitmapImage(new Uri(@"pack://application:,,,/SC360;component/Resources/360.ico"));
this.DeviceName = "Controller " + ControllerID;
x360device myDevice = new x360device();
InputChannels.Add(myDevice.LSx);
InputChannels.Add(myDevice.LSy);
InputChannels.Add(myDevice.RSx);
InputChannels.Add(myDevice.RSy);
InputChannels.Add(myDevice.LS);
InputChannels.Add(myDevice.RS);
InputChannels.Add(myDevice.LT);
InputChannels.Add(myDevice.RT);
InputChannels.Add(myDevice.LB);
InputChannels.Add(myDevice.RB);
InputChannels.Add(myDevice.DUp);
InputChannels.Add(myDevice.DDown);
InputChannels.Add(myDevice.DLeft);
InputChannels.Add(myDevice.DRight);
InputChannels.Add(myDevice.A);
InputChannels.Add(myDevice.B);
InputChannels.Add(myDevice.X);
InputChannels.Add(myDevice.Y);
InputChannels.Add(myDevice.Start);
InputChannels.Add(myDevice.Back);
InputChannels.Add(myDevice.Guide);
OutputChannels.Add(myDevice.SmallRumble);
OutputChannels.Add(myDevice.BigRumble);
if (SC360Globals.scBus.Open() && SC360Globals.scBus.Start())
{
SC360Globals.scBus.Plugin(device);
if (settings.getSetting("Low latency mode"))
{
byte[] Report = new byte[28];
byte[] Rumble = new byte[8];
foreach (DeviceChannel channel in InputChannels)
{
channel.PropertyChanged += (s, e) => {
SC360Globals.scBus.Parse(myDevice, Report, ControllerID);
if (SC360Globals.scBus.Report(Report, Rumble))
{
// if (Rumble[1] == 0x08)
// {
// myDevice.SmallRumble.Value = Rumble[3];
// myDevice.BigRumble.Value = Rumble[4];
// }
}
};
}
} else
{
ReportingThread = new Thread(() => ReportingThreadLoop(myDevice));
ReportingThread.Start();
}
}
}
[DllImport("msvcrt.dll", CallingConvention = CallingConvention.Cdecl)]
static extern int memcmp(byte[] b1, byte[] b2, IntPtr count);
private void ReportingThreadLoop(x360device device)
{
byte[] Report = new byte[28];
byte[] Rumble = new byte[8];
while (!Global.IsShuttingDown)
{
Thread.Sleep(1);
SC360Globals.scBus.Parse(device, Report, ControllerID);
if (SC360Globals.scBus.Report(Report, Rumble))
{
if (Rumble[1] == 0x08)
{
device.SmallRumble.Value = Rumble[3]/255d;
device.BigRumble.Value = Rumble[4]/255d;
}
}
}
}
public SC360_Device_Plugin()
{
}
public bool StartDevice()
{
return true;
}
protected override void Dispose(bool disposing)
{
if (ReportingThread != null && ReportingThread.IsAlive)
ReportingThread.Abort();
SC360Globals.scBus.Unplug(ControllerID);
base.Dispose(disposing);
}
}
}