select builtin gpu plugin on configure
[pcsx_rearmed.git] / frontend / main.c
1 /*
2  * (C) notaz, 2010-2011
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
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>
15 #include <signal.h>
16 #include <time.h>
17
18 #include "main.h"
19 #include "plugin.h"
20 #include "plugin_lib.h"
21 #include "pcnt.h"
22 #include "menu.h"
23 #include "plat.h"
24 #include "../libpcsxcore/misc.h"
25 #include "../libpcsxcore/cheat.h"
26 #include "../libpcsxcore/new_dynarec/new_dynarec.h"
27 #include "../plugins/cdrcimg/cdrcimg.h"
28 #include "common/plat.h"
29 #include "common/readpng.h"
30 #include "common/input.h"
31 #include "linux/in_evdev.h"
32 #include "revision.h"
33
34 // don't include debug.h - it breaks ARM build (R1 redefined)
35 void StartDebugger();
36 void StopDebugger();
37
38 // sound plugin
39 extern int iUseReverb;
40 extern int iUseInterpolation;
41 extern int iXAPitch;
42 extern int iVolume;
43
44 int ready_to_go;
45 unsigned long gpuDisp;
46 char cfgfile_basename[MAXPATHLEN];
47 int state_slot;
48 enum sched_action emu_action, emu_action_old;
49 char hud_msg[64];
50 int hud_new_msg;
51
52 static 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
62 static 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
69 static 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);
80         create_profile_dir(PCSX_DOT_DIR "cfg");
81         create_profile_dir("/screenshots/");
82 }
83
84 static 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;
99 }
100
101 void set_cd_image(const char *fname)
102 {
103         const char *ext = NULL;
104         
105         if (fname != NULL)
106                 ext = strrchr(fname, '.');
107
108         if (ext && (
109             strcasecmp(ext, ".z") == 0 || strcasecmp(ext, ".bz") == 0 ||
110             strcasecmp(ext, ".znx") == 0 /*|| strcasecmp(ext, ".pbp") == 0*/)) {
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
120 static 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");
133
134         snprintf(Config.PatchesDir, sizeof(Config.PatchesDir), "." PATCHES_DIR);
135 }
136
137 void emu_set_default_config(void)
138 {
139         // try to set sane config on which most games work
140         Config.Xa = Config.Cdda = Config.Sio =
141         Config.SpuIrq = Config.RCntFix = Config.VSyncWA = 0;
142         Config.CdrReschedule = 0;
143         Config.PsxAuto = 1;
144
145         pl_rearmed_cbs.gpu_neon.allow_interlace = 2; // auto
146         pl_rearmed_cbs.gpu_peops.iUseDither = 0;
147         pl_rearmed_cbs.gpu_peops.dwActFixes = 1<<7;
148         pl_rearmed_cbs.gpu_unai.abe_hack =
149         pl_rearmed_cbs.gpu_unai.no_light =
150         pl_rearmed_cbs.gpu_unai.no_blend = 0;
151         memset(&pl_rearmed_cbs.gpu_peopsgl, 0, sizeof(pl_rearmed_cbs.gpu_peopsgl));
152         pl_rearmed_cbs.gpu_peopsgl.iVRamSize = 64;
153         pl_rearmed_cbs.gpu_peopsgl.iTexGarbageCollection = 1;
154
155         iUseReverb = 2;
156         iUseInterpolation = 1;
157         iXAPitch = 0;
158         iVolume = 768;
159 #ifndef __ARM_ARCH_7A__ /* XXX */
160         iUseReverb = 0;
161         iUseInterpolation = 0;
162 #endif
163         new_dynarec_hacks = 0;
164         cycle_multiplier = 200;
165
166         in_type1 = PSE_PAD_TYPE_STANDARD;
167         in_type2 = PSE_PAD_TYPE_STANDARD;
168 }
169
170 static void check_memcards(void)
171 {
172         char buf[MAXPATHLEN];
173         FILE *f;
174         int i;
175
176         for (i = 1; i <= 9; i++) {
177                 snprintf(buf, sizeof(buf), ".%scard%d.mcd", MEMCARD_DIR, i);
178
179                 f = fopen(buf, "rb");
180                 if (f == NULL) {
181                         printf("Creating memcard: %s\n", buf);
182                         CreateMcd(buf);
183                 }
184                 else
185                         fclose(f);
186         }
187 }
188
189 void do_emu_action(void)
190 {
191         int ret;
192
193         emu_action_old = emu_action;
194
195         switch (emu_action) {
196         case SACTION_LOAD_STATE:
197                 ret = emu_load_state(state_slot);
198                 snprintf(hud_msg, sizeof(hud_msg), ret == 0 ? "LOADED" : "FAIL!");
199                 break;
200         case SACTION_SAVE_STATE:
201                 ret = emu_save_state(state_slot);
202                 snprintf(hud_msg, sizeof(hud_msg), ret == 0 ? "SAVED" : "FAIL!");
203                 break;
204 #ifndef NO_FRONTEND
205         case SACTION_ENTER_MENU:
206                 menu_loop();
207                 return;
208         case SACTION_NEXT_SSLOT:
209                 state_slot++;
210                 if (state_slot > 9)
211                         state_slot = 0;
212                 goto do_state_slot;
213         case SACTION_PREV_SSLOT:
214                 state_slot--;
215                 if (state_slot < 0)
216                         state_slot = 9;
217 do_state_slot:
218                 snprintf(hud_msg, sizeof(hud_msg), "STATE SLOT %d [%s]", state_slot,
219                         emu_check_state(state_slot) == 0 ? "USED" : "FREE");
220                 hud_new_msg = 3;
221                 printf("* %s\n", hud_msg);
222                 break;
223         case SACTION_TOGGLE_FSKIP:
224                 pl_rearmed_cbs.fskip_advice = 0;
225                 pl_rearmed_cbs.frameskip++;
226                 if (pl_rearmed_cbs.frameskip > 1)
227                         pl_rearmed_cbs.frameskip = -1;
228                 snprintf(hud_msg, sizeof(hud_msg), "FRAMESKIP: %s",
229                         pl_rearmed_cbs.frameskip == -1 ? "AUTO" :
230                         pl_rearmed_cbs.frameskip == 0 ? "OFF" : "1" );
231                 plugin_call_rearmed_cbs();
232                 break;
233         case SACTION_SCREENSHOT:
234                 {
235                         char buf[MAXPATHLEN];
236                         void *scrbuf;
237                         int w, h, bpp;
238                         time_t t = time(NULL);
239                         struct tm *tb = localtime(&t);
240                         int ti = tb->tm_yday * 1000000 + tb->tm_hour * 10000 +
241                                 tb->tm_min * 100 + tb->tm_sec;
242
243                         scrbuf = pl_prepare_screenshot(&w, &h, &bpp);
244                         get_gameid_filename(buf, sizeof(buf),
245                                 "screenshots/%.32s-%.9s.%d.png", ti);
246                         ret = -1;
247                         if (scrbuf != 0 && bpp == 16)
248                                 ret = writepng(buf, scrbuf, w, h);
249                         if (ret == 0)
250                                 snprintf(hud_msg, sizeof(hud_msg), "SCREENSHOT TAKEN");
251                         break;
252                 }
253         case SACTION_VOLUME_UP:
254         case SACTION_VOLUME_DOWN:
255                 plat_step_volume(emu_action == SACTION_VOLUME_UP);
256                 return;
257         case SACTION_MINIMIZE:
258                 plat_minimize();
259                 return;
260 #endif
261         default:
262                 return;
263         }
264
265         hud_new_msg = 3;
266 }
267
268 static int cdidcmp(const char *id1, const char *id2)
269 {
270         while (*id1 != 0 && *id2 != 0) {
271                 if (*id1 == '_') { id1++; continue; }
272                 if (*id2 == '_') { id2++; continue; }
273                 if (*id1 != *id2)
274                         break;
275                 id1++;
276                 id2++;
277         }
278
279         return *id1 - *id2;
280 }
281
282 static void parse_cwcheat(void)
283 {
284         char line[256], buf[64], name[64], *p;
285         int newcheat = 1;
286         u32 a, v;
287         FILE *f;
288
289         f = fopen("cheatpops.db", "r");
290         if (f == NULL)
291                 return;
292
293         /* find the game */
294         while (fgets(line, sizeof(line), f)) {
295                 if (sscanf(line, "_S %63s", buf) != 1)
296                         continue;
297                 if (cdidcmp(buf, CdromId) == 0)
298                         break;
299         }
300
301         if (feof(f))
302                 goto out;
303
304         printf("cwcheat section found for %s\n", CdromId);
305         while (fgets(line, sizeof(line), f))
306         {
307                 p = line + strlen(line);
308                 for (p--; p >= line && (*p == '\r' || *p == '\n' || *p == ' '); p--)
309                         *p = 0;
310                 if (*p == 0 || *p == '#' || *p == ';')
311                         continue;
312
313                 if (strncmp(line, "_S", 2) == 0)
314                         break;
315                 if (strncmp(line, "_G", 2) == 0) {
316                         printf("  cwcheat game name: '%s'\n", line + 3);
317                         continue;
318                 }
319                 if (strncmp(line, "_C0", 3) == 0) {
320                         if (!newcheat && Cheats[NumCheats - 1].n == 0) {
321                                 printf("cheat '%s' failed to parse\n", name);
322                                 free(Cheats[NumCheats - 1].Descr);
323                                 NumCheats--;
324                         }
325                         snprintf(name, sizeof(name), "%s", line + 4);
326                         newcheat = 1;
327                         continue;
328                 }
329                 if (sscanf(line, "_L %x %x", &a, &v) != 2) {
330                         printf("line failed to parse: '%s'\n", line);
331                         continue;
332                 }
333
334                 if (newcheat) {
335                         if (NumCheats >= NumCheatsAllocated) {
336                                 NumCheatsAllocated += 16;
337                                 Cheats = realloc(Cheats, sizeof(Cheat) *
338                                                 NumCheatsAllocated);
339                                 if (Cheats == NULL)
340                                         break;
341                         }
342                         Cheats[NumCheats].Descr = strdup(name);
343                         Cheats[NumCheats].Enabled = 0;
344                         Cheats[NumCheats].WasEnabled = 0;
345                         Cheats[NumCheats].First = NumCodes;
346                         Cheats[NumCheats].n = 0;
347                         NumCheats++;
348                         newcheat = 0;
349                 }
350
351                 if (NumCodes >= NumCodesAllocated) {
352                         NumCodesAllocated += 16;
353                         CheatCodes = realloc(CheatCodes, sizeof(CheatCode) *
354                                 NumCodesAllocated);
355                         if (CheatCodes == NULL)
356                                 break;
357                 }
358                 CheatCodes[NumCodes].Addr = a;
359                 CheatCodes[NumCodes].Val = v;
360                 NumCodes++;
361                 Cheats[NumCheats - 1].n++;
362         }
363
364 out:
365         fclose(f);
366 }
367
368 void emu_on_new_cd(void)
369 {
370         ClearAllCheats();
371         parse_cwcheat();
372
373         if (Config.HLE) {
374                 printf("note: running with HLE BIOS, expect compatibility problems\n");
375                 printf("----------------------------------------------------------\n");
376         }
377
378         snprintf(hud_msg, sizeof(hud_msg), "Booting up...");
379         hud_new_msg = 2;
380 }
381
382 int emu_core_preinit(void)
383 {
384         // what is the name of the config file?
385         // it may be redefined by -cfg on the command line
386         strcpy(cfgfile_basename, "pcsx.cfg");
387
388         emuLog = stdout;
389         SetIsoFile(NULL);
390
391         memset(&Config, 0, sizeof(Config));
392
393         set_default_paths();
394         emu_set_default_config();
395         strcpy(Config.Bios, "HLE");
396
397         return 0;
398 }
399
400 int emu_core_init(void)
401 {
402         CheckSubDir();
403         check_memcards();
404
405         if (EmuInit() == -1) {
406                 printf("PSX emulator couldn't be initialized.\n");
407                 return -1;
408         }
409
410         LoadMcds(Config.Mcd1, Config.Mcd2);
411
412         if (Config.Debug) {
413                 StartDebugger();
414         }
415
416         return 0;
417 }
418
419 #ifndef NO_FRONTEND
420 int main(int argc, char *argv[])
421 {
422         char file[MAXPATHLEN] = "";
423         char path[MAXPATHLEN];
424         const char *cdfile = NULL;
425         const char *loadst_f = NULL;
426         int psxout = 0;
427         int loadst = 0;
428         int i;
429
430         emu_core_preinit();
431
432         // read command line options
433         for (i = 1; i < argc; i++) {
434                      if (!strcmp(argv[i], "-psxout")) psxout = 1;
435                 else if (!strcmp(argv[i], "-load")) loadst = atol(argv[++i]);
436                 else if (!strcmp(argv[i], "-cfg")) {
437                         if (i+1 >= argc) break;
438                         strncpy(cfgfile_basename, argv[++i], MAXPATHLEN-100);   /* TODO buffer overruns */
439                         printf("Using config file %s.\n", cfgfile_basename);
440                 }
441                 else if (!strcmp(argv[i], "-cdfile")) {
442                         char isofilename[MAXPATHLEN];
443
444                         if (i+1 >= argc) break;
445                         strncpy(isofilename, argv[++i], MAXPATHLEN);
446                         if (isofilename[0] != '/') {
447                                 getcwd(path, MAXPATHLEN);
448                                 if (strlen(path) + strlen(isofilename) + 1 < MAXPATHLEN) {
449                                         strcat(path, "/");
450                                         strcat(path, isofilename);
451                                         strcpy(isofilename, path);
452                                 } else
453                                         isofilename[0] = 0;
454                         }
455
456                         cdfile = isofilename;
457                 }
458                 else if (!strcmp(argv[i], "-loadf")) {
459                         if (i+1 >= argc) break;
460                         loadst_f = argv[++i];
461                 }
462                 else if (!strcmp(argv[i], "-h") ||
463                          !strcmp(argv[i], "-help") ||
464                          !strcmp(argv[i], "--help")) {
465                          printf("PCSX-ReARMed " REV "\n");
466                          printf("%s\n", _(
467                                                         " pcsx [options] [file]\n"
468                                                         "\toptions:\n"
469                                                         "\t-cdfile FILE\tRuns a CD image file\n"
470                                                         "\t-cfg FILE\tLoads desired configuration file (default: ~/.pcsx/pcsx.cfg)\n"
471                                                         "\t-psxout\t\tEnable PSX output\n"
472                                                         "\t-load STATENUM\tLoads savestate STATENUM (1-5)\n"
473                                                         "\t-h -help\tDisplay this message\n"
474                                                         "\tfile\t\tLoads a PSX EXE file\n"));
475                          return 0;
476                 } else {
477                         strncpy(file, argv[i], MAXPATHLEN);
478                         if (file[0] != '/') {
479                                 getcwd(path, MAXPATHLEN);
480                                 if (strlen(path) + strlen(file) + 1 < MAXPATHLEN) {
481                                         strcat(path, "/");
482                                         strcat(path, file);
483                                         strcpy(file, path);
484                                 } else
485                                         file[0] = 0;
486                         }
487                 }
488         }
489
490         if (cdfile)
491                 set_cd_image(cdfile);
492
493         // frontend stuff
494         // init input but leave probing to platform code,
495         // they add input drivers and may need to modify them after probe
496         in_init();
497         pl_init();
498         plat_init();
499         menu_init(); // loads config
500
501         emu_core_init();
502
503         if (psxout)
504                 Config.PsxOut = 1;
505
506         if (LoadPlugins() == -1) {
507                 // FIXME: this recovery doesn't work, just delete bad config and bail out
508                 // SysMessage("could not load plugins, retrying with defaults\n");
509                 set_default_paths();
510                 snprintf(path, sizeof(path), "." PCSX_DOT_DIR "%s", cfgfile_basename);
511                 remove(path);
512                 SysMessage("Failed loading plugins!");
513                 return 1;
514         }
515         pcnt_hook_plugins();
516
517         if (OpenPlugins() == -1) {
518                 return 1;
519         }
520         plugin_call_rearmed_cbs();
521
522         CheckCdrom();
523         SysReset();
524
525         if (file[0] != '\0') {
526                 if (Load(file) != -1)
527                         ready_to_go = 1;
528         } else {
529                 if (cdfile) {
530                         if (LoadCdrom() == -1) {
531                                 ClosePlugins();
532                                 printf(_("Could not load CD-ROM!\n"));
533                                 return -1;
534                         }
535                         emu_on_new_cd();
536                         ready_to_go = 1;
537                 }
538         }
539
540         if (ready_to_go) {
541                 menu_prepare_emu();
542
543                 // If a state has been specified, then load that
544                 if (loadst) {
545                         int ret = emu_load_state(loadst - 1);
546                         printf("%s state %d\n", ret ? "failed to load" : "loaded", loadst);
547                 }
548                 if (loadst_f) {
549                         int ret = LoadState(loadst_f);
550                         printf("%s state file: %s\n", ret ? "failed to load" : "loaded", loadst_f);
551                 }
552         }
553         else
554                 menu_loop();
555
556         pl_start_watchdog();
557
558         while (1)
559         {
560                 stop = 0;
561                 emu_action = SACTION_NONE;
562
563                 psxCpu->Execute();
564                 if (emu_action != SACTION_NONE)
565                         do_emu_action();
566         }
567
568         return 0;
569 }
570 #endif
571
572 void SysRunGui() {
573         printf("SysRunGui\n");
574 }
575
576 static void dummy_lace()
577 {
578 }
579
580 void SysReset() {
581         // rearmed hack: EmuReset() runs some code when real BIOS is used,
582         // but we usually do reset from menu while GPU is not open yet,
583         // so we need to prevent updateLace() call..
584         void *real_lace = GPU_updateLace;
585         GPU_updateLace = dummy_lace;
586
587         // reset can run code, timing must be set
588         pl_timing_prepare(Config.PsxType);
589
590         EmuReset();
591
592         // hmh core forgets this
593         CDR_stop();
594
595         GPU_updateLace = real_lace;
596 }
597
598 void SysClose() {
599         EmuShutdown();
600         ReleasePlugins();
601
602         StopDebugger();
603
604         if (emuLog != NULL) fclose(emuLog);
605 }
606
607 void SysUpdate() {
608 }
609
610 void OnFile_Exit() {
611         printf("OnFile_Exit\n");
612         SysClose();
613 #ifndef NO_FRONTEND
614         menu_finish();
615         plat_finish();
616         exit(0);
617 #endif
618 }
619
620 int get_state_filename(char *buf, int size, int i) {
621         return get_gameid_filename(buf, size,
622                 "." STATES_DIR "%.32s-%.9s.%3.3d", i);
623 }
624
625 int emu_check_state(int slot)
626 {
627         char fname[MAXPATHLEN];
628         int ret;
629
630         ret = get_state_filename(fname, sizeof(fname), slot);
631         if (ret != 0)
632                 return ret;
633
634         return CheckState(fname);
635 }
636
637 int emu_save_state(int slot)
638 {
639         char fname[MAXPATHLEN];
640         int ret;
641
642         ret = get_state_filename(fname, sizeof(fname), slot);
643         if (ret != 0)
644                 return ret;
645
646         ret = SaveState(fname);
647 #ifndef __ARM_ARCH_7A__ /* XXX */
648         sync();
649 #endif
650         printf("* %s \"%s\" [%d]\n", ret == 0 ? "saved" : "failed to save", fname, slot);
651         return ret;
652 }
653
654 int emu_load_state(int slot)
655 {
656         char fname[MAXPATHLEN];
657         int ret;
658
659         ret = get_state_filename(fname, sizeof(fname), slot);
660         if (ret != 0)
661                 return ret;
662
663         return LoadState(fname);
664 }
665
666 void SysPrintf(const char *fmt, ...) {
667         va_list list;
668         char msg[512];
669
670         va_start(list, fmt);
671         vsprintf(msg, fmt, list);
672         va_end(list);
673
674         fprintf(emuLog, "%s", msg);
675 }
676
677 void SysMessage(const char *fmt, ...) {
678         va_list list;
679         char msg[512];
680
681         va_start(list, fmt);
682         vsprintf(msg, fmt, list);
683         va_end(list);
684
685         if (msg[strlen(msg) - 1] == '\n')
686                 msg[strlen(msg) - 1] = 0;
687
688         fprintf(stderr, "%s\n", msg);
689 }
690
691 static void SignalExit(int sig) {
692         ClosePlugins();
693         OnFile_Exit();
694 }
695
696 #define PARSEPATH(dst, src) \
697         ptr = src + strlen(src); \
698         while (*ptr != '\\' && ptr != src) ptr--; \
699         if (ptr != src) { \
700                 strcpy(dst, ptr+1); \
701         }
702
703 static int _OpenPlugins(void) {
704         int ret;
705
706         signal(SIGINT, SignalExit);
707         signal(SIGPIPE, SignalExit);
708
709         GPU_clearDynarec(clearDynarec);
710
711         ret = CDR_open();
712         if (ret < 0) { SysMessage(_("Error opening CD-ROM plugin!")); return -1; }
713         ret = SPU_open();
714         if (ret < 0) { SysMessage(_("Error opening SPU plugin!")); return -1; }
715         SPU_registerCallback(SPUirq);
716         // pcsx-rearmed: we handle gpu elsewhere
717         //ret = GPU_open(&gpuDisp, "PCSX", NULL);
718         //if (ret < 0) { SysMessage(_("Error opening GPU plugin!")); return -1; }
719         ret = PAD1_open(&gpuDisp);
720         if (ret < 0) { SysMessage(_("Error opening Controller 1 plugin!")); return -1; }
721         ret = PAD2_open(&gpuDisp);
722         if (ret < 0) { SysMessage(_("Error opening Controller 2 plugin!")); return -1; }
723
724         if (Config.UseNet && !NetOpened) {
725                 netInfo info;
726                 char path[MAXPATHLEN];
727                 char dotdir[MAXPATHLEN];
728
729                 MAKE_PATH(dotdir, "/.pcsx/plugins/", NULL);
730
731                 strcpy(info.EmuName, "PCSX");
732                 strncpy(info.CdromID, CdromId, 9);
733                 strncpy(info.CdromLabel, CdromLabel, 9);
734                 info.psxMem = psxM;
735                 info.GPU_showScreenPic = GPU_showScreenPic;
736                 info.GPU_displayText = GPU_displayText;
737                 info.GPU_showScreenPic = GPU_showScreenPic;
738                 info.PAD_setSensitive = PAD1_setSensitive;
739                 sprintf(path, "%s%s", Config.BiosDir, Config.Bios);
740                 strcpy(info.BIOSpath, path);
741                 strcpy(info.MCD1path, Config.Mcd1);
742                 strcpy(info.MCD2path, Config.Mcd2);
743                 sprintf(path, "%s%s", dotdir, Config.Gpu);
744                 strcpy(info.GPUpath, path);
745                 sprintf(path, "%s%s", dotdir, Config.Spu);
746                 strcpy(info.SPUpath, path);
747                 sprintf(path, "%s%s", dotdir, Config.Cdr);
748                 strcpy(info.CDRpath, path);
749                 NET_setInfo(&info);
750
751                 ret = NET_open(&gpuDisp);
752                 if (ret < 0) {
753                         if (ret == -2) {
754                                 // -2 is returned when something in the info
755                                 // changed and needs to be synced
756                                 char *ptr;
757
758                                 PARSEPATH(Config.Bios, info.BIOSpath);
759                                 PARSEPATH(Config.Gpu,  info.GPUpath);
760                                 PARSEPATH(Config.Spu,  info.SPUpath);
761                                 PARSEPATH(Config.Cdr,  info.CDRpath);
762
763                                 strcpy(Config.Mcd1, info.MCD1path);
764                                 strcpy(Config.Mcd2, info.MCD2path);
765                                 return -2;
766                         } else {
767                                 Config.UseNet = FALSE;
768                         }
769                 } else {
770                         if (NET_queryPlayer() == 1) {
771                                 if (SendPcsxInfo() == -1) Config.UseNet = FALSE;
772                         } else {
773                                 if (RecvPcsxInfo() == -1) Config.UseNet = FALSE;
774                         }
775                 }
776                 NetOpened = TRUE;
777         } else if (Config.UseNet) {
778                 NET_resume();
779         }
780
781         return 0;
782 }
783
784 int OpenPlugins() {
785         int ret;
786
787         while ((ret = _OpenPlugins()) == -2) {
788                 ReleasePlugins();
789                 LoadMcds(Config.Mcd1, Config.Mcd2);
790                 if (LoadPlugins() == -1) return -1;
791         }
792         return ret;
793 }
794
795 void ClosePlugins() {
796         int ret;
797
798         signal(SIGINT, SIG_DFL);
799         signal(SIGPIPE, SIG_DFL);
800         ret = CDR_close();
801         if (ret < 0) { SysMessage(_("Error closing CD-ROM plugin!")); return; }
802         ret = SPU_close();
803         if (ret < 0) { SysMessage(_("Error closing SPU plugin!")); return; }
804         ret = PAD1_close();
805         if (ret < 0) { SysMessage(_("Error closing Controller 1 Plugin!")); return; }
806         ret = PAD2_close();
807         if (ret < 0) { SysMessage(_("Error closing Controller 2 plugin!")); return; }
808         // pcsx-rearmed: we handle gpu elsewhere
809         //ret = GPU_close();
810         //if (ret < 0) { SysMessage(_("Error closing GPU plugin!")); return; }
811
812         if (Config.UseNet) {
813                 NET_pause();
814         }
815 }
816
817 /* we hook statically linked plugins here */
818 static const char *builtin_plugins[] = {
819         "builtin_gpu", "builtin_spu", "builtin_cdr", "builtin_pad",
820         "builtin_cdrcimg",
821 };
822
823 static const int builtin_plugin_ids[] = {
824         PLUGIN_GPU, PLUGIN_SPU, PLUGIN_CDR, PLUGIN_PAD,
825         PLUGIN_CDRCIMG,
826 };
827
828 void *SysLoadLibrary(const char *lib) {
829         const char *tmp = strrchr(lib, '/');
830         void *ret;
831         int i;
832
833         printf("plugin: %s\n", lib);
834
835         if (tmp != NULL) {
836                 tmp++;
837                 for (i = 0; i < ARRAY_SIZE(builtin_plugins); i++)
838                         if (strcmp(tmp, builtin_plugins[i]) == 0)
839                                 return (void *)(long)(PLUGIN_DL_BASE + builtin_plugin_ids[i]);
840         }
841
842 #if defined(__x86_64__) || defined(__i386__)
843         // convenience hack
844         if (strstr(lib, ".x86") == NULL) {
845                 char name[MAXPATHLEN];
846                 snprintf(name, sizeof(name), "%s.x86_64", lib);
847                 lib = name;
848         }
849 #endif
850
851         ret = dlopen(lib, RTLD_NOW);
852         if (ret == NULL)
853                 fprintf(stderr, "dlopen: %s\n", dlerror());
854         return ret;
855 }
856
857 void *SysLoadSym(void *lib, const char *sym) {
858         unsigned int plugid = (unsigned int)(long)lib;
859
860         if (PLUGIN_DL_BASE <= plugid && plugid < PLUGIN_DL_BASE + ARRAY_SIZE(builtin_plugins))
861                 return plugin_link(plugid - PLUGIN_DL_BASE, sym);
862
863         return dlsym(lib, sym);
864 }
865
866 const char *SysLibError() {
867         return dlerror();
868 }
869
870 void SysCloseLibrary(void *lib) {
871         unsigned int plugid = (unsigned int)(long)lib;
872
873         if (PLUGIN_DL_BASE <= plugid && plugid < PLUGIN_DL_BASE + ARRAY_SIZE(builtin_plugins))
874                 return;
875
876         dlclose(lib);
877 }
878