(iOS) Send touch events directly to a shared RAInputResponder instance, bypassing NSNotificationCenter.

This commit is contained in:
meancoot 2013-03-18 20:52:35 -04:00
parent 2a4573854e
commit 00d809436b
4 changed files with 39 additions and 13 deletions

View File

@ -25,6 +25,12 @@ typedef struct touch_data
} touch_data_t;
@interface RAInputResponder : NSObject
+ (RAInputResponder*)sharedInstance;
- (void)reset;
- (void)handleTouches:(NSArray*)touches;
- (void)poll;
- (bool)isKeyPressed:(unsigned)index;
- (const touch_data_t*)getTouchDataAtIndex:(unsigned)index;

View File

@ -18,7 +18,8 @@
extern NSString* const GSEventKeyDownNotification;
extern NSString* const GSEventKeyUpNotification;
extern NSString* const RATouchNotification;
static RAInputResponder* g_inputInstance;
@implementation RAInputResponder
{
@ -27,11 +28,18 @@ extern NSString* const RATouchNotification;
bool _keys[MAX_KEYS];
}
+ (RAInputResponder*)sharedInstance
{
if (!g_inputInstance)
g_inputInstance = [RAInputResponder new];
return g_inputInstance;
}
-(id)init
{
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyPressed:) name: GSEventKeyDownNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyReleased:) name: GSEventKeyUpNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleTouches:) name: RATouchNotification object:nil];
return self;
}
@ -40,6 +48,13 @@ extern NSString* const RATouchNotification;
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (void)reset
{
_touchCount = 0;
memset(_touches, 0, sizeof(_touches));
memset(_keys, 0, sizeof(_keys));
}
- (void)poll
{
for (int i = 0; i != _touchCount; i ++)
@ -73,10 +88,8 @@ extern NSString* const RATouchNotification;
if (keycode < MAX_KEYS) _keys[keycode] = false;
}
- (void)handleTouches:(NSNotification*)notification
- (void)handleTouches:(NSArray*)touches
{
UIEvent* event = [notification.userInfo objectForKey:@"event"];
NSArray* touches = [[event allTouches] allObjects];
const int numTouches = [touches count];
_touchCount = 0;

View File

@ -31,9 +31,7 @@ static RAInputResponder* g_input_driver;
static bool ios_key_pressed(enum retro_key key)
{
if ((int)key >= 0 && key < RETROK_LAST)
{
return [g_input_driver isKeyPressed:input_translate_rk_to_keysym(key)];
}
return false;
}
@ -46,8 +44,10 @@ static bool ios_is_pressed(unsigned port_num, const struct retro_keybind *key)
// Exported input driver
static void *ios_input_init(void)
{
g_input_driver = [RAInputResponder sharedInstance];
[g_input_driver reset];
input_init_keyboard_lut(rarch_key_map_hidusage);
g_input_driver = [RAInputResponder new];
return (void*)-1;
}

View File

@ -14,6 +14,7 @@
*/
#import <UIKit/UIKit.h>
#include "input/RAInputResponder.h"
#define GSEVENT_TYPE 2
#define GSEVENT_FLAGS 12
@ -23,22 +24,28 @@
NSString *const GSEventKeyDownNotification = @"GSEventKeyDownHackNotification";
NSString *const GSEventKeyUpNotification = @"GSEventKeyUpHackNotification";
NSString *const RATouchNotification = @"RATouchNotification";
static RAInputResponder* inputResponder;
@interface RApplication : UIApplication
@end
@implementation RApplication
- (RApplication*)init
{
self = [super init];
inputResponder = [RAInputResponder sharedInstance];
return self;
}
- (void)sendEvent:(UIEvent *)event
{
[super sendEvent:event];
if ([[event allTouches] count])
{
NSDictionary* inf = [[NSDictionary alloc] initWithObjectsAndKeys:
event, @"event", nil];
[[NSNotificationCenter defaultCenter] postNotificationName:RATouchNotification object:nil userInfo:inf];
[inputResponder handleTouches:[[event allTouches] allObjects]];
}
// Stolen from: http://nacho4d-nacho4d.blogspot.com/2012/01/catching-keyboard-events-in-ios.html
else if ([event respondsToSelector:@selector(_gsEvent)])