restore defaults function + some refactoring
[libpicofe.git] / common / menu.c
... / ...
CommitLineData
1// (c) Copyright 2006-2009 notaz, All rights reserved.\r
2// Free for non-commercial use.\r
3\r
4// For commercial use, separate licencing terms must be obtained.\r
5\r
6#include <stdio.h>\r
7#include <string.h>\r
8#include <stdlib.h>\r
9#include <stdarg.h>\r
10\r
11#include "menu.h"\r
12#include "fonts.h"\r
13#include "readpng.h"\r
14#include "lprintf.h"\r
15#include "input.h"\r
16#include "emu.h"\r
17#include "plat.h"\r
18#include "posix.h"\r
19#include <version.h>\r
20\r
21#include <pico/pico_int.h>\r
22#include <pico/patch.h>\r
23#include <zlib/zlib.h>\r
24\r
25#define array_size(x) (sizeof(x) / sizeof(x[0]))\r
26\r
27static char static_buff[64];\r
28static char menu_error_msg[64] = { 0, };\r
29static int menu_error_time = 0;\r
30\r
31#ifndef UIQ3\r
32\r
33static unsigned char *menu_font_data = NULL;\r
34static int menu_text_color = 0xffff; // default to white\r
35static int menu_sel_color = -1; // disabled\r
36\r
37/* note: these might become non-constant in future */\r
38#if MENU_X2\r
39static const int me_mfont_w = 16, me_mfont_h = 20;\r
40static const int me_sfont_w = 12, me_sfont_h = 20;\r
41#else\r
42static const int me_mfont_w = 8, me_mfont_h = 10;\r
43static const int me_sfont_w = 6, me_sfont_h = 10;\r
44#endif\r
45\r
46// draws text to current bbp16 screen\r
47static void text_out16_(int x, int y, const char *text, int color)\r
48{\r
49 int i, l, u, tr, tg, tb, len;\r
50 unsigned short *dest = (unsigned short *)g_screen_ptr + x + y * g_screen_width;\r
51 tr = (color & 0xf800) >> 8;\r
52 tg = (color & 0x07e0) >> 3;\r
53 tb = (color & 0x001f) << 3;\r
54\r
55 if (text == (void *)1)\r
56 {\r
57 // selector symbol\r
58 text = "";\r
59 len = 1;\r
60 }\r
61 else\r
62 {\r
63 const char *p;\r
64 for (p = text; *p != 0 && *p != '\n'; p++)\r
65 ;\r
66 len = p - text;\r
67 }\r
68\r
69 for (i = 0; i < len; i++)\r
70 {\r
71 unsigned char *src = menu_font_data + (unsigned int)text[i] * me_mfont_w * me_mfont_h / 2;\r
72 unsigned short *dst = dest;\r
73 for (l = 0; l < me_mfont_h; l++, dst += g_screen_width - me_mfont_w)\r
74 {\r
75 for (u = me_mfont_w / 2; u > 0; u--, src++)\r
76 {\r
77 int c, r, g, b;\r
78 c = *src >> 4;\r
79 r = (*dst & 0xf800) >> 8;\r
80 g = (*dst & 0x07e0) >> 3;\r
81 b = (*dst & 0x001f) << 3;\r
82 r = (c^0xf)*r/15 + c*tr/15;\r
83 g = (c^0xf)*g/15 + c*tg/15;\r
84 b = (c^0xf)*b/15 + c*tb/15;\r
85 *dst++ = ((r<<8)&0xf800) | ((g<<3)&0x07e0) | (b>>3);\r
86 c = *src & 0xf;\r
87 r = (*dst & 0xf800) >> 8;\r
88 g = (*dst & 0x07e0) >> 3;\r
89 b = (*dst & 0x001f) << 3;\r
90 r = (c^0xf)*r/15 + c*tr/15;\r
91 g = (c^0xf)*g/15 + c*tg/15;\r
92 b = (c^0xf)*b/15 + c*tb/15;\r
93 *dst++ = ((r<<8)&0xf800) | ((g<<3)&0x07e0) | (b>>3);\r
94 }\r
95 }\r
96 dest += me_mfont_w;\r
97 }\r
98}\r
99\r
100void text_out16(int x, int y, const char *texto, ...)\r
101{\r
102 va_list args;\r
103 char buffer[256];\r
104 int maxw = (g_screen_width - x) / me_mfont_w;\r
105\r
106 if (maxw < 0)\r
107 return;\r
108\r
109 va_start(args, texto);\r
110 vsnprintf(buffer, sizeof(buffer), texto, args);\r
111 va_end(args);\r
112\r
113 if (maxw > sizeof(buffer) - 1)\r
114 maxw = sizeof(buffer) - 1;\r
115 buffer[maxw] = 0;\r
116\r
117 text_out16_(x,y,buffer,menu_text_color);\r
118}\r
119\r
120/* draws in 6x8 font, might multiply size by integer */\r
121static void smalltext_out16_(int x, int y, const char *texto, int color)\r
122{\r
123 unsigned char *src;\r
124 unsigned short *dst;\r
125 int multiplier = me_sfont_w / 6;\r
126 int i;\r
127\r
128 for (i = 0;; i++, x += me_sfont_w)\r
129 {\r
130 unsigned char c = (unsigned char) texto[i];\r
131 int h = 8;\r
132\r
133 if (!c || c == '\n')\r
134 break;\r
135\r
136 src = fontdata6x8[c];\r
137 dst = (unsigned short *)g_screen_ptr + x + y * g_screen_width;\r
138\r
139 while (h--)\r
140 {\r
141 int m, w2, h2;\r
142 for (h2 = multiplier; h2 > 0; h2--)\r
143 {\r
144 for (m = 0x20; m; m >>= 1) {\r
145 if (*src & m)\r
146 for (w2 = multiplier; w2 > 0; w2--)\r
147 *dst++ = color;\r
148 else\r
149 dst += multiplier;\r
150 }\r
151\r
152 dst += g_screen_width - me_sfont_w;\r
153 }\r
154 src++;\r
155 }\r
156 }\r
157}\r
158\r
159static void smalltext_out16(int x, int y, const char *texto, int color)\r
160{\r
161 char buffer[128];\r
162 int maxw = (g_screen_width - x) / 6;\r
163\r
164 if (maxw < 0)\r
165 return;\r
166\r
167 strncpy(buffer, texto, sizeof(buffer));\r
168 if (maxw > sizeof(buffer) - 1)\r
169 maxw = sizeof(buffer) - 1;\r
170 buffer[maxw] = 0;\r
171\r
172 smalltext_out16_(x, y, buffer, color);\r
173}\r
174\r
175static void menu_draw_selection(int x, int y, int w)\r
176{\r
177 int i, h;\r
178 unsigned short *dst, *dest;\r
179\r
180 text_out16_(x, y, (void *)1, (menu_sel_color < 0) ? menu_text_color : menu_sel_color);\r
181\r
182 if (menu_sel_color < 0) return; // no selection hilight\r
183\r
184 if (y > 0) y--;\r
185 dest = (unsigned short *)g_screen_ptr + x + y * g_screen_width + 14;\r
186 for (h = me_mfont_h + 1; h > 0; h--)\r
187 {\r
188 dst = dest;\r
189 for (i = w - 14; i > 0; i--)\r
190 *dst++ = menu_sel_color;\r
191 dest += g_screen_width;\r
192 }\r
193}\r
194\r
195static int parse_hex_color(char *buff)\r
196{\r
197 char *endp = buff;\r
198 int t = (int) strtoul(buff, &endp, 16);\r
199 if (endp != buff)\r
200#ifdef PSP\r
201 return ((t<<8)&0xf800) | ((t>>5)&0x07e0) | ((t>>19)&0x1f);\r
202#else\r
203 return ((t>>8)&0xf800) | ((t>>5)&0x07e0) | ((t>>3)&0x1f);\r
204#endif\r
205 return -1;\r
206}\r
207\r
208void menu_init(void)\r
209{\r
210 int i, c, l;\r
211 unsigned char *fd, *fds;\r
212 char buff[256];\r
213 FILE *f;\r
214\r
215 if (menu_font_data != NULL)\r
216 free(menu_font_data);\r
217\r
218 menu_font_data = calloc((MENU_X2 ? 256 * 320 : 128 * 160) / 2, 1);\r
219 if (menu_font_data == NULL)\r
220 return;\r
221\r
222 // generate default 8x10 font from fontdata8x8\r
223 for (c = 0, fd = menu_font_data; c < 256; c++)\r
224 {\r
225 for (l = 0; l < 8; l++)\r
226 {\r
227 unsigned char fd8x8 = fontdata8x8[c*8+l];\r
228 if (fd8x8&0x80) *fd = 0xf0;\r
229 if (fd8x8&0x40) *fd |= 0x0f; fd++;\r
230 if (fd8x8&0x20) *fd = 0xf0;\r
231 if (fd8x8&0x10) *fd |= 0x0f; fd++;\r
232 if (fd8x8&0x08) *fd = 0xf0;\r
233 if (fd8x8&0x04) *fd |= 0x0f; fd++;\r
234 if (fd8x8&0x02) *fd = 0xf0;\r
235 if (fd8x8&0x01) *fd |= 0x0f; fd++;\r
236 }\r
237 fd += 8*2/2; // 2 empty lines\r
238 }\r
239\r
240 if (MENU_X2) {\r
241 // expand default font\r
242 fds = menu_font_data + 128 * 160 / 2 - 4;\r
243 fd = menu_font_data + 256 * 320 / 2 - 1;\r
244 for (c = 255; c >= 0; c--)\r
245 {\r
246 for (l = 9; l >= 0; l--, fds -= 4)\r
247 {\r
248 for (i = 3; i >= 0; i--) {\r
249 int px = fds[i] & 0x0f;\r
250 *fd-- = px | (px << 4);\r
251 px = (fds[i] >> 4) & 0x0f;\r
252 *fd-- = px | (px << 4);\r
253 }\r
254 for (i = 3; i >= 0; i--) {\r
255 int px = fds[i] & 0x0f;\r
256 *fd-- = px | (px << 4);\r
257 px = (fds[i] >> 4) & 0x0f;\r
258 *fd-- = px | (px << 4);\r
259 }\r
260 }\r
261 }\r
262 }\r
263\r
264 // load custom font and selector (stored as 1st symbol in font table)\r
265 emu_make_path(buff, "skin/font.png", sizeof(buff));\r
266 readpng(menu_font_data, buff, READPNG_FONT);\r
267 // default selector symbol is '>'\r
268 memcpy(menu_font_data, menu_font_data + ((int)'>') * me_mfont_w * me_mfont_h / 2,\r
269 me_mfont_w * me_mfont_h / 2);\r
270 emu_make_path(buff, "skin/selector.png", sizeof(buff));\r
271 readpng(menu_font_data, buff, READPNG_SELECTOR);\r
272\r
273 // load custom colors\r
274 emu_make_path(buff, "skin/skin.txt", sizeof(buff));\r
275 f = fopen(buff, "r");\r
276 if (f != NULL)\r
277 {\r
278 lprintf("found skin.txt\n");\r
279 while (!feof(f))\r
280 {\r
281 fgets(buff, sizeof(buff), f);\r
282 if (buff[0] == '#' || buff[0] == '/') continue; // comment\r
283 if (buff[0] == '\r' || buff[0] == '\n') continue; // empty line\r
284 if (strncmp(buff, "text_color=", 11) == 0)\r
285 {\r
286 int tmp = parse_hex_color(buff+11);\r
287 if (tmp >= 0) menu_text_color = tmp;\r
288 else lprintf("skin.txt: parse error for text_color\n");\r
289 }\r
290 else if (strncmp(buff, "selection_color=", 16) == 0)\r
291 {\r
292 int tmp = parse_hex_color(buff+16);\r
293 if (tmp >= 0) menu_sel_color = tmp;\r
294 else lprintf("skin.txt: parse error for selection_color\n");\r
295 }\r
296 else\r
297 lprintf("skin.txt: parse error: %s\n", buff);\r
298 }\r
299 fclose(f);\r
300 }\r
301}\r
302\r
303\r
304static int me_id2offset(const menu_entry *ent, menu_id id)\r
305{\r
306 int i;\r
307 for (i = 0; ent->name; ent++, i++)\r
308 if (ent->id == id) return i;\r
309\r
310 lprintf("%s: id %i not found\n", __FUNCTION__, id);\r
311 return 0;\r
312}\r
313\r
314static void me_enable(menu_entry *entries, menu_id id, int enable)\r
315{\r
316 int i = me_id2offset(entries, id);\r
317 entries[i].enabled = enable;\r
318}\r
319\r
320static int me_count(const menu_entry *ent)\r
321{\r
322 int ret;\r
323\r
324 for (ret = 0; ent->name; ent++, ret++)\r
325 ;\r
326\r
327 return ret;\r
328}\r
329\r
330static void me_draw(const menu_entry *entries, int sel)\r
331{\r
332 const menu_entry *ent;\r
333 int x, y, w = 0, h = 0;\r
334 int offs, col2_offs = 27 * me_mfont_w;\r
335 const char *name;\r
336 int asel = 0;\r
337 int i, n;\r
338\r
339 /* calculate size of menu rect */\r
340 for (ent = entries, i = n = 0; ent->name; ent++, i++)\r
341 {\r
342 int wt;\r
343\r
344 if (!ent->enabled)\r
345 continue;\r
346\r
347 if (i == sel)\r
348 asel = n;\r
349\r
350 name = NULL;\r
351 wt = strlen(ent->name) * me_mfont_w;\r
352 if (wt == 0 && ent->generate_name)\r
353 name = ent->generate_name(ent->id, &offs);\r
354 if (name != NULL)\r
355 wt = strlen(name) * me_mfont_w;\r
356\r
357 if (ent->beh != MB_NONE)\r
358 {\r
359 if (wt > col2_offs)\r
360 col2_offs = wt + me_mfont_w;\r
361 wt = col2_offs;\r
362\r
363 switch (ent->beh) {\r
364 case MB_NONE: break;\r
365 case MB_OPT_ONOFF:\r
366 case MB_OPT_RANGE: wt += me_mfont_w * 3; break;\r
367 case MB_OPT_CUSTOM:\r
368 case MB_OPT_CUSTONOFF:\r
369 case MB_OPT_CUSTRANGE:\r
370 name = NULL;\r
371 offs = 0;\r
372 if (ent->generate_name != NULL)\r
373 name = ent->generate_name(ent->id, &offs);\r
374 if (name != NULL)\r
375 wt += (strlen(name) + offs) * me_mfont_w;\r
376 break;\r
377 }\r
378 }\r
379\r
380 if (wt > w)\r
381 w = wt;\r
382 n++;\r
383 }\r
384 h = n * me_mfont_h;\r
385 w += me_mfont_w * 2; /* selector */\r
386\r
387 if (w > g_screen_width) {\r
388 lprintf("width %d > %d\n", w, g_screen_width);\r
389 w = g_screen_width;\r
390 }\r
391 if (h > g_screen_height) {\r
392 lprintf("height %d > %d\n", w, g_screen_height);\r
393 h = g_screen_height;\r
394 }\r
395\r
396 x = g_screen_width / 2 - w / 2;\r
397 y = g_screen_height / 2 - h / 2;\r
398\r
399 /* draw */\r
400 plat_video_menu_begin();\r
401 menu_draw_selection(x, y + asel * me_mfont_h, w);\r
402 x += me_mfont_w * 2;\r
403\r
404 for (ent = entries; ent->name; ent++)\r
405 {\r
406 if (!ent->enabled)\r
407 continue;\r
408\r
409 name = ent->name;\r
410 if (strlen(name) == 0) {\r
411 if (ent->generate_name)\r
412 name = ent->generate_name(ent->id, &offs);\r
413 }\r
414 if (name != NULL)\r
415 text_out16(x, y, name);\r
416\r
417 switch (ent->beh) {\r
418 case MB_NONE:\r
419 break;\r
420 case MB_OPT_ONOFF:\r
421 text_out16(x + col2_offs, y, (*(int *)ent->var & ent->mask) ? "ON" : "OFF");\r
422 break;\r
423 case MB_OPT_RANGE:\r
424 text_out16(x + col2_offs, y, "%i", *(int *)ent->var);\r
425 break;\r
426 case MB_OPT_CUSTOM:\r
427 case MB_OPT_CUSTONOFF:\r
428 case MB_OPT_CUSTRANGE:\r
429 name = NULL;\r
430 offs = 0;\r
431 if (ent->generate_name)\r
432 name = ent->generate_name(ent->id, &offs);\r
433 if (name != NULL)\r
434 text_out16(x + col2_offs + offs * me_mfont_w, y, "%s", name);\r
435 break;\r
436 }\r
437\r
438 y += me_mfont_h;\r
439 }\r
440\r
441 /* display message if we have one */\r
442 if (menu_error_msg[0] != 0) {\r
443 if (g_screen_height - h >= 2 * me_mfont_h)\r
444 text_out16(5, g_screen_height - me_mfont_h - 4, menu_error_msg);\r
445 else\r
446 lprintf("menu msg doesn't fit!\n");\r
447\r
448 if (plat_get_ticks_ms() - menu_error_time > 2048)\r
449 menu_error_msg[0] = 0;\r
450 }\r
451\r
452 plat_video_menu_end();\r
453}\r
454\r
455static int me_process(menu_entry *entry, int is_next)\r
456{\r
457 switch (entry->beh)\r
458 {\r
459 case MB_OPT_ONOFF:\r
460 case MB_OPT_CUSTONOFF:\r
461 *(int *)entry->var ^= entry->mask;\r
462 return 1;\r
463 case MB_OPT_RANGE:\r
464 case MB_OPT_CUSTRANGE:\r
465 *(int *)entry->var += is_next ? 1 : -1;\r
466 if (*(int *)entry->var < (int)entry->min)\r
467 *(int *)entry->var = (int)entry->max;\r
468 if (*(int *)entry->var > (int)entry->max)\r
469 *(int *)entry->var = (int)entry->min;\r
470 return 1;\r
471 default:\r
472 return 0;\r
473 }\r
474}\r
475\r
476static void debug_menu_loop(void);\r
477\r
478static void me_loop(menu_entry *menu, int *menu_sel)\r
479{\r
480 int ret, inp, sel = *menu_sel, menu_sel_max;\r
481\r
482 menu_sel_max = me_count(menu) - 1;\r
483 if (menu_sel_max < 1) {\r
484 lprintf("no enabled menu entries\n");\r
485 return;\r
486 }\r
487\r
488 while ((!menu[sel].enabled || !menu[sel].selectable) && sel < menu_sel_max)\r
489 sel++;\r
490\r
491 /* make sure action buttons are not pressed on entering menu */\r
492 me_draw(menu, sel);\r
493 while (in_menu_wait_any(50) & (PBTN_MOK|PBTN_MBACK|PBTN_MENU));\r
494\r
495 for (;;)\r
496 {\r
497 me_draw(menu, sel);\r
498 inp = in_menu_wait(PBTN_UP|PBTN_DOWN|PBTN_LEFT|PBTN_RIGHT|\r
499 PBTN_MOK|PBTN_MBACK|PBTN_MENU|PBTN_L|PBTN_R, 70);\r
500 if (inp & (PBTN_MENU|PBTN_MBACK))\r
501 break;\r
502\r
503 if (inp & PBTN_UP ) {\r
504 do {\r
505 sel--;\r
506 if (sel < 0)\r
507 sel = menu_sel_max;\r
508 }\r
509 while (!menu[sel].enabled || !menu[sel].selectable);\r
510 }\r
511 if (inp & PBTN_DOWN) {\r
512 do {\r
513 sel++;\r
514 if (sel > menu_sel_max)\r
515 sel = 0;\r
516 }\r
517 while (!menu[sel].enabled || !menu[sel].selectable);\r
518 }\r
519\r
520 /* a bit hacky but oh well */\r
521 if ((inp & (PBTN_L|PBTN_R)) == (PBTN_L|PBTN_R))\r
522 debug_menu_loop();\r
523\r
524 if (inp & (PBTN_LEFT|PBTN_RIGHT)) { /* multi choice */\r
525 if (me_process(&menu[sel], (inp & PBTN_RIGHT) ? 1 : 0))\r
526 continue;\r
527 }\r
528\r
529 if (inp & (PBTN_MOK|PBTN_LEFT|PBTN_RIGHT))\r
530 {\r
531 if (menu[sel].handler != NULL) {\r
532 ret = menu[sel].handler(menu[sel].id, inp);\r
533 if (ret) break;\r
534 menu_sel_max = me_count(menu) - 1; /* might change */\r
535 }\r
536 }\r
537 }\r
538 *menu_sel = sel;\r
539}\r
540\r
541/* ***************************************** */\r
542\r
543static void draw_menu_credits(void)\r
544{\r
545 const char *creds, *p;\r
546 int x, y, h, w, wt;\r
547\r
548 p = creds = plat_get_credits();\r
549\r
550 for (h = 1, w = 0; *p != 0; h++) {\r
551 for (wt = 0; *p != 0 && *p != '\n'; p++)\r
552 wt++;\r
553\r
554 if (wt > w)\r
555 w = wt;\r
556 if (*p == 0)\r
557 break;\r
558 p++;\r
559 }\r
560\r
561 x = g_screen_width / 2 - w * me_mfont_w / 2;\r
562 y = g_screen_height / 2 - h * me_mfont_h / 2;\r
563 if (x < 0) x = 0;\r
564 if (y < 0) y = 0;\r
565\r
566 plat_video_menu_begin();\r
567\r
568 for (p = creds; *p != 0 && y <= g_screen_height - me_mfont_h; y += me_mfont_h) {\r
569 text_out16(x, y, p);\r
570\r
571 for (; *p != 0 && *p != '\n'; p++)\r
572 ;\r
573 if (*p != 0)\r
574 p++;\r
575 }\r
576\r
577 plat_video_menu_end();\r
578}\r
579\r
580// --------- loading ROM screen ----------\r
581\r
582static int cdload_called = 0;\r
583\r
584static void load_progress_cb(int percent)\r
585{\r
586 int ln, len = percent * g_screen_width / 100;\r
587 unsigned short *dst = (unsigned short *)g_screen_ptr + g_screen_width * 10 * 2;\r
588\r
589 if (len > g_screen_width)\r
590 len = g_screen_width;\r
591 for (ln = 10 - 2; ln > 0; ln--, dst += g_screen_width)\r
592 memset(dst, 0xff, len * 2);\r
593 plat_video_menu_end();\r
594}\r
595\r
596static void cdload_progress_cb(int percent)\r
597{\r
598 int ln, len = percent * g_screen_width / 100;\r
599 unsigned short *dst = (unsigned short *)g_screen_ptr + g_screen_width * 10 * 2;\r
600\r
601 memset(dst, 0xff, g_screen_width * (me_sfont_h - 2) * 2);\r
602\r
603 smalltext_out16(1, 3 * me_sfont_h, "Processing CD image / MP3s", 0xffff);\r
604 smalltext_out16(1, 4 * me_sfont_h, rom_fname_loaded, 0xffff);\r
605 dst += g_screen_width * me_sfont_h * 3;\r
606\r
607 if (len > g_screen_width)\r
608 len = g_screen_width;\r
609 for (ln = (me_sfont_h - 2); ln > 0; ln--, dst += g_screen_width)\r
610 memset(dst, 0xff, len * 2);\r
611\r
612 plat_video_menu_end();\r
613 cdload_called = 1;\r
614}\r
615\r
616void menu_romload_prepare(const char *rom_name)\r
617{\r
618 const char *p = rom_name + strlen(rom_name);\r
619\r
620 while (p > rom_name && *p != '/')\r
621 p--;\r
622\r
623 /* fill both buffers, callbacks won't update in full */\r
624 plat_video_menu_begin();\r
625 smalltext_out16(1, 1, "Loading", 0xffff);\r
626 smalltext_out16(1, me_sfont_h, p, 0xffff);\r
627 plat_video_menu_end();\r
628\r
629 plat_video_menu_begin();\r
630 smalltext_out16(1, 1, "Loading", 0xffff);\r
631 smalltext_out16(1, me_sfont_h, p, 0xffff);\r
632 plat_video_menu_end();\r
633\r
634 PicoCartLoadProgressCB = load_progress_cb;\r
635 PicoCDLoadProgressCB = cdload_progress_cb;\r
636 cdload_called = 0;\r
637}\r
638\r
639void menu_romload_end(void)\r
640{\r
641 PicoCartLoadProgressCB = PicoCDLoadProgressCB = NULL;\r
642 smalltext_out16(1, (cdload_called ? 6 : 3) * me_sfont_h,\r
643 "Starting emulation...", 0xffff);\r
644 plat_video_menu_end();\r
645}\r
646\r
647// -------------- del confirm ---------------\r
648\r
649static void do_delete(const char *fpath, const char *fname)\r
650{\r
651 int len, mid, inp;\r
652 const char *nm;\r
653 char tmp[64];\r
654\r
655 plat_video_menu_begin();\r
656\r
657 if (!rom_loaded)\r
658 menu_darken_bg(g_screen_ptr, g_screen_width * g_screen_height, 0);\r
659\r
660 len = strlen(fname);\r
661 if (len > g_screen_width/6)\r
662 len = g_screen_width/6;\r
663\r
664 mid = g_screen_width / 2;\r
665 text_out16(mid - me_mfont_w * 15 / 2, 8 * me_mfont_h, "About to delete");\r
666 smalltext_out16(mid - len * me_sfont_w / 2, 9 * me_mfont_h + 5, fname, 0xbdff);\r
667 text_out16(mid - me_mfont_w * 13 / 2, 11 * me_mfont_h, "Are you sure?");\r
668\r
669 nm = in_get_key_name(-1, -PBTN_MA3);\r
670 snprintf(tmp, sizeof(tmp), "(%s - confirm, ", nm);\r
671 len = strlen(tmp);\r
672 nm = in_get_key_name(-1, -PBTN_MBACK);\r
673 snprintf(tmp + len, sizeof(tmp) - len, "%s - cancel)", nm);\r
674 len = strlen(tmp);\r
675\r
676 text_out16(mid - me_mfont_w * len / 2, 12 * me_mfont_h, tmp);\r
677 plat_video_menu_end();\r
678\r
679 while (in_menu_wait_any(50) & (PBTN_MENU|PBTN_MA2));\r
680 inp = in_menu_wait(PBTN_MA3|PBTN_MBACK, 100);\r
681 if (inp & PBTN_MA3)\r
682 remove(fpath);\r
683}\r
684\r
685// -------------- ROM selector --------------\r
686\r
687// rrrr rggg gggb bbbb\r
688static unsigned short file2color(const char *fname)\r
689{\r
690 const char *ext = fname + strlen(fname) - 3;\r
691 static const char *rom_exts[] = { "zip", "bin", "smd", "gen", "iso", "cso", "cue" };\r
692 static const char *other_exts[] = { "gmv", "pat" };\r
693 int i;\r
694\r
695 if (ext < fname) ext = fname;\r
696 for (i = 0; i < array_size(rom_exts); i++)\r
697 if (strcasecmp(ext, rom_exts[i]) == 0) return 0xbdff; // FIXME: mk defines\r
698 for (i = 0; i < array_size(other_exts); i++)\r
699 if (strcasecmp(ext, other_exts[i]) == 0) return 0xaff5;\r
700 return 0xffff;\r
701}\r
702\r
703static void draw_dirlist(char *curdir, struct dirent **namelist, int n, int sel)\r
704{\r
705 int max_cnt, start, i, x, pos;\r
706\r
707 max_cnt = g_screen_height / me_sfont_h;\r
708 start = max_cnt / 2 - sel;\r
709 n--; // exclude current dir (".")\r
710\r
711 plat_video_menu_begin();\r
712\r
713// if (!rom_loaded)\r
714// menu_darken_bg(gp2x_screen, 320*240, 0);\r
715\r
716 menu_darken_bg((short *)g_screen_ptr + g_screen_width * max_cnt/2 * 10, g_screen_width * 8, 0);\r
717\r
718 x = 5 + me_mfont_w + 1;\r
719 if (start - 2 >= 0)\r
720 smalltext_out16(14, (start - 2) * me_sfont_h, curdir, 0xffff);\r
721 for (i = 0; i < n; i++) {\r
722 pos = start + i;\r
723 if (pos < 0) continue;\r
724 if (pos >= max_cnt) break;\r
725 if (namelist[i+1]->d_type == DT_DIR) {\r
726 smalltext_out16(x, pos * me_sfont_h, "/", 0xfff6);\r
727 smalltext_out16(x + me_sfont_w, pos * me_sfont_h, namelist[i+1]->d_name, 0xfff6);\r
728 } else {\r
729 unsigned short color = file2color(namelist[i+1]->d_name);\r
730 smalltext_out16(x, pos * me_sfont_h, namelist[i+1]->d_name, color);\r
731 }\r
732 }\r
733 smalltext_out16(5, max_cnt/2 * me_sfont_h, ">", 0xffff);\r
734 plat_video_menu_end();\r
735}\r
736\r
737static int scandir_cmp(const void *p1, const void *p2)\r
738{\r
739 struct dirent **d1 = (struct dirent **)p1, **d2 = (struct dirent **)p2;\r
740 if ((*d1)->d_type == (*d2)->d_type) return alphasort(d1, d2);\r
741 if ((*d1)->d_type == DT_DIR) return -1; // put before\r
742 if ((*d2)->d_type == DT_DIR) return 1;\r
743 return alphasort(d1, d2);\r
744}\r
745\r
746static const char *filter_exts[] = {\r
747 ".mp3", ".MP3", ".srm", ".brm", "s.gz", ".mds", "bcfg", ".txt", ".htm", "html",\r
748 ".jpg", ".gpe"\r
749};\r
750\r
751static int scandir_filter(const struct dirent *ent)\r
752{\r
753 const char *p;\r
754 int i;\r
755\r
756 if (ent == NULL || ent->d_name == NULL) return 0;\r
757 if (strlen(ent->d_name) < 5) return 1;\r
758\r
759 p = ent->d_name + strlen(ent->d_name) - 4;\r
760\r
761 for (i = 0; i < array_size(filter_exts); i++)\r
762 if (strcmp(p, filter_exts[i]) == 0)\r
763 return 0;\r
764\r
765 return 1;\r
766}\r
767\r
768static char *menu_loop_romsel(char *curr_path, int len)\r
769{\r
770 struct dirent **namelist;\r
771 int n, inp, sel = 0;\r
772 char *ret = NULL, *fname = NULL;\r
773\r
774rescan:\r
775 // is this a dir or a full path?\r
776 if (!plat_is_dir(curr_path)) {\r
777 char *p = curr_path + strlen(curr_path) - 1;\r
778 for (; p > curr_path && *p != '/'; p--)\r
779 ;\r
780 *p = 0;\r
781 fname = p+1;\r
782 }\r
783\r
784 n = scandir(curr_path, &namelist, scandir_filter, scandir_cmp);\r
785 if (n < 0) {\r
786 lprintf("menu_loop_romsel failed, dir: %s\n", curr_path);\r
787\r
788 // try root\r
789 getcwd(curr_path, len);\r
790 n = scandir(curr_path, &namelist, scandir_filter, scandir_cmp);\r
791 if (n < 0) {\r
792 // oops, we failed\r
793 lprintf("menu_loop_romsel failed, dir: %s\n", curr_path);\r
794 return NULL;\r
795 }\r
796 }\r
797\r
798 // try to find sel\r
799 if (fname != NULL) {\r
800 int i;\r
801 for (i = 1; i < n; i++) {\r
802 if (strcmp(namelist[i]->d_name, fname) == 0) {\r
803 sel = i - 1;\r
804 break;\r
805 }\r
806 }\r
807 }\r
808\r
809 for (;;)\r
810 {\r
811 draw_dirlist(curr_path, namelist, n, sel);\r
812 inp = in_menu_wait(PBTN_UP|PBTN_DOWN|PBTN_LEFT|PBTN_RIGHT|\r
813 PBTN_L|PBTN_R|PBTN_MA2|PBTN_MOK|PBTN_MBACK|PBTN_MENU, 33);\r
814 if (inp & PBTN_UP ) { sel--; if (sel < 0) sel = n-2; }\r
815 if (inp & PBTN_DOWN) { sel++; if (sel > n-2) sel = 0; }\r
816 if (inp & PBTN_LEFT) { sel-=10; if (sel < 0) sel = 0; }\r
817 if (inp & PBTN_L) { sel-=24; if (sel < 0) sel = 0; }\r
818 if (inp & PBTN_RIGHT) { sel+=10; if (sel > n-2) sel = n-2; }\r
819 if (inp & PBTN_R) { sel+=24; if (sel > n-2) sel = n-2; }\r
820 if ((inp & PBTN_MOK) || (inp & (PBTN_MENU|PBTN_MA2)) == (PBTN_MENU|PBTN_MA2))\r
821 {\r
822 again:\r
823 if (namelist[sel+1]->d_type == DT_REG)\r
824 {\r
825 strcpy(rom_fname_reload, curr_path);\r
826 strcat(rom_fname_reload, "/");\r
827 strcat(rom_fname_reload, namelist[sel+1]->d_name);\r
828 if (inp & PBTN_MOK) { // return sel\r
829 ret = rom_fname_reload;\r
830 break;\r
831 }\r
832 do_delete(rom_fname_reload, namelist[sel+1]->d_name);\r
833 if (n > 0) {\r
834 while (n--) free(namelist[n]);\r
835 free(namelist);\r
836 }\r
837 goto rescan;\r
838 }\r
839 else if (namelist[sel+1]->d_type == DT_DIR)\r
840 {\r
841 int newlen;\r
842 char *p, *newdir;\r
843 if (!(inp & PBTN_MOK)) continue;\r
844 newlen = strlen(curr_path) + strlen(namelist[sel+1]->d_name) + 2;\r
845 newdir = malloc(newlen);\r
846 if (strcmp(namelist[sel+1]->d_name, "..") == 0) {\r
847 char *start = curr_path;\r
848 p = start + strlen(start) - 1;\r
849 while (*p == '/' && p > start) p--;\r
850 while (*p != '/' && p > start) p--;\r
851 if (p <= start) strcpy(newdir, "/");\r
852 else { strncpy(newdir, start, p-start); newdir[p-start] = 0; }\r
853 } else {\r
854 strcpy(newdir, curr_path);\r
855 p = newdir + strlen(newdir) - 1;\r
856 while (*p == '/' && p >= newdir) *p-- = 0;\r
857 strcat(newdir, "/");\r
858 strcat(newdir, namelist[sel+1]->d_name);\r
859 }\r
860 ret = menu_loop_romsel(newdir, newlen);\r
861 free(newdir);\r
862 break;\r
863 }\r
864 else\r
865 {\r
866 // unknown file type, happens on NTFS mounts. Try to guess.\r
867 FILE *tstf; int tmp;\r
868 strcpy(rom_fname_reload, curr_path);\r
869 strcat(rom_fname_reload, "/");\r
870 strcat(rom_fname_reload, namelist[sel+1]->d_name);\r
871 tstf = fopen(rom_fname_reload, "rb");\r
872 if (tstf != NULL)\r
873 {\r
874 if (fread(&tmp, 1, 1, tstf) > 0 || ferror(tstf) == 0)\r
875 namelist[sel+1]->d_type = DT_REG;\r
876 else namelist[sel+1]->d_type = DT_DIR;\r
877 fclose(tstf);\r
878 goto again;\r
879 }\r
880 }\r
881 }\r
882 if (inp & PBTN_MBACK)\r
883 break;\r
884 }\r
885\r
886 if (n > 0) {\r
887 while (n--) free(namelist[n]);\r
888 free(namelist);\r
889 }\r
890\r
891 return ret;\r
892}\r
893\r
894// ------------ patch/gg menu ------------\r
895\r
896static void draw_patchlist(int sel)\r
897{\r
898 int max_cnt, start, i, pos, active;\r
899\r
900 max_cnt = g_screen_height / 10;\r
901 start = max_cnt / 2 - sel;\r
902\r
903 plat_video_menu_begin();\r
904\r
905 for (i = 0; i < PicoPatchCount; i++) {\r
906 pos = start + i;\r
907 if (pos < 0) continue;\r
908 if (pos >= max_cnt) break;\r
909 active = PicoPatches[i].active;\r
910 smalltext_out16(14, pos * me_sfont_h, active ? "ON " : "OFF", active ? 0xfff6 : 0xffff);\r
911 smalltext_out16(14+6*4, pos * me_sfont_h, PicoPatches[i].name, active ? 0xfff6 : 0xffff);\r
912 }\r
913 pos = start + i;\r
914 if (pos < max_cnt)\r
915 smalltext_out16(14, pos * me_sfont_h, "done", 0xffff);\r
916\r
917 text_out16(5, max_cnt / 2 * me_sfont_h, ">");\r
918 plat_video_menu_end();\r
919}\r
920\r
921static void menu_loop_patches(void)\r
922{\r
923 static int menu_sel = 0;\r
924 int inp;\r
925\r
926 for (;;)\r
927 {\r
928 draw_patchlist(menu_sel);\r
929 inp = in_menu_wait(PBTN_UP|PBTN_DOWN|PBTN_LEFT|PBTN_RIGHT|PBTN_L|PBTN_R|PBTN_MOK|PBTN_MBACK, 33);\r
930 if (inp & PBTN_UP ) { menu_sel--; if (menu_sel < 0) menu_sel = PicoPatchCount; }\r
931 if (inp & PBTN_DOWN) { menu_sel++; if (menu_sel > PicoPatchCount) menu_sel = 0; }\r
932 if (inp &(PBTN_LEFT|PBTN_L)) { menu_sel-=10; if (menu_sel < 0) menu_sel = 0; }\r
933 if (inp &(PBTN_RIGHT|PBTN_R)) { menu_sel+=10; if (menu_sel > PicoPatchCount) menu_sel = PicoPatchCount; }\r
934 if (inp & PBTN_MOK) { // action\r
935 if (menu_sel < PicoPatchCount)\r
936 PicoPatches[menu_sel].active = !PicoPatches[menu_sel].active;\r
937 else break;\r
938 }\r
939 if (inp & PBTN_MBACK)\r
940 break;\r
941 }\r
942}\r
943\r
944// ------------ savestate loader ------------\r
945\r
946static int state_slot_flags = 0;\r
947\r
948static void state_check_slots(void)\r
949{\r
950 int slot;\r
951\r
952 state_slot_flags = 0;\r
953\r
954 for (slot = 0; slot < 10; slot++) {\r
955 if (emu_check_save_file(slot))\r
956 state_slot_flags |= 1 << slot;\r
957 }\r
958}\r
959\r
960static void draw_savestate_bg(int slot)\r
961{\r
962 struct PicoVideo tmp_pv;\r
963 unsigned short tmp_cram[0x40];\r
964 unsigned short tmp_vsram[0x40];\r
965 void *tmp_vram, *file;\r
966 char *fname;\r
967\r
968 fname = emu_get_save_fname(1, 0, slot);\r
969 if (!fname) return;\r
970\r
971 tmp_vram = malloc(sizeof(Pico.vram));\r
972 if (tmp_vram == NULL) return;\r
973\r
974 memcpy(tmp_vram, Pico.vram, sizeof(Pico.vram));\r
975 memcpy(tmp_cram, Pico.cram, sizeof(Pico.cram));\r
976 memcpy(tmp_vsram, Pico.vsram, sizeof(Pico.vsram));\r
977 memcpy(&tmp_pv, &Pico.video, sizeof(Pico.video));\r
978\r
979 if (strcmp(fname + strlen(fname) - 3, ".gz") == 0) {\r
980 file = gzopen(fname, "rb");\r
981 emu_setSaveStateCbs(1);\r
982 } else {\r
983 file = fopen(fname, "rb");\r
984 emu_setSaveStateCbs(0);\r
985 }\r
986\r
987 if (file) {\r
988 if (PicoAHW & PAHW_MCD) {\r
989 PicoCdLoadStateGfx(file);\r
990 } else {\r
991 areaSeek(file, 0x10020, SEEK_SET); // skip header and RAM in state file\r
992 areaRead(Pico.vram, 1, sizeof(Pico.vram), file);\r
993 areaSeek(file, 0x2000, SEEK_CUR);\r
994 areaRead(Pico.cram, 1, sizeof(Pico.cram), file);\r
995 areaRead(Pico.vsram, 1, sizeof(Pico.vsram), file);\r
996 areaSeek(file, 0x221a0, SEEK_SET);\r
997 areaRead(&Pico.video, 1, sizeof(Pico.video), file);\r
998 }\r
999 areaClose(file);\r
1000 }\r
1001\r
1002 /* do a frame and fetch menu bg */\r
1003 pemu_forced_frame(POPT_EN_SOFTSCALE);\r
1004 plat_video_menu_enter(1);\r
1005\r
1006 memcpy(Pico.vram, tmp_vram, sizeof(Pico.vram));\r
1007 memcpy(Pico.cram, tmp_cram, sizeof(Pico.cram));\r
1008 memcpy(Pico.vsram, tmp_vsram, sizeof(Pico.vsram));\r
1009 memcpy(&Pico.video, &tmp_pv, sizeof(Pico.video));\r
1010 free(tmp_vram);\r
1011}\r
1012\r
1013static void draw_savestate_menu(int menu_sel, int is_loading)\r
1014{\r
1015 int i, x, y, w, h;\r
1016\r
1017 if (state_slot_flags & (1 << menu_sel))\r
1018 draw_savestate_bg(menu_sel);\r
1019\r
1020 w = (13 + 2) * me_mfont_w;\r
1021 h = (1+2+10+1) * me_mfont_h;\r
1022 x = g_screen_width / 2 - w / 2;\r
1023 if (x < 0) x = 0;\r
1024 y = g_screen_height / 2 - h / 2;\r
1025 if (y < 0) y = 0;\r
1026\r
1027 plat_video_menu_begin();\r
1028\r
1029 text_out16(x, y, is_loading ? "Load state" : "Save state");\r
1030 y += 3 * me_mfont_h;\r
1031\r
1032 menu_draw_selection(x - me_mfont_w * 2, y + menu_sel * me_mfont_h, (13 + 2) * me_mfont_w + 4);\r
1033\r
1034 /* draw all 10 slots */\r
1035 for (i = 0; i < 10; i++, y += me_mfont_h)\r
1036 {\r
1037 text_out16(x, y, "SLOT %i (%s)", i, (state_slot_flags & (1 << i)) ? "USED" : "free");\r
1038 }\r
1039 text_out16(x, y, "back");\r
1040\r
1041 plat_video_menu_end();\r
1042}\r
1043\r
1044static int menu_loop_savestate(int is_loading)\r
1045{\r
1046 static int menu_sel = 10;\r
1047 int menu_sel_max = 10;\r
1048 unsigned long inp = 0;\r
1049\r
1050 state_check_slots();\r
1051\r
1052 for (;;)\r
1053 {\r
1054 draw_savestate_menu(menu_sel, is_loading);\r
1055 inp = in_menu_wait(PBTN_UP|PBTN_DOWN|PBTN_MOK|PBTN_MBACK, 100);\r
1056 if (inp & PBTN_UP) {\r
1057 do {\r
1058 menu_sel--;\r
1059 if (menu_sel < 0)\r
1060 menu_sel = menu_sel_max;\r
1061 } while (!(state_slot_flags & (1 << menu_sel)) && menu_sel != menu_sel_max && is_loading);\r
1062 }\r
1063 if (inp & PBTN_DOWN) {\r
1064 do {\r
1065 menu_sel++;\r
1066 if (menu_sel > menu_sel_max)\r
1067 menu_sel = 0;\r
1068 } while (!(state_slot_flags & (1 << menu_sel)) && menu_sel != menu_sel_max && is_loading);\r
1069 }\r
1070 if (inp & PBTN_MOK) { // save/load\r
1071 if (menu_sel < 10) {\r
1072 state_slot = menu_sel;\r
1073 if (emu_save_load_game(is_loading, 0)) {\r
1074 me_update_msg(is_loading ? "Load failed" : "Save failed");\r
1075 return 0;\r
1076 }\r
1077 return 1;\r
1078 }\r
1079 return 0;\r
1080 }\r
1081 if (inp & PBTN_MBACK)\r
1082 return 0;\r
1083 }\r
1084}\r
1085\r
1086// -------------- key config --------------\r
1087\r
1088static char *action_binds(int player_idx, int action_mask, int dev_id)\r
1089{\r
1090 int k, count, can_combo, type;\r
1091 const int *binds;\r
1092\r
1093 static_buff[0] = 0;\r
1094\r
1095 binds = in_get_dev_binds(dev_id);\r
1096 if (binds == NULL)\r
1097 return static_buff;\r
1098\r
1099 count = in_get_dev_info(dev_id, IN_INFO_BIND_COUNT);\r
1100 can_combo = in_get_dev_info(dev_id, IN_INFO_DOES_COMBOS);\r
1101\r
1102 type = IN_BINDTYPE_EMU;\r
1103 if (player_idx >= 0) {\r
1104 can_combo = 0;\r
1105 type = IN_BINDTYPE_PLAYER12;\r
1106 }\r
1107 if (player_idx == 1)\r
1108 action_mask <<= 16;\r
1109\r
1110 for (k = 0; k < count; k++)\r
1111 {\r
1112 const char *xname;\r
1113 int len;\r
1114\r
1115 if (!(binds[IN_BIND_OFFS(k, type)] & action_mask))\r
1116 continue;\r
1117\r
1118 xname = in_get_key_name(dev_id, k);\r
1119 len = strlen(static_buff);\r
1120 if (len) {\r
1121 strncat(static_buff, can_combo ? " + " : ", ",\r
1122 sizeof(static_buff) - len - 1);\r
1123 len += can_combo ? 3 : 2;\r
1124 }\r
1125 strncat(static_buff, xname, sizeof(static_buff) - len - 1);\r
1126 }\r
1127\r
1128 return static_buff;\r
1129}\r
1130\r
1131static int count_bound_keys(int dev_id, int action_mask, int bindtype)\r
1132{\r
1133 const int *binds;\r
1134 int k, keys = 0;\r
1135 int count;\r
1136\r
1137 binds = in_get_dev_binds(dev_id);\r
1138 if (binds == NULL)\r
1139 return 0;\r
1140\r
1141 count = in_get_dev_info(dev_id, IN_INFO_BIND_COUNT);\r
1142 for (k = 0; k < count; k++)\r
1143 {\r
1144 if (binds[IN_BIND_OFFS(k, bindtype)] & action_mask)\r
1145 keys++;\r
1146 }\r
1147\r
1148 return keys;\r
1149}\r
1150\r
1151static void draw_key_config(const me_bind_action *opts, int opt_cnt, int player_idx,\r
1152 int sel, int dev_id, int dev_count, int is_bind)\r
1153{\r
1154 char buff[64], buff2[32];\r
1155 const char *dev_name;\r
1156 int x, y, w, i;\r
1157\r
1158 w = ((player_idx >= 0) ? 20 : 30) * me_mfont_w;\r
1159 x = g_screen_width / 2 - w / 2;\r
1160 y = (g_screen_height - 4 * me_mfont_h) / 2 - (2 + opt_cnt) * me_mfont_h / 2;\r
1161 if (x < me_mfont_w * 2)\r
1162 x = me_mfont_w * 2;\r
1163\r
1164 plat_video_menu_begin();\r
1165 if (player_idx >= 0)\r
1166 text_out16(x, y, "Player %i controls", player_idx + 1);\r
1167 else\r
1168 text_out16(x, y, "Emulator controls");\r
1169\r
1170 y += 2 * me_mfont_h;\r
1171 menu_draw_selection(x - me_mfont_w * 2, y + sel * me_mfont_h, w + 2 * me_mfont_w);\r
1172\r
1173 for (i = 0; i < opt_cnt; i++, y += me_mfont_h)\r
1174 text_out16(x, y, "%s : %s", opts[i].name,\r
1175 action_binds(player_idx, opts[i].mask, dev_id));\r
1176\r
1177 dev_name = in_get_dev_name(dev_id, 1, 1);\r
1178 w = strlen(dev_name) * me_mfont_w;\r
1179 if (w < 30 * me_mfont_w)\r
1180 w = 30 * me_mfont_w;\r
1181 if (w > g_screen_width)\r
1182 w = g_screen_width;\r
1183\r
1184 x = g_screen_width / 2 - w / 2;\r
1185\r
1186 if (!is_bind) {\r
1187 snprintf(buff2, sizeof(buff2), "%s", in_get_key_name(-1, -PBTN_MOK));\r
1188 snprintf(buff, sizeof(buff), "%s - bind, %s - clear", buff2,\r
1189 in_get_key_name(-1, -PBTN_MA2));\r
1190 text_out16(x, g_screen_height - 4 * me_mfont_h, buff);\r
1191 }\r
1192 else\r
1193 text_out16(x, g_screen_height - 4 * me_mfont_h, "Press a button to bind/unbind");\r
1194\r
1195 if (dev_count > 1) {\r
1196 text_out16(x, g_screen_height - 3 * me_mfont_h, dev_name);\r
1197 text_out16(x, g_screen_height - 2 * me_mfont_h, "Press left/right for other devs");\r
1198 }\r
1199\r
1200 plat_video_menu_end();\r
1201}\r
1202\r
1203static void key_config_loop(const me_bind_action *opts, int opt_cnt, int player_idx)\r
1204{\r
1205 int i, sel = 0, menu_sel_max = opt_cnt - 1;\r
1206 int dev_id, dev_count, kc, is_down, mkey;\r
1207 int unbind, bindtype, mask_shift;\r
1208\r
1209 for (i = 0, dev_id = -1, dev_count = 0; i < IN_MAX_DEVS; i++) {\r
1210 if (in_get_dev_name(i, 1, 0) != NULL) {\r
1211 dev_count++;\r
1212 if (dev_id < 0)\r
1213 dev_id = i;\r
1214 }\r
1215 }\r
1216\r
1217 if (dev_id == -1) {\r
1218 lprintf("no devs, can't do config\n");\r
1219 return;\r
1220 }\r
1221\r
1222 mask_shift = 0;\r
1223 if (player_idx == 1)\r
1224 mask_shift = 16;\r
1225 bindtype = player_idx >= 0 ? IN_BINDTYPE_PLAYER12 : IN_BINDTYPE_EMU;\r
1226\r
1227 for (;;)\r
1228 {\r
1229 draw_key_config(opts, opt_cnt, player_idx, sel, dev_id, dev_count, 0);\r
1230 mkey = in_menu_wait(PBTN_UP|PBTN_DOWN|PBTN_LEFT|PBTN_RIGHT|PBTN_MBACK|PBTN_MOK|PBTN_MA2, 100);\r
1231 switch (mkey) {\r
1232 case PBTN_UP: sel--; if (sel < 0) sel = menu_sel_max; continue;\r
1233 case PBTN_DOWN: sel++; if (sel > menu_sel_max) sel = 0; continue;\r
1234 case PBTN_LEFT:\r
1235 for (i = 0, dev_id--; i < IN_MAX_DEVS; i++, dev_id--) {\r
1236 if (dev_id < 0)\r
1237 dev_id = IN_MAX_DEVS - 1;\r
1238 if (in_get_dev_name(dev_id, 1, 0) != NULL)\r
1239 break;\r
1240 }\r
1241 continue;\r
1242 case PBTN_RIGHT:\r
1243 for (i = 0, dev_id++; i < IN_MAX_DEVS; i++, dev_id++) {\r
1244 if (dev_id >= IN_MAX_DEVS)\r
1245 dev_id = 0;\r
1246 if (in_get_dev_name(dev_id, 1, 0) != NULL)\r
1247 break;\r
1248 }\r
1249 continue;\r
1250 case PBTN_MBACK: return;\r
1251 case PBTN_MOK:\r
1252 if (sel >= opt_cnt)\r
1253 return;\r
1254 while (in_menu_wait_any(30) & PBTN_MOK);\r
1255 break;\r
1256 case PBTN_MA2:\r
1257 in_unbind_all(dev_id, opts[sel].mask << mask_shift, bindtype);\r
1258 continue;\r
1259 default:continue;\r
1260 }\r
1261\r
1262 draw_key_config(opts, opt_cnt, player_idx, sel, dev_id, dev_count, 1);\r
1263\r
1264 /* wait for some up event */\r
1265 for (is_down = 1; is_down; )\r
1266 kc = in_update_keycode(&dev_id, &is_down, -1);\r
1267\r
1268 i = count_bound_keys(dev_id, opts[sel].mask << mask_shift, bindtype);\r
1269 unbind = (i > 0);\r
1270\r
1271 /* allow combos if device supports them */\r
1272 if (i == 1 && bindtype == IN_BINDTYPE_EMU &&\r
1273 in_get_dev_info(dev_id, IN_INFO_DOES_COMBOS))\r
1274 unbind = 0;\r
1275\r
1276 in_bind_key(dev_id, kc, opts[sel].mask << mask_shift, bindtype, unbind);\r
1277 }\r
1278}\r
1279\r
1280// PicoPad[] format: MXYZ SACB RLDU\r
1281me_bind_action me_ctrl_actions[15] =\r
1282{\r
1283 { "UP ", 0x0001 },\r
1284 { "DOWN ", 0x0002 },\r
1285 { "LEFT ", 0x0004 },\r
1286 { "RIGHT ", 0x0008 },\r
1287 { "A ", 0x0040 },\r
1288 { "B ", 0x0010 },\r
1289 { "C ", 0x0020 },\r
1290 { "A turbo", 0x4000 },\r
1291 { "B turbo", 0x1000 },\r
1292 { "C turbo", 0x2000 },\r
1293 { "START ", 0x0080 },\r
1294 { "MODE ", 0x0800 },\r
1295 { "X ", 0x0400 },\r
1296 { "Y ", 0x0200 },\r
1297 { "Z ", 0x0100 }\r
1298};\r
1299\r
1300me_bind_action emuctrl_actions[] =\r
1301{\r
1302 { "Load State ", PEV_STATE_LOAD },\r
1303 { "Save State ", PEV_STATE_SAVE },\r
1304 { "Prev Save Slot ", PEV_SSLOT_PREV },\r
1305 { "Next Save Slot ", PEV_SSLOT_NEXT },\r
1306 { "Switch Renderer ", PEV_SWITCH_RND },\r
1307 { "Volume Down ", PEV_VOL_DOWN },\r
1308 { "Volume Up ", PEV_VOL_UP },\r
1309 { "Fast forward ", PEV_FF },\r
1310 { "Enter Menu ", PEV_MENU },\r
1311 { "Pico Next page ", PEV_PICO_PNEXT },\r
1312 { "Pico Prev page ", PEV_PICO_PPREV },\r
1313 { "Pico Switch input", PEV_PICO_SWINP },\r
1314 { NULL, 0 }\r
1315};\r
1316\r
1317static int key_config_loop_wrap(menu_id id, int keys)\r
1318{\r
1319 switch (id) {\r
1320 case MA_CTRL_PLAYER1:\r
1321 key_config_loop(me_ctrl_actions, array_size(me_ctrl_actions), 0);\r
1322 break;\r
1323 case MA_CTRL_PLAYER2:\r
1324 key_config_loop(me_ctrl_actions, array_size(me_ctrl_actions), 1);\r
1325 break;\r
1326 case MA_CTRL_EMU:\r
1327 key_config_loop(emuctrl_actions, array_size(emuctrl_actions) - 1, -1);\r
1328 break;\r
1329 default:\r
1330 break;\r
1331 }\r
1332 return 0;\r
1333}\r
1334\r
1335static const char *mgn_dev_name(menu_id id, int *offs)\r
1336{\r
1337 const char *name = NULL;\r
1338 static int it = 0;\r
1339\r
1340 if (id == MA_CTRL_DEV_FIRST)\r
1341 it = 0;\r
1342\r
1343 for (; it < IN_MAX_DEVS; it++) {\r
1344 name = in_get_dev_name(it, 1, 1);\r
1345 if (name != NULL)\r
1346 break;\r
1347 }\r
1348\r
1349 it++;\r
1350 return name;\r
1351}\r
1352\r
1353static int mh_saveloadcfg(menu_id id, int keys);\r
1354static const char *mgn_savecfg(menu_id id, int *offs);\r
1355\r
1356static menu_entry e_menu_keyconfig[] =\r
1357{\r
1358 mee_handler_id("Player 1", MA_CTRL_PLAYER1, key_config_loop_wrap),\r
1359 mee_handler_id("Player 2", MA_CTRL_PLAYER2, key_config_loop_wrap),\r
1360 mee_handler_id("Emulator controls", MA_CTRL_EMU, key_config_loop_wrap),\r
1361 mee_onoff ("6 button pad", MA_OPT_6BUTTON_PAD, PicoOpt, POPT_6BTN_PAD),\r
1362 mee_range ("Turbo rate", MA_CTRL_TURBO_RATE, currentConfig.turbo_rate, 1, 30),\r
1363 mee_handler_mkname_id(MA_OPT_SAVECFG, mh_saveloadcfg, mgn_savecfg),\r
1364 mee_handler_id("Save cfg for loaded game", MA_OPT_SAVECFG_GAME, mh_saveloadcfg),\r
1365 mee_label (""),\r
1366 mee_label ("Input devices:"),\r
1367 mee_label_mk (MA_CTRL_DEV_FIRST, mgn_dev_name),\r
1368 mee_label_mk (MA_CTRL_DEV_NEXT, mgn_dev_name),\r
1369 mee_label_mk (MA_CTRL_DEV_NEXT, mgn_dev_name),\r
1370 mee_label_mk (MA_CTRL_DEV_NEXT, mgn_dev_name),\r
1371 mee_label_mk (MA_CTRL_DEV_NEXT, mgn_dev_name),\r
1372 mee_label_mk (MA_CTRL_DEV_NEXT, mgn_dev_name),\r
1373 mee_label_mk (MA_CTRL_DEV_NEXT, mgn_dev_name),\r
1374 mee_end,\r
1375};\r
1376\r
1377static int menu_loop_keyconfig(menu_id id, int keys)\r
1378{\r
1379 static int sel = 0;\r
1380\r
1381 me_enable(e_menu_keyconfig, MA_OPT_SAVECFG_GAME, rom_loaded);\r
1382 me_loop(e_menu_keyconfig, &sel);\r
1383 return 0;\r
1384}\r
1385\r
1386// ------------ SCD options menu ------------\r
1387\r
1388static const char *mgn_cdopt_ra(menu_id id, int *offs)\r
1389{\r
1390 *offs = -5;\r
1391 if (PicoCDBuffers <= 0)\r
1392 return " OFF";\r
1393 sprintf(static_buff, "%5iK", PicoCDBuffers * 2);\r
1394 return static_buff;\r
1395}\r
1396\r
1397static int mh_cdopt_ra(menu_id id, int keys)\r
1398{\r
1399 if (keys & PBTN_LEFT) {\r
1400 PicoCDBuffers >>= 1;\r
1401 if (PicoCDBuffers < 2)\r
1402 PicoCDBuffers = 0;\r
1403 } else {\r
1404 if (PicoCDBuffers <= 0)\r
1405 PicoCDBuffers = 1;\r
1406 PicoCDBuffers <<= 1;\r
1407 if (PicoCDBuffers > 8*1024)\r
1408 PicoCDBuffers = 8*1024; // 16M\r
1409 }\r
1410 return 0;\r
1411}\r
1412\r
1413static menu_entry e_menu_cd_options[] =\r
1414{\r
1415 mee_onoff("CD LEDs", MA_CDOPT_LEDS, currentConfig.EmuOpt, 0x0400),\r
1416 mee_onoff("CDDA audio", MA_CDOPT_CDDA, PicoOpt, POPT_EN_MCD_CDDA),\r
1417 mee_onoff("PCM audio", MA_CDOPT_PCM, PicoOpt, POPT_EN_MCD_PCM),\r
1418 mee_cust ("ReadAhead buffer", MA_CDOPT_READAHEAD, mh_cdopt_ra, mgn_cdopt_ra),\r
1419 mee_onoff("SaveRAM cart", MA_CDOPT_SAVERAM, PicoOpt, POPT_EN_MCD_RAMCART),\r
1420 mee_onoff("Scale/Rot. fx (slow)", MA_CDOPT_SCALEROT_CHIP, PicoOpt, POPT_EN_MCD_GFX),\r
1421 mee_onoff("Better sync (slow)", MA_CDOPT_BETTER_SYNC, PicoOpt, POPT_EN_MCD_PSYNC),\r
1422 mee_end,\r
1423};\r
1424\r
1425static int menu_loop_cd_options(menu_id id, int keys)\r
1426{\r
1427 static int sel = 0;\r
1428 me_loop(e_menu_cd_options, &sel);\r
1429 return 0;\r
1430}\r
1431\r
1432// ------------ adv options menu ------------\r
1433\r
1434static menu_entry e_menu_adv_options[] =\r
1435{\r
1436 mee_onoff ("SRAM/BRAM saves", MA_OPT_SRAM_STATES, currentConfig.EmuOpt, EOPT_EN_SRAM),\r
1437 mee_onoff ("Disable sprite limit", MA_OPT2_NO_SPRITE_LIM, PicoOpt, POPT_DIS_SPRITE_LIM),\r
1438 mee_onoff ("Use second CPU for sound", MA_OPT_ARM940_SOUND, PicoOpt, POPT_EXT_FM),\r
1439 mee_onoff ("Emulate Z80", MA_OPT2_ENABLE_Z80, PicoOpt, POPT_EN_Z80),\r
1440 mee_onoff ("Emulate YM2612 (FM)", MA_OPT2_ENABLE_YM2612, PicoOpt, POPT_EN_FM),\r
1441 mee_onoff ("Emulate SN76496 (PSG)", MA_OPT2_ENABLE_SN76496,PicoOpt, POPT_EN_PSG),\r
1442 mee_onoff ("gzip savestates", MA_OPT2_GZIP_STATES, currentConfig.EmuOpt, EOPT_GZIP_SAVES),\r
1443 mee_onoff ("Don't save last used ROM", MA_OPT2_NO_LAST_ROM, currentConfig.EmuOpt, EOPT_NO_AUTOSVCFG),\r
1444 mee_onoff ("RAM overclock", MA_OPT2_RAMTIMINGS, currentConfig.EmuOpt, EOPT_RAM_TIMINGS),\r
1445 mee_onoff ("MMU hack", MA_OPT2_SQUIDGEHACK, currentConfig.EmuOpt, EOPT_MMUHACK),\r
1446 mee_onoff ("SVP dynarec", MA_OPT2_SVP_DYNAREC, PicoOpt, POPT_EN_SVP_DRC),\r
1447 mee_onoff ("Disable idle loop patching",MA_OPT2_NO_IDLE_LOOPS,PicoOpt, POPT_DIS_IDLE_DET),\r
1448 mee_end,\r
1449};\r
1450\r
1451static int menu_loop_adv_options(menu_id id, int keys)\r
1452{\r
1453 static int sel = 0;\r
1454 me_loop(e_menu_adv_options, &sel);\r
1455 return 0;\r
1456}\r
1457\r
1458// ------------ gfx options menu ------------\r
1459\r
1460static int mh_opt_render(menu_id id, int keys)\r
1461{\r
1462 plat_video_toggle_renderer((keys & PBTN_RIGHT) ? 1 : 0, 1);\r
1463 return 0;\r
1464}\r
1465\r
1466static const char *mgn_opt_renderer(menu_id id, int *offs)\r
1467{\r
1468 *offs = -11;\r
1469 if (PicoOpt & POPT_ALT_RENDERER)\r
1470 return " 8bit fast";\r
1471 else if (currentConfig.EmuOpt & EOPT_16BPP)\r
1472 return "16bit accurate";\r
1473 else\r
1474 return " 8bit accurate";\r
1475}\r
1476\r
1477static const char *mgn_opt_scaling(menu_id id, int *offs)\r
1478{\r
1479 *offs = -13;\r
1480 switch (currentConfig.scaling) {\r
1481 default: return " OFF";\r
1482 case EOPT_SCALE_HW_H: return " hw horizontal";\r
1483 case EOPT_SCALE_HW_HV: return "hw horiz. + vert";\r
1484 case EOPT_SCALE_SW_H: return " sw horizontal";\r
1485 }\r
1486}\r
1487\r
1488static const char *mgn_aopt_gamma(menu_id id, int *offs)\r
1489{\r
1490 sprintf(static_buff, "%i.%02i", currentConfig.gamma / 100, currentConfig.gamma%100);\r
1491 return static_buff;\r
1492}\r
1493\r
1494static menu_entry e_menu_gfx_options[] =\r
1495{\r
1496 mee_cust ("Renderer", MA_OPT_RENDERER, mh_opt_render, mgn_opt_renderer),\r
1497 mee_range_cust("Scaling", MA_OPT_SCALING, currentConfig.scaling, 0, 3, mgn_opt_scaling),\r
1498 mee_range_cust("Gamma correction", MA_OPT2_GAMMA, currentConfig.gamma, 1, 300, mgn_aopt_gamma),\r
1499 mee_onoff ("A_SN's gamma curve", MA_OPT2_A_SN_GAMMA, currentConfig.EmuOpt, EOPT_A_SN_GAMMA),\r
1500 mee_onoff ("Perfect vsync", MA_OPT2_VSYNC, currentConfig.EmuOpt, EOPT_PSYNC),\r
1501 mee_end,\r
1502};\r
1503\r
1504static int menu_loop_gfx_options(menu_id id, int keys)\r
1505{\r
1506 static int sel = 0;\r
1507 me_loop(e_menu_gfx_options, &sel);\r
1508 return 0;\r
1509}\r
1510\r
1511// ------------ options menu ------------\r
1512\r
1513static menu_entry e_menu_options[];\r
1514\r
1515static int sndrate_prevnext(int rate, int dir)\r
1516{\r
1517 static const int rates[] = { 8000, 11025, 16000, 22050, 44100 };\r
1518 int i;\r
1519\r
1520 for (i = 0; i < 5; i++)\r
1521 if (rates[i] == rate) break;\r
1522\r
1523 i += dir ? 1 : -1;\r
1524 if (i > 4) {\r
1525 if (!(PicoOpt & POPT_EN_STEREO)) {\r
1526 PicoOpt |= POPT_EN_STEREO;\r
1527 return rates[0];\r
1528 }\r
1529 return rates[4];\r
1530 }\r
1531 if (i < 0) {\r
1532 if (PicoOpt & POPT_EN_STEREO) {\r
1533 PicoOpt &= ~POPT_EN_STEREO;\r
1534 return rates[4];\r
1535 }\r
1536 return rates[0];\r
1537 }\r
1538 return rates[i];\r
1539}\r
1540\r
1541static void region_prevnext(int right)\r
1542{\r
1543 // jp_ntsc=1, jp_pal=2, usa=4, eu=8\r
1544 static const int rgn_orders[] = { 0x148, 0x184, 0x814, 0x418, 0x841, 0x481 };\r
1545 int i;\r
1546\r
1547 if (right) {\r
1548 if (!PicoRegionOverride) {\r
1549 for (i = 0; i < 6; i++)\r
1550 if (rgn_orders[i] == PicoAutoRgnOrder) break;\r
1551 if (i < 5) PicoAutoRgnOrder = rgn_orders[i+1];\r
1552 else PicoRegionOverride=1;\r
1553 }\r
1554 else\r
1555 PicoRegionOverride <<= 1;\r
1556 if (PicoRegionOverride > 8)\r
1557 PicoRegionOverride = 8;\r
1558 } else {\r
1559 if (!PicoRegionOverride) {\r
1560 for (i = 0; i < 6; i++)\r
1561 if (rgn_orders[i] == PicoAutoRgnOrder) break;\r
1562 if (i > 0) PicoAutoRgnOrder = rgn_orders[i-1];\r
1563 }\r
1564 else\r
1565 PicoRegionOverride >>= 1;\r
1566 }\r
1567}\r
1568\r
1569static int mh_opt_misc(menu_id id, int keys)\r
1570{\r
1571 int i;\r
1572\r
1573 switch (id) {\r
1574 case MA_OPT_SOUND_QUALITY:\r
1575 PsndRate = sndrate_prevnext(PsndRate, keys & PBTN_RIGHT);\r
1576 break;\r
1577 case MA_OPT_REGION:\r
1578 region_prevnext(keys & PBTN_RIGHT);\r
1579 break;\r
1580 case MA_OPT_CONFIRM_STATES:\r
1581 i = ((currentConfig.EmuOpt>>9)&1) | ((currentConfig.EmuOpt>>10)&2);\r
1582 i += (keys & PBTN_LEFT) ? -1 : 1;\r
1583 if (i < 0) i = 0; else if (i > 3) i = 3;\r
1584 i |= i << 1; i &= ~2;\r
1585 currentConfig.EmuOpt &= ~0xa00;\r
1586 currentConfig.EmuOpt |= i << 9;\r
1587 break;\r
1588 default:\r
1589 break;\r
1590 }\r
1591 return 0;\r
1592}\r
1593\r
1594static int mh_saveloadcfg(menu_id id, int keys)\r
1595{\r
1596 int ret;\r
1597\r
1598 if (keys & (PBTN_LEFT|PBTN_RIGHT)) { // multi choice\r
1599 config_slot += (keys & PBTN_LEFT) ? -1 : 1;\r
1600 if (config_slot < 0) config_slot = 9;\r
1601 else if (config_slot > 9) config_slot = 0;\r
1602 me_enable(e_menu_options, MA_OPT_LOADCFG, config_slot != config_slot_current);\r
1603 return 0;\r
1604 }\r
1605\r
1606 switch (id) {\r
1607 case MA_OPT_SAVECFG:\r
1608 case MA_OPT_SAVECFG_GAME:\r
1609 if (emu_write_config(id == MA_OPT_SAVECFG_GAME ? 1 : 0))\r
1610 me_update_msg("config saved");\r
1611 else\r
1612 me_update_msg("failed to write config");\r
1613 break;\r
1614 case MA_OPT_LOADCFG:\r
1615 ret = emu_read_config(1, 1);\r
1616 if (!ret) ret = emu_read_config(0, 1);\r
1617 if (ret) me_update_msg("config loaded");\r
1618 else me_update_msg("failed to load config");\r
1619 break;\r
1620 default:\r
1621 return 0;\r
1622 }\r
1623\r
1624 return 1;\r
1625}\r
1626\r
1627static int mh_restore_defaults(menu_id id, int keys)\r
1628{\r
1629 emu_set_defconfig();\r
1630 me_update_msg("defaults restored");\r
1631 return 1;\r
1632}\r
1633\r
1634static const char *mgn_opt_fskip(menu_id id, int *offs)\r
1635{\r
1636 if (currentConfig.Frameskip < 0)\r
1637 return "Auto";\r
1638 sprintf(static_buff, "%d", currentConfig.Frameskip);\r
1639 return static_buff;\r
1640}\r
1641\r
1642static const char *mgn_opt_sound(menu_id id, int *offs)\r
1643{\r
1644 const char *str2;\r
1645 *offs = -8;\r
1646 str2 = (PicoOpt & POPT_EN_STEREO) ? "stereo" : "mono";\r
1647 sprintf(static_buff, "%5iHz %s", PsndRate, str2);\r
1648 return static_buff;\r
1649}\r
1650\r
1651static const char *mgn_opt_region(menu_id id, int *offs)\r
1652{\r
1653 static const char *names[] = { "Auto", " Japan NTSC", " Japan PAL", " USA", " Europe" };\r
1654 static const char *names_short[] = { "", " JP", " JP", " US", " EU" };\r
1655 int code = PicoRegionOverride;\r
1656 int u, i = 0;\r
1657\r
1658 *offs = -6;\r
1659 if (code) {\r
1660 code <<= 1;\r
1661 while ((code >>= 1)) i++;\r
1662 if (i > 4)\r
1663 return "unknown";\r
1664 return names[i];\r
1665 } else {\r
1666 strcpy(static_buff, "Auto:");\r
1667 for (u = 0; u < 3; u++) {\r
1668 code = (PicoAutoRgnOrder >> u*4) & 0xf;\r
1669 for (i = 0; code; code >>= 1, i++)\r
1670 ;\r
1671 strcat(static_buff, names_short[i]);\r
1672 }\r
1673 return static_buff;\r
1674 }\r
1675}\r
1676\r
1677static const char *mgn_opt_c_saves(menu_id id, int *offs)\r
1678{\r
1679 switch ((currentConfig.EmuOpt >> 9) & 5) {\r
1680 default: return "OFF";\r
1681 case 1: return "writes";\r
1682 case 4: return "loads";\r
1683 case 5: return "both";\r
1684 }\r
1685}\r
1686\r
1687static const char *mgn_savecfg(menu_id id, int *offs)\r
1688{\r
1689 strcpy(static_buff, "Save global config");\r
1690 if (config_slot != 0)\r
1691 sprintf(static_buff + strlen(static_buff), " (profile: %i)", config_slot);\r
1692 return static_buff;\r
1693}\r
1694\r
1695static const char *mgn_loadcfg(menu_id id, int *offs)\r
1696{\r
1697 sprintf(static_buff, "Load cfg from profile %i", config_slot);\r
1698 return static_buff;\r
1699}\r
1700\r
1701static menu_entry e_menu_options[] =\r
1702{\r
1703 mee_range ("Save slot", MA_OPT_SAVE_SLOT, state_slot, 0, 9),\r
1704 mee_range_cust("Frameskip", MA_OPT_FRAMESKIP, currentConfig.Frameskip, -1, 16, mgn_opt_fskip),\r
1705 mee_cust ("Region", MA_OPT_REGION, mh_opt_misc, mgn_opt_region),\r
1706 mee_onoff ("Show FPS", MA_OPT_SHOW_FPS, currentConfig.EmuOpt, EOPT_SHOW_FPS),\r
1707 mee_onoff ("Enable sound", MA_OPT_ENABLE_SOUND, currentConfig.EmuOpt, EOPT_EN_SOUND),\r
1708 mee_cust ("Sound Quality", MA_OPT_SOUND_QUALITY, mh_opt_misc, mgn_opt_sound),\r
1709 mee_cust ("Confirm savestate", MA_OPT_CONFIRM_STATES,mh_opt_misc, mgn_opt_c_saves),\r
1710 mee_range (cpu_clk_name, MA_OPT_CPU_CLOCKS, currentConfig.CPUclock, 20, 900),\r
1711 mee_handler ("[Display options]", menu_loop_gfx_options),\r
1712 mee_handler ("[Advanced options]", menu_loop_adv_options),\r
1713 mee_handler ("[Sega/Mega CD options]", menu_loop_cd_options),\r
1714 mee_handler_mkname_id(MA_OPT_SAVECFG, mh_saveloadcfg, mgn_savecfg),\r
1715 mee_handler_id("Save cfg for current game only", MA_OPT_SAVECFG_GAME, mh_saveloadcfg),\r
1716 mee_handler_mkname_id(MA_OPT_LOADCFG, mh_saveloadcfg, mgn_loadcfg),\r
1717 mee_handler ("Restore defaults", mh_restore_defaults),\r
1718 mee_end,\r
1719};\r
1720\r
1721static int menu_loop_options(menu_id id, int keys)\r
1722{\r
1723 static int sel = 0;\r
1724\r
1725 me_enable(e_menu_options, MA_OPT_SAVECFG_GAME, rom_loaded);\r
1726 me_enable(e_menu_options, MA_OPT_LOADCFG, config_slot != config_slot_current);\r
1727\r
1728 me_loop(e_menu_options, &sel);\r
1729\r
1730 if (PicoRegionOverride)\r
1731 // force setting possibly changed..\r
1732 Pico.m.pal = (PicoRegionOverride == 2 || PicoRegionOverride == 8) ? 1 : 0;\r
1733\r
1734 return 0;\r
1735}\r
1736\r
1737// ------------ debug menu ------------\r
1738\r
1739#include <pico/debug.h>\r
1740\r
1741extern void SekStepM68k(void);\r
1742\r
1743static void mplayer_loop(void)\r
1744{\r
1745 pemu_sound_start();\r
1746\r
1747 while (1)\r
1748 {\r
1749 PDebugZ80Frame();\r
1750 if (in_menu_wait_any(0) & PBTN_MA3)\r
1751 break;\r
1752 pemu_sound_wait();\r
1753 }\r
1754\r
1755 pemu_sound_stop();\r
1756}\r
1757\r
1758static void draw_text_debug(const char *str, int skip, int from)\r
1759{\r
1760 const char *p;\r
1761 int line;\r
1762\r
1763 p = str;\r
1764 while (skip-- > 0)\r
1765 {\r
1766 while (*p && *p != '\n')\r
1767 p++;\r
1768 if (*p == 0 || p[1] == 0)\r
1769 return;\r
1770 p++;\r
1771 }\r
1772\r
1773 str = p;\r
1774 for (line = from; line < g_screen_height / me_sfont_h; line++)\r
1775 {\r
1776 smalltext_out16(1, line * me_sfont_h, str, 0xffff);\r
1777 while (*p && *p != '\n')\r
1778 p++;\r
1779 if (*p == 0)\r
1780 break;\r
1781 p++; str = p;\r
1782 }\r
1783}\r
1784\r
1785static void draw_frame_debug(void)\r
1786{\r
1787 char layer_str[48] = "layers: ";\r
1788 if (PicoDrawMask & PDRAW_LAYERB_ON) memcpy(layer_str + 8, "B", 1);\r
1789 if (PicoDrawMask & PDRAW_LAYERA_ON) memcpy(layer_str + 10, "A", 1);\r
1790 if (PicoDrawMask & PDRAW_SPRITES_LOW_ON) memcpy(layer_str + 12, "spr_lo", 6);\r
1791 if (PicoDrawMask & PDRAW_SPRITES_HI_ON) memcpy(layer_str + 19, "spr_hi", 6);\r
1792\r
1793 memset(g_screen_ptr, 0, g_screen_width * g_screen_height * 2);\r
1794 pemu_forced_frame(0);\r
1795 smalltext_out16(4, 1, "build: " __DATE__ " " __TIME__, 0xffff);\r
1796 smalltext_out16(4, g_screen_height - me_sfont_h, layer_str, 0xffff);\r
1797}\r
1798\r
1799static void debug_menu_loop(void)\r
1800{\r
1801 int inp, mode = 0;\r
1802 int spr_offs = 0, dumped = 0;\r
1803 char *tmp;\r
1804\r
1805 while (1)\r
1806 {\r
1807 switch (mode)\r
1808 {\r
1809 case 0: plat_video_menu_begin();\r
1810 tmp = PDebugMain();\r
1811 plat_debug_cat(tmp);\r
1812 draw_text_debug(tmp, 0, 0);\r
1813 if (dumped) {\r
1814 smalltext_out16(g_screen_width - 6 * me_sfont_h,\r
1815 g_screen_height - me_mfont_h, "dumped", 0xffff);\r
1816 dumped = 0;\r
1817 }\r
1818 break;\r
1819 case 1: draw_frame_debug(); break;\r
1820 case 2: memset(g_screen_ptr, 0, g_screen_width * g_screen_height * 2);\r
1821 pemu_forced_frame(0);\r
1822 menu_darken_bg(g_screen_ptr, g_screen_width * g_screen_height, 0);\r
1823 PDebugShowSpriteStats((unsigned short *)g_screen_ptr + (g_screen_height/2 - 240/2)*g_screen_width +\r
1824 g_screen_width/2 - 320/2, g_screen_width); break;\r
1825 case 3: memset(g_screen_ptr, 0, g_screen_width * g_screen_height * 2);\r
1826 PDebugShowPalette(g_screen_ptr, g_screen_width);\r
1827 PDebugShowSprite((unsigned short *)g_screen_ptr + g_screen_width*120 + g_screen_width/2 + 16,\r
1828 g_screen_width, spr_offs);\r
1829 draw_text_debug(PDebugSpriteList(), spr_offs, 6);\r
1830 break;\r
1831 }\r
1832 plat_video_menu_end();\r
1833\r
1834 inp = in_menu_wait(PBTN_MOK|PBTN_MBACK|PBTN_MA2|PBTN_MA3|PBTN_L|PBTN_R |\r
1835 PBTN_UP|PBTN_DOWN|PBTN_LEFT|PBTN_RIGHT, 70);\r
1836 if (inp & PBTN_MBACK) return;\r
1837 if (inp & PBTN_L) { mode--; if (mode < 0) mode = 3; }\r
1838 if (inp & PBTN_R) { mode++; if (mode > 3) mode = 0; }\r
1839 switch (mode)\r
1840 {\r
1841 case 0:\r
1842 if (inp & PBTN_MOK)\r
1843 SekStepM68k();\r
1844 if (inp & PBTN_MA3) {\r
1845 while (inp & PBTN_MA3)\r
1846 inp = in_menu_wait_any(-1);\r
1847 mplayer_loop();\r
1848 }\r
1849 if ((inp & (PBTN_MA2|PBTN_LEFT)) == (PBTN_MA2|PBTN_LEFT)) {\r
1850 mkdir("dumps", 0777);\r
1851 PDebugDumpMem();\r
1852 while (inp & PBTN_MA2) inp = in_menu_wait_any(-1);\r
1853 dumped = 1;\r
1854 }\r
1855 break;\r
1856 case 1:\r
1857 if (inp & PBTN_LEFT) PicoDrawMask ^= PDRAW_LAYERB_ON;\r
1858 if (inp & PBTN_RIGHT) PicoDrawMask ^= PDRAW_LAYERA_ON;\r
1859 if (inp & PBTN_DOWN) PicoDrawMask ^= PDRAW_SPRITES_LOW_ON;\r
1860 if (inp & PBTN_UP) PicoDrawMask ^= PDRAW_SPRITES_HI_ON;\r
1861 if (inp & PBTN_MOK) {\r
1862 PsndOut = NULL; // just in case\r
1863 PicoSkipFrame = 1;\r
1864 PicoFrame();\r
1865 PicoSkipFrame = 0;\r
1866 while (inp & PBTN_MOK) inp = in_menu_wait_any(-1);\r
1867 }\r
1868 break;\r
1869 case 3:\r
1870 if (inp & PBTN_DOWN) spr_offs++;\r
1871 if (inp & PBTN_UP) spr_offs--;\r
1872 if (spr_offs < 0) spr_offs = 0;\r
1873 break;\r
1874 }\r
1875 }\r
1876}\r
1877\r
1878// ------------ main menu ------------\r
1879\r
1880static char *romsel_run(void)\r
1881{\r
1882 char *ret, *sel_name;\r
1883\r
1884 sel_name = malloc(sizeof(rom_fname_loaded));\r
1885 if (sel_name == NULL)\r
1886 return NULL;\r
1887 strcpy(sel_name, rom_fname_loaded);\r
1888\r
1889 ret = menu_loop_romsel(sel_name, sizeof(rom_fname_loaded));\r
1890 free(sel_name);\r
1891 return ret;\r
1892}\r
1893\r
1894static int main_menu_handler(menu_id id, int keys)\r
1895{\r
1896 char *ret_name;\r
1897\r
1898 switch (id)\r
1899 {\r
1900 case MA_MAIN_RESUME_GAME:\r
1901 if (rom_loaded)\r
1902 return 1;\r
1903 break;\r
1904 case MA_MAIN_SAVE_STATE:\r
1905 if (rom_loaded)\r
1906 return menu_loop_savestate(0);\r
1907 break;\r
1908 case MA_MAIN_LOAD_STATE:\r
1909 if (rom_loaded)\r
1910 return menu_loop_savestate(1);\r
1911 break;\r
1912 case MA_MAIN_RESET_GAME:\r
1913 if (rom_loaded) {\r
1914 emu_reset_game();\r
1915 return 1;\r
1916 }\r
1917 break;\r
1918 case MA_MAIN_LOAD_ROM:\r
1919 ret_name = romsel_run();\r
1920 if (ret_name != NULL) {\r
1921 lprintf("selected file: %s\n", ret_name);\r
1922 engineState = PGS_ReloadRom;\r
1923 return 1;\r
1924 }\r
1925 break;\r
1926 case MA_MAIN_CREDITS:\r
1927 draw_menu_credits();\r
1928 in_menu_wait(PBTN_MOK|PBTN_MBACK, 70);\r
1929 break;\r
1930 case MA_MAIN_EXIT:\r
1931 engineState = PGS_Quit;\r
1932 return 1;\r
1933 case MA_MAIN_PATCHES:\r
1934 if (rom_loaded && PicoPatches) {\r
1935 menu_loop_patches();\r
1936 PicoPatchApply();\r
1937 me_update_msg("Patches applied");\r
1938 }\r
1939 break;\r
1940 default:\r
1941 lprintf("%s: something unknown selected\n", __FUNCTION__);\r
1942 break;\r
1943 }\r
1944\r
1945 return 0;\r
1946}\r
1947\r
1948static menu_entry e_menu_main[] =\r
1949{\r
1950 mee_label ("PicoDrive " VERSION),\r
1951 mee_label (""),\r
1952 mee_label (""),\r
1953 mee_label (""),\r
1954 mee_handler_id("Resume game", MA_MAIN_RESUME_GAME, main_menu_handler),\r
1955 mee_handler_id("Save State", MA_MAIN_SAVE_STATE, main_menu_handler),\r
1956 mee_handler_id("Load State", MA_MAIN_LOAD_STATE, main_menu_handler),\r
1957 mee_handler_id("Reset game", MA_MAIN_RESET_GAME, main_menu_handler),\r
1958 mee_handler_id("Load new ROM/ISO", MA_MAIN_LOAD_ROM, main_menu_handler),\r
1959 mee_handler_id("Change options", MA_MAIN_OPTIONS, menu_loop_options),\r
1960 mee_handler_id("Configure controls", MA_MAIN_OPTIONS, menu_loop_keyconfig),\r
1961 mee_handler_id("Credits", MA_MAIN_CREDITS, main_menu_handler),\r
1962 mee_handler_id("Patches / GameGenie",MA_MAIN_PATCHES, main_menu_handler),\r
1963 mee_handler_id("Exit", MA_MAIN_EXIT, main_menu_handler),\r
1964 mee_end,\r
1965};\r
1966\r
1967void menu_loop(void)\r
1968{\r
1969 static int sel = 0;\r
1970\r
1971 me_enable(e_menu_main, MA_MAIN_RESUME_GAME, rom_loaded);\r
1972 me_enable(e_menu_main, MA_MAIN_SAVE_STATE, rom_loaded);\r
1973 me_enable(e_menu_main, MA_MAIN_LOAD_STATE, rom_loaded);\r
1974 me_enable(e_menu_main, MA_MAIN_RESET_GAME, rom_loaded);\r
1975 me_enable(e_menu_main, MA_MAIN_PATCHES, PicoPatches != NULL);\r
1976\r
1977 plat_video_menu_enter(rom_loaded);\r
1978 in_set_blocking(1);\r
1979 me_loop(e_menu_main, &sel);\r
1980\r
1981 if (rom_loaded) {\r
1982 if (engineState == PGS_Menu)\r
1983 engineState = PGS_Running;\r
1984 /* wait until menu, ok, back is released */\r
1985 while (in_menu_wait_any(50) & (PBTN_MENU|PBTN_MOK|PBTN_MBACK));\r
1986 }\r
1987\r
1988 in_set_blocking(0);\r
1989}\r
1990\r
1991// --------- CD tray close menu ----------\r
1992\r
1993static int mh_tray_load_cd(menu_id id, int keys)\r
1994{\r
1995 cd_img_type cd_type;\r
1996 char *ret_name;\r
1997 int ret = -1;\r
1998\r
1999 ret_name = romsel_run();\r
2000 if (ret_name == NULL)\r
2001 return 0;\r
2002\r
2003 cd_type = emu_cd_check(NULL, ret_name);\r
2004 if (cd_type != CIT_NOT_CD)\r
2005 ret = Insert_CD(ret_name, cd_type);\r
2006 if (ret != 0) {\r
2007 me_update_msg("Load failed, invalid CD image?");\r
2008 return 0;\r
2009 }\r
2010\r
2011 engineState = PGS_RestartRun;\r
2012 return 1;\r
2013}\r
2014\r
2015static int mh_tray_nothing(menu_id id, int keys)\r
2016{\r
2017 return 1;\r
2018}\r
2019\r
2020static menu_entry e_menu_tray[] =\r
2021{\r
2022 mee_label ("The unit is about to"),\r
2023 mee_label ("close the CD tray."),\r
2024 mee_label (""),\r
2025 mee_label (""),\r
2026 mee_handler("Load CD image", mh_tray_load_cd),\r
2027 mee_handler("Insert nothing", mh_tray_nothing),\r
2028};\r
2029\r
2030int menu_loop_tray(void)\r
2031{\r
2032 int ret = 1, sel = 0;\r
2033\r
2034 plat_video_menu_enter(rom_loaded);\r
2035\r
2036 in_set_blocking(1);\r
2037 me_loop(e_menu_tray, &sel);\r
2038\r
2039 if (engineState != PGS_RestartRun) {\r
2040 engineState = PGS_RestartRun;\r
2041 ret = 0; /* no CD inserted */\r
2042 }\r
2043\r
2044 while (in_menu_wait_any(50) & (PBTN_MENU|PBTN_MOK|PBTN_MBACK));\r
2045 in_set_blocking(0);\r
2046\r
2047 return ret;\r
2048}\r
2049\r
2050#endif // !UIQ3\r
2051\r
2052void me_update_msg(const char *msg)\r
2053{\r
2054 strncpy(menu_error_msg, msg, sizeof(menu_error_msg));\r
2055 menu_error_msg[sizeof(menu_error_msg) - 1] = 0;\r
2056\r
2057 menu_error_time = plat_get_ticks_ms();\r
2058 lprintf("msg: %s\n", menu_error_msg);\r
2059}\r
2060\r
2061// ------------ util ------------\r
2062\r
2063/* wiz for now, probably extend later */\r
2064void menu_plat_setup(int is_wiz)\r
2065{\r
2066 int i;\r
2067\r
2068 if (!is_wiz)\r
2069 return;\r
2070\r
2071 me_enable(e_menu_adv_options, MA_OPT_ARM940_SOUND, 0);\r
2072 me_enable(e_menu_gfx_options, MA_OPT2_GAMMA, 0);\r
2073 me_enable(e_menu_gfx_options, MA_OPT2_A_SN_GAMMA, 0);\r
2074\r
2075 i = me_id2offset(e_menu_gfx_options, MA_OPT_SCALING);\r
2076 e_menu_gfx_options[i].max = 1; /* only off and sw */\r
2077}\r
2078\r
2079/* TODO: rename */\r
2080void menu_darken_bg(void *dst, int pixels, int darker)\r
2081{\r
2082 unsigned int *screen = dst;\r
2083 pixels /= 2;\r
2084 if (darker)\r
2085 {\r
2086 while (pixels--)\r
2087 {\r
2088 unsigned int p = *screen;\r
2089 *screen++ = ((p&0xf79ef79e)>>1) - ((p&0xc618c618)>>3);\r
2090 }\r
2091 }\r
2092 else\r
2093 {\r
2094 while (pixels--)\r
2095 {\r
2096 unsigned int p = *screen;\r
2097 *screen++ = (p&0xf79ef79e)>>1;\r
2098 }\r
2099 }\r
2100}\r
2101\r
2102/* hidden options for config engine only */\r
2103static menu_entry e_menu_hidden[] =\r
2104{\r
2105 mee_onoff("Accurate sprites", MA_OPT_ACC_SPRITES, PicoOpt, 0x080),\r
2106 mee_end,\r
2107};\r
2108\r
2109static menu_entry *e_menu_table[] =\r
2110{\r
2111 e_menu_options,\r
2112 e_menu_gfx_options,\r
2113 e_menu_adv_options,\r
2114 e_menu_cd_options,\r
2115 e_menu_keyconfig,\r
2116 e_menu_hidden,\r
2117};\r
2118\r
2119static menu_entry *me_list_table = NULL;\r
2120static menu_entry *me_list_i = NULL;\r
2121\r
2122menu_entry *me_list_get_first(void)\r
2123{\r
2124 me_list_table = me_list_i = e_menu_table[0];\r
2125 return me_list_i;\r
2126}\r
2127\r
2128menu_entry *me_list_get_next(void)\r
2129{\r
2130 int i;\r
2131\r
2132 me_list_i++;\r
2133 if (me_list_i->name != NULL)\r
2134 return me_list_i;\r
2135\r
2136 for (i = 0; i < array_size(e_menu_table); i++)\r
2137 if (me_list_table == e_menu_table[i])\r
2138 break;\r
2139\r
2140 if (i + 1 < array_size(e_menu_table))\r
2141 me_list_table = me_list_i = e_menu_table[i + 1];\r
2142 else\r
2143 me_list_table = me_list_i = NULL;\r
2144\r
2145 return me_list_i;\r
2146}\r
2147\r