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