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>
18 #include "../gui/Linux.h"
19 #include "../libpcsxcore/misc.h"
22 static char *(*real_getenv)(const char *name);
24 static void make_path(char *buf, size_t size, const char *dir, const char *fname)
27 snprintf(buf, size, ".%s%s", dir, fname);
29 snprintf(buf, size, ".%s", dir);
31 #define MAKE_PATH(buf, dir, fname) \
32 make_path(buf, sizeof(buf), dir, fname)
34 static void create_profile_dir(const char *directory) {
35 char path[MAXPATHLEN];
37 MAKE_PATH(path, directory, NULL);
38 mkdir(path, S_IRWXU | S_IRWXG);
41 static void CheckSubDir() {
42 // make sure that ~/.pcsx exists
43 create_profile_dir(PCSX_DOT_DIR);
45 create_profile_dir(BIOS_DIR);
46 create_profile_dir(MEMCARD_DIR);
47 create_profile_dir(STATES_DIR);
48 create_profile_dir(PLUGINS_DIR);
49 create_profile_dir(PLUGINS_CFG_DIR);
50 create_profile_dir(CHEATS_DIR);
51 create_profile_dir(PATCHES_DIR);
54 static void CreateMemcard(char *filename, char *conf_mcd) {
57 make_path(conf_mcd, MAXPATHLEN, MEMCARD_DIR, filename);
59 /* Only create a memory card if an existing one does not exist */
60 if (stat(conf_mcd, &buf) == -1) {
61 SysPrintf(_("Creating memory card: %s\n"), conf_mcd);
66 int main(int argc, char *argv[])
68 char file[MAXPATHLEN] = "";
69 char path[MAXPATHLEN];
75 tmp = dlopen("/lib/libdl.so.2", RTLD_LAZY);
77 tmp = dlopen("/lib32/libdl.so.2", RTLD_LAZY);
79 real_getenv = dlsym(tmp, "getenv");
80 if (real_getenv == NULL) {
81 fprintf(stderr, "%s\n", dlerror());
86 // what is the name of the config file?
87 // it may be redefined by -cfg on the command line
88 strcpy(cfgfile_basename, "pcsx.cfg");
93 // read command line options
94 for (i = 1; i < argc; i++) {
95 if (!strcmp(argv[i], "-psxout")) Config.PsxOut = 1;
96 else if (!strcmp(argv[i], "-load")) loadst = atol(argv[++i]);
97 else if (!strcmp(argv[i], "-cfg")) {
98 if (i+1 >= argc) break;
99 strncpy(cfgfile_basename, argv[++i], MAXPATHLEN-100); /* TODO buffer overruns */
100 printf("Using config file %s.\n", cfgfile_basename);
102 else if (!strcmp(argv[i], "-cdfile")) {
103 char isofilename[MAXPATHLEN];
105 if (i+1 >= argc) break;
106 strncpy(isofilename, argv[++i], MAXPATHLEN);
107 if (isofilename[0] != '/') {
108 getcwd(path, MAXPATHLEN);
109 if (strlen(path) + strlen(isofilename) + 1 < MAXPATHLEN) {
111 strcat(path, isofilename);
112 strcpy(isofilename, path);
117 SetIsoFile(isofilename);
120 else if (!strcmp(argv[i], "-h") ||
121 !strcmp(argv[i], "-help") ||
122 !strcmp(argv[i], "--help")) {
123 printf(PACKAGE_NAME " " PACKAGE_VERSION "\n");
125 " pcsx [options] [file]\n"
127 "\t-cdfile FILE\tRuns a CD image file\n"
128 "\t-nogui\t\tDon't open the GTK GUI\n"
129 "\t-cfg FILE\tLoads desired configuration file (default: ~/.pcsx/pcsx.cfg)\n"
130 "\t-psxout\t\tEnable PSX output\n"
131 "\t-load STATENUM\tLoads savestate STATENUM (1-5)\n"
132 "\t-h -help\tDisplay this message\n"
133 "\tfile\t\tLoads file\n"));
136 strncpy(file, argv[i], MAXPATHLEN);
137 if (file[0] != '/') {
138 getcwd(path, MAXPATHLEN);
139 if (strlen(path) + strlen(file) + 1 < MAXPATHLEN) {
149 memset(&Config, 0, sizeof(PcsxConfig));
150 strcpy(Config.Net, "Disabled");
155 strcpy(Config.Bios, "HLE");
156 strcpy(Config.BiosDir, "./");
158 strcpy(Config.PluginsDir, "plugins");
159 strcpy(Config.Gpu, "builtin_gpu");
160 strcpy(Config.Spu, "builtin_spu");
161 strcpy(Config.Cdr, "builtin_cdr");
162 strcpy(Config.Pad1, "builtin_pad");
163 strcpy(Config.Pad2, "builtin_pad");
165 // try to load config
166 // if the config file doesn't exist
167 if (LoadConfig() == -1) {
168 // Uh oh, no config file found, use some defaults
171 // create & load default memcards if they don't exist
172 CreateMemcard("card1.mcd", Config.Mcd1);
173 CreateMemcard("card2.mcd", Config.Mcd2);
175 LoadMcds(Config.Mcd1, Config.Mcd2);
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);
188 if (SysInit() == -1) return 1;
192 // the following only occurs if the gui isn't started
193 if (LoadPlugins() == -1) {
194 SysMessage("Failed loading plugins!");
199 if (OpenPlugins() == -1) {
206 if (file[0] != '\0') {
210 if (LoadCdrom() == -1) {
212 printf(_("Could not load CD-ROM!\n"));
218 // If a state has been specified, then load that
220 StatesC = loadst - 1;
221 char *state_filename = get_state_filename(StatesC);
222 int ret = LoadState(state_filename);
223 printf("%s state %s\n", ret ? "failed to load" : "loaded", state_filename);
224 free(state_filename);
234 if (EmuInit() == -1) {
235 printf("PSX emulator couldn't be initialized.\n");
239 LoadMcds(Config.Mcd1, Config.Mcd2); /* TODO Do we need to have this here, or in the calling main() function?? */
249 printf("SysRunGui\n");
253 printf("StartGui\n");
266 if (emuLog != NULL) fclose(emuLog);
270 PADhandleKey(PAD1_keypressed());
271 PADhandleKey(PAD2_keypressed());
274 void UpdateMenuSlots() {
278 printf("OnFile_Exit\n");
282 void state_save(gchar *state_filename) {
283 char Text[MAXPATHLEN + 20];
287 if (SaveState(state_filename) == 0)
288 sprintf(Text, _("Saved state %s."), state_filename);
290 sprintf(Text, _("Error saving state %s!"), state_filename);
292 GPU_displayText(Text);
295 void state_load(gchar *state_filename) {
297 char Text[MAXPATHLEN + 20];
300 // check if the state file actually exists
301 fp = fopen(state_filename, "rb");
303 // file does not exist
309 ret = CheckState(state_filename);
313 ret = LoadState(state_filename);
317 // Check the CD-ROM is valid
318 if (CheckCdrom() == -1) {
324 sprintf(Text, _("Loaded state %s."), state_filename);
326 sprintf(Text, _("Error loading state %s!"), state_filename);
328 GPU_displayText(Text);
331 char *get_state_filename(int i) {
332 char SStateFile[256];
336 strncpy(trimlabel, CdromLabel, 32);
338 for (j = 31; j >= 0; j--)
339 if (trimlabel[j] == ' ')
344 snprintf(SStateFile, sizeof(SStateFile), "." STATES_DIR "%.32s-%.9s.%3.3d",
345 trimlabel, CdromId, i);
347 return strdup(SStateFile);
350 void SysPrintf(const char *fmt, ...) {
355 vsprintf(msg, fmt, list);
358 fprintf(emuLog, "%s", msg);
361 void SysMessage(const char *fmt, ...) {
366 vsprintf(msg, fmt, list);
369 if (msg[strlen(msg) - 1] == '\n')
370 msg[strlen(msg) - 1] = 0;
372 fprintf(stderr, "%s\n", msg);
376 /* this is to avoid having to hack every plugin to stop using $HOME */
377 char *getenv(const char *name)
379 static char ret[8] = ".";
381 if (name && strcmp(name, "HOME") == 0)
384 return real_getenv(name);
388 /* we hook statically linked plugins here */
389 static const char *builtin_plugins[] = {
390 "builtin_gpu", "builtin_spu", "builtin_cdr", "builtin_pad"
393 static const int builtin_plugin_ids[] = {
394 PLUGIN_GPU, PLUGIN_SPU, PLUGIN_CDR, PLUGIN_PAD,
397 void *SysLoadLibrary(const char *lib) {
398 const char *tmp = strrchr(lib, '/');
401 printf("dlopen %s\n", lib);
404 for (i = 0; i < ARRAY_SIZE(builtin_plugins); i++)
405 if (strcmp(tmp, builtin_plugins[i]) == 0)
406 return (void *)(long)(PLUGIN_DL_BASE + builtin_plugin_ids[i]);
409 return dlopen(lib, RTLD_NOW);
412 void *SysLoadSym(void *lib, const char *sym) {
413 unsigned int plugid = (unsigned int)(long)lib;
415 if (PLUGIN_DL_BASE <= plugid && plugid < PLUGIN_DL_BASE + ARRAY_SIZE(builtin_plugins))
416 return plugin_link(plugid - PLUGIN_DL_BASE, sym);
418 return dlsym(lib, sym);
421 const char *SysLibError() {
425 void SysCloseLibrary(void *lib) {
426 unsigned int plugid = (unsigned int)(long)lib;
428 if (PLUGIN_DL_BASE <= plugid && plugid < PLUGIN_DL_BASE + ARRAY_SIZE(builtin_plugins))