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