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