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>
21 #include "../libpcsxcore/misc.h"
22 #include "../plugins/cdrcimg/cdrcimg.h"
23 #include "common/plat.h"
24 #include "common/input.h"
27 unsigned long gpuDisp;
28 char cfgfile_basename[MAXPATHLEN];
29 static char *(*real_getenv)(const char *name);
31 static void make_path(char *buf, size_t size, const char *dir, const char *fname)
34 snprintf(buf, size, ".%s%s", dir, fname);
36 snprintf(buf, size, ".%s", dir);
38 #define MAKE_PATH(buf, dir, fname) \
39 make_path(buf, sizeof(buf), dir, fname)
41 static void create_profile_dir(const char *directory) {
42 char path[MAXPATHLEN];
44 MAKE_PATH(path, directory, NULL);
45 mkdir(path, S_IRWXU | S_IRWXG);
48 static void CheckSubDir() {
49 // make sure that ~/.pcsx exists
50 create_profile_dir(PCSX_DOT_DIR);
52 create_profile_dir(BIOS_DIR);
53 create_profile_dir(MEMCARD_DIR);
54 create_profile_dir(STATES_DIR);
55 create_profile_dir(PLUGINS_DIR);
56 create_profile_dir(PLUGINS_CFG_DIR);
57 create_profile_dir(CHEATS_DIR);
58 create_profile_dir(PATCHES_DIR);
59 create_profile_dir(PCSX_DOT_DIR "cfg");
62 void set_cd_image(const char *fname)
70 ext = fname + len - 2;
72 if (strcasecmp(ext, ".z") == 0) {
74 cdrcimg_set_fname(fname);
75 strcpy(Config.Cdr, "builtin_cdrcimg");
78 strcpy(Config.Cdr, "builtin_cdr");
82 // from softgpu plugin
83 extern int iUseDither;
84 extern int UseFrameSkip;
85 extern int UseFrameLimit;
86 extern uint32_t dwActFixes;
87 extern float fFrameRateHz;
88 extern int dwFrameRateTicks;
91 extern int iUseReverb;
92 extern int iUseInterpolation;
94 extern int iSPUIRQWait;
97 static void ChangeWorkingDirectory(char *exe)
101 sprintf(exepath, "%s", exe);
102 s = strrchr(exepath, '/');
109 int main(int argc, char *argv[])
111 ChangeWorkingDirectory("c");
112 char file[MAXPATHLEN] = "";
113 char path[MAXPATHLEN];
114 const char *cdfile = NULL;
119 tmp = dlopen("/lib/libdl.so.2", RTLD_LAZY);
121 tmp = dlopen("/lib32/libdl.so.2", RTLD_LAZY);
123 real_getenv = dlsym(tmp, "getenv");
124 if (real_getenv == NULL) {
125 fprintf(stderr, "%s\n", dlerror());
130 // what is the name of the config file?
131 // it may be redefined by -cfg on the command line
132 strcpy(cfgfile_basename, "pcsx.cfg");
137 memset(&Config, 0, sizeof(PcsxConfig));
138 strcpy(Config.Net, "Disabled");
142 MAKE_PATH(Config.Mcd1, MEMCARD_DIR, "card1.mcd");
143 MAKE_PATH(Config.Mcd2, MEMCARD_DIR, "card2.mcd");
144 strcpy(Config.Bios, "HLE");
145 strcpy(Config.BiosDir, "/home/user/MyDocs");
150 strcpy(Config.PluginsDir, "/opt/maemo/usr/games/plugins");
151 strcpy(Config.Gpu, "builtin_gpu");
152 strcpy(Config.Spu, "builtin_spu");
153 strcpy(Config.Cdr, "builtin_cdr");
154 strcpy(Config.Pad1, "builtin_pad");
155 strcpy(Config.Pad2, "builtin_pad");
158 // read command line options
159 for (i = 1; i < argc; i++) {
160 if (!strcmp(argv[i], "-psxout")) Config.PsxOut = 1;
161 else if (!strcmp(argv[i], "-load")) loadst = atol(argv[++i]);
162 else if (!strcmp(argv[i], "-cdfile")) {
163 char isofilename[MAXPATHLEN];
165 if (i+1 >= argc) break;
166 strncpy(isofilename, argv[++i], MAXPATHLEN);
167 if (isofilename[0] != '/') {
168 getcwd(path, MAXPATHLEN);
169 if (strlen(path) + strlen(isofilename) + 1 < MAXPATHLEN) {
171 strcat(path, isofilename);
172 strcpy(isofilename, path);
177 cdfile = isofilename;
179 else if (!strcmp(argv[i],"-frameskip")){
181 int tv_reg=atol(argv[++i]);
184 fFrameRateHz = (tv_reg==1)?50.0f: 59.94f;
185 dwFrameRateTicks = (100000*100 / (unsigned long)(fFrameRateHz*100));
188 else if (!strcmp(argv[i],"-sputhreaded")){
191 else if (!strcmp(argv[i],"-nosound")){
192 strcpy(Config.Spu, "spunull.so");
194 else if(!strcmp(argv[i], "-bdir")) sprintf(Config.BiosDir, "%s", argv[++i]);
195 else if(!strcmp(argv[i], "-bios")) sprintf(Config.Bios, "%s", argv[++i]);
196 else if (!strcmp(argv[i],"-gles")){
197 strcpy(Config.Gpu, "gpuGLES.so");
199 else if (!strcmp(argv[i], "-cdda")) Config.Cdda = 1;
200 else if (!strcmp(argv[i], "-xa")) Config.Xa = 1;
201 else if (!strcmp(argv[i], "-rcnt")) Config.RCntFix = 1 ;
202 else if (!strcmp(argv[i], "-sio")) Config.Sio = 1;
203 else if (!strcmp(argv[i], "-spuirq")) Config.SpuIrq = 1;
204 else if (!strcmp(argv[i], "-vsync")) Config.VSyncWA = 1;
205 else if (!strcmp(argv[i], "-h") ||
206 !strcmp(argv[i], "-help") ||
207 !strcmp(argv[i], "--help")) {
208 printf(PACKAGE_NAME " " PACKAGE_VERSION "\n");
210 " pcsx [options] [file]\n"
212 "\t-cdfile FILE\tRuns a CD image file\n"
213 "\t-psxout\t\tEnable PSX output\n"
214 "\t-nosound\t\tDisable sound using spunull plugin\n"
215 "\t-sputhreaded\t\tMove sound to separate thread\n"
216 "\t-frameskip\t\tEnable frameskip\n"
217 "\t-load STATENUM\tLoads savestate STATENUM (1-5)\n"
218 "\t-h -help\tDisplay this message\n"
219 "\tfile\t\tLoads file\n"));
222 strncpy(file, argv[i], MAXPATHLEN);
223 if (file[0] != '/') {
224 getcwd(path, MAXPATHLEN);
225 if (strlen(path) + strlen(file) + 1 < MAXPATHLEN) {
238 snprintf(Config.PatchesDir, sizeof(Config.PatchesDir), "/opt/maemo/usr/games" PATCHES_DIR);
240 // switch to plugin dotdir
241 // this lets plugins work without modification!
242 gchar *plugin_default_dir = g_build_filename(getenv("HOME"), PLUGINS_DIR, NULL);
243 chdir(plugin_default_dir);
244 g_free(plugin_default_dir);
247 hildon_init(&argc, &argv);
250 set_cd_image(cdfile);
258 if (LoadPlugins() == -1) {
259 SysMessage("Failed loading plugins!");
264 if (OpenPlugins() == -1) {
267 plugin_call_rearmed_cbs();
272 if (file[0] != '\0') {
273 if (Load(file) != -1)
277 if (LoadCdrom() == -1) {
279 printf(_("Could not load CD-ROM!\n"));
286 // If a state has been specified, then load that
288 char state_filename[MAXPATHLEN];
289 int ret = get_state_filename(state_filename, sizeof(state_filename), loadst - 1);
291 ret = LoadState(state_filename);
292 printf("%s state %s\n", ret ? "failed to load" : "loaded", state_filename);
300 printf ("somethings goes wrong, maybe you forgot -cdfile ? \n");
313 if (EmuInit() == -1) {
314 printf("PSX emulator couldn't be initialized.\n");
318 LoadMcds(Config.Mcd1, Config.Mcd2); /* TODO Do we need to have this here, or in the calling main() function?? */
328 printf("SysRunGui\n");
332 printf("StartGui\n");
338 // hmh core forgets this
348 if (emuLog != NULL) fclose(emuLog);
355 printf("OnFile_Exit\n");
361 int get_state_filename(char *buf, int size, int i) {
365 strncpy(trimlabel, CdromLabel, 32);
367 for (j = 31; j >= 0; j--)
368 if (trimlabel[j] == ' ')
373 snprintf(buf, size, "." STATES_DIR "%.32s-%.9s.%3.3d",
374 trimlabel, CdromId, i);
379 void SysPrintf(const char *fmt, ...) {
384 vsprintf(msg, fmt, list);
387 fprintf(emuLog, "%s", msg);
390 void SysMessage(const char *fmt, ...) {
395 vsprintf(msg, fmt, list);
398 if (msg[strlen(msg) - 1] == '\n')
399 msg[strlen(msg) - 1] = 0;
401 fprintf(stderr, "%s\n", msg);
404 static void SignalExit(int sig) {
409 #define PARSEPATH(dst, src) \
410 ptr = src + strlen(src); \
411 while (*ptr != '\' && ptr != src) ptr--; \
413 strcpy(dst, ptr+1); \
416 static int _OpenPlugins(void) {
419 signal(SIGINT, SignalExit);
420 signal(SIGPIPE, SignalExit);
422 GPU_clearDynarec(clearDynarec);
425 if (ret < 0) { SysMessage(_("Error opening CD-ROM plugin!")); return -1; }
427 if (ret < 0) { SysMessage(_("Error opening SPU plugin!")); return -1; }
428 SPU_registerCallback(SPUirq);
429 // pcsx-rearmed: we handle gpu elsewhere
430 //ret = GPU_open(&gpuDisp, "PCSX", NULL);
431 //if (ret < 0) { SysMessage(_("Error opening GPU plugin!")); return -1; }
432 ret = PAD1_open(&gpuDisp);
433 if (ret < 0) { SysMessage(_("Error opening Controller 1 plugin!")); return -1; }
434 ret = PAD2_open(&gpuDisp);
435 if (ret < 0) { SysMessage(_("Error opening Controller 2 plugin!")); return -1; }
437 if (Config.UseNet && !NetOpened) {
439 char path[MAXPATHLEN];
440 char dotdir[MAXPATHLEN];
442 MAKE_PATH(dotdir, "/.pcsx/plugins/", NULL);
444 strcpy(info.EmuName, "PCSX " PACKAGE_VERSION);
445 strncpy(info.CdromID, CdromId, 9);
446 strncpy(info.CdromLabel, CdromLabel, 9);
448 info.GPU_showScreenPic = GPU_showScreenPic;
449 info.GPU_displayText = GPU_displayText;
450 info.GPU_showScreenPic = GPU_showScreenPic;
451 info.PAD_setSensitive = PAD1_setSensitive;
452 sprintf(path, "%s%s", Config.BiosDir, Config.Bios);
453 strcpy(info.BIOSpath, path);
454 strcpy(info.MCD1path, Config.Mcd1);
455 strcpy(info.MCD2path, Config.Mcd2);
456 sprintf(path, "%s%s", dotdir, Config.Gpu);
457 strcpy(info.GPUpath, path);
458 sprintf(path, "%s%s", dotdir, Config.Spu);
459 strcpy(info.SPUpath, path);
460 sprintf(path, "%s%s", dotdir, Config.Cdr);
461 strcpy(info.CDRpath, path);
464 ret = NET_open(&gpuDisp);
467 // -2 is returned when something in the info
468 // changed and needs to be synced
471 PARSEPATH(Config.Bios, info.BIOSpath);
472 PARSEPATH(Config.Gpu, info.GPUpath);
473 PARSEPATH(Config.Spu, info.SPUpath);
474 PARSEPATH(Config.Cdr, info.CDRpath);
476 strcpy(Config.Mcd1, info.MCD1path);
477 strcpy(Config.Mcd2, info.MCD2path);
480 Config.UseNet = FALSE;
483 if (NET_queryPlayer() == 1) {
484 if (SendPcsxInfo() == -1) Config.UseNet = FALSE;
486 if (RecvPcsxInfo() == -1) Config.UseNet = FALSE;
490 } else if (Config.UseNet) {
500 while ((ret = _OpenPlugins()) == -2) {
502 LoadMcds(Config.Mcd1, Config.Mcd2);
503 if (LoadPlugins() == -1) return -1;
508 void ClosePlugins() {
511 signal(SIGINT, SIG_DFL);
512 signal(SIGPIPE, SIG_DFL);
514 if (ret < 0) { SysMessage(_("Error closing CD-ROM plugin!")); return; }
516 if (ret < 0) { SysMessage(_("Error closing SPU plugin!")); return; }
518 if (ret < 0) { SysMessage(_("Error closing Controller 1 Plugin!")); return; }
520 if (ret < 0) { SysMessage(_("Error closing Controller 2 plugin!")); return; }
521 // pcsx-rearmed: we handle gpu elsewhere
523 //if (ret < 0) { SysMessage(_("Error closing GPU plugin!")); return; }
531 /* this is to avoid having to hack every plugin to stop using $HOME */
532 char *getenv(const char *name)
534 static char ret[8] = ".";
536 if (name && strcmp(name, "HOME") == 0 &&
537 ((int)name >> 28) == 0) // HACK: let libs find home
540 return real_getenv(name);
544 /* we hook statically linked plugins here */
545 static const char *builtin_plugins[] = {
546 "builtin_gpu", "builtin_spu", "builtin_cdr", "builtin_pad",
550 static const int builtin_plugin_ids[] = {
551 PLUGIN_GPU, PLUGIN_SPU, PLUGIN_CDR, PLUGIN_PAD,
555 void *SysLoadLibrary(const char *lib) {
556 const char *tmp = strrchr(lib, '/');
560 printf("plugin: %s\n", lib);
564 for (i = 0; i < ARRAY_SIZE(builtin_plugins); i++)
565 if (strcmp(tmp, builtin_plugins[i]) == 0)
566 return (void *)(long)(PLUGIN_DL_BASE + builtin_plugin_ids[i]);
569 #if defined(__x86_64__) || defined(__i386__)
571 char name[MAXPATHLEN];
572 snprintf(name, sizeof(name), "%s.x86", lib);
576 ret = dlopen(lib, RTLD_NOW);
578 fprintf(stderr, "dlopen: %s\n", dlerror());
582 void *SysLoadSym(void *lib, const char *sym) {
583 unsigned int plugid = (unsigned int)(long)lib;
585 if (PLUGIN_DL_BASE <= plugid && plugid < PLUGIN_DL_BASE + ARRAY_SIZE(builtin_plugins))
586 return plugin_link(plugid - PLUGIN_DL_BASE, sym);
588 return dlsym(lib, sym);
591 const char *SysLibError() {
595 void SysCloseLibrary(void *lib) {
596 unsigned int plugid = (unsigned int)(long)lib;
598 if (PLUGIN_DL_BASE <= plugid && plugid < PLUGIN_DL_BASE + ARRAY_SIZE(builtin_plugins))