| 1 | /* |
| 2 | * (C) notaz, 2010 |
| 3 | * |
| 4 | * This work is licensed under the terms of the GNU GPLv2 or later. |
| 5 | * See the COPYING file in the top-level directory. |
| 6 | */ |
| 7 | |
| 8 | #include <stdio.h> |
| 9 | #include <string.h> |
| 10 | #include <stdarg.h> |
| 11 | #include <dlfcn.h> |
| 12 | #include <sys/stat.h> |
| 13 | #include <sys/types.h> |
| 14 | #include <unistd.h> |
| 15 | |
| 16 | #include "plugin.h" |
| 17 | #include "pcnt.h" |
| 18 | #include "menu.h" |
| 19 | #include "../gui/Linux.h" |
| 20 | #include "../libpcsxcore/misc.h" |
| 21 | #include "../plugins/cdrcimg/cdrcimg.h" |
| 22 | #include "common/plat.h" |
| 23 | #include "common/input.h" |
| 24 | |
| 25 | int ready_to_go; |
| 26 | int UseGui; |
| 27 | static char *(*real_getenv)(const char *name); |
| 28 | |
| 29 | static void make_path(char *buf, size_t size, const char *dir, const char *fname) |
| 30 | { |
| 31 | if (fname) |
| 32 | snprintf(buf, size, ".%s%s", dir, fname); |
| 33 | else |
| 34 | snprintf(buf, size, ".%s", dir); |
| 35 | } |
| 36 | #define MAKE_PATH(buf, dir, fname) \ |
| 37 | make_path(buf, sizeof(buf), dir, fname) |
| 38 | |
| 39 | static void create_profile_dir(const char *directory) { |
| 40 | char path[MAXPATHLEN]; |
| 41 | |
| 42 | MAKE_PATH(path, directory, NULL); |
| 43 | mkdir(path, S_IRWXU | S_IRWXG); |
| 44 | } |
| 45 | |
| 46 | static void CheckSubDir() { |
| 47 | // make sure that ~/.pcsx exists |
| 48 | create_profile_dir(PCSX_DOT_DIR); |
| 49 | |
| 50 | create_profile_dir(BIOS_DIR); |
| 51 | create_profile_dir(MEMCARD_DIR); |
| 52 | create_profile_dir(STATES_DIR); |
| 53 | create_profile_dir(PLUGINS_DIR); |
| 54 | create_profile_dir(PLUGINS_CFG_DIR); |
| 55 | create_profile_dir(CHEATS_DIR); |
| 56 | create_profile_dir(PATCHES_DIR); |
| 57 | } |
| 58 | |
| 59 | static void CreateMemcard(char *filename, char *conf_mcd) { |
| 60 | struct stat buf; |
| 61 | |
| 62 | make_path(conf_mcd, MAXPATHLEN, MEMCARD_DIR, filename); |
| 63 | |
| 64 | /* Only create a memory card if an existing one does not exist */ |
| 65 | if (stat(conf_mcd, &buf) == -1) { |
| 66 | SysPrintf(_("Creating memory card: %s\n"), conf_mcd); |
| 67 | CreateMcd(conf_mcd); |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | void set_cd_image(const char *fname) |
| 72 | { |
| 73 | const char *ext; |
| 74 | int len; |
| 75 | |
| 76 | len = strlen(fname); |
| 77 | ext = fname; |
| 78 | if (len > 2) |
| 79 | ext = fname + len - 2; |
| 80 | |
| 81 | if (strcasecmp(ext, ".z") == 0) { |
| 82 | SetIsoFile(NULL); |
| 83 | cdrcimg_set_fname(fname); |
| 84 | strcpy(Config.Cdr, "builtin_cdrcimg"); |
| 85 | } else { |
| 86 | SetIsoFile(fname); |
| 87 | strcpy(Config.Cdr, "builtin_cdr"); |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | int main(int argc, char *argv[]) |
| 92 | { |
| 93 | char file[MAXPATHLEN] = ""; |
| 94 | char path[MAXPATHLEN]; |
| 95 | const char *cdfile = NULL; |
| 96 | int loadst = 0; |
| 97 | void *tmp; |
| 98 | int i; |
| 99 | |
| 100 | tmp = dlopen("/lib/libdl.so.2", RTLD_LAZY); |
| 101 | if (tmp == NULL) |
| 102 | tmp = dlopen("/lib32/libdl.so.2", RTLD_LAZY); |
| 103 | if (tmp != NULL) |
| 104 | real_getenv = dlsym(tmp, "getenv"); |
| 105 | if (real_getenv == NULL) { |
| 106 | fprintf(stderr, "%s\n", dlerror()); |
| 107 | return 1; |
| 108 | } |
| 109 | dlclose(tmp); |
| 110 | |
| 111 | // what is the name of the config file? |
| 112 | // it may be redefined by -cfg on the command line |
| 113 | strcpy(cfgfile_basename, "pcsx.cfg"); |
| 114 | |
| 115 | emuLog = stdout; |
| 116 | SetIsoFile(NULL); |
| 117 | |
| 118 | // read command line options |
| 119 | for (i = 1; i < argc; i++) { |
| 120 | if (!strcmp(argv[i], "-psxout")) Config.PsxOut = 1; |
| 121 | else if (!strcmp(argv[i], "-load")) loadst = atol(argv[++i]); |
| 122 | else if (!strcmp(argv[i], "-cfg")) { |
| 123 | if (i+1 >= argc) break; |
| 124 | strncpy(cfgfile_basename, argv[++i], MAXPATHLEN-100); /* TODO buffer overruns */ |
| 125 | printf("Using config file %s.\n", cfgfile_basename); |
| 126 | } |
| 127 | else if (!strcmp(argv[i], "-cdfile")) { |
| 128 | char isofilename[MAXPATHLEN]; |
| 129 | |
| 130 | if (i+1 >= argc) break; |
| 131 | strncpy(isofilename, argv[++i], MAXPATHLEN); |
| 132 | if (isofilename[0] != '/') { |
| 133 | getcwd(path, MAXPATHLEN); |
| 134 | if (strlen(path) + strlen(isofilename) + 1 < MAXPATHLEN) { |
| 135 | strcat(path, "/"); |
| 136 | strcat(path, isofilename); |
| 137 | strcpy(isofilename, path); |
| 138 | } else |
| 139 | isofilename[0] = 0; |
| 140 | } |
| 141 | |
| 142 | cdfile = isofilename; |
| 143 | } |
| 144 | else if (!strcmp(argv[i], "-h") || |
| 145 | !strcmp(argv[i], "-help") || |
| 146 | !strcmp(argv[i], "--help")) { |
| 147 | printf(PACKAGE_NAME " " PACKAGE_VERSION "\n"); |
| 148 | printf("%s\n", _( |
| 149 | " pcsx [options] [file]\n" |
| 150 | "\toptions:\n" |
| 151 | "\t-cdfile FILE\tRuns a CD image file\n" |
| 152 | "\t-nogui\t\tDon't open the GTK GUI\n" |
| 153 | "\t-cfg FILE\tLoads desired configuration file (default: ~/.pcsx/pcsx.cfg)\n" |
| 154 | "\t-psxout\t\tEnable PSX output\n" |
| 155 | "\t-load STATENUM\tLoads savestate STATENUM (1-5)\n" |
| 156 | "\t-h -help\tDisplay this message\n" |
| 157 | "\tfile\t\tLoads file\n")); |
| 158 | return 0; |
| 159 | } else { |
| 160 | strncpy(file, argv[i], MAXPATHLEN); |
| 161 | if (file[0] != '/') { |
| 162 | getcwd(path, MAXPATHLEN); |
| 163 | if (strlen(path) + strlen(file) + 1 < MAXPATHLEN) { |
| 164 | strcat(path, "/"); |
| 165 | strcat(path, file); |
| 166 | strcpy(file, path); |
| 167 | } else |
| 168 | file[0] = 0; |
| 169 | } |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | memset(&Config, 0, sizeof(PcsxConfig)); |
| 174 | strcpy(Config.Net, "Disabled"); |
| 175 | |
| 176 | CheckSubDir(); |
| 177 | // ScanAllPlugins(); |
| 178 | |
| 179 | strcpy(Config.Bios, "HLE"); |
| 180 | strcpy(Config.BiosDir, "./"); |
| 181 | |
| 182 | strcpy(Config.PluginsDir, "plugins"); |
| 183 | strcpy(Config.Gpu, "builtin_gpu"); |
| 184 | strcpy(Config.Spu, "builtin_spu"); |
| 185 | strcpy(Config.Cdr, "builtin_cdr"); |
| 186 | strcpy(Config.Pad1, "builtin_pad"); |
| 187 | strcpy(Config.Pad2, "builtin_pad"); |
| 188 | Config.PsxAuto = 1; |
| 189 | |
| 190 | snprintf(Config.PatchesDir, sizeof(Config.PatchesDir), "." PATCHES_DIR); |
| 191 | /* |
| 192 | // switch to plugin dotdir |
| 193 | // this lets plugins work without modification! |
| 194 | gchar *plugin_default_dir = g_build_filename(getenv("HOME"), PLUGINS_DIR, NULL); |
| 195 | chdir(plugin_default_dir); |
| 196 | g_free(plugin_default_dir); |
| 197 | */ |
| 198 | |
| 199 | if (cdfile) |
| 200 | set_cd_image(cdfile); |
| 201 | |
| 202 | if (SysInit() == -1) |
| 203 | return 1; |
| 204 | |
| 205 | // frontend stuff |
| 206 | in_init(); |
| 207 | in_probe(); |
| 208 | plat_init(); |
| 209 | menu_init(); |
| 210 | |
| 211 | if (LoadPlugins() == -1) { |
| 212 | SysMessage("Failed loading plugins!"); |
| 213 | return 1; |
| 214 | } |
| 215 | pcnt_hook_plugins(); |
| 216 | |
| 217 | if (OpenPlugins() == -1) { |
| 218 | return 1; |
| 219 | } |
| 220 | |
| 221 | SysReset(); |
| 222 | CheckCdrom(); |
| 223 | |
| 224 | if (file[0] != '\0') { |
| 225 | if (Load(file) != -1) |
| 226 | ready_to_go = 1; |
| 227 | } else { |
| 228 | if (cdfile) { |
| 229 | if (LoadCdrom() == -1) { |
| 230 | ClosePlugins(); |
| 231 | printf(_("Could not load CD-ROM!\n")); |
| 232 | return -1; |
| 233 | } |
| 234 | ready_to_go = 1; |
| 235 | } |
| 236 | } |
| 237 | |
| 238 | // If a state has been specified, then load that |
| 239 | if (loadst) { |
| 240 | StatesC = loadst - 1; |
| 241 | char *state_filename = get_state_filename(StatesC); |
| 242 | int ret = LoadState(state_filename); |
| 243 | printf("%s state %s\n", ret ? "failed to load" : "loaded", state_filename); |
| 244 | free(state_filename); |
| 245 | } |
| 246 | |
| 247 | if (ready_to_go) |
| 248 | menu_prepare_emu(); |
| 249 | else |
| 250 | menu_loop(); |
| 251 | |
| 252 | while (1) |
| 253 | { |
| 254 | psxCpu->Execute(); |
| 255 | menu_loop(); |
| 256 | } |
| 257 | |
| 258 | return 0; |
| 259 | } |
| 260 | |
| 261 | int SysInit() { |
| 262 | if (EmuInit() == -1) { |
| 263 | printf("PSX emulator couldn't be initialized.\n"); |
| 264 | return -1; |
| 265 | } |
| 266 | |
| 267 | LoadMcds(Config.Mcd1, Config.Mcd2); /* TODO Do we need to have this here, or in the calling main() function?? */ |
| 268 | |
| 269 | if (Config.Debug) { |
| 270 | StartDebugger(); |
| 271 | } |
| 272 | |
| 273 | return 0; |
| 274 | } |
| 275 | |
| 276 | void SysRunGui() { |
| 277 | printf("SysRunGui\n"); |
| 278 | } |
| 279 | |
| 280 | void StartGui() { |
| 281 | printf("StartGui\n"); |
| 282 | } |
| 283 | |
| 284 | void SysReset() { |
| 285 | EmuReset(); |
| 286 | |
| 287 | // hmh core forgets this |
| 288 | CDR_stop(); |
| 289 | } |
| 290 | |
| 291 | void SysClose() { |
| 292 | EmuShutdown(); |
| 293 | ReleasePlugins(); |
| 294 | |
| 295 | StopDebugger(); |
| 296 | |
| 297 | if (emuLog != NULL) fclose(emuLog); |
| 298 | } |
| 299 | |
| 300 | void SysUpdate() { |
| 301 | PADhandleKey(PAD1_keypressed()); |
| 302 | PADhandleKey(PAD2_keypressed()); |
| 303 | } |
| 304 | |
| 305 | void UpdateMenuSlots() { |
| 306 | } |
| 307 | |
| 308 | void OnFile_Exit() { |
| 309 | printf("OnFile_Exit\n"); |
| 310 | plat_finish(); |
| 311 | SysClose(); |
| 312 | exit(0); |
| 313 | } |
| 314 | |
| 315 | void state_save(gchar *state_filename) { |
| 316 | char Text[MAXPATHLEN + 20]; |
| 317 | |
| 318 | GPU_updateLace(); |
| 319 | |
| 320 | if (SaveState(state_filename) == 0) |
| 321 | sprintf(Text, _("Saved state %s."), state_filename); |
| 322 | else |
| 323 | sprintf(Text, _("Error saving state %s!"), state_filename); |
| 324 | |
| 325 | GPU_displayText(Text); |
| 326 | } |
| 327 | |
| 328 | void state_load(gchar *state_filename) { |
| 329 | int ret; |
| 330 | char Text[MAXPATHLEN + 20]; |
| 331 | FILE *fp; |
| 332 | |
| 333 | // check if the state file actually exists |
| 334 | fp = fopen(state_filename, "rb"); |
| 335 | if (fp == NULL) { |
| 336 | // file does not exist |
| 337 | return; |
| 338 | } |
| 339 | |
| 340 | fclose(fp); |
| 341 | |
| 342 | ret = CheckState(state_filename); |
| 343 | |
| 344 | if (ret == 0) { |
| 345 | SysReset(); |
| 346 | ret = LoadState(state_filename); |
| 347 | } |
| 348 | |
| 349 | if (ret == 0) { |
| 350 | // Check the CD-ROM is valid |
| 351 | if (CheckCdrom() == -1) { |
| 352 | ClosePlugins(); |
| 353 | SysRunGui(); |
| 354 | return; |
| 355 | } |
| 356 | |
| 357 | sprintf(Text, _("Loaded state %s."), state_filename); |
| 358 | } else { |
| 359 | sprintf(Text, _("Error loading state %s!"), state_filename); |
| 360 | } |
| 361 | GPU_displayText(Text); |
| 362 | } |
| 363 | |
| 364 | char *get_state_filename(int i) { |
| 365 | char SStateFile[256]; |
| 366 | char trimlabel[33]; |
| 367 | int j; |
| 368 | |
| 369 | strncpy(trimlabel, CdromLabel, 32); |
| 370 | trimlabel[32] = 0; |
| 371 | for (j = 31; j >= 0; j--) |
| 372 | if (trimlabel[j] == ' ') |
| 373 | trimlabel[j] = 0; |
| 374 | else |
| 375 | continue; |
| 376 | |
| 377 | snprintf(SStateFile, sizeof(SStateFile), "." STATES_DIR "%.32s-%.9s.%3.3d", |
| 378 | trimlabel, CdromId, i); |
| 379 | |
| 380 | return strdup(SStateFile); |
| 381 | } |
| 382 | |
| 383 | void SysPrintf(const char *fmt, ...) { |
| 384 | va_list list; |
| 385 | char msg[512]; |
| 386 | |
| 387 | va_start(list, fmt); |
| 388 | vsprintf(msg, fmt, list); |
| 389 | va_end(list); |
| 390 | |
| 391 | fprintf(emuLog, "%s", msg); |
| 392 | } |
| 393 | |
| 394 | void SysMessage(const char *fmt, ...) { |
| 395 | va_list list; |
| 396 | char msg[512]; |
| 397 | |
| 398 | va_start(list, fmt); |
| 399 | vsprintf(msg, fmt, list); |
| 400 | va_end(list); |
| 401 | |
| 402 | if (msg[strlen(msg) - 1] == '\n') |
| 403 | msg[strlen(msg) - 1] = 0; |
| 404 | |
| 405 | fprintf(stderr, "%s\n", msg); |
| 406 | } |
| 407 | |
| 408 | #if 1 |
| 409 | /* this is to avoid having to hack every plugin to stop using $HOME */ |
| 410 | char *getenv(const char *name) |
| 411 | { |
| 412 | static char ret[8] = "."; |
| 413 | |
| 414 | if (name && strcmp(name, "HOME") == 0 && |
| 415 | ((int)name >> 28) == 0) // HACK: let libs find home |
| 416 | return ret; |
| 417 | |
| 418 | return real_getenv(name); |
| 419 | } |
| 420 | #endif |
| 421 | |
| 422 | /* we hook statically linked plugins here */ |
| 423 | static const char *builtin_plugins[] = { |
| 424 | "builtin_gpu", "builtin_spu", "builtin_cdr", "builtin_pad", |
| 425 | "builtin_cdrcimg", |
| 426 | }; |
| 427 | |
| 428 | static const int builtin_plugin_ids[] = { |
| 429 | PLUGIN_GPU, PLUGIN_SPU, PLUGIN_CDR, PLUGIN_PAD, |
| 430 | PLUGIN_CDRCIMG, |
| 431 | }; |
| 432 | |
| 433 | void *SysLoadLibrary(const char *lib) { |
| 434 | const char *tmp = strrchr(lib, '/'); |
| 435 | int i; |
| 436 | |
| 437 | printf("dlopen %s\n", lib); |
| 438 | if (tmp != NULL) { |
| 439 | tmp++; |
| 440 | for (i = 0; i < ARRAY_SIZE(builtin_plugins); i++) |
| 441 | if (strcmp(tmp, builtin_plugins[i]) == 0) |
| 442 | return (void *)(long)(PLUGIN_DL_BASE + builtin_plugin_ids[i]); |
| 443 | } |
| 444 | |
| 445 | return dlopen(lib, RTLD_NOW); |
| 446 | } |
| 447 | |
| 448 | void *SysLoadSym(void *lib, const char *sym) { |
| 449 | unsigned int plugid = (unsigned int)(long)lib; |
| 450 | |
| 451 | if (PLUGIN_DL_BASE <= plugid && plugid < PLUGIN_DL_BASE + ARRAY_SIZE(builtin_plugins)) |
| 452 | return plugin_link(plugid - PLUGIN_DL_BASE, sym); |
| 453 | |
| 454 | return dlsym(lib, sym); |
| 455 | } |
| 456 | |
| 457 | const char *SysLibError() { |
| 458 | return dlerror(); |
| 459 | } |
| 460 | |
| 461 | void SysCloseLibrary(void *lib) { |
| 462 | unsigned int plugid = (unsigned int)(long)lib; |
| 463 | |
| 464 | if (PLUGIN_DL_BASE <= plugid && plugid < PLUGIN_DL_BASE + ARRAY_SIZE(builtin_plugins)) |
| 465 | return; |
| 466 | |
| 467 | dlclose(lib); |
| 468 | } |
| 469 | |
| 470 | |