(Apple) Start to clean the objective-c code

This commit is contained in:
meancoot 2013-09-09 19:25:34 -04:00
parent ad7c442e8c
commit c3909c6468
13 changed files with 60 additions and 129 deletions

View File

@ -36,7 +36,10 @@
- (void)loadingCore:(RAModuleInfo*)core withFile:(const char*)file;
- (void)unloadingCore:(RAModuleInfo*)core;
- (NSString*)configPath;
@property (strong, nonatomic) NSString* configDirectory; // e.g. /var/mobile/Documents/.RetroArch
@property (strong, nonatomic) NSString* globalConfigFile; // e.g. /var/mobile/Documents/.RetroArch/retroarch.cfg
@property (strong, nonatomic) NSString* coreDirectory; // e.g. /Applications/RetroArch.app/modules
@end

View File

@ -20,14 +20,6 @@
#include "rarch_wrapper.h"
#include "apple/common/apple_input.h"
// If USE_XATTR is defined any loaded file will get a com.RetroArch.Core extended attribute
// specifying which core was used to load.
//#define USE_XATTR
#if defined(USE_XATTR)
#include "sys/xattr.h"
#endif
#include "file.h"
@interface RApplication : NSApplication
@ -66,6 +58,11 @@
apple_platform = self;
_loaded = true;
NSArray* paths = NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES);
self.configDirectory = [paths[0] stringByAppendingPathComponent:@"RetroArch"];
self.globalConfigFile = [NSString stringWithFormat:@"%@/retroarch.cfg", self.configDirectory];
self.coreDirectory = [NSBundle.mainBundle.bundlePath stringByAppendingPathComponent:@"Contents/Resources/modules"];
[window setCollectionBehavior:NSWindowCollectionBehaviorFullScreenPrimary];
RAGameView.get.frame = [window.contentView bounds];
@ -76,7 +73,7 @@
// Create core select list
NSComboBox* cb = (NSComboBox*)[_coreSelectSheet.contentView viewWithTag:1];
for (RAModuleInfo* i in RAModuleInfo.getModules)
for (RAModuleInfo* i in apple_get_modules())
[cb addItemWithObjectValue:i];
if (cb.numberOfItems)
@ -162,23 +159,6 @@
- (void)chooseCore
{
#ifdef USE_XATTR
char stored_name[PATH_MAX];
if (getxattr(_file.UTF8String, "com.RetroArch.Core", stored_name, PATH_MAX, 0, 0) > 0)
{
for (RAModuleInfo* i in RAModuleInfo.getModules)
{
const char* core_name = i.path.lastPathComponent.UTF8String;
if (strcmp(core_name, stored_name) == 0)
{
_core = i;
[self runCore];
return;
}
}
}
#endif
[NSApplication.sharedApplication beginSheet:_coreSelectSheet modalForWindow:window modalDelegate:nil didEndSelector:nil contextInfo:nil];
[NSApplication.sharedApplication runModalForWindow:_coreSelectSheet];
}
@ -202,14 +182,7 @@
- (void)loadingCore:(RAModuleInfo*)core withFile:(const char*)file
{
if (file)
{
[NSDocumentController.sharedDocumentController noteNewRecentDocumentURL:[NSURL fileURLWithPath:[NSString stringWithUTF8String:file]]];
#ifdef USE_XATTR
const char* core_name = core.path.lastPathComponent.UTF8String;
setxattr(file, "com.RetroArch.Core", core_name, strlen(core_name) + 1, 0, 0);
#endif
}
[NSDocumentController.sharedDocumentController noteNewRecentDocumentURL:[NSURL fileURLWithPath:@(file)]];
}
- (void)unloadingCore:(RAModuleInfo*)core
@ -227,26 +200,10 @@
_wantReload = false;
}
- (NSString*)configPath
{
return [[self retroarchConfigPath] stringByAppendingPathComponent:@"retroarch.cfg"];
}
- (NSString*)retroarchConfigPath
{
NSArray* paths = NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES);
return [paths[0] stringByAppendingPathComponent:@"RetroArch"];
}
- (NSString*)corePath
{
return [NSBundle.mainBundle.bundlePath stringByAppendingPathComponent:@"Contents/Resources/modules"];
}
#pragma mark Menus
- (IBAction)showCoresDirectory:(id)sender
{
[[NSWorkspace sharedWorkspace] openFile:self.corePath];
[[NSWorkspace sharedWorkspace] openFile:self.coreDirectory];
}
- (IBAction)showPreferences:(id)sender

View File

