-
Notifications
You must be signed in to change notification settings - Fork 18
/
menu.c
1543 lines (1315 loc) · 38 KB
/
menu.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* (C) Gražvydas "notaz" Ignotas, 2006-2012
*
* This work is licensed under the terms of any of these licenses
* (at your option):
* - GNU GPL, version 2 or later.
* - GNU LGPL, version 2.1 or later.
* - MAME license.
* See the COPYING file in the top-level directory.
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdarg.h>
#include <time.h>
#include <locale.h> // savestate date
#include "menu.h"
#include "fonts.h"
#include "readpng.h"
#include "lprintf.h"
#include "input.h"
#include "plat.h"
#include "posix.h"
#if defined(__GNUC__) && __GNUC__ >= 7
#pragma GCC diagnostic ignored "-Wformat-truncation"
#endif
static char static_buff[64];
static int menu_error_time = 0;
char menu_error_msg[64] = { 0, };
// g_menuscreen is the current output buffer the menu is rendered to.
void *g_menuscreen_ptr;
// g_menubg is the menu background and has the same w/h as g_menuscreen, but
// pp=w. It is filled on menu entry from file or from g_menubg_src if available.
void *g_menubg_ptr;
// g_menubg_src points to a buffer containing a bg image. This is usually either
// the emulator screen buffer or the host frame buffer.
void *g_menubg_src_ptr;
int g_menuscreen_w;
int g_menuscreen_h;
int g_menuscreen_pp;
int g_menubg_src_w;
int g_menubg_src_h;
int g_menubg_src_pp;
int g_autostateld_opt;
static unsigned char *menu_font_data = NULL;
static int menu_text_color = 0xfffe; // default to white
static int menu_sel_color = -1; // disabled
/* note: these might become non-constant in future */
#if MENU_X2
static const int me_mfont_w = 16, me_mfont_h = 20;
static const int me_sfont_w = 12, me_sfont_h = 20;
#else
static const int me_mfont_w = 8, me_mfont_h = 10;
static const int me_sfont_w = 6, me_sfont_h = 10;
#endif
static int g_menu_filter_off;
static int g_border_style;
static int border_left, border_right, border_top, border_bottom;
void menuscreen_memset_lines(unsigned short *dst, int c, int l)
{
for (; l > 0; l--, dst += g_menuscreen_pp)
memset(dst, c, g_menuscreen_w * 2);
}
// draws text to current bbp16 screen
static void text_out16_(int x, int y, const char *text, int color)
{
int i, lh, tr, tg, tb, len;
unsigned short *dest = (unsigned short *)g_menuscreen_ptr + x + y * g_menuscreen_pp;
tr = (color & 0xf800) >> 8;
tg = (color & 0x07e0) >> 3;
tb = (color & 0x001f) << 3;
if (text == (void *)1)
{
// selector symbol
text = "";
len = 1;
}
else
{
const char *p;
for (p = text; *p != 0 && *p != '\n'; p++)
;
len = p - text;
}
lh = me_mfont_h;
if (y + lh > g_menuscreen_h)
lh = g_menuscreen_h - y;
for (i = 0; i < len; i++)
{
unsigned char *src = menu_font_data + (unsigned int)text[i] * me_mfont_w * me_mfont_h / 2;
unsigned short *dst = dest;
int u, l;
for (l = 0; l < lh; l++, dst += g_menuscreen_pp - me_mfont_w)
{
for (u = me_mfont_w / 2; u > 0; u--, src++)
{
int c, r, g, b;
c = *src >> 4;
r = (*dst & 0xf800) >> 8;
g = (*dst & 0x07e0) >> 3;
b = (*dst & 0x001f) << 3;
r = (c^0xf)*r/15 + c*tr/15;
g = (c^0xf)*g/15 + c*tg/15;
b = (c^0xf)*b/15 + c*tb/15;
*dst++ = ((r<<8)&0xf800) | ((g<<3)&0x07e0) | (b>>3);
c = *src & 0xf;
r = (*dst & 0xf800) >> 8;
g = (*dst & 0x07e0) >> 3;
b = (*dst & 0x001f) << 3;
r = (c^0xf)*r/15 + c*tr/15;
g = (c^0xf)*g/15 + c*tg/15;
b = (c^0xf)*b/15 + c*tb/15;
*dst++ = ((r<<8)&0xf800) | ((g<<3)&0x07e0) | (b>>3);
}
}
dest += me_mfont_w;
}
if (x < border_left)
border_left = x;
if (x + i * me_mfont_w > border_right)
border_right = x + i * me_mfont_w;
if (y < border_top)
border_top = y;
if (y + me_mfont_h > border_bottom)
border_bottom = y + me_mfont_h;
}
void text_out16(int x, int y, const char *texto, ...)
{
va_list args;
char buffer[256];
int maxw = (g_menuscreen_w - x) / me_mfont_w;
if (maxw < 0)
return;
va_start(args, texto);
vsnprintf(buffer, sizeof(buffer), texto, args);
va_end(args);
if (maxw > sizeof(buffer) - 1)
maxw = sizeof(buffer) - 1;
buffer[maxw] = 0;
text_out16_(x,y,buffer,menu_text_color);
}
/* draws in 6x8 font, might multiply size by integer */
static void smalltext_out16_(int x, int y, const char *texto, int color)
{
unsigned char *src;
unsigned short *dst;
int multiplier = me_sfont_w / 6;
int i;
for (i = 0;; i++, x += me_sfont_w)
{
unsigned char c = (unsigned char) texto[i];
int h = 8;
if (!c || c == '\n')
break;
src = fontdata6x8[c];
dst = (unsigned short *)g_menuscreen_ptr + x + y * g_menuscreen_pp;
while (h--)
{
int m, w2, h2;
for (h2 = multiplier; h2 > 0; h2--)
{
for (m = 0x20; m; m >>= 1) {
if (*src & m)
for (w2 = multiplier; w2 > 0; w2--)
*dst++ = color;
else
dst += multiplier;
}
dst += g_menuscreen_pp - me_sfont_w;
}
src++;
}
}
}
static void smalltext_out16(int x, int y, const char *texto, int color)
{
char buffer[128];
int maxw = (g_menuscreen_w - x) / me_sfont_w;
if (maxw < 0)
return;
if (maxw > sizeof(buffer) - 1)
maxw = sizeof(buffer) - 1;
strncpy(buffer, texto, maxw);
buffer[maxw] = 0;
smalltext_out16_(x, y, buffer, color);
}
static void menu_draw_selection(int x, int y, int w)
{
int i, h;
unsigned short *dst, *dest;
text_out16_(x, y, (void *)1, (menu_sel_color < 0) ? menu_text_color : menu_sel_color);
if (menu_sel_color < 0) return; // no selection hilight
if (y > 0) y--;
dest = (unsigned short *)g_menuscreen_ptr + x + y * g_menuscreen_pp + me_mfont_w * 2 - 2;
for (h = me_mfont_h + 1; h > 0; h--)
{
dst = dest;
for (i = w - (me_mfont_w * 2 - 2); i > 0; i--)
*dst++ = menu_sel_color;
dest += g_menuscreen_pp;
}
}
static int parse_hex_color(char *buff)
{
char *endp = buff;
int t = (int) strtoul(buff, &endp, 16);
if (endp != buff)
#ifdef PSP
return ((t<<8)&0xf800) | ((t>>5)&0x07e0) | ((t>>19)&0x1f);
#else
return ((t>>8)&0xf800) | ((t>>5)&0x07e0) | ((t>>3)&0x1f);
#endif
return -1;
}
static char tolower_simple(char c)
{
if ('A' <= c && c <= 'Z')
c = c - 'A' + 'a';
return c;
}
void menu_init_base(void)
{
int i, c, l, pos;
unsigned char *fd, *fds;
char buff[256];
FILE *f;
if (menu_font_data != NULL)
free(menu_font_data);
menu_font_data = calloc((MENU_X2 ? 256 * 320 : 128 * 160) / 2, 1);
if (menu_font_data == NULL)
return;
// generate default 8x10 font from fontdata8x8
for (c = 0, fd = menu_font_data; c < 128; c++)
{
for (l = 0; l < 8; l++)
{
unsigned char fd8x8 = fontdata8x8[c*8+l];
if (fd8x8&0x80) { *fd = 0xf0; }
if (fd8x8&0x40) { *fd |= 0x0f; }; fd++;
if (fd8x8&0x20) { *fd = 0xf0; }
if (fd8x8&0x10) { *fd |= 0x0f; }; fd++;
if (fd8x8&0x08) { *fd = 0xf0; }
if (fd8x8&0x04) { *fd |= 0x0f; }; fd++;
if (fd8x8&0x02) { *fd = 0xf0; }
if (fd8x8&0x01) { *fd |= 0x0f; }; fd++;
}
fd += 8*2/2; // 2 empty lines
}
if (MENU_X2) {
// expand default font
fds = menu_font_data + 128 * 160 / 2 - 4;
fd = menu_font_data + 256 * 320 / 2 - 1;
for (c = 255; c >= 0; c--)
{
for (l = 9; l >= 0; l--, fds -= 4)
{
for (i = 3; i >= 0; i--) {
int px = fds[i] & 0x0f;
*fd-- = px | (px << 4);
px = (fds[i] >> 4) & 0x0f;
*fd-- = px | (px << 4);
}
for (i = 3; i >= 0; i--) {
int px = fds[i] & 0x0f;
*fd-- = px | (px << 4);
px = (fds[i] >> 4) & 0x0f;
*fd-- = px | (px << 4);
}
}
}
}
// load custom font and selector (stored as 1st symbol in font table)
pos = plat_get_skin_dir(buff, sizeof(buff));
strcpy(buff + pos, "font.png");
readpng(menu_font_data, buff, READPNG_FONT,
MENU_X2 ? 256 : 128, MENU_X2 ? 320 : 160);
// default selector symbol is '>'
memcpy(menu_font_data, menu_font_data + ((int)'>') * me_mfont_w * me_mfont_h / 2,
me_mfont_w * me_mfont_h / 2);
strcpy(buff + pos, "selector.png");
readpng(menu_font_data, buff, READPNG_SELECTOR, me_mfont_w, me_mfont_h);
// load custom colors
strcpy(buff + pos, "skin.txt");
f = fopen(buff, "r");
if (f != NULL)
{
lprintf("found skin.txt\n");
while (!feof(f))
{
if (fgets(buff, sizeof(buff), f) == NULL)
break;
if (buff[0] == '#' || buff[0] == '/') continue; // comment
if (buff[0] == '\r' || buff[0] == '\n') continue; // empty line
if (strncmp(buff, "text_color=", 11) == 0)
{
int tmp = parse_hex_color(buff+11);
if (tmp >= 0) menu_text_color = tmp;
else lprintf("skin.txt: parse error for text_color\n");
}
else if (strncmp(buff, "selection_color=", 16) == 0)
{
int tmp = parse_hex_color(buff+16);
if (tmp >= 0) menu_sel_color = tmp;
else lprintf("skin.txt: parse error for selection_color\n");
}
else
lprintf("skin.txt: parse error: %s\n", buff);
}
fclose(f);
}
// use user's locale for savestate date display
setlocale(LC_TIME, "");
}
static void menu_darken_bg(void *dst, void *src, int pixels, int darker)
{
unsigned int *dest = dst;
unsigned int *sorc = src;
pixels /= 2;
if (darker)
{
while (pixels--)
{
unsigned int p = *sorc++;
*dest++ = ((p&0xf79ef79e)>>1) - ((p&0xc618c618)>>3);
}
}
else
{
while (pixels--)
{
unsigned int p = *sorc++;
*dest++ = (p&0xf79ef79e)>>1;
}
}
}
static void menu_darken_text_bg(void)
{
int x, y, xmin, xmax, ymax, ls;
unsigned short *screen = g_menuscreen_ptr;
xmin = border_left - 3;
if (xmin < 0)
xmin = 0;
xmax = border_right + 2;
if (xmax > g_menuscreen_w - 1)
xmax = g_menuscreen_w - 1;
y = border_top - 3;
if (y < 0)
y = 0;
ymax = border_bottom + 2;
if (ymax > g_menuscreen_h - 1)
ymax = g_menuscreen_h - 1;
for (x = xmin; x <= xmax; x++)
screen[y * g_menuscreen_pp + x] = 0xa514;
for (y++; y < ymax; y++)
{
ls = y * g_menuscreen_pp;
screen[ls + xmin] = 0xffff;
for (x = xmin + 1; x < xmax; x++)
{
unsigned int p = screen[ls + x];
if (p != menu_text_color)
screen[ls + x] = ((p&0xf79e)>>1) - ((p&0xc618)>>3);
}
screen[ls + xmax] = 0xffff;
}
ls = y * g_menuscreen_pp;
for (x = xmin; x <= xmax; x++)
screen[ls + x] = 0xffff;
}
static int borders_pending;
static void menu_reset_borders(void)
{
border_left = g_menuscreen_w;
border_right = 0;
border_top = g_menuscreen_h;
border_bottom = 0;
}
static void menu_draw_begin(int need_bg, int no_borders)
{
int y;
plat_video_menu_begin();
menu_reset_borders();
borders_pending = g_border_style && !no_borders;
if (need_bg) {
if (g_border_style && no_borders) {
for (y = 0; y < g_menuscreen_h; y++)
menu_darken_bg((short *)g_menuscreen_ptr + g_menuscreen_pp * y,
(short *)g_menubg_ptr + g_menuscreen_w * y, g_menuscreen_w, 1);
}
else {
for (y = 0; y < g_menuscreen_h; y++)
memcpy((short *)g_menuscreen_ptr + g_menuscreen_pp * y,
(short *)g_menubg_ptr + g_menuscreen_w * y, g_menuscreen_w * 2);
}
}
}
static void menu_draw_end(void)
{
if (borders_pending)
menu_darken_text_bg();
plat_video_menu_end();
}
static void menu_separation(void)
{
if (borders_pending) {
menu_darken_text_bg();
menu_reset_borders();
}
}
static int me_id2offset(const menu_entry *ent, menu_id id)
{
int i;
for (i = 0; ent->name; ent++, i++)
if (ent->id == id) return i;
lprintf("%s: id %i not found\n", __FUNCTION__, id);
return 0;
}
static void me_enable(menu_entry *entries, menu_id id, int enable)
{
int i = me_id2offset(entries, id);
entries[i].enabled = enable;
}
static int me_count(const menu_entry *ent)
{
int ret;
for (ret = 0; ent->name; ent++, ret++)
;
return ret;
}
static unsigned int me_read_onoff(const menu_entry *ent)
{
return *(unsigned int *)ent->var & ent->mask;
}
static void me_toggle_onoff(menu_entry *ent)
{
*(unsigned int *)ent->var ^= ent->mask;
}
static void me_draw(const menu_entry *entries, int sel, void (*draw_more)(void))
{
const menu_entry *ent, *ent_sel = entries;
int x, y, w = 0, h = 0;
int offs, col2_offs = 27 * me_mfont_w;
int vi_sel_ln = 0;
const char *name;
int i, n;
/* calculate size of menu rect */
for (ent = entries, i = n = 0; ent->name; ent++, i++)
{
int wt;
if (!ent->enabled)
continue;
if (i == sel) {
ent_sel = ent;
vi_sel_ln = n;
}
name = NULL;
wt = strlen(ent->name) * me_mfont_w;
if (wt == 0 && ent->generate_name)
name = ent->generate_name(ent->id, &offs);
if (name != NULL)
wt = strlen(name) * me_mfont_w;
if (ent->beh != MB_NONE)
{
if (wt > col2_offs)
col2_offs = wt + me_mfont_w;
wt = col2_offs;
switch (ent->beh) {
case MB_NONE:
break;
case MB_OPT_ONOFF:
case MB_OPT_RANGE:
wt += me_mfont_w * 3;
break;
case MB_OPT_CUSTOM:
case MB_OPT_CUSTONOFF:
case MB_OPT_CUSTRANGE:
name = NULL;
offs = 0;
if (ent->generate_name != NULL)
name = ent->generate_name(ent->id, &offs);
if (name != NULL)
wt += (strlen(name) + offs) * me_mfont_w;
break;
case MB_OPT_ENUM:
wt += 10 * me_mfont_w;
break;
}
}
if (wt > w)
w = wt;
n++;
}
h = n * me_mfont_h;
w += me_mfont_w * 2; /* selector */
if (w > g_menuscreen_w) {
lprintf("width %d > %d\n", w, g_menuscreen_w);
w = g_menuscreen_w;
}
if (h > g_menuscreen_h) {
lprintf("height %d > %d\n", w, g_menuscreen_h);
h = g_menuscreen_h;
}
x = g_menuscreen_w / 2 - w / 2;
y = g_menuscreen_h / 2 - h / 2;
#ifdef MENU_ALIGN_LEFT
if (x > 12) x = 12;
#endif
/* draw */
menu_draw_begin(1, 0);
menu_draw_selection(x, y + vi_sel_ln * me_mfont_h, w);
x += me_mfont_w * 2;
for (ent = entries; ent->name; ent++)
{
const char **names;
int len, leftname_end = 0;
if (!ent->enabled)
continue;
name = ent->name;
if (strlen(name) == 0) {
if (ent->generate_name)
name = ent->generate_name(ent->id, &offs);
}
if (name != NULL) {
text_out16(x, y, "%s", name);
leftname_end = x + (strlen(name) + 1) * me_mfont_w;
}
switch (ent->beh) {
case MB_NONE:
break;
case MB_OPT_ONOFF:
text_out16(x + col2_offs, y, "%s", me_read_onoff(ent) ? "ON" : "OFF");
break;
case MB_OPT_RANGE:
text_out16(x + col2_offs, y, "%i", *(int *)ent->var);
break;
case MB_OPT_CUSTOM:
case MB_OPT_CUSTONOFF:
case MB_OPT_CUSTRANGE:
name = NULL;
offs = 0;
if (ent->generate_name)
name = ent->generate_name(ent->id, &offs);
if (name != NULL)
text_out16(x + col2_offs + offs * me_mfont_w, y, "%s", name);
break;
case MB_OPT_ENUM:
names = (const char **)ent->data;
for (i = 0; names[i] != NULL; i++) {
offs = x + col2_offs;
len = strlen(names[i]);
if (len > 10)
offs += (10 - len) * me_mfont_w;
if (offs < leftname_end)
offs = leftname_end;
if (i == *(int *)ent->var) {
text_out16(offs, y, "%s", names[i]);
break;
}
}
break;
}
y += me_mfont_h;
}
menu_separation();
/* display help or message if we have one */
h = (g_menuscreen_h - h) / 2; // bottom area height
if (menu_error_msg[0] != 0) {
if (h >= me_mfont_h + 4)
text_out16(5, g_menuscreen_h - me_mfont_h - 4, "%s", menu_error_msg);
else
lprintf("menu msg doesn't fit!\n");
if (plat_get_ticks_ms() - menu_error_time > 2048)
menu_error_msg[0] = 0;
}
else if (ent_sel->help != NULL) {
const char *tmp = ent_sel->help;
int l;
for (l = 0; tmp != NULL && *tmp != 0; l++)
tmp = strchr(tmp + 1, '\n');
if (h >= l * me_sfont_h + 4)
for (tmp = ent_sel->help; l > 0; l--, tmp = strchr(tmp, '\n') + 1)
smalltext_out16(5, g_menuscreen_h - (l * me_sfont_h + 4), tmp, 0xffff);
}
menu_separation();
if (draw_more != NULL)
draw_more();
menu_draw_end();
}
static int me_process(menu_entry *entry, int is_next, int is_lr)
{
const char **names;
int c;
switch (entry->beh)
{
case MB_OPT_ONOFF:
case MB_OPT_CUSTONOFF:
me_toggle_onoff(entry);
return 1;
case MB_OPT_RANGE:
case MB_OPT_CUSTRANGE:
c = is_lr ? 10 : 1;
*(int *)entry->var += is_next ? c : -c;
if (*(int *)entry->var < (int)entry->min)
*(int *)entry->var = (int)entry->max;
if (*(int *)entry->var > (int)entry->max)
*(int *)entry->var = (int)entry->min;
return 1;
case MB_OPT_ENUM:
names = (const char **)entry->data;
for (c = 0; names[c] != NULL; c++)
;
*(int *)entry->var += is_next ? 1 : -1;
if (*(int *)entry->var < 0)
*(int *)entry->var = 0;
if (*(int *)entry->var >= c)
*(int *)entry->var = c - 1;
return 1;
default:
return 0;
}
}
static void debug_menu_loop(void);
static int me_loop_d(menu_entry *menu, int *menu_sel, void (*draw_prep)(void), void (*draw_more)(void))
{
int ret = 0, inp, sel = *menu_sel, menu_sel_max;
menu_sel_max = me_count(menu) - 1;
if (menu_sel_max < 0) {
lprintf("no enabled menu entries\n");
return 0;
}
while ((!menu[sel].enabled || !menu[sel].selectable) && sel < menu_sel_max)
sel++;
/* make sure action buttons are not pressed on entering menu */
me_draw(menu, sel, NULL);
while (in_menu_wait_any(NULL, 50) & (PBTN_MOK|PBTN_MBACK|PBTN_MENU));
for (;;)
{
if (draw_prep != NULL)
draw_prep();
me_draw(menu, sel, draw_more);
inp = in_menu_wait(PBTN_UP|PBTN_DOWN|PBTN_LEFT|PBTN_RIGHT|
PBTN_MOK|PBTN_MBACK|PBTN_MENU|PBTN_L|PBTN_R, NULL, 70);
if (inp & (PBTN_MENU|PBTN_MBACK))
break;
if (inp & PBTN_UP ) {
do {
sel--;
if (sel < 0)
sel = menu_sel_max;
}
while (!menu[sel].enabled || !menu[sel].selectable);
}
if (inp & PBTN_DOWN) {
do {
sel++;
if (sel > menu_sel_max)
sel = 0;
}
while (!menu[sel].enabled || !menu[sel].selectable);
}
/* a bit hacky but oh well */
if ((inp & (PBTN_L|PBTN_R)) == (PBTN_L|PBTN_R))
debug_menu_loop();
if (inp & (PBTN_LEFT|PBTN_RIGHT|PBTN_L|PBTN_R)) { /* multi choice */
if (me_process(&menu[sel], (inp & (PBTN_RIGHT|PBTN_R)) ? 1 : 0,
inp & (PBTN_L|PBTN_R)))
continue;
}
if (inp & (PBTN_MOK|PBTN_LEFT|PBTN_RIGHT|PBTN_L|PBTN_R))
{
/* require PBTN_MOK for MB_NONE */
if (menu[sel].handler != NULL && (menu[sel].beh != MB_NONE || (inp & PBTN_MOK))) {
ret = menu[sel].handler(menu[sel].id, inp);
if (ret) break;
menu_sel_max = me_count(menu) - 1; /* might change, so update */
}
}
}
*menu_sel = sel;
return ret;
}
static int me_loop(menu_entry *menu, int *menu_sel)
{
return me_loop_d(menu, menu_sel, NULL, NULL);
}
/* ***************************************** */
static void draw_menu_message(const char *msg, void (*draw_more)(void))
{
int x, y, h, w, wt;
const char *p;
p = msg;
for (h = 1, w = 0; *p != 0; h++) {
for (wt = 0; *p != 0 && *p != '\n'; p++)
wt++;
if (wt > w)
w = wt;
if (*p == 0)
break;
p++;
}
x = g_menuscreen_w / 2 - w * me_mfont_w / 2;
y = g_menuscreen_h / 2 - h * me_mfont_h / 2;
if (x < 0) x = 0;
if (y < 0) y = 0;
menu_draw_begin(1, 0);
for (p = msg; *p != 0 && y <= g_menuscreen_h - me_mfont_h; y += me_mfont_h) {
text_out16(x, y, "%s", p);
for (; *p != 0 && *p != '\n'; p++)
;
if (*p != 0)
p++;
}
menu_separation();
if (draw_more != NULL)
draw_more();
menu_draw_end();
}
// -------------- del confirm ---------------
static void do_delete(const char *fpath, const char *fname)
{
int len, mid, inp;
const char *nm;
char tmp[64];
menu_draw_begin(1, 0);
len = strlen(fname);
if (len > g_menuscreen_w / me_sfont_w)
len = g_menuscreen_w / me_sfont_w;
mid = g_menuscreen_w / 2;
text_out16(mid - me_mfont_w * 15 / 2, 8 * me_mfont_h, "About to delete");
smalltext_out16(mid - len * me_sfont_w / 2, 9 * me_mfont_h + 5, fname, 0xbdff);
text_out16(mid - me_mfont_w * 13 / 2, 11 * me_mfont_h, "Are you sure?");
nm = in_get_key_name(-1, -PBTN_MA3);
snprintf(tmp, sizeof(tmp), "(%s - confirm, ", nm);
len = strlen(tmp);
nm = in_get_key_name(-1, -PBTN_MBACK);
snprintf(tmp + len, sizeof(tmp) - len, "%s - cancel)", nm);
len = strlen(tmp);
text_out16(mid - me_mfont_w * len / 2, 12 * me_mfont_h, "%s", tmp);
menu_draw_end();
while (in_menu_wait_any(NULL, 50) & (PBTN_MENU|PBTN_MA2));
inp = in_menu_wait(PBTN_MA3|PBTN_MBACK, NULL, 100);
if (inp & PBTN_MA3)
remove(fpath);
}
// -------------- ROM selector --------------
static void draw_dirlist(char *curdir, struct dirent **namelist,
int n, int sel, int show_help)
{
int max_cnt, start, i, x, pos;
void *darken_ptr;
char buff[64];
max_cnt = g_menuscreen_h / me_sfont_h;
start = max_cnt / 2 - sel;
menu_draw_begin(1, 1);
// if (!rom_loaded)
// menu_darken_bg(gp2x_screen, 320*240, 0);
darken_ptr = (short *)g_menuscreen_ptr + g_menuscreen_pp * max_cnt/2 * me_sfont_h;
menu_darken_bg(darken_ptr, darken_ptr, g_menuscreen_pp * me_sfont_h * 8 / 10, 0);
x = 5 + me_mfont_w + 1;
if (start - 2 >= 0)
smalltext_out16(14, (start - 2) * me_sfont_h, curdir, 0xffff);
for (i = 0; i < n; i++) {
pos = start + i;
if (pos < 0) continue;
if (pos >= max_cnt) break;
if (namelist[i]->d_type == DT_DIR) {
smalltext_out16(x, pos * me_sfont_h, "/", 0xfff6);
smalltext_out16(x + me_sfont_w, pos * me_sfont_h, namelist[i]->d_name, 0xfff6);
} else {
unsigned short color = fname2color(namelist[i]->d_name);
smalltext_out16(x, pos * me_sfont_h, namelist[i]->d_name, color);
}
}
smalltext_out16(5, max_cnt/2 * me_sfont_h, ">", 0xffff);
if (show_help) {
darken_ptr = (short *)g_menuscreen_ptr
+ g_menuscreen_pp * (g_menuscreen_h - me_sfont_h * 5 / 2);
menu_darken_bg(darken_ptr, darken_ptr,
g_menuscreen_pp * (me_sfont_h * 5 / 2), 1);
snprintf(buff, sizeof(buff), "%s - select, %s - back",
in_get_key_name(-1, -PBTN_MOK), in_get_key_name(-1, -PBTN_MBACK));
smalltext_out16(x, g_menuscreen_h - me_sfont_h * 3 - 2, buff, 0xe78c);
snprintf(buff, sizeof(buff), g_menu_filter_off ?
"%s - hide unknown files" : "%s - show all files",
in_get_key_name(-1, -PBTN_MA3));
smalltext_out16(x, g_menuscreen_h - me_sfont_h * 2 - 2, buff, 0xe78c);
snprintf(buff, sizeof(buff), g_autostateld_opt ?
"%s - autoload save is ON" : "%s - autoload save is OFF",
in_get_key_name(-1, -PBTN_MA2));
smalltext_out16(x, g_menuscreen_h - me_sfont_h * 1 - 2, buff, 0xe78c);
}
menu_draw_end();
}
static int scandir_cmp(const void *p1, const void *p2)
{
const struct dirent **d1 = (const struct dirent **)p1;
const struct dirent **d2 = (const struct dirent **)p2;
const char *p;
if ((p = (*d1)->d_name)[0] == '.' && p[1] == '.' && p[2] == 0)
return -1; // ".." first
if ((p = (*d2)->d_name)[0] == '.' && p[1] == '.' && p[2] == 0)
return 1;
if ((*d1)->d_type == (*d2)->d_type)
return alphasort(d1, d2);
if ((*d1)->d_type == DT_DIR)
return -1; // directories before files/links
if ((*d2)->d_type == DT_DIR)
return 1;
return alphasort(d1, d2);
}
static const char **filter_exts_internal;
static int scandir_filter(const struct dirent *ent)
{
const char **filter = filter_exts_internal;
const char *ext;
int i;
if (ent == NULL)
return 0;
switch (ent->d_type) {
case DT_DIR:
// leave out useless reference to current directory
return strcmp(ent->d_name, ".") != 0;
case DT_LNK:
case DT_UNKNOWN:
// could be a dir, deal with it later..
return 1;
}
ext = strrchr(ent->d_name, '.');
if (ext == NULL)
return 0;
ext++;
for (i = 0; filter[i] != NULL; i++)
if (strcasecmp(ext, filter[i]) == 0)
return 1;
return 0;
}
static int dirent_seek_char(struct dirent **namelist, int len, int sel, char c)
{
int i;
for (i = sel + 1; ; i++) {
if (i >= len)
i = 0;
if (i == sel)
break;
if (tolower_simple(namelist[i]->d_name[0]) == c)
break;
}
return i;
}
static const char *menu_loop_romsel_d(char *curr_path, int len,
const char **filter_exts,
int (*extra_filter)(struct dirent **namelist, int count,
const char *basedir),