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