pcsxr-1.9.92
[pcsx_rearmed.git] / macosx / main.m
1 //
2 //  main.m
3 //
4 //  Created by Gil Pedersen on Fri Jun 06 2003.
5 //  Copyright (c) 2003 SoftWorkz. All rights reserved.
6 //
7
8 #import <Cocoa/Cocoa.h>
9 #import <Carbon/Carbon.h>
10 #import "EmuThread.h"
11 #include <dlfcn.h>
12 //#import <sys/param.h>
13 #import <unistd.h>
14 #include "psxcommon.h"
15 #include "sio.h"
16
17 static BOOL sysInited = NO;
18 //#define EMU_LOG
19
20 int main(int argc, const char *argv[]) {
21     if ( argc >= 2 && strncmp (argv[1], "-psn", 4) == 0 ) {
22         char parentdir[MAXPATHLEN];
23         char *c;
24
25         strncpy ( parentdir, argv[0], sizeof(parentdir) );
26         c = (char*) parentdir;
27
28         while (*c != '\0')     /* go to end */
29                c++;
30
31         while (*c != '/')      /* back up to parent */
32                c--;
33
34         *c++ = '\0';           /* cut off last part (binary name) */
35
36         assert ( chdir (parentdir) == 0 );   /* chdir to the binary app's parent */
37         assert ( chdir ("../../../") == 0 ); /* chdir to the .app's parent */
38     }
39
40     strcpy(Config.BiosDir,    "Bios/");
41     strcpy(Config.PatchesDir, "Patches/");
42
43     // Setup the X11 window
44     if (getenv("DISPLAY") == NULL)
45         setenv("DISPLAY", ":0.0", 0); // Default to first local display
46
47     return NSApplicationMain(argc, argv);
48 }
49
50 int SysInit() {
51         if (!sysInited) {
52 #ifdef EMU_LOG
53 #ifndef LOG_STDOUT
54                 emuLog = fopen("emuLog.txt","wb");
55 #else
56                 emuLog = stdout;
57 #endif
58                 setvbuf(emuLog, NULL, _IONBF, 0);
59 #endif
60
61                 if (EmuInit() != 0)
62                         return -1;
63
64                 sysInited = YES;
65         }
66
67         if (LoadPlugins() == -1) {
68                 return -1;
69         }
70
71         LoadMcds(Config.Mcd1, Config.Mcd2);
72
73         return 0;
74 }
75
76 void SysReset() {
77     [EmuThread resetNow];
78     //EmuReset();
79 }
80
81 void SysPrintf(const char *fmt, ...) {
82     va_list list;
83     char msg[512];
84
85     va_start(list, fmt);
86     vsprintf(msg, fmt, list);
87     va_end(list);
88
89     if (Config.PsxOut) printf ("%s", msg);
90 #ifdef EMU_LOG
91 #ifndef LOG_STDOUT
92     fprintf(emuLog, "%s", msg);
93 #endif
94 #endif
95 }
96
97 void SysMessage(const char *fmt, ...) {
98         va_list list;
99         char msg[512];
100
101         NSString *locFmtString = NSLocalizedString([NSString stringWithCString:fmt], nil);
102
103         va_start(list, fmt);
104         vsprintf(msg, [locFmtString lossyCString], list);
105         va_end(list);
106
107         NSRunAlertPanel(NSLocalizedString(@"Error!", nil),
108                 [NSString stringWithCString:msg], 
109                 nil, nil, nil);
110 }
111
112 void *SysLoadLibrary(const char *lib) {
113         NSBundle *bundle = [NSBundle bundleWithPath:[NSString stringWithCString:lib]];
114         if (bundle != nil) {
115                 return dlopen([[bundle executablePath] fileSystemRepresentation], RTLD_LAZY /*RTLD_NOW*/);
116         }
117         return dlopen(lib, RTLD_LAZY);
118 }
119
120 void *SysLoadSym(void *lib, const char *sym) {
121         return dlsym(lib, sym);
122 }
123
124 const char *SysLibError() {
125         return dlerror();
126 }
127
128 void SysCloseLibrary(void *lib) {
129         //dlclose(lib);
130 }
131
132 // Called periodically from the emu thread
133 void SysUpdate() {
134         UpdateSystemActivity(UsrActivity);
135
136         [emuThread handleEvents];
137 }
138
139 // Returns to the Gui
140 void SysRunGui() {
141 }
142
143 // Close mem and plugins
144 void SysClose() {
145     EmuShutdown();
146     ReleasePlugins();
147
148     if (emuLog != NULL) fclose(emuLog);
149
150     sysInited = NO;
151 }
152
153 void OnFile_Exit() {
154     SysClose();
155     exit(0);
156 }