frontend: always detect region
[pcsx_rearmed.git] / frontend / main.c
CommitLineData
e906c010 1/*
201c21e2 2 * (C) notaz, 2010-2011
e906c010 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
80c2304e 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>
c5061935 15#include <signal.h>
29a8c4f3 16#include <time.h>
80c2304e 17
c5061935 18#include "main.h"
e906c010 19#include "plugin.h"
7c0f51de 20#include "plugin_lib.h"
14dffdb7 21#include "pcnt.h"
3c70c47b 22#include "menu.h"
80c2304e 23#include "../libpcsxcore/misc.h"
799b0b87 24#include "../libpcsxcore/new_dynarec/new_dynarec.h"
47bf65ab 25#include "../plugins/cdrcimg/cdrcimg.h"
69af03a2 26#include "common/plat.h"
27#include "common/input.h"
29a8c4f3 28#include "common/readpng.h"
80c2304e 29
69af03a2 30int ready_to_go;
c5061935 31unsigned long gpuDisp;
32char cfgfile_basename[MAXPATHLEN];
b60f2812 33static char *(*real_getenv)(const char *name);
8f892648 34int state_slot;
35enum sched_action emu_action, emu_action_old;
36char hud_msg[64];
37int hud_new_msg;
38
80c2304e 39static 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
49static 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
56static 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);
cd6e8d0f 67 create_profile_dir(PCSX_DOT_DIR "cfg");
29a8c4f3 68 create_profile_dir("/screenshots/");
69}
70
71static 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;
80c2304e 86}
87
47bf65ab 88void set_cd_image(const char *fname)
89{
e16a7e51 90 const char *ext = NULL;
47bf65ab 91
33716956 92 if (fname != NULL)
93 ext = strrchr(fname, '.');
47bf65ab 94
33716956 95 if (ext && (
96 strcasecmp(ext, ".z") == 0 || strcasecmp(ext, ".bz") == 0 ||
9a92bffb 97 strcasecmp(ext, ".znx") == 0 /*|| strcasecmp(ext, ".pbp") == 0*/)) {
47bf65ab 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
6fe1f056 107static 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
51f77282 125static 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
8f892648 144void do_emu_action(void)
145{
29a8c4f3 146 char buf[MAXPATHLEN];
8f892648 147 int ret;
148
149 emu_action_old = emu_action;
150
151 switch (emu_action) {
8f892648 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:
ea4a16e7 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();
8f892648 182 break;
29a8c4f3 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 }
4c08b9e7 202 default:
203 return;
8f892648 204 }
205 hud_new_msg = 3;
206 return;
207
208do_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;
4c08b9e7 212 printf("* %s\n", hud_msg);
8f892648 213}
214
80c2304e 215int main(int argc, char *argv[])
216{
b60f2812 217 void *tmp;
80c2304e 218
b60f2812 219 tmp = dlopen("/lib/libdl.so.2", RTLD_LAZY);
fa9cfe0a 220 if (tmp == NULL)
221 tmp = dlopen("/lib32/libdl.so.2", RTLD_LAZY);
b60f2812 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
80c2304e 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
e906c010 234 emuLog = stdout;
80c2304e 235 SetIsoFile(NULL);
80c2304e 236
e0c692d9 237 memset(&Config, 0, sizeof(Config));
238
239 CheckSubDir();
240 set_default_paths();
51f77282 241 check_memcards();
e0c692d9 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;
de910c6b 251 const char *loadst_f = NULL;
0a151868 252 int psxout = 0;
e0c692d9 253 int loadst = 0;
254 int i;
255
80c2304e 256 // read command line options
257 for (i = 1; i < argc; i++) {
0a151868 258 if (!strcmp(argv[i], "-psxout")) psxout = 1;
80c2304e 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
47bf65ab 280 cdfile = isofilename;
80c2304e 281 }
de910c6b 282 else if (!strcmp(argv[i], "-loadf")) {
283 if (i+1 >= argc) break;
284 loadst_f = argv[++i];
285 }
80c2304e 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
47bf65ab 315 if (cdfile)
316 set_cd_image(cdfile);
317
69af03a2 318 if (SysInit() == -1)
319 return 1;
80c2304e 320
69af03a2 321 // frontend stuff
322 in_init();
ef94866c 323 //in_probe();
69af03a2 324 plat_init();
0a151868 325 menu_init(); // loads config
4c08b9e7 326 pl_init();
0a151868 327
328 if (psxout)
329 Config.PsxOut = 1;
80c2304e 330
69af03a2 331 if (LoadPlugins() == -1) {
6fe1f056 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);
69af03a2 337 SysMessage("Failed loading plugins!");
338 return 1;
339 }
340 pcnt_hook_plugins();
80c2304e 341
69af03a2 342 if (OpenPlugins() == -1) {
343 return 1;
344 }
201c21e2 345 plugin_call_rearmed_cbs();
80c2304e 346
69af03a2 347 CheckCdrom();
bbd837c6 348 SysReset();
69af03a2 349
350 if (file[0] != '\0') {
351 if (Load(file) != -1)
352 ready_to_go = 1;
353 } else {
47bf65ab 354 if (cdfile) {
69af03a2 355 if (LoadCdrom() == -1) {
356 ClosePlugins();
357 printf(_("Could not load CD-ROM!\n"));
358 return -1;
80c2304e 359 }
69af03a2 360 ready_to_go = 1;
80c2304e 361 }
69af03a2 362 }
80c2304e 363
45d45c1e 364 if (ready_to_go) {
3c70c47b 365 menu_prepare_emu();
45d45c1e 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 }
de910c6b 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 }
45d45c1e 376 }
3c70c47b 377 else
69af03a2 378 menu_loop();
379
7c0f51de 380 pl_start_watchdog();
381
69af03a2 382 while (1)
383 {
799b0b87 384 stop = 0;
8f892648 385 emu_action = SACTION_NONE;
386
80c2304e 387 psxCpu->Execute();
8f892648 388 if (emu_action != SACTION_NONE)
389 do_emu_action();
80c2304e 390 }
391
392 return 0;
e0c692d9 393#endif
80c2304e 394}
395
396int SysInit() {
80c2304e 397 if (EmuInit() == -1) {
398 printf("PSX emulator couldn't be initialized.\n");
399 return -1;
400 }
401
51f77282 402 LoadMcds(Config.Mcd1, Config.Mcd2);
80c2304e 403
404 if (Config.Debug) {
405 StartDebugger();
406 }
407
408 return 0;
409}
410
411void SysRunGui() {
412 printf("SysRunGui\n");
413}
414
415void StartGui() {
416 printf("StartGui\n");
417}
418
e6eb2066 419static void dummy_lace()
420{
421}
422
80c2304e 423void SysReset() {
e6eb2066 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
80c2304e 430 EmuReset();
bd6267e6 431
432 // hmh core forgets this
433 CDR_stop();
e6eb2066 434
435 GPU_updateLace = real_lace;
80c2304e 436}
437
438void SysClose() {
439 EmuShutdown();
440 ReleasePlugins();
441
442 StopDebugger();
443
444 if (emuLog != NULL) fclose(emuLog);
445}
446
447void SysUpdate() {
80c2304e 448}
449
450void OnFile_Exit() {
bd6267e6 451 printf("OnFile_Exit\n");
e0c692d9 452#ifndef MAEMO
201c21e2 453 menu_finish();
e0c692d9 454#endif
bd6267e6 455 SysClose();
2c886904 456 plat_finish();
80c2304e 457 exit(0);
458}
459
c5061935 460int get_state_filename(char *buf, int size, int i) {
29a8c4f3 461 return get_gameid_filename(buf, size,
462 "." STATES_DIR "%.32s-%.9s.%3.3d", i);
80c2304e 463}
464
8f892648 465int 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
477int 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
90f1d26c 486 ret = SaveState(fname);
487 printf("* %s \"%s\" [%d]\n", ret == 0 ? "saved" : "failed to save", fname, slot);
488 return ret;
8f892648 489}
490
491int 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
80c2304e 503void 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
80c2304e 511 fprintf(emuLog, "%s", msg);
512}
513
514void 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
c5061935 528static 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
540static 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
621int 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
632void 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
e906c010 654#if 1
655/* this is to avoid having to hack every plugin to stop using $HOME */
656char *getenv(const char *name)
657{
658 static char ret[8] = ".";
659
69af03a2 660 if (name && strcmp(name, "HOME") == 0 &&
661 ((int)name >> 28) == 0) // HACK: let libs find home
b60f2812 662 return ret;
e906c010 663
b60f2812 664 return real_getenv(name);
e906c010 665}
666#endif
667
668/* we hook statically linked plugins here */
669static const char *builtin_plugins[] = {
47bf65ab 670 "builtin_gpu", "builtin_spu", "builtin_cdr", "builtin_pad",
671 "builtin_cdrcimg",
e906c010 672};
673
674static const int builtin_plugin_ids[] = {
675 PLUGIN_GPU, PLUGIN_SPU, PLUGIN_CDR, PLUGIN_PAD,
47bf65ab 676 PLUGIN_CDRCIMG,
e906c010 677};
678
80c2304e 679void *SysLoadLibrary(const char *lib) {
e906c010 680 const char *tmp = strrchr(lib, '/');
bbd837c6 681 void *ret;
e906c010 682 int i;
683
bbd837c6 684 printf("plugin: %s\n", lib);
685
e906c010 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
2185e39b 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
bbd837c6 700 ret = dlopen(lib, RTLD_NOW);
701 if (ret == NULL)
702 fprintf(stderr, "dlopen: %s\n", dlerror());
703 return ret;
80c2304e 704}
705
706void *SysLoadSym(void *lib, const char *sym) {
e906c010 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
80c2304e 712 return dlsym(lib, sym);
713}
714
715const char *SysLibError() {
716 return dlerror();
717}
718
719void SysCloseLibrary(void *lib) {
e906c010 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
80c2304e 725 dlclose(lib);
726}
727