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