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>
19 #include "plugin_lib.h"
22 #include "../libpcsxcore/misc.h"
23 #include "../plugins/cdrcimg/cdrcimg.h"
24 #include "common/plat.h"
25 #include "common/input.h"
28 unsigned long gpuDisp;
29 char cfgfile_basename[MAXPATHLEN];
30 static char *(*real_getenv)(const char *name);
32 static void make_path(char *buf, size_t size, const char *dir, const char *fname)
35 snprintf(buf, size, ".%s%s", dir, fname);
37 snprintf(buf, size, ".%s", dir);
39 #define MAKE_PATH(buf, dir, fname) \
40 make_path(buf, sizeof(buf), dir, fname)
42 static void create_profile_dir(const char *directory) {
43 char path[MAXPATHLEN];
45 MAKE_PATH(path, directory, NULL);
46 mkdir(path, S_IRWXU | S_IRWXG);
49 static void CheckSubDir() {
50 // make sure that ~/.pcsx exists
51 create_profile_dir(PCSX_DOT_DIR);
53 create_profile_dir(BIOS_DIR);
54 create_profile_dir(MEMCARD_DIR);
55 create_profile_dir(STATES_DIR);
56 create_profile_dir(PLUGINS_DIR);
57 create_profile_dir(PLUGINS_CFG_DIR);
58 create_profile_dir(CHEATS_DIR);
59 create_profile_dir(PATCHES_DIR);
60 create_profile_dir(PCSX_DOT_DIR "cfg");
63 void set_cd_image(const char *fname)
65 const char *ext = NULL;
68 ext = strrchr(fname, '.');
71 strcasecmp(ext, ".z") == 0 || strcasecmp(ext, ".bz") == 0 ||
72 strcasecmp(ext, ".znx") == 0 || strcasecmp(ext, ".pbp") == 0)) {
74 cdrcimg_set_fname(fname);
75 strcpy(Config.Cdr, "builtin_cdrcimg");
78 strcpy(Config.Cdr, "builtin_cdr");
82 static void set_default_paths(void)
84 MAKE_PATH(Config.Mcd1, MEMCARD_DIR, "card1.mcd");
85 MAKE_PATH(Config.Mcd2, MEMCARD_DIR, "card2.mcd");
86 strcpy(Config.BiosDir, "bios");
88 strcpy(Config.PluginsDir, "plugins");
89 strcpy(Config.Gpu, "builtin_gpu");
90 strcpy(Config.Spu, "builtin_spu");
91 strcpy(Config.Cdr, "builtin_cdr");
92 strcpy(Config.Pad1, "builtin_pad");
93 strcpy(Config.Pad2, "builtin_pad");
94 strcpy(Config.Net, "Disabled");
97 snprintf(Config.PatchesDir, sizeof(Config.PatchesDir), "." PATCHES_DIR);
100 int main(int argc, char *argv[])
104 tmp = dlopen("/lib/libdl.so.2", RTLD_LAZY);
106 tmp = dlopen("/lib32/libdl.so.2", RTLD_LAZY);
108 real_getenv = dlsym(tmp, "getenv");
109 if (real_getenv == NULL) {
110 fprintf(stderr, "%s\n", dlerror());
115 // what is the name of the config file?
116 // it may be redefined by -cfg on the command line
117 strcpy(cfgfile_basename, "pcsx.cfg");
122 memset(&Config, 0, sizeof(Config));
126 strcpy(Config.Bios, "HLE");
129 extern int maemo_main(int argc, char **argv);
130 return maemo_main(argc, argv);
132 char file[MAXPATHLEN] = "";
133 char path[MAXPATHLEN];
134 const char *cdfile = NULL;
138 // read command line options
139 for (i = 1; i < argc; i++) {
140 if (!strcmp(argv[i], "-psxout")) Config.PsxOut = 1;
141 else if (!strcmp(argv[i], "-load")) loadst = atol(argv[++i]);
142 else if (!strcmp(argv[i], "-cfg")) {
143 if (i+1 >= argc) break;
144 strncpy(cfgfile_basename, argv[++i], MAXPATHLEN-100); /* TODO buffer overruns */
145 printf("Using config file %s.\n", cfgfile_basename);
147 else if (!strcmp(argv[i], "-cdfile")) {
148 char isofilename[MAXPATHLEN];
150 if (i+1 >= argc) break;
151 strncpy(isofilename, argv[++i], MAXPATHLEN);
152 if (isofilename[0] != '/') {
153 getcwd(path, MAXPATHLEN);
154 if (strlen(path) + strlen(isofilename) + 1 < MAXPATHLEN) {
156 strcat(path, isofilename);
157 strcpy(isofilename, path);
162 cdfile = isofilename;
164 else if (!strcmp(argv[i], "-h") ||
165 !strcmp(argv[i], "-help") ||
166 !strcmp(argv[i], "--help")) {
167 printf(PACKAGE_NAME " " PACKAGE_VERSION "\n");
169 " pcsx [options] [file]\n"
171 "\t-cdfile FILE\tRuns a CD image file\n"
172 "\t-nogui\t\tDon't open the GTK GUI\n"
173 "\t-cfg FILE\tLoads desired configuration file (default: ~/.pcsx/pcsx.cfg)\n"
174 "\t-psxout\t\tEnable PSX output\n"
175 "\t-load STATENUM\tLoads savestate STATENUM (1-5)\n"
176 "\t-h -help\tDisplay this message\n"
177 "\tfile\t\tLoads file\n"));
180 strncpy(file, argv[i], MAXPATHLEN);
181 if (file[0] != '/') {
182 getcwd(path, MAXPATHLEN);
183 if (strlen(path) + strlen(file) + 1 < MAXPATHLEN) {
194 set_cd_image(cdfile);
205 if (LoadPlugins() == -1) {
206 // FIXME: this recovery doesn't work, just delete bad config and bail out
207 // SysMessage("could not load plugins, retrying with defaults\n");
209 snprintf(path, sizeof(path), "." PCSX_DOT_DIR "%s", cfgfile_basename);
211 SysMessage("Failed loading plugins!");
216 if (OpenPlugins() == -1) {
219 plugin_call_rearmed_cbs();
224 if (file[0] != '\0') {
225 if (Load(file) != -1)
229 if (LoadCdrom() == -1) {
231 printf(_("Could not load CD-ROM!\n"));
238 // If a state has been specified, then load that
240 char state_filename[MAXPATHLEN];
241 int ret = get_state_filename(state_filename, sizeof(state_filename), loadst - 1);
243 ret = LoadState(state_filename);
244 printf("%s state %s\n", ret ? "failed to load" : "loaded", state_filename);
265 if (EmuInit() == -1) {
266 printf("PSX emulator couldn't be initialized.\n");
270 LoadMcds(Config.Mcd1, Config.Mcd2); /* TODO Do we need to have this here, or in the calling main() function?? */
280 printf("SysRunGui\n");
284 printf("StartGui\n");
287 static void dummy_lace()
292 // rearmed hack: EmuReset() runs some code when real BIOS is used,
293 // but we usually do reset from menu while GPU is not open yet,
294 // so we need to prevent updateLace() call..
295 void *real_lace = GPU_updateLace;
296 GPU_updateLace = dummy_lace;
300 // hmh core forgets this
303 GPU_updateLace = real_lace;
312 if (emuLog != NULL) fclose(emuLog);
319 printf("OnFile_Exit\n");
328 int get_state_filename(char *buf, int size, int i) {
332 strncpy(trimlabel, CdromLabel, 32);
334 for (j = 31; j >= 0; j--)
335 if (trimlabel[j] == ' ')
340 snprintf(buf, size, "." STATES_DIR "%.32s-%.9s.%3.3d",
341 trimlabel, CdromId, i);
346 void SysPrintf(const char *fmt, ...) {
351 vsprintf(msg, fmt, list);
354 fprintf(emuLog, "%s", msg);
357 void SysMessage(const char *fmt, ...) {
362 vsprintf(msg, fmt, list);
365 if (msg[strlen(msg) - 1] == '\n')
366 msg[strlen(msg) - 1] = 0;
368 fprintf(stderr, "%s\n", msg);
371 static void SignalExit(int sig) {
376 #define PARSEPATH(dst, src) \
377 ptr = src + strlen(src); \
378 while (*ptr != '\\' && ptr != src) ptr--; \
380 strcpy(dst, ptr+1); \
383 static int _OpenPlugins(void) {
386 signal(SIGINT, SignalExit);
387 signal(SIGPIPE, SignalExit);
389 GPU_clearDynarec(clearDynarec);
392 if (ret < 0) { SysMessage(_("Error opening CD-ROM plugin!")); return -1; }
394 if (ret < 0) { SysMessage(_("Error opening SPU plugin!")); return -1; }
395 SPU_registerCallback(SPUirq);
396 // pcsx-rearmed: we handle gpu elsewhere
397 //ret = GPU_open(&gpuDisp, "PCSX", NULL);
398 //if (ret < 0) { SysMessage(_("Error opening GPU plugin!")); return -1; }
399 ret = PAD1_open(&gpuDisp);
400 if (ret < 0) { SysMessage(_("Error opening Controller 1 plugin!")); return -1; }
401 ret = PAD2_open(&gpuDisp);
402 if (ret < 0) { SysMessage(_("Error opening Controller 2 plugin!")); return -1; }
404 if (Config.UseNet && !NetOpened) {
406 char path[MAXPATHLEN];
407 char dotdir[MAXPATHLEN];
409 MAKE_PATH(dotdir, "/.pcsx/plugins/", NULL);
411 strcpy(info.EmuName, "PCSX " PACKAGE_VERSION);
412 strncpy(info.CdromID, CdromId, 9);
413 strncpy(info.CdromLabel, CdromLabel, 9);
415 info.GPU_showScreenPic = GPU_showScreenPic;
416 info.GPU_displayText = GPU_displayText;
417 info.GPU_showScreenPic = GPU_showScreenPic;
418 info.PAD_setSensitive = PAD1_setSensitive;
419 sprintf(path, "%s%s", Config.BiosDir, Config.Bios);
420 strcpy(info.BIOSpath, path);
421 strcpy(info.MCD1path, Config.Mcd1);
422 strcpy(info.MCD2path, Config.Mcd2);
423 sprintf(path, "%s%s", dotdir, Config.Gpu);
424 strcpy(info.GPUpath, path);
425 sprintf(path, "%s%s", dotdir, Config.Spu);
426 strcpy(info.SPUpath, path);
427 sprintf(path, "%s%s", dotdir, Config.Cdr);
428 strcpy(info.CDRpath, path);
431 ret = NET_open(&gpuDisp);
434 // -2 is returned when something in the info
435 // changed and needs to be synced
438 PARSEPATH(Config.Bios, info.BIOSpath);
439 PARSEPATH(Config.Gpu, info.GPUpath);
440 PARSEPATH(Config.Spu, info.SPUpath);
441 PARSEPATH(Config.Cdr, info.CDRpath);
443 strcpy(Config.Mcd1, info.MCD1path);
444 strcpy(Config.Mcd2, info.MCD2path);
447 Config.UseNet = FALSE;
450 if (NET_queryPlayer() == 1) {
451 if (SendPcsxInfo() == -1) Config.UseNet = FALSE;
453 if (RecvPcsxInfo() == -1) Config.UseNet = FALSE;
457 } else if (Config.UseNet) {
467 while ((ret = _OpenPlugins()) == -2) {
469 LoadMcds(Config.Mcd1, Config.Mcd2);
470 if (LoadPlugins() == -1) return -1;
475 void ClosePlugins() {
478 signal(SIGINT, SIG_DFL);
479 signal(SIGPIPE, SIG_DFL);
481 if (ret < 0) { SysMessage(_("Error closing CD-ROM plugin!")); return; }
483 if (ret < 0) { SysMessage(_("Error closing SPU plugin!")); return; }
485 if (ret < 0) { SysMessage(_("Error closing Controller 1 Plugin!")); return; }
487 if (ret < 0) { SysMessage(_("Error closing Controller 2 plugin!")); return; }
488 // pcsx-rearmed: we handle gpu elsewhere
490 //if (ret < 0) { SysMessage(_("Error closing GPU plugin!")); return; }
498 /* this is to avoid having to hack every plugin to stop using $HOME */
499 char *getenv(const char *name)
501 static char ret[8] = ".";
503 if (name && strcmp(name, "HOME") == 0 &&
504 ((int)name >> 28) == 0) // HACK: let libs find home
507 return real_getenv(name);
511 /* we hook statically linked plugins here */
512 static const char *builtin_plugins[] = {
513 "builtin_gpu", "builtin_spu", "builtin_cdr", "builtin_pad",
517 static const int builtin_plugin_ids[] = {
518 PLUGIN_GPU, PLUGIN_SPU, PLUGIN_CDR, PLUGIN_PAD,
522 void *SysLoadLibrary(const char *lib) {
523 const char *tmp = strrchr(lib, '/');
527 printf("plugin: %s\n", lib);
531 for (i = 0; i < ARRAY_SIZE(builtin_plugins); i++)
532 if (strcmp(tmp, builtin_plugins[i]) == 0)
533 return (void *)(long)(PLUGIN_DL_BASE + builtin_plugin_ids[i]);
536 #if defined(__x86_64__) || defined(__i386__)
538 char name[MAXPATHLEN];
539 snprintf(name, sizeof(name), "%s.x86", lib);
543 ret = dlopen(lib, RTLD_NOW);
545 fprintf(stderr, "dlopen: %s\n", dlerror());
549 void *SysLoadSym(void *lib, const char *sym) {
550 unsigned int plugid = (unsigned int)(long)lib;
552 if (PLUGIN_DL_BASE <= plugid && plugid < PLUGIN_DL_BASE + ARRAY_SIZE(builtin_plugins))
553 return plugin_link(plugid - PLUGIN_DL_BASE, sym);
555 return dlsym(lib, sym);
558 const char *SysLibError() {
562 void SysCloseLibrary(void *lib) {
563 unsigned int plugid = (unsigned int)(long)lib;
565 if (PLUGIN_DL_BASE <= plugid && plugid < PLUGIN_DL_BASE + ARRAY_SIZE(builtin_plugins))