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