more complete credit listing in readme
[pcsx_rearmed.git] / frontend / menu.c
1 /*
2  * (C) GraÅžvydas "notaz" Ignotas, 2010
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>
13 #include <errno.h>
14
15 #include "menu.h"
16 #include "config.h"
17 #include "plugin_lib.h"
18 #include "omap.h"
19 #include "common/plat.h"
20 #include "../gui/Linux.h"
21 #include "../libpcsxcore/misc.h"
22 #include "../libpcsxcore/new_dynarec/new_dynarec.h"
23 #include "revision.h"
24
25 #define MENU_X2 1
26 #define array_size(x) (sizeof(x) / sizeof(x[0]))
27
28 typedef enum
29 {
30         MA_NONE = 1,
31         MA_MAIN_RESUME_GAME,
32         MA_MAIN_SAVE_STATE,
33         MA_MAIN_LOAD_STATE,
34         MA_MAIN_RESET_GAME,
35         MA_MAIN_LOAD_ROM,
36         MA_MAIN_CONTROLS,
37         MA_MAIN_CREDITS,
38         MA_MAIN_EXIT,
39         MA_CTRL_PLAYER1,
40         MA_CTRL_PLAYER2,
41         MA_CTRL_EMU,
42         MA_CTRL_DEV_FIRST,
43         MA_CTRL_DEV_NEXT,
44         MA_CTRL_DONE,
45         MA_OPT_SAVECFG,
46         MA_OPT_SAVECFG_GAME,
47         MA_OPT_CPU_CLOCKS,
48         MA_OPT_FILTERING,
49 } menu_id;
50
51 enum {
52         SCALE_1_1,
53         SCALE_4_3,
54         SCALE_FULLSCREEN,
55         SCALE_CUSTOM,
56 };
57
58 extern int ready_to_go;
59 static int game_config_loaded;
60 static int last_psx_w, last_psx_h, last_psx_bpp;
61 static int scaling, filter, state_slot, cpu_clock;
62 static char rom_fname_reload[MAXPATHLEN];
63 static char last_selected_fname[MAXPATHLEN];
64 int g_opts;
65
66 // from softgpu plugin
67 extern int iUseDither;
68 extern int UseFrameSkip;
69 extern uint32_t dwActFixes;
70
71 // sound plugin
72 extern int iUseReverb;
73 extern int iUseInterpolation;
74 extern int iXAPitch;
75 extern int iSPUIRQWait;
76 extern int iUseTimer;
77
78
79 static int min(int x, int y) { return x < y ? x : y; }
80 static int max(int x, int y) { return x > y ? x : y; }
81
82 void emu_make_path(char *buff, const char *end, int size)
83 {
84         int pos, end_len;
85
86         end_len = strlen(end);
87         pos = plat_get_root_dir(buff, size);
88         strncpy(buff + pos, end, size - pos);
89         buff[size - 1] = 0;
90         if (pos + end_len > size - 1)
91                 printf("Warning: path truncated: %s\n", buff);
92 }
93
94 static int emu_check_save_file(int slot)
95 {
96         char *fname;
97         int ret;
98
99         fname = get_state_filename(slot);
100         if (fname == NULL)
101                 return 0;
102
103         ret = CheckState(fname);
104         free(fname);
105         return ret == 0 ? 1 : 0;
106 }
107
108 static int emu_save_load_game(int load, int sram)
109 {
110         char *fname;
111         int ret;
112
113         fname = get_state_filename(state_slot);
114         if (fname == NULL)
115                 return 0;
116
117         if (load)
118                 ret = LoadState(fname);
119         else
120                 ret = SaveState(fname);
121         free(fname);
122
123         return ret;
124 }
125
126 static void draw_savestate_bg(int slot)
127 {
128 }
129
130 static void menu_set_defconfig(void)
131 {
132         scaling = SCALE_4_3;
133
134         Config.Xa = Config.Cdda = Config.Sio =
135         Config.SpuIrq = Config.RCntFix = Config.VSyncWA = 0;
136
137         iUseDither = UseFrameSkip = 0;
138         dwActFixes = 1<<7;
139
140         iUseReverb = 2;
141         iUseInterpolation = 1;
142         iXAPitch = iSPUIRQWait = 0;
143         iUseTimer = 2;
144 }
145
146 #define CE_CONFIG_STR(val) \
147         { #val, 0, Config.val }
148
149 #define CE_CONFIG_VAL(val) \
150         { #val, sizeof(Config.val), &Config.val }
151
152 #define CE_STR(val) \
153         { #val, 0, val }
154
155 #define CE_INTVAL(val) \
156         { #val, sizeof(val), &val }
157
158 static const struct {
159         const char *name;
160         size_t len;
161         void *val;
162 } config_data[] = {
163         CE_CONFIG_STR(Bios),
164         CE_CONFIG_STR(Gpu),
165         CE_CONFIG_STR(Spu),
166         CE_CONFIG_STR(Cdr),
167         CE_CONFIG_VAL(Xa),
168         CE_CONFIG_VAL(Sio),
169         CE_CONFIG_VAL(Mdec),
170         CE_CONFIG_VAL(PsxAuto),
171         CE_CONFIG_VAL(Cdda),
172         CE_CONFIG_VAL(Debug),
173         CE_CONFIG_VAL(PsxOut),
174         CE_CONFIG_VAL(SpuIrq),
175         CE_CONFIG_VAL(RCntFix),
176         CE_CONFIG_VAL(VSyncWA),
177         CE_CONFIG_VAL(Cpu),
178         CE_CONFIG_VAL(PsxType),
179         CE_INTVAL(scaling),
180         CE_INTVAL(filter),
181         CE_INTVAL(state_slot),
182         CE_INTVAL(cpu_clock),
183         CE_INTVAL(g_opts),
184         CE_INTVAL(iUseDither),
185         CE_INTVAL(UseFrameSkip),
186         CE_INTVAL(dwActFixes),
187         CE_INTVAL(iUseReverb),
188         CE_INTVAL(iUseInterpolation),
189         CE_INTVAL(iXAPitch),
190         CE_INTVAL(iSPUIRQWait),
191         CE_INTVAL(iUseTimer),
192 };
193
194 static int menu_write_config(int is_game)
195 {
196         char cfgfile[MAXPATHLEN];
197         FILE *f;
198         int i;
199
200         if (is_game)
201                 return -1;
202
203         snprintf(cfgfile, sizeof(cfgfile), "." PCSX_DOT_DIR "%s", cfgfile_basename);
204         f = fopen(cfgfile, "w");
205         if (f == NULL) {
206                 printf("failed to open: %s\n", cfgfile);
207                 return -1;
208         }
209
210         for (i = 0; i < ARRAY_SIZE(config_data); i++) {
211                 fprintf(f, "%s = ", config_data[i].name);
212                 switch (config_data[i].len) {
213                 case 0:
214                         fprintf(f, "%s\n", (char *)config_data[i].val);
215                         break;
216                 case 1:
217                         fprintf(f, "%x\n", *(u8 *)config_data[i].val);
218                         break;
219                 case 2:
220                         fprintf(f, "%x\n", *(u16 *)config_data[i].val);
221                         break;
222                 case 4:
223                         fprintf(f, "%x\n", *(u32 *)config_data[i].val);
224                         break;
225                 default:
226                         printf("menu_write_config: unhandled len %d for %s\n",
227                                  config_data[i].len, config_data[i].name);
228                         break;
229                 }
230         }
231
232         if (!is_game)
233                 fprintf(f, "lastcdimg = %s\n", last_selected_fname);
234
235         fclose(f);
236         return 0;
237 }
238
239 static void parse_str_val(char *cval, const char *src)
240 {
241         char *tmp;
242         strncpy(cval, src, MAXPATHLEN);
243         cval[MAXPATHLEN - 1] = 0;
244         tmp = strchr(cval, '\n');
245         if (tmp == NULL)
246                 tmp = strchr(cval, '\r');
247         if (tmp != NULL)
248                 *tmp = 0;
249 }
250
251 static int menu_load_config(int is_game)
252 {
253         char cfgfile[MAXPATHLEN];
254         int i, ret = -1;
255         long size;
256         char *cfg;
257         FILE *f;
258
259         if (is_game)
260                 return -1;
261
262         snprintf(cfgfile, sizeof(cfgfile), "." PCSX_DOT_DIR "%s", cfgfile_basename);
263         f = fopen(cfgfile, "r");
264         if (f == NULL) {
265                 printf("failed to open: %s\n", cfgfile);
266                 return -1;
267         }
268
269         fseek(f, 0, SEEK_END);
270         size = ftell(f);
271         if (size <= 0) {
272                 printf("bad size %ld: %s\n", size, cfgfile);
273                 goto fail;
274         }
275
276         cfg = malloc(size + 1);
277         if (cfg == NULL)
278                 goto fail;
279
280         fseek(f, 0, SEEK_SET);
281         if (fread(cfg, 1, size, f) != size) {
282                 printf("failed to read: %s\n", cfgfile);
283                 goto fail_read;
284         }
285         cfg[size] = 0;
286
287         for (i = 0; i < ARRAY_SIZE(config_data); i++) {
288                 char *tmp, *tmp2;
289                 u32 val;
290
291                 tmp = strstr(cfg, config_data[i].name);
292                 if (tmp == NULL)
293                         continue;
294                 tmp += strlen(config_data[i].name);
295                 if (strncmp(tmp, " = ", 3) != 0)
296                         continue;
297                 tmp += 3;
298
299                 if (config_data[i].len == 0) {
300                         parse_str_val(config_data[i].val, tmp);
301                         continue;
302                 }
303
304                 tmp2 = NULL;
305                 val = strtoul(tmp, &tmp2, 16);
306                 if (tmp2 == NULL || tmp == tmp2)
307                         continue; // parse failed
308
309                 switch (config_data[i].len) {
310                 case 1:
311                         *(u8 *)config_data[i].val = val;
312                         break;
313                 case 2:
314                         *(u16 *)config_data[i].val = val;
315                         break;
316                 case 4:
317                         *(u32 *)config_data[i].val = val;
318                         break;
319                 default:
320                         printf("menu_load_config: unhandled len %d for %s\n",
321                                  config_data[i].len, config_data[i].name);
322                         break;
323                 }
324         }
325
326         if (!is_game) {
327                 char *tmp = strstr(cfg, "lastcdimg = ");
328                 if (tmp != NULL) {
329                         tmp += 12;
330                         parse_str_val(last_selected_fname, tmp);
331                 }
332         }
333
334 fail_read:
335         free(cfg);
336 fail:
337         fclose(f);
338         return ret;
339 }
340
341 // rrrr rggg gggb bbbb
342 static unsigned short fname2color(const char *fname)
343 {
344         static const char *cdimg_exts[] = { ".bin", ".img", ".iso", ".z", ".cue" };
345         static const char *other_exts[] = { ".ccd", ".toc", ".mds", ".sub", ".table" };
346         const char *ext = strrchr(fname, '.');
347         int i;
348
349         if (ext == NULL)
350                 return 0xffff;
351         for (i = 0; i < array_size(cdimg_exts); i++)
352                 if (strcasecmp(ext, cdimg_exts[i]) == 0)
353                         return 0x7bff;
354         for (i = 0; i < array_size(other_exts); i++)
355                 if (strcasecmp(ext, other_exts[i]) == 0)
356                         return 0xa514;
357         return 0xffff;
358 }
359
360 #define MENU_ALIGN_LEFT
361 #define menu_init menu_init_common
362 #include "common/menu.c"
363 #undef menu_init
364
365 // ---------- pandora specific -----------
366
367 static const char pnd_script_base[] = "sudo -n /usr/pandora/scripts";
368 static char **pnd_filter_list;
369
370 static int get_cpu_clock(void)
371 {
372         FILE *f;
373         int ret = 0;
374         f = fopen("/proc/pandora/cpu_mhz_max", "r");
375         if (f) {
376                 fscanf(f, "%d", &ret);
377                 fclose(f);
378         }
379         return ret;
380 }
381
382 static void apply_cpu_clock(void)
383 {
384         char buf[128];
385
386         if (cpu_clock != 0 && cpu_clock != get_cpu_clock()) {
387                 snprintf(buf, sizeof(buf), "unset DISPLAY; echo y | %s/op_cpuspeed.sh %d",
388                          pnd_script_base, cpu_clock);
389                 system(buf);
390         }
391 }
392
393 static void apply_filter(int which)
394 {
395         static int old = -1;
396         char buf[128];
397         int i;
398
399         if (pnd_filter_list == NULL || which == old)
400                 return;
401
402         for (i = 0; i < which; i++)
403                 if (pnd_filter_list[i] == NULL)
404                         return;
405
406         if (pnd_filter_list[i] == NULL)
407                 return;
408
409         snprintf(buf, sizeof(buf), "%s/op_videofir.sh %s", pnd_script_base, pnd_filter_list[i]);
410         system(buf);
411         old = which;
412 }
413
414 static menu_entry e_menu_gfx_options[];
415
416 static void pnd_menu_init(void)
417 {
418         struct dirent *ent;
419         int i, count = 0;
420         char **mfilters;
421         char buff[64], *p;
422         DIR *dir;
423
424         cpu_clock = get_cpu_clock();
425
426         dir = opendir("/etc/pandora/conf/dss_fir");
427         if (dir == NULL) {
428                 perror("filter opendir");
429                 return;
430         }
431
432         while (1) {
433                 errno = 0;
434                 ent = readdir(dir);
435                 if (ent == NULL) {
436                         if (errno != 0)
437                                 perror("readdir");
438                         break;
439                 }
440                 p = strstr(ent->d_name, "_up");
441                 if (p != NULL && (p[3] == 0 || !strcmp(p + 3, "_h")))
442                         count++;
443         }
444
445         if (count == 0)
446                 return;
447
448         mfilters = calloc(count + 1, sizeof(mfilters[0]));
449         if (mfilters == NULL)
450                 return;
451
452         rewinddir(dir);
453         for (i = 0; (ent = readdir(dir)); ) {
454                 size_t len;
455
456                 p = strstr(ent->d_name, "_up");
457                 if (p == NULL || (p[3] != 0 && strcmp(p + 3, "_h")))
458                         continue;
459
460                 len = p - ent->d_name;
461                 if (len > sizeof(buff) - 1)
462                         continue;
463
464                 strncpy(buff, ent->d_name, len);
465                 buff[len] = 0;
466                 mfilters[i] = strdup(buff);
467                 if (mfilters[i] != NULL)
468                         i++;
469         }
470         closedir(dir);
471
472         i = me_id2offset(e_menu_gfx_options, MA_OPT_FILTERING);
473         e_menu_gfx_options[i].data = (void *)mfilters;
474         pnd_filter_list = mfilters;
475 }
476
477 // -------------- key config --------------
478
479 me_bind_action me_ctrl_actions[] =
480 {
481         { "UP      ", 1 << DKEY_UP},
482         { "DOWN    ", 1 << DKEY_DOWN },
483         { "LEFT    ", 1 << DKEY_LEFT },
484         { "RIGHT   ", 1 << DKEY_RIGHT },
485         { "TRIANGLE", 1 << DKEY_TRIANGLE },
486         { "CIRCLE  ", 1 << DKEY_CIRCLE },
487         { "CROSS   ", 1 << DKEY_CROSS },
488         { "SQUARE  ", 1 << DKEY_SQUARE },
489         { "L1      ", 1 << DKEY_L1 },
490         { "R1      ", 1 << DKEY_R1 },
491         { "L2      ", 1 << DKEY_L2 },
492         { "R2      ", 1 << DKEY_R2 },
493         { "START   ", 1 << DKEY_START },
494         { "SELECT  ", 1 << DKEY_SELECT },
495         { NULL,       0 }
496 };
497
498 me_bind_action emuctrl_actions[] =
499 {
500 /*
501         { "Load State       ", PEV_STATE_LOAD },
502         { "Save State       ", PEV_STATE_SAVE },
503         { "Prev Save Slot   ", PEV_SSLOT_PREV },
504         { "Next Save Slot   ", PEV_SSLOT_NEXT },
505 */
506         { "Enter Menu       ", PEV_MENU },
507         { NULL,                0 }
508 };
509
510 static int key_config_loop_wrap(int id, int keys)
511 {
512         switch (id) {
513                 case MA_CTRL_PLAYER1:
514                         key_config_loop(me_ctrl_actions, array_size(me_ctrl_actions) - 1, 0);
515                         break;
516                 case MA_CTRL_PLAYER2:
517                         key_config_loop(me_ctrl_actions, array_size(me_ctrl_actions) - 1, 1);
518                         break;
519                 case MA_CTRL_EMU:
520                         key_config_loop(emuctrl_actions, array_size(emuctrl_actions) - 1, -1);
521                         break;
522                 default:
523                         break;
524         }
525         return 0;
526 }
527
528 static const char *mgn_dev_name(int id, int *offs)
529 {
530         const char *name = NULL;
531         static int it = 0;
532
533         if (id == MA_CTRL_DEV_FIRST)
534                 it = 0;
535
536         for (; it < IN_MAX_DEVS; it++) {
537                 name = in_get_dev_name(it, 1, 1);
538                 if (name != NULL)
539                         break;
540         }
541
542         it++;
543         return name;
544 }
545
546 static const char *mgn_saveloadcfg(int id, int *offs)
547 {
548         return "";
549 }
550
551 static int mh_saveloadcfg(int id, int keys)
552 {
553         switch (id) {
554         case MA_OPT_SAVECFG:
555         case MA_OPT_SAVECFG_GAME:
556                 if (menu_write_config(id == MA_OPT_SAVECFG_GAME ? 1 : 0) == 0)
557                         me_update_msg("config saved");
558                 else
559                         me_update_msg("failed to write config");
560                 break;
561         default:
562                 return 0;
563         }
564
565         return 1;
566 }
567
568 static menu_entry e_menu_keyconfig[] =
569 {
570         mee_handler_id("Player 1",          MA_CTRL_PLAYER1,    key_config_loop_wrap),
571         mee_handler_id("Player 2",          MA_CTRL_PLAYER2,    key_config_loop_wrap),
572         mee_handler_id("Emulator controls", MA_CTRL_EMU,        key_config_loop_wrap),
573         mee_cust_nosave("Save global config",       MA_OPT_SAVECFG, mh_saveloadcfg, mgn_saveloadcfg),
574 //      mee_cust_nosave("Save cfg for loaded game", MA_OPT_SAVECFG_GAME, mh_saveloadcfg, mgn_saveloadcfg),
575         mee_label     (""),
576         mee_label     ("Input devices:"),
577         mee_label_mk  (MA_CTRL_DEV_FIRST, mgn_dev_name),
578         mee_label_mk  (MA_CTRL_DEV_NEXT,  mgn_dev_name),
579         mee_label_mk  (MA_CTRL_DEV_NEXT,  mgn_dev_name),
580         mee_label_mk  (MA_CTRL_DEV_NEXT,  mgn_dev_name),
581         mee_label_mk  (MA_CTRL_DEV_NEXT,  mgn_dev_name),
582         mee_label_mk  (MA_CTRL_DEV_NEXT,  mgn_dev_name),
583         mee_label_mk  (MA_CTRL_DEV_NEXT,  mgn_dev_name),
584         mee_end,
585 };
586
587 static int menu_loop_keyconfig(int id, int keys)
588 {
589         static int sel = 0;
590
591 //      me_enable(e_menu_keyconfig, MA_OPT_SAVECFG_GAME, ready_to_go);
592         me_loop(e_menu_keyconfig, &sel, NULL);
593         return 0;
594 }
595
596 // ------------ gfx options menu ------------
597
598 static const char *men_scaler[] = { "1x1", "scaled 4:3", "fullscreen", "custom", NULL };
599 static const char h_cscaler[]   = "Displays the scaler layer, you can resize it\n"
600                                   "using d-pad or move it using R+d-pad";
601 static const char *men_dummy[] = { NULL };
602
603 static int menu_loop_cscaler(int id, int keys)
604 {
605         unsigned int inp;
606
607         scaling = SCALE_CUSTOM;
608
609         omap_enable_layer(1);
610         //pnd_restore_layer_data();
611
612         for (;;)
613         {
614                 menu_draw_begin(0);
615                 memset(g_menuscreen_ptr, 0, g_menuscreen_w * g_menuscreen_h * 2);
616                 text_out16(2, 480 - 18, "%dx%d | d-pad to resize, R+d-pad to move", g_layer_w, g_layer_h);
617                 menu_draw_end();
618
619                 inp = in_menu_wait(PBTN_UP|PBTN_DOWN|PBTN_LEFT|PBTN_RIGHT|PBTN_R|PBTN_MOK|PBTN_MBACK, 40);
620                 if (inp & PBTN_UP)    g_layer_y--;
621                 if (inp & PBTN_DOWN)  g_layer_y++;
622                 if (inp & PBTN_LEFT)  g_layer_x--;
623                 if (inp & PBTN_RIGHT) g_layer_x++;
624                 if (!(inp & PBTN_R)) {
625                         if (inp & PBTN_UP)    g_layer_h += 2;
626                         if (inp & PBTN_DOWN)  g_layer_h -= 2;
627                         if (inp & PBTN_LEFT)  g_layer_w += 2;
628                         if (inp & PBTN_RIGHT) g_layer_w -= 2;
629                 }
630                 if (inp & (PBTN_MOK|PBTN_MBACK))
631                         break;
632
633                 if (inp & (PBTN_UP|PBTN_DOWN|PBTN_LEFT|PBTN_RIGHT)) {
634                         if (g_layer_x < 0)   g_layer_x = 0;
635                         if (g_layer_x > 640) g_layer_x = 640;
636                         if (g_layer_y < 0)   g_layer_y = 0;
637                         if (g_layer_y > 420) g_layer_y = 420;
638                         if (g_layer_w < 160) g_layer_w = 160;
639                         if (g_layer_h < 60)  g_layer_h = 60;
640                         if (g_layer_x + g_layer_w > 800)
641                                 g_layer_w = 800 - g_layer_x;
642                         if (g_layer_y + g_layer_h > 480)
643                                 g_layer_h = 480 - g_layer_y;
644                         omap_enable_layer(1);
645                 }
646         }
647
648         omap_enable_layer(0);
649
650         return 0;
651 }
652
653 static menu_entry e_menu_gfx_options[] =
654 {
655         mee_enum      ("Scaler",                   0, scaling, men_scaler),
656         mee_enum      ("Filter",                   MA_OPT_FILTERING, filter, men_dummy),
657 //      mee_onoff     ("Vsync",                    0, vsync, 1),
658         mee_cust_h    ("Setup custom scaler",      0, menu_loop_cscaler, NULL, h_cscaler),
659         mee_end,
660 };
661
662 static int menu_loop_gfx_options(int id, int keys)
663 {
664         static int sel = 0;
665
666         me_loop(e_menu_gfx_options, &sel, NULL);
667
668         return 0;
669 }
670
671 // ------------ bios/plugins ------------
672
673 static const char *men_gpu_dithering[] = { "None", "Game dependant", "Always", NULL };
674 static const char h_gpu[]              = "Configure built-in P.E.Op.S. SoftGL Driver V1.17\n"
675                                          "Coded by Pete Bernert and the P.E.Op.S. team";
676 static const char h_gpu_0[]            = "Needed for Chrono Cross";
677 static const char h_gpu_1[]            = "Capcom fighting games";
678 static const char h_gpu_2[]            = "Black screens in Lunar";
679 static const char h_gpu_3[]            = "Compatibility mode";
680 static const char h_gpu_6[]            = "Pandemonium 2";
681 static const char h_gpu_7[]            = "Skip every second frame";
682 static const char h_gpu_8[]            = "Needed by Dark Forces";
683 static const char h_gpu_9[]            = "better g-colors, worse textures";
684 static const char h_gpu_10[]           = "Toggle busy flags after drawing";
685
686 static menu_entry e_menu_plugin_gpu[] =
687 {
688         mee_enum      ("Dithering",                  0, iUseDither, men_gpu_dithering),
689         mee_onoff_h   ("Odd/even bit hack",          0, dwActFixes, 1<<0, h_gpu_0),
690         mee_onoff_h   ("Expand screen width",        0, dwActFixes, 1<<1, h_gpu_1),
691         mee_onoff_h   ("Ignore brightness color",    0, dwActFixes, 1<<2, h_gpu_2),
692         mee_onoff_h   ("Disable coordinate check",   0, dwActFixes, 1<<3, h_gpu_3),
693         mee_onoff_h   ("Lazy screen update",         0, dwActFixes, 1<<6, h_gpu_6),
694         mee_onoff_h   ("Old frame skipping",         0, dwActFixes, 1<<7, h_gpu_7),
695         mee_onoff_h   ("Repeated flat tex triangles ",0,dwActFixes, 1<<8, h_gpu_8),
696         mee_onoff_h   ("Draw quads with triangles",  0, dwActFixes, 1<<9, h_gpu_9),
697         mee_onoff_h   ("Fake 'gpu busy' states",     0, dwActFixes, 1<<10, h_gpu_10),
698         mee_end,
699 };
700
701 static int menu_loop_plugin_gpu(int id, int keys)
702 {
703         static int sel = 0;
704         me_loop(e_menu_plugin_gpu, &sel, NULL);
705         return 0;
706 }
707
708 static const char *men_spu_reverb[] = { "Off", "Fake", "On", NULL };
709 static const char *men_spu_interp[] = { "None", "Simple", "Gaussian", "Cubic", NULL };
710 static const char h_spu[]           = "Configure built-in P.E.Op.S. Sound Driver V1.7\n"
711                                       "Coded by Pete Bernert and the P.E.Op.S. team";
712 static const char h_spu_irq_wait[]  = "Wait for CPU; only useful for some games, may cause glitches";
713 static const char h_spu_thread[]    = "Run sound emulation in separate thread";
714
715 static menu_entry e_menu_plugin_spu[] =
716 {
717         mee_enum      ("Reverb",                    0, iUseReverb, men_spu_reverb),
718         mee_enum      ("Interpolation",             0, iUseInterpolation, men_spu_interp),
719         mee_onoff     ("Adjust XA pitch",           0, iXAPitch, 1),
720         mee_onoff_h   ("SPU IRQ Wait",              0, iSPUIRQWait, 1, h_spu_irq_wait),
721         mee_onoff_h   ("Use sound thread",          0, iUseTimer, 1, h_spu_thread),
722         mee_end,
723 };
724
725 static int menu_loop_plugin_spu(int id, int keys)
726 {
727         static int sel = 0;
728         me_loop(e_menu_plugin_spu, &sel, NULL);
729         return 0;
730 }
731
732 static menu_entry e_menu_plugin_options[] =
733 {
734         mee_handler_h ("Configure built-in GPU plugin", menu_loop_plugin_gpu, h_gpu),
735         mee_handler_h ("Configure built-in SPU plugin", menu_loop_plugin_spu, h_spu),
736         mee_end,
737 };
738
739 static int menu_loop_plugin_options(int id, int keys)
740 {
741         static int sel = 0;
742         me_loop(e_menu_plugin_options, &sel, NULL);
743         return 0;
744 }
745
746 // ------------ adv options menu ------------
747
748 static const char h_cfg_xa[]     = "Disables XA sound, which can sometimes improve performance";
749 static const char h_cfg_cdda[]   = "Disable CD Audio for a performance boost\n"
750                                    "(proper .cue/.bin dump is needed otherwise)";
751 static const char h_cfg_sio[]    = "This should be enabled for certain memcards/gamepads";
752 static const char h_cfg_spuirq[] = "Compatibility tweak; should probably be left off";
753 static const char h_cfg_rcnt1[]  = "Parasite Eve 2, Vandal Hearts 1/2 Fix";
754 static const char h_cfg_rcnt2[]  = "InuYasha Sengoku Battle Fix";
755
756 static menu_entry e_menu_adv_options[] =
757 {
758         mee_onoff     ("Show CPU load",          0, g_opts, OPT_SHOWCPU),
759         mee_onoff_h   ("Disable XA Decoding",    0, Config.Xa, 1, h_cfg_xa),
760         mee_onoff_h   ("Disable CD Audio",       0, Config.Cdda, 1, h_cfg_cdda),
761         mee_onoff_h   ("SIO IRQ Always Enabled", 0, Config.Sio, 1, h_cfg_sio),
762         mee_onoff_h   ("SPU IRQ Always Enabled", 0, Config.SpuIrq, 1, h_cfg_spuirq),
763         mee_onoff_h   ("Rootcounter hack",       0, Config.RCntFix, 1, h_cfg_rcnt1),
764         mee_onoff_h   ("Rootcounter hack 2",     0, Config.VSyncWA, 1, h_cfg_rcnt2),
765         mee_end,
766 };
767
768 static int menu_loop_adv_options(int id, int keys)
769 {
770         static int sel = 0;
771         me_loop(e_menu_adv_options, &sel, NULL);
772         return 0;
773 }
774
775 // ------------ options menu ------------
776
777 static int mh_restore_defaults(int id, int keys)
778 {
779         menu_set_defconfig();
780         me_update_msg("defaults restored");
781         return 1;
782 }
783
784 static const char *men_region[]       = { "NTSC", "PAL", NULL };
785 /*
786 static const char *men_confirm_save[] = { "OFF", "writes", "loads", "both", NULL };
787 static const char h_confirm_save[]    = "Ask for confirmation when overwriting save,\n"
788                                         "loading state or both";
789 */
790 static const char h_restore_def[]     = "Switches back to default / recommended\n"
791                                         "configuration";
792
793 static menu_entry e_menu_options[] =
794 {
795 //      mee_range     ("Save slot",                0, state_slot, 0, 9),
796 //      mee_enum_h    ("Confirm savestate",        0, dummy, men_confirm_save, h_confirm_save),
797         mee_onoff     ("Frameskip",                0, UseFrameSkip, 1),
798         mee_onoff     ("Show FPS",                 0, g_opts, OPT_SHOWFPS),
799         mee_enum      ("Region",                   0, Config.PsxType, men_region),
800         mee_range     ("CPU clock",                MA_OPT_CPU_CLOCKS, cpu_clock, 20, 5000),
801         mee_handler   ("[Display]",                menu_loop_gfx_options),
802         mee_handler   ("[BIOS/Plugins]",           menu_loop_plugin_options),
803         mee_handler   ("[Advanced]",               menu_loop_adv_options),
804         mee_cust_nosave("Save global config",      MA_OPT_SAVECFG,      mh_saveloadcfg, mgn_saveloadcfg),
805 //      mee_cust_nosave("Save cfg for loaded game",MA_OPT_SAVECFG_GAME, mh_saveloadcfg, mgn_saveloadcfg),
806         mee_handler_h ("Restore default config",   mh_restore_defaults, h_restore_def),
807         mee_end,
808 };
809
810 static int menu_loop_options(int id, int keys)
811 {
812         static int sel = 0;
813         int i;
814
815         i = me_id2offset(e_menu_options, MA_OPT_CPU_CLOCKS);
816         e_menu_options[i].enabled = cpu_clock != 0 ? 1 : 0;
817 //      me_enable(e_menu_options, MA_OPT_SAVECFG_GAME, ready_to_go);
818
819         me_loop(e_menu_options, &sel, NULL);
820
821         return 0;
822 }
823
824 // ------------ debug menu ------------
825
826 static void draw_frame_debug(void)
827 {
828         smalltext_out16(4, 1, "build: "__DATE__ " " __TIME__ " " REV, 0xe7fc);
829 }
830
831 static void debug_menu_loop(void)
832 {
833         int inp;
834
835         while (1)
836         {
837                 menu_draw_begin(1);
838                 draw_frame_debug();
839                 menu_draw_end();
840
841                 inp = in_menu_wait(PBTN_MOK|PBTN_MBACK|PBTN_MA2|PBTN_MA3|PBTN_L|PBTN_R |
842                                         PBTN_UP|PBTN_DOWN|PBTN_LEFT|PBTN_RIGHT, 70);
843                 if (inp & PBTN_MBACK)
844                         return;
845         }
846 }
847
848 // ------------ main menu ------------
849
850 void OnFile_Exit();
851
852 const char *plat_get_credits(void)
853 {
854         return  "PCSX-ReARMed\n\n"
855                 "(C) 1999-2003 PCSX Team\n"
856                 "(C) 2005-2009 PCSX-df Team\n"
857                 "(C) 2009-2010 PCSX-Reloaded Team\n\n"
858                 "GPU and SPU code by Pete Bernert\n"
859                 "  and the P.E.Op.S. team\n"
860                 "ARM recompiler (C) 2009-2010 Ari64\n\n"
861                 "integration, optimization and\n"
862                 "  frontend (C) 2010 notaz\n";
863 }
864
865 static char *romsel_run(void)
866 {
867         extern void set_cd_image(const char *fname);
868         char *ret;
869
870         ret = menu_loop_romsel(last_selected_fname, sizeof(last_selected_fname));
871         if (ret == NULL)
872                 return NULL;
873
874         lprintf("selected file: %s\n", ret);
875         ready_to_go = 0;
876
877         set_cd_image(ret);
878         LoadPlugins();
879         NetOpened = 0;
880         if (OpenPlugins() == -1) {
881                 me_update_msg("failed to open plugins");
882                 return NULL;
883         }
884
885         SysReset();
886
887         if (CheckCdrom() == -1) {
888                 // Only check the CD if we are starting the console with a CD
889                 ClosePlugins();
890                 me_update_msg("unsupported/invalid CD image");
891                 return NULL;
892         }
893
894         // Read main executable directly from CDRom and start it
895         if (LoadCdrom() == -1) {
896                 ClosePlugins();
897                 me_update_msg("failed to load CD image");
898                 return NULL;
899         }
900
901         strcpy(last_selected_fname, rom_fname_reload);
902         game_config_loaded = 0;
903         ready_to_go = 1;
904         return ret;
905 }
906
907 static int main_menu_handler(int id, int keys)
908 {
909         char *ret_name;
910
911         switch (id)
912         {
913         case MA_MAIN_RESUME_GAME:
914                 if (ready_to_go)
915                         return 1;
916                 break;
917         case MA_MAIN_SAVE_STATE:
918                 if (ready_to_go)
919                         return menu_loop_savestate(0);
920                 break;
921         case MA_MAIN_LOAD_STATE:
922                 if (ready_to_go)
923                         return menu_loop_savestate(1);
924                 break;
925         case MA_MAIN_RESET_GAME:
926                 if (ready_to_go) {
927                         OpenPlugins();
928                         SysReset();
929                         if (CheckCdrom() != -1) {
930                                 LoadCdrom();
931                         }
932                         return 1;
933                 }
934                 break;
935         case MA_MAIN_LOAD_ROM:
936                 ret_name = romsel_run();
937                 if (ret_name != NULL)
938                         return 1;
939                 break;
940         case MA_MAIN_CREDITS:
941                 draw_menu_credits(draw_frame_debug);
942                 in_menu_wait(PBTN_MOK|PBTN_MBACK, 70);
943                 break;
944         case MA_MAIN_EXIT:
945                 OnFile_Exit();
946                 break;
947         default:
948                 lprintf("%s: something unknown selected\n", __FUNCTION__);
949                 break;
950         }
951
952         return 0;
953 }
954
955 static menu_entry e_menu_main[] =
956 {
957         mee_label     (""),
958         mee_label     (""),
959         mee_handler_id("Resume game",        MA_MAIN_RESUME_GAME, main_menu_handler),
960         mee_handler_id("Save State",         MA_MAIN_SAVE_STATE,  main_menu_handler),
961         mee_handler_id("Load State",         MA_MAIN_LOAD_STATE,  main_menu_handler),
962         mee_handler_id("Reset game",         MA_MAIN_RESET_GAME,  main_menu_handler),
963         mee_handler_id("Load CD image",      MA_MAIN_LOAD_ROM,    main_menu_handler),
964         mee_handler   ("Options",            menu_loop_options),
965         mee_handler   ("Controls",           menu_loop_keyconfig),
966         mee_handler_id("Credits",            MA_MAIN_CREDITS,     main_menu_handler),
967         mee_handler_id("Exit",               MA_MAIN_EXIT,        main_menu_handler),
968         mee_end,
969 };
970
971 // ----------------------------
972
973 static void menu_leave_emu(void);
974
975 void menu_loop(void)
976 {
977         static int sel = 0;
978
979         menu_leave_emu();
980
981         me_enable(e_menu_main, MA_MAIN_RESUME_GAME, ready_to_go);
982         me_enable(e_menu_main, MA_MAIN_SAVE_STATE,  ready_to_go);
983         me_enable(e_menu_main, MA_MAIN_LOAD_STATE,  ready_to_go);
984         me_enable(e_menu_main, MA_MAIN_RESET_GAME,  ready_to_go);
985
986 //      menu_enter(ready_to_go);
987         in_set_config_int(0, IN_CFG_BLOCKING, 1);
988
989         do {
990                 me_loop(e_menu_main, &sel, NULL);
991         } while (!ready_to_go);
992
993         /* wait until menu, ok, back is released */
994         while (in_menu_wait_any(50) & (PBTN_MENU|PBTN_MOK|PBTN_MBACK))
995                 ;
996
997         in_set_config_int(0, IN_CFG_BLOCKING, 0);
998
999         memset(g_menuscreen_ptr, 0, g_menuscreen_w * g_menuscreen_h * 2);
1000         menu_draw_end();
1001         menu_prepare_emu();
1002 }
1003
1004 void menu_init(void)
1005 {
1006         char buff[MAXPATHLEN];
1007
1008         strcpy(last_selected_fname, "/media");
1009
1010         pnd_menu_init();
1011         menu_init_common();
1012
1013         menu_set_defconfig();
1014         menu_load_config(0);
1015         last_psx_w = 320;
1016         last_psx_h = 240;
1017         last_psx_bpp = 16;
1018
1019         g_menubg_src_ptr = calloc(g_menuscreen_w * g_menuscreen_h * 2, 1);
1020         if (g_menubg_src_ptr == NULL)
1021                 exit(1);
1022         emu_make_path(buff, "skin/background.png", sizeof(buff));
1023         readpng(g_menubg_src_ptr, buff, READPNG_BG, g_menuscreen_w, g_menuscreen_h);
1024 }
1025
1026 void menu_notify_mode_change(int w, int h, int bpp)
1027 {
1028         last_psx_w = w;
1029         last_psx_h = h;
1030         last_psx_bpp = bpp;
1031
1032         if (scaling == SCALE_1_1) {
1033                 g_layer_x = 800/2 - w/2;  g_layer_y = 480/2 - h/2;
1034                 g_layer_w = w; g_layer_h = h;
1035                 omap_enable_layer(1);
1036         }
1037 }
1038
1039 static void menu_leave_emu(void)
1040 {
1041         omap_enable_layer(0);
1042
1043         memcpy(g_menubg_ptr, g_menubg_src_ptr, g_menuscreen_w * g_menuscreen_h * 2);
1044         if (ready_to_go && last_psx_bpp == 16) {
1045                 int x = max(0, g_menuscreen_w - last_psx_w);
1046                 int y = max(0, g_menuscreen_h / 2 - last_psx_h / 2);
1047                 int w = min(g_menuscreen_w, last_psx_w);
1048                 int h = min(g_menuscreen_h, last_psx_h);
1049                 u16 *d = (u16 *)g_menubg_ptr + g_menuscreen_w * y + x;
1050                 u16 *s = pl_fbdev_buf;
1051
1052                 for (; h > 0; h--, d += g_menuscreen_w, s += last_psx_w)
1053                         menu_darken_bg(d, s, w, 0);
1054         }
1055 }
1056
1057 void menu_prepare_emu(void)
1058 {
1059         if (!game_config_loaded) {
1060                 menu_set_defconfig();
1061                 menu_load_config(1);
1062                 game_config_loaded = 1;
1063         }
1064
1065         switch (scaling) {
1066         case SCALE_1_1:
1067                 menu_notify_mode_change(last_psx_w, last_psx_h, last_psx_bpp);
1068                 break;
1069         case SCALE_4_3:
1070                 g_layer_x = 80;  g_layer_y = 0;
1071                 g_layer_w = 640; g_layer_h = 480;
1072                 break;
1073         case SCALE_FULLSCREEN:
1074                 g_layer_x = 0;   g_layer_y = 0;
1075                 g_layer_w = 800; g_layer_h = 480;
1076                 break;
1077         case SCALE_CUSTOM:
1078                 break;
1079         }
1080         omap_enable_layer(1);
1081         apply_filter(filter);
1082         apply_cpu_clock();
1083         stop = 0;
1084
1085         // core doesn't care about Config.Cdda changes,
1086         // so handle them manually here
1087         if (Config.Cdda)
1088                 CDR_stop();
1089 }
1090
1091 void me_update_msg(const char *msg)
1092 {
1093         strncpy(menu_error_msg, msg, sizeof(menu_error_msg));
1094         menu_error_msg[sizeof(menu_error_msg) - 1] = 0;
1095
1096         menu_error_time = plat_get_ticks_ms();
1097         lprintf("msg: %s\n", menu_error_msg);
1098 }
1099