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