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