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 int len = strlen(fname);
71 ext = fname + len - 2;
74 if (ext && strcasecmp(ext, ".z") == 0) {
76 cdrcimg_set_fname(fname);
77 strcpy(Config.Cdr, "builtin_cdrcimg");
80 strcpy(Config.Cdr, "builtin_cdr");
84 static void set_default_paths(void)
86 MAKE_PATH(Config.Mcd1, MEMCARD_DIR, "card1.mcd");
87 MAKE_PATH(Config.Mcd2, MEMCARD_DIR, "card2.mcd");
88 strcpy(Config.BiosDir, "bios");
90 strcpy(Config.PluginsDir, "plugins");
91 strcpy(Config.Gpu, "builtin_gpu");
92 strcpy(Config.Spu, "builtin_spu");
93 strcpy(Config.Cdr, "builtin_cdr");
94 strcpy(Config.Pad1, "builtin_pad");
95 strcpy(Config.Pad2, "builtin_pad");
96 strcpy(Config.Net, "Disabled");
99 snprintf(Config.PatchesDir, sizeof(Config.PatchesDir), "." PATCHES_DIR);
102 int main(int argc, char *argv[])
104 char file[MAXPATHLEN] = "";
105 char path[MAXPATHLEN];
106 const char *cdfile = NULL;
111 tmp = dlopen("/lib/libdl.so.2", RTLD_LAZY);
113 tmp = dlopen("/lib32/libdl.so.2", RTLD_LAZY);
115 real_getenv = dlsym(tmp, "getenv");
116 if (real_getenv == NULL) {
117 fprintf(stderr, "%s\n", dlerror());
122 // what is the name of the config file?
123 // it may be redefined by -cfg on the command line
124 strcpy(cfgfile_basename, "pcsx.cfg");
129 // read command line options
130 for (i = 1; i < argc; i++) {
131 if (!strcmp(argv[i], "-psxout")) Config.PsxOut = 1;
132 else if (!strcmp(argv[i], "-load")) loadst = atol(argv[++i]);
133 else if (!strcmp(argv[i], "-cfg")) {
134 if (i+1 >= argc) break;
135 strncpy(cfgfile_basename, argv[++i], MAXPATHLEN-100); /* TODO buffer overruns */
136 printf("Using config file %s.\n", cfgfile_basename);
138 else if (!strcmp(argv[i], "-cdfile")) {
139 char isofilename[MAXPATHLEN];
141 if (i+1 >= argc) break;
142 strncpy(isofilename, argv[++i], MAXPATHLEN);
143 if (isofilename[0] != '/') {
144 getcwd(path, MAXPATHLEN);
145 if (strlen(path) + strlen(isofilename) + 1 < MAXPATHLEN) {
147 strcat(path, isofilename);
148 strcpy(isofilename, path);
153 cdfile = isofilename;
155 else if (!strcmp(argv[i], "-h") ||
156 !strcmp(argv[i], "-help") ||
157 !strcmp(argv[i], "--help")) {
158 printf(PACKAGE_NAME " " PACKAGE_VERSION "\n");
160 " pcsx [options] [file]\n"
162 "\t-cdfile FILE\tRuns a CD image file\n"
163 "\t-nogui\t\tDon't open the GTK GUI\n"
164 "\t-cfg FILE\tLoads desired configuration file (default: ~/.pcsx/pcsx.cfg)\n"
165 "\t-psxout\t\tEnable PSX output\n"
166 "\t-load STATENUM\tLoads savestate STATENUM (1-5)\n"
167 "\t-h -help\tDisplay this message\n"
168 "\tfile\t\tLoads file\n"));
171 strncpy(file, argv[i], MAXPATHLEN);
172 if (file[0] != '/') {
173 getcwd(path, MAXPATHLEN);
174 if (strlen(path) + strlen(file) + 1 < MAXPATHLEN) {
184 memset(&Config, 0, sizeof(PcsxConfig));
188 strcpy(Config.Bios, "HLE");
191 set_cd_image(cdfile);
202 if (LoadPlugins() == -1) {
203 // FIXME: this recovery doesn't work, just delete bad config and bail out
204 // SysMessage("could not load plugins, retrying with defaults\n");
206 snprintf(path, sizeof(path), "." PCSX_DOT_DIR "%s", cfgfile_basename);
208 SysMessage("Failed loading plugins!");
213 if (OpenPlugins() == -1) {
216 plugin_call_rearmed_cbs();
221 if (file[0] != '\0') {
222 if (Load(file) != -1)
226 if (LoadCdrom() == -1) {
228 printf(_("Could not load CD-ROM!\n"));
235 // If a state has been specified, then load that
237 char state_filename[MAXPATHLEN];
238 int ret = get_state_filename(state_filename, sizeof(state_filename), loadst - 1);
240 ret = LoadState(state_filename);
241 printf("%s state %s\n", ret ? "failed to load" : "loaded", state_filename);
261 if (EmuInit() == -1) {
262 printf("PSX emulator couldn't be initialized.\n");
266 LoadMcds(Config.Mcd1, Config.Mcd2); /* TODO Do we need to have this here, or in the calling main() function?? */
276 printf("SysRunGui\n");
280 printf("StartGui\n");
283 static void dummy_lace()
288 // rearmed hack: EmuReset() runs some code when real BIOS is used,
289 // but we usually do reset from menu while GPU is not open yet,
290 // so we need to prevent updateLace() call..
291 void *real_lace = GPU_updateLace;
292 GPU_updateLace = dummy_lace;
296 // hmh core forgets this
299 GPU_updateLace = real_lace;
308 if (emuLog != NULL) fclose(emuLog);
315 printf("OnFile_Exit\n");
322 int get_state_filename(char *buf, int size, int i) {
326 strncpy(trimlabel, CdromLabel, 32);
328 for (j = 31; j >= 0; j--)
329 if (trimlabel[j] == ' ')
334 snprintf(buf, size, "." STATES_DIR "%.32s-%.9s.%3.3d",
335 trimlabel, CdromId, i);
340 void SysPrintf(const char *fmt, ...) {
345 vsprintf(msg, fmt, list);
348 fprintf(emuLog, "%s", msg);
351 void SysMessage(const char *fmt, ...) {
356 vsprintf(msg, fmt, list);
359 if (msg[strlen(msg) - 1] == '\n')
360 msg[strlen(msg) - 1] = 0;
362 fprintf(stderr, "%s\n", msg);
365 static void SignalExit(int sig) {
370 #define PARSEPATH(dst, src) \
371 ptr = src + strlen(src); \
372 while (*ptr != '\\' && ptr != src) ptr--; \
374 strcpy(dst, ptr+1); \
377 static int _OpenPlugins(void) {
380 signal(SIGINT, SignalExit);
381 signal(SIGPIPE, SignalExit);
383 GPU_clearDynarec(clearDynarec);
386 if (ret < 0) { SysMessage(_("Error opening CD-ROM plugin!")); return -1; }
388 if (ret < 0) { SysMessage(_("Error opening SPU plugin!")); return -1; }
389 SPU_registerCallback(SPUirq);
390 // pcsx-rearmed: we handle gpu elsewhere
391 //ret = GPU_open(&gpuDisp, "PCSX", NULL);
392 //if (ret < 0) { SysMessage(_("Error opening GPU plugin!")); return -1; }
393 ret = PAD1_open(&gpuDisp);
394 if (ret < 0) { SysMessage(_("Error opening Controller 1 plugin!")); return -1; }
395 ret = PAD2_open(&gpuDisp);
396 if (ret < 0) { SysMessage(_("Error opening Controller 2 plugin!")); return -1; }
398 if (Config.UseNet && !NetOpened) {
400 char path[MAXPATHLEN];
401 char dotdir[MAXPATHLEN];
403 MAKE_PATH(dotdir, "/.pcsx/plugins/", NULL);
405 strcpy(info.EmuName, "PCSX " PACKAGE_VERSION);
406 strncpy(info.CdromID, CdromId, 9);
407 strncpy(info.CdromLabel, CdromLabel, 9);
409 info.GPU_showScreenPic = GPU_showScreenPic;
410 info.GPU_displayText = GPU_displayText;
411 info.GPU_showScreenPic = GPU_showScreenPic;
412 info.PAD_setSensitive = PAD1_setSensitive;
413 sprintf(path, "%s%s", Config.BiosDir, Config.Bios);
414 strcpy(info.BIOSpath, path);
415 strcpy(info.MCD1path, Config.Mcd1);
416 strcpy(info.MCD2path, Config.Mcd2);
417 sprintf(path, "%s%s", dotdir, Config.Gpu);
418 strcpy(info.GPUpath, path);
419 sprintf(path, "%s%s", dotdir, Config.Spu);
420 strcpy(info.SPUpath, path);
421 sprintf(path, "%s%s", dotdir, Config.Cdr);
422 strcpy(info.CDRpath, path);
425 ret = NET_open(&gpuDisp);
428 // -2 is returned when something in the info
429 // changed and needs to be synced
432 PARSEPATH(Config.Bios, info.BIOSpath);
433 PARSEPATH(Config.Gpu, info.GPUpath);
434 PARSEPATH(Config.Spu, info.SPUpath);
435 PARSEPATH(Config.Cdr, info.CDRpath);
437 strcpy(Config.Mcd1, info.MCD1path);
438 strcpy(Config.Mcd2, info.MCD2path);
441 Config.UseNet = FALSE;
444 if (NET_queryPlayer() == 1) {
445 if (SendPcsxInfo() == -1) Config.UseNet = FALSE;
447 if (RecvPcsxInfo() == -1) Config.UseNet = FALSE;
451 } else if (Config.UseNet) {
461 while ((ret = _OpenPlugins()) == -2) {
463 LoadMcds(Config.Mcd1, Config.Mcd2);
464 if (LoadPlugins() == -1) return -1;
469 void ClosePlugins() {
472 signal(SIGINT, SIG_DFL);
473 signal(SIGPIPE, SIG_DFL);
475 if (ret < 0) { SysMessage(_("Error closing CD-ROM plugin!")); return; }
477 if (ret < 0) { SysMessage(_("Error closing SPU plugin!")); return; }
479 if (ret < 0) { SysMessage(_("Error closing Controller 1 Plugin!")); return; }
481 if (ret < 0) { SysMessage(_("Error closing Controller 2 plugin!")); return; }
482 // pcsx-rearmed: we handle gpu elsewhere
484 //if (ret < 0) { SysMessage(_("Error closing GPU plugin!")); return; }
492 /* this is to avoid having to hack every plugin to stop using $HOME */
493 char *getenv(const char *name)
495 static char ret[8] = ".";
497 if (name && strcmp(name, "HOME") == 0 &&
498 ((int)name >> 28) == 0) // HACK: let libs find home
501 return real_getenv(name);
505 /* we hook statically linked plugins here */
506 static const char *builtin_plugins[] = {
507 "builtin_gpu", "builtin_spu", "builtin_cdr", "builtin_pad",
511 static const int builtin_plugin_ids[] = {
512 PLUGIN_GPU, PLUGIN_SPU, PLUGIN_CDR, PLUGIN_PAD,
516 void *SysLoadLibrary(const char *lib) {
517 const char *tmp = strrchr(lib, '/');
521 printf("plugin: %s\n", lib);
525 for (i = 0; i < ARRAY_SIZE(builtin_plugins); i++)
526 if (strcmp(tmp, builtin_plugins[i]) == 0)
527 return (void *)(long)(PLUGIN_DL_BASE + builtin_plugin_ids[i]);
530 #if defined(__x86_64__) || defined(__i386__)
532 char name[MAXPATHLEN];
533 snprintf(name, sizeof(name), "%s.x86", lib);
537 ret = dlopen(lib, RTLD_NOW);
539 fprintf(stderr, "dlopen: %s\n", dlerror());
543 void *SysLoadSym(void *lib, const char *sym) {
544 unsigned int plugid = (unsigned int)(long)lib;
546 if (PLUGIN_DL_BASE <= plugid && plugid < PLUGIN_DL_BASE + ARRAY_SIZE(builtin_plugins))
547 return plugin_link(plugid - PLUGIN_DL_BASE, sym);
549 return dlsym(lib, sym);
552 const char *SysLibError() {
556 void SysCloseLibrary(void *lib) {
557 unsigned int plugid = (unsigned int)(long)lib;
559 if (PLUGIN_DL_BASE <= plugid && plugid < PLUGIN_DL_BASE + ARRAY_SIZE(builtin_plugins))