gte: parametrize remaining ops
[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 "../libpcsxcore/misc.h"
24 #include "../libpcsxcore/new_dynarec/new_dynarec.h"
25 #include "../plugins/cdrcimg/cdrcimg.h"
26 #include "common/plat.h"
27 #include "common/input.h"
28 #include "common/readpng.h"
29
30 // don't include debug.h - it breaks ARM build (R1 redefined)
31 void StartDebugger();
32 void StopDebugger();
33
34 int ready_to_go;
35 unsigned long gpuDisp;
36 char cfgfile_basename[MAXPATHLEN];
37 static char *(*real_getenv)(const char *name);
38 int state_slot;
39 enum sched_action emu_action, emu_action_old;
40 char hud_msg[64];
41 int hud_new_msg;
42
43 static void make_path(char *buf, size_t size, const char *dir, const char *fname)
44 {
45         if (fname)
46                 snprintf(buf, size, ".%s%s", dir, fname);
47         else
48                 snprintf(buf, size, ".%s", dir);
49 }
50 #define MAKE_PATH(buf, dir, fname) \
51         make_path(buf, sizeof(buf), dir, fname)
52
53 static void create_profile_dir(const char *directory) {
54         char path[MAXPATHLEN];
55
56         MAKE_PATH(path, directory, NULL);
57         mkdir(path, S_IRWXU | S_IRWXG);
58 }
59
60 static void CheckSubDir() {
61         // make sure that ~/.pcsx exists
62         create_profile_dir(PCSX_DOT_DIR);
63
64         create_profile_dir(BIOS_DIR);
65         create_profile_dir(MEMCARD_DIR);
66         create_profile_dir(STATES_DIR);
67         create_profile_dir(PLUGINS_DIR);
68         create_profile_dir(PLUGINS_CFG_DIR);
69         create_profile_dir(CHEATS_DIR);
70         create_profile_dir(PATCHES_DIR);
71         create_profile_dir(PCSX_DOT_DIR "cfg");
72         create_profile_dir("/screenshots/");
73 }
74
75 static int get_gameid_filename(char *buf, int size, const char *fmt, int i) {
76         char trimlabel[33];
77         int j;
78
79         strncpy(trimlabel, CdromLabel, 32);
80         trimlabel[32] = 0;
81         for (j = 31; j >= 0; j--)
82                 if (trimlabel[j] == ' ')
83                         trimlabel[j] = 0;
84                 else
85                         continue;
86
87         snprintf(buf, size, fmt, trimlabel, CdromId, i);
88
89         return 0;
90 }
91
92 void set_cd_image(const char *fname)
93 {
94         const char *ext = NULL;
95         
96         if (fname != NULL)
97                 ext = strrchr(fname, '.');
98
99         if (ext && (
100             strcasecmp(ext, ".z") == 0 || strcasecmp(ext, ".bz") == 0 ||
101             strcasecmp(ext, ".znx") == 0 /*|| strcasecmp(ext, ".pbp") == 0*/)) {
102                 SetIsoFile(NULL);
103                 cdrcimg_set_fname(fname);
104                 strcpy(Config.Cdr, "builtin_cdrcimg");
105         } else {
106                 SetIsoFile(fname);
107                 strcpy(Config.Cdr, "builtin_cdr");
108         }
109 }
110
111 static void set_default_paths(void)
112 {
113         MAKE_PATH(Config.Mcd1, MEMCARD_DIR, "card1.mcd");
114         MAKE_PATH(Config.Mcd2, MEMCARD_DIR, "card2.mcd");
115         strcpy(Config.BiosDir, "bios");
116
117         strcpy(Config.PluginsDir, "plugins");
118         strcpy(Config.Gpu, "builtin_gpu");
119         strcpy(Config.Spu, "builtin_spu");
120         strcpy(Config.Cdr, "builtin_cdr");
121         strcpy(Config.Pad1, "builtin_pad");
122         strcpy(Config.Pad2, "builtin_pad");
123         strcpy(Config.Net, "Disabled");
124 #if defined(__arm__) && !defined(__ARM_ARCH_7A__) /* XXX */
125         strcpy(Config.Gpu, "gpuPCSX4ALL.so");
126 #endif
127         Config.PsxAuto = 1;
128
129         snprintf(Config.PatchesDir, sizeof(Config.PatchesDir), "." PATCHES_DIR);
130 }
131
132 static void check_memcards(void)
133 {
134         char buf[MAXPATHLEN];
135         FILE *f;
136         int i;
137
138         for (i = 1; i <= 9; i++) {
139                 snprintf(buf, sizeof(buf), ".%scard%d.mcd", MEMCARD_DIR, i);
140
141                 f = fopen(buf, "rb");
142                 if (f == NULL) {
143                         printf("Creating memcard: %s\n", buf);
144                         CreateMcd(buf);
145                 }
146                 else
147                         fclose(f);
148         }
149 }
150
151 void do_emu_action(void)
152 {
153         char buf[MAXPATHLEN];
154         int ret;
155
156         emu_action_old = emu_action;
157
158         switch (emu_action) {
159         case SACTION_ENTER_MENU:
160                 menu_loop();
161                 return;
162         case SACTION_LOAD_STATE:
163                 ret = emu_load_state(state_slot);
164                 snprintf(hud_msg, sizeof(hud_msg), ret == 0 ? "LOADED" : "FAIL!");
165                 break;
166         case SACTION_SAVE_STATE:
167                 ret = emu_save_state(state_slot);
168                 snprintf(hud_msg, sizeof(hud_msg), ret == 0 ? "SAVED" : "FAIL!");
169                 break;
170         case SACTION_NEXT_SSLOT:
171                 state_slot++;
172                 if (state_slot > 9)
173                         state_slot = 0;
174                 goto do_state_slot;
175         case SACTION_PREV_SSLOT:
176                 state_slot--;
177                 if (state_slot < 0)
178                         state_slot = 9;
179                 goto do_state_slot;
180         case SACTION_TOGGLE_FSKIP:
181                 pl_rearmed_cbs.fskip_advice = 0;
182                 pl_rearmed_cbs.frameskip++;
183                 if (pl_rearmed_cbs.frameskip > 1)
184                         pl_rearmed_cbs.frameskip = -1;
185                 snprintf(hud_msg, sizeof(hud_msg), "FRAMESKIP: %s",
186                         pl_rearmed_cbs.frameskip == -1 ? "AUTO" :
187                         pl_rearmed_cbs.frameskip == 0 ? "OFF" : "1" );
188                 plugin_call_rearmed_cbs();
189                 break;
190         case SACTION_SCREENSHOT:
191                 {
192                         void *scrbuf;
193                         int w, h, bpp;
194                         time_t t = time(NULL);
195                         struct tm *tb = localtime(&t);
196                         int ti = tb->tm_yday * 1000000 + tb->tm_hour * 10000 +
197                                 tb->tm_min * 100 + tb->tm_sec;
198
199                         scrbuf = pl_prepare_screenshot(&w, &h, &bpp);
200                         get_gameid_filename(buf, sizeof(buf),
201                                 "screenshots/%.32s-%.9s.%d.png", ti);
202                         ret = -1;
203                         if (scrbuf != 0 && bpp == 16)
204                                 ret = writepng(buf, scrbuf, w, h);
205                         if (ret == 0)
206                                 snprintf(hud_msg, sizeof(hud_msg), "SCREENSHOT TAKEN");
207                         break;
208                 }
209         default:
210                 return;
211         }
212         hud_new_msg = 3;
213         return;
214
215 do_state_slot:
216         snprintf(hud_msg, sizeof(hud_msg), "STATE SLOT %d [%s]", state_slot,
217                 emu_check_state(state_slot) == 0 ? "USED" : "FREE");
218         hud_new_msg = 3;
219         printf("* %s\n", hud_msg);
220 }
221
222 int main(int argc, char *argv[])
223 {
224         void *tmp;
225
226         tmp = dlopen("/lib/libdl.so.2", RTLD_LAZY);
227         if (tmp == NULL)
228                 tmp = dlopen("/lib32/libdl.so.2", RTLD_LAZY);
229         if (tmp != NULL)
230                 real_getenv = dlsym(tmp, "getenv");
231         if (real_getenv == NULL) {
232                 fprintf(stderr, "%s\n", dlerror());
233                 return 1;
234         }
235         dlclose(tmp);
236
237         // what is the name of the config file?
238         // it may be redefined by -cfg on the command line
239         strcpy(cfgfile_basename, "pcsx.cfg");
240
241         emuLog = stdout;
242         SetIsoFile(NULL);
243
244         memset(&Config, 0, sizeof(Config));
245
246         CheckSubDir();
247         set_default_paths();
248         check_memcards();
249         strcpy(Config.Bios, "HLE");
250
251 #ifdef MAEMO
252         extern int maemo_main(int argc, char **argv);
253         return maemo_main(argc, argv);
254 #else
255         char file[MAXPATHLEN] = "";
256         char path[MAXPATHLEN];
257         const char *cdfile = NULL;
258         const char *loadst_f = NULL;
259         int psxout = 0;
260         int loadst = 0;
261         int i;
262
263         // read command line options
264         for (i = 1; i < argc; i++) {
265                      if (!strcmp(argv[i], "-psxout")) psxout = 1;
266                 else if (!strcmp(argv[i], "-load")) loadst = atol(argv[++i]);
267                 else if (!strcmp(argv[i], "-cfg")) {
268                         if (i+1 >= argc) break;
269                         strncpy(cfgfile_basename, argv[++i], MAXPATHLEN-100);   /* TODO buffer overruns */
270                         printf("Using config file %s.\n", cfgfile_basename);
271                 }
272                 else if (!strcmp(argv[i], "-cdfile")) {
273                         char isofilename[MAXPATHLEN];
274
275                         if (i+1 >= argc) break;
276                         strncpy(isofilename, argv[++i], MAXPATHLEN);
277                         if (isofilename[0] != '/') {
278                                 getcwd(path, MAXPATHLEN);
279                                 if (strlen(path) + strlen(isofilename) + 1 < MAXPATHLEN) {
280                                         strcat(path, "/");
281                                         strcat(path, isofilename);
282                                         strcpy(isofilename, path);
283                                 } else
284                                         isofilename[0] = 0;
285                         }
286
287                         cdfile = isofilename;
288                 }
289                 else if (!strcmp(argv[i], "-loadf")) {
290                         if (i+1 >= argc) break;
291                         loadst_f = argv[++i];
292                 }
293                 else if (!strcmp(argv[i], "-h") ||
294                          !strcmp(argv[i], "-help") ||
295                          !strcmp(argv[i], "--help")) {
296                          printf(PACKAGE_NAME " " PACKAGE_VERSION "\n");
297                          printf("%s\n", _(
298                                                         " pcsx [options] [file]\n"
299                                                         "\toptions:\n"
300                                                         "\t-cdfile FILE\tRuns a CD image file\n"
301                                                         "\t-nogui\t\tDon't open the GTK GUI\n"
302                                                         "\t-cfg FILE\tLoads desired configuration file (default: ~/.pcsx/pcsx.cfg)\n"
303                                                         "\t-psxout\t\tEnable PSX output\n"
304                                                         "\t-load STATENUM\tLoads savestate STATENUM (1-5)\n"
305                                                         "\t-h -help\tDisplay this message\n"
306                                                         "\tfile\t\tLoads file\n"));
307                          return 0;
308                 } else {
309                         strncpy(file, argv[i], MAXPATHLEN);
310                         if (file[0] != '/') {
311                                 getcwd(path, MAXPATHLEN);
312                                 if (strlen(path) + strlen(file) + 1 < MAXPATHLEN) {
313                                         strcat(path, "/");
314                                         strcat(path, file);
315                                         strcpy(file, path);
316                                 } else
317                                         file[0] = 0;
318                         }
319                 }
320         }
321
322         if (cdfile)
323                 set_cd_image(cdfile);
324
325         if (SysInit() == -1)
326                 return 1;
327
328         // frontend stuff
329         in_init();
330         //in_probe();
331         plat_init();
332         menu_init(); // loads config
333         pl_init();
334
335         if (psxout)
336                 Config.PsxOut = 1;
337
338         if (LoadPlugins() == -1) {
339                 // FIXME: this recovery doesn't work, just delete bad config and bail out
340                 // SysMessage("could not load plugins, retrying with defaults\n");
341                 set_default_paths();
342                 snprintf(path, sizeof(path), "." PCSX_DOT_DIR "%s", cfgfile_basename);
343                 remove(path);
344                 SysMessage("Failed loading plugins!");
345                 return 1;
346         }
347         pcnt_hook_plugins();
348
349         if (OpenPlugins() == -1) {
350                 return 1;
351         }
352         plugin_call_rearmed_cbs();
353
354         CheckCdrom();
355         SysReset();
356
357         if (file[0] != '\0') {
358                 if (Load(file) != -1)
359                         ready_to_go = 1;
360         } else {
361                 if (cdfile) {
362                         if (LoadCdrom() == -1) {
363                                 ClosePlugins();
364                                 printf(_("Could not load CD-ROM!\n"));
365                                 return -1;
366                         }
367                         ready_to_go = 1;
368                 }
369         }
370
371         if (ready_to_go) {
372                 menu_prepare_emu();
373
374                 // If a state has been specified, then load that
375                 if (loadst) {
376                         int ret = emu_load_state(loadst - 1);
377                         printf("%s state %d\n", ret ? "failed to load" : "loaded", loadst);
378                 }
379                 if (loadst_f) {
380                         int ret = LoadState(loadst_f);
381                         printf("%s state file: %s\n", ret ? "failed to load" : "loaded", loadst_f);
382                 }
383         }
384         else
385                 menu_loop();
386
387         pl_start_watchdog();
388
389         while (1)
390         {
391                 stop = 0;
392                 emu_action = SACTION_NONE;
393
394                 psxCpu->Execute();
395                 if (emu_action != SACTION_NONE)
396                         do_emu_action();
397         }
398
399         return 0;
400 #endif
401 }
402
403 int SysInit() {
404         if (EmuInit() == -1) {
405                 printf("PSX emulator couldn't be initialized.\n");
406                 return -1;
407         }
408
409         LoadMcds(Config.Mcd1, Config.Mcd2);
410
411         if (Config.Debug) {
412                 StartDebugger();
413         }
414
415         return 0;
416 }
417
418 void SysRunGui() {
419         printf("SysRunGui\n");
420 }
421
422 void StartGui() {
423         printf("StartGui\n");
424 }
425
426 static void dummy_lace()
427 {
428 }
429
430 void SysReset() {
431         // rearmed hack: EmuReset() runs some code when real BIOS is used,
432         // but we usually do reset from menu while GPU is not open yet,
433         // so we need to prevent updateLace() call..
434         void *real_lace = GPU_updateLace;
435         GPU_updateLace = dummy_lace;
436
437         EmuReset();
438
439         // hmh core forgets this
440         CDR_stop();
441
442         GPU_updateLace = real_lace;
443 }
444
445 void SysClose() {
446         EmuShutdown();
447         ReleasePlugins();
448
449         StopDebugger();
450
451         if (emuLog != NULL) fclose(emuLog);
452 }
453
454 void SysUpdate() {
455 }
456
457 void OnFile_Exit() {
458         printf("OnFile_Exit\n");
459 #ifndef MAEMO
460         menu_finish();
461 #endif
462         SysClose();
463         plat_finish();
464         exit(0);
465 }
466
467 int get_state_filename(char *buf, int size, int i) {
468         return get_gameid_filename(buf, size,
469                 "." STATES_DIR "%.32s-%.9s.%3.3d", i);
470 }
471
472 int emu_check_state(int slot)
473 {
474         char fname[MAXPATHLEN];
475         int ret;
476
477         ret = get_state_filename(fname, sizeof(fname), slot);
478         if (ret != 0)
479                 return ret;
480
481         return CheckState(fname);
482 }
483
484 int emu_save_state(int slot)
485 {
486         char fname[MAXPATHLEN];
487         int ret;
488
489         ret = get_state_filename(fname, sizeof(fname), slot);
490         if (ret != 0)
491                 return ret;
492
493         ret = SaveState(fname);
494         printf("* %s \"%s\" [%d]\n", ret == 0 ? "saved" : "failed to save", fname, slot);
495         return ret;
496 }
497
498 int emu_load_state(int slot)
499 {
500         char fname[MAXPATHLEN];
501         int ret;
502
503         ret = get_state_filename(fname, sizeof(fname), slot);
504         if (ret != 0)
505                 return ret;
506
507         return LoadState(fname);
508 }
509
510 void SysPrintf(const char *fmt, ...) {
511         va_list list;
512         char msg[512];
513
514         va_start(list, fmt);
515         vsprintf(msg, fmt, list);
516         va_end(list);
517
518         fprintf(emuLog, "%s", msg);
519 }
520
521 void SysMessage(const char *fmt, ...) {
522         va_list list;
523         char msg[512];
524
525         va_start(list, fmt);
526         vsprintf(msg, fmt, list);
527         va_end(list);
528
529         if (msg[strlen(msg) - 1] == '\n')
530                 msg[strlen(msg) - 1] = 0;
531
532         fprintf(stderr, "%s\n", msg);
533 }
534
535 static void SignalExit(int sig) {
536         ClosePlugins();
537         OnFile_Exit();
538 }
539
540 #define PARSEPATH(dst, src) \
541         ptr = src + strlen(src); \
542         while (*ptr != '\\' && ptr != src) ptr--; \
543         if (ptr != src) { \
544                 strcpy(dst, ptr+1); \
545         }
546
547 static int _OpenPlugins(void) {
548         int ret;
549
550         signal(SIGINT, SignalExit);
551         signal(SIGPIPE, SignalExit);
552
553         GPU_clearDynarec(clearDynarec);
554
555         ret = CDR_open();
556         if (ret < 0) { SysMessage(_("Error opening CD-ROM plugin!")); return -1; }
557         ret = SPU_open();
558         if (ret < 0) { SysMessage(_("Error opening SPU plugin!")); return -1; }
559         SPU_registerCallback(SPUirq);
560         // pcsx-rearmed: we handle gpu elsewhere
561         //ret = GPU_open(&gpuDisp, "PCSX", NULL);
562         //if (ret < 0) { SysMessage(_("Error opening GPU plugin!")); return -1; }
563         ret = PAD1_open(&gpuDisp);
564         if (ret < 0) { SysMessage(_("Error opening Controller 1 plugin!")); return -1; }
565         ret = PAD2_open(&gpuDisp);
566         if (ret < 0) { SysMessage(_("Error opening Controller 2 plugin!")); return -1; }
567
568         if (Config.UseNet && !NetOpened) {
569                 netInfo info;
570                 char path[MAXPATHLEN];
571                 char dotdir[MAXPATHLEN];
572
573                 MAKE_PATH(dotdir, "/.pcsx/plugins/", NULL);
574
575                 strcpy(info.EmuName, "PCSX " PACKAGE_VERSION);
576                 strncpy(info.CdromID, CdromId, 9);
577                 strncpy(info.CdromLabel, CdromLabel, 9);
578                 info.psxMem = psxM;
579                 info.GPU_showScreenPic = GPU_showScreenPic;
580                 info.GPU_displayText = GPU_displayText;
581                 info.GPU_showScreenPic = GPU_showScreenPic;
582                 info.PAD_setSensitive = PAD1_setSensitive;
583                 sprintf(path, "%s%s", Config.BiosDir, Config.Bios);
584                 strcpy(info.BIOSpath, path);
585                 strcpy(info.MCD1path, Config.Mcd1);
586                 strcpy(info.MCD2path, Config.Mcd2);
587                 sprintf(path, "%s%s", dotdir, Config.Gpu);
588                 strcpy(info.GPUpath, path);
589                 sprintf(path, "%s%s", dotdir, Config.Spu);
590                 strcpy(info.SPUpath, path);
591                 sprintf(path, "%s%s", dotdir, Config.Cdr);
592                 strcpy(info.CDRpath, path);
593                 NET_setInfo(&info);
594
595                 ret = NET_open(&gpuDisp);
596                 if (ret < 0) {
597                         if (ret == -2) {
598                                 // -2 is returned when something in the info
599                                 // changed and needs to be synced
600                                 char *ptr;
601
602                                 PARSEPATH(Config.Bios, info.BIOSpath);
603                                 PARSEPATH(Config.Gpu,  info.GPUpath);
604                                 PARSEPATH(Config.Spu,  info.SPUpath);
605                                 PARSEPATH(Config.Cdr,  info.CDRpath);
606
607                                 strcpy(Config.Mcd1, info.MCD1path);
608                                 strcpy(Config.Mcd2, info.MCD2path);
609                                 return -2;
610                         } else {
611                                 Config.UseNet = FALSE;
612                         }
613                 } else {
614                         if (NET_queryPlayer() == 1) {
615                                 if (SendPcsxInfo() == -1) Config.UseNet = FALSE;
616                         } else {
617                                 if (RecvPcsxInfo() == -1) Config.UseNet = FALSE;
618                         }
619                 }
620                 NetOpened = TRUE;
621         } else if (Config.UseNet) {
622                 NET_resume();
623         }
624
625         return 0;
626 }
627
628 int OpenPlugins() {
629         int ret;
630
631         while ((ret = _OpenPlugins()) == -2) {
632                 ReleasePlugins();
633                 LoadMcds(Config.Mcd1, Config.Mcd2);
634                 if (LoadPlugins() == -1) return -1;
635         }
636         return ret;
637 }
638
639 void ClosePlugins() {
640         int ret;
641
642         signal(SIGINT, SIG_DFL);
643         signal(SIGPIPE, SIG_DFL);
644         ret = CDR_close();
645         if (ret < 0) { SysMessage(_("Error closing CD-ROM plugin!")); return; }
646         ret = SPU_close();
647         if (ret < 0) { SysMessage(_("Error closing SPU plugin!")); return; }
648         ret = PAD1_close();
649         if (ret < 0) { SysMessage(_("Error closing Controller 1 Plugin!")); return; }
650         ret = PAD2_close();
651         if (ret < 0) { SysMessage(_("Error closing Controller 2 plugin!")); return; }
652         // pcsx-rearmed: we handle gpu elsewhere
653         //ret = GPU_close();
654         //if (ret < 0) { SysMessage(_("Error closing GPU plugin!")); return; }
655
656         if (Config.UseNet) {
657                 NET_pause();
658         }
659 }
660
661 #if 1
662 /* this is to avoid having to hack every plugin to stop using $HOME */
663 char *getenv(const char *name)
664 {
665         static char ret[8] = ".";
666
667         if (name && strcmp(name, "HOME") == 0 &&
668                         ((int)name >> 28) == 0) // HACK: let libs find home
669                 return ret;
670
671         return real_getenv(name);
672 }
673 #endif
674
675 /* we hook statically linked plugins here */
676 static const char *builtin_plugins[] = {
677         "builtin_gpu", "builtin_spu", "builtin_cdr", "builtin_pad",
678         "builtin_cdrcimg",
679 };
680
681 static const int builtin_plugin_ids[] = {
682         PLUGIN_GPU, PLUGIN_SPU, PLUGIN_CDR, PLUGIN_PAD,
683         PLUGIN_CDRCIMG,
684 };
685
686 void *SysLoadLibrary(const char *lib) {
687         const char *tmp = strrchr(lib, '/');
688         void *ret;
689         int i;
690
691         printf("plugin: %s\n", lib);
692
693         if (tmp != NULL) {
694                 tmp++;
695                 for (i = 0; i < ARRAY_SIZE(builtin_plugins); i++)
696                         if (strcmp(tmp, builtin_plugins[i]) == 0)
697                                 return (void *)(long)(PLUGIN_DL_BASE + builtin_plugin_ids[i]);
698         }
699
700 #if defined(__x86_64__) || defined(__i386__)
701         // convenience hack
702         char name[MAXPATHLEN];
703         snprintf(name, sizeof(name), "%s.x86", lib);
704         lib = name;
705 #endif
706
707         ret = dlopen(lib, RTLD_NOW);
708         if (ret == NULL)
709                 fprintf(stderr, "dlopen: %s\n", dlerror());
710         return ret;
711 }
712
713 void *SysLoadSym(void *lib, const char *sym) {
714         unsigned int plugid = (unsigned int)(long)lib;
715
716         if (PLUGIN_DL_BASE <= plugid && plugid < PLUGIN_DL_BASE + ARRAY_SIZE(builtin_plugins))
717                 return plugin_link(plugid - PLUGIN_DL_BASE, sym);
718
719         return dlsym(lib, sym);
720 }
721
722 const char *SysLibError() {
723         return dlerror();
724 }
725
726 void SysCloseLibrary(void *lib) {
727         unsigned int plugid = (unsigned int)(long)lib;
728
729         if (PLUGIN_DL_BASE <= plugid && plugid < PLUGIN_DL_BASE + ARRAY_SIZE(builtin_plugins))
730                 return;
731
732         dlclose(lib);
733 }
734