From 00d809436ba3a04beadac14d07e6e33adf2a1181 Mon Sep 17 00:00:00 2001 From: meancoot Date: Mon, 18 Mar 2013 20:52:35 -0400 Subject: [PATCH] (iOS) Send touch events directly to a shared RAInputResponder instance, bypassing NSNotificationCenter. --- ios/RetroArch/input/RAInputResponder.h | 6 ++++++ ios/RetroArch/input/RAInputResponder.m | 23 ++++++++++++++++++----- ios/RetroArch/input/ios_input.m | 6 +++--- ios/RetroArch/main.m | 17 ++++++++++++----- 4 files changed, 39 insertions(+), 13 deletions(-) diff --git a/ios/RetroArch/input/RAInputResponder.h b/ios/RetroArch/input/RAInputResponder.h index b02b07fed3..393c84f6df 100644 --- a/ios/RetroArch/input/RAInputResponder.h +++ b/ios/RetroArch/input/RAInputResponder.h @@ -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; diff --git a/ios/RetroArch/input/RAInputResponder.m b/ios/RetroArch/input/RAInputResponder.m index 98af742500..cf45333b38 100644 --- a/ios/RetroArch/input/RAInputResponder.m +++ b/ios/RetroArch/input/RAInputResponder.m @@ -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; diff --git a/ios/RetroArch/input/ios_input.m b/ios/RetroArch/input/ios_input.m index eb50fc73ec..87c71f1cd4 100644 --- a/ios/RetroArch/input/ios_input.m +++ b/ios/RetroArch/input/ios_input.m @@ -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; } diff --git a/ios/RetroArch/main.m b/ios/RetroArch/main.m index faf433bb63..79cffd8349 100644 --- a/ios/RetroArch/main.m +++ b/ios/RetroArch/main.m @@ -14,6 +14,7 @@ */ #import +#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)])