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 "common/plat.h"
22 #include "common/input.h"
26 static char *(*real_getenv)(const char *name);
28 static void make_path(char *buf, size_t size, const char *dir, const char *fname)
31 snprintf(buf, size, ".%s%s", dir, fname);
33 snprintf(buf, size, ".%s", dir);
35 #define MAKE_PATH(buf, dir, fname) \
36 make_path(buf, sizeof(buf), dir, fname)
38 static void create_profile_dir(const char *directory) {
39 char path[MAXPATHLEN];
41 MAKE_PATH(path, directory, NULL);
42 mkdir(path, S_IRWXU | S_IRWXG);
45 static void CheckSubDir() {
46 // make sure that ~/.pcsx exists
47 create_profile_dir(PCSX_DOT_DIR);
49 create_profile_dir(BIOS_DIR);
50 create_profile_dir(MEMCARD_DIR);
51 create_profile_dir(STATES_DIR);
52 create_profile_dir(PLUGINS_DIR);
53 create_profile_dir(PLUGINS_CFG_DIR);
54 create_profile_dir(CHEATS_DIR);
55 create_profile_dir(PATCHES_DIR);
58 static void CreateMemcard(char *filename, char *conf_mcd) {
61 make_path(conf_mcd, MAXPATHLEN, MEMCARD_DIR, filename);
63 /* Only create a memory card if an existing one does not exist */
64 if (stat(conf_mcd, &buf) == -1) {
65 SysPrintf(_("Creating memory card: %s\n"), conf_mcd);
70 int main(int argc, char *argv[])
72 char file[MAXPATHLEN] = "";
73 char path[MAXPATHLEN];
79 tmp = dlopen("/lib/libdl.so.2", RTLD_LAZY);
81 tmp = dlopen("/lib32/libdl.so.2", RTLD_LAZY);
83 real_getenv = dlsym(tmp, "getenv");
84 if (real_getenv == NULL) {
85 fprintf(stderr, "%s\n", dlerror());
90 // what is the name of the config file?
91 // it may be redefined by -cfg on the command line
92 strcpy(cfgfile_basename, "pcsx.cfg");
97 // read command line options
98 for (i = 1; i < argc; i++) {
99 if (!strcmp(argv[i], "-psxout")) Config.PsxOut = 1;
100 else if (!strcmp(argv[i], "-load")) loadst = atol(argv[++i]);
101 else if (!strcmp(argv[i], "-cfg")) {
102 if (i+1 >= argc) break;
103 strncpy(cfgfile_basename, argv[++i], MAXPATHLEN-100); /* TODO buffer overruns */
104 printf("Using config file %s.\n", cfgfile_basename);
106 else if (!strcmp(argv[i], "-cdfile")) {
107 char isofilename[MAXPATHLEN];
109 if (i+1 >= argc) break;
110 strncpy(isofilename, argv[++i], MAXPATHLEN);
111 if (isofilename[0] != '/') {
112 getcwd(path, MAXPATHLEN);
113 if (strlen(path) + strlen(isofilename) + 1 < MAXPATHLEN) {
115 strcat(path, isofilename);
116 strcpy(isofilename, path);
121 SetIsoFile(isofilename);
124 else if (!strcmp(argv[i], "-h") ||
125 !strcmp(argv[i], "-help") ||
126 !strcmp(argv[i], "--help")) {
127 printf(PACKAGE_NAME " " PACKAGE_VERSION "\n");
129 " pcsx [options] [file]\n"
131 "\t-cdfile FILE\tRuns a CD image file\n"
132 "\t-nogui\t\tDon't open the GTK GUI\n"
133 "\t-cfg FILE\tLoads desired configuration file (default: ~/.pcsx/pcsx.cfg)\n"
134 "\t-psxout\t\tEnable PSX output\n"
135 "\t-load STATENUM\tLoads savestate STATENUM (1-5)\n"
136 "\t-h -help\tDisplay this message\n"
137 "\tfile\t\tLoads file\n"));
140 strncpy(file, argv[i], MAXPATHLEN);
141 if (file[0] != '/') {
142 getcwd(path, MAXPATHLEN);
143 if (strlen(path) + strlen(file) + 1 < MAXPATHLEN) {
153 memset(&Config, 0, sizeof(PcsxConfig));
154 strcpy(Config.Net, "Disabled");
159 strcpy(Config.Bios, "HLE");
160 strcpy(Config.BiosDir, "./");
162 strcpy(Config.PluginsDir, "plugins");
163 strcpy(Config.Gpu, "builtin_gpu");
164 strcpy(Config.Spu, "builtin_spu");
165 strcpy(Config.Cdr, "builtin_cdr");
166 strcpy(Config.Pad1, "builtin_pad");
167 strcpy(Config.Pad2, "builtin_pad");
169 // try to load config
170 // if the config file doesn't exist
171 if (LoadConfig() == -1) {
172 // Uh oh, no config file found, use some defaults
175 // create & load default memcards if they don't exist
176 CreateMemcard("card1.mcd", Config.Mcd1);
177 CreateMemcard("card2.mcd", Config.Mcd2);
179 LoadMcds(Config.Mcd1, Config.Mcd2);
184 snprintf(Config.PatchesDir, sizeof(Config.PatchesDir), "." PATCHES_DIR);
186 // switch to plugin dotdir
187 // this lets plugins work without modification!
188 gchar *plugin_default_dir = g_build_filename(getenv("HOME"), PLUGINS_DIR, NULL);
189 chdir(plugin_default_dir);
190 g_free(plugin_default_dir);
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");
284 if (emuLog != NULL) fclose(emuLog);
288 PADhandleKey(PAD1_keypressed());
289 PADhandleKey(PAD2_keypressed());
292 void UpdateMenuSlots() {
296 printf("OnFile_Exit\n");
300 void state_save(gchar *state_filename) {
301 char Text[MAXPATHLEN + 20];
305 if (SaveState(state_filename) == 0)
306 sprintf(Text, _("Saved state %s."), state_filename);
308 sprintf(Text, _("Error saving state %s!"), state_filename);
310 GPU_displayText(Text);
313 void state_load(gchar *state_filename) {
315 char Text[MAXPATHLEN + 20];
318 // check if the state file actually exists
319 fp = fopen(state_filename, "rb");
321 // file does not exist
327 ret = CheckState(state_filename);
331 ret = LoadState(state_filename);
335 // Check the CD-ROM is valid
336 if (CheckCdrom() == -1) {
342 sprintf(Text, _("Loaded state %s."), state_filename);
344 sprintf(Text, _("Error loading state %s!"), state_filename);
346 GPU_displayText(Text);
349 char *get_state_filename(int i) {
350 char SStateFile[256];
354 strncpy(trimlabel, CdromLabel, 32);
356 for (j = 31; j >= 0; j--)
357 if (trimlabel[j] == ' ')
362 snprintf(SStateFile, sizeof(SStateFile), "." STATES_DIR "%.32s-%.9s.%3.3d",
363 trimlabel, CdromId, i);
365 return strdup(SStateFile);
368 void SysPrintf(const char *fmt, ...) {
373 vsprintf(msg, fmt, list);
376 fprintf(emuLog, "%s", msg);
379 void SysMessage(const char *fmt, ...) {
384 vsprintf(msg, fmt, list);
387 if (msg[strlen(msg) - 1] == '\n')
388 msg[strlen(msg) - 1] = 0;
390 fprintf(stderr, "%s\n", msg);
394 /* this is to avoid having to hack every plugin to stop using $HOME */
395 char *getenv(const char *name)
397 static char ret[8] = ".";
399 if (name && strcmp(name, "HOME") == 0 &&
400 ((int)name >> 28) == 0) // HACK: let libs find home
403 return real_getenv(name);
407 /* we hook statically linked plugins here */
408 static const char *builtin_plugins[] = {
409 "builtin_gpu", "builtin_spu", "builtin_cdr", "builtin_pad"
412 static const int builtin_plugin_ids[] = {
413 PLUGIN_GPU, PLUGIN_SPU, PLUGIN_CDR, PLUGIN_PAD,
416 void *SysLoadLibrary(const char *lib) {
417 const char *tmp = strrchr(lib, '/');
420 printf("dlopen %s\n", lib);
423 for (i = 0; i < ARRAY_SIZE(builtin_plugins); i++)
424 if (strcmp(tmp, builtin_plugins[i]) == 0)
425 return (void *)(long)(PLUGIN_DL_BASE + builtin_plugin_ids[i]);
428 return dlopen(lib, RTLD_NOW);
431 void *SysLoadSym(void *lib, const char *sym) {
432 unsigned int plugid = (unsigned int)(long)lib;
434 if (PLUGIN_DL_BASE <= plugid && plugid < PLUGIN_DL_BASE + ARRAY_SIZE(builtin_plugins))
435 return plugin_link(plugid - PLUGIN_DL_BASE, sym);
437 return dlsym(lib, sym);
440 const char *SysLibError() {
444 void SysCloseLibrary(void *lib) {
445 unsigned int plugid = (unsigned int)(long)lib;
447 if (PLUGIN_DL_BASE <= plugid && plugid < PLUGIN_DL_BASE + ARRAY_SIZE(builtin_plugins))