@ -343,7 +343,7 @@ static const char* get_axis_name(const rarch_setting_t* setting)
- (void)load
{
config_file_t* conf = config_file_new([RetroArch_OSX get].configPath.UTF8String);
config_file_t* conf = config_file_new(apple_platform.globalConfigFile.UTF8String);
for (int i = 0; setting_data[i].type; i ++)
{
@ -372,7 +372,7 @@ static const char* get_axis_name(const rarch_setting_t* setting)
- (void)windowWillClose:(NSNotification *)notification
{
config_file_t* conf = config_file_new([RetroArch_OSX get].configPath.UTF8String);
config_file_t* conf = config_file_new(apple_platform.globalConfigFile.UTF8String);
conf = conf ? conf : config_file_new(0);
for (int i = 0; setting_data[i].type; i ++)
@ -397,7 +397,7 @@ static const char* get_axis_name(const rarch_setting_t* setting)
default: break;
}
}
config_file_write(conf, [RetroArch_OSX get].configPath.UTF8String);
config_file_write(conf, apple_platform.globalConfigFile.UTF8String);
config_file_free(conf);
apple_refresh_config();

View File

@ -22,22 +22,22 @@
#include "conf/config_file.h"
#include "core_info.h"
extern NSArray* apple_get_modules();
@interface RAModuleInfo : NSObject
@property (strong) NSString* path;
@property NSString* path; // e.g. /path/to/corename_libretro.dylib
@property NSString* baseName; // e.g. corename_libretro
@property core_info_t* info;
@property config_file_t* data;
@property (strong) NSString* description;
@property (strong) NSString* customConfigPath;
@property NSString* description; // Friendly name from config file, else just the filename
@property NSString* customConfigFile; // Path where custom config file would reside
@property NSString* configFile; // Path to effective config file
+ (NSArray*)getModules;
- (bool)supportsFileAtPath:(NSString*)path;
+ (NSString*)globalConfigPath;
- (void)createCustomConfig;
- (void)deleteCustomConfig;
- (bool)hasCustomConfig;
- (NSString*)configPath;
@end

View File

@ -22,12 +22,11 @@
static NSMutableArray* moduleList;
static core_info_list_t* coreList;
@implementation RAModuleInfo
+ (NSArray*)getModules
NSArray* apple_get_modules()
{
if (!moduleList)
{
coreList = get_core_info_list(apple_platform.corePath.UTF8String);
coreList = get_core_info_list(apple_platform.coreDirectory.UTF8String);
if (!coreList)
return nil;
@ -39,13 +38,13 @@ static core_info_list_t* coreList;
core_info_t* core = &coreList->list[i];
RAModuleInfo* newInfo = [RAModuleInfo new];
newInfo.path = [NSString stringWithUTF8String:core->path];
newInfo.path = @(core->path);
newInfo.baseName = newInfo.path.lastPathComponent.stringByDeletingPathExtension;
newInfo.info = core;
newInfo.data = core->data;
newInfo.description = [NSString stringWithUTF8String:core->display_name];
NSString* baseName = newInfo.path.lastPathComponent.stringByDeletingPathExtension;
newInfo.customConfigPath = [NSString stringWithFormat:@"%@/%@.cfg", apple_platform.retroarchConfigPath, baseName];
newInfo.description = @(core->display_name);
newInfo.customConfigFile = [NSString stringWithFormat:@"%@/%@.cfg", apple_platform.configDirectory, newInfo.baseName];
newInfo.configFile = newInfo.hasCustomConfig ? newInfo.customConfigFile : apple_platform.globalConfigFile;
[moduleList addObject:newInfo];
}
@ -59,9 +58,7 @@ static core_info_list_t* coreList;
return moduleList;
}
- (void)dealloc
{
}
@implementation RAModuleInfo
- (id)copyWithZone:(NSZone *)zone
{
@ -73,35 +70,21 @@ static core_info_list_t* coreList;
return does_core_support_file(self.info, path.UTF8String);
}
+ (NSString*)globalConfigPath
{
static NSString* path;
if (!path)
path = [NSString stringWithFormat:@"%@/retroarch.cfg", apple_platform.retroarchConfigPath];
return path;
}
- (void)createCustomConfig
{
if (!self.hasCustomConfig)
[NSFileManager.defaultManager copyItemAtPath:RAModuleInfo.globalConfigPath toPath:self.customConfigPath error:nil];
[NSFileManager.defaultManager copyItemAtPath:apple_platform.globalConfigFile toPath:self.customConfigFile error:nil];
}
- (void)deleteCustomConfig
{
if (self.hasCustomConfig)
[NSFileManager.defaultManager removeItemAtPath:self.customConfigPath error:nil];
[NSFileManager.defaultManager removeItemAtPath:self.customConfigFile error:nil];
}
- (bool)hasCustomConfig
{
return path_file_exists(self.customConfigPath.UTF8String);
}
- (NSString*)configPath
{
return self.hasCustomConfig ? self.customConfigPath : RAModuleInfo.globalConfigPath;
return path_file_exists(self.customConfigFile.UTF8String);
}
@end
@ -148,7 +131,7 @@ static NSString* build_string_pair(NSString* stringA, NSString* stringB)
for (int i = 0; i < firmwareCount; i ++)
{
NSString* path = objc_get_value_from_config(_data.data, [NSString stringWithFormat:@"firmware%d_path", i + 1], @"Unspecified");
path = [path stringByReplacingOccurrencesOfString:@"%sysdir%" withString:RetroArch_iOS.get.systemDirectory];
path = [path stringByReplacingOccurrencesOfString:@"%sysdir%" withString:[RetroArch_iOS get].systemDirectory];
[firmwareSection addObject:build_string_pair(objc_get_value_from_config(_data.data, [NSString stringWithFormat:@"firmware%d_desc", i + 1], @"Unspecified"), path)];
}

View File

@ -28,8 +28,9 @@
- (void)loadingCore:(RAModuleInfo*)core withFile:(const char*)file;
- (void)unloadingCore:(RAModuleInfo*)core;
- (NSString*)retroarchConfigPath; // < This returns the directory that contains retroarch.cfg and other custom configs
- (NSString*)corePath;
- (NSString*)configDirectory; // < This returns the directory that contains retroarch.cfg and other custom configs
- (NSString*)globalConfigFile; // < This is the full path to retroarch.cfg
- (NSString*)coreDirectory; // < This is the default path to where libretro cores are installed
@end
#ifdef IOS

View File

@ -25,8 +25,6 @@
char** apple_argv;
//#define HAVE_DEBUG_FILELOG
id<RetroArch_Platform> apple_platform;
void apple_event_basic_command(void* userdata)
@ -134,27 +132,19 @@ void apple_run_core(RAModuleInfo* core, const char* file)
if (!apple_argv)
{
static const char* argv[] = { "retroarch", "-c", config_path, "-L", core_path, file_path, 0 };
NSString* config_to_use = apple_core ? apple_core.configFile : apple_platform.globalConfigFile;
strlcpy(config_path, config_to_use.UTF8String, sizeof(config_path));
if (apple_core)
strlcpy(config_path, apple_core.configPath.UTF8String, sizeof(config_path));
else
strlcpy(config_path, RAModuleInfo.globalConfigPath.UTF8String, sizeof(config_path));
static const char* const argv_game[] = { "retroarch", "-c", config_path, "-L", core_path, file_path, 0 };
static const char* const argv_menu[] = { "retroarch", "-c", config_path, "--menu", 0 };
if (file && core)
{
argv[3] = "-L";
argv[4] = core_path;
strlcpy(core_path, apple_core.path.UTF8String, sizeof(core_path));
strlcpy(file_path, file, sizeof(file_path));
}
else
{
argv[3] = "--menu";
argv[4] = 0;
}
apple_argv = (char**)argv;
apple_argv = (char**)((file && core) ? argv_game : argv_menu);
}
if (pthread_create(&apple_retro_thread, 0, rarch_main_spring, apple_argv))

View File

@ -57,7 +57,7 @@ NSString* objc_get_value_from_config(config_file_t* config, NSString* name, NSSt
if (config)
config_get_string(config, [name UTF8String], &data);
NSString* result = data ? [NSString stringWithUTF8String:data] : defaultValue;
NSString* result = data ? @(data) : defaultValue;
free(data);
return result;
}

View File

@ -34,7 +34,7 @@ void ios_add_log_message(const char* format, ...)
va_start(args, format);
vsnprintf(buffer, 512, format, args);
va_end(args);
[g_messages addObject:[NSString stringWithUTF8String: buffer]];
[g_messages addObject:@(buffer)];
pthread_mutex_unlock(&g_lock);
}

