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