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"));
294 // If a state has been specified, then load that
296 int ret = emu_load_state(loadst - 1);
297 printf("%s state %d\n", ret ? "failed to load" : "loaded", loadst);
308 emu_action = SACTION_NONE;
311 if (emu_action != SACTION_NONE)
320 if (EmuInit() == -1) {
321 printf("PSX emulator couldn't be initialized.\n");
325 LoadMcds(Config.Mcd1, Config.Mcd2); /* TODO Do we need to have this here, or in the calling main() function?? */
335 printf("SysRunGui\n");
339 printf("StartGui\n");
342 static void dummy_lace()
347 // rearmed hack: EmuReset() runs some code when real BIOS is used,
348 // but we usually do reset from menu while GPU is not open yet,
349 // so we need to prevent updateLace() call..
350 void *real_lace = GPU_updateLace;
351 GPU_updateLace = dummy_lace;
355 // hmh core forgets this
358 GPU_updateLace = real_lace;
367 if (emuLog != NULL) fclose(emuLog);
374 printf("OnFile_Exit\n");
383 int get_state_filename(char *buf, int size, int i) {
387 strncpy(trimlabel, CdromLabel, 32);
389 for (j = 31; j >= 0; j--)
390 if (trimlabel[j] == ' ')
395 snprintf(buf, size, "." STATES_DIR "%.32s-%.9s.%3.3d",
396 trimlabel, CdromId, i);
401 int emu_check_state(int slot)
403 char fname[MAXPATHLEN];
406 ret = get_state_filename(fname, sizeof(fname), slot);
410 return CheckState(fname);
413 int emu_save_state(int slot)
415 char fname[MAXPATHLEN];
418 ret = get_state_filename(fname, sizeof(fname), slot);
422 return SaveState(fname);
425 int emu_load_state(int slot)
427 char fname[MAXPATHLEN];
430 ret = get_state_filename(fname, sizeof(fname), slot);
434 return LoadState(fname);
437 void SysPrintf(const char *fmt, ...) {
442 vsprintf(msg, fmt, list);
445 fprintf(emuLog, "%s", msg);
448 void SysMessage(const char *fmt, ...) {
453 vsprintf(msg, fmt, list);
456 if (msg[strlen(msg) - 1] == '\n')
457 msg[strlen(msg) - 1] = 0;
459 fprintf(stderr, "%s\n", msg);
462 static void SignalExit(int sig) {
467 #define PARSEPATH(dst, src) \
468 ptr = src + strlen(src); \
469 while (*ptr != '\\' && ptr != src) ptr--; \
471 strcpy(dst, ptr+1); \
474 static int _OpenPlugins(void) {
477 signal(SIGINT, SignalExit);
478 signal(SIGPIPE, SignalExit);
480 GPU_clearDynarec(clearDynarec);
483 if (ret < 0) { SysMessage(_("Error opening CD-ROM plugin!")); return -1; }
485 if (ret < 0) { SysMessage(_("Error opening SPU plugin!")); return -1; }
486 SPU_registerCallback(SPUirq);
487 // pcsx-rearmed: we handle gpu elsewhere
488 //ret = GPU_open(&gpuDisp, "PCSX", NULL);
489 //if (ret < 0) { SysMessage(_("Error opening GPU plugin!")); return -1; }
490 ret = PAD1_open(&gpuDisp);
491 if (ret < 0) { SysMessage(_("Error opening Controller 1 plugin!")); return -1; }
492 ret = PAD2_open(&gpuDisp);
493 if (ret < 0) { SysMessage(_("Error opening Controller 2 plugin!")); return -1; }
495 if (Config.UseNet && !NetOpened) {
497 char path[MAXPATHLEN];
498 char dotdir[MAXPATHLEN];
500 MAKE_PATH(dotdir, "/.pcsx/plugins/", NULL);
502 strcpy(info.EmuName, "PCSX " PACKAGE_VERSION);
503 strncpy(info.CdromID, CdromId, 9);
504 strncpy(info.CdromLabel, CdromLabel, 9);
506 info.GPU_showScreenPic = GPU_showScreenPic;
507 info.GPU_displayText = GPU_displayText;
508 info.GPU_showScreenPic = GPU_showScreenPic;
509 info.PAD_setSensitive = PAD1_setSensitive;
510 sprintf(path, "%s%s", Config.BiosDir, Config.Bios);
511 strcpy(info.BIOSpath, path);
512 strcpy(info.MCD1path, Config.Mcd1);
513 strcpy(info.MCD2path, Config.Mcd2);
514 sprintf(path, "%s%s", dotdir, Config.Gpu);
515 strcpy(info.GPUpath, path);
516 sprintf(path, "%s%s", dotdir, Config.Spu);
517 strcpy(info.SPUpath, path);
518 sprintf(path, "%s%s", dotdir, Config.Cdr);
519 strcpy(info.CDRpath, path);
522 ret = NET_open(&gpuDisp);
525 // -2 is returned when something in the info
526 // changed and needs to be synced
529 PARSEPATH(Config.Bios, info.BIOSpath);
530 PARSEPATH(Config.Gpu, info.GPUpath);
531 PARSEPATH(Config.Spu, info.SPUpath);
532 PARSEPATH(Config.Cdr, info.CDRpath);
534 strcpy(Config.Mcd1, info.MCD1path);
535 strcpy(Config.Mcd2, info.MCD2path);
538 Config.UseNet = FALSE;
541 if (NET_queryPlayer() == 1) {
542 if (SendPcsxInfo() == -1) Config.UseNet = FALSE;
544 if (RecvPcsxInfo() == -1) Config.UseNet = FALSE;
548 } else if (Config.UseNet) {
558 while ((ret = _OpenPlugins()) == -2) {
560 LoadMcds(Config.Mcd1, Config.Mcd2);
561 if (LoadPlugins() == -1) return -1;
566 void ClosePlugins() {
569 signal(SIGINT, SIG_DFL);
570 signal(SIGPIPE, SIG_DFL);
572 if (ret < 0) { SysMessage(_("Error closing CD-ROM plugin!")); return; }
574 if (ret < 0) { SysMessage(_("Error closing SPU plugin!")); return; }
576 if (ret < 0) { SysMessage(_("Error closing Controller 1 Plugin!")); return; }
578 if (ret < 0) { SysMessage(_("Error closing Controller 2 plugin!")); return; }
579 // pcsx-rearmed: we handle gpu elsewhere
581 //if (ret < 0) { SysMessage(_("Error closing GPU plugin!")); return; }
589 /* this is to avoid having to hack every plugin to stop using $HOME */
590 char *getenv(const char *name)
592 static char ret[8] = ".";
594 if (name && strcmp(name, "HOME") == 0 &&
595 ((int)name >> 28) == 0) // HACK: let libs find home
598 return real_getenv(name);
602 /* we hook statically linked plugins here */
603 static const char *builtin_plugins[] = {
604 "builtin_gpu", "builtin_spu", "builtin_cdr", "builtin_pad",
608 static const int builtin_plugin_ids[] = {
609 PLUGIN_GPU, PLUGIN_SPU, PLUGIN_CDR, PLUGIN_PAD,
613 void *SysLoadLibrary(const char *lib) {
614 const char *tmp = strrchr(lib, '/');
618 printf("plugin: %s\n", lib);
622 for (i = 0; i < ARRAY_SIZE(builtin_plugins); i++)
623 if (strcmp(tmp, builtin_plugins[i]) == 0)
624 return (void *)(long)(PLUGIN_DL_BASE + builtin_plugin_ids[i]);
627 #if defined(__x86_64__) || defined(__i386__)
629 char name[MAXPATHLEN];
630 snprintf(name, sizeof(name), "%s.x86", lib);
634 ret = dlopen(lib, RTLD_NOW);
636 fprintf(stderr, "dlopen: %s\n", dlerror());
640 void *SysLoadSym(void *lib, const char *sym) {
641 unsigned int plugid = (unsigned int)(long)lib;
643 if (PLUGIN_DL_BASE <= plugid && plugid < PLUGIN_DL_BASE + ARRAY_SIZE(builtin_plugins))
644 return plugin_link(plugid - PLUGIN_DL_BASE, sym);
646 return dlsym(lib, sym);
649 const char *SysLibError() {
653 void SysCloseLibrary(void *lib) {
654 unsigned int plugid = (unsigned int)(long)lib;
656 if (PLUGIN_DL_BASE <= plugid && plugid < PLUGIN_DL_BASE + ARRAY_SIZE(builtin_plugins))