forked from willoftw/ShockEmu
-
Notifications
You must be signed in to change notification settings - Fork 13
/
iohid_wrap.m
260 lines (223 loc) · 8.58 KB
/
iohid_wrap.m
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
#include <CoreFoundation/CoreFoundation.h>
#include <IOKit/IOTypes.h>
#include <IOKit/IOReturn.h>
#include <IOKit/hid/IOHIDLib.h>
#include <stdio.h>
#include <unistd.h>
#include <dlfcn.h>
#import <Foundation/Foundation.h>
#import <AppKit/AppKit.h>
#import <objc/runtime.h>
typedef struct {
uint8_t id,
left_x, left_y,
right_x, right_y,
buttons1, buttons2, buttons3,
left_trigger, right_trigger,
unk1, unk2, unk3;
int16_t gyro_x, gyro_y, gyro_z;
int16_t accel_x, accel_y, accel_z;
uint8_t unk4[39];
} PSReport;
IOHIDManagerRef IOHIDManagerCreate( CFAllocatorRef allocator, IOOptionBits options) {
printf("IOHIDManagerCreate\n");
return (IOHIDManagerRef) 0xDEADBEEF;
}
IOReturn IOHIDManagerOpen( IOHIDManagerRef manager, IOOptionBits options) {
printf("IOHIDManagerOpen\n");
return kIOReturnSuccess;
}
IOReturn IOHIDManagerClose( IOHIDManagerRef manager, IOOptionBits options) {
printf("IOHIDManagerClose\n");
return kIOReturnSuccess;
}
CFSetRef IOHIDManagerCopyDevices( IOHIDManagerRef manager) {
IOHIDDeviceRef dev = (IOHIDDeviceRef) 0xDEADBEEF;
IOHIDDeviceRef devs[1] = {dev};
printf("IOHIDManagerCopyDevices\n");
return CFSetCreate(NULL, (const void **) devs, 1, NULL);
}
void IOHIDManagerRegisterDeviceMatchingCallback( IOHIDManagerRef manager, IOHIDDeviceCallback callback, void *context) {
printf("IOHIDManagerRegisterDeviceMatchingCallback\n");
}
void IOHIDManagerRegisterDeviceRemovalCallback( IOHIDManagerRef manager, IOHIDDeviceCallback callback, void *context) {
printf("IOHIDManagerRegisterDeviceMatchingCallback\n");
}
void IOHIDManagerSetDeviceMatchingMultiple( IOHIDManagerRef manager, CFArrayRef multiple) {
printf("IOHIDManagerSetDeviceMatchingMultiple\n");
}
void IOHIDManagerUnscheduleFromRunLoop( IOHIDManagerRef manager, CFRunLoopRef runLoop, CFStringRef runLoopMode) {
printf("IOHIDManagerUnscheduleFromRunLoop\n");
}
IOReturn IOHIDDeviceOpen( IOHIDDeviceRef device, IOOptionBits options) {
printf("IOHIDDeviceOpen %08lx\n", (unsigned long int) device);
return kIOReturnSuccess;
}
CFNumberRef makeUShort(unsigned short value) {
return CFNumberCreate(NULL, kCFNumberShortType, &value);
}
CFTypeRef IOHIDDeviceGetProperty( IOHIDDeviceRef device, CFStringRef key) {
printf("IOHIDDeviceGetProperty('%s')\n", CFStringGetCStringPtr(key, kCFStringEncodingMacRoman));
if(CFStringCompare(key, CFSTR("VendorID"), 0) == 0) {
return makeUShort(0x54c);
} else if(CFStringCompare(key, CFSTR("ProductID"), 0) == 0) {
return makeUShort(0x5c4);
} else if(CFStringCompare(key, CFSTR("Transport"), 0) == 0) {
return CFSTR("USB");
} else if(CFStringCompare(key, CFSTR("VersionNumber"), 0) == 0) {
return makeUShort(0x100);
}
return NULL;
}
IOReturn IOHIDDeviceGetReport( IOHIDDeviceRef device, IOHIDReportType reportType, CFIndex reportID, uint8_t *report, CFIndex *pReportLength) {
printf("IOHIDDeviceGetReport(0x%x, %i)\n", (int) reportID, pReportLength == NULL ? 0 : (int) *pReportLength);
if(reportID == 0x12) {
uint8_t report12[] = {0x12, 0x8B, 0x09, 0x07, 0x6D, 0x66, 0x1C, 0x08, 0x25, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
assert(pReportLength != NULL && *pReportLength >= sizeof(report12));
memcpy(report, report12, sizeof(report12));
} else if(reportID == 0xa3) {
uint8_t reporta3[] = {0xA3, 0x41, 0x75, 0x67, 0x20, 0x20, 0x33, 0x20, 0x32, 0x30, 0x31, 0x33, 0x00, 0x00, 0x00, 0x00, 0x00, 0x30, 0x37, 0x3A, 0x30, 0x31, 0x3A, 0x31, 0x32, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x31, 0x03, 0x00, 0x00, 0x00, 0x49, 0x00, 0x05, 0x00, 0x00, 0x80, 0x03, 0x00};
assert(pReportLength != NULL && *pReportLength >= sizeof(reporta3));
memcpy(report, reporta3, sizeof(reporta3));
} else if(reportID == 0x02) {
uint8_t report02[] = {0x02, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x87, 0x22, 0x7B, 0xDD, 0xB2, 0x22, 0x47, 0xDD, 0xBD, 0x22, 0x43, 0xDD, 0x1C, 0x02, 0x1C, 0x02, 0x7F, 0x1E, 0x2E, 0xDF, 0x60, 0x1F, 0x4C, 0xE0, 0x3A, 0x1D, 0xC6, 0xDE, 0x08, 0x00};
assert(pReportLength != NULL && *pReportLength >= sizeof(report02));
memcpy(report, report02, sizeof(report02));
}
return kIOReturnSuccess;
}
IOReturn IOHIDDeviceSetReport( IOHIDDeviceRef device, IOHIDReportType reportType, CFIndex reportID, const uint8_t *report, CFIndex reportLength) {
printf("IOHIDDeviceSetReport\n");
return kIOReturnSuccess;
}
@interface HIDRunner:NSObject
{
CFRunLoopRef runLoop;
CFStringRef runLoopMode;
IOHIDReportCallback callback;
void *context;
uint8_t *report;
CFIndex reportLength;
uint64_t ticks;
bool X, O, square, triangle, PS, touchpad, options, share,
L1, L2, L3, R1, R2, R3, dpadUp, dpadDown, dpadLeft, dpadRight;
float leftX, leftY, rightX, rightY; // -1 to 1
bool keys[256];
bool kicked, decayKicked;
}
@end
static HIDRunner *hid;
#define SWAP(ocls, sel) do { \
id rcls = NSClassFromString(@"_TtC10RemotePlay17RPWindowStreaming"); \
SEL selector = @selector sel; \
Method original = class_getInstanceMethod(rcls, selector); \
Method new = class_getInstanceMethod(cls, selector); \
method_exchangeImplementations(original, new); \
} while(0)
@implementation HIDRunner
+ (void)load {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
id cls = NSClassFromString(@"HIDRunner");
SWAP(@"_TtC10RemotePlay17RPWindowStreaming", (keyDown:));
SWAP(@"_TtC10RemotePlay17RPWindowStreaming", (keyUp:));
});
}
- (id)initWithRunLoop:(CFRunLoopRef)_runLoop andMode:(CFStringRef)_mode {
hid = self = [super init];
runLoop = _runLoop;
runLoopMode = _mode;
ticks = 0;
for(int i = 0; i < 256; ++i)
keys[i] = false;
return self;
}
- (void)registerCallback:(IOHIDReportCallback)cb withContext:(void *)ctx andReport:(uint8_t *)rep withLength:(CFIndex) repLen {
callback = cb;
context = ctx;
report = rep;
reportLength = repLen;
}
- (void)tick {
uint8_t brep[] = {0x01, 0x7f, 0x81, 0x82, 0x7d, 0x08, 0x00, 0xb4, 0x00, 0x00, 0xc8, 0xad, 0xf9, 0x04, 0x00, 0xfe, 0xff, 0xfc, 0xff, 0xe5, 0xfe, 0xcb, 0x1f, 0x69, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1b, 0x00, 0x00, 0x01, 0x63, 0x8b, 0x80, 0xc1, 0x2e, 0x80, 0x00, 0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00, 0x00, 0x80, 0x00};
PSReport *prep = (PSReport *) report;
memcpy(report, brep, sizeof(brep));
[self mapKeys];
uint8_t dpad = 8;
if(dpadLeft) {
if(dpadUp)
dpad = 7;
else if(dpadDown)
dpad = 5;
else
dpad = 6;
} else if(dpadRight) {
if(dpadUp)
dpad = 1;
else if(dpadDown)
dpad = 3;
else
dpad = 2;
} else if(dpadUp)
dpad = 0;
else if(dpadDown)
dpad = 4;
prep->buttons1 = (triangle ? (1 << 7) : 0) | (O ? (1 << 6) : 0) | (X ? (1 << 5) : 0) | (square ? (1 << 4) : 0) | dpad;
prep->buttons2 = (R3 ? (1 << 7) : 0) | (L3 ? (1 << 6) : 0) | (options ? (1 << 5) : 0) | (share ? (1 << 4) : 0) |
(R2 ? (1 << 3) : 0) | (L2 ? (1 << 2) : 0) | (R1 ? (1 << 1) : 0) | (L1 ? (1 << 0) : 0);
prep->buttons3 = ((ticks << 2) & 0xFF) | (touchpad ? 2 : 0) | (PS ? 1 : 0);
prep->left_trigger = L2 ? 255 : 0;
prep->right_trigger = R2 ? 255 : 0;
prep->left_x = (uint8_t) fmin(fmax(128 + leftX * 127, 0), 255);
prep->left_y = (uint8_t) fmin(fmax(128 + leftY * 127, 0), 255);
prep->right_x = (uint8_t) fmin(fmax(128 + rightX * 127, 0), 255);
prep->right_y = (uint8_t) fmin(fmax(128 + rightY * 127, 0), 255);
callback(context, kIOReturnSuccess, (void *)0xDEADBEEF, kIOHIDReportTypeInput, 0x01, report, 64);
ticks++;
}
- (void)kick {
if(kicked)
return;
kicked = true;
CFRunLoopPerformBlock(runLoop, runLoopMode, ^void() {
kicked = false;
[self tick];
});
}
- (void)decayKick {
if(decayKicked)
return;
decayKicked = true;
CFRunLoopPerformBlock(runLoop, runLoopMode, ^void() {
decayKicked = false;
[self tick];
});
}
#define JOYDECAY 5
#define DEADZONE .1
#define DOWN(key) keys[key]
- (void)mapKeys {
#include "mapKeys.h"
}
- (void)keyDown:(NSEvent *)event {
printf("down %hu", [event keyCode]);
hid->keys[[event keyCode]] = true;
[hid kick];
}
- (void)keyUp:(NSEvent *)event {
printf("up %hu", [event keyCode]);
hid->keys[[event keyCode]] = false;
[hid kick];
}
@end
void IOHIDManagerScheduleWithRunLoop( IOHIDManagerRef manager, CFRunLoopRef runLoop, CFStringRef runLoopMode) {
printf("IOHIDManagerScheduleWithRunLoop\n");
[[HIDRunner alloc] initWithRunLoop:runLoop andMode:runLoopMode];
}
void IOHIDDeviceScheduleWithRunLoop( IOHIDDeviceRef device, CFRunLoopRef runLoop, CFStringRef runLoopMode) {
printf("IOHIDDeviceScheduleWithRunLoop\n");
}
void IOHIDDeviceRegisterInputReportCallback( IOHIDDeviceRef device, uint8_t *report, CFIndex reportLength, IOHIDReportCallback callback, void *context) {
printf("IOHIDDeviceRegisterInputReportCallback\n");
[hid registerCallback:callback withContext:context andReport:report withLength:reportLength];
}