frontend: avoid loading msg when not needed
[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>
29a8c4f3 16#include <time.h>
80c2304e 17
c5061935 18#include "main.h"
e906c010 19#include "plugin.h"
7c0f51de 20#include "plugin_lib.h"
14dffdb7 21#include "pcnt.h"
3c70c47b 22#include "menu.h"
47821672 23#include "plat.h"
80c2304e 24#include "../libpcsxcore/misc.h"
9c27c205 25#include "../libpcsxcore/cheat.h"
799b0b87 26#include "../libpcsxcore/new_dynarec/new_dynarec.h"
47bf65ab 27#include "../plugins/cdrcimg/cdrcimg.h"
38c2028e 28#include "revision.h"
80c2304e 29
cc56203b 30#ifndef NO_FRONTEND
31#include "libpicofe/input.h"
32#include "libpicofe/plat.h"
33#include "libpicofe/readpng.h"
34#endif
a1b44e36 35#ifndef BOOT_MSG
36#define BOOT_MSG "Booting up..."
37#endif
cc56203b 38
fc8145b7 39// don't include debug.h - it breaks ARM build (R1 redefined)
40void StartDebugger();
41void StopDebugger();
42
33400707 43// sound plugin
44extern int iUseReverb;
45extern int iUseInterpolation;
46extern int iXAPitch;
33400707 47extern int iVolume;
48
69af03a2 49int ready_to_go;
c5061935 50unsigned long gpuDisp;
51char cfgfile_basename[MAXPATHLEN];
8f892648 52int state_slot;
53enum sched_action emu_action, emu_action_old;
54char hud_msg[64];
55int hud_new_msg;
56
456f1b86 57static void toggle_fast_forward(int force_off);
58
80c2304e 59static void make_path(char *buf, size_t size, const char *dir, const char *fname)
60{
61 if (fname)
62 snprintf(buf, size, ".%s%s", dir, fname);
63 else
64 snprintf(buf, size, ".%s", dir);
65}
66#define MAKE_PATH(buf, dir, fname) \
67 make_path(buf, sizeof(buf), dir, fname)
68
69static void create_profile_dir(const char *directory) {
70 char path[MAXPATHLEN];
71
72 MAKE_PATH(path, directory, NULL);
73 mkdir(path, S_IRWXU | S_IRWXG);
74}
75
76static void CheckSubDir() {
77 // make sure that ~/.pcsx exists
78 create_profile_dir(PCSX_DOT_DIR);
79
80 create_profile_dir(BIOS_DIR);
81 create_profile_dir(MEMCARD_DIR);
82 create_profile_dir(STATES_DIR);
83 create_profile_dir(PLUGINS_DIR);
84 create_profile_dir(PLUGINS_CFG_DIR);
85 create_profile_dir(CHEATS_DIR);
86 create_profile_dir(PATCHES_DIR);
cd6e8d0f 87 create_profile_dir(PCSX_DOT_DIR "cfg");
29a8c4f3 88 create_profile_dir("/screenshots/");
89}
90
91static int get_gameid_filename(char *buf, int size, const char *fmt, int i) {
92 char trimlabel[33];
93 int j;
94
95 strncpy(trimlabel, CdromLabel, 32);
96 trimlabel[32] = 0;
97 for (j = 31; j >= 0; j--)
98 if (trimlabel[j] == ' ')
99 trimlabel[j] = 0;
100 else
101 continue;
102
103 snprintf(buf, size, fmt, trimlabel, CdromId, i);
104
105 return 0;
80c2304e 106}
107
47bf65ab 108void set_cd_image(const char *fname)
109{
e16a7e51 110 const char *ext = NULL;
47bf65ab 111
33716956 112 if (fname != NULL)
113 ext = strrchr(fname, '.');
47bf65ab 114
33716956 115 if (ext && (
116 strcasecmp(ext, ".z") == 0 || strcasecmp(ext, ".bz") == 0 ||
9a92bffb 117 strcasecmp(ext, ".znx") == 0 /*|| strcasecmp(ext, ".pbp") == 0*/)) {
47bf65ab 118 SetIsoFile(NULL);
119 cdrcimg_set_fname(fname);
120 strcpy(Config.Cdr, "builtin_cdrcimg");
121 } else {
122 SetIsoFile(fname);
123 strcpy(Config.Cdr, "builtin_cdr");
124 }
125}
126
6fe1f056 127static void set_default_paths(void)
128{
129 MAKE_PATH(Config.Mcd1, MEMCARD_DIR, "card1.mcd");
130 MAKE_PATH(Config.Mcd2, MEMCARD_DIR, "card2.mcd");
131 strcpy(Config.BiosDir, "bios");
132
133 strcpy(Config.PluginsDir, "plugins");
134 strcpy(Config.Gpu, "builtin_gpu");
135 strcpy(Config.Spu, "builtin_spu");
136 strcpy(Config.Cdr, "builtin_cdr");
137 strcpy(Config.Pad1, "builtin_pad");
138 strcpy(Config.Pad2, "builtin_pad");
139 strcpy(Config.Net, "Disabled");
6fe1f056 140
141 snprintf(Config.PatchesDir, sizeof(Config.PatchesDir), "." PATCHES_DIR);
142}
143
33400707 144void emu_set_default_config(void)
145{
146 // try to set sane config on which most games work
147 Config.Xa = Config.Cdda = Config.Sio =
148 Config.SpuIrq = Config.RCntFix = Config.VSyncWA = 0;
149 Config.CdrReschedule = 0;
150 Config.PsxAuto = 1;
151
746fee51 152 pl_rearmed_cbs.gpu_neon.allow_interlace = 2; // auto
0b02eb77 153 pl_rearmed_cbs.gpu_neon.enhancement_enable =
154 pl_rearmed_cbs.gpu_neon.enhancement_no_main = 0;
33400707 155 pl_rearmed_cbs.gpu_peops.iUseDither = 0;
156 pl_rearmed_cbs.gpu_peops.dwActFixes = 1<<7;
157 pl_rearmed_cbs.gpu_unai.abe_hack =
158 pl_rearmed_cbs.gpu_unai.no_light =
159 pl_rearmed_cbs.gpu_unai.no_blend = 0;
e60da159 160 memset(&pl_rearmed_cbs.gpu_peopsgl, 0, sizeof(pl_rearmed_cbs.gpu_peopsgl));
746fee51 161 pl_rearmed_cbs.gpu_peopsgl.iVRamSize = 64;
cbb26048 162 pl_rearmed_cbs.gpu_peopsgl.iTexGarbageCollection = 1;
33400707 163
164 iUseReverb = 2;
165 iUseInterpolation = 1;
166 iXAPitch = 0;
33400707 167 iVolume = 768;
168#ifndef __ARM_ARCH_7A__ /* XXX */
169 iUseReverb = 0;
170 iUseInterpolation = 0;
171#endif
172 new_dynarec_hacks = 0;
173 cycle_multiplier = 200;
174
175 in_type1 = PSE_PAD_TYPE_STANDARD;
176 in_type2 = PSE_PAD_TYPE_STANDARD;
177}
178
51f77282 179static void check_memcards(void)
180{
181 char buf[MAXPATHLEN];
182 FILE *f;
183 int i;
184
185 for (i = 1; i <= 9; i++) {
186 snprintf(buf, sizeof(buf), ".%scard%d.mcd", MEMCARD_DIR, i);
187
188 f = fopen(buf, "rb");
189 if (f == NULL) {
190 printf("Creating memcard: %s\n", buf);
191 CreateMcd(buf);
192 }
193 else
194 fclose(f);
195 }
196}
197
8f892648 198void do_emu_action(void)
199{
200 int ret;
201
202 emu_action_old = emu_action;
203
204 switch (emu_action) {
8f892648 205 case SACTION_LOAD_STATE:
206 ret = emu_load_state(state_slot);
207 snprintf(hud_msg, sizeof(hud_msg), ret == 0 ? "LOADED" : "FAIL!");
208 break;
209 case SACTION_SAVE_STATE:
210 ret = emu_save_state(state_slot);
211 snprintf(hud_msg, sizeof(hud_msg), ret == 0 ? "SAVED" : "FAIL!");
212 break;
38c2028e 213#ifndef NO_FRONTEND
214 case SACTION_ENTER_MENU:
456f1b86 215 toggle_fast_forward(1);
38c2028e 216 menu_loop();
217 return;
8f892648 218 case SACTION_NEXT_SSLOT:
219 state_slot++;
220 if (state_slot > 9)
221 state_slot = 0;
222 goto do_state_slot;
223 case SACTION_PREV_SSLOT:
224 state_slot--;
225 if (state_slot < 0)
226 state_slot = 9;
38c2028e 227do_state_slot:
228 snprintf(hud_msg, sizeof(hud_msg), "STATE SLOT %d [%s]", state_slot,
229 emu_check_state(state_slot) == 0 ? "USED" : "FREE");
230 hud_new_msg = 3;
231 printf("* %s\n", hud_msg);
232 break;
8f892648 233 case SACTION_TOGGLE_FSKIP:
ea4a16e7 234 pl_rearmed_cbs.fskip_advice = 0;
235 pl_rearmed_cbs.frameskip++;
236 if (pl_rearmed_cbs.frameskip > 1)
237 pl_rearmed_cbs.frameskip = -1;
238 snprintf(hud_msg, sizeof(hud_msg), "FRAMESKIP: %s",
239 pl_rearmed_cbs.frameskip == -1 ? "AUTO" :
240 pl_rearmed_cbs.frameskip == 0 ? "OFF" : "1" );
241 plugin_call_rearmed_cbs();
8f892648 242 break;
fa56d360 243 case SACTION_SWITCH_DISPMODE:
244 pl_switch_dispmode();
d81b8e97 245 plugin_call_rearmed_cbs();
fa56d360 246 if (GPU_open != NULL && GPU_close != NULL) {
247 GPU_close();
248 GPU_open(&gpuDisp, "PCSX", NULL);
249 }
d81b8e97 250 break;
456f1b86 251 case SACTION_FAST_FORWARD:
252 toggle_fast_forward(0);
253 plugin_call_rearmed_cbs();
254 break;
29a8c4f3 255 case SACTION_SCREENSHOT:
256 {
38c2028e 257 char buf[MAXPATHLEN];
29a8c4f3 258 void *scrbuf;
259 int w, h, bpp;
260 time_t t = time(NULL);
261 struct tm *tb = localtime(&t);
262 int ti = tb->tm_yday * 1000000 + tb->tm_hour * 10000 +
263 tb->tm_min * 100 + tb->tm_sec;
264
265 scrbuf = pl_prepare_screenshot(&w, &h, &bpp);
266 get_gameid_filename(buf, sizeof(buf),
267 "screenshots/%.32s-%.9s.%d.png", ti);
268 ret = -1;
269 if (scrbuf != 0 && bpp == 16)
270 ret = writepng(buf, scrbuf, w, h);
271 if (ret == 0)
272 snprintf(hud_msg, sizeof(hud_msg), "SCREENSHOT TAKEN");
273 break;
274 }
221be40d 275 case SACTION_VOLUME_UP:
276 case SACTION_VOLUME_DOWN:
cc56203b 277 plat_target_step_volume(emu_action == SACTION_VOLUME_UP);
221be40d 278 return;
a805c855 279 case SACTION_MINIMIZE:
36dfb787 280 if (GPU_close != NULL)
281 GPU_close();
282
a805c855 283 plat_minimize();
36dfb787 284
285 if (GPU_open != NULL) {
286 ret = GPU_open(&gpuDisp, "PCSX", NULL);
287 if (ret)
288 fprintf(stderr, "GPU_open returned %d\n", ret);
289 }
a805c855 290 return;
38c2028e 291#endif
4c08b9e7 292 default:
293 return;
8f892648 294 }
8f892648 295
8f892648 296 hud_new_msg = 3;
297}
298
9c27c205 299static int cdidcmp(const char *id1, const char *id2)
300{
301 while (*id1 != 0 && *id2 != 0) {
302 if (*id1 == '_') { id1++; continue; }
303 if (*id2 == '_') { id2++; continue; }
304 if (*id1 != *id2)
305 break;
306 id1++;
307 id2++;
308 }
309
310 return *id1 - *id2;
311}
312
313static void parse_cwcheat(void)
314{
315 char line[256], buf[64], name[64], *p;
316 int newcheat = 1;
317 u32 a, v;
318 FILE *f;
319
320 f = fopen("cheatpops.db", "r");
321 if (f == NULL)
322 return;
323
324 /* find the game */
325 while (fgets(line, sizeof(line), f)) {
326 if (sscanf(line, "_S %63s", buf) != 1)
327 continue;
328 if (cdidcmp(buf, CdromId) == 0)
329 break;
330 }
331
332 if (feof(f))
333 goto out;
334
335 printf("cwcheat section found for %s\n", CdromId);
336 while (fgets(line, sizeof(line), f))
337 {
338 p = line + strlen(line);
339 for (p--; p >= line && (*p == '\r' || *p == '\n' || *p == ' '); p--)
340 *p = 0;
341 if (*p == 0 || *p == '#' || *p == ';')
342 continue;
343
344 if (strncmp(line, "_S", 2) == 0)
345 break;
346 if (strncmp(line, "_G", 2) == 0) {
347 printf(" cwcheat game name: '%s'\n", line + 3);
348 continue;
349 }
350 if (strncmp(line, "_C0", 3) == 0) {
351 if (!newcheat && Cheats[NumCheats - 1].n == 0) {
352 printf("cheat '%s' failed to parse\n", name);
353 free(Cheats[NumCheats - 1].Descr);
354 NumCheats--;
355 }
356 snprintf(name, sizeof(name), "%s", line + 4);
357 newcheat = 1;
358 continue;
359 }
360 if (sscanf(line, "_L %x %x", &a, &v) != 2) {
361 printf("line failed to parse: '%s'\n", line);
362 continue;
363 }
364
365 if (newcheat) {
366 if (NumCheats >= NumCheatsAllocated) {
367 NumCheatsAllocated += 16;
368 Cheats = realloc(Cheats, sizeof(Cheat) *
369 NumCheatsAllocated);
370 if (Cheats == NULL)
371 break;
372 }
373 Cheats[NumCheats].Descr = strdup(name);
374 Cheats[NumCheats].Enabled = 0;
2c843d96 375 Cheats[NumCheats].WasEnabled = 0;
9c27c205 376 Cheats[NumCheats].First = NumCodes;
377 Cheats[NumCheats].n = 0;
378 NumCheats++;
379 newcheat = 0;
380 }
381
382 if (NumCodes >= NumCodesAllocated) {
383 NumCodesAllocated += 16;
384 CheatCodes = realloc(CheatCodes, sizeof(CheatCode) *
385 NumCodesAllocated);
386 if (CheatCodes == NULL)
387 break;
388 }
389 CheatCodes[NumCodes].Addr = a;
390 CheatCodes[NumCodes].Val = v;
391 NumCodes++;
392 Cheats[NumCheats - 1].n++;
393 }
394
395out:
396 fclose(f);
397}
398
79f216e3 399void emu_on_new_cd(int show_hud_msg)
9c27c205 400{
401 ClearAllCheats();
402 parse_cwcheat();
403
404 if (Config.HLE) {
405 printf("note: running with HLE BIOS, expect compatibility problems\n");
406 printf("----------------------------------------------------------\n");
407 }
408
79f216e3 409 if (show_hud_msg) {
410 snprintf(hud_msg, sizeof(hud_msg), BOOT_MSG);
411 hud_new_msg = 3;
412 }
9c27c205 413}
414
38c2028e 415int emu_core_preinit(void)
80c2304e 416{
80c2304e 417 // what is the name of the config file?
418 // it may be redefined by -cfg on the command line
419 strcpy(cfgfile_basename, "pcsx.cfg");
420
e906c010 421 emuLog = stdout;
80c2304e 422 SetIsoFile(NULL);
80c2304e 423
e0c692d9 424 memset(&Config, 0, sizeof(Config));
425
e0c692d9 426 set_default_paths();
33400707 427 emu_set_default_config();
e0c692d9 428 strcpy(Config.Bios, "HLE");
429
38c2028e 430 return 0;
431}
432
433int emu_core_init(void)
434{
435 CheckSubDir();
436 check_memcards();
437
438 if (EmuInit() == -1) {
439 printf("PSX emulator couldn't be initialized.\n");
440 return -1;
441 }
442
443 LoadMcds(Config.Mcd1, Config.Mcd2);
444
445 if (Config.Debug) {
446 StartDebugger();
447 }
448
449 return 0;
450}
451
452#ifndef NO_FRONTEND
453int main(int argc, char *argv[])
454{
e0c692d9 455 char file[MAXPATHLEN] = "";
456 char path[MAXPATHLEN];
457 const char *cdfile = NULL;
de910c6b 458 const char *loadst_f = NULL;
0a151868 459 int psxout = 0;
e0c692d9 460 int loadst = 0;
461 int i;
462
38c2028e 463 emu_core_preinit();
464
80c2304e 465 // read command line options
466 for (i = 1; i < argc; i++) {
0a151868 467 if (!strcmp(argv[i], "-psxout")) psxout = 1;
80c2304e 468 else if (!strcmp(argv[i], "-load")) loadst = atol(argv[++i]);
469 else if (!strcmp(argv[i], "-cfg")) {
470 if (i+1 >= argc) break;
471 strncpy(cfgfile_basename, argv[++i], MAXPATHLEN-100); /* TODO buffer overruns */
472 printf("Using config file %s.\n", cfgfile_basename);
473 }
474 else if (!strcmp(argv[i], "-cdfile")) {
475 char isofilename[MAXPATHLEN];
476
477 if (i+1 >= argc) break;
478 strncpy(isofilename, argv[++i], MAXPATHLEN);
479 if (isofilename[0] != '/') {
480 getcwd(path, MAXPATHLEN);
481 if (strlen(path) + strlen(isofilename) + 1 < MAXPATHLEN) {
482 strcat(path, "/");
483 strcat(path, isofilename);
484 strcpy(isofilename, path);
485 } else
486 isofilename[0] = 0;
487 }
488
47bf65ab 489 cdfile = isofilename;
80c2304e 490 }
de910c6b 491 else if (!strcmp(argv[i], "-loadf")) {
492 if (i+1 >= argc) break;
493 loadst_f = argv[++i];
494 }
80c2304e 495 else if (!strcmp(argv[i], "-h") ||
496 !strcmp(argv[i], "-help") ||
497 !strcmp(argv[i], "--help")) {
38c2028e 498 printf("PCSX-ReARMed " REV "\n");
80c2304e 499 printf("%s\n", _(
500 " pcsx [options] [file]\n"
501 "\toptions:\n"
502 "\t-cdfile FILE\tRuns a CD image file\n"
80c2304e 503 "\t-cfg FILE\tLoads desired configuration file (default: ~/.pcsx/pcsx.cfg)\n"
504 "\t-psxout\t\tEnable PSX output\n"
505 "\t-load STATENUM\tLoads savestate STATENUM (1-5)\n"
506 "\t-h -help\tDisplay this message\n"
38c2028e 507 "\tfile\t\tLoads a PSX EXE file\n"));
80c2304e 508 return 0;
509 } else {
510 strncpy(file, argv[i], MAXPATHLEN);
511 if (file[0] != '/') {
512 getcwd(path, MAXPATHLEN);
513 if (strlen(path) + strlen(file) + 1 < MAXPATHLEN) {
514 strcat(path, "/");
515 strcat(path, file);
516 strcpy(file, path);
517 } else
518 file[0] = 0;
519 }
520 }
521 }
522
47bf65ab 523 if (cdfile)
524 set_cd_image(cdfile);
525
69af03a2 526 // frontend stuff
b07c18e8 527 // init input but leave probing to platform code,
528 // they add input drivers and may need to modify them after probe
69af03a2 529 in_init();
59f1c85c 530 pl_init();
69af03a2 531 plat_init();
0a151868 532 menu_init(); // loads config
533
38c2028e 534 emu_core_init();
535
0a151868 536 if (psxout)
537 Config.PsxOut = 1;
80c2304e 538
69af03a2 539 if (LoadPlugins() == -1) {
6fe1f056 540 // FIXME: this recovery doesn't work, just delete bad config and bail out
541 // SysMessage("could not load plugins, retrying with defaults\n");
542 set_default_paths();
543 snprintf(path, sizeof(path), "." PCSX_DOT_DIR "%s", cfgfile_basename);
544 remove(path);
69af03a2 545 SysMessage("Failed loading plugins!");
546 return 1;
547 }
548 pcnt_hook_plugins();
80c2304e 549
69af03a2 550 if (OpenPlugins() == -1) {
551 return 1;
552 }
201c21e2 553 plugin_call_rearmed_cbs();
80c2304e 554
69af03a2 555 CheckCdrom();
bbd837c6 556 SysReset();
69af03a2 557
558 if (file[0] != '\0') {
559 if (Load(file) != -1)
560 ready_to_go = 1;
561 } else {
47bf65ab 562 if (cdfile) {
69af03a2 563 if (LoadCdrom() == -1) {
564 ClosePlugins();
565 printf(_("Could not load CD-ROM!\n"));
566 return -1;
80c2304e 567 }
79f216e3 568 emu_on_new_cd(!loadst);
69af03a2 569 ready_to_go = 1;
80c2304e 570 }
69af03a2 571 }
80c2304e 572
45d45c1e 573 if (ready_to_go) {
3c70c47b 574 menu_prepare_emu();
45d45c1e 575
576 // If a state has been specified, then load that
577 if (loadst) {
578 int ret = emu_load_state(loadst - 1);
579 printf("%s state %d\n", ret ? "failed to load" : "loaded", loadst);
580 }
de910c6b 581 if (loadst_f) {
582 int ret = LoadState(loadst_f);
583 printf("%s state file: %s\n", ret ? "failed to load" : "loaded", loadst_f);
584 }
45d45c1e 585 }
3c70c47b 586 else
69af03a2 587 menu_loop();
588
7c0f51de 589 pl_start_watchdog();
590
69af03a2 591 while (1)
592 {
799b0b87 593 stop = 0;
8f892648 594 emu_action = SACTION_NONE;
595
80c2304e 596 psxCpu->Execute();
8f892648 597 if (emu_action != SACTION_NONE)
598 do_emu_action();
80c2304e 599 }
600
80c2304e 601 return 0;
602}
456f1b86 603
604static void toggle_fast_forward(int force_off)
605{
606 static int fast_forward;
607 static int normal_g_opts;
608 static int normal_frameskip;
609 static int normal_enhancement_enable;
610
611 if (force_off && !fast_forward)
612 return;
613
614 fast_forward = !fast_forward;
615 if (fast_forward) {
616 normal_g_opts = g_opts;
617 normal_frameskip = pl_rearmed_cbs.frameskip;
618 normal_enhancement_enable =
619 pl_rearmed_cbs.gpu_neon.enhancement_enable;
620
621 g_opts |= OPT_NO_FRAMELIM;
622 pl_rearmed_cbs.frameskip = 3;
623 pl_rearmed_cbs.gpu_neon.enhancement_enable = 0;
624 } else {
625 g_opts = normal_g_opts;
626 pl_rearmed_cbs.frameskip = normal_frameskip;
627 pl_rearmed_cbs.gpu_neon.enhancement_enable =
628 normal_enhancement_enable;
629
630 pl_timing_prepare(Config.PsxType);
631 }
632
633 if (!force_off)
634 snprintf(hud_msg, sizeof(hud_msg), "FAST FORWARD %s",
635 fast_forward ? "ON" : "OFF");
636}
38c2028e 637#endif
80c2304e 638
639void SysRunGui() {
640 printf("SysRunGui\n");
641}
642
e6eb2066 643static void dummy_lace()
644{
645}
646
80c2304e 647void SysReset() {
e6eb2066 648 // rearmed hack: EmuReset() runs some code when real BIOS is used,
649 // but we usually do reset from menu while GPU is not open yet,
650 // so we need to prevent updateLace() call..
651 void *real_lace = GPU_updateLace;
652 GPU_updateLace = dummy_lace;
653
33400707 654 // reset can run code, timing must be set
655 pl_timing_prepare(Config.PsxType);
656
80c2304e 657 EmuReset();
bd6267e6 658
659 // hmh core forgets this
660 CDR_stop();
e6eb2066 661
662 GPU_updateLace = real_lace;
80c2304e 663}
664
665void SysClose() {
666 EmuShutdown();
667 ReleasePlugins();
668
669 StopDebugger();
670
671 if (emuLog != NULL) fclose(emuLog);
672}
673
674void SysUpdate() {
80c2304e 675}
676
677void OnFile_Exit() {
bd6267e6 678 printf("OnFile_Exit\n");
bd6267e6 679 SysClose();
38c2028e 680#ifndef NO_FRONTEND
681 menu_finish();
2c886904 682 plat_finish();
80c2304e 683 exit(0);
38c2028e 684#endif
80c2304e 685}
686
c5061935 687int get_state_filename(char *buf, int size, int i) {
29a8c4f3 688 return get_gameid_filename(buf, size,
689 "." STATES_DIR "%.32s-%.9s.%3.3d", i);
80c2304e 690}
691
8f892648 692int emu_check_state(int slot)
693{
694 char fname[MAXPATHLEN];
695 int ret;
696
697 ret = get_state_filename(fname, sizeof(fname), slot);
698 if (ret != 0)
699 return ret;
700
701 return CheckState(fname);
702}
703
704int emu_save_state(int slot)
705{
706 char fname[MAXPATHLEN];
707 int ret;
708
709 ret = get_state_filename(fname, sizeof(fname), slot);
710 if (ret != 0)
711 return ret;
712
90f1d26c 713 ret = SaveState(fname);
0b1dbe6b 714#ifndef __ARM_ARCH_7A__ /* XXX */
715 sync();
716#endif
90f1d26c 717 printf("* %s \"%s\" [%d]\n", ret == 0 ? "saved" : "failed to save", fname, slot);
718 return ret;
8f892648 719}
720
721int emu_load_state(int slot)
722{
723 char fname[MAXPATHLEN];
724 int ret;
725
726 ret = get_state_filename(fname, sizeof(fname), slot);
727 if (ret != 0)
728 return ret;
729
730 return LoadState(fname);
731}
732
80c2304e 733void SysPrintf(const char *fmt, ...) {
734 va_list list;
735 char msg[512];
736
737 va_start(list, fmt);
738 vsprintf(msg, fmt, list);
739 va_end(list);
740
80c2304e 741 fprintf(emuLog, "%s", msg);
742}
743
744void SysMessage(const char *fmt, ...) {
745 va_list list;
746 char msg[512];
747
748 va_start(list, fmt);
749 vsprintf(msg, fmt, list);
750 va_end(list);
751
752 if (msg[strlen(msg) - 1] == '\n')
753 msg[strlen(msg) - 1] = 0;
754
755 fprintf(stderr, "%s\n", msg);
756}
757
c5061935 758static void SignalExit(int sig) {
759 ClosePlugins();
760 OnFile_Exit();
761}
762
763#define PARSEPATH(dst, src) \
764 ptr = src + strlen(src); \
765 while (*ptr != '\\' && ptr != src) ptr--; \
766 if (ptr != src) { \
767 strcpy(dst, ptr+1); \
768 }
769
770static int _OpenPlugins(void) {
771 int ret;
772
773 signal(SIGINT, SignalExit);
774 signal(SIGPIPE, SignalExit);
775
776 GPU_clearDynarec(clearDynarec);
777
778 ret = CDR_open();
779 if (ret < 0) { SysMessage(_("Error opening CD-ROM plugin!")); return -1; }
780 ret = SPU_open();
781 if (ret < 0) { SysMessage(_("Error opening SPU plugin!")); return -1; }
782 SPU_registerCallback(SPUirq);
783 // pcsx-rearmed: we handle gpu elsewhere
784 //ret = GPU_open(&gpuDisp, "PCSX", NULL);
785 //if (ret < 0) { SysMessage(_("Error opening GPU plugin!")); return -1; }
786 ret = PAD1_open(&gpuDisp);
787 if (ret < 0) { SysMessage(_("Error opening Controller 1 plugin!")); return -1; }
788 ret = PAD2_open(&gpuDisp);
789 if (ret < 0) { SysMessage(_("Error opening Controller 2 plugin!")); return -1; }
790
791 if (Config.UseNet && !NetOpened) {
792 netInfo info;
793 char path[MAXPATHLEN];
794 char dotdir[MAXPATHLEN];
795
796 MAKE_PATH(dotdir, "/.pcsx/plugins/", NULL);
797
38c2028e 798 strcpy(info.EmuName, "PCSX");
c5061935 799 strncpy(info.CdromID, CdromId, 9);
800 strncpy(info.CdromLabel, CdromLabel, 9);
801 info.psxMem = psxM;
802 info.GPU_showScreenPic = GPU_showScreenPic;
803 info.GPU_displayText = GPU_displayText;
804 info.GPU_showScreenPic = GPU_showScreenPic;
805 info.PAD_setSensitive = PAD1_setSensitive;
806 sprintf(path, "%s%s", Config.BiosDir, Config.Bios);
807 strcpy(info.BIOSpath, path);
808 strcpy(info.MCD1path, Config.Mcd1);
809 strcpy(info.MCD2path, Config.Mcd2);
810 sprintf(path, "%s%s", dotdir, Config.Gpu);
811 strcpy(info.GPUpath, path);
812 sprintf(path, "%s%s", dotdir, Config.Spu);
813 strcpy(info.SPUpath, path);
814 sprintf(path, "%s%s", dotdir, Config.Cdr);
815 strcpy(info.CDRpath, path);
816 NET_setInfo(&info);
817
818 ret = NET_open(&gpuDisp);
819 if (ret < 0) {
820 if (ret == -2) {
821 // -2 is returned when something in the info
822 // changed and needs to be synced
823 char *ptr;
824
825 PARSEPATH(Config.Bios, info.BIOSpath);
826 PARSEPATH(Config.Gpu, info.GPUpath);
827 PARSEPATH(Config.Spu, info.SPUpath);
828 PARSEPATH(Config.Cdr, info.CDRpath);
829
830 strcpy(Config.Mcd1, info.MCD1path);
831 strcpy(Config.Mcd2, info.MCD2path);
832 return -2;
833 } else {
834 Config.UseNet = FALSE;
835 }
836 } else {
837 if (NET_queryPlayer() == 1) {
838 if (SendPcsxInfo() == -1) Config.UseNet = FALSE;
839 } else {
840 if (RecvPcsxInfo() == -1) Config.UseNet = FALSE;
841 }
842 }
843 NetOpened = TRUE;
844 } else if (Config.UseNet) {
845 NET_resume();
846 }
847
848 return 0;
849}
850
851int OpenPlugins() {
852 int ret;
853
854 while ((ret = _OpenPlugins()) == -2) {
855 ReleasePlugins();
856 LoadMcds(Config.Mcd1, Config.Mcd2);
857 if (LoadPlugins() == -1) return -1;
858 }
859 return ret;
860}
861
862void ClosePlugins() {
863 int ret;
864
865 signal(SIGINT, SIG_DFL);
866 signal(SIGPIPE, SIG_DFL);
867 ret = CDR_close();
868 if (ret < 0) { SysMessage(_("Error closing CD-ROM plugin!")); return; }
869 ret = SPU_close();
870 if (ret < 0) { SysMessage(_("Error closing SPU plugin!")); return; }
871 ret = PAD1_close();
872 if (ret < 0) { SysMessage(_("Error closing Controller 1 Plugin!")); return; }
873 ret = PAD2_close();
874 if (ret < 0) { SysMessage(_("Error closing Controller 2 plugin!")); return; }
875 // pcsx-rearmed: we handle gpu elsewhere
876 //ret = GPU_close();
877 //if (ret < 0) { SysMessage(_("Error closing GPU plugin!")); return; }
878
879 if (Config.UseNet) {
880 NET_pause();
881 }
882}
883
e906c010 884/* we hook statically linked plugins here */
885static const char *builtin_plugins[] = {
47bf65ab 886 "builtin_gpu", "builtin_spu", "builtin_cdr", "builtin_pad",
887 "builtin_cdrcimg",
e906c010 888};
889
890static const int builtin_plugin_ids[] = {
891 PLUGIN_GPU, PLUGIN_SPU, PLUGIN_CDR, PLUGIN_PAD,
47bf65ab 892 PLUGIN_CDRCIMG,
e906c010 893};
894
80c2304e 895void *SysLoadLibrary(const char *lib) {
e906c010 896 const char *tmp = strrchr(lib, '/');
bbd837c6 897 void *ret;
e906c010 898 int i;
899
bbd837c6 900 printf("plugin: %s\n", lib);
901
e906c010 902 if (tmp != NULL) {
903 tmp++;
904 for (i = 0; i < ARRAY_SIZE(builtin_plugins); i++)
905 if (strcmp(tmp, builtin_plugins[i]) == 0)
906 return (void *)(long)(PLUGIN_DL_BASE + builtin_plugin_ids[i]);
907 }
908
bbd837c6 909 ret = dlopen(lib, RTLD_NOW);
910 if (ret == NULL)
911 fprintf(stderr, "dlopen: %s\n", dlerror());
912 return ret;
80c2304e 913}
914
915void *SysLoadSym(void *lib, const char *sym) {
e906c010 916 unsigned int plugid = (unsigned int)(long)lib;
917
918 if (PLUGIN_DL_BASE <= plugid && plugid < PLUGIN_DL_BASE + ARRAY_SIZE(builtin_plugins))
919 return plugin_link(plugid - PLUGIN_DL_BASE, sym);
920
80c2304e 921 return dlsym(lib, sym);
922}
923
924const char *SysLibError() {
925 return dlerror();
926}
927
928void SysCloseLibrary(void *lib) {
e906c010 929 unsigned int plugid = (unsigned int)(long)lib;
930
931 if (PLUGIN_DL_BASE <= plugid && plugid < PLUGIN_DL_BASE + ARRAY_SIZE(builtin_plugins))
932 return;
933
80c2304e 934 dlclose(lib);
935}
936