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