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/readpng.h"
28 #include "common/input.h"
29 #include "linux/in_evdev.h"
31 // don't include debug.h - it breaks ARM build (R1 redefined)
36 unsigned long gpuDisp;
37 char cfgfile_basename[MAXPATHLEN];
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[])
224 // what is the name of the config file?
225 // it may be redefined by -cfg on the command line
226 strcpy(cfgfile_basename, "pcsx.cfg");
231 memset(&Config, 0, sizeof(Config));
236 strcpy(Config.Bios, "HLE");
239 extern int maemo_main(int argc, char **argv);
240 return maemo_main(argc, argv);
242 char file[MAXPATHLEN] = "";
243 char path[MAXPATHLEN];
244 const char *cdfile = NULL;
245 const char *loadst_f = NULL;
250 // read command line options
251 for (i = 1; i < argc; i++) {
252 if (!strcmp(argv[i], "-psxout")) psxout = 1;
253 else if (!strcmp(argv[i], "-load")) loadst = atol(argv[++i]);
254 else if (!strcmp(argv[i], "-cfg")) {
255 if (i+1 >= argc) break;
256 strncpy(cfgfile_basename, argv[++i], MAXPATHLEN-100); /* TODO buffer overruns */
257 printf("Using config file %s.\n", cfgfile_basename);
259 else if (!strcmp(argv[i], "-cdfile")) {
260 char isofilename[MAXPATHLEN];
262 if (i+1 >= argc) break;
263 strncpy(isofilename, argv[++i], MAXPATHLEN);
264 if (isofilename[0] != '/') {
265 getcwd(path, MAXPATHLEN);
266 if (strlen(path) + strlen(isofilename) + 1 < MAXPATHLEN) {
268 strcat(path, isofilename);
269 strcpy(isofilename, path);
274 cdfile = isofilename;
276 else if (!strcmp(argv[i], "-loadf")) {
277 if (i+1 >= argc) break;
278 loadst_f = argv[++i];
280 else if (!strcmp(argv[i], "-h") ||
281 !strcmp(argv[i], "-help") ||
282 !strcmp(argv[i], "--help")) {
283 printf(PACKAGE_NAME " " PACKAGE_VERSION "\n");
285 " pcsx [options] [file]\n"
287 "\t-cdfile FILE\tRuns a CD image file\n"
288 "\t-nogui\t\tDon't open the GTK GUI\n"
289 "\t-cfg FILE\tLoads desired configuration file (default: ~/.pcsx/pcsx.cfg)\n"
290 "\t-psxout\t\tEnable PSX output\n"
291 "\t-load STATENUM\tLoads savestate STATENUM (1-5)\n"
292 "\t-h -help\tDisplay this message\n"
293 "\tfile\t\tLoads file\n"));
296 strncpy(file, argv[i], MAXPATHLEN);
297 if (file[0] != '/') {
298 getcwd(path, MAXPATHLEN);
299 if (strlen(path) + strlen(file) + 1 < MAXPATHLEN) {
310 set_cd_image(cdfile);
320 menu_init(); // loads config
322 plat_rescan_inputs();
327 if (LoadPlugins() == -1) {
328 // FIXME: this recovery doesn't work, just delete bad config and bail out
329 // SysMessage("could not load plugins, retrying with defaults\n");
331 snprintf(path, sizeof(path), "." PCSX_DOT_DIR "%s", cfgfile_basename);
333 SysMessage("Failed loading plugins!");
338 if (OpenPlugins() == -1) {
341 plugin_call_rearmed_cbs();
346 if (file[0] != '\0') {
347 if (Load(file) != -1)
351 if (LoadCdrom() == -1) {
353 printf(_("Could not load CD-ROM!\n"));
363 // If a state has been specified, then load that
365 int ret = emu_load_state(loadst - 1);
366 printf("%s state %d\n", ret ? "failed to load" : "loaded", loadst);
369 int ret = LoadState(loadst_f);
370 printf("%s state file: %s\n", ret ? "failed to load" : "loaded", loadst_f);
381 emu_action = SACTION_NONE;
384 if (emu_action != SACTION_NONE)
393 if (EmuInit() == -1) {
394 printf("PSX emulator couldn't be initialized.\n");
398 LoadMcds(Config.Mcd1, Config.Mcd2);
408 printf("SysRunGui\n");
412 printf("StartGui\n");
415 static void dummy_lace()
420 // rearmed hack: EmuReset() runs some code when real BIOS is used,
421 // but we usually do reset from menu while GPU is not open yet,
422 // so we need to prevent updateLace() call..
423 void *real_lace = GPU_updateLace;
424 GPU_updateLace = dummy_lace;
428 // hmh core forgets this
431 GPU_updateLace = real_lace;
440 if (emuLog != NULL) fclose(emuLog);
447 printf("OnFile_Exit\n");
456 int get_state_filename(char *buf, int size, int i) {
457 return get_gameid_filename(buf, size,
458 "." STATES_DIR "%.32s-%.9s.%3.3d", i);
461 int emu_check_state(int slot)
463 char fname[MAXPATHLEN];
466 ret = get_state_filename(fname, sizeof(fname), slot);
470 return CheckState(fname);
473 int emu_save_state(int slot)
475 char fname[MAXPATHLEN];
478 ret = get_state_filename(fname, sizeof(fname), slot);
482 ret = SaveState(fname);
483 printf("* %s \"%s\" [%d]\n", ret == 0 ? "saved" : "failed to save", fname, slot);
487 int emu_load_state(int slot)
489 char fname[MAXPATHLEN];
492 ret = get_state_filename(fname, sizeof(fname), slot);
496 return LoadState(fname);
499 void SysPrintf(const char *fmt, ...) {
504 vsprintf(msg, fmt, list);
507 fprintf(emuLog, "%s", msg);
510 void SysMessage(const char *fmt, ...) {
515 vsprintf(msg, fmt, list);
518 if (msg[strlen(msg) - 1] == '\n')
519 msg[strlen(msg) - 1] = 0;
521 fprintf(stderr, "%s\n", msg);
524 static void SignalExit(int sig) {
529 #define PARSEPATH(dst, src) \
530 ptr = src + strlen(src); \
531 while (*ptr != '\\' && ptr != src) ptr--; \
533 strcpy(dst, ptr+1); \
536 static int _OpenPlugins(void) {
539 signal(SIGINT, SignalExit);
540 signal(SIGPIPE, SignalExit);
542 GPU_clearDynarec(clearDynarec);
545 if (ret < 0) { SysMessage(_("Error opening CD-ROM plugin!")); return -1; }
547 if (ret < 0) { SysMessage(_("Error opening SPU plugin!")); return -1; }
548 SPU_registerCallback(SPUirq);
549 // pcsx-rearmed: we handle gpu elsewhere
550 //ret = GPU_open(&gpuDisp, "PCSX", NULL);
551 //if (ret < 0) { SysMessage(_("Error opening GPU plugin!")); return -1; }
552 ret = PAD1_open(&gpuDisp);
553 if (ret < 0) { SysMessage(_("Error opening Controller 1 plugin!")); return -1; }
554 ret = PAD2_open(&gpuDisp);
555 if (ret < 0) { SysMessage(_("Error opening Controller 2 plugin!")); return -1; }
557 if (Config.UseNet && !NetOpened) {
559 char path[MAXPATHLEN];
560 char dotdir[MAXPATHLEN];
562 MAKE_PATH(dotdir, "/.pcsx/plugins/", NULL);
564 strcpy(info.EmuName, "PCSX " PACKAGE_VERSION);
565 strncpy(info.CdromID, CdromId, 9);
566 strncpy(info.CdromLabel, CdromLabel, 9);
568 info.GPU_showScreenPic = GPU_showScreenPic;
569 info.GPU_displayText = GPU_displayText;
570 info.GPU_showScreenPic = GPU_showScreenPic;
571 info.PAD_setSensitive = PAD1_setSensitive;
572 sprintf(path, "%s%s", Config.BiosDir, Config.Bios);
573 strcpy(info.BIOSpath, path);
574 strcpy(info.MCD1path, Config.Mcd1);
575 strcpy(info.MCD2path, Config.Mcd2);
576 sprintf(path, "%s%s", dotdir, Config.Gpu);
577 strcpy(info.GPUpath, path);
578 sprintf(path, "%s%s", dotdir, Config.Spu);
579 strcpy(info.SPUpath, path);
580 sprintf(path, "%s%s", dotdir, Config.Cdr);
581 strcpy(info.CDRpath, path);
584 ret = NET_open(&gpuDisp);
587 // -2 is returned when something in the info
588 // changed and needs to be synced
591 PARSEPATH(Config.Bios, info.BIOSpath);
592 PARSEPATH(Config.Gpu, info.GPUpath);
593 PARSEPATH(Config.Spu, info.SPUpath);
594 PARSEPATH(Config.Cdr, info.CDRpath);
596 strcpy(Config.Mcd1, info.MCD1path);
597 strcpy(Config.Mcd2, info.MCD2path);
600 Config.UseNet = FALSE;
603 if (NET_queryPlayer() == 1) {
604 if (SendPcsxInfo() == -1) Config.UseNet = FALSE;
606 if (RecvPcsxInfo() == -1) Config.UseNet = FALSE;
610 } else if (Config.UseNet) {
620 while ((ret = _OpenPlugins()) == -2) {
622 LoadMcds(Config.Mcd1, Config.Mcd2);
623 if (LoadPlugins() == -1) return -1;
628 void ClosePlugins() {
631 signal(SIGINT, SIG_DFL);
632 signal(SIGPIPE, SIG_DFL);
634 if (ret < 0) { SysMessage(_("Error closing CD-ROM plugin!")); return; }
636 if (ret < 0) { SysMessage(_("Error closing SPU plugin!")); return; }
638 if (ret < 0) { SysMessage(_("Error closing Controller 1 Plugin!")); return; }
640 if (ret < 0) { SysMessage(_("Error closing Controller 2 plugin!")); return; }
641 // pcsx-rearmed: we handle gpu elsewhere
643 //if (ret < 0) { SysMessage(_("Error closing GPU plugin!")); return; }
650 /* we hook statically linked plugins here */
651 static const char *builtin_plugins[] = {
652 "builtin_gpu", "builtin_spu", "builtin_cdr", "builtin_pad",
656 static const int builtin_plugin_ids[] = {
657 PLUGIN_GPU, PLUGIN_SPU, PLUGIN_CDR, PLUGIN_PAD,
661 void *SysLoadLibrary(const char *lib) {
662 const char *tmp = strrchr(lib, '/');
666 printf("plugin: %s\n", lib);
670 for (i = 0; i < ARRAY_SIZE(builtin_plugins); i++)
671 if (strcmp(tmp, builtin_plugins[i]) == 0)
672 return (void *)(long)(PLUGIN_DL_BASE + builtin_plugin_ids[i]);
675 #if defined(__x86_64__) || defined(__i386__)
677 char name[MAXPATHLEN];
678 snprintf(name, sizeof(name), "%s.x86", lib);
682 ret = dlopen(lib, RTLD_NOW);
684 fprintf(stderr, "dlopen: %s\n", dlerror());
688 void *SysLoadSym(void *lib, const char *sym) {
689 unsigned int plugid = (unsigned int)(long)lib;
691 if (PLUGIN_DL_BASE <= plugid && plugid < PLUGIN_DL_BASE + ARRAY_SIZE(builtin_plugins))
692 return plugin_link(plugid - PLUGIN_DL_BASE, sym);
694 return dlsym(lib, sym);
697 const char *SysLibError() {
701 void SysCloseLibrary(void *lib) {
702 unsigned int plugid = (unsigned int)(long)lib;
704 if (PLUGIN_DL_BASE <= plugid && plugid < PLUGIN_DL_BASE + ARRAY_SIZE(builtin_plugins))