2013-08-14 10:07:36 -04:00
|
|
|
/* RetroArch - A frontend for libretro.
|
|
|
|
* Copyright (C) 2013 - Jason Fetters
|
|
|
|
*
|
|
|
|
* 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 <pthread.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2013-11-22 15:30:02 +01:00
|
|
|
#include <objc/runtime.h>
|
|
|
|
|
2013-08-14 10:07:36 -04:00
|
|
|
#import "RetroArch_Apple.h"
|
|
|
|
#include "rarch_wrapper.h"
|
2013-09-05 01:20:56 -04:00
|
|
|
#include "apple/common/apple_input.h"
|
2013-08-14 10:07:36 -04:00
|
|
|
|
|
|
|
#include "file.h"
|
|
|
|
|
2013-12-13 19:23:22 -05:00
|
|
|
static void* const associated_core_key = (void*)&associated_core_key;
|
2013-11-22 15:30:02 +01:00
|
|
|
|
2013-08-14 10:07:36 -04:00
|
|
|
@interface RApplication : NSApplication
|
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation RApplication
|
|
|
|
|
|
|
|
- (void)sendEvent:(NSEvent *)event
|
|
|
|
{
|
|
|
|
[super sendEvent:event];
|
2013-12-11 13:47:57 -05:00
|
|
|
|
2013-12-14 20:35:47 -05:00
|
|
|
NSEventType event_type = [event type];
|
|
|
|
|
|
|
|
if (event_type == NSKeyDown || event_type == NSKeyUp)
|
|
|
|
apple_input_handle_key_event([event keyCode], event_type == GSEVENT_TYPE_KEYDOWN);
|
|
|
|
else if (event_type == NSFlagsChanged)
|
2013-12-11 13:47:57 -05:00
|
|
|
{
|
|
|
|
static uint32_t old_flags = 0;
|
2013-12-14 20:35:47 -05:00
|
|
|
uint32_t new_flags = [event modifierFlags];
|
2013-12-11 13:47:57 -05:00
|
|
|
bool down = (new_flags & old_flags) == old_flags;
|
|
|
|
old_flags = new_flags;
|
|
|
|
|
2013-12-14 20:35:47 -05:00
|
|
|
apple_input_handle_key_event([event keyCode], down);
|
2013-12-11 13:47:57 -05:00
|
|
|
}
|
2013-12-14 20:35:47 -05:00
|
|
|
else if (event_type == NSMouseMoved || event_type == NSLeftMouseDragged ||
|
|
|
|
event_type == NSRightMouseDragged || event_type == NSOtherMouseDragged)
|
2013-12-11 02:22:46 -05:00
|
|
|
{
|
|
|
|
// Relative
|
2013-12-14 20:35:47 -05:00
|
|
|
g_current_input_data.mouse_delta[0] += [event deltaX];
|
|
|
|
g_current_input_data.mouse_delta[1] += [event deltaY];
|
2013-12-11 02:22:46 -05:00
|
|
|
|
|
|
|
// Absolute
|
|
|
|
NSPoint pos = [[RAGameView get] convertPoint:[event locationInWindow] fromView:nil];
|
|
|
|
g_current_input_data.touches[0].screen_x = pos.x;
|
|
|
|
g_current_input_data.touches[0].screen_y = pos.y;
|
|
|
|
}
|
2013-12-14 20:35:47 -05:00
|
|
|
else if (event_type == NSLeftMouseDown || event_type == NSRightMouseDown || event_type == NSOtherMouseDown)
|
2013-12-11 02:22:46 -05:00
|
|
|
{
|
2013-12-14 20:35:47 -05:00
|
|
|
g_current_input_data.mouse_buttons |= 1 << [event buttonNumber];
|
2013-12-11 02:22:46 -05:00
|
|
|
g_current_input_data.touch_count = 1;
|
|
|
|
}
|
2013-12-14 20:35:47 -05:00
|
|
|
else if (event_type == NSLeftMouseUp || event_type == NSRightMouseUp || event_type == NSOtherMouseUp)
|
2013-12-11 02:22:46 -05:00
|
|
|
{
|
2013-12-14 20:35:47 -05:00
|
|
|
g_current_input_data.mouse_buttons &= ~(1 << [event buttonNumber]);
|
2013-12-11 02:22:46 -05:00
|
|
|
g_current_input_data.touch_count = 0;
|
|
|
|
}
|
2013-08-14 10:07:36 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
2013-12-13 19:23:22 -05:00
|
|
|
@interface RetroArch_OSX()
|
|
|
|
@property (nonatomic, retain) NSWindowController* settingsWindow;
|
|
|
|
@property (nonatomic, retain) NSWindow IBOutlet* coreSelectSheet;
|
|
|
|
@property (nonatomic, copy) NSString* file;
|
|
|
|
@property (nonatomic, copy) NSString* core;
|
|
|
|
@end
|
|
|
|
|
2013-08-14 10:07:36 -04:00
|
|
|
@implementation RetroArch_OSX
|
2013-12-13 19:23:22 -05:00
|
|
|
|
|
|
|
@synthesize window = _window;
|
|
|
|
@synthesize configDirectory = _configDirectory;
|
|
|
|
@synthesize globalConfigFile = _globalConfigFile;
|
|
|
|
@synthesize coreDirectory = _coreDirectory;
|
|
|
|
@synthesize settingsWindow = _settingsWindow;
|
|
|
|
@synthesize coreSelectSheet = _coreSelectSheet;
|
|
|
|
@synthesize file = _file;
|
|
|
|
@synthesize core = _core;
|
|
|
|
|
|
|
|
- (void)dealloc
|
|
|
|
{
|
|
|
|
[_window release];
|
|
|
|
[_configDirectory release];
|
|
|
|
[_globalConfigFile release];
|
|
|
|
[_coreDirectory release];
|
|
|
|
[_coreSelectSheet release];
|
|
|
|
[_settingsWindow release];
|
|
|
|
[_file release];
|
|
|
|
[_core release];
|
|
|
|
[super dealloc];
|
2013-08-14 10:07:36 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
+ (RetroArch_OSX*)get
|
|
|
|
{
|
|
|
|
return (RetroArch_OSX*)[[NSApplication sharedApplication] delegate];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
|
|
|
|
{
|
|
|
|
apple_platform = self;
|
|
|
|
_loaded = true;
|
|
|
|
|
2013-09-09 19:25:34 -04:00
|
|
|
NSArray* paths = NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES);
|
2013-12-13 19:23:22 -05:00
|
|
|
self.configDirectory = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"RetroArch"];
|
2013-09-09 19:25:34 -04:00
|
|
|
self.globalConfigFile = [NSString stringWithFormat:@"%@/retroarch.cfg", self.configDirectory];
|
2013-12-14 20:35:47 -05:00
|
|
|
self.coreDirectory = [[[NSBundle mainBundle] bundlePath] stringByAppendingPathComponent:@"Contents/Resources/modules"];
|
2013-09-09 19:25:34 -04:00
|
|
|
|
2013-12-14 20:35:47 -05:00
|
|
|
[self.window setAcceptsMouseMovedEvents: YES];
|
2013-12-13 19:23:22 -05:00
|
|
|
|
2013-12-14 20:35:47 -05:00
|
|
|
[[RAGameView get] setFrame: [[self.window contentView] bounds]];
|
|
|
|
[[self.window contentView] setAutoresizesSubviews:YES];
|
|
|
|
[[self.window contentView] addSubview:RAGameView.get];
|
|
|
|
[self.window makeFirstResponder:[RAGameView get]];
|
2013-08-14 10:07:36 -04:00
|
|
|
|
2013-12-13 19:23:22 -05:00
|
|
|
self.settingsWindow = [[[NSWindowController alloc] initWithWindowNibName:@"Settings"] autorelease];
|
2013-08-14 10:07:36 -04:00
|
|
|
|
|
|
|
// Create core select list
|
2013-12-14 20:35:47 -05:00
|
|
|
NSComboBox* cb = (NSComboBox*)[[self.coreSelectSheet contentView] viewWithTag:1];
|
2013-11-22 15:30:02 +01:00
|
|
|
|
2013-12-14 20:35:47 -05:00
|
|
|
apple_core_info_set_core_path([self.coreDirectory UTF8String]);
|
|
|
|
apple_core_info_set_config_path([self.configDirectory UTF8String]);
|
2013-11-22 15:30:02 +01:00
|
|
|
const core_info_list_t* cores = apple_core_info_list_get();
|
|
|
|
for (int i = 0; cores && i != cores->count; i ++)
|
|
|
|
{
|
2013-12-12 14:49:18 -05:00
|
|
|
NSString* desc = BOXSTRING(cores->list[i].display_name);
|
2013-11-22 15:30:02 +01:00
|
|
|
objc_setAssociatedObject(desc, associated_core_key, apple_get_core_id(&cores->list[i]), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
|
|
|
|
[cb addItemWithObjectValue:desc];
|
|
|
|
}
|
2013-08-14 10:07:36 -04:00
|
|
|
|
2013-12-14 20:35:47 -05:00
|
|
|
if ([cb numberOfItems])
|
2013-08-14 10:07:36 -04:00
|
|
|
[cb selectItemAtIndex:0];
|
|
|
|
else
|
2013-08-25 19:40:31 -04:00
|
|
|
apple_display_alert(@"No libretro cores were found.\nSelect \"Go->Cores Directory\" from the menu and place libretro dylib files there.", @"RetroArch");
|
2013-11-22 15:30:02 +01:00
|
|
|
|
2013-08-14 10:07:36 -04:00
|
|
|
// Run RGUI if needed
|
2013-08-27 12:07:59 -04:00
|
|
|
if (!_wantReload || apple_argv)
|
2013-08-14 10:07:36 -04:00
|
|
|
apple_run_core(nil, 0);
|
|
|
|
else
|
|
|
|
[self chooseCore];
|
|
|
|
|
|
|
|
_wantReload = false;
|
2013-12-11 02:22:46 -05:00
|
|
|
|
2013-08-14 10:07:36 -04:00
|
|
|
extern void osx_pad_init();
|
|
|
|
osx_pad_init();
|
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)theApplication
|
|
|
|
{
|
|
|
|
return YES;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication *)sender
|
|
|
|
{
|
|
|
|
_isTerminating = true;
|
|
|
|
|
|
|
|
if (apple_is_running)
|
|
|
|
apple_frontend_post_event(apple_event_basic_command, (void*)QUIT);
|
|
|
|
|
|
|
|
return apple_is_running ? NSTerminateCancel : NSTerminateNow;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
- (void)application:(NSApplication *)sender openFiles:(NSArray *)filenames
|
|
|
|
{
|
2013-12-14 20:35:47 -05:00
|
|
|
if ([filenames count] == 1 && [filenames objectAtIndex:0])
|
2013-08-14 10:07:36 -04:00
|
|
|
{
|
2013-12-13 19:23:22 -05:00
|
|
|
self.file = [filenames objectAtIndex:0];
|
2013-08-14 10:07:36 -04:00
|
|
|
|
|
|
|
if (!_loaded)
|
|
|
|
_wantReload = true;
|
|
|
|
else
|
|
|
|
[self chooseCore];
|
|
|
|
|
|
|
|
[sender replyToOpenOrPrint:NSApplicationDelegateReplySuccess];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
apple_display_alert(@"Cannot open multiple files", @"RetroArch");
|
|
|
|
[sender replyToOpenOrPrint:NSApplicationDelegateReplyFailure];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)openDocument:(id)sender
|
|
|
|
{
|
|
|
|
NSOpenPanel* panel = [NSOpenPanel openPanel];
|
2013-12-13 19:23:22 -05:00
|
|
|
[panel beginSheetModalForWindow:self.window completionHandler:^(NSInteger result)
|
2013-08-14 10:07:36 -04:00
|
|
|
{
|
2013-12-14 20:35:47 -05:00
|
|
|
[[NSApplication sharedApplication] stopModal];
|
2013-08-14 10:07:36 -04:00
|
|
|
|
2013-12-14 20:35:47 -05:00
|
|
|
if (result == NSOKButton && [panel URL])
|
2013-08-14 10:07:36 -04:00
|
|
|
{
|
2013-12-14 20:35:47 -05:00
|
|
|
self.file = [[panel URL] path];
|
2013-08-14 10:07:36 -04:00
|
|
|
[self performSelector:@selector(chooseCore) withObject:nil afterDelay:.5f];
|
|
|
|
}
|
|
|
|
}];
|
2013-12-14 20:35:47 -05:00
|
|
|
[[NSApplication sharedApplication] runModalForWindow:panel];
|
2013-08-14 10:07:36 -04:00
|
|
|
}
|
|
|
|
|
2013-12-13 19:23:22 -05:00
|
|
|
// This utility function will queue the self.core and self.file instance values for running.
|
2013-08-14 10:07:36 -04:00
|
|
|
// If the emulator thread is already running it will tell it to quit.
|
|
|
|
- (void)runCore
|
|
|
|
{
|
|
|
|
_wantReload = apple_is_running;
|
|
|
|
|
|
|
|
if (!apple_is_running)
|
2013-12-14 20:35:47 -05:00
|
|
|
apple_run_core(self.core, [self.file UTF8String]);
|
2013-08-14 10:07:36 -04:00
|
|
|
else
|
|
|
|
apple_frontend_post_event(apple_event_basic_command, (void*)QUIT);
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)chooseCore
|
|
|
|
{
|
2013-12-14 20:35:47 -05:00
|
|
|
[[NSApplication sharedApplication] beginSheet:self.coreSelectSheet modalForWindow:self.window modalDelegate:nil didEndSelector:nil contextInfo:nil];
|
|
|
|
[[NSApplication sharedApplication] runModalForWindow:self.coreSelectSheet];
|
2013-08-14 10:07:36 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
- (IBAction)coreWasChosen:(id)sender
|
|
|
|
{
|
2013-12-14 20:35:47 -05:00
|
|
|
[[NSApplication sharedApplication] stopModal];
|
|
|
|
[[NSApplication sharedApplication] endSheet:self.coreSelectSheet returnCode:0];
|
2013-12-13 19:23:22 -05:00
|
|
|
[self.coreSelectSheet orderOut:self];
|
2013-08-14 10:07:36 -04:00
|
|
|
|
|
|
|
if (_isTerminating)
|
|
|
|
return;
|
|
|
|
|
2013-12-14 20:35:47 -05:00
|
|
|
NSComboBox* cb = (NSComboBox*)[[self.coreSelectSheet contentView] viewWithTag:1];
|
|
|
|
self.core = objc_getAssociatedObject([cb objectValueOfSelectedItem], associated_core_key);
|
2013-08-14 10:07:36 -04:00
|
|
|
|
|
|
|
[self runCore];
|
|
|
|
}
|
|
|
|
|
|
|
|
#pragma mark RetroArch_Platform
|
2013-11-22 15:30:02 +01:00
|
|
|
- (void)loadingCore:(const NSString*)core withFile:(const char*)file
|
2013-08-14 10:07:36 -04:00
|
|
|
{
|
|
|
|
if (file)
|
2013-12-14 20:35:47 -05:00
|
|
|
[[NSDocumentController sharedDocumentController] noteNewRecentDocumentURL:[NSURL fileURLWithPath:BOXSTRING(file)]];
|
2013-08-14 10:07:36 -04:00
|
|
|
}
|
|
|
|
|
2013-11-22 15:30:02 +01:00
|
|
|
- (void)unloadingCore:(const NSString*)core
|
2013-08-14 10:07:36 -04:00
|
|
|
{
|
|
|
|
if (_isTerminating)
|
2013-12-14 20:35:47 -05:00
|
|
|
[[NSApplication sharedApplication] terminate:nil];
|
2013-08-14 10:07:36 -04:00
|
|
|
|
|
|
|
if (_wantReload)
|
2013-12-14 20:35:47 -05:00
|
|
|
apple_run_core(self.core, [self.file UTF8String]);
|
2013-08-14 10:07:36 -04:00
|
|
|
else if(apple_use_tv_mode)
|
|
|
|
apple_run_core(nil, 0);
|
|
|
|
else
|
2013-12-14 20:35:47 -05:00
|
|
|
[[NSApplication sharedApplication] terminate:nil];
|
2013-08-14 10:07:36 -04:00
|
|
|
|
|
|
|
_wantReload = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
#pragma mark Menus
|
2013-08-25 19:40:31 -04:00
|
|
|
- (IBAction)showCoresDirectory:(id)sender
|
|
|
|
{
|
2013-09-09 19:25:34 -04:00
|
|
|
[[NSWorkspace sharedWorkspace] openFile:self.coreDirectory];
|
2013-08-25 19:40:31 -04:00
|
|
|
}
|
|
|
|
|
2013-08-14 10:07:36 -04:00
|
|
|
- (IBAction)showPreferences:(id)sender
|
|
|
|
{
|
2013-12-14 20:35:47 -05:00
|
|
|
[NSApp runModalForWindow:[self.settingsWindow window]];
|
2013-08-14 10:07:36 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
- (IBAction)basicEvent:(id)sender
|
|
|
|
{
|
|
|
|
if (apple_is_running)
|
2013-12-14 20:35:47 -05:00
|
|
|
apple_frontend_post_event(&apple_event_basic_command, (void*)[sender tag]);
|
2013-08-14 10:07:36 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)alertDidEnd:(NSAlert *)alert returnCode:(NSInteger)returnCode contextInfo:(void *)contextInfo
|
|
|
|
{
|
2013-12-14 20:35:47 -05:00
|
|
|
[[NSApplication sharedApplication] stopModal];
|
2013-08-14 10:07:36 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
2013-08-27 12:07:59 -04:00
|
|
|
uint32_t current_argc = 0;
|
|
|
|
|
|
|
|
for (int i = 0; i != argc; i ++)
|
|
|
|
{
|
|
|
|
if (strcmp(argv[i], "--") == 0)
|
|
|
|
{
|
|
|
|
current_argc = 1;
|
|
|
|
apple_argv = malloc(sizeof(char*) * (argc + 1));
|
|
|
|
memset(apple_argv, 0, sizeof(char*) * (argc + 1));
|
|
|
|
apple_argv[0] = argv[0];
|
|
|
|
}
|
|
|
|
else if (current_argc)
|
|
|
|
{
|
|
|
|
apple_argv[current_argc ++] = argv[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-14 10:07:36 -04:00
|
|
|
return NSApplicationMain(argc, (const char **) argv);
|
|
|
|
}
|
|
|
|
|