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 void do_emu_action(void)
127 char buf[MAXPATHLEN];
130 emu_action_old = emu_action;
132 switch (emu_action) {
135 case SACTION_ENTER_MENU:
138 case SACTION_LOAD_STATE:
139 ret = emu_load_state(state_slot);
140 snprintf(hud_msg, sizeof(hud_msg), ret == 0 ? "LOADED" : "FAIL!");
142 case SACTION_SAVE_STATE:
143 ret = emu_save_state(state_slot);
144 snprintf(hud_msg, sizeof(hud_msg), ret == 0 ? "SAVED" : "FAIL!");
146 case SACTION_NEXT_SSLOT:
151 case SACTION_PREV_SSLOT:
156 case SACTION_TOGGLE_FSKIP:
157 pl_rearmed_cbs.frameskip ^= 1;
158 snprintf(hud_msg, sizeof(hud_msg), "FRAMESKIP %s",
159 pl_rearmed_cbs.frameskip ? "ON" : "OFF");
161 case SACTION_SCREENSHOT:
165 time_t t = time(NULL);
166 struct tm *tb = localtime(&t);
167 int ti = tb->tm_yday * 1000000 + tb->tm_hour * 10000 +
168 tb->tm_min * 100 + tb->tm_sec;
170 scrbuf = pl_prepare_screenshot(&w, &h, &bpp);
171 get_gameid_filename(buf, sizeof(buf),
172 "screenshots/%.32s-%.9s.%d.png", ti);
174 if (scrbuf != 0 && bpp == 16)
175 ret = writepng(buf, scrbuf, w, h);
177 snprintf(hud_msg, sizeof(hud_msg), "SCREENSHOT TAKEN");
185 snprintf(hud_msg, sizeof(hud_msg), "STATE SLOT %d [%s]", state_slot,
186 emu_check_state(state_slot) == 0 ? "USED" : "FREE");
190 int main(int argc, char *argv[])
194 tmp = dlopen("/lib/libdl.so.2", RTLD_LAZY);
196 tmp = dlopen("/lib32/libdl.so.2", RTLD_LAZY);
198 real_getenv = dlsym(tmp, "getenv");
199 if (real_getenv == NULL) {
200 fprintf(stderr, "%s\n", dlerror());
205 // what is the name of the config file?
206 // it may be redefined by -cfg on the command line
207 strcpy(cfgfile_basename, "pcsx.cfg");
212 memset(&Config, 0, sizeof(Config));
216 strcpy(Config.Bios, "HLE");
219 extern int maemo_main(int argc, char **argv);
220 return maemo_main(argc, argv);
222 char file[MAXPATHLEN] = "";
223 char path[MAXPATHLEN];
224 const char *cdfile = NULL;
225 const char *loadst_f = NULL;
230 // read command line options
231 for (i = 1; i < argc; i++) {
232 if (!strcmp(argv[i], "-psxout")) psxout = 1;
233 else if (!strcmp(argv[i], "-load")) loadst = atol(argv[++i]);
234 else if (!strcmp(argv[i], "-cfg")) {
235 if (i+1 >= argc) break;
236 strncpy(cfgfile_basename, argv[++i], MAXPATHLEN-100); /* TODO buffer overruns */
237 printf("Using config file %s.\n", cfgfile_basename);
239 else if (!strcmp(argv[i], "-cdfile")) {
240 char isofilename[MAXPATHLEN];
242 if (i+1 >= argc) break;
243 strncpy(isofilename, argv[++i], MAXPATHLEN);
244 if (isofilename[0] != '/') {
245 getcwd(path, MAXPATHLEN);
246 if (strlen(path) + strlen(isofilename) + 1 < MAXPATHLEN) {
248 strcat(path, isofilename);
249 strcpy(isofilename, path);
254 cdfile = isofilename;
256 else if (!strcmp(argv[i], "-loadf")) {
257 if (i+1 >= argc) break;
258 loadst_f = argv[++i];
260 else if (!strcmp(argv[i], "-h") ||
261 !strcmp(argv[i], "-help") ||
262 !strcmp(argv[i], "--help")) {
263 printf(PACKAGE_NAME " " PACKAGE_VERSION "\n");
265 " pcsx [options] [file]\n"
267 "\t-cdfile FILE\tRuns a CD image file\n"
268 "\t-nogui\t\tDon't open the GTK GUI\n"
269 "\t-cfg FILE\tLoads desired configuration file (default: ~/.pcsx/pcsx.cfg)\n"
270 "\t-psxout\t\tEnable PSX output\n"
271 "\t-load STATENUM\tLoads savestate STATENUM (1-5)\n"
272 "\t-h -help\tDisplay this message\n"
273 "\tfile\t\tLoads file\n"));
276 strncpy(file, argv[i], MAXPATHLEN);
277 if (file[0] != '/') {
278 getcwd(path, MAXPATHLEN);
279 if (strlen(path) + strlen(file) + 1 < MAXPATHLEN) {
290 set_cd_image(cdfile);
299 menu_init(); // loads config
304 if (LoadPlugins() == -1) {
305 // FIXME: this recovery doesn't work, just delete bad config and bail out
306 // SysMessage("could not load plugins, retrying with defaults\n");
308 snprintf(path, sizeof(path), "." PCSX_DOT_DIR "%s", cfgfile_basename);
310 SysMessage("Failed loading plugins!");
315 if (OpenPlugins() == -1) {
318 plugin_call_rearmed_cbs();
323 if (file[0] != '\0') {
324 if (Load(file) != -1)
328 if (LoadCdrom() == -1) {
330 printf(_("Could not load CD-ROM!\n"));
340 // If a state has been specified, then load that
342 int ret = emu_load_state(loadst - 1);
343 printf("%s state %d\n", ret ? "failed to load" : "loaded", loadst);
346 int ret = LoadState(loadst_f);
347 printf("%s state file: %s\n", ret ? "failed to load" : "loaded", loadst_f);
358 emu_action = SACTION_NONE;
361 if (emu_action != SACTION_NONE)
370 if (EmuInit() == -1) {
371 printf("PSX emulator couldn't be initialized.\n");
375 LoadMcds(Config.Mcd1, Config.Mcd2); /* TODO Do we need to have this here, or in the calling main() function?? */
385 printf("SysRunGui\n");
389 printf("StartGui\n");
392 static void dummy_lace()
397 // rearmed hack: EmuReset() runs some code when real BIOS is used,
398 // but we usually do reset from menu while GPU is not open yet,
399 // so we need to prevent updateLace() call..
400 void *real_lace = GPU_updateLace;
401 GPU_updateLace = dummy_lace;
405 // hmh core forgets this
408 GPU_updateLace = real_lace;
417 if (emuLog != NULL) fclose(emuLog);
424 printf("OnFile_Exit\n");
433 int get_state_filename(char *buf, int size, int i) {
434 return get_gameid_filename(buf, size,
435 "." STATES_DIR "%.32s-%.9s.%3.3d", i);
438 int emu_check_state(int slot)
440 char fname[MAXPATHLEN];
443 ret = get_state_filename(fname, sizeof(fname), slot);
447 return CheckState(fname);
450 int emu_save_state(int slot)
452 char fname[MAXPATHLEN];
455 ret = get_state_filename(fname, sizeof(fname), slot);
459 return SaveState(fname);
462 int emu_load_state(int slot)
464 char fname[MAXPATHLEN];
467 ret = get_state_filename(fname, sizeof(fname), slot);
471 return LoadState(fname);
474 void SysPrintf(const char *fmt, ...) {
479 vsprintf(msg, fmt, list);
482 fprintf(emuLog, "%s", msg);
485 void SysMessage(const char *fmt, ...) {
490 vsprintf(msg, fmt, list);
493 if (msg[strlen(msg) - 1] == '\n')
494 msg[strlen(msg) - 1] = 0;
496 fprintf(stderr, "%s\n", msg);
499 static void SignalExit(int sig) {
504 #define PARSEPATH(dst, src) \
505 ptr = src + strlen(src); \
506 while (*ptr != '\\' && ptr != src) ptr--; \
508 strcpy(dst, ptr+1); \
511 static int _OpenPlugins(void) {
514 signal(SIGINT, SignalExit);
515 signal(SIGPIPE, SignalExit);
517 GPU_clearDynarec(clearDynarec);
520 if (ret < 0) { SysMessage(_("Error opening CD-ROM plugin!")); return -1; }
522 if (ret < 0) { SysMessage(_("Error opening SPU plugin!")); return -1; }
523 SPU_registerCallback(SPUirq);
524 // pcsx-rearmed: we handle gpu elsewhere
525 //ret = GPU_open(&gpuDisp, "PCSX", NULL);
526 //if (ret < 0) { SysMessage(_("Error opening GPU plugin!")); return -1; }
527 ret = PAD1_open(&gpuDisp);
528 if (ret < 0) { SysMessage(_("Error opening Controller 1 plugin!")); return -1; }
529 ret = PAD2_open(&gpuDisp);
530 if (ret < 0) { SysMessage(_("Error opening Controller 2 plugin!")); return -1; }
532 if (Config.UseNet && !NetOpened) {
534 char path[MAXPATHLEN];
535 char dotdir[MAXPATHLEN];
537 MAKE_PATH(dotdir, "/.pcsx/plugins/", NULL);
539 strcpy(info.EmuName, "PCSX " PACKAGE_VERSION);
540 strncpy(info.CdromID, CdromId, 9);
541 strncpy(info.CdromLabel, CdromLabel, 9);
543 info.GPU_showScreenPic = GPU_showScreenPic;
544 info.GPU_displayText = GPU_displayText;
545 info.GPU_showScreenPic = GPU_showScreenPic;
546 info.PAD_setSensitive = PAD1_setSensitive;
547 sprintf(path, "%s%s", Config.BiosDir, Config.Bios);
548 strcpy(info.BIOSpath, path);
549 strcpy(info.MCD1path, Config.Mcd1);
550 strcpy(info.MCD2path, Config.Mcd2);
551 sprintf(path, "%s%s", dotdir, Config.Gpu);
552 strcpy(info.GPUpath, path);
553 sprintf(path, "%s%s", dotdir, Config.Spu);
554 strcpy(info.SPUpath, path);
555 sprintf(path, "%s%s", dotdir, Config.Cdr);
556 strcpy(info.CDRpath, path);
559 ret = NET_open(&gpuDisp);
562 // -2 is returned when something in the info
563 // changed and needs to be synced
566 PARSEPATH(Config.Bios, info.BIOSpath);
567 PARSEPATH(Config.Gpu, info.GPUpath);
568 PARSEPATH(Config.Spu, info.SPUpath);
569 PARSEPATH(Config.Cdr, info.CDRpath);
571 strcpy(Config.Mcd1, info.MCD1path);
572 strcpy(Config.Mcd2, info.MCD2path);
575 Config.UseNet = FALSE;
578 if (NET_queryPlayer() == 1) {
579 if (SendPcsxInfo() == -1) Config.UseNet = FALSE;
581 if (RecvPcsxInfo() == -1) Config.UseNet = FALSE;
585 } else if (Config.UseNet) {
595 while ((ret = _OpenPlugins()) == -2) {
597 LoadMcds(Config.Mcd1, Config.Mcd2);
598 if (LoadPlugins() == -1) return -1;
603 void ClosePlugins() {
606 signal(SIGINT, SIG_DFL);
607 signal(SIGPIPE, SIG_DFL);
609 if (ret < 0) { SysMessage(_("Error closing CD-ROM plugin!")); return; }
611 if (ret < 0) { SysMessage(_("Error closing SPU plugin!")); return; }
613 if (ret < 0) { SysMessage(_("Error closing Controller 1 Plugin!")); return; }
615 if (ret < 0) { SysMessage(_("Error closing Controller 2 plugin!")); return; }
616 // pcsx-rearmed: we handle gpu elsewhere
618 //if (ret < 0) { SysMessage(_("Error closing GPU plugin!")); return; }
626 /* this is to avoid having to hack every plugin to stop using $HOME */
627 char *getenv(const char *name)
629 static char ret[8] = ".";
631 if (name && strcmp(name, "HOME") == 0 &&
632 ((int)name >> 28) == 0) // HACK: let libs find home
635 return real_getenv(name);
639 /* we hook statically linked plugins here */
640 static const char *builtin_plugins[] = {
641 "builtin_gpu", "builtin_spu", "builtin_cdr", "builtin_pad",
645 static const int builtin_plugin_ids[] = {
646 PLUGIN_GPU, PLUGIN_SPU, PLUGIN_CDR, PLUGIN_PAD,
650 void *SysLoadLibrary(const char *lib) {
651 const char *tmp = strrchr(lib, '/');
655 printf("plugin: %s\n", lib);
659 for (i = 0; i < ARRAY_SIZE(builtin_plugins); i++)
660 if (strcmp(tmp, builtin_plugins[i]) == 0)
661 return (void *)(long)(PLUGIN_DL_BASE + builtin_plugin_ids[i]);
664 #if defined(__x86_64__) || defined(__i386__)
666 char name[MAXPATHLEN];
667 snprintf(name, sizeof(name), "%s.x86", lib);
671 ret = dlopen(lib, RTLD_NOW);
673 fprintf(stderr, "dlopen: %s\n", dlerror());
677 void *SysLoadSym(void *lib, const char *sym) {
678 unsigned int plugid = (unsigned int)(long)lib;
680 if (PLUGIN_DL_BASE <= plugid && plugid < PLUGIN_DL_BASE + ARRAY_SIZE(builtin_plugins))
681 return plugin_link(plugid - PLUGIN_DL_BASE, sym);
683 return dlsym(lib, sym);
686 const char *SysLibError() {
690 void SysCloseLibrary(void *lib) {
691 unsigned int plugid = (unsigned int)(long)lib;
693 if (PLUGIN_DL_BASE <= plugid && plugid < PLUGIN_DL_BASE + ARRAY_SIZE(builtin_plugins))