fix a buffer overflow
[libpicofe.git] / menu.c
1 /*\r
2  * (C) GraÅžvydas "notaz" Ignotas, 2006-2012\r
3  *\r
4  * This work is licensed under the terms of any of these licenses\r
5  * (at your option):\r
6  *  - GNU GPL, version 2 or later.\r
7  *  - GNU LGPL, version 2.1 or later.\r
8  *  - MAME license.\r
9  * See the COPYING file in the top-level directory.\r
10  */\r
11 \r
12 #include <stdio.h>\r
13 #include <string.h>\r
14 #include <stdlib.h>\r
15 #include <stdarg.h>\r
16 #include <time.h>\r
17 #include <locale.h> // savestate date\r
18 \r
19 #include "menu.h"\r
20 #include "fonts.h"\r
21 #include "readpng.h"\r
22 #include "lprintf.h"\r
23 #include "input.h"\r
24 #include "plat.h"\r
25 #include "posix.h"\r
26 \r
27 static char static_buff[64];\r
28 static int  menu_error_time = 0;\r
29 char menu_error_msg[64] = { 0, };\r
30 void *g_menuscreen_ptr;\r
31 void *g_menubg_src_ptr;\r
32 void *g_menubg_ptr;\r
33 \r
34 int g_menuscreen_w;\r
35 int g_menuscreen_h;\r
36 \r
37 int g_autostateld_opt;\r
38 \r
39 static unsigned char *menu_font_data = NULL;\r
40 static int menu_text_color = 0xfffe; // default to white\r
41 static int menu_sel_color = -1; // disabled\r
42 \r
43 /* note: these might become non-constant in future */\r
44 #if MENU_X2\r
45 static const int me_mfont_w = 16, me_mfont_h = 20;\r
46 static const int me_sfont_w = 12, me_sfont_h = 20;\r
47 #else\r
48 static const int me_mfont_w = 8, me_mfont_h = 10;\r
49 static const int me_sfont_w = 6, me_sfont_h = 10;\r
50 #endif\r
51 \r
52 static int g_menu_filter_off;\r
53 static int g_border_style;\r
54 static int border_left, border_right, border_top, border_bottom;\r
55 \r
56 // draws text to current bbp16 screen\r
57 static void text_out16_(int x, int y, const char *text, int color)\r
58 {\r
59         int i, lh, tr, tg, tb, len;\r
60         unsigned short *dest = (unsigned short *)g_menuscreen_ptr + x + y * g_menuscreen_w;\r
61         tr = (color & 0xf800) >> 8;\r
62         tg = (color & 0x07e0) >> 3;\r
63         tb = (color & 0x001f) << 3;\r
64 \r
65         if (text == (void *)1)\r
66         {\r
67                 // selector symbol\r
68                 text = "";\r
69                 len = 1;\r
70         }\r
71         else\r
72         {\r
73                 const char *p;\r
74                 for (p = text; *p != 0 && *p != '\n'; p++)\r
75                         ;\r
76                 len = p - text;\r
77         }\r
78 \r
79         lh = me_mfont_h;\r
80         if (y + lh > g_menuscreen_h)\r
81                 lh = g_menuscreen_h - y;\r
82 \r
83         for (i = 0; i < len; i++)\r
84         {\r
85                 unsigned char  *src = menu_font_data + (unsigned int)text[i] * me_mfont_w * me_mfont_h / 2;\r
86                 unsigned short *dst = dest;\r
87                 int u, l;\r
88 \r
89                 for (l = 0; l < lh; l++, dst += g_menuscreen_w - me_mfont_w)\r
90                 {\r
91                         for (u = me_mfont_w / 2; u > 0; u--, src++)\r
92                         {\r
93                                 int c, r, g, b;\r
94                                 c = *src >> 4;\r
95                                 r = (*dst & 0xf800) >> 8;\r
96                                 g = (*dst & 0x07e0) >> 3;\r
97                                 b = (*dst & 0x001f) << 3;\r
98                                 r = (c^0xf)*r/15 + c*tr/15;\r
99                                 g = (c^0xf)*g/15 + c*tg/15;\r
100                                 b = (c^0xf)*b/15 + c*tb/15;\r
101                                 *dst++ = ((r<<8)&0xf800) | ((g<<3)&0x07e0) | (b>>3);\r
102                                 c = *src & 0xf;\r
103                                 r = (*dst & 0xf800) >> 8;\r
104                                 g = (*dst & 0x07e0) >> 3;\r
105                                 b = (*dst & 0x001f) << 3;\r
106                                 r = (c^0xf)*r/15 + c*tr/15;\r
107                                 g = (c^0xf)*g/15 + c*tg/15;\r
108                                 b = (c^0xf)*b/15 + c*tb/15;\r
109                                 *dst++ = ((r<<8)&0xf800) | ((g<<3)&0x07e0) | (b>>3);\r
110                         }\r
111                 }\r
112                 dest += me_mfont_w;\r
113         }\r
114 \r
115         if (x < border_left)\r
116                 border_left = x;\r
117         if (x + i * me_mfont_w > border_right)\r
118                 border_right = x + i * me_mfont_w;\r
119         if (y < border_top)\r
120                 border_top = y;\r
121         if (y + me_mfont_h > border_bottom)\r
122                 border_bottom = y + me_mfont_h;\r
123 }\r
124 \r
125 void text_out16(int x, int y, const char *texto, ...)\r
126 {\r
127         va_list args;\r
128         char    buffer[256];\r
129         int     maxw = (g_menuscreen_w - x) / me_mfont_w;\r
130 \r
131         if (maxw < 0)\r
132                 return;\r
133 \r
134         va_start(args, texto);\r
135         vsnprintf(buffer, sizeof(buffer), texto, args);\r
136         va_end(args);\r
137 \r
138         if (maxw > sizeof(buffer) - 1)\r
139                 maxw = sizeof(buffer) - 1;\r
140         buffer[maxw] = 0;\r
141 \r
142         text_out16_(x,y,buffer,menu_text_color);\r
143 }\r
144 \r
145 /* draws in 6x8 font, might multiply size by integer */\r
146 static void smalltext_out16_(int x, int y, const char *texto, int color)\r
147 {\r
148         unsigned char  *src;\r
149         unsigned short *dst;\r
150         int multiplier = me_sfont_w / 6;\r
151         int i;\r
152 \r
153         for (i = 0;; i++, x += me_sfont_w)\r
154         {\r
155                 unsigned char c = (unsigned char) texto[i];\r
156                 int h = 8;\r
157 \r
158                 if (!c || c == '\n')\r
159                         break;\r
160 \r
161                 src = fontdata6x8[c];\r
162                 dst = (unsigned short *)g_menuscreen_ptr + x + y * g_menuscreen_w;\r
163 \r
164                 while (h--)\r
165                 {\r
166                         int m, w2, h2;\r
167                         for (h2 = multiplier; h2 > 0; h2--)\r
168                         {\r
169                                 for (m = 0x20; m; m >>= 1) {\r
170                                         if (*src & m)\r
171                                                 for (w2 = multiplier; w2 > 0; w2--)\r
172                                                         *dst++ = color;\r
173                                         else\r
174                                                 dst += multiplier;\r
175                                 }\r
176 \r
177                                 dst += g_menuscreen_w - me_sfont_w;\r
178                         }\r
179                         src++;\r
180                 }\r
181         }\r
182 }\r
183 \r
184 static void smalltext_out16(int x, int y, const char *texto, int color)\r
185 {\r
186         char buffer[128];\r
187         int maxw = (g_menuscreen_w - x) / me_sfont_w;\r
188 \r
189         if (maxw < 0)\r
190                 return;\r
191 \r
192         strncpy(buffer, texto, sizeof(buffer));\r
193         if (maxw > sizeof(buffer) - 1)\r
194                 maxw = sizeof(buffer) - 1;\r
195         buffer[maxw] = 0;\r
196 \r
197         smalltext_out16_(x, y, buffer, color);\r
198 }\r
199 \r
200 static void menu_draw_selection(int x, int y, int w)\r
201 {\r
202         int i, h;\r
203         unsigned short *dst, *dest;\r
204 \r
205         text_out16_(x, y, (void *)1, (menu_sel_color < 0) ? menu_text_color : menu_sel_color);\r
206 \r
207         if (menu_sel_color < 0) return; // no selection hilight\r
208 \r
209         if (y > 0) y--;\r
210         dest = (unsigned short *)g_menuscreen_ptr + x + y * g_menuscreen_w + me_mfont_w * 2 - 2;\r
211         for (h = me_mfont_h + 1; h > 0; h--)\r
212         {\r
213                 dst = dest;\r
214                 for (i = w - (me_mfont_w * 2 - 2); i > 0; i--)\r
215                         *dst++ = menu_sel_color;\r
216                 dest += g_menuscreen_w;\r
217         }\r
218 }\r
219 \r
220 static int parse_hex_color(char *buff)\r
221 {\r
222         char *endp = buff;\r
223         int t = (int) strtoul(buff, &endp, 16);\r
224         if (endp != buff)\r
225 #ifdef PSP\r
226                 return ((t<<8)&0xf800) | ((t>>5)&0x07e0) | ((t>>19)&0x1f);\r
227 #else\r
228                 return ((t>>8)&0xf800) | ((t>>5)&0x07e0) | ((t>>3)&0x1f);\r
229 #endif\r
230         return -1;\r
231 }\r
232 \r
233 static char tolower_simple(char c)\r
234 {\r
235         if ('A' <= c && c <= 'Z')\r
236                 c = c - 'A' + 'a';\r
237         return c;\r
238 }\r
239 \r
240 void menu_init_base(void)\r
241 {\r
242         int i, c, l, pos;\r
243         unsigned char *fd, *fds;\r
244         char buff[256];\r
245         FILE *f;\r
246 \r
247         if (menu_font_data != NULL)\r
248                 free(menu_font_data);\r
249 \r
250         menu_font_data = calloc((MENU_X2 ? 256 * 320 : 128 * 160) / 2, 1);\r
251         if (menu_font_data == NULL)\r
252                 return;\r
253 \r
254         // generate default 8x10 font from fontdata8x8\r
255         for (c = 0, fd = menu_font_data; c < 128; c++)\r
256         {\r
257                 for (l = 0; l < 8; l++)\r
258                 {\r
259                         unsigned char fd8x8 = fontdata8x8[c*8+l];\r
260                         if (fd8x8&0x80) *fd  = 0xf0;\r
261                         if (fd8x8&0x40) *fd |= 0x0f; fd++;\r
262                         if (fd8x8&0x20) *fd  = 0xf0;\r
263                         if (fd8x8&0x10) *fd |= 0x0f; fd++;\r
264                         if (fd8x8&0x08) *fd  = 0xf0;\r
265                         if (fd8x8&0x04) *fd |= 0x0f; fd++;\r
266                         if (fd8x8&0x02) *fd  = 0xf0;\r
267                         if (fd8x8&0x01) *fd |= 0x0f; fd++;\r
268                 }\r
269                 fd += 8*2/2; // 2 empty lines\r
270         }\r
271 \r
272         if (MENU_X2) {\r
273                 // expand default font\r
274                 fds = menu_font_data + 128 * 160 / 2 - 4;\r
275                 fd  = menu_font_data + 256 * 320 / 2 - 1;\r
276                 for (c = 255; c >= 0; c--)\r
277                 {\r
278                         for (l = 9; l >= 0; l--, fds -= 4)\r
279                         {\r
280                                 for (i = 3; i >= 0; i--) {\r
281                                         int px = fds[i] & 0x0f;\r
282                                         *fd-- = px | (px << 4);\r
283                                         px = (fds[i] >> 4) & 0x0f;\r
284                                         *fd-- = px | (px << 4);\r
285                                 }\r
286                                 for (i = 3; i >= 0; i--) {\r
287                                         int px = fds[i] & 0x0f;\r
288                                         *fd-- = px | (px << 4);\r
289                                         px = (fds[i] >> 4) & 0x0f;\r
290                                         *fd-- = px | (px << 4);\r
291                                 }\r
292                         }\r
293                 }\r
294         }\r
295 \r
296         // load custom font and selector (stored as 1st symbol in font table)\r
297         pos = plat_get_skin_dir(buff, sizeof(buff));\r
298         strcpy(buff + pos, "font.png");\r
299         readpng(menu_font_data, buff, READPNG_FONT,\r
300                 MENU_X2 ? 256 : 128, MENU_X2 ? 320 : 160);\r
301         // default selector symbol is '>'\r
302         memcpy(menu_font_data, menu_font_data + ((int)'>') * me_mfont_w * me_mfont_h / 2,\r
303                 me_mfont_w * me_mfont_h / 2);\r
304         strcpy(buff + pos, "selector.png");\r
305         readpng(menu_font_data, buff, READPNG_SELECTOR, me_mfont_w, me_mfont_h);\r
306 \r
307         // load custom colors\r
308         strcpy(buff + pos, "skin.txt");\r
309         f = fopen(buff, "r");\r
310         if (f != NULL)\r
311         {\r
312                 lprintf("found skin.txt\n");\r
313                 while (!feof(f))\r
314                 {\r
315                         if (fgets(buff, sizeof(buff), f) == NULL)\r
316                                 break;\r
317                         if (buff[0] == '#'  || buff[0] == '/')  continue; // comment\r
318                         if (buff[0] == '\r' || buff[0] == '\n') continue; // empty line\r
319                         if (strncmp(buff, "text_color=", 11) == 0)\r
320                         {\r
321                                 int tmp = parse_hex_color(buff+11);\r
322                                 if (tmp >= 0) menu_text_color = tmp;\r
323                                 else lprintf("skin.txt: parse error for text_color\n");\r
324                         }\r
325                         else if (strncmp(buff, "selection_color=", 16) == 0)\r
326                         {\r
327                                 int tmp = parse_hex_color(buff+16);\r
328                                 if (tmp >= 0) menu_sel_color = tmp;\r
329                                 else lprintf("skin.txt: parse error for selection_color\n");\r
330                         }\r
331                         else\r
332                                 lprintf("skin.txt: parse error: %s\n", buff);\r
333                 }\r
334                 fclose(f);\r
335         }\r
336 \r
337         // use user's locale for savestate date display\r
338         setlocale(LC_TIME, "");\r
339 }\r
340 \r
341 static void menu_darken_bg(void *dst, void *src, int pixels, int darker)\r
342 {\r
343         unsigned int *dest = dst;\r
344         unsigned int *sorc = src;\r
345         pixels /= 2;\r
346         if (darker)\r
347         {\r
348                 while (pixels--)\r
349                 {\r
350                         unsigned int p = *sorc++;\r
351                         *dest++ = ((p&0xf79ef79e)>>1) - ((p&0xc618c618)>>3);\r
352                 }\r
353         }\r
354         else\r
355         {\r
356                 while (pixels--)\r
357                 {\r
358                         unsigned int p = *sorc++;\r
359                         *dest++ = (p&0xf79ef79e)>>1;\r
360                 }\r
361         }\r
362 }\r
363 \r
364 static void menu_darken_text_bg(void)\r
365 {\r
366         int x, y, xmin, xmax, ymax, ls;\r
367         unsigned short *screen = g_menuscreen_ptr;\r
368 \r
369         xmin = border_left - 3;\r
370         if (xmin < 0)\r
371                 xmin = 0;\r
372         xmax = border_right + 2;\r
373         if (xmax > g_menuscreen_w - 1)\r
374                 xmax = g_menuscreen_w - 1;\r
375 \r
376         y = border_top - 3;\r
377         if (y < 0)\r
378                 y = 0;\r
379         ymax = border_bottom + 2;\r
380         if (ymax > g_menuscreen_h - 1)\r
381                 ymax = g_menuscreen_h - 1;\r
382 \r
383         for (x = xmin; x <= xmax; x++)\r
384                 screen[y * g_menuscreen_w + x] = 0xa514;\r
385         for (y++; y < ymax; y++)\r
386         {\r
387                 ls = y * g_menuscreen_w;\r
388                 screen[ls + xmin] = 0xffff;\r
389                 for (x = xmin + 1; x < xmax; x++)\r
390                 {\r
391                         unsigned int p = screen[ls + x];\r
392                         if (p != menu_text_color)\r
393                                 screen[ls + x] = ((p&0xf79e)>>1) - ((p&0xc618)>>3);\r
394                 }\r
395                 screen[ls + xmax] = 0xffff;\r
396         }\r
397         ls = y * g_menuscreen_w;\r
398         for (x = xmin; x <= xmax; x++)\r
399                 screen[ls + x] = 0xffff;\r
400 }\r
401 \r
402 static int borders_pending;\r
403 \r
404 static void menu_reset_borders(void)\r
405 {\r
406         border_left = g_menuscreen_w;\r
407         border_right = 0;\r
408         border_top = g_menuscreen_h;\r
409         border_bottom = 0;\r
410 }\r
411 \r
412 static void menu_draw_begin(int need_bg, int no_borders)\r
413 {\r
414         plat_video_menu_begin();\r
415 \r
416         menu_reset_borders();\r
417         borders_pending = g_border_style && !no_borders;\r
418 \r
419         if (need_bg) {\r
420                 if (g_border_style && no_borders) {\r
421                         menu_darken_bg(g_menuscreen_ptr, g_menubg_ptr,\r
422                                 g_menuscreen_w * g_menuscreen_h, 1);\r
423                 }\r
424                 else {\r
425                         memcpy(g_menuscreen_ptr, g_menubg_ptr,\r
426                                 g_menuscreen_w * g_menuscreen_h * 2);\r
427                 }\r
428         }\r
429 }\r
430 \r
431 static void menu_draw_end(void)\r
432 {\r
433         if (borders_pending)\r
434                 menu_darken_text_bg();\r
435         plat_video_menu_end();\r
436 }\r
437 \r
438 static void menu_separation(void)\r
439 {\r
440         if (borders_pending) {\r
441                 menu_darken_text_bg();\r
442                 menu_reset_borders();\r
443         }\r
444 }\r
445 \r
446 static int me_id2offset(const menu_entry *ent, menu_id id)\r
447 {\r
448         int i;\r
449         for (i = 0; ent->name; ent++, i++)\r
450                 if (ent->id == id) return i;\r
451 \r
452         lprintf("%s: id %i not found\n", __FUNCTION__, id);\r
453         return 0;\r
454 }\r
455 \r
456 static void me_enable(menu_entry *entries, menu_id id, int enable)\r
457 {\r
458         int i = me_id2offset(entries, id);\r
459         entries[i].enabled = enable;\r
460 }\r
461 \r
462 static int me_count(const menu_entry *ent)\r
463 {\r
464         int ret;\r
465 \r
466         for (ret = 0; ent->name; ent++, ret++)\r
467                 ;\r
468 \r
469         return ret;\r
470 }\r
471 \r
472 static unsigned int me_read_onoff(const menu_entry *ent)\r
473 {\r
474         // guess var size based on mask to avoid reading too much\r
475         if (ent->mask & 0xffff0000)\r
476                 return *(unsigned int *)ent->var & ent->mask;\r
477         else if (ent->mask & 0xff00)\r
478                 return *(unsigned short *)ent->var & ent->mask;\r
479         else\r
480                 return *(unsigned char *)ent->var & ent->mask;\r
481 }\r
482 \r
483 static void me_toggle_onoff(menu_entry *ent)\r
484 {\r
485         // guess var size based on mask to avoid reading too much\r
486         if (ent->mask & 0xffff0000)\r
487                 *(unsigned int *)ent->var ^= ent->mask;\r
488         else if (ent->mask & 0xff00)\r
489                 *(unsigned short *)ent->var ^= ent->mask;\r
490         else\r
491                 *(unsigned char *)ent->var ^= ent->mask;\r
492 }\r
493 \r
494 static void me_draw(const menu_entry *entries, int sel, void (*draw_more)(void))\r
495 {\r
496         const menu_entry *ent, *ent_sel = entries;\r
497         int x, y, w = 0, h = 0;\r
498         int offs, col2_offs = 27 * me_mfont_w;\r
499         int vi_sel_ln = 0;\r
500         const char *name;\r
501         int i, n;\r
502 \r
503         /* calculate size of menu rect */\r
504         for (ent = entries, i = n = 0; ent->name; ent++, i++)\r
505         {\r
506                 int wt;\r
507 \r
508                 if (!ent->enabled)\r
509                         continue;\r
510 \r
511                 if (i == sel) {\r
512                         ent_sel = ent;\r
513                         vi_sel_ln = n;\r
514                 }\r
515 \r
516                 name = NULL;\r
517                 wt = strlen(ent->name) * me_mfont_w;\r
518                 if (wt == 0 && ent->generate_name)\r
519                         name = ent->generate_name(ent->id, &offs);\r
520                 if (name != NULL)\r
521                         wt = strlen(name) * me_mfont_w;\r
522 \r
523                 if (ent->beh != MB_NONE)\r
524                 {\r
525                         if (wt > col2_offs)\r
526                                 col2_offs = wt + me_mfont_w;\r
527                         wt = col2_offs;\r
528 \r
529                         switch (ent->beh) {\r
530                         case MB_NONE:\r
531                                 break;\r
532                         case MB_OPT_ONOFF:\r
533                         case MB_OPT_RANGE:\r
534                                 wt += me_mfont_w * 3;\r
535                                 break;\r
536                         case MB_OPT_CUSTOM:\r
537                         case MB_OPT_CUSTONOFF:\r
538                         case MB_OPT_CUSTRANGE:\r
539                                 name = NULL;\r
540                                 offs = 0;\r
541                                 if (ent->generate_name != NULL)\r
542                                         name = ent->generate_name(ent->id, &offs);\r
543                                 if (name != NULL)\r
544                                         wt += (strlen(name) + offs) * me_mfont_w;\r
545                                 break;\r
546                         case MB_OPT_ENUM:\r
547                                 wt += 10 * me_mfont_w;\r
548                                 break;\r
549                         }\r
550                 }\r
551 \r
552                 if (wt > w)\r
553                         w = wt;\r
554                 n++;\r
555         }\r
556         h = n * me_mfont_h;\r
557         w += me_mfont_w * 2; /* selector */\r
558 \r
559         if (w > g_menuscreen_w) {\r
560                 lprintf("width %d > %d\n", w, g_menuscreen_w);\r
561                 w = g_menuscreen_w;\r
562         }\r
563         if (h > g_menuscreen_h) {\r
564                 lprintf("height %d > %d\n", w, g_menuscreen_h);\r
565                 h = g_menuscreen_h;\r
566         }\r
567 \r
568         x = g_menuscreen_w / 2 - w / 2;\r
569         y = g_menuscreen_h / 2 - h / 2;\r
570 #ifdef MENU_ALIGN_LEFT\r
571         if (x > 12) x = 12;\r
572 #endif\r
573 \r
574         /* draw */\r
575         menu_draw_begin(1, 0);\r
576         menu_draw_selection(x, y + vi_sel_ln * me_mfont_h, w);\r
577         x += me_mfont_w * 2;\r
578 \r
579         for (ent = entries; ent->name; ent++)\r
580         {\r
581                 const char **names;\r
582                 int len, leftname_end = 0;\r
583 \r
584                 if (!ent->enabled)\r
585                         continue;\r
586 \r
587                 name = ent->name;\r
588                 if (strlen(name) == 0) {\r
589                         if (ent->generate_name)\r
590                                 name = ent->generate_name(ent->id, &offs);\r
591                 }\r
592                 if (name != NULL) {\r
593                         text_out16(x, y, name);\r
594                         leftname_end = x + (strlen(name) + 1) * me_mfont_w;\r
595                 }\r
596 \r
597                 switch (ent->beh) {\r
598                 case MB_NONE:\r
599                         break;\r
600                 case MB_OPT_ONOFF:\r
601                         text_out16(x + col2_offs, y, me_read_onoff(ent) ? "ON" : "OFF");\r
602                         break;\r
603                 case MB_OPT_RANGE:\r
604                         text_out16(x + col2_offs, y, "%i", *(int *)ent->var);\r
605                         break;\r
606                 case MB_OPT_CUSTOM:\r
607                 case MB_OPT_CUSTONOFF:\r
608                 case MB_OPT_CUSTRANGE:\r
609                         name = NULL;\r
610                         offs = 0;\r
611                         if (ent->generate_name)\r
612                                 name = ent->generate_name(ent->id, &offs);\r
613                         if (name != NULL)\r
614                                 text_out16(x + col2_offs + offs * me_mfont_w, y, "%s", name);\r
615                         break;\r
616                 case MB_OPT_ENUM:\r
617                         names = (const char **)ent->data;\r
618                         for (i = 0; names[i] != NULL; i++) {\r
619                                 offs = x + col2_offs;\r
620                                 len = strlen(names[i]);\r
621                                 if (len > 10)\r
622                                         offs += (10 - len - 2) * me_mfont_w;\r
623                                 if (offs < leftname_end)\r
624                                         offs = leftname_end;\r
625                                 if (i == *(unsigned char *)ent->var) {\r
626                                         text_out16(offs, y, "%s", names[i]);\r
627                                         break;\r
628                                 }\r
629                         }\r
630                         break;\r
631                 }\r
632 \r
633                 y += me_mfont_h;\r
634         }\r
635 \r
636         menu_separation();\r
637 \r
638         /* display help or message if we have one */\r
639         h = (g_menuscreen_h - h) / 2; // bottom area height\r
640         if (menu_error_msg[0] != 0) {\r
641                 if (h >= me_mfont_h + 4)\r
642                         text_out16(5, g_menuscreen_h - me_mfont_h - 4, menu_error_msg);\r
643                 else\r
644                         lprintf("menu msg doesn't fit!\n");\r
645 \r
646                 if (plat_get_ticks_ms() - menu_error_time > 2048)\r
647                         menu_error_msg[0] = 0;\r
648         }\r
649         else if (ent_sel->help != NULL) {\r
650                 const char *tmp = ent_sel->help;\r
651                 int l;\r
652                 for (l = 0; tmp != NULL && *tmp != 0; l++)\r
653                         tmp = strchr(tmp + 1, '\n');\r
654                 if (h >= l * me_sfont_h + 4)\r
655                         for (tmp = ent_sel->help; l > 0; l--, tmp = strchr(tmp, '\n') + 1)\r
656                                 smalltext_out16(5, g_menuscreen_h - (l * me_sfont_h + 4), tmp, 0xffff);\r
657         }\r
658 \r
659         menu_separation();\r
660 \r
661         if (draw_more != NULL)\r
662                 draw_more();\r
663 \r
664         menu_draw_end();\r
665 }\r
666 \r
667 static int me_process(menu_entry *entry, int is_next, int is_lr)\r
668 {\r
669         const char **names;\r
670         int c;\r
671         switch (entry->beh)\r
672         {\r
673                 case MB_OPT_ONOFF:\r
674                 case MB_OPT_CUSTONOFF:\r
675                         me_toggle_onoff(entry);\r
676                         return 1;\r
677                 case MB_OPT_RANGE:\r
678                 case MB_OPT_CUSTRANGE:\r
679                         c = is_lr ? 10 : 1;\r
680                         *(int *)entry->var += is_next ? c : -c;\r
681                         if (*(int *)entry->var < (int)entry->min)\r
682                                 *(int *)entry->var = (int)entry->max;\r
683                         if (*(int *)entry->var > (int)entry->max)\r
684                                 *(int *)entry->var = (int)entry->min;\r
685                         return 1;\r
686                 case MB_OPT_ENUM:\r
687                         names = (const char **)entry->data;\r
688                         for (c = 0; names[c] != NULL; c++)\r
689                                 ;\r
690                         *(signed char *)entry->var += is_next ? 1 : -1;\r
691                         if (*(signed char *)entry->var < 0)\r
692                                 *(signed char *)entry->var = 0;\r
693                         if (*(signed char *)entry->var >= c)\r
694                                 *(signed char *)entry->var = c - 1;\r
695                         return 1;\r
696                 default:\r
697                         return 0;\r
698         }\r
699 }\r
700 \r
701 static void debug_menu_loop(void);\r
702 \r
703 static int me_loop_d(menu_entry *menu, int *menu_sel, void (*draw_prep)(void), void (*draw_more)(void))\r
704 {\r
705         int ret = 0, inp, sel = *menu_sel, menu_sel_max;\r
706 \r
707         menu_sel_max = me_count(menu) - 1;\r
708         if (menu_sel_max < 0) {\r
709                 lprintf("no enabled menu entries\n");\r
710                 return 0;\r
711         }\r
712 \r
713         while ((!menu[sel].enabled || !menu[sel].selectable) && sel < menu_sel_max)\r
714                 sel++;\r
715 \r
716         /* make sure action buttons are not pressed on entering menu */\r
717         me_draw(menu, sel, NULL);\r
718         while (in_menu_wait_any(NULL, 50) & (PBTN_MOK|PBTN_MBACK|PBTN_MENU));\r
719 \r
720         for (;;)\r
721         {\r
722                 if (draw_prep != NULL)\r
723                         draw_prep();\r
724 \r
725                 me_draw(menu, sel, draw_more);\r
726                 inp = in_menu_wait(PBTN_UP|PBTN_DOWN|PBTN_LEFT|PBTN_RIGHT|\r
727                         PBTN_MOK|PBTN_MBACK|PBTN_MENU|PBTN_L|PBTN_R, NULL, 70);\r
728                 if (inp & (PBTN_MENU|PBTN_MBACK))\r
729                         break;\r
730 \r
731                 if (inp & PBTN_UP  ) {\r
732                         do {\r
733                                 sel--;\r
734                                 if (sel < 0)\r
735                                         sel = menu_sel_max;\r
736                         }\r
737                         while (!menu[sel].enabled || !menu[sel].selectable);\r
738                 }\r
739                 if (inp & PBTN_DOWN) {\r
740                         do {\r
741                                 sel++;\r
742                                 if (sel > menu_sel_max)\r
743                                         sel = 0;\r
744                         }\r
745                         while (!menu[sel].enabled || !menu[sel].selectable);\r
746                 }\r
747 \r
748                 /* a bit hacky but oh well */\r
749                 if ((inp & (PBTN_L|PBTN_R)) == (PBTN_L|PBTN_R))\r
750                         debug_menu_loop();\r
751 \r
752                 if (inp & (PBTN_LEFT|PBTN_RIGHT|PBTN_L|PBTN_R)) { /* multi choice */\r
753                         if (me_process(&menu[sel], (inp & (PBTN_RIGHT|PBTN_R)) ? 1 : 0,\r
754                                                 inp & (PBTN_L|PBTN_R)))\r
755                                 continue;\r
756                 }\r
757 \r
758                 if (inp & (PBTN_MOK|PBTN_LEFT|PBTN_RIGHT|PBTN_L|PBTN_R))\r
759                 {\r
760                         /* require PBTN_MOK for MB_NONE */\r
761                         if (menu[sel].handler != NULL && (menu[sel].beh != MB_NONE || (inp & PBTN_MOK))) {\r
762                                 ret = menu[sel].handler(menu[sel].id, inp);\r
763                                 if (ret) break;\r
764                                 menu_sel_max = me_count(menu) - 1; /* might change, so update */\r
765                         }\r
766                 }\r
767         }\r
768         *menu_sel = sel;\r
769 \r
770         return ret;\r
771 }\r
772 \r
773 static int me_loop(menu_entry *menu, int *menu_sel)\r
774 {\r
775         return me_loop_d(menu, menu_sel, NULL, NULL);\r
776 }\r
777 \r
778 /* ***************************************** */\r
779 \r
780 static void draw_menu_message(const char *msg, void (*draw_more)(void))\r
781 {\r
782         int x, y, h, w, wt;\r
783         const char *p;\r
784 \r
785         p = msg;\r
786         for (h = 1, w = 0; *p != 0; h++) {\r
787                 for (wt = 0; *p != 0 && *p != '\n'; p++)\r
788                         wt++;\r
789 \r
790                 if (wt > w)\r
791                         w = wt;\r
792                 if (*p == 0)\r
793                         break;\r
794                 p++;\r
795         }\r
796 \r
797         x = g_menuscreen_w / 2 - w * me_mfont_w / 2;\r
798         y = g_menuscreen_h / 2 - h * me_mfont_h / 2;\r
799         if (x < 0) x = 0;\r
800         if (y < 0) y = 0;\r
801 \r
802         menu_draw_begin(1, 0);\r
803 \r
804         for (p = msg; *p != 0 && y <= g_menuscreen_h - me_mfont_h; y += me_mfont_h) {\r
805                 text_out16(x, y, p);\r
806 \r
807                 for (; *p != 0 && *p != '\n'; p++)\r
808                         ;\r
809                 if (*p != 0)\r
810                         p++;\r
811         }\r
812 \r
813         menu_separation();\r
814 \r
815         if (draw_more != NULL)\r
816                 draw_more();\r
817 \r
818         menu_draw_end();\r
819 }\r
820 \r
821 // -------------- del confirm ---------------\r
822 \r
823 static void do_delete(const char *fpath, const char *fname)\r
824 {\r
825         int len, mid, inp;\r
826         const char *nm;\r
827         char tmp[64];\r
828 \r
829         menu_draw_begin(1, 0);\r
830 \r
831         len = strlen(fname);\r
832         if (len > g_menuscreen_w / me_sfont_w)\r
833                 len = g_menuscreen_w / me_sfont_w;\r
834 \r
835         mid = g_menuscreen_w / 2;\r
836         text_out16(mid - me_mfont_w * 15 / 2,  8 * me_mfont_h, "About to delete");\r
837         smalltext_out16(mid - len * me_sfont_w / 2, 9 * me_mfont_h + 5, fname, 0xbdff);\r
838         text_out16(mid - me_mfont_w * 13 / 2, 11 * me_mfont_h, "Are you sure?");\r
839 \r
840         nm = in_get_key_name(-1, -PBTN_MA3);\r
841         snprintf(tmp, sizeof(tmp), "(%s - confirm, ", nm);\r
842         len = strlen(tmp);\r
843         nm = in_get_key_name(-1, -PBTN_MBACK);\r
844         snprintf(tmp + len, sizeof(tmp) - len, "%s - cancel)", nm);\r
845         len = strlen(tmp);\r
846 \r
847         text_out16(mid - me_mfont_w * len / 2, 12 * me_mfont_h, tmp);\r
848         menu_draw_end();\r
849 \r
850         while (in_menu_wait_any(NULL, 50) & (PBTN_MENU|PBTN_MA2));\r
851         inp = in_menu_wait(PBTN_MA3|PBTN_MBACK, NULL, 100);\r
852         if (inp & PBTN_MA3)\r
853                 remove(fpath);\r
854 }\r
855 \r
856 // -------------- ROM selector --------------\r
857 \r
858 static void draw_dirlist(char *curdir, struct dirent **namelist,\r
859         int n, int sel, int show_help)\r
860 {\r
861         int max_cnt, start, i, x, pos;\r
862         void *darken_ptr;\r
863         char buff[64];\r
864 \r
865         max_cnt = g_menuscreen_h / me_sfont_h;\r
866         start = max_cnt / 2 - sel;\r
867         n--; // exclude current dir (".")\r
868 \r
869         menu_draw_begin(1, 1);\r
870 \r
871 //      if (!rom_loaded)\r
872 //              menu_darken_bg(gp2x_screen, 320*240, 0);\r
873 \r
874         darken_ptr = (short *)g_menuscreen_ptr + g_menuscreen_w * max_cnt/2 * me_sfont_h;\r
875         menu_darken_bg(darken_ptr, darken_ptr, g_menuscreen_w * me_sfont_h * 8 / 10, 0);\r
876 \r
877         x = 5 + me_mfont_w + 1;\r
878         if (start - 2 >= 0)\r
879                 smalltext_out16(14, (start - 2) * me_sfont_h, curdir, 0xffff);\r
880         for (i = 0; i < n; i++) {\r
881                 pos = start + i;\r
882                 if (pos < 0)  continue;\r
883                 if (pos >= max_cnt) break;\r
884                 if (namelist[i+1]->d_type == DT_DIR) {\r
885                         smalltext_out16(x, pos * me_sfont_h, "/", 0xfff6);\r
886                         smalltext_out16(x + me_sfont_w, pos * me_sfont_h, namelist[i+1]->d_name, 0xfff6);\r
887                 } else {\r
888                         unsigned short color = fname2color(namelist[i+1]->d_name);\r
889                         smalltext_out16(x, pos * me_sfont_h, namelist[i+1]->d_name, color);\r
890                 }\r
891         }\r
892         smalltext_out16(5, max_cnt/2 * me_sfont_h, ">", 0xffff);\r
893 \r
894         if (show_help) {\r
895                 darken_ptr = (short *)g_menuscreen_ptr\r
896                         + g_menuscreen_w * (g_menuscreen_h - me_sfont_h * 5 / 2);\r
897                 menu_darken_bg(darken_ptr, darken_ptr,\r
898                         g_menuscreen_w * (me_sfont_h * 5 / 2), 1);\r
899 \r
900                 snprintf(buff, sizeof(buff), "%s - select, %s - back",\r
901                         in_get_key_name(-1, -PBTN_MOK), in_get_key_name(-1, -PBTN_MBACK));\r
902                 smalltext_out16(x, g_menuscreen_h - me_sfont_h * 3 - 2, buff, 0xe78c);\r
903 \r
904                 snprintf(buff, sizeof(buff), g_menu_filter_off ?\r
905                          "%s - hide unknown files" : "%s - show all files",\r
906                         in_get_key_name(-1, -PBTN_MA3));\r
907                 smalltext_out16(x, g_menuscreen_h - me_sfont_h * 2 - 2, buff, 0xe78c);\r
908 \r
909                 snprintf(buff, sizeof(buff), g_autostateld_opt ?\r
910                          "%s - autoload save is ON" : "%s - autoload save is OFF",\r
911                         in_get_key_name(-1, -PBTN_MA2));\r
912                 smalltext_out16(x, g_menuscreen_h - me_sfont_h * 1 - 2, buff, 0xe78c);\r
913         }\r
914 \r
915         menu_draw_end();\r
916 }\r
917 \r
918 static int scandir_cmp(const void *p1, const void *p2)\r
919 {\r
920         const struct dirent **d1 = (const struct dirent **)p1;\r
921         const struct dirent **d2 = (const struct dirent **)p2;\r
922         if ((*d1)->d_type == (*d2)->d_type)\r
923                 return alphasort(d1, d2);\r
924         if ((*d1)->d_type == DT_DIR)\r
925                 return -1; // put before\r
926         if ((*d2)->d_type == DT_DIR)\r
927                 return  1;\r
928 \r
929         return alphasort(d1, d2);\r
930 }\r
931 \r
932 static const char **filter_exts_internal;\r
933 \r
934 static int scandir_filter(const struct dirent *ent)\r
935 {\r
936         const char **filter = filter_exts_internal;\r
937         const char *ext;\r
938         int i;\r
939 \r
940         if (ent == NULL || ent->d_name == NULL)\r
941                 return 0;\r
942 \r
943         if (ent->d_type == DT_DIR)\r
944                 return 1;\r
945 \r
946         ext = strrchr(ent->d_name, '.');\r
947         if (ext == NULL)\r
948                 return 0;\r
949 \r
950         ext++;\r
951         for (i = 0; filter[i] != NULL; i++)\r
952                 if (strcasecmp(ext, filter[i]) == 0)\r
953                         return 1;\r
954 \r
955         return 0;\r
956 }\r
957 \r
958 static int dirent_seek_char(struct dirent **namelist, int len, int sel, char c)\r
959 {\r
960         int i;\r
961 \r
962         sel++;\r
963         for (i = sel + 1; ; i++) {\r
964                 if (i >= len)\r
965                         i = 1;\r
966                 if (i == sel)\r
967                         break;\r
968 \r
969                 if (tolower_simple(namelist[i]->d_name[0]) == c)\r
970                         break;\r
971         }\r
972 \r
973         return i - 1;\r
974 }\r
975 \r
976 static const char *menu_loop_romsel(char *curr_path, int len,\r
977         const char **filter_exts,\r
978         int (*extra_filter)(struct dirent **namelist, int count,\r
979                             const char *basedir))\r
980 {\r
981         static char rom_fname_reload[256]; // used for return\r
982         char sel_fname[256];\r
983         int (*filter)(const struct dirent *);\r
984         struct dirent **namelist = NULL;\r
985         int n = 0, inp = 0, sel = 0, show_help = 0;\r
986         char *curr_path_restore = NULL;\r
987         const char *ret = NULL;\r
988         char cinp;\r
989 \r
990         filter_exts_internal = filter_exts;\r
991         sel_fname[0] = 0;\r
992 \r
993         // is this a dir or a full path?\r
994         if (!plat_is_dir(curr_path)) {\r
995                 char *p = strrchr(curr_path, '/');\r
996                 if (p != NULL) {\r
997                         *p = 0;\r
998                         curr_path_restore = p;\r
999                         snprintf(sel_fname, sizeof(sel_fname), "%s", p + 1);\r
1000                 }\r
1001 \r
1002                 if (rom_fname_reload[0] == 0)\r
1003                         show_help = 2;\r
1004         }\r
1005 \r
1006 rescan:\r
1007         if (namelist != NULL) {\r
1008                 while (n-- > 0)\r
1009                         free(namelist[n]);\r
1010                 free(namelist);\r
1011                 namelist = NULL;\r
1012         }\r
1013 \r
1014         filter = NULL;\r
1015         if (!g_menu_filter_off)\r
1016                 filter = scandir_filter;\r
1017 \r
1018         n = scandir(curr_path, &namelist, filter, (void *)scandir_cmp);\r
1019         if (n < 0) {\r
1020                 char *t;\r
1021                 lprintf("menu_loop_romsel failed, dir: %s\n", curr_path);\r
1022 \r
1023                 // try root\r
1024                 t = getcwd(curr_path, len);\r
1025                 if (t == NULL)\r
1026                         plat_get_root_dir(curr_path, len);\r
1027                 n = scandir(curr_path, &namelist, filter, (void *)scandir_cmp);\r
1028                 if (n < 0) {\r
1029                         // oops, we failed\r
1030                         lprintf("menu_loop_romsel failed, dir: %s\n", curr_path);\r
1031                         return NULL;\r
1032                 }\r
1033         }\r
1034 \r
1035         if (!g_menu_filter_off && extra_filter != NULL)\r
1036                 n = extra_filter(namelist, n, curr_path);\r
1037 \r
1038         // try to find selected file\r
1039         // note: we don't show '.' so sel is namelist index - 1\r
1040         sel = 0;\r
1041         if (sel_fname[0] != 0) {\r
1042                 int i;\r
1043                 for (i = 1; i < n; i++) {\r
1044                         char *dname = namelist[i]->d_name;\r
1045                         if (dname[0] == sel_fname[0] && strcmp(dname, sel_fname) == 0) {\r
1046                                 sel = i - 1;\r
1047                                 break;\r
1048                         }\r
1049                 }\r
1050         }\r
1051 \r
1052         /* make sure action buttons are not pressed on entering menu */\r
1053         draw_dirlist(curr_path, namelist, n, sel, show_help);\r
1054         while (in_menu_wait_any(NULL, 50) & (PBTN_MOK|PBTN_MBACK|PBTN_MENU))\r
1055                 ;\r
1056 \r
1057         for (;;)\r
1058         {\r
1059                 draw_dirlist(curr_path, namelist, n, sel, show_help);\r
1060                 inp = in_menu_wait(PBTN_UP|PBTN_DOWN|PBTN_LEFT|PBTN_RIGHT\r
1061                         | PBTN_L|PBTN_R|PBTN_MA2|PBTN_MA3|PBTN_MOK|PBTN_MBACK\r
1062                         | PBTN_MENU|PBTN_CHAR, &cinp, 33);\r
1063                 if (inp & PBTN_MA3)   {\r
1064                         g_menu_filter_off = !g_menu_filter_off;\r
1065                         snprintf(sel_fname, sizeof(sel_fname), "%s",\r
1066                                 namelist[sel+1]->d_name);\r
1067                         goto rescan;\r
1068                 }\r
1069                 if (inp & PBTN_UP  )  { sel--;   if (sel < 0)   sel = n-2; }\r
1070                 if (inp & PBTN_DOWN)  { sel++;   if (sel > n-2) sel = 0; }\r
1071                 if (inp & PBTN_LEFT)  { sel-=10; if (sel < 0)   sel = 0; }\r
1072                 if (inp & PBTN_L)     { sel-=24; if (sel < 0)   sel = 0; }\r
1073                 if (inp & PBTN_RIGHT) { sel+=10; if (sel > n-2) sel = n-2; }\r
1074                 if (inp & PBTN_R)     { sel+=24; if (sel > n-2) sel = n-2; }\r
1075 \r
1076                 if ((inp & PBTN_MOK) || (inp & (PBTN_MENU|PBTN_MA2)) == (PBTN_MENU|PBTN_MA2))\r
1077                 {\r
1078                         again:\r
1079                         if (namelist[sel+1]->d_type == DT_REG)\r
1080                         {\r
1081                                 snprintf(rom_fname_reload, sizeof(rom_fname_reload),\r
1082                                         "%s/%s", curr_path, namelist[sel+1]->d_name);\r
1083                                 if (inp & PBTN_MOK) { // return sel\r
1084                                         ret = rom_fname_reload;\r
1085                                         break;\r
1086                                 }\r
1087                                 do_delete(rom_fname_reload, namelist[sel+1]->d_name);\r
1088                                 goto rescan;\r
1089                         }\r
1090                         else if (namelist[sel+1]->d_type == DT_DIR)\r
1091                         {\r
1092                                 int newlen;\r
1093                                 char *p, *newdir;\r
1094                                 if (!(inp & PBTN_MOK))\r
1095                                         continue;\r
1096                                 newlen = strlen(curr_path) + strlen(namelist[sel+1]->d_name) + 2;\r
1097                                 newdir = malloc(newlen);\r
1098                                 if (newdir == NULL)\r
1099                                         break;\r
1100                                 if (strcmp(namelist[sel+1]->d_name, "..") == 0) {\r
1101                                         char *start = curr_path;\r
1102                                         p = start + strlen(start) - 1;\r
1103                                         while (*p == '/' && p > start) p--;\r
1104                                         while (*p != '/' && p > start) p--;\r
1105                                         if (p <= start) strcpy(newdir, "/");\r
1106                                         else { strncpy(newdir, start, p-start); newdir[p-start] = 0; }\r
1107                                 } else {\r
1108                                         strcpy(newdir, curr_path);\r
1109                                         p = newdir + strlen(newdir) - 1;\r
1110                                         while (*p == '/' && p >= newdir) *p-- = 0;\r
1111                                         strcat(newdir, "/");\r
1112                                         strcat(newdir, namelist[sel+1]->d_name);\r
1113                                 }\r
1114                                 ret = menu_loop_romsel(newdir, newlen, filter_exts, extra_filter);\r
1115                                 free(newdir);\r
1116                                 break;\r
1117                         }\r
1118                         else\r
1119                         {\r
1120                                 // unknown file type, happens on NTFS mounts. Try to guess.\r
1121                                 FILE *tstf; int tmp;\r
1122                                 snprintf(rom_fname_reload, sizeof(rom_fname_reload),\r
1123                                         "%s/%s", curr_path, namelist[sel+1]->d_name);\r
1124                                 tstf = fopen(rom_fname_reload, "rb");\r
1125                                 if (tstf != NULL)\r
1126                                 {\r
1127                                         if (fread(&tmp, 1, 1, tstf) > 0 || ferror(tstf) == 0)\r
1128                                                 namelist[sel+1]->d_type = DT_REG;\r
1129                                         else    namelist[sel+1]->d_type = DT_DIR;\r
1130                                         fclose(tstf);\r
1131                                         goto again;\r
1132                                 }\r
1133                         }\r
1134                 }\r
1135                 else if (inp & PBTN_MA2) {\r
1136                         g_autostateld_opt = !g_autostateld_opt;\r
1137                         show_help = 3;\r
1138                 }\r
1139                 else if (inp & PBTN_CHAR) {\r
1140                         // must be last\r
1141                         sel = dirent_seek_char(namelist, n, sel, cinp);\r
1142                 }\r
1143 \r
1144                 if (inp & PBTN_MBACK)\r
1145                         break;\r
1146 \r
1147                 if (show_help > 0)\r
1148                         show_help--;\r
1149         }\r
1150 \r
1151         if (n > 0) {\r
1152                 while (n-- > 0)\r
1153                         free(namelist[n]);\r
1154                 free(namelist);\r
1155         }\r
1156 \r
1157         // restore curr_path\r
1158         if (curr_path_restore != NULL)\r
1159                 *curr_path_restore = '/';\r
1160 \r
1161         return ret;\r
1162 }\r
1163 \r
1164 // ------------ savestate loader ------------\r
1165 \r
1166 #define STATE_SLOT_COUNT 10\r
1167 \r
1168 static int state_slot_flags = 0;\r
1169 static int state_slot_times[STATE_SLOT_COUNT];\r
1170 \r
1171 static void state_check_slots(void)\r
1172 {\r
1173         int slot;\r
1174 \r
1175         state_slot_flags = 0;\r
1176 \r
1177         for (slot = 0; slot < STATE_SLOT_COUNT; slot++) {\r
1178                 state_slot_times[slot] = 0;\r
1179                 if (emu_check_save_file(slot, &state_slot_times[slot]))\r
1180                         state_slot_flags |= 1 << slot;\r
1181         }\r
1182 }\r
1183 \r
1184 static void draw_savestate_bg(int slot);\r
1185 \r
1186 static void draw_savestate_menu(int menu_sel, int is_loading)\r
1187 {\r
1188         int i, x, y, w, h;\r
1189         char time_buf[32];\r
1190 \r
1191         if (state_slot_flags & (1 << menu_sel))\r
1192                 draw_savestate_bg(menu_sel);\r
1193 \r
1194         w = (13 + 2) * me_mfont_w;\r
1195         h = (1+2+STATE_SLOT_COUNT+1) * me_mfont_h;\r
1196         x = g_menuscreen_w / 2 - w / 2;\r
1197         if (x < 0) x = 0;\r
1198         y = g_menuscreen_h / 2 - h / 2;\r
1199         if (y < 0) y = 0;\r
1200 #ifdef MENU_ALIGN_LEFT\r
1201         if (x > 12 + me_mfont_w * 2)\r
1202                 x = 12 + me_mfont_w * 2;\r
1203 #endif\r
1204 \r
1205         menu_draw_begin(1, 1);\r
1206 \r
1207         text_out16(x, y, is_loading ? "Load state" : "Save state");\r
1208         y += 3 * me_mfont_h;\r
1209 \r
1210         menu_draw_selection(x - me_mfont_w * 2, y + menu_sel * me_mfont_h, (23 + 2) * me_mfont_w + 4);\r
1211 \r
1212         /* draw all slots */\r
1213         for (i = 0; i < STATE_SLOT_COUNT; i++, y += me_mfont_h)\r
1214         {\r
1215                 if (!(state_slot_flags & (1 << i)))\r
1216                         strcpy(time_buf, "free");\r
1217                 else {\r
1218                         strcpy(time_buf, "USED");\r
1219                         if (state_slot_times[i] != 0) {\r
1220                                 time_t time = state_slot_times[i];\r
1221                                 struct tm *t = localtime(&time);\r
1222                                 strftime(time_buf, sizeof(time_buf), "%x %R", t);\r
1223                         }\r
1224                 }\r
1225 \r
1226                 text_out16(x, y, "SLOT %i (%s)", i, time_buf);\r
1227         }\r
1228         text_out16(x, y, "back");\r
1229 \r
1230         menu_draw_end();\r
1231 }\r
1232 \r
1233 static int menu_loop_savestate(int is_loading)\r
1234 {\r
1235         static int menu_sel = STATE_SLOT_COUNT;\r
1236         int menu_sel_max = STATE_SLOT_COUNT;\r
1237         unsigned long inp = 0;\r
1238         int ret = 0;\r
1239 \r
1240         state_check_slots();\r
1241 \r
1242         if (!(state_slot_flags & (1 << menu_sel)) && is_loading)\r
1243                 menu_sel = menu_sel_max;\r
1244 \r
1245         for (;;)\r
1246         {\r
1247                 draw_savestate_menu(menu_sel, is_loading);\r
1248                 inp = in_menu_wait(PBTN_UP|PBTN_DOWN|PBTN_MOK|PBTN_MBACK, NULL, 100);\r
1249                 if (inp & PBTN_UP) {\r
1250                         do {\r
1251                                 menu_sel--;\r
1252                                 if (menu_sel < 0)\r
1253                                         menu_sel = menu_sel_max;\r
1254                         } while (!(state_slot_flags & (1 << menu_sel)) && menu_sel != menu_sel_max && is_loading);\r
1255                 }\r
1256                 if (inp & PBTN_DOWN) {\r
1257                         do {\r
1258                                 menu_sel++;\r
1259                                 if (menu_sel > menu_sel_max)\r
1260                                         menu_sel = 0;\r
1261                         } while (!(state_slot_flags & (1 << menu_sel)) && menu_sel != menu_sel_max && is_loading);\r
1262                 }\r
1263                 if (inp & PBTN_MOK) { // save/load\r
1264                         if (menu_sel < STATE_SLOT_COUNT) {\r
1265                                 state_slot = menu_sel;\r
1266                                 if (emu_save_load_game(is_loading, 0)) {\r
1267                                         menu_update_msg(is_loading ? "Load failed" : "Save failed");\r
1268                                         break;\r
1269                                 }\r
1270                                 ret = 1;\r
1271                                 break;\r
1272                         }\r
1273                         break;\r
1274                 }\r
1275                 if (inp & PBTN_MBACK)\r
1276                         break;\r
1277         }\r
1278 \r
1279         return ret;\r
1280 }\r
1281 \r
1282 // -------------- key config --------------\r
1283 \r
1284 static char *action_binds(int player_idx, int action_mask, int dev_id)\r
1285 {\r
1286         int dev = 0, dev_last = IN_MAX_DEVS - 1;\r
1287         int can_combo = 1, type;\r
1288 \r
1289         static_buff[0] = 0;\r
1290 \r
1291         type = IN_BINDTYPE_EMU;\r
1292         if (player_idx >= 0) {\r
1293                 can_combo = 0;\r
1294                 type = IN_BINDTYPE_PLAYER12;\r
1295         }\r
1296         if (player_idx == 1)\r
1297                 action_mask <<= 16;\r
1298 \r
1299         if (dev_id >= 0)\r
1300                 dev = dev_last = dev_id;\r
1301 \r
1302         for (; dev <= dev_last; dev++) {\r
1303                 int k, count = 0, combo = 0;\r
1304                 const int *binds;\r
1305 \r
1306                 binds = in_get_dev_binds(dev);\r
1307                 if (binds == NULL)\r
1308                         continue;\r
1309 \r
1310                 in_get_config(dev, IN_CFG_BIND_COUNT, &count);\r
1311                 in_get_config(dev, IN_CFG_DOES_COMBOS, &combo);\r
1312                 combo = combo && can_combo;\r
1313 \r
1314                 for (k = 0; k < count; k++) {\r
1315                         const char *xname;\r
1316                         int len;\r
1317 \r
1318                         if (!(binds[IN_BIND_OFFS(k, type)] & action_mask))\r
1319                                 continue;\r
1320 \r
1321                         xname = in_get_key_name(dev, k);\r
1322                         len = strlen(static_buff);\r
1323                         if (len) {\r
1324                                 strncat(static_buff, combo ? " + " : ", ",\r
1325                                         sizeof(static_buff) - len - 1);\r
1326                                 len += combo ? 3 : 2;\r
1327                         }\r
1328                         strncat(static_buff, xname, sizeof(static_buff) - len - 1);\r
1329                 }\r
1330         }\r
1331 \r
1332         return static_buff;\r
1333 }\r
1334 \r
1335 static int count_bound_keys(int dev_id, int action_mask, int bindtype)\r
1336 {\r
1337         const int *binds;\r
1338         int k, keys = 0;\r
1339         int count = 0;\r
1340 \r
1341         binds = in_get_dev_binds(dev_id);\r
1342         if (binds == NULL)\r
1343                 return 0;\r
1344 \r
1345         in_get_config(dev_id, IN_CFG_BIND_COUNT, &count);\r
1346         for (k = 0; k < count; k++)\r
1347         {\r
1348                 if (binds[IN_BIND_OFFS(k, bindtype)] & action_mask)\r
1349                         keys++;\r
1350         }\r
1351 \r
1352         return keys;\r
1353 }\r
1354 \r
1355 static void draw_key_config(const me_bind_action *opts, int opt_cnt, int player_idx,\r
1356                 int sel, int dev_id, int dev_count, int is_bind)\r
1357 {\r
1358         char buff[64], buff2[32];\r
1359         const char *dev_name;\r
1360         int x, y, w, i;\r
1361 \r
1362         w = ((player_idx >= 0) ? 20 : 30) * me_mfont_w;\r
1363         x = g_menuscreen_w / 2 - w / 2;\r
1364         y = (g_menuscreen_h - 4 * me_mfont_h) / 2 - (2 + opt_cnt) * me_mfont_h / 2;\r
1365         if (x < me_mfont_w * 2)\r
1366                 x = me_mfont_w * 2;\r
1367 \r
1368         menu_draw_begin(1, 0);\r
1369         if (player_idx >= 0)\r
1370                 text_out16(x, y, "Player %i controls", player_idx + 1);\r
1371         else\r
1372                 text_out16(x, y, "Emulator controls");\r
1373 \r
1374         y += 2 * me_mfont_h;\r
1375         menu_draw_selection(x - me_mfont_w * 2, y + sel * me_mfont_h, w + 2 * me_mfont_w);\r
1376 \r
1377         for (i = 0; i < opt_cnt; i++, y += me_mfont_h)\r
1378                 text_out16(x, y, "%s : %s", opts[i].name,\r
1379                         action_binds(player_idx, opts[i].mask, dev_id));\r
1380 \r
1381         menu_separation();\r
1382 \r
1383         if (dev_id < 0)\r
1384                 dev_name = "(all devices)";\r
1385         else\r
1386                 dev_name = in_get_dev_name(dev_id, 0, 1);\r
1387         w = strlen(dev_name) * me_mfont_w;\r
1388         if (w < 30 * me_mfont_w)\r
1389                 w = 30 * me_mfont_w;\r
1390         if (w > g_menuscreen_w)\r
1391                 w = g_menuscreen_w;\r
1392 \r
1393         x = g_menuscreen_w / 2 - w / 2;\r
1394 \r
1395         if (!is_bind) {\r
1396                 snprintf(buff2, sizeof(buff2), "%s", in_get_key_name(-1, -PBTN_MOK));\r
1397                 snprintf(buff, sizeof(buff), "%s - bind, %s - clear", buff2,\r
1398                                 in_get_key_name(-1, -PBTN_MA2));\r
1399                 text_out16(x, g_menuscreen_h - 4 * me_mfont_h, buff);\r
1400         }\r
1401         else\r
1402                 text_out16(x, g_menuscreen_h - 4 * me_mfont_h, "Press a button to bind/unbind");\r
1403 \r
1404         if (dev_count > 1) {\r
1405                 text_out16(x, g_menuscreen_h - 3 * me_mfont_h, dev_name);\r
1406                 text_out16(x, g_menuscreen_h - 2 * me_mfont_h, "Press left/right for other devs");\r
1407         }\r
1408 \r
1409         menu_draw_end();\r
1410 }\r
1411 \r
1412 static void key_config_loop(const me_bind_action *opts, int opt_cnt, int player_idx)\r
1413 {\r
1414         int i, sel = 0, menu_sel_max = opt_cnt - 1, does_combos = 0;\r
1415         int dev_id, bind_dev_id, dev_count, kc, is_down, mkey;\r
1416         int unbind, bindtype, mask_shift;\r
1417 \r
1418         for (i = 0, dev_id = -1, dev_count = 0; i < IN_MAX_DEVS; i++) {\r
1419                 if (in_get_dev_name(i, 1, 0) != NULL) {\r
1420                         dev_count++;\r
1421                         if (dev_id < 0)\r
1422                                 dev_id = i;\r
1423                 }\r
1424         }\r
1425 \r
1426         if (dev_id == -1) {\r
1427                 lprintf("no devs, can't do config\n");\r
1428                 return;\r
1429         }\r
1430 \r
1431         dev_id = -1; // show all\r
1432         mask_shift = 0;\r
1433         if (player_idx == 1)\r
1434                 mask_shift = 16;\r
1435         bindtype = player_idx >= 0 ? IN_BINDTYPE_PLAYER12 : IN_BINDTYPE_EMU;\r
1436 \r
1437         for (;;)\r
1438         {\r
1439                 draw_key_config(opts, opt_cnt, player_idx, sel, dev_id, dev_count, 0);\r
1440                 mkey = in_menu_wait(PBTN_UP|PBTN_DOWN|PBTN_LEFT|PBTN_RIGHT\r
1441                                 |PBTN_MBACK|PBTN_MOK|PBTN_MA2, NULL, 100);\r
1442                 switch (mkey) {\r
1443                         case PBTN_UP:   sel--; if (sel < 0) sel = menu_sel_max; continue;\r
1444                         case PBTN_DOWN: sel++; if (sel > menu_sel_max) sel = 0; continue;\r
1445                         case PBTN_LEFT:\r
1446                                 for (i = 0, dev_id--; i < IN_MAX_DEVS + 1; i++, dev_id--) {\r
1447                                         if (dev_id < -1)\r
1448                                                 dev_id = IN_MAX_DEVS - 1;\r
1449                                         if (dev_id == -1 || in_get_dev_name(dev_id, 0, 0) != NULL)\r
1450                                                 break;\r
1451                                 }\r
1452                                 continue;\r
1453                         case PBTN_RIGHT:\r
1454                                 for (i = 0, dev_id++; i < IN_MAX_DEVS; i++, dev_id++) {\r
1455                                         if (dev_id >= IN_MAX_DEVS)\r
1456                                                 dev_id = -1;\r
1457                                         if (dev_id == -1 || in_get_dev_name(dev_id, 0, 0) != NULL)\r
1458                                                 break;\r
1459                                 }\r
1460                                 continue;\r
1461                         case PBTN_MBACK:\r
1462                                 return;\r
1463                         case PBTN_MOK:\r
1464                                 if (sel >= opt_cnt)\r
1465                                         return;\r
1466                                 while (in_menu_wait_any(NULL, 30) & PBTN_MOK)\r
1467                                         ;\r
1468                                 break;\r
1469                         case PBTN_MA2:\r
1470                                 in_unbind_all(dev_id, opts[sel].mask << mask_shift, bindtype);\r
1471                                 continue;\r
1472                         default:continue;\r
1473                 }\r
1474 \r
1475                 draw_key_config(opts, opt_cnt, player_idx, sel, dev_id, dev_count, 1);\r
1476 \r
1477                 /* wait for some up event */\r
1478                 for (is_down = 1; is_down; )\r
1479                         kc = in_update_keycode(&bind_dev_id, &is_down, NULL, -1);\r
1480 \r
1481                 i = count_bound_keys(bind_dev_id, opts[sel].mask << mask_shift, bindtype);\r
1482                 unbind = (i > 0);\r
1483 \r
1484                 /* allow combos if device supports them */\r
1485                 in_get_config(bind_dev_id, IN_CFG_DOES_COMBOS, &does_combos);\r
1486                 if (i == 1 && bindtype == IN_BINDTYPE_EMU && does_combos)\r
1487                         unbind = 0;\r
1488 \r
1489                 if (unbind)\r
1490                         in_unbind_all(bind_dev_id, opts[sel].mask << mask_shift, bindtype);\r
1491 \r
1492                 in_bind_key(bind_dev_id, kc, opts[sel].mask << mask_shift, bindtype, 0);\r
1493         }\r
1494 }\r
1495 \r