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