pull in more code from PicoDrive
[pcsx_rearmed.git] / frontend / main.c
CommitLineData
e906c010 1/*
2 * (C) notaz, 2010
3 *
4 * This work is licensed under the terms of the GNU GPLv2 or later.
5 * See the COPYING file in the top-level directory.
6 */
7
80c2304e 8#include <stdio.h>
9#include <string.h>
10#include <stdarg.h>
11#include <dlfcn.h>
12#include <sys/stat.h>
13#include <sys/types.h>
14#include <unistd.h>
15
e906c010 16#include "plugin.h"
14dffdb7 17#include "pcnt.h"
80c2304e 18#include "../gui/Linux.h"
19#include "../libpcsxcore/misc.h"
20
21int UseGui;
b60f2812 22static char *(*real_getenv)(const char *name);
80c2304e 23
24static void make_path(char *buf, size_t size, const char *dir, const char *fname)
25{
26 if (fname)
27 snprintf(buf, size, ".%s%s", dir, fname);
28 else
29 snprintf(buf, size, ".%s", dir);
30}
31#define MAKE_PATH(buf, dir, fname) \
32 make_path(buf, sizeof(buf), dir, fname)
33
34static void create_profile_dir(const char *directory) {
35 char path[MAXPATHLEN];
36
37 MAKE_PATH(path, directory, NULL);
38 mkdir(path, S_IRWXU | S_IRWXG);
39}
40
41static void CheckSubDir() {
42 // make sure that ~/.pcsx exists
43 create_profile_dir(PCSX_DOT_DIR);
44
45 create_profile_dir(BIOS_DIR);
46 create_profile_dir(MEMCARD_DIR);
47 create_profile_dir(STATES_DIR);
48 create_profile_dir(PLUGINS_DIR);
49 create_profile_dir(PLUGINS_CFG_DIR);
50 create_profile_dir(CHEATS_DIR);
51 create_profile_dir(PATCHES_DIR);
52}
53
54static void CreateMemcard(char *filename, char *conf_mcd) {
55 struct stat buf;
56
57 make_path(conf_mcd, MAXPATHLEN, MEMCARD_DIR, filename);
58
59 /* Only create a memory card if an existing one does not exist */
60 if (stat(conf_mcd, &buf) == -1) {
61 SysPrintf(_("Creating memory card: %s\n"), conf_mcd);
62 CreateMcd(conf_mcd);
63 }
64}
65
66int main(int argc, char *argv[])
67{
68 char file[MAXPATHLEN] = "";
69 char path[MAXPATHLEN];
70 int runcd = 0;
71 int loadst = 0;
b60f2812 72 void *tmp;
80c2304e 73 int i;
74
b60f2812 75 tmp = dlopen("/lib/libdl.so.2", RTLD_LAZY);
fa9cfe0a 76 if (tmp == NULL)
77 tmp = dlopen("/lib32/libdl.so.2", RTLD_LAZY);
b60f2812 78 if (tmp != NULL)
79 real_getenv = dlsym(tmp, "getenv");
80 if (real_getenv == NULL) {
81 fprintf(stderr, "%s\n", dlerror());
82 return 1;
83 }
84 dlclose(tmp);
85
80c2304e 86 // what is the name of the config file?
87 // it may be redefined by -cfg on the command line
88 strcpy(cfgfile_basename, "pcsx.cfg");
89
e906c010 90 emuLog = stdout;
80c2304e 91 SetIsoFile(NULL);
80c2304e 92
93 // read command line options
94 for (i = 1; i < argc; i++) {
e906c010 95 if (!strcmp(argv[i], "-psxout")) Config.PsxOut = 1;
80c2304e 96 else if (!strcmp(argv[i], "-load")) loadst = atol(argv[++i]);
97 else if (!strcmp(argv[i], "-cfg")) {
98 if (i+1 >= argc) break;
99 strncpy(cfgfile_basename, argv[++i], MAXPATHLEN-100); /* TODO buffer overruns */
100 printf("Using config file %s.\n", cfgfile_basename);
101 }
102 else if (!strcmp(argv[i], "-cdfile")) {
103 char isofilename[MAXPATHLEN];
104
105 if (i+1 >= argc) break;
106 strncpy(isofilename, argv[++i], MAXPATHLEN);
107 if (isofilename[0] != '/') {
108 getcwd(path, MAXPATHLEN);
109 if (strlen(path) + strlen(isofilename) + 1 < MAXPATHLEN) {
110 strcat(path, "/");
111 strcat(path, isofilename);
112 strcpy(isofilename, path);
113 } else
114 isofilename[0] = 0;
115 }
116
117 SetIsoFile(isofilename);
118 runcd = 1;
119 }
120 else if (!strcmp(argv[i], "-h") ||
121 !strcmp(argv[i], "-help") ||
122 !strcmp(argv[i], "--help")) {
123 printf(PACKAGE_NAME " " PACKAGE_VERSION "\n");
124 printf("%s\n", _(
125 " pcsx [options] [file]\n"
126 "\toptions:\n"
127 "\t-cdfile FILE\tRuns a CD image file\n"
128 "\t-nogui\t\tDon't open the GTK GUI\n"
129 "\t-cfg FILE\tLoads desired configuration file (default: ~/.pcsx/pcsx.cfg)\n"
130 "\t-psxout\t\tEnable PSX output\n"
131 "\t-load STATENUM\tLoads savestate STATENUM (1-5)\n"
132 "\t-h -help\tDisplay this message\n"
133 "\tfile\t\tLoads file\n"));
134 return 0;
135 } else {
136 strncpy(file, argv[i], MAXPATHLEN);
137 if (file[0] != '/') {
138 getcwd(path, MAXPATHLEN);
139 if (strlen(path) + strlen(file) + 1 < MAXPATHLEN) {
140 strcat(path, "/");
141 strcat(path, file);
142 strcpy(file, path);
143 } else
144 file[0] = 0;
145 }
146 }
147 }
148
149 memset(&Config, 0, sizeof(PcsxConfig));
150 strcpy(Config.Net, "Disabled");
151
152 CheckSubDir();
153// ScanAllPlugins();
154
e906c010 155 strcpy(Config.Bios, "HLE");
156 strcpy(Config.BiosDir, "./");
157
158 strcpy(Config.PluginsDir, "plugins");
159 strcpy(Config.Gpu, "builtin_gpu");
160 strcpy(Config.Spu, "builtin_spu");
161 strcpy(Config.Cdr, "builtin_cdr");
162 strcpy(Config.Pad1, "builtin_pad");
163 strcpy(Config.Pad2, "builtin_pad");
164
80c2304e 165 // try to load config
166 // if the config file doesn't exist
167 if (LoadConfig() == -1) {
168 // Uh oh, no config file found, use some defaults
169 Config.PsxAuto = 1;
170
80c2304e 171 // create & load default memcards if they don't exist
172 CreateMemcard("card1.mcd", Config.Mcd1);
173 CreateMemcard("card2.mcd", Config.Mcd2);
174
175 LoadMcds(Config.Mcd1, Config.Mcd2);
176
177 SaveConfig();
178 }
179
180 snprintf(Config.PatchesDir, sizeof(Config.PatchesDir), "." PATCHES_DIR);
181/*
182 // switch to plugin dotdir
183 // this lets plugins work without modification!
184 gchar *plugin_default_dir = g_build_filename(getenv("HOME"), PLUGINS_DIR, NULL);
185 chdir(plugin_default_dir);
186 g_free(plugin_default_dir);
187*/
188 if (SysInit() == -1) return 1;
189
190 // if !gui
191 {
192 // the following only occurs if the gui isn't started
193 if (LoadPlugins() == -1) {
194 SysMessage("Failed loading plugins!");
195 return 1;
196 }
14dffdb7 197 pcnt_hook_plugins();
80c2304e 198
199 if (OpenPlugins() == -1) {
200 return 1;
201 }
202
203 SysReset();
204 CheckCdrom();
205
206 if (file[0] != '\0') {
207 Load(file);
208 } else {
209 if (runcd) {
210 if (LoadCdrom() == -1) {
211 ClosePlugins();
212 printf(_("Could not load CD-ROM!\n"));
213 return -1;
214 }
215 }
216 }
217
218 // If a state has been specified, then load that
219 if (loadst) {
220 StatesC = loadst - 1;
221 char *state_filename = get_state_filename(StatesC);
fa9cfe0a 222 int ret = LoadState(state_filename);
223 printf("%s state %s\n", ret ? "failed to load" : "loaded", state_filename);
80c2304e 224 free(state_filename);
225 }
226
227 psxCpu->Execute();
228 }
229
230 return 0;
231}
232
233int SysInit() {
80c2304e 234 if (EmuInit() == -1) {
235 printf("PSX emulator couldn't be initialized.\n");
236 return -1;
237 }
238
239 LoadMcds(Config.Mcd1, Config.Mcd2); /* TODO Do we need to have this here, or in the calling main() function?? */
240
241 if (Config.Debug) {
242 StartDebugger();
243 }
244
245 return 0;
246}
247
248void SysRunGui() {
249 printf("SysRunGui\n");
250}
251
252void StartGui() {
253 printf("StartGui\n");
254}
255
256void SysReset() {
257 EmuReset();
258}
259
260void SysClose() {
261 EmuShutdown();
262 ReleasePlugins();
263
264 StopDebugger();
265
266 if (emuLog != NULL) fclose(emuLog);
267}
268
269void SysUpdate() {
270 PADhandleKey(PAD1_keypressed());
271 PADhandleKey(PAD2_keypressed());
272}
273
274void UpdateMenuSlots() {
275}
276
277void OnFile_Exit() {
278 printf("OnFile_Exit\n");
279 exit(0);
280}
281
282void state_save(gchar *state_filename) {
283 char Text[MAXPATHLEN + 20];
284
285 GPU_updateLace();
286
287 if (SaveState(state_filename) == 0)
288 sprintf(Text, _("Saved state %s."), state_filename);
289 else
290 sprintf(Text, _("Error saving state %s!"), state_filename);
291
292 GPU_displayText(Text);
293}
294
295void state_load(gchar *state_filename) {
296 int ret;
297 char Text[MAXPATHLEN + 20];
298 FILE *fp;
299
300 // check if the state file actually exists
301 fp = fopen(state_filename, "rb");
302 if (fp == NULL) {
303 // file does not exist
304 return;
305 }
306
307 fclose(fp);
308
309 ret = CheckState(state_filename);
310
311 if (ret == 0) {
312 SysReset();
313 ret = LoadState(state_filename);
314 }
315
316 if (ret == 0) {
317 // Check the CD-ROM is valid
318 if (CheckCdrom() == -1) {
319 ClosePlugins();
320 SysRunGui();
321 return;
322 }
323
324 sprintf(Text, _("Loaded state %s."), state_filename);
325 } else {
326 sprintf(Text, _("Error loading state %s!"), state_filename);
327 }
328 GPU_displayText(Text);
329}
330
331char *get_state_filename(int i) {
332 char SStateFile[256];
333 char trimlabel[33];
334 int j;
335
336 strncpy(trimlabel, CdromLabel, 32);
337 trimlabel[32] = 0;
338 for (j = 31; j >= 0; j--)
339 if (trimlabel[j] == ' ')
340 trimlabel[j] = 0;
341 else
342 continue;
343
344 snprintf(SStateFile, sizeof(SStateFile), "." STATES_DIR "%.32s-%.9s.%3.3d",
345 trimlabel, CdromId, i);
346
347 return strdup(SStateFile);
348}
349
350void SysPrintf(const char *fmt, ...) {
351 va_list list;
352 char msg[512];
353
354 va_start(list, fmt);
355 vsprintf(msg, fmt, list);
356 va_end(list);
357
80c2304e 358 fprintf(emuLog, "%s", msg);
359}
360
361void SysMessage(const char *fmt, ...) {
362 va_list list;
363 char msg[512];
364
365 va_start(list, fmt);
366 vsprintf(msg, fmt, list);
367 va_end(list);
368
369 if (msg[strlen(msg) - 1] == '\n')
370 msg[strlen(msg) - 1] = 0;
371
372 fprintf(stderr, "%s\n", msg);
373}
374
e906c010 375#if 1
376/* this is to avoid having to hack every plugin to stop using $HOME */
377char *getenv(const char *name)
378{
379 static char ret[8] = ".";
380
b60f2812 381 if (name && strcmp(name, "HOME") == 0)
382 return ret;
e906c010 383
b60f2812 384 return real_getenv(name);
e906c010 385}
386#endif
387
388/* we hook statically linked plugins here */
389static const char *builtin_plugins[] = {
390 "builtin_gpu", "builtin_spu", "builtin_cdr", "builtin_pad"
391};
392
393static const int builtin_plugin_ids[] = {
394 PLUGIN_GPU, PLUGIN_SPU, PLUGIN_CDR, PLUGIN_PAD,
395};
396
80c2304e 397void *SysLoadLibrary(const char *lib) {
e906c010 398 const char *tmp = strrchr(lib, '/');
399 int i;
400
401 printf("dlopen %s\n", lib);
402 if (tmp != NULL) {
403 tmp++;
404 for (i = 0; i < ARRAY_SIZE(builtin_plugins); i++)
405 if (strcmp(tmp, builtin_plugins[i]) == 0)
406 return (void *)(long)(PLUGIN_DL_BASE + builtin_plugin_ids[i]);
407 }
408
80c2304e 409 return dlopen(lib, RTLD_NOW);
410}
411
412void *SysLoadSym(void *lib, const char *sym) {
e906c010 413 unsigned int plugid = (unsigned int)(long)lib;
414
415 if (PLUGIN_DL_BASE <= plugid && plugid < PLUGIN_DL_BASE + ARRAY_SIZE(builtin_plugins))
416 return plugin_link(plugid - PLUGIN_DL_BASE, sym);
417
80c2304e 418 return dlsym(lib, sym);
419}
420
421const char *SysLibError() {
422 return dlerror();
423}
424
425void SysCloseLibrary(void *lib) {
e906c010 426 unsigned int plugid = (unsigned int)(long)lib;
427
428 if (PLUGIN_DL_BASE <= plugid && plugid < PLUGIN_DL_BASE + ARRAY_SIZE(builtin_plugins))
429 return;
430
80c2304e 431 dlclose(lib);
432}
433
434