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