mirror of
https://github.com/libretro/RetroArch
synced 2025-01-30 12:32:52 +00:00
458 lines
14 KiB
Objective-C
458 lines
14 KiB
Objective-C
/* RetroArch - A frontend for libretro.
|
|
* Copyright (C) 2013-2014 - Jason Fetters
|
|
* Copyright (C) 2011-2014 - Daniel De Matteis
|
|
*
|
|
* 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/>.
|
|
*/
|
|
|
|
#import "RetroArch_Apple.h"
|
|
#include "../../general.h"
|
|
|
|
// Define compatibility symbols and categories
|
|
#ifdef IOS
|
|
|
|
#ifdef HAVE_CAMERA
|
|
#include <AVFoundation/AVCaptureSession.h>
|
|
#include <AVFoundation/AVCaptureDevice.h>
|
|
#include <AVFoundation/AVCaptureOutput.h>
|
|
#include <AVFoundation/AVCaptureInput.h>
|
|
#include <AVFoundation/AVMediaFormat.h>
|
|
#include <CoreVideo/CVOpenGLESTextureCache.h>
|
|
#endif
|
|
|
|
#define APP_HAS_FOCUS ([[UIApplication sharedApplication] applicationState] == UIApplicationStateActive)
|
|
|
|
#define GLContextClass EAGLContext
|
|
#define GLAPIType GFX_CTX_OPENGL_ES_API
|
|
#define GLFrameworkID CFSTR("com.apple.opengles")
|
|
#define RAScreen UIScreen
|
|
|
|
@interface EAGLContext (OSXCompat) @end
|
|
@implementation EAGLContext (OSXCompat)
|
|
+ (void)clearCurrentContext { [EAGLContext setCurrentContext:nil]; }
|
|
- (void)makeCurrentContext { [EAGLContext setCurrentContext:self]; }
|
|
@end
|
|
|
|
#elif defined(OSX)
|
|
#define APP_HAS_FOCUS ([NSApp isActive])
|
|
|
|
#define GLContextClass NSOpenGLContext
|
|
#define GLAPIType GFX_CTX_OPENGL_API
|
|
#define GLFrameworkID CFSTR("com.apple.opengl")
|
|
#define RAScreen NSScreen
|
|
|
|
#define g_view g_instance // < RAGameView is a container on iOS; on OSX these are both the same object
|
|
|
|
@interface NSScreen (IOSCompat) @end
|
|
@implementation NSScreen (IOSCompat)
|
|
- (CGRect)bounds
|
|
{
|
|
CGRect cgrect = (CGRect)NSRectToCGRect(self.frame);
|
|
return CGRectMake(0, 0, CGRectGetWidth(cgrect), CGRectGetHeight(cgrect));
|
|
}
|
|
- (float) scale { return 1.0f; }
|
|
@end
|
|
|
|
#endif
|
|
|
|
#ifdef IOS
|
|
|
|
#include <GLKit/GLKit.h>
|
|
#include "../iOS/views.h"
|
|
#define ALMOST_INVISIBLE (.021f)
|
|
static GLKView *g_view;
|
|
static UIView *g_pause_indicator_view;
|
|
|
|
#elif defined(OSX)
|
|
|
|
static bool g_has_went_fullscreen;
|
|
static NSOpenGLPixelFormat* g_format;
|
|
|
|
#endif
|
|
|
|
static bool g_initialized;
|
|
static RAGameView* g_instance;
|
|
static GLContextClass* g_context;
|
|
|
|
static int g_fast_forward_skips;
|
|
static bool g_is_syncing = true;
|
|
|
|
|
|
@implementation RAGameView
|
|
+ (RAGameView*)get
|
|
{
|
|
if (!g_instance)
|
|
g_instance = [RAGameView new];
|
|
|
|
return g_instance;
|
|
}
|
|
|
|
#ifdef OSX
|
|
|
|
- (id)init
|
|
{
|
|
self = [super init];
|
|
[self setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable];
|
|
return self;
|
|
}
|
|
|
|
- (void)setFrame:(NSRect)frameRect
|
|
{
|
|
[super setFrame:frameRect];
|
|
|
|
if (g_view && g_context)
|
|
[g_context update];
|
|
}
|
|
|
|
- (void)display
|
|
{
|
|
[g_context flushBuffer];
|
|
}
|
|
|
|
// Stop the annoying sound when pressing a key
|
|
- (BOOL)acceptsFirstResponder
|
|
{
|
|
return YES;
|
|
}
|
|
|
|
- (BOOL)isFlipped
|
|
{
|
|
return YES;
|
|
}
|
|
|
|
- (void)keyDown:(NSEvent*)theEvent
|
|
{
|
|
}
|
|
|
|
#elif defined(IOS)
|
|
// < iOS Pause menu and lifecycle
|
|
- (id)init
|
|
{
|
|
UINib *xib;
|
|
self = [super init];
|
|
|
|
xib = (UINib*)[UINib nibWithNibName:BOXSTRING("PauseIndicatorView") bundle:nil];
|
|
g_pause_indicator_view = [[xib instantiateWithOwner:[RetroArch_iOS get] options:nil] lastObject];
|
|
|
|
g_view = [GLKView new];
|
|
g_view.multipleTouchEnabled = YES;
|
|
g_view.enableSetNeedsDisplay = NO;
|
|
[g_view addSubview:g_pause_indicator_view];
|
|
|
|
self.view = g_view;
|
|
|
|
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(showPauseIndicator) name:UIApplicationWillEnterForegroundNotification object:nil];
|
|
return self;
|
|
}
|
|
|
|
// Pause Menus
|
|
- (void)viewDidAppear:(BOOL)animated
|
|
{
|
|
[self showPauseIndicator];
|
|
}
|
|
|
|
- (void)showPauseIndicator
|
|
{
|
|
g_pause_indicator_view.alpha = 1.0f;
|
|
[NSObject cancelPreviousPerformRequestsWithTarget:g_instance];
|
|
[g_instance performSelector:@selector(hidePauseButton) withObject:g_instance afterDelay:3.0f];
|
|
}
|
|
|
|
- (void)viewWillLayoutSubviews
|
|
{
|
|
UIInterfaceOrientation orientation = self.interfaceOrientation;
|
|
CGRect screenSize = [[UIScreen mainScreen] bounds];
|
|
float width = ((int)orientation < 3) ? CGRectGetWidth(screenSize) : CGRectGetHeight(screenSize);
|
|
float height = ((int)orientation < 3) ? CGRectGetHeight(screenSize) : CGRectGetWidth(screenSize);
|
|
float tenpctw = width / 10.0f;
|
|
float tenpcth = height / 10.0f;
|
|
|
|
g_pause_indicator_view.frame = CGRectMake(tenpctw * 4.0f, 0.0f, tenpctw * 2.0f, tenpcth);
|
|
[g_pause_indicator_view viewWithTag:1].frame = CGRectMake(0, 0, tenpctw * 2.0f, tenpcth);
|
|
}
|
|
|
|
- (void)hidePauseButton
|
|
{
|
|
[UIView animateWithDuration:0.2
|
|
animations:^{ g_pause_indicator_view.alpha = ALMOST_INVISIBLE; }
|
|
completion:^(BOOL finished) { }
|
|
];
|
|
}
|
|
|
|
// NOTE: This version only runs on iOS6
|
|
- (NSUInteger)supportedInterfaceOrientations
|
|
{
|
|
return apple_frontend_settings.orientation_flags;
|
|
}
|
|
|
|
// NOTE: This version runs on iOS2-iOS5, but not iOS6
|
|
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
|
|
{
|
|
switch (interfaceOrientation)
|
|
{
|
|
case UIInterfaceOrientationPortrait:
|
|
return (apple_frontend_settings.orientation_flags & UIInterfaceOrientationMaskPortrait);
|
|
case UIInterfaceOrientationPortraitUpsideDown:
|
|
return (apple_frontend_settings.orientation_flags & UIInterfaceOrientationMaskPortraitUpsideDown);
|
|
case UIInterfaceOrientationLandscapeLeft:
|
|
return (apple_frontend_settings.orientation_flags & UIInterfaceOrientationMaskLandscapeLeft);
|
|
case UIInterfaceOrientationLandscapeRight:
|
|
return (apple_frontend_settings.orientation_flags & UIInterfaceOrientationMaskLandscapeRight);
|
|
}
|
|
|
|
return YES;
|
|
}
|
|
|
|
#ifdef HAVE_CAMERA
|
|
|
|
static AVCaptureSession *_session;
|
|
static NSString *_sessionPreset;
|
|
CVOpenGLESTextureCacheRef textureCache;
|
|
GLuint outputTexture;
|
|
static bool newFrame = false;
|
|
|
|
void event_process_camera_frame(void* pixelBufferPtr)
|
|
{
|
|
CVOpenGLESTextureRef renderTexture;
|
|
CVPixelBufferRef pixelBuffer = (CVPixelBufferRef)pixelBufferPtr;
|
|
CVReturn ret;
|
|
size_t width = CVPixelBufferGetWidth(pixelBuffer);
|
|
size_t height = CVPixelBufferGetHeight(pixelBuffer);
|
|
|
|
CVPixelBufferLockBaseAddress(pixelBuffer, 0);
|
|
|
|
//TODO - rewrite all this
|
|
// create a texture from our render target.
|
|
// textureCache will be what you previously made with CVOpenGLESTextureCacheCreate
|
|
ret = CVOpenGLESTextureCacheCreateTextureFromImage(kCFAllocatorDefault,
|
|
textureCache, pixelBuffer, NULL, GL_TEXTURE_2D,
|
|
GL_RGBA, (GLsizei)width, (GLsizei)height, GL_BGRA, GL_UNSIGNED_BYTE, 0, &renderTexture);
|
|
if (!renderTexture || ret)
|
|
{
|
|
RARCH_ERR("ioscamera: CVOpenGLESTextureCacheCreateTextureFromImage failed.\n");
|
|
return;
|
|
}
|
|
|
|
outputTexture = CVOpenGLESTextureGetName(renderTexture);
|
|
glBindTexture(GL_TEXTURE_2D, outputTexture);
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
|
|
|
|
CVPixelBufferUnlockBaseAddress(pixelBuffer, 0);
|
|
[[NSNotificationCenter defaultCenter] postNotificationName:@"NewCameraTextureReady" object:nil];
|
|
newFrame = true;
|
|
|
|
glBindTexture(GL_TEXTURE_2D, 0);
|
|
|
|
CVOpenGLESTextureCacheFlush(textureCache, 0);
|
|
CFRelease(renderTexture);
|
|
|
|
CFRelease(pixelBuffer);
|
|
pixelBuffer = 0;
|
|
}
|
|
|
|
- (void)captureOutput:(AVCaptureOutput *)captureOutput
|
|
didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer
|
|
fromConnection:(AVCaptureConnection *)connection
|
|
{
|
|
// TODO: Don't post if event queue is full
|
|
CVPixelBufferRef pixelBuffer = (CVPixelBufferRef)CVPixelBufferRetain(CMSampleBufferGetImageBuffer(sampleBuffer));
|
|
event_process_camera_frame(pixelBuffer);
|
|
}
|
|
|
|
- (void) onCameraInit
|
|
{
|
|
NSError *error;
|
|
AVCaptureVideoDataOutput * dataOutput;
|
|
AVCaptureDeviceInput *input;
|
|
AVCaptureDevice *videoDevice;
|
|
|
|
//FIXME - dehardcode this
|
|
int width = 640;
|
|
int height = 480;
|
|
|
|
#if COREVIDEO_USE_EAGLCONTEXT_CLASS_IN_API
|
|
CVReturn ret = CVOpenGLESTextureCacheCreate(kCFAllocatorDefault, NULL, g_context, NULL, &textureCache);
|
|
|
|
#else
|
|
CVReturn ret = CVOpenGLESTextureCacheCreate(kCFAllocatorDefault, NULL, (__bridge void *)g_context, NULL, &textureCache);
|
|
#endif
|
|
|
|
//-- Setup Capture Session.
|
|
_session = [[AVCaptureSession alloc] init];
|
|
[_session beginConfiguration];
|
|
|
|
// TODO: dehardcode this based on device capabilities
|
|
_sessionPreset = AVCaptureSessionPreset640x480;
|
|
|
|
//-- Set preset session size.
|
|
[_session setSessionPreset:_sessionPreset];
|
|
|
|
//-- Creata a video device and input from that Device. Add the input to the capture session.
|
|
videoDevice = (AVCaptureDevice*)[AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
|
|
if (videoDevice == nil)
|
|
assert(0);
|
|
|
|
//-- Add the device to the session.
|
|
input = (AVCaptureDeviceInput*)[AVCaptureDeviceInput deviceInputWithDevice:videoDevice error:&error];
|
|
if (error)
|
|
{
|
|
RARCH_ERR("video device input %s\n", error.localizedDescription.UTF8String);
|
|
assert(0);
|
|
}
|
|
|
|
[_session addInput:input];
|
|
|
|
//-- Create the output for the capture session.
|
|
dataOutput = (AVCaptureVideoDataOutput*)[[AVCaptureVideoDataOutput alloc] init];
|
|
[dataOutput setAlwaysDiscardsLateVideoFrames:NO]; // Probably want to set this to NO when recording
|
|
|
|
[dataOutput setVideoSettings:[NSDictionary dictionaryWithObject:[NSNumber numberWithInt:kCVPixelFormatType_32BGRA] forKey:(id)kCVPixelBufferPixelFormatTypeKey]];
|
|
|
|
// Set dispatch to be on the main thread so OpenGL can do things with the data
|
|
[dataOutput setSampleBufferDelegate:self queue:dispatch_get_main_queue()];
|
|
|
|
[_session addOutput:dataOutput];
|
|
[_session commitConfiguration];
|
|
}
|
|
|
|
- (void) onCameraStart
|
|
{
|
|
[_session startRunning];
|
|
}
|
|
|
|
- (void) onCameraStop
|
|
{
|
|
[_session stopRunning];
|
|
}
|
|
|
|
- (void) onCameraFree
|
|
{
|
|
CVOpenGLESTextureCacheFlush(textureCache, 0);
|
|
CFRelease(textureCache);
|
|
}
|
|
#endif
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_LOCATION
|
|
#include <CoreLocation/CoreLocation.h>
|
|
|
|
static CLLocationManager *locationManager;
|
|
static bool locationChanged;
|
|
static CLLocationDegrees currentLatitude;
|
|
static CLLocationDegrees currentLongitude;
|
|
static CLLocationAccuracy currentHorizontalAccuracy;
|
|
static CLLocationAccuracy currentVerticalAccuracy;
|
|
|
|
- (bool)onLocationHasChanged
|
|
{
|
|
bool hasChanged = locationChanged;
|
|
|
|
if (hasChanged)
|
|
locationChanged = false;
|
|
|
|
return hasChanged;
|
|
}
|
|
|
|
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
|
|
{
|
|
locationChanged = true;
|
|
currentLatitude = newLocation.coordinate.latitude;
|
|
currentLongitude = newLocation.coordinate.longitude;
|
|
currentHorizontalAccuracy = newLocation.horizontalAccuracy;
|
|
currentVerticalAccuracy = newLocation.verticalAccuracy;
|
|
RARCH_LOG("didUpdateToLocation - latitude %f, longitude %f\n", (float)currentLatitude, (float)currentLongitude);
|
|
}
|
|
|
|
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
|
|
{
|
|
CLLocation *location = (CLLocation*)[locations objectAtIndex:([locations count] - 1)];
|
|
|
|
locationChanged = true;
|
|
currentLatitude = [location coordinate].latitude;
|
|
currentLongitude = [location coordinate].longitude;
|
|
currentHorizontalAccuracy = location.horizontalAccuracy;
|
|
currentVerticalAccuracy = location.verticalAccuracy;
|
|
RARCH_LOG("didUpdateLocations - latitude %f, longitude %f\n", (float)currentLatitude, (float)currentLongitude);
|
|
}
|
|
|
|
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
|
|
{
|
|
RARCH_LOG("didFailWithError - %s\n", [[error localizedDescription] UTF8String]);
|
|
}
|
|
|
|
- (void)locationManagerDidPauseLocationUpdates:(CLLocationManager *)manager
|
|
{
|
|
RARCH_LOG("didPauseLocationUpdates\n");
|
|
}
|
|
|
|
- (void)locationManagerDidResumeLocationUpdates:(CLLocationManager *)manager
|
|
{
|
|
RARCH_LOG("didResumeLocationUpdates\n");
|
|
}
|
|
|
|
- (void)onLocationInit
|
|
{
|
|
// Create the location manager if this object does not
|
|
// already have one.
|
|
|
|
if (locationManager == nil)
|
|
locationManager = [[CLLocationManager alloc] init];
|
|
locationManager.delegate = self;
|
|
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
|
|
locationManager.distanceFilter = kCLDistanceFilterNone;
|
|
[locationManager startUpdatingLocation];
|
|
}
|
|
#endif
|
|
|
|
@end
|
|
|
|
static RAScreen* get_chosen_screen(void)
|
|
{
|
|
#if defined(OSX) && !defined(MAC_OS_X_VERSION_10_6)
|
|
return [NSScreen mainScreen];
|
|
#else
|
|
NSArray *screens;
|
|
if (g_settings.video.monitor_index >= RAScreen.screens.count)
|
|
{
|
|
RARCH_WARN("video_monitor_index is greater than the number of connected monitors; using main screen instead.\n");
|
|
return RAScreen.mainScreen;
|
|
}
|
|
|
|
screens = (NSArray*)RAScreen.screens;
|
|
return (RAScreen*)[screens objectAtIndex:g_settings.video.monitor_index];
|
|
#endif
|
|
}
|
|
|
|
|
|
#ifdef IOS
|
|
void apple_bind_game_view_fbo(void)
|
|
{
|
|
if (g_context)
|
|
[g_view bindDrawable];
|
|
}
|
|
|
|
#ifdef HAVE_CAMERA
|
|
#include "apple_camera_ios.c.inl"
|
|
#endif
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_LOCATION
|
|
#include "apple_location.c.inl"
|
|
#endif
|
|
|
|
#include "apple_gfx_context.c.inl"
|