forked from backslash-f/ShockEmu
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathgamepad.c
170 lines (151 loc) · 5.69 KB
/
gamepad.c
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
#include <IOKit/hid/IOHIDManager.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "gamepad.h"
struct gamepad_device {
char name[1024];
IOHIDDeviceRef device;
struct gamepad_device* next;
};
struct gamepad_context {
IOHIDManagerRef hid_manager;
void (*callback)(int type, int page, int usage, int value);
struct gamepad_device* devices_head;
struct gamepad_device* devices_tail;
};
static void append_matching_dictionary(CFMutableArrayRef matcher, uint32_t page, uint32_t use)
{
CFMutableDictionaryRef result = CFDictionaryCreateMutable(kCFAllocatorDefault, 0, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
if (!result) return;
CFNumberRef pageNumber = CFNumberCreate(kCFAllocatorDefault, kCFNumberIntType, &page);
CFDictionarySetValue(result, CFSTR(kIOHIDDeviceUsagePageKey), pageNumber);
CFRelease(pageNumber);
CFNumberRef useNumber = CFNumberCreate(kCFAllocatorDefault, kCFNumberIntType, &use);
CFDictionarySetValue(result, CFSTR(kIOHIDDeviceUsageKey), useNumber);
CFRelease(useNumber);
CFArrayAppendValue(matcher, result);
CFRelease(result);
}
static void device_input(void* ctx, IOReturn result, void* sender, IOHIDValueRef value)
{
struct gamepad_context* c = (struct gamepad_context*)ctx;
if (c->callback) {
IOHIDElementRef element = IOHIDValueGetElement(value);
c->callback((int)IOHIDElementGetType(element), (int)IOHIDElementGetUsagePage(element), (int)IOHIDElementGetUsage(element), (int)IOHIDValueGetIntegerValue(value));
}
}
static void device_attached(void* ctx, IOReturn result, void* sender, IOHIDDeviceRef device)
{
struct gamepad_context* c = (struct gamepad_context*)ctx;
struct gamepad_device* d = (struct gamepad_device*)malloc(sizeof(struct gamepad_device));
CFStringRef name;
if (!d) return;
memset(d, 0, sizeof(struct gamepad_device));
d->device = device;
name = IOHIDDeviceGetProperty(device, CFSTR(kIOHIDProductKey));
if (!name) {
free(d);
return;
}
CFStringGetCString(name, d->name, 1024, kCFStringEncodingUTF8);
#ifdef DEBUG
printf("attched device: %s\n", d->name);
#endif
IOHIDDeviceOpen(device, kIOHIDOptionsTypeNone);
IOHIDDeviceScheduleWithRunLoop(device, CFRunLoopGetCurrent(), kCFRunLoopCommonModes);
IOHIDDeviceRegisterInputValueCallback(device, device_input, c);
if (c->devices_tail) {
c->devices_tail->next = d;
} else {
c->devices_head = d;
c->devices_tail = c->devices_head;
}
}
static void device_detached(void* ctx, IOReturn result, void* sender, IOHIDDeviceRef device)
{
struct gamepad_context* c = (struct gamepad_context*)ctx;
struct gamepad_device* prev;
struct gamepad_device* cur;
for (prev = NULL, cur = c->devices_head; cur; cur = cur->next) {
if (cur->device == device) {
if (NULL == prev) {
c->devices_head = cur->next;
if (NULL == c->devices_head) c->devices_tail = NULL;
} else if (cur == c->devices_tail) {
c->devices_tail = prev;
prev->next = NULL;
} else {
prev->next = cur->next;
}
#ifdef DEBUG
printf("detached device: %s\n", cur->name);
#endif
IOHIDDeviceClose(cur->device, kIOHIDOptionsTypeNone);
free(cur);
return;
}
prev = cur;
}
}
void* gamepad_init(int useGamePad, int useKeybord, int useMouse)
{
struct gamepad_context* c;
CFMutableArrayRef matcher;
c = (struct gamepad_context*)malloc(sizeof(struct gamepad_context));
if (!c) return NULL;
memset(c, 0, sizeof(struct gamepad_context));
c->hid_manager = IOHIDManagerCreate(kCFAllocatorDefault, kIOHIDOptionsTypeNone);
if (!c->hid_manager) {
gamepad_term(c);
return NULL;
}
matcher = CFArrayCreateMutable(kCFAllocatorDefault, 0, &kCFTypeArrayCallBacks);
if (!matcher) {
gamepad_term(c);
return NULL;
}
if (useGamePad) {
append_matching_dictionary(matcher, kHIDPage_GenericDesktop, kHIDUsage_GD_Joystick);
append_matching_dictionary(matcher, kHIDPage_GenericDesktop, kHIDUsage_GD_GamePad);
}
if (useKeybord) {
append_matching_dictionary(matcher, kHIDPage_GenericDesktop, kHIDUsage_GD_Keyboard);
}
if (useMouse) {
append_matching_dictionary(matcher, kHIDPage_GenericDesktop, kHIDUsage_GD_Mouse);
}
IOHIDManagerSetDeviceMatchingMultiple(c->hid_manager, matcher);
CFRelease(matcher);
IOHIDManagerRegisterDeviceMatchingCallback(c->hid_manager, device_attached, c);
IOHIDManagerRegisterDeviceRemovalCallback(c->hid_manager, device_detached, c);
IOHIDManagerScheduleWithRunLoop(c->hid_manager, CFRunLoopGetMain(), kCFRunLoopCommonModes);
IOHIDManagerOpen(c->hid_manager, kIOHIDOptionsTypeNone);
return c;
}
void gamepad_set_callback(void* ctx, void (*callback)(int type, int page, int usage, int value))
{
struct gamepad_context* c = (struct gamepad_context*)ctx;
if (!c) return;
c->callback = callback;
}
void gamepad_term(void* ctx)
{
struct gamepad_context* c = (struct gamepad_context*)ctx;
if (!c) return;
if (c->devices_head) {
struct gamepad_device* work;
struct gamepad_device* next;
for (work = c->devices_head; work; work = next) {
next = work->next;
IOHIDDeviceClose(work->device, kIOHIDOptionsTypeNone);
free(work);
}
}
if (c->hid_manager) {
IOHIDManagerUnscheduleFromRunLoop(c->hid_manager, CFRunLoopGetCurrent(), kCFRunLoopCommonModes);
IOHIDManagerClose(c->hid_manager, kIOHIDOptionsTypeNone);
CFRelease(c->hid_manager);
}
free(c);
}