mirror of
https://github.com/libretro/RetroArch
synced 2025-02-20 06:40:18 +00:00
(joypad_connection.c) Refactor pt. 2
This commit is contained in:
parent
29f640fe5d
commit
fdd616abcb
@ -16,6 +16,10 @@
|
||||
#ifndef __APPLE_RARCH_GAMECONTROLLER_H__
|
||||
#define __APPLE_RARCH_GAMECONTROLLER_H__
|
||||
|
||||
#include "../../input/connect/joypad_connection.h"
|
||||
|
||||
joypad_connection_t *slots;
|
||||
|
||||
void apple_gamecontroller_init(void);
|
||||
void apple_gamecontroller_poll_all(void);
|
||||
|
||||
|
@ -16,6 +16,7 @@
|
||||
#include <Availability.h>
|
||||
#include "RetroArch_Apple.h"
|
||||
#import <GameController/GameController.h>
|
||||
#include "apple_gamecontroller.h"
|
||||
#include "../../input/apple_input.h"
|
||||
|
||||
static BOOL apple_gamecontroller_available(void)
|
||||
@ -111,7 +112,7 @@ static void apple_gamecontroller_register(GCGamepad *gamepad)
|
||||
|
||||
static void apple_gamecontroller_connect(GCController *controller)
|
||||
{
|
||||
int32_t slot = apple_joypad_connect_gcapi();
|
||||
int32_t slot = apple_joypad_connect_gcapi(slots);
|
||||
|
||||
controller.playerIndex = (slot >= 0 && slot < MAX_PLAYERS) ? slot : GCControllerPlayerIndexUnset;
|
||||
|
||||
@ -123,10 +124,11 @@ static void apple_gamecontroller_connect(GCController *controller)
|
||||
|
||||
static void apple_gamecontroller_disconnect(GCController* controller)
|
||||
{
|
||||
if (controller.playerIndex == GCControllerPlayerIndexUnset)
|
||||
unsigned pad = (uint32_t)controller.playerIndex;
|
||||
if (pad == GCControllerPlayerIndexUnset)
|
||||
return;
|
||||
|
||||
pad_connection_disconnect((uint32_t)controller.playerIndex);
|
||||
pad_connection_disconnect(&slots[pad], pad);
|
||||
}
|
||||
|
||||
void apple_gamecontroller_init(void)
|
||||
|
@ -142,7 +142,7 @@ void btpad_packet_handler(uint8_t packet_type,
|
||||
&& (connection->channels[0] == channel ||
|
||||
connection->channels[1] == channel))
|
||||
{
|
||||
pad_connection_packet(connection->slot, packet, size);
|
||||
pad_connection_packet(&slots[connection->slot], connection->slot, packet, size);
|
||||
}
|
||||
}
|
||||
break;
|
||||
@ -326,7 +326,7 @@ void btpad_packet_handler(uint8_t packet_type,
|
||||
|
||||
RARCH_LOG("[BTpad]: Got %.200s.\n", (char*)&packet[9]);
|
||||
|
||||
connection->slot = pad_connection_connect(
|
||||
connection->slot = pad_connection_connect(&slots[connection->slot],
|
||||
(char*)packet + 9, connection, &btpad_connection_send_control);
|
||||
connection->state = BTPAD_CONNECTED;
|
||||
}
|
||||
@ -353,7 +353,7 @@ void btpad_packet_handler(uint8_t packet_type,
|
||||
{
|
||||
connection->handle = 0;
|
||||
|
||||
pad_connection_disconnect(connection->slot);
|
||||
pad_connection_disconnect(&slots[connection->slot], connection->slot);
|
||||
btpad_close_connection(connection);
|
||||
}
|
||||
}
|
||||
|
@ -29,6 +29,7 @@ struct pad_connection
|
||||
uint8_t data[2048];
|
||||
};
|
||||
|
||||
static joypad_connection_t *slots;
|
||||
static IOHIDManagerRef g_hid_manager;
|
||||
|
||||
static void hid_pad_connection_send_control(void *data, uint8_t* data_buf, size_t size)
|
||||
@ -132,7 +133,7 @@ static void remove_device(void* context, IOReturn result, void* sender)
|
||||
apple->buttons[connection->slot] = 0;
|
||||
memset(apple->axes[connection->slot], 0, sizeof(apple->axes));
|
||||
|
||||
pad_connection_disconnect(connection->slot);
|
||||
pad_connection_disconnect(&slots[connection->slot], connection->slot);
|
||||
free(connection);
|
||||
}
|
||||
|
||||
@ -146,7 +147,7 @@ static void hid_device_report(void* context, IOReturn result, void *sender,
|
||||
struct pad_connection* connection = (struct pad_connection*)context;
|
||||
|
||||
if (connection)
|
||||
pad_connection_packet(connection->slot,
|
||||
pad_connection_packet(&slots[connection->slot], connection->slot,
|
||||
connection->data, reportLength + 1);
|
||||
}
|
||||
|
||||
@ -181,10 +182,10 @@ static void add_device(void* context, IOReturn result,
|
||||
productID = (CFNumberRef)IOHIDDeviceGetProperty(device, CFSTR(kIOHIDProductIDKey));
|
||||
CFNumberGetValue(productID, kCFNumberIntType, &connection->p_id);
|
||||
|
||||
connection->slot = pad_connection_connect(device_name,
|
||||
connection->slot = pad_connection_connect(slots, device_name,
|
||||
connection, &hid_pad_connection_send_control);
|
||||
|
||||
if (pad_connection_has_interface(connection->slot))
|
||||
if (pad_connection_has_interface(slots, connection->slot))
|
||||
IOHIDDeviceRegisterInputReportCallback(device,
|
||||
connection->data + 1, sizeof(connection->data) - 1,
|
||||
hid_device_report, connection);
|
||||
@ -250,7 +251,7 @@ static bool apple_joypad_init(void)
|
||||
|
||||
IOHIDManagerOpen(g_hid_manager, kIOHIDOptionsTypeNone);
|
||||
|
||||
pad_connection_init();
|
||||
slots = (joypad_connection_t*)pad_connection_init(MAX_PLAYERS);
|
||||
|
||||
return true;
|
||||
}
|
||||
@ -276,14 +277,14 @@ static void apple_joypad_hid_destroy(void)
|
||||
|
||||
static void apple_joypad_destroy(void)
|
||||
{
|
||||
pad_connection_destroy();
|
||||
pad_connection_destroy(slots);
|
||||
apple_joypad_hid_destroy();
|
||||
}
|
||||
|
||||
static bool apple_joypad_button(unsigned port, uint16_t joykey)
|
||||
{
|
||||
apple_input_data_t *apple = (apple_input_data_t*)driver.input_data;
|
||||
uint32_t buttons = pad_connection_get_buttons(port);
|
||||
uint32_t buttons = pad_connection_get_buttons(&slots[port], port);
|
||||
if (!apple || joykey == NO_BTN)
|
||||
return false;
|
||||
|
||||
@ -309,13 +310,13 @@ static int16_t apple_joypad_axis(unsigned port, uint32_t joyaxis)
|
||||
if (AXIS_NEG_GET(joyaxis) < 4)
|
||||
{
|
||||
val = apple->axes[port][AXIS_NEG_GET(joyaxis)];
|
||||
val += pad_connection_get_axis(port, AXIS_NEG_GET(joyaxis));
|
||||
val += pad_connection_get_axis(&slots[port], port, AXIS_NEG_GET(joyaxis));
|
||||
val = (val < 0) ? val : 0;
|
||||
}
|
||||
else if(AXIS_POS_GET(joyaxis) < 4)
|
||||
{
|
||||
val = apple->axes[port][AXIS_POS_GET(joyaxis)];
|
||||
val += pad_connection_get_axis(port, AXIS_POS_GET(joyaxis));
|
||||
val += pad_connection_get_axis(&slots[port], port, AXIS_POS_GET(joyaxis));
|
||||
val = (val > 0) ? val : 0;
|
||||
}
|
||||
|
||||
@ -329,7 +330,7 @@ static void apple_joypad_poll(void)
|
||||
static bool apple_joypad_rumble(unsigned pad,
|
||||
enum retro_rumble_effect effect, uint16_t strength)
|
||||
{
|
||||
return pad_connection_rumble(pad, effect, strength);
|
||||
return pad_connection_rumble(&slots[pad], pad, effect, strength);
|
||||
}
|
||||
|
||||
static const char *apple_joypad_name(unsigned pad)
|
||||
|
@ -24,9 +24,14 @@
|
||||
#include "../apple/iOS/bluetooth/btpad_queue.c"
|
||||
#include "connect/joypad_connection.h"
|
||||
|
||||
joypad_connection_t *slots;
|
||||
|
||||
static bool apple_joypad_init(void)
|
||||
{
|
||||
pad_connection_init();
|
||||
slots = (joypad_connection_t*)pad_connection_init(MAX_PLAYERS);
|
||||
|
||||
if (!slots)
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
@ -37,13 +42,13 @@ static bool apple_joypad_query_pad(unsigned pad)
|
||||
|
||||
static void apple_joypad_destroy(void)
|
||||
{
|
||||
pad_connection_destroy();
|
||||
pad_connection_destroy(slots);
|
||||
}
|
||||
|
||||
static bool apple_joypad_button(unsigned port, uint16_t joykey)
|
||||
{
|
||||
apple_input_data_t *apple = (apple_input_data_t*)driver.input_data;
|
||||
uint32_t buttons = pad_connection_get_buttons(port);
|
||||
uint32_t buttons = pad_connection_get_buttons(&slots[port], port);
|
||||
if (!apple || joykey == NO_BTN)
|
||||
return false;
|
||||
|
||||
@ -68,13 +73,13 @@ static int16_t apple_joypad_axis(unsigned port, uint32_t joyaxis)
|
||||
if (AXIS_NEG_GET(joyaxis) < 4)
|
||||
{
|
||||
val = apple->axes[port][AXIS_NEG_GET(joyaxis)];
|
||||
val += pad_connection_get_axis(port, AXIS_NEG_GET(joyaxis));
|
||||
val += pad_connection_get_axis(&slots[port], port, AXIS_NEG_GET(joyaxis));
|
||||
val = (val < 0) ? val : 0;
|
||||
}
|
||||
else if(AXIS_POS_GET(joyaxis) < 4)
|
||||
{
|
||||
val = apple->axes[port][AXIS_POS_GET(joyaxis)];
|
||||
val += pad_connection_get_axis(port, AXIS_POS_GET(joyaxis));
|
||||
val += pad_connection_get_axis(&slots[port], port, AXIS_POS_GET(joyaxis));
|
||||
val = (val > 0) ? val : 0;
|
||||
}
|
||||
|
||||
@ -89,7 +94,7 @@ static void apple_joypad_poll(void)
|
||||
static bool apple_joypad_rumble(unsigned pad,
|
||||
enum retro_rumble_effect effect, uint16_t strength)
|
||||
{
|
||||
return pad_connection_rumble(pad, effect, strength);
|
||||
return pad_connection_rumble(&slots[pad], pad, effect, strength);
|
||||
}
|
||||
|
||||
static const char *apple_joypad_name(unsigned joypad)
|
||||
|
@ -16,37 +16,48 @@
|
||||
|
||||
#include "joypad_connection.h"
|
||||
|
||||
static joypad_connection_t slots[MAX_PLAYERS];
|
||||
|
||||
static int find_vacant_pad(void)
|
||||
static int find_vacant_pad(joypad_connection_t *joyconn)
|
||||
{
|
||||
unsigned i;
|
||||
|
||||
for (i = 0; i < MAX_PLAYERS; i++)
|
||||
{
|
||||
if (slots[i].used)
|
||||
continue;
|
||||
|
||||
return i;
|
||||
joypad_connection_t *conn = (joypad_connection_t*)&joyconn[i];
|
||||
if (conn && !conn->used)
|
||||
return i;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
void pad_connection_init()
|
||||
void *pad_connection_init(unsigned pads)
|
||||
{
|
||||
int i;
|
||||
for (i = 0; i < MAX_PLAYERS; i++)
|
||||
memset(&slots[i], 0, sizeof(slots[0]));
|
||||
joypad_connection_t *joyconn = (joypad_connection_t*)
|
||||
calloc(pads, sizeof(*joyconn));
|
||||
if (!joyconn)
|
||||
return NULL;
|
||||
|
||||
for (i = 0; i < pads; i++)
|
||||
{
|
||||
joypad_connection_t *conn = (joypad_connection_t*)&joyconn[i];
|
||||
conn->used = false;
|
||||
conn->iface = NULL;
|
||||
conn->is_gcapi = false;
|
||||
conn->data = NULL;
|
||||
}
|
||||
|
||||
return joyconn;
|
||||
}
|
||||
|
||||
int32_t pad_connection_connect(const char* name, void *data, send_control_t ptr)
|
||||
int32_t pad_connection_connect(joypad_connection_t *joyconn,
|
||||
const char* name, void *data, send_control_t ptr)
|
||||
{
|
||||
int pad = find_vacant_pad();
|
||||
int pad = find_vacant_pad(joyconn);
|
||||
|
||||
if (pad >= 0 && pad < MAX_PLAYERS)
|
||||
if (pad != -1)
|
||||
{
|
||||
unsigned i;
|
||||
joypad_connection_t* s = (joypad_connection_t*)&slots[pad];
|
||||
joypad_connection_t* s = (joypad_connection_t*)&joyconn[pad];
|
||||
|
||||
s->used = true;
|
||||
|
||||
@ -76,13 +87,13 @@ int32_t pad_connection_connect(const char* name, void *data, send_control_t ptr)
|
||||
return pad;
|
||||
}
|
||||
|
||||
int32_t apple_joypad_connect_gcapi(void)
|
||||
int32_t apple_joypad_connect_gcapi(joypad_connection_t *joyconn)
|
||||
{
|
||||
int pad = find_vacant_pad();
|
||||
int pad = find_vacant_pad(joyconn);
|
||||
|
||||
if (pad >= 0 && pad < MAX_PLAYERS)
|
||||
{
|
||||
joypad_connection_t *s = (joypad_connection_t*)&slots[pad];
|
||||
joypad_connection_t *s = (joypad_connection_t*)&joyconn[pad];
|
||||
|
||||
if (s)
|
||||
{
|
||||
@ -94,84 +105,73 @@ int32_t apple_joypad_connect_gcapi(void)
|
||||
return pad;
|
||||
}
|
||||
|
||||
void pad_connection_disconnect(uint32_t pad)
|
||||
void pad_connection_disconnect(joypad_connection_t *s, uint32_t pad)
|
||||
{
|
||||
if (pad < MAX_PLAYERS && slots[pad].used)
|
||||
{
|
||||
joypad_connection_t* s = (joypad_connection_t*)&slots[pad];
|
||||
|
||||
if (s->iface && s->data && s->iface->disconnect)
|
||||
s->iface->disconnect(s->data);
|
||||
|
||||
memset(s, 0, sizeof(joypad_connection_t));
|
||||
}
|
||||
if (!s || !s->used)
|
||||
return;
|
||||
|
||||
if (s->iface)
|
||||
{
|
||||
s->iface->set_rumble(s->data, RETRO_RUMBLE_STRONG, 0);
|
||||
s->iface->set_rumble(s->data, RETRO_RUMBLE_WEAK, 0);
|
||||
if (s->iface->disconnect)
|
||||
s->iface->disconnect(s->data);
|
||||
}
|
||||
|
||||
s->iface = NULL;
|
||||
s->used = false;
|
||||
s->is_gcapi = false;
|
||||
}
|
||||
|
||||
void pad_connection_packet(uint32_t pad,
|
||||
void pad_connection_packet(joypad_connection_t *s, uint32_t pad,
|
||||
uint8_t* data, uint32_t length)
|
||||
{
|
||||
if (pad < MAX_PLAYERS && slots[pad].used)
|
||||
{
|
||||
joypad_connection_t *s = (joypad_connection_t*)&slots[pad];
|
||||
|
||||
if (!s)
|
||||
return;
|
||||
|
||||
if (s->iface && s->data && s->iface->packet_handler)
|
||||
s->iface->packet_handler(s->data, data, length);
|
||||
}
|
||||
if (!s->used)
|
||||
return;
|
||||
|
||||
if (s->iface && s->data && s->iface->packet_handler)
|
||||
s->iface->packet_handler(s->data, data, length);
|
||||
}
|
||||
|
||||
uint32_t pad_connection_get_buttons(unsigned index)
|
||||
uint32_t pad_connection_get_buttons(joypad_connection_t *s, unsigned pad)
|
||||
{
|
||||
joypad_connection_t *s = (joypad_connection_t*)&slots[index];
|
||||
|
||||
if (s && s->iface && s->data)
|
||||
if (s->iface)
|
||||
return s->iface->get_buttons(s->data);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int16_t pad_connection_get_axis(unsigned index, unsigned i)
|
||||
int16_t pad_connection_get_axis(joypad_connection_t *s,
|
||||
unsigned index, unsigned i)
|
||||
{
|
||||
joypad_connection_t *s = (joypad_connection_t*)&slots[index];
|
||||
|
||||
if (s && s->iface && s->data)
|
||||
if (s->iface)
|
||||
return s->iface->get_axis(s->data, i);
|
||||
return 0;
|
||||
}
|
||||
|
||||
bool pad_connection_has_interface(uint32_t pad)
|
||||
bool pad_connection_has_interface(joypad_connection_t *s, unsigned pad)
|
||||
{
|
||||
if (pad < MAX_PLAYERS && slots[pad].used)
|
||||
return slots[pad].iface ? true : false;
|
||||
|
||||
if (s->used && s->iface)
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
void pad_connection_destroy(void)
|
||||
void pad_connection_destroy(joypad_connection_t *joyconn)
|
||||
{
|
||||
unsigned i;
|
||||
|
||||
for (i = 0; i < MAX_PLAYERS; i ++)
|
||||
{
|
||||
if (slots[i].used && slots[i].iface
|
||||
&& slots[i].iface->set_rumble)
|
||||
{
|
||||
slots[i].used = false;
|
||||
slots[i].iface = NULL;
|
||||
slots[i].iface->set_rumble(slots[i].data, RETRO_RUMBLE_STRONG, 0);
|
||||
slots[i].iface->set_rumble(slots[i].data, RETRO_RUMBLE_WEAK, 0);
|
||||
}
|
||||
joypad_connection_t *s = (joypad_connection_t*)&joyconn[i];
|
||||
pad_connection_disconnect(s, i);
|
||||
}
|
||||
}
|
||||
|
||||
bool pad_connection_rumble(unsigned pad,
|
||||
enum retro_rumble_effect effect, uint16_t strength)
|
||||
bool pad_connection_rumble(joypad_connection_t *s,
|
||||
unsigned pad, enum retro_rumble_effect effect, uint16_t strength)
|
||||
{
|
||||
if (pad < MAX_PLAYERS && slots[pad].used && slots[pad].iface
|
||||
&& slots[pad].iface->set_rumble)
|
||||
if (s->used && s->iface && s->iface->set_rumble)
|
||||
{
|
||||
slots[pad].iface->set_rumble(slots[pad].data, effect, strength);
|
||||
s->iface->set_rumble(s->data, effect, strength);
|
||||
return true;
|
||||
}
|
||||
|
||||
|
@ -19,6 +19,7 @@
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stddef.h>
|
||||
#include "../input_common.h"
|
||||
|
||||
typedef void (*send_control_t)(void *data, uint8_t *buf, size_t size);
|
||||
|
||||
@ -45,25 +46,31 @@ typedef struct
|
||||
bool is_gcapi;
|
||||
} joypad_connection_t;
|
||||
|
||||
int32_t pad_connection_connect(const char* name, void *data, send_control_t ptr);
|
||||
int32_t pad_connection_connect(joypad_connection_t *joyconn,
|
||||
const char* name, void *data, send_control_t ptr);
|
||||
|
||||
int32_t apple_joypad_connect_gcapi(void);
|
||||
int32_t apple_joypad_connect_gcapi(joypad_connection_t *joyconn);
|
||||
|
||||
void pad_connection_init(void);
|
||||
void *pad_connection_init(unsigned pads);
|
||||
|
||||
void pad_connection_destroy(void);
|
||||
void pad_connection_destroy(joypad_connection_t *joyconn);
|
||||
|
||||
void pad_connection_disconnect(uint32_t slot);
|
||||
void pad_connection_disconnect(joypad_connection_t *joyconn,
|
||||
unsigned index);
|
||||
|
||||
void pad_connection_packet(uint32_t slot, uint8_t* data, uint32_t length);
|
||||
void pad_connection_packet(joypad_connection_t *joyconn,
|
||||
unsigned index, uint8_t* data, uint32_t length);
|
||||
|
||||
uint32_t pad_connection_get_buttons(unsigned index);
|
||||
uint32_t pad_connection_get_buttons(joypad_connection_t *joyconn,
|
||||
unsigned index);
|
||||
|
||||
int16_t pad_connection_get_axis(unsigned index, unsigned i);
|
||||
int16_t pad_connection_get_axis(joypad_connection_t *joyconn,
|
||||
unsigned index, unsigned i);
|
||||
|
||||
/* Determine if connected joypad is a hidpad backed device.
|
||||
* If false, pad_connection_packet cannot be used */
|
||||
|
||||
bool pad_connection_has_interface(uint32_t slot);
|
||||
bool pad_connection_has_interface(joypad_connection_t *joyconn,
|
||||
unsigned index);
|
||||
|
||||
#endif
|
||||
|
Loading…
x
Reference in New Issue
Block a user