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 #define menu_init menu_init_common
129 #include "common/menu.c"
132 // ---------- pandora specific -----------
134 static const char pnd_script_base[] = "sudo -n /usr/pandora/scripts";
135 static char **pnd_filter_list;
137 static int get_cpu_clock(void)
141 f = fopen("/proc/pandora/cpu_mhz_max", "r");
143 fscanf(f, "%d", &ret);
149 static void apply_cpu_clock(void)
153 if (cpu_clock != 0 && cpu_clock != get_cpu_clock()) {
154 snprintf(buf, sizeof(buf), "unset DISPLAY; echo y | %s/op_cpuspeed.sh %d",
155 pnd_script_base, cpu_clock);
160 static void apply_filter(int which)
165 if (pnd_filter_list == NULL)
168 for (i = 0; i < which; i++)
169 if (pnd_filter_list[i] == NULL)
172 if (pnd_filter_list[i] == NULL)
175 snprintf(buf, sizeof(buf), "%s/op_videofir.sh %s", pnd_script_base, pnd_filter_list[i]);
179 static menu_entry e_menu_gfx_options[];
181 static void pnd_menu_init(void)
189 cpu_clock = get_cpu_clock();
191 dir = opendir("/etc/pandora/conf/dss_fir");
193 perror("filter opendir");
205 p = strstr(ent->d_name, "_up");
206 if (p != NULL && (p[3] == 0 || !strcmp(p + 3, "_h")))
213 mfilters = calloc(count + 1, sizeof(mfilters[0]));
214 if (mfilters == NULL)
218 for (i = 0; (ent = readdir(dir)); ) {
221 p = strstr(ent->d_name, "_up");
222 if (p == NULL || (p[3] != 0 && strcmp(p + 3, "_h")))
225 len = p - ent->d_name;
226 if (len > sizeof(buff) - 1)
229 strncpy(buff, ent->d_name, len);
231 mfilters[i] = strdup(buff);
232 if (mfilters[i] != NULL)
237 i = me_id2offset(e_menu_gfx_options, MA_OPT_FILTERING);
238 e_menu_gfx_options[i].data = (void *)mfilters;
239 pnd_filter_list = mfilters;
242 // -------------- key config --------------
244 me_bind_action me_ctrl_actions[] =
246 { "UP ", 1 << DKEY_UP},
247 { "DOWN ", 1 << DKEY_DOWN },
248 { "LEFT ", 1 << DKEY_LEFT },
249 { "RIGHT ", 1 << DKEY_RIGHT },
250 { "TRIANGLE", 1 << DKEY_TRIANGLE },
251 { "CIRCLE ", 1 << DKEY_SQUARE },
252 { "CROSS ", 1 << DKEY_CROSS },
253 { "SQUARE ", 1 << DKEY_SQUARE },
254 { "L1 ", 1 << DKEY_L1 },
255 { "R1 ", 1 << DKEY_R1 },
256 { "L2 ", 1 << DKEY_L2 },
257 { "R2 ", 1 << DKEY_R2 },
258 { "START ", 1 << DKEY_START },
259 { "SELECT ", 1 << DKEY_SELECT },
263 me_bind_action emuctrl_actions[] =
265 { "Load State ", PEV_STATE_LOAD },
266 { "Save State ", PEV_STATE_SAVE },
267 { "Prev Save Slot ", PEV_SSLOT_PREV },
268 { "Next Save Slot ", PEV_SSLOT_NEXT },
269 { "Enter Menu ", PEV_MENU },
273 static int key_config_loop_wrap(int id, int keys)
276 case MA_CTRL_PLAYER1:
277 key_config_loop(me_ctrl_actions, array_size(me_ctrl_actions) - 1, 0);
279 case MA_CTRL_PLAYER2:
280 key_config_loop(me_ctrl_actions, array_size(me_ctrl_actions) - 1, 1);
283 key_config_loop(emuctrl_actions, array_size(emuctrl_actions) - 1, -1);
291 static const char *mgn_dev_name(int id, int *offs)
293 const char *name = NULL;
296 if (id == MA_CTRL_DEV_FIRST)
299 for (; it < IN_MAX_DEVS; it++) {
300 name = in_get_dev_name(it, 1, 1);
309 static const char *mgn_saveloadcfg(int id, int *offs)
314 static int mh_saveloadcfg(int id, int keys)
318 case MA_OPT_SAVECFG_GAME:
319 if (menu_write_config(id == MA_OPT_SAVECFG_GAME ? 1 : 0) == 0)
320 me_update_msg("config saved");
322 me_update_msg("failed to write config");
331 static menu_entry e_menu_keyconfig[] =
333 mee_handler_id("Player 1", MA_CTRL_PLAYER1, key_config_loop_wrap),
334 mee_handler_id("Player 2", MA_CTRL_PLAYER2, key_config_loop_wrap),
335 mee_handler_id("Emulator controls", MA_CTRL_EMU, key_config_loop_wrap),
336 mee_cust_nosave("Save global config", MA_OPT_SAVECFG, mh_saveloadcfg, mgn_saveloadcfg),
337 mee_cust_nosave("Save cfg for loaded game", MA_OPT_SAVECFG_GAME, mh_saveloadcfg, mgn_saveloadcfg),
339 mee_label ("Input devices:"),
340 mee_label_mk (MA_CTRL_DEV_FIRST, mgn_dev_name),
341 mee_label_mk (MA_CTRL_DEV_NEXT, mgn_dev_name),
342 mee_label_mk (MA_CTRL_DEV_NEXT, mgn_dev_name),
343 mee_label_mk (MA_CTRL_DEV_NEXT, mgn_dev_name),
344 mee_label_mk (MA_CTRL_DEV_NEXT, mgn_dev_name),
345 mee_label_mk (MA_CTRL_DEV_NEXT, mgn_dev_name),
346 mee_label_mk (MA_CTRL_DEV_NEXT, mgn_dev_name),
350 static int menu_loop_keyconfig(int id, int keys)
354 me_enable(e_menu_keyconfig, MA_OPT_SAVECFG_GAME, ready_to_go);
355 me_loop(e_menu_keyconfig, &sel, NULL);
359 // ------------ adv options menu ------------
361 static menu_entry e_menu_adv_options[] =
363 mee_onoff ("captain placeholder", 0, dummy, 1),
367 static int menu_loop_adv_options(int id, int keys)
370 me_loop(e_menu_adv_options, &sel, NULL);
374 // ------------ gfx options menu ------------
376 static const char *men_scaler[] = { "1x1", "scaled 4:3", "fullscreen", "custom", NULL };
377 static const char h_cscaler[] = "Displays the scaler layer, you can resize it\n"
378 "using d-pad or move it using R+d-pad";
379 static const char *men_dummy[] = { NULL };
381 static int menu_loop_cscaler(int id, int keys)
385 scaling = SCALE_CUSTOM;
387 omap_enable_layer(1);
388 //pnd_restore_layer_data();
393 memset(g_menuscreen_ptr, 0, g_menuscreen_w * g_menuscreen_h * 2);
394 text_out16(2, 480 - 18, "%dx%d | d-pad to resize, R+d-pad to move", g_layer_w, g_layer_h);
397 inp = in_menu_wait(PBTN_UP|PBTN_DOWN|PBTN_LEFT|PBTN_RIGHT|PBTN_R|PBTN_MOK|PBTN_MBACK, 40);
398 if (inp & PBTN_UP) g_layer_y--;
399 if (inp & PBTN_DOWN) g_layer_y++;
400 if (inp & PBTN_LEFT) g_layer_x--;
401 if (inp & PBTN_RIGHT) g_layer_x++;
402 if (!(inp & PBTN_R)) {
403 if (inp & PBTN_UP) g_layer_h += 2;
404 if (inp & PBTN_DOWN) g_layer_h -= 2;
405 if (inp & PBTN_LEFT) g_layer_w += 2;
406 if (inp & PBTN_RIGHT) g_layer_w -= 2;
408 if (inp & (PBTN_MOK|PBTN_MBACK))
411 if (inp & (PBTN_UP|PBTN_DOWN|PBTN_LEFT|PBTN_RIGHT)) {
412 if (g_layer_x < 0) g_layer_x = 0;
413 if (g_layer_x > 640) g_layer_x = 640;
414 if (g_layer_y < 0) g_layer_y = 0;
415 if (g_layer_y > 420) g_layer_y = 420;
416 if (g_layer_w < 160) g_layer_w = 160;
417 if (g_layer_h < 60) g_layer_h = 60;
418 if (g_layer_x + g_layer_w > 800)
419 g_layer_w = 800 - g_layer_x;
420 if (g_layer_y + g_layer_h > 480)
421 g_layer_h = 480 - g_layer_y;
422 omap_enable_layer(1);
426 omap_enable_layer(0);
431 static menu_entry e_menu_gfx_options[] =
433 mee_enum ("Scaler", 0, scaling, men_scaler),
434 mee_enum ("Filter", MA_OPT_FILTERING, filter, men_dummy),
435 // mee_onoff ("Vsync", 0, vsync, 1),
436 mee_cust_h ("Setup custom scaler", 0, menu_loop_cscaler, NULL, h_cscaler),
440 static int menu_loop_gfx_options(int id, int keys)
444 me_loop(e_menu_gfx_options, &sel, NULL);
449 // ------------ options menu ------------
451 static int mh_restore_defaults(int id, int keys)
453 menu_set_defconfig();
454 me_update_msg("defaults restored");
458 static const char *men_confirm_save[] = { "OFF", "writes", "loads", "both", NULL };
459 static const char h_confirm_save[] = "Ask for confirmation when overwriting save,\n"
460 "loading state or both";
462 static menu_entry e_menu_options[] =
464 mee_range ("Save slot", 0, state_slot, 0, 9),
465 mee_enum_h ("Confirm savestate", 0, dummy, men_confirm_save, h_confirm_save),
466 mee_range ("CPU clock", MA_OPT_CPU_CLOCKS, cpu_clock, 20, 5000),
467 mee_handler ("[Display]", menu_loop_gfx_options),
468 mee_handler ("[Advanced]", menu_loop_adv_options),
469 mee_cust_nosave("Save global config", MA_OPT_SAVECFG, mh_saveloadcfg, mgn_saveloadcfg),
470 mee_cust_nosave("Save cfg for loaded game",MA_OPT_SAVECFG_GAME, mh_saveloadcfg, mgn_saveloadcfg),
471 mee_handler ("Restore defaults", mh_restore_defaults),
475 static int menu_loop_options(int id, int keys)
480 i = me_id2offset(e_menu_options, MA_OPT_CPU_CLOCKS);
481 e_menu_options[i].enabled = cpu_clock != 0 ? 1 : 0;
482 me_enable(e_menu_options, MA_OPT_SAVECFG_GAME, ready_to_go);
484 me_loop(e_menu_options, &sel, NULL);
489 // ------------ debug menu ------------
491 static void draw_frame_debug(void)
493 smalltext_out16(4, 1, "build: "__DATE__ " " __TIME__ " " REV, 0xe7fc);
496 static void debug_menu_loop(void)
506 inp = in_menu_wait(PBTN_MOK|PBTN_MBACK|PBTN_MA2|PBTN_MA3|PBTN_L|PBTN_R |
507 PBTN_UP|PBTN_DOWN|PBTN_LEFT|PBTN_RIGHT, 70);
508 if (inp & PBTN_MBACK)
513 // ------------ main menu ------------
515 const char *plat_get_credits(void)
517 return "(C) 1999-2003 PCSX Team\n"
518 "(C) 2005-2009 PCSX-df Team\n"
519 "(C) 2009-2010 PCSX-Reloaded Team\n\n"
520 "GPU and SPU code by Pete Bernert\n"
521 " and the P.E.Op.S. team\n"
522 "ARM recompiler by Ari64\n\n"
523 "integration, optimization and\n"
524 " frontend by (C) notaz, 2010\n";
527 static char *romsel_run(void)
531 ret = menu_loop_romsel(last_selected_fname, sizeof(last_selected_fname));
535 lprintf("selected file: %s\n", ret);
541 if (OpenPlugins() == -1) {
542 me_update_msg("failed to open plugins");
548 if (CheckCdrom() == -1) {
549 // Only check the CD if we are starting the console with a CD
551 me_update_msg("unsupported/invalid CD image");
555 // Read main executable directly from CDRom and start it
556 if (LoadCdrom() == -1) {
558 me_update_msg("failed to load CD image");
562 game_config_loaded = 0;
567 static int main_menu_handler(int id, int keys)
573 case MA_MAIN_RESUME_GAME:
577 case MA_MAIN_SAVE_STATE:
579 return menu_loop_savestate(0);
581 case MA_MAIN_LOAD_STATE:
583 return menu_loop_savestate(1);
585 case MA_MAIN_RESET_GAME:
589 if (CheckCdrom() != -1) {
595 case MA_MAIN_LOAD_ROM:
596 ret_name = romsel_run();
597 if (ret_name != NULL)
600 case MA_MAIN_CREDITS:
601 draw_menu_credits(draw_frame_debug);
602 in_menu_wait(PBTN_MOK|PBTN_MBACK, 70);
607 lprintf("%s: something unknown selected\n", __FUNCTION__);
614 static menu_entry e_menu_main[] =
618 mee_handler_id("Resume game", MA_MAIN_RESUME_GAME, main_menu_handler),
619 mee_handler_id("Save State", MA_MAIN_SAVE_STATE, main_menu_handler),
620 mee_handler_id("Load State", MA_MAIN_LOAD_STATE, main_menu_handler),
621 mee_handler_id("Reset game", MA_MAIN_RESET_GAME, main_menu_handler),
622 mee_handler_id("Load CD image", MA_MAIN_LOAD_ROM, main_menu_handler),
623 mee_handler ("Options", menu_loop_options),
624 mee_handler ("Controls", menu_loop_keyconfig),
625 mee_handler_id("Credits", MA_MAIN_CREDITS, main_menu_handler),
626 mee_handler_id("Exit", MA_MAIN_EXIT, main_menu_handler),
630 // ----------------------------
636 omap_enable_layer(0);
638 me_enable(e_menu_main, MA_MAIN_RESUME_GAME, ready_to_go);
639 me_enable(e_menu_main, MA_MAIN_SAVE_STATE, ready_to_go);
640 me_enable(e_menu_main, MA_MAIN_LOAD_STATE, ready_to_go);
641 me_enable(e_menu_main, MA_MAIN_RESET_GAME, ready_to_go);
643 strcpy(last_selected_fname, "/mnt/ntz/stuff/psx");
644 menu_enter(ready_to_go);
645 in_set_config_int(0, IN_CFG_BLOCKING, 1);
648 me_loop(e_menu_main, &sel, draw_frame_debug);
649 } while (!ready_to_go);
651 /* wait until menu, ok, back is released */
652 while (in_menu_wait_any(50) & (PBTN_MENU|PBTN_MOK|PBTN_MBACK))
655 in_set_config_int(0, IN_CFG_BLOCKING, 0);
657 memset(g_menuscreen_ptr, 0, g_menuscreen_w * g_menuscreen_h * 2);
664 menu_set_defconfig();
672 void menu_notify_mode_change(int w, int h)
677 if (scaling == SCALE_1_1) {
678 g_layer_x = 800/2 - w/2; g_layer_y = 480/2 - h/2;
679 g_layer_w = w; g_layer_h = h;
680 omap_enable_layer(1);
684 void menu_prepare_emu(void)
686 if (!game_config_loaded) {
688 game_config_loaded = 1;
693 menu_notify_mode_change(last_psx_w, last_psx_h);
696 g_layer_x = 80; g_layer_y = 0;
697 g_layer_w = 640; g_layer_h = 480;
699 case SCALE_FULLSCREEN:
700 g_layer_x = 0; g_layer_y = 0;
701 g_layer_w = 800; g_layer_h = 480;
706 omap_enable_layer(1);
707 apply_filter(filter);
712 void me_update_msg(const char *msg)
714 strncpy(menu_error_msg, msg, sizeof(menu_error_msg));
715 menu_error_msg[sizeof(menu_error_msg) - 1] = 0;
717 menu_error_time = plat_get_ticks_ms();
718 lprintf("msg: %s\n", menu_error_msg);