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");
124 #if defined(__arm__) && !defined(__ARM_ARCH_7A__) /* XXX */
125 strcpy(Config.Gpu, "gpuPCSX4ALL.so");
129 snprintf(Config.PatchesDir, sizeof(Config.PatchesDir), "." PATCHES_DIR);
132 static void check_memcards(void)
134 char buf[MAXPATHLEN];
138 for (i = 1; i <= 9; i++) {
139 snprintf(buf, sizeof(buf), ".%scard%d.mcd", MEMCARD_DIR, i);
141 f = fopen(buf, "rb");
143 printf("Creating memcard: %s\n", buf);
151 void do_emu_action(void)
153 char buf[MAXPATHLEN];
156 emu_action_old = emu_action;
158 switch (emu_action) {
159 case SACTION_ENTER_MENU:
162 case SACTION_LOAD_STATE:
163 ret = emu_load_state(state_slot);
164 snprintf(hud_msg, sizeof(hud_msg), ret == 0 ? "LOADED" : "FAIL!");
166 case SACTION_SAVE_STATE:
167 ret = emu_save_state(state_slot);
168 snprintf(hud_msg, sizeof(hud_msg), ret == 0 ? "SAVED" : "FAIL!");
170 case SACTION_NEXT_SSLOT:
175 case SACTION_PREV_SSLOT:
180 case SACTION_TOGGLE_FSKIP:
181 pl_rearmed_cbs.fskip_advice = 0;
182 pl_rearmed_cbs.frameskip++;
183 if (pl_rearmed_cbs.frameskip > 1)
184 pl_rearmed_cbs.frameskip = -1;
185 snprintf(hud_msg, sizeof(hud_msg), "FRAMESKIP: %s",
186 pl_rearmed_cbs.frameskip == -1 ? "AUTO" :
187 pl_rearmed_cbs.frameskip == 0 ? "OFF" : "1" );
188 plugin_call_rearmed_cbs();
190 case SACTION_SCREENSHOT:
194 time_t t = time(NULL);
195 struct tm *tb = localtime(&t);
196 int ti = tb->tm_yday * 1000000 + tb->tm_hour * 10000 +
197 tb->tm_min * 100 + tb->tm_sec;
199 scrbuf = pl_prepare_screenshot(&w, &h, &bpp);
200 get_gameid_filename(buf, sizeof(buf),
201 "screenshots/%.32s-%.9s.%d.png", ti);
203 if (scrbuf != 0 && bpp == 16)
204 ret = writepng(buf, scrbuf, w, h);
206 snprintf(hud_msg, sizeof(hud_msg), "SCREENSHOT TAKEN");
216 snprintf(hud_msg, sizeof(hud_msg), "STATE SLOT %d [%s]", state_slot,
217 emu_check_state(state_slot) == 0 ? "USED" : "FREE");
219 printf("* %s\n", hud_msg);
222 int main(int argc, char *argv[])
226 tmp = dlopen("/lib/libdl.so.2", RTLD_LAZY);
228 tmp = dlopen("/lib32/libdl.so.2", RTLD_LAZY);
230 real_getenv = dlsym(tmp, "getenv");
231 if (real_getenv == NULL) {
232 fprintf(stderr, "%s\n", dlerror());
237 // what is the name of the config file?
238 // it may be redefined by -cfg on the command line
239 strcpy(cfgfile_basename, "pcsx.cfg");
244 memset(&Config, 0, sizeof(Config));
249 strcpy(Config.Bios, "HLE");
252 extern int maemo_main(int argc, char **argv);
253 return maemo_main(argc, argv);
255 char file[MAXPATHLEN] = "";
256 char path[MAXPATHLEN];
257 const char *cdfile = NULL;
258 const char *loadst_f = NULL;
263 // read command line options
264 for (i = 1; i < argc; i++) {
265 if (!strcmp(argv[i], "-psxout")) psxout = 1;
266 else if (!strcmp(argv[i], "-load")) loadst = atol(argv[++i]);
267 else if (!strcmp(argv[i], "-cfg")) {
268 if (i+1 >= argc) break;
269 strncpy(cfgfile_basename, argv[++i], MAXPATHLEN-100); /* TODO buffer overruns */
270 printf("Using config file %s.\n", cfgfile_basename);
272 else if (!strcmp(argv[i], "-cdfile")) {
273 char isofilename[MAXPATHLEN];
275 if (i+1 >= argc) break;
276 strncpy(isofilename, argv[++i], MAXPATHLEN);
277 if (isofilename[0] != '/') {
278 getcwd(path, MAXPATHLEN);
279 if (strlen(path) + strlen(isofilename) + 1 < MAXPATHLEN) {
281 strcat(path, isofilename);
282 strcpy(isofilename, path);
287 cdfile = isofilename;
289 else if (!strcmp(argv[i], "-loadf")) {
290 if (i+1 >= argc) break;
291 loadst_f = argv[++i];
293 else if (!strcmp(argv[i], "-h") ||
294 !strcmp(argv[i], "-help") ||
295 !strcmp(argv[i], "--help")) {
296 printf(PACKAGE_NAME " " PACKAGE_VERSION "\n");
298 " pcsx [options] [file]\n"
300 "\t-cdfile FILE\tRuns a CD image file\n"
301 "\t-nogui\t\tDon't open the GTK GUI\n"
302 "\t-cfg FILE\tLoads desired configuration file (default: ~/.pcsx/pcsx.cfg)\n"
303 "\t-psxout\t\tEnable PSX output\n"
304 "\t-load STATENUM\tLoads savestate STATENUM (1-5)\n"
305 "\t-h -help\tDisplay this message\n"
306 "\tfile\t\tLoads file\n"));
309 strncpy(file, argv[i], MAXPATHLEN);
310 if (file[0] != '/') {
311 getcwd(path, MAXPATHLEN);
312 if (strlen(path) + strlen(file) + 1 < MAXPATHLEN) {
323 set_cd_image(cdfile);
332 menu_init(); // loads config
338 if (LoadPlugins() == -1) {
339 // FIXME: this recovery doesn't work, just delete bad config and bail out
340 // SysMessage("could not load plugins, retrying with defaults\n");
342 snprintf(path, sizeof(path), "." PCSX_DOT_DIR "%s", cfgfile_basename);
344 SysMessage("Failed loading plugins!");
349 if (OpenPlugins() == -1) {
352 plugin_call_rearmed_cbs();
357 if (file[0] != '\0') {
358 if (Load(file) != -1)
362 if (LoadCdrom() == -1) {
364 printf(_("Could not load CD-ROM!\n"));
374 // If a state has been specified, then load that
376 int ret = emu_load_state(loadst - 1);
377 printf("%s state %d\n", ret ? "failed to load" : "loaded", loadst);
380 int ret = LoadState(loadst_f);
381 printf("%s state file: %s\n", ret ? "failed to load" : "loaded", loadst_f);
392 emu_action = SACTION_NONE;
395 if (emu_action != SACTION_NONE)
404 if (EmuInit() == -1) {
405 printf("PSX emulator couldn't be initialized.\n");
409 LoadMcds(Config.Mcd1, Config.Mcd2);
419 printf("SysRunGui\n");
423 printf("StartGui\n");
426 static void dummy_lace()
431 // rearmed hack: EmuReset() runs some code when real BIOS is used,
432 // but we usually do reset from menu while GPU is not open yet,
433 // so we need to prevent updateLace() call..
434 void *real_lace = GPU_updateLace;
435 GPU_updateLace = dummy_lace;
439 // hmh core forgets this
442 GPU_updateLace = real_lace;
451 if (emuLog != NULL) fclose(emuLog);
458 printf("OnFile_Exit\n");
467 int get_state_filename(char *buf, int size, int i) {
468 return get_gameid_filename(buf, size,
469 "." STATES_DIR "%.32s-%.9s.%3.3d", i);
472 int emu_check_state(int slot)
474 char fname[MAXPATHLEN];
477 ret = get_state_filename(fname, sizeof(fname), slot);
481 return CheckState(fname);
484 int emu_save_state(int slot)
486 char fname[MAXPATHLEN];
489 ret = get_state_filename(fname, sizeof(fname), slot);
493 ret = SaveState(fname);
494 printf("* %s \"%s\" [%d]\n", ret == 0 ? "saved" : "failed to save", fname, slot);
498 int emu_load_state(int slot)
500 char fname[MAXPATHLEN];
503 ret = get_state_filename(fname, sizeof(fname), slot);
507 return LoadState(fname);
510 void SysPrintf(const char *fmt, ...) {
515 vsprintf(msg, fmt, list);
518 fprintf(emuLog, "%s", msg);
521 void SysMessage(const char *fmt, ...) {
526 vsprintf(msg, fmt, list);
529 if (msg[strlen(msg) - 1] == '\n')
530 msg[strlen(msg) - 1] = 0;
532 fprintf(stderr, "%s\n", msg);
535 static void SignalExit(int sig) {
540 #define PARSEPATH(dst, src) \
541 ptr = src + strlen(src); \
542 while (*ptr != '\\' && ptr != src) ptr--; \
544 strcpy(dst, ptr+1); \
547 static int _OpenPlugins(void) {
550 signal(SIGINT, SignalExit);
551 signal(SIGPIPE, SignalExit);
553 GPU_clearDynarec(clearDynarec);
556 if (ret < 0) { SysMessage(_("Error opening CD-ROM plugin!")); return -1; }
558 if (ret < 0) { SysMessage(_("Error opening SPU plugin!")); return -1; }
559 SPU_registerCallback(SPUirq);
560 // pcsx-rearmed: we handle gpu elsewhere
561 //ret = GPU_open(&gpuDisp, "PCSX", NULL);
562 //if (ret < 0) { SysMessage(_("Error opening GPU plugin!")); return -1; }
563 ret = PAD1_open(&gpuDisp);
564 if (ret < 0) { SysMessage(_("Error opening Controller 1 plugin!")); return -1; }
565 ret = PAD2_open(&gpuDisp);
566 if (ret < 0) { SysMessage(_("Error opening Controller 2 plugin!")); return -1; }
568 if (Config.UseNet && !NetOpened) {
570 char path[MAXPATHLEN];
571 char dotdir[MAXPATHLEN];
573 MAKE_PATH(dotdir, "/.pcsx/plugins/", NULL);
575 strcpy(info.EmuName, "PCSX " PACKAGE_VERSION);
576 strncpy(info.CdromID, CdromId, 9);
577 strncpy(info.CdromLabel, CdromLabel, 9);
579 info.GPU_showScreenPic = GPU_showScreenPic;
580 info.GPU_displayText = GPU_displayText;
581 info.GPU_showScreenPic = GPU_showScreenPic;
582 info.PAD_setSensitive = PAD1_setSensitive;
583 sprintf(path, "%s%s", Config.BiosDir, Config.Bios);
584 strcpy(info.BIOSpath, path);
585 strcpy(info.MCD1path, Config.Mcd1);
586 strcpy(info.MCD2path, Config.Mcd2);
587 sprintf(path, "%s%s", dotdir, Config.Gpu);
588 strcpy(info.GPUpath, path);
589 sprintf(path, "%s%s", dotdir, Config.Spu);
590 strcpy(info.SPUpath, path);
591 sprintf(path, "%s%s", dotdir, Config.Cdr);
592 strcpy(info.CDRpath, path);
595 ret = NET_open(&gpuDisp);
598 // -2 is returned when something in the info
599 // changed and needs to be synced
602 PARSEPATH(Config.Bios, info.BIOSpath);
603 PARSEPATH(Config.Gpu, info.GPUpath);
604 PARSEPATH(Config.Spu, info.SPUpath);
605 PARSEPATH(Config.Cdr, info.CDRpath);
607 strcpy(Config.Mcd1, info.MCD1path);
608 strcpy(Config.Mcd2, info.MCD2path);
611 Config.UseNet = FALSE;
614 if (NET_queryPlayer() == 1) {
615 if (SendPcsxInfo() == -1) Config.UseNet = FALSE;
617 if (RecvPcsxInfo() == -1) Config.UseNet = FALSE;
621 } else if (Config.UseNet) {
631 while ((ret = _OpenPlugins()) == -2) {
633 LoadMcds(Config.Mcd1, Config.Mcd2);
634 if (LoadPlugins() == -1) return -1;
639 void ClosePlugins() {
642 signal(SIGINT, SIG_DFL);
643 signal(SIGPIPE, SIG_DFL);
645 if (ret < 0) { SysMessage(_("Error closing CD-ROM plugin!")); return; }
647 if (ret < 0) { SysMessage(_("Error closing SPU plugin!")); return; }
649 if (ret < 0) { SysMessage(_("Error closing Controller 1 Plugin!")); return; }
651 if (ret < 0) { SysMessage(_("Error closing Controller 2 plugin!")); return; }
652 // pcsx-rearmed: we handle gpu elsewhere
654 //if (ret < 0) { SysMessage(_("Error closing GPU plugin!")); return; }
662 /* this is to avoid having to hack every plugin to stop using $HOME */
663 char *getenv(const char *name)
665 static char ret[8] = ".";
667 if (name && strcmp(name, "HOME") == 0 &&
668 ((int)name >> 28) == 0) // HACK: let libs find home
671 return real_getenv(name);
675 /* we hook statically linked plugins here */
676 static const char *builtin_plugins[] = {
677 "builtin_gpu", "builtin_spu", "builtin_cdr", "builtin_pad",
681 static const int builtin_plugin_ids[] = {
682 PLUGIN_GPU, PLUGIN_SPU, PLUGIN_CDR, PLUGIN_PAD,
686 void *SysLoadLibrary(const char *lib) {
687 const char *tmp = strrchr(lib, '/');
691 printf("plugin: %s\n", lib);
695 for (i = 0; i < ARRAY_SIZE(builtin_plugins); i++)
696 if (strcmp(tmp, builtin_plugins[i]) == 0)
697 return (void *)(long)(PLUGIN_DL_BASE + builtin_plugin_ids[i]);
700 #if defined(__x86_64__) || defined(__i386__)
702 char name[MAXPATHLEN];
703 snprintf(name, sizeof(name), "%s.x86", lib);
707 ret = dlopen(lib, RTLD_NOW);
709 fprintf(stderr, "dlopen: %s\n", dlerror());
713 void *SysLoadSym(void *lib, const char *sym) {
714 unsigned int plugid = (unsigned int)(long)lib;
716 if (PLUGIN_DL_BASE <= plugid && plugid < PLUGIN_DL_BASE + ARRAY_SIZE(builtin_plugins))
717 return plugin_link(plugid - PLUGIN_DL_BASE, sym);
719 return dlsym(lib, sym);
722 const char *SysLibError() {
726 void SysCloseLibrary(void *lib) {
727 unsigned int plugid = (unsigned int)(long)lib;
729 if (PLUGIN_DL_BASE <= plugid && plugid < PLUGIN_DL_BASE + ARRAY_SIZE(builtin_plugins))