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