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