-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGpdControl.cs
More file actions
882 lines (774 loc) · 40.3 KB
/
GpdControl.cs
File metadata and controls
882 lines (774 loc) · 40.3 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
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
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;
using Microsoft.Win32.SafeHandles;
namespace GpdControl
{
static class NativeMethods
{
[DllImport("hid.dll", SetLastError = true)]
public static extern void HidD_GetHidGuid(out Guid hidGuid);
[DllImport("setupapi.dll", SetLastError = true, CharSet = CharSet.Auto)]
public static extern IntPtr SetupDiGetClassDevs(ref Guid ClassGuid, IntPtr Enumerator, IntPtr hwndParent, uint Flags);
[DllImport("setupapi.dll", SetLastError = true)]
public static extern bool SetupDiEnumDeviceInterfaces(IntPtr DeviceInfoSet, IntPtr DeviceInfoData, ref Guid InterfaceClassGuid, uint MemberIndex, ref SP_DEVICE_INTERFACE_DATA DeviceInterfaceData);
[DllImport("setupapi.dll", SetLastError = true, CharSet = CharSet.Auto)]
public static extern bool SetupDiGetDeviceInterfaceDetail(IntPtr DeviceInfoSet, ref SP_DEVICE_INTERFACE_DATA DeviceInterfaceData, IntPtr DeviceInterfaceDetailData, uint DeviceInterfaceDetailDataSize, out uint RequiredSize, IntPtr DeviceInfoData);
[DllImport("setupapi.dll", SetLastError = true)]
public static extern bool SetupDiDestroyDeviceInfoList(IntPtr DeviceInfoSet);
[DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)]
public static extern SafeFileHandle CreateFile(string lpFileName, uint dwDesiredAccess, uint dwShareMode, IntPtr lpSecurityAttributes, uint dwCreationDisposition, uint dwFlagsAndAttributes, IntPtr hTemplateFile);
[DllImport("hid.dll", SetLastError = true)]
public static extern bool HidD_GetAttributes(SafeFileHandle HidDeviceObject, ref HIDD_ATTRIBUTES Attributes);
[DllImport("hid.dll", SetLastError = true)]
public static extern bool HidD_GetPreparsedData(SafeFileHandle HidDeviceObject, out IntPtr PreparsedData);
[DllImport("hid.dll", SetLastError = true)]
public static extern bool HidD_FreePreparsedData(IntPtr PreparsedData);
[DllImport("hid.dll", SetLastError = true)]
public static extern int HidP_GetCaps(IntPtr PreparsedData, ref HIDP_CAPS Capabilities);
[DllImport("hid.dll", SetLastError = true)]
public static extern bool HidD_SetFeature(SafeFileHandle HidDeviceObject, byte[] ReportBuffer, int ReportBufferLength);
[DllImport("hid.dll", SetLastError = true)]
public static extern bool HidD_SetOutputReport(SafeFileHandle HidDeviceObject, byte[] ReportBuffer, int ReportBufferLength);
[DllImport("hid.dll", SetLastError = true)]
public static extern bool HidD_GetInputReport(SafeFileHandle HidDeviceObject, byte[] ReportBuffer, int ReportBufferLength);
[DllImport("hid.dll", SetLastError = true)]
public static extern bool HidD_GetFeature(SafeFileHandle HidDeviceObject, byte[] ReportBuffer, int ReportBufferLength);
public const uint DIGCF_PRESENT = 0x02;
public const uint DIGCF_DEVICEINTERFACE = 0x10;
public const uint GENERIC_READ = 0x80000000;
public const uint GENERIC_WRITE = 0x40000000;
public const uint FILE_SHARE_READ = 0x00000001;
public const uint FILE_SHARE_WRITE = 0x00000002;
public const uint OPEN_EXISTING = 3;
public const uint FILE_FLAG_OVERLAPPED = 0x40000000;
[StructLayout(LayoutKind.Sequential)]
public struct SP_DEVICE_INTERFACE_DATA
{
public uint cbSize;
public Guid InterfaceClassGuid;
public uint Flags;
public IntPtr Reserved;
}
[StructLayout(LayoutKind.Sequential)]
public struct HIDD_ATTRIBUTES
{
public int Size;
public ushort VendorID;
public ushort ProductID;
public ushort VersionNumber;
}
[StructLayout(LayoutKind.Sequential)]
public struct HIDP_CAPS
{
public ushort Usage;
public ushort UsagePage;
public ushort InputReportByteLength;
public ushort OutputReportByteLength;
public ushort FeatureReportByteLength;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 17)]
public ushort[] Reserved;
public ushort NumberLinkCollectionNodes;
public ushort NumberInputButtonCaps;
public ushort NumberInputValueCaps;
public ushort NumberInputDataIndices;
public ushort NumberOutputButtonCaps;
public ushort NumberOutputValueCaps;
public ushort NumberOutputDataIndices;
public ushort NumberFeatureButtonCaps;
public ushort NumberFeatureValueCaps;
public ushort NumberFeatureDataIndices;
}
}
public class GpdDevice : IDisposable
{
private SafeFileHandle _handle;
private const ushort VID = 0x2f24;
private const ushort USAGE_PAGE = 0xff00;
private readonly byte[] _requestBuffer = new byte[33];
private readonly byte[] _outputReport0Buffer = new byte[34];
private readonly byte[] _responseBuffer = new byte[65];
private readonly byte[] _writePayloadBuffer = new byte[16];
public void Open()
{
Guid hidGuid;
NativeMethods.HidD_GetHidGuid(out hidGuid);
IntPtr deviceInfoSet = NativeMethods.SetupDiGetClassDevs(ref hidGuid, IntPtr.Zero, IntPtr.Zero, NativeMethods.DIGCF_PRESENT | NativeMethods.DIGCF_DEVICEINTERFACE);
try
{
NativeMethods.SP_DEVICE_INTERFACE_DATA deviceInterfaceData = new NativeMethods.SP_DEVICE_INTERFACE_DATA();
deviceInterfaceData.cbSize = (uint)Marshal.SizeOf(deviceInterfaceData);
uint index = 0;
while (NativeMethods.SetupDiEnumDeviceInterfaces(deviceInfoSet, IntPtr.Zero, ref hidGuid, index, ref deviceInterfaceData))
{
uint requiredSize = 0;
NativeMethods.SetupDiGetDeviceInterfaceDetail(deviceInfoSet, ref deviceInterfaceData, IntPtr.Zero, 0, out requiredSize, IntPtr.Zero);
IntPtr detailDataBuffer = Marshal.AllocHGlobal((int)requiredSize);
try
{
Marshal.WriteInt32(detailDataBuffer, (IntPtr.Size == 4) ? (4 + Marshal.SystemDefaultCharSize) : 8); // cbSize
if (NativeMethods.SetupDiGetDeviceInterfaceDetail(deviceInfoSet, ref deviceInterfaceData, detailDataBuffer, requiredSize, out requiredSize, IntPtr.Zero))
{
IntPtr pDevicePath = (IntPtr)((long)detailDataBuffer + 4); // Offset for DevicePath
string devicePath = Marshal.PtrToStringAuto(pDevicePath);
// Console.WriteLine("Found Device Path: " + devicePath);
var handle = NativeMethods.CreateFile(devicePath, NativeMethods.GENERIC_READ | NativeMethods.GENERIC_WRITE, NativeMethods.FILE_SHARE_READ | NativeMethods.FILE_SHARE_WRITE, IntPtr.Zero, NativeMethods.OPEN_EXISTING, 0, IntPtr.Zero);
if (!handle.IsInvalid)
{
NativeMethods.HIDD_ATTRIBUTES attributes = new NativeMethods.HIDD_ATTRIBUTES();
attributes.Size = Marshal.SizeOf(attributes);
if (NativeMethods.HidD_GetAttributes(handle, ref attributes))
{
// Console.WriteLine(string.Format(" VendorID: 0x{0:X4}, ProductID: 0x{1:X4}", attributes.VendorID, attributes.ProductID));
if (attributes.VendorID == VID)
{
IntPtr preparsedData;
if (NativeMethods.HidD_GetPreparsedData(handle, out preparsedData))
{
NativeMethods.HIDP_CAPS caps = new NativeMethods.HIDP_CAPS();
NativeMethods.HidP_GetCaps(preparsedData, ref caps);
NativeMethods.HidD_FreePreparsedData(preparsedData);
// Console.WriteLine(string.Format(" UsagePage: 0x{0:X4}", caps.UsagePage));
if (caps.UsagePage == USAGE_PAGE)
{
_handle = handle;
return; // Found it
}
}
}
}
handle.Close();
}
}
}
finally
{
Marshal.FreeHGlobal(detailDataBuffer);
}
index++;
}
}
finally
{
NativeMethods.SetupDiDestroyDeviceInfoList(deviceInfoSet);
}
throw new Exception("GPD Win Controller not found.");
}
public void Dispose()
{
if (_handle != null) _handle.Close();
}
private byte[] SendReq(byte id, byte minorSerial, byte[] data = null)
{
return SendReqWithRetry(id, minorSerial, data);
}
private byte[] SendReqWithRetry(byte id, byte minorSerial, byte[] data = null, int retries = 3)
{
Exception lastEx = null;
for(int i=0; i<=retries; i++)
{
try
{
return SendReqInternal(id, minorSerial, data);
}
catch(Exception ex)
{
lastEx = ex;
System.Threading.Thread.Sleep(50);
}
}
throw lastEx;
}
private byte[] SendReqInternal(byte id, byte minorSerial, byte[] data = null)
{
Array.Clear(_requestBuffer, 0, _requestBuffer.Length);
_requestBuffer[0] = 0x01; // ID
_requestBuffer[1] = 0xa5;
_requestBuffer[2] = id;
_requestBuffer[3] = 0x5a;
_requestBuffer[4] = (byte)(id ^ 0xFF);
_requestBuffer[5] = 0x00; // Reserved / Padding
_requestBuffer[6] = minorSerial; // Index Low
_requestBuffer[7] = 0x00; // Index High
if (data != null) Buffer.BlockCopy(data, 0, _requestBuffer, 8, Math.Min(data.Length, 25));
bool writeSuccess = false;
// Method 1: Feature ID 1 (PRIORITY for Config Write)
if (NativeMethods.HidD_SetFeature(_handle, _requestBuffer, _requestBuffer.Length))
{
writeSuccess = true;
}
else
{
// Fallback: Output ID 1
if (NativeMethods.HidD_SetOutputReport(_handle, _requestBuffer, _requestBuffer.Length))
{
writeSuccess = true;
}
else
{
// Fallback: Output ID 0 (Shifted Payload)
_outputReport0Buffer[0] = 0x00; // Dummy
Buffer.BlockCopy(_requestBuffer, 0, _outputReport0Buffer, 1, 33);
if (NativeMethods.HidD_SetOutputReport(_handle, _outputReport0Buffer, _outputReport0Buffer.Length))
{
writeSuccess = true;
}
}
}
if (!writeSuccess) throw new Exception("Failed to write to device (SetFeature/SetOutput failed).");
if (id == 0x21 || id == 0x23) return null; // No response needed for writes (usually)
// Wait for device to generate NEW report
// Reduced from 100ms to 20ms to avoid timeout but allow processing
System.Threading.Thread.Sleep(20);
// Read Response
Array.Clear(_responseBuffer, 0, _responseBuffer.Length);
_responseBuffer[0] = 0x01;
if (NativeMethods.HidD_GetInputReport(_handle, _responseBuffer, _responseBuffer.Length))
{
return (byte[])_responseBuffer.Clone();
}
// Fallback for devices/firmware that only expose this data through feature reports.
if (NativeMethods.HidD_GetFeature(_handle, _responseBuffer, _responseBuffer.Length))
{
return (byte[])_responseBuffer.Clone();
}
throw new Exception("Failed to read response.");
}
public string FirmwareVersion { get; private set; }
public byte[] ReadConfig()
{
WaitReady(0x10);
// Read all minor serials (0-3) for Major 1.
byte[] load0_raw = SendReq(0x11, 0x00, null); // Minor0
System.Threading.Thread.Sleep(50);
byte[] load1_raw = SendReq(0x11, 0x01, null); // Minor1
System.Threading.Thread.Sleep(50);
byte[] load2_raw = SendReq(0x11, 0x02, null); // Minor2
System.Threading.Thread.Sleep(50);
byte[] load3_raw = SendReq(0x11, 0x03, null); // Minor3
System.Threading.Thread.Sleep(50);
// Console.WriteLine("DEBUG LOAD0: " + BitConverter.ToString(load0_raw));
// Console.WriteLine("DEBUG LOAD1: " + BitConverter.ToString(load1_raw));
// Console.WriteLine("DEBUG LOAD2: " + BitConverter.ToString(load2_raw));
// Console.WriteLine("DEBUG LOAD3: " + BitConverter.ToString(load3_raw));
byte[] configData = new byte[256];
// Extract the 64 byte payload from each response
// DEBUG logs show data starts at index 0 (e.g. E8 00 ...), so no Report ID offset is needed.
Buffer.BlockCopy(load0_raw, 0, configData, 0, 64);
Buffer.BlockCopy(load1_raw, 0, configData, 64, 64);
Buffer.BlockCopy(load2_raw, 0, configData, 128, 64);
Buffer.BlockCopy(load3_raw, 0, configData, 192, 64);
byte[] checkResponse = SendReq(0x12, 0x00, null); // Checksum command is Minor 0
// Parse Firmware info
byte xMaj = checkResponse[9];
byte xMin = checkResponse[10];
byte kMaj = checkResponse[11];
byte kMin = checkResponse[12];
FirmwareVersion = string.Format("X{0:x}{1:02x} K{2:x}{3:02x}", xMaj, xMin, kMaj, kMin);
uint checksum = BitConverter.ToUInt32(checkResponse, 24);
uint calculatedChecksum = 0;
for (int i = 0; i < configData.Length; i++)
{
calculatedChecksum += configData[i];
}
if (checksum != calculatedChecksum)
{
throw new Exception(string.Format("Checksum mismatch while reading. Device: {0}, Calc: {1}", checksum, calculatedChecksum));
}
return configData;
}
public void WriteConfig(byte[] config)
{
if (config.Length != 256) throw new ArgumentException("Config must be 256 bytes");
WaitReady(0x20);
// Reference protocol writes 8 x 16-byte blocks.
// In this implementation, the block index is sent in the request index byte (minorSerial).
for (int block = 0; block < 8; block++)
{
Buffer.BlockCopy(config, block * 16, _writePayloadBuffer, 0, 16);
SendReq(0x21, (byte)block, _writePayloadBuffer);
}
byte[] response = SendReq(0x22, 0x00, null); // Checksum. Minor 0
uint checksum = BitConverter.ToUInt32(response, 24);
uint calculatedChecksum = 0;
for (int i = 0; i < config.Length; i++)
{
calculatedChecksum += config[i];
}
if (checksum == calculatedChecksum)
{
SendReq(0x23, 0x00, null); // Commit. Minor 0
}
else
{
throw new Exception(string.Format("Checksum mismatch while writing. Device: {0}, Calc: {1}", checksum, calculatedChecksum));
}
}
private void WaitReady(byte id)
{
byte[] response = null;
int safeguard = 0;
while (true)
{
response = SendReq(id, 0x00, null); // Minor 0 for WaitReady?
if (response != null && response[8] == 0xaa)
{
return;
}
safeguard++;
if (safeguard > 100) throw new Exception("WaitReady timed out.");
System.Threading.Thread.Sleep(5);
}
}
}
public class KeyCodes
{
public static readonly Dictionary<string, byte> Map = new Dictionary<string, byte>(StringComparer.OrdinalIgnoreCase)
{
{"NONE", 0x00},
{"A", 0x04}, {"B", 0x05}, {"C", 0x06}, {"D", 0x07}, {"E", 0x08},
{"F", 0x09}, {"G", 0x0a}, {"H", 0x0b}, {"I", 0x0c}, {"J", 0x0d},
{"K", 0x0e}, {"L", 0x0f}, {"M", 0x10}, {"N", 0x11}, {"O", 0x12},
{"P", 0x13}, {"Q", 0x14}, {"R", 0x15}, {"S", 0x16}, {"T", 0x17},
{"U", 0x18}, {"V", 0x19}, {"W", 0x1a}, {"X", 0x1b}, {"Y", 0x1c},
{"Z", 0x1d},
{"1", 0x1e}, {"2", 0x1f}, {"3", 0x20}, {"4", 0x21}, {"5", 0x22},
{"6", 0x23}, {"7", 0x24}, {"8", 0x25}, {"9", 0x26}, {"0", 0x27},
{"ENTER", 0x28}, {"ESC", 0x29}, {"BACKSPACE", 0x2a}, {"TAB", 0x2b},
{"SPACE", 0x2c}, {"MINUS", 0x2d}, {"EQUAL", 0x2e}, {"LEFTBRACE", 0x2f},
{"RIGHTBRACE", 0x30}, {"BACKSLASH", 0x31}, {"HASHTILDE", 0x32},
{"SEMICOLON", 0x33}, {"APOSTROPHE", 0x34}, {"GRAVE", 0x35},
{"COMMA", 0x36}, {"DOT", 0x37}, {"SLASH", 0x38}, {"CAPSLOCK", 0x39},
{"F1", 0x3a}, {"F2", 0x3b}, {"F3", 0x3c}, {"F4", 0x3d}, {"F5", 0x3e},
{"F6", 0x3f}, {"F7", 0x40}, {"F8", 0x41}, {"F9", 0x42}, {"F10", 0x43},
{"F11", 0x44}, {"F12", 0x45},
{"SYSRQ", 0x46}, {"SCROLLLOCK", 0x47}, {"PAUSE", 0x48}, {"INSERT", 0x49},
{"HOME", 0x4a}, {"PAGEUP", 0x4b}, {"DELETE", 0x4c}, {"END", 0x4d},
{"PAGEDOWN", 0x4e}, {"RIGHT", 0x4f}, {"LEFT", 0x50}, {"DOWN", 0x51},
{"UP", 0x52}, {"NUMLOCK", 0x53}, {"KPSLASH", 0x54}, {"KPASTERISK", 0x55},
{"KPMINUS", 0x56}, {"KPPLUS", 0x57}, {"KPENTER", 0x58},
{"KP1", 0x59}, {"KP2", 0x5a}, {"KP3", 0x5b}, {"KP4", 0x5c},
{"KP5", 0x5d}, {"KP6", 0x5e}, {"KP7", 0x5f}, {"KP8", 0x60},
{"KP9", 0x61}, {"KP0", 0x62}, {"KPDOT", 0x63},
{"APPLICATIONS", 0x65}, {"POWER", 0x66},
{"MUTE", 0x7F}, {"VOLUP", 0x80}, {"VOLDN", 0x81},
{"LEFTCTRL", 0xe0}, {"LEFTSHIFT", 0xe1}, {"LEFTALT", 0xe2}, {"LEFTMETA", 0xe3}, {"LWIN", 0xe3},
{"RIGHTCTRL", 0xe4}, {"RIGHTSHIFT", 0xe5}, {"RIGHTALT", 0xe6}, {"RIGHTMETA", 0xe7}, {"RWIN", 0xe7},
{"MOUSE_WHEELUP", 0xe8}, {"MOUSE_WHEELDOWN", 0xe9}, {"MOUSE_LEFT", 0xea},
{"MOUSE_RIGHT", 0xeb}, {"MOUSE_MIDDLE", 0xec}, {"MOUSE_FAST", 0xed}
};
private static readonly Dictionary<byte, string> ReverseMap = BuildReverseMap();
private static Dictionary<byte, string> BuildReverseMap()
{
Dictionary<byte, string> reverse = new Dictionary<byte, string>();
foreach (KeyValuePair<string, byte> kvp in Map)
{
if (!reverse.ContainsKey(kvp.Value))
{
reverse[kvp.Value] = kvp.Key;
}
}
return reverse;
}
public static string GetName(byte code)
{
string name;
if (ReverseMap.TryGetValue(code, out name))
{
return name;
}
return string.Format("0x{0:X2}", code);
}
}
public class Config
{
public byte[] Raw { get; private set; }
// Definition of fields
// Offset, Name, Description
public class FieldDef { public int Offset; public string Name; public string Desc; public int Size; public string Type; }
public static List<FieldDef> Fields = new List<FieldDef>
{
// Block 0: DPad + ABXY (0-15)
new FieldDef{Offset=0, Name="du", Desc="dpad up", Size=2, Type="Key"},
new FieldDef{Offset=2, Name="dd", Desc="dpad down", Size=2, Type="Key"},
new FieldDef{Offset=4, Name="dl", Desc="dpad left", Size=2, Type="Key"},
new FieldDef{Offset=6, Name="dr", Desc="dpad right", Size=2, Type="Key"},
new FieldDef{Offset=8, Name="a", Desc="A button", Size=2, Type="Key"},
new FieldDef{Offset=10, Name="b", Desc="B button", Size=2, Type="Key"},
new FieldDef{Offset=12, Name="x", Desc="X button", Size=2, Type="Key"},
new FieldDef{Offset=14, Name="y", Desc="Y button", Size=2, Type="Key"},
// Block 1: Left Stick + Clicks + Start/Select (16-31)
new FieldDef{Offset=16, Name="lu", Desc="left stick up", Size=2, Type="Key"},
new FieldDef{Offset=18, Name="ld", Desc="left stick down", Size=2, Type="Key"},
new FieldDef{Offset=20, Name="ll", Desc="left stick left", Size=2, Type="Key"},
new FieldDef{Offset=22, Name="lr", Desc="left stick right", Size=2, Type="Key"},
new FieldDef{Offset=24, Name="l3", Desc="left stick click", Size=2, Type="Key"},
new FieldDef{Offset=26, Name="r3", Desc="right stick click", Size=2, Type="Key"},
new FieldDef{Offset=28, Name="start", Desc="Start button", Size=2, Type="Key"},
new FieldDef{Offset=30, Name="select", Desc="Select button", Size=2, Type="Key"},
// Block 2: Menu + Shoulders (32-47)
new FieldDef{Offset=32, Name="menu", Desc="Menu button", Size=2, Type="Key"},
new FieldDef{Offset=34, Name="l1", Desc="L1 shoulder button", Size=2, Type="Key"},
new FieldDef{Offset=36, Name="r1", Desc="R1 shoulder button", Size=2, Type="Key"},
new FieldDef{Offset=38, Name="l2", Desc="L2 trigger", Size=2, Type="Key"},
new FieldDef{Offset=40, Name="r2", Desc="R2 trigger", Size=2, Type="Key"},
// Block 3/4: Back Keys (50-64)
new FieldDef{Offset=50, Name="l41", Desc="L4 macro key 1", Size=2, Type="Key"},
new FieldDef{Offset=52, Name="l42", Desc="L4 macro key 2", Size=2, Type="Key"},
new FieldDef{Offset=54, Name="l43", Desc="L4 macro key 3", Size=2, Type="Key"},
new FieldDef{Offset=56, Name="l44", Desc="L4 macro key 4", Size=2, Type="Key"},
new FieldDef{Offset=58, Name="r41", Desc="R4 macro key 1", Size=2, Type="Key"},
new FieldDef{Offset=60, Name="r42", Desc="R4 macro key 2", Size=2, Type="Key"},
new FieldDef{Offset=62, Name="r43", Desc="R4 macro key 3", Size=2, Type="Key"},
new FieldDef{Offset=64, Name="r44", Desc="R4 macro key 4", Size=2, Type="Key"},
// Block 4/5: Misc + Delays (66-95)
new FieldDef{Offset=66, Name="rumble", Desc="Rumble", Size=1, Type="Rumble"},
new FieldDef{Offset=68, Name="ledmode", Desc="LED mode", Size=1, Type="LedMode"},
new FieldDef{Offset=69, Name="colour", Desc="LED colour", Size=3, Type="Colour"},
new FieldDef{Offset=72, Name="ldead", Desc="Left stick deadzone", Size=1, Type="Signed"},
new FieldDef{Offset=73, Name="lcent", Desc="Left stick centering", Size=1, Type="Signed"},
new FieldDef{Offset=74, Name="rdead", Desc="Right stick deadzone", Size=1, Type="Signed"},
new FieldDef{Offset=75, Name="rcent", Desc="Right stick centering", Size=1, Type="Signed"},
new FieldDef{Offset=80,Name="l4delay1",Desc="L4 macro delay 1",Size=2,Type="Millis"},
new FieldDef{Offset=82,Name="l4delay2",Desc="L4 macro delay 2",Size=2,Type="Millis"},
new FieldDef{Offset=84,Name="l4delay3",Desc="L4 macro delay 3",Size=2,Type="Millis"},
new FieldDef{Offset=86,Name="l4delay4",Desc="L4 macro delay 4",Size=2,Type="Millis"},
new FieldDef{Offset=88,Name="r4delay1",Desc="R4 macro delay 1",Size=2,Type="Millis"},
new FieldDef{Offset=90,Name="r4delay2",Desc="R4 macro delay 2",Size=2,Type="Millis"},
new FieldDef{Offset=92,Name="r4delay3",Desc="R4 macro delay 3",Size=2,Type="Millis"},
new FieldDef{Offset=94,Name="r4delay4",Desc="R4 macro delay 4",Size=2,Type="Millis"},
};
private static readonly Dictionary<string, FieldDef> FieldsByName = BuildFieldLookup();
private static Dictionary<string, FieldDef> BuildFieldLookup()
{
Dictionary<string, FieldDef> lookup = new Dictionary<string, FieldDef>(StringComparer.OrdinalIgnoreCase);
foreach (FieldDef def in Fields)
{
lookup[def.Name] = def;
}
return lookup;
}
public Config(byte[] raw)
{
Raw = raw;
}
public void Set(string key, string value)
{
FieldDef def;
if (!FieldsByName.TryGetValue(key, out def))
{
throw new Exception(string.Format("Unknown setting: {0}", key));
}
switch (def.Type)
{
case "Key":
byte code;
string normalized = value.Trim().ToUpperInvariant();
if (normalized == "PRINTSCREEN") normalized = "SYSRQ";
if (normalized.StartsWith("0X"))
{
if (!byte.TryParse(normalized.Substring(2), System.Globalization.NumberStyles.HexNumber, System.Globalization.CultureInfo.InvariantCulture, out code))
{
throw new Exception(string.Format("Invalid hex keycode: {0}", value));
}
}
else if (!KeyCodes.Map.TryGetValue(normalized, out code))
{
throw new Exception(string.Format("Invalid key: {0}", value));
}
Raw[def.Offset] = code; // Write keycode to first byte (Little Endian)
Raw[def.Offset + 1] = 0x00; // Zero second byte
break;
case "Signed":
sbyte sb = sbyte.Parse(value);
Raw[def.Offset] = (byte)sb;
break;
case "Rumble":
byte rb = byte.Parse(value);
if (rb > 2) throw new Exception("Rumble must be 0, 1, or 2");
Raw[def.Offset] = rb;
break;
case "LedMode":
string mode = value.Trim().ToLowerInvariant();
if (mode == "off") Raw[def.Offset] = 0x00;
else if (mode == "solid") Raw[def.Offset] = 0x01;
else if (mode == "breathe") Raw[def.Offset] = 0x11;
else if (mode == "rotate") Raw[def.Offset] = 0x21;
else throw new Exception("LED mode must be off, solid, breathe, or rotate");
break;
case "Colour":
string hex = value.Trim();
if (hex.StartsWith("#")) hex = hex.Substring(1);
if (hex.Length != 6) throw new Exception("Colour must be a 6-digit hex value (RRGGBB)");
int rgb;
if (!int.TryParse(hex, System.Globalization.NumberStyles.HexNumber, System.Globalization.CultureInfo.InvariantCulture, out rgb))
{
throw new Exception("Colour must be a valid hex value (RRGGBB)");
}
// Device format is B, G, R.
Raw[def.Offset] = (byte)(rgb & 0xFF);
Raw[def.Offset + 1] = (byte)((rgb >> 8) & 0xFF);
Raw[def.Offset + 2] = (byte)((rgb >> 16) & 0xFF);
break;
case "Millis":
ushort ms = ushort.Parse(value);
BitConverter.GetBytes(ms).CopyTo(Raw, def.Offset);
break;
// Add other types...
}
}
public string GetValue(FieldDef def)
{
switch (def.Type)
{
case "Key":
// Keycodes are stored as XX 00. The actual keycode is the first byte.
return KeyCodes.GetName(Raw[def.Offset]);
case "Signed":
return ((sbyte)Raw[def.Offset]).ToString();
case "Rumble":
return Raw[def.Offset].ToString();
case "LedMode":
byte m = Raw[def.Offset];
if (m == 0) return "off";
if (m == 1) return "solid";
if (m == 0x11) return "breathe";
if (m == 0x21) return "rotate";
return string.Format("0x{0:X2}", m);
case "Colour":
return string.Format("{0:X2}{1:X2}{2:X2}", Raw[def.Offset+2], Raw[def.Offset+1], Raw[def.Offset]);
case "Millis":
return BitConverter.ToUInt16(Raw, def.Offset).ToString();
default:
return "?";
}
}
public string ToProfileString()
{
StringBuilder sb = new StringBuilder();
foreach (var def in Fields)
{
sb.AppendLine(string.Format("{0}={1}", def.Name, GetValue(def)));
}
return sb.ToString();
}
public static void LoadFromProfile(Config config, string[] lines)
{
for (int i = 0; i < lines.Length; i++)
{
string line = lines[i];
if (string.IsNullOrWhiteSpace(line) || line.Trim().StartsWith("#")) continue;
string[] parts = line.Split(new char[] { '=' }, 2);
if (parts.Length != 2)
{
throw new Exception(string.Format("Invalid config line {0}: {1}", i + 1, line));
}
string key = parts[0].Trim();
string val = parts[1].Trim();
int parenIdx = val.IndexOf('(');
if (parenIdx > 0) val = val.Substring(0, parenIdx).Trim();
try
{
config.Set(key, val);
}
catch (Exception ex)
{
throw new Exception(string.Format("Invalid config line {0}: {1} ({2})", i + 1, line, ex.Message));
}
}
}
public void Dump()
{
foreach (var def in Fields)
{
Console.WriteLine(string.Format("{0} = {1} ({2})", def.Name, GetValue(def), def.Desc));
}
}
}
class CliProgram
{
private static bool TryResolveProfilePath(string profilesDir, string profileName, out string fullPath, out string error)
{
fullPath = null;
error = null;
if (string.IsNullOrWhiteSpace(profileName))
{
error = "Profile name cannot be empty.";
return false;
}
if (profileName.IndexOfAny(Path.GetInvalidFileNameChars()) >= 0 || profileName.Contains("\\") || profileName.Contains("/"))
{
error = "Profile name contains invalid characters.";
return false;
}
string candidate = Path.GetFullPath(Path.Combine(profilesDir, profileName + ".txt"));
string baseDir = Path.GetFullPath(profilesDir).TrimEnd(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar)
+ Path.DirectorySeparatorChar;
if (!candidate.StartsWith(baseDir, StringComparison.OrdinalIgnoreCase))
{
error = "Profile path escapes the profiles directory.";
return false;
}
fullPath = candidate;
return true;
}
static void Main(string[] args)
{
if (args.Length == 0)
{
Console.WriteLine("Usage: GpdControl <command> [args]");
Console.WriteLine("Commands:");
Console.WriteLine(" list Show current device configuration");
Console.WriteLine(" reset Reset to default mappings (requires confirmation)");
Console.WriteLine(" profile load <name> Load a profile from 'profiles' folder");
Console.WriteLine(" profile del <name> Delete a profile");
Console.WriteLine(" apply <file> Apply a specific mapping file");
Console.WriteLine(" set <key> <val> Set a single key");
return;
}
try
{
using (var device = new GpdDevice())
{
device.Open();
byte[] data = device.ReadConfig();
var config = new Config(data);
string command = args[0].ToLower();
if (command == "list")
{
Console.WriteLine(string.Format("Firmware: {0}", device.FirmwareVersion));
config.Dump();
}
else if (command == "reset")
{
Console.Write("Are you sure you want to reset to defaults? Type 'yes' to confirm: ");
string confirm = Console.ReadLine();
if (confirm == "yes")
{
if (File.Exists("default_mappings.txt"))
{
string[] lines = File.ReadAllLines("default_mappings.txt");
Config.LoadFromProfile(config, lines);
device.WriteConfig(config.Raw);
Console.WriteLine("Reset complete. Defaults applied.");
}
else
{
Console.WriteLine("Error: default_mappings.txt not found.");
}
}
else
{
Console.WriteLine("Operation terminated.");
}
}
else if (command == "profile")
{
string profilesDir = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "profiles");
if (!Directory.Exists(profilesDir)) Directory.CreateDirectory(profilesDir);
if (args.Length < 2)
{
Console.WriteLine("Usage: profile <load|del> <name>");
return;
}
string subCmd = args[1].ToLower();
if (subCmd == "load")
{
if (args.Length < 3) { Console.WriteLine("Usage: profile load <name>"); return; }
string path;
string err;
if (!TryResolveProfilePath(profilesDir, args[2], out path, out err))
{
Console.WriteLine("Error: " + err);
return;
}
if (File.Exists(path))
{
string[] lines = File.ReadAllLines(path);
Config.LoadFromProfile(config, lines);
device.WriteConfig(config.Raw);
Console.WriteLine("Profile '" + args[2] + "' loaded to device.");
}
else
{
Console.WriteLine("Profile not found: " + path);
}
}
else if (subCmd == "del")
{
if (args.Length < 3) { Console.WriteLine("Usage: profile del <name>"); return; }
string path;
string err;
if (!TryResolveProfilePath(profilesDir, args[2], out path, out err))
{
Console.WriteLine("Error: " + err);
return;
}
if (File.Exists(path))
{
File.Delete(path);
Console.WriteLine("Profile '" + args[2] + "' deleted.");
}
else
{
Console.WriteLine("Profile not found.");
}
}
}
else if (command == "listdump")
{
if (args.Length < 2) { Console.WriteLine("Usage: listdump <filename>"); return; }
using (StreamWriter sw = new StreamWriter(args[1]))
{
Console.SetOut(sw); // Redirect Console.WriteLine to file
Console.WriteLine(string.Format("Firmware: {0}", device.FirmwareVersion));
config.Dump();
sw.WriteLine("RAW DATA DUMP:");
sw.WriteLine(BitConverter.ToString(data));
}
var standardOutput = new StreamWriter(Console.OpenStandardOutput());
standardOutput.AutoFlush = true;
Console.SetOut(standardOutput);
Console.WriteLine("Dump written to " + args[1]);
}
else if (command == "set")
{
if (args.Length < 3) { Console.WriteLine("Usage: set <key> <value>"); return; }
config.Set(args[1], args[2]);
device.WriteConfig(config.Raw);
Console.WriteLine("Applied.");
}
else if (command == "apply")
{
if (args.Length < 2) { Console.WriteLine("Usage: apply <filename>"); return; }
string[] lines = File.ReadAllLines(args[1]);
Config.LoadFromProfile(config, lines);
device.WriteConfig(config.Raw);
Console.WriteLine("Applied configuration from file.");
}
else if (command == "dumpraw")
{
if (args.Length < 2) { Console.WriteLine("Usage: dumpraw <filename>"); return; }
File.WriteAllBytes(args[1], data);
Console.WriteLine(string.Format("Raw config dumped to {0}", args[1]));
StringBuilder sb = new StringBuilder();
for (int i = 0; i < data.Length; i++)
{
sb.Append(string.Format("{0:X2} ", data[i]));
if ((i + 1) % 16 == 0) sb.AppendLine();
}
File.WriteAllText(args[1] + ".txt", sb.ToString());
Console.WriteLine(string.Format("Hex text dump written to {0}.txt", args[1]));
}
}
}
catch (Exception ex)
{
Console.WriteLine(string.Format("Error: {0}", ex.Message));
Environment.ExitCode = 1;
}
}
}
}