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 int main(int argc, char *argv[])
86 char file[MAXPATHLEN] = "";
87 char path[MAXPATHLEN];
88 const char *cdfile = NULL;
93 tmp = dlopen("/lib/libdl.so.2", RTLD_LAZY);
95 tmp = dlopen("/lib32/libdl.so.2", RTLD_LAZY);
97 real_getenv = dlsym(tmp, "getenv");
98 if (real_getenv == NULL) {
99 fprintf(stderr, "%s\n", dlerror());
104 // what is the name of the config file?
105 // it may be redefined by -cfg on the command line
106 strcpy(cfgfile_basename, "pcsx.cfg");
111 // read command line options
112 for (i = 1; i < argc; i++) {
113 if (!strcmp(argv[i], "-psxout")) Config.PsxOut = 1;
114 else if (!strcmp(argv[i], "-load")) loadst = atol(argv[++i]);
115 else if (!strcmp(argv[i], "-cfg")) {
116 if (i+1 >= argc) break;
117 strncpy(cfgfile_basename, argv[++i], MAXPATHLEN-100); /* TODO buffer overruns */
118 printf("Using config file %s.\n", cfgfile_basename);
120 else if (!strcmp(argv[i], "-cdfile")) {
121 char isofilename[MAXPATHLEN];
123 if (i+1 >= argc) break;
124 strncpy(isofilename, argv[++i], MAXPATHLEN);
125 if (isofilename[0] != '/') {
126 getcwd(path, MAXPATHLEN);
127 if (strlen(path) + strlen(isofilename) + 1 < MAXPATHLEN) {
129 strcat(path, isofilename);
130 strcpy(isofilename, path);
135 cdfile = isofilename;
137 else if (!strcmp(argv[i], "-h") ||
138 !strcmp(argv[i], "-help") ||
139 !strcmp(argv[i], "--help")) {
140 printf(PACKAGE_NAME " " PACKAGE_VERSION "\n");
142 " pcsx [options] [file]\n"
144 "\t-cdfile FILE\tRuns a CD image file\n"
145 "\t-nogui\t\tDon't open the GTK GUI\n"
146 "\t-cfg FILE\tLoads desired configuration file (default: ~/.pcsx/pcsx.cfg)\n"
147 "\t-psxout\t\tEnable PSX output\n"
148 "\t-load STATENUM\tLoads savestate STATENUM (1-5)\n"
149 "\t-h -help\tDisplay this message\n"
150 "\tfile\t\tLoads file\n"));
153 strncpy(file, argv[i], MAXPATHLEN);
154 if (file[0] != '/') {
155 getcwd(path, MAXPATHLEN);
156 if (strlen(path) + strlen(file) + 1 < MAXPATHLEN) {
166 memset(&Config, 0, sizeof(PcsxConfig));
167 strcpy(Config.Net, "Disabled");
171 MAKE_PATH(Config.Mcd1, MEMCARD_DIR, "card1.mcd");
172 MAKE_PATH(Config.Mcd2, MEMCARD_DIR, "card2.mcd");
173 strcpy(Config.Bios, "HLE");
174 strcpy(Config.BiosDir, "bios");
176 strcpy(Config.PluginsDir, "plugins");
177 strcpy(Config.Gpu, "builtin_gpu");
178 strcpy(Config.Spu, "builtin_spu");
179 strcpy(Config.Cdr, "builtin_cdr");
180 strcpy(Config.Pad1, "builtin_pad");
181 strcpy(Config.Pad2, "builtin_pad");
184 snprintf(Config.PatchesDir, sizeof(Config.PatchesDir), "." PATCHES_DIR);
186 // switch to plugin dotdir
187 // this lets plugins work without modification!
188 gchar *plugin_default_dir = g_build_filename(getenv("HOME"), PLUGINS_DIR, NULL);
189 chdir(plugin_default_dir);
190 g_free(plugin_default_dir);
194 set_cd_image(cdfile);
205 if (LoadPlugins() == -1) {
206 SysMessage("Failed loading plugins!");
211 if (OpenPlugins() == -1) {
214 plugin_call_rearmed_cbs();
219 if (file[0] != '\0') {
220 if (Load(file) != -1)
224 if (LoadCdrom() == -1) {
226 printf(_("Could not load CD-ROM!\n"));
233 // If a state has been specified, then load that
235 char state_filename[MAXPATHLEN];
236 int ret = get_state_filename(state_filename, sizeof(state_filename), loadst - 1);
238 ret = LoadState(state_filename);
239 printf("%s state %s\n", ret ? "failed to load" : "loaded", state_filename);
259 if (EmuInit() == -1) {
260 printf("PSX emulator couldn't be initialized.\n");
264 LoadMcds(Config.Mcd1, Config.Mcd2); /* TODO Do we need to have this here, or in the calling main() function?? */
274 printf("SysRunGui\n");
278 printf("StartGui\n");
281 static void dummy_lace()
286 // rearmed hack: EmuReset() runs some code when real BIOS is used,
287 // but we usually do reset from menu while GPU is not open yet,
288 // so we need to prevent updateLace() call..
289 void *real_lace = GPU_updateLace;
290 GPU_updateLace = dummy_lace;
294 // hmh core forgets this
297 GPU_updateLace = real_lace;
306 if (emuLog != NULL) fclose(emuLog);
313 printf("OnFile_Exit\n");
320 int get_state_filename(char *buf, int size, int i) {
324 strncpy(trimlabel, CdromLabel, 32);
326 for (j = 31; j >= 0; j--)
327 if (trimlabel[j] == ' ')
332 snprintf(buf, size, "." STATES_DIR "%.32s-%.9s.%3.3d",
333 trimlabel, CdromId, i);
338 void SysPrintf(const char *fmt, ...) {
343 vsprintf(msg, fmt, list);
346 fprintf(emuLog, "%s", msg);
349 void SysMessage(const char *fmt, ...) {
354 vsprintf(msg, fmt, list);
357 if (msg[strlen(msg) - 1] == '\n')
358 msg[strlen(msg) - 1] = 0;
360 fprintf(stderr, "%s\n", msg);
363 static void SignalExit(int sig) {
368 #define PARSEPATH(dst, src) \
369 ptr = src + strlen(src); \
370 while (*ptr != '\\' && ptr != src) ptr--; \
372 strcpy(dst, ptr+1); \
375 static int _OpenPlugins(void) {
378 signal(SIGINT, SignalExit);
379 signal(SIGPIPE, SignalExit);
381 GPU_clearDynarec(clearDynarec);
384 if (ret < 0) { SysMessage(_("Error opening CD-ROM plugin!")); return -1; }
386 if (ret < 0) { SysMessage(_("Error opening SPU plugin!")); return -1; }
387 SPU_registerCallback(SPUirq);
388 // pcsx-rearmed: we handle gpu elsewhere
389 //ret = GPU_open(&gpuDisp, "PCSX", NULL);
390 //if (ret < 0) { SysMessage(_("Error opening GPU plugin!")); return -1; }
391 ret = PAD1_open(&gpuDisp);
392 if (ret < 0) { SysMessage(_("Error opening Controller 1 plugin!")); return -1; }
393 ret = PAD2_open(&gpuDisp);
394 if (ret < 0) { SysMessage(_("Error opening Controller 2 plugin!")); return -1; }
396 if (Config.UseNet && !NetOpened) {
398 char path[MAXPATHLEN];
399 char dotdir[MAXPATHLEN];
401 MAKE_PATH(dotdir, "/.pcsx/plugins/", NULL);
403 strcpy(info.EmuName, "PCSX " PACKAGE_VERSION);
404 strncpy(info.CdromID, CdromId, 9);
405 strncpy(info.CdromLabel, CdromLabel, 9);
407 info.GPU_showScreenPic = GPU_showScreenPic;
408 info.GPU_displayText = GPU_displayText;
409 info.GPU_showScreenPic = GPU_showScreenPic;
410 info.PAD_setSensitive = PAD1_setSensitive;
411 sprintf(path, "%s%s", Config.BiosDir, Config.Bios);
412 strcpy(info.BIOSpath, path);
413 strcpy(info.MCD1path, Config.Mcd1);
414 strcpy(info.MCD2path, Config.Mcd2);
415 sprintf(path, "%s%s", dotdir, Config.Gpu);
416 strcpy(info.GPUpath, path);
417 sprintf(path, "%s%s", dotdir, Config.Spu);
418 strcpy(info.SPUpath, path);
419 sprintf(path, "%s%s", dotdir, Config.Cdr);
420 strcpy(info.CDRpath, path);
423 ret = NET_open(&gpuDisp);
426 // -2 is returned when something in the info
427 // changed and needs to be synced
430 PARSEPATH(Config.Bios, info.BIOSpath);
431 PARSEPATH(Config.Gpu, info.GPUpath);
432 PARSEPATH(Config.Spu, info.SPUpath);
433 PARSEPATH(Config.Cdr, info.CDRpath);
435 strcpy(Config.Mcd1, info.MCD1path);
436 strcpy(Config.Mcd2, info.MCD2path);
439 Config.UseNet = FALSE;
442 if (NET_queryPlayer() == 1) {
443 if (SendPcsxInfo() == -1) Config.UseNet = FALSE;
445 if (RecvPcsxInfo() == -1) Config.UseNet = FALSE;
449 } else if (Config.UseNet) {
459 while ((ret = _OpenPlugins()) == -2) {
461 LoadMcds(Config.Mcd1, Config.Mcd2);
462 if (LoadPlugins() == -1) return -1;
467 void ClosePlugins() {
470 signal(SIGINT, SIG_DFL);
471 signal(SIGPIPE, SIG_DFL);
473 if (ret < 0) { SysMessage(_("Error closing CD-ROM plugin!")); return; }
475 if (ret < 0) { SysMessage(_("Error closing SPU plugin!")); return; }
477 if (ret < 0) { SysMessage(_("Error closing Controller 1 Plugin!")); return; }
479 if (ret < 0) { SysMessage(_("Error closing Controller 2 plugin!")); return; }
480 // pcsx-rearmed: we handle gpu elsewhere
482 //if (ret < 0) { SysMessage(_("Error closing GPU plugin!")); return; }
490 /* this is to avoid having to hack every plugin to stop using $HOME */
491 char *getenv(const char *name)
493 static char ret[8] = ".";
495 if (name && strcmp(name, "HOME") == 0 &&
496 ((int)name >> 28) == 0) // HACK: let libs find home
499 return real_getenv(name);
503 /* we hook statically linked plugins here */
504 static const char *builtin_plugins[] = {
505 "builtin_gpu", "builtin_spu", "builtin_cdr", "builtin_pad",
509 static const int builtin_plugin_ids[] = {
510 PLUGIN_GPU, PLUGIN_SPU, PLUGIN_CDR, PLUGIN_PAD,
514 void *SysLoadLibrary(const char *lib) {
515 const char *tmp = strrchr(lib, '/');
519 printf("plugin: %s\n", lib);
523 for (i = 0; i < ARRAY_SIZE(builtin_plugins); i++)
524 if (strcmp(tmp, builtin_plugins[i]) == 0)
525 return (void *)(long)(PLUGIN_DL_BASE + builtin_plugin_ids[i]);
528 #if defined(__x86_64__) || defined(__i386__)
530 char name[MAXPATHLEN];
531 snprintf(name, sizeof(name), "%s.x86", lib);
535 ret = dlopen(lib, RTLD_NOW);
537 fprintf(stderr, "dlopen: %s\n", dlerror());
541 void *SysLoadSym(void *lib, const char *sym) {
542 unsigned int plugid = (unsigned int)(long)lib;
544 if (PLUGIN_DL_BASE <= plugid && plugid < PLUGIN_DL_BASE + ARRAY_SIZE(builtin_plugins))
545 return plugin_link(plugid - PLUGIN_DL_BASE, sym);
547 return dlsym(lib, sym);
550 const char *SysLibError() {
554 void SysCloseLibrary(void *lib) {
555 unsigned int plugid = (unsigned int)(long)lib;
557 if (PLUGIN_DL_BASE <= plugid && plugid < PLUGIN_DL_BASE + ARRAY_SIZE(builtin_plugins))