2 * (C) GraÅžvydas "notaz" Ignotas, 2010
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.
17 #include "plugin_lib.h"
19 #include "common/plat.h"
20 #include "../gui/Linux.h"
21 #include "../libpcsxcore/misc.h"
22 #include "../libpcsxcore/new_dynarec/new_dynarec.h"
26 #define array_size(x) (sizeof(x) / sizeof(x[0]))
58 extern int ready_to_go;
59 static int game_config_loaded, last_psx_w, last_psx_h;
60 static int scaling, filter, state_slot, cpu_clock;
61 static int dummy, state_slot;
62 static char rom_fname_reload[MAXPATHLEN];
63 static char last_selected_fname[MAXPATHLEN];
65 void emu_make_path(char *buff, const char *end, int size)
69 end_len = strlen(end);
70 pos = plat_get_root_dir(buff, size);
71 strncpy(buff + pos, end, size - pos);
73 if (pos + end_len > size - 1)
74 printf("Warning: path truncated: %s\n", buff);
77 static int emu_check_save_file(int slot)
82 fname = get_state_filename(slot);
86 ret = CheckState(fname);
88 return ret == 0 ? 1 : 0;
91 static int emu_save_load_game(int load, int sram)
96 fname = get_state_filename(state_slot);
101 ret = LoadState(fname);
103 ret = SaveState(fname);
109 static void draw_savestate_bg(int slot)
113 static void menu_set_defconfig(void)
118 static int menu_write_config(int is_game)
123 static int menu_load_config(int is_game)
128 // rrrr rggg gggb bbbb
129 static unsigned short fname2color(const char *fname)
131 static const char *cdimg_exts[] = { ".bin", ".img", ".iso", ".z", ".cue" };
132 static const char *other_exts[] = { ".ccd", ".toc", ".mds", ".sub", ".table" };
133 const char *ext = strrchr(fname, '.');
138 for (i = 0; i < array_size(cdimg_exts); i++)
139 if (strcasecmp(ext, cdimg_exts[i]) == 0)
141 for (i = 0; i < array_size(other_exts); i++)
142 if (strcasecmp(ext, other_exts[i]) == 0)
147 #define menu_init menu_init_common
148 #include "common/menu.c"
151 // ---------- pandora specific -----------
153 static const char pnd_script_base[] = "sudo -n /usr/pandora/scripts";
154 static char **pnd_filter_list;
156 static int get_cpu_clock(void)
160 f = fopen("/proc/pandora/cpu_mhz_max", "r");
162 fscanf(f, "%d", &ret);
168 static void apply_cpu_clock(void)
172 if (cpu_clock != 0 && cpu_clock != get_cpu_clock()) {
173 snprintf(buf, sizeof(buf), "unset DISPLAY; echo y | %s/op_cpuspeed.sh %d",
174 pnd_script_base, cpu_clock);
179 static void apply_filter(int which)
184 if (pnd_filter_list == NULL)
187 for (i = 0; i < which; i++)
188 if (pnd_filter_list[i] == NULL)
191 if (pnd_filter_list[i] == NULL)
194 snprintf(buf, sizeof(buf), "%s/op_videofir.sh %s", pnd_script_base, pnd_filter_list[i]);
198 static menu_entry e_menu_gfx_options[];
200 static void pnd_menu_init(void)
208 cpu_clock = get_cpu_clock();
210 dir = opendir("/etc/pandora/conf/dss_fir");
212 perror("filter opendir");
224 p = strstr(ent->d_name, "_up");
225 if (p != NULL && (p[3] == 0 || !strcmp(p + 3, "_h")))
232 mfilters = calloc(count + 1, sizeof(mfilters[0]));
233 if (mfilters == NULL)
237 for (i = 0; (ent = readdir(dir)); ) {
240 p = strstr(ent->d_name, "_up");
241 if (p == NULL || (p[3] != 0 && strcmp(p + 3, "_h")))
244 len = p - ent->d_name;
245 if (len > sizeof(buff) - 1)
248 strncpy(buff, ent->d_name, len);
250 mfilters[i] = strdup(buff);
251 if (mfilters[i] != NULL)
256 i = me_id2offset(e_menu_gfx_options, MA_OPT_FILTERING);
257 e_menu_gfx_options[i].data = (void *)mfilters;
258 pnd_filter_list = mfilters;
261 // -------------- key config --------------
263 me_bind_action me_ctrl_actions[] =
265 { "UP ", 1 << DKEY_UP},
266 { "DOWN ", 1 << DKEY_DOWN },
267 { "LEFT ", 1 << DKEY_LEFT },
268 { "RIGHT ", 1 << DKEY_RIGHT },
269 { "TRIANGLE", 1 << DKEY_TRIANGLE },
270 { "CIRCLE ", 1 << DKEY_SQUARE },
271 { "CROSS ", 1 << DKEY_CROSS },
272 { "SQUARE ", 1 << DKEY_SQUARE },
273 { "L1 ", 1 << DKEY_L1 },
274 { "R1 ", 1 << DKEY_R1 },
275 { "L2 ", 1 << DKEY_L2 },
276 { "R2 ", 1 << DKEY_R2 },
277 { "START ", 1 << DKEY_START },
278 { "SELECT ", 1 << DKEY_SELECT },
282 me_bind_action emuctrl_actions[] =
284 { "Load State ", PEV_STATE_LOAD },
285 { "Save State ", PEV_STATE_SAVE },
286 { "Prev Save Slot ", PEV_SSLOT_PREV },
287 { "Next Save Slot ", PEV_SSLOT_NEXT },
288 { "Enter Menu ", PEV_MENU },
292 static int key_config_loop_wrap(int id, int keys)
295 case MA_CTRL_PLAYER1:
296 key_config_loop(me_ctrl_actions, array_size(me_ctrl_actions) - 1, 0);
298 case MA_CTRL_PLAYER2:
299 key_config_loop(me_ctrl_actions, array_size(me_ctrl_actions) - 1, 1);
302 key_config_loop(emuctrl_actions, array_size(emuctrl_actions) - 1, -1);
310 static const char *mgn_dev_name(int id, int *offs)
312 const char *name = NULL;
315 if (id == MA_CTRL_DEV_FIRST)
318 for (; it < IN_MAX_DEVS; it++) {
319 name = in_get_dev_name(it, 1, 1);
328 static const char *mgn_saveloadcfg(int id, int *offs)
333 static int mh_saveloadcfg(int id, int keys)
337 case MA_OPT_SAVECFG_GAME:
338 if (menu_write_config(id == MA_OPT_SAVECFG_GAME ? 1 : 0) == 0)
339 me_update_msg("config saved");
341 me_update_msg("failed to write config");
350 static menu_entry e_menu_keyconfig[] =
352 mee_handler_id("Player 1", MA_CTRL_PLAYER1, key_config_loop_wrap),
353 mee_handler_id("Player 2", MA_CTRL_PLAYER2, key_config_loop_wrap),
354 mee_handler_id("Emulator controls", MA_CTRL_EMU, key_config_loop_wrap),
355 mee_cust_nosave("Save global config", MA_OPT_SAVECFG, mh_saveloadcfg, mgn_saveloadcfg),
356 mee_cust_nosave("Save cfg for loaded game", MA_OPT_SAVECFG_GAME, mh_saveloadcfg, mgn_saveloadcfg),
358 mee_label ("Input devices:"),
359 mee_label_mk (MA_CTRL_DEV_FIRST, mgn_dev_name),
360 mee_label_mk (MA_CTRL_DEV_NEXT, mgn_dev_name),
361 mee_label_mk (MA_CTRL_DEV_NEXT, mgn_dev_name),
362 mee_label_mk (MA_CTRL_DEV_NEXT, mgn_dev_name),
363 mee_label_mk (MA_CTRL_DEV_NEXT, mgn_dev_name),
364 mee_label_mk (MA_CTRL_DEV_NEXT, mgn_dev_name),
365 mee_label_mk (MA_CTRL_DEV_NEXT, mgn_dev_name),
369 static int menu_loop_keyconfig(int id, int keys)
373 me_enable(e_menu_keyconfig, MA_OPT_SAVECFG_GAME, ready_to_go);
374 me_loop(e_menu_keyconfig, &sel, NULL);
378 // ------------ adv options menu ------------
380 static menu_entry e_menu_adv_options[] =
382 mee_onoff ("captain placeholder", 0, dummy, 1),
386 static int menu_loop_adv_options(int id, int keys)
389 me_loop(e_menu_adv_options, &sel, NULL);
393 // ------------ gfx options menu ------------
395 static const char *men_scaler[] = { "1x1", "scaled 4:3", "fullscreen", "custom", NULL };
396 static const char h_cscaler[] = "Displays the scaler layer, you can resize it\n"
397 "using d-pad or move it using R+d-pad";
398 static const char *men_dummy[] = { NULL };
400 static int menu_loop_cscaler(int id, int keys)
404 scaling = SCALE_CUSTOM;
406 omap_enable_layer(1);
407 //pnd_restore_layer_data();
412 memset(g_menuscreen_ptr, 0, g_menuscreen_w * g_menuscreen_h * 2);
413 text_out16(2, 480 - 18, "%dx%d | d-pad to resize, R+d-pad to move", g_layer_w, g_layer_h);
416 inp = in_menu_wait(PBTN_UP|PBTN_DOWN|PBTN_LEFT|PBTN_RIGHT|PBTN_R|PBTN_MOK|PBTN_MBACK, 40);
417 if (inp & PBTN_UP) g_layer_y--;
418 if (inp & PBTN_DOWN) g_layer_y++;
419 if (inp & PBTN_LEFT) g_layer_x--;
420 if (inp & PBTN_RIGHT) g_layer_x++;
421 if (!(inp & PBTN_R)) {
422 if (inp & PBTN_UP) g_layer_h += 2;
423 if (inp & PBTN_DOWN) g_layer_h -= 2;
424 if (inp & PBTN_LEFT) g_layer_w += 2;
425 if (inp & PBTN_RIGHT) g_layer_w -= 2;
427 if (inp & (PBTN_MOK|PBTN_MBACK))
430 if (inp & (PBTN_UP|PBTN_DOWN|PBTN_LEFT|PBTN_RIGHT)) {
431 if (g_layer_x < 0) g_layer_x = 0;
432 if (g_layer_x > 640) g_layer_x = 640;
433 if (g_layer_y < 0) g_layer_y = 0;
434 if (g_layer_y > 420) g_layer_y = 420;
435 if (g_layer_w < 160) g_layer_w = 160;
436 if (g_layer_h < 60) g_layer_h = 60;
437 if (g_layer_x + g_layer_w > 800)
438 g_layer_w = 800 - g_layer_x;
439 if (g_layer_y + g_layer_h > 480)
440 g_layer_h = 480 - g_layer_y;
441 omap_enable_layer(1);
445 omap_enable_layer(0);
450 static menu_entry e_menu_gfx_options[] =
452 mee_enum ("Scaler", 0, scaling, men_scaler),
453 mee_enum ("Filter", MA_OPT_FILTERING, filter, men_dummy),
454 // mee_onoff ("Vsync", 0, vsync, 1),
455 mee_cust_h ("Setup custom scaler", 0, menu_loop_cscaler, NULL, h_cscaler),
459 static int menu_loop_gfx_options(int id, int keys)
463 me_loop(e_menu_gfx_options, &sel, NULL);
468 // ------------ options menu ------------
470 static int mh_restore_defaults(int id, int keys)
472 menu_set_defconfig();
473 me_update_msg("defaults restored");
477 static const char *men_confirm_save[] = { "OFF", "writes", "loads", "both", NULL };
478 static const char h_confirm_save[] = "Ask for confirmation when overwriting save,\n"
479 "loading state or both";
481 static menu_entry e_menu_options[] =
483 mee_range ("Save slot", 0, state_slot, 0, 9),
484 mee_enum_h ("Confirm savestate", 0, dummy, men_confirm_save, h_confirm_save),
485 mee_range ("CPU clock", MA_OPT_CPU_CLOCKS, cpu_clock, 20, 5000),
486 mee_handler ("[Display]", menu_loop_gfx_options),
487 mee_handler ("[Advanced]", menu_loop_adv_options),
488 mee_cust_nosave("Save global config", MA_OPT_SAVECFG, mh_saveloadcfg, mgn_saveloadcfg),
489 mee_cust_nosave("Save cfg for loaded game",MA_OPT_SAVECFG_GAME, mh_saveloadcfg, mgn_saveloadcfg),
490 mee_handler ("Restore defaults", mh_restore_defaults),
494 static int menu_loop_options(int id, int keys)
499 i = me_id2offset(e_menu_options, MA_OPT_CPU_CLOCKS);
500 e_menu_options[i].enabled = cpu_clock != 0 ? 1 : 0;
501 me_enable(e_menu_options, MA_OPT_SAVECFG_GAME, ready_to_go);
503 me_loop(e_menu_options, &sel, NULL);
508 // ------------ debug menu ------------
510 static void draw_frame_debug(void)
512 smalltext_out16(4, 1, "build: "__DATE__ " " __TIME__ " " REV, 0xe7fc);
515 static void debug_menu_loop(void)
525 inp = in_menu_wait(PBTN_MOK|PBTN_MBACK|PBTN_MA2|PBTN_MA3|PBTN_L|PBTN_R |
526 PBTN_UP|PBTN_DOWN|PBTN_LEFT|PBTN_RIGHT, 70);
527 if (inp & PBTN_MBACK)
532 // ------------ main menu ------------
534 const char *plat_get_credits(void)
536 return "(C) 1999-2003 PCSX Team\n"
537 "(C) 2005-2009 PCSX-df Team\n"
538 "(C) 2009-2010 PCSX-Reloaded Team\n\n"
539 "GPU and SPU code by Pete Bernert\n"
540 " and the P.E.Op.S. team\n"
541 "ARM recompiler by Ari64\n\n"
542 "integration, optimization and\n"
543 " frontend by (C) notaz, 2010\n";
546 static char *romsel_run(void)
548 extern void set_cd_image(const char *fname);
551 ret = menu_loop_romsel(last_selected_fname, sizeof(last_selected_fname));
555 lprintf("selected file: %s\n", ret);
561 if (OpenPlugins() == -1) {
562 me_update_msg("failed to open plugins");
568 if (CheckCdrom() == -1) {
569 // Only check the CD if we are starting the console with a CD
571 me_update_msg("unsupported/invalid CD image");
575 // Read main executable directly from CDRom and start it
576 if (LoadCdrom() == -1) {
578 me_update_msg("failed to load CD image");
582 game_config_loaded = 0;
587 static int main_menu_handler(int id, int keys)
593 case MA_MAIN_RESUME_GAME:
597 case MA_MAIN_SAVE_STATE:
599 return menu_loop_savestate(0);
601 case MA_MAIN_LOAD_STATE:
603 return menu_loop_savestate(1);
605 case MA_MAIN_RESET_GAME:
609 if (CheckCdrom() != -1) {
615 case MA_MAIN_LOAD_ROM:
616 ret_name = romsel_run();
617 if (ret_name != NULL)
620 case MA_MAIN_CREDITS:
621 draw_menu_credits(draw_frame_debug);
622 in_menu_wait(PBTN_MOK|PBTN_MBACK, 70);
627 lprintf("%s: something unknown selected\n", __FUNCTION__);
634 static menu_entry e_menu_main[] =
638 mee_handler_id("Resume game", MA_MAIN_RESUME_GAME, main_menu_handler),
639 mee_handler_id("Save State", MA_MAIN_SAVE_STATE, main_menu_handler),
640 mee_handler_id("Load State", MA_MAIN_LOAD_STATE, main_menu_handler),
641 mee_handler_id("Reset game", MA_MAIN_RESET_GAME, main_menu_handler),
642 mee_handler_id("Load CD image", MA_MAIN_LOAD_ROM, main_menu_handler),
643 mee_handler ("Options", menu_loop_options),
644 mee_handler ("Controls", menu_loop_keyconfig),
645 mee_handler_id("Credits", MA_MAIN_CREDITS, main_menu_handler),
646 mee_handler_id("Exit", MA_MAIN_EXIT, main_menu_handler),
650 // ----------------------------
656 omap_enable_layer(0);
658 me_enable(e_menu_main, MA_MAIN_RESUME_GAME, ready_to_go);
659 me_enable(e_menu_main, MA_MAIN_SAVE_STATE, ready_to_go);
660 me_enable(e_menu_main, MA_MAIN_LOAD_STATE, ready_to_go);
661 me_enable(e_menu_main, MA_MAIN_RESET_GAME, ready_to_go);
663 strcpy(last_selected_fname, "/mnt/ntz/stuff/psx");
664 menu_enter(ready_to_go);
665 in_set_config_int(0, IN_CFG_BLOCKING, 1);
668 me_loop(e_menu_main, &sel, draw_frame_debug);
669 } while (!ready_to_go);
671 /* wait until menu, ok, back is released */
672 while (in_menu_wait_any(50) & (PBTN_MENU|PBTN_MOK|PBTN_MBACK))
675 in_set_config_int(0, IN_CFG_BLOCKING, 0);
677 memset(g_menuscreen_ptr, 0, g_menuscreen_w * g_menuscreen_h * 2);
684 menu_set_defconfig();
692 void menu_notify_mode_change(int w, int h)
697 if (scaling == SCALE_1_1) {
698 g_layer_x = 800/2 - w/2; g_layer_y = 480/2 - h/2;
699 g_layer_w = w; g_layer_h = h;
700 omap_enable_layer(1);
704 void menu_prepare_emu(void)
706 if (!game_config_loaded) {
708 game_config_loaded = 1;
713 menu_notify_mode_change(last_psx_w, last_psx_h);
716 g_layer_x = 80; g_layer_y = 0;
717 g_layer_w = 640; g_layer_h = 480;
719 case SCALE_FULLSCREEN:
720 g_layer_x = 0; g_layer_y = 0;
721 g_layer_w = 800; g_layer_h = 480;
726 omap_enable_layer(1);
727 apply_filter(filter);
732 void me_update_msg(const char *msg)
734 strncpy(menu_error_msg, msg, sizeof(menu_error_msg));
735 menu_error_msg[sizeof(menu_error_msg) - 1] = 0;
737 menu_error_time = plat_get_ticks_ms();
738 lprintf("msg: %s\n", menu_error_msg);