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>
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"
27 static char *(*real_getenv)(const char *name);
29 static void make_path(char *buf, size_t size, const char *dir, const char *fname)
32 snprintf(buf, size, ".%s%s", dir, fname);
34 snprintf(buf, size, ".%s", dir);
36 #define MAKE_PATH(buf, dir, fname) \
37 make_path(buf, sizeof(buf), dir, fname)
39 static void create_profile_dir(const char *directory) {
40 char path[MAXPATHLEN];
42 MAKE_PATH(path, directory, NULL);
43 mkdir(path, S_IRWXU | S_IRWXG);
46 static void CheckSubDir() {
47 // make sure that ~/.pcsx exists
48 create_profile_dir(PCSX_DOT_DIR);
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 create_profile_dir(PCSX_DOT_DIR "cfg");
60 void set_cd_image(const char *fname)
68 ext = fname + len - 2;
70 if (strcasecmp(ext, ".z") == 0) {
72 cdrcimg_set_fname(fname);
73 strcpy(Config.Cdr, "builtin_cdrcimg");
76 strcpy(Config.Cdr, "builtin_cdr");
80 int main(int argc, char *argv[])
82 char file[MAXPATHLEN] = "";
83 char path[MAXPATHLEN];
84 const char *cdfile = NULL;
89 tmp = dlopen("/lib/libdl.so.2", RTLD_LAZY);
91 tmp = dlopen("/lib32/libdl.so.2", RTLD_LAZY);
93 real_getenv = dlsym(tmp, "getenv");
94 if (real_getenv == NULL) {
95 fprintf(stderr, "%s\n", dlerror());
100 // what is the name of the config file?
101 // it may be redefined by -cfg on the command line
102 strcpy(cfgfile_basename, "pcsx.cfg");
107 // read command line options
108 for (i = 1; i < argc; i++) {
109 if (!strcmp(argv[i], "-psxout")) Config.PsxOut = 1;
110 else if (!strcmp(argv[i], "-load")) loadst = atol(argv[++i]);
111 else if (!strcmp(argv[i], "-cfg")) {
112 if (i+1 >= argc) break;
113 strncpy(cfgfile_basename, argv[++i], MAXPATHLEN-100); /* TODO buffer overruns */
114 printf("Using config file %s.\n", cfgfile_basename);
116 else if (!strcmp(argv[i], "-cdfile")) {
117 char isofilename[MAXPATHLEN];
119 if (i+1 >= argc) break;
120 strncpy(isofilename, argv[++i], MAXPATHLEN);
121 if (isofilename[0] != '/') {
122 getcwd(path, MAXPATHLEN);
123 if (strlen(path) + strlen(isofilename) + 1 < MAXPATHLEN) {
125 strcat(path, isofilename);
126 strcpy(isofilename, path);
131 cdfile = isofilename;
133 else if (!strcmp(argv[i], "-h") ||
134 !strcmp(argv[i], "-help") ||
135 !strcmp(argv[i], "--help")) {
136 printf(PACKAGE_NAME " " PACKAGE_VERSION "\n");
138 " pcsx [options] [file]\n"
140 "\t-cdfile FILE\tRuns a CD image file\n"
141 "\t-nogui\t\tDon't open the GTK GUI\n"
142 "\t-cfg FILE\tLoads desired configuration file (default: ~/.pcsx/pcsx.cfg)\n"
143 "\t-psxout\t\tEnable PSX output\n"
144 "\t-load STATENUM\tLoads savestate STATENUM (1-5)\n"
145 "\t-h -help\tDisplay this message\n"
146 "\tfile\t\tLoads file\n"));
149 strncpy(file, argv[i], MAXPATHLEN);
150 if (file[0] != '/') {
151 getcwd(path, MAXPATHLEN);
152 if (strlen(path) + strlen(file) + 1 < MAXPATHLEN) {
162 memset(&Config, 0, sizeof(PcsxConfig));
163 strcpy(Config.Net, "Disabled");
167 MAKE_PATH(Config.Mcd1, MEMCARD_DIR, "card1.mcd");
168 MAKE_PATH(Config.Mcd2, MEMCARD_DIR, "card2.mcd");
169 strcpy(Config.Bios, "HLE");
170 strcpy(Config.BiosDir, "./");
172 strcpy(Config.PluginsDir, "plugins");
173 strcpy(Config.Gpu, "builtin_gpu");
174 strcpy(Config.Spu, "builtin_spu");
175 strcpy(Config.Cdr, "builtin_cdr");
176 strcpy(Config.Pad1, "builtin_pad");
177 strcpy(Config.Pad2, "builtin_pad");
180 snprintf(Config.PatchesDir, sizeof(Config.PatchesDir), "." PATCHES_DIR);
182 // switch to plugin dotdir
183 // this lets plugins work without modification!
184 gchar *plugin_default_dir = g_build_filename(getenv("HOME"), PLUGINS_DIR, NULL);
185 chdir(plugin_default_dir);
186 g_free(plugin_default_dir);
190 set_cd_image(cdfile);
201 if (LoadPlugins() == -1) {
202 SysMessage("Failed loading plugins!");
207 if (OpenPlugins() == -1) {
214 if (file[0] != '\0') {
215 if (Load(file) != -1)
219 if (LoadCdrom() == -1) {
221 printf(_("Could not load CD-ROM!\n"));
228 // If a state has been specified, then load that
230 StatesC = loadst - 1;
231 char *state_filename = get_state_filename(StatesC);
232 int ret = LoadState(state_filename);
233 printf("%s state %s\n", ret ? "failed to load" : "loaded", state_filename);
234 free(state_filename);
252 if (EmuInit() == -1) {
253 printf("PSX emulator couldn't be initialized.\n");
257 LoadMcds(Config.Mcd1, Config.Mcd2); /* TODO Do we need to have this here, or in the calling main() function?? */
267 printf("SysRunGui\n");
271 printf("StartGui\n");
277 // hmh core forgets this
287 if (emuLog != NULL) fclose(emuLog);
291 PADhandleKey(PAD1_keypressed());
292 PADhandleKey(PAD2_keypressed());
295 void UpdateMenuSlots() {
299 printf("OnFile_Exit\n");
305 void state_save(gchar *state_filename) {
306 char Text[MAXPATHLEN + 20];
310 if (SaveState(state_filename) == 0)
311 sprintf(Text, _("Saved state %s."), state_filename);
313 sprintf(Text, _("Error saving state %s!"), state_filename);
315 GPU_displayText(Text);
318 void state_load(gchar *state_filename) {
320 char Text[MAXPATHLEN + 20];
323 // check if the state file actually exists
324 fp = fopen(state_filename, "rb");
326 // file does not exist
332 ret = CheckState(state_filename);
336 ret = LoadState(state_filename);
340 // Check the CD-ROM is valid
341 if (CheckCdrom() == -1) {
347 sprintf(Text, _("Loaded state %s."), state_filename);
349 sprintf(Text, _("Error loading state %s!"), state_filename);
351 GPU_displayText(Text);
354 char *get_state_filename(int i) {
355 char SStateFile[256];
359 strncpy(trimlabel, CdromLabel, 32);
361 for (j = 31; j >= 0; j--)
362 if (trimlabel[j] == ' ')
367 snprintf(SStateFile, sizeof(SStateFile), "." STATES_DIR "%.32s-%.9s.%3.3d",
368 trimlabel, CdromId, i);
370 return strdup(SStateFile);
373 void SysPrintf(const char *fmt, ...) {
378 vsprintf(msg, fmt, list);
381 fprintf(emuLog, "%s", msg);
384 void SysMessage(const char *fmt, ...) {
389 vsprintf(msg, fmt, list);
392 if (msg[strlen(msg) - 1] == '\n')
393 msg[strlen(msg) - 1] = 0;
395 fprintf(stderr, "%s\n", msg);
399 /* this is to avoid having to hack every plugin to stop using $HOME */
400 char *getenv(const char *name)
402 static char ret[8] = ".";
404 if (name && strcmp(name, "HOME") == 0 &&
405 ((int)name >> 28) == 0) // HACK: let libs find home
408 return real_getenv(name);
412 /* we hook statically linked plugins here */
413 static const char *builtin_plugins[] = {
414 "builtin_gpu", "builtin_spu", "builtin_cdr", "builtin_pad",
418 static const int builtin_plugin_ids[] = {
419 PLUGIN_GPU, PLUGIN_SPU, PLUGIN_CDR, PLUGIN_PAD,
423 void *SysLoadLibrary(const char *lib) {
424 const char *tmp = strrchr(lib, '/');
428 printf("plugin: %s\n", lib);
432 for (i = 0; i < ARRAY_SIZE(builtin_plugins); i++)
433 if (strcmp(tmp, builtin_plugins[i]) == 0)
434 return (void *)(long)(PLUGIN_DL_BASE + builtin_plugin_ids[i]);
437 #if defined(__x86_64__) || defined(__i386__)
439 char name[MAXPATHLEN];
440 snprintf(name, sizeof(name), "%s.x86", lib);
444 ret = dlopen(lib, RTLD_NOW);
446 fprintf(stderr, "dlopen: %s\n", dlerror());
450 void *SysLoadSym(void *lib, const char *sym) {
451 unsigned int plugid = (unsigned int)(long)lib;
453 if (PLUGIN_DL_BASE <= plugid && plugid < PLUGIN_DL_BASE + ARRAY_SIZE(builtin_plugins))
454 return plugin_link(plugid - PLUGIN_DL_BASE, sym);
456 return dlsym(lib, sym);
459 const char *SysLibError() {
463 void SysCloseLibrary(void *lib) {
464 unsigned int plugid = (unsigned int)(long)lib;
466 if (PLUGIN_DL_BASE <= plugid && plugid < PLUGIN_DL_BASE + ARRAY_SIZE(builtin_plugins))