mirror of
https://github.com/libretro/RetroArch
synced 2025-03-24 22:43:41 +00:00
(iOS Thread) Sync input data between main and retroarch thread
This commit is contained in:
parent
bae11ddc24
commit
bfe1bf138d
ios/RetroArch
@ -15,6 +15,7 @@
|
||||
|
||||
#include "general.h"
|
||||
#include "rarch_wrapper.h"
|
||||
#include "input/ios_input.h"
|
||||
|
||||
static const float ALMOST_INVISIBLE = .021f;
|
||||
static float g_screen_scale;
|
||||
@ -158,6 +159,9 @@ void ios_flip_game_view()
|
||||
{
|
||||
dispatch_sync(dispatch_get_main_queue(), ^{
|
||||
[g_view display];
|
||||
|
||||
// HACK: While here, copy input structures
|
||||
ios_copy_input(&g_ios_input_data);
|
||||
});
|
||||
g_fast_forward_skips = g_is_syncing ? 0 : 3;
|
||||
}
|
||||
|
@ -13,7 +13,6 @@
|
||||
* If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include <pthread.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/sysctl.h>
|
||||
#include <stdio.h>
|
||||
@ -27,50 +26,34 @@
|
||||
static struct btpad_interface* btpad_iface;
|
||||
static void* btpad_device;
|
||||
static bool btpad_want_wiimote;
|
||||
static pthread_mutex_t btpad_lock = PTHREAD_MUTEX_INITIALIZER;
|
||||
|
||||
// MAIN THREAD ONLY
|
||||
uint32_t btpad_get_buttons()
|
||||
{
|
||||
pthread_mutex_lock(&btpad_lock);
|
||||
uint32_t result = (btpad_device && btpad_iface) ? btpad_iface->get_buttons(btpad_device) : 0;
|
||||
pthread_mutex_unlock(&btpad_lock);
|
||||
|
||||
return result;
|
||||
return (btpad_device && btpad_iface) ? btpad_iface->get_buttons(btpad_device) : 0;
|
||||
}
|
||||
|
||||
int16_t btpad_get_axis(unsigned axis)
|
||||
{
|
||||
pthread_mutex_lock(&btpad_lock);
|
||||
int16_t result = (btpad_device && btpad_iface) ? btpad_iface->get_axis(btpad_device, axis) : 0;
|
||||
pthread_mutex_unlock(&btpad_lock);
|
||||
return result;
|
||||
return (btpad_device && btpad_iface) ? btpad_iface->get_axis(btpad_device, axis) : 0;
|
||||
}
|
||||
|
||||
void btpad_set_pad_type(bool wiimote)
|
||||
{
|
||||
pthread_mutex_lock(&btpad_lock);
|
||||
btpad_want_wiimote = wiimote;
|
||||
pthread_mutex_unlock(&btpad_lock);
|
||||
}
|
||||
|
||||
// BT THREAD ONLY
|
||||
static void btpad_connect_pad(bool wiimote)
|
||||
{
|
||||
pthread_mutex_lock(&btpad_lock);
|
||||
|
||||
ios_add_log_message("BTpad: Connecting to %s", wiimote ? "WiiMote" : "PS3");
|
||||
|
||||
btpad_iface = (wiimote) ? &btpad_wii : &btpad_ps3;
|
||||
btpad_device = btpad_iface->connect();
|
||||
|
||||
pthread_mutex_unlock(&btpad_lock);
|
||||
}
|
||||
|
||||
static void btpad_disconnect_pad()
|
||||
{
|
||||
pthread_mutex_lock(&btpad_lock);
|
||||
|
||||
if (btpad_iface && btpad_device)
|
||||
{
|
||||
ios_add_log_message("BTpad: Disconnecting");
|
||||
@ -79,8 +62,6 @@ static void btpad_disconnect_pad()
|
||||
btpad_device = 0;
|
||||
btpad_iface = 0;
|
||||
}
|
||||
|
||||
pthread_mutex_unlock(&btpad_lock);
|
||||
}
|
||||
|
||||
void btpad_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size)
|
||||
@ -94,9 +75,5 @@ void btpad_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet
|
||||
}
|
||||
|
||||
if (btpad_device && btpad_iface)
|
||||
{
|
||||
pthread_mutex_lock(&btpad_lock);
|
||||
btpad_iface->packet_handler(btpad_device, packet_type, channel, packet, size);
|
||||
pthread_mutex_unlock(&btpad_lock);
|
||||
}
|
||||
}
|
||||
|
@ -24,6 +24,8 @@ static const rarch_joypad_driver_t* const g_joydriver = &ios_joypad;
|
||||
|
||||
static const struct rarch_key_map rarch_key_map_hidusage[];
|
||||
|
||||
ios_input_data_t g_ios_input_data;
|
||||
|
||||
// Key event data, called in main.m
|
||||
#define MAX_KEY_EVENTS 32
|
||||
|
||||
@ -58,7 +60,7 @@ void ios_add_key_event(bool down, unsigned keycode, uint32_t character, uint16_t
|
||||
static bool ios_key_pressed(enum retro_key key)
|
||||
{
|
||||
if ((int)key >= 0 && key < RETROK_LAST)
|
||||
return ios_key_list[input_translate_rk_to_keysym(key)];
|
||||
return g_ios_input_data.keys[input_translate_rk_to_keysym(key)];
|
||||
|
||||
return false;
|
||||
}
|
||||
@ -90,11 +92,11 @@ static void ios_input_poll(void *data)
|
||||
g_pending_key_events = 0;
|
||||
|
||||
|
||||
for (int i = 0; i != ios_touch_count; i ++)
|
||||
for (int i = 0; i != g_ios_input_data.touch_count; i ++)
|
||||
{
|
||||
input_translate_coord_viewport(ios_touch_list[i].screen_x, ios_touch_list[i].screen_y,
|
||||
&ios_touch_list[i].fixed_x, &ios_touch_list[i].fixed_y,
|
||||
&ios_touch_list[i].full_x, &ios_touch_list[i].full_y);
|
||||
input_translate_coord_viewport(g_ios_input_data.touches[i].screen_x, g_ios_input_data.touches[i].screen_y,
|
||||
&g_ios_input_data.touches[i].fixed_x, &g_ios_input_data.touches[i].fixed_y,
|
||||
&g_ios_input_data.touches[i].full_x, &g_ios_input_data.touches[i].full_y);
|
||||
}
|
||||
|
||||
input_joypad_poll(g_joydriver);
|
||||
@ -118,9 +120,9 @@ static int16_t ios_input_state(void *data, const struct retro_keybind **binds, u
|
||||
{
|
||||
const bool want_full = device == RARCH_DEVICE_POINTER_SCREEN;
|
||||
|
||||
if (index < ios_touch_count && index < MAX_TOUCHES)
|
||||
if (index < g_ios_input_data.touch_count && index < MAX_TOUCHES)
|
||||
{
|
||||
const touch_data_t* touch = &ios_touch_list[index];
|
||||
const ios_touch_data_t* touch = &g_ios_input_data.touches[index];
|
||||
|
||||
switch (id)
|
||||
{
|
||||
|
@ -20,18 +20,30 @@
|
||||
#define MAX_TOUCHES 16
|
||||
#define MAX_KEYS 256
|
||||
|
||||
typedef struct touch_data
|
||||
typedef struct
|
||||
{
|
||||
int16_t screen_x, screen_y;
|
||||
int16_t fixed_x, fixed_y;
|
||||
int16_t full_x, full_y;
|
||||
} touch_data_t;
|
||||
} ios_touch_data_t;
|
||||
|
||||
// Defined in main.m, lists are filled by the sendEvent selector
|
||||
extern uint32_t ios_key_list[MAX_KEYS];
|
||||
extern uint32_t ios_touch_count;
|
||||
extern touch_data_t ios_touch_list[MAX_TOUCHES];
|
||||
typedef struct
|
||||
{
|
||||
ios_touch_data_t touches[MAX_TOUCHES];
|
||||
uint32_t touch_count;
|
||||
|
||||
uint32_t keys[MAX_KEYS];
|
||||
|
||||
uint32_t pad_buttons;
|
||||
int16_t pad_axis[4];
|
||||
} ios_input_data_t;
|
||||
|
||||
extern ios_input_data_t g_ios_input_data;
|
||||
|
||||
// Defined in main.m, must be called on the emu thread in a dispatch_sync block
|
||||
void ios_copy_input(ios_input_data_t* data);
|
||||
|
||||
// Called from main.m, defined in ios_input.c
|
||||
void ios_add_key_event(bool down, unsigned keycode, uint32_t character, uint16_t keyModifiers);
|
||||
|
||||
#endif
|
||||
|
@ -14,11 +14,10 @@
|
||||
*/
|
||||
|
||||
#include "input/input_common.h"
|
||||
#include "ios_input.h"
|
||||
#include "BTStack/btpad.h"
|
||||
#include "general.h"
|
||||
|
||||
static uint32_t g_buttons[MAX_PLAYERS];
|
||||
|
||||
static bool ios_joypad_init(void)
|
||||
{
|
||||
return true;
|
||||
@ -42,23 +41,23 @@ static bool ios_joypad_button(unsigned port, uint16_t joykey)
|
||||
if (GET_HAT_DIR(joykey))
|
||||
return false;
|
||||
else // Check the button
|
||||
return (port < MAX_PLAYERS && joykey < 32) ? (g_buttons[port] & (1 << joykey)) != 0 : false;
|
||||
return (port == 0 && joykey < 32) ? (g_ios_input_data.pad_buttons & (1 << joykey)) != 0 : false;
|
||||
}
|
||||
|
||||
static int16_t ios_joypad_axis(unsigned port, uint32_t joyaxis)
|
||||
{
|
||||
if (joyaxis == AXIS_NONE)
|
||||
if (joyaxis == AXIS_NONE || port != 0)
|
||||
return 0;
|
||||
|
||||
int16_t val = 0;
|
||||
if (AXIS_NEG_GET(joyaxis) < 4)
|
||||
{
|
||||
val = btpad_get_axis(AXIS_NEG_GET(joyaxis));
|
||||
val = g_ios_input_data.pad_axis[AXIS_NEG_GET(joyaxis)];
|
||||
val = (val < 0) ? val : 0;
|
||||
}
|
||||
else if(AXIS_POS_GET(joyaxis) < 4)
|
||||
{
|
||||
val = btpad_get_axis(AXIS_POS_GET(joyaxis));
|
||||
val = g_ios_input_data.pad_axis[AXIS_POS_GET(joyaxis)];
|
||||
val = (val > 0) ? val : 0;
|
||||
}
|
||||
|
||||
@ -67,7 +66,6 @@ static int16_t ios_joypad_axis(unsigned port, uint32_t joyaxis)
|
||||
|
||||
static void ios_joypad_poll(void)
|
||||
{
|
||||
g_buttons[0] = btpad_get_buttons();
|
||||
}
|
||||
|
||||
const rarch_joypad_driver_t ios_joypad = {
|
||||
|
@ -16,6 +16,7 @@
|
||||
#import <UIKit/UIKit.h>
|
||||
#include "input/ios_input.h"
|
||||
#include "input/keycode.h"
|
||||
#include "input/BTStack/btpad.h"
|
||||
#include "libretro.h"
|
||||
|
||||
#include <sys/stat.h>
|
||||
@ -39,11 +40,20 @@
|
||||
#define GSEVENT_MOD_ALT (1 << 19)
|
||||
#define GSEVENT_MOD_CTRL (1 << 20)
|
||||
|
||||
uint32_t ios_key_list[MAX_KEYS];
|
||||
uint32_t ios_touch_count;
|
||||
touch_data_t ios_touch_list[MAX_TOUCHES];
|
||||
static ios_input_data_t g_input_data;
|
||||
|
||||
// Input helpers
|
||||
void ios_copy_input(ios_input_data_t* data)
|
||||
{
|
||||
// Call only from main thread
|
||||
|
||||
memcpy(data, &g_input_data, sizeof(g_input_data));
|
||||
data->pad_buttons = btpad_get_buttons();
|
||||
|
||||
for (int i = 0; i < 4; i ++)
|
||||
data->pad_axis[i] = btpad_get_axis(i);
|
||||
}
|
||||
|
||||
static uint32_t translate_mods(uint32_t flags)
|
||||
{
|
||||
uint32_t result = 0;
|
||||
@ -69,21 +79,22 @@ static uint32_t translate_mods(uint32_t flags)
|
||||
const int numTouches = [touches count];
|
||||
const float scale = [[UIScreen mainScreen] scale];
|
||||
|
||||
ios_touch_count = 0;
|
||||
g_input_data.touch_count = 0;
|
||||
|
||||
for(int i = 0; i != numTouches && ios_touch_count < MAX_TOUCHES; i ++)
|
||||
for(int i = 0; i != numTouches && g_input_data.touch_count < MAX_TOUCHES; i ++)
|
||||
{
|
||||
UITouch* touch = [touches objectAtIndex:i];
|
||||
const CGPoint coord = [touch locationInView:touch.view];
|
||||
|
||||
if (touch.phase != UITouchPhaseEnded && touch.phase != UITouchPhaseCancelled)
|
||||
{
|
||||
ios_touch_list[ios_touch_count ].screen_x = coord.x * scale;
|
||||
ios_touch_list[ios_touch_count ++].screen_y = coord.y * scale;
|
||||
g_input_data.touches[g_input_data.touch_count ].screen_x = coord.x * scale;
|
||||
g_input_data.touches[g_input_data.touch_count ++].screen_y = coord.y * scale;
|
||||
}
|
||||
}
|
||||
}
|
||||
// Stolen from: http://nacho4d-nacho4d.blogspot.com/2012/01/catching-keyboard-events-in-ios.html
|
||||
// TODO: Key events need to be synced, I just disabled them because the data isn't available on device (only in simulator)
|
||||
else if ([event respondsToSelector:@selector(_gsEvent)])
|
||||
{
|
||||
uint8_t* eventMem = (uint8_t*)(void*)CFBridgingRetain([event performSelector:@selector(_gsEvent)]);
|
||||
@ -94,12 +105,13 @@ static uint32_t translate_mods(uint32_t flags)
|
||||
uint16_t* data = (uint16_t*)&eventMem[0x3C];
|
||||
|
||||
if (data[0] < MAX_KEYS)
|
||||
ios_key_list[data[0]] = (eventType == GSEVENT_TYPE_KEYDOWN) ? 1 : 0;
|
||||
g_input_data.keys[data[0]] = (eventType == GSEVENT_TYPE_KEYDOWN) ? 1 : 0;
|
||||
|
||||
// Key events
|
||||
ios_add_key_event(eventType == GSEVENT_TYPE_KEYDOWN, data[0], data[1], translate_mods(*(uint32_t*)&eventMem[0x30]));
|
||||
// ios_add_key_event(eventType == GSEVENT_TYPE_KEYDOWN, data[0], data[1], translate_mods(*(uint32_t*)&eventMem[0x30]));
|
||||
// printf("%d %d %d %08X\n", data[0], data[1], data[2], *(uint32_t*)&eventMem[0x30]);
|
||||
}
|
||||
#if 0
|
||||
else if(eventType == GSEVENT_TYPE_MODS)
|
||||
{
|
||||
static const struct
|
||||
@ -138,11 +150,12 @@ static uint32_t translate_mods(uint32_t flags)
|
||||
if (key == modmap[i].key)
|
||||
{
|
||||
keystate[i] = !keystate[i];
|
||||
ios_key_list[modmap[i].hidid] = keystate[i];
|
||||
ios_add_key_event(keystate[i], modmap[i].retrokey, 0, translate_mods(*(uint32_t*)&eventMem[0x30]));
|
||||
g_input_data.keys[modmap[i].hidid] = keystate[i];
|
||||
// ios_add_key_event(keystate[i], modmap[i].retrokey, 0, translate_mods(*(uint32_t*)&eventMem[0x30]));
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
CFBridgingRelease(eventMem);
|
||||
}
|
||||
|
@ -373,10 +373,13 @@ static const struct
|
||||
|
||||
- (void)checkInput
|
||||
{
|
||||
ios_input_data_t data;
|
||||
ios_copy_input(&data);
|
||||
|
||||
// Keyboard
|
||||
for (int i = 0; ios_key_name_map[i].hid_id; i++)
|
||||
{
|
||||
if (ios_key_list[ios_key_name_map[i].hid_id])
|
||||
if (data.keys[ios_key_name_map[i].hid_id])
|
||||
{
|
||||
_value.msubValues[0] = [NSString stringWithUTF8String:ios_key_name_map[i].keyname];
|
||||
[self finish];
|
||||
@ -385,11 +388,9 @@ static const struct
|
||||
}
|
||||
|
||||
// Pad Buttons
|
||||
uint32_t buttons = btpad_get_buttons();
|
||||
|
||||
for (int i = 0; buttons && i < sizeof(buttons) * 8; i++)
|
||||
for (int i = 0; data.pad_buttons && i < sizeof(data.pad_buttons) * 8; i++)
|
||||
{
|
||||
if (buttons & (1 << i))
|
||||
if (data.pad_buttons & (1 << i))
|
||||
{
|
||||
_value.msubValues[1] = [NSString stringWithFormat:@"%d", i];
|
||||
[self finish];
|
||||
@ -400,7 +401,7 @@ static const struct
|
||||
// Pad Axis
|
||||
for (int i = 0; i < 4; i++)
|
||||
{
|
||||
int16_t value = btpad_get_axis(i);
|
||||
int16_t value = data.pad_axis[i];
|
||||
|
||||
if (abs(value) > 0x1000)
|
||||
{
|
||||
|
Loading…
x
Reference in New Issue
Block a user