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