frontend: enable SPUIRQWait by default
[pcsx_rearmed.git] / frontend / menu.c
CommitLineData
69af03a2 1/*
201c21e2 2 * (C) GraÅžvydas "notaz" Ignotas, 2010-2011
69af03a2 3 *
4 * This work is licensed under the terms of any of these licenses
5 * (at your option):
6 * - GNU GPL, version 2 or later.
7 * - GNU LGPL, version 2.1 or later.
8 * See the COPYING file in the top-level directory.
9 */
10
11#include <stdio.h>
12#include <string.h>
3c70c47b 13#include <errno.h>
bbd837c6 14#include <dlfcn.h>
61363062 15#include <zlib.h>
69af03a2 16
c5061935 17#include "main.h"
3c70c47b 18#include "menu.h"
69af03a2 19#include "config.h"
201c21e2 20#include "plugin.h"
69af03a2 21#include "plugin_lib.h"
22#include "omap.h"
23#include "common/plat.h"
24#include "../libpcsxcore/misc.h"
799b0b87 25#include "../libpcsxcore/psemu_plugin_defs.h"
3c70c47b 26#include "revision.h"
69af03a2 27
28#define MENU_X2 1
29#define array_size(x) (sizeof(x) / sizeof(x[0]))
30
31typedef enum
32{
33 MA_NONE = 1,
34 MA_MAIN_RESUME_GAME,
35 MA_MAIN_SAVE_STATE,
36 MA_MAIN_LOAD_STATE,
37 MA_MAIN_RESET_GAME,
38 MA_MAIN_LOAD_ROM,
e16a7e51 39 MA_MAIN_RUN_BIOS,
69af03a2 40 MA_MAIN_CONTROLS,
41 MA_MAIN_CREDITS,
42 MA_MAIN_EXIT,
43 MA_CTRL_PLAYER1,
44 MA_CTRL_PLAYER2,
45 MA_CTRL_EMU,
46 MA_CTRL_DEV_FIRST,
47 MA_CTRL_DEV_NEXT,
48 MA_CTRL_DONE,
49 MA_OPT_SAVECFG,
50 MA_OPT_SAVECFG_GAME,
51 MA_OPT_CPU_CLOCKS,
3c70c47b 52 MA_OPT_FILTERING,
69af03a2 53} menu_id;
54
3c70c47b 55enum {
56 SCALE_1_1,
57 SCALE_4_3,
58 SCALE_FULLSCREEN,
59 SCALE_CUSTOM,
60};
69af03a2 61
bd6267e6 62static int last_psx_w, last_psx_h, last_psx_bpp;
8f892648 63static int scaling, filter, cpu_clock, cpu_clock_st;
69af03a2 64static char rom_fname_reload[MAXPATHLEN];
65static char last_selected_fname[MAXPATHLEN];
799b0b87 66static int region, in_type_sel;
bd6267e6 67int g_opts;
68
69// from softgpu plugin
70extern int iUseDither;
71extern int UseFrameSkip;
72extern uint32_t dwActFixes;
fba06457 73extern float fFrameRateHz;
74extern int dwFrameRateTicks;
bd6267e6 75
76// sound plugin
77extern int iUseReverb;
78extern int iUseInterpolation;
79extern int iXAPitch;
80extern int iSPUIRQWait;
81extern int iUseTimer;
82
e6eb2066 83static const char *bioses[24];
bbd837c6 84static const char *gpu_plugins[16];
85static const char *spu_plugins[16];
e6eb2066 86static int bios_sel, gpu_plugsel, spu_plugsel;
bbd837c6 87
bd6267e6 88
89static int min(int x, int y) { return x < y ? x : y; }
90static int max(int x, int y) { return x > y ? x : y; }
69af03a2 91
92void emu_make_path(char *buff, const char *end, int size)
93{
94 int pos, end_len;
95
96 end_len = strlen(end);
97 pos = plat_get_root_dir(buff, size);
98 strncpy(buff + pos, end, size - pos);
99 buff[size - 1] = 0;
100 if (pos + end_len > size - 1)
101 printf("Warning: path truncated: %s\n", buff);
102}
103
104static int emu_check_save_file(int slot)
105{
8f892648 106 int ret = emu_check_state(slot);
3c70c47b 107 return ret == 0 ? 1 : 0;
69af03a2 108}
109
8f892648 110static int emu_save_load_game(int load, int unused)
69af03a2 111{
3c70c47b 112 int ret;
113
33f56da1 114 if (load) {
8f892648 115 ret = emu_load_state(state_slot);
33f56da1 116
117 // reflect hle/bios mode from savestate
118 if (Config.HLE)
119 bios_sel = 0;
120 else if (bios_sel == 0 && bioses[1] != NULL)
121 // XXX: maybe find the right bios instead
122 bios_sel = 1;
123 }
3c70c47b 124 else
8f892648 125 ret = emu_save_state(state_slot);
3c70c47b 126
127 return ret;
69af03a2 128}
129
907b1e90 130// propagate menu settings to the emu vars
131static void menu_sync_config(void)
132{
133 Config.PsxAuto = 1;
134 if (region > 0) {
135 Config.PsxAuto = 0;
136 Config.PsxType = region - 1;
137 }
799b0b87 138 in_type = in_type_sel ? PSE_PAD_TYPE_ANALOGPAD : PSE_PAD_TYPE_STANDARD;
907b1e90 139
799b0b87 140 pl_frame_interval = Config.PsxType ? 20000 : 16667;
907b1e90 141 // used by P.E.Op.S. frameskip code
142 fFrameRateHz = Config.PsxType ? 50.0f : 59.94f;
143 dwFrameRateTicks = (100000*100 / (unsigned long)(fFrameRateHz*100));
144}
145
3c70c47b 146static void menu_set_defconfig(void)
147{
bce6b056 148 g_opts = 0;
3c70c47b 149 scaling = SCALE_4_3;
bd6267e6 150
907b1e90 151 region = 0;
799b0b87 152 in_type_sel = 0;
bd6267e6 153 Config.Xa = Config.Cdda = Config.Sio =
154 Config.SpuIrq = Config.RCntFix = Config.VSyncWA = 0;
155
799b0b87 156 iUseDither = 0;
157 UseFrameSkip = 1;
bd6267e6 158 dwActFixes = 1<<7;
159
160 iUseReverb = 2;
161 iUseInterpolation = 1;
cdb31c95 162 iXAPitch = 0;
163 iSPUIRQWait = 1;
bd6267e6 164 iUseTimer = 2;
907b1e90 165
166 menu_sync_config();
3c70c47b 167}
168
4f3639fa 169#define CE_CONFIG_STR(val) \
170 { #val, 0, Config.val }
171
172#define CE_CONFIG_VAL(val) \
173 { #val, sizeof(Config.val), &Config.val }
174
175#define CE_STR(val) \
176 { #val, 0, val }
177
178#define CE_INTVAL(val) \
179 { #val, sizeof(val), &val }
180
cdb31c95 181// 'versioned' var, used when defaults change
182#define CE_INTVAL_V(val, ver) \
183 { #val #ver, sizeof(val), &val }
184
4f3639fa 185static const struct {
186 const char *name;
187 size_t len;
188 void *val;
189} config_data[] = {
190 CE_CONFIG_STR(Bios),
191 CE_CONFIG_STR(Gpu),
192 CE_CONFIG_STR(Spu),
33716956 193// CE_CONFIG_STR(Cdr),
4f3639fa 194 CE_CONFIG_VAL(Xa),
195 CE_CONFIG_VAL(Sio),
196 CE_CONFIG_VAL(Mdec),
4f3639fa 197 CE_CONFIG_VAL(Cdda),
198 CE_CONFIG_VAL(Debug),
199 CE_CONFIG_VAL(PsxOut),
200 CE_CONFIG_VAL(SpuIrq),
201 CE_CONFIG_VAL(RCntFix),
202 CE_CONFIG_VAL(VSyncWA),
203 CE_CONFIG_VAL(Cpu),
907b1e90 204 CE_INTVAL(region),
4f3639fa 205 CE_INTVAL(scaling),
cd6e8d0f 206 CE_INTVAL(g_layer_x),
207 CE_INTVAL(g_layer_y),
208 CE_INTVAL(g_layer_w),
209 CE_INTVAL(g_layer_h),
4f3639fa 210 CE_INTVAL(filter),
211 CE_INTVAL(state_slot),
212 CE_INTVAL(cpu_clock),
213 CE_INTVAL(g_opts),
799b0b87 214 CE_INTVAL(in_type_sel),
4f3639fa 215 CE_INTVAL(iUseDither),
216 CE_INTVAL(UseFrameSkip),
217 CE_INTVAL(dwActFixes),
218 CE_INTVAL(iUseReverb),
4f3639fa 219 CE_INTVAL(iXAPitch),
cdb31c95 220 CE_INTVAL_V(iUseInterpolation, 2),
221 CE_INTVAL_V(iSPUIRQWait, 2),
4f3639fa 222 CE_INTVAL(iUseTimer),
223};
224
1bd9ee68 225static char *get_cd_label(void)
cd6e8d0f 226{
1bd9ee68 227 static char trimlabel[33];
cd6e8d0f 228 int j;
229
230 strncpy(trimlabel, CdromLabel, 32);
231 trimlabel[32] = 0;
232 for (j = 31; j >= 0; j--)
233 if (trimlabel[j] == ' ')
234 trimlabel[j] = 0;
235
1bd9ee68 236 return trimlabel;
237}
238
239static void make_cfg_fname(char *buf, size_t size, int is_game)
240{
cd6e8d0f 241 if (is_game)
1bd9ee68 242 snprintf(buf, size, "." PCSX_DOT_DIR "cfg/%.32s-%.9s.cfg", get_cd_label(), CdromId);
cd6e8d0f 243 else
bbd837c6 244 snprintf(buf, size, "." PCSX_DOT_DIR "%s", cfgfile_basename);
cd6e8d0f 245}
246
43bca6fb 247static void keys_write_all(FILE *f);
248
3c70c47b 249static int menu_write_config(int is_game)
250{
4f3639fa 251 char cfgfile[MAXPATHLEN];
252 FILE *f;
253 int i;
254
cd6e8d0f 255 make_cfg_fname(cfgfile, sizeof(cfgfile), is_game);
4f3639fa 256 f = fopen(cfgfile, "w");
257 if (f == NULL) {
bbd837c6 258 printf("menu_write_config: failed to open: %s\n", cfgfile);
4f3639fa 259 return -1;
260 }
261
262 for (i = 0; i < ARRAY_SIZE(config_data); i++) {
263 fprintf(f, "%s = ", config_data[i].name);
264 switch (config_data[i].len) {
265 case 0:
266 fprintf(f, "%s\n", (char *)config_data[i].val);
267 break;
268 case 1:
269 fprintf(f, "%x\n", *(u8 *)config_data[i].val);
270 break;
271 case 2:
272 fprintf(f, "%x\n", *(u16 *)config_data[i].val);
273 break;
274 case 4:
275 fprintf(f, "%x\n", *(u32 *)config_data[i].val);
276 break;
277 default:
278 printf("menu_write_config: unhandled len %d for %s\n",
279 config_data[i].len, config_data[i].name);
280 break;
281 }
282 }
283
284 if (!is_game)
285 fprintf(f, "lastcdimg = %s\n", last_selected_fname);
286
43bca6fb 287 keys_write_all(f);
4f3639fa 288 fclose(f);
43bca6fb 289
4f3639fa 290 return 0;
291}
292
293static void parse_str_val(char *cval, const char *src)
294{
295 char *tmp;
296 strncpy(cval, src, MAXPATHLEN);
297 cval[MAXPATHLEN - 1] = 0;
298 tmp = strchr(cval, '\n');
299 if (tmp == NULL)
300 tmp = strchr(cval, '\r');
301 if (tmp != NULL)
302 *tmp = 0;
3c70c47b 303}
304
43bca6fb 305static void keys_load_all(const char *cfg);
306
3c70c47b 307static int menu_load_config(int is_game)
69af03a2 308{
4f3639fa 309 char cfgfile[MAXPATHLEN];
310 int i, ret = -1;
311 long size;
312 char *cfg;
313 FILE *f;
314
cd6e8d0f 315 make_cfg_fname(cfgfile, sizeof(cfgfile), is_game);
4f3639fa 316 f = fopen(cfgfile, "r");
317 if (f == NULL) {
bbd837c6 318 printf("menu_load_config: failed to open: %s\n", cfgfile);
4f3639fa 319 return -1;
320 }
321
322 fseek(f, 0, SEEK_END);
323 size = ftell(f);
324 if (size <= 0) {
325 printf("bad size %ld: %s\n", size, cfgfile);
326 goto fail;
327 }
328
329 cfg = malloc(size + 1);
330 if (cfg == NULL)
331 goto fail;
332
333 fseek(f, 0, SEEK_SET);
334 if (fread(cfg, 1, size, f) != size) {
335 printf("failed to read: %s\n", cfgfile);
336 goto fail_read;
337 }
338 cfg[size] = 0;
339
340 for (i = 0; i < ARRAY_SIZE(config_data); i++) {
341 char *tmp, *tmp2;
342 u32 val;
343
344 tmp = strstr(cfg, config_data[i].name);
345 if (tmp == NULL)
346 continue;
347 tmp += strlen(config_data[i].name);
348 if (strncmp(tmp, " = ", 3) != 0)
349 continue;
350 tmp += 3;
351
352 if (config_data[i].len == 0) {
353 parse_str_val(config_data[i].val, tmp);
354 continue;
355 }
356
357 tmp2 = NULL;
358 val = strtoul(tmp, &tmp2, 16);
359 if (tmp2 == NULL || tmp == tmp2)
360 continue; // parse failed
361
362 switch (config_data[i].len) {
363 case 1:
364 *(u8 *)config_data[i].val = val;
365 break;
366 case 2:
367 *(u16 *)config_data[i].val = val;
368 break;
369 case 4:
370 *(u32 *)config_data[i].val = val;
371 break;
372 default:
373 printf("menu_load_config: unhandled len %d for %s\n",
374 config_data[i].len, config_data[i].name);
375 break;
376 }
377 }
378
379 if (!is_game) {
380 char *tmp = strstr(cfg, "lastcdimg = ");
381 if (tmp != NULL) {
382 tmp += 12;
383 parse_str_val(last_selected_fname, tmp);
384 }
385 }
386
907b1e90 387 menu_sync_config();
388
bbd837c6 389 // sync plugins
e6eb2066 390 for (i = bios_sel = 0; bioses[i] != NULL; i++)
391 if (strcmp(Config.Bios, bioses[i]) == 0)
392 { bios_sel = i; break; }
393
bbd837c6 394 for (i = gpu_plugsel = 0; gpu_plugins[i] != NULL; i++)
395 if (strcmp(Config.Gpu, gpu_plugins[i]) == 0)
396 { gpu_plugsel = i; break; }
397
398 for (i = spu_plugsel = 0; spu_plugins[i] != NULL; i++)
399 if (strcmp(Config.Spu, spu_plugins[i]) == 0)
400 { spu_plugsel = i; break; }
401
43bca6fb 402 keys_load_all(cfg);
bbd837c6 403 ret = 0;
4f3639fa 404fail_read:
405 free(cfg);
406fail:
407 fclose(f);
408 return ret;
69af03a2 409}
410
9564e73d 411// rrrr rggg gggb bbbb
412static unsigned short fname2color(const char *fname)
413{
33716956 414 static const char *cdimg_exts[] = { ".bin", ".img", ".iso", ".cue", ".z", ".bz", ".znx", ".pbp" };
415 static const char *other_exts[] = { ".ccd", ".toc", ".mds", ".sub", ".table", ".index" };
9564e73d 416 const char *ext = strrchr(fname, '.');
417 int i;
418
419 if (ext == NULL)
420 return 0xffff;
421 for (i = 0; i < array_size(cdimg_exts); i++)
422 if (strcasecmp(ext, cdimg_exts[i]) == 0)
423 return 0x7bff;
424 for (i = 0; i < array_size(other_exts); i++)
425 if (strcasecmp(ext, other_exts[i]) == 0)
426 return 0xa514;
427 return 0xffff;
428}
429
61363062 430static void draw_savestate_bg(int slot);
431
bd6267e6 432#define MENU_ALIGN_LEFT
3c70c47b 433#define menu_init menu_init_common
69af03a2 434#include "common/menu.c"
3c70c47b 435#undef menu_init
69af03a2 436
61363062 437// a bit of black magic here
438static void draw_savestate_bg(int slot)
439{
440 extern void bgr555_to_rgb565(void *dst, void *src, int bytes);
441 static const int psx_widths[8] = { 256, 368, 320, 384, 512, 512, 640, 640 };
442 int x, y, w, h;
443 char fname[MAXPATHLEN];
444 GPUFreeze_t *gpu;
445 u16 *s, *d;
446 gzFile f;
447 int ret;
448 u32 tmp;
449
450 ret = get_state_filename(fname, sizeof(fname), slot);
451 if (ret != 0)
452 return;
453
454 f = gzopen(fname, "rb");
455 if (f == NULL)
456 return;
457
458 if (gzseek(f, 0x29933d, SEEK_SET) != 0x29933d) {
459 fprintf(stderr, "gzseek failed\n");
460 gzclose(f);
461 return;
462 }
463
464 gpu = malloc(sizeof(*gpu));
465 if (gpu == NULL) {
466 gzclose(f);
467 return;
468 }
469
470 ret = gzread(f, gpu, sizeof(*gpu));
471 gzclose(f);
472 if (ret != sizeof(*gpu)) {
473 fprintf(stderr, "gzread failed\n");
474 goto out;
475 }
476
477 memcpy(g_menubg_ptr, g_menubg_src_ptr, g_menuscreen_w * g_menuscreen_h * 2);
478
479 if ((gpu->ulStatus & 0x800000) || (gpu->ulStatus & 0x200000))
480 goto out; // disabled || 24bpp (NYET)
481
482 x = gpu->ulControl[5] & 0x3ff;
483 y = (gpu->ulControl[5] >> 10) & 0x1ff;
484 s = (u16 *)gpu->psxVRam + y * 1024 + (x & ~3);
485 w = psx_widths[(gpu->ulStatus >> 16) & 7];
486 tmp = gpu->ulControl[7];
487 h = ((tmp >> 10) & 0x3ff) - (tmp & 0x3ff);
488 if (gpu->ulStatus & 0x80000) // doubleheight
489 h *= 2;
490
491 x = max(0, g_menuscreen_w - w) & ~3;
492 y = max(0, g_menuscreen_h / 2 - h / 2);
493 w = min(g_menuscreen_w, w);
494 h = min(g_menuscreen_h, h);
495 d = (u16 *)g_menubg_ptr + g_menuscreen_w * y + x;
496
497 for (; h > 0; h--, d += g_menuscreen_w, s += 1024)
498 bgr555_to_rgb565(d, s, w * 2);
499
500out:
501 free(gpu);
502}
503
3c70c47b 504// ---------- pandora specific -----------
505
506static const char pnd_script_base[] = "sudo -n /usr/pandora/scripts";
507static char **pnd_filter_list;
508
509static int get_cpu_clock(void)
69af03a2 510{
3c70c47b 511 FILE *f;
512 int ret = 0;
513 f = fopen("/proc/pandora/cpu_mhz_max", "r");
514 if (f) {
515 fscanf(f, "%d", &ret);
516 fclose(f);
517 }
518 return ret;
519}
520
521static void apply_cpu_clock(void)
522{
523 char buf[128];
524
525 if (cpu_clock != 0 && cpu_clock != get_cpu_clock()) {
526 snprintf(buf, sizeof(buf), "unset DISPLAY; echo y | %s/op_cpuspeed.sh %d",
527 pnd_script_base, cpu_clock);
528 system(buf);
529 }
530}
531
532static void apply_filter(int which)
533{
4f3639fa 534 static int old = -1;
3c70c47b 535 char buf[128];
536 int i;
537
4f3639fa 538 if (pnd_filter_list == NULL || which == old)
3c70c47b 539 return;
540
541 for (i = 0; i < which; i++)
542 if (pnd_filter_list[i] == NULL)
543 return;
544
545 if (pnd_filter_list[i] == NULL)
546 return;
547
548 snprintf(buf, sizeof(buf), "%s/op_videofir.sh %s", pnd_script_base, pnd_filter_list[i]);
549 system(buf);
4f3639fa 550 old = which;
3c70c47b 551}
552
553static menu_entry e_menu_gfx_options[];
554
555static void pnd_menu_init(void)
556{
557 struct dirent *ent;
558 int i, count = 0;
559 char **mfilters;
1bd9ee68 560 char buff[64];
3c70c47b 561 DIR *dir;
562
201c21e2 563 cpu_clock_st = cpu_clock = get_cpu_clock();
3c70c47b 564
565 dir = opendir("/etc/pandora/conf/dss_fir");
566 if (dir == NULL) {
567 perror("filter opendir");
568 return;
569 }
570
571 while (1) {
572 errno = 0;
573 ent = readdir(dir);
574 if (ent == NULL) {
575 if (errno != 0)
576 perror("readdir");
577 break;
578 }
1bd9ee68 579
580 if (ent->d_type != DT_REG && ent->d_type != DT_LNK)
581 continue;
582
583 count++;
3c70c47b 584 }
585
586 if (count == 0)
587 return;
588
589 mfilters = calloc(count + 1, sizeof(mfilters[0]));
590 if (mfilters == NULL)
591 return;
592
593 rewinddir(dir);
594 for (i = 0; (ent = readdir(dir)); ) {
595 size_t len;
596
1bd9ee68 597 if (ent->d_type != DT_REG && ent->d_type != DT_LNK)
598 continue;
599
600 len = strlen(ent->d_name);
601
602 // skip pre-HF5 extra files
603 if (len >= 3 && strcmp(ent->d_name + len - 3, "_v3") == 0)
604 continue;
605 if (len >= 3 && strcmp(ent->d_name + len - 3, "_v5") == 0)
3c70c47b 606 continue;
607
1bd9ee68 608 // have to cut "_up_h" for pre-HF5
609 if (len > 5 && strcmp(ent->d_name + len - 5, "_up_h") == 0)
610 len -= 5;
611
3c70c47b 612 if (len > sizeof(buff) - 1)
613 continue;
614
615 strncpy(buff, ent->d_name, len);
616 buff[len] = 0;
617 mfilters[i] = strdup(buff);
618 if (mfilters[i] != NULL)
619 i++;
620 }
621 closedir(dir);
622
623 i = me_id2offset(e_menu_gfx_options, MA_OPT_FILTERING);
624 e_menu_gfx_options[i].data = (void *)mfilters;
625 pnd_filter_list = mfilters;
69af03a2 626}
627
201c21e2 628void menu_finish(void)
629{
630 cpu_clock = cpu_clock_st;
631 apply_cpu_clock();
632}
633
69af03a2 634// -------------- key config --------------
635
636me_bind_action me_ctrl_actions[] =
637{
638 { "UP ", 1 << DKEY_UP},
639 { "DOWN ", 1 << DKEY_DOWN },
640 { "LEFT ", 1 << DKEY_LEFT },
641 { "RIGHT ", 1 << DKEY_RIGHT },
642 { "TRIANGLE", 1 << DKEY_TRIANGLE },
22a8a805 643 { "CIRCLE ", 1 << DKEY_CIRCLE },
69af03a2 644 { "CROSS ", 1 << DKEY_CROSS },
645 { "SQUARE ", 1 << DKEY_SQUARE },
646 { "L1 ", 1 << DKEY_L1 },
647 { "R1 ", 1 << DKEY_R1 },
648 { "L2 ", 1 << DKEY_L2 },
649 { "R2 ", 1 << DKEY_R2 },
43bca6fb 650 { "L3 ", 1 << DKEY_L3 },
651 { "R3 ", 1 << DKEY_R3 },
69af03a2 652 { "START ", 1 << DKEY_START },
653 { "SELECT ", 1 << DKEY_SELECT },
654 { NULL, 0 }
655};
656
657me_bind_action emuctrl_actions[] =
658{
8f892648 659 { "Save State ", 1 << SACTION_SAVE_STATE },
660 { "Load State ", 1 << SACTION_LOAD_STATE },
661 { "Prev Save Slot ", 1 << SACTION_PREV_SSLOT },
662 { "Next Save Slot ", 1 << SACTION_NEXT_SSLOT },
663 { "Toggle Frameskip ", 1 << SACTION_TOGGLE_FSKIP },
664 { "Enter Menu ", 1 << SACTION_ENTER_MENU },
69af03a2 665 { NULL, 0 }
666};
667
43bca6fb 668static char *mystrip(char *str)
669{
670 int i, len;
671
672 len = strlen(str);
673 for (i = 0; i < len; i++)
674 if (str[i] != ' ') break;
675 if (i > 0) memmove(str, str + i, len - i + 1);
676
677 len = strlen(str);
678 for (i = len - 1; i >= 0; i--)
679 if (str[i] != ' ') break;
680 str[i+1] = 0;
681
682 return str;
683}
684
685static void get_line(char *d, size_t size, const char *s)
686{
687 const char *pe;
688 size_t len;
689
690 for (pe = s; *pe != '\r' && *pe != '\n' && *pe != 0; pe++)
691 ;
692 len = pe - s;
693 if (len > size - 1)
694 len = size - 1;
695 strncpy(d, s, len);
696 d[len] = 0;
697
698 mystrip(d);
699}
700
701static void keys_write_all(FILE *f)
702{
703 int d;
704
705 for (d = 0; d < IN_MAX_DEVS; d++)
706 {
707 const int *binds = in_get_dev_binds(d);
708 const char *name = in_get_dev_name(d, 0, 0);
709 int k, count = 0;
710
711 if (binds == NULL || name == NULL)
712 continue;
713
714 fprintf(f, "binddev = %s\n", name);
715 in_get_config(d, IN_CFG_BIND_COUNT, &count);
716
717 for (k = 0; k < count; k++)
718 {
719 int i, kbinds, mask;
720 char act[32];
721
722 act[0] = act[31] = 0;
723 name = in_get_key_name(d, k);
724
725 kbinds = binds[IN_BIND_OFFS(k, IN_BINDTYPE_PLAYER12)];
726 for (i = 0; kbinds && i < ARRAY_SIZE(me_ctrl_actions) - 1; i++) {
727 mask = me_ctrl_actions[i].mask;
728 if (mask & kbinds) {
729 strncpy(act, me_ctrl_actions[i].name, 31);
730 fprintf(f, "bind %s = player1 %s\n", name, mystrip(act));
731 kbinds &= ~mask;
732 }
733 mask = me_ctrl_actions[i].mask << 16;
734 if (mask & kbinds) {
735 strncpy(act, me_ctrl_actions[i].name, 31);
736 fprintf(f, "bind %s = player2 %s\n", name, mystrip(act));
737 kbinds &= ~mask;
738 }
739 }
740
741 kbinds = binds[IN_BIND_OFFS(k, IN_BINDTYPE_EMU)];
742 for (i = 0; kbinds && i < ARRAY_SIZE(emuctrl_actions) - 1; i++) {
743 mask = emuctrl_actions[i].mask;
744 if (mask & kbinds) {
745 strncpy(act, emuctrl_actions[i].name, 31);
746 fprintf(f, "bind %s = %s\n", name, mystrip(act));
747 kbinds &= ~mask;
748 }
749 }
750 }
751 }
752}
753
754static int parse_bind_val(const char *val, int *type)
755{
756 int i;
757
758 *type = IN_BINDTYPE_NONE;
759 if (val[0] == 0)
760 return 0;
761
762 if (strncasecmp(val, "player", 6) == 0)
763 {
764 int player, shift = 0;
765 player = atoi(val + 6) - 1;
766
767 if ((unsigned int)player > 1)
768 return -1;
769 if (player == 1)
770 shift = 16;
771
772 *type = IN_BINDTYPE_PLAYER12;
773 for (i = 0; me_ctrl_actions[i].name != NULL; i++) {
774 if (strncasecmp(me_ctrl_actions[i].name, val + 8, strlen(val + 8)) == 0)
775 return me_ctrl_actions[i].mask << shift;
776 }
777 }
778 for (i = 0; emuctrl_actions[i].name != NULL; i++) {
779 if (strncasecmp(emuctrl_actions[i].name, val, strlen(val)) == 0) {
780 *type = IN_BINDTYPE_EMU;
781 return emuctrl_actions[i].mask;
782 }
783 }
784
785 return -1;
786}
787
788static void keys_load_all(const char *cfg)
789{
790 char dev[256], key[128], *act;
791 const char *p;
792 int bind, bindtype;
793 int dev_id;
794
795 p = cfg;
796 while (p != NULL && (p = strstr(p, "binddev = ")) != NULL) {
797 p += 10;
798
799 get_line(dev, sizeof(dev), p);
800 dev_id = in_config_parse_dev(dev);
801 if (dev_id < 0) {
802 printf("input: can't handle dev: %s\n", dev);
803 continue;
804 }
805
806 in_unbind_all(dev_id, -1, -1);
807 while ((p = strstr(p, "bind"))) {
808 if (strncmp(p, "binddev = ", 10) == 0)
809 break;
810
811 p += 4;
812 if (*p != ' ') {
813 printf("input: parse error: %16s..\n", p);
814 continue;
815 }
816
817 get_line(key, sizeof(key), p);
818 act = strchr(key, '=');
819 if (act == NULL) {
820 printf("parse failed: %16s..\n", p);
821 continue;
822 }
823 *act = 0;
824 act++;
825 mystrip(key);
826 mystrip(act);
827
828 bind = parse_bind_val(act, &bindtype);
829 if (bind != -1 && bind != 0) {
8f892648 830 //printf("bind #%d '%s' %08x (%s)\n", dev_id, key, bind, act);
43bca6fb 831 in_config_bind_key(dev_id, key, bind, bindtype);
832 }
833 else
834 lprintf("config: unhandled action \"%s\"\n", act);
835 }
836 }
837}
838
69af03a2 839static int key_config_loop_wrap(int id, int keys)
840{
841 switch (id) {
842 case MA_CTRL_PLAYER1:
843 key_config_loop(me_ctrl_actions, array_size(me_ctrl_actions) - 1, 0);
844 break;
845 case MA_CTRL_PLAYER2:
846 key_config_loop(me_ctrl_actions, array_size(me_ctrl_actions) - 1, 1);
847 break;
848 case MA_CTRL_EMU:
849 key_config_loop(emuctrl_actions, array_size(emuctrl_actions) - 1, -1);
850 break;
851 default:
852 break;
853 }
854 return 0;
855}
856
857static const char *mgn_dev_name(int id, int *offs)
858{
859 const char *name = NULL;
860 static int it = 0;
861
862 if (id == MA_CTRL_DEV_FIRST)
863 it = 0;
864
865 for (; it < IN_MAX_DEVS; it++) {
866 name = in_get_dev_name(it, 1, 1);
867 if (name != NULL)
868 break;
869 }
870
871 it++;
872 return name;
873}
874
875static const char *mgn_saveloadcfg(int id, int *offs)
876{
877 return "";
878}
879
cd6e8d0f 880static int mh_savecfg(int id, int keys)
69af03a2 881{
cd6e8d0f 882 if (menu_write_config(id == MA_OPT_SAVECFG_GAME ? 1 : 0) == 0)
883 me_update_msg("config saved");
884 else
885 me_update_msg("failed to write config");
69af03a2 886
887 return 1;
888}
889
799b0b87 890static const char *men_in_type_sel[] = { "Standard (SCPH-1080)", "Analog (SCPH-1150)", NULL };
891
69af03a2 892static menu_entry e_menu_keyconfig[] =
893{
894 mee_handler_id("Player 1", MA_CTRL_PLAYER1, key_config_loop_wrap),
895 mee_handler_id("Player 2", MA_CTRL_PLAYER2, key_config_loop_wrap),
896 mee_handler_id("Emulator controls", MA_CTRL_EMU, key_config_loop_wrap),
799b0b87 897 mee_label (""),
898 mee_enum ("Controller", 0, in_type_sel, men_in_type_sel),
899 mee_cust_nosave("Save global config", MA_OPT_SAVECFG, mh_savecfg, mgn_saveloadcfg),
900 mee_cust_nosave("Save cfg for loaded game", MA_OPT_SAVECFG_GAME, mh_savecfg, mgn_saveloadcfg),
69af03a2 901 mee_label (""),
902 mee_label ("Input devices:"),
903 mee_label_mk (MA_CTRL_DEV_FIRST, mgn_dev_name),
904 mee_label_mk (MA_CTRL_DEV_NEXT, mgn_dev_name),
905 mee_label_mk (MA_CTRL_DEV_NEXT, mgn_dev_name),
906 mee_label_mk (MA_CTRL_DEV_NEXT, mgn_dev_name),
907 mee_label_mk (MA_CTRL_DEV_NEXT, mgn_dev_name),
908 mee_label_mk (MA_CTRL_DEV_NEXT, mgn_dev_name),
909 mee_label_mk (MA_CTRL_DEV_NEXT, mgn_dev_name),
910 mee_end,
911};
912
913static int menu_loop_keyconfig(int id, int keys)
914{
915 static int sel = 0;
916
e16a7e51 917// me_enable(e_menu_keyconfig, MA_OPT_SAVECFG_GAME, ready_to_go && CdromId[0]);
69af03a2 918 me_loop(e_menu_keyconfig, &sel, NULL);
919 return 0;
920}
921
69af03a2 922// ------------ gfx options menu ------------
923
3c70c47b 924static const char *men_scaler[] = { "1x1", "scaled 4:3", "fullscreen", "custom", NULL };
925static const char h_cscaler[] = "Displays the scaler layer, you can resize it\n"
926 "using d-pad or move it using R+d-pad";
927static const char *men_dummy[] = { NULL };
928
929static int menu_loop_cscaler(int id, int keys)
930{
931 unsigned int inp;
932
933 scaling = SCALE_CUSTOM;
934
935 omap_enable_layer(1);
3c70c47b 936
937 for (;;)
938 {
939 menu_draw_begin(0);
201c21e2 940 memset(g_menuscreen_ptr, 4, g_menuscreen_w * g_menuscreen_h * 2);
941 text_out16(2, 2, "%d,%d", g_layer_x, g_layer_y);
942 text_out16(2, 480 - 18, "%dx%d | d-pad: resize, R+d-pad: move", g_layer_w, g_layer_h);
3c70c47b 943 menu_draw_end();
944
945 inp = in_menu_wait(PBTN_UP|PBTN_DOWN|PBTN_LEFT|PBTN_RIGHT|PBTN_R|PBTN_MOK|PBTN_MBACK, 40);
946 if (inp & PBTN_UP) g_layer_y--;
947 if (inp & PBTN_DOWN) g_layer_y++;
948 if (inp & PBTN_LEFT) g_layer_x--;
949 if (inp & PBTN_RIGHT) g_layer_x++;
950 if (!(inp & PBTN_R)) {
951 if (inp & PBTN_UP) g_layer_h += 2;
952 if (inp & PBTN_DOWN) g_layer_h -= 2;
953 if (inp & PBTN_LEFT) g_layer_w += 2;
954 if (inp & PBTN_RIGHT) g_layer_w -= 2;
955 }
956 if (inp & (PBTN_MOK|PBTN_MBACK))
957 break;
958
959 if (inp & (PBTN_UP|PBTN_DOWN|PBTN_LEFT|PBTN_RIGHT)) {
960 if (g_layer_x < 0) g_layer_x = 0;
961 if (g_layer_x > 640) g_layer_x = 640;
962 if (g_layer_y < 0) g_layer_y = 0;
963 if (g_layer_y > 420) g_layer_y = 420;
964 if (g_layer_w < 160) g_layer_w = 160;
965 if (g_layer_h < 60) g_layer_h = 60;
966 if (g_layer_x + g_layer_w > 800)
967 g_layer_w = 800 - g_layer_x;
968 if (g_layer_y + g_layer_h > 480)
969 g_layer_h = 480 - g_layer_y;
970 omap_enable_layer(1);
971 }
972 }
973
974 omap_enable_layer(0);
975
976 return 0;
977}
978
69af03a2 979static menu_entry e_menu_gfx_options[] =
980{
3c70c47b 981 mee_enum ("Scaler", 0, scaling, men_scaler),
982 mee_enum ("Filter", MA_OPT_FILTERING, filter, men_dummy),
983// mee_onoff ("Vsync", 0, vsync, 1),
984 mee_cust_h ("Setup custom scaler", 0, menu_loop_cscaler, NULL, h_cscaler),
69af03a2 985 mee_end,
986};
987
988static int menu_loop_gfx_options(int id, int keys)
989{
990 static int sel = 0;
991
992 me_loop(e_menu_gfx_options, &sel, NULL);
993
994 return 0;
995}
996
bd6267e6 997// ------------ bios/plugins ------------
998
999static const char *men_gpu_dithering[] = { "None", "Game dependant", "Always", NULL };
bd6267e6 1000static const char h_gpu_0[] = "Needed for Chrono Cross";
1001static const char h_gpu_1[] = "Capcom fighting games";
1002static const char h_gpu_2[] = "Black screens in Lunar";
1003static const char h_gpu_3[] = "Compatibility mode";
1004static const char h_gpu_6[] = "Pandemonium 2";
1005static const char h_gpu_7[] = "Skip every second frame";
1006static const char h_gpu_8[] = "Needed by Dark Forces";
1007static const char h_gpu_9[] = "better g-colors, worse textures";
1008static const char h_gpu_10[] = "Toggle busy flags after drawing";
1009
1010static menu_entry e_menu_plugin_gpu[] =
1011{
1012 mee_enum ("Dithering", 0, iUseDither, men_gpu_dithering),
1013 mee_onoff_h ("Odd/even bit hack", 0, dwActFixes, 1<<0, h_gpu_0),
1014 mee_onoff_h ("Expand screen width", 0, dwActFixes, 1<<1, h_gpu_1),
1015 mee_onoff_h ("Ignore brightness color", 0, dwActFixes, 1<<2, h_gpu_2),
1016 mee_onoff_h ("Disable coordinate check", 0, dwActFixes, 1<<3, h_gpu_3),
1017 mee_onoff_h ("Lazy screen update", 0, dwActFixes, 1<<6, h_gpu_6),
1018 mee_onoff_h ("Old frame skipping", 0, dwActFixes, 1<<7, h_gpu_7),
1019 mee_onoff_h ("Repeated flat tex triangles ",0,dwActFixes, 1<<8, h_gpu_8),
1020 mee_onoff_h ("Draw quads with triangles", 0, dwActFixes, 1<<9, h_gpu_9),
1021 mee_onoff_h ("Fake 'gpu busy' states", 0, dwActFixes, 1<<10, h_gpu_10),
1022 mee_end,
1023};
1024
1025static int menu_loop_plugin_gpu(int id, int keys)
1026{
1027 static int sel = 0;
1028 me_loop(e_menu_plugin_gpu, &sel, NULL);
1029 return 0;
1030}
1031
1032static const char *men_spu_reverb[] = { "Off", "Fake", "On", NULL };
1033static const char *men_spu_interp[] = { "None", "Simple", "Gaussian", "Cubic", NULL };
cdb31c95 1034static const char h_spu_irq_wait[] = "Wait for CPU (recommended set to ON)";
1bd9ee68 1035static const char h_spu_thread[] = "Run sound emulation in main thread (recommended)";
bd6267e6 1036
1037static menu_entry e_menu_plugin_spu[] =
1038{
1039 mee_enum ("Reverb", 0, iUseReverb, men_spu_reverb),
1040 mee_enum ("Interpolation", 0, iUseInterpolation, men_spu_interp),
1041 mee_onoff ("Adjust XA pitch", 0, iXAPitch, 1),
1042 mee_onoff_h ("SPU IRQ Wait", 0, iSPUIRQWait, 1, h_spu_irq_wait),
1bd9ee68 1043 mee_onoff_h ("Sound in main thread", 0, iUseTimer, 2, h_spu_thread),
bd6267e6 1044 mee_end,
1045};
1046
1047static int menu_loop_plugin_spu(int id, int keys)
1048{
1049 static int sel = 0;
1050 me_loop(e_menu_plugin_spu, &sel, NULL);
1051 return 0;
1052}
1053
e6eb2066 1054static const char h_bios[] = "HLE is simulated BIOS. BIOS is saved in savestates.\n"
1055 "Must save config and reload the game\n"
1056 "for change to take effect";
bbd837c6 1057static const char h_plugin_xpu[] = "Must save config and reload the game\n"
1058 "for plugin change to take effect";
e6eb2066 1059static const char h_gpu[] = "Configure built-in P.E.Op.S. SoftGL Driver V1.17";
1060static const char h_spu[] = "Configure built-in P.E.Op.S. Sound Driver V1.7";
bbd837c6 1061
bd6267e6 1062static menu_entry e_menu_plugin_options[] =
1063{
e6eb2066 1064 mee_enum_h ("BIOS", 0, bios_sel, bioses, h_bios),
bbd837c6 1065 mee_enum_h ("GPU plugin", 0, gpu_plugsel, gpu_plugins, h_plugin_xpu),
1066 mee_enum_h ("SPU plugin", 0, spu_plugsel, spu_plugins, h_plugin_xpu),
bd6267e6 1067 mee_handler_h ("Configure built-in GPU plugin", menu_loop_plugin_gpu, h_gpu),
1068 mee_handler_h ("Configure built-in SPU plugin", menu_loop_plugin_spu, h_spu),
1069 mee_end,
1070};
1071
e16a7e51 1072static menu_entry e_menu_main[];
1073
bd6267e6 1074static int menu_loop_plugin_options(int id, int keys)
1075{
1076 static int sel = 0;
1077 me_loop(e_menu_plugin_options, &sel, NULL);
bbd837c6 1078
e6eb2066 1079 // sync BIOS/plugins
1080 snprintf(Config.Bios, sizeof(Config.Bios), "%s", bioses[bios_sel]);
bbd837c6 1081 snprintf(Config.Gpu, sizeof(Config.Gpu), "%s", gpu_plugins[gpu_plugsel]);
1082 snprintf(Config.Spu, sizeof(Config.Spu), "%s", spu_plugins[spu_plugsel]);
e16a7e51 1083 me_enable(e_menu_main, MA_MAIN_RUN_BIOS, bios_sel != 0);
bbd837c6 1084
bd6267e6 1085 return 0;
1086}
1087
1088// ------------ adv options menu ------------
1089
8f892648 1090static const char h_cfg_cpul[] = "Shows CPU usage in %";
1bd9ee68 1091static const char h_cfg_fl[] = "Frame Limiter keeps the game from running too fast";
bd6267e6 1092static const char h_cfg_xa[] = "Disables XA sound, which can sometimes improve performance";
1093static const char h_cfg_cdda[] = "Disable CD Audio for a performance boost\n"
1094 "(proper .cue/.bin dump is needed otherwise)";
1095static const char h_cfg_sio[] = "This should be enabled for certain memcards/gamepads";
1096static const char h_cfg_spuirq[] = "Compatibility tweak; should probably be left off";
1097static const char h_cfg_rcnt1[] = "Parasite Eve 2, Vandal Hearts 1/2 Fix";
1098static const char h_cfg_rcnt2[] = "InuYasha Sengoku Battle Fix";
b5e7e49a 1099static const char h_cfg_nodrc[] = "Disable dynamic recompiler and use interpreter\n"
1100 "Might be useful to overcome some dynarec bugs";
bd6267e6 1101
1102static menu_entry e_menu_adv_options[] =
1103{
fba06457 1104 mee_onoff_h ("Show CPU load", 0, g_opts, OPT_SHOWCPU, h_cfg_cpul),
bce6b056 1105 mee_onoff_h ("Disable Frame Limiter", 0, g_opts, OPT_NO_FRAMELIM, h_cfg_fl),
bd6267e6 1106 mee_onoff_h ("Disable XA Decoding", 0, Config.Xa, 1, h_cfg_xa),
1107 mee_onoff_h ("Disable CD Audio", 0, Config.Cdda, 1, h_cfg_cdda),
1108 mee_onoff_h ("SIO IRQ Always Enabled", 0, Config.Sio, 1, h_cfg_sio),
1109 mee_onoff_h ("SPU IRQ Always Enabled", 0, Config.SpuIrq, 1, h_cfg_spuirq),
1110 mee_onoff_h ("Rootcounter hack", 0, Config.RCntFix, 1, h_cfg_rcnt1),
1111 mee_onoff_h ("Rootcounter hack 2", 0, Config.VSyncWA, 1, h_cfg_rcnt2),
b5e7e49a 1112 mee_onoff_h ("Disable dynarec (slow!)",0, Config.Cpu, 1, h_cfg_nodrc),
bd6267e6 1113 mee_end,
1114};
1115
1116static int menu_loop_adv_options(int id, int keys)
1117{
1118 static int sel = 0;
1119 me_loop(e_menu_adv_options, &sel, NULL);
1120 return 0;
1121}
1122
69af03a2 1123// ------------ options menu ------------
1124
69af03a2 1125static int mh_restore_defaults(int id, int keys)
1126{
3c70c47b 1127 menu_set_defconfig();
69af03a2 1128 me_update_msg("defaults restored");
1129 return 1;
1130}
1131
907b1e90 1132static const char *men_region[] = { "Auto", "NTSC", "PAL", NULL };
bd6267e6 1133/*
69af03a2 1134static const char *men_confirm_save[] = { "OFF", "writes", "loads", "both", NULL };
1135static const char h_confirm_save[] = "Ask for confirmation when overwriting save,\n"
1136 "loading state or both";
bd6267e6 1137*/
1138static const char h_restore_def[] = "Switches back to default / recommended\n"
1139 "configuration";
69af03a2 1140
1141static menu_entry e_menu_options[] =
1142{
bd6267e6 1143// mee_range ("Save slot", 0, state_slot, 0, 9),
1144// mee_enum_h ("Confirm savestate", 0, dummy, men_confirm_save, h_confirm_save),
1145 mee_onoff ("Frameskip", 0, UseFrameSkip, 1),
1146 mee_onoff ("Show FPS", 0, g_opts, OPT_SHOWFPS),
907b1e90 1147 mee_enum ("Region", 0, region, men_region),
3c70c47b 1148 mee_range ("CPU clock", MA_OPT_CPU_CLOCKS, cpu_clock, 20, 5000),
69af03a2 1149 mee_handler ("[Display]", menu_loop_gfx_options),
bd6267e6 1150 mee_handler ("[BIOS/Plugins]", menu_loop_plugin_options),
69af03a2 1151 mee_handler ("[Advanced]", menu_loop_adv_options),
cd6e8d0f 1152 mee_cust_nosave("Save global config", MA_OPT_SAVECFG, mh_savecfg, mgn_saveloadcfg),
1153 mee_cust_nosave("Save cfg for loaded game",MA_OPT_SAVECFG_GAME, mh_savecfg, mgn_saveloadcfg),
bd6267e6 1154 mee_handler_h ("Restore default config", mh_restore_defaults, h_restore_def),
69af03a2 1155 mee_end,
1156};
1157
1158static int menu_loop_options(int id, int keys)
1159{
1160 static int sel = 0;
1161 int i;
1162
1163 i = me_id2offset(e_menu_options, MA_OPT_CPU_CLOCKS);
3c70c47b 1164 e_menu_options[i].enabled = cpu_clock != 0 ? 1 : 0;
e16a7e51 1165 me_enable(e_menu_options, MA_OPT_SAVECFG_GAME, ready_to_go && CdromId[0]);
69af03a2 1166
1167 me_loop(e_menu_options, &sel, NULL);
1168
1169 return 0;
1170}
1171
1172// ------------ debug menu ------------
1173
69af03a2 1174static void draw_frame_debug(void)
1175{
3c70c47b 1176 smalltext_out16(4, 1, "build: "__DATE__ " " __TIME__ " " REV, 0xe7fc);
69af03a2 1177}
1178
1179static void debug_menu_loop(void)
1180{
1181 int inp;
1182
1183 while (1)
1184 {
1185 menu_draw_begin(1);
1186 draw_frame_debug();
1187 menu_draw_end();
1188
1189 inp = in_menu_wait(PBTN_MOK|PBTN_MBACK|PBTN_MA2|PBTN_MA3|PBTN_L|PBTN_R |
1190 PBTN_UP|PBTN_DOWN|PBTN_LEFT|PBTN_RIGHT, 70);
1191 if (inp & PBTN_MBACK)
1192 return;
1193 }
1194}
1195
1196// ------------ main menu ------------
1197
bd6267e6 1198void OnFile_Exit();
1199
1bd9ee68 1200static void draw_frame_main(void)
1201{
1202 if (CdromId[0] != 0) {
1203 char buff[64];
907b1e90 1204 snprintf(buff, sizeof(buff), "%.32s/%.9s (running as %s)",
1205 get_cd_label(), CdromId, Config.PsxType ? "PAL" : "NTSC");
1bd9ee68 1206 smalltext_out16(4, 1, buff, 0x105f);
1207 }
1208}
1209
1210static void draw_frame_credits(void)
1211{
1212 smalltext_out16(4, 1, "build: "__DATE__ " " __TIME__ " " REV, 0xe7fc);
1213}
1214
69af03a2 1215const char *plat_get_credits(void)
1216{
bd6267e6 1217 return "PCSX-ReARMed\n\n"
1218 "(C) 1999-2003 PCSX Team\n"
69af03a2 1219 "(C) 2005-2009 PCSX-df Team\n"
c89cd762 1220 "(C) 2009-2011 PCSX-Reloaded Team\n\n"
69af03a2 1221 "GPU and SPU code by Pete Bernert\n"
1222 " and the P.E.Op.S. team\n"
c89cd762 1223 "ARM recompiler (C) 2009-2011 Ari64\n"
ee78346e 1224 "PCSX4ALL plugins by PCSX4ALL team\n"
1225 " Chui, Franxis, Unai\n\n"
69af03a2 1226 "integration, optimization and\n"
201c21e2 1227 " frontend (C) 2010-2011 notaz\n";
69af03a2 1228}
1229
e16a7e51 1230static int reset_game(void)
1231{
1232 // sanity check
1233 if (bios_sel == 0 && !Config.HLE)
1234 return -1;
1235
1236 ClosePlugins();
1237 OpenPlugins();
1238 SysReset();
1239 if (CheckCdrom() != -1) {
1240 LoadCdrom();
1241 }
1242 return 0;
1243}
1244
1245static int run_bios(void)
1246{
1247 if (bios_sel == 0)
1248 return -1;
1249
1250 ready_to_go = 0;
1251 pl_fbdev_buf = NULL;
1252
1253 ClosePlugins();
1254 set_cd_image(NULL);
1255 LoadPlugins();
1256 NetOpened = 0;
1257 if (OpenPlugins() == -1) {
1258 me_update_msg("failed to open plugins");
1259 return -1;
1260 }
1261 plugin_call_rearmed_cbs();
1262
1263 CdromId[0] = '\0';
1264 CdromLabel[0] = '\0';
1265
1266 SysReset();
1267
1268 ready_to_go = 1;
1269 return 0;
1270}
1271
bbd837c6 1272static int run_cd_image(const char *fname)
69af03a2 1273{
69af03a2 1274 ready_to_go = 0;
fba06457 1275 pl_fbdev_buf = NULL;
69af03a2 1276
fba06457 1277 ClosePlugins();
bbd837c6 1278 set_cd_image(fname);
69af03a2 1279 LoadPlugins();
1280 NetOpened = 0;
1281 if (OpenPlugins() == -1) {
1282 me_update_msg("failed to open plugins");
bbd837c6 1283 return -1;
69af03a2 1284 }
201c21e2 1285 plugin_call_rearmed_cbs();
69af03a2 1286
69af03a2 1287 if (CheckCdrom() == -1) {
1288 // Only check the CD if we are starting the console with a CD
1289 ClosePlugins();
1290 me_update_msg("unsupported/invalid CD image");
bbd837c6 1291 return -1;
69af03a2 1292 }
1293
bbd837c6 1294 SysReset();
1295
69af03a2 1296 // Read main executable directly from CDRom and start it
1297 if (LoadCdrom() == -1) {
1298 ClosePlugins();
1299 me_update_msg("failed to load CD image");
bbd837c6 1300 return -1;
69af03a2 1301 }
1302
1303 ready_to_go = 1;
bbd837c6 1304 return 0;
69af03a2 1305}
1306
bbd837c6 1307static int romsel_run(void)
69af03a2 1308{
bbd837c6 1309 int prev_gpu, prev_spu;
1310 char *fname;
1311
1312 fname = menu_loop_romsel(last_selected_fname, sizeof(last_selected_fname));
1313 if (fname == NULL)
1314 return -1;
69af03a2 1315
bbd837c6 1316 printf("selected file: %s\n", fname);
1317
1318 if (run_cd_image(fname) != 0)
1319 return -1;
1320
1321 prev_gpu = gpu_plugsel;
1322 prev_spu = spu_plugsel;
1323 if (menu_load_config(1) != 0)
1324 menu_load_config(0);
1325
1326 // check for plugin changes, have to repeat
1327 // loading if game config changed plugins to reload them
1328 if (prev_gpu != gpu_plugsel || prev_spu != spu_plugsel) {
1329 printf("plugin change detected, reloading plugins..\n");
1330 if (run_cd_image(fname) != 0)
1331 return -1;
1332 }
1333
1334 strcpy(last_selected_fname, rom_fname_reload);
1335 return 0;
1336}
1337
1338static int main_menu_handler(int id, int keys)
1339{
69af03a2 1340 switch (id)
1341 {
1342 case MA_MAIN_RESUME_GAME:
3c70c47b 1343 if (ready_to_go)
1344 return 1;
69af03a2 1345 break;
1346 case MA_MAIN_SAVE_STATE:
1347 if (ready_to_go)
1348 return menu_loop_savestate(0);
1349 break;
1350 case MA_MAIN_LOAD_STATE:
1351 if (ready_to_go)
1352 return menu_loop_savestate(1);
1353 break;
1354 case MA_MAIN_RESET_GAME:
e16a7e51 1355 if (ready_to_go && reset_game() == 0)
3c70c47b 1356 return 1;
69af03a2 1357 break;
1358 case MA_MAIN_LOAD_ROM:
bbd837c6 1359 if (romsel_run() == 0)
69af03a2 1360 return 1;
1361 break;
e16a7e51 1362 case MA_MAIN_RUN_BIOS:
1363 if (run_bios() == 0)
1364 return 1;
1365 break;
69af03a2 1366 case MA_MAIN_CREDITS:
1bd9ee68 1367 draw_menu_credits(draw_frame_credits);
69af03a2 1368 in_menu_wait(PBTN_MOK|PBTN_MBACK, 70);
1369 break;
1370 case MA_MAIN_EXIT:
bd6267e6 1371 OnFile_Exit();
1372 break;
69af03a2 1373 default:
1374 lprintf("%s: something unknown selected\n", __FUNCTION__);
1375 break;
1376 }
1377
1378 return 0;
1379}
1380
1381static menu_entry e_menu_main[] =
1382{
1383 mee_label (""),
1384 mee_label (""),
1385 mee_handler_id("Resume game", MA_MAIN_RESUME_GAME, main_menu_handler),
1386 mee_handler_id("Save State", MA_MAIN_SAVE_STATE, main_menu_handler),
1387 mee_handler_id("Load State", MA_MAIN_LOAD_STATE, main_menu_handler),
1388 mee_handler_id("Reset game", MA_MAIN_RESET_GAME, main_menu_handler),
1389 mee_handler_id("Load CD image", MA_MAIN_LOAD_ROM, main_menu_handler),
e16a7e51 1390 mee_handler_id("Run BIOS", MA_MAIN_RUN_BIOS, main_menu_handler),
69af03a2 1391 mee_handler ("Options", menu_loop_options),
1392 mee_handler ("Controls", menu_loop_keyconfig),
1393 mee_handler_id("Credits", MA_MAIN_CREDITS, main_menu_handler),
1394 mee_handler_id("Exit", MA_MAIN_EXIT, main_menu_handler),
1395 mee_end,
1396};
1397
3c70c47b 1398// ----------------------------
1399
bd6267e6 1400static void menu_leave_emu(void);
1401
69af03a2 1402void menu_loop(void)
1403{
1404 static int sel = 0;
1405
bd6267e6 1406 menu_leave_emu();
69af03a2 1407
1408 me_enable(e_menu_main, MA_MAIN_RESUME_GAME, ready_to_go);
e16a7e51 1409 me_enable(e_menu_main, MA_MAIN_SAVE_STATE, ready_to_go && CdromId[0]);
1410 me_enable(e_menu_main, MA_MAIN_LOAD_STATE, ready_to_go && CdromId[0]);
69af03a2 1411 me_enable(e_menu_main, MA_MAIN_RESET_GAME, ready_to_go);
e16a7e51 1412 me_enable(e_menu_main, MA_MAIN_RUN_BIOS, bios_sel != 0);
69af03a2 1413
69af03a2 1414 in_set_config_int(0, IN_CFG_BLOCKING, 1);
1415
1416 do {
1bd9ee68 1417 me_loop(e_menu_main, &sel, draw_frame_main);
69af03a2 1418 } while (!ready_to_go);
1419
1420 /* wait until menu, ok, back is released */
1421 while (in_menu_wait_any(50) & (PBTN_MENU|PBTN_MOK|PBTN_MBACK))
1422 ;
1423
1424 in_set_config_int(0, IN_CFG_BLOCKING, 0);
1425
3c70c47b 1426 menu_prepare_emu();
1427}
1428
e6eb2066 1429static void scan_bios_plugins(void)
bbd837c6 1430{
1431 char fname[MAXPATHLEN];
1432 struct dirent *ent;
e6eb2066 1433 int bios_i, gpu_i, spu_i;
bbd837c6 1434 char *p;
1435 DIR *dir;
1436
e6eb2066 1437 bioses[0] = "HLE";
bbd837c6 1438 gpu_plugins[0] = "builtin_gpu";
1439 spu_plugins[0] = "builtin_spu";
e6eb2066 1440 bios_i = gpu_i = spu_i = 1;
1441
1442 snprintf(fname, sizeof(fname), "%s/", Config.BiosDir);
1443 dir = opendir(fname);
1444 if (dir == NULL) {
1445 perror("scan_bios_plugins bios opendir");
1446 goto do_plugins;
1447 }
1448
1449 while (1) {
1450 struct stat st;
1451
1452 errno = 0;
1453 ent = readdir(dir);
1454 if (ent == NULL) {
1455 if (errno != 0)
1456 perror("readdir");
1457 break;
1458 }
1459
1460 if (ent->d_type != DT_REG && ent->d_type != DT_LNK)
1461 continue;
1462
1463 snprintf(fname, sizeof(fname), "%s/%s", Config.BiosDir, ent->d_name);
1464 if (stat(fname, &st) != 0 || st.st_size != 512*1024) {
1465 printf("bad BIOS file: %s\n", ent->d_name);
1466 continue;
1467 }
1468
1469 if (bios_i < ARRAY_SIZE(bioses) - 1) {
1470 bioses[bios_i++] = strdup(ent->d_name);
1471 continue;
1472 }
1473
1474 printf("too many BIOSes, dropping \"%s\"\n", ent->d_name);
1475 }
1476
1477 closedir(dir);
bbd837c6 1478
e6eb2066 1479do_plugins:
bbd837c6 1480 snprintf(fname, sizeof(fname), "%s/", Config.PluginsDir);
1481 dir = opendir(fname);
1482 if (dir == NULL) {
e6eb2066 1483 perror("scan_bios_plugins opendir");
bbd837c6 1484 return;
1485 }
1486
1487 while (1) {
1488 void *h, *tmp;
1489
1490 errno = 0;
1491 ent = readdir(dir);
1492 if (ent == NULL) {
1493 if (errno != 0)
1494 perror("readdir");
1495 break;
1496 }
1497 p = strstr(ent->d_name, ".so");
1498 if (p == NULL)
1499 continue;
1500
1501 snprintf(fname, sizeof(fname), "%s/%s", Config.PluginsDir, ent->d_name);
1502 h = dlopen(fname, RTLD_LAZY | RTLD_LOCAL);
1503 if (h == NULL) {
1504 fprintf(stderr, "%s\n", dlerror());
1505 continue;
1506 }
1507
1508 // now what do we have here?
1509 tmp = dlsym(h, "GPUinit");
1510 if (tmp) {
1511 dlclose(h);
1512 if (gpu_i < ARRAY_SIZE(gpu_plugins) - 1)
1513 gpu_plugins[gpu_i++] = strdup(ent->d_name);
1514 continue;
1515 }
1516
1517 tmp = dlsym(h, "SPUinit");
1518 if (tmp) {
1519 dlclose(h);
1520 if (spu_i < ARRAY_SIZE(spu_plugins) - 1)
1521 spu_plugins[spu_i++] = strdup(ent->d_name);
1522 continue;
1523 }
1524
1525 fprintf(stderr, "ignoring unidentified plugin: %s\n", fname);
1526 dlclose(h);
1527 }
1528
1529 closedir(dir);
1530}
1531
3c70c47b 1532void menu_init(void)
1533{
4f3639fa 1534 char buff[MAXPATHLEN];
1535
1536 strcpy(last_selected_fname, "/media");
1537
e6eb2066 1538 scan_bios_plugins();
4f3639fa 1539 pnd_menu_init();
1540 menu_init_common();
1541
3c70c47b 1542 menu_set_defconfig();
1543 menu_load_config(0);
3c70c47b 1544 last_psx_w = 320;
1545 last_psx_h = 240;
bd6267e6 1546 last_psx_bpp = 16;
1547
4f3639fa 1548 g_menubg_src_ptr = calloc(g_menuscreen_w * g_menuscreen_h * 2, 1);
1549 if (g_menubg_src_ptr == NULL)
1550 exit(1);
1551 emu_make_path(buff, "skin/background.png", sizeof(buff));
1552 readpng(g_menubg_src_ptr, buff, READPNG_BG, g_menuscreen_w, g_menuscreen_h);
3c70c47b 1553}
1554
bd6267e6 1555void menu_notify_mode_change(int w, int h, int bpp)
3c70c47b 1556{
1557 last_psx_w = w;
1558 last_psx_h = h;
bd6267e6 1559 last_psx_bpp = bpp;
3c70c47b 1560
1561 if (scaling == SCALE_1_1) {
1562 g_layer_x = 800/2 - w/2; g_layer_y = 480/2 - h/2;
1563 g_layer_w = w; g_layer_h = h;
3c70c47b 1564 }
1565}
1566
bd6267e6 1567static void menu_leave_emu(void)
1568{
6d1a1ac2 1569 if (GPU_close != NULL) {
1570 int ret = GPU_close();
1571 if (ret)
1572 fprintf(stderr, "Warning: GPU_close returned %d\n", ret);
1573 }
bd6267e6 1574
4f3639fa 1575 memcpy(g_menubg_ptr, g_menubg_src_ptr, g_menuscreen_w * g_menuscreen_h * 2);
fba06457 1576 if (pl_fbdev_buf != NULL && ready_to_go && last_psx_bpp == 16) {
bd6267e6 1577 int x = max(0, g_menuscreen_w - last_psx_w);
1578 int y = max(0, g_menuscreen_h / 2 - last_psx_h / 2);
1579 int w = min(g_menuscreen_w, last_psx_w);
1580 int h = min(g_menuscreen_h, last_psx_h);
4f3639fa 1581 u16 *d = (u16 *)g_menubg_ptr + g_menuscreen_w * y + x;
bd6267e6 1582 u16 *s = pl_fbdev_buf;
1583
1584 for (; h > 0; h--, d += g_menuscreen_w, s += last_psx_w)
4f3639fa 1585 menu_darken_bg(d, s, w, 0);
bd6267e6 1586 }
fba06457 1587
1bd9ee68 1588 if (ready_to_go)
1589 cpu_clock = get_cpu_clock();
1590
fba06457 1591 plat_video_menu_enter(ready_to_go);
bd6267e6 1592}
1593
3c70c47b 1594void menu_prepare_emu(void)
1595{
b5e7e49a 1596 R3000Acpu *prev_cpu = psxCpu;
1597
fba06457 1598 plat_video_menu_leave();
1599
3c70c47b 1600 switch (scaling) {
1601 case SCALE_1_1:
bd6267e6 1602 menu_notify_mode_change(last_psx_w, last_psx_h, last_psx_bpp);
3c70c47b 1603 break;
1604 case SCALE_4_3:
1605 g_layer_x = 80; g_layer_y = 0;
1606 g_layer_w = 640; g_layer_h = 480;
1607 break;
1608 case SCALE_FULLSCREEN:
1609 g_layer_x = 0; g_layer_y = 0;
1610 g_layer_w = 800; g_layer_h = 480;
1611 break;
1612 case SCALE_CUSTOM:
1613 break;
1614 }
3c70c47b 1615 apply_filter(filter);
1616 apply_cpu_clock();
bd6267e6 1617
b5e7e49a 1618 psxCpu = (Config.Cpu == CPU_INTERPRETER) ? &psxInt : &psxRec;
1619 if (psxCpu != prev_cpu)
1620 // note that this does not really reset, just clears drc caches
1621 psxCpu->Reset();
1622
bd6267e6 1623 // core doesn't care about Config.Cdda changes,
1624 // so handle them manually here
1625 if (Config.Cdda)
1626 CDR_stop();
6d1a1ac2 1627
907b1e90 1628 menu_sync_config();
fba06457 1629
6d1a1ac2 1630 if (GPU_open != NULL) {
6d1a1ac2 1631 int ret = GPU_open(&gpuDisp, "PCSX", NULL);
1632 if (ret)
1633 fprintf(stderr, "Warning: GPU_open returned %d\n", ret);
1634 }
69af03a2 1635}
1636
1637void me_update_msg(const char *msg)
1638{
1639 strncpy(menu_error_msg, msg, sizeof(menu_error_msg));
1640 menu_error_msg[sizeof(menu_error_msg) - 1] = 0;
1641
1642 menu_error_time = plat_get_ticks_ms();
1643 lprintf("msg: %s\n", menu_error_msg);
1644}
1645