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"
31 unsigned long gpuDisp;
32 char cfgfile_basename[MAXPATHLEN];
33 static char *(*real_getenv)(const char *name);
35 enum sched_action emu_action, emu_action_old;
39 static void make_path(char *buf, size_t size, const char *dir, const char *fname)
42 snprintf(buf, size, ".%s%s", dir, fname);
44 snprintf(buf, size, ".%s", dir);
46 #define MAKE_PATH(buf, dir, fname) \
47 make_path(buf, sizeof(buf), dir, fname)
49 static void create_profile_dir(const char *directory) {
50 char path[MAXPATHLEN];
52 MAKE_PATH(path, directory, NULL);
53 mkdir(path, S_IRWXU | S_IRWXG);
56 static void CheckSubDir() {
57 // make sure that ~/.pcsx exists
58 create_profile_dir(PCSX_DOT_DIR);
60 create_profile_dir(BIOS_DIR);
61 create_profile_dir(MEMCARD_DIR);
62 create_profile_dir(STATES_DIR);
63 create_profile_dir(PLUGINS_DIR);
64 create_profile_dir(PLUGINS_CFG_DIR);
65 create_profile_dir(CHEATS_DIR);
66 create_profile_dir(PATCHES_DIR);
67 create_profile_dir(PCSX_DOT_DIR "cfg");
68 create_profile_dir("/screenshots/");
71 static int get_gameid_filename(char *buf, int size, const char *fmt, int i) {
75 strncpy(trimlabel, CdromLabel, 32);
77 for (j = 31; j >= 0; j--)
78 if (trimlabel[j] == ' ')
83 snprintf(buf, size, fmt, trimlabel, CdromId, i);
88 void set_cd_image(const char *fname)
90 const char *ext = NULL;
93 ext = strrchr(fname, '.');
96 strcasecmp(ext, ".z") == 0 || strcasecmp(ext, ".bz") == 0 ||
97 strcasecmp(ext, ".znx") == 0 /*|| strcasecmp(ext, ".pbp") == 0*/)) {
99 cdrcimg_set_fname(fname);
100 strcpy(Config.Cdr, "builtin_cdrcimg");
103 strcpy(Config.Cdr, "builtin_cdr");
107 static void set_default_paths(void)
109 MAKE_PATH(Config.Mcd1, MEMCARD_DIR, "card1.mcd");
110 MAKE_PATH(Config.Mcd2, MEMCARD_DIR, "card2.mcd");
111 strcpy(Config.BiosDir, "bios");
113 strcpy(Config.PluginsDir, "plugins");
114 strcpy(Config.Gpu, "builtin_gpu");
115 strcpy(Config.Spu, "builtin_spu");
116 strcpy(Config.Cdr, "builtin_cdr");
117 strcpy(Config.Pad1, "builtin_pad");
118 strcpy(Config.Pad2, "builtin_pad");
119 strcpy(Config.Net, "Disabled");
122 snprintf(Config.PatchesDir, sizeof(Config.PatchesDir), "." PATCHES_DIR);
125 static void check_memcards(void)
127 char buf[MAXPATHLEN];
131 for (i = 1; i <= 9; i++) {
132 snprintf(buf, sizeof(buf), ".%scard%d.mcd", MEMCARD_DIR, i);
134 f = fopen(buf, "rb");
136 printf("Creating memcard: %s\n", buf);
144 void do_emu_action(void)
146 char buf[MAXPATHLEN];
149 emu_action_old = emu_action;
151 switch (emu_action) {
152 case SACTION_ENTER_MENU:
155 case SACTION_LOAD_STATE:
156 ret = emu_load_state(state_slot);
157 snprintf(hud_msg, sizeof(hud_msg), ret == 0 ? "LOADED" : "FAIL!");
159 case SACTION_SAVE_STATE:
160 ret = emu_save_state(state_slot);
161 snprintf(hud_msg, sizeof(hud_msg), ret == 0 ? "SAVED" : "FAIL!");
163 case SACTION_NEXT_SSLOT:
168 case SACTION_PREV_SSLOT:
173 case SACTION_TOGGLE_FSKIP:
174 pl_rearmed_cbs.fskip_advice = 0;
175 pl_rearmed_cbs.frameskip++;
176 if (pl_rearmed_cbs.frameskip > 1)
177 pl_rearmed_cbs.frameskip = -1;
178 snprintf(hud_msg, sizeof(hud_msg), "FRAMESKIP: %s",
179 pl_rearmed_cbs.frameskip == -1 ? "AUTO" :
180 pl_rearmed_cbs.frameskip == 0 ? "OFF" : "1" );
181 plugin_call_rearmed_cbs();
183 case SACTION_SCREENSHOT:
187 time_t t = time(NULL);
188 struct tm *tb = localtime(&t);
189 int ti = tb->tm_yday * 1000000 + tb->tm_hour * 10000 +
190 tb->tm_min * 100 + tb->tm_sec;
192 scrbuf = pl_prepare_screenshot(&w, &h, &bpp);
193 get_gameid_filename(buf, sizeof(buf),
194 "screenshots/%.32s-%.9s.%d.png", ti);
196 if (scrbuf != 0 && bpp == 16)
197 ret = writepng(buf, scrbuf, w, h);
199 snprintf(hud_msg, sizeof(hud_msg), "SCREENSHOT TAKEN");
209 snprintf(hud_msg, sizeof(hud_msg), "STATE SLOT %d [%s]", state_slot,
210 emu_check_state(state_slot) == 0 ? "USED" : "FREE");
212 printf("* %s\n", hud_msg);
215 int main(int argc, char *argv[])
219 tmp = dlopen("/lib/libdl.so.2", RTLD_LAZY);
221 tmp = dlopen("/lib32/libdl.so.2", RTLD_LAZY);
223 real_getenv = dlsym(tmp, "getenv");
224 if (real_getenv == NULL) {
225 fprintf(stderr, "%s\n", dlerror());
230 // what is the name of the config file?
231 // it may be redefined by -cfg on the command line
232 strcpy(cfgfile_basename, "pcsx.cfg");
237 memset(&Config, 0, sizeof(Config));
242 strcpy(Config.Bios, "HLE");
245 extern int maemo_main(int argc, char **argv);
246 return maemo_main(argc, argv);
248 char file[MAXPATHLEN] = "";
249 char path[MAXPATHLEN];
250 const char *cdfile = NULL;
251 const char *loadst_f = NULL;
256 // read command line options
257 for (i = 1; i < argc; i++) {
258 if (!strcmp(argv[i], "-psxout")) psxout = 1;
259 else if (!strcmp(argv[i], "-load")) loadst = atol(argv[++i]);
260 else if (!strcmp(argv[i], "-cfg")) {
261 if (i+1 >= argc) break;
262 strncpy(cfgfile_basename, argv[++i], MAXPATHLEN-100); /* TODO buffer overruns */
263 printf("Using config file %s.\n", cfgfile_basename);
265 else if (!strcmp(argv[i], "-cdfile")) {
266 char isofilename[MAXPATHLEN];
268 if (i+1 >= argc) break;
269 strncpy(isofilename, argv[++i], MAXPATHLEN);
270 if (isofilename[0] != '/') {
271 getcwd(path, MAXPATHLEN);
272 if (strlen(path) + strlen(isofilename) + 1 < MAXPATHLEN) {
274 strcat(path, isofilename);
275 strcpy(isofilename, path);
280 cdfile = isofilename;
282 else if (!strcmp(argv[i], "-loadf")) {
283 if (i+1 >= argc) break;
284 loadst_f = argv[++i];
286 else if (!strcmp(argv[i], "-h") ||
287 !strcmp(argv[i], "-help") ||
288 !strcmp(argv[i], "--help")) {
289 printf(PACKAGE_NAME " " PACKAGE_VERSION "\n");
291 " pcsx [options] [file]\n"
293 "\t-cdfile FILE\tRuns a CD image file\n"
294 "\t-nogui\t\tDon't open the GTK GUI\n"
295 "\t-cfg FILE\tLoads desired configuration file (default: ~/.pcsx/pcsx.cfg)\n"
296 "\t-psxout\t\tEnable PSX output\n"
297 "\t-load STATENUM\tLoads savestate STATENUM (1-5)\n"
298 "\t-h -help\tDisplay this message\n"
299 "\tfile\t\tLoads file\n"));
302 strncpy(file, argv[i], MAXPATHLEN);
303 if (file[0] != '/') {
304 getcwd(path, MAXPATHLEN);
305 if (strlen(path) + strlen(file) + 1 < MAXPATHLEN) {
316 set_cd_image(cdfile);
325 menu_init(); // loads config
331 if (LoadPlugins() == -1) {
332 // FIXME: this recovery doesn't work, just delete bad config and bail out
333 // SysMessage("could not load plugins, retrying with defaults\n");
335 snprintf(path, sizeof(path), "." PCSX_DOT_DIR "%s", cfgfile_basename);
337 SysMessage("Failed loading plugins!");
342 if (OpenPlugins() == -1) {
345 plugin_call_rearmed_cbs();
350 if (file[0] != '\0') {
351 if (Load(file) != -1)
355 if (LoadCdrom() == -1) {
357 printf(_("Could not load CD-ROM!\n"));
367 // If a state has been specified, then load that
369 int ret = emu_load_state(loadst - 1);
370 printf("%s state %d\n", ret ? "failed to load" : "loaded", loadst);
373 int ret = LoadState(loadst_f);
374 printf("%s state file: %s\n", ret ? "failed to load" : "loaded", loadst_f);
385 emu_action = SACTION_NONE;
388 if (emu_action != SACTION_NONE)
397 if (EmuInit() == -1) {
398 printf("PSX emulator couldn't be initialized.\n");
402 LoadMcds(Config.Mcd1, Config.Mcd2);
412 printf("SysRunGui\n");
416 printf("StartGui\n");
419 static void dummy_lace()
424 // rearmed hack: EmuReset() runs some code when real BIOS is used,
425 // but we usually do reset from menu while GPU is not open yet,
426 // so we need to prevent updateLace() call..
427 void *real_lace = GPU_updateLace;
428 GPU_updateLace = dummy_lace;
432 // hmh core forgets this
435 GPU_updateLace = real_lace;
444 if (emuLog != NULL) fclose(emuLog);
451 printf("OnFile_Exit\n");
460 int get_state_filename(char *buf, int size, int i) {
461 return get_gameid_filename(buf, size,
462 "." STATES_DIR "%.32s-%.9s.%3.3d", i);
465 int emu_check_state(int slot)
467 char fname[MAXPATHLEN];
470 ret = get_state_filename(fname, sizeof(fname), slot);
474 return CheckState(fname);
477 int emu_save_state(int slot)
479 char fname[MAXPATHLEN];
482 ret = get_state_filename(fname, sizeof(fname), slot);
486 ret = SaveState(fname);
487 printf("* %s \"%s\" [%d]\n", ret == 0 ? "saved" : "failed to save", fname, slot);
491 int emu_load_state(int slot)
493 char fname[MAXPATHLEN];
496 ret = get_state_filename(fname, sizeof(fname), slot);
500 return LoadState(fname);
503 void SysPrintf(const char *fmt, ...) {
508 vsprintf(msg, fmt, list);
511 fprintf(emuLog, "%s", msg);
514 void SysMessage(const char *fmt, ...) {
519 vsprintf(msg, fmt, list);
522 if (msg[strlen(msg) - 1] == '\n')
523 msg[strlen(msg) - 1] = 0;
525 fprintf(stderr, "%s\n", msg);
528 static void SignalExit(int sig) {
533 #define PARSEPATH(dst, src) \
534 ptr = src + strlen(src); \
535 while (*ptr != '\\' && ptr != src) ptr--; \
537 strcpy(dst, ptr+1); \
540 static int _OpenPlugins(void) {
543 signal(SIGINT, SignalExit);
544 signal(SIGPIPE, SignalExit);
546 GPU_clearDynarec(clearDynarec);
549 if (ret < 0) { SysMessage(_("Error opening CD-ROM plugin!")); return -1; }
551 if (ret < 0) { SysMessage(_("Error opening SPU plugin!")); return -1; }
552 SPU_registerCallback(SPUirq);
553 // pcsx-rearmed: we handle gpu elsewhere
554 //ret = GPU_open(&gpuDisp, "PCSX", NULL);
555 //if (ret < 0) { SysMessage(_("Error opening GPU plugin!")); return -1; }
556 ret = PAD1_open(&gpuDisp);
557 if (ret < 0) { SysMessage(_("Error opening Controller 1 plugin!")); return -1; }
558 ret = PAD2_open(&gpuDisp);
559 if (ret < 0) { SysMessage(_("Error opening Controller 2 plugin!")); return -1; }
561 if (Config.UseNet && !NetOpened) {
563 char path[MAXPATHLEN];
564 char dotdir[MAXPATHLEN];
566 MAKE_PATH(dotdir, "/.pcsx/plugins/", NULL);
568 strcpy(info.EmuName, "PCSX " PACKAGE_VERSION);
569 strncpy(info.CdromID, CdromId, 9);
570 strncpy(info.CdromLabel, CdromLabel, 9);
572 info.GPU_showScreenPic = GPU_showScreenPic;
573 info.GPU_displayText = GPU_displayText;
574 info.GPU_showScreenPic = GPU_showScreenPic;
575 info.PAD_setSensitive = PAD1_setSensitive;
576 sprintf(path, "%s%s", Config.BiosDir, Config.Bios);
577 strcpy(info.BIOSpath, path);
578 strcpy(info.MCD1path, Config.Mcd1);
579 strcpy(info.MCD2path, Config.Mcd2);
580 sprintf(path, "%s%s", dotdir, Config.Gpu);
581 strcpy(info.GPUpath, path);
582 sprintf(path, "%s%s", dotdir, Config.Spu);
583 strcpy(info.SPUpath, path);
584 sprintf(path, "%s%s", dotdir, Config.Cdr);
585 strcpy(info.CDRpath, path);
588 ret = NET_open(&gpuDisp);
591 // -2 is returned when something in the info
592 // changed and needs to be synced
595 PARSEPATH(Config.Bios, info.BIOSpath);
596 PARSEPATH(Config.Gpu, info.GPUpath);
597 PARSEPATH(Config.Spu, info.SPUpath);
598 PARSEPATH(Config.Cdr, info.CDRpath);
600 strcpy(Config.Mcd1, info.MCD1path);
601 strcpy(Config.Mcd2, info.MCD2path);
604 Config.UseNet = FALSE;
607 if (NET_queryPlayer() == 1) {
608 if (SendPcsxInfo() == -1) Config.UseNet = FALSE;
610 if (RecvPcsxInfo() == -1) Config.UseNet = FALSE;
614 } else if (Config.UseNet) {
624 while ((ret = _OpenPlugins()) == -2) {
626 LoadMcds(Config.Mcd1, Config.Mcd2);
627 if (LoadPlugins() == -1) return -1;
632 void ClosePlugins() {
635 signal(SIGINT, SIG_DFL);
636 signal(SIGPIPE, SIG_DFL);
638 if (ret < 0) { SysMessage(_("Error closing CD-ROM plugin!")); return; }
640 if (ret < 0) { SysMessage(_("Error closing SPU plugin!")); return; }
642 if (ret < 0) { SysMessage(_("Error closing Controller 1 Plugin!")); return; }
644 if (ret < 0) { SysMessage(_("Error closing Controller 2 plugin!")); return; }
645 // pcsx-rearmed: we handle gpu elsewhere
647 //if (ret < 0) { SysMessage(_("Error closing GPU plugin!")); return; }
655 /* this is to avoid having to hack every plugin to stop using $HOME */
656 char *getenv(const char *name)
658 static char ret[8] = ".";
660 if (name && strcmp(name, "HOME") == 0 &&
661 ((int)name >> 28) == 0) // HACK: let libs find home
664 return real_getenv(name);
668 /* we hook statically linked plugins here */
669 static const char *builtin_plugins[] = {
670 "builtin_gpu", "builtin_spu", "builtin_cdr", "builtin_pad",
674 static const int builtin_plugin_ids[] = {
675 PLUGIN_GPU, PLUGIN_SPU, PLUGIN_CDR, PLUGIN_PAD,
679 void *SysLoadLibrary(const char *lib) {
680 const char *tmp = strrchr(lib, '/');
684 printf("plugin: %s\n", lib);
688 for (i = 0; i < ARRAY_SIZE(builtin_plugins); i++)
689 if (strcmp(tmp, builtin_plugins[i]) == 0)
690 return (void *)(long)(PLUGIN_DL_BASE + builtin_plugin_ids[i]);
693 #if defined(__x86_64__) || defined(__i386__)
695 char name[MAXPATHLEN];
696 snprintf(name, sizeof(name), "%s.x86", lib);
700 ret = dlopen(lib, RTLD_NOW);
702 fprintf(stderr, "dlopen: %s\n", dlerror());
706 void *SysLoadSym(void *lib, const char *sym) {
707 unsigned int plugid = (unsigned int)(long)lib;
709 if (PLUGIN_DL_BASE <= plugid && plugid < PLUGIN_DL_BASE + ARRAY_SIZE(builtin_plugins))
710 return plugin_link(plugid - PLUGIN_DL_BASE, sym);
712 return dlsym(lib, sym);
715 const char *SysLibError() {
719 void SysCloseLibrary(void *lib) {
720 unsigned int plugid = (unsigned int)(long)lib;
722 if (PLUGIN_DL_BASE <= plugid && plugid < PLUGIN_DL_BASE + ARRAY_SIZE(builtin_plugins))