frontend: add gles plugin options
[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/new_dynarec/new_dynarec.h"
26 #include "../plugins/cdrcimg/cdrcimg.h"
27 #include "common/plat.h"
28 #include "common/readpng.h"
29 #include "common/input.h"
30 #include "linux/in_evdev.h"
31
32 // don't include debug.h - it breaks ARM build (R1 redefined)
33 void StartDebugger();
34 void StopDebugger();
35
36 // sound plugin
37 extern int iUseReverb;
38 extern int iUseInterpolation;
39 extern int iXAPitch;
40 extern int iSPUIRQWait;
41 extern int iUseTimer;
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 #if defined(__arm__) && !defined(__ARM_ARCH_7A__) /* XXX */
134         strcpy(Config.Gpu, "gpuPCSX4ALL.so");
135 #endif
136
137         snprintf(Config.PatchesDir, sizeof(Config.PatchesDir), "." PATCHES_DIR);
138 }
139
140 void 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_neon.allow_interlace = 2; // auto
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;
154         pl_rearmed_cbs.gpu_peopsgl.iVRamSize = 64;
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
173 static 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
192 void do_emu_action(void)
193 {
194         char buf[MAXPATHLEN];
195         int ret;
196
197         emu_action_old = emu_action;
198
199         switch (emu_action) {
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:
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();
230                 break;
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                 }
250         case SACTION_VOLUME_UP:
251         case SACTION_VOLUME_DOWN:
252                 plat_step_volume(emu_action == SACTION_VOLUME_UP);
253                 return;
254         case SACTION_MINIMIZE:
255                 plat_minimize();
256                 return;
257         default:
258                 return;
259         }
260         hud_new_msg = 3;
261         return;
262
263 do_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;
267         printf("* %s\n", hud_msg);
268 }
269
270 int main(int argc, char *argv[])
271 {
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
276         emuLog = stdout;
277         SetIsoFile(NULL);
278
279         memset(&Config, 0, sizeof(Config));
280
281         CheckSubDir();
282         set_default_paths();
283         emu_set_default_config();
284         check_memcards();
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;
294         const char *loadst_f = NULL;
295         int psxout = 0;
296         int loadst = 0;
297         int i;
298
299         // read command line options
300         for (i = 1; i < argc; i++) {
301                      if (!strcmp(argv[i], "-psxout")) psxout = 1;
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
323                         cdfile = isofilename;
324                 }
325                 else if (!strcmp(argv[i], "-loadf")) {
326                         if (i+1 >= argc) break;
327                         loadst_f = argv[++i];
328                 }
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
358         if (cdfile)
359                 set_cd_image(cdfile);
360
361         if (SysInit() == -1)
362                 return 1;
363
364         // frontend stuff
365         in_init();
366         in_evdev_init();
367         //in_probe();
368         pl_init();
369         plat_init();
370         menu_init(); // loads config
371
372         if (psxout)
373                 Config.PsxOut = 1;
374
375         if (LoadPlugins() == -1) {
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);
381                 SysMessage("Failed loading plugins!");
382                 return 1;
383         }
384         pcnt_hook_plugins();
385
386         if (OpenPlugins() == -1) {
387                 return 1;
388         }
389         plugin_call_rearmed_cbs();
390
391         CheckCdrom();
392         SysReset();
393
394         if (file[0] != '\0') {
395                 if (Load(file) != -1)
396                         ready_to_go = 1;
397         } else {
398                 if (cdfile) {
399                         if (LoadCdrom() == -1) {
400                                 ClosePlugins();
401                                 printf(_("Could not load CD-ROM!\n"));
402                                 return -1;
403                         }
404                         ready_to_go = 1;
405                 }
406         }
407
408         if (ready_to_go) {
409                 menu_prepare_emu();
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                 }
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                 }
420         }
421         else
422                 menu_loop();
423
424         pl_start_watchdog();
425
426         while (1)
427         {
428                 stop = 0;
429                 emu_action = SACTION_NONE;
430
431                 psxCpu->Execute();
432                 if (emu_action != SACTION_NONE)
433                         do_emu_action();
434         }
435
436         return 0;
437 #endif
438 }
439
440 int SysInit() {
441         if (EmuInit() == -1) {
442                 printf("PSX emulator couldn't be initialized.\n");
443                 return -1;
444         }
445
446         LoadMcds(Config.Mcd1, Config.Mcd2);
447
448         if (Config.Debug) {
449                 StartDebugger();
450         }
451
452         return 0;
453 }
454
455 void SysRunGui() {
456         printf("SysRunGui\n");
457 }
458
459 void StartGui() {
460         printf("StartGui\n");
461 }
462
463 static void dummy_lace()
464 {
465 }
466
467 void SysReset() {
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
474         // reset can run code, timing must be set
475         pl_timing_prepare(Config.PsxType);
476
477         EmuReset();
478
479         // hmh core forgets this
480         CDR_stop();
481
482         GPU_updateLace = real_lace;
483 }
484
485 void SysClose() {
486         EmuShutdown();
487         ReleasePlugins();
488
489         StopDebugger();
490
491         if (emuLog != NULL) fclose(emuLog);
492 }
493
494 void SysUpdate() {
495 }
496
497 void OnFile_Exit() {
498         printf("OnFile_Exit\n");
499 #ifndef MAEMO
500         menu_finish();
501 #endif
502         SysClose();
503         plat_finish();
504         exit(0);
505 }
506
507 int get_state_filename(char *buf, int size, int i) {
508         return get_gameid_filename(buf, size,
509                 "." STATES_DIR "%.32s-%.9s.%3.3d", i);
510 }
511
512 int 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
524 int 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
533         ret = SaveState(fname);
534         printf("* %s \"%s\" [%d]\n", ret == 0 ? "saved" : "failed to save", fname, slot);
535         return ret;
536 }
537
538 int emu_load_state(int slot)
539 {
540         char fname[MAXPATHLEN];
541         int ret;
542
543         ret = get_state_filename(fname, sizeof(fname), slot);
544         if (ret != 0)
545                 return ret;
546
547         return LoadState(fname);
548 }
549
550 void SysPrintf(const char *fmt, ...) {
551         va_list list;
552         char msg[512];
553
554         va_start(list, fmt);
555         vsprintf(msg, fmt, list);
556         va_end(list);
557
558         fprintf(emuLog, "%s", msg);
559 }
560
561 void SysMessage(const char *fmt, ...) {
562         va_list list;
563         char msg[512];
564
565         va_start(list, fmt);
566         vsprintf(msg, fmt, list);
567         va_end(list);
568
569         if (msg[strlen(msg) - 1] == '\n')
570                 msg[strlen(msg) - 1] = 0;
571
572         fprintf(stderr, "%s\n", msg);
573 }
574
575 static void SignalExit(int sig) {
576         ClosePlugins();
577         OnFile_Exit();
578 }
579
580 #define PARSEPATH(dst, src) \
581         ptr = src + strlen(src); \
582         while (*ptr != '\\' && ptr != src) ptr--; \
583         if (ptr != src) { \
584                 strcpy(dst, ptr+1); \
585         }
586
587 static int _OpenPlugins(void) {
588         int ret;
589
590         signal(SIGINT, SignalExit);
591         signal(SIGPIPE, SignalExit);
592
593         GPU_clearDynarec(clearDynarec);
594
595         ret = CDR_open();
596         if (ret < 0) { SysMessage(_("Error opening CD-ROM plugin!")); return -1; }
597         ret = SPU_open();
598         if (ret < 0) { SysMessage(_("Error opening SPU plugin!")); return -1; }
599         SPU_registerCallback(SPUirq);
600         // pcsx-rearmed: we handle gpu elsewhere
601         //ret = GPU_open(&gpuDisp, "PCSX", NULL);
602         //if (ret < 0) { SysMessage(_("Error opening GPU plugin!")); return -1; }
603         ret = PAD1_open(&gpuDisp);
604         if (ret < 0) { SysMessage(_("Error opening Controller 1 plugin!")); return -1; }
605         ret = PAD2_open(&gpuDisp);
606         if (ret < 0) { SysMessage(_("Error opening Controller 2 plugin!")); return -1; }
607
608         if (Config.UseNet && !NetOpened) {
609                 netInfo info;
610                 char path[MAXPATHLEN];
611                 char dotdir[MAXPATHLEN];
612
613                 MAKE_PATH(dotdir, "/.pcsx/plugins/", NULL);
614
615                 strcpy(info.EmuName, "PCSX " PACKAGE_VERSION);
616                 strncpy(info.CdromID, CdromId, 9);
617                 strncpy(info.CdromLabel, CdromLabel, 9);
618                 info.psxMem = psxM;
619                 info.GPU_showScreenPic = GPU_showScreenPic;
620                 info.GPU_displayText = GPU_displayText;
621                 info.GPU_showScreenPic = GPU_showScreenPic;
622                 info.PAD_setSensitive = PAD1_setSensitive;
623                 sprintf(path, "%s%s", Config.BiosDir, Config.Bios);
624                 strcpy(info.BIOSpath, path);
625                 strcpy(info.MCD1path, Config.Mcd1);
626                 strcpy(info.MCD2path, Config.Mcd2);
627                 sprintf(path, "%s%s", dotdir, Config.Gpu);
628                 strcpy(info.GPUpath, path);
629                 sprintf(path, "%s%s", dotdir, Config.Spu);
630                 strcpy(info.SPUpath, path);
631                 sprintf(path, "%s%s", dotdir, Config.Cdr);
632                 strcpy(info.CDRpath, path);
633                 NET_setInfo(&info);
634
635                 ret = NET_open(&gpuDisp);
636                 if (ret < 0) {
637                         if (ret == -2) {
638                                 // -2 is returned when something in the info
639                                 // changed and needs to be synced
640                                 char *ptr;
641
642                                 PARSEPATH(Config.Bios, info.BIOSpath);
643                                 PARSEPATH(Config.Gpu,  info.GPUpath);
644                                 PARSEPATH(Config.Spu,  info.SPUpath);
645                                 PARSEPATH(Config.Cdr,  info.CDRpath);
646
647                                 strcpy(Config.Mcd1, info.MCD1path);
648                                 strcpy(Config.Mcd2, info.MCD2path);
649                                 return -2;
650                         } else {
651                                 Config.UseNet = FALSE;
652                         }
653                 } else {
654                         if (NET_queryPlayer() == 1) {
655                                 if (SendPcsxInfo() == -1) Config.UseNet = FALSE;
656                         } else {
657                                 if (RecvPcsxInfo() == -1) Config.UseNet = FALSE;
658                         }
659                 }
660                 NetOpened = TRUE;
661         } else if (Config.UseNet) {
662                 NET_resume();
663         }
664
665         return 0;
666 }
667
668 int OpenPlugins() {
669         int ret;
670
671         while ((ret = _OpenPlugins()) == -2) {
672                 ReleasePlugins();
673                 LoadMcds(Config.Mcd1, Config.Mcd2);
674                 if (LoadPlugins() == -1) return -1;
675         }
676         return ret;
677 }
678
679 void ClosePlugins() {
680         int ret;
681
682         signal(SIGINT, SIG_DFL);
683         signal(SIGPIPE, SIG_DFL);
684         ret = CDR_close();
685         if (ret < 0) { SysMessage(_("Error closing CD-ROM plugin!")); return; }
686         ret = SPU_close();
687         if (ret < 0) { SysMessage(_("Error closing SPU plugin!")); return; }
688         ret = PAD1_close();
689         if (ret < 0) { SysMessage(_("Error closing Controller 1 Plugin!")); return; }
690         ret = PAD2_close();
691         if (ret < 0) { SysMessage(_("Error closing Controller 2 plugin!")); return; }
692         // pcsx-rearmed: we handle gpu elsewhere
693         //ret = GPU_close();
694         //if (ret < 0) { SysMessage(_("Error closing GPU plugin!")); return; }
695
696         if (Config.UseNet) {
697                 NET_pause();
698         }
699 }
700
701 /* we hook statically linked plugins here */
702 static const char *builtin_plugins[] = {
703         "builtin_gpu", "builtin_spu", "builtin_cdr", "builtin_pad",
704         "builtin_cdrcimg",
705 };
706
707 static const int builtin_plugin_ids[] = {
708         PLUGIN_GPU, PLUGIN_SPU, PLUGIN_CDR, PLUGIN_PAD,
709         PLUGIN_CDRCIMG,
710 };
711
712 void *SysLoadLibrary(const char *lib) {
713         const char *tmp = strrchr(lib, '/');
714         void *ret;
715         int i;
716
717         printf("plugin: %s\n", lib);
718
719         if (tmp != NULL) {
720                 tmp++;
721                 for (i = 0; i < ARRAY_SIZE(builtin_plugins); i++)
722                         if (strcmp(tmp, builtin_plugins[i]) == 0)
723                                 return (void *)(long)(PLUGIN_DL_BASE + builtin_plugin_ids[i]);
724         }
725
726 #if defined(__x86_64__) || defined(__i386__)
727         // convenience hack
728         char name[MAXPATHLEN];
729         snprintf(name, sizeof(name), "%s.x86", lib);
730         lib = name;
731 #endif
732
733         ret = dlopen(lib, RTLD_NOW);
734         if (ret == NULL)
735                 fprintf(stderr, "dlopen: %s\n", dlerror());
736         return ret;
737 }
738
739 void *SysLoadSym(void *lib, const char *sym) {
740         unsigned int plugid = (unsigned int)(long)lib;
741
742         if (PLUGIN_DL_BASE <= plugid && plugid < PLUGIN_DL_BASE + ARRAY_SIZE(builtin_plugins))
743                 return plugin_link(plugid - PLUGIN_DL_BASE, sym);
744
745         return dlsym(lib, sym);
746 }
747
748 const char *SysLibError() {
749         return dlerror();
750 }
751
752 void SysCloseLibrary(void *lib) {
753         unsigned int plugid = (unsigned int)(long)lib;
754
755         if (PLUGIN_DL_BASE <= plugid && plugid < PLUGIN_DL_BASE + ARRAY_SIZE(builtin_plugins))
756                 return;
757
758         dlclose(lib);
759 }
760