pcsxr-1.9.92
[pcsx_rearmed.git] / gui / LnxMain.c
1 /*  Pcsx - Pc Psx Emulator
2  *  Copyright (C) 1999-2002  Pcsx Team
3  *
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.
8  *
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.
13  *
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
17  */
18
19 #include "config.h"
20
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <unistd.h>
24 #include <stdarg.h>
25 #include <dlfcn.h>
26 #include <sys/mman.h>
27 #include <errno.h>
28 #include <string.h>
29 #include <time.h>
30 #include <gtk/gtk.h>
31 #include <pthread.h>
32 #include <dirent.h>
33 #include <sys/stat.h>
34 #include "../libpcsxcore/sio.h"
35
36 #include "Linux.h"
37 #include "ConfDlg.h"
38
39 #ifdef ENABLE_NLS
40 #include <locale.h>
41 #endif
42
43 #include <X11/extensions/XTest.h>
44
45 enum {
46         RUN = 0,
47         RUN_CD,
48 };
49
50 gboolean UseGui = TRUE;
51
52 static void CreateMemcard(char *filename, char *conf_mcd) {
53         gchar *mcd;
54         struct stat buf;
55
56         mcd = g_build_filename(getenv("HOME"), MEMCARD_DIR, filename, NULL);
57
58         strcpy(conf_mcd, mcd);
59
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);
63                 CreateMcd(mcd);
64         }
65
66         g_free (mcd);
67 }
68
69 /* Create a directory under the $HOME directory, if that directory doesn't already exist */
70 static void CreateHomeConfigDir(char *directory) {
71         struct stat buf;
72
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);
76                 g_free (dir_name);
77         }
78 }
79
80 static void CheckSubDir() {
81         // make sure that ~/.pcsx exists
82         CreateHomeConfigDir(PCSX_DOT_DIR);
83
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);
91 }
92
93 static void ScanPlugins(gchar* scandir) {
94         // scan for plugins and configuration tools
95         DIR *dir;
96         struct dirent *ent;
97
98         gchar *linkname;
99         gchar *filename;
100
101         /* Any plugins found will be symlinked to the following directory */
102         dir = opendir(scandir);
103         if (dir != NULL) {
104                 while ((ent = readdir(dir)) != NULL) {
105                         filename = g_build_filename (scandir, ent->d_name, NULL);
106
107                         if (match(filename, ".*\\.so$") == 0 &&
108                                 match(filename, ".*\\.dylib$") == 0 &&
109                                 match(filename, "cfg.*") == 0) {
110                                 continue;       /* Skip this file */
111                         } else {
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);
115
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);
121                                 }
122                                 g_free (linkname);
123                         }
124                         g_free (filename);
125                 }
126                 closedir(dir);
127         }
128 }
129
130 static void ScanBios(gchar* scandir) {
131         // scan for bioses
132         DIR *dir;
133         struct dirent *ent;
134
135         gchar *linkname;
136         gchar *filename;
137
138         /* Any bioses found will be symlinked to the following directory */
139         dir = opendir(scandir);
140         if (dir != NULL) {
141                 while ((ent = readdir(dir)) != NULL) {
142                         filename = g_build_filename(scandir, ent->d_name, NULL);
143
144                         if (match(filename, ".*\\.bin$") == 0 &&
145                                 match(filename, ".*\\.BIN$") == 0) {
146                                 continue;       /* Skip this file */
147                         } else {
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);
151
152                                 g_free(linkname);
153                         }
154                         g_free(filename);
155                 }
156                 closedir(dir);
157         }
158 }
159
160 static void CheckSymlinksInPath(char* dotdir) {
161         DIR *dir;
162         struct dirent *ent;
163         struct stat stbuf;
164         gchar *linkname;
165
166         dir = opendir(dotdir);
167         if (dir == NULL) {
168                 SysMessage(_("Could not open directory: '%s'\n"), dotdir);
169                 return;
170         }
171
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);
176
177                 if (stat(linkname, &stbuf) == -1) {
178                         /* File link is bad, remove it */
179                         unlink(linkname);
180                 }
181                 g_free (linkname);
182         }
183         closedir(dir);
184 }
185
186 static void ScanAllPlugins (void) {
187         gchar *currentdir;
188
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");
213
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");
239
240         currentdir = g_strconcat(getenv("HOME"), "/.psemu-plugins/", NULL);
241         ScanPlugins(currentdir);
242         g_free(currentdir);
243
244         currentdir = g_strconcat(getenv("HOME"), "/.psemu/", NULL);
245         ScanPlugins(currentdir);
246         g_free(currentdir);
247
248         // Check for bad links in ~/.pcsx/plugins/
249         currentdir = g_build_filename(getenv("HOME"), PLUGINS_DIR, NULL);
250         CheckSymlinksInPath(currentdir);
251         g_free(currentdir);
252
253         // Check for bad links in ~/.pcsx/plugins/cfg
254         currentdir = g_build_filename(getenv("HOME"), PLUGINS_CFG_DIR, NULL);
255         CheckSymlinksInPath(currentdir);
256         g_free(currentdir);
257
258         // Check for bad links in ~/.pcsx/bios
259         currentdir = g_build_filename(getenv("HOME"), BIOS_DIR, NULL);
260         CheckSymlinksInPath(currentdir);
261         g_free(currentdir);
262 }
263
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);
269         } else
270                 printf("No default plugin could be found for %s\n", conf_plugin_name);
271 }
272
273 int main(int argc, char *argv[]) {
274         char file[MAXPATHLEN] = "";
275         char path[MAXPATHLEN];
276         int runcd = RUN;
277         int loadst = 0;
278         int i;
279
280 #ifdef ENABLE_NLS
281         setlocale (LC_ALL, "");
282         bindtextdomain (GETTEXT_PACKAGE, LOCALE_DIR);
283         bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
284         textdomain (GETTEXT_PACKAGE);
285 #endif
286
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");
290
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);
301                 }
302                 else if (!strcmp(argv[i], "-cdfile")) {
303                         char isofilename[MAXPATHLEN];
304
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) {
310                                         strcat(path, "/");
311                                         strcat(path, isofilename);
312                                         strcpy(isofilename, path);
313                                 } else
314                                         isofilename[0] = 0;
315                         }
316
317                         SetIsoFile(isofilename);
318                         runcd = RUN_CD;
319                 }
320                 else if (!strcmp(argv[i], "-h") ||
321                          !strcmp(argv[i], "-help") ||
322                          !strcmp(argv[i], "--help")) {
323                          printf(PACKAGE_STRING "\n");
324                          printf("%s\n", _(
325                                                         " pcsx [options] [file]\n"
326                                                         "\toptions:\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"));
335                          return 0;
336                 } else {
337                         strncpy(file, argv[i], MAXPATHLEN);
338                         if (file[0] != '/') {
339                                 getcwd(path, MAXPATHLEN);
340                                 if (strlen(path) + strlen(file) + 1 < MAXPATHLEN) {
341                                         strcat(path, "/");
342                                         strcat(path, file);
343                                         strcpy(file, path);
344                                 } else
345                                         file[0] = 0;
346                         }
347                 }
348         }
349
350         memset(&Config, 0, sizeof(PcsxConfig));
351         strcpy(Config.Net, "Disabled");
352
353         if (UseGui) gtk_init(NULL, NULL);
354
355         CheckSubDir();
356         ScanAllPlugins();
357
358         // try to load config
359         // if the config file doesn't exist
360         if (LoadConfig() == -1) {
361                 if (!UseGui) {
362                         printf(_("PCSX cannot be configured without using the GUI -- you should restart without -nogui.\n"));
363                         return 1;
364                 }
365
366                 // Uh oh, no config file found, use some defaults
367                 Config.PsxAuto = 1;
368
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);
372
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);
376
377                 gtk_init(NULL, NULL);
378
379                 // Update available plugins, but not GUI
380                 UpdatePluginsBIOS();
381
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);
389
390                 // create & load default memcards if they don't exist
391                 CreateMemcard("card1.mcd", Config.Mcd1);
392                 CreateMemcard("card2.mcd", Config.Mcd2);
393
394                 LoadMcds(Config.Mcd1, Config.Mcd2);
395
396                 SaveConfig();
397         }
398
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);
402
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);
408
409         if (UseGui) SetIsoFile(NULL);
410
411         if (SysInit() == -1) return 1;
412
413         if (UseGui) {
414                 StartGui();
415         } else {
416                 // the following only occurs if the gui isn't started
417                 if (LoadPlugins() == -1) {
418                         SysErrorMessage(_("Error"), _("Failed loading plugins!"));
419                         return 1;
420                 }
421
422                 if (OpenPlugins() == -1 || plugins_configured() == FALSE) {
423                         return 1;
424                 }
425
426                 SysReset();
427                 CheckCdrom();
428
429                 if (file[0] != '\0') {
430                         Load(file);
431                 } else {
432                         if (runcd == RUN_CD) {
433                                 if (LoadCdrom() == -1) {
434                                         ClosePlugins();
435                                         printf(_("Could not load CD-ROM!\n"));
436                                         return -1;
437                                 }
438                         }
439                 }
440
441                 // If a state has been specified, then load that
442                 if (loadst) {
443                         StatesC = loadst - 1;
444                         gchar *state_filename = get_state_filename(StatesC);
445                         LoadState(state_filename);
446                         g_free(state_filename);
447                 }
448
449                 psxCpu->Execute();
450         }
451
452         return 0;
453 }
454
455 int SysInit() {
456 #ifdef EMU_LOG
457 #ifndef LOG_STDOUT
458         emuLog = fopen("emuLog.txt","wb");
459 #else
460         emuLog = stdout;
461 #endif
462         setvbuf(emuLog, NULL, _IONBF, 0);
463 #endif
464
465         if (EmuInit() == -1) {
466                 printf(_("PSX emulator couldn't be initialized.\n"));
467                 return -1;
468         }
469
470         LoadMcds(Config.Mcd1, Config.Mcd2);     /* TODO Do we need to have this here, or in the calling main() function?? */
471
472         if (Config.Debug) {
473                 StartDebugger();
474         }
475
476         return 0;
477 }
478
479 void SysReset() {
480         EmuReset();
481 }
482
483 void SysClose() {
484         EmuShutdown();
485         ReleasePlugins();
486
487         StopDebugger();
488
489         if (emuLog != NULL) fclose(emuLog);
490 }
491
492 void SysPrintf(const char *fmt, ...) {
493         va_list list;
494         char msg[512];
495
496         va_start(list, fmt);
497         vsprintf(msg, fmt, list);
498         va_end(list);
499
500         if (Config.PsxOut) {
501                 static char linestart = 1;
502                 int l = strlen(msg);
503
504                 printf(linestart ? " * %s" : "%s", msg);
505
506                 if (l > 0 && msg[l - 1] == '\n') {
507                         linestart = 1;
508                 } else {
509                         linestart = 0;
510                 }
511         }
512
513 #ifdef EMU_LOG
514 #ifndef LOG_STDOUT
515         fprintf(emuLog, "%s", msg);
516 #endif
517 #endif
518 }
519
520 void *SysLoadLibrary(const char *lib) {
521         return dlopen(lib, RTLD_NOW);
522 }
523
524 void *SysLoadSym(void *lib, const char *sym) {
525         return dlsym(lib, sym);
526 }
527
528 const char *SysLibError() {
529         return dlerror();
530 }
531
532 void SysCloseLibrary(void *lib) {
533         dlclose(lib);
534 }
535
536 static void SysDisableScreenSaver() {
537         static time_t fake_key_timer = 0;
538         static char first_time = 1, has_test_ext = 0, t = 1;
539         Display *display;
540         extern unsigned long gpuDisp;
541
542         display = (Display *)gpuDisp;
543
544         if (first_time) {
545                 // check if xtest is available
546                 int a, b, c, d;
547                 has_test_ext = XTestQueryExtension(display, &a, &b, &c, &d);
548
549                 first_time = 0;
550         }
551
552         if (has_test_ext && fake_key_timer < time(NULL)) {
553                 XTestFakeRelativeMotionEvent(display, t *= -1, 0, 0);
554                 fake_key_timer = time(NULL) + 55;
555         }
556 }
557
558 void SysUpdate() {
559         PADhandleKey(PAD1_keypressed());
560         PADhandleKey(PAD2_keypressed());
561
562         SysDisableScreenSaver();
563 }
564
565 /* ADB TODO Replace RunGui() with StartGui ()*/
566 void SysRunGui() {
567         StartGui();
568 }