-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathJoyconManager.cs
353 lines (290 loc) · 8.46 KB
/
JoyconManager.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.NetworkInformation;
using System.Timers;
using System.Runtime.InteropServices;
using Android.Widget;
using static Joycon.HIDapi;
using PCLAppConfig;
namespace Joycon
{
public class JoyconManager
{
public bool EnableIMU = true;
public bool EnableLocalize = false;
private const ushort vendor_id = 0x57e;
private const ushort product_l = 0x2006;
private const ushort product_r = 0x2007;
private const ushort product_pro = 0x2009;
private const ushort product_snes = 0x2017;
public List<Joycon> j; // Array of all connected Joy-Cons
static JoyconManager instance;
public MainActivity mActivity;
Timer controllerCheck;
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 for new controllers 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 mActivity.con)
{
if (b.Enabled & b.Tag == (Object)v)
{
b.SetBackgroundColor(Android.Graphics.Color.Gray); //= System.Drawing.Color.FromArgb(0x00, System.Drawing.SystemColors.Control);
b.Enabled = false;
b.SetBackgroundResource(Resource.Drawable.cross);
break;
}
}
mActivity.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.IntValue("ProgressiveScan") == 1)
{
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.serial_number == null)
{
ptr = enumerate.next; // can't believe it took me this long to figure out why USB connections used up so much CPU.
// it was getting stuck in an inf loop here!
continue;
}
if (mActivity.nonOriginal)
{
enumerate.product_id = product_pro;
}
bool validController = (enumerate.product_id == product_l || enumerate.product_id == product_r ||
enumerate.product_id == product_pro || enumerate.product_id == product_snes);
if (validController && !ControllerAlreadyAdded(enumerate.path))
{
switch (enumerate.product_id)
{
case product_l:
isLeft = true;
mActivity.AppendTextBox("Left Joy-Con connected.\r\n"); break;
case product_r:
isLeft = false;
mActivity.AppendTextBox("Right Joy-Con connected.\r\n"); break;
case product_pro:
isLeft = true;
mActivity.AppendTextBox("Pro controller connected.\r\n"); break;
case product_snes:
isLeft = true;
mActivity.AppendTextBox("SNES controller connected.\r\n"); break;
default:
mActivity.AppendTextBox("Non Joy-Con Nintendo input device skipped.\r\n"); break;
}
IntPtr handle = HIDapi.hid_open_path(enumerate.path);
try
{
HIDapi.hid_set_nonblocking(handle, 1);
}
catch
{
mActivity.AppendTextBox("Unable to open path to device - are you using the correct (64 vs 32-bit) version for your PC?\r\n");
break;
}
bool isPro = enumerate.product_id == product_pro;
bool isSnes = enumerate.product_id == product_snes;
j.Add(new Joycon(handle, EnableIMU, EnableLocalize & EnableIMU, 0.05f, isLeft, enumerate.path, enumerate.serial_number, j.Count, isPro, isSnes));
foundNew = true;
j.Last().mActivity = mActivity;
if (j.Count < 5)
{
int ii = -1;
foreach (Button v in mActivity.con)
{
ii++;
if (!v.Enabled)
{
int temp;
switch (enumerate.product_id)
{
case (product_l):
temp = Resource.Drawable.jc_left_s; break;
case (product_r):
temp = Resource.Drawable.jc_right_s; break;
case (product_pro):
temp = Resource.Drawable.pro; break;
case (product_snes):
temp = Resource.Drawable.snes; break;
default:
temp = Resource.Drawable.cross; break;
}
v.Tag = j.Last(); // assign controller to button
v.Enabled = true;
v.Click += new EventHandler(mActivity.conBtnClick);
v.SetBackgroundResource(temp);
mActivity.loc[ii].Tag = v;
mActivity.loc[ii].Click += new EventHandler(mActivity.locBtnClick);
break;
}
}
}
byte[] mac = new byte[6];
Console.WriteLine(string.Format("mac address => {0}", enumerate.serial_number));
//onsole.WriteLine("mac address => {0} , len => {1}",, enumerate.serial_number.Length);
for (int n = 0; n < 6; n++)
mac[n] = byte.Parse(enumerate.serial_number.Substring(n * 2, 2), System.Globalization.NumberStyles.HexNumber); //TODO
j[j.Count - 1].PadMacAddress = new PhysicalAddress(mac);
}
ptr = enumerate.next;
}
if (foundNew)
{ // attempt to auto join-up joycons on connection
Joycon temp = null;
foreach (Joycon v in j)
{
if (!v.isPro)
{
if (temp == null)
temp = v;
else if (temp.isLeft != v.isLeft && v.other == null)
{
temp.other = v;
v.other = temp;
//Set both Joycon LEDs to the one with the lowest ID
byte led = temp.LED <= v.LED ? temp.LED : v.LED;
temp.LED = led;
v.LED = led;
temp.SetPlayerLED(led);
v.SetPlayerLED(led);
/*
if (temp.xin != null)
{
try
{
temp.xin.Disconnect();
}
catch (Exception e)
{
// it wasn't connected in the first place, go figure
}
}
if (temp.ds4 != null)
{
try
{
temp.ds4.Disconnect();
}
catch (Exception e)
{
// it wasn't connected in the first place, go figure
}
}
temp.xin = null;
temp.ds4 = null;
*/
foreach (Button b in mActivity.con)
if (b.Tag == (Object)v || b.Tag == (Object)temp)
{
Joycon tt = (b.Tag == (Object)v) ? v : (b.Tag == (Object)temp) ? temp : v;
//b.BackgroundImage = tt.isLeft ? Properties.Resources.jc_left : Properties.Resources.jc_right;
b.SetBackgroundResource(tt.isLeft ? Resource.Drawable.jc_left : Resource.Drawable.jc_right);
}
temp = null; // repeat
}
}
}
}
HIDapi.hid_free_enumeration(ptr);
foreach (Joycon jc in j)
{ // Connect device straight away
if (jc.state == Joycon.state_.NOT_ATTACHED)
{
/*
if (jc.xin != null)
jc.xin.Connect();
if (jc.ds4 != null)
jc.ds4.Connect();
*/
jc.Attach(leds_: jc.LED);
bool on = ConfigurationManager.AppSettings["HomeLEDOn"].ToLower() == "true"; //TODO
foreach (Joycon j in Program.mgr.j)
{
j.SetHomeLight(on);
}
jc.Begin();
if (mActivity.nonOriginal)
{
jc.getActiveData();
}
}
}
}
public void Update()
{
for (int i = 0; i < j.Count; ++i)
j[i].Update();
}
public void OnApplicationQuit()
{
foreach (Joycon v in j)
{
if (Boolean.Parse(ConfigurationManager.AppSettings["AutoPowerOff"]))
v.PowerOff();
else
v.Detach();
/*
if (v.xin != null)
{
v.xin.Disconnect();
}
if (v.ds4 != null)
{
v.ds4.Disconnect();
}*/
}
controllerCheck.Stop();
HIDapi.hid_exit();
}
}
}