4 * This work is licensed under the terms of the GNU GPLv2 or later.
5 * See the COPYING file in the top-level directory.
13 #include <sys/types.h>
19 #include "plugin_lib.h"
22 #include "../libpcsxcore/misc.h"
23 #include "../libpcsxcore/new_dynarec/new_dynarec.h"
24 #include "../plugins/cdrcimg/cdrcimg.h"
25 #include "common/plat.h"
26 #include "common/input.h"
29 unsigned long gpuDisp;
30 char cfgfile_basename[MAXPATHLEN];
31 static char *(*real_getenv)(const char *name);
33 enum sched_action emu_action, emu_action_old;
37 // from softgpu plugin
38 extern int UseFrameSkip;
40 static void make_path(char *buf, size_t size, const char *dir, const char *fname)
43 snprintf(buf, size, ".%s%s", dir, fname);
45 snprintf(buf, size, ".%s", dir);
47 #define MAKE_PATH(buf, dir, fname) \
48 make_path(buf, sizeof(buf), dir, fname)
50 static void create_profile_dir(const char *directory) {
51 char path[MAXPATHLEN];
53 MAKE_PATH(path, directory, NULL);
54 mkdir(path, S_IRWXU | S_IRWXG);
57 static void CheckSubDir() {
58 // make sure that ~/.pcsx exists
59 create_profile_dir(PCSX_DOT_DIR);
61 create_profile_dir(BIOS_DIR);
62 create_profile_dir(MEMCARD_DIR);
63 create_profile_dir(STATES_DIR);
64 create_profile_dir(PLUGINS_DIR);
65 create_profile_dir(PLUGINS_CFG_DIR);
66 create_profile_dir(CHEATS_DIR);
67 create_profile_dir(PATCHES_DIR);
68 create_profile_dir(PCSX_DOT_DIR "cfg");
71 void set_cd_image(const char *fname)
73 const char *ext = NULL;
76 ext = strrchr(fname, '.');
79 strcasecmp(ext, ".z") == 0 || strcasecmp(ext, ".bz") == 0 ||
80 strcasecmp(ext, ".znx") == 0 || strcasecmp(ext, ".pbp") == 0)) {
82 cdrcimg_set_fname(fname);
83 strcpy(Config.Cdr, "builtin_cdrcimg");
86 strcpy(Config.Cdr, "builtin_cdr");
90 static void set_default_paths(void)
92 MAKE_PATH(Config.Mcd1, MEMCARD_DIR, "card1.mcd");
93 MAKE_PATH(Config.Mcd2, MEMCARD_DIR, "card2.mcd");
94 strcpy(Config.BiosDir, "bios");
96 strcpy(Config.PluginsDir, "plugins");
97 strcpy(Config.Gpu, "builtin_gpu");
98 strcpy(Config.Spu, "builtin_spu");
99 strcpy(Config.Cdr, "builtin_cdr");
100 strcpy(Config.Pad1, "builtin_pad");
101 strcpy(Config.Pad2, "builtin_pad");
102 strcpy(Config.Net, "Disabled");
105 snprintf(Config.PatchesDir, sizeof(Config.PatchesDir), "." PATCHES_DIR);
108 void do_emu_action(void)
112 emu_action_old = emu_action;
114 switch (emu_action) {
117 case SACTION_ENTER_MENU:
120 case SACTION_LOAD_STATE:
121 ret = emu_load_state(state_slot);
122 snprintf(hud_msg, sizeof(hud_msg), ret == 0 ? "LOADED" : "FAIL!");
124 case SACTION_SAVE_STATE:
125 ret = emu_save_state(state_slot);
126 snprintf(hud_msg, sizeof(hud_msg), ret == 0 ? "SAVED" : "FAIL!");
128 case SACTION_NEXT_SSLOT:
133 case SACTION_PREV_SSLOT:
138 case SACTION_TOGGLE_FSKIP:
140 snprintf(hud_msg, sizeof(hud_msg), "FRAMESKIP %s",
141 UseFrameSkip ? "ON" : "OFF");
148 snprintf(hud_msg, sizeof(hud_msg), "STATE SLOT %d [%s]", state_slot,
149 emu_check_state(state_slot) == 0 ? "USED" : "FREE");
153 int main(int argc, char *argv[])
157 tmp = dlopen("/lib/libdl.so.2", RTLD_LAZY);
159 tmp = dlopen("/lib32/libdl.so.2", RTLD_LAZY);
161 real_getenv = dlsym(tmp, "getenv");
162 if (real_getenv == NULL) {
163 fprintf(stderr, "%s\n", dlerror());
168 // what is the name of the config file?
169 // it may be redefined by -cfg on the command line
170 strcpy(cfgfile_basename, "pcsx.cfg");
175 memset(&Config, 0, sizeof(Config));
179 strcpy(Config.Bios, "HLE");
182 extern int maemo_main(int argc, char **argv);
183 return maemo_main(argc, argv);
185 char file[MAXPATHLEN] = "";
186 char path[MAXPATHLEN];
187 const char *cdfile = NULL;
191 // read command line options
192 for (i = 1; i < argc; i++) {
193 if (!strcmp(argv[i], "-psxout")) Config.PsxOut = 1;
194 else if (!strcmp(argv[i], "-load")) loadst = atol(argv[++i]);
195 else if (!strcmp(argv[i], "-cfg")) {
196 if (i+1 >= argc) break;
197 strncpy(cfgfile_basename, argv[++i], MAXPATHLEN-100); /* TODO buffer overruns */
198 printf("Using config file %s.\n", cfgfile_basename);
200 else if (!strcmp(argv[i], "-cdfile")) {
201 char isofilename[MAXPATHLEN];
203 if (i+1 >= argc) break;
204 strncpy(isofilename, argv[++i], MAXPATHLEN);
205 if (isofilename[0] != '/') {
206 getcwd(path, MAXPATHLEN);
207 if (strlen(path) + strlen(isofilename) + 1 < MAXPATHLEN) {
209 strcat(path, isofilename);
210 strcpy(isofilename, path);
215 cdfile = isofilename;
217 else if (!strcmp(argv[i], "-h") ||
218 !strcmp(argv[i], "-help") ||
219 !strcmp(argv[i], "--help")) {
220 printf(PACKAGE_NAME " " PACKAGE_VERSION "\n");
222 " pcsx [options] [file]\n"
224 "\t-cdfile FILE\tRuns a CD image file\n"
225 "\t-nogui\t\tDon't open the GTK GUI\n"
226 "\t-cfg FILE\tLoads desired configuration file (default: ~/.pcsx/pcsx.cfg)\n"
227 "\t-psxout\t\tEnable PSX output\n"
228 "\t-load STATENUM\tLoads savestate STATENUM (1-5)\n"
229 "\t-h -help\tDisplay this message\n"
230 "\tfile\t\tLoads file\n"));
233 strncpy(file, argv[i], MAXPATHLEN);
234 if (file[0] != '/') {
235 getcwd(path, MAXPATHLEN);
236 if (strlen(path) + strlen(file) + 1 < MAXPATHLEN) {
247 set_cd_image(cdfile);
258 if (LoadPlugins() == -1) {
259 // FIXME: this recovery doesn't work, just delete bad config and bail out
260 // SysMessage("could not load plugins, retrying with defaults\n");
262 snprintf(path, sizeof(path), "." PCSX_DOT_DIR "%s", cfgfile_basename);
264 SysMessage("Failed loading plugins!");
269 if (OpenPlugins() == -1) {
272 plugin_call_rearmed_cbs();
277 if (file[0] != '\0') {
278 if (Load(file) != -1)
282 if (LoadCdrom() == -1) {
284 printf(_("Could not load CD-ROM!\n"));
291 // If a state has been specified, then load that
293 int ret = emu_load_state(loadst - 1);
294 printf("%s state %d\n", ret ? "failed to load" : "loaded", loadst);
307 emu_action = SACTION_NONE;
310 if (emu_action != SACTION_NONE)
319 if (EmuInit() == -1) {
320 printf("PSX emulator couldn't be initialized.\n");
324 LoadMcds(Config.Mcd1, Config.Mcd2); /* TODO Do we need to have this here, or in the calling main() function?? */
334 printf("SysRunGui\n");
338 printf("StartGui\n");
341 static void dummy_lace()
346 // rearmed hack: EmuReset() runs some code when real BIOS is used,
347 // but we usually do reset from menu while GPU is not open yet,
348 // so we need to prevent updateLace() call..
349 void *real_lace = GPU_updateLace;
350 GPU_updateLace = dummy_lace;
354 // hmh core forgets this
357 GPU_updateLace = real_lace;
366 if (emuLog != NULL) fclose(emuLog);
373 printf("OnFile_Exit\n");
382 int get_state_filename(char *buf, int size, int i) {
386 strncpy(trimlabel, CdromLabel, 32);
388 for (j = 31; j >= 0; j--)
389 if (trimlabel[j] == ' ')
394 snprintf(buf, size, "." STATES_DIR "%.32s-%.9s.%3.3d",
395 trimlabel, CdromId, i);
400 int emu_check_state(int slot)
402 char fname[MAXPATHLEN];
405 ret = get_state_filename(fname, sizeof(fname), slot);
409 return CheckState(fname);
412 int emu_save_state(int slot)
414 char fname[MAXPATHLEN];
417 ret = get_state_filename(fname, sizeof(fname), slot);
421 return SaveState(fname);
424 int emu_load_state(int slot)
426 char fname[MAXPATHLEN];
429 ret = get_state_filename(fname, sizeof(fname), slot);
433 return LoadState(fname);
436 void SysPrintf(const char *fmt, ...) {
441 vsprintf(msg, fmt, list);
444 fprintf(emuLog, "%s", msg);
447 void SysMessage(const char *fmt, ...) {
452 vsprintf(msg, fmt, list);
455 if (msg[strlen(msg) - 1] == '\n')
456 msg[strlen(msg) - 1] = 0;
458 fprintf(stderr, "%s\n", msg);
461 static void SignalExit(int sig) {
466 #define PARSEPATH(dst, src) \
467 ptr = src + strlen(src); \
468 while (*ptr != '\\' && ptr != src) ptr--; \
470 strcpy(dst, ptr+1); \
473 static int _OpenPlugins(void) {
476 signal(SIGINT, SignalExit);
477 signal(SIGPIPE, SignalExit);
479 GPU_clearDynarec(clearDynarec);
482 if (ret < 0) { SysMessage(_("Error opening CD-ROM plugin!")); return -1; }
484 if (ret < 0) { SysMessage(_("Error opening SPU plugin!")); return -1; }
485 SPU_registerCallback(SPUirq);
486 // pcsx-rearmed: we handle gpu elsewhere
487 //ret = GPU_open(&gpuDisp, "PCSX", NULL);
488 //if (ret < 0) { SysMessage(_("Error opening GPU plugin!")); return -1; }
489 ret = PAD1_open(&gpuDisp);
490 if (ret < 0) { SysMessage(_("Error opening Controller 1 plugin!")); return -1; }
491 ret = PAD2_open(&gpuDisp);
492 if (ret < 0) { SysMessage(_("Error opening Controller 2 plugin!")); return -1; }
494 if (Config.UseNet && !NetOpened) {
496 char path[MAXPATHLEN];
497 char dotdir[MAXPATHLEN];
499 MAKE_PATH(dotdir, "/.pcsx/plugins/", NULL);
501 strcpy(info.EmuName, "PCSX " PACKAGE_VERSION);
502 strncpy(info.CdromID, CdromId, 9);
503 strncpy(info.CdromLabel, CdromLabel, 9);
505 info.GPU_showScreenPic = GPU_showScreenPic;
506 info.GPU_displayText = GPU_displayText;
507 info.GPU_showScreenPic = GPU_showScreenPic;
508 info.PAD_setSensitive = PAD1_setSensitive;
509 sprintf(path, "%s%s", Config.BiosDir, Config.Bios);
510 strcpy(info.BIOSpath, path);
511 strcpy(info.MCD1path, Config.Mcd1);
512 strcpy(info.MCD2path, Config.Mcd2);
513 sprintf(path, "%s%s", dotdir, Config.Gpu);
514 strcpy(info.GPUpath, path);
515 sprintf(path, "%s%s", dotdir, Config.Spu);
516 strcpy(info.SPUpath, path);
517 sprintf(path, "%s%s", dotdir, Config.Cdr);
518 strcpy(info.CDRpath, path);
521 ret = NET_open(&gpuDisp);
524 // -2 is returned when something in the info
525 // changed and needs to be synced
528 PARSEPATH(Config.Bios, info.BIOSpath);
529 PARSEPATH(Config.Gpu, info.GPUpath);
530 PARSEPATH(Config.Spu, info.SPUpath);
531 PARSEPATH(Config.Cdr, info.CDRpath);
533 strcpy(Config.Mcd1, info.MCD1path);
534 strcpy(Config.Mcd2, info.MCD2path);
537 Config.UseNet = FALSE;
540 if (NET_queryPlayer() == 1) {
541 if (SendPcsxInfo() == -1) Config.UseNet = FALSE;
543 if (RecvPcsxInfo() == -1) Config.UseNet = FALSE;
547 } else if (Config.UseNet) {
557 while ((ret = _OpenPlugins()) == -2) {
559 LoadMcds(Config.Mcd1, Config.Mcd2);
560 if (LoadPlugins() == -1) return -1;
565 void ClosePlugins() {
568 signal(SIGINT, SIG_DFL);
569 signal(SIGPIPE, SIG_DFL);
571 if (ret < 0) { SysMessage(_("Error closing CD-ROM plugin!")); return; }
573 if (ret < 0) { SysMessage(_("Error closing SPU plugin!")); return; }
575 if (ret < 0) { SysMessage(_("Error closing Controller 1 Plugin!")); return; }
577 if (ret < 0) { SysMessage(_("Error closing Controller 2 plugin!")); return; }
578 // pcsx-rearmed: we handle gpu elsewhere
580 //if (ret < 0) { SysMessage(_("Error closing GPU plugin!")); return; }
588 /* this is to avoid having to hack every plugin to stop using $HOME */
589 char *getenv(const char *name)
591 static char ret[8] = ".";
593 if (name && strcmp(name, "HOME") == 0 &&
594 ((int)name >> 28) == 0) // HACK: let libs find home
597 return real_getenv(name);
601 /* we hook statically linked plugins here */
602 static const char *builtin_plugins[] = {
603 "builtin_gpu", "builtin_spu", "builtin_cdr", "builtin_pad",
607 static const int builtin_plugin_ids[] = {
608 PLUGIN_GPU, PLUGIN_SPU, PLUGIN_CDR, PLUGIN_PAD,
612 void *SysLoadLibrary(const char *lib) {
613 const char *tmp = strrchr(lib, '/');
617 printf("plugin: %s\n", lib);
621 for (i = 0; i < ARRAY_SIZE(builtin_plugins); i++)
622 if (strcmp(tmp, builtin_plugins[i]) == 0)
623 return (void *)(long)(PLUGIN_DL_BASE + builtin_plugin_ids[i]);
626 #if defined(__x86_64__) || defined(__i386__)
628 char name[MAXPATHLEN];
629 snprintf(name, sizeof(name), "%s.x86", lib);
633 ret = dlopen(lib, RTLD_NOW);
635 fprintf(stderr, "dlopen: %s\n", dlerror());
639 void *SysLoadSym(void *lib, const char *sym) {
640 unsigned int plugid = (unsigned int)(long)lib;
642 if (PLUGIN_DL_BASE <= plugid && plugid < PLUGIN_DL_BASE + ARRAY_SIZE(builtin_plugins))
643 return plugin_link(plugid - PLUGIN_DL_BASE, sym);
645 return dlsym(lib, sym);
648 const char *SysLibError() {
652 void SysCloseLibrary(void *lib) {
653 unsigned int plugid = (unsigned int)(long)lib;
655 if (PLUGIN_DL_BASE <= plugid && plugid < PLUGIN_DL_BASE + ARRAY_SIZE(builtin_plugins))