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 unsigned long gpuDisp;
38 char cfgfile_basename[MAXPATHLEN];
40 enum sched_action emu_action, emu_action_old;
44 static void make_path(char *buf, size_t size, const char *dir, const char *fname)
47 snprintf(buf, size, ".%s%s", dir, fname);
49 snprintf(buf, size, ".%s", dir);
51 #define MAKE_PATH(buf, dir, fname) \
52 make_path(buf, sizeof(buf), dir, fname)
54 static void create_profile_dir(const char *directory) {
55 char path[MAXPATHLEN];
57 MAKE_PATH(path, directory, NULL);
58 mkdir(path, S_IRWXU | S_IRWXG);
61 static void CheckSubDir() {
62 // make sure that ~/.pcsx exists
63 create_profile_dir(PCSX_DOT_DIR);
65 create_profile_dir(BIOS_DIR);
66 create_profile_dir(MEMCARD_DIR);
67 create_profile_dir(STATES_DIR);
68 create_profile_dir(PLUGINS_DIR);
69 create_profile_dir(PLUGINS_CFG_DIR);
70 create_profile_dir(CHEATS_DIR);
71 create_profile_dir(PATCHES_DIR);
72 create_profile_dir(PCSX_DOT_DIR "cfg");
73 create_profile_dir("/screenshots/");
76 static int get_gameid_filename(char *buf, int size, const char *fmt, int i) {
80 strncpy(trimlabel, CdromLabel, 32);
82 for (j = 31; j >= 0; j--)
83 if (trimlabel[j] == ' ')
88 snprintf(buf, size, fmt, trimlabel, CdromId, i);
93 void set_cd_image(const char *fname)
95 const char *ext = NULL;
98 ext = strrchr(fname, '.');
101 strcasecmp(ext, ".z") == 0 || strcasecmp(ext, ".bz") == 0 ||
102 strcasecmp(ext, ".znx") == 0 /*|| strcasecmp(ext, ".pbp") == 0*/)) {
104 cdrcimg_set_fname(fname);
105 strcpy(Config.Cdr, "builtin_cdrcimg");
108 strcpy(Config.Cdr, "builtin_cdr");
112 static void set_default_paths(void)
114 MAKE_PATH(Config.Mcd1, MEMCARD_DIR, "card1.mcd");
115 MAKE_PATH(Config.Mcd2, MEMCARD_DIR, "card2.mcd");
116 strcpy(Config.BiosDir, "bios");
118 strcpy(Config.PluginsDir, "plugins");
119 strcpy(Config.Gpu, "builtin_gpu");
120 strcpy(Config.Spu, "builtin_spu");
121 strcpy(Config.Cdr, "builtin_cdr");
122 strcpy(Config.Pad1, "builtin_pad");
123 strcpy(Config.Pad2, "builtin_pad");
124 strcpy(Config.Net, "Disabled");
125 #if defined(__arm__) && !defined(__ARM_ARCH_7A__) /* XXX */
126 strcpy(Config.Gpu, "gpuPCSX4ALL.so");
130 snprintf(Config.PatchesDir, sizeof(Config.PatchesDir), "." PATCHES_DIR);
133 static void check_memcards(void)
135 char buf[MAXPATHLEN];
139 for (i = 1; i <= 9; i++) {
140 snprintf(buf, sizeof(buf), ".%scard%d.mcd", MEMCARD_DIR, i);
142 f = fopen(buf, "rb");
144 printf("Creating memcard: %s\n", buf);
152 void do_emu_action(void)
154 char buf[MAXPATHLEN];
157 emu_action_old = emu_action;
159 switch (emu_action) {
160 case SACTION_ENTER_MENU:
163 case SACTION_LOAD_STATE:
164 ret = emu_load_state(state_slot);
165 snprintf(hud_msg, sizeof(hud_msg), ret == 0 ? "LOADED" : "FAIL!");
167 case SACTION_SAVE_STATE:
168 ret = emu_save_state(state_slot);
169 snprintf(hud_msg, sizeof(hud_msg), ret == 0 ? "SAVED" : "FAIL!");
171 case SACTION_NEXT_SSLOT:
176 case SACTION_PREV_SSLOT:
181 case SACTION_TOGGLE_FSKIP:
182 pl_rearmed_cbs.fskip_advice = 0;
183 pl_rearmed_cbs.frameskip++;
184 if (pl_rearmed_cbs.frameskip > 1)
185 pl_rearmed_cbs.frameskip = -1;
186 snprintf(hud_msg, sizeof(hud_msg), "FRAMESKIP: %s",
187 pl_rearmed_cbs.frameskip == -1 ? "AUTO" :
188 pl_rearmed_cbs.frameskip == 0 ? "OFF" : "1" );
189 plugin_call_rearmed_cbs();
191 case SACTION_SCREENSHOT:
195 time_t t = time(NULL);
196 struct tm *tb = localtime(&t);
197 int ti = tb->tm_yday * 1000000 + tb->tm_hour * 10000 +
198 tb->tm_min * 100 + tb->tm_sec;
200 scrbuf = pl_prepare_screenshot(&w, &h, &bpp);
201 get_gameid_filename(buf, sizeof(buf),
202 "screenshots/%.32s-%.9s.%d.png", ti);
204 if (scrbuf != 0 && bpp == 16)
205 ret = writepng(buf, scrbuf, w, h);
207 snprintf(hud_msg, sizeof(hud_msg), "SCREENSHOT TAKEN");
217 snprintf(hud_msg, sizeof(hud_msg), "STATE SLOT %d [%s]", state_slot,
218 emu_check_state(state_slot) == 0 ? "USED" : "FREE");
220 printf("* %s\n", hud_msg);
223 int main(int argc, char *argv[])
225 // what is the name of the config file?
226 // it may be redefined by -cfg on the command line
227 strcpy(cfgfile_basename, "pcsx.cfg");
232 memset(&Config, 0, sizeof(Config));
237 strcpy(Config.Bios, "HLE");
240 extern int maemo_main(int argc, char **argv);
241 return maemo_main(argc, argv);
243 char file[MAXPATHLEN] = "";
244 char path[MAXPATHLEN];
245 const char *cdfile = NULL;
246 const char *loadst_f = NULL;
251 // read command line options
252 for (i = 1; i < argc; i++) {
253 if (!strcmp(argv[i], "-psxout")) psxout = 1;
254 else if (!strcmp(argv[i], "-load")) loadst = atol(argv[++i]);
255 else if (!strcmp(argv[i], "-cfg")) {
256 if (i+1 >= argc) break;
257 strncpy(cfgfile_basename, argv[++i], MAXPATHLEN-100); /* TODO buffer overruns */
258 printf("Using config file %s.\n", cfgfile_basename);
260 else if (!strcmp(argv[i], "-cdfile")) {
261 char isofilename[MAXPATHLEN];
263 if (i+1 >= argc) break;
264 strncpy(isofilename, argv[++i], MAXPATHLEN);
265 if (isofilename[0] != '/') {
266 getcwd(path, MAXPATHLEN);
267 if (strlen(path) + strlen(isofilename) + 1 < MAXPATHLEN) {
269 strcat(path, isofilename);
270 strcpy(isofilename, path);
275 cdfile = isofilename;
277 else if (!strcmp(argv[i], "-loadf")) {
278 if (i+1 >= argc) break;
279 loadst_f = argv[++i];
281 else if (!strcmp(argv[i], "-h") ||
282 !strcmp(argv[i], "-help") ||
283 !strcmp(argv[i], "--help")) {
284 printf(PACKAGE_NAME " " PACKAGE_VERSION "\n");
286 " pcsx [options] [file]\n"
288 "\t-cdfile FILE\tRuns a CD image file\n"
289 "\t-nogui\t\tDon't open the GTK GUI\n"
290 "\t-cfg FILE\tLoads desired configuration file (default: ~/.pcsx/pcsx.cfg)\n"
291 "\t-psxout\t\tEnable PSX output\n"
292 "\t-load STATENUM\tLoads savestate STATENUM (1-5)\n"
293 "\t-h -help\tDisplay this message\n"
294 "\tfile\t\tLoads file\n"));
297 strncpy(file, argv[i], MAXPATHLEN);
298 if (file[0] != '/') {
299 getcwd(path, MAXPATHLEN);
300 if (strlen(path) + strlen(file) + 1 < MAXPATHLEN) {
311 set_cd_image(cdfile);
321 menu_init(); // loads config
323 plat_rescan_inputs();
328 if (LoadPlugins() == -1) {
329 // FIXME: this recovery doesn't work, just delete bad config and bail out
330 // SysMessage("could not load plugins, retrying with defaults\n");
332 snprintf(path, sizeof(path), "." PCSX_DOT_DIR "%s", cfgfile_basename);
334 SysMessage("Failed loading plugins!");
339 if (OpenPlugins() == -1) {
342 plugin_call_rearmed_cbs();
347 if (file[0] != '\0') {
348 if (Load(file) != -1)
352 if (LoadCdrom() == -1) {
354 printf(_("Could not load CD-ROM!\n"));
364 // If a state has been specified, then load that
366 int ret = emu_load_state(loadst - 1);
367 printf("%s state %d\n", ret ? "failed to load" : "loaded", loadst);
370 int ret = LoadState(loadst_f);
371 printf("%s state file: %s\n", ret ? "failed to load" : "loaded", loadst_f);
382 emu_action = SACTION_NONE;
385 if (emu_action != SACTION_NONE)
394 if (EmuInit() == -1) {
395 printf("PSX emulator couldn't be initialized.\n");
399 LoadMcds(Config.Mcd1, Config.Mcd2);
409 printf("SysRunGui\n");
413 printf("StartGui\n");
416 static void dummy_lace()
421 // rearmed hack: EmuReset() runs some code when real BIOS is used,
422 // but we usually do reset from menu while GPU is not open yet,
423 // so we need to prevent updateLace() call..
424 void *real_lace = GPU_updateLace;
425 GPU_updateLace = dummy_lace;
429 // hmh core forgets this
432 GPU_updateLace = real_lace;
441 if (emuLog != NULL) fclose(emuLog);
448 printf("OnFile_Exit\n");
457 int get_state_filename(char *buf, int size, int i) {
458 return get_gameid_filename(buf, size,
459 "." STATES_DIR "%.32s-%.9s.%3.3d", i);
462 int emu_check_state(int slot)
464 char fname[MAXPATHLEN];
467 ret = get_state_filename(fname, sizeof(fname), slot);
471 return CheckState(fname);
474 int emu_save_state(int slot)
476 char fname[MAXPATHLEN];
479 ret = get_state_filename(fname, sizeof(fname), slot);
483 ret = SaveState(fname);
484 printf("* %s \"%s\" [%d]\n", ret == 0 ? "saved" : "failed to save", fname, slot);
488 int emu_load_state(int slot)
490 char fname[MAXPATHLEN];
493 ret = get_state_filename(fname, sizeof(fname), slot);
497 return LoadState(fname);
500 void SysPrintf(const char *fmt, ...) {
505 vsprintf(msg, fmt, list);
508 fprintf(emuLog, "%s", msg);
511 void SysMessage(const char *fmt, ...) {
516 vsprintf(msg, fmt, list);
519 if (msg[strlen(msg) - 1] == '\n')
520 msg[strlen(msg) - 1] = 0;
522 fprintf(stderr, "%s\n", msg);
525 static void SignalExit(int sig) {
530 #define PARSEPATH(dst, src) \
531 ptr = src + strlen(src); \
532 while (*ptr != '\\' && ptr != src) ptr--; \
534 strcpy(dst, ptr+1); \
537 static int _OpenPlugins(void) {
540 signal(SIGINT, SignalExit);
541 signal(SIGPIPE, SignalExit);
543 GPU_clearDynarec(clearDynarec);
546 if (ret < 0) { SysMessage(_("Error opening CD-ROM plugin!")); return -1; }
548 if (ret < 0) { SysMessage(_("Error opening SPU plugin!")); return -1; }
549 SPU_registerCallback(SPUirq);
550 // pcsx-rearmed: we handle gpu elsewhere
551 //ret = GPU_open(&gpuDisp, "PCSX", NULL);
552 //if (ret < 0) { SysMessage(_("Error opening GPU plugin!")); return -1; }
553 ret = PAD1_open(&gpuDisp);
554 if (ret < 0) { SysMessage(_("Error opening Controller 1 plugin!")); return -1; }
555 ret = PAD2_open(&gpuDisp);
556 if (ret < 0) { SysMessage(_("Error opening Controller 2 plugin!")); return -1; }
558 if (Config.UseNet && !NetOpened) {
560 char path[MAXPATHLEN];
561 char dotdir[MAXPATHLEN];
563 MAKE_PATH(dotdir, "/.pcsx/plugins/", NULL);
565 strcpy(info.EmuName, "PCSX " PACKAGE_VERSION);
566 strncpy(info.CdromID, CdromId, 9);
567 strncpy(info.CdromLabel, CdromLabel, 9);
569 info.GPU_showScreenPic = GPU_showScreenPic;
570 info.GPU_displayText = GPU_displayText;
571 info.GPU_showScreenPic = GPU_showScreenPic;
572 info.PAD_setSensitive = PAD1_setSensitive;
573 sprintf(path, "%s%s", Config.BiosDir, Config.Bios);
574 strcpy(info.BIOSpath, path);
575 strcpy(info.MCD1path, Config.Mcd1);
576 strcpy(info.MCD2path, Config.Mcd2);
577 sprintf(path, "%s%s", dotdir, Config.Gpu);
578 strcpy(info.GPUpath, path);
579 sprintf(path, "%s%s", dotdir, Config.Spu);
580 strcpy(info.SPUpath, path);
581 sprintf(path, "%s%s", dotdir, Config.Cdr);
582 strcpy(info.CDRpath, path);
585 ret = NET_open(&gpuDisp);
588 // -2 is returned when something in the info
589 // changed and needs to be synced
592 PARSEPATH(Config.Bios, info.BIOSpath);
593 PARSEPATH(Config.Gpu, info.GPUpath);
594 PARSEPATH(Config.Spu, info.SPUpath);
595 PARSEPATH(Config.Cdr, info.CDRpath);
597 strcpy(Config.Mcd1, info.MCD1path);
598 strcpy(Config.Mcd2, info.MCD2path);
601 Config.UseNet = FALSE;
604 if (NET_queryPlayer() == 1) {
605 if (SendPcsxInfo() == -1) Config.UseNet = FALSE;
607 if (RecvPcsxInfo() == -1) Config.UseNet = FALSE;
611 } else if (Config.UseNet) {
621 while ((ret = _OpenPlugins()) == -2) {
623 LoadMcds(Config.Mcd1, Config.Mcd2);
624 if (LoadPlugins() == -1) return -1;
629 void ClosePlugins() {
632 signal(SIGINT, SIG_DFL);
633 signal(SIGPIPE, SIG_DFL);
635 if (ret < 0) { SysMessage(_("Error closing CD-ROM plugin!")); return; }
637 if (ret < 0) { SysMessage(_("Error closing SPU plugin!")); return; }
639 if (ret < 0) { SysMessage(_("Error closing Controller 1 Plugin!")); return; }
641 if (ret < 0) { SysMessage(_("Error closing Controller 2 plugin!")); return; }
642 // pcsx-rearmed: we handle gpu elsewhere
644 //if (ret < 0) { SysMessage(_("Error closing GPU plugin!")); return; }
651 /* we hook statically linked plugins here */
652 static const char *builtin_plugins[] = {
653 "builtin_gpu", "builtin_spu", "builtin_cdr", "builtin_pad",
657 static const int builtin_plugin_ids[] = {
658 PLUGIN_GPU, PLUGIN_SPU, PLUGIN_CDR, PLUGIN_PAD,
662 void *SysLoadLibrary(const char *lib) {
663 const char *tmp = strrchr(lib, '/');
667 printf("plugin: %s\n", lib);
671 for (i = 0; i < ARRAY_SIZE(builtin_plugins); i++)
672 if (strcmp(tmp, builtin_plugins[i]) == 0)
673 return (void *)(long)(PLUGIN_DL_BASE + builtin_plugin_ids[i]);
676 #if defined(__x86_64__) || defined(__i386__)
678 char name[MAXPATHLEN];
679 snprintf(name, sizeof(name), "%s.x86", lib);
683 ret = dlopen(lib, RTLD_NOW);
685 fprintf(stderr, "dlopen: %s\n", dlerror());
689 void *SysLoadSym(void *lib, const char *sym) {
690 unsigned int plugid = (unsigned int)(long)lib;
692 if (PLUGIN_DL_BASE <= plugid && plugid < PLUGIN_DL_BASE + ARRAY_SIZE(builtin_plugins))
693 return plugin_link(plugid - PLUGIN_DL_BASE, sym);
695 return dlsym(lib, sym);
698 const char *SysLibError() {
702 void SysCloseLibrary(void *lib) {
703 unsigned int plugid = (unsigned int)(long)lib;
705 if (PLUGIN_DL_BASE <= plugid && plugid < PLUGIN_DL_BASE + ARRAY_SIZE(builtin_plugins))