set memcard paths, rm dead code
[pcsx_rearmed.git] / frontend / main.c
CommitLineData
e906c010 1/*
2 * (C) notaz, 2010
3 *
4 * This work is licensed under the terms of the GNU GPLv2 or later.
5 * See the COPYING file in the top-level directory.
6 */
7
80c2304e 8#include <stdio.h>
9#include <string.h>
10#include <stdarg.h>
11#include <dlfcn.h>
12#include <sys/stat.h>
13#include <sys/types.h>
14#include <unistd.h>
15
e906c010 16#include "plugin.h"
14dffdb7 17#include "pcnt.h"
3c70c47b 18#include "menu.h"
80c2304e 19#include "../gui/Linux.h"
20#include "../libpcsxcore/misc.h"
47bf65ab 21#include "../plugins/cdrcimg/cdrcimg.h"
69af03a2 22#include "common/plat.h"
23#include "common/input.h"
80c2304e 24
69af03a2 25int ready_to_go;
80c2304e 26int UseGui;
b60f2812 27static char *(*real_getenv)(const char *name);
80c2304e 28
29static void make_path(char *buf, size_t size, const char *dir, const char *fname)
30{
31 if (fname)
32 snprintf(buf, size, ".%s%s", dir, fname);
33 else
34 snprintf(buf, size, ".%s", dir);
35}
36#define MAKE_PATH(buf, dir, fname) \
37 make_path(buf, sizeof(buf), dir, fname)
38
39static void create_profile_dir(const char *directory) {
40 char path[MAXPATHLEN];
41
42 MAKE_PATH(path, directory, NULL);
43 mkdir(path, S_IRWXU | S_IRWXG);
44}
45
46static void CheckSubDir() {
47 // make sure that ~/.pcsx exists
48 create_profile_dir(PCSX_DOT_DIR);
49
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);
57}
58
47bf65ab 59void set_cd_image(const char *fname)
60{
61 const char *ext;
62 int len;
63
64 len = strlen(fname);
65 ext = fname;
66 if (len > 2)
67 ext = fname + len - 2;
68
69 if (strcasecmp(ext, ".z") == 0) {
70 SetIsoFile(NULL);
71 cdrcimg_set_fname(fname);
72 strcpy(Config.Cdr, "builtin_cdrcimg");
73 } else {
74 SetIsoFile(fname);
75 strcpy(Config.Cdr, "builtin_cdr");
76 }
77}
78
80c2304e 79int main(int argc, char *argv[])
80{
81 char file[MAXPATHLEN] = "";
82 char path[MAXPATHLEN];
47bf65ab 83 const char *cdfile = NULL;
80c2304e 84 int loadst = 0;
b60f2812 85 void *tmp;
80c2304e 86 int i;
87
b60f2812 88 tmp = dlopen("/lib/libdl.so.2", RTLD_LAZY);
fa9cfe0a 89 if (tmp == NULL)
90 tmp = dlopen("/lib32/libdl.so.2", RTLD_LAZY);
b60f2812 91 if (tmp != NULL)
92 real_getenv = dlsym(tmp, "getenv");
93 if (real_getenv == NULL) {
94 fprintf(stderr, "%s\n", dlerror());
95 return 1;
96 }
97 dlclose(tmp);
98
80c2304e 99 // what is the name of the config file?
100 // it may be redefined by -cfg on the command line
101 strcpy(cfgfile_basename, "pcsx.cfg");
102
e906c010 103 emuLog = stdout;
80c2304e 104 SetIsoFile(NULL);
80c2304e 105
106 // read command line options
107 for (i = 1; i < argc; i++) {
e906c010 108 if (!strcmp(argv[i], "-psxout")) Config.PsxOut = 1;
80c2304e 109 else if (!strcmp(argv[i], "-load")) loadst = atol(argv[++i]);
110 else if (!strcmp(argv[i], "-cfg")) {
111 if (i+1 >= argc) break;
112 strncpy(cfgfile_basename, argv[++i], MAXPATHLEN-100); /* TODO buffer overruns */
113 printf("Using config file %s.\n", cfgfile_basename);
114 }
115 else if (!strcmp(argv[i], "-cdfile")) {
116 char isofilename[MAXPATHLEN];
117
118 if (i+1 >= argc) break;
119 strncpy(isofilename, argv[++i], MAXPATHLEN);
120 if (isofilename[0] != '/') {
121 getcwd(path, MAXPATHLEN);
122 if (strlen(path) + strlen(isofilename) + 1 < MAXPATHLEN) {
123 strcat(path, "/");
124 strcat(path, isofilename);
125 strcpy(isofilename, path);
126 } else
127 isofilename[0] = 0;
128 }
129
47bf65ab 130 cdfile = isofilename;
80c2304e 131 }
132 else if (!strcmp(argv[i], "-h") ||
133 !strcmp(argv[i], "-help") ||
134 !strcmp(argv[i], "--help")) {
135 printf(PACKAGE_NAME " " PACKAGE_VERSION "\n");
136 printf("%s\n", _(
137 " pcsx [options] [file]\n"
138 "\toptions:\n"
139 "\t-cdfile FILE\tRuns a CD image file\n"
140 "\t-nogui\t\tDon't open the GTK GUI\n"
141 "\t-cfg FILE\tLoads desired configuration file (default: ~/.pcsx/pcsx.cfg)\n"
142 "\t-psxout\t\tEnable PSX output\n"
143 "\t-load STATENUM\tLoads savestate STATENUM (1-5)\n"
144 "\t-h -help\tDisplay this message\n"
145 "\tfile\t\tLoads file\n"));
146 return 0;
147 } else {
148 strncpy(file, argv[i], MAXPATHLEN);
149 if (file[0] != '/') {
150 getcwd(path, MAXPATHLEN);
151 if (strlen(path) + strlen(file) + 1 < MAXPATHLEN) {
152 strcat(path, "/");
153 strcat(path, file);
154 strcpy(file, path);
155 } else
156 file[0] = 0;
157 }
158 }
159 }
160
161 memset(&Config, 0, sizeof(PcsxConfig));
162 strcpy(Config.Net, "Disabled");
163
164 CheckSubDir();
165// ScanAllPlugins();
166
7e400e1c 167 MAKE_PATH(Config.Mcd1, MEMCARD_DIR, "card1.mcd");
168 MAKE_PATH(Config.Mcd2, MEMCARD_DIR, "card2.mcd");
e906c010 169 strcpy(Config.Bios, "HLE");
170 strcpy(Config.BiosDir, "./");
171
172 strcpy(Config.PluginsDir, "plugins");
173 strcpy(Config.Gpu, "builtin_gpu");
174 strcpy(Config.Spu, "builtin_spu");
175 strcpy(Config.Cdr, "builtin_cdr");
176 strcpy(Config.Pad1, "builtin_pad");
177 strcpy(Config.Pad2, "builtin_pad");
4f3639fa 178 Config.PsxAuto = 1;
80c2304e 179
180 snprintf(Config.PatchesDir, sizeof(Config.PatchesDir), "." PATCHES_DIR);
181/*
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);
187*/
47bf65ab 188
189 if (cdfile)
190 set_cd_image(cdfile);
191
69af03a2 192 if (SysInit() == -1)
193 return 1;
80c2304e 194
69af03a2 195 // frontend stuff
196 in_init();
197 in_probe();
198 plat_init();
199 menu_init();
80c2304e 200
69af03a2 201 if (LoadPlugins() == -1) {
202 SysMessage("Failed loading plugins!");
203 return 1;
204 }
205 pcnt_hook_plugins();
80c2304e 206
69af03a2 207 if (OpenPlugins() == -1) {
208 return 1;
209 }
80c2304e 210
69af03a2 211 SysReset();
212 CheckCdrom();
213
214 if (file[0] != '\0') {
215 if (Load(file) != -1)
216 ready_to_go = 1;
217 } else {
47bf65ab 218 if (cdfile) {
69af03a2 219 if (LoadCdrom() == -1) {
220 ClosePlugins();
221 printf(_("Could not load CD-ROM!\n"));
222 return -1;
80c2304e 223 }
69af03a2 224 ready_to_go = 1;
80c2304e 225 }
69af03a2 226 }
80c2304e 227
69af03a2 228 // If a state has been specified, then load that
229 if (loadst) {
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);
235 }
80c2304e 236
3c70c47b 237 if (ready_to_go)
238 menu_prepare_emu();
239 else
69af03a2 240 menu_loop();
241
242 while (1)
243 {
80c2304e 244 psxCpu->Execute();
69af03a2 245 menu_loop();
80c2304e 246 }
247
248 return 0;
249}
250
251int SysInit() {
80c2304e 252 if (EmuInit() == -1) {
253 printf("PSX emulator couldn't be initialized.\n");
254 return -1;
255 }
256
257 LoadMcds(Config.Mcd1, Config.Mcd2); /* TODO Do we need to have this here, or in the calling main() function?? */
258
259 if (Config.Debug) {
260 StartDebugger();
261 }
262
263 return 0;
264}
265
266void SysRunGui() {
267 printf("SysRunGui\n");
268}
269
270void StartGui() {
271 printf("StartGui\n");
272}
273
274void SysReset() {
275 EmuReset();
bd6267e6 276
277 // hmh core forgets this
278 CDR_stop();
80c2304e 279}
280
281void SysClose() {
282 EmuShutdown();
283 ReleasePlugins();
284
285 StopDebugger();
286
287 if (emuLog != NULL) fclose(emuLog);
288}
289
290void SysUpdate() {
291 PADhandleKey(PAD1_keypressed());
292 PADhandleKey(PAD2_keypressed());
293}
294
295void UpdateMenuSlots() {
296}
297
298void OnFile_Exit() {
bd6267e6 299 printf("OnFile_Exit\n");
300 plat_finish();
301 SysClose();
80c2304e 302 exit(0);
303}
304
305void state_save(gchar *state_filename) {
306 char Text[MAXPATHLEN + 20];
307
308 GPU_updateLace();
309
310 if (SaveState(state_filename) == 0)
311 sprintf(Text, _("Saved state %s."), state_filename);
312 else
313 sprintf(Text, _("Error saving state %s!"), state_filename);
314
315 GPU_displayText(Text);
316}
317
318void state_load(gchar *state_filename) {
319 int ret;
320 char Text[MAXPATHLEN + 20];
321 FILE *fp;
322
323 // check if the state file actually exists
324 fp = fopen(state_filename, "rb");
325 if (fp == NULL) {
326 // file does not exist
327 return;
328 }
329
330 fclose(fp);
331
332 ret = CheckState(state_filename);
333
334 if (ret == 0) {
335 SysReset();
336 ret = LoadState(state_filename);
337 }
338
339 if (ret == 0) {
340 // Check the CD-ROM is valid
341 if (CheckCdrom() == -1) {
342 ClosePlugins();
343 SysRunGui();
344 return;
345 }
346
347 sprintf(Text, _("Loaded state %s."), state_filename);
348 } else {
349 sprintf(Text, _("Error loading state %s!"), state_filename);
350 }
351 GPU_displayText(Text);
352}
353
354char *get_state_filename(int i) {
355 char SStateFile[256];
356 char trimlabel[33];
357 int j;
358
359 strncpy(trimlabel, CdromLabel, 32);
360 trimlabel[32] = 0;
361 for (j = 31; j >= 0; j--)
362 if (trimlabel[j] == ' ')
363 trimlabel[j] = 0;
364 else
365 continue;
366
367 snprintf(SStateFile, sizeof(SStateFile), "." STATES_DIR "%.32s-%.9s.%3.3d",
368 trimlabel, CdromId, i);
369
370 return strdup(SStateFile);
371}
372
373void SysPrintf(const char *fmt, ...) {
374 va_list list;
375 char msg[512];
376
377 va_start(list, fmt);
378 vsprintf(msg, fmt, list);
379 va_end(list);
380
80c2304e 381 fprintf(emuLog, "%s", msg);
382}
383
384void SysMessage(const char *fmt, ...) {
385 va_list list;
386 char msg[512];
387
388 va_start(list, fmt);
389 vsprintf(msg, fmt, list);
390 va_end(list);
391
392 if (msg[strlen(msg) - 1] == '\n')
393 msg[strlen(msg) - 1] = 0;
394
395 fprintf(stderr, "%s\n", msg);
396}
397
e906c010 398#if 1
399/* this is to avoid having to hack every plugin to stop using $HOME */
400char *getenv(const char *name)
401{
402 static char ret[8] = ".";
403
69af03a2 404 if (name && strcmp(name, "HOME") == 0 &&
405 ((int)name >> 28) == 0) // HACK: let libs find home
b60f2812 406 return ret;
e906c010 407
b60f2812 408 return real_getenv(name);
e906c010 409}
410#endif
411
412/* we hook statically linked plugins here */
413static const char *builtin_plugins[] = {
47bf65ab 414 "builtin_gpu", "builtin_spu", "builtin_cdr", "builtin_pad",
415 "builtin_cdrcimg",
e906c010 416};
417
418static const int builtin_plugin_ids[] = {
419 PLUGIN_GPU, PLUGIN_SPU, PLUGIN_CDR, PLUGIN_PAD,
47bf65ab 420 PLUGIN_CDRCIMG,
e906c010 421};
422
80c2304e 423void *SysLoadLibrary(const char *lib) {
e906c010 424 const char *tmp = strrchr(lib, '/');
425 int i;
426
427 printf("dlopen %s\n", lib);
428 if (tmp != NULL) {
429 tmp++;
430 for (i = 0; i < ARRAY_SIZE(builtin_plugins); i++)
431 if (strcmp(tmp, builtin_plugins[i]) == 0)
432 return (void *)(long)(PLUGIN_DL_BASE + builtin_plugin_ids[i]);
433 }
434
80c2304e 435 return dlopen(lib, RTLD_NOW);
436}
437
438void *SysLoadSym(void *lib, const char *sym) {
e906c010 439 unsigned int plugid = (unsigned int)(long)lib;
440
441 if (PLUGIN_DL_BASE <= plugid && plugid < PLUGIN_DL_BASE + ARRAY_SIZE(builtin_plugins))
442 return plugin_link(plugid - PLUGIN_DL_BASE, sym);
443
80c2304e 444 return dlsym(lib, sym);
445}
446
447const char *SysLibError() {
448 return dlerror();
449}
450
451void SysCloseLibrary(void *lib) {
e906c010 452 unsigned int plugid = (unsigned int)(long)lib;
453
454 if (PLUGIN_DL_BASE <= plugid && plugid < PLUGIN_DL_BASE + ARRAY_SIZE(builtin_plugins))
455 return;
456
80c2304e 457 dlclose(lib);
458}
459
460