-
Notifications
You must be signed in to change notification settings - Fork 340
/
HIDapi.cs
115 lines (90 loc) · 4.75 KB
/
HIDapi.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
using System;
using System.Runtime.InteropServices;
namespace BetterJoyForCemu {
public class HIDapi {
#if LINUX
const string dll = "libhidapi.so";
#else
const string dll = "hidapi.dll";
#endif
public struct hid_device_info {
[MarshalAs(UnmanagedType.LPStr)]
public string path;
public ushort vendor_id;
public ushort product_id;
[MarshalAs(UnmanagedType.LPWStr)]
public string serial_number;
public ushort release_number;
[MarshalAs(UnmanagedType.LPWStr)]
public string manufacturer_string;
[MarshalAs(UnmanagedType.LPWStr)]
public string product_string;
public ushort usage_page;
public ushort usage;
public int interface_number;
public IntPtr next;
};
[DllImport(dll, CallingConvention = CallingConvention.Cdecl)]
public static extern int hid_init();
[DllImport(dll, CallingConvention = CallingConvention.Cdecl)]
public static extern int hid_exit();
[DllImport(dll, CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr hid_enumerate(ushort vendor_id, ushort product_id);
[DllImport(dll, CallingConvention = CallingConvention.Cdecl)]
public static extern void hid_free_enumeration(IntPtr phid_device_info);
[DllImport(dll, CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr hid_open(ushort vendor_id, ushort product_id, [MarshalAs(UnmanagedType.LPWStr)]string serial_number);
[DllImport(dll, CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr hid_open_path([MarshalAs(UnmanagedType.LPStr)]string path);
[DllImport(dll, CallingConvention = CallingConvention.Cdecl)]
public static extern int hid_write(IntPtr device, byte[] data, UIntPtr length);
[DllImport(dll, CallingConvention = CallingConvention.Cdecl)]
public static extern int hid_read_timeout(IntPtr dev, byte[] data, UIntPtr length, int milliseconds);
[DllImport(dll, CallingConvention = CallingConvention.Cdecl)]
public static extern int hid_read(IntPtr device, byte[] data, UIntPtr length);
[DllImport(dll, CallingConvention = CallingConvention.Cdecl)]
public static extern int hid_set_nonblocking(IntPtr device, int nonblock);
[DllImport(dll, CallingConvention = CallingConvention.Cdecl)]
public static extern int hid_send_feature_report(IntPtr device, byte[] data, UIntPtr length);
[DllImport(dll, CallingConvention = CallingConvention.Cdecl)]
public static extern int hid_get_feature_report(IntPtr device, byte[] data, UIntPtr length);
[DllImport(dll, CallingConvention = CallingConvention.Cdecl)]
public static extern void hid_close(IntPtr device);
[DllImport(dll, CallingConvention = CallingConvention.Cdecl)]
public static extern int hid_get_manufacturer_string(IntPtr device, [MarshalAs(UnmanagedType.LPWStr)]string string_, UIntPtr maxlen);
[DllImport(dll, CallingConvention = CallingConvention.Cdecl)]
public static extern int hid_get_product_string(IntPtr device, [MarshalAs(UnmanagedType.LPWStr)]string string_, UIntPtr maxlen);
[DllImport(dll, CallingConvention = CallingConvention.Cdecl)]
public static extern int hid_get_serial_number_string(IntPtr device, [MarshalAs(UnmanagedType.LPWStr)]string string_, UIntPtr maxlen);
[DllImport(dll, CallingConvention = CallingConvention.Cdecl)]
public static extern int hid_get_indexed_string(IntPtr device, int string_index, [MarshalAs(UnmanagedType.LPWStr)]string string_, UIntPtr maxlen);
[DllImport(dll, CallingConvention = CallingConvention.Cdecl)]
[return: MarshalAs(UnmanagedType.LPWStr)]
public static extern string hid_error(IntPtr device);
static void PrintEnumeration(IntPtr phid_device_info) {
if (!phid_device_info.Equals(IntPtr.Zero)) {
hid_device_info hdev = (hid_device_info)Marshal.PtrToStructure(phid_device_info, typeof(hid_device_info));
Console.WriteLine(string.Format("path: {0}", hdev.path));
Console.WriteLine(string.Format("vendor id: {0:X}", hdev.vendor_id));
Console.WriteLine(string.Format("product id: {0:X}", hdev.product_id));
Console.WriteLine(string.Format("usage page: {0:X}", hdev.usage_page));
Console.WriteLine(string.Format("usage: {0:X}", hdev.usage));
Console.WriteLine("");
PrintEnumeration(hdev.next);
}
}
static string _getDevicePath(IntPtr phid_device_info, ushort usagePage, ushort usage) {
if (!phid_device_info.Equals(IntPtr.Zero)) {
hid_device_info hdev = (hid_device_info)Marshal.PtrToStructure(phid_device_info, typeof(hid_device_info));
if (usagePage == hdev.usage_page && usage == hdev.usage)
return hdev.path;
else
return _getDevicePath(hdev.next, usagePage, usage);
}
return null;
}
public static string GetDevicePath(ushort vendorId, ushort productId, ushort usagePage, ushort usage) {
return _getDevicePath(hid_enumerate(vendorId, productId), usagePage, usage);
}
}
}