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