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);
59 static void CreateMemcard(char *filename, char *conf_mcd) {
62 make_path(conf_mcd, MAXPATHLEN, MEMCARD_DIR, filename);
64 /* Only create a memory card if an existing one does not exist */
65 if (stat(conf_mcd, &buf) == -1) {
66 SysPrintf(_("Creating memory card: %s\n"), conf_mcd);
71 void set_cd_image(const char *fname)
79 ext = fname + len - 2;
81 if (strcasecmp(ext, ".z") == 0) {
83 cdrcimg_set_fname(fname);
84 strcpy(Config.Cdr, "builtin_cdrcimg");
87 strcpy(Config.Cdr, "builtin_cdr");
91 int main(int argc, char *argv[])
93 char file[MAXPATHLEN] = "";
94 char path[MAXPATHLEN];
95 const char *cdfile = NULL;
100 tmp = dlopen("/lib/libdl.so.2", RTLD_LAZY);
102 tmp = dlopen("/lib32/libdl.so.2", RTLD_LAZY);
104 real_getenv = dlsym(tmp, "getenv");
105 if (real_getenv == NULL) {
106 fprintf(stderr, "%s\n", dlerror());
111 // what is the name of the config file?
112 // it may be redefined by -cfg on the command line
113 strcpy(cfgfile_basename, "pcsx.cfg");
118 // read command line options
119 for (i = 1; i < argc; i++) {
120 if (!strcmp(argv[i], "-psxout")) Config.PsxOut = 1;
121 else if (!strcmp(argv[i], "-load")) loadst = atol(argv[++i]);
122 else if (!strcmp(argv[i], "-cfg")) {
123 if (i+1 >= argc) break;
124 strncpy(cfgfile_basename, argv[++i], MAXPATHLEN-100); /* TODO buffer overruns */
125 printf("Using config file %s.\n", cfgfile_basename);
127 else if (!strcmp(argv[i], "-cdfile")) {
128 char isofilename[MAXPATHLEN];
130 if (i+1 >= argc) break;
131 strncpy(isofilename, argv[++i], MAXPATHLEN);
132 if (isofilename[0] != '/') {
133 getcwd(path, MAXPATHLEN);
134 if (strlen(path) + strlen(isofilename) + 1 < MAXPATHLEN) {
136 strcat(path, isofilename);
137 strcpy(isofilename, path);
142 cdfile = isofilename;
144 else if (!strcmp(argv[i], "-h") ||
145 !strcmp(argv[i], "-help") ||
146 !strcmp(argv[i], "--help")) {
147 printf(PACKAGE_NAME " " PACKAGE_VERSION "\n");
149 " pcsx [options] [file]\n"
151 "\t-cdfile FILE\tRuns a CD image file\n"
152 "\t-nogui\t\tDon't open the GTK GUI\n"
153 "\t-cfg FILE\tLoads desired configuration file (default: ~/.pcsx/pcsx.cfg)\n"
154 "\t-psxout\t\tEnable PSX output\n"
155 "\t-load STATENUM\tLoads savestate STATENUM (1-5)\n"
156 "\t-h -help\tDisplay this message\n"
157 "\tfile\t\tLoads file\n"));
160 strncpy(file, argv[i], MAXPATHLEN);
161 if (file[0] != '/') {
162 getcwd(path, MAXPATHLEN);
163 if (strlen(path) + strlen(file) + 1 < MAXPATHLEN) {
173 memset(&Config, 0, sizeof(PcsxConfig));
174 strcpy(Config.Net, "Disabled");
179 strcpy(Config.Bios, "HLE");
180 strcpy(Config.BiosDir, "./");
182 strcpy(Config.PluginsDir, "plugins");
183 strcpy(Config.Gpu, "builtin_gpu");
184 strcpy(Config.Spu, "builtin_spu");
185 strcpy(Config.Cdr, "builtin_cdr");
186 strcpy(Config.Pad1, "builtin_pad");
187 strcpy(Config.Pad2, "builtin_pad");
189 // try to load config
190 // if the config file doesn't exist
191 if (LoadConfig() == -1) {
192 // Uh oh, no config file found, use some defaults
195 // create & load default memcards if they don't exist
196 CreateMemcard("card1.mcd", Config.Mcd1);
197 CreateMemcard("card2.mcd", Config.Mcd2);
199 LoadMcds(Config.Mcd1, Config.Mcd2);
204 snprintf(Config.PatchesDir, sizeof(Config.PatchesDir), "." PATCHES_DIR);
206 // switch to plugin dotdir
207 // this lets plugins work without modification!
208 gchar *plugin_default_dir = g_build_filename(getenv("HOME"), PLUGINS_DIR, NULL);
209 chdir(plugin_default_dir);
210 g_free(plugin_default_dir);
214 set_cd_image(cdfile);
225 if (LoadPlugins() == -1) {
226 SysMessage("Failed loading plugins!");
231 if (OpenPlugins() == -1) {
238 if (file[0] != '\0') {
239 if (Load(file) != -1)
243 if (LoadCdrom() == -1) {
245 printf(_("Could not load CD-ROM!\n"));
252 // If a state has been specified, then load that
254 StatesC = loadst - 1;
255 char *state_filename = get_state_filename(StatesC);
256 int ret = LoadState(state_filename);
257 printf("%s state %s\n", ret ? "failed to load" : "loaded", state_filename);
258 free(state_filename);
276 if (EmuInit() == -1) {
277 printf("PSX emulator couldn't be initialized.\n");
281 LoadMcds(Config.Mcd1, Config.Mcd2); /* TODO Do we need to have this here, or in the calling main() function?? */
291 printf("SysRunGui\n");
295 printf("StartGui\n");
308 if (emuLog != NULL) fclose(emuLog);
312 PADhandleKey(PAD1_keypressed());
313 PADhandleKey(PAD2_keypressed());
316 void UpdateMenuSlots() {
320 printf("OnFile_Exit\n");
324 void state_save(gchar *state_filename) {
325 char Text[MAXPATHLEN + 20];
329 if (SaveState(state_filename) == 0)
330 sprintf(Text, _("Saved state %s."), state_filename);
332 sprintf(Text, _("Error saving state %s!"), state_filename);
334 GPU_displayText(Text);
337 void state_load(gchar *state_filename) {
339 char Text[MAXPATHLEN + 20];
342 // check if the state file actually exists
343 fp = fopen(state_filename, "rb");
345 // file does not exist
351 ret = CheckState(state_filename);
355 ret = LoadState(state_filename);
359 // Check the CD-ROM is valid
360 if (CheckCdrom() == -1) {
366 sprintf(Text, _("Loaded state %s."), state_filename);
368 sprintf(Text, _("Error loading state %s!"), state_filename);
370 GPU_displayText(Text);
373 char *get_state_filename(int i) {
374 char SStateFile[256];
378 strncpy(trimlabel, CdromLabel, 32);
380 for (j = 31; j >= 0; j--)
381 if (trimlabel[j] == ' ')
386 snprintf(SStateFile, sizeof(SStateFile), "." STATES_DIR "%.32s-%.9s.%3.3d",
387 trimlabel, CdromId, i);
389 return strdup(SStateFile);
392 void SysPrintf(const char *fmt, ...) {
397 vsprintf(msg, fmt, list);
400 fprintf(emuLog, "%s", msg);
403 void SysMessage(const char *fmt, ...) {
408 vsprintf(msg, fmt, list);
411 if (msg[strlen(msg) - 1] == '\n')
412 msg[strlen(msg) - 1] = 0;
414 fprintf(stderr, "%s\n", msg);
418 /* this is to avoid having to hack every plugin to stop using $HOME */
419 char *getenv(const char *name)
421 static char ret[8] = ".";
423 if (name && strcmp(name, "HOME") == 0 &&
424 ((int)name >> 28) == 0) // HACK: let libs find home
427 return real_getenv(name);
431 /* we hook statically linked plugins here */
432 static const char *builtin_plugins[] = {
433 "builtin_gpu", "builtin_spu", "builtin_cdr", "builtin_pad",
437 static const int builtin_plugin_ids[] = {
438 PLUGIN_GPU, PLUGIN_SPU, PLUGIN_CDR, PLUGIN_PAD,
442 void *SysLoadLibrary(const char *lib) {
443 const char *tmp = strrchr(lib, '/');
446 printf("dlopen %s\n", lib);
449 for (i = 0; i < ARRAY_SIZE(builtin_plugins); i++)
450 if (strcmp(tmp, builtin_plugins[i]) == 0)
451 return (void *)(long)(PLUGIN_DL_BASE + builtin_plugin_ids[i]);
454 return dlopen(lib, RTLD_NOW);
457 void *SysLoadSym(void *lib, const char *sym) {
458 unsigned int plugid = (unsigned int)(long)lib;
460 if (PLUGIN_DL_BASE <= plugid && plugid < PLUGIN_DL_BASE + ARRAY_SIZE(builtin_plugins))
461 return plugin_link(plugid - PLUGIN_DL_BASE, sym);
463 return dlsym(lib, sym);
466 const char *SysLibError() {
470 void SysCloseLibrary(void *lib) {
471 unsigned int plugid = (unsigned int)(long)lib;
473 if (PLUGIN_DL_BASE <= plugid && plugid < PLUGIN_DL_BASE + ARRAY_SIZE(builtin_plugins))