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 "../libpcsxcore/new_dynarec/new_dynarec.h"
24 #include "../plugins/cdrcimg/cdrcimg.h"
25 #include "common/plat.h"
26 #include "common/input.h"
29 unsigned long gpuDisp;
30 char cfgfile_basename[MAXPATHLEN];
31 static char *(*real_getenv)(const char *name);
33 static void make_path(char *buf, size_t size, const char *dir, const char *fname)
36 snprintf(buf, size, ".%s%s", dir, fname);
38 snprintf(buf, size, ".%s", dir);
40 #define MAKE_PATH(buf, dir, fname) \
41 make_path(buf, sizeof(buf), dir, fname)
43 static void create_profile_dir(const char *directory) {
44 char path[MAXPATHLEN];
46 MAKE_PATH(path, directory, NULL);
47 mkdir(path, S_IRWXU | S_IRWXG);
50 static void CheckSubDir() {
51 // make sure that ~/.pcsx exists
52 create_profile_dir(PCSX_DOT_DIR);
54 create_profile_dir(BIOS_DIR);
55 create_profile_dir(MEMCARD_DIR);
56 create_profile_dir(STATES_DIR);
57 create_profile_dir(PLUGINS_DIR);
58 create_profile_dir(PLUGINS_CFG_DIR);
59 create_profile_dir(CHEATS_DIR);
60 create_profile_dir(PATCHES_DIR);
61 create_profile_dir(PCSX_DOT_DIR "cfg");
64 void set_cd_image(const char *fname)
66 const char *ext = NULL;
69 ext = strrchr(fname, '.');
72 strcasecmp(ext, ".z") == 0 || strcasecmp(ext, ".bz") == 0 ||
73 strcasecmp(ext, ".znx") == 0 || strcasecmp(ext, ".pbp") == 0)) {
75 cdrcimg_set_fname(fname);
76 strcpy(Config.Cdr, "builtin_cdrcimg");
79 strcpy(Config.Cdr, "builtin_cdr");
83 static void set_default_paths(void)
85 MAKE_PATH(Config.Mcd1, MEMCARD_DIR, "card1.mcd");
86 MAKE_PATH(Config.Mcd2, MEMCARD_DIR, "card2.mcd");
87 strcpy(Config.BiosDir, "bios");
89 strcpy(Config.PluginsDir, "plugins");
90 strcpy(Config.Gpu, "builtin_gpu");
91 strcpy(Config.Spu, "builtin_spu");
92 strcpy(Config.Cdr, "builtin_cdr");
93 strcpy(Config.Pad1, "builtin_pad");
94 strcpy(Config.Pad2, "builtin_pad");
95 strcpy(Config.Net, "Disabled");
98 snprintf(Config.PatchesDir, sizeof(Config.PatchesDir), "." PATCHES_DIR);
101 int main(int argc, char *argv[])
105 tmp = dlopen("/lib/libdl.so.2", RTLD_LAZY);
107 tmp = dlopen("/lib32/libdl.so.2", RTLD_LAZY);
109 real_getenv = dlsym(tmp, "getenv");
110 if (real_getenv == NULL) {
111 fprintf(stderr, "%s\n", dlerror());
116 // what is the name of the config file?
117 // it may be redefined by -cfg on the command line
118 strcpy(cfgfile_basename, "pcsx.cfg");
123 memset(&Config, 0, sizeof(Config));
127 strcpy(Config.Bios, "HLE");
130 extern int maemo_main(int argc, char **argv);
131 return maemo_main(argc, argv);
133 char file[MAXPATHLEN] = "";
134 char path[MAXPATHLEN];
135 const char *cdfile = NULL;
139 // read command line options
140 for (i = 1; i < argc; i++) {
141 if (!strcmp(argv[i], "-psxout")) Config.PsxOut = 1;
142 else if (!strcmp(argv[i], "-load")) loadst = atol(argv[++i]);
143 else if (!strcmp(argv[i], "-cfg")) {
144 if (i+1 >= argc) break;
145 strncpy(cfgfile_basename, argv[++i], MAXPATHLEN-100); /* TODO buffer overruns */
146 printf("Using config file %s.\n", cfgfile_basename);
148 else if (!strcmp(argv[i], "-cdfile")) {
149 char isofilename[MAXPATHLEN];
151 if (i+1 >= argc) break;
152 strncpy(isofilename, argv[++i], MAXPATHLEN);
153 if (isofilename[0] != '/') {
154 getcwd(path, MAXPATHLEN);
155 if (strlen(path) + strlen(isofilename) + 1 < MAXPATHLEN) {
157 strcat(path, isofilename);
158 strcpy(isofilename, path);
163 cdfile = isofilename;
165 else if (!strcmp(argv[i], "-h") ||
166 !strcmp(argv[i], "-help") ||
167 !strcmp(argv[i], "--help")) {
168 printf(PACKAGE_NAME " " PACKAGE_VERSION "\n");
170 " pcsx [options] [file]\n"
172 "\t-cdfile FILE\tRuns a CD image file\n"
173 "\t-nogui\t\tDon't open the GTK GUI\n"
174 "\t-cfg FILE\tLoads desired configuration file (default: ~/.pcsx/pcsx.cfg)\n"
175 "\t-psxout\t\tEnable PSX output\n"
176 "\t-load STATENUM\tLoads savestate STATENUM (1-5)\n"
177 "\t-h -help\tDisplay this message\n"
178 "\tfile\t\tLoads file\n"));
181 strncpy(file, argv[i], MAXPATHLEN);
182 if (file[0] != '/') {
183 getcwd(path, MAXPATHLEN);
184 if (strlen(path) + strlen(file) + 1 < MAXPATHLEN) {
195 set_cd_image(cdfile);
206 if (LoadPlugins() == -1) {
207 // FIXME: this recovery doesn't work, just delete bad config and bail out
208 // SysMessage("could not load plugins, retrying with defaults\n");
210 snprintf(path, sizeof(path), "." PCSX_DOT_DIR "%s", cfgfile_basename);
212 SysMessage("Failed loading plugins!");
217 if (OpenPlugins() == -1) {
220 plugin_call_rearmed_cbs();
225 if (file[0] != '\0') {
226 if (Load(file) != -1)
230 if (LoadCdrom() == -1) {
232 printf(_("Could not load CD-ROM!\n"));
239 // If a state has been specified, then load that
241 char state_filename[MAXPATHLEN];
242 int ret = get_state_filename(state_filename, sizeof(state_filename), loadst - 1);
244 ret = LoadState(state_filename);
245 printf("%s state %s\n", ret ? "failed to load" : "loaded", state_filename);
267 if (EmuInit() == -1) {
268 printf("PSX emulator couldn't be initialized.\n");
272 LoadMcds(Config.Mcd1, Config.Mcd2); /* TODO Do we need to have this here, or in the calling main() function?? */
282 printf("SysRunGui\n");
286 printf("StartGui\n");
289 static void dummy_lace()
294 // rearmed hack: EmuReset() runs some code when real BIOS is used,
295 // but we usually do reset from menu while GPU is not open yet,
296 // so we need to prevent updateLace() call..
297 void *real_lace = GPU_updateLace;
298 GPU_updateLace = dummy_lace;
302 // hmh core forgets this
305 GPU_updateLace = real_lace;
314 if (emuLog != NULL) fclose(emuLog);
321 printf("OnFile_Exit\n");
330 int get_state_filename(char *buf, int size, int i) {
334 strncpy(trimlabel, CdromLabel, 32);
336 for (j = 31; j >= 0; j--)
337 if (trimlabel[j] == ' ')
342 snprintf(buf, size, "." STATES_DIR "%.32s-%.9s.%3.3d",
343 trimlabel, CdromId, i);
348 void SysPrintf(const char *fmt, ...) {
353 vsprintf(msg, fmt, list);
356 fprintf(emuLog, "%s", msg);
359 void SysMessage(const char *fmt, ...) {
364 vsprintf(msg, fmt, list);
367 if (msg[strlen(msg) - 1] == '\n')
368 msg[strlen(msg) - 1] = 0;
370 fprintf(stderr, "%s\n", msg);
373 static void SignalExit(int sig) {
378 #define PARSEPATH(dst, src) \
379 ptr = src + strlen(src); \
380 while (*ptr != '\\' && ptr != src) ptr--; \
382 strcpy(dst, ptr+1); \
385 static int _OpenPlugins(void) {
388 signal(SIGINT, SignalExit);
389 signal(SIGPIPE, SignalExit);
391 GPU_clearDynarec(clearDynarec);
394 if (ret < 0) { SysMessage(_("Error opening CD-ROM plugin!")); return -1; }
396 if (ret < 0) { SysMessage(_("Error opening SPU plugin!")); return -1; }
397 SPU_registerCallback(SPUirq);
398 // pcsx-rearmed: we handle gpu elsewhere
399 //ret = GPU_open(&gpuDisp, "PCSX", NULL);
400 //if (ret < 0) { SysMessage(_("Error opening GPU plugin!")); return -1; }
401 ret = PAD1_open(&gpuDisp);
402 if (ret < 0) { SysMessage(_("Error opening Controller 1 plugin!")); return -1; }
403 ret = PAD2_open(&gpuDisp);
404 if (ret < 0) { SysMessage(_("Error opening Controller 2 plugin!")); return -1; }
406 if (Config.UseNet && !NetOpened) {
408 char path[MAXPATHLEN];
409 char dotdir[MAXPATHLEN];
411 MAKE_PATH(dotdir, "/.pcsx/plugins/", NULL);
413 strcpy(info.EmuName, "PCSX " PACKAGE_VERSION);
414 strncpy(info.CdromID, CdromId, 9);
415 strncpy(info.CdromLabel, CdromLabel, 9);
417 info.GPU_showScreenPic = GPU_showScreenPic;
418 info.GPU_displayText = GPU_displayText;
419 info.GPU_showScreenPic = GPU_showScreenPic;
420 info.PAD_setSensitive = PAD1_setSensitive;
421 sprintf(path, "%s%s", Config.BiosDir, Config.Bios);
422 strcpy(info.BIOSpath, path);
423 strcpy(info.MCD1path, Config.Mcd1);
424 strcpy(info.MCD2path, Config.Mcd2);
425 sprintf(path, "%s%s", dotdir, Config.Gpu);
426 strcpy(info.GPUpath, path);
427 sprintf(path, "%s%s", dotdir, Config.Spu);
428 strcpy(info.SPUpath, path);
429 sprintf(path, "%s%s", dotdir, Config.Cdr);
430 strcpy(info.CDRpath, path);
433 ret = NET_open(&gpuDisp);
436 // -2 is returned when something in the info
437 // changed and needs to be synced
440 PARSEPATH(Config.Bios, info.BIOSpath);
441 PARSEPATH(Config.Gpu, info.GPUpath);
442 PARSEPATH(Config.Spu, info.SPUpath);
443 PARSEPATH(Config.Cdr, info.CDRpath);
445 strcpy(Config.Mcd1, info.MCD1path);
446 strcpy(Config.Mcd2, info.MCD2path);
449 Config.UseNet = FALSE;
452 if (NET_queryPlayer() == 1) {
453 if (SendPcsxInfo() == -1) Config.UseNet = FALSE;
455 if (RecvPcsxInfo() == -1) Config.UseNet = FALSE;
459 } else if (Config.UseNet) {
469 while ((ret = _OpenPlugins()) == -2) {
471 LoadMcds(Config.Mcd1, Config.Mcd2);
472 if (LoadPlugins() == -1) return -1;
477 void ClosePlugins() {
480 signal(SIGINT, SIG_DFL);
481 signal(SIGPIPE, SIG_DFL);
483 if (ret < 0) { SysMessage(_("Error closing CD-ROM plugin!")); return; }
485 if (ret < 0) { SysMessage(_("Error closing SPU plugin!")); return; }
487 if (ret < 0) { SysMessage(_("Error closing Controller 1 Plugin!")); return; }
489 if (ret < 0) { SysMessage(_("Error closing Controller 2 plugin!")); return; }
490 // pcsx-rearmed: we handle gpu elsewhere
492 //if (ret < 0) { SysMessage(_("Error closing GPU plugin!")); return; }
500 /* this is to avoid having to hack every plugin to stop using $HOME */
501 char *getenv(const char *name)
503 static char ret[8] = ".";
505 if (name && strcmp(name, "HOME") == 0 &&
506 ((int)name >> 28) == 0) // HACK: let libs find home
509 return real_getenv(name);
513 /* we hook statically linked plugins here */
514 static const char *builtin_plugins[] = {
515 "builtin_gpu", "builtin_spu", "builtin_cdr", "builtin_pad",
519 static const int builtin_plugin_ids[] = {
520 PLUGIN_GPU, PLUGIN_SPU, PLUGIN_CDR, PLUGIN_PAD,
524 void *SysLoadLibrary(const char *lib) {
525 const char *tmp = strrchr(lib, '/');
529 printf("plugin: %s\n", lib);
533 for (i = 0; i < ARRAY_SIZE(builtin_plugins); i++)
534 if (strcmp(tmp, builtin_plugins[i]) == 0)
535 return (void *)(long)(PLUGIN_DL_BASE + builtin_plugin_ids[i]);
538 #if defined(__x86_64__) || defined(__i386__)
540 char name[MAXPATHLEN];
541 snprintf(name, sizeof(name), "%s.x86", lib);
545 ret = dlopen(lib, RTLD_NOW);
547 fprintf(stderr, "dlopen: %s\n", dlerror());
551 void *SysLoadSym(void *lib, const char *sym) {
552 unsigned int plugid = (unsigned int)(long)lib;
554 if (PLUGIN_DL_BASE <= plugid && plugid < PLUGIN_DL_BASE + ARRAY_SIZE(builtin_plugins))
555 return plugin_link(plugid - PLUGIN_DL_BASE, sym);
557 return dlsym(lib, sym);
560 const char *SysLibError() {
564 void SysCloseLibrary(void *lib) {
565 unsigned int plugid = (unsigned int)(long)lib;
567 if (PLUGIN_DL_BASE <= plugid && plugid < PLUGIN_DL_BASE + ARRAY_SIZE(builtin_plugins))