frontend: add renderer toggle option
[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"
47821672 23#include "plat.h"
80c2304e 24#include "../libpcsxcore/misc.h"
9c27c205 25#include "../libpcsxcore/cheat.h"
799b0b87 26#include "../libpcsxcore/new_dynarec/new_dynarec.h"
47bf65ab 27#include "../plugins/cdrcimg/cdrcimg.h"
69af03a2 28#include "common/plat.h"
29a8c4f3 29#include "common/readpng.h"
f2de172b 30#include "common/input.h"
31#include "linux/in_evdev.h"
38c2028e 32#include "revision.h"
80c2304e 33
fc8145b7 34// don't include debug.h - it breaks ARM build (R1 redefined)
35void StartDebugger();
36void StopDebugger();
37
33400707 38// sound plugin
39extern int iUseReverb;
40extern int iUseInterpolation;
41extern int iXAPitch;
33400707 42extern int iVolume;
43
69af03a2 44int ready_to_go;
c5061935 45unsigned long gpuDisp;
46char cfgfile_basename[MAXPATHLEN];
8f892648 47int state_slot;
48enum sched_action emu_action, emu_action_old;
49char hud_msg[64];
50int hud_new_msg;
51
80c2304e 52static 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
62static 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
69static 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);
cd6e8d0f 80 create_profile_dir(PCSX_DOT_DIR "cfg");
29a8c4f3 81 create_profile_dir("/screenshots/");
82}
83
84static 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;
80c2304e 99}
100
47bf65ab 101void set_cd_image(const char *fname)
102{
e16a7e51 103 const char *ext = NULL;
47bf65ab 104
33716956 105 if (fname != NULL)
106 ext = strrchr(fname, '.');
47bf65ab 107
33716956 108 if (ext && (
109 strcasecmp(ext, ".z") == 0 || strcasecmp(ext, ".bz") == 0 ||
9a92bffb 110 strcasecmp(ext, ".znx") == 0 /*|| strcasecmp(ext, ".pbp") == 0*/)) {
47bf65ab 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
6fe1f056 120static 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");
6fe1f056 133
134 snprintf(Config.PatchesDir, sizeof(Config.PatchesDir), "." PATCHES_DIR);
135}
136
33400707 137void emu_set_default_config(void)
138{
139 // try to set sane config on which most games work
140 Config.Xa = Config.Cdda = Config.Sio =
141 Config.SpuIrq = Config.RCntFix = Config.VSyncWA = 0;
142 Config.CdrReschedule = 0;
143 Config.PsxAuto = 1;
144
746fee51 145 pl_rearmed_cbs.gpu_neon.allow_interlace = 2; // auto
0b02eb77 146 pl_rearmed_cbs.gpu_neon.enhancement_enable =
147 pl_rearmed_cbs.gpu_neon.enhancement_no_main = 0;
33400707 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;
e60da159 153 memset(&pl_rearmed_cbs.gpu_peopsgl, 0, sizeof(pl_rearmed_cbs.gpu_peopsgl));
746fee51 154 pl_rearmed_cbs.gpu_peopsgl.iVRamSize = 64;
cbb26048 155 pl_rearmed_cbs.gpu_peopsgl.iTexGarbageCollection = 1;
33400707 156
157 iUseReverb = 2;
158 iUseInterpolation = 1;
159 iXAPitch = 0;
33400707 160 iVolume = 768;
161#ifndef __ARM_ARCH_7A__ /* XXX */
162 iUseReverb = 0;
163 iUseInterpolation = 0;
164#endif
165 new_dynarec_hacks = 0;
166 cycle_multiplier = 200;
167
168 in_type1 = PSE_PAD_TYPE_STANDARD;
169 in_type2 = PSE_PAD_TYPE_STANDARD;
170}
171
51f77282 172static void check_memcards(void)
173{
174 char buf[MAXPATHLEN];
175 FILE *f;
176 int i;
177
178 for (i = 1; i <= 9; i++) {
179 snprintf(buf, sizeof(buf), ".%scard%d.mcd", MEMCARD_DIR, i);
180
181 f = fopen(buf, "rb");
182 if (f == NULL) {
183 printf("Creating memcard: %s\n", buf);
184 CreateMcd(buf);
185 }
186 else
187 fclose(f);
188 }
189}
190
8f892648 191void do_emu_action(void)
192{
193 int ret;
194
195 emu_action_old = emu_action;
196
197 switch (emu_action) {
8f892648 198 case SACTION_LOAD_STATE:
199 ret = emu_load_state(state_slot);
200 snprintf(hud_msg, sizeof(hud_msg), ret == 0 ? "LOADED" : "FAIL!");
201 break;
202 case SACTION_SAVE_STATE:
203 ret = emu_save_state(state_slot);
204 snprintf(hud_msg, sizeof(hud_msg), ret == 0 ? "SAVED" : "FAIL!");
205 break;
38c2028e 206#ifndef NO_FRONTEND
207 case SACTION_ENTER_MENU:
208 menu_loop();
209 return;
8f892648 210 case SACTION_NEXT_SSLOT:
211 state_slot++;
212 if (state_slot > 9)
213 state_slot = 0;
214 goto do_state_slot;
215 case SACTION_PREV_SSLOT:
216 state_slot--;
217 if (state_slot < 0)
218 state_slot = 9;
38c2028e 219do_state_slot:
220 snprintf(hud_msg, sizeof(hud_msg), "STATE SLOT %d [%s]", state_slot,
221 emu_check_state(state_slot) == 0 ? "USED" : "FREE");
222 hud_new_msg = 3;
223 printf("* %s\n", hud_msg);
224 break;
8f892648 225 case SACTION_TOGGLE_FSKIP:
ea4a16e7 226 pl_rearmed_cbs.fskip_advice = 0;
227 pl_rearmed_cbs.frameskip++;
228 if (pl_rearmed_cbs.frameskip > 1)
229 pl_rearmed_cbs.frameskip = -1;
230 snprintf(hud_msg, sizeof(hud_msg), "FRAMESKIP: %s",
231 pl_rearmed_cbs.frameskip == -1 ? "AUTO" :
232 pl_rearmed_cbs.frameskip == 0 ? "OFF" : "1" );
233 plugin_call_rearmed_cbs();
8f892648 234 break;
d81b8e97 235 case SACTION_TOGGLE_RENDERER:
236 pl_rearmed_cbs.gpu_neon.enhancement_enable =
237 !pl_rearmed_cbs.gpu_neon.enhancement_enable;
238 plugin_call_rearmed_cbs();
239 break;
29a8c4f3 240 case SACTION_SCREENSHOT:
241 {
38c2028e 242 char buf[MAXPATHLEN];
29a8c4f3 243 void *scrbuf;
244 int w, h, bpp;
245 time_t t = time(NULL);
246 struct tm *tb = localtime(&t);
247 int ti = tb->tm_yday * 1000000 + tb->tm_hour * 10000 +
248 tb->tm_min * 100 + tb->tm_sec;
249
250 scrbuf = pl_prepare_screenshot(&w, &h, &bpp);
251 get_gameid_filename(buf, sizeof(buf),
252 "screenshots/%.32s-%.9s.%d.png", ti);
253 ret = -1;
254 if (scrbuf != 0 && bpp == 16)
255 ret = writepng(buf, scrbuf, w, h);
256 if (ret == 0)
257 snprintf(hud_msg, sizeof(hud_msg), "SCREENSHOT TAKEN");
258 break;
259 }
221be40d 260 case SACTION_VOLUME_UP:
261 case SACTION_VOLUME_DOWN:
262 plat_step_volume(emu_action == SACTION_VOLUME_UP);
263 return;
a805c855 264 case SACTION_MINIMIZE:
36dfb787 265 if (GPU_close != NULL)
266 GPU_close();
267
a805c855 268 plat_minimize();
36dfb787 269
270 if (GPU_open != NULL) {
271 ret = GPU_open(&gpuDisp, "PCSX", NULL);
272 if (ret)
273 fprintf(stderr, "GPU_open returned %d\n", ret);
274 }
a805c855 275 return;
38c2028e 276#endif
4c08b9e7 277 default:
278 return;
8f892648 279 }
8f892648 280
8f892648 281 hud_new_msg = 3;
282}
283
9c27c205 284static int cdidcmp(const char *id1, const char *id2)
285{
286 while (*id1 != 0 && *id2 != 0) {
287 if (*id1 == '_') { id1++; continue; }
288 if (*id2 == '_') { id2++; continue; }
289 if (*id1 != *id2)
290 break;
291 id1++;
292 id2++;
293 }
294
295 return *id1 - *id2;
296}
297
298static void parse_cwcheat(void)
299{
300 char line[256], buf[64], name[64], *p;
301 int newcheat = 1;
302 u32 a, v;
303 FILE *f;
304
305 f = fopen("cheatpops.db", "r");
306 if (f == NULL)
307 return;
308
309 /* find the game */
310 while (fgets(line, sizeof(line), f)) {
311 if (sscanf(line, "_S %63s", buf) != 1)
312 continue;
313 if (cdidcmp(buf, CdromId) == 0)
314 break;
315 }
316
317 if (feof(f))
318 goto out;
319
320 printf("cwcheat section found for %s\n", CdromId);
321 while (fgets(line, sizeof(line), f))
322 {
323 p = line + strlen(line);
324 for (p--; p >= line && (*p == '\r' || *p == '\n' || *p == ' '); p--)
325 *p = 0;
326 if (*p == 0 || *p == '#' || *p == ';')
327 continue;
328
329 if (strncmp(line, "_S", 2) == 0)
330 break;
331 if (strncmp(line, "_G", 2) == 0) {
332 printf(" cwcheat game name: '%s'\n", line + 3);
333 continue;
334 }
335 if (strncmp(line, "_C0", 3) == 0) {
336 if (!newcheat && Cheats[NumCheats - 1].n == 0) {
337 printf("cheat '%s' failed to parse\n", name);
338 free(Cheats[NumCheats - 1].Descr);
339 NumCheats--;
340 }
341 snprintf(name, sizeof(name), "%s", line + 4);
342 newcheat = 1;
343 continue;
344 }
345 if (sscanf(line, "_L %x %x", &a, &v) != 2) {
346 printf("line failed to parse: '%s'\n", line);
347 continue;
348 }
349
350 if (newcheat) {
351 if (NumCheats >= NumCheatsAllocated) {
352 NumCheatsAllocated += 16;
353 Cheats = realloc(Cheats, sizeof(Cheat) *
354 NumCheatsAllocated);
355 if (Cheats == NULL)
356 break;
357 }
358 Cheats[NumCheats].Descr = strdup(name);
359 Cheats[NumCheats].Enabled = 0;
2c843d96 360 Cheats[NumCheats].WasEnabled = 0;
9c27c205 361 Cheats[NumCheats].First = NumCodes;
362 Cheats[NumCheats].n = 0;
363 NumCheats++;
364 newcheat = 0;
365 }
366
367 if (NumCodes >= NumCodesAllocated) {
368 NumCodesAllocated += 16;
369 CheatCodes = realloc(CheatCodes, sizeof(CheatCode) *
370 NumCodesAllocated);
371 if (CheatCodes == NULL)
372 break;
373 }
374 CheatCodes[NumCodes].Addr = a;
375 CheatCodes[NumCodes].Val = v;
376 NumCodes++;
377 Cheats[NumCheats - 1].n++;
378 }
379
380out:
381 fclose(f);
382}
383
384void emu_on_new_cd(void)
385{
386 ClearAllCheats();
387 parse_cwcheat();
388
389 if (Config.HLE) {
390 printf("note: running with HLE BIOS, expect compatibility problems\n");
391 printf("----------------------------------------------------------\n");
392 }
393
394 snprintf(hud_msg, sizeof(hud_msg), "Booting up...");
395 hud_new_msg = 2;
396}
397
38c2028e 398int emu_core_preinit(void)
80c2304e 399{
80c2304e 400 // what is the name of the config file?
401 // it may be redefined by -cfg on the command line
402 strcpy(cfgfile_basename, "pcsx.cfg");
403
e906c010 404 emuLog = stdout;
80c2304e 405 SetIsoFile(NULL);
80c2304e 406
e0c692d9 407 memset(&Config, 0, sizeof(Config));
408
e0c692d9 409 set_default_paths();
33400707 410 emu_set_default_config();
e0c692d9 411 strcpy(Config.Bios, "HLE");
412
38c2028e 413 return 0;
414}
415
416int emu_core_init(void)
417{
418 CheckSubDir();
419 check_memcards();
420
421 if (EmuInit() == -1) {
422 printf("PSX emulator couldn't be initialized.\n");
423 return -1;
424 }
425
426 LoadMcds(Config.Mcd1, Config.Mcd2);
427
428 if (Config.Debug) {
429 StartDebugger();
430 }
431
432 return 0;
433}
434
435#ifndef NO_FRONTEND
436int main(int argc, char *argv[])
437{
e0c692d9 438 char file[MAXPATHLEN] = "";
439 char path[MAXPATHLEN];
440 const char *cdfile = NULL;
de910c6b 441 const char *loadst_f = NULL;
0a151868 442 int psxout = 0;
e0c692d9 443 int loadst = 0;
444 int i;
445
38c2028e 446 emu_core_preinit();
447
80c2304e 448 // read command line options
449 for (i = 1; i < argc; i++) {
0a151868 450 if (!strcmp(argv[i], "-psxout")) psxout = 1;
80c2304e 451 else if (!strcmp(argv[i], "-load")) loadst = atol(argv[++i]);
452 else if (!strcmp(argv[i], "-cfg")) {
453 if (i+1 >= argc) break;
454 strncpy(cfgfile_basename, argv[++i], MAXPATHLEN-100); /* TODO buffer overruns */
455 printf("Using config file %s.\n", cfgfile_basename);
456 }
457 else if (!strcmp(argv[i], "-cdfile")) {
458 char isofilename[MAXPATHLEN];
459
460 if (i+1 >= argc) break;
461 strncpy(isofilename, argv[++i], MAXPATHLEN);
462 if (isofilename[0] != '/') {
463 getcwd(path, MAXPATHLEN);
464 if (strlen(path) + strlen(isofilename) + 1 < MAXPATHLEN) {
465 strcat(path, "/");
466 strcat(path, isofilename);
467 strcpy(isofilename, path);
468 } else
469 isofilename[0] = 0;
470 }
471
47bf65ab 472 cdfile = isofilename;
80c2304e 473 }
de910c6b 474 else if (!strcmp(argv[i], "-loadf")) {
475 if (i+1 >= argc) break;
476 loadst_f = argv[++i];
477 }
80c2304e 478 else if (!strcmp(argv[i], "-h") ||
479 !strcmp(argv[i], "-help") ||
480 !strcmp(argv[i], "--help")) {
38c2028e 481 printf("PCSX-ReARMed " REV "\n");
80c2304e 482 printf("%s\n", _(
483 " pcsx [options] [file]\n"
484 "\toptions:\n"
485 "\t-cdfile FILE\tRuns a CD image file\n"
80c2304e 486 "\t-cfg FILE\tLoads desired configuration file (default: ~/.pcsx/pcsx.cfg)\n"
487 "\t-psxout\t\tEnable PSX output\n"
488 "\t-load STATENUM\tLoads savestate STATENUM (1-5)\n"
489 "\t-h -help\tDisplay this message\n"
38c2028e 490 "\tfile\t\tLoads a PSX EXE file\n"));
80c2304e 491 return 0;
492 } else {
493 strncpy(file, argv[i], MAXPATHLEN);
494 if (file[0] != '/') {
495 getcwd(path, MAXPATHLEN);
496 if (strlen(path) + strlen(file) + 1 < MAXPATHLEN) {
497 strcat(path, "/");
498 strcat(path, file);
499 strcpy(file, path);
500 } else
501 file[0] = 0;
502 }
503 }
504 }
505
47bf65ab 506 if (cdfile)
507 set_cd_image(cdfile);
508
69af03a2 509 // frontend stuff
b07c18e8 510 // init input but leave probing to platform code,
511 // they add input drivers and may need to modify them after probe
69af03a2 512 in_init();
59f1c85c 513 pl_init();
69af03a2 514 plat_init();
0a151868 515 menu_init(); // loads config
516
38c2028e 517 emu_core_init();
518
0a151868 519 if (psxout)
520 Config.PsxOut = 1;
80c2304e 521
69af03a2 522 if (LoadPlugins() == -1) {
6fe1f056 523 // FIXME: this recovery doesn't work, just delete bad config and bail out
524 // SysMessage("could not load plugins, retrying with defaults\n");
525 set_default_paths();
526 snprintf(path, sizeof(path), "." PCSX_DOT_DIR "%s", cfgfile_basename);
527 remove(path);
69af03a2 528 SysMessage("Failed loading plugins!");
529 return 1;
530 }
531 pcnt_hook_plugins();
80c2304e 532
69af03a2 533 if (OpenPlugins() == -1) {
534 return 1;
535 }
201c21e2 536 plugin_call_rearmed_cbs();
80c2304e 537
69af03a2 538 CheckCdrom();
bbd837c6 539 SysReset();
69af03a2 540
541 if (file[0] != '\0') {
542 if (Load(file) != -1)
543 ready_to_go = 1;
544 } else {
47bf65ab 545 if (cdfile) {
69af03a2 546 if (LoadCdrom() == -1) {
547 ClosePlugins();
548 printf(_("Could not load CD-ROM!\n"));
549 return -1;
80c2304e 550 }
9c27c205 551 emu_on_new_cd();
69af03a2 552 ready_to_go = 1;
80c2304e 553 }
69af03a2 554 }
80c2304e 555
45d45c1e 556 if (ready_to_go) {
3c70c47b 557 menu_prepare_emu();
45d45c1e 558
559 // If a state has been specified, then load that
560 if (loadst) {
561 int ret = emu_load_state(loadst - 1);
562 printf("%s state %d\n", ret ? "failed to load" : "loaded", loadst);
563 }
de910c6b 564 if (loadst_f) {
565 int ret = LoadState(loadst_f);
566 printf("%s state file: %s\n", ret ? "failed to load" : "loaded", loadst_f);
567 }
45d45c1e 568 }
3c70c47b 569 else
69af03a2 570 menu_loop();
571
7c0f51de 572 pl_start_watchdog();
573
69af03a2 574 while (1)
575 {
799b0b87 576 stop = 0;
8f892648 577 emu_action = SACTION_NONE;
578
80c2304e 579 psxCpu->Execute();
8f892648 580 if (emu_action != SACTION_NONE)
581 do_emu_action();
80c2304e 582 }
583
80c2304e 584 return 0;
585}
38c2028e 586#endif
80c2304e 587
588void SysRunGui() {
589 printf("SysRunGui\n");
590}
591
e6eb2066 592static void dummy_lace()
593{
594}
595
80c2304e 596void SysReset() {
e6eb2066 597 // rearmed hack: EmuReset() runs some code when real BIOS is used,
598 // but we usually do reset from menu while GPU is not open yet,
599 // so we need to prevent updateLace() call..
600 void *real_lace = GPU_updateLace;
601 GPU_updateLace = dummy_lace;
602
33400707 603 // reset can run code, timing must be set
604 pl_timing_prepare(Config.PsxType);
605
80c2304e 606 EmuReset();
bd6267e6 607
608 // hmh core forgets this
609 CDR_stop();
e6eb2066 610
611 GPU_updateLace = real_lace;
80c2304e 612}
613
614void SysClose() {
615 EmuShutdown();
616 ReleasePlugins();
617
618 StopDebugger();
619
620 if (emuLog != NULL) fclose(emuLog);
621}
622
623void SysUpdate() {
80c2304e 624}
625
626void OnFile_Exit() {
bd6267e6 627 printf("OnFile_Exit\n");
bd6267e6 628 SysClose();
38c2028e 629#ifndef NO_FRONTEND
630 menu_finish();
2c886904 631 plat_finish();
80c2304e 632 exit(0);
38c2028e 633#endif
80c2304e 634}
635
c5061935 636int get_state_filename(char *buf, int size, int i) {
29a8c4f3 637 return get_gameid_filename(buf, size,
638 "." STATES_DIR "%.32s-%.9s.%3.3d", i);
80c2304e 639}
640
8f892648 641int emu_check_state(int slot)
642{
643 char fname[MAXPATHLEN];
644 int ret;
645
646 ret = get_state_filename(fname, sizeof(fname), slot);
647 if (ret != 0)
648 return ret;
649
650 return CheckState(fname);
651}
652
653int emu_save_state(int slot)
654{
655 char fname[MAXPATHLEN];
656 int ret;
657
658 ret = get_state_filename(fname, sizeof(fname), slot);
659 if (ret != 0)
660 return ret;
661
90f1d26c 662 ret = SaveState(fname);
0b1dbe6b 663#ifndef __ARM_ARCH_7A__ /* XXX */
664 sync();
665#endif
90f1d26c 666 printf("* %s \"%s\" [%d]\n", ret == 0 ? "saved" : "failed to save", fname, slot);
667 return ret;
8f892648 668}
669
670int emu_load_state(int slot)
671{
672 char fname[MAXPATHLEN];
673 int ret;
674
675 ret = get_state_filename(fname, sizeof(fname), slot);
676 if (ret != 0)
677 return ret;
678
679 return LoadState(fname);
680}
681
80c2304e 682void SysPrintf(const char *fmt, ...) {
683 va_list list;
684 char msg[512];
685
686 va_start(list, fmt);
687 vsprintf(msg, fmt, list);
688 va_end(list);
689
80c2304e 690 fprintf(emuLog, "%s", msg);
691}
692
693void SysMessage(const char *fmt, ...) {
694 va_list list;
695 char msg[512];
696
697 va_start(list, fmt);
698 vsprintf(msg, fmt, list);
699 va_end(list);
700
701 if (msg[strlen(msg) - 1] == '\n')
702 msg[strlen(msg) - 1] = 0;
703
704 fprintf(stderr, "%s\n", msg);
705}
706
c5061935 707static void SignalExit(int sig) {
708 ClosePlugins();
709 OnFile_Exit();
710}
711
712#define PARSEPATH(dst, src) \
713 ptr = src + strlen(src); \
714 while (*ptr != '\\' && ptr != src) ptr--; \
715 if (ptr != src) { \
716 strcpy(dst, ptr+1); \
717 }
718
719static int _OpenPlugins(void) {
720 int ret;
721
722 signal(SIGINT, SignalExit);
723 signal(SIGPIPE, SignalExit);
724
725 GPU_clearDynarec(clearDynarec);
726
727 ret = CDR_open();
728 if (ret < 0) { SysMessage(_("Error opening CD-ROM plugin!")); return -1; }
729 ret = SPU_open();
730 if (ret < 0) { SysMessage(_("Error opening SPU plugin!")); return -1; }
731 SPU_registerCallback(SPUirq);
732 // pcsx-rearmed: we handle gpu elsewhere
733 //ret = GPU_open(&gpuDisp, "PCSX", NULL);
734 //if (ret < 0) { SysMessage(_("Error opening GPU plugin!")); return -1; }
735 ret = PAD1_open(&gpuDisp);
736 if (ret < 0) { SysMessage(_("Error opening Controller 1 plugin!")); return -1; }
737 ret = PAD2_open(&gpuDisp);
738 if (ret < 0) { SysMessage(_("Error opening Controller 2 plugin!")); return -1; }
739
740 if (Config.UseNet && !NetOpened) {
741 netInfo info;
742 char path[MAXPATHLEN];
743 char dotdir[MAXPATHLEN];
744
745 MAKE_PATH(dotdir, "/.pcsx/plugins/", NULL);
746
38c2028e 747 strcpy(info.EmuName, "PCSX");
c5061935 748 strncpy(info.CdromID, CdromId, 9);
749 strncpy(info.CdromLabel, CdromLabel, 9);
750 info.psxMem = psxM;
751 info.GPU_showScreenPic = GPU_showScreenPic;
752 info.GPU_displayText = GPU_displayText;
753 info.GPU_showScreenPic = GPU_showScreenPic;
754 info.PAD_setSensitive = PAD1_setSensitive;
755 sprintf(path, "%s%s", Config.BiosDir, Config.Bios);
756 strcpy(info.BIOSpath, path);
757 strcpy(info.MCD1path, Config.Mcd1);
758 strcpy(info.MCD2path, Config.Mcd2);
759 sprintf(path, "%s%s", dotdir, Config.Gpu);
760 strcpy(info.GPUpath, path);
761 sprintf(path, "%s%s", dotdir, Config.Spu);
762 strcpy(info.SPUpath, path);
763 sprintf(path, "%s%s", dotdir, Config.Cdr);
764 strcpy(info.CDRpath, path);
765 NET_setInfo(&info);
766
767 ret = NET_open(&gpuDisp);
768 if (ret < 0) {
769 if (ret == -2) {
770 // -2 is returned when something in the info
771 // changed and needs to be synced
772 char *ptr;
773
774 PARSEPATH(Config.Bios, info.BIOSpath);
775 PARSEPATH(Config.Gpu, info.GPUpath);
776 PARSEPATH(Config.Spu, info.SPUpath);
777 PARSEPATH(Config.Cdr, info.CDRpath);
778
779 strcpy(Config.Mcd1, info.MCD1path);
780 strcpy(Config.Mcd2, info.MCD2path);
781 return -2;
782 } else {
783 Config.UseNet = FALSE;
784 }
785 } else {
786 if (NET_queryPlayer() == 1) {
787 if (SendPcsxInfo() == -1) Config.UseNet = FALSE;
788 } else {
789 if (RecvPcsxInfo() == -1) Config.UseNet = FALSE;
790 }
791 }
792 NetOpened = TRUE;
793 } else if (Config.UseNet) {
794 NET_resume();
795 }
796
797 return 0;
798}
799
800int OpenPlugins() {
801 int ret;
802
803 while ((ret = _OpenPlugins()) == -2) {
804 ReleasePlugins();
805 LoadMcds(Config.Mcd1, Config.Mcd2);
806 if (LoadPlugins() == -1) return -1;
807 }
808 return ret;
809}
810
811void ClosePlugins() {
812 int ret;
813
814 signal(SIGINT, SIG_DFL);
815 signal(SIGPIPE, SIG_DFL);
816 ret = CDR_close();
817 if (ret < 0) { SysMessage(_("Error closing CD-ROM plugin!")); return; }
818 ret = SPU_close();
819 if (ret < 0) { SysMessage(_("Error closing SPU plugin!")); return; }
820 ret = PAD1_close();
821 if (ret < 0) { SysMessage(_("Error closing Controller 1 Plugin!")); return; }
822 ret = PAD2_close();
823 if (ret < 0) { SysMessage(_("Error closing Controller 2 plugin!")); return; }
824 // pcsx-rearmed: we handle gpu elsewhere
825 //ret = GPU_close();
826 //if (ret < 0) { SysMessage(_("Error closing GPU plugin!")); return; }
827
828 if (Config.UseNet) {
829 NET_pause();
830 }
831}
832
e906c010 833/* we hook statically linked plugins here */
834static const char *builtin_plugins[] = {
47bf65ab 835 "builtin_gpu", "builtin_spu", "builtin_cdr", "builtin_pad",
836 "builtin_cdrcimg",
e906c010 837};
838
839static const int builtin_plugin_ids[] = {
840 PLUGIN_GPU, PLUGIN_SPU, PLUGIN_CDR, PLUGIN_PAD,
47bf65ab 841 PLUGIN_CDRCIMG,
e906c010 842};
843
80c2304e 844void *SysLoadLibrary(const char *lib) {
e906c010 845 const char *tmp = strrchr(lib, '/');
bbd837c6 846 void *ret;
e906c010 847 int i;
848
bbd837c6 849 printf("plugin: %s\n", lib);
850
e906c010 851 if (tmp != NULL) {
852 tmp++;
853 for (i = 0; i < ARRAY_SIZE(builtin_plugins); i++)
854 if (strcmp(tmp, builtin_plugins[i]) == 0)
855 return (void *)(long)(PLUGIN_DL_BASE + builtin_plugin_ids[i]);
856 }
857
2185e39b 858#if defined(__x86_64__) || defined(__i386__)
859 // convenience hack
ac6575cd 860 if (strstr(lib, ".x86") == NULL) {
861 char name[MAXPATHLEN];
dd4d5a35 862 snprintf(name, sizeof(name), "%s.x86_64", lib);
ac6575cd 863 lib = name;
864 }
2185e39b 865#endif
866
bbd837c6 867 ret = dlopen(lib, RTLD_NOW);
868 if (ret == NULL)
869 fprintf(stderr, "dlopen: %s\n", dlerror());
870 return ret;
80c2304e 871}
872
873void *SysLoadSym(void *lib, const char *sym) {
e906c010 874 unsigned int plugid = (unsigned int)(long)lib;
875
876 if (PLUGIN_DL_BASE <= plugid && plugid < PLUGIN_DL_BASE + ARRAY_SIZE(builtin_plugins))
877 return plugin_link(plugid - PLUGIN_DL_BASE, sym);
878
80c2304e 879 return dlsym(lib, sym);
880}
881
882const char *SysLibError() {
883 return dlerror();
884}
885
886void SysCloseLibrary(void *lib) {
e906c010 887 unsigned int plugid = (unsigned int)(long)lib;
888
889 if (PLUGIN_DL_BASE <= plugid && plugid < PLUGIN_DL_BASE + ARRAY_SIZE(builtin_plugins))
890 return;
891
80c2304e 892 dlclose(lib);
893}
894