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
|
#pragma once
#include <IOKit/hid/IOHIDLib.h>
auto deviceMatchingCallback(void* context, IOReturn result, void* sender, IOHIDDeviceRef device) -> void;
auto deviceRemovalCallback(void* context, IOReturn result, void* sender, IOHIDDeviceRef device) -> void;
struct InputJoypadIOKit {
Input& input;
InputJoypadIOKit(Input& input) : input(input) {}
struct Joypad {
auto appendElements(CFArrayRef elements) -> void {
for(u32 n : range(CFArrayGetCount(elements))) {
IOHIDElementRef element = (IOHIDElementRef)CFArrayGetValueAtIndex(elements, n);
IOHIDElementType type = IOHIDElementGetType(element);
u32 page = IOHIDElementGetUsagePage(element);
u32 usage = IOHIDElementGetUsage(element);
switch(type) {
case kIOHIDElementTypeInput_Button:
appendButton(element);
break;
case kIOHIDElementTypeInput_Axis:
case kIOHIDElementTypeInput_Misc:
if(page != kHIDPage_GenericDesktop && page != kHIDPage_Simulation) break;
if(usage == kHIDUsage_Sim_Accelerator || usage == kHIDUsage_Sim_Brake
|| usage == kHIDUsage_Sim_Rudder || usage == kHIDUsage_Sim_Throttle
|| usage == kHIDUsage_GD_X || usage == kHIDUsage_GD_Y || usage == kHIDUsage_GD_Z
|| usage == kHIDUsage_GD_Rx || usage == kHIDUsage_GD_Ry || usage == kHIDUsage_GD_Rz
|| usage == kHIDUsage_GD_Slider || usage == kHIDUsage_GD_Dial || usage == kHIDUsage_GD_Wheel
) appendAxis(element);
if(usage == kHIDUsage_GD_DPadUp || usage == kHIDUsage_GD_DPadDown
|| usage == kHIDUsage_GD_DPadLeft || usage == kHIDUsage_GD_DPadRight
|| usage == kHIDUsage_GD_Start || usage == kHIDUsage_GD_Select
|| usage == kHIDUsage_GD_SystemMainMenu
) appendButton(element);
if(usage == kHIDUsage_GD_Hatswitch
) appendHat(element);
break;
case kIOHIDElementTypeCollection:
if(CFArrayRef children = IOHIDElementGetChildren(element)) appendElements(children);
break;
}
}
}
auto appendAxis(IOHIDElementRef element) -> void {
IOHIDElementCookie cookie = IOHIDElementGetCookie(element);
if(auto duplicate = axes.find([cookie](auto axis) { return IOHIDElementGetCookie(axis) == cookie; })) {
return;
}
s32 min = IOHIDElementGetLogicalMin(element);
s32 max = IOHIDElementGetLogicalMax(element);
s32 range = max - min;
if(range == 0) return;
hid->axes().append(axes.size());
axes.append(element);
}
auto appendHat(IOHIDElementRef element) -> void {
IOHIDElementCookie cookie = IOHIDElementGetCookie(element);
if(auto duplicate = hats.find([cookie](auto hat) { return IOHIDElementGetCookie(hat) == cookie; })) {
return;
}
u32 n = hats.size() * 2;
hid->hats().append(n + 0);
hid->hats().append(n + 1);
hats.append(element);
}
auto appendButton(IOHIDElementRef element) -> void {
IOHIDElementCookie cookie = IOHIDElementGetCookie(element);
if(auto duplicate = buttons.find([cookie](auto button) { return IOHIDElementGetCookie(button) == cookie; })) {
return;
}
hid->buttons().append(buttons.size());
buttons.append(element);
}
shared_pointer<HID::Joypad> hid{new HID::Joypad};
IOHIDDeviceRef device = nullptr;
vector<IOHIDElementRef> axes;
vector<IOHIDElementRef> hats;
vector<IOHIDElementRef> buttons;
};
vector<Joypad> joypads;
IOHIDManagerRef manager = nullptr;
enum : s32 { Center = 0, Up = -1, Down = +1, Left = -1, Right = +1 };
auto assign(shared_pointer<HID::Joypad> hid, u32 groupID, u32 inputID, s16 value) -> void {
auto& group = hid->group(groupID);
if(group.input(inputID).value() == value) return;
input.doChange(hid, groupID, inputID, group.input(inputID).value(), value);
group.input(inputID).setValue(value);
}
auto poll(vector<shared_pointer<HID::Device>>& devices) -> void {
for(auto& jp : joypads) {
IOHIDDeviceRef device = jp.device;
for(u32 n : range(jp.axes.size())) {
s32 value = 0;
IOHIDValueRef valueRef;
if(IOHIDDeviceGetValue(device, jp.axes[n], &valueRef) == kIOReturnSuccess) {
s32 min = IOHIDElementGetLogicalMin(jp.axes[n]);
s32 max = IOHIDElementGetLogicalMax(jp.axes[n]);
s32 range = max - min;
value = (IOHIDValueGetIntegerValue(valueRef) - min) * 65535LL / range - 32767;
}
assign(jp.hid, HID::Joypad::GroupID::Axis, n, sclamp<16>(value));
}
for(u32 n : range(jp.hats.size())) {
s32 x = Center;
s32 y = Center;
IOHIDValueRef valueRef;
if(IOHIDDeviceGetValue(device, jp.hats[n], &valueRef) == kIOReturnSuccess) {
s32 position = IOHIDValueGetIntegerValue(valueRef);
s32 min = IOHIDElementGetLogicalMin(jp.hats[n]);
s32 max = IOHIDElementGetLogicalMax(jp.hats[n]);
if(position >= min && position <= max) {
position -= min;
s32 range = max - min + 1;
if(range == 4) {
position *= 2;
}
if(range == 8) {
switch(position) {
case 0: x = Up; y = Center; break;
case 1: x = Up; y = Right; break;
case 2: x = Center; y = Right; break;
case 3: x = Down; y = Right; break;
case 4: x = Down; y = Center; break;
case 5: x = Down; y = Left; break;
case 6: x = Center; y = Left; break;
case 7: x = Up; y = Left; break;
}
}
}
}
assign(jp.hid, HID::Joypad::GroupID::Hat, n * 2 + 0, x * 32767);
assign(jp.hid, HID::Joypad::GroupID::Hat, n * 2 + 1, y * 32767);
}
for(u32 n : range(jp.buttons.size())) {
s32 value = 0;
IOHIDValueRef valueRef;
if(IOHIDDeviceGetValue(device, jp.buttons[n], &valueRef) == kIOReturnSuccess) {
value = IOHIDValueGetIntegerValue(valueRef);
}
assign(jp.hid, HID::Joypad::GroupID::Button, n, (bool)value);
}
devices.append(jp.hid);
}
}
auto rumble(u64 id, bool enable) -> bool {
//todo
return false;
}
auto initialize() -> bool {
manager = IOHIDManagerCreate(kCFAllocatorDefault, kIOHIDOptionsTypeNone);
if(!manager) return false;
CFArrayRef matcher = createMatcher();
if(!matcher) {
releaseManager();
return false;
}
IOHIDManagerSetDeviceMatchingMultiple(manager, matcher);
IOHIDManagerRegisterDeviceMatchingCallback(manager, deviceMatchingCallback, this);
IOHIDManagerRegisterDeviceRemovalCallback(manager, deviceRemovalCallback, this);
IOHIDManagerScheduleWithRunLoop(manager, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode);
CFRelease(matcher);
if(IOHIDManagerOpen(manager, kIOHIDOptionsTypeNone) != kIOReturnSuccess) {
releaseManager();
return false;
}
return true;
}
auto terminate() -> void {
if(!manager) return;
IOHIDManagerClose(manager, kIOHIDOptionsTypeNone);
releaseManager();
}
auto appendJoypad(IOHIDDeviceRef device) -> void {
Joypad jp;
jp.device = device;
s32 vendorID, productID;
CFNumberGetValue((CFNumberRef)IOHIDDeviceGetProperty(device, CFSTR(kIOHIDVendorIDKey)), kCFNumberSInt32Type, &vendorID);
CFNumberGetValue((CFNumberRef)IOHIDDeviceGetProperty(device, CFSTR(kIOHIDProductIDKey)), kCFNumberSInt32Type, &productID);
jp.hid->setVendorID(vendorID);
jp.hid->setProductID(productID);
CFArrayRef elements = IOHIDDeviceCopyMatchingElements(device, nullptr, kIOHIDOptionsTypeNone);
if(elements) {
jp.appendElements(elements);
CFRelease(elements);
joypads.append(jp);
}
}
auto removeJoypad(IOHIDDeviceRef device) -> void {
for(u32 n : range(joypads.size())) {
if(joypads[n].device == device) {
joypads.remove(n);
return;
}
}
}
private:
auto releaseManager() -> void {
CFRelease(manager);
manager = nullptr;
}
auto createMatcher() -> CFArrayRef {
CFDictionaryRef dict1 = createMatcherCriteria(kHIDPage_GenericDesktop, kHIDUsage_GD_Joystick);
if(!dict1) return nullptr;
CFDictionaryRef dict2 = createMatcherCriteria(kHIDPage_GenericDesktop, kHIDUsage_GD_GamePad);
if(!dict2) return CFRelease(dict1), nullptr;
CFDictionaryRef dict3 = createMatcherCriteria(kHIDPage_GenericDesktop, kHIDUsage_GD_MultiAxisController);
if(!dict3) return CFRelease(dict1), CFRelease(dict2), nullptr;
const void* values[] = {dict1, dict2, dict3};
CFArrayRef array = CFArrayCreate(kCFAllocatorDefault, values, 3, &kCFTypeArrayCallBacks);
CFRelease(dict1), CFRelease(dict2), CFRelease(dict3);
return array;
}
auto createMatcherCriteria(u32 page, u32 usage) -> CFDictionaryRef {
CFNumberRef pageNumber = CFNumberCreate(kCFAllocatorDefault, kCFNumberIntType, &page);
if(!pageNumber) return nullptr;
CFNumberRef usageNumber = CFNumberCreate(kCFAllocatorDefault, kCFNumberIntType, &usage);
if(!usageNumber) return CFRelease(pageNumber), nullptr;
const void* keys[] = {CFSTR(kIOHIDDeviceUsagePageKey), CFSTR(kIOHIDDeviceUsageKey)};
const void* values[] = {pageNumber, usageNumber};
CFDictionaryRef dict = CFDictionaryCreate(
kCFAllocatorDefault, keys, values, 2,
&kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks
);
CFRelease(pageNumber), CFRelease(usageNumber);
return dict;
}
};
auto deviceMatchingCallback(void* context, IOReturn result, void* sender, IOHIDDeviceRef device) -> void {
((InputJoypadIOKit*)context)->appendJoypad(device);
}
auto deviceRemovalCallback(void* context, IOReturn result, void* sender, IOHIDDeviceRef device) -> void {
((InputJoypadIOKit*)context)->removeJoypad(device);
}
|