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"
24 #include "../libpcsxcore/misc.h"
25 #include "../libpcsxcore/new_dynarec/new_dynarec.h"
26 #include "../plugins/cdrcimg/cdrcimg.h"
27 #include "common/plat.h"
28 #include "common/readpng.h"
29 #include "common/input.h"
30 #include "linux/in_evdev.h"
32 // don't include debug.h - it breaks ARM build (R1 redefined)
37 extern int iUseReverb;
38 extern int iUseInterpolation;
40 extern int iSPUIRQWait;
44 unsigned long gpuDisp;
45 char cfgfile_basename[MAXPATHLEN];
47 enum sched_action emu_action, emu_action_old;
51 static void make_path(char *buf, size_t size, const char *dir, const char *fname)
54 snprintf(buf, size, ".%s%s", dir, fname);
56 snprintf(buf, size, ".%s", dir);
58 #define MAKE_PATH(buf, dir, fname) \
59 make_path(buf, sizeof(buf), dir, fname)
61 static void create_profile_dir(const char *directory) {
62 char path[MAXPATHLEN];
64 MAKE_PATH(path, directory, NULL);
65 mkdir(path, S_IRWXU | S_IRWXG);
68 static void CheckSubDir() {
69 // make sure that ~/.pcsx exists
70 create_profile_dir(PCSX_DOT_DIR);
72 create_profile_dir(BIOS_DIR);
73 create_profile_dir(MEMCARD_DIR);
74 create_profile_dir(STATES_DIR);
75 create_profile_dir(PLUGINS_DIR);
76 create_profile_dir(PLUGINS_CFG_DIR);
77 create_profile_dir(CHEATS_DIR);
78 create_profile_dir(PATCHES_DIR);
79 create_profile_dir(PCSX_DOT_DIR "cfg");
80 create_profile_dir("/screenshots/");
83 static int get_gameid_filename(char *buf, int size, const char *fmt, int i) {
87 strncpy(trimlabel, CdromLabel, 32);
89 for (j = 31; j >= 0; j--)
90 if (trimlabel[j] == ' ')
95 snprintf(buf, size, fmt, trimlabel, CdromId, i);
100 void set_cd_image(const char *fname)
102 const char *ext = NULL;
105 ext = strrchr(fname, '.');
108 strcasecmp(ext, ".z") == 0 || strcasecmp(ext, ".bz") == 0 ||
109 strcasecmp(ext, ".znx") == 0 /*|| strcasecmp(ext, ".pbp") == 0*/)) {
111 cdrcimg_set_fname(fname);
112 strcpy(Config.Cdr, "builtin_cdrcimg");
115 strcpy(Config.Cdr, "builtin_cdr");
119 static void set_default_paths(void)
121 MAKE_PATH(Config.Mcd1, MEMCARD_DIR, "card1.mcd");
122 MAKE_PATH(Config.Mcd2, MEMCARD_DIR, "card2.mcd");
123 strcpy(Config.BiosDir, "bios");
125 strcpy(Config.PluginsDir, "plugins");
126 strcpy(Config.Gpu, "builtin_gpu");
127 strcpy(Config.Spu, "builtin_spu");
128 strcpy(Config.Cdr, "builtin_cdr");
129 strcpy(Config.Pad1, "builtin_pad");
130 strcpy(Config.Pad2, "builtin_pad");
131 strcpy(Config.Net, "Disabled");
132 #if defined(__arm__) && !defined(__ARM_ARCH_7A__) /* XXX */
133 strcpy(Config.Gpu, "gpu_unai.so");
136 snprintf(Config.PatchesDir, sizeof(Config.PatchesDir), "." PATCHES_DIR);
139 void emu_set_default_config(void)
141 // try to set sane config on which most games work
142 Config.Xa = Config.Cdda = Config.Sio =
143 Config.SpuIrq = Config.RCntFix = Config.VSyncWA = 0;
144 Config.CdrReschedule = 0;
147 pl_rearmed_cbs.gpu_neon.allow_interlace = 2; // auto
148 pl_rearmed_cbs.gpu_peops.iUseDither = 0;
149 pl_rearmed_cbs.gpu_peops.dwActFixes = 1<<7;
150 pl_rearmed_cbs.gpu_unai.abe_hack =
151 pl_rearmed_cbs.gpu_unai.no_light =
152 pl_rearmed_cbs.gpu_unai.no_blend = 0;
153 memset(&pl_rearmed_cbs.gpu_peopsgl, 0, sizeof(pl_rearmed_cbs.gpu_peopsgl));
154 pl_rearmed_cbs.gpu_peopsgl.iVRamSize = 64;
155 pl_rearmed_cbs.gpu_peopsgl.iTexGarbageCollection = 1;
158 iUseInterpolation = 1;
162 #ifndef __ARM_ARCH_7A__ /* XXX */
164 iUseInterpolation = 0;
166 new_dynarec_hacks = 0;
167 cycle_multiplier = 200;
169 in_type1 = PSE_PAD_TYPE_STANDARD;
170 in_type2 = PSE_PAD_TYPE_STANDARD;
173 static void check_memcards(void)
175 char buf[MAXPATHLEN];
179 for (i = 1; i <= 9; i++) {
180 snprintf(buf, sizeof(buf), ".%scard%d.mcd", MEMCARD_DIR, i);
182 f = fopen(buf, "rb");
184 printf("Creating memcard: %s\n", buf);
192 void do_emu_action(void)
194 char buf[MAXPATHLEN];
197 emu_action_old = emu_action;
199 switch (emu_action) {
200 case SACTION_ENTER_MENU:
203 case SACTION_LOAD_STATE:
204 ret = emu_load_state(state_slot);
205 snprintf(hud_msg, sizeof(hud_msg), ret == 0 ? "LOADED" : "FAIL!");
207 case SACTION_SAVE_STATE:
208 ret = emu_save_state(state_slot);
209 snprintf(hud_msg, sizeof(hud_msg), ret == 0 ? "SAVED" : "FAIL!");
211 case SACTION_NEXT_SSLOT:
216 case SACTION_PREV_SSLOT:
221 case SACTION_TOGGLE_FSKIP:
222 pl_rearmed_cbs.fskip_advice = 0;
223 pl_rearmed_cbs.frameskip++;
224 if (pl_rearmed_cbs.frameskip > 1)
225 pl_rearmed_cbs.frameskip = -1;
226 snprintf(hud_msg, sizeof(hud_msg), "FRAMESKIP: %s",
227 pl_rearmed_cbs.frameskip == -1 ? "AUTO" :
228 pl_rearmed_cbs.frameskip == 0 ? "OFF" : "1" );
229 plugin_call_rearmed_cbs();
231 case SACTION_SCREENSHOT:
235 time_t t = time(NULL);
236 struct tm *tb = localtime(&t);
237 int ti = tb->tm_yday * 1000000 + tb->tm_hour * 10000 +
238 tb->tm_min * 100 + tb->tm_sec;
240 scrbuf = pl_prepare_screenshot(&w, &h, &bpp);
241 get_gameid_filename(buf, sizeof(buf),
242 "screenshots/%.32s-%.9s.%d.png", ti);
244 if (scrbuf != 0 && bpp == 16)
245 ret = writepng(buf, scrbuf, w, h);
247 snprintf(hud_msg, sizeof(hud_msg), "SCREENSHOT TAKEN");
250 case SACTION_VOLUME_UP:
251 case SACTION_VOLUME_DOWN:
252 plat_step_volume(emu_action == SACTION_VOLUME_UP);
254 case SACTION_MINIMIZE:
264 snprintf(hud_msg, sizeof(hud_msg), "STATE SLOT %d [%s]", state_slot,
265 emu_check_state(state_slot) == 0 ? "USED" : "FREE");
267 printf("* %s\n", hud_msg);
270 int main(int argc, char *argv[])
272 // what is the name of the config file?
273 // it may be redefined by -cfg on the command line
274 strcpy(cfgfile_basename, "pcsx.cfg");
279 memset(&Config, 0, sizeof(Config));
283 emu_set_default_config();
285 strcpy(Config.Bios, "HLE");
288 extern int maemo_main(int argc, char **argv);
289 return maemo_main(argc, argv);
291 char file[MAXPATHLEN] = "";
292 char path[MAXPATHLEN];
293 const char *cdfile = NULL;
294 const char *loadst_f = NULL;
299 // read command line options
300 for (i = 1; i < argc; i++) {
301 if (!strcmp(argv[i], "-psxout")) psxout = 1;
302 else if (!strcmp(argv[i], "-load")) loadst = atol(argv[++i]);
303 else if (!strcmp(argv[i], "-cfg")) {
304 if (i+1 >= argc) break;
305 strncpy(cfgfile_basename, argv[++i], MAXPATHLEN-100); /* TODO buffer overruns */
306 printf("Using config file %s.\n", cfgfile_basename);
308 else if (!strcmp(argv[i], "-cdfile")) {
309 char isofilename[MAXPATHLEN];
311 if (i+1 >= argc) break;
312 strncpy(isofilename, argv[++i], MAXPATHLEN);
313 if (isofilename[0] != '/') {
314 getcwd(path, MAXPATHLEN);
315 if (strlen(path) + strlen(isofilename) + 1 < MAXPATHLEN) {
317 strcat(path, isofilename);
318 strcpy(isofilename, path);
323 cdfile = isofilename;
325 else if (!strcmp(argv[i], "-loadf")) {
326 if (i+1 >= argc) break;
327 loadst_f = argv[++i];
329 else if (!strcmp(argv[i], "-h") ||
330 !strcmp(argv[i], "-help") ||
331 !strcmp(argv[i], "--help")) {
332 printf(PACKAGE_NAME " " PACKAGE_VERSION "\n");
334 " pcsx [options] [file]\n"
336 "\t-cdfile FILE\tRuns a CD image file\n"
337 "\t-nogui\t\tDon't open the GTK GUI\n"
338 "\t-cfg FILE\tLoads desired configuration file (default: ~/.pcsx/pcsx.cfg)\n"
339 "\t-psxout\t\tEnable PSX output\n"
340 "\t-load STATENUM\tLoads savestate STATENUM (1-5)\n"
341 "\t-h -help\tDisplay this message\n"
342 "\tfile\t\tLoads file\n"));
345 strncpy(file, argv[i], MAXPATHLEN);
346 if (file[0] != '/') {
347 getcwd(path, MAXPATHLEN);
348 if (strlen(path) + strlen(file) + 1 < MAXPATHLEN) {
359 set_cd_image(cdfile);
365 // init input but leave probing to platform code,
366 // they add input drivers and may need to modify them after probe
370 menu_init(); // loads config
375 if (LoadPlugins() == -1) {
376 // FIXME: this recovery doesn't work, just delete bad config and bail out
377 // SysMessage("could not load plugins, retrying with defaults\n");
379 snprintf(path, sizeof(path), "." PCSX_DOT_DIR "%s", cfgfile_basename);
381 SysMessage("Failed loading plugins!");
386 if (OpenPlugins() == -1) {
389 plugin_call_rearmed_cbs();
394 if (file[0] != '\0') {
395 if (Load(file) != -1)
399 if (LoadCdrom() == -1) {
401 printf(_("Could not load CD-ROM!\n"));
411 // If a state has been specified, then load that
413 int ret = emu_load_state(loadst - 1);
414 printf("%s state %d\n", ret ? "failed to load" : "loaded", loadst);
417 int ret = LoadState(loadst_f);
418 printf("%s state file: %s\n", ret ? "failed to load" : "loaded", loadst_f);
429 emu_action = SACTION_NONE;
432 if (emu_action != SACTION_NONE)
441 if (EmuInit() == -1) {
442 printf("PSX emulator couldn't be initialized.\n");
446 LoadMcds(Config.Mcd1, Config.Mcd2);
456 printf("SysRunGui\n");
460 printf("StartGui\n");
463 static void dummy_lace()
468 // rearmed hack: EmuReset() runs some code when real BIOS is used,
469 // but we usually do reset from menu while GPU is not open yet,
470 // so we need to prevent updateLace() call..
471 void *real_lace = GPU_updateLace;
472 GPU_updateLace = dummy_lace;
474 // reset can run code, timing must be set
475 pl_timing_prepare(Config.PsxType);
479 // hmh core forgets this
482 GPU_updateLace = real_lace;
491 if (emuLog != NULL) fclose(emuLog);
498 printf("OnFile_Exit\n");
507 int get_state_filename(char *buf, int size, int i) {
508 return get_gameid_filename(buf, size,
509 "." STATES_DIR "%.32s-%.9s.%3.3d", i);
512 int emu_check_state(int slot)
514 char fname[MAXPATHLEN];
517 ret = get_state_filename(fname, sizeof(fname), slot);
521 return CheckState(fname);
524 int emu_save_state(int slot)
526 char fname[MAXPATHLEN];
529 ret = get_state_filename(fname, sizeof(fname), slot);
533 ret = SaveState(fname);
534 #ifndef __ARM_ARCH_7A__ /* XXX */
537 printf("* %s \"%s\" [%d]\n", ret == 0 ? "saved" : "failed to save", fname, slot);
541 int emu_load_state(int slot)
543 char fname[MAXPATHLEN];
546 ret = get_state_filename(fname, sizeof(fname), slot);
550 return LoadState(fname);
553 void SysPrintf(const char *fmt, ...) {
558 vsprintf(msg, fmt, list);
561 fprintf(emuLog, "%s", msg);
564 void SysMessage(const char *fmt, ...) {
569 vsprintf(msg, fmt, list);
572 if (msg[strlen(msg) - 1] == '\n')
573 msg[strlen(msg) - 1] = 0;
575 fprintf(stderr, "%s\n", msg);
578 static void SignalExit(int sig) {
583 #define PARSEPATH(dst, src) \
584 ptr = src + strlen(src); \
585 while (*ptr != '\\' && ptr != src) ptr--; \
587 strcpy(dst, ptr+1); \
590 static int _OpenPlugins(void) {
593 signal(SIGINT, SignalExit);
594 signal(SIGPIPE, SignalExit);
596 GPU_clearDynarec(clearDynarec);
599 if (ret < 0) { SysMessage(_("Error opening CD-ROM plugin!")); return -1; }
601 if (ret < 0) { SysMessage(_("Error opening SPU plugin!")); return -1; }
602 SPU_registerCallback(SPUirq);
603 // pcsx-rearmed: we handle gpu elsewhere
604 //ret = GPU_open(&gpuDisp, "PCSX", NULL);
605 //if (ret < 0) { SysMessage(_("Error opening GPU plugin!")); return -1; }
606 ret = PAD1_open(&gpuDisp);
607 if (ret < 0) { SysMessage(_("Error opening Controller 1 plugin!")); return -1; }
608 ret = PAD2_open(&gpuDisp);
609 if (ret < 0) { SysMessage(_("Error opening Controller 2 plugin!")); return -1; }
611 if (Config.UseNet && !NetOpened) {
613 char path[MAXPATHLEN];
614 char dotdir[MAXPATHLEN];
616 MAKE_PATH(dotdir, "/.pcsx/plugins/", NULL);
618 strcpy(info.EmuName, "PCSX " PACKAGE_VERSION);
619 strncpy(info.CdromID, CdromId, 9);
620 strncpy(info.CdromLabel, CdromLabel, 9);
622 info.GPU_showScreenPic = GPU_showScreenPic;
623 info.GPU_displayText = GPU_displayText;
624 info.GPU_showScreenPic = GPU_showScreenPic;
625 info.PAD_setSensitive = PAD1_setSensitive;
626 sprintf(path, "%s%s", Config.BiosDir, Config.Bios);
627 strcpy(info.BIOSpath, path);
628 strcpy(info.MCD1path, Config.Mcd1);
629 strcpy(info.MCD2path, Config.Mcd2);
630 sprintf(path, "%s%s", dotdir, Config.Gpu);
631 strcpy(info.GPUpath, path);
632 sprintf(path, "%s%s", dotdir, Config.Spu);
633 strcpy(info.SPUpath, path);
634 sprintf(path, "%s%s", dotdir, Config.Cdr);
635 strcpy(info.CDRpath, path);
638 ret = NET_open(&gpuDisp);
641 // -2 is returned when something in the info
642 // changed and needs to be synced
645 PARSEPATH(Config.Bios, info.BIOSpath);
646 PARSEPATH(Config.Gpu, info.GPUpath);
647 PARSEPATH(Config.Spu, info.SPUpath);
648 PARSEPATH(Config.Cdr, info.CDRpath);
650 strcpy(Config.Mcd1, info.MCD1path);
651 strcpy(Config.Mcd2, info.MCD2path);
654 Config.UseNet = FALSE;
657 if (NET_queryPlayer() == 1) {
658 if (SendPcsxInfo() == -1) Config.UseNet = FALSE;
660 if (RecvPcsxInfo() == -1) Config.UseNet = FALSE;
664 } else if (Config.UseNet) {
674 while ((ret = _OpenPlugins()) == -2) {
676 LoadMcds(Config.Mcd1, Config.Mcd2);
677 if (LoadPlugins() == -1) return -1;
682 void ClosePlugins() {
685 signal(SIGINT, SIG_DFL);
686 signal(SIGPIPE, SIG_DFL);
688 if (ret < 0) { SysMessage(_("Error closing CD-ROM plugin!")); return; }
690 if (ret < 0) { SysMessage(_("Error closing SPU plugin!")); return; }
692 if (ret < 0) { SysMessage(_("Error closing Controller 1 Plugin!")); return; }
694 if (ret < 0) { SysMessage(_("Error closing Controller 2 plugin!")); return; }
695 // pcsx-rearmed: we handle gpu elsewhere
697 //if (ret < 0) { SysMessage(_("Error closing GPU plugin!")); return; }
704 /* we hook statically linked plugins here */
705 static const char *builtin_plugins[] = {
706 "builtin_gpu", "builtin_spu", "builtin_cdr", "builtin_pad",
710 static const int builtin_plugin_ids[] = {
711 PLUGIN_GPU, PLUGIN_SPU, PLUGIN_CDR, PLUGIN_PAD,
715 void *SysLoadLibrary(const char *lib) {
716 const char *tmp = strrchr(lib, '/');
720 printf("plugin: %s\n", lib);
724 for (i = 0; i < ARRAY_SIZE(builtin_plugins); i++)
725 if (strcmp(tmp, builtin_plugins[i]) == 0)
726 return (void *)(long)(PLUGIN_DL_BASE + builtin_plugin_ids[i]);
729 #if defined(__x86_64__) || defined(__i386__)
731 if (strstr(lib, ".x86") == NULL) {
732 char name[MAXPATHLEN];
733 snprintf(name, sizeof(name), "%s.x86_64", lib);
738 ret = dlopen(lib, RTLD_NOW);
740 fprintf(stderr, "dlopen: %s\n", dlerror());
744 void *SysLoadSym(void *lib, const char *sym) {
745 unsigned int plugid = (unsigned int)(long)lib;
747 if (PLUGIN_DL_BASE <= plugid && plugid < PLUGIN_DL_BASE + ARRAY_SIZE(builtin_plugins))
748 return plugin_link(plugid - PLUGIN_DL_BASE, sym);
750 return dlsym(lib, sym);
753 const char *SysLibError() {
757 void SysCloseLibrary(void *lib) {
758 unsigned int plugid = (unsigned int)(long)lib;
760 if (PLUGIN_DL_BASE <= plugid && plugid < PLUGIN_DL_BASE + ARRAY_SIZE(builtin_plugins))