(Apple) OSX HID - some cleanups

This commit is contained in:
twinaphex 2014-10-04 16:46:52 +02:00
parent c07aa7ec0a
commit 1e2f8651b9
1 changed files with 232 additions and 221 deletions

View File

@ -21,7 +21,7 @@
#include "../general.h" #include "../general.h"
static void apple_pad_send_control(void *connect_data, static void apple_pad_send_control(void *connect_data,
uint8_t* data, size_t size); uint8_t* data, size_t size);
#include "wiimote.c" #include "wiimote.c"
#include "apple_joypad_ps3.c" #include "apple_joypad_ps3.c"
@ -33,7 +33,7 @@ typedef struct
bool used; bool used;
struct apple_pad_interface* iface; struct apple_pad_interface* iface;
void* data; void* data;
bool is_gcapi; bool is_gcapi;
} joypad_slot_t; } joypad_slot_t;
@ -41,222 +41,223 @@ static joypad_slot_t slots[MAX_PLAYERS];
struct apple_pad_connection struct apple_pad_connection
{ {
int v_id; int v_id;
int p_id; int p_id;
uint32_t slot; uint32_t slot;
IOHIDDeviceRef device_handle; IOHIDDeviceRef device_handle;
uint8_t data[2048]; uint8_t data[2048];
}; };
static IOHIDManagerRef g_hid_manager; static IOHIDManagerRef g_hid_manager;
static void apple_pad_send_control(void *connect_data, static void apple_pad_send_control(void *connect_data,
uint8_t* data, size_t size) uint8_t* data, size_t size)
{ {
struct apple_pad_connection* connection = struct apple_pad_connection* connection =
(struct apple_pad_connection*)connect_data; (struct apple_pad_connection*)connect_data;
if (connection) if (connection)
IOHIDDeviceSetReport(connection->device_handle, IOHIDDeviceSetReport(connection->device_handle,
kIOHIDReportTypeOutput, 0x01, data + 1, size - 1); kIOHIDReportTypeOutput, 0x01, data + 1, size - 1);
} }
/* NOTE: I pieced this together through trial and error, /* NOTE: I pieced this together through trial and error,
* any corrections are welcome. */ * any corrections are welcome. */
static void hid_device_input_callback(void* context, IOReturn result, static void hid_device_input_callback(void* context, IOReturn result,
void* sender, IOHIDValueRef value) void* sender, IOHIDValueRef value)
{ {
apple_input_data_t *apple = (apple_input_data_t*)driver.input_data; apple_input_data_t *apple = (apple_input_data_t*)driver.input_data;
struct apple_pad_connection* connection = (struct apple_pad_connection*) struct apple_pad_connection* connection = (struct apple_pad_connection*)
context; context;
IOHIDElementRef element = IOHIDValueGetElement(value); IOHIDElementRef element = IOHIDValueGetElement(value);
uint32_t type = IOHIDElementGetType(element); uint32_t type = IOHIDElementGetType(element);
uint32_t page = IOHIDElementGetUsagePage(element); uint32_t page = IOHIDElementGetUsagePage(element);
uint32_t use = IOHIDElementGetUsage(element); uint32_t use = IOHIDElementGetUsage(element);
/* Joystick handler. /* Joystick handler.
* TODO: Can GamePad work the same? */ * TODO: Can GamePad work the same? */
if ( if (
(type == kIOHIDElementTypeInput_Misc) || (type == kIOHIDElementTypeInput_Misc) ||
(type == kIOHIDElementTypeInput_Button) || (type == kIOHIDElementTypeInput_Button) ||
(type == kIOHIDElementTypeInput_Axis) (type == kIOHIDElementTypeInput_Axis)
) )
{ {
switch (page) switch (page)
{ {
case kHIDPage_GenericDesktop: case kHIDPage_GenericDesktop:
switch (type) switch (type)
{ {
case kIOHIDElementTypeInput_Misc: case kIOHIDElementTypeInput_Misc:
switch (use) switch (use)
{ {
case kHIDUsage_GD_Hatswitch: case kHIDUsage_GD_Hatswitch:
break; break;
default: default:
{
static const uint32_t axis_use_ids[4] = { 48, 49, 50, 53 };
int i;
for (i = 0; i < 4; i ++)
{ {
CFIndex min, max, state; static const uint32_t axis_use_ids[4] = { 48, 49, 50, 53 };
float val; int i;
if (use != axis_use_ids[i]) for (i = 0; i < 4; i ++)
continue; {
CFIndex min, max, state;
min = IOHIDElementGetPhysicalMin(element); float val;
max = IOHIDElementGetPhysicalMax(element) - min;
state = IOHIDValueGetIntegerValue(value) - min; if (use != axis_use_ids[i])
continue;
val = (float)state / (float)max;
apple->axes[connection->slot][i] = min = IOHIDElementGetPhysicalMin(element);
((val * 2.0f) - 1.0f) * 32767.0f; max = IOHIDElementGetPhysicalMax(element) - min;
state = IOHIDValueGetIntegerValue(value) - min;
val = (float)state / (float)max;
apple->axes[connection->slot][i] =
((val * 2.0f) - 1.0f) * 32767.0f;
}
} }
}
break; break;
} }
break; break;
} }
break; break;
case kHIDPage_Button: case kHIDPage_Button:
switch (type) switch (type)
{ {
case kIOHIDElementTypeInput_Button: case kIOHIDElementTypeInput_Button:
{ {
CFIndex state = IOHIDValueGetIntegerValue(value); CFIndex state = IOHIDValueGetIntegerValue(value);
if (state) if (state)
apple->buttons[connection->slot] |= (1 << (use - 1)); apple->buttons[connection->slot] |= (1 << (use - 1));
else else
apple->buttons[connection->slot] &= ~(1 << (use - 1)); apple->buttons[connection->slot] &= ~(1 << (use - 1));
} }
break; break;
} }
break; break;
} }
} }
} }
static void hid_device_removed(void* context, IOReturn result, void* sender) static void remove_device(void* context, IOReturn result, void* sender)
{ {
apple_input_data_t *apple = (apple_input_data_t*)driver.input_data; apple_input_data_t *apple = (apple_input_data_t*)driver.input_data;
struct apple_pad_connection* connection = (struct apple_pad_connection*) struct apple_pad_connection* connection = (struct apple_pad_connection*)
context; context;
if (connection && connection->slot < MAX_PLAYERS) if (connection && connection->slot < MAX_PLAYERS)
{ {
char msg[512]; char msg[512];
snprintf(msg, sizeof(msg), "Joypad #%u (%s) disconnected.", snprintf(msg, sizeof(msg), "Joypad #%u (%s) disconnected.",
connection->slot, "N/A"); connection->slot, "N/A");
msg_queue_push(g_extern.msg_queue, msg, 0, 60); msg_queue_push(g_extern.msg_queue, msg, 0, 60);
RARCH_LOG("[apple_input]: %s\n", msg); RARCH_LOG("[apple_input]: %s\n", msg);
apple->buttons[connection->slot] = 0; apple->buttons[connection->slot] = 0;
memset(apple->axes[connection->slot], 0, sizeof(apple->axes)); memset(apple->axes[connection->slot], 0, sizeof(apple->axes));
apple_joypad_disconnect(connection->slot); apple_joypad_disconnect(connection->slot);
free(connection); free(connection);
} }
IOHIDDeviceClose(sender, kIOHIDOptionsTypeSeizeDevice); IOHIDDeviceClose(sender, kIOHIDOptionsTypeSeizeDevice);
} }
static void hid_device_report(void* context, IOReturn result, void *sender, static void hid_device_report(void* context, IOReturn result, void *sender,
IOHIDReportType type, uint32_t reportID, uint8_t *report, IOHIDReportType type, uint32_t reportID, uint8_t *report,
CFIndex reportLength) CFIndex reportLength)
{ {
struct apple_pad_connection* connection = (struct apple_pad_connection*) struct apple_pad_connection* connection = (struct apple_pad_connection*)
context; context;
apple_joypad_packet(connection->slot, connection->data, reportLength + 1);
if (connection)
apple_joypad_packet(connection->slot, connection->data, reportLength + 1);
} }
static void hid_manager_device_attached(void* context, IOReturn result, static void add_device(void* context, IOReturn result,
void* sender, IOHIDDeviceRef device) void* sender, IOHIDDeviceRef device)
{ {
char device_name[PATH_MAX]; char device_name[PATH_MAX];
CFStringRef device_name_ref; CFStringRef device_name_ref;
CFNumberRef vendorID, productID; CFNumberRef vendorID, productID;
struct apple_pad_connection* connection = (struct apple_pad_connection*) struct apple_pad_connection* connection = (struct apple_pad_connection*)
calloc(1, sizeof(*connection)); calloc(1, sizeof(*connection));
connection->device_handle = device; connection->device_handle = device;
connection->slot = MAX_PLAYERS; connection->slot = MAX_PLAYERS;
IOHIDDeviceOpen(device, kIOHIDOptionsTypeNone); IOHIDDeviceOpen(device, kIOHIDOptionsTypeNone);
/* Move the device's run loop to this thread. */ /* Move the device's run loop to this thread. */
IOHIDDeviceScheduleWithRunLoop(device, CFRunLoopGetCurrent(), IOHIDDeviceScheduleWithRunLoop(device, CFRunLoopGetCurrent(),
kCFRunLoopCommonModes); kCFRunLoopCommonModes);
IOHIDDeviceRegisterRemovalCallback(device, hid_device_removed, connection); IOHIDDeviceRegisterRemovalCallback(device, remove_device, connection);
#ifndef IOS #ifndef IOS
device_name_ref = IOHIDDeviceGetProperty(device, CFSTR(kIOHIDProductKey)); device_name_ref = IOHIDDeviceGetProperty(device, CFSTR(kIOHIDProductKey));
CFStringGetCString(device_name_ref, device_name, CFStringGetCString(device_name_ref, device_name,
sizeof(device_name), kCFStringEncodingUTF8); sizeof(device_name), kCFStringEncodingUTF8);
#endif #endif
vendorID = (CFNumberRef)IOHIDDeviceGetProperty(device, CFSTR(kIOHIDVendorIDKey)); vendorID = (CFNumberRef)IOHIDDeviceGetProperty(device, CFSTR(kIOHIDVendorIDKey));
CFNumberGetValue(vendorID, kCFNumberIntType, &connection->v_id); CFNumberGetValue(vendorID, kCFNumberIntType, &connection->v_id);
productID = (CFNumberRef)IOHIDDeviceGetProperty(device, CFSTR(kIOHIDProductIDKey)); productID = (CFNumberRef)IOHIDDeviceGetProperty(device, CFSTR(kIOHIDProductIDKey));
CFNumberGetValue(productID, kCFNumberIntType, &connection->p_id); CFNumberGetValue(productID, kCFNumberIntType, &connection->p_id);
connection->slot = apple_joypad_connect(device_name, connection); connection->slot = apple_joypad_connect(device_name, connection);
if (apple_joypad_has_interface(connection->slot)) if (apple_joypad_has_interface(connection->slot))
IOHIDDeviceRegisterInputReportCallback(device, IOHIDDeviceRegisterInputReportCallback(device,
connection->data + 1, sizeof(connection->data) - 1, connection->data + 1, sizeof(connection->data) - 1,
hid_device_report, connection); hid_device_report, connection);
else else
IOHIDDeviceRegisterInputValueCallback(device, IOHIDDeviceRegisterInputValueCallback(device,
hid_device_input_callback, connection); hid_device_input_callback, connection);
if (device_name[0] != '\0') if (device_name[0] != '\0')
{ {
strlcpy(g_settings.input.device_names[connection->slot], strlcpy(g_settings.input.device_names[connection->slot],
device_name, sizeof(g_settings.input.device_names)); device_name, sizeof(g_settings.input.device_names));
input_config_autoconfigure_joypad(connection->slot, input_config_autoconfigure_joypad(connection->slot,
device_name, connection->v_id, connection->p_id, apple_hid_joypad.ident); device_name, connection->v_id, connection->p_id, apple_hid_joypad.ident);
RARCH_LOG("Port %d: %s.\n", connection->slot, device_name); RARCH_LOG("Port %d: %s.\n", connection->slot, device_name);
} }
} }
static void append_matching_dictionary(CFMutableArrayRef array, static void append_matching_dictionary(CFMutableArrayRef array,
uint32_t page, uint32_t use) uint32_t page, uint32_t use)
{ {
CFNumberRef pagen, usen; CFNumberRef pagen, usen;
CFMutableDictionaryRef matcher; CFMutableDictionaryRef matcher;
matcher = CFDictionaryCreateMutable(kCFAllocatorDefault, 0, matcher = CFDictionaryCreateMutable(kCFAllocatorDefault, 0,
&kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks); &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
pagen = CFNumberCreate(kCFAllocatorDefault, kCFNumberIntType, &page); pagen = CFNumberCreate(kCFAllocatorDefault, kCFNumberIntType, &page);
CFDictionarySetValue(matcher, CFSTR(kIOHIDDeviceUsagePageKey), pagen); CFDictionarySetValue(matcher, CFSTR(kIOHIDDeviceUsagePageKey), pagen);
CFRelease(pagen); CFRelease(pagen);
usen = CFNumberCreate(kCFAllocatorDefault, kCFNumberIntType, &use); usen = CFNumberCreate(kCFAllocatorDefault, kCFNumberIntType, &use);
CFDictionarySetValue(matcher, CFSTR(kIOHIDDeviceUsageKey), usen); CFDictionarySetValue(matcher, CFSTR(kIOHIDDeviceUsageKey), usen);
CFRelease(usen); CFRelease(usen);
CFArrayAppendValue(array, matcher); CFArrayAppendValue(array, matcher);
CFRelease(matcher); CFRelease(matcher);
} }
static int32_t find_empty_slot(void) static int find_vacant_pad(void)
{ {
unsigned i; unsigned i;
for (i = 0; i < MAX_PLAYERS; i++) for (i = 0; i < MAX_PLAYERS; i++)
{ {
if (!slots[i].used) if (slots[i].used)
{ continue;
memset(&slots[i], 0, sizeof(slots[0]));
return i; memset(&slots[i], 0, sizeof(slots[0]));
} return i;
} }
return -1; return -1;
} }
@ -265,12 +266,12 @@ int32_t apple_joypad_connect(const char* name, void *data)
{ {
struct apple_pad_connection* connection = struct apple_pad_connection* connection =
(struct apple_pad_connection*)data; (struct apple_pad_connection*)data;
int32_t slot = find_empty_slot(); int pad = find_vacant_pad();
if (slot >= 0 && slot < MAX_PLAYERS) if (pad >= 0 && pad < MAX_PLAYERS)
{ {
unsigned i; unsigned i;
joypad_slot_t* s = (joypad_slot_t*)&slots[slot]; joypad_slot_t* s = (joypad_slot_t*)&slots[pad];
s->used = true; s->used = true;
static const struct static const struct
@ -290,26 +291,26 @@ int32_t apple_joypad_connect(const char* name, void *data)
if (strstr(name, pad_map[i].name)) if (strstr(name, pad_map[i].name))
{ {
s->iface = pad_map[i].iface; s->iface = pad_map[i].iface;
s->data = s->iface->connect(connection, slot); s->data = s->iface->connect(connection, pad);
} }
} }
return slot; return pad;
} }
int32_t apple_joypad_connect_gcapi(void) int32_t apple_joypad_connect_gcapi(void)
{ {
int32_t slot = find_empty_slot(); int pad = find_vacant_pad();
if (slot >= 0 && slot < MAX_PLAYERS) if (pad >= 0 && pad < MAX_PLAYERS)
{ {
joypad_slot_t *s = (joypad_slot_t*)&slots[slot]; joypad_slot_t *s = (joypad_slot_t*)&slots[pad];
s->used = true; s->used = true;
s->is_gcapi = true; s->is_gcapi = true;
} }
return slot; return pad;
} }
void apple_joypad_disconnect(uint32_t slot) void apple_joypad_disconnect(uint32_t slot)
@ -347,32 +348,31 @@ bool apple_joypad_has_interface(uint32_t slot)
static bool apple_joypad_init(void) static bool apple_joypad_init(void)
{ {
CFMutableArrayRef matcher; CFMutableArrayRef matcher;
g_hid_manager = IOHIDManagerCreate( if (!(g_hid_manager = IOHIDManagerCreate(
kCFAllocatorDefault, kIOHIDOptionsTypeNone); kCFAllocatorDefault, kIOHIDOptionsTypeNone)))
return false;
if (!g_hid_manager)
return false; matcher = CFArrayCreateMutable(kCFAllocatorDefault, 0,
&kCFTypeArrayCallBacks);
matcher = CFArrayCreateMutable(kCFAllocatorDefault, 0,
&kCFTypeArrayCallBacks); append_matching_dictionary(matcher, kHIDPage_GenericDesktop,
append_matching_dictionary(matcher, kHIDPage_GenericDesktop, kHIDUsage_GD_Joystick);
kHIDUsage_GD_Joystick); append_matching_dictionary(matcher, kHIDPage_GenericDesktop,
append_matching_dictionary(matcher, kHIDPage_GenericDesktop, kHIDUsage_GD_GamePad);
kHIDUsage_GD_GamePad);
IOHIDManagerSetDeviceMatchingMultiple(g_hid_manager, matcher);
IOHIDManagerSetDeviceMatchingMultiple(g_hid_manager, matcher); CFRelease(matcher);
CFRelease(matcher);
IOHIDManagerRegisterDeviceMatchingCallback(g_hid_manager,
IOHIDManagerRegisterDeviceMatchingCallback(g_hid_manager, add_device, 0);
hid_manager_device_attached, 0); IOHIDManagerScheduleWithRunLoop(g_hid_manager, CFRunLoopGetMain(),
IOHIDManagerScheduleWithRunLoop(g_hid_manager, CFRunLoopGetMain(), kCFRunLoopCommonModes);
kCFRunLoopCommonModes);
IOHIDManagerOpen(g_hid_manager, kIOHIDOptionsTypeNone);
IOHIDManagerOpen(g_hid_manager, kIOHIDOptionsTypeNone);
return true;
return true;
} }
static bool apple_joypad_query_pad(unsigned pad) static bool apple_joypad_query_pad(unsigned pad)
@ -380,29 +380,35 @@ static bool apple_joypad_query_pad(unsigned pad)
return pad < MAX_PLAYERS; return pad < MAX_PLAYERS;
} }
static void apple_joypad_hid_destroy(void)
{
if (!g_hid_manager)
return;
IOHIDManagerClose(g_hid_manager, kIOHIDOptionsTypeNone);
IOHIDManagerUnscheduleFromRunLoop(g_hid_manager,
CFRunLoopGetCurrent(), kCFRunLoopCommonModes);
CFRelease(g_hid_manager);
g_hid_manager = NULL;
}
static void apple_joypad_destroy(void) static void apple_joypad_destroy(void)
{ {
unsigned i; unsigned i;
for (i = 0; i < MAX_PLAYERS; i ++) for (i = 0; i < MAX_PLAYERS; i ++)
{ {
if (slots[i].used && slots[i].iface && slots[i].iface->set_rumble) if (slots[i].used && slots[i].iface
&& slots[i].iface->set_rumble)
{ {
slots[i].iface->set_rumble(slots[i].data, RETRO_RUMBLE_STRONG, 0); slots[i].iface->set_rumble(slots[i].data, RETRO_RUMBLE_STRONG, 0);
slots[i].iface->set_rumble(slots[i].data, RETRO_RUMBLE_WEAK, 0); slots[i].iface->set_rumble(slots[i].data, RETRO_RUMBLE_WEAK, 0);
} }
} }
if (!g_hid_manager) apple_joypad_hid_destroy();
return;
IOHIDManagerClose(g_hid_manager, kIOHIDOptionsTypeNone);
IOHIDManagerUnscheduleFromRunLoop(g_hid_manager,
CFRunLoopGetCurrent(), kCFRunLoopCommonModes);
CFRelease(g_hid_manager);
g_hid_manager = NULL;
} }
static bool apple_joypad_button(unsigned port, uint16_t joykey) static bool apple_joypad_button(unsigned port, uint16_t joykey)
@ -449,7 +455,7 @@ static bool apple_joypad_rumble(unsigned pad,
enum retro_rumble_effect effect, uint16_t strength) enum retro_rumble_effect effect, uint16_t strength)
{ {
if (pad < MAX_PLAYERS && slots[pad].used && slots[pad].iface if (pad < MAX_PLAYERS && slots[pad].used && slots[pad].iface
&& slots[pad].iface->set_rumble) && slots[pad].iface->set_rumble)
{ {
slots[pad].iface->set_rumble(slots[pad].data, effect, strength); slots[pad].iface->set_rumble(slots[pad].data, effect, strength);
return true; return true;
@ -460,7 +466,12 @@ static bool apple_joypad_rumble(unsigned pad,
static const char *apple_joypad_name(unsigned joypad) static const char *apple_joypad_name(unsigned joypad)
{ {
/* TODO/FIXME - implement properly */
if (pad >= MAX_PLAYERS)
return NULL;
(void)joypad; (void)joypad;
return NULL; return NULL;
} }