forked from MWisBest/StadiaToSCP
/
StadiaController.cs
269 lines (239 loc) · 7.76 KB
/
StadiaController.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
using System;
using System.Threading;
using HidLibrary;
using ScpDriverInterface;
namespace StadiaToSCP
{
public class StadiaController
{
public HidDevice Device { get; set; }
public int Index;
private Thread rThread, iThread, ssThread;
private ScpBus ScpBus;
private byte[] Vibration = { 0x05, 0x00, 0x00, 0x00, 0x00 };
private Mutex rumble_mutex = new Mutex();
AutoResetEvent rumbleWaitHandle = new AutoResetEvent( false );
private bool Running = true;
//private byte[] enableAccelerometer = { 0x31, 0x01, 0x08 };
public StadiaController(HidDevice device, ScpBus scpBus, int index)
{
Index = index;
ScpBus = scpBus;
Device = device;
Device.Write(Vibration);
rThread = new Thread(() => rumble_thread(Device));
// rThread.Priority = ThreadPriority.BelowNormal;
rThread.Start();
iThread = new Thread(() => input_thread(Device, scpBus, index));
iThread.Priority = ThreadPriority.Highest;
iThread.Start();
}
public bool check_connected()
{
return true;//Device.Write(Vibration);
}
public void unplug()
{
Running = false;
rThread.Join();
iThread.Join();
ScpBus.Unplug(Index);
Device.CloseDevice();
}
private void rumble_thread(HidDevice Device)
{
byte[] local_vibration = { 0x05, 0x00, 0x00, 0x00, 0x00 };
while (Running)
{
rumbleWaitHandle.WaitOne();
rumble_mutex.WaitOne();
local_vibration[4] = Vibration[3];
local_vibration[2] = Vibration[1];
rumble_mutex.ReleaseMutex();
local_vibration[3] = local_vibration[4];
local_vibration[1] = local_vibration[2];
Device.Write(local_vibration);
}
}
private void input_thread(HidDevice Device, ScpBus scpBus, int index)
{
scpBus.PlugIn(index);
X360Controller controller = new X360Controller();
int timeout = 10;
//long last_changed = 0;
//long last_mi_button = 0;
bool ss_button_pressed = false;
bool ss_button_held = false;
while (Running)
{
HidDeviceData data = Device.Read(timeout);
var currentState = data.Data;
//bool changed = false;
if (data.Status == HidDeviceData.ReadStatus.Success && currentState.Length >= 10 && currentState[0] == 3)
{
// NOTE: Console.WriteLine is blocking. If main thread sends a WriteLine while we do a WriteLine here, we're boned and will miss reports!
//Console.WriteLine(Program.ByteArrayToHexString(currentState));
X360Buttons Buttons = X360Buttons.None;
if ((currentState[3] & 64) != 0) Buttons |= X360Buttons.A;
if ((currentState[3] & 32) != 0) Buttons |= X360Buttons.B;
if ((currentState[3] & 16) != 0) Buttons |= X360Buttons.X;
if ((currentState[3] & 8) != 0) Buttons |= X360Buttons.Y;
if ((currentState[3] & 4) != 0) Buttons |= X360Buttons.LeftBumper;
if ((currentState[3] & 2) != 0) Buttons |= X360Buttons.RightBumper;
if ((currentState[3] & 1) != 0) Buttons |= X360Buttons.LeftStick;
if ((currentState[2] & 128) != 0) Buttons |= X360Buttons.RightStick;
ss_button_pressed = ( currentState[2] & 1 ) != 0;
// [2] & 2 == Assistant, [2] & 1 == Screenshot
switch (currentState[1])
{
default:
break;
case 0:
Buttons |= X360Buttons.Up;
break;
case 1:
Buttons |= X360Buttons.UpRight;
break;
case 2:
Buttons |= X360Buttons.Right;
break;
case 3:
Buttons |= X360Buttons.DownRight;
break;
case 4:
Buttons |= X360Buttons.Down;
break;
case 5:
Buttons |= X360Buttons.DownLeft;
break;
case 6:
Buttons |= X360Buttons.Left;
break;
case 7:
Buttons |= X360Buttons.UpLeft;
break;
}
if ((currentState[2] & 32) != 0) Buttons |= X360Buttons.Start;
if ((currentState[2] & 64) != 0) Buttons |= X360Buttons.Back;
if ((currentState[2] & 16) != 0)
{
//last_mi_button = (DateTime.Now.Ticks / TimeSpan.TicksPerMillisecond);
Buttons |= X360Buttons.Logo;
}
//if (controller.Buttons != Buttons)
{
//changed = true;
controller.Buttons = Buttons;
}
// Note: The HID reports do not allow stick values of 00.
// This seems to make sense: 0x80 is center, so usable values are:
// 0x01 to 0x7F and 0x81 to 0xFF.
// For our purposes I believe this is undesirable. Subtract 1 from negative
// values to allow maxing out the stick values.
// TODO: Get an Xbox controller and verify this is standard behavior.
for( int i = 4; i <= 7; ++i )
{
if( currentState[i] <= 0x7F && currentState[i] > 0x00 )
{
currentState[i] -= 0x01;
}
}
ushort LeftStickXunsigned = (ushort)( currentState[4] << 8 | ( currentState[4] << 1 & 255 ) );
if (LeftStickXunsigned == 0xFFFE)
LeftStickXunsigned = 0xFFFF;
short LeftStickX = (short)( LeftStickXunsigned - 0x8000 );
//if (LeftStickX != controller.LeftStickX)
{
// changed = true;
controller.LeftStickX = LeftStickX;
}
ushort LeftStickYunsigned = (ushort)( currentState[5] << 8 | ( currentState[5] << 1 & 255 ) );
if (LeftStickYunsigned == 0xFFFE)
LeftStickYunsigned = 0xFFFF;
short LeftStickY = (short)( -LeftStickYunsigned + 0x7FFF );
if (LeftStickY == -1)
LeftStickY = 0;
//if (LeftStickY != controller.LeftStickY)
{
// changed = true;
controller.LeftStickY = LeftStickY;
}
ushort RightStickXunsigned = (ushort)( currentState[6] << 8 | ( currentState[6] << 1 & 255 ) );
if (RightStickXunsigned == 0xFFFE)
RightStickXunsigned = 0xFFFF;
short RightStickX = (short)( RightStickXunsigned - 0x8000 );
//if (RightStickX != controller.RightStickX)
{
// changed = true;
controller.RightStickX = RightStickX;
}
ushort RightStickYunsigned = (ushort)( currentState[7] << 8 | ( currentState[7] << 1 & 255 ) );
if (RightStickYunsigned == 0xFFFE)
RightStickYunsigned = 0xFFFF;
short RightStickY = (short)( -RightStickYunsigned + 0x7FFF );
if (RightStickY == -1)
RightStickY = 0;
//if (RightStickY != controller.RightStickY)
{
// changed = true;
controller.RightStickY = RightStickY;
}
//if (controller.LeftTrigger != currentState[8])
{
// changed = true;
controller.LeftTrigger = currentState[8];
}
//if (controller.RightTrigger != currentState[9])
{
// changed = true;
controller.RightTrigger = currentState[9];
}
}
/*
if (data.Status == HidDeviceData.ReadStatus.WaitTimedOut || (!changed && ((last_changed + timeout) < (DateTime.Now.Ticks / TimeSpan.TicksPerMillisecond))))
{
changed = true;
}*/
//if (changed)
{
//Console.WriteLine("changed");
//Console.WriteLine((DateTime.Now.Ticks / TimeSpan.TicksPerMillisecond));
byte[] outputReport = new byte[8];
scpBus.Report(index, controller.GetReport(), outputReport);
if (outputReport[1] == 0x08)
{
byte bigMotor = outputReport[3];
byte smallMotor = outputReport[4];
if (smallMotor != Vibration[3] || Vibration[1] != bigMotor)
{
// We only need to take the mutex if we're modifying the data
rumble_mutex.WaitOne();
Vibration[1] = bigMotor;
Vibration[3] = smallMotor;
rumble_mutex.ReleaseMutex();
rumbleWaitHandle.Set();
}
}
//last_changed = DateTime.Now.Ticks / TimeSpan.TicksPerMillisecond;
}
if (ss_button_pressed && !ss_button_held)
{
ss_button_held = true;
try
{
// TODO: Allow configuring this keybind.
ssThread = new Thread( () => System.Windows.Forms.SendKeys.SendWait( "^+Z" ) );
ssThread.Start();
}
catch
{
}
}
else if (ss_button_held && !ss_button_pressed)
{
ss_button_held = false;
}
}
}
}
}