2009-06-09 13:34:52 +02:00
|
|
|
//
|
|
|
|
// main.m
|
|
|
|
//
|
|
|
|
// Created by Gil Pedersen on Fri Jun 06 2003.
|
|
|
|
// Copyright (c) 2003 SoftWorkz. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
#import <Cocoa/Cocoa.h>
|
|
|
|
#import "EmuThread.h"
|
2013-06-22 08:11:06 +02:00
|
|
|
#import "PcsxrController.h"
|
2009-06-09 13:34:52 +02:00
|
|
|
#include <dlfcn.h>
|
|
|
|
//#import <sys/param.h>
|
|
|
|
#import <unistd.h>
|
|
|
|
#include "psxcommon.h"
|
|
|
|
#include "sio.h"
|
2013-01-09 02:50:38 +01:00
|
|
|
#include <IOKit/pwr_mgt/IOPMLib.h>
|
2012-12-15 22:07:55 +01:00
|
|
|
#import "hotkeys.h"
|
2013-01-09 02:50:38 +01:00
|
|
|
#import "ARCBridge.h"
|
2009-06-09 13:34:52 +02:00
|
|
|
|
2009-11-04 10:20:55 +01:00
|
|
|
static BOOL sysInited = NO;
|
2009-06-09 13:34:52 +02:00
|
|
|
//#define EMU_LOG
|
2011-11-12 20:44:47 +01:00
|
|
|
static IOPMAssertionID powerAssertion = kIOPMNullAssertionID;
|
2011-02-19 04:30:16 +01:00
|
|
|
|
2010-05-20 02:27:30 +02:00
|
|
|
int main(int argc, const char *argv[]) {
|
2009-06-09 13:34:52 +02:00
|
|
|
if ( argc >= 2 && strncmp (argv[1], "-psn", 4) == 0 ) {
|
|
|
|
char parentdir[MAXPATHLEN];
|
|
|
|
char *c;
|
|
|
|
|
2013-01-10 21:46:32 +01:00
|
|
|
strlcpy ( parentdir, argv[0], sizeof(parentdir) );
|
2009-06-09 13:34:52 +02:00
|
|
|
c = (char*) parentdir;
|
|
|
|
|
|
|
|
while (*c != '\0') /* go to end */
|
|
|
|
c++;
|
|
|
|
|
|
|
|
while (*c != '/') /* back up to parent */
|
|
|
|
c--;
|
|
|
|
|
|
|
|
*c++ = '\0'; /* cut off last part (binary name) */
|
|
|
|
|
|
|
|
assert ( chdir (parentdir) == 0 ); /* chdir to the binary app's parent */
|
|
|
|
assert ( chdir ("../../../") == 0 ); /* chdir to the .app's parent */
|
2013-06-22 08:11:06 +02:00
|
|
|
} else {
|
|
|
|
for (int i = 1; i < argc; i++) {
|
|
|
|
//All the other option will be handled in the app delegate's awakeFromNib
|
|
|
|
if (!strcasecmp("--help", argv[i])) {
|
|
|
|
fprintf(stdout, "%s\n", argv[0]);
|
|
|
|
ShowHelpAndExit(stdout, EXIT_SUCCESS);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-06-09 13:34:52 +02:00
|
|
|
|
|
|
|
strcpy(Config.BiosDir, "Bios/");
|
2010-01-23 09:44:23 +01:00
|
|
|
strcpy(Config.PatchesDir, "Patches/");
|
|
|
|
|
|
|
|
// Setup the X11 window
|
2009-06-09 13:34:52 +02:00
|
|
|
if (getenv("DISPLAY") == NULL)
|
|
|
|
setenv("DISPLAY", ":0.0", 0); // Default to first local display
|
|
|
|
|
|
|
|
return NSApplicationMain(argc, argv);
|
|
|
|
}
|
|
|
|
|
2009-11-04 10:20:55 +01:00
|
|
|
int SysInit() {
|
|
|
|
if (!sysInited) {
|
2009-06-09 13:34:52 +02:00
|
|
|
#ifdef EMU_LOG
|
|
|
|
#ifndef LOG_STDOUT
|
2009-11-04 10:20:55 +01:00
|
|
|
emuLog = fopen("emuLog.txt","wb");
|
2009-06-09 13:34:52 +02:00
|
|
|
#else
|
2009-11-04 10:20:55 +01:00
|
|
|
emuLog = stdout;
|
2009-06-09 13:34:52 +02:00
|
|
|
#endif
|
2009-11-04 10:20:55 +01:00
|
|
|
setvbuf(emuLog, NULL, _IONBF, 0);
|
2009-06-09 13:34:52 +02:00
|
|
|
#endif
|
|
|
|
|
2010-05-08 03:16:38 +02:00
|
|
|
if (EmuInit() != 0)
|
2009-06-09 13:34:52 +02:00
|
|
|
return -1;
|
|
|
|
|
2009-11-04 10:20:55 +01:00
|
|
|
sysInited = YES;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (LoadPlugins() == -1) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
LoadMcds(Config.Mcd1, Config.Mcd2);
|
2011-11-12 20:44:47 +01:00
|
|
|
|
2013-01-10 21:46:32 +01:00
|
|
|
IOReturn success = IOPMAssertionCreateWithName(kIOPMAssertionTypeNoDisplaySleep, kIOPMAssertionLevelOn, CFSTR("PSX Emu Running"), &powerAssertion);
|
2010-12-14 09:15:46 +01:00
|
|
|
if (success != kIOReturnSuccess) {
|
|
|
|
NSLog(@"Unable to stop sleep, error code %d", success);
|
|
|
|
}
|
2012-12-15 22:07:55 +01:00
|
|
|
|
|
|
|
attachHotkeys();
|
2011-11-12 20:44:47 +01:00
|
|
|
|
2009-11-04 10:20:55 +01:00
|
|
|
return 0;
|
2009-06-09 13:34:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void SysReset() {
|
|
|
|
[EmuThread resetNow];
|
2010-05-08 03:16:38 +02:00
|
|
|
//EmuReset();
|
2009-06-09 13:34:52 +02:00
|
|
|
}
|
|
|
|
|
2010-05-20 02:27:30 +02:00
|
|
|
void SysPrintf(const char *fmt, ...) {
|
2009-06-09 13:34:52 +02:00
|
|
|
va_list list;
|
|
|
|
char msg[512];
|
|
|
|
|
|
|
|
va_start(list, fmt);
|
|
|
|
vsprintf(msg, fmt, list);
|
|
|
|
va_end(list);
|
|
|
|
|
|
|
|
if (Config.PsxOut) printf ("%s", msg);
|
|
|
|
#ifdef EMU_LOG
|
|
|
|
#ifndef LOG_STDOUT
|
|
|
|
fprintf(emuLog, "%s", msg);
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2010-05-20 02:27:30 +02:00
|
|
|
void SysMessage(const char *fmt, ...) {
|
2009-06-09 13:34:52 +02:00
|
|
|
va_list list;
|
2009-11-29 14:31:23 +01:00
|
|
|
|
2013-06-22 21:55:35 +02:00
|
|
|
NSString *locFmtString = NSLocalizedString(@(fmt), nil);
|
2009-11-29 14:31:23 +01:00
|
|
|
|
2009-06-09 13:34:52 +02:00
|
|
|
va_start(list, fmt);
|
2011-11-09 21:49:54 +01:00
|
|
|
NSString *msg = [[NSString alloc] initWithFormat:locFmtString arguments:list];
|
2009-06-09 13:34:52 +02:00
|
|
|
va_end(list);
|
2011-11-09 21:49:54 +01:00
|
|
|
|
|
|
|
NSDictionary *userInfo = [NSDictionary dictionaryWithObject:msg forKey:NSLocalizedFailureReasonErrorKey];
|
2013-06-22 21:55:35 +02:00
|
|
|
dispatch_sync(dispatch_get_main_queue(), ^{
|
|
|
|
[NSApp presentError:[NSError errorWithDomain:@"Unknown Domain" code:-1 userInfo:userInfo]];
|
|
|
|
});
|
2011-11-09 21:49:54 +01:00
|
|
|
|
2013-01-09 02:50:38 +01:00
|
|
|
RELEASEOBJ(msg);
|
2009-06-09 13:34:52 +02:00
|
|
|
}
|
|
|
|
|
2010-05-20 02:27:30 +02:00
|
|
|
void *SysLoadLibrary(const char *lib) {
|
2011-11-09 21:49:54 +01:00
|
|
|
NSBundle *bundle = [NSBundle bundleWithPath:[[NSFileManager defaultManager] stringWithFileSystemRepresentation:lib length:strlen(lib)]];
|
2009-11-29 14:31:23 +01:00
|
|
|
if (bundle != nil) {
|
|
|
|
return dlopen([[bundle executablePath] fileSystemRepresentation], RTLD_LAZY /*RTLD_NOW*/);
|
|
|
|
}
|
2009-06-09 13:34:52 +02:00
|
|
|
return dlopen(lib, RTLD_LAZY);
|
|
|
|
}
|
|
|
|
|
2010-05-20 02:27:30 +02:00
|
|
|
void *SysLoadSym(void *lib, const char *sym) {
|
2009-06-09 13:34:52 +02:00
|
|
|
return dlsym(lib, sym);
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *SysLibError() {
|
2013-07-05 09:11:57 +02:00
|
|
|
#ifdef DEBUG
|
|
|
|
const char *theErr = dlerror();
|
|
|
|
if (theErr) {
|
|
|
|
NSLog(@"Error loading binary: %s", theErr);
|
|
|
|
}
|
|
|
|
return theErr;
|
|
|
|
#else
|
2009-06-09 13:34:52 +02:00
|
|
|
return dlerror();
|
2013-07-05 09:11:57 +02:00
|
|
|
#endif
|
2009-06-09 13:34:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void SysCloseLibrary(void *lib) {
|
2011-12-18 01:46:51 +01:00
|
|
|
dlclose(lib);
|
2009-06-09 13:34:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Called periodically from the emu thread
|
2010-05-20 02:27:30 +02:00
|
|
|
void SysUpdate() {
|
2011-06-20 00:57:11 +02:00
|
|
|
PAD1_keypressed();
|
|
|
|
PAD2_keypressed();
|
2009-06-09 13:34:52 +02:00
|
|
|
[emuThread handleEvents];
|
|
|
|
}
|
|
|
|
|
2010-05-20 02:27:30 +02:00
|
|
|
// Returns to the Gui
|
|
|
|
void SysRunGui() {
|
2011-11-12 08:05:45 +01:00
|
|
|
if (powerAssertion != kIOPMNullAssertionID) {
|
2010-12-14 09:15:46 +01:00
|
|
|
IOPMAssertionRelease(powerAssertion);
|
2011-11-12 08:05:45 +01:00
|
|
|
powerAssertion = kIOPMNullAssertionID;
|
2010-12-14 09:15:46 +01:00
|
|
|
}
|
2009-06-09 13:34:52 +02:00
|
|
|
}
|
2010-05-20 02:27:30 +02:00
|
|
|
|
|
|
|
// Close mem and plugins
|
|
|
|
void SysClose() {
|
2010-05-08 03:16:38 +02:00
|
|
|
EmuShutdown();
|
2013-01-09 02:50:38 +01:00
|
|
|
//ReleasePlugins();
|
2011-02-19 03:25:15 +01:00
|
|
|
|
2011-11-12 08:05:45 +01:00
|
|
|
if (powerAssertion != kIOPMNullAssertionID) {
|
2010-12-14 09:15:46 +01:00
|
|
|
IOPMAssertionRelease(powerAssertion);
|
2011-11-12 08:05:45 +01:00
|
|
|
powerAssertion = kIOPMNullAssertionID;
|
2010-12-14 09:15:46 +01:00
|
|
|
}
|
2009-06-09 13:34:52 +02:00
|
|
|
|
|
|
|
if (emuLog != NULL) fclose(emuLog);
|
2009-11-04 10:20:55 +01:00
|
|
|
|
|
|
|
sysInited = NO;
|
2012-12-15 22:07:55 +01:00
|
|
|
detachHotkeys();
|
2013-06-22 08:11:06 +02:00
|
|
|
if (((PcsxrController *)[NSApp delegate]).endAtEmuClose) {
|
|
|
|
[NSApp stop:nil];
|
|
|
|
}
|
2009-06-09 13:34:52 +02:00
|
|
|
}
|
|
|
|
|
2010-05-20 02:27:30 +02:00
|
|
|
void OnFile_Exit() {
|
2009-06-09 13:34:52 +02:00
|
|
|
SysClose();
|
|
|
|
exit(0);
|
2009-06-18 15:38:28 +02:00
|
|
|
}
|
2011-11-17 02:58:25 +01:00
|
|
|
|
2013-03-03 01:15:09 +01:00
|
|
|
char* Pcsxr_locale_text(char* toloc){
|
2011-11-17 02:58:25 +01:00
|
|
|
NSBundle *mainBundle = [NSBundle mainBundle];
|
|
|
|
NSString *origString = nil, *transString = nil;
|
2013-06-22 21:55:35 +02:00
|
|
|
origString = @(toloc);
|
2011-11-17 02:58:25 +01:00
|
|
|
transString = [mainBundle localizedStringForKey:origString value:nil table:nil];
|
2013-06-22 08:11:06 +02:00
|
|
|
return (char*)[transString UTF8String];
|
2011-11-17 02:58:25 +01:00
|
|
|
}
|