2015-03-31 14:44:51 +02:00
|
|
|
/* RetroArch - A frontend for libretro.
|
|
|
|
* Copyright (C) 2013-2014 - Jason Fetters
|
|
|
|
* Copyright (C) 2011-2015 - Daniel De Matteis
|
|
|
|
*
|
|
|
|
* RetroArch is free software: you can redistribute it and/or modify it under the terms
|
|
|
|
* of the GNU General Public License as published by the Free Software Found-
|
|
|
|
* ation, either version 3 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* RetroArch is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
|
|
|
|
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
|
|
|
|
* PURPOSE. See the GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along with RetroArch.
|
|
|
|
* If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <IOKit/hid/IOHIDManager.h>
|
|
|
|
#include <IOKit/hid/IOHIDKeys.h>
|
2015-03-31 15:14:39 +02:00
|
|
|
#include "../connect/joypad_connection.h"
|
|
|
|
#include "../drivers/apple_input.h"
|
2015-03-31 14:44:51 +02:00
|
|
|
|
2015-03-31 15:14:39 +02:00
|
|
|
typedef struct apple_hid
|
|
|
|
{
|
|
|
|
IOHIDManagerRef hid_ptr;
|
|
|
|
joypad_connection_t *slots;
|
|
|
|
} apple_hid_t;
|
|
|
|
|
|
|
|
static apple_hid_t *hid_apple;
|
2015-03-31 14:44:51 +02:00
|
|
|
|
|
|
|
struct pad_connection
|
|
|
|
{
|
|
|
|
uint32_t slot;
|
|
|
|
IOHIDDeviceRef device_handle;
|
2015-03-31 16:35:20 +02:00
|
|
|
char device_name[PATH_MAX_LENGTH];
|
2015-03-31 14:44:51 +02:00
|
|
|
uint8_t data[2048];
|
|
|
|
};
|
|
|
|
|
2015-03-31 15:14:39 +02:00
|
|
|
static bool apple_hid_joypad_button(unsigned port, uint16_t joykey)
|
|
|
|
{
|
2015-03-31 16:03:06 +02:00
|
|
|
driver_t *driver = driver_get_ptr();
|
2015-03-31 15:14:39 +02:00
|
|
|
apple_input_data_t *apple = (apple_input_data_t*)driver->input_data;
|
|
|
|
uint64_t buttons = pad_connection_get_buttons(&hid_apple->slots[port], port);
|
|
|
|
|
|
|
|
if (!apple || joykey == NO_BTN)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
/* Check hat. */
|
|
|
|
if (GET_HAT_DIR(joykey))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
/* Check the button. */
|
|
|
|
if ((port < MAX_USERS) && (joykey < 32))
|
|
|
|
return ((apple->buttons[port] & (1 << joykey)) != 0) ||
|
|
|
|
((buttons & (1 << joykey)) != 0);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
static uint64_t apple_hid_joypad_get_buttons(unsigned port)
|
|
|
|
{
|
|
|
|
return pad_connection_get_buttons(&hid_apple->slots[port], port);
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool apple_hid_joypad_rumble(unsigned pad,
|
|
|
|
enum retro_rumble_effect effect, uint16_t strength)
|
|
|
|
{
|
|
|
|
return pad_connection_rumble(&hid_apple->slots[pad], pad, effect, strength);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int16_t apple_hid_joypad_axis(unsigned port, uint32_t joyaxis)
|
|
|
|
{
|
2015-03-31 16:03:06 +02:00
|
|
|
driver_t *driver = driver_get_ptr();
|
2015-03-31 15:14:39 +02:00
|
|
|
apple_input_data_t *apple = (apple_input_data_t*)driver->input_data;
|
|
|
|
int16_t val = 0;
|
|
|
|
|
|
|
|
if (!apple || joyaxis == AXIS_NONE)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (AXIS_NEG_GET(joyaxis) < 4)
|
|
|
|
{
|
|
|
|
val = apple->axes[port][AXIS_NEG_GET(joyaxis)];
|
|
|
|
val += pad_connection_get_axis(&hid_apple->slots[port], port, AXIS_NEG_GET(joyaxis));
|
|
|
|
|
|
|
|
if (val >= 0)
|
|
|
|
val = 0;
|
|
|
|
}
|
|
|
|
else if(AXIS_POS_GET(joyaxis) < 4)
|
|
|
|
{
|
|
|
|
val = apple->axes[port][AXIS_POS_GET(joyaxis)];
|
|
|
|
val += pad_connection_get_axis(&hid_apple->slots[port], port, AXIS_POS_GET(joyaxis));
|
|
|
|
|
|
|
|
if (val <= 0)
|
|
|
|
val = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return val;
|
|
|
|
}
|
2015-03-31 14:46:44 +02:00
|
|
|
|
2015-03-31 14:44:51 +02:00
|
|
|
static void hid_pad_connection_send_control(void *data, uint8_t* data_buf, size_t size)
|
|
|
|
{
|
2015-03-31 16:03:06 +02:00
|
|
|
struct pad_connection *connection = (struct pad_connection*)data;
|
2015-03-31 14:44:51 +02:00
|
|
|
|
|
|
|
if (connection)
|
|
|
|
IOHIDDeviceSetReport(connection->device_handle,
|
|
|
|
kIOHIDReportTypeOutput, 0x01, data_buf + 1, size - 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* NOTE: I pieced this together through trial and error,
|
|
|
|
* any corrections are welcome. */
|
|
|
|
|
|
|
|
static void hid_device_input_callback(void* context, IOReturn result,
|
|
|
|
void* sender, IOHIDValueRef value)
|
|
|
|
{
|
2015-03-31 16:03:06 +02:00
|
|
|
driver_t *driver = driver_get_ptr();
|
|
|
|
apple_input_data_t *apple = (apple_input_data_t*)driver->input_data;
|
|
|
|
struct pad_connection *connection = (struct pad_connection*)context;
|
2015-03-31 14:44:51 +02:00
|
|
|
IOHIDElementRef element = IOHIDValueGetElement(value);
|
|
|
|
uint32_t type = IOHIDElementGetType(element);
|
|
|
|
uint32_t page = IOHIDElementGetUsagePage(element);
|
|
|
|
uint32_t use = IOHIDElementGetUsage(element);
|
|
|
|
|
|
|
|
if (type != kIOHIDElementTypeInput_Misc)
|
|
|
|
if (type != kIOHIDElementTypeInput_Button)
|
|
|
|
if (type != kIOHIDElementTypeInput_Axis)
|
|
|
|
return;
|
|
|
|
|
|
|
|
/* Joystick handler.
|
|
|
|
* TODO: Can GamePad work the same? */
|
|
|
|
|
|
|
|
switch (page)
|
|
|
|
{
|
|
|
|
case kHIDPage_GenericDesktop:
|
|
|
|
switch (type)
|
|
|
|
{
|
|
|
|
case kIOHIDElementTypeInput_Misc:
|
|
|
|
switch (use)
|
|
|
|
{
|
|
|
|
case kHIDUsage_GD_Hatswitch:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
static const uint32_t axis_use_ids[4] = { 48, 49, 50, 53 };
|
|
|
|
|
|
|
|
for (i = 0; i < 4; i ++)
|
|
|
|
{
|
|
|
|
CFIndex min = IOHIDElementGetPhysicalMin(element);
|
|
|
|
CFIndex max = IOHIDElementGetPhysicalMax(element) - min;
|
|
|
|
CFIndex state = IOHIDValueGetIntegerValue(value) - min;
|
|
|
|
float val = (float)state / (float)max;
|
|
|
|
|
|
|
|
if (use != axis_use_ids[i])
|
|
|
|
continue;
|
|
|
|
|
|
|
|
apple->axes[connection->slot][i] =
|
|
|
|
((val * 2.0f) - 1.0f) * 32767.0f;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case kHIDPage_Button:
|
|
|
|
switch (type)
|
|
|
|
{
|
|
|
|
case kIOHIDElementTypeInput_Button:
|
|
|
|
{
|
|
|
|
CFIndex state = IOHIDValueGetIntegerValue(value);
|
|
|
|
unsigned id = use - 1;
|
|
|
|
|
|
|
|
if (state)
|
|
|
|
BIT64_SET(apple->buttons[connection->slot], id);
|
|
|
|
else
|
|
|
|
BIT64_CLEAR(apple->buttons[connection->slot], id);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void remove_device(void* context, IOReturn result, void* sender)
|
|
|
|
{
|
2015-03-31 16:03:06 +02:00
|
|
|
driver_t *driver = driver_get_ptr();
|
|
|
|
apple_input_data_t *apple = (apple_input_data_t*)driver->input_data;
|
|
|
|
struct pad_connection *connection = (struct pad_connection*)context;
|
2015-03-31 14:44:51 +02:00
|
|
|
|
|
|
|
if (connection && (connection->slot < MAX_USERS))
|
|
|
|
{
|
|
|
|
char msg[PATH_MAX_LENGTH];
|
|
|
|
|
|
|
|
snprintf(msg, sizeof(msg), "Joypad #%u (%s) disconnected.",
|
2015-03-31 16:35:20 +02:00
|
|
|
connection->slot, connection->device_name);
|
2015-03-31 14:44:51 +02:00
|
|
|
rarch_main_msg_queue_push(msg, 0, 60, false);
|
|
|
|
|
|
|
|
apple->buttons[connection->slot] = 0;
|
|
|
|
memset(apple->axes[connection->slot], 0, sizeof(apple->axes));
|
|
|
|
|
2015-03-31 15:14:39 +02:00
|
|
|
pad_connection_pad_deinit(&hid_apple->slots[connection->slot], connection->slot);
|
2015-03-31 14:44:51 +02:00
|
|
|
free(connection);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void hid_device_report(void* context, IOReturn result, void *sender,
|
|
|
|
IOHIDReportType type, uint32_t reportID, uint8_t *report,
|
|
|
|
CFIndex reportLength)
|
|
|
|
{
|
|
|
|
struct pad_connection* connection = (struct pad_connection*)context;
|
|
|
|
|
2015-03-31 16:03:06 +02:00
|
|
|
if (connection)
|
|
|
|
pad_connection_packet(&hid_apple->slots[connection->slot], connection->slot,
|
2015-03-31 14:44:51 +02:00
|
|
|
connection->data, reportLength + 1);
|
|
|
|
}
|
|
|
|
|
2015-03-31 16:00:11 +02:00
|
|
|
static int32_t apple_hid_get_int_property(IOHIDDeviceRef device, CFStringRef key)
|
|
|
|
{
|
|
|
|
int32_t value;
|
2015-03-31 16:28:46 +02:00
|
|
|
CFNumberRef ref = IOHIDDeviceGetProperty(device, key);
|
2015-03-31 16:00:11 +02:00
|
|
|
|
|
|
|
if (ref)
|
|
|
|
{
|
|
|
|
if (CFGetTypeID(ref) == CFNumberGetTypeID())
|
|
|
|
{
|
2015-03-31 16:28:46 +02:00
|
|
|
CFNumberGetValue((CFNumberRef)ref, kCFNumberIntType, &value);
|
2015-03-31 16:00:11 +02:00
|
|
|
return value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static uint16_t apple_hid_get_vendor_id(IOHIDDeviceRef device)
|
|
|
|
{
|
|
|
|
return apple_hid_get_int_property(device, CFSTR(kIOHIDVendorIDKey));
|
|
|
|
}
|
|
|
|
|
|
|
|
static uint16_t apple_hid_get_product_id(IOHIDDeviceRef device)
|
|
|
|
{
|
2015-03-31 16:28:46 +02:00
|
|
|
return apple_hid_get_int_property(device, CFSTR(kIOHIDProductIDKey));
|
2015-03-31 16:00:11 +02:00
|
|
|
}
|
|
|
|
|
2015-03-31 14:44:51 +02:00
|
|
|
static void add_device(void* context, IOReturn result,
|
|
|
|
void* sender, IOHIDDeviceRef device)
|
|
|
|
{
|
2015-03-31 16:00:11 +02:00
|
|
|
uint16_t dev_vid, dev_pid;
|
2015-03-31 14:44:51 +02:00
|
|
|
CFStringRef device_name_ref;
|
|
|
|
autoconfig_params_t params = {{0}};
|
|
|
|
settings_t *settings = config_get_ptr();
|
|
|
|
struct pad_connection* connection = (struct pad_connection*)
|
|
|
|
calloc(1, sizeof(*connection));
|
2015-03-31 16:00:11 +02:00
|
|
|
|
|
|
|
if (!connection)
|
|
|
|
return;
|
2015-03-31 14:44:51 +02:00
|
|
|
|
|
|
|
connection->device_handle = device;
|
|
|
|
connection->slot = MAX_USERS;
|
|
|
|
|
|
|
|
IOHIDDeviceOpen(device, kIOHIDOptionsTypeNone);
|
|
|
|
|
|
|
|
/* Move the device's run loop to this thread. */
|
|
|
|
IOHIDDeviceScheduleWithRunLoop(device, CFRunLoopGetCurrent(),
|
|
|
|
kCFRunLoopCommonModes);
|
|
|
|
IOHIDDeviceRegisterRemovalCallback(device, remove_device, connection);
|
|
|
|
|
|
|
|
#ifndef IOS
|
|
|
|
device_name_ref = IOHIDDeviceGetProperty(device, CFSTR(kIOHIDProductKey));
|
2015-03-31 16:35:20 +02:00
|
|
|
CFStringGetCString(device_name_ref, connection->device_name,
|
|
|
|
sizeof(connection->device_name), kCFStringEncodingUTF8);
|
2015-03-31 14:44:51 +02:00
|
|
|
#endif
|
|
|
|
|
2015-03-31 16:00:11 +02:00
|
|
|
dev_vid = apple_hid_get_vendor_id (device);
|
|
|
|
dev_pid = apple_hid_get_product_id (device);
|
2015-03-31 14:44:51 +02:00
|
|
|
|
2015-03-31 16:35:20 +02:00
|
|
|
connection->slot = pad_connection_pad_init(hid_apple->slots,
|
|
|
|
connection->device_name,
|
2015-03-31 14:44:51 +02:00
|
|
|
connection, &hid_pad_connection_send_control);
|
|
|
|
|
2015-03-31 15:14:39 +02:00
|
|
|
if (pad_connection_has_interface(hid_apple->slots, connection->slot))
|
2015-03-31 14:44:51 +02:00
|
|
|
IOHIDDeviceRegisterInputReportCallback(device,
|
|
|
|
connection->data + 1, sizeof(connection->data) - 1,
|
|
|
|
hid_device_report, connection);
|
|
|
|
else
|
|
|
|
IOHIDDeviceRegisterInputValueCallback(device,
|
|
|
|
hid_device_input_callback, connection);
|
|
|
|
|
2015-03-31 16:35:20 +02:00
|
|
|
if (connection->device_name[0] == '\0')
|
2015-03-31 14:44:51 +02:00
|
|
|
return;
|
|
|
|
|
|
|
|
strlcpy(settings->input.device_names[connection->slot],
|
2015-03-31 16:35:20 +02:00
|
|
|
connection->device_name, sizeof(settings->input.device_names));
|
2015-03-31 14:44:51 +02:00
|
|
|
|
|
|
|
params.idx = connection->slot;
|
2015-03-31 16:00:11 +02:00
|
|
|
params.vid = dev_vid;
|
|
|
|
params.pid = dev_pid;
|
2015-03-31 16:35:20 +02:00
|
|
|
strlcpy(params.name, connection->device_name, sizeof(params.name));
|
2015-03-31 14:44:51 +02:00
|
|
|
strlcpy(params.driver, apple_hid_joypad.ident, sizeof(params.driver));
|
|
|
|
|
|
|
|
input_config_autoconfigure_joypad(¶ms);
|
2015-03-31 16:35:20 +02:00
|
|
|
RARCH_LOG("Port %d: %s.\n", connection->slot, connection->device_name);
|
2015-03-31 14:44:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void append_matching_dictionary(CFMutableArrayRef array,
|
|
|
|
uint32_t page, uint32_t use)
|
|
|
|
{
|
|
|
|
CFNumberRef usen, pagen;
|
|
|
|
CFMutableDictionaryRef matcher = CFDictionaryCreateMutable(kCFAllocatorDefault, 0,
|
|
|
|
&kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
|
|
|
|
|
|
|
|
pagen = CFNumberCreate(kCFAllocatorDefault, kCFNumberIntType, &page);
|
|
|
|
CFDictionarySetValue(matcher, CFSTR(kIOHIDDeviceUsagePageKey), pagen);
|
|
|
|
CFRelease(pagen);
|
|
|
|
|
|
|
|
usen = CFNumberCreate(kCFAllocatorDefault, kCFNumberIntType, &use);
|
|
|
|
CFDictionarySetValue(matcher, CFSTR(kIOHIDDeviceUsageKey), usen);
|
|
|
|
CFRelease(usen);
|
|
|
|
|
|
|
|
CFArrayAppendValue(array, matcher);
|
|
|
|
CFRelease(matcher);
|
|
|
|
}
|
|
|
|
|
2015-03-31 16:51:04 +02:00
|
|
|
static int apple_hid_manager_init(apple_hid_t *hid)
|
|
|
|
{
|
|
|
|
if (!hid)
|
|
|
|
return -1;
|
|
|
|
if (hid->hid_ptr) /* already initialized. */
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
hid->hid_ptr = IOHIDManagerCreate(kCFAllocatorDefault, kIOHIDOptionsTypeNone);
|
|
|
|
|
|
|
|
if (hid->hid_ptr)
|
|
|
|
{
|
|
|
|
IOHIDManagerSetDeviceMatching(hid->hid_ptr, NULL);
|
|
|
|
IOHIDManagerScheduleWithRunLoop(hid->hid_ptr, CFRunLoopGetCurrent(),
|
|
|
|
kCFRunLoopDefaultMode);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int apple_hid_manager_exit(apple_hid_t *hid)
|
|
|
|
{
|
|
|
|
if (!hid)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (hid->hid_ptr)
|
|
|
|
{
|
|
|
|
IOHIDManagerClose(hid->hid_ptr, kIOHIDOptionsTypeNone);
|
|
|
|
CFRelease(hid->hid_ptr);
|
|
|
|
hid->hid_ptr = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-03-31 14:44:51 +02:00
|
|
|
static bool apple_hid_init(void)
|
|
|
|
{
|
|
|
|
CFMutableArrayRef matcher;
|
|
|
|
|
2015-03-31 15:14:39 +02:00
|
|
|
hid_apple = (apple_hid_t*)calloc(1, sizeof(*hid_apple));
|
|
|
|
|
|
|
|
if (!hid_apple)
|
|
|
|
goto error;
|
2015-03-31 16:51:04 +02:00
|
|
|
if (apple_hid_manager_init(hid_apple) == -1)
|
|
|
|
goto error;
|
2015-03-31 15:44:19 +02:00
|
|
|
|
2015-03-31 14:44:51 +02:00
|
|
|
matcher = CFArrayCreateMutable(kCFAllocatorDefault, 0,
|
|
|
|
&kCFTypeArrayCallBacks);
|
|
|
|
|
|
|
|
append_matching_dictionary(matcher, kHIDPage_GenericDesktop,
|
|
|
|
kHIDUsage_GD_Joystick);
|
|
|
|
append_matching_dictionary(matcher, kHIDPage_GenericDesktop,
|
|
|
|
kHIDUsage_GD_GamePad);
|
|
|
|
|
2015-03-31 15:14:39 +02:00
|
|
|
IOHIDManagerSetDeviceMatchingMultiple(hid_apple->hid_ptr, matcher);
|
2015-03-31 14:44:51 +02:00
|
|
|
CFRelease(matcher);
|
|
|
|
|
2015-03-31 15:14:39 +02:00
|
|
|
IOHIDManagerRegisterDeviceMatchingCallback(hid_apple->hid_ptr,
|
2015-03-31 14:44:51 +02:00
|
|
|
add_device, 0);
|
2015-03-31 15:14:39 +02:00
|
|
|
|
|
|
|
hid_apple->slots = (joypad_connection_t*)pad_connection_init(MAX_USERS);
|
2015-03-31 14:44:51 +02:00
|
|
|
|
|
|
|
return true;
|
2015-03-31 15:14:39 +02:00
|
|
|
|
|
|
|
error:
|
|
|
|
if (hid_apple)
|
|
|
|
free(hid_apple);
|
|
|
|
return false;
|
2015-03-31 14:44:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void apple_hid_free(void)
|
|
|
|
{
|
2015-03-31 15:14:39 +02:00
|
|
|
if (!hid_apple || !hid_apple->hid_ptr)
|
2015-03-31 14:44:51 +02:00
|
|
|
return;
|
|
|
|
|
2015-03-31 15:14:39 +02:00
|
|
|
pad_connection_destroy(hid_apple->slots);
|
|
|
|
|
2015-03-31 16:51:04 +02:00
|
|
|
IOHIDManagerUnscheduleFromRunLoop(hid_apple->hid_ptr,
|
2015-03-31 14:44:51 +02:00
|
|
|
CFRunLoopGetCurrent(), kCFRunLoopCommonModes);
|
|
|
|
|
2015-03-31 16:51:04 +02:00
|
|
|
apple_hid_manager_exit(hid_apple);
|
2015-03-31 15:15:54 +02:00
|
|
|
|
2015-03-31 16:51:04 +02:00
|
|
|
if (hid_apple)
|
|
|
|
free(hid_apple);
|
|
|
|
hid_apple = NULL;
|
2015-03-31 14:44:51 +02:00
|
|
|
}
|