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