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