2 * (C) GraÅžvydas "notaz" Ignotas, 2010-2011
4 * This work is licensed under the terms of any of these licenses
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.
21 #include "plugin_lib.h"
23 #include "common/plat.h"
24 #include "../libpcsxcore/misc.h"
25 #include "../libpcsxcore/new_dynarec/new_dynarec.h"
29 #define array_size(x) (sizeof(x) / sizeof(x[0]))
61 static int last_psx_w, last_psx_h, last_psx_bpp;
62 static int scaling, filter, state_slot, cpu_clock, cpu_clock_st;
63 static char rom_fname_reload[MAXPATHLEN];
64 static char last_selected_fname[MAXPATHLEN];
67 // from softgpu plugin
68 extern int iUseDither;
69 extern int UseFrameSkip;
70 extern int UseFrameLimit;
71 extern uint32_t dwActFixes;
72 extern float fFrameRateHz;
73 extern int dwFrameRateTicks;
76 extern int iUseReverb;
77 extern int iUseInterpolation;
79 extern int iSPUIRQWait;
82 static const char *gpu_plugins[16];
83 static const char *spu_plugins[16];
84 static int gpu_plugsel, spu_plugsel;
87 static int min(int x, int y) { return x < y ? x : y; }
88 static int max(int x, int y) { return x > y ? x : y; }
90 void emu_make_path(char *buff, const char *end, int size)
94 end_len = strlen(end);
95 pos = plat_get_root_dir(buff, size);
96 strncpy(buff + pos, end, size - pos);
98 if (pos + end_len > size - 1)
99 printf("Warning: path truncated: %s\n", buff);
102 static int emu_check_save_file(int slot)
104 char fname[MAXPATHLEN];
107 ret = get_state_filename(fname, sizeof(fname), slot);
111 ret = CheckState(fname);
112 return ret == 0 ? 1 : 0;
115 static int emu_save_load_game(int load, int sram)
117 char fname[MAXPATHLEN];
120 ret = get_state_filename(fname, sizeof(fname), state_slot);
125 ret = LoadState(fname);
127 ret = SaveState(fname);
132 static void menu_set_defconfig(void)
136 Config.Xa = Config.Cdda = Config.Sio =
137 Config.SpuIrq = Config.RCntFix = Config.VSyncWA = 0;
139 iUseDither = UseFrameSkip = 0;
144 iUseInterpolation = 1;
145 iXAPitch = iSPUIRQWait = 0;
149 #define CE_CONFIG_STR(val) \
150 { #val, 0, Config.val }
152 #define CE_CONFIG_VAL(val) \
153 { #val, sizeof(Config.val), &Config.val }
155 #define CE_STR(val) \
158 #define CE_INTVAL(val) \
159 { #val, sizeof(val), &val }
161 static const struct {
173 CE_CONFIG_VAL(PsxAuto),
175 CE_CONFIG_VAL(Debug),
176 CE_CONFIG_VAL(PsxOut),
177 CE_CONFIG_VAL(SpuIrq),
178 CE_CONFIG_VAL(RCntFix),
179 CE_CONFIG_VAL(VSyncWA),
181 CE_CONFIG_VAL(PsxType),
183 CE_INTVAL(g_layer_x),
184 CE_INTVAL(g_layer_y),
185 CE_INTVAL(g_layer_w),
186 CE_INTVAL(g_layer_h),
188 CE_INTVAL(state_slot),
189 CE_INTVAL(cpu_clock),
191 CE_INTVAL(iUseDither),
192 CE_INTVAL(UseFrameSkip),
193 CE_INTVAL(UseFrameLimit),
194 CE_INTVAL(dwActFixes),
195 CE_INTVAL(iUseReverb),
196 CE_INTVAL(iUseInterpolation),
198 CE_INTVAL(iSPUIRQWait),
199 CE_INTVAL(iUseTimer),
202 static void make_cfg_fname(char *buf, size_t size, int is_game)
207 strncpy(trimlabel, CdromLabel, 32);
209 for (j = 31; j >= 0; j--)
210 if (trimlabel[j] == ' ')
214 snprintf(buf, size, "." PCSX_DOT_DIR "cfg/%.32s-%.9s.cfg", trimlabel, CdromId);
216 snprintf(buf, size, "." PCSX_DOT_DIR "%s", cfgfile_basename);
219 static int menu_write_config(int is_game)
221 char cfgfile[MAXPATHLEN];
225 make_cfg_fname(cfgfile, sizeof(cfgfile), is_game);
226 f = fopen(cfgfile, "w");
228 printf("menu_write_config: failed to open: %s\n", cfgfile);
232 for (i = 0; i < ARRAY_SIZE(config_data); i++) {
233 fprintf(f, "%s = ", config_data[i].name);
234 switch (config_data[i].len) {
236 fprintf(f, "%s\n", (char *)config_data[i].val);
239 fprintf(f, "%x\n", *(u8 *)config_data[i].val);
242 fprintf(f, "%x\n", *(u16 *)config_data[i].val);
245 fprintf(f, "%x\n", *(u32 *)config_data[i].val);
248 printf("menu_write_config: unhandled len %d for %s\n",
249 config_data[i].len, config_data[i].name);
255 fprintf(f, "lastcdimg = %s\n", last_selected_fname);
261 static void parse_str_val(char *cval, const char *src)
264 strncpy(cval, src, MAXPATHLEN);
265 cval[MAXPATHLEN - 1] = 0;
266 tmp = strchr(cval, '\n');
268 tmp = strchr(cval, '\r');
273 static int menu_load_config(int is_game)
275 char cfgfile[MAXPATHLEN];
281 make_cfg_fname(cfgfile, sizeof(cfgfile), is_game);
282 f = fopen(cfgfile, "r");
284 printf("menu_load_config: failed to open: %s\n", cfgfile);
288 fseek(f, 0, SEEK_END);
291 printf("bad size %ld: %s\n", size, cfgfile);
295 cfg = malloc(size + 1);
299 fseek(f, 0, SEEK_SET);
300 if (fread(cfg, 1, size, f) != size) {
301 printf("failed to read: %s\n", cfgfile);
306 for (i = 0; i < ARRAY_SIZE(config_data); i++) {
310 tmp = strstr(cfg, config_data[i].name);
313 tmp += strlen(config_data[i].name);
314 if (strncmp(tmp, " = ", 3) != 0)
318 if (config_data[i].len == 0) {
319 parse_str_val(config_data[i].val, tmp);
324 val = strtoul(tmp, &tmp2, 16);
325 if (tmp2 == NULL || tmp == tmp2)
326 continue; // parse failed
328 switch (config_data[i].len) {
330 *(u8 *)config_data[i].val = val;
333 *(u16 *)config_data[i].val = val;
336 *(u32 *)config_data[i].val = val;
339 printf("menu_load_config: unhandled len %d for %s\n",
340 config_data[i].len, config_data[i].name);
346 char *tmp = strstr(cfg, "lastcdimg = ");
349 parse_str_val(last_selected_fname, tmp);
354 for (i = gpu_plugsel = 0; gpu_plugins[i] != NULL; i++)
355 if (strcmp(Config.Gpu, gpu_plugins[i]) == 0)
356 { gpu_plugsel = i; break; }
358 for (i = spu_plugsel = 0; spu_plugins[i] != NULL; i++)
359 if (strcmp(Config.Spu, spu_plugins[i]) == 0)
360 { spu_plugsel = i; break; }
370 // rrrr rggg gggb bbbb
371 static unsigned short fname2color(const char *fname)
373 static const char *cdimg_exts[] = { ".bin", ".img", ".iso", ".z", ".cue" };
374 static const char *other_exts[] = { ".ccd", ".toc", ".mds", ".sub", ".table" };
375 const char *ext = strrchr(fname, '.');
380 for (i = 0; i < array_size(cdimg_exts); i++)
381 if (strcasecmp(ext, cdimg_exts[i]) == 0)
383 for (i = 0; i < array_size(other_exts); i++)
384 if (strcasecmp(ext, other_exts[i]) == 0)
389 static void draw_savestate_bg(int slot);
391 #define MENU_ALIGN_LEFT
392 #define menu_init menu_init_common
393 #include "common/menu.c"
396 // a bit of black magic here
397 static void draw_savestate_bg(int slot)
399 extern void bgr555_to_rgb565(void *dst, void *src, int bytes);
400 static const int psx_widths[8] = { 256, 368, 320, 384, 512, 512, 640, 640 };
402 char fname[MAXPATHLEN];
409 ret = get_state_filename(fname, sizeof(fname), slot);
413 f = gzopen(fname, "rb");
417 if (gzseek(f, 0x29933d, SEEK_SET) != 0x29933d) {
418 fprintf(stderr, "gzseek failed\n");
423 gpu = malloc(sizeof(*gpu));
429 ret = gzread(f, gpu, sizeof(*gpu));
431 if (ret != sizeof(*gpu)) {
432 fprintf(stderr, "gzread failed\n");
436 memcpy(g_menubg_ptr, g_menubg_src_ptr, g_menuscreen_w * g_menuscreen_h * 2);
438 if ((gpu->ulStatus & 0x800000) || (gpu->ulStatus & 0x200000))
439 goto out; // disabled || 24bpp (NYET)
441 x = gpu->ulControl[5] & 0x3ff;
442 y = (gpu->ulControl[5] >> 10) & 0x1ff;
443 s = (u16 *)gpu->psxVRam + y * 1024 + (x & ~3);
444 w = psx_widths[(gpu->ulStatus >> 16) & 7];
445 tmp = gpu->ulControl[7];
446 h = ((tmp >> 10) & 0x3ff) - (tmp & 0x3ff);
447 if (gpu->ulStatus & 0x80000) // doubleheight
450 x = max(0, g_menuscreen_w - w) & ~3;
451 y = max(0, g_menuscreen_h / 2 - h / 2);
452 w = min(g_menuscreen_w, w);
453 h = min(g_menuscreen_h, h);
454 d = (u16 *)g_menubg_ptr + g_menuscreen_w * y + x;
456 for (; h > 0; h--, d += g_menuscreen_w, s += 1024)
457 bgr555_to_rgb565(d, s, w * 2);
463 // ---------- pandora specific -----------
465 static const char pnd_script_base[] = "sudo -n /usr/pandora/scripts";
466 static char **pnd_filter_list;
468 static int get_cpu_clock(void)
472 f = fopen("/proc/pandora/cpu_mhz_max", "r");
474 fscanf(f, "%d", &ret);
480 static void apply_cpu_clock(void)
484 if (cpu_clock != 0 && cpu_clock != get_cpu_clock()) {
485 snprintf(buf, sizeof(buf), "unset DISPLAY; echo y | %s/op_cpuspeed.sh %d",
486 pnd_script_base, cpu_clock);
491 static void apply_filter(int which)
497 if (pnd_filter_list == NULL || which == old)
500 for (i = 0; i < which; i++)
501 if (pnd_filter_list[i] == NULL)
504 if (pnd_filter_list[i] == NULL)
507 snprintf(buf, sizeof(buf), "%s/op_videofir.sh %s", pnd_script_base, pnd_filter_list[i]);
512 static menu_entry e_menu_gfx_options[];
514 static void pnd_menu_init(void)
522 cpu_clock_st = cpu_clock = get_cpu_clock();
524 dir = opendir("/etc/pandora/conf/dss_fir");
526 perror("filter opendir");
538 p = strstr(ent->d_name, "_up");
539 if (p != NULL && (p[3] == 0 || !strcmp(p + 3, "_h")))
546 mfilters = calloc(count + 1, sizeof(mfilters[0]));
547 if (mfilters == NULL)
551 for (i = 0; (ent = readdir(dir)); ) {
554 p = strstr(ent->d_name, "_up");
555 if (p == NULL || (p[3] != 0 && strcmp(p + 3, "_h")))
558 len = p - ent->d_name;
559 if (len > sizeof(buff) - 1)
562 strncpy(buff, ent->d_name, len);
564 mfilters[i] = strdup(buff);
565 if (mfilters[i] != NULL)
570 i = me_id2offset(e_menu_gfx_options, MA_OPT_FILTERING);
571 e_menu_gfx_options[i].data = (void *)mfilters;
572 pnd_filter_list = mfilters;
575 void menu_finish(void)
577 cpu_clock = cpu_clock_st;
581 // -------------- key config --------------
583 me_bind_action me_ctrl_actions[] =
585 { "UP ", 1 << DKEY_UP},
586 { "DOWN ", 1 << DKEY_DOWN },
587 { "LEFT ", 1 << DKEY_LEFT },
588 { "RIGHT ", 1 << DKEY_RIGHT },
589 { "TRIANGLE", 1 << DKEY_TRIANGLE },
590 { "CIRCLE ", 1 << DKEY_CIRCLE },
591 { "CROSS ", 1 << DKEY_CROSS },
592 { "SQUARE ", 1 << DKEY_SQUARE },
593 { "L1 ", 1 << DKEY_L1 },
594 { "R1 ", 1 << DKEY_R1 },
595 { "L2 ", 1 << DKEY_L2 },
596 { "R2 ", 1 << DKEY_R2 },
597 { "START ", 1 << DKEY_START },
598 { "SELECT ", 1 << DKEY_SELECT },
602 me_bind_action emuctrl_actions[] =
605 { "Load State ", PEV_STATE_LOAD },
606 { "Save State ", PEV_STATE_SAVE },
607 { "Prev Save Slot ", PEV_SSLOT_PREV },
608 { "Next Save Slot ", PEV_SSLOT_NEXT },
610 { "Enter Menu ", PEV_MENU },
614 static int key_config_loop_wrap(int id, int keys)
617 case MA_CTRL_PLAYER1:
618 key_config_loop(me_ctrl_actions, array_size(me_ctrl_actions) - 1, 0);
620 case MA_CTRL_PLAYER2:
621 key_config_loop(me_ctrl_actions, array_size(me_ctrl_actions) - 1, 1);
624 key_config_loop(emuctrl_actions, array_size(emuctrl_actions) - 1, -1);
632 static const char *mgn_dev_name(int id, int *offs)
634 const char *name = NULL;
637 if (id == MA_CTRL_DEV_FIRST)
640 for (; it < IN_MAX_DEVS; it++) {
641 name = in_get_dev_name(it, 1, 1);
650 static const char *mgn_saveloadcfg(int id, int *offs)
655 static int mh_savecfg(int id, int keys)
657 if (menu_write_config(id == MA_OPT_SAVECFG_GAME ? 1 : 0) == 0)
658 me_update_msg("config saved");
660 me_update_msg("failed to write config");
665 static menu_entry e_menu_keyconfig[] =
667 mee_handler_id("Player 1", MA_CTRL_PLAYER1, key_config_loop_wrap),
668 mee_handler_id("Player 2", MA_CTRL_PLAYER2, key_config_loop_wrap),
669 mee_handler_id("Emulator controls", MA_CTRL_EMU, key_config_loop_wrap),
670 // mee_cust_nosave("Save global config", MA_OPT_SAVECFG, mh_savecfg, mgn_saveloadcfg),
671 // mee_cust_nosave("Save cfg for loaded game", MA_OPT_SAVECFG_GAME, mh_savecfg, mgn_saveloadcfg),
673 mee_label ("Input devices:"),
674 mee_label_mk (MA_CTRL_DEV_FIRST, mgn_dev_name),
675 mee_label_mk (MA_CTRL_DEV_NEXT, mgn_dev_name),
676 mee_label_mk (MA_CTRL_DEV_NEXT, mgn_dev_name),
677 mee_label_mk (MA_CTRL_DEV_NEXT, mgn_dev_name),
678 mee_label_mk (MA_CTRL_DEV_NEXT, mgn_dev_name),
679 mee_label_mk (MA_CTRL_DEV_NEXT, mgn_dev_name),
680 mee_label_mk (MA_CTRL_DEV_NEXT, mgn_dev_name),
684 static int menu_loop_keyconfig(int id, int keys)
688 // me_enable(e_menu_keyconfig, MA_OPT_SAVECFG_GAME, ready_to_go);
689 me_loop(e_menu_keyconfig, &sel, NULL);
693 // ------------ gfx options menu ------------
695 static const char *men_scaler[] = { "1x1", "scaled 4:3", "fullscreen", "custom", NULL };
696 static const char h_cscaler[] = "Displays the scaler layer, you can resize it\n"
697 "using d-pad or move it using R+d-pad";
698 static const char *men_dummy[] = { NULL };
700 static int menu_loop_cscaler(int id, int keys)
704 scaling = SCALE_CUSTOM;
706 omap_enable_layer(1);
711 memset(g_menuscreen_ptr, 4, g_menuscreen_w * g_menuscreen_h * 2);
712 text_out16(2, 2, "%d,%d", g_layer_x, g_layer_y);
713 text_out16(2, 480 - 18, "%dx%d | d-pad: resize, R+d-pad: move", g_layer_w, g_layer_h);
716 inp = in_menu_wait(PBTN_UP|PBTN_DOWN|PBTN_LEFT|PBTN_RIGHT|PBTN_R|PBTN_MOK|PBTN_MBACK, 40);
717 if (inp & PBTN_UP) g_layer_y--;
718 if (inp & PBTN_DOWN) g_layer_y++;
719 if (inp & PBTN_LEFT) g_layer_x--;
720 if (inp & PBTN_RIGHT) g_layer_x++;
721 if (!(inp & PBTN_R)) {
722 if (inp & PBTN_UP) g_layer_h += 2;
723 if (inp & PBTN_DOWN) g_layer_h -= 2;
724 if (inp & PBTN_LEFT) g_layer_w += 2;
725 if (inp & PBTN_RIGHT) g_layer_w -= 2;
727 if (inp & (PBTN_MOK|PBTN_MBACK))
730 if (inp & (PBTN_UP|PBTN_DOWN|PBTN_LEFT|PBTN_RIGHT)) {
731 if (g_layer_x < 0) g_layer_x = 0;
732 if (g_layer_x > 640) g_layer_x = 640;
733 if (g_layer_y < 0) g_layer_y = 0;
734 if (g_layer_y > 420) g_layer_y = 420;
735 if (g_layer_w < 160) g_layer_w = 160;
736 if (g_layer_h < 60) g_layer_h = 60;
737 if (g_layer_x + g_layer_w > 800)
738 g_layer_w = 800 - g_layer_x;
739 if (g_layer_y + g_layer_h > 480)
740 g_layer_h = 480 - g_layer_y;
741 omap_enable_layer(1);
745 omap_enable_layer(0);
750 static menu_entry e_menu_gfx_options[] =
752 mee_enum ("Scaler", 0, scaling, men_scaler),
753 mee_enum ("Filter", MA_OPT_FILTERING, filter, men_dummy),
754 // mee_onoff ("Vsync", 0, vsync, 1),
755 mee_cust_h ("Setup custom scaler", 0, menu_loop_cscaler, NULL, h_cscaler),
759 static int menu_loop_gfx_options(int id, int keys)
763 me_loop(e_menu_gfx_options, &sel, NULL);
768 // ------------ bios/plugins ------------
770 static const char *men_gpu_dithering[] = { "None", "Game dependant", "Always", NULL };
771 static const char h_gpu_0[] = "Needed for Chrono Cross";
772 static const char h_gpu_1[] = "Capcom fighting games";
773 static const char h_gpu_2[] = "Black screens in Lunar";
774 static const char h_gpu_3[] = "Compatibility mode";
775 static const char h_gpu_6[] = "Pandemonium 2";
776 static const char h_gpu_7[] = "Skip every second frame";
777 static const char h_gpu_8[] = "Needed by Dark Forces";
778 static const char h_gpu_9[] = "better g-colors, worse textures";
779 static const char h_gpu_10[] = "Toggle busy flags after drawing";
781 static menu_entry e_menu_plugin_gpu[] =
783 mee_enum ("Dithering", 0, iUseDither, men_gpu_dithering),
784 mee_onoff_h ("Odd/even bit hack", 0, dwActFixes, 1<<0, h_gpu_0),
785 mee_onoff_h ("Expand screen width", 0, dwActFixes, 1<<1, h_gpu_1),
786 mee_onoff_h ("Ignore brightness color", 0, dwActFixes, 1<<2, h_gpu_2),
787 mee_onoff_h ("Disable coordinate check", 0, dwActFixes, 1<<3, h_gpu_3),
788 mee_onoff_h ("Lazy screen update", 0, dwActFixes, 1<<6, h_gpu_6),
789 mee_onoff_h ("Old frame skipping", 0, dwActFixes, 1<<7, h_gpu_7),
790 mee_onoff_h ("Repeated flat tex triangles ",0,dwActFixes, 1<<8, h_gpu_8),
791 mee_onoff_h ("Draw quads with triangles", 0, dwActFixes, 1<<9, h_gpu_9),
792 mee_onoff_h ("Fake 'gpu busy' states", 0, dwActFixes, 1<<10, h_gpu_10),
796 static int menu_loop_plugin_gpu(int id, int keys)
799 me_loop(e_menu_plugin_gpu, &sel, NULL);
803 static const char *men_spu_reverb[] = { "Off", "Fake", "On", NULL };
804 static const char *men_spu_interp[] = { "None", "Simple", "Gaussian", "Cubic", NULL };
805 static const char h_spu_irq_wait[] = "Wait for CPU; only useful for some games, may cause glitches";
806 static const char h_spu_thread[] = "Run sound emulation in separate thread";
808 static menu_entry e_menu_plugin_spu[] =
810 mee_enum ("Reverb", 0, iUseReverb, men_spu_reverb),
811 mee_enum ("Interpolation", 0, iUseInterpolation, men_spu_interp),
812 mee_onoff ("Adjust XA pitch", 0, iXAPitch, 1),
813 mee_onoff_h ("SPU IRQ Wait", 0, iSPUIRQWait, 1, h_spu_irq_wait),
814 mee_onoff_h ("Use sound thread", 0, iUseTimer, 1, h_spu_thread),
818 static int menu_loop_plugin_spu(int id, int keys)
821 me_loop(e_menu_plugin_spu, &sel, NULL);
825 static const char h_gpu[] = "Configure built-in P.E.Op.S. SoftGL Driver V1.17";
826 static const char h_spu[] = "Configure built-in P.E.Op.S. Sound Driver V1.7";
827 static const char h_plugin_xpu[] = "Must save config and reload the game\n"
828 "for plugin change to take effect";
830 static menu_entry e_menu_plugin_options[] =
832 mee_enum_h ("GPU plugin", 0, gpu_plugsel, gpu_plugins, h_plugin_xpu),
833 mee_enum_h ("SPU plugin", 0, spu_plugsel, spu_plugins, h_plugin_xpu),
834 mee_handler_h ("Configure built-in GPU plugin", menu_loop_plugin_gpu, h_gpu),
835 mee_handler_h ("Configure built-in SPU plugin", menu_loop_plugin_spu, h_spu),
839 static int menu_loop_plugin_options(int id, int keys)
842 me_loop(e_menu_plugin_options, &sel, NULL);
845 snprintf(Config.Gpu, sizeof(Config.Gpu), "%s", gpu_plugins[gpu_plugsel]);
846 snprintf(Config.Spu, sizeof(Config.Spu), "%s", spu_plugins[spu_plugsel]);
851 // ------------ adv options menu ------------
853 static const char h_cfg_cpul[] = "Shows CPU usage in %%";
854 static const char h_cfg_fl[] = "Keeps the game from running too fast";
855 static const char h_cfg_xa[] = "Disables XA sound, which can sometimes improve performance";
856 static const char h_cfg_cdda[] = "Disable CD Audio for a performance boost\n"
857 "(proper .cue/.bin dump is needed otherwise)";
858 static const char h_cfg_sio[] = "This should be enabled for certain memcards/gamepads";
859 static const char h_cfg_spuirq[] = "Compatibility tweak; should probably be left off";
860 static const char h_cfg_rcnt1[] = "Parasite Eve 2, Vandal Hearts 1/2 Fix";
861 static const char h_cfg_rcnt2[] = "InuYasha Sengoku Battle Fix";
863 static menu_entry e_menu_adv_options[] =
865 mee_onoff_h ("Show CPU load", 0, g_opts, OPT_SHOWCPU, h_cfg_cpul),
866 mee_onoff_h ("Frame Limiter", 0, UseFrameLimit, 1, h_cfg_fl),
867 mee_onoff_h ("Disable XA Decoding", 0, Config.Xa, 1, h_cfg_xa),
868 mee_onoff_h ("Disable CD Audio", 0, Config.Cdda, 1, h_cfg_cdda),
869 mee_onoff_h ("SIO IRQ Always Enabled", 0, Config.Sio, 1, h_cfg_sio),
870 mee_onoff_h ("SPU IRQ Always Enabled", 0, Config.SpuIrq, 1, h_cfg_spuirq),
871 mee_onoff_h ("Rootcounter hack", 0, Config.RCntFix, 1, h_cfg_rcnt1),
872 mee_onoff_h ("Rootcounter hack 2", 0, Config.VSyncWA, 1, h_cfg_rcnt2),
876 static int menu_loop_adv_options(int id, int keys)
879 me_loop(e_menu_adv_options, &sel, NULL);
883 // ------------ options menu ------------
885 static int mh_restore_defaults(int id, int keys)
887 menu_set_defconfig();
888 me_update_msg("defaults restored");
892 static const char *men_region[] = { "NTSC", "PAL", NULL };
894 static const char *men_confirm_save[] = { "OFF", "writes", "loads", "both", NULL };
895 static const char h_confirm_save[] = "Ask for confirmation when overwriting save,\n"
896 "loading state or both";
898 static const char h_restore_def[] = "Switches back to default / recommended\n"
901 static menu_entry e_menu_options[] =
903 // mee_range ("Save slot", 0, state_slot, 0, 9),
904 // mee_enum_h ("Confirm savestate", 0, dummy, men_confirm_save, h_confirm_save),
905 mee_onoff ("Frameskip", 0, UseFrameSkip, 1),
906 mee_onoff ("Show FPS", 0, g_opts, OPT_SHOWFPS),
907 mee_enum ("Region", 0, Config.PsxType, men_region),
908 mee_range ("CPU clock", MA_OPT_CPU_CLOCKS, cpu_clock, 20, 5000),
909 mee_handler ("[Display]", menu_loop_gfx_options),
910 mee_handler ("[BIOS/Plugins]", menu_loop_plugin_options),
911 mee_handler ("[Advanced]", menu_loop_adv_options),
912 mee_cust_nosave("Save global config", MA_OPT_SAVECFG, mh_savecfg, mgn_saveloadcfg),
913 mee_cust_nosave("Save cfg for loaded game",MA_OPT_SAVECFG_GAME, mh_savecfg, mgn_saveloadcfg),
914 mee_handler_h ("Restore default config", mh_restore_defaults, h_restore_def),
918 static int menu_loop_options(int id, int keys)
923 i = me_id2offset(e_menu_options, MA_OPT_CPU_CLOCKS);
924 e_menu_options[i].enabled = cpu_clock != 0 ? 1 : 0;
925 me_enable(e_menu_options, MA_OPT_SAVECFG_GAME, ready_to_go);
927 me_loop(e_menu_options, &sel, NULL);
932 // ------------ debug menu ------------
934 static void draw_frame_debug(void)
936 smalltext_out16(4, 1, "build: "__DATE__ " " __TIME__ " " REV, 0xe7fc);
939 static void debug_menu_loop(void)
949 inp = in_menu_wait(PBTN_MOK|PBTN_MBACK|PBTN_MA2|PBTN_MA3|PBTN_L|PBTN_R |
950 PBTN_UP|PBTN_DOWN|PBTN_LEFT|PBTN_RIGHT, 70);
951 if (inp & PBTN_MBACK)
956 // ------------ main menu ------------
960 const char *plat_get_credits(void)
962 return "PCSX-ReARMed\n\n"
963 "(C) 1999-2003 PCSX Team\n"
964 "(C) 2005-2009 PCSX-df Team\n"
965 "(C) 2009-2010 PCSX-Reloaded Team\n\n"
966 "GPU and SPU code by Pete Bernert\n"
967 " and the P.E.Op.S. team\n"
968 "ARM recompiler (C) 2009-2010 Ari64\n"
969 "PCSX4ALL plugins by PCSX4ALL team\n"
970 " Chui, Franxis, Unai\n\n"
971 "integration, optimization and\n"
972 " frontend (C) 2010-2011 notaz\n";
975 static int run_cd_image(const char *fname)
977 extern void set_cd_image(const char *fname);
985 if (OpenPlugins() == -1) {
986 me_update_msg("failed to open plugins");
989 plugin_call_rearmed_cbs();
991 if (CheckCdrom() == -1) {
992 // Only check the CD if we are starting the console with a CD
994 me_update_msg("unsupported/invalid CD image");
1000 // Read main executable directly from CDRom and start it
1001 if (LoadCdrom() == -1) {
1003 me_update_msg("failed to load CD image");
1011 static int romsel_run(void)
1013 int prev_gpu, prev_spu;
1016 fname = menu_loop_romsel(last_selected_fname, sizeof(last_selected_fname));
1020 printf("selected file: %s\n", fname);
1022 if (run_cd_image(fname) != 0)
1025 prev_gpu = gpu_plugsel;
1026 prev_spu = spu_plugsel;
1027 if (menu_load_config(1) != 0)
1028 menu_load_config(0);
1030 // check for plugin changes, have to repeat
1031 // loading if game config changed plugins to reload them
1032 if (prev_gpu != gpu_plugsel || prev_spu != spu_plugsel) {
1033 printf("plugin change detected, reloading plugins..\n");
1034 if (run_cd_image(fname) != 0)
1038 strcpy(last_selected_fname, rom_fname_reload);
1042 static int main_menu_handler(int id, int keys)
1046 case MA_MAIN_RESUME_GAME:
1050 case MA_MAIN_SAVE_STATE:
1052 return menu_loop_savestate(0);
1054 case MA_MAIN_LOAD_STATE:
1056 return menu_loop_savestate(1);
1058 case MA_MAIN_RESET_GAME:
1063 if (CheckCdrom() != -1) {
1069 case MA_MAIN_LOAD_ROM:
1070 if (romsel_run() == 0)
1073 case MA_MAIN_CREDITS:
1074 draw_menu_credits(draw_frame_debug);
1075 in_menu_wait(PBTN_MOK|PBTN_MBACK, 70);
1081 lprintf("%s: something unknown selected\n", __FUNCTION__);
1088 static menu_entry e_menu_main[] =
1092 mee_handler_id("Resume game", MA_MAIN_RESUME_GAME, main_menu_handler),
1093 mee_handler_id("Save State", MA_MAIN_SAVE_STATE, main_menu_handler),
1094 mee_handler_id("Load State", MA_MAIN_LOAD_STATE, main_menu_handler),
1095 mee_handler_id("Reset game", MA_MAIN_RESET_GAME, main_menu_handler),
1096 mee_handler_id("Load CD image", MA_MAIN_LOAD_ROM, main_menu_handler),
1097 mee_handler ("Options", menu_loop_options),
1098 mee_handler ("Controls", menu_loop_keyconfig),
1099 mee_handler_id("Credits", MA_MAIN_CREDITS, main_menu_handler),
1100 mee_handler_id("Exit", MA_MAIN_EXIT, main_menu_handler),
1104 // ----------------------------
1106 static void menu_leave_emu(void);
1108 void menu_loop(void)
1114 me_enable(e_menu_main, MA_MAIN_RESUME_GAME, ready_to_go);
1115 me_enable(e_menu_main, MA_MAIN_SAVE_STATE, ready_to_go);
1116 me_enable(e_menu_main, MA_MAIN_LOAD_STATE, ready_to_go);
1117 me_enable(e_menu_main, MA_MAIN_RESET_GAME, ready_to_go);
1119 // menu_enter(ready_to_go);
1120 in_set_config_int(0, IN_CFG_BLOCKING, 1);
1123 me_loop(e_menu_main, &sel, NULL);
1124 } while (!ready_to_go);
1126 /* wait until menu, ok, back is released */
1127 while (in_menu_wait_any(50) & (PBTN_MENU|PBTN_MOK|PBTN_MBACK))
1130 in_set_config_int(0, IN_CFG_BLOCKING, 0);
1135 static void scan_plugins(void)
1137 char fname[MAXPATHLEN];
1143 gpu_plugins[0] = "builtin_gpu";
1144 spu_plugins[0] = "builtin_spu";
1147 snprintf(fname, sizeof(fname), "%s/", Config.PluginsDir);
1148 dir = opendir(fname);
1150 perror("scan_plugins opendir");
1164 p = strstr(ent->d_name, ".so");
1168 snprintf(fname, sizeof(fname), "%s/%s", Config.PluginsDir, ent->d_name);
1169 h = dlopen(fname, RTLD_LAZY | RTLD_LOCAL);
1171 fprintf(stderr, "%s\n", dlerror());
1175 // now what do we have here?
1176 tmp = dlsym(h, "GPUinit");
1179 if (gpu_i < ARRAY_SIZE(gpu_plugins) - 1)
1180 gpu_plugins[gpu_i++] = strdup(ent->d_name);
1184 tmp = dlsym(h, "SPUinit");
1187 if (spu_i < ARRAY_SIZE(spu_plugins) - 1)
1188 spu_plugins[spu_i++] = strdup(ent->d_name);
1192 fprintf(stderr, "ignoring unidentified plugin: %s\n", fname);
1199 void menu_init(void)
1201 char buff[MAXPATHLEN];
1203 strcpy(last_selected_fname, "/media");
1209 menu_set_defconfig();
1210 menu_load_config(0);
1215 g_menubg_src_ptr = calloc(g_menuscreen_w * g_menuscreen_h * 2, 1);
1216 if (g_menubg_src_ptr == NULL)
1218 emu_make_path(buff, "skin/background.png", sizeof(buff));
1219 readpng(g_menubg_src_ptr, buff, READPNG_BG, g_menuscreen_w, g_menuscreen_h);
1222 void menu_notify_mode_change(int w, int h, int bpp)
1228 if (scaling == SCALE_1_1) {
1229 g_layer_x = 800/2 - w/2; g_layer_y = 480/2 - h/2;
1230 g_layer_w = w; g_layer_h = h;
1234 static void menu_leave_emu(void)
1236 if (GPU_close != NULL) {
1237 int ret = GPU_close();
1239 fprintf(stderr, "Warning: GPU_close returned %d\n", ret);
1242 memcpy(g_menubg_ptr, g_menubg_src_ptr, g_menuscreen_w * g_menuscreen_h * 2);
1243 if (pl_fbdev_buf != NULL && ready_to_go && last_psx_bpp == 16) {
1244 int x = max(0, g_menuscreen_w - last_psx_w);
1245 int y = max(0, g_menuscreen_h / 2 - last_psx_h / 2);
1246 int w = min(g_menuscreen_w, last_psx_w);
1247 int h = min(g_menuscreen_h, last_psx_h);
1248 u16 *d = (u16 *)g_menubg_ptr + g_menuscreen_w * y + x;
1249 u16 *s = pl_fbdev_buf;
1251 for (; h > 0; h--, d += g_menuscreen_w, s += last_psx_w)
1252 menu_darken_bg(d, s, w, 0);
1255 plat_video_menu_enter(ready_to_go);
1258 void menu_prepare_emu(void)
1260 plat_video_menu_leave();
1264 menu_notify_mode_change(last_psx_w, last_psx_h, last_psx_bpp);
1267 g_layer_x = 80; g_layer_y = 0;
1268 g_layer_w = 640; g_layer_h = 480;
1270 case SCALE_FULLSCREEN:
1271 g_layer_x = 0; g_layer_y = 0;
1272 g_layer_w = 800; g_layer_h = 480;
1277 apply_filter(filter);
1281 // core doesn't care about Config.Cdda changes,
1282 // so handle them manually here
1286 // HACK to set up the frame limiter if softgpu is not used..
1287 if (gpu_plugsel != 0) {
1288 fFrameRateHz = Config.PsxType ? 50.0f : 59.94f;
1289 dwFrameRateTicks = (100000*100 / (unsigned long)(fFrameRateHz*100));
1292 if (GPU_open != NULL) {
1293 int ret = GPU_open(&gpuDisp, "PCSX", NULL);
1295 fprintf(stderr, "Warning: GPU_open returned %d\n", ret);
1299 void me_update_msg(const char *msg)
1301 strncpy(menu_error_msg, msg, sizeof(menu_error_msg));
1302 menu_error_msg[sizeof(menu_error_msg) - 1] = 0;
1304 menu_error_time = plat_get_ticks_ms();
1305 lprintf("msg: %s\n", menu_error_msg);