9 #include "../gui/Linux.h"
10 #include "../libpcsxcore/misc.h"
14 static void make_path(char *buf, size_t size, const char *dir, const char *fname)
17 snprintf(buf, size, ".%s%s", dir, fname);
19 snprintf(buf, size, ".%s", dir);
21 #define MAKE_PATH(buf, dir, fname) \
22 make_path(buf, sizeof(buf), dir, fname)
24 static void create_profile_dir(const char *directory) {
25 char path[MAXPATHLEN];
27 MAKE_PATH(path, directory, NULL);
28 mkdir(path, S_IRWXU | S_IRWXG);
31 static void CheckSubDir() {
32 // make sure that ~/.pcsx exists
33 create_profile_dir(PCSX_DOT_DIR);
35 create_profile_dir(BIOS_DIR);
36 create_profile_dir(MEMCARD_DIR);
37 create_profile_dir(STATES_DIR);
38 create_profile_dir(PLUGINS_DIR);
39 create_profile_dir(PLUGINS_CFG_DIR);
40 create_profile_dir(CHEATS_DIR);
41 create_profile_dir(PATCHES_DIR);
44 static void CreateMemcard(char *filename, char *conf_mcd) {
47 make_path(conf_mcd, MAXPATHLEN, MEMCARD_DIR, filename);
49 /* Only create a memory card if an existing one does not exist */
50 if (stat(conf_mcd, &buf) == -1) {
51 SysPrintf(_("Creating memory card: %s\n"), conf_mcd);
56 int main(int argc, char *argv[])
58 char file[MAXPATHLEN] = "";
59 char path[MAXPATHLEN];
64 // what is the name of the config file?
65 // it may be redefined by -cfg on the command line
66 strcpy(cfgfile_basename, "pcsx.cfg");
71 // read command line options
72 for (i = 1; i < argc; i++) {
73 if (!strcmp(argv[i], "-nogui")) UseGui = FALSE;
74 else if (!strcmp(argv[i], "-psxout")) Config.PsxOut = 1;
75 else if (!strcmp(argv[i], "-load")) loadst = atol(argv[++i]);
76 else if (!strcmp(argv[i], "-cfg")) {
77 if (i+1 >= argc) break;
78 strncpy(cfgfile_basename, argv[++i], MAXPATHLEN-100); /* TODO buffer overruns */
79 printf("Using config file %s.\n", cfgfile_basename);
81 else if (!strcmp(argv[i], "-cdfile")) {
82 char isofilename[MAXPATHLEN];
84 if (i+1 >= argc) break;
85 strncpy(isofilename, argv[++i], MAXPATHLEN);
86 if (isofilename[0] != '/') {
87 getcwd(path, MAXPATHLEN);
88 if (strlen(path) + strlen(isofilename) + 1 < MAXPATHLEN) {
90 strcat(path, isofilename);
91 strcpy(isofilename, path);
96 SetIsoFile(isofilename);
99 else if (!strcmp(argv[i], "-h") ||
100 !strcmp(argv[i], "-help") ||
101 !strcmp(argv[i], "--help")) {
102 printf(PACKAGE_NAME " " PACKAGE_VERSION "\n");
104 " pcsx [options] [file]\n"
106 "\t-cdfile FILE\tRuns a CD image file\n"
107 "\t-nogui\t\tDon't open the GTK GUI\n"
108 "\t-cfg FILE\tLoads desired configuration file (default: ~/.pcsx/pcsx.cfg)\n"
109 "\t-psxout\t\tEnable PSX output\n"
110 "\t-load STATENUM\tLoads savestate STATENUM (1-5)\n"
111 "\t-h -help\tDisplay this message\n"
112 "\tfile\t\tLoads file\n"));
115 strncpy(file, argv[i], MAXPATHLEN);
116 if (file[0] != '/') {
117 getcwd(path, MAXPATHLEN);
118 if (strlen(path) + strlen(file) + 1 < MAXPATHLEN) {
128 memset(&Config, 0, sizeof(PcsxConfig));
129 strcpy(Config.Net, "Disabled");
134 // try to load config
135 // if the config file doesn't exist
136 if (LoadConfig() == -1) {
137 // Uh oh, no config file found, use some defaults
140 snprintf(Config.BiosDir, sizeof(Config.BiosDir), "." BIOS_DIR);
141 snprintf(Config.PluginsDir, sizeof(Config.PluginsDir), "." PLUGINS_DIR);
143 // Update available plugins, but not GUI
144 //UpdatePluginsBIOS();
146 // Pick some defaults, if they're available
148 set_default_plugin(GpuConfS.plist[0], Config.Gpu);
149 set_default_plugin(SpuConfS.plist[0], Config.Spu);
150 set_default_plugin(CdrConfS.plist[0], Config.Cdr);
151 set_default_plugin(Pad1ConfS.plist[0], Config.Pad1);
152 set_default_plugin(Pad2ConfS.plist[0], Config.Pad2);
153 set_default_plugin(BiosConfS.plist[0], Config.Bios);
155 // create & load default memcards if they don't exist
156 CreateMemcard("card1.mcd", Config.Mcd1);
157 CreateMemcard("card2.mcd", Config.Mcd2);
159 LoadMcds(Config.Mcd1, Config.Mcd2);
164 snprintf(Config.PatchesDir, sizeof(Config.PatchesDir), "." PATCHES_DIR);
166 // switch to plugin dotdir
167 // this lets plugins work without modification!
168 gchar *plugin_default_dir = g_build_filename(getenv("HOME"), PLUGINS_DIR, NULL);
169 chdir(plugin_default_dir);
170 g_free(plugin_default_dir);
172 if (SysInit() == -1) return 1;
176 // the following only occurs if the gui isn't started
177 if (LoadPlugins() == -1) {
178 SysMessage("Failed loading plugins!");
182 if (OpenPlugins() == -1) {
189 if (file[0] != '\0') {
193 if (LoadCdrom() == -1) {
195 printf(_("Could not load CD-ROM!\n"));
201 // If a state has been specified, then load that
203 StatesC = loadst - 1;
204 char *state_filename = get_state_filename(StatesC);
205 LoadState(state_filename);
206 free(state_filename);
218 if (EmuInit() == -1) {
219 printf("PSX emulator couldn't be initialized.\n");
223 LoadMcds(Config.Mcd1, Config.Mcd2); /* TODO Do we need to have this here, or in the calling main() function?? */
233 printf("SysRunGui\n");
237 printf("StartGui\n");
250 if (emuLog != NULL) fclose(emuLog);
254 PADhandleKey(PAD1_keypressed());
255 PADhandleKey(PAD2_keypressed());
258 void UpdateMenuSlots() {
262 printf("OnFile_Exit\n");
266 void state_save(gchar *state_filename) {
267 char Text[MAXPATHLEN + 20];
271 if (SaveState(state_filename) == 0)
272 sprintf(Text, _("Saved state %s."), state_filename);
274 sprintf(Text, _("Error saving state %s!"), state_filename);
276 GPU_displayText(Text);
279 void state_load(gchar *state_filename) {
281 char Text[MAXPATHLEN + 20];
284 // check if the state file actually exists
285 fp = fopen(state_filename, "rb");
287 // file does not exist
293 ret = CheckState(state_filename);
297 ret = LoadState(state_filename);
301 // Check the CD-ROM is valid
302 if (CheckCdrom() == -1) {
308 sprintf(Text, _("Loaded state %s."), state_filename);
310 sprintf(Text, _("Error loading state %s!"), state_filename);
312 GPU_displayText(Text);
315 char *get_state_filename(int i) {
316 char SStateFile[256];
320 strncpy(trimlabel, CdromLabel, 32);
322 for (j = 31; j >= 0; j--)
323 if (trimlabel[j] == ' ')
328 snprintf(SStateFile, sizeof(SStateFile), "." STATES_DIR "%.32s-%.9s.%3.3d",
329 trimlabel, CdromId, i);
331 return strdup(SStateFile);
334 void SysPrintf(const char *fmt, ...) {
339 vsprintf(msg, fmt, list);
343 static char linestart = 1;
346 printf(linestart ? " * %s" : "%s", msg);
348 if (l > 0 && msg[l - 1] == '\n') {
355 fprintf(emuLog, "%s", msg);
358 void SysMessage(const char *fmt, ...) {
363 vsprintf(msg, fmt, list);
366 if (msg[strlen(msg) - 1] == '\n')
367 msg[strlen(msg) - 1] = 0;
369 fprintf(stderr, "%s\n", msg);
372 void *SysLoadLibrary(const char *lib) {
373 return dlopen(lib, RTLD_NOW);
376 void *SysLoadSym(void *lib, const char *sym) {
377 return dlsym(lib, sym);
380 const char *SysLibError() {
384 void SysCloseLibrary(void *lib) {