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