RetroArch/ui/drivers/ui_cocoatouch.m

540 lines
16 KiB
Mathematica
Raw Normal View History

2015-04-12 01:02:48 +00:00
/* RetroArch - A frontend for libretro.
2016-06-04 02:24:54 +00:00
* Copyright (C) 2011-2016 - Daniel De Matteis
2015-04-12 01:02:48 +00:00
*
* RetroArch is free software: you can redistribute it and/or modify it under the terms
* of the GNU General Public License as published by the Free Software Found-
* ation, either version 3 of the License, or (at your option) any later version.
*
* RetroArch is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
* PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with RetroArch.
* If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdint.h>
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#include <boolean.h>
2015-04-12 01:02:48 +00:00
#include <file/file_path.h>
2016-05-16 15:29:02 +00:00
#include <queues/task_queue.h>
#include <string/stdstring.h>
2017-06-28 02:41:38 +00:00
#include <retro_timers.h>
#include "cocoa/cocoa_common.h"
#include "cocoa/apple_platform.h"
2015-04-12 01:02:48 +00:00
#include "../ui_companion_driver.h"
2017-05-13 19:06:57 +00:00
#include "../../configuration.h"
#include "../../frontend/frontend.h"
#include "../../input/drivers/cocoa_input.h"
2015-11-29 15:30:38 +00:00
#include "../../input/drivers_keyboard/keyboard_event_apple.h"
2015-08-15 00:59:36 +00:00
#include "../../retroarch.h"
2015-12-06 17:18:50 +00:00
#ifdef HAVE_MENU
#include "../../menu/menu_setting.h"
#endif
#import <AVFoundation/AVFoundation.h>
#if defined(HAVE_COCOA_METAL) || defined(HAVE_COCOATOUCH)
id<ApplePlatform> apple_platform;
#else
static id apple_platform;
#endif
static CFRunLoopObserverRef iterate_observer;
2020-06-04 12:41:28 +00:00
/* Forward declaration */
2016-06-04 16:10:52 +00:00
static void apple_rarch_exited(void);
2015-04-21 09:51:35 +00:00
2015-08-05 09:41:41 +00:00
static void rarch_enable_ui(void)
{
2016-01-18 20:35:09 +00:00
bool boolean = true;
2015-09-26 11:20:15 +00:00
2015-08-05 11:56:59 +00:00
ui_companion_set_foreground(true);
2015-09-26 11:20:15 +00:00
2017-05-15 03:06:23 +00:00
rarch_ctl(RARCH_CTL_SET_PAUSED, &boolean);
rarch_ctl(RARCH_CTL_SET_IDLE, &boolean);
retroarch_menu_running();
2015-08-05 09:41:41 +00:00
}
2015-08-05 10:06:37 +00:00
static void rarch_disable_ui(void)
{
2015-09-26 11:20:15 +00:00
bool boolean = false;
2015-08-05 11:56:59 +00:00
ui_companion_set_foreground(false);
2015-09-26 11:20:15 +00:00
2017-05-15 03:06:23 +00:00
rarch_ctl(RARCH_CTL_SET_PAUSED, &boolean);
rarch_ctl(RARCH_CTL_SET_IDLE, &boolean);
retroarch_menu_running_finished(false);
2015-08-05 10:06:37 +00:00
}
2015-11-11 02:24:53 +00:00
static void ui_companion_cocoatouch_event_command(
2020-02-13 21:39:29 +00:00
void *data, enum event_command cmd) { }
2015-11-11 02:24:53 +00:00
2015-08-21 02:42:38 +00:00
static void rarch_draw_observer(CFRunLoopObserverRef observer,
CFRunLoopActivity activity, void *info)
{
int ret = runloop_iterate();
2015-07-08 22:01:55 +00:00
2017-05-14 18:43:39 +00:00
task_queue_check();
2015-07-08 22:01:55 +00:00
if (ret == -1)
{
2020-09-15 19:12:27 +00:00
ui_companion_cocoatouch_event_command(
NULL, CMD_EVENT_MENU_SAVE_CURRENT_CONFIG);
2015-07-08 22:01:55 +00:00
main_exit(NULL);
return;
}
2017-05-15 03:06:23 +00:00
if (rarch_ctl(RARCH_CTL_IS_IDLE, NULL))
2015-07-08 22:01:55 +00:00
return;
CFRunLoopWakeUp(CFRunLoopGetMain());
}
apple_frontend_settings_t apple_frontend_settings;
void get_ios_version(int *major, int *minor)
{
NSArray *decomposed_os_version = [[UIDevice currentDevice].systemVersion componentsSeparatedByString:@"."];
2019-02-03 23:49:35 +00:00
if (major && decomposed_os_version.count > 0)
*major = (int)[decomposed_os_version[0] integerValue];
if (minor && decomposed_os_version.count > 1)
*minor = (int)[decomposed_os_version[1] integerValue];
}
extern float cocoagl_gfx_ctx_get_native_scale(void);
/* Input helpers: This is kept here because it needs ObjC */
static void handle_touch_event(NSArray* touches)
{
unsigned i;
cocoa_input_data_t *apple = (cocoa_input_data_t*)input_driver_get_data();
float scale = cocoagl_gfx_ctx_get_native_scale();
if (!apple)
return;
apple->touch_count = 0;
2019-02-03 23:49:35 +00:00
for (i = 0; i < touches.count && (apple->touch_count < MAX_TOUCHES); i++)
{
UITouch *touch = [touches objectAtIndex:i];
2020-09-15 19:12:27 +00:00
CGPoint coord = [touch locationInView:[touch view]];
if (touch.phase != UITouchPhaseEnded && touch.phase != UITouchPhaseCancelled)
{
apple->touches[apple->touch_count ].screen_x = coord.x * scale;
apple->touches[apple->touch_count ++].screen_y = coord.y * scale;
}
}
}
#ifndef HAVE_APPLE_STORE
2020-09-15 19:12:27 +00:00
/* iOS7 Keyboard support */
@interface UIEvent(iOS7Keyboard)
@property(readonly, nonatomic) long long _keyCode;
@property(readonly, nonatomic) _Bool _isKeyDown;
@property(retain, nonatomic) NSString *_privateInput;
@property(nonatomic) long long _modifierFlags;
- (struct __IOHIDEvent { }*)_hidEvent;
@end
@interface UIApplication(iOS7Keyboard)
- (void)handleKeyUIEvent:(UIEvent*)event;
- (id)_keyCommandForEvent:(UIEvent*)event;
@end
#endif
@interface RApplication : UIApplication
@end
@implementation RApplication
#ifndef HAVE_APPLE_STORE
/* Keyboard handler for iOS 7. */
/* This is copied here as it isn't
* defined in any standard iOS header */
enum
{
NSAlphaShiftKeyMask = 1 << 16,
NSShiftKeyMask = 1 << 17,
NSControlKeyMask = 1 << 18,
NSAlternateKeyMask = 1 << 19,
NSCommandKeyMask = 1 << 20,
NSNumericPadKeyMask = 1 << 21,
NSHelpKeyMask = 1 << 22,
NSFunctionKeyMask = 1 << 23,
NSDeviceIndependentModifierFlagsMask = 0xffff0000U
};
2020-09-15 19:12:27 +00:00
/* This is specifically for iOS 9, according to the private headers */
-(void)handleKeyUIEvent:(UIEvent *)event
{
/* This gets called twice with the same timestamp
* for each keypress, that's fine for polling
* but is bad for business with events. */
static double last_time_stamp;
2019-02-03 23:49:35 +00:00
2017-01-17 18:04:42 +00:00
if (last_time_stamp == event.timestamp)
return [super handleKeyUIEvent:event];
2019-02-03 23:49:35 +00:00
last_time_stamp = event.timestamp;
2019-02-03 23:49:35 +00:00
/* If the _hidEvent is null, [event _keyCode] will crash.
* (This happens with the on screen keyboard). */
if (event._hidEvent)
{
NSString *ch = (NSString*)event._privateInput;
uint32_t character = 0;
uint32_t mod = 0;
2019-02-03 23:49:35 +00:00
mod |= (event._modifierFlags & NSAlphaShiftKeyMask) ? RETROKMOD_CAPSLOCK : 0;
mod |= (event._modifierFlags & NSShiftKeyMask ) ? RETROKMOD_SHIFT : 0;
mod |= (event._modifierFlags & NSControlKeyMask ) ? RETROKMOD_CTRL : 0;
mod |= (event._modifierFlags & NSAlternateKeyMask ) ? RETROKMOD_ALT : 0;
mod |= (event._modifierFlags & NSCommandKeyMask ) ? RETROKMOD_META : 0;
mod |= (event._modifierFlags & NSNumericPadKeyMask) ? RETROKMOD_NUMLOCK : 0;
2019-02-03 23:49:35 +00:00
if (ch && ch.length != 0)
{
unsigned i;
character = [ch characterAtIndex:0];
2019-02-03 23:49:35 +00:00
apple_input_keyboard_event(event._isKeyDown,
(uint32_t)event._keyCode, 0, mod,
RETRO_DEVICE_KEYBOARD);
2019-02-03 23:49:35 +00:00
for (i = 1; i < ch.length; i++)
apple_input_keyboard_event(event._isKeyDown,
0, [ch characterAtIndex:i], mod,
RETRO_DEVICE_KEYBOARD);
}
2019-02-03 23:49:35 +00:00
apple_input_keyboard_event(event._isKeyDown,
(uint32_t)event._keyCode, character, mod,
RETRO_DEVICE_KEYBOARD);
}
2019-02-03 23:49:35 +00:00
[super handleKeyUIEvent:event];
}
2020-09-15 19:12:27 +00:00
/* This is for iOS versions < 9.0 */
- (id)_keyCommandForEvent:(UIEvent*)event
{
/* This gets called twice with the same timestamp
* for each keypress, that's fine for polling
* but is bad for business with events. */
static double last_time_stamp;
2019-02-03 23:49:35 +00:00
if (last_time_stamp == event.timestamp)
return [super _keyCommandForEvent:event];
last_time_stamp = event.timestamp;
2019-02-03 23:49:35 +00:00
/* If the _hidEvent is null, [event _keyCode] will crash.
* (This happens with the on screen keyboard). */
if (event._hidEvent)
{
NSString *ch = (NSString*)event._privateInput;
uint32_t character = 0;
uint32_t mod = 0;
2019-02-03 23:49:35 +00:00
mod |= (event._modifierFlags & NSAlphaShiftKeyMask) ? RETROKMOD_CAPSLOCK : 0;
mod |= (event._modifierFlags & NSShiftKeyMask ) ? RETROKMOD_SHIFT : 0;
mod |= (event._modifierFlags & NSControlKeyMask ) ? RETROKMOD_CTRL : 0;
mod |= (event._modifierFlags & NSAlternateKeyMask ) ? RETROKMOD_ALT : 0;
mod |= (event._modifierFlags & NSCommandKeyMask ) ? RETROKMOD_META : 0;
mod |= (event._modifierFlags & NSNumericPadKeyMask) ? RETROKMOD_NUMLOCK : 0;
2019-02-03 23:49:35 +00:00
if (ch && ch.length != 0)
{
unsigned i;
character = [ch characterAtIndex:0];
2015-11-29 15:26:13 +00:00
apple_input_keyboard_event(event._isKeyDown,
(uint32_t)event._keyCode, 0, mod,
RETRO_DEVICE_KEYBOARD);
2019-02-03 23:49:35 +00:00
for (i = 1; i < ch.length; i++)
2015-11-29 15:26:13 +00:00
apple_input_keyboard_event(event._isKeyDown,
0, [ch characterAtIndex:i], mod,
RETRO_DEVICE_KEYBOARD);
}
2019-02-03 23:49:35 +00:00
2015-11-29 15:26:13 +00:00
apple_input_keyboard_event(event._isKeyDown,
(uint32_t)event._keyCode, character, mod,
RETRO_DEVICE_KEYBOARD);
}
return [super _keyCommandForEvent:event];
}
#endif
#define GSEVENT_TYPE_KEYDOWN 10
#define GSEVENT_TYPE_KEYUP 11
- (void)sendEvent:(UIEvent *)event
{
int major, minor;
[super sendEvent:event];
if (event.allTouches.count)
handle_touch_event(event.allTouches.allObjects);
get_ios_version(&major, &minor);
2019-02-03 23:49:35 +00:00
#if __IPHONE_OS_VERSION_MAX_ALLOWED < 70000
if ((major < 7) && [event respondsToSelector:@selector(_gsEvent)])
{
/* Keyboard event hack for iOS versions prior to iOS 7.
*
* Derived from:
2020-09-15 19:12:27 +00:00
* http://nacho4d-nacho4d.blogspot.com/2012/01/
* catching-keyboard-events-in-ios.html
*/
const uint8_t *eventMem = objc_unretainedPointer([event performSelector:@selector(_gsEvent)]);
int eventType = eventMem ? *(int*)&eventMem[8] : 0;
switch (eventType)
{
case GSEVENT_TYPE_KEYDOWN:
2015-11-29 15:26:13 +00:00
case GSEVENT_TYPE_KEYUP:
apple_input_keyboard_event(eventType == GSEVENT_TYPE_KEYDOWN,
*(uint16_t*)&eventMem[0x3C], 0, 0, RETRO_DEVICE_KEYBOARD);
break;
}
}
#endif
}
@end
@implementation RetroArch_iOS
#pragma mark - ApplePlatform
2020-09-15 19:12:27 +00:00
-(id)renderView { return _renderView; }
-(bool)hasFocus { return YES; }
- (void)setViewType:(apple_view_type_t)vt
{
if (vt == _vt)
return;
_vt = vt;
if (_renderView != nil)
{
[_renderView removeFromSuperview];
_renderView = nil;
}
2020-09-14 19:54:46 +00:00
switch (vt)
{
#ifdef HAVE_COCOA_METAL
case APPLE_VIEW_TYPE_VULKAN:
2020-09-14 19:54:46 +00:00
case APPLE_VIEW_TYPE_METAL:
{
MetalView *v = [MetalView new];
v.paused = YES;
v.enableSetNeedsDisplay = NO;
#if TARGET_OS_IOS
2020-09-14 19:54:46 +00:00
v.multipleTouchEnabled = YES;
#endif
2020-09-14 19:54:46 +00:00
_renderView = v;
}
break;
#endif
2020-09-14 19:54:46 +00:00
case APPLE_VIEW_TYPE_OPENGL_ES:
_renderView = (BRIDGE GLKView*)glkitview_init();
break;
2020-09-14 19:54:46 +00:00
case APPLE_VIEW_TYPE_NONE:
2020-09-15 19:12:27 +00:00
default:
return;
}
2020-09-15 19:12:27 +00:00
_renderView.translatesAutoresizingMaskIntoConstraints = NO;
UIView *rootView = [CocoaView get].view;
[rootView addSubview:_renderView];
[[_renderView.topAnchor constraintEqualToAnchor:rootView.topAnchor] setActive:YES];
[[_renderView.bottomAnchor constraintEqualToAnchor:rootView.bottomAnchor] setActive:YES];
[[_renderView.leadingAnchor constraintEqualToAnchor:rootView.leadingAnchor] setActive:YES];
[[_renderView.trailingAnchor constraintEqualToAnchor:rootView.trailingAnchor] setActive:YES];
}
2020-09-15 19:12:27 +00:00
- (apple_view_type_t)viewType { return _vt; }
2020-09-15 19:12:27 +00:00
- (void)setVideoMode:(gfx_ctx_mode_t)mode
{
#ifdef HAVE_COCOA_METAL
2020-09-15 19:12:27 +00:00
MetalView *metalView = (MetalView*) _renderView;
CGFloat scale = [[UIScreen mainScreen] scale];
[metalView setDrawableSize:CGSizeMake(
_renderView.bounds.size.width * scale,
_renderView.bounds.size.height * scale
)];
#endif
}
2020-09-15 19:12:27 +00:00
- (void)setCursorVisible:(bool)v { /* no-op for iOS */ }
- (bool)setDisableDisplaySleep:(bool)disable { /* no-op for iOS */ return NO; }
+ (RetroArch_iOS*)get { return (RetroArch_iOS*)[[UIApplication sharedApplication] delegate]; }
2020-09-15 19:12:27 +00:00
-(NSString*)documentsDirectory
{
2020-09-15 19:12:27 +00:00
if (_documentsDirectory == nil)
{
2019-02-03 23:49:35 +00:00
#if TARGET_OS_IOS
2020-09-15 19:12:27 +00:00
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
#elif TARGET_OS_TV
2020-09-15 19:12:27 +00:00
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
2019-02-03 23:49:35 +00:00
#endif
2020-09-15 19:12:27 +00:00
_documentsDirectory = paths.firstObject;
}
return _documentsDirectory;
}
- (void)applicationDidFinishLaunching:(UIApplication *)application
{
2020-09-14 19:54:46 +00:00
NSError *error;
2016-04-05 18:58:17 +00:00
char arguments[] = "retroarch";
char *argv[] = {arguments, NULL};
2016-04-05 18:53:02 +00:00
int argc = 1;
apple_platform = self;
[self setDelegate:self];
2016-01-16 21:02:49 +00:00
/* Setup window */
2020-09-15 19:12:27 +00:00
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
[self.window makeKeyAndVisible];
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryAmbient error:&error];
2015-04-20 21:16:35 +00:00
[self refreshSystemConfig];
[self showGameView];
2016-01-16 21:02:49 +00:00
2016-04-05 19:22:23 +00:00
if (rarch_main(argc, argv, NULL))
apple_rarch_exited();
2020-09-15 19:12:27 +00:00
iterate_observer = CFRunLoopObserverCreate(0, kCFRunLoopBeforeWaiting,
true, 0, rarch_draw_observer, 0);
CFRunLoopAddObserver(CFRunLoopGetMain(), iterate_observer, kCFRunLoopCommonModes);
2019-02-03 23:49:35 +00:00
#ifdef HAVE_MFI
2020-09-15 19:12:27 +00:00
extern bool apple_gamecontroller_joypad_init(void *data);
apple_gamecontroller_joypad_init(NULL);
#endif
}
2020-09-15 19:12:27 +00:00
- (void)applicationDidEnterBackground:(UIApplication *)application { }
- (void)applicationWillTerminate:(UIApplication *)application
{
CFRunLoopObserverInvalidate(iterate_observer);
CFRelease(iterate_observer);
iterate_observer = NULL;
}
- (void)applicationDidBecomeActive:(UIApplication *)application
{
2020-02-19 20:05:38 +00:00
settings_t *settings = config_get_ptr();
bool ui_companion_start_on_boot = settings->bools.ui_companion_start_on_boot;
2019-02-03 23:49:35 +00:00
2020-09-15 19:12:27 +00:00
if (!ui_companion_start_on_boot)
[self showGameView];
}
-(BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<UIApplicationOpenURLOptionsKey, id> *)options {
NSFileManager *manager = [NSFileManager defaultManager];
NSString *filename = (NSString*)url.path.lastPathComponent;
NSError *error = nil;
NSString *destination = [self.documentsDirectory stringByAppendingPathComponent:filename];
// copy file to documents directory if its not already inside of documents directory
if ([url startAccessingSecurityScopedResource]) {
if (![[url path] containsString: self.documentsDirectory])
if (![manager fileExistsAtPath:destination])
if (![manager copyItemAtPath:[url path] toPath:destination error:&error])
printf("%s\n", [[error description] UTF8String]);
[url stopAccessingSecurityScopedResource];
}
return true;
}
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
#if TARGET_OS_IOS
[self setToolbarHidden:![[viewController toolbarItems] count] animated:YES];
#endif
[self refreshSystemConfig];
}
- (void)showGameView
{
2020-09-15 19:12:27 +00:00
[self popToRootViewControllerAnimated:NO];
#if TARGET_OS_IOS
[self setToolbarHidden:true animated:NO];
[[UIApplication sharedApplication] setStatusBarHidden:true withAnimation:UIStatusBarAnimationNone];
#endif
2019-02-03 23:49:35 +00:00
2020-09-15 19:12:27 +00:00
[[UIApplication sharedApplication] setIdleTimerDisabled:true];
2015-04-20 18:39:39 +00:00
[self.window setRootViewController:[CocoaView get]];
2020-09-15 19:12:27 +00:00
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 1.0 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
command_event(CMD_EVENT_AUDIO_START, NULL);
});
2015-08-05 10:06:37 +00:00
rarch_disable_ui();
}
- (IBAction)showPauseMenu:(id)sender
{
2015-08-05 09:41:41 +00:00
rarch_enable_ui();
#if TARGET_OS_IOS
[[UIApplication sharedApplication] setStatusBarHidden:false withAnimation:UIStatusBarAnimationNone];
#endif
2019-02-03 23:49:35 +00:00
[[UIApplication sharedApplication] setIdleTimerDisabled:false];
[self.window setRootViewController:self];
}
- (void)refreshSystemConfig
{
#if TARGET_OS_IOS
/* Get enabled orientations */
apple_frontend_settings.orientation_flags = UIInterfaceOrientationMaskAll;
2019-02-03 23:49:35 +00:00
if (string_is_equal(apple_frontend_settings.orientations, "landscape"))
2020-09-15 19:12:27 +00:00
apple_frontend_settings.orientation_flags =
UIInterfaceOrientationMaskLandscape;
else if (string_is_equal(apple_frontend_settings.orientations, "portrait"))
2020-09-15 19:12:27 +00:00
apple_frontend_settings.orientation_flags =
UIInterfaceOrientationMaskPortrait
| UIInterfaceOrientationMaskPortraitUpsideDown;
#endif
}
2020-09-15 19:12:27 +00:00
- (void)supportOtherAudioSessions { }
@end
int main(int argc, char *argv[])
{
@autoreleasepool {
return UIApplicationMain(argc, argv, NSStringFromClass([RApplication class]), NSStringFromClass([RetroArch_iOS class]));
}
}
2015-04-12 01:02:48 +00:00
2016-06-04 16:10:52 +00:00
static void apple_rarch_exited(void)
2015-04-21 09:51:35 +00:00
{
2020-09-15 19:12:27 +00:00
RetroArch_iOS *ap = (RetroArch_iOS *)apple_platform;
2019-02-03 23:49:35 +00:00
2020-09-15 19:12:27 +00:00
if (!ap)
return;
[ap showPauseMenu:ap];
2015-04-21 09:51:35 +00:00
}