2016-09-06 00:47:54 +02:00
|
|
|
/*************************************************************************/
|
2017-01-08 21:29:57 +01:00
|
|
|
/* joypad_osx.cpp */
|
2016-09-06 00:47:54 +02:00
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
2017-08-27 14:16:55 +02:00
|
|
|
/* https://godotengine.org */
|
2016-09-06 00:47:54 +02:00
|
|
|
/*************************************************************************/
|
2021-01-01 20:13:46 +01:00
|
|
|
/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
|
2016-09-06 00:47:54 +02:00
|
|
|
/* */
|
|
|
|
/* Permission is hereby granted, free of charge, to any person obtaining */
|
|
|
|
/* a copy of this software and associated documentation files (the */
|
|
|
|
/* "Software"), to deal in the Software without restriction, including */
|
|
|
|
/* without limitation the rights to use, copy, modify, merge, publish, */
|
|
|
|
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
|
|
|
/* permit persons to whom the Software is furnished to do so, subject to */
|
|
|
|
/* the following conditions: */
|
|
|
|
/* */
|
|
|
|
/* The above copyright notice and this permission notice shall be */
|
|
|
|
/* included in all copies or substantial portions of the Software. */
|
|
|
|
/* */
|
|
|
|
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
|
|
|
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
|
|
|
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
|
|
|
|
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
|
|
|
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
|
|
|
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
|
|
|
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
|
|
|
/*************************************************************************/
|
2018-01-05 00:50:27 +01:00
|
|
|
|
2017-01-08 21:05:51 +01:00
|
|
|
#include "joypad_osx.h"
|
2017-01-08 21:29:57 +01:00
|
|
|
|
2016-09-06 00:47:54 +02:00
|
|
|
#include <machine/endian.h>
|
|
|
|
|
2017-01-08 21:05:51 +01:00
|
|
|
#define GODOT_JOY_LOOP_RUN_MODE CFSTR("GodotJoypad")
|
2016-09-06 00:47:54 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
static JoypadOSX *self = NULL;
|
2016-09-06 00:47:54 +02:00
|
|
|
|
2017-01-08 21:05:51 +01:00
|
|
|
joypad::joypad() {
|
2016-09-06 00:47:54 +02:00
|
|
|
device_ref = NULL;
|
|
|
|
ff_device = NULL;
|
|
|
|
ff_axes = NULL;
|
|
|
|
ff_directions = NULL;
|
|
|
|
ffservice = 0;
|
|
|
|
ff_timestamp = 0;
|
|
|
|
id = 0;
|
|
|
|
|
|
|
|
ff_constant_force.lMagnitude = 10000;
|
|
|
|
ff_effect.dwDuration = 0;
|
|
|
|
ff_effect.dwSamplePeriod = 0;
|
|
|
|
ff_effect.dwGain = 10000;
|
|
|
|
ff_effect.dwFlags = FFEFF_OBJECTOFFSETS;
|
|
|
|
ff_effect.dwTriggerButton = FFEB_NOTRIGGER;
|
|
|
|
ff_effect.dwStartDelay = 0;
|
|
|
|
ff_effect.dwTriggerRepeatInterval = 0;
|
|
|
|
ff_effect.lpEnvelope = NULL;
|
|
|
|
ff_effect.cbTypeSpecificParams = sizeof(FFCONSTANTFORCE);
|
|
|
|
ff_effect.lpvTypeSpecificParams = &ff_constant_force;
|
|
|
|
ff_effect.dwSize = sizeof(ff_effect);
|
|
|
|
}
|
|
|
|
|
2017-01-08 21:05:51 +01:00
|
|
|
void joypad::free() {
|
2016-09-06 00:47:54 +02:00
|
|
|
if (device_ref) {
|
|
|
|
IOHIDDeviceUnscheduleFromRunLoop(device_ref, CFRunLoopGetCurrent(), GODOT_JOY_LOOP_RUN_MODE);
|
|
|
|
}
|
|
|
|
if (ff_device) {
|
|
|
|
FFDeviceReleaseEffect(ff_device, ff_object);
|
|
|
|
FFReleaseDevice(ff_device);
|
|
|
|
memfree(ff_axes);
|
|
|
|
memfree(ff_directions);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-08 21:05:51 +01:00
|
|
|
bool joypad::has_element(IOHIDElementCookie p_cookie, Vector<rec_element> *p_list) const {
|
2016-09-06 00:47:54 +02:00
|
|
|
for (int i = 0; i < p_list->size(); i++) {
|
|
|
|
if (p_cookie == p_list->get(i).cookie) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-01-08 21:05:51 +01:00
|
|
|
int joypad::get_hid_element_state(rec_element *p_element) const {
|
2016-09-06 00:47:54 +02:00
|
|
|
int value = 0;
|
|
|
|
if (p_element && p_element->ref) {
|
|
|
|
IOHIDValueRef valueRef;
|
|
|
|
if (IOHIDDeviceGetValue(device_ref, p_element->ref, &valueRef) == kIOReturnSuccess) {
|
2017-03-05 16:44:50 +01:00
|
|
|
value = (SInt32)IOHIDValueGetIntegerValue(valueRef);
|
2016-09-06 00:47:54 +02:00
|
|
|
|
|
|
|
/* record min and max for auto calibration */
|
|
|
|
if (value < p_element->min) {
|
|
|
|
p_element->min = value;
|
|
|
|
}
|
|
|
|
if (value > p_element->max) {
|
|
|
|
p_element->max = value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return value;
|
|
|
|
}
|
2017-01-08 21:05:51 +01:00
|
|
|
void joypad::add_hid_element(IOHIDElementRef p_element) {
|
2016-09-06 00:47:54 +02:00
|
|
|
const CFTypeID elementTypeID = p_element ? CFGetTypeID(p_element) : 0;
|
|
|
|
|
|
|
|
if (p_element && (elementTypeID == IOHIDElementGetTypeID())) {
|
|
|
|
const IOHIDElementCookie cookie = IOHIDElementGetCookie(p_element);
|
|
|
|
const uint32_t usagePage = IOHIDElementGetUsagePage(p_element);
|
|
|
|
const uint32_t usage = IOHIDElementGetUsage(p_element);
|
|
|
|
Vector<rec_element> *list = NULL;
|
|
|
|
|
|
|
|
switch (IOHIDElementGetType(p_element)) {
|
2017-03-05 16:44:50 +01:00
|
|
|
case kIOHIDElementTypeInput_Misc:
|
|
|
|
case kIOHIDElementTypeInput_Button:
|
|
|
|
case kIOHIDElementTypeInput_Axis: {
|
|
|
|
switch (usagePage) {
|
|
|
|
case kHIDPage_GenericDesktop:
|
|
|
|
switch (usage) {
|
|
|
|
case kHIDUsage_GD_X:
|
|
|
|
case kHIDUsage_GD_Y:
|
|
|
|
case kHIDUsage_GD_Z:
|
|
|
|
case kHIDUsage_GD_Rx:
|
|
|
|
case kHIDUsage_GD_Ry:
|
|
|
|
case kHIDUsage_GD_Rz:
|
|
|
|
case kHIDUsage_GD_Slider:
|
|
|
|
case kHIDUsage_GD_Dial:
|
|
|
|
case kHIDUsage_GD_Wheel:
|
|
|
|
if (!has_element(cookie, &axis_elements)) {
|
|
|
|
list = &axis_elements;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case kHIDUsage_GD_Hatswitch:
|
|
|
|
if (!has_element(cookie, &hat_elements)) {
|
|
|
|
list = &hat_elements;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case kHIDUsage_GD_DPadUp:
|
|
|
|
case kHIDUsage_GD_DPadDown:
|
|
|
|
case kHIDUsage_GD_DPadRight:
|
|
|
|
case kHIDUsage_GD_DPadLeft:
|
|
|
|
case kHIDUsage_GD_Start:
|
|
|
|
case kHIDUsage_GD_Select:
|
|
|
|
if (!has_element(cookie, &button_elements)) {
|
|
|
|
list = &button_elements;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case kHIDPage_Simulation:
|
|
|
|
switch (usage) {
|
|
|
|
case kHIDUsage_Sim_Rudder:
|
|
|
|
case kHIDUsage_Sim_Throttle:
|
|
|
|
if (!has_element(cookie, &axis_elements)) {
|
|
|
|
list = &axis_elements;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case kHIDPage_Button:
|
|
|
|
case kHIDPage_Consumer:
|
|
|
|
if (!has_element(cookie, &button_elements)) {
|
|
|
|
list = &button_elements;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
2016-09-06 00:47:54 +02:00
|
|
|
}
|
2017-03-05 16:44:50 +01:00
|
|
|
} break;
|
2016-09-06 00:47:54 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
case kIOHIDElementTypeCollection: {
|
|
|
|
CFArrayRef array = IOHIDElementGetChildren(p_element);
|
|
|
|
if (array) {
|
|
|
|
add_hid_elements(array);
|
2016-09-06 00:47:54 +02:00
|
|
|
}
|
2017-03-05 16:44:50 +01:00
|
|
|
} break;
|
2016-09-06 00:47:54 +02:00
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (list) { /* add to list */
|
2016-09-06 00:47:54 +02:00
|
|
|
rec_element element;
|
|
|
|
|
|
|
|
element.ref = p_element;
|
|
|
|
element.usage = usage;
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
element.min = (SInt32)IOHIDElementGetLogicalMin(p_element);
|
|
|
|
element.max = (SInt32)IOHIDElementGetLogicalMax(p_element);
|
2016-09-06 00:47:54 +02:00
|
|
|
element.cookie = IOHIDElementGetCookie(p_element);
|
|
|
|
list->push_back(element);
|
|
|
|
list->sort_custom<rec_element::Comparator>();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void hid_element_added(const void *p_value, void *p_parameter) {
|
2017-03-05 16:44:50 +01:00
|
|
|
joypad *joy = (joypad *)p_parameter;
|
|
|
|
joy->add_hid_element((IOHIDElementRef)p_value);
|
2016-09-06 00:47:54 +02:00
|
|
|
}
|
|
|
|
|
2017-01-08 21:05:51 +01:00
|
|
|
void joypad::add_hid_elements(CFArrayRef p_array) {
|
2016-09-06 00:47:54 +02:00
|
|
|
CFRange range = { 0, CFArrayGetCount(p_array) };
|
2017-03-05 16:44:50 +01:00
|
|
|
CFArrayApplyFunction(p_array, range, hid_element_added, this);
|
2016-09-06 00:47:54 +02:00
|
|
|
}
|
|
|
|
|
2020-03-06 10:57:58 +01:00
|
|
|
static void joypad_removed_callback(void *ctx, IOReturn res, void *sender, IOHIDDeviceRef ioHIDDeviceObject) {
|
|
|
|
self->_device_removed(res, ioHIDDeviceObject);
|
2016-09-06 00:47:54 +02:00
|
|
|
}
|
|
|
|
|
2017-01-08 21:05:51 +01:00
|
|
|
static void joypad_added_callback(void *ctx, IOReturn res, void *sender, IOHIDDeviceRef ioHIDDeviceObject) {
|
2016-09-06 00:47:54 +02:00
|
|
|
self->_device_added(res, ioHIDDeviceObject);
|
|
|
|
}
|
|
|
|
|
2017-01-08 21:05:51 +01:00
|
|
|
static bool is_joypad(IOHIDDeviceRef p_device_ref) {
|
2016-09-06 00:47:54 +02:00
|
|
|
int usage_page = 0;
|
|
|
|
int usage = 0;
|
2017-08-21 21:15:36 +02:00
|
|
|
CFTypeRef refCF = IOHIDDeviceGetProperty(p_device_ref, CFSTR(kIOHIDPrimaryUsagePageKey));
|
2016-09-06 00:47:54 +02:00
|
|
|
if (refCF) {
|
2017-03-05 16:44:50 +01:00
|
|
|
CFNumberGetValue((CFNumberRef)refCF, kCFNumberSInt32Type, &usage_page);
|
2016-09-06 00:47:54 +02:00
|
|
|
}
|
|
|
|
if (usage_page != kHIDPage_GenericDesktop) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
refCF = IOHIDDeviceGetProperty(p_device_ref, CFSTR(kIOHIDPrimaryUsageKey));
|
|
|
|
if (refCF) {
|
2017-03-05 16:44:50 +01:00
|
|
|
CFNumberGetValue((CFNumberRef)refCF, kCFNumberSInt32Type, &usage);
|
2016-09-06 00:47:54 +02:00
|
|
|
}
|
2017-01-09 10:05:50 +01:00
|
|
|
if ((usage != kHIDUsage_GD_Joystick &&
|
2017-03-05 16:44:50 +01:00
|
|
|
usage != kHIDUsage_GD_GamePad &&
|
|
|
|
usage != kHIDUsage_GD_MultiAxisController)) {
|
2016-09-06 00:47:54 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-01-08 21:05:51 +01:00
|
|
|
void JoypadOSX::_device_added(IOReturn p_res, IOHIDDeviceRef p_device) {
|
2016-09-06 00:47:54 +02:00
|
|
|
|
|
|
|
if (p_res != kIOReturnSuccess || have_device(p_device)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-01-08 21:05:51 +01:00
|
|
|
joypad new_joypad;
|
|
|
|
if (is_joypad(p_device)) {
|
|
|
|
configure_joypad(p_device, &new_joypad);
|
2016-09-06 00:47:54 +02:00
|
|
|
#if MAC_OS_X_VERSION_MIN_REQUIRED < 1060
|
|
|
|
if (IOHIDDeviceGetService != NULL) {
|
|
|
|
#endif
|
|
|
|
const io_service_t ioservice = IOHIDDeviceGetService(p_device);
|
2017-03-05 16:44:50 +01:00
|
|
|
if ((ioservice) && (FFIsForceFeedback(ioservice) == FF_OK) && new_joypad.config_force_feedback(ioservice)) {
|
2017-01-08 21:05:51 +01:00
|
|
|
new_joypad.ffservice = ioservice;
|
2016-09-06 00:47:54 +02:00
|
|
|
}
|
|
|
|
#if MAC_OS_X_VERSION_MIN_REQUIRED < 1060
|
|
|
|
}
|
|
|
|
#endif
|
2017-01-08 21:05:51 +01:00
|
|
|
device_list.push_back(new_joypad);
|
2016-09-06 00:47:54 +02:00
|
|
|
}
|
|
|
|
IOHIDDeviceScheduleWithRunLoop(p_device, CFRunLoopGetCurrent(), GODOT_JOY_LOOP_RUN_MODE);
|
|
|
|
}
|
|
|
|
|
2020-03-06 10:57:58 +01:00
|
|
|
void JoypadOSX::_device_removed(IOReturn p_res, IOHIDDeviceRef p_device) {
|
2016-09-06 00:47:54 +02:00
|
|
|
|
2020-03-06 10:57:58 +01:00
|
|
|
int device = get_joy_ref(p_device);
|
2016-09-06 00:47:54 +02:00
|
|
|
ERR_FAIL_COND(device == -1);
|
|
|
|
|
2020-03-06 10:57:58 +01:00
|
|
|
input->joy_connection_changed(device_list[device].id, false, "");
|
2018-07-25 03:11:03 +02:00
|
|
|
device_list.write[device].free();
|
2016-09-06 00:47:54 +02:00
|
|
|
device_list.remove(device);
|
|
|
|
}
|
|
|
|
|
|
|
|
static String _hex_str(uint8_t p_byte) {
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
static const char *dict = "0123456789abcdef";
|
2016-09-06 00:47:54 +02:00
|
|
|
char ret[3];
|
|
|
|
ret[2] = 0;
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
ret[0] = dict[p_byte >> 4];
|
2016-09-06 00:47:54 +02:00
|
|
|
ret[1] = dict[p_byte & 0xF];
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
bool JoypadOSX::configure_joypad(IOHIDDeviceRef p_device_ref, joypad *p_joy) {
|
2016-09-06 00:47:54 +02:00
|
|
|
|
|
|
|
p_joy->device_ref = p_device_ref;
|
|
|
|
/* get device name */
|
|
|
|
String name;
|
|
|
|
char c_name[256];
|
2017-08-21 21:15:36 +02:00
|
|
|
CFTypeRef refCF = IOHIDDeviceGetProperty(p_device_ref, CFSTR(kIOHIDProductKey));
|
2016-09-06 00:47:54 +02:00
|
|
|
if (!refCF) {
|
|
|
|
refCF = IOHIDDeviceGetProperty(p_device_ref, CFSTR(kIOHIDManufacturerKey));
|
|
|
|
}
|
2017-03-05 16:44:50 +01:00
|
|
|
if ((!refCF) || (!CFStringGetCString((CFStringRef)refCF, c_name, sizeof(c_name), kCFStringEncodingUTF8))) {
|
2017-01-08 21:05:51 +01:00
|
|
|
name = "Unidentified Joypad";
|
2016-09-06 00:47:54 +02:00
|
|
|
}
|
|
|
|
name = c_name;
|
|
|
|
|
2017-02-21 17:02:49 +01:00
|
|
|
int id = input->get_unused_joy_id();
|
2016-09-06 00:47:54 +02:00
|
|
|
ERR_FAIL_COND_V(id == -1, false);
|
|
|
|
p_joy->id = id;
|
|
|
|
int vendor = 0;
|
|
|
|
refCF = IOHIDDeviceGetProperty(p_device_ref, CFSTR(kIOHIDVendorIDKey));
|
|
|
|
if (refCF) {
|
|
|
|
CFNumberGetValue((CFNumberRef)refCF, kCFNumberSInt32Type, &vendor);
|
|
|
|
}
|
|
|
|
|
|
|
|
int product_id = 0;
|
|
|
|
refCF = IOHIDDeviceGetProperty(p_device_ref, CFSTR(kIOHIDProductIDKey));
|
|
|
|
if (refCF) {
|
|
|
|
CFNumberGetValue((CFNumberRef)refCF, kCFNumberSInt32Type, &product_id);
|
|
|
|
}
|
2020-05-30 17:02:19 +02:00
|
|
|
|
|
|
|
int version = 0;
|
|
|
|
refCF = IOHIDDeviceGetProperty(p_device_ref, CFSTR(kIOHIDVersionNumberKey));
|
|
|
|
if (refCF) {
|
|
|
|
CFNumberGetValue((CFNumberRef)refCF, kCFNumberSInt32Type, &version);
|
|
|
|
}
|
|
|
|
|
2016-09-06 00:47:54 +02:00
|
|
|
if (vendor && product_id) {
|
|
|
|
char uid[128];
|
2020-05-30 17:02:19 +02:00
|
|
|
sprintf(uid, "%08x%08x%08x%08x", OSSwapHostToBigInt32(3), OSSwapHostToBigInt32(vendor), OSSwapHostToBigInt32(product_id), OSSwapHostToBigInt32(version));
|
2016-09-06 00:47:54 +02:00
|
|
|
input->joy_connection_changed(id, true, name, uid);
|
2017-03-05 16:44:50 +01:00
|
|
|
} else {
|
2016-09-06 00:47:54 +02:00
|
|
|
//bluetooth device
|
|
|
|
String guid = "05000000";
|
|
|
|
for (int i = 0; i < 12; i++) {
|
2017-03-05 16:44:50 +01:00
|
|
|
if (i < name.size())
|
|
|
|
guid += _hex_str(name[i]);
|
|
|
|
else
|
|
|
|
guid += "00";
|
2016-09-06 00:47:54 +02:00
|
|
|
}
|
|
|
|
input->joy_connection_changed(id, true, name, guid);
|
|
|
|
}
|
|
|
|
|
2017-08-21 21:15:36 +02:00
|
|
|
CFArrayRef array = IOHIDDeviceCopyMatchingElements(p_device_ref, NULL, kIOHIDOptionsTypeNone);
|
2016-09-06 00:47:54 +02:00
|
|
|
if (array) {
|
|
|
|
p_joy->add_hid_elements(array);
|
|
|
|
CFRelease(array);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
#define FF_ERR() \
|
|
|
|
{ \
|
|
|
|
if (ret != FF_OK) { \
|
|
|
|
FFReleaseDevice(ff_device); \
|
|
|
|
return false; \
|
|
|
|
} \
|
|
|
|
}
|
2017-01-08 21:05:51 +01:00
|
|
|
bool joypad::config_force_feedback(io_service_t p_service) {
|
2016-09-06 00:47:54 +02:00
|
|
|
|
|
|
|
HRESULT ret = FFCreateDevice(p_service, &ff_device);
|
|
|
|
ERR_FAIL_COND_V(ret != FF_OK, false);
|
|
|
|
|
|
|
|
ret = FFDeviceSendForceFeedbackCommand(ff_device, FFSFFC_RESET);
|
|
|
|
FF_ERR();
|
|
|
|
|
|
|
|
ret = FFDeviceSendForceFeedbackCommand(ff_device, FFSFFC_SETACTUATORSON);
|
|
|
|
FF_ERR();
|
|
|
|
|
|
|
|
if (check_ff_features()) {
|
|
|
|
ret = FFDeviceCreateEffect(ff_device, kFFEffectType_ConstantForce_ID, &ff_effect, &ff_object);
|
|
|
|
FF_ERR();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
FFReleaseDevice(ff_device);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
#undef FF_ERR
|
|
|
|
|
|
|
|
#define TEST_FF(ff) (features.supportedEffects & (ff))
|
2017-01-08 21:05:51 +01:00
|
|
|
bool joypad::check_ff_features() {
|
2016-09-06 00:47:54 +02:00
|
|
|
|
|
|
|
FFCAPABILITIES features;
|
|
|
|
HRESULT ret = FFDeviceGetForceFeedbackCapabilities(ff_device, &features);
|
|
|
|
if (ret == FF_OK && (features.supportedEffects & FFCAP_ET_CONSTANTFORCE)) {
|
|
|
|
uint32_t val;
|
|
|
|
ret = FFDeviceGetForceFeedbackProperty(ff_device, FFPROP_FFGAIN, &val, sizeof(val));
|
|
|
|
if (ret != FF_OK) return false;
|
|
|
|
int num_axes = features.numFfAxes;
|
2017-03-05 16:44:50 +01:00
|
|
|
ff_axes = (DWORD *)memalloc(sizeof(DWORD) * num_axes);
|
|
|
|
ff_directions = (LONG *)memalloc(sizeof(LONG) * num_axes);
|
2016-09-06 00:47:54 +02:00
|
|
|
|
|
|
|
for (int i = 0; i < num_axes; i++) {
|
|
|
|
ff_axes[i] = features.ffAxes[i];
|
|
|
|
ff_directions[i] = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
ff_effect.cAxes = num_axes;
|
|
|
|
ff_effect.rgdwAxes = ff_axes;
|
|
|
|
ff_effect.rglDirection = ff_directions;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int process_hat_value(int p_min, int p_max, int p_value) {
|
|
|
|
int range = (p_max - p_min + 1);
|
|
|
|
int value = p_value - p_min;
|
|
|
|
int hat_value = InputDefault::HAT_MASK_CENTER;
|
|
|
|
if (range == 4) {
|
|
|
|
value *= 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (value) {
|
2017-03-05 16:44:50 +01:00
|
|
|
case 0:
|
|
|
|
hat_value = InputDefault::HAT_MASK_UP;
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
hat_value = InputDefault::HAT_MASK_UP | InputDefault::HAT_MASK_RIGHT;
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
hat_value = InputDefault::HAT_MASK_RIGHT;
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
hat_value = InputDefault::HAT_MASK_DOWN | InputDefault::HAT_MASK_RIGHT;
|
|
|
|
break;
|
|
|
|
case 4:
|
|
|
|
hat_value = InputDefault::HAT_MASK_DOWN;
|
|
|
|
break;
|
|
|
|
case 5:
|
|
|
|
hat_value = InputDefault::HAT_MASK_DOWN | InputDefault::HAT_MASK_LEFT;
|
|
|
|
break;
|
|
|
|
case 6:
|
|
|
|
hat_value = InputDefault::HAT_MASK_LEFT;
|
|
|
|
break;
|
|
|
|
case 7:
|
|
|
|
hat_value = InputDefault::HAT_MASK_UP | InputDefault::HAT_MASK_LEFT;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
hat_value = InputDefault::HAT_MASK_CENTER;
|
|
|
|
break;
|
2016-09-06 00:47:54 +02:00
|
|
|
}
|
|
|
|
return hat_value;
|
|
|
|
}
|
|
|
|
|
2017-01-08 21:05:51 +01:00
|
|
|
void JoypadOSX::poll_joypads() const {
|
2017-03-05 16:44:50 +01:00
|
|
|
while (CFRunLoopRunInMode(GODOT_JOY_LOOP_RUN_MODE, 0, TRUE) == kCFRunLoopRunHandledSource) {
|
2016-09-06 00:47:54 +02:00
|
|
|
/* no-op. Pending callbacks will fire. */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static const InputDefault::JoyAxis axis_correct(int p_value, int p_min, int p_max) {
|
|
|
|
InputDefault::JoyAxis jx;
|
|
|
|
if (p_min < 0) {
|
|
|
|
jx.min = -1;
|
|
|
|
if (p_value < 0) {
|
2017-03-05 16:44:50 +01:00
|
|
|
jx.value = (float)-p_value / p_min;
|
|
|
|
} else
|
|
|
|
jx.value = (float)p_value / p_max;
|
2016-09-06 00:47:54 +02:00
|
|
|
}
|
|
|
|
if (p_min == 0) {
|
|
|
|
jx.min = 0;
|
2017-03-05 16:44:50 +01:00
|
|
|
jx.value = 0.0f + (float)p_value / p_max;
|
2016-09-06 00:47:54 +02:00
|
|
|
}
|
|
|
|
return jx;
|
|
|
|
}
|
|
|
|
|
2017-03-26 15:59:13 +02:00
|
|
|
void JoypadOSX::process_joypads() {
|
2017-01-08 21:05:51 +01:00
|
|
|
poll_joypads();
|
2016-09-06 00:47:54 +02:00
|
|
|
|
|
|
|
for (int i = 0; i < device_list.size(); i++) {
|
2018-07-25 03:11:03 +02:00
|
|
|
joypad &joy = device_list.write[i];
|
2016-09-06 00:47:54 +02:00
|
|
|
|
|
|
|
for (int j = 0; j < joy.axis_elements.size(); j++) {
|
2018-07-25 03:11:03 +02:00
|
|
|
rec_element &elem = joy.axis_elements.write[j];
|
2016-09-06 00:47:54 +02:00
|
|
|
int value = joy.get_hid_element_state(&elem);
|
2017-03-26 15:59:13 +02:00
|
|
|
input->joy_axis(joy.id, j, axis_correct(value, elem.min, elem.max));
|
2016-09-06 00:47:54 +02:00
|
|
|
}
|
|
|
|
for (int j = 0; j < joy.button_elements.size(); j++) {
|
2018-07-25 03:11:03 +02:00
|
|
|
int value = joy.get_hid_element_state(&joy.button_elements.write[j]);
|
2017-03-26 15:59:13 +02:00
|
|
|
input->joy_button(joy.id, j, (value >= 1));
|
2016-09-06 00:47:54 +02:00
|
|
|
}
|
|
|
|
for (int j = 0; j < joy.hat_elements.size(); j++) {
|
2018-07-25 03:11:03 +02:00
|
|
|
rec_element &elem = joy.hat_elements.write[j];
|
2016-09-06 00:47:54 +02:00
|
|
|
int value = joy.get_hid_element_state(&elem);
|
|
|
|
int hat_value = process_hat_value(elem.min, elem.max, value);
|
2017-03-26 15:59:13 +02:00
|
|
|
input->joy_hat(joy.id, hat_value);
|
2016-09-06 00:47:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (joy.ffservice) {
|
|
|
|
uint64_t timestamp = input->get_joy_vibration_timestamp(joy.id);
|
|
|
|
if (timestamp > joy.ff_timestamp) {
|
|
|
|
Vector2 strength = input->get_joy_vibration_strength(joy.id);
|
|
|
|
float duration = input->get_joy_vibration_duration(joy.id);
|
|
|
|
if (strength.x == 0 && strength.y == 0) {
|
2017-01-08 21:05:51 +01:00
|
|
|
joypad_vibration_stop(joy.id, timestamp);
|
2017-03-05 16:44:50 +01:00
|
|
|
} else {
|
2016-09-06 00:47:54 +02:00
|
|
|
float gain = MAX(strength.x, strength.y);
|
2017-01-08 21:05:51 +01:00
|
|
|
joypad_vibration_start(joy.id, gain, duration, timestamp);
|
2016-09-06 00:47:54 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-08 21:05:51 +01:00
|
|
|
void JoypadOSX::joypad_vibration_start(int p_id, float p_magnitude, float p_duration, uint64_t p_timestamp) {
|
2018-07-25 03:11:03 +02:00
|
|
|
joypad *joy = &device_list.write[get_joy_index(p_id)];
|
2016-09-06 00:47:54 +02:00
|
|
|
joy->ff_timestamp = p_timestamp;
|
|
|
|
joy->ff_effect.dwDuration = p_duration * FF_SECONDS;
|
|
|
|
joy->ff_effect.dwGain = p_magnitude * FF_FFNOMINALMAX;
|
|
|
|
FFEffectSetParameters(joy->ff_object, &joy->ff_effect, FFEP_DURATION | FFEP_GAIN);
|
|
|
|
FFEffectStart(joy->ff_object, 1, 0);
|
|
|
|
}
|
|
|
|
|
2017-01-08 21:05:51 +01:00
|
|
|
void JoypadOSX::joypad_vibration_stop(int p_id, uint64_t p_timestamp) {
|
2018-07-25 03:11:03 +02:00
|
|
|
joypad *joy = &device_list.write[get_joy_index(p_id)];
|
2016-09-06 00:47:54 +02:00
|
|
|
joy->ff_timestamp = p_timestamp;
|
|
|
|
FFEffectStop(joy->ff_object);
|
|
|
|
}
|
|
|
|
|
2017-01-08 21:05:51 +01:00
|
|
|
int JoypadOSX::get_joy_index(int p_id) const {
|
2016-09-06 00:47:54 +02:00
|
|
|
for (int i = 0; i < device_list.size(); i++) {
|
|
|
|
if (device_list[i].id == p_id) return i;
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2020-03-06 10:57:58 +01:00
|
|
|
int JoypadOSX::get_joy_ref(IOHIDDeviceRef p_device) const {
|
|
|
|
for (int i = 0; i < device_list.size(); i++) {
|
|
|
|
if (device_list[i].device_ref == p_device) return i;
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2017-01-08 21:05:51 +01:00
|
|
|
bool JoypadOSX::have_device(IOHIDDeviceRef p_device) const {
|
2016-09-06 00:47:54 +02:00
|
|
|
for (int i = 0; i < device_list.size(); i++) {
|
|
|
|
if (device_list[i].device_ref == p_device) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
static CFDictionaryRef create_match_dictionary(const UInt32 page, const UInt32 usage, int *okay) {
|
2016-09-06 00:47:54 +02:00
|
|
|
CFDictionaryRef retval = NULL;
|
|
|
|
CFNumberRef pageNumRef = CFNumberCreate(kCFAllocatorDefault, kCFNumberIntType, &page);
|
|
|
|
CFNumberRef usageNumRef = CFNumberCreate(kCFAllocatorDefault, kCFNumberIntType, &usage);
|
2017-03-05 16:44:50 +01:00
|
|
|
const void *keys[2] = { (void *)CFSTR(kIOHIDDeviceUsagePageKey), (void *)CFSTR(kIOHIDDeviceUsageKey) };
|
|
|
|
const void *vals[2] = { (void *)pageNumRef, (void *)usageNumRef };
|
2016-09-06 00:47:54 +02:00
|
|
|
|
|
|
|
if (pageNumRef && usageNumRef) {
|
|
|
|
retval = CFDictionaryCreate(kCFAllocatorDefault, keys, vals, 2, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pageNumRef) {
|
|
|
|
CFRelease(pageNumRef);
|
|
|
|
}
|
|
|
|
if (usageNumRef) {
|
|
|
|
CFRelease(usageNumRef);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!retval) {
|
|
|
|
*okay = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
2017-01-08 21:05:51 +01:00
|
|
|
void JoypadOSX::config_hid_manager(CFArrayRef p_matching_array) const {
|
2016-09-06 00:47:54 +02:00
|
|
|
|
|
|
|
CFRunLoopRef runloop = CFRunLoopGetCurrent();
|
|
|
|
IOReturn ret = IOHIDManagerOpen(hid_manager, kIOHIDOptionsTypeNone);
|
|
|
|
ERR_FAIL_COND(ret != kIOReturnSuccess);
|
|
|
|
|
|
|
|
IOHIDManagerSetDeviceMatchingMultiple(hid_manager, p_matching_array);
|
2017-01-08 21:05:51 +01:00
|
|
|
IOHIDManagerRegisterDeviceMatchingCallback(hid_manager, joypad_added_callback, NULL);
|
2020-03-06 10:57:58 +01:00
|
|
|
IOHIDManagerRegisterDeviceRemovalCallback(hid_manager, joypad_removed_callback, NULL);
|
2016-09-06 00:47:54 +02:00
|
|
|
IOHIDManagerScheduleWithRunLoop(hid_manager, runloop, GODOT_JOY_LOOP_RUN_MODE);
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
while (CFRunLoopRunInMode(GODOT_JOY_LOOP_RUN_MODE, 0, TRUE) == kCFRunLoopRunHandledSource) {
|
2016-09-06 00:47:54 +02:00
|
|
|
/* no-op. Callback fires once per existing device. */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
JoypadOSX::JoypadOSX() {
|
2016-09-06 00:47:54 +02:00
|
|
|
self = this;
|
2017-03-05 16:44:50 +01:00
|
|
|
input = (InputDefault *)Input::get_singleton();
|
2016-09-06 00:47:54 +02:00
|
|
|
|
|
|
|
int okay = 1;
|
|
|
|
const void *vals[] = {
|
2017-03-05 16:44:50 +01:00
|
|
|
(void *)create_match_dictionary(kHIDPage_GenericDesktop, kHIDUsage_GD_Joystick, &okay),
|
|
|
|
(void *)create_match_dictionary(kHIDPage_GenericDesktop, kHIDUsage_GD_GamePad, &okay),
|
|
|
|
(void *)create_match_dictionary(kHIDPage_GenericDesktop, kHIDUsage_GD_MultiAxisController, &okay),
|
2016-09-06 00:47:54 +02:00
|
|
|
};
|
2017-03-05 16:44:50 +01:00
|
|
|
const size_t n_elements = sizeof(vals) / sizeof(vals[0]);
|
2016-09-06 00:47:54 +02:00
|
|
|
CFArrayRef array = okay ? CFArrayCreate(kCFAllocatorDefault, vals, n_elements, &kCFTypeArrayCallBacks) : NULL;
|
|
|
|
|
2019-10-24 16:34:18 +02:00
|
|
|
for (size_t i = 0; i < n_elements; i++) {
|
2016-09-06 00:47:54 +02:00
|
|
|
if (vals[i]) {
|
2017-03-05 16:44:50 +01:00
|
|
|
CFRelease((CFTypeRef)vals[i]);
|
2016-09-06 00:47:54 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (array) {
|
|
|
|
hid_manager = IOHIDManagerCreate(kCFAllocatorDefault, kIOHIDOptionsTypeNone);
|
|
|
|
if (hid_manager != NULL) {
|
|
|
|
config_hid_manager(array);
|
|
|
|
}
|
|
|
|
CFRelease(array);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-08 21:05:51 +01:00
|
|
|
JoypadOSX::~JoypadOSX() {
|
2016-09-06 00:47:54 +02:00
|
|
|
|
|
|
|
for (int i = 0; i < device_list.size(); i++) {
|
2018-07-25 03:11:03 +02:00
|
|
|
device_list.write[i].free();
|
2016-09-06 00:47:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
IOHIDManagerUnscheduleFromRunLoop(hid_manager, CFRunLoopGetCurrent(), GODOT_JOY_LOOP_RUN_MODE);
|
|
|
|
IOHIDManagerClose(hid_manager, kIOHIDOptionsTypeNone);
|
|
|
|
CFRelease(hid_manager);
|
|
|
|
hid_manager = NULL;
|
|
|
|
}
|