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