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>
17 #include "../gui/Linux.h"
18 #include "../libpcsxcore/misc.h"
22 static void make_path(char *buf, size_t size, const char *dir, const char *fname)
25 snprintf(buf, size, ".%s%s", dir, fname);
27 snprintf(buf, size, ".%s", dir);
29 #define MAKE_PATH(buf, dir, fname) \
30 make_path(buf, sizeof(buf), dir, fname)
32 static void create_profile_dir(const char *directory) {
33 char path[MAXPATHLEN];
35 MAKE_PATH(path, directory, NULL);
36 mkdir(path, S_IRWXU | S_IRWXG);
39 static void CheckSubDir() {
40 // make sure that ~/.pcsx exists
41 create_profile_dir(PCSX_DOT_DIR);
43 create_profile_dir(BIOS_DIR);
44 create_profile_dir(MEMCARD_DIR);
45 create_profile_dir(STATES_DIR);
46 create_profile_dir(PLUGINS_DIR);
47 create_profile_dir(PLUGINS_CFG_DIR);
48 create_profile_dir(CHEATS_DIR);
49 create_profile_dir(PATCHES_DIR);
52 static void CreateMemcard(char *filename, char *conf_mcd) {
55 make_path(conf_mcd, MAXPATHLEN, MEMCARD_DIR, filename);
57 /* Only create a memory card if an existing one does not exist */
58 if (stat(conf_mcd, &buf) == -1) {
59 SysPrintf(_("Creating memory card: %s\n"), conf_mcd);
64 int main(int argc, char *argv[])
66 char file[MAXPATHLEN] = "";
67 char path[MAXPATHLEN];
72 // what is the name of the config file?
73 // it may be redefined by -cfg on the command line
74 strcpy(cfgfile_basename, "pcsx.cfg");
80 // read command line options
81 for (i = 1; i < argc; i++) {
82 if (!strcmp(argv[i], "-psxout")) Config.PsxOut = 1;
83 else if (!strcmp(argv[i], "-load")) loadst = atol(argv[++i]);
84 else if (!strcmp(argv[i], "-cfg")) {
85 if (i+1 >= argc) break;
86 strncpy(cfgfile_basename, argv[++i], MAXPATHLEN-100); /* TODO buffer overruns */
87 printf("Using config file %s.\n", cfgfile_basename);
89 else if (!strcmp(argv[i], "-cdfile")) {
90 char isofilename[MAXPATHLEN];
92 if (i+1 >= argc) break;
93 strncpy(isofilename, argv[++i], MAXPATHLEN);
94 if (isofilename[0] != '/') {
95 getcwd(path, MAXPATHLEN);
96 if (strlen(path) + strlen(isofilename) + 1 < MAXPATHLEN) {
98 strcat(path, isofilename);
99 strcpy(isofilename, path);
104 SetIsoFile(isofilename);
107 else if (!strcmp(argv[i], "-h") ||
108 !strcmp(argv[i], "-help") ||
109 !strcmp(argv[i], "--help")) {
110 printf(PACKAGE_NAME " " PACKAGE_VERSION "\n");
112 " pcsx [options] [file]\n"
114 "\t-cdfile FILE\tRuns a CD image file\n"
115 "\t-nogui\t\tDon't open the GTK GUI\n"
116 "\t-cfg FILE\tLoads desired configuration file (default: ~/.pcsx/pcsx.cfg)\n"
117 "\t-psxout\t\tEnable PSX output\n"
118 "\t-load STATENUM\tLoads savestate STATENUM (1-5)\n"
119 "\t-h -help\tDisplay this message\n"
120 "\tfile\t\tLoads file\n"));
123 strncpy(file, argv[i], MAXPATHLEN);
124 if (file[0] != '/') {
125 getcwd(path, MAXPATHLEN);
126 if (strlen(path) + strlen(file) + 1 < MAXPATHLEN) {
136 memset(&Config, 0, sizeof(PcsxConfig));
137 strcpy(Config.Net, "Disabled");
142 strcpy(Config.Bios, "HLE");
143 strcpy(Config.BiosDir, "./");
145 strcpy(Config.PluginsDir, "plugins");
146 strcpy(Config.Gpu, "builtin_gpu");
147 strcpy(Config.Spu, "builtin_spu");
148 strcpy(Config.Cdr, "builtin_cdr");
149 strcpy(Config.Pad1, "builtin_pad");
150 strcpy(Config.Pad2, "builtin_pad");
152 // try to load config
153 // if the config file doesn't exist
154 if (LoadConfig() == -1) {
155 // Uh oh, no config file found, use some defaults
158 // create & load default memcards if they don't exist
159 CreateMemcard("card1.mcd", Config.Mcd1);
160 CreateMemcard("card2.mcd", Config.Mcd2);
162 LoadMcds(Config.Mcd1, Config.Mcd2);
167 snprintf(Config.PatchesDir, sizeof(Config.PatchesDir), "." PATCHES_DIR);
169 // switch to plugin dotdir
170 // this lets plugins work without modification!
171 gchar *plugin_default_dir = g_build_filename(getenv("HOME"), PLUGINS_DIR, NULL);
172 chdir(plugin_default_dir);
173 g_free(plugin_default_dir);
175 if (SysInit() == -1) return 1;
179 // the following only occurs if the gui isn't started
180 if (LoadPlugins() == -1) {
181 SysMessage("Failed loading plugins!");
185 if (OpenPlugins() == -1) {
192 if (file[0] != '\0') {
196 if (LoadCdrom() == -1) {
198 printf(_("Could not load CD-ROM!\n"));
204 // If a state has been specified, then load that
206 StatesC = loadst - 1;
207 char *state_filename = get_state_filename(StatesC);
208 LoadState(state_filename);
209 free(state_filename);
219 if (EmuInit() == -1) {
220 printf("PSX emulator couldn't be initialized.\n");
224 LoadMcds(Config.Mcd1, Config.Mcd2); /* TODO Do we need to have this here, or in the calling main() function?? */
234 printf("SysRunGui\n");
238 printf("StartGui\n");
251 if (emuLog != NULL) fclose(emuLog);
255 PADhandleKey(PAD1_keypressed());
256 PADhandleKey(PAD2_keypressed());
259 void UpdateMenuSlots() {
263 printf("OnFile_Exit\n");
267 void state_save(gchar *state_filename) {
268 char Text[MAXPATHLEN + 20];
272 if (SaveState(state_filename) == 0)
273 sprintf(Text, _("Saved state %s."), state_filename);
275 sprintf(Text, _("Error saving state %s!"), state_filename);
277 GPU_displayText(Text);
280 void state_load(gchar *state_filename) {
282 char Text[MAXPATHLEN + 20];
285 // check if the state file actually exists
286 fp = fopen(state_filename, "rb");
288 // file does not exist
294 ret = CheckState(state_filename);
298 ret = LoadState(state_filename);
302 // Check the CD-ROM is valid
303 if (CheckCdrom() == -1) {
309 sprintf(Text, _("Loaded state %s."), state_filename);
311 sprintf(Text, _("Error loading state %s!"), state_filename);
313 GPU_displayText(Text);
316 char *get_state_filename(int i) {
317 char SStateFile[256];
321 strncpy(trimlabel, CdromLabel, 32);
323 for (j = 31; j >= 0; j--)
324 if (trimlabel[j] == ' ')
329 snprintf(SStateFile, sizeof(SStateFile), "." STATES_DIR "%.32s-%.9s.%3.3d",
330 trimlabel, CdromId, i);
332 return strdup(SStateFile);
335 void SysPrintf(const char *fmt, ...) {
340 vsprintf(msg, fmt, list);
344 static char linestart = 1;
347 printf(linestart ? " * %s" : "%s", msg);
349 if (l > 0 && msg[l - 1] == '\n') {
356 fprintf(emuLog, "%s", msg);
359 void SysMessage(const char *fmt, ...) {
364 vsprintf(msg, fmt, list);
367 if (msg[strlen(msg) - 1] == '\n')
368 msg[strlen(msg) - 1] = 0;
370 fprintf(stderr, "%s\n", msg);
374 /* this is to avoid having to hack every plugin to stop using $HOME */
375 char *getenv(const char *name)
377 static char ret[8] = ".";
380 if (name && strcmp(name, "DISPLAY") == 0)
383 if (name && strcmp(name, "HOME") != 0)
384 fprintf(stderr, "getenv called with %s\n", name);
390 /* we hook statically linked plugins here */
391 static const char *builtin_plugins[] = {
392 "builtin_gpu", "builtin_spu", "builtin_cdr", "builtin_pad"
395 static const int builtin_plugin_ids[] = {
396 PLUGIN_GPU, PLUGIN_SPU, PLUGIN_CDR, PLUGIN_PAD,
399 void *SysLoadLibrary(const char *lib) {
400 const char *tmp = strrchr(lib, '/');
403 printf("dlopen %s\n", lib);
406 for (i = 0; i < ARRAY_SIZE(builtin_plugins); i++)
407 if (strcmp(tmp, builtin_plugins[i]) == 0)
408 return (void *)(long)(PLUGIN_DL_BASE + builtin_plugin_ids[i]);
411 return dlopen(lib, RTLD_NOW);
414 void *SysLoadSym(void *lib, const char *sym) {
415 unsigned int plugid = (unsigned int)(long)lib;
417 if (PLUGIN_DL_BASE <= plugid && plugid < PLUGIN_DL_BASE + ARRAY_SIZE(builtin_plugins))
418 return plugin_link(plugid - PLUGIN_DL_BASE, sym);
420 return dlsym(lib, sym);
423 const char *SysLibError() {
427 void SysCloseLibrary(void *lib) {
428 unsigned int plugid = (unsigned int)(long)lib;
430 if (PLUGIN_DL_BASE <= plugid && plugid < PLUGIN_DL_BASE + ARRAY_SIZE(builtin_plugins))