-
Notifications
You must be signed in to change notification settings - Fork 340
/
Program.cs
426 lines (347 loc) · 16.1 KB
/
Program.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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Numerics;
using System.Threading;
using System.Runtime.InteropServices;
using System.Timers;
using System.Net.NetworkInformation;
using System.Diagnostics;
using static BetterJoyForCemu.HIDapi;
using Nefarius.ViGEm.Client;
using Nefarius.ViGEm.Client.Targets;
using System.Net;
using System.Configuration;
using System.Net.Http;
using System.IO;
using System.Windows.Forms;
using System.ServiceProcess;
namespace BetterJoyForCemu {
public class JoyconManager {
public bool EnableIMU = true;
public bool EnableLocalize = false;
private const ushort vendor_id = 0x57e;
private const ushort vendor_id_ = 0x057e;
private const ushort product_l = 0x2006;
private const ushort product_r = 0x2007;
private const ushort product_pro = 0x2009;
public List<Joycon> j; // Array of all connected Joy-Cons
static JoyconManager instance;
public MainForm form;
System.Timers.Timer controllerCheck;
bool useHIDG = Boolean.Parse(ConfigurationManager.AppSettings["UseHIDG"]);
public static JoyconManager Instance {
get { return instance; }
}
public void Awake() {
instance = this;
j = new List<Joycon>();
HIDapi.hid_init();
}
public void Start() {
controllerCheck = new System.Timers.Timer(2000); // check every 2 seconds
controllerCheck.Elapsed += CheckForNewControllersTime;
controllerCheck.Start();
}
bool ControllerAlreadyAdded(string path) {
foreach (Joycon v in j)
if (v.path == path)
return true;
return false;
}
void CleanUp() { // removes dropped controllers from list
List<Joycon> rem = new List<Joycon>();
for (int i = 0; i < j.Count; i++) {
Joycon v = j[i];
if (v.state == Joycon.state_.DROPPED) {
if (v.other != null)
v.other.other = null; // The other of the other is the joycon itself
v.Detach(); rem.Add(v);
foreach (Button b in form.con) {
if (b.Enabled & b.Tag == v) {
b.Invoke(new MethodInvoker(delegate {
b.Enabled = false;
b.BackgroundImage = Properties.Resources.cross;
}));
break;
}
}
form.AppendTextBox("Removed dropped controller to list. Can be reconnected.\r\n");
}
}
foreach (Joycon v in rem)
j.Remove(v);
}
void CheckForNewControllersTime(Object source, ElapsedEventArgs e) {
if (Config.Value("ProgressiveScan")) {
CheckForNewControllers();
}
}
public void CheckForNewControllers() {
CleanUp();
// move all code for initializing devices here and well as the initial code from Start()
bool isLeft = false;
IntPtr ptr = HIDapi.hid_enumerate(vendor_id, 0x0);
IntPtr top_ptr = ptr;
hid_device_info enumerate; // Add device to list
bool foundNew = false;
while (ptr != IntPtr.Zero) {
enumerate = (hid_device_info)Marshal.PtrToStructure(ptr, typeof(hid_device_info));
if ((enumerate.product_id == product_l || enumerate.product_id == product_r || enumerate.product_id == product_pro) && !ControllerAlreadyAdded(enumerate.path)) {
switch (enumerate.product_id) {
case product_l:
isLeft = true;
form.AppendTextBox("Left Joy-Con connected.\r\n"); break;
case product_r:
isLeft = false;
form.AppendTextBox("Right Joy-Con connected.\r\n"); break;
case product_pro:
isLeft = true;
form.AppendTextBox("Pro controller connected.\r\n"); break;
default:
form.AppendTextBox("Non Joy-Con Nintendo input device skipped.\r\n"); break;
}
// Add controller to block-list for HidGuardian
if (useHIDG) {
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(@"http://localhost:26762/api/v1/hidguardian/affected/add/");
string postData = @"hwids=HID\" + enumerate.path.Split('#')[1].ToUpper();
var data = Encoding.UTF8.GetBytes(postData);
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded; charset=UTF-8";
request.ContentLength = data.Length;
using (var stream = request.GetRequestStream())
stream.Write(data, 0, data.Length);
try {
var response = (HttpWebResponse)request.GetResponse();
var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();
} catch {
form.AppendTextBox("Unable to add controller to block-list.\r\n");
}
} else { // Remove affected devices from list
try {
HttpWebResponse r1 = (HttpWebResponse)WebRequest.Create(@"http://localhost:26762/api/v1/hidguardian/affected/purge/").GetResponse();
} catch { }
}
// -------------------- //
IntPtr handle = HIDapi.hid_open_path(enumerate.path);
try {
HIDapi.hid_set_nonblocking(handle, 1);
} catch {
form.AppendTextBox("Unable to open path to device - are you using the correct (64 vs 32-bit) version for your PC?\r\n");
break;
}
j.Add(new Joycon(handle, EnableIMU, EnableLocalize & EnableIMU, 0.05f, isLeft, enumerate.path, j.Count, enumerate.product_id == product_pro, enumerate.serial_number == "000000000001"));
foundNew = true;
j.Last().form = form;
if (j.Count < 5) {
int ii = -1;
foreach (Button v in form.con) {
ii++;
if (!v.Enabled) {
System.Drawing.Bitmap temp;
switch (enumerate.product_id) {
case (product_l):
temp = Properties.Resources.jc_left_s; break;
case (product_r):
temp = Properties.Resources.jc_right_s; break;
case (product_pro):
temp = Properties.Resources.pro; break;
default:
temp = Properties.Resources.cross; break;
}
v.Invoke(new MethodInvoker(delegate {
v.Tag = j.Last(); // assign controller to button
v.Enabled = true;
v.Click += new EventHandler(form.conBtnClick);
v.BackgroundImage = temp;
}));
form.loc[ii].Invoke(new MethodInvoker(delegate {
form.loc[ii].Tag = v;
form.loc[ii].Click += new EventHandler(form.locBtnClick);
}));
break;
}
}
}
byte[] mac = new byte[6];
for (int n = 0; n < 6; n++)
mac[n] = byte.Parse(enumerate.serial_number.Substring(n * 2, 2), System.Globalization.NumberStyles.HexNumber);
j[j.Count - 1].PadMacAddress = new PhysicalAddress(mac);
}
ptr = enumerate.next;
}
int found = 0;
int minPadID = 10;
foreach (Joycon v in j) { // current system is designed for a maximum of two joycons connected to the PC
if (!v.isPro) {
found++;
minPadID = Math.Min(v.PadId, minPadID);
}
v.LED = (byte)(0x1 << v.PadId);
}
if (found == 2 && foundNew) {
form.AppendTextBox("Both joycons successfully found.\r\n");
Joycon temp = null;
foreach (Joycon v in j) {
if (!v.isPro) {
v.LED = (byte)(0x1 << minPadID);
if (temp == null)
temp = v;
else {
temp.other = v;
v.other = temp;
temp.xin.Dispose();
temp.xin = null;
}
foreach (Button b in form.con) {
if (b.Tag == v) {
if (v.isLeft)
b.BackgroundImage = Properties.Resources.jc_left;
else
b.BackgroundImage = Properties.Resources.jc_right;
}
}
}
} // Join up the two joycons
}
HIDapi.hid_free_enumeration(top_ptr);
foreach (Joycon jc in j) { // Connect device straight away
if (jc.state == Joycon.state_.NOT_ATTACHED) {
if (jc.xin != null)
jc.xin.Connect();
jc.Attach(leds_: jc.LED);
jc.Begin();
}
}
}
public void Update() {
for (int i = 0; i < j.Count; ++i)
j[i].Update();
}
public void OnApplicationQuit() {
foreach (Joycon v in j) {
v.Detach();
if (v.xin != null) {
v.xin.Disconnect();
v.xin.Dispose();
}
}
controllerCheck.Stop();
HIDapi.hid_exit();
}
}
// Custom timer class because system timers have a limit of 15.6ms
class HighResTimer {
double interval = 0;
double frequency = 0;
Thread thread;
public delegate void ActionDelegate();
ActionDelegate func;
bool run = false;
public HighResTimer(double f, ActionDelegate a) {
frequency = f;
interval = 1.0 / f;
func = a;
}
public void Start() {
run = true;
thread = new Thread(new ThreadStart(Run));
thread.IsBackground = true;
thread.Start();
}
void Run() {
while (run) {
func();
int timeToSleep = (int)(interval * 1000);
Thread.Sleep(timeToSleep);
}
}
public void Stop() {
run = false;
}
}
class Program {
public static PhysicalAddress btMAC = new PhysicalAddress(new byte[] { 0, 0, 0, 0, 0, 0 });
public static UdpServer server;
static double pollsPerSecond = 120.0;
public static ViGEmClient emClient;
private static readonly HttpClient client = new HttpClient();
public static JoyconManager mgr;
static HighResTimer timer;
static string pid;
static MainForm form;
static bool useHIDG = Boolean.Parse(ConfigurationManager.AppSettings["UseHIDG"]);
public static void Start() {
pid = Process.GetCurrentProcess().Id.ToString(); // get current process id for HidCerberus.Srv
if (useHIDG) {
try {
var HidCerberusService = new ServiceController("HidCerberus Service");
if (HidCerberusService.Status == ServiceControllerStatus.Stopped) {
form.console.Text += "HidGuardian was stopped. Starting...\r\n";
try {
HidCerberusService.Start();
} catch (Exception e) {
form.console.Text += "Unable to start HidGuardian - everything should work fine without it, but if you need it, run the app again as an admin.\r\n";
}
}
} catch (Exception e) {
form.console.Text += "Unable to start HidGuardian - everything should work fine without it, but if you need it, install it properly as admin.\r\n";
}
HttpWebResponse response;
if (Boolean.Parse(ConfigurationManager.AppSettings["PurgeWhitelist"])) {
try {
response = (HttpWebResponse)WebRequest.Create(@"http://localhost:26762/api/v1/hidguardian/whitelist/purge/").GetResponse(); // remove all programs allowed to see controller
} catch (Exception e) {
form.console.Text += "Unable to purge whitelist.\r\n";
}
}
try {
response = (HttpWebResponse)WebRequest.Create(@"http://localhost:26762/api/v1/hidguardian/whitelist/add/" + pid).GetResponse(); // add BetterJoyForCemu to allowed processes
} catch (Exception e) {
form.console.Text += "Unable to add program to whitelist.\r\n";
}
} else {
form.console.Text += "HidGuardian is disabled.\r\n";
}
emClient = new ViGEmClient(); // Manages emulated XInput
foreach (NetworkInterface nic in NetworkInterface.GetAllNetworkInterfaces()) {
// Get local BT host MAC
if (nic.NetworkInterfaceType != NetworkInterfaceType.FastEthernetFx && nic.NetworkInterfaceType != NetworkInterfaceType.Wireless80211) {
if (nic.Name.Split()[0] == "Bluetooth") {
btMAC = nic.GetPhysicalAddress();
}
}
}
mgr = new JoyconManager();
mgr.form = form;
mgr.Awake();
mgr.CheckForNewControllers();
mgr.Start();
server = new UdpServer(mgr.j);
server.form = form;
server.Start(IPAddress.Parse(ConfigurationManager.AppSettings["IP"]), Int32.Parse(ConfigurationManager.AppSettings["Port"]));
timer = new HighResTimer(pollsPerSecond, new HighResTimer.ActionDelegate(mgr.Update));
timer.Start();
form.console.Text += "All systems go\r\n";
}
public static void Stop() {
try {
HttpWebResponse response = (HttpWebResponse)WebRequest.Create(@"http://localhost:26762/api/v1/hidguardian/whitelist/remove/" + pid).GetResponse();
} catch (Exception e) {
form.console.Text += "Unable to remove program from whitelist.\r\n";
}
server.Stop();
timer.Stop();
mgr.OnApplicationQuit();
form.console.Text += "";
}
static void Main(string[] args) {
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
form = new MainForm();
Application.Run(form);
}
}
}