drc: use correct clear size
[pcsx_rearmed.git] / frontend / main.c
CommitLineData
e906c010 1/*
201c21e2 2 * (C) notaz, 2010-2011
e906c010 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>
c5061935 15#include <signal.h>
80c2304e 16
c5061935 17#include "main.h"
e906c010 18#include "plugin.h"
14dffdb7 19#include "pcnt.h"
3c70c47b 20#include "menu.h"
80c2304e 21#include "../libpcsxcore/misc.h"
47bf65ab 22#include "../plugins/cdrcimg/cdrcimg.h"
69af03a2 23#include "common/plat.h"
24#include "common/input.h"
80c2304e 25
69af03a2 26int ready_to_go;
c5061935 27unsigned long gpuDisp;
28char cfgfile_basename[MAXPATHLEN];
b60f2812 29static char *(*real_getenv)(const char *name);
80c2304e 30
31static void make_path(char *buf, size_t size, const char *dir, const char *fname)
32{
33 if (fname)
34 snprintf(buf, size, ".%s%s", dir, fname);
35 else
36 snprintf(buf, size, ".%s", dir);
37}
38#define MAKE_PATH(buf, dir, fname) \
39 make_path(buf, sizeof(buf), dir, fname)
40
41static void create_profile_dir(const char *directory) {
42 char path[MAXPATHLEN];
43
44 MAKE_PATH(path, directory, NULL);
45 mkdir(path, S_IRWXU | S_IRWXG);
46}
47
48static void CheckSubDir() {
49 // make sure that ~/.pcsx exists
50 create_profile_dir(PCSX_DOT_DIR);
51
52 create_profile_dir(BIOS_DIR);
53 create_profile_dir(MEMCARD_DIR);
54 create_profile_dir(STATES_DIR);
55 create_profile_dir(PLUGINS_DIR);
56 create_profile_dir(PLUGINS_CFG_DIR);
57 create_profile_dir(CHEATS_DIR);
58 create_profile_dir(PATCHES_DIR);
cd6e8d0f 59 create_profile_dir(PCSX_DOT_DIR "cfg");
80c2304e 60}
61
47bf65ab 62void set_cd_image(const char *fname)
63{
e16a7e51 64 const char *ext = NULL;
47bf65ab 65
e16a7e51 66 if (fname != NULL) {
67 int len = strlen(fname);
68 ext = fname;
69 if (len > 2)
70 ext = fname + len - 2;
71 }
47bf65ab 72
e16a7e51 73 if (ext && strcasecmp(ext, ".z") == 0) {
47bf65ab 74 SetIsoFile(NULL);
75 cdrcimg_set_fname(fname);
76 strcpy(Config.Cdr, "builtin_cdrcimg");
77 } else {
78 SetIsoFile(fname);
79 strcpy(Config.Cdr, "builtin_cdr");
80 }
81}
82
80c2304e 83int main(int argc, char *argv[])
84{
85 char file[MAXPATHLEN] = "";
86 char path[MAXPATHLEN];
47bf65ab 87 const char *cdfile = NULL;
80c2304e 88 int loadst = 0;
b60f2812 89 void *tmp;
80c2304e 90 int i;
91
b60f2812 92 tmp = dlopen("/lib/libdl.so.2", RTLD_LAZY);
fa9cfe0a 93 if (tmp == NULL)
94 tmp = dlopen("/lib32/libdl.so.2", RTLD_LAZY);
b60f2812 95 if (tmp != NULL)
96 real_getenv = dlsym(tmp, "getenv");
97 if (real_getenv == NULL) {
98 fprintf(stderr, "%s\n", dlerror());
99 return 1;
100 }
101 dlclose(tmp);
102
80c2304e 103 // what is the name of the config file?
104 // it may be redefined by -cfg on the command line
105 strcpy(cfgfile_basename, "pcsx.cfg");
106
e906c010 107 emuLog = stdout;
80c2304e 108 SetIsoFile(NULL);
80c2304e 109
110 // read command line options
111 for (i = 1; i < argc; i++) {
e906c010 112 if (!strcmp(argv[i], "-psxout")) Config.PsxOut = 1;
80c2304e 113 else if (!strcmp(argv[i], "-load")) loadst = atol(argv[++i]);
114 else if (!strcmp(argv[i], "-cfg")) {
115 if (i+1 >= argc) break;
116 strncpy(cfgfile_basename, argv[++i], MAXPATHLEN-100); /* TODO buffer overruns */
117 printf("Using config file %s.\n", cfgfile_basename);
118 }
119 else if (!strcmp(argv[i], "-cdfile")) {
120 char isofilename[MAXPATHLEN];
121
122 if (i+1 >= argc) break;
123 strncpy(isofilename, argv[++i], MAXPATHLEN);
124 if (isofilename[0] != '/') {
125 getcwd(path, MAXPATHLEN);
126 if (strlen(path) + strlen(isofilename) + 1 < MAXPATHLEN) {
127 strcat(path, "/");
128 strcat(path, isofilename);
129 strcpy(isofilename, path);
130 } else
131 isofilename[0] = 0;
132 }
133
47bf65ab 134 cdfile = isofilename;
80c2304e 135 }
136 else if (!strcmp(argv[i], "-h") ||
137 !strcmp(argv[i], "-help") ||
138 !strcmp(argv[i], "--help")) {
139 printf(PACKAGE_NAME " " PACKAGE_VERSION "\n");
140 printf("%s\n", _(
141 " pcsx [options] [file]\n"
142 "\toptions:\n"
143 "\t-cdfile FILE\tRuns a CD image file\n"
144 "\t-nogui\t\tDon't open the GTK GUI\n"
145 "\t-cfg FILE\tLoads desired configuration file (default: ~/.pcsx/pcsx.cfg)\n"
146 "\t-psxout\t\tEnable PSX output\n"
147 "\t-load STATENUM\tLoads savestate STATENUM (1-5)\n"
148 "\t-h -help\tDisplay this message\n"
149 "\tfile\t\tLoads file\n"));
150 return 0;
151 } else {
152 strncpy(file, argv[i], MAXPATHLEN);
153 if (file[0] != '/') {
154 getcwd(path, MAXPATHLEN);
155 if (strlen(path) + strlen(file) + 1 < MAXPATHLEN) {
156 strcat(path, "/");
157 strcat(path, file);
158 strcpy(file, path);
159 } else
160 file[0] = 0;
161 }
162 }
163 }
164
165 memset(&Config, 0, sizeof(PcsxConfig));
166 strcpy(Config.Net, "Disabled");
167
168 CheckSubDir();
80c2304e 169
7e400e1c 170 MAKE_PATH(Config.Mcd1, MEMCARD_DIR, "card1.mcd");
171 MAKE_PATH(Config.Mcd2, MEMCARD_DIR, "card2.mcd");
e906c010 172 strcpy(Config.Bios, "HLE");
e6eb2066 173 strcpy(Config.BiosDir, "bios");
e906c010 174
175 strcpy(Config.PluginsDir, "plugins");
176 strcpy(Config.Gpu, "builtin_gpu");
177 strcpy(Config.Spu, "builtin_spu");
178 strcpy(Config.Cdr, "builtin_cdr");
179 strcpy(Config.Pad1, "builtin_pad");
180 strcpy(Config.Pad2, "builtin_pad");
4f3639fa 181 Config.PsxAuto = 1;
80c2304e 182
183 snprintf(Config.PatchesDir, sizeof(Config.PatchesDir), "." PATCHES_DIR);
184/*
185 // switch to plugin dotdir
186 // this lets plugins work without modification!
187 gchar *plugin_default_dir = g_build_filename(getenv("HOME"), PLUGINS_DIR, NULL);
188 chdir(plugin_default_dir);
189 g_free(plugin_default_dir);
190*/
47bf65ab 191
192 if (cdfile)
193 set_cd_image(cdfile);
194
69af03a2 195 if (SysInit() == -1)
196 return 1;
80c2304e 197
69af03a2 198 // frontend stuff
199 in_init();
200 in_probe();
201 plat_init();
202 menu_init();
80c2304e 203
69af03a2 204 if (LoadPlugins() == -1) {
205 SysMessage("Failed loading plugins!");
206 return 1;
207 }
208 pcnt_hook_plugins();
80c2304e 209
69af03a2 210 if (OpenPlugins() == -1) {
211 return 1;
212 }
201c21e2 213 plugin_call_rearmed_cbs();
80c2304e 214
69af03a2 215 CheckCdrom();
bbd837c6 216 SysReset();
69af03a2 217
218 if (file[0] != '\0') {
219 if (Load(file) != -1)
220 ready_to_go = 1;
221 } else {
47bf65ab 222 if (cdfile) {
69af03a2 223 if (LoadCdrom() == -1) {
224 ClosePlugins();
225 printf(_("Could not load CD-ROM!\n"));
226 return -1;
80c2304e 227 }
69af03a2 228 ready_to_go = 1;
80c2304e 229 }
69af03a2 230 }
80c2304e 231
69af03a2 232 // If a state has been specified, then load that
233 if (loadst) {
c5061935 234 char state_filename[MAXPATHLEN];
235 int ret = get_state_filename(state_filename, sizeof(state_filename), loadst - 1);
236 if (ret == 0)
237 ret = LoadState(state_filename);
69af03a2 238 printf("%s state %s\n", ret ? "failed to load" : "loaded", state_filename);
69af03a2 239 }
80c2304e 240
3c70c47b 241 if (ready_to_go)
242 menu_prepare_emu();
243 else
69af03a2 244 menu_loop();
245
246 while (1)
247 {
80c2304e 248 psxCpu->Execute();
69af03a2 249 menu_loop();
80c2304e 250 }
251
252 return 0;
253}
254
255int SysInit() {
80c2304e 256 if (EmuInit() == -1) {
257 printf("PSX emulator couldn't be initialized.\n");
258 return -1;
259 }
260
261 LoadMcds(Config.Mcd1, Config.Mcd2); /* TODO Do we need to have this here, or in the calling main() function?? */
262
263 if (Config.Debug) {
264 StartDebugger();
265 }
266
267 return 0;
268}
269
270void SysRunGui() {
271 printf("SysRunGui\n");
272}
273
274void StartGui() {
275 printf("StartGui\n");
276}
277
e6eb2066 278static void dummy_lace()
279{
280}
281
80c2304e 282void SysReset() {
e6eb2066 283 // rearmed hack: EmuReset() runs some code when real BIOS is used,
284 // but we usually do reset from menu while GPU is not open yet,
285 // so we need to prevent updateLace() call..
286 void *real_lace = GPU_updateLace;
287 GPU_updateLace = dummy_lace;
288
80c2304e 289 EmuReset();
bd6267e6 290
291 // hmh core forgets this
292 CDR_stop();
e6eb2066 293
294 GPU_updateLace = real_lace;
80c2304e 295}
296
297void SysClose() {
298 EmuShutdown();
299 ReleasePlugins();
300
301 StopDebugger();
302
303 if (emuLog != NULL) fclose(emuLog);
304}
305
306void SysUpdate() {
80c2304e 307}
308
309void OnFile_Exit() {
bd6267e6 310 printf("OnFile_Exit\n");
201c21e2 311 menu_finish();
bd6267e6 312 plat_finish();
313 SysClose();
80c2304e 314 exit(0);
315}
316
c5061935 317int get_state_filename(char *buf, int size, int i) {
80c2304e 318 char trimlabel[33];
319 int j;
320
321 strncpy(trimlabel, CdromLabel, 32);
322 trimlabel[32] = 0;
323 for (j = 31; j >= 0; j--)
324 if (trimlabel[j] == ' ')
325 trimlabel[j] = 0;
326 else
327 continue;
328
c5061935 329 snprintf(buf, size, "." STATES_DIR "%.32s-%.9s.%3.3d",
80c2304e 330 trimlabel, CdromId, i);
331
c5061935 332 return 0;
80c2304e 333}
334
335void SysPrintf(const char *fmt, ...) {
336 va_list list;
337 char msg[512];
338
339 va_start(list, fmt);
340 vsprintf(msg, fmt, list);
341 va_end(list);
342
80c2304e 343 fprintf(emuLog, "%s", msg);
344}
345
346void SysMessage(const char *fmt, ...) {
347 va_list list;
348 char msg[512];
349
350 va_start(list, fmt);
351 vsprintf(msg, fmt, list);
352 va_end(list);
353
354 if (msg[strlen(msg) - 1] == '\n')
355 msg[strlen(msg) - 1] = 0;
356
357 fprintf(stderr, "%s\n", msg);
358}
359
c5061935 360static void SignalExit(int sig) {
361 ClosePlugins();
362 OnFile_Exit();
363}
364
365#define PARSEPATH(dst, src) \
366 ptr = src + strlen(src); \
367 while (*ptr != '\\' && ptr != src) ptr--; \
368 if (ptr != src) { \
369 strcpy(dst, ptr+1); \
370 }
371
372static int _OpenPlugins(void) {
373 int ret;
374
375 signal(SIGINT, SignalExit);
376 signal(SIGPIPE, SignalExit);
377
378 GPU_clearDynarec(clearDynarec);
379
380 ret = CDR_open();
381 if (ret < 0) { SysMessage(_("Error opening CD-ROM plugin!")); return -1; }
382 ret = SPU_open();
383 if (ret < 0) { SysMessage(_("Error opening SPU plugin!")); return -1; }
384 SPU_registerCallback(SPUirq);
385 // pcsx-rearmed: we handle gpu elsewhere
386 //ret = GPU_open(&gpuDisp, "PCSX", NULL);
387 //if (ret < 0) { SysMessage(_("Error opening GPU plugin!")); return -1; }
388 ret = PAD1_open(&gpuDisp);
389 if (ret < 0) { SysMessage(_("Error opening Controller 1 plugin!")); return -1; }
390 ret = PAD2_open(&gpuDisp);
391 if (ret < 0) { SysMessage(_("Error opening Controller 2 plugin!")); return -1; }
392
393 if (Config.UseNet && !NetOpened) {
394 netInfo info;
395 char path[MAXPATHLEN];
396 char dotdir[MAXPATHLEN];
397
398 MAKE_PATH(dotdir, "/.pcsx/plugins/", NULL);
399
400 strcpy(info.EmuName, "PCSX " PACKAGE_VERSION);
401 strncpy(info.CdromID, CdromId, 9);
402 strncpy(info.CdromLabel, CdromLabel, 9);
403 info.psxMem = psxM;
404 info.GPU_showScreenPic = GPU_showScreenPic;
405 info.GPU_displayText = GPU_displayText;
406 info.GPU_showScreenPic = GPU_showScreenPic;
407 info.PAD_setSensitive = PAD1_setSensitive;
408 sprintf(path, "%s%s", Config.BiosDir, Config.Bios);
409 strcpy(info.BIOSpath, path);
410 strcpy(info.MCD1path, Config.Mcd1);
411 strcpy(info.MCD2path, Config.Mcd2);
412 sprintf(path, "%s%s", dotdir, Config.Gpu);
413 strcpy(info.GPUpath, path);
414 sprintf(path, "%s%s", dotdir, Config.Spu);
415 strcpy(info.SPUpath, path);
416 sprintf(path, "%s%s", dotdir, Config.Cdr);
417 strcpy(info.CDRpath, path);
418 NET_setInfo(&info);
419
420 ret = NET_open(&gpuDisp);
421 if (ret < 0) {
422 if (ret == -2) {
423 // -2 is returned when something in the info
424 // changed and needs to be synced
425 char *ptr;
426
427 PARSEPATH(Config.Bios, info.BIOSpath);
428 PARSEPATH(Config.Gpu, info.GPUpath);
429 PARSEPATH(Config.Spu, info.SPUpath);
430 PARSEPATH(Config.Cdr, info.CDRpath);
431
432 strcpy(Config.Mcd1, info.MCD1path);
433 strcpy(Config.Mcd2, info.MCD2path);
434 return -2;
435 } else {
436 Config.UseNet = FALSE;
437 }
438 } else {
439 if (NET_queryPlayer() == 1) {
440 if (SendPcsxInfo() == -1) Config.UseNet = FALSE;
441 } else {
442 if (RecvPcsxInfo() == -1) Config.UseNet = FALSE;
443 }
444 }
445 NetOpened = TRUE;
446 } else if (Config.UseNet) {
447 NET_resume();
448 }
449
450 return 0;
451}
452
453int OpenPlugins() {
454 int ret;
455
456 while ((ret = _OpenPlugins()) == -2) {
457 ReleasePlugins();
458 LoadMcds(Config.Mcd1, Config.Mcd2);
459 if (LoadPlugins() == -1) return -1;
460 }
461 return ret;
462}
463
464void ClosePlugins() {
465 int ret;
466
467 signal(SIGINT, SIG_DFL);
468 signal(SIGPIPE, SIG_DFL);
469 ret = CDR_close();
470 if (ret < 0) { SysMessage(_("Error closing CD-ROM plugin!")); return; }
471 ret = SPU_close();
472 if (ret < 0) { SysMessage(_("Error closing SPU plugin!")); return; }
473 ret = PAD1_close();
474 if (ret < 0) { SysMessage(_("Error closing Controller 1 Plugin!")); return; }
475 ret = PAD2_close();
476 if (ret < 0) { SysMessage(_("Error closing Controller 2 plugin!")); return; }
477 // pcsx-rearmed: we handle gpu elsewhere
478 //ret = GPU_close();
479 //if (ret < 0) { SysMessage(_("Error closing GPU plugin!")); return; }
480
481 if (Config.UseNet) {
482 NET_pause();
483 }
484}
485
e906c010 486#if 1
487/* this is to avoid having to hack every plugin to stop using $HOME */
488char *getenv(const char *name)
489{
490 static char ret[8] = ".";
491
69af03a2 492 if (name && strcmp(name, "HOME") == 0 &&
493 ((int)name >> 28) == 0) // HACK: let libs find home
b60f2812 494 return ret;
e906c010 495
b60f2812 496 return real_getenv(name);
e906c010 497}
498#endif
499
500/* we hook statically linked plugins here */
501static const char *builtin_plugins[] = {
47bf65ab 502 "builtin_gpu", "builtin_spu", "builtin_cdr", "builtin_pad",
503 "builtin_cdrcimg",
e906c010 504};
505
506static const int builtin_plugin_ids[] = {
507 PLUGIN_GPU, PLUGIN_SPU, PLUGIN_CDR, PLUGIN_PAD,
47bf65ab 508 PLUGIN_CDRCIMG,
e906c010 509};
510
80c2304e 511void *SysLoadLibrary(const char *lib) {
e906c010 512 const char *tmp = strrchr(lib, '/');
bbd837c6 513 void *ret;
e906c010 514 int i;
515
bbd837c6 516 printf("plugin: %s\n", lib);
517
e906c010 518 if (tmp != NULL) {
519 tmp++;
520 for (i = 0; i < ARRAY_SIZE(builtin_plugins); i++)
521 if (strcmp(tmp, builtin_plugins[i]) == 0)
522 return (void *)(long)(PLUGIN_DL_BASE + builtin_plugin_ids[i]);
523 }
524
2185e39b 525#if defined(__x86_64__) || defined(__i386__)
526 // convenience hack
527 char name[MAXPATHLEN];
528 snprintf(name, sizeof(name), "%s.x86", lib);
529 lib = name;
530#endif
531
bbd837c6 532 ret = dlopen(lib, RTLD_NOW);
533 if (ret == NULL)
534 fprintf(stderr, "dlopen: %s\n", dlerror());
535 return ret;
80c2304e 536}
537
538void *SysLoadSym(void *lib, const char *sym) {
e906c010 539 unsigned int plugid = (unsigned int)(long)lib;
540
541 if (PLUGIN_DL_BASE <= plugid && plugid < PLUGIN_DL_BASE + ARRAY_SIZE(builtin_plugins))
542 return plugin_link(plugid - PLUGIN_DL_BASE, sym);
543
80c2304e 544 return dlsym(lib, sym);
545}
546
547const char *SysLibError() {
548 return dlerror();
549}
550
551void SysCloseLibrary(void *lib) {
e906c010 552 unsigned int plugid = (unsigned int)(long)lib;
553
554 if (PLUGIN_DL_BASE <= plugid && plugid < PLUGIN_DL_BASE + ARRAY_SIZE(builtin_plugins))
555 return;
556
80c2304e 557 dlclose(lib);
558}
559