/* Pcsx - Pc Psx Emulator * Copyright (C) 1999-2002 Pcsx Team * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Steet, Fifth Floor, Boston, MA 02111-1307 USA */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include "Linux.h" /* FIXME */ #include "../libpcsxcore/sio.h" #include "config.h" #ifdef ENABLE_NLS #include #endif enum { DONT_USE_GUI = 0, USE_GUI }; enum { RUN = 0, RUN_CD, }; int UseGui = USE_GUI; static void CreateMemcard(char *filename, char *conf_mcd) { gchar *mcd; struct stat buf; mcd = g_build_filename(getenv("HOME"), MEMCARD_DIR, filename, NULL); strcpy(conf_mcd, mcd); /* Only create a memory card if an existing one does not exist */ if (stat(mcd, &buf) == -1) { SysPrintf(_("Creating memory card: %s\n"), mcd); CreateMcd(mcd); } g_free (mcd); } /* Create a directory under the $HOME directory, if that directory doesn't already exist */ static void CreateHomeConfigDir(char *directory) { struct stat buf; if (stat(directory, &buf) == -1) { gchar *dir_name = g_build_filename (getenv("HOME"), directory, NULL); mkdir(dir_name, S_IRWXU | S_IRWXG); g_free (dir_name); } } static void CheckSubDir() { // make sure that ~/.pcsx exists CreateHomeConfigDir(PCSX_DOT_DIR); CreateHomeConfigDir(BIOS_DIR); CreateHomeConfigDir(MEMCARD_DIR); CreateHomeConfigDir(STATES_DIR); CreateHomeConfigDir(PLUGINS_DIR); CreateHomeConfigDir(PLUGINS_CFG_DIR); CreateHomeConfigDir(CHEATS_DIR); } void ScanPlugins(gchar* scandir) { // scan for plugins and configuration tools DIR *dir; struct dirent *ent; gchar *linkname; gchar *filename; /* Any plugins found will be symlinked to the following directory */ dir = opendir(scandir); if (dir == NULL) { // printf(_("Could not open plugins directory: '%s'\n"), scandir); } else { // printf("Scanning %s for plugins\n", scandir); while ((ent = readdir(dir)) != NULL) { filename = g_build_filename (scandir, ent->d_name, NULL); if (match(filename, ".*\\.so$") == 0 && match(filename, ".*\\.dylib$") == 0 && match(filename, "cfg.*") == 0) { continue; /* Skip this file */ } else { /* Create a symlink from this file to the directory ~/.pcsx/plugin */ linkname = g_build_filename (getenv("HOME"), PLUGINS_DIR, ent->d_name, NULL); // printf(" Plugin file symlink: %s -> %s\n", filename, linkname); symlink(filename, linkname); /* If it's a config tool, make one in the cfg dir as well. This allows plugins with retarded cfg finding to work :- ) */ if (match(filename, "cfg.*") == 1) { linkname = g_build_filename (getenv("HOME"), PLUGINS_CFG_DIR, ent->d_name, NULL); // printf(" Config file symlink: %s -> %s\n", filename, linkname); symlink(filename, linkname); } g_free (linkname); } g_free (filename); } closedir(dir); } } static void CheckSymlinksInPath (char* dotdir) { DIR *dir; struct dirent *ent; struct stat stbuf; gchar *linkname; dir = opendir(dotdir); if (dir == NULL) { SysMessage(_("Could not open directory: '%s'\n"), dotdir); return; } /* Check for any bad links in the directory. If the remote file no longer exists, remove the link */ while ((ent = readdir(dir)) != NULL) { linkname = g_strconcat (dotdir, ent->d_name, NULL); if (stat(linkname, &stbuf) == -1) { /* File link is bad, remove it */ // printf("unlink: %s\n", linkname); unlink(linkname); } g_free (linkname); } closedir(dir); } static void ScanAllPlugins (void) { gchar *currentdir; // scan some default locations to find plugins ScanPlugins("/usr/lib/games/psemu/"); ScanPlugins("/usr/lib/games/psemu/lib/"); ScanPlugins("/usr/lib/games/psemu/config/"); ScanPlugins("/usr/local/lib/games/psemu/lib/"); ScanPlugins("/usr/local/lib/games/psemu/config/"); ScanPlugins("/usr/local/lib/games/psemu/"); ScanPlugins("/usr/lib64/games/psemu/"); ScanPlugins("/usr/lib64/games/psemu/lib/"); ScanPlugins("/usr/lib64/games/psemu/config/"); ScanPlugins("/usr/local/lib64/games/psemu/lib/"); ScanPlugins("/usr/local/lib64/games/psemu/config/"); ScanPlugins("/usr/local/lib64/games/psemu/"); ScanPlugins(DEF_PLUGIN_DIR); ScanPlugins(DEF_PLUGIN_DIR "/lib"); ScanPlugins(DEF_PLUGIN_DIR "/lib64"); ScanPlugins(DEF_PLUGIN_DIR "/config"); currentdir = g_strconcat (getenv("HOME"), "/.psemu-plugins/", NULL); ScanPlugins(currentdir); g_free (currentdir); currentdir = g_strconcat (getenv("HOME"), "/.psemu/", NULL); ScanPlugins(currentdir); g_free (currentdir); /* Check for bad links in ~/.pcsx/plugins/ */ currentdir = g_build_filename (getenv("HOME"), PLUGINS_DIR, NULL); CheckSymlinksInPath (currentdir); g_free (currentdir); /* Check for bad links in ~/.pcsx/plugins/cfg */ currentdir = g_build_filename (getenv("HOME"), PLUGINS_CFG_DIR, NULL); CheckSymlinksInPath (currentdir); g_free (currentdir); } /* Set the default plugin name */ void set_default_plugin (char *plugin_name, char *conf_plugin_name) { if (strlen(plugin_name) != 0) { strcpy(conf_plugin_name, plugin_name); printf("Picking default plugin: %s\n", plugin_name); } else printf("No default plugin could be found for %s\n", conf_plugin_name); } int main(int argc, char *argv[]) { char file[MAXPATHLEN] = ""; char path[MAXPATHLEN]; int runcd = RUN; int loadst = 0; int i; #ifdef ENABLE_NLS setlocale (LC_ALL, ""); bindtextdomain (GETTEXT_PACKAGE, LOCALE_DIR); bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8"); textdomain (GETTEXT_PACKAGE); #endif // what is the name of the config file? // it may be redefined by -cfg on the command line strcpy(cfgfile_basename, "pcsx.cfg"); // read command line options for (i=1; i= argc) break; strncpy(cfgfile_basename, argv[++i], MAXPATHLEN-100); /* TODO buffer overruns */ printf("Using config file %s.\n", cfgfile_basename); } else if (!strcmp(argv[i], "-cdfile")) { if (i+1 >= argc) break; strncpy(cdrfilename, argv[++i], MAXPATHLEN); if (cdrfilename[0] != '/') { getcwd(path, MAXPATHLEN); if (strlen(path) + strlen(cdrfilename) + 1 < MAXPATHLEN) { strcat(path, "/"); strcat(path, cdrfilename); strcpy(cdrfilename, path); } else cdrfilename[0] = 0; } runcd = RUN_CD; } else if (!strcmp(argv[i], "-h") || !strcmp(argv[i], "-help") || !strcmp(argv[i], "--help")) { printf(PACKAGE_STRING "\n"); printf("%s\n", _( " pcsx [options] [file]\n" "\toptions:\n" "\t-runcd\t\tRuns CD-ROM\n" "\t-cdfile FILE\tRuns a CD image file\n" "\t-nogui\t\tDon't open the GTK GUI\n" "\t-cfg FILE\tLoads desired configuration file (default: ~/.pcsx/pcsx.cfg)\n" "\t-psxout\t\tEnable PSX output\n" "\t-load STATENUM\tLoads savestate STATENUM (1-5)\n" "\t-h -help\tDisplay this message\n" "\tfile\t\tLoads file\n")); return 0; } else { strncpy(file, argv[i], MAXPATHLEN); if (file[0] != '/') { getcwd(path, MAXPATHLEN); if (strlen(path) + strlen(file) + 1 < MAXPATHLEN) { strcat(path, "/"); strcat(path, file); strcpy(file, path); } else file[0] = 0; } } } memset(&Config, 0, sizeof(PcsxConfig)); strcpy(Config.Net, "Disabled"); if (UseGui == USE_GUI) gtk_init(NULL, NULL); CheckSubDir(); ScanAllPlugins(); // try to load config // if the config file doesn't exist if (LoadConfig() == -1) { if (UseGui == DONT_USE_GUI) { printf(_("PCSX cannot be configured without using the GUI -- you should restart without -nogui.\n")); return 1; } // Uh oh, no config file found, use some defaults Config.PsxAuto = 1; /* ADB TODO */ gchar *str_bios_dir = g_strconcat (getenv("HOME"), BIOS_DIR, NULL); strcpy(Config.BiosDir, str_bios_dir); g_free(str_bios_dir); gchar *str_plugin_dir = g_strconcat (getenv("HOME"), PLUGINS_DIR, NULL); strcpy(Config.PluginsDir, str_plugin_dir); g_free(str_plugin_dir); gtk_init(NULL, NULL); // Update available plugins, but not GUI UpdatePluginsBIOS(); // Pick some defaults, if they're available set_default_plugin(GpuConfS.plist[0], Config.Gpu); set_default_plugin(SpuConfS.plist[0], Config.Spu); set_default_plugin(CdrConfS.plist[0], Config.Cdr); set_default_plugin(Pad1ConfS.plist[0], Config.Pad1); set_default_plugin(Pad2ConfS.plist[0], Config.Pad2); set_default_plugin(BiosConfS.plist[0], Config.Bios); // create & load default memcards if they don't exist CreateMemcard("card1.mcd", Config.Mcd1); CreateMemcard("card2.mcd", Config.Mcd2); LoadMcds(Config.Mcd1, Config.Mcd2); SaveConfig(); } // switch to plugin dotdir // this lets plugins work without modification! gchar *plugin_default_dir = g_build_filename (getenv("HOME"), PLUGINS_DIR, NULL); chdir(plugin_default_dir); /* TODO Error checking - make sure this directory is available */ g_free(plugin_default_dir); if (UseGui != DONT_USE_GUI) { cdrfilename[0] = '\0'; } if (SysInit() == -1) return 1; if (UseGui == USE_GUI) { StartGui(); } else { // the following only occurs if the gui isn't started if (LoadPlugins() == -1) { SysErrorMessage(_("Error"), _("Failed loading plugins!")); return 1; } if (OpenPlugins() == -1 || plugins_configured() == FALSE) { return 1; } SysReset(); CheckCdrom(); if (file[0] != '\0') { Load(file); } else { if (runcd == RUN_CD) { if (LoadCdrom() == -1) { ClosePlugins(); printf(_("Could not load CD-ROM!\n")); return -1; } } } /* If a state has been specified, then load that */ if (loadst) { StatesC = loadst - 1; gchar *state_filename = get_state_filename (StatesC); LoadState(state_filename); /* TODO Error checking */ g_free (state_filename); } psxCpu->Execute(); } return 0; } DIR *dir; int SysInit() { #ifdef GTE_DUMP gteLog = fopen("gteLog.txt","wb"); setvbuf(gteLog, NULL, _IONBF, 0); #endif #ifdef EMU_LOG #ifndef LOG_STDOUT emuLog = fopen("emuLog.txt","wb"); #else emuLog = stdout; #endif setvbuf(emuLog, NULL, _IONBF, 0); #endif if (psxInit() == -1) { /* TODO Error handling */ printf(_("PSX emulator couldn't be initialized.\n")); return -1; } LoadMcds(Config.Mcd1, Config.Mcd2); /* TODO Do we need to have this here, or in the calling main() function?? */ return 0; } void SysReset() { psxReset(); } void SysClose() { psxShutdown(); ReleasePlugins(); if (emuLog != NULL) fclose(emuLog); } void SysPrintf(char *fmt, ...) { va_list list; char msg[512]; va_start(list, fmt); vsprintf(msg, fmt, list); va_end(list); if (Config.PsxOut) { static char linestart = 1; int l = strlen(msg); printf(linestart ? " * %s" : "%s", msg); if (l > 0 && msg[l - 1] == '\n') { linestart = 1; } else { linestart = 0; } } #ifdef EMU_LOG #ifndef LOG_STDOUT fprintf(emuLog, "%s", msg); #endif #endif } void *SysLoadLibrary(char *lib) { return dlopen(lib, RTLD_NOW); } void *SysLoadSym(void *lib, char *sym) { return dlsym(lib, sym); } const char *SysLibError() { return dlerror(); } void SysCloseLibrary(void *lib) { dlclose(lib); } void SysUpdate() { PADhandleKey(PAD1_keypressed()); PADhandleKey(PAD2_keypressed()); } /* ADB TODO Replace RunGui() with StartGui ()*/ void SysRunGui() { StartGui(); }