maemo: clean up and integrate
[pcsx_rearmed.git] / frontend / main.c
CommitLineData
e906c010 1/*
201c21e2 2 * (C) notaz, 2010-2011
e906c010 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>
c5061935 15#include <signal.h>
80c2304e 16
c5061935 17#include "main.h"
e906c010 18#include "plugin.h"
7c0f51de 19#include "plugin_lib.h"
14dffdb7 20#include "pcnt.h"
3c70c47b 21#include "menu.h"
80c2304e 22#include "../libpcsxcore/misc.h"
47bf65ab 23#include "../plugins/cdrcimg/cdrcimg.h"
69af03a2 24#include "common/plat.h"
25#include "common/input.h"
80c2304e 26
69af03a2 27int ready_to_go;
c5061935 28unsigned long gpuDisp;
29char cfgfile_basename[MAXPATHLEN];
b60f2812 30static char *(*real_getenv)(const char *name);
80c2304e 31
32static void make_path(char *buf, size_t size, const char *dir, const char *fname)
33{
34 if (fname)
35 snprintf(buf, size, ".%s%s", dir, fname);
36 else
37 snprintf(buf, size, ".%s", dir);
38}
39#define MAKE_PATH(buf, dir, fname) \
40 make_path(buf, sizeof(buf), dir, fname)
41
42static void create_profile_dir(const char *directory) {
43 char path[MAXPATHLEN];
44
45 MAKE_PATH(path, directory, NULL);
46 mkdir(path, S_IRWXU | S_IRWXG);
47}
48
49static void CheckSubDir() {
50 // make sure that ~/.pcsx exists
51 create_profile_dir(PCSX_DOT_DIR);
52
53 create_profile_dir(BIOS_DIR);
54 create_profile_dir(MEMCARD_DIR);
55 create_profile_dir(STATES_DIR);
56 create_profile_dir(PLUGINS_DIR);
57 create_profile_dir(PLUGINS_CFG_DIR);
58 create_profile_dir(CHEATS_DIR);
59 create_profile_dir(PATCHES_DIR);
cd6e8d0f 60 create_profile_dir(PCSX_DOT_DIR "cfg");
80c2304e 61}
62
47bf65ab 63void set_cd_image(const char *fname)
64{
e16a7e51 65 const char *ext = NULL;
47bf65ab 66
33716956 67 if (fname != NULL)
68 ext = strrchr(fname, '.');
47bf65ab 69
33716956 70 if (ext && (
71 strcasecmp(ext, ".z") == 0 || strcasecmp(ext, ".bz") == 0 ||
72 strcasecmp(ext, ".znx") == 0 || strcasecmp(ext, ".pbp") == 0)) {
47bf65ab 73 SetIsoFile(NULL);
74 cdrcimg_set_fname(fname);
75 strcpy(Config.Cdr, "builtin_cdrcimg");
76 } else {
77 SetIsoFile(fname);
78 strcpy(Config.Cdr, "builtin_cdr");
79 }
80}
81
6fe1f056 82static void set_default_paths(void)
83{
84 MAKE_PATH(Config.Mcd1, MEMCARD_DIR, "card1.mcd");
85 MAKE_PATH(Config.Mcd2, MEMCARD_DIR, "card2.mcd");
86 strcpy(Config.BiosDir, "bios");
87
88 strcpy(Config.PluginsDir, "plugins");
89 strcpy(Config.Gpu, "builtin_gpu");
90 strcpy(Config.Spu, "builtin_spu");
91 strcpy(Config.Cdr, "builtin_cdr");
92 strcpy(Config.Pad1, "builtin_pad");
93 strcpy(Config.Pad2, "builtin_pad");
94 strcpy(Config.Net, "Disabled");
95 Config.PsxAuto = 1;
96
97 snprintf(Config.PatchesDir, sizeof(Config.PatchesDir), "." PATCHES_DIR);
98}
99
80c2304e 100int main(int argc, char *argv[])
101{
b60f2812 102 void *tmp;
80c2304e 103
b60f2812 104 tmp = dlopen("/lib/libdl.so.2", RTLD_LAZY);
fa9cfe0a 105 if (tmp == NULL)
106 tmp = dlopen("/lib32/libdl.so.2", RTLD_LAZY);
b60f2812 107 if (tmp != NULL)
108 real_getenv = dlsym(tmp, "getenv");
109 if (real_getenv == NULL) {
110 fprintf(stderr, "%s\n", dlerror());
111 return 1;
112 }
113 dlclose(tmp);
114
80c2304e 115 // what is the name of the config file?
116 // it may be redefined by -cfg on the command line
117 strcpy(cfgfile_basename, "pcsx.cfg");
118
e906c010 119 emuLog = stdout;
80c2304e 120 SetIsoFile(NULL);
80c2304e 121
e0c692d9 122 memset(&Config, 0, sizeof(Config));
123
124 CheckSubDir();
125 set_default_paths();
126 strcpy(Config.Bios, "HLE");
127
128#ifdef MAEMO
129 extern int maemo_main(int argc, char **argv);
130 return maemo_main(argc, argv);
131#else
132 char file[MAXPATHLEN] = "";
133 char path[MAXPATHLEN];
134 const char *cdfile = NULL;
135 int loadst = 0;
136 int i;
137
80c2304e 138 // read command line options
139 for (i = 1; i < argc; i++) {
e906c010 140 if (!strcmp(argv[i], "-psxout")) Config.PsxOut = 1;
80c2304e 141 else if (!strcmp(argv[i], "-load")) loadst = atol(argv[++i]);
142 else if (!strcmp(argv[i], "-cfg")) {
143 if (i+1 >= argc) break;
144 strncpy(cfgfile_basename, argv[++i], MAXPATHLEN-100); /* TODO buffer overruns */
145 printf("Using config file %s.\n", cfgfile_basename);
146 }
147 else if (!strcmp(argv[i], "-cdfile")) {
148 char isofilename[MAXPATHLEN];
149
150 if (i+1 >= argc) break;
151 strncpy(isofilename, argv[++i], MAXPATHLEN);
152 if (isofilename[0] != '/') {
153 getcwd(path, MAXPATHLEN);
154 if (strlen(path) + strlen(isofilename) + 1 < MAXPATHLEN) {
155 strcat(path, "/");
156 strcat(path, isofilename);
157 strcpy(isofilename, path);
158 } else
159 isofilename[0] = 0;
160 }
161
47bf65ab 162 cdfile = isofilename;
80c2304e 163 }
164 else if (!strcmp(argv[i], "-h") ||
165 !strcmp(argv[i], "-help") ||
166 !strcmp(argv[i], "--help")) {
167 printf(PACKAGE_NAME " " PACKAGE_VERSION "\n");
168 printf("%s\n", _(
169 " pcsx [options] [file]\n"
170 "\toptions:\n"
171 "\t-cdfile FILE\tRuns a CD image file\n"
172 "\t-nogui\t\tDon't open the GTK GUI\n"
173 "\t-cfg FILE\tLoads desired configuration file (default: ~/.pcsx/pcsx.cfg)\n"
174 "\t-psxout\t\tEnable PSX output\n"
175 "\t-load STATENUM\tLoads savestate STATENUM (1-5)\n"
176 "\t-h -help\tDisplay this message\n"
177 "\tfile\t\tLoads file\n"));
178 return 0;
179 } else {
180 strncpy(file, argv[i], MAXPATHLEN);
181 if (file[0] != '/') {
182 getcwd(path, MAXPATHLEN);
183 if (strlen(path) + strlen(file) + 1 < MAXPATHLEN) {
184 strcat(path, "/");
185 strcat(path, file);
186 strcpy(file, path);
187 } else
188 file[0] = 0;
189 }
190 }
191 }
192
47bf65ab 193 if (cdfile)
194 set_cd_image(cdfile);
195
69af03a2 196 if (SysInit() == -1)
197 return 1;
80c2304e 198
69af03a2 199 // frontend stuff
200 in_init();
201 in_probe();
202 plat_init();
203 menu_init();
80c2304e 204
69af03a2 205 if (LoadPlugins() == -1) {
6fe1f056 206 // FIXME: this recovery doesn't work, just delete bad config and bail out
207 // SysMessage("could not load plugins, retrying with defaults\n");
208 set_default_paths();
209 snprintf(path, sizeof(path), "." PCSX_DOT_DIR "%s", cfgfile_basename);
210 remove(path);
69af03a2 211 SysMessage("Failed loading plugins!");
212 return 1;
213 }
214 pcnt_hook_plugins();
80c2304e 215
69af03a2 216 if (OpenPlugins() == -1) {
217 return 1;
218 }
201c21e2 219 plugin_call_rearmed_cbs();
80c2304e 220
69af03a2 221 CheckCdrom();
bbd837c6 222 SysReset();
69af03a2 223
224 if (file[0] != '\0') {
225 if (Load(file) != -1)
226 ready_to_go = 1;
227 } else {
47bf65ab 228 if (cdfile) {
69af03a2 229 if (LoadCdrom() == -1) {
230 ClosePlugins();
231 printf(_("Could not load CD-ROM!\n"));
232 return -1;
80c2304e 233 }
69af03a2 234 ready_to_go = 1;
80c2304e 235 }
69af03a2 236 }
80c2304e 237
69af03a2 238 // If a state has been specified, then load that
239 if (loadst) {
c5061935 240 char state_filename[MAXPATHLEN];
241 int ret = get_state_filename(state_filename, sizeof(state_filename), loadst - 1);
242 if (ret == 0)
243 ret = LoadState(state_filename);
69af03a2 244 printf("%s state %s\n", ret ? "failed to load" : "loaded", state_filename);
69af03a2 245 }
80c2304e 246
3c70c47b 247 if (ready_to_go)
248 menu_prepare_emu();
249 else
69af03a2 250 menu_loop();
251
7c0f51de 252 pl_start_watchdog();
253
69af03a2 254 while (1)
255 {
80c2304e 256 psxCpu->Execute();
69af03a2 257 menu_loop();
80c2304e 258 }
259
260 return 0;
e0c692d9 261#endif
80c2304e 262}
263
264int SysInit() {
80c2304e 265 if (EmuInit() == -1) {
266 printf("PSX emulator couldn't be initialized.\n");
267 return -1;
268 }
269
270 LoadMcds(Config.Mcd1, Config.Mcd2); /* TODO Do we need to have this here, or in the calling main() function?? */
271
272 if (Config.Debug) {
273 StartDebugger();
274 }
275
276 return 0;
277}
278
279void SysRunGui() {
280 printf("SysRunGui\n");
281}
282
283void StartGui() {
284 printf("StartGui\n");
285}
286
e6eb2066 287static void dummy_lace()
288{
289}
290
80c2304e 291void SysReset() {
e6eb2066 292 // rearmed hack: EmuReset() runs some code when real BIOS is used,
293 // but we usually do reset from menu while GPU is not open yet,
294 // so we need to prevent updateLace() call..
295 void *real_lace = GPU_updateLace;
296 GPU_updateLace = dummy_lace;
297
80c2304e 298 EmuReset();
bd6267e6 299
300 // hmh core forgets this
301 CDR_stop();
e6eb2066 302
303 GPU_updateLace = real_lace;
80c2304e 304}
305
306void SysClose() {
307 EmuShutdown();
308 ReleasePlugins();
309
310 StopDebugger();
311
312 if (emuLog != NULL) fclose(emuLog);
313}
314
315void SysUpdate() {
80c2304e 316}
317
318void OnFile_Exit() {
bd6267e6 319 printf("OnFile_Exit\n");
e0c692d9 320#ifndef MAEMO
201c21e2 321 menu_finish();
e0c692d9 322#endif
bd6267e6 323 plat_finish();
324 SysClose();
80c2304e 325 exit(0);
326}
327
c5061935 328int get_state_filename(char *buf, int size, int i) {
80c2304e 329 char trimlabel[33];
330 int j;
331
332 strncpy(trimlabel, CdromLabel, 32);
333 trimlabel[32] = 0;
334 for (j = 31; j >= 0; j--)
335 if (trimlabel[j] == ' ')
336 trimlabel[j] = 0;
337 else
338 continue;
339
c5061935 340 snprintf(buf, size, "." STATES_DIR "%.32s-%.9s.%3.3d",
80c2304e 341 trimlabel, CdromId, i);
342
c5061935 343 return 0;
80c2304e 344}
345
346void SysPrintf(const char *fmt, ...) {
347 va_list list;
348 char msg[512];
349
350 va_start(list, fmt);
351 vsprintf(msg, fmt, list);
352 va_end(list);
353
80c2304e 354 fprintf(emuLog, "%s", msg);
355}
356
357void SysMessage(const char *fmt, ...) {
358 va_list list;
359 char msg[512];
360
361 va_start(list, fmt);
362 vsprintf(msg, fmt, list);
363 va_end(list);
364
365 if (msg[strlen(msg) - 1] == '\n')
366 msg[strlen(msg) - 1] = 0;
367
368 fprintf(stderr, "%s\n", msg);
369}
370
c5061935 371static void SignalExit(int sig) {
372 ClosePlugins();
373 OnFile_Exit();
374}
375
376#define PARSEPATH(dst, src) \
377 ptr = src + strlen(src); \
378 while (*ptr != '\\' && ptr != src) ptr--; \
379 if (ptr != src) { \
380 strcpy(dst, ptr+1); \
381 }
382
383static int _OpenPlugins(void) {
384 int ret;
385
386 signal(SIGINT, SignalExit);
387 signal(SIGPIPE, SignalExit);
388
389 GPU_clearDynarec(clearDynarec);
390
391 ret = CDR_open();
392 if (ret < 0) { SysMessage(_("Error opening CD-ROM plugin!")); return -1; }
393 ret = SPU_open();
394 if (ret < 0) { SysMessage(_("Error opening SPU plugin!")); return -1; }
395 SPU_registerCallback(SPUirq);
396 // pcsx-rearmed: we handle gpu elsewhere
397 //ret = GPU_open(&gpuDisp, "PCSX", NULL);
398 //if (ret < 0) { SysMessage(_("Error opening GPU plugin!")); return -1; }
399 ret = PAD1_open(&gpuDisp);
400 if (ret < 0) { SysMessage(_("Error opening Controller 1 plugin!")); return -1; }
401 ret = PAD2_open(&gpuDisp);
402 if (ret < 0) { SysMessage(_("Error opening Controller 2 plugin!")); return -1; }
403
404 if (Config.UseNet && !NetOpened) {
405 netInfo info;
406 char path[MAXPATHLEN];
407 char dotdir[MAXPATHLEN];
408
409 MAKE_PATH(dotdir, "/.pcsx/plugins/", NULL);
410
411 strcpy(info.EmuName, "PCSX " PACKAGE_VERSION);
412 strncpy(info.CdromID, CdromId, 9);
413 strncpy(info.CdromLabel, CdromLabel, 9);
414 info.psxMem = psxM;
415 info.GPU_showScreenPic = GPU_showScreenPic;
416 info.GPU_displayText = GPU_displayText;
417 info.GPU_showScreenPic = GPU_showScreenPic;
418 info.PAD_setSensitive = PAD1_setSensitive;
419 sprintf(path, "%s%s", Config.BiosDir, Config.Bios);
420 strcpy(info.BIOSpath, path);
421 strcpy(info.MCD1path, Config.Mcd1);
422 strcpy(info.MCD2path, Config.Mcd2);
423 sprintf(path, "%s%s", dotdir, Config.Gpu);
424 strcpy(info.GPUpath, path);
425 sprintf(path, "%s%s", dotdir, Config.Spu);
426 strcpy(info.SPUpath, path);
427 sprintf(path, "%s%s", dotdir, Config.Cdr);
428 strcpy(info.CDRpath, path);
429 NET_setInfo(&info);
430
431 ret = NET_open(&gpuDisp);
432 if (ret < 0) {
433 if (ret == -2) {
434 // -2 is returned when something in the info
435 // changed and needs to be synced
436 char *ptr;
437
438 PARSEPATH(Config.Bios, info.BIOSpath);
439 PARSEPATH(Config.Gpu, info.GPUpath);
440 PARSEPATH(Config.Spu, info.SPUpath);
441 PARSEPATH(Config.Cdr, info.CDRpath);
442
443 strcpy(Config.Mcd1, info.MCD1path);
444 strcpy(Config.Mcd2, info.MCD2path);
445 return -2;
446 } else {
447 Config.UseNet = FALSE;
448 }
449 } else {
450 if (NET_queryPlayer() == 1) {
451 if (SendPcsxInfo() == -1) Config.UseNet = FALSE;
452 } else {
453 if (RecvPcsxInfo() == -1) Config.UseNet = FALSE;
454 }
455 }
456 NetOpened = TRUE;
457 } else if (Config.UseNet) {
458 NET_resume();
459 }
460
461 return 0;
462}
463
464int OpenPlugins() {
465 int ret;
466
467 while ((ret = _OpenPlugins()) == -2) {
468 ReleasePlugins();
469 LoadMcds(Config.Mcd1, Config.Mcd2);
470 if (LoadPlugins() == -1) return -1;
471 }
472 return ret;
473}
474
475void ClosePlugins() {
476 int ret;
477
478 signal(SIGINT, SIG_DFL);
479 signal(SIGPIPE, SIG_DFL);
480 ret = CDR_close();
481 if (ret < 0) { SysMessage(_("Error closing CD-ROM plugin!")); return; }
482 ret = SPU_close();
483 if (ret < 0) { SysMessage(_("Error closing SPU plugin!")); return; }
484 ret = PAD1_close();
485 if (ret < 0) { SysMessage(_("Error closing Controller 1 Plugin!")); return; }
486 ret = PAD2_close();
487 if (ret < 0) { SysMessage(_("Error closing Controller 2 plugin!")); return; }
488 // pcsx-rearmed: we handle gpu elsewhere
489 //ret = GPU_close();
490 //if (ret < 0) { SysMessage(_("Error closing GPU plugin!")); return; }
491
492 if (Config.UseNet) {
493 NET_pause();
494 }
495}
496
e906c010 497#if 1
498/* this is to avoid having to hack every plugin to stop using $HOME */
499char *getenv(const char *name)
500{
501 static char ret[8] = ".";
502
69af03a2 503 if (name && strcmp(name, "HOME") == 0 &&
504 ((int)name >> 28) == 0) // HACK: let libs find home
b60f2812 505 return ret;
e906c010 506
b60f2812 507 return real_getenv(name);
e906c010 508}
509#endif
510
511/* we hook statically linked plugins here */
512static const char *builtin_plugins[] = {
47bf65ab 513 "builtin_gpu", "builtin_spu", "builtin_cdr", "builtin_pad",
514 "builtin_cdrcimg",
e906c010 515};
516
517static const int builtin_plugin_ids[] = {
518 PLUGIN_GPU, PLUGIN_SPU, PLUGIN_CDR, PLUGIN_PAD,
47bf65ab 519 PLUGIN_CDRCIMG,
e906c010 520};
521
80c2304e 522void *SysLoadLibrary(const char *lib) {
e906c010 523 const char *tmp = strrchr(lib, '/');
bbd837c6 524 void *ret;
e906c010 525 int i;
526
bbd837c6 527 printf("plugin: %s\n", lib);
528
e906c010 529 if (tmp != NULL) {
530 tmp++;
531 for (i = 0; i < ARRAY_SIZE(builtin_plugins); i++)
532 if (strcmp(tmp, builtin_plugins[i]) == 0)
533 return (void *)(long)(PLUGIN_DL_BASE + builtin_plugin_ids[i]);
534 }
535
2185e39b 536#if defined(__x86_64__) || defined(__i386__)
537 // convenience hack
538 char name[MAXPATHLEN];
539 snprintf(name, sizeof(name), "%s.x86", lib);
540 lib = name;
541#endif
542
bbd837c6 543 ret = dlopen(lib, RTLD_NOW);
544 if (ret == NULL)
545 fprintf(stderr, "dlopen: %s\n", dlerror());
546 return ret;
80c2304e 547}
548
549void *SysLoadSym(void *lib, const char *sym) {
e906c010 550 unsigned int plugid = (unsigned int)(long)lib;
551
552 if (PLUGIN_DL_BASE <= plugid && plugid < PLUGIN_DL_BASE + ARRAY_SIZE(builtin_plugins))
553 return plugin_link(plugid - PLUGIN_DL_BASE, sym);
554
80c2304e 555 return dlsym(lib, sym);
556}
557
558const char *SysLibError() {
559 return dlerror();
560}
561
562void SysCloseLibrary(void *lib) {
e906c010 563 unsigned int plugid = (unsigned int)(long)lib;
564
565 if (PLUGIN_DL_BASE <= plugid && plugid < PLUGIN_DL_BASE + ARRAY_SIZE(builtin_plugins))
566 return;
567
80c2304e 568 dlclose(lib);
569}
570