View File

@ -101,7 +101,7 @@
section = contents->elems[i].attr.b ? 0 : section;
RADirectoryItem* item = RADirectoryItem.new;
item.path = [NSString stringWithUTF8String:contents->elems[i].data];
item.path = @(contents->elems[i].data);
item.isDirectory = contents->elems[i].attr.b;
[sectionLists[section] addObject:item];
}
@ -221,7 +221,7 @@
_delegate = delegate;
// Load the modules with their data
NSArray* moduleList = [RAModuleInfo getModules];
NSArray* moduleList = apple_get_modules();
NSMutableArray* supported = [NSMutableArray arrayWithObject:@"Suggested Cores"];
NSMutableArray* other = [NSMutableArray arrayWithObject:@"Other Cores"];

View File

@ -32,10 +32,13 @@
- (void)loadingCore:(RAModuleInfo*)core withFile:(const char*)file;
- (void)unloadingCore:(RAModuleInfo*)core;
- (NSString*)retroarchConfigPath;
- (void)refreshSystemConfig;
@property (strong, nonatomic) NSString* configDirectory; // e.g. /var/mobile/Documents/.RetroArch
@property (strong, nonatomic) NSString* globalConfigFile; // e.g. /var/mobile/Documents/.RetroArch/retroarch.cfg
@property (strong, nonatomic) NSString* coreDirectory; // e.g. /Applications/RetroArch.app/modules
@property (strong, nonatomic) NSString* documentsDirectory; // e.g. /var/mobile/Documents
@property (strong, nonatomic) NSString* systemDirectory; // e.g. /var/mobile/Documents/.RetroArch
@property (strong, nonatomic) NSString* systemConfigPath; // e.g. /var/mobile/Documents/.RetroArch/frontend.cfg

View File

@ -113,6 +113,10 @@ static void handle_touch_event(NSArray* touches)
self.systemDirectory = [self.documentsDirectory stringByAppendingPathComponent:@".RetroArch"];
self.systemConfigPath = [self.systemDirectory stringByAppendingPathComponent:@"frontend.cfg"];
self.configDirectory = self.systemDirectory;
self.globalConfigFile = [NSString stringWithFormat:@"%@/retroarch.cfg", self.configDirectory];
self.coreDirectory = [NSBundle.mainBundle.bundlePath stringByAppendingPathComponent:@"modules"];
if (!path_make_and_check_directory(self.documentsDirectory.UTF8String, 0755, R_OK | W_OK | X_OK))
apple_display_alert([NSString stringWithFormat:@"Failed to create or access base directory: %@", self.documentsDirectory], 0);
else if (!path_make_and_check_directory(self.systemDirectory.UTF8String, 0755, R_OK | W_OK | X_OK))
@ -122,7 +126,7 @@ static void handle_touch_event(NSArray* touches)
// Warn if there are no cores present
if ([RAModuleInfo getModules].count == 0)
if (apple_get_modules().count == 0)
apple_display_alert(@"No libretro cores were found. You will not be able to play any games.", 0);
}
@ -252,16 +256,6 @@ static void handle_touch_event(NSArray* touches)
btpad_set_inquiry_state(true);
}
- (NSString*)retroarchConfigPath
{
return self.systemDirectory;
}
- (NSString*)corePath
{
return [NSBundle.mainBundle.bundlePath stringByAppendingPathComponent:@"modules"];
}
#pragma mark FRONTEND CONFIG
- (void)refreshSystemConfig
{

View File

@ -275,7 +275,7 @@ static NSArray* build_input_port_group(config_file_t* config, uint32_t player)
- (id)initWithModule:(RAModuleInfo*)module
{
_module = module;
_configPath = _module ? _module.configPath : RAModuleInfo.globalConfigPath;
_configPath = _module ? _module.configFile : apple_platform.globalConfigFile;
config_file_t* config = config_file_new([_configPath UTF8String]);
@ -408,7 +408,7 @@ static void bluetooth_option_changed(RASettingData* setting)
[modules addObject:@"Cores"];
[modules addObject:custom_action(@"Global Core Config", nil, nil, 0)];
NSArray* moduleList = [RAModuleInfo getModules];
NSArray* moduleList = apple_get_modules();
for (RAModuleInfo* i in moduleList)
{
[modules addObject:custom_action(i.description, nil, i, reload_core_config_state)];
@ -843,7 +843,7 @@ static void bluetooth_option_changed(RASettingData* setting)
int32_t value = 0;
if ((value = apple_input_find_any_key()))
_value->value = [NSString stringWithUTF8String:apple_keycode_hidusage_to_name(value)];
_value->value = @(apple_keycode_hidusage_to_name(value));
else if ((value = apple_input_find_any_button(0)) >= 0)
_value->button_bind = [NSString stringWithFormat:@"%d", value];
else if ((value = apple_input_find_any_axis(0)))