1 /* Pcsx - Pc Psx Emulator
2 * Copyright (C) 1999-2002 Pcsx Team
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1307 USA
34 #include "../libpcsxcore/sio.h"
43 #include <X11/extensions/XTest.h>
50 gboolean UseGui = TRUE;
52 static void CreateMemcard(char *filename, char *conf_mcd) {
56 mcd = g_build_filename(getenv("HOME"), MEMCARD_DIR, filename, NULL);
58 strcpy(conf_mcd, mcd);
60 /* Only create a memory card if an existing one does not exist */
61 if (stat(mcd, &buf) == -1) {
62 SysPrintf(_("Creating memory card: %s\n"), mcd);
69 /* Create a directory under the $HOME directory, if that directory doesn't already exist */
70 static void CreateHomeConfigDir(char *directory) {
73 if (stat(directory, &buf) == -1) {
74 gchar *dir_name = g_build_filename (getenv("HOME"), directory, NULL);
75 mkdir(dir_name, S_IRWXU | S_IRWXG);
80 static void CheckSubDir() {
81 // make sure that ~/.pcsx exists
82 CreateHomeConfigDir(PCSX_DOT_DIR);
84 CreateHomeConfigDir(BIOS_DIR);
85 CreateHomeConfigDir(MEMCARD_DIR);
86 CreateHomeConfigDir(STATES_DIR);
87 CreateHomeConfigDir(PLUGINS_DIR);
88 CreateHomeConfigDir(PLUGINS_CFG_DIR);
89 CreateHomeConfigDir(CHEATS_DIR);
90 CreateHomeConfigDir(PATCHES_DIR);
93 static void ScanPlugins(gchar* scandir) {
94 // scan for plugins and configuration tools
101 /* Any plugins found will be symlinked to the following directory */
102 dir = opendir(scandir);
104 while ((ent = readdir(dir)) != NULL) {
105 filename = g_build_filename (scandir, ent->d_name, NULL);
107 if (match(filename, ".*\\.so$") == 0 &&
108 match(filename, ".*\\.dylib$") == 0 &&
109 match(filename, "cfg.*") == 0) {
110 continue; /* Skip this file */
112 /* Create a symlink from this file to the directory ~/.pcsx/plugin */
113 linkname = g_build_filename (getenv("HOME"), PLUGINS_DIR, ent->d_name, NULL);
114 symlink(filename, linkname);
116 /* If it's a config tool, make one in the cfg dir as well.
117 This allows plugins with retarded cfg finding to work :- ) */
118 if (match(filename, "cfg.*") == 1) {
119 linkname = g_build_filename (getenv("HOME"), PLUGINS_CFG_DIR, ent->d_name, NULL);
120 symlink(filename, linkname);
130 static void ScanBios(gchar* scandir) {
138 /* Any bioses found will be symlinked to the following directory */
139 dir = opendir(scandir);
141 while ((ent = readdir(dir)) != NULL) {
142 filename = g_build_filename(scandir, ent->d_name, NULL);
144 if (match(filename, ".*\\.bin$") == 0 &&
145 match(filename, ".*\\.BIN$") == 0) {
146 continue; /* Skip this file */
148 /* Create a symlink from this file to the directory ~/.pcsx/plugin */
149 linkname = g_build_filename(getenv("HOME"), BIOS_DIR, ent->d_name, NULL);
150 symlink(filename, linkname);
160 static void CheckSymlinksInPath(char* dotdir) {
166 dir = opendir(dotdir);
168 SysMessage(_("Could not open directory: '%s'\n"), dotdir);
172 /* Check for any bad links in the directory. If the remote
173 file no longer exists, remove the link */
174 while ((ent = readdir(dir)) != NULL) {
175 linkname = g_strconcat (dotdir, ent->d_name, NULL);
177 if (stat(linkname, &stbuf) == -1) {
178 /* File link is bad, remove it */
186 static void ScanAllPlugins (void) {
189 // scan some default locations to find plugins
190 ScanPlugins("/usr/lib/games/psemu/");
191 ScanPlugins("/usr/lib/games/psemu/lib/");
192 ScanPlugins("/usr/lib/games/psemu/config/");
193 ScanPlugins("/usr/local/lib/games/psemu/lib/");
194 ScanPlugins("/usr/local/lib/games/psemu/config/");
195 ScanPlugins("/usr/local/lib/games/psemu/");
196 ScanPlugins("/usr/lib64/games/psemu/");
197 ScanPlugins("/usr/lib64/games/psemu/lib/");
198 ScanPlugins("/usr/lib64/games/psemu/config/");
199 ScanPlugins("/usr/local/lib64/games/psemu/lib/");
200 ScanPlugins("/usr/local/lib64/games/psemu/config/");
201 ScanPlugins("/usr/local/lib64/games/psemu/");
202 ScanPlugins("/usr/lib32/games/psemu/");
203 ScanPlugins("/usr/lib32/games/psemu/lib/");
204 ScanPlugins("/usr/lib32/games/psemu/config/");
205 ScanPlugins("/usr/local/lib32/games/psemu/lib/");
206 ScanPlugins("/usr/local/lib32/games/psemu/config/");
207 ScanPlugins("/usr/local/lib32/games/psemu/");
208 ScanPlugins(DEF_PLUGIN_DIR);
209 ScanPlugins(DEF_PLUGIN_DIR "/lib");
210 ScanPlugins(DEF_PLUGIN_DIR "/lib64");
211 ScanPlugins(DEF_PLUGIN_DIR "/lib32");
212 ScanPlugins(DEF_PLUGIN_DIR "/config");
214 // scan some default locations to find bioses
215 ScanBios("/usr/lib/games/psemu");
216 ScanBios("/usr/lib/games/psemu/bios");
217 ScanBios("/usr/lib64/games/psemu");
218 ScanBios("/usr/lib64/games/psemu/bios");
219 ScanBios("/usr/lib32/games/psemu");
220 ScanBios("/usr/lib32/games/psemu/bios");
221 ScanBios("/usr/share/psemu");
222 ScanBios("/usr/share/psemu/bios");
223 ScanBios("/usr/share/pcsx");
224 ScanBios("/usr/share/pcsx/bios");
225 ScanBios("/usr/local/lib/games/psemu");
226 ScanBios("/usr/local/lib/games/psemu/bios");
227 ScanBios("/usr/local/lib64/games/psemu");
228 ScanBios("/usr/local/lib64/games/psemu/bios");
229 ScanBios("/usr/local/lib32/games/psemu");
230 ScanBios("/usr/local/lib32/games/psemu/bios");
231 ScanBios("/usr/local/share/psemu");
232 ScanBios("/usr/local/share/psemu/bios");
233 ScanBios("/usr/local/share/pcsx");
234 ScanBios("/usr/local/share/pcsx/bios");
235 ScanBios(PACKAGE_DATA_DIR);
236 ScanBios(PSEMU_DATA_DIR);
237 ScanBios(PACKAGE_DATA_DIR "/bios");
238 ScanBios(PSEMU_DATA_DIR "/bios");
240 currentdir = g_strconcat(getenv("HOME"), "/.psemu-plugins/", NULL);
241 ScanPlugins(currentdir);
244 currentdir = g_strconcat(getenv("HOME"), "/.psemu/", NULL);
245 ScanPlugins(currentdir);
248 // Check for bad links in ~/.pcsx/plugins/
249 currentdir = g_build_filename(getenv("HOME"), PLUGINS_DIR, NULL);
250 CheckSymlinksInPath(currentdir);
253 // Check for bad links in ~/.pcsx/plugins/cfg
254 currentdir = g_build_filename(getenv("HOME"), PLUGINS_CFG_DIR, NULL);
255 CheckSymlinksInPath(currentdir);
258 // Check for bad links in ~/.pcsx/bios
259 currentdir = g_build_filename(getenv("HOME"), BIOS_DIR, NULL);
260 CheckSymlinksInPath(currentdir);
264 // Set the default plugin name
265 void set_default_plugin(char *plugin_name, char *conf_plugin_name) {
266 if (strlen(plugin_name) != 0) {
267 strcpy(conf_plugin_name, plugin_name);
268 printf("Picking default plugin: %s\n", plugin_name);
270 printf("No default plugin could be found for %s\n", conf_plugin_name);
273 int main(int argc, char *argv[]) {
274 char file[MAXPATHLEN] = "";
275 char path[MAXPATHLEN];
281 setlocale (LC_ALL, "");
282 bindtextdomain (GETTEXT_PACKAGE, LOCALE_DIR);
283 bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
284 textdomain (GETTEXT_PACKAGE);
287 // what is the name of the config file?
288 // it may be redefined by -cfg on the command line
289 strcpy(cfgfile_basename, "pcsx.cfg");
291 // read command line options
292 for (i = 1; i < argc; i++) {
293 if (!strcmp(argv[i], "-runcd")) runcd = RUN_CD;
294 else if (!strcmp(argv[i], "-nogui")) UseGui = FALSE;
295 else if (!strcmp(argv[i], "-psxout")) Config.PsxOut = 1;
296 else if (!strcmp(argv[i], "-load")) loadst = atol(argv[++i]);
297 else if (!strcmp(argv[i], "-cfg")) {
298 if (i+1 >= argc) break;
299 strncpy(cfgfile_basename, argv[++i], MAXPATHLEN-100); /* TODO buffer overruns */
300 printf("Using config file %s.\n", cfgfile_basename);
302 else if (!strcmp(argv[i], "-cdfile")) {
303 char isofilename[MAXPATHLEN];
305 if (i+1 >= argc) break;
306 strncpy(isofilename, argv[++i], MAXPATHLEN);
307 if (isofilename[0] != '/') {
308 getcwd(path, MAXPATHLEN);
309 if (strlen(path) + strlen(isofilename) + 1 < MAXPATHLEN) {
311 strcat(path, isofilename);
312 strcpy(isofilename, path);
317 SetIsoFile(isofilename);
320 else if (!strcmp(argv[i], "-h") ||
321 !strcmp(argv[i], "-help") ||
322 !strcmp(argv[i], "--help")) {
323 printf(PACKAGE_STRING "\n");
325 " pcsx [options] [file]\n"
327 "\t-runcd\t\tRuns CD-ROM\n"
328 "\t-cdfile FILE\tRuns a CD image file\n"
329 "\t-nogui\t\tDon't open the GTK GUI\n"
330 "\t-cfg FILE\tLoads desired configuration file (default: ~/.pcsx/pcsx.cfg)\n"
331 "\t-psxout\t\tEnable PSX output\n"
332 "\t-load STATENUM\tLoads savestate STATENUM (1-5)\n"
333 "\t-h -help\tDisplay this message\n"
334 "\tfile\t\tLoads file\n"));
337 strncpy(file, argv[i], MAXPATHLEN);
338 if (file[0] != '/') {
339 getcwd(path, MAXPATHLEN);
340 if (strlen(path) + strlen(file) + 1 < MAXPATHLEN) {
350 memset(&Config, 0, sizeof(PcsxConfig));
351 strcpy(Config.Net, "Disabled");
353 if (UseGui) gtk_init(NULL, NULL);
358 // try to load config
359 // if the config file doesn't exist
360 if (LoadConfig() == -1) {
362 printf(_("PCSX cannot be configured without using the GUI -- you should restart without -nogui.\n"));
366 // Uh oh, no config file found, use some defaults
369 gchar *str_bios_dir = g_strconcat(getenv("HOME"), BIOS_DIR, NULL);
370 strcpy(Config.BiosDir, str_bios_dir);
371 g_free(str_bios_dir);
373 gchar *str_plugin_dir = g_strconcat(getenv("HOME"), PLUGINS_DIR, NULL);
374 strcpy(Config.PluginsDir, str_plugin_dir);
375 g_free(str_plugin_dir);
377 gtk_init(NULL, NULL);
379 // Update available plugins, but not GUI
382 // Pick some defaults, if they're available
383 set_default_plugin(GpuConfS.plist[0], Config.Gpu);
384 set_default_plugin(SpuConfS.plist[0], Config.Spu);
385 set_default_plugin(CdrConfS.plist[0], Config.Cdr);
386 set_default_plugin(Pad1ConfS.plist[0], Config.Pad1);
387 set_default_plugin(Pad2ConfS.plist[0], Config.Pad2);
388 set_default_plugin(BiosConfS.plist[0], Config.Bios);
390 // create & load default memcards if they don't exist
391 CreateMemcard("card1.mcd", Config.Mcd1);
392 CreateMemcard("card2.mcd", Config.Mcd2);
394 LoadMcds(Config.Mcd1, Config.Mcd2);
399 gchar *str_patches_dir = g_strconcat(getenv("HOME"), PATCHES_DIR, NULL);
400 strcpy(Config.PatchesDir, str_patches_dir);
401 g_free(str_patches_dir);
403 // switch to plugin dotdir
404 // this lets plugins work without modification!
405 gchar *plugin_default_dir = g_build_filename(getenv("HOME"), PLUGINS_DIR, NULL);
406 chdir(plugin_default_dir);
407 g_free(plugin_default_dir);
409 if (UseGui) SetIsoFile(NULL);
411 if (SysInit() == -1) return 1;
416 // the following only occurs if the gui isn't started
417 if (LoadPlugins() == -1) {
418 SysErrorMessage(_("Error"), _("Failed loading plugins!"));
422 if (OpenPlugins() == -1 || plugins_configured() == FALSE) {
429 if (file[0] != '\0') {
432 if (runcd == RUN_CD) {
433 if (LoadCdrom() == -1) {
435 printf(_("Could not load CD-ROM!\n"));
441 // If a state has been specified, then load that
443 StatesC = loadst - 1;
444 gchar *state_filename = get_state_filename(StatesC);
445 LoadState(state_filename);
446 g_free(state_filename);
458 emuLog = fopen("emuLog.txt","wb");
462 setvbuf(emuLog, NULL, _IONBF, 0);
465 if (EmuInit() == -1) {
466 printf(_("PSX emulator couldn't be initialized.\n"));
470 LoadMcds(Config.Mcd1, Config.Mcd2); /* TODO Do we need to have this here, or in the calling main() function?? */
489 if (emuLog != NULL) fclose(emuLog);
492 void SysPrintf(const char *fmt, ...) {
497 vsprintf(msg, fmt, list);
501 static char linestart = 1;
504 printf(linestart ? " * %s" : "%s", msg);
506 if (l > 0 && msg[l - 1] == '\n') {
515 fprintf(emuLog, "%s", msg);
520 void *SysLoadLibrary(const char *lib) {
521 return dlopen(lib, RTLD_NOW);
524 void *SysLoadSym(void *lib, const char *sym) {
525 return dlsym(lib, sym);
528 const char *SysLibError() {
532 void SysCloseLibrary(void *lib) {
536 static void SysDisableScreenSaver() {
537 static time_t fake_key_timer = 0;
538 static char first_time = 1, has_test_ext = 0, t = 1;
540 extern unsigned long gpuDisp;
542 display = (Display *)gpuDisp;
545 // check if xtest is available
547 has_test_ext = XTestQueryExtension(display, &a, &b, &c, &d);
552 if (has_test_ext && fake_key_timer < time(NULL)) {
553 XTestFakeRelativeMotionEvent(display, t *= -1, 0, 0);
554 fake_key_timer = time(NULL) + 55;
559 PADhandleKey(PAD1_keypressed());
560 PADhandleKey(PAD2_keypressed());
562 SysDisableScreenSaver();
565 /* ADB TODO Replace RunGui() with StartGui ()*/