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