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>
20 #include "plugin_lib.h"
23 #include "../libpcsxcore/misc.h"
24 #include "../libpcsxcore/new_dynarec/new_dynarec.h"
25 #include "../plugins/cdrcimg/cdrcimg.h"
26 #include "common/plat.h"
27 #include "common/input.h"
28 #include "common/readpng.h"
30 // don't include debug.h - it breaks ARM build (R1 redefined)
35 unsigned long gpuDisp;
36 char cfgfile_basename[MAXPATHLEN];
37 static char *(*real_getenv)(const char *name);
39 enum sched_action emu_action, emu_action_old;
43 static void make_path(char *buf, size_t size, const char *dir, const char *fname)
46 snprintf(buf, size, ".%s%s", dir, fname);
48 snprintf(buf, size, ".%s", dir);
50 #define MAKE_PATH(buf, dir, fname) \
51 make_path(buf, sizeof(buf), dir, fname)
53 static void create_profile_dir(const char *directory) {
54 char path[MAXPATHLEN];
56 MAKE_PATH(path, directory, NULL);
57 mkdir(path, S_IRWXU | S_IRWXG);
60 static void CheckSubDir() {
61 // make sure that ~/.pcsx exists
62 create_profile_dir(PCSX_DOT_DIR);
64 create_profile_dir(BIOS_DIR);
65 create_profile_dir(MEMCARD_DIR);
66 create_profile_dir(STATES_DIR);
67 create_profile_dir(PLUGINS_DIR);
68 create_profile_dir(PLUGINS_CFG_DIR);
69 create_profile_dir(CHEATS_DIR);
70 create_profile_dir(PATCHES_DIR);
71 create_profile_dir(PCSX_DOT_DIR "cfg");
72 create_profile_dir("/screenshots/");
75 static int get_gameid_filename(char *buf, int size, const char *fmt, int i) {
79 strncpy(trimlabel, CdromLabel, 32);
81 for (j = 31; j >= 0; j--)
82 if (trimlabel[j] == ' ')
87 snprintf(buf, size, fmt, trimlabel, CdromId, i);
92 void set_cd_image(const char *fname)
94 const char *ext = NULL;
97 ext = strrchr(fname, '.');
100 strcasecmp(ext, ".z") == 0 || strcasecmp(ext, ".bz") == 0 ||
101 strcasecmp(ext, ".znx") == 0 /*|| strcasecmp(ext, ".pbp") == 0*/)) {
103 cdrcimg_set_fname(fname);
104 strcpy(Config.Cdr, "builtin_cdrcimg");
107 strcpy(Config.Cdr, "builtin_cdr");
111 static void set_default_paths(void)
113 MAKE_PATH(Config.Mcd1, MEMCARD_DIR, "card1.mcd");
114 MAKE_PATH(Config.Mcd2, MEMCARD_DIR, "card2.mcd");
115 strcpy(Config.BiosDir, "bios");
117 strcpy(Config.PluginsDir, "plugins");
118 strcpy(Config.Gpu, "builtin_gpu");
119 strcpy(Config.Spu, "builtin_spu");
120 strcpy(Config.Cdr, "builtin_cdr");
121 strcpy(Config.Pad1, "builtin_pad");
122 strcpy(Config.Pad2, "builtin_pad");
123 strcpy(Config.Net, "Disabled");
126 snprintf(Config.PatchesDir, sizeof(Config.PatchesDir), "." PATCHES_DIR);
129 static void check_memcards(void)
131 char buf[MAXPATHLEN];
135 for (i = 1; i <= 9; i++) {
136 snprintf(buf, sizeof(buf), ".%scard%d.mcd", MEMCARD_DIR, i);
138 f = fopen(buf, "rb");
140 printf("Creating memcard: %s\n", buf);
148 void do_emu_action(void)
150 char buf[MAXPATHLEN];
153 emu_action_old = emu_action;
155 switch (emu_action) {
156 case SACTION_ENTER_MENU:
159 case SACTION_LOAD_STATE:
160 ret = emu_load_state(state_slot);
161 snprintf(hud_msg, sizeof(hud_msg), ret == 0 ? "LOADED" : "FAIL!");
163 case SACTION_SAVE_STATE:
164 ret = emu_save_state(state_slot);
165 snprintf(hud_msg, sizeof(hud_msg), ret == 0 ? "SAVED" : "FAIL!");
167 case SACTION_NEXT_SSLOT:
172 case SACTION_PREV_SSLOT:
177 case SACTION_TOGGLE_FSKIP:
178 pl_rearmed_cbs.fskip_advice = 0;
179 pl_rearmed_cbs.frameskip++;
180 if (pl_rearmed_cbs.frameskip > 1)
181 pl_rearmed_cbs.frameskip = -1;
182 snprintf(hud_msg, sizeof(hud_msg), "FRAMESKIP: %s",
183 pl_rearmed_cbs.frameskip == -1 ? "AUTO" :
184 pl_rearmed_cbs.frameskip == 0 ? "OFF" : "1" );
185 plugin_call_rearmed_cbs();
187 case SACTION_SCREENSHOT:
191 time_t t = time(NULL);
192 struct tm *tb = localtime(&t);
193 int ti = tb->tm_yday * 1000000 + tb->tm_hour * 10000 +
194 tb->tm_min * 100 + tb->tm_sec;
196 scrbuf = pl_prepare_screenshot(&w, &h, &bpp);
197 get_gameid_filename(buf, sizeof(buf),
198 "screenshots/%.32s-%.9s.%d.png", ti);
200 if (scrbuf != 0 && bpp == 16)
201 ret = writepng(buf, scrbuf, w, h);
203 snprintf(hud_msg, sizeof(hud_msg), "SCREENSHOT TAKEN");
213 snprintf(hud_msg, sizeof(hud_msg), "STATE SLOT %d [%s]", state_slot,
214 emu_check_state(state_slot) == 0 ? "USED" : "FREE");
216 printf("* %s\n", hud_msg);
219 int main(int argc, char *argv[])
223 tmp = dlopen("/lib/libdl.so.2", RTLD_LAZY);
225 tmp = dlopen("/lib32/libdl.so.2", RTLD_LAZY);
227 real_getenv = dlsym(tmp, "getenv");
228 if (real_getenv == NULL) {
229 fprintf(stderr, "%s\n", dlerror());
234 // what is the name of the config file?
235 // it may be redefined by -cfg on the command line
236 strcpy(cfgfile_basename, "pcsx.cfg");
241 memset(&Config, 0, sizeof(Config));
246 strcpy(Config.Bios, "HLE");
249 extern int maemo_main(int argc, char **argv);
250 return maemo_main(argc, argv);
252 char file[MAXPATHLEN] = "";
253 char path[MAXPATHLEN];
254 const char *cdfile = NULL;
255 const char *loadst_f = NULL;
260 // read command line options
261 for (i = 1; i < argc; i++) {
262 if (!strcmp(argv[i], "-psxout")) psxout = 1;
263 else if (!strcmp(argv[i], "-load")) loadst = atol(argv[++i]);
264 else if (!strcmp(argv[i], "-cfg")) {
265 if (i+1 >= argc) break;
266 strncpy(cfgfile_basename, argv[++i], MAXPATHLEN-100); /* TODO buffer overruns */
267 printf("Using config file %s.\n", cfgfile_basename);
269 else if (!strcmp(argv[i], "-cdfile")) {
270 char isofilename[MAXPATHLEN];
272 if (i+1 >= argc) break;
273 strncpy(isofilename, argv[++i], MAXPATHLEN);
274 if (isofilename[0] != '/') {
275 getcwd(path, MAXPATHLEN);
276 if (strlen(path) + strlen(isofilename) + 1 < MAXPATHLEN) {
278 strcat(path, isofilename);
279 strcpy(isofilename, path);
284 cdfile = isofilename;
286 else if (!strcmp(argv[i], "-loadf")) {
287 if (i+1 >= argc) break;
288 loadst_f = argv[++i];
290 else if (!strcmp(argv[i], "-h") ||
291 !strcmp(argv[i], "-help") ||
292 !strcmp(argv[i], "--help")) {
293 printf(PACKAGE_NAME " " PACKAGE_VERSION "\n");
295 " pcsx [options] [file]\n"
297 "\t-cdfile FILE\tRuns a CD image file\n"
298 "\t-nogui\t\tDon't open the GTK GUI\n"
299 "\t-cfg FILE\tLoads desired configuration file (default: ~/.pcsx/pcsx.cfg)\n"
300 "\t-psxout\t\tEnable PSX output\n"
301 "\t-load STATENUM\tLoads savestate STATENUM (1-5)\n"
302 "\t-h -help\tDisplay this message\n"
303 "\tfile\t\tLoads file\n"));
306 strncpy(file, argv[i], MAXPATHLEN);
307 if (file[0] != '/') {
308 getcwd(path, MAXPATHLEN);
309 if (strlen(path) + strlen(file) + 1 < MAXPATHLEN) {
320 set_cd_image(cdfile);
329 menu_init(); // loads config
335 if (LoadPlugins() == -1) {
336 // FIXME: this recovery doesn't work, just delete bad config and bail out
337 // SysMessage("could not load plugins, retrying with defaults\n");
339 snprintf(path, sizeof(path), "." PCSX_DOT_DIR "%s", cfgfile_basename);
341 SysMessage("Failed loading plugins!");
346 if (OpenPlugins() == -1) {
349 plugin_call_rearmed_cbs();
354 if (file[0] != '\0') {
355 if (Load(file) != -1)
359 if (LoadCdrom() == -1) {
361 printf(_("Could not load CD-ROM!\n"));
371 // If a state has been specified, then load that
373 int ret = emu_load_state(loadst - 1);
374 printf("%s state %d\n", ret ? "failed to load" : "loaded", loadst);
377 int ret = LoadState(loadst_f);
378 printf("%s state file: %s\n", ret ? "failed to load" : "loaded", loadst_f);
389 emu_action = SACTION_NONE;
392 if (emu_action != SACTION_NONE)
401 if (EmuInit() == -1) {
402 printf("PSX emulator couldn't be initialized.\n");
406 LoadMcds(Config.Mcd1, Config.Mcd2);
416 printf("SysRunGui\n");
420 printf("StartGui\n");
423 static void dummy_lace()
428 // rearmed hack: EmuReset() runs some code when real BIOS is used,
429 // but we usually do reset from menu while GPU is not open yet,
430 // so we need to prevent updateLace() call..
431 void *real_lace = GPU_updateLace;
432 GPU_updateLace = dummy_lace;
436 // hmh core forgets this
439 GPU_updateLace = real_lace;
448 if (emuLog != NULL) fclose(emuLog);
455 printf("OnFile_Exit\n");
464 int get_state_filename(char *buf, int size, int i) {
465 return get_gameid_filename(buf, size,
466 "." STATES_DIR "%.32s-%.9s.%3.3d", i);
469 int emu_check_state(int slot)
471 char fname[MAXPATHLEN];
474 ret = get_state_filename(fname, sizeof(fname), slot);
478 return CheckState(fname);
481 int emu_save_state(int slot)
483 char fname[MAXPATHLEN];
486 ret = get_state_filename(fname, sizeof(fname), slot);
490 ret = SaveState(fname);
491 printf("* %s \"%s\" [%d]\n", ret == 0 ? "saved" : "failed to save", fname, slot);
495 int emu_load_state(int slot)
497 char fname[MAXPATHLEN];
500 ret = get_state_filename(fname, sizeof(fname), slot);
504 return LoadState(fname);
507 void SysPrintf(const char *fmt, ...) {
512 vsprintf(msg, fmt, list);
515 fprintf(emuLog, "%s", msg);
518 void SysMessage(const char *fmt, ...) {
523 vsprintf(msg, fmt, list);
526 if (msg[strlen(msg) - 1] == '\n')
527 msg[strlen(msg) - 1] = 0;
529 fprintf(stderr, "%s\n", msg);
532 static void SignalExit(int sig) {
537 #define PARSEPATH(dst, src) \
538 ptr = src + strlen(src); \
539 while (*ptr != '\\' && ptr != src) ptr--; \
541 strcpy(dst, ptr+1); \
544 static int _OpenPlugins(void) {
547 signal(SIGINT, SignalExit);
548 signal(SIGPIPE, SignalExit);
550 GPU_clearDynarec(clearDynarec);
553 if (ret < 0) { SysMessage(_("Error opening CD-ROM plugin!")); return -1; }
555 if (ret < 0) { SysMessage(_("Error opening SPU plugin!")); return -1; }
556 SPU_registerCallback(SPUirq);
557 // pcsx-rearmed: we handle gpu elsewhere
558 //ret = GPU_open(&gpuDisp, "PCSX", NULL);
559 //if (ret < 0) { SysMessage(_("Error opening GPU plugin!")); return -1; }
560 ret = PAD1_open(&gpuDisp);
561 if (ret < 0) { SysMessage(_("Error opening Controller 1 plugin!")); return -1; }
562 ret = PAD2_open(&gpuDisp);
563 if (ret < 0) { SysMessage(_("Error opening Controller 2 plugin!")); return -1; }
565 if (Config.UseNet && !NetOpened) {
567 char path[MAXPATHLEN];
568 char dotdir[MAXPATHLEN];
570 MAKE_PATH(dotdir, "/.pcsx/plugins/", NULL);
572 strcpy(info.EmuName, "PCSX " PACKAGE_VERSION);
573 strncpy(info.CdromID, CdromId, 9);
574 strncpy(info.CdromLabel, CdromLabel, 9);
576 info.GPU_showScreenPic = GPU_showScreenPic;
577 info.GPU_displayText = GPU_displayText;
578 info.GPU_showScreenPic = GPU_showScreenPic;
579 info.PAD_setSensitive = PAD1_setSensitive;
580 sprintf(path, "%s%s", Config.BiosDir, Config.Bios);
581 strcpy(info.BIOSpath, path);
582 strcpy(info.MCD1path, Config.Mcd1);
583 strcpy(info.MCD2path, Config.Mcd2);
584 sprintf(path, "%s%s", dotdir, Config.Gpu);
585 strcpy(info.GPUpath, path);
586 sprintf(path, "%s%s", dotdir, Config.Spu);
587 strcpy(info.SPUpath, path);
588 sprintf(path, "%s%s", dotdir, Config.Cdr);
589 strcpy(info.CDRpath, path);
592 ret = NET_open(&gpuDisp);
595 // -2 is returned when something in the info
596 // changed and needs to be synced
599 PARSEPATH(Config.Bios, info.BIOSpath);
600 PARSEPATH(Config.Gpu, info.GPUpath);
601 PARSEPATH(Config.Spu, info.SPUpath);
602 PARSEPATH(Config.Cdr, info.CDRpath);
604 strcpy(Config.Mcd1, info.MCD1path);
605 strcpy(Config.Mcd2, info.MCD2path);
608 Config.UseNet = FALSE;
611 if (NET_queryPlayer() == 1) {
612 if (SendPcsxInfo() == -1) Config.UseNet = FALSE;
614 if (RecvPcsxInfo() == -1) Config.UseNet = FALSE;
618 } else if (Config.UseNet) {
628 while ((ret = _OpenPlugins()) == -2) {
630 LoadMcds(Config.Mcd1, Config.Mcd2);
631 if (LoadPlugins() == -1) return -1;
636 void ClosePlugins() {
639 signal(SIGINT, SIG_DFL);
640 signal(SIGPIPE, SIG_DFL);
642 if (ret < 0) { SysMessage(_("Error closing CD-ROM plugin!")); return; }
644 if (ret < 0) { SysMessage(_("Error closing SPU plugin!")); return; }
646 if (ret < 0) { SysMessage(_("Error closing Controller 1 Plugin!")); return; }
648 if (ret < 0) { SysMessage(_("Error closing Controller 2 plugin!")); return; }
649 // pcsx-rearmed: we handle gpu elsewhere
651 //if (ret < 0) { SysMessage(_("Error closing GPU plugin!")); return; }
659 /* this is to avoid having to hack every plugin to stop using $HOME */
660 char *getenv(const char *name)
662 static char ret[8] = ".";
664 if (name && strcmp(name, "HOME") == 0 &&
665 ((int)name >> 28) == 0) // HACK: let libs find home
668 return real_getenv(name);
672 /* we hook statically linked plugins here */
673 static const char *builtin_plugins[] = {
674 "builtin_gpu", "builtin_spu", "builtin_cdr", "builtin_pad",
678 static const int builtin_plugin_ids[] = {
679 PLUGIN_GPU, PLUGIN_SPU, PLUGIN_CDR, PLUGIN_PAD,
683 void *SysLoadLibrary(const char *lib) {
684 const char *tmp = strrchr(lib, '/');
688 printf("plugin: %s\n", lib);
692 for (i = 0; i < ARRAY_SIZE(builtin_plugins); i++)
693 if (strcmp(tmp, builtin_plugins[i]) == 0)
694 return (void *)(long)(PLUGIN_DL_BASE + builtin_plugin_ids[i]);
697 #if defined(__x86_64__) || defined(__i386__)
699 char name[MAXPATHLEN];
700 snprintf(name, sizeof(name), "%s.x86", lib);
704 ret = dlopen(lib, RTLD_NOW);
706 fprintf(stderr, "dlopen: %s\n", dlerror());
710 void *SysLoadSym(void *lib, const char *sym) {
711 unsigned int plugid = (unsigned int)(long)lib;
713 if (PLUGIN_DL_BASE <= plugid && plugid < PLUGIN_DL_BASE + ARRAY_SIZE(builtin_plugins))
714 return plugin_link(plugid - PLUGIN_DL_BASE, sym);
716 return dlsym(lib, sym);
719 const char *SysLibError() {
723 void SysCloseLibrary(void *lib) {
724 unsigned int plugid = (unsigned int)(long)lib;
726 if (PLUGIN_DL_BASE <= plugid && plugid < PLUGIN_DL_BASE + ARRAY_SIZE(builtin_plugins))