c7eb229a |
1 | // (c) Copyright 2006-2010 notaz, All rights reserved.\r |
e5f426aa |
2 | // Free for non-commercial use.\r |
3 | \r |
4 | // For commercial use, separate licencing terms must be obtained.\r |
5 | \r |
6 | #include <stdio.h>\r |
7 | #include <string.h>\r |
8 | #include <stdlib.h>\r |
9 | #include <stdarg.h>\r |
10 | \r |
11 | #include "menu.h"\r |
12 | #include "fonts.h"\r |
13 | #include "readpng.h"\r |
14 | #include "lprintf.h"\r |
b6820926 |
15 | #include "input.h"\r |
1fb0dd88 |
16 | #include "plat.h"\r |
713c9224 |
17 | #include "posix.h"\r |
e5f426aa |
18 | \r |
713c9224 |
19 | static char static_buff[64];\r |
c6c6c9cd |
20 | static int menu_error_time = 0;\r |
697746df |
21 | char menu_error_msg[64] = { 0, };\r |
c7eb229a |
22 | void *g_menuscreen_ptr;\r |
23 | void *g_menubg_src_ptr;\r |
697746df |
24 | void *g_menubg_ptr;\r |
1ca2ea4f |
25 | \r |
d40231e2 |
26 | #if !MSCREEN_SIZE_FIXED\r |
d2fd8a7a |
27 | int g_menuscreen_w;\r |
28 | int g_menuscreen_h;\r |
c7eb229a |
29 | #endif\r |
30 | \r |
c6c6c9cd |
31 | static unsigned char *menu_font_data = NULL;\r |
e5f426aa |
32 | static int menu_text_color = 0xffff; // default to white\r |
33 | static int menu_sel_color = -1; // disabled\r |
cc33f567 |
34 | \r |
35 | /* note: these might become non-constant in future */\r |
c6c6c9cd |
36 | #if MENU_X2\r |
37 | static const int me_mfont_w = 16, me_mfont_h = 20;\r |
cc33f567 |
38 | static const int me_sfont_w = 12, me_sfont_h = 20;\r |
c6c6c9cd |
39 | #else\r |
40 | static const int me_mfont_w = 8, me_mfont_h = 10;\r |
cc33f567 |
41 | static const int me_sfont_w = 6, me_sfont_h = 10;\r |
c6c6c9cd |
42 | #endif\r |
e5f426aa |
43 | \r |
44 | // draws text to current bbp16 screen\r |
45 | static void text_out16_(int x, int y, const char *text, int color)\r |
46 | {\r |
89e6909b |
47 | int i, lh, tr, tg, tb, len;\r |
c7eb229a |
48 | unsigned short *dest = (unsigned short *)g_menuscreen_ptr + x + y * g_menuscreen_w;\r |
e5f426aa |
49 | tr = (color & 0xf800) >> 8;\r |
50 | tg = (color & 0x07e0) >> 3;\r |
51 | tb = (color & 0x001f) << 3;\r |
52 | \r |
53 | if (text == (void *)1)\r |
54 | {\r |
55 | // selector symbol\r |
56 | text = "";\r |
57 | len = 1;\r |
58 | }\r |
59 | else\r |
713c9224 |
60 | {\r |
61 | const char *p;\r |
62 | for (p = text; *p != 0 && *p != '\n'; p++)\r |
63 | ;\r |
64 | len = p - text;\r |
65 | }\r |
e5f426aa |
66 | \r |
89e6909b |
67 | lh = me_mfont_h;\r |
c7eb229a |
68 | if (y + lh > g_menuscreen_h)\r |
69 | lh = g_menuscreen_h - y;\r |
89e6909b |
70 | \r |
e5f426aa |
71 | for (i = 0; i < len; i++)\r |
72 | {\r |
c6c6c9cd |
73 | unsigned char *src = menu_font_data + (unsigned int)text[i] * me_mfont_w * me_mfont_h / 2;\r |
e5f426aa |
74 | unsigned short *dst = dest;\r |
89e6909b |
75 | int u, l;\r |
76 | \r |
c7eb229a |
77 | for (l = 0; l < lh; l++, dst += g_menuscreen_w - me_mfont_w)\r |
e5f426aa |
78 | {\r |
c6c6c9cd |
79 | for (u = me_mfont_w / 2; u > 0; u--, src++)\r |
e5f426aa |
80 | {\r |
81 | int c, r, g, b;\r |
82 | c = *src >> 4;\r |
83 | r = (*dst & 0xf800) >> 8;\r |
84 | g = (*dst & 0x07e0) >> 3;\r |
85 | b = (*dst & 0x001f) << 3;\r |
86 | r = (c^0xf)*r/15 + c*tr/15;\r |
87 | g = (c^0xf)*g/15 + c*tg/15;\r |
88 | b = (c^0xf)*b/15 + c*tb/15;\r |
89 | *dst++ = ((r<<8)&0xf800) | ((g<<3)&0x07e0) | (b>>3);\r |
90 | c = *src & 0xf;\r |
91 | r = (*dst & 0xf800) >> 8;\r |
92 | g = (*dst & 0x07e0) >> 3;\r |
93 | b = (*dst & 0x001f) << 3;\r |
94 | r = (c^0xf)*r/15 + c*tr/15;\r |
95 | g = (c^0xf)*g/15 + c*tg/15;\r |
96 | b = (c^0xf)*b/15 + c*tb/15;\r |
97 | *dst++ = ((r<<8)&0xf800) | ((g<<3)&0x07e0) | (b>>3);\r |
98 | }\r |
99 | }\r |
c6c6c9cd |
100 | dest += me_mfont_w;\r |
e5f426aa |
101 | }\r |
102 | }\r |
103 | \r |
104 | void text_out16(int x, int y, const char *texto, ...)\r |
105 | {\r |
106 | va_list args;\r |
1fb0dd88 |
107 | char buffer[256];\r |
c7eb229a |
108 | int maxw = (g_menuscreen_w - x) / me_mfont_w;\r |
e5f426aa |
109 | \r |
19e1d027 |
110 | if (maxw < 0)\r |
111 | return;\r |
112 | \r |
1fb0dd88 |
113 | va_start(args, texto);\r |
114 | vsnprintf(buffer, sizeof(buffer), texto, args);\r |
e5f426aa |
115 | va_end(args);\r |
116 | \r |
19e1d027 |
117 | if (maxw > sizeof(buffer) - 1)\r |
118 | maxw = sizeof(buffer) - 1;\r |
1fb0dd88 |
119 | buffer[maxw] = 0;\r |
120 | \r |
e5f426aa |
121 | text_out16_(x,y,buffer,menu_text_color);\r |
122 | }\r |
123 | \r |
cc33f567 |
124 | /* draws in 6x8 font, might multiply size by integer */\r |
713c9224 |
125 | static void smalltext_out16_(int x, int y, const char *texto, int color)\r |
e5f426aa |
126 | {\r |
e5f426aa |
127 | unsigned char *src;\r |
128 | unsigned short *dst;\r |
cc33f567 |
129 | int multiplier = me_sfont_w / 6;\r |
130 | int i;\r |
e5f426aa |
131 | \r |
cc33f567 |
132 | for (i = 0;; i++, x += me_sfont_w)\r |
e5f426aa |
133 | {\r |
134 | unsigned char c = (unsigned char) texto[i];\r |
135 | int h = 8;\r |
136 | \r |
cc33f567 |
137 | if (!c || c == '\n')\r |
138 | break;\r |
e5f426aa |
139 | \r |
140 | src = fontdata6x8[c];\r |
c7eb229a |
141 | dst = (unsigned short *)g_menuscreen_ptr + x + y * g_menuscreen_w;\r |
e5f426aa |
142 | \r |
143 | while (h--)\r |
144 | {\r |
cc33f567 |
145 | int m, w2, h2;\r |
146 | for (h2 = multiplier; h2 > 0; h2--)\r |
e5f426aa |
147 | {\r |
cc33f567 |
148 | for (m = 0x20; m; m >>= 1) {\r |
149 | if (*src & m)\r |
150 | for (w2 = multiplier; w2 > 0; w2--)\r |
151 | *dst++ = color;\r |
152 | else\r |
153 | dst += multiplier;\r |
154 | }\r |
155 | \r |
c7eb229a |
156 | dst += g_menuscreen_w - me_sfont_w;\r |
e5f426aa |
157 | }\r |
158 | src++;\r |
e5f426aa |
159 | }\r |
160 | }\r |
161 | }\r |
162 | \r |
c6c6c9cd |
163 | static void smalltext_out16(int x, int y, const char *texto, int color)\r |
e5f426aa |
164 | {\r |
e2de9939 |
165 | char buffer[128];\r |
c7eb229a |
166 | int maxw = (g_menuscreen_w - x) / me_sfont_w;\r |
19e1d027 |
167 | \r |
168 | if (maxw < 0)\r |
169 | return;\r |
e5f426aa |
170 | \r |
713c9224 |
171 | strncpy(buffer, texto, sizeof(buffer));\r |
19e1d027 |
172 | if (maxw > sizeof(buffer) - 1)\r |
173 | maxw = sizeof(buffer) - 1;\r |
174 | buffer[maxw] = 0;\r |
e5f426aa |
175 | \r |
713c9224 |
176 | smalltext_out16_(x, y, buffer, color);\r |
e5f426aa |
177 | }\r |
178 | \r |
713c9224 |
179 | static void menu_draw_selection(int x, int y, int w)\r |
e5f426aa |
180 | {\r |
181 | int i, h;\r |
182 | unsigned short *dst, *dest;\r |
183 | \r |
184 | text_out16_(x, y, (void *)1, (menu_sel_color < 0) ? menu_text_color : menu_sel_color);\r |
185 | \r |
186 | if (menu_sel_color < 0) return; // no selection hilight\r |
187 | \r |
188 | if (y > 0) y--;\r |
c7eb229a |
189 | dest = (unsigned short *)g_menuscreen_ptr + x + y * g_menuscreen_w + me_mfont_w * 2 - 2;\r |
c6c6c9cd |
190 | for (h = me_mfont_h + 1; h > 0; h--)\r |
e5f426aa |
191 | {\r |
192 | dst = dest;\r |
be672de7 |
193 | for (i = w - (me_mfont_w * 2 - 2); i > 0; i--)\r |
e5f426aa |
194 | *dst++ = menu_sel_color;\r |
c7eb229a |
195 | dest += g_menuscreen_w;\r |
e5f426aa |
196 | }\r |
197 | }\r |
198 | \r |
199 | static int parse_hex_color(char *buff)\r |
200 | {\r |
201 | char *endp = buff;\r |
202 | int t = (int) strtoul(buff, &endp, 16);\r |
7d4906bf |
203 | if (endp != buff)\r |
204 | #ifdef PSP\r |
205 | return ((t<<8)&0xf800) | ((t>>5)&0x07e0) | ((t>>19)&0x1f);\r |
206 | #else\r |
207 | return ((t>>8)&0xf800) | ((t>>5)&0x07e0) | ((t>>3)&0x1f);\r |
208 | #endif\r |
e5f426aa |
209 | return -1;\r |
210 | }\r |
211 | \r |
212 | void menu_init(void)\r |
213 | {\r |
c6c6c9cd |
214 | int i, c, l;\r |
215 | unsigned char *fd, *fds;\r |
e5f426aa |
216 | char buff[256];\r |
217 | FILE *f;\r |
218 | \r |
c6c6c9cd |
219 | if (menu_font_data != NULL)\r |
220 | free(menu_font_data);\r |
221 | \r |
222 | menu_font_data = calloc((MENU_X2 ? 256 * 320 : 128 * 160) / 2, 1);\r |
223 | if (menu_font_data == NULL)\r |
224 | return;\r |
225 | \r |
226 | // generate default 8x10 font from fontdata8x8\r |
227 | for (c = 0, fd = menu_font_data; c < 256; c++)\r |
e5f426aa |
228 | {\r |
229 | for (l = 0; l < 8; l++)\r |
230 | {\r |
231 | unsigned char fd8x8 = fontdata8x8[c*8+l];\r |
c6c6c9cd |
232 | if (fd8x8&0x80) *fd = 0xf0;\r |
e5f426aa |
233 | if (fd8x8&0x40) *fd |= 0x0f; fd++;\r |
c6c6c9cd |
234 | if (fd8x8&0x20) *fd = 0xf0;\r |
e5f426aa |
235 | if (fd8x8&0x10) *fd |= 0x0f; fd++;\r |
c6c6c9cd |
236 | if (fd8x8&0x08) *fd = 0xf0;\r |
e5f426aa |
237 | if (fd8x8&0x04) *fd |= 0x0f; fd++;\r |
c6c6c9cd |
238 | if (fd8x8&0x02) *fd = 0xf0;\r |
e5f426aa |
239 | if (fd8x8&0x01) *fd |= 0x0f; fd++;\r |
240 | }\r |
241 | fd += 8*2/2; // 2 empty lines\r |
242 | }\r |
243 | \r |
c6c6c9cd |
244 | if (MENU_X2) {\r |
245 | // expand default font\r |
246 | fds = menu_font_data + 128 * 160 / 2 - 4;\r |
247 | fd = menu_font_data + 256 * 320 / 2 - 1;\r |
248 | for (c = 255; c >= 0; c--)\r |
249 | {\r |
250 | for (l = 9; l >= 0; l--, fds -= 4)\r |
251 | {\r |
252 | for (i = 3; i >= 0; i--) {\r |
253 | int px = fds[i] & 0x0f;\r |
254 | *fd-- = px | (px << 4);\r |
255 | px = (fds[i] >> 4) & 0x0f;\r |
256 | *fd-- = px | (px << 4);\r |
257 | }\r |
258 | for (i = 3; i >= 0; i--) {\r |
259 | int px = fds[i] & 0x0f;\r |
260 | *fd-- = px | (px << 4);\r |
261 | px = (fds[i] >> 4) & 0x0f;\r |
262 | *fd-- = px | (px << 4);\r |
263 | }\r |
264 | }\r |
265 | }\r |
266 | }\r |
267 | \r |
e5f426aa |
268 | // load custom font and selector (stored as 1st symbol in font table)\r |
27701801 |
269 | emu_make_path(buff, "skin/font.png", sizeof(buff));\r |
be672de7 |
270 | readpng(menu_font_data, buff, READPNG_FONT,\r |
271 | MENU_X2 ? 256 : 128, MENU_X2 ? 320 : 160);\r |
c6c6c9cd |
272 | // default selector symbol is '>'\r |
273 | memcpy(menu_font_data, menu_font_data + ((int)'>') * me_mfont_w * me_mfont_h / 2,\r |
274 | me_mfont_w * me_mfont_h / 2);\r |
27701801 |
275 | emu_make_path(buff, "skin/selector.png", sizeof(buff));\r |
74e1b42b |
276 | readpng(menu_font_data, buff, READPNG_SELECTOR, me_mfont_w, me_mfont_h);\r |
e5f426aa |
277 | \r |
278 | // load custom colors\r |
27701801 |
279 | emu_make_path(buff, "skin/skin.txt", sizeof(buff));\r |
280 | f = fopen(buff, "r");\r |
e5f426aa |
281 | if (f != NULL)\r |
282 | {\r |
283 | lprintf("found skin.txt\n");\r |
284 | while (!feof(f))\r |
285 | {\r |
b8a1c09a |
286 | if (fgets(buff, sizeof(buff), f) == NULL)\r |
287 | break;\r |
e5f426aa |
288 | if (buff[0] == '#' || buff[0] == '/') continue; // comment\r |
289 | if (buff[0] == '\r' || buff[0] == '\n') continue; // empty line\r |
290 | if (strncmp(buff, "text_color=", 11) == 0)\r |
291 | {\r |
292 | int tmp = parse_hex_color(buff+11);\r |
293 | if (tmp >= 0) menu_text_color = tmp;\r |
294 | else lprintf("skin.txt: parse error for text_color\n");\r |
295 | }\r |
296 | else if (strncmp(buff, "selection_color=", 16) == 0)\r |
297 | {\r |
298 | int tmp = parse_hex_color(buff+16);\r |
299 | if (tmp >= 0) menu_sel_color = tmp;\r |
300 | else lprintf("skin.txt: parse error for selection_color\n");\r |
301 | }\r |
302 | else\r |
303 | lprintf("skin.txt: parse error: %s\n", buff);\r |
304 | }\r |
305 | fclose(f);\r |
306 | }\r |
307 | }\r |
308 | \r |
45285368 |
309 | static void menu_draw_begin(int need_bg)\r |
310 | {\r |
311 | plat_video_menu_begin();\r |
312 | if (need_bg)\r |
313 | memcpy(g_menuscreen_ptr, g_menubg_ptr, g_menuscreen_w * g_menuscreen_h * 2);\r |
314 | }\r |
315 | \r |
316 | static void menu_draw_end(void)\r |
317 | {\r |
318 | plat_video_menu_end();\r |
319 | }\r |
e5f426aa |
320 | \r |
697746df |
321 | static void menu_darken_bg(void *dst, void *src, int pixels, int darker)\r |
322 | {\r |
323 | unsigned int *dest = dst;\r |
324 | unsigned int *sorc = src;\r |
325 | pixels /= 2;\r |
326 | if (darker)\r |
327 | {\r |
328 | while (pixels--)\r |
329 | {\r |
330 | unsigned int p = *sorc++;\r |
331 | *dest++ = ((p&0xf79ef79e)>>1) - ((p&0xc618c618)>>3);\r |
332 | }\r |
333 | }\r |
334 | else\r |
335 | {\r |
336 | while (pixels--)\r |
337 | {\r |
338 | unsigned int p = *sorc++;\r |
339 | *dest++ = (p&0xf79ef79e)>>1;\r |
340 | }\r |
341 | }\r |
342 | }\r |
343 | \r |
344 | static void menu_enter(int is_rom_loaded)\r |
345 | {\r |
346 | if (is_rom_loaded)\r |
347 | {\r |
348 | // darken the active framebuffer\r |
45285368 |
349 | menu_darken_bg(g_menubg_ptr, g_menubg_src_ptr, g_menuscreen_w * g_menuscreen_h, 1);\r |
697746df |
350 | }\r |
351 | else\r |
352 | {\r |
353 | char buff[256];\r |
354 | \r |
355 | // should really only happen once, on startup..\r |
356 | emu_make_path(buff, "skin/background.png", sizeof(buff));\r |
c7eb229a |
357 | if (readpng(g_menubg_ptr, buff, READPNG_BG, g_menuscreen_w, g_menuscreen_h) < 0)\r |
358 | memset(g_menubg_ptr, 0, g_menuscreen_w * g_menuscreen_h * 2);\r |
697746df |
359 | }\r |
360 | \r |
361 | plat_video_menu_enter(is_rom_loaded);\r |
362 | }\r |
363 | \r |
713c9224 |
364 | static int me_id2offset(const menu_entry *ent, menu_id id)\r |
e5f426aa |
365 | {\r |
366 | int i;\r |
1fb0dd88 |
367 | for (i = 0; ent->name; ent++, i++)\r |
368 | if (ent->id == id) return i;\r |
e5f426aa |
369 | \r |
370 | lprintf("%s: id %i not found\n", __FUNCTION__, id);\r |
371 | return 0;\r |
372 | }\r |
373 | \r |
713c9224 |
374 | static void me_enable(menu_entry *entries, menu_id id, int enable)\r |
e5f426aa |
375 | {\r |
1fb0dd88 |
376 | int i = me_id2offset(entries, id);\r |
e5f426aa |
377 | entries[i].enabled = enable;\r |
378 | }\r |
379 | \r |
713c9224 |
380 | static int me_count(const menu_entry *ent)\r |
e5f426aa |
381 | {\r |
1fb0dd88 |
382 | int ret;\r |
e5f426aa |
383 | \r |
1fb0dd88 |
384 | for (ret = 0; ent->name; ent++, ret++)\r |
385 | ;\r |
e5f426aa |
386 | \r |
387 | return ret;\r |
388 | }\r |
389 | \r |
61753a67 |
390 | static void me_draw(const menu_entry *entries, int sel, void (*draw_more)(void))\r |
1fb0dd88 |
391 | {\r |
be672de7 |
392 | const menu_entry *ent, *ent_sel = entries;\r |
1fb0dd88 |
393 | int x, y, w = 0, h = 0;\r |
093b8a42 |
394 | int offs, col2_offs = 27 * me_mfont_w;\r |
be672de7 |
395 | int vi_sel_ln = 0;\r |
1fb0dd88 |
396 | const char *name;\r |
1fb0dd88 |
397 | int i, n;\r |
398 | \r |
399 | /* calculate size of menu rect */\r |
400 | for (ent = entries, i = n = 0; ent->name; ent++, i++)\r |
401 | {\r |
402 | int wt;\r |
403 | \r |
404 | if (!ent->enabled)\r |
405 | continue;\r |
406 | \r |
be672de7 |
407 | if (i == sel) {\r |
408 | ent_sel = ent;\r |
409 | vi_sel_ln = n;\r |
410 | }\r |
1fb0dd88 |
411 | \r |
412 | name = NULL;\r |
c6c6c9cd |
413 | wt = strlen(ent->name) * me_mfont_w;\r |
1fb0dd88 |
414 | if (wt == 0 && ent->generate_name)\r |
713c9224 |
415 | name = ent->generate_name(ent->id, &offs);\r |
1fb0dd88 |
416 | if (name != NULL)\r |
c6c6c9cd |
417 | wt = strlen(name) * me_mfont_w;\r |
1fb0dd88 |
418 | \r |
419 | if (ent->beh != MB_NONE)\r |
420 | {\r |
093b8a42 |
421 | if (wt > col2_offs)\r |
422 | col2_offs = wt + me_mfont_w;\r |
423 | wt = col2_offs;\r |
1fb0dd88 |
424 | \r |
425 | switch (ent->beh) {\r |
74e1b42b |
426 | case MB_NONE:\r |
427 | break;\r |
1fb0dd88 |
428 | case MB_OPT_ONOFF:\r |
74e1b42b |
429 | case MB_OPT_RANGE:\r |
430 | wt += me_mfont_w * 3;\r |
431 | break;\r |
1fb0dd88 |
432 | case MB_OPT_CUSTOM:\r |
713c9224 |
433 | case MB_OPT_CUSTONOFF:\r |
434 | case MB_OPT_CUSTRANGE:\r |
1fb0dd88 |
435 | name = NULL;\r |
713c9224 |
436 | offs = 0;\r |
1fb0dd88 |
437 | if (ent->generate_name != NULL)\r |
713c9224 |
438 | name = ent->generate_name(ent->id, &offs);\r |
1fb0dd88 |
439 | if (name != NULL)\r |
c6c6c9cd |
440 | wt += (strlen(name) + offs) * me_mfont_w;\r |
1fb0dd88 |
441 | break;\r |
fcdefcf6 |
442 | case MB_OPT_ENUM:\r |
443 | wt += 10 * me_mfont_w;\r |
444 | break;\r |
1fb0dd88 |
445 | }\r |
446 | }\r |
447 | \r |
448 | if (wt > w)\r |
449 | w = wt;\r |
450 | n++;\r |
451 | }\r |
c6c6c9cd |
452 | h = n * me_mfont_h;\r |
453 | w += me_mfont_w * 2; /* selector */\r |
1fb0dd88 |
454 | \r |
c7eb229a |
455 | if (w > g_menuscreen_w) {\r |
456 | lprintf("width %d > %d\n", w, g_menuscreen_w);\r |
457 | w = g_menuscreen_w;\r |
1fb0dd88 |
458 | }\r |
c7eb229a |
459 | if (h > g_menuscreen_h) {\r |
460 | lprintf("height %d > %d\n", w, g_menuscreen_h);\r |
461 | h = g_menuscreen_h;\r |
1fb0dd88 |
462 | }\r |
463 | \r |
c7eb229a |
464 | x = g_menuscreen_w / 2 - w / 2;\r |
465 | y = g_menuscreen_h / 2 - h / 2;\r |
1fb0dd88 |
466 | \r |
467 | /* draw */\r |
45285368 |
468 | menu_draw_begin(1);\r |
be672de7 |
469 | menu_draw_selection(x, y + vi_sel_ln * me_mfont_h, w);\r |
c6c6c9cd |
470 | x += me_mfont_w * 2;\r |
1fb0dd88 |
471 | \r |
472 | for (ent = entries; ent->name; ent++)\r |
473 | {\r |
fcdefcf6 |
474 | const char **names;\r |
475 | int len;\r |
476 | \r |
1fb0dd88 |
477 | if (!ent->enabled)\r |
478 | continue;\r |
479 | \r |
480 | name = ent->name;\r |
481 | if (strlen(name) == 0) {\r |
482 | if (ent->generate_name)\r |
713c9224 |
483 | name = ent->generate_name(ent->id, &offs);\r |
1fb0dd88 |
484 | }\r |
485 | if (name != NULL)\r |
c6c6c9cd |
486 | text_out16(x, y, name);\r |
1fb0dd88 |
487 | \r |
488 | switch (ent->beh) {\r |
489 | case MB_NONE:\r |
490 | break;\r |
491 | case MB_OPT_ONOFF:\r |
093b8a42 |
492 | text_out16(x + col2_offs, y, (*(int *)ent->var & ent->mask) ? "ON" : "OFF");\r |
1fb0dd88 |
493 | break;\r |
494 | case MB_OPT_RANGE:\r |
093b8a42 |
495 | text_out16(x + col2_offs, y, "%i", *(int *)ent->var);\r |
1fb0dd88 |
496 | break;\r |
497 | case MB_OPT_CUSTOM:\r |
713c9224 |
498 | case MB_OPT_CUSTONOFF:\r |
499 | case MB_OPT_CUSTRANGE:\r |
1fb0dd88 |
500 | name = NULL;\r |
713c9224 |
501 | offs = 0;\r |
1fb0dd88 |
502 | if (ent->generate_name)\r |
713c9224 |
503 | name = ent->generate_name(ent->id, &offs);\r |
1fb0dd88 |
504 | if (name != NULL)\r |
093b8a42 |
505 | text_out16(x + col2_offs + offs * me_mfont_w, y, "%s", name);\r |
1fb0dd88 |
506 | break;\r |
fcdefcf6 |
507 | case MB_OPT_ENUM:\r |
508 | names = (const char **)ent->data;\r |
509 | offs = 0;\r |
510 | for (i = 0; names[i] != NULL; i++) {\r |
511 | len = strlen(names[i]);\r |
512 | if (len > 10)\r |
513 | offs = 10 - len - 2;\r |
514 | if (i == *(int *)ent->var) {\r |
515 | text_out16(x + col2_offs + offs * me_mfont_w, y, "%s", names[i]);\r |
516 | break;\r |
517 | }\r |
518 | }\r |
519 | break;\r |
1fb0dd88 |
520 | }\r |
521 | \r |
c6c6c9cd |
522 | y += me_mfont_h;\r |
1fb0dd88 |
523 | }\r |
524 | \r |
fcdefcf6 |
525 | /* display help or message if we have one */\r |
c7eb229a |
526 | h = (g_menuscreen_h - h) / 2; // bottom area height\r |
c6c6c9cd |
527 | if (menu_error_msg[0] != 0) {\r |
fcdefcf6 |
528 | if (h >= me_mfont_h + 4)\r |
c7eb229a |
529 | text_out16(5, g_menuscreen_h - me_mfont_h - 4, menu_error_msg);\r |
713c9224 |
530 | else\r |
531 | lprintf("menu msg doesn't fit!\n");\r |
532 | \r |
c6c6c9cd |
533 | if (plat_get_ticks_ms() - menu_error_time > 2048)\r |
534 | menu_error_msg[0] = 0;\r |
713c9224 |
535 | }\r |
be672de7 |
536 | else if (ent_sel->help != NULL) {\r |
537 | const char *tmp = ent_sel->help;\r |
fcdefcf6 |
538 | int l;\r |
539 | for (l = 0; tmp != NULL && *tmp != 0; l++)\r |
540 | tmp = strchr(tmp + 1, '\n');\r |
541 | if (h >= l * me_sfont_h + 4)\r |
be672de7 |
542 | for (tmp = ent_sel->help; l > 0; l--, tmp = strchr(tmp, '\n') + 1)\r |
c7eb229a |
543 | smalltext_out16(5, g_menuscreen_h - (l * me_sfont_h + 4), tmp, 0xffff);\r |
fcdefcf6 |
544 | }\r |
713c9224 |
545 | \r |
61753a67 |
546 | if (draw_more != NULL)\r |
547 | draw_more();\r |
548 | \r |
45285368 |
549 | menu_draw_end();\r |
e5f426aa |
550 | }\r |
551 | \r |
4b973b89 |
552 | static int me_process(menu_entry *entry, int is_next, int is_lr)\r |
e5f426aa |
553 | {\r |
fcdefcf6 |
554 | const char **names;\r |
4b973b89 |
555 | int c;\r |
e5f426aa |
556 | switch (entry->beh)\r |
557 | {\r |
1fb0dd88 |
558 | case MB_OPT_ONOFF:\r |
713c9224 |
559 | case MB_OPT_CUSTONOFF:\r |
e5f426aa |
560 | *(int *)entry->var ^= entry->mask;\r |
561 | return 1;\r |
1fb0dd88 |
562 | case MB_OPT_RANGE:\r |
713c9224 |
563 | case MB_OPT_CUSTRANGE:\r |
4b973b89 |
564 | c = is_lr ? 10 : 1;\r |
565 | *(int *)entry->var += is_next ? c : -c;\r |
713c9224 |
566 | if (*(int *)entry->var < (int)entry->min)\r |
567 | *(int *)entry->var = (int)entry->max;\r |
568 | if (*(int *)entry->var > (int)entry->max)\r |
569 | *(int *)entry->var = (int)entry->min;\r |
e5f426aa |
570 | return 1;\r |
fcdefcf6 |
571 | case MB_OPT_ENUM:\r |
572 | names = (const char **)entry->data;\r |
573 | for (c = 0; names[c] != NULL; c++)\r |
574 | ;\r |
575 | *(int *)entry->var += is_next ? 1 : -1;\r |
576 | if (*(int *)entry->var < 0)\r |
577 | *(int *)entry->var = 0;\r |
578 | if (*(int *)entry->var >= c)\r |
579 | *(int *)entry->var = c - 1;\r |
580 | return 1;\r |
e5f426aa |
581 | default:\r |
582 | return 0;\r |
583 | }\r |
584 | }\r |
585 | \r |
713c9224 |
586 | static void debug_menu_loop(void);\r |
587 | \r |
61753a67 |
588 | static void me_loop(menu_entry *menu, int *menu_sel, void (*draw_more)(void))\r |
1fb0dd88 |
589 | {\r |
590 | int ret, inp, sel = *menu_sel, menu_sel_max;\r |
591 | \r |
592 | menu_sel_max = me_count(menu) - 1;\r |
61753a67 |
593 | if (menu_sel_max < 0) {\r |
1fb0dd88 |
594 | lprintf("no enabled menu entries\n");\r |
595 | return;\r |
596 | }\r |
597 | \r |
abfc7703 |
598 | while ((!menu[sel].enabled || !menu[sel].selectable) && sel < menu_sel_max)\r |
1fb0dd88 |
599 | sel++;\r |
600 | \r |
601 | /* make sure action buttons are not pressed on entering menu */\r |
61753a67 |
602 | me_draw(menu, sel, NULL);\r |
1fb0dd88 |
603 | while (in_menu_wait_any(50) & (PBTN_MOK|PBTN_MBACK|PBTN_MENU));\r |
604 | \r |
605 | for (;;)\r |
606 | {\r |
61753a67 |
607 | me_draw(menu, sel, draw_more);\r |
713c9224 |
608 | inp = in_menu_wait(PBTN_UP|PBTN_DOWN|PBTN_LEFT|PBTN_RIGHT|\r |
609 | PBTN_MOK|PBTN_MBACK|PBTN_MENU|PBTN_L|PBTN_R, 70);\r |
610 | if (inp & (PBTN_MENU|PBTN_MBACK))\r |
611 | break;\r |
612 | \r |
1fb0dd88 |
613 | if (inp & PBTN_UP ) {\r |
614 | do {\r |
615 | sel--;\r |
616 | if (sel < 0)\r |
617 | sel = menu_sel_max;\r |
618 | }\r |
713c9224 |
619 | while (!menu[sel].enabled || !menu[sel].selectable);\r |
1fb0dd88 |
620 | }\r |
621 | if (inp & PBTN_DOWN) {\r |
622 | do {\r |
623 | sel++;\r |
624 | if (sel > menu_sel_max)\r |
625 | sel = 0;\r |
626 | }\r |
713c9224 |
627 | while (!menu[sel].enabled || !menu[sel].selectable);\r |
628 | }\r |
629 | \r |
630 | /* a bit hacky but oh well */\r |
631 | if ((inp & (PBTN_L|PBTN_R)) == (PBTN_L|PBTN_R))\r |
632 | debug_menu_loop();\r |
633 | \r |
4b973b89 |
634 | if (inp & (PBTN_LEFT|PBTN_RIGHT|PBTN_L|PBTN_R)) { /* multi choice */\r |
635 | if (me_process(&menu[sel], (inp & (PBTN_RIGHT|PBTN_R)) ? 1 : 0,\r |
636 | inp & (PBTN_L|PBTN_R)))\r |
713c9224 |
637 | continue;\r |
1fb0dd88 |
638 | }\r |
1fb0dd88 |
639 | \r |
fcdefcf6 |
640 | if (inp & (PBTN_MOK|PBTN_LEFT|PBTN_RIGHT|PBTN_L|PBTN_R))\r |
1fb0dd88 |
641 | {\r |
fcdefcf6 |
642 | /* require PBTN_MOK for MB_NONE */\r |
643 | if (menu[sel].handler != NULL && (menu[sel].beh != MB_NONE || (inp & PBTN_MOK))) {\r |
713c9224 |
644 | ret = menu[sel].handler(menu[sel].id, inp);\r |
1fb0dd88 |
645 | if (ret) break;\r |
fcdefcf6 |
646 | menu_sel_max = me_count(menu) - 1; /* might change, so update */\r |
1fb0dd88 |
647 | }\r |
648 | }\r |
1fb0dd88 |
649 | }\r |
650 | *menu_sel = sel;\r |
651 | }\r |
652 | \r |
653 | /* ***************************************** */\r |
654 | \r |
713c9224 |
655 | static void draw_menu_credits(void)\r |
656 | {\r |
657 | const char *creds, *p;\r |
658 | int x, y, h, w, wt;\r |
1fb0dd88 |
659 | \r |
713c9224 |
660 | p = creds = plat_get_credits();\r |
1fb0dd88 |
661 | \r |
713c9224 |
662 | for (h = 1, w = 0; *p != 0; h++) {\r |
663 | for (wt = 0; *p != 0 && *p != '\n'; p++)\r |
664 | wt++;\r |
1fb0dd88 |
665 | \r |
713c9224 |
666 | if (wt > w)\r |
667 | w = wt;\r |
668 | if (*p == 0)\r |
1fb0dd88 |
669 | break;\r |
713c9224 |
670 | p++;\r |
1fb0dd88 |
671 | }\r |
672 | \r |
c7eb229a |
673 | x = g_menuscreen_w / 2 - w * me_mfont_w / 2;\r |
674 | y = g_menuscreen_h / 2 - h * me_mfont_h / 2;\r |
713c9224 |
675 | if (x < 0) x = 0;\r |
676 | if (y < 0) y = 0;\r |
677 | \r |
45285368 |
678 | menu_draw_begin(1);\r |
713c9224 |
679 | \r |
c7eb229a |
680 | for (p = creds; *p != 0 && y <= g_menuscreen_h - me_mfont_h; y += me_mfont_h) {\r |
713c9224 |
681 | text_out16(x, y, p);\r |
682 | \r |
683 | for (; *p != 0 && *p != '\n'; p++)\r |
684 | ;\r |
685 | if (*p != 0)\r |
686 | p++;\r |
687 | }\r |
688 | \r |
45285368 |
689 | menu_draw_end();\r |
1fb0dd88 |
690 | }\r |
691 | \r |
991473ad |
692 | // -------------- del confirm ---------------\r |
693 | \r |
694 | static void do_delete(const char *fpath, const char *fname)\r |
695 | {\r |
696 | int len, mid, inp;\r |
697 | const char *nm;\r |
698 | char tmp[64];\r |
699 | \r |
45285368 |
700 | menu_draw_begin(1);\r |
991473ad |
701 | \r |
991473ad |
702 | len = strlen(fname);\r |
c7eb229a |
703 | if (len > g_menuscreen_w / me_sfont_w)\r |
704 | len = g_menuscreen_w / me_sfont_w;\r |
991473ad |
705 | \r |
c7eb229a |
706 | mid = g_menuscreen_w / 2;\r |
991473ad |
707 | text_out16(mid - me_mfont_w * 15 / 2, 8 * me_mfont_h, "About to delete");\r |
708 | smalltext_out16(mid - len * me_sfont_w / 2, 9 * me_mfont_h + 5, fname, 0xbdff);\r |
709 | text_out16(mid - me_mfont_w * 13 / 2, 11 * me_mfont_h, "Are you sure?");\r |
710 | \r |
711 | nm = in_get_key_name(-1, -PBTN_MA3);\r |
712 | snprintf(tmp, sizeof(tmp), "(%s - confirm, ", nm);\r |
713 | len = strlen(tmp);\r |
714 | nm = in_get_key_name(-1, -PBTN_MBACK);\r |
715 | snprintf(tmp + len, sizeof(tmp) - len, "%s - cancel)", nm);\r |
716 | len = strlen(tmp);\r |
717 | \r |
718 | text_out16(mid - me_mfont_w * len / 2, 12 * me_mfont_h, tmp);\r |
45285368 |
719 | menu_draw_end();\r |
991473ad |
720 | \r |
721 | while (in_menu_wait_any(50) & (PBTN_MENU|PBTN_MA2));\r |
722 | inp = in_menu_wait(PBTN_MA3|PBTN_MBACK, 100);\r |
723 | if (inp & PBTN_MA3)\r |
724 | remove(fpath);\r |
725 | }\r |
726 | \r |
713c9224 |
727 | // -------------- ROM selector --------------\r |
7b3f44c6 |
728 | \r |
713c9224 |
729 | // rrrr rggg gggb bbbb\r |
730 | static unsigned short file2color(const char *fname)\r |
731 | {\r |
732 | const char *ext = fname + strlen(fname) - 3;\r |
733 | static const char *rom_exts[] = { "zip", "bin", "smd", "gen", "iso", "cso", "cue" };\r |
734 | static const char *other_exts[] = { "gmv", "pat" };\r |
735 | int i;\r |
736 | \r |
737 | if (ext < fname) ext = fname;\r |
738 | for (i = 0; i < array_size(rom_exts); i++)\r |
739 | if (strcasecmp(ext, rom_exts[i]) == 0) return 0xbdff; // FIXME: mk defines\r |
740 | for (i = 0; i < array_size(other_exts); i++)\r |
741 | if (strcasecmp(ext, other_exts[i]) == 0) return 0xaff5;\r |
742 | return 0xffff;\r |
7b3f44c6 |
743 | }\r |
744 | \r |
713c9224 |
745 | static void draw_dirlist(char *curdir, struct dirent **namelist, int n, int sel)\r |
f579f7b8 |
746 | {\r |
cc33f567 |
747 | int max_cnt, start, i, x, pos;\r |
697746df |
748 | void *darken_ptr;\r |
f579f7b8 |
749 | \r |
c7eb229a |
750 | max_cnt = g_menuscreen_h / me_sfont_h;\r |
713c9224 |
751 | start = max_cnt / 2 - sel;\r |
752 | n--; // exclude current dir (".")\r |
f579f7b8 |
753 | \r |
45285368 |
754 | menu_draw_begin(1);\r |
713c9224 |
755 | \r |
756 | // if (!rom_loaded)\r |
757 | // menu_darken_bg(gp2x_screen, 320*240, 0);\r |
758 | \r |
c7eb229a |
759 | darken_ptr = (short *)g_menuscreen_ptr + g_menuscreen_w * max_cnt/2 * me_sfont_h;\r |
760 | menu_darken_bg(darken_ptr, darken_ptr, g_menuscreen_w * me_sfont_h * 8 / 10, 0);\r |
713c9224 |
761 | \r |
cc33f567 |
762 | x = 5 + me_mfont_w + 1;\r |
713c9224 |
763 | if (start - 2 >= 0)\r |
cc33f567 |
764 | smalltext_out16(14, (start - 2) * me_sfont_h, curdir, 0xffff);\r |
713c9224 |
765 | for (i = 0; i < n; i++) {\r |
766 | pos = start + i;\r |
767 | if (pos < 0) continue;\r |
768 | if (pos >= max_cnt) break;\r |
769 | if (namelist[i+1]->d_type == DT_DIR) {\r |
cc33f567 |
770 | smalltext_out16(x, pos * me_sfont_h, "/", 0xfff6);\r |
771 | smalltext_out16(x + me_sfont_w, pos * me_sfont_h, namelist[i+1]->d_name, 0xfff6);\r |
713c9224 |
772 | } else {\r |
773 | unsigned short color = file2color(namelist[i+1]->d_name);\r |
cc33f567 |
774 | smalltext_out16(x, pos * me_sfont_h, namelist[i+1]->d_name, color);\r |
713c9224 |
775 | }\r |
f579f7b8 |
776 | }\r |
cc33f567 |
777 | smalltext_out16(5, max_cnt/2 * me_sfont_h, ">", 0xffff);\r |
45285368 |
778 | menu_draw_end();\r |
f579f7b8 |
779 | }\r |
780 | \r |
713c9224 |
781 | static int scandir_cmp(const void *p1, const void *p2)\r |
f579f7b8 |
782 | {\r |
b8a1c09a |
783 | const struct dirent **d1 = (const struct dirent **)p1;\r |
784 | const struct dirent **d2 = (const struct dirent **)p2;\r |
785 | if ((*d1)->d_type == (*d2)->d_type)\r |
786 | return alphasort(d1, d2);\r |
787 | if ((*d1)->d_type == DT_DIR)\r |
788 | return -1; // put before\r |
789 | if ((*d2)->d_type == DT_DIR)\r |
790 | return 1;\r |
791 | \r |
713c9224 |
792 | return alphasort(d1, d2);\r |
793 | }\r |
f579f7b8 |
794 | \r |
713c9224 |
795 | static const char *filter_exts[] = {\r |
796 | ".mp3", ".MP3", ".srm", ".brm", "s.gz", ".mds", "bcfg", ".txt", ".htm", "html",\r |
797 | ".jpg", ".gpe"\r |
798 | };\r |
799 | \r |
800 | static int scandir_filter(const struct dirent *ent)\r |
801 | {\r |
802 | const char *p;\r |
803 | int i;\r |
804 | \r |
805 | if (ent == NULL || ent->d_name == NULL) return 0;\r |
806 | if (strlen(ent->d_name) < 5) return 1;\r |
807 | \r |
808 | p = ent->d_name + strlen(ent->d_name) - 4;\r |
809 | \r |
810 | for (i = 0; i < array_size(filter_exts); i++)\r |
811 | if (strcmp(p, filter_exts[i]) == 0)\r |
812 | return 0;\r |
813 | \r |
814 | return 1;\r |
f579f7b8 |
815 | }\r |
816 | \r |
713c9224 |
817 | static char *menu_loop_romsel(char *curr_path, int len)\r |
f579f7b8 |
818 | {\r |
713c9224 |
819 | struct dirent **namelist;\r |
820 | int n, inp, sel = 0;\r |
821 | char *ret = NULL, *fname = NULL;\r |
822 | \r |
823 | rescan:\r |
824 | // is this a dir or a full path?\r |
825 | if (!plat_is_dir(curr_path)) {\r |
826 | char *p = curr_path + strlen(curr_path) - 1;\r |
827 | for (; p > curr_path && *p != '/'; p--)\r |
828 | ;\r |
829 | *p = 0;\r |
830 | fname = p+1;\r |
831 | }\r |
f579f7b8 |
832 | \r |
b8a1c09a |
833 | n = scandir(curr_path, &namelist, scandir_filter, (void *)scandir_cmp);\r |
713c9224 |
834 | if (n < 0) {\r |
8beb4430 |
835 | char *t;\r |
713c9224 |
836 | lprintf("menu_loop_romsel failed, dir: %s\n", curr_path);\r |
837 | \r |
838 | // try root\r |
8beb4430 |
839 | t = getcwd(curr_path, len);\r |
840 | if (t == NULL)\r |
841 | plat_get_root_dir(curr_path, len);\r |
b8a1c09a |
842 | n = scandir(curr_path, &namelist, scandir_filter, (void *)scandir_cmp);\r |
713c9224 |
843 | if (n < 0) {\r |
844 | // oops, we failed\r |
845 | lprintf("menu_loop_romsel failed, dir: %s\n", curr_path);\r |
846 | return NULL;\r |
847 | }\r |
848 | }\r |
849 | \r |
850 | // try to find sel\r |
851 | if (fname != NULL) {\r |
852 | int i;\r |
853 | for (i = 1; i < n; i++) {\r |
854 | if (strcmp(namelist[i]->d_name, fname) == 0) {\r |
855 | sel = i - 1;\r |
f579f7b8 |
856 | break;\r |
713c9224 |
857 | }\r |
f579f7b8 |
858 | }\r |
713c9224 |
859 | }\r |
f579f7b8 |
860 | \r |
2aaac89c |
861 | /* make sure action buttons are not pressed on entering menu */\r |
862 | draw_dirlist(curr_path, namelist, n, sel);\r |
863 | while (in_menu_wait_any(50) & (PBTN_MOK|PBTN_MBACK|PBTN_MENU))\r |
864 | ;\r |
865 | \r |
713c9224 |
866 | for (;;)\r |
867 | {\r |
868 | draw_dirlist(curr_path, namelist, n, sel);\r |
869 | inp = in_menu_wait(PBTN_UP|PBTN_DOWN|PBTN_LEFT|PBTN_RIGHT|\r |
e2de9939 |
870 | PBTN_L|PBTN_R|PBTN_MA2|PBTN_MOK|PBTN_MBACK|PBTN_MENU, 33);\r |
713c9224 |
871 | if (inp & PBTN_UP ) { sel--; if (sel < 0) sel = n-2; }\r |
872 | if (inp & PBTN_DOWN) { sel++; if (sel > n-2) sel = 0; }\r |
873 | if (inp & PBTN_LEFT) { sel-=10; if (sel < 0) sel = 0; }\r |
874 | if (inp & PBTN_L) { sel-=24; if (sel < 0) sel = 0; }\r |
875 | if (inp & PBTN_RIGHT) { sel+=10; if (sel > n-2) sel = n-2; }\r |
876 | if (inp & PBTN_R) { sel+=24; if (sel > n-2) sel = n-2; }\r |
e2de9939 |
877 | if ((inp & PBTN_MOK) || (inp & (PBTN_MENU|PBTN_MA2)) == (PBTN_MENU|PBTN_MA2))\r |
f579f7b8 |
878 | {\r |
713c9224 |
879 | again:\r |
880 | if (namelist[sel+1]->d_type == DT_REG)\r |
881 | {\r |
882 | strcpy(rom_fname_reload, curr_path);\r |
883 | strcat(rom_fname_reload, "/");\r |
884 | strcat(rom_fname_reload, namelist[sel+1]->d_name);\r |
885 | if (inp & PBTN_MOK) { // return sel\r |
886 | ret = rom_fname_reload;\r |
887 | break;\r |
7b3f44c6 |
888 | }\r |
991473ad |
889 | do_delete(rom_fname_reload, namelist[sel+1]->d_name);\r |
713c9224 |
890 | if (n > 0) {\r |
891 | while (n--) free(namelist[n]);\r |
892 | free(namelist);\r |
893 | }\r |
894 | goto rescan;\r |
895 | }\r |
896 | else if (namelist[sel+1]->d_type == DT_DIR)\r |
897 | {\r |
898 | int newlen;\r |
899 | char *p, *newdir;\r |
900 | if (!(inp & PBTN_MOK)) continue;\r |
901 | newlen = strlen(curr_path) + strlen(namelist[sel+1]->d_name) + 2;\r |
902 | newdir = malloc(newlen);\r |
903 | if (strcmp(namelist[sel+1]->d_name, "..") == 0) {\r |
904 | char *start = curr_path;\r |
905 | p = start + strlen(start) - 1;\r |
906 | while (*p == '/' && p > start) p--;\r |
907 | while (*p != '/' && p > start) p--;\r |
908 | if (p <= start) strcpy(newdir, "/");\r |
909 | else { strncpy(newdir, start, p-start); newdir[p-start] = 0; }\r |
910 | } else {\r |
911 | strcpy(newdir, curr_path);\r |
912 | p = newdir + strlen(newdir) - 1;\r |
913 | while (*p == '/' && p >= newdir) *p-- = 0;\r |
914 | strcat(newdir, "/");\r |
915 | strcat(newdir, namelist[sel+1]->d_name);\r |
f579f7b8 |
916 | }\r |
713c9224 |
917 | ret = menu_loop_romsel(newdir, newlen);\r |
918 | free(newdir);\r |
f579f7b8 |
919 | break;\r |
713c9224 |
920 | }\r |
921 | else\r |
922 | {\r |
923 | // unknown file type, happens on NTFS mounts. Try to guess.\r |
924 | FILE *tstf; int tmp;\r |
925 | strcpy(rom_fname_reload, curr_path);\r |
926 | strcat(rom_fname_reload, "/");\r |
927 | strcat(rom_fname_reload, namelist[sel+1]->d_name);\r |
928 | tstf = fopen(rom_fname_reload, "rb");\r |
929 | if (tstf != NULL)\r |
930 | {\r |
931 | if (fread(&tmp, 1, 1, tstf) > 0 || ferror(tstf) == 0)\r |
932 | namelist[sel+1]->d_type = DT_REG;\r |
933 | else namelist[sel+1]->d_type = DT_DIR;\r |
934 | fclose(tstf);\r |
935 | goto again;\r |
936 | }\r |
937 | }\r |
938 | }\r |
939 | if (inp & PBTN_MBACK)\r |
940 | break;\r |
941 | }\r |
942 | \r |
943 | if (n > 0) {\r |
944 | while (n--) free(namelist[n]);\r |
945 | free(namelist);\r |
946 | }\r |
947 | \r |
948 | return ret;\r |
949 | }\r |
950 | \r |
713c9224 |
951 | // ------------ savestate loader ------------\r |
952 | \r |
953 | static int state_slot_flags = 0;\r |
954 | \r |
955 | static void state_check_slots(void)\r |
956 | {\r |
957 | int slot;\r |
958 | \r |
959 | state_slot_flags = 0;\r |
960 | \r |
961 | for (slot = 0; slot < 10; slot++) {\r |
a47dd663 |
962 | if (emu_check_save_file(slot))\r |
713c9224 |
963 | state_slot_flags |= 1 << slot;\r |
964 | }\r |
965 | }\r |
966 | \r |
d2fd8a7a |
967 | static void draw_savestate_bg(int slot);\r |
713c9224 |
968 | \r |
969 | static void draw_savestate_menu(int menu_sel, int is_loading)\r |
970 | {\r |
971 | int i, x, y, w, h;\r |
972 | \r |
973 | if (state_slot_flags & (1 << menu_sel))\r |
974 | draw_savestate_bg(menu_sel);\r |
975 | \r |
cc33f567 |
976 | w = (13 + 2) * me_mfont_w;\r |
c6c6c9cd |
977 | h = (1+2+10+1) * me_mfont_h;\r |
c7eb229a |
978 | x = g_menuscreen_w / 2 - w / 2;\r |
713c9224 |
979 | if (x < 0) x = 0;\r |
c7eb229a |
980 | y = g_menuscreen_h / 2 - h / 2;\r |
713c9224 |
981 | if (y < 0) y = 0;\r |
982 | \r |
45285368 |
983 | menu_draw_begin(1);\r |
713c9224 |
984 | \r |
985 | text_out16(x, y, is_loading ? "Load state" : "Save state");\r |
c6c6c9cd |
986 | y += 3 * me_mfont_h;\r |
713c9224 |
987 | \r |
abfc7703 |
988 | menu_draw_selection(x - me_mfont_w * 2, y + menu_sel * me_mfont_h, (13 + 2) * me_mfont_w + 4);\r |
713c9224 |
989 | \r |
990 | /* draw all 10 slots */\r |
c6c6c9cd |
991 | for (i = 0; i < 10; i++, y += me_mfont_h)\r |
713c9224 |
992 | {\r |
993 | text_out16(x, y, "SLOT %i (%s)", i, (state_slot_flags & (1 << i)) ? "USED" : "free");\r |
994 | }\r |
995 | text_out16(x, y, "back");\r |
996 | \r |
45285368 |
997 | menu_draw_end();\r |
713c9224 |
998 | }\r |
999 | \r |
1000 | static int menu_loop_savestate(int is_loading)\r |
1001 | {\r |
1002 | static int menu_sel = 10;\r |
1003 | int menu_sel_max = 10;\r |
1004 | unsigned long inp = 0;\r |
b4db550e |
1005 | int ret = 0;\r |
713c9224 |
1006 | \r |
1007 | state_check_slots();\r |
1008 | \r |
b3158e98 |
1009 | if (!(state_slot_flags & (1 << menu_sel)) && is_loading)\r |
1010 | menu_sel = menu_sel_max;\r |
1011 | \r |
713c9224 |
1012 | for (;;)\r |
1013 | {\r |
1014 | draw_savestate_menu(menu_sel, is_loading);\r |
1015 | inp = in_menu_wait(PBTN_UP|PBTN_DOWN|PBTN_MOK|PBTN_MBACK, 100);\r |
1016 | if (inp & PBTN_UP) {\r |
1017 | do {\r |
1018 | menu_sel--;\r |
1019 | if (menu_sel < 0)\r |
1020 | menu_sel = menu_sel_max;\r |
1021 | } while (!(state_slot_flags & (1 << menu_sel)) && menu_sel != menu_sel_max && is_loading);\r |
1022 | }\r |
1023 | if (inp & PBTN_DOWN) {\r |
1024 | do {\r |
1025 | menu_sel++;\r |
1026 | if (menu_sel > menu_sel_max)\r |
1027 | menu_sel = 0;\r |
1028 | } while (!(state_slot_flags & (1 << menu_sel)) && menu_sel != menu_sel_max && is_loading);\r |
1029 | }\r |
1030 | if (inp & PBTN_MOK) { // save/load\r |
1031 | if (menu_sel < 10) {\r |
1032 | state_slot = menu_sel;\r |
a47dd663 |
1033 | if (emu_save_load_game(is_loading, 0)) {\r |
c6c6c9cd |
1034 | me_update_msg(is_loading ? "Load failed" : "Save failed");\r |
b4db550e |
1035 | break;\r |
713c9224 |
1036 | }\r |
b4db550e |
1037 | ret = 1;\r |
1038 | break;\r |
713c9224 |
1039 | }\r |
b4db550e |
1040 | break;\r |
713c9224 |
1041 | }\r |
1042 | if (inp & PBTN_MBACK)\r |
b4db550e |
1043 | break;\r |
713c9224 |
1044 | }\r |
b4db550e |
1045 | \r |
1046 | return ret;\r |
713c9224 |
1047 | }\r |
1048 | \r |
1049 | // -------------- key config --------------\r |
1050 | \r |
1051 | static char *action_binds(int player_idx, int action_mask, int dev_id)\r |
1052 | {\r |
45285368 |
1053 | int k, count = 0, can_combo = 0, type;\r |
713c9224 |
1054 | const int *binds;\r |
713c9224 |
1055 | \r |
1056 | static_buff[0] = 0;\r |
1057 | \r |
1058 | binds = in_get_dev_binds(dev_id);\r |
1059 | if (binds == NULL)\r |
1060 | return static_buff;\r |
1061 | \r |
45285368 |
1062 | in_get_config(dev_id, IN_CFG_BIND_COUNT, &count);\r |
1063 | in_get_config(dev_id, IN_CFG_DOES_COMBOS, &can_combo);\r |
093b8a42 |
1064 | \r |
1065 | type = IN_BINDTYPE_EMU;\r |
1066 | if (player_idx >= 0) {\r |
1067 | can_combo = 0;\r |
1068 | type = IN_BINDTYPE_PLAYER12;\r |
1069 | }\r |
1070 | if (player_idx == 1)\r |
1071 | action_mask <<= 16;\r |
1072 | \r |
713c9224 |
1073 | for (k = 0; k < count; k++)\r |
1074 | {\r |
1075 | const char *xname;\r |
cc33f567 |
1076 | int len;\r |
713c9224 |
1077 | \r |
093b8a42 |
1078 | if (!(binds[IN_BIND_OFFS(k, type)] & action_mask))\r |
713c9224 |
1079 | continue;\r |
1080 | \r |
1081 | xname = in_get_key_name(dev_id, k);\r |
cc33f567 |
1082 | len = strlen(static_buff);\r |
1083 | if (len) {\r |
093b8a42 |
1084 | strncat(static_buff, can_combo ? " + " : ", ",\r |
1085 | sizeof(static_buff) - len - 1);\r |
1086 | len += can_combo ? 3 : 2;\r |
cc33f567 |
1087 | }\r |
1088 | strncat(static_buff, xname, sizeof(static_buff) - len - 1);\r |
713c9224 |
1089 | }\r |
1090 | \r |
1091 | return static_buff;\r |
1092 | }\r |
1093 | \r |
093b8a42 |
1094 | static int count_bound_keys(int dev_id, int action_mask, int bindtype)\r |
713c9224 |
1095 | {\r |
1096 | const int *binds;\r |
1097 | int k, keys = 0;\r |
45285368 |
1098 | int count = 0;\r |
713c9224 |
1099 | \r |
1100 | binds = in_get_dev_binds(dev_id);\r |
1101 | if (binds == NULL)\r |
1102 | return 0;\r |
1103 | \r |
45285368 |
1104 | in_get_config(dev_id, IN_CFG_BIND_COUNT, &count);\r |
713c9224 |
1105 | for (k = 0; k < count; k++)\r |
1106 | {\r |
093b8a42 |
1107 | if (binds[IN_BIND_OFFS(k, bindtype)] & action_mask)\r |
1108 | keys++;\r |
713c9224 |
1109 | }\r |
1110 | \r |
1111 | return keys;\r |
1112 | }\r |
1113 | \r |
1114 | static void draw_key_config(const me_bind_action *opts, int opt_cnt, int player_idx,\r |
1115 | int sel, int dev_id, int dev_count, int is_bind)\r |
1116 | {\r |
37807164 |
1117 | char buff[64], buff2[32];\r |
713c9224 |
1118 | const char *dev_name;\r |
abfc7703 |
1119 | int x, y, w, i;\r |
713c9224 |
1120 | \r |
abfc7703 |
1121 | w = ((player_idx >= 0) ? 20 : 30) * me_mfont_w;\r |
c7eb229a |
1122 | x = g_menuscreen_w / 2 - w / 2;\r |
1123 | y = (g_menuscreen_h - 4 * me_mfont_h) / 2 - (2 + opt_cnt) * me_mfont_h / 2;\r |
c6c6c9cd |
1124 | if (x < me_mfont_w * 2)\r |
1125 | x = me_mfont_w * 2;\r |
713c9224 |
1126 | \r |
45285368 |
1127 | menu_draw_begin(1);\r |
713c9224 |
1128 | if (player_idx >= 0)\r |
c6c6c9cd |
1129 | text_out16(x, y, "Player %i controls", player_idx + 1);\r |
713c9224 |
1130 | else\r |
c6c6c9cd |
1131 | text_out16(x, y, "Emulator controls");\r |
713c9224 |
1132 | \r |
c6c6c9cd |
1133 | y += 2 * me_mfont_h;\r |
abfc7703 |
1134 | menu_draw_selection(x - me_mfont_w * 2, y + sel * me_mfont_h, w + 2 * me_mfont_w);\r |
713c9224 |
1135 | \r |
c6c6c9cd |
1136 | for (i = 0; i < opt_cnt; i++, y += me_mfont_h)\r |
713c9224 |
1137 | text_out16(x, y, "%s : %s", opts[i].name,\r |
1138 | action_binds(player_idx, opts[i].mask, dev_id));\r |
1139 | \r |
1140 | dev_name = in_get_dev_name(dev_id, 1, 1);\r |
c6c6c9cd |
1141 | w = strlen(dev_name) * me_mfont_w;\r |
1142 | if (w < 30 * me_mfont_w)\r |
1143 | w = 30 * me_mfont_w;\r |
c7eb229a |
1144 | if (w > g_menuscreen_w)\r |
1145 | w = g_menuscreen_w;\r |
713c9224 |
1146 | \r |
c7eb229a |
1147 | x = g_menuscreen_w / 2 - w / 2;\r |
713c9224 |
1148 | \r |
37807164 |
1149 | if (!is_bind) {\r |
1150 | snprintf(buff2, sizeof(buff2), "%s", in_get_key_name(-1, -PBTN_MOK));\r |
1151 | snprintf(buff, sizeof(buff), "%s - bind, %s - clear", buff2,\r |
1152 | in_get_key_name(-1, -PBTN_MA2));\r |
c7eb229a |
1153 | text_out16(x, g_menuscreen_h - 4 * me_mfont_h, buff);\r |
713c9224 |
1154 | }\r |
37807164 |
1155 | else\r |
c7eb229a |
1156 | text_out16(x, g_menuscreen_h - 4 * me_mfont_h, "Press a button to bind/unbind");\r |
713c9224 |
1157 | \r |
37807164 |
1158 | if (dev_count > 1) {\r |
c7eb229a |
1159 | text_out16(x, g_menuscreen_h - 3 * me_mfont_h, dev_name);\r |
1160 | text_out16(x, g_menuscreen_h - 2 * me_mfont_h, "Press left/right for other devs");\r |
37807164 |
1161 | }\r |
713c9224 |
1162 | \r |
45285368 |
1163 | menu_draw_end();\r |
713c9224 |
1164 | }\r |
1165 | \r |
1166 | static void key_config_loop(const me_bind_action *opts, int opt_cnt, int player_idx)\r |
1167 | {\r |
45285368 |
1168 | int i, sel = 0, menu_sel_max = opt_cnt - 1, does_combos = 0;\r |
093b8a42 |
1169 | int dev_id, dev_count, kc, is_down, mkey;\r |
37807164 |
1170 | int unbind, bindtype, mask_shift;\r |
713c9224 |
1171 | \r |
1172 | for (i = 0, dev_id = -1, dev_count = 0; i < IN_MAX_DEVS; i++) {\r |
1173 | if (in_get_dev_name(i, 1, 0) != NULL) {\r |
1174 | dev_count++;\r |
1175 | if (dev_id < 0)\r |
1176 | dev_id = i;\r |
1177 | }\r |
1178 | }\r |
1179 | \r |
1180 | if (dev_id == -1) {\r |
1181 | lprintf("no devs, can't do config\n");\r |
1182 | return;\r |
1183 | }\r |
1184 | \r |
37807164 |
1185 | mask_shift = 0;\r |
1186 | if (player_idx == 1)\r |
1187 | mask_shift = 16;\r |
1188 | bindtype = player_idx >= 0 ? IN_BINDTYPE_PLAYER12 : IN_BINDTYPE_EMU;\r |
1189 | \r |
713c9224 |
1190 | for (;;)\r |
1191 | {\r |
1192 | draw_key_config(opts, opt_cnt, player_idx, sel, dev_id, dev_count, 0);\r |
37807164 |
1193 | mkey = in_menu_wait(PBTN_UP|PBTN_DOWN|PBTN_LEFT|PBTN_RIGHT|PBTN_MBACK|PBTN_MOK|PBTN_MA2, 100);\r |
713c9224 |
1194 | switch (mkey) {\r |
1195 | case PBTN_UP: sel--; if (sel < 0) sel = menu_sel_max; continue;\r |
1196 | case PBTN_DOWN: sel++; if (sel > menu_sel_max) sel = 0; continue;\r |
1197 | case PBTN_LEFT:\r |
1198 | for (i = 0, dev_id--; i < IN_MAX_DEVS; i++, dev_id--) {\r |
1199 | if (dev_id < 0)\r |
1200 | dev_id = IN_MAX_DEVS - 1;\r |
1201 | if (in_get_dev_name(dev_id, 1, 0) != NULL)\r |
1202 | break;\r |
1203 | }\r |
1204 | continue;\r |
1205 | case PBTN_RIGHT:\r |
1206 | for (i = 0, dev_id++; i < IN_MAX_DEVS; i++, dev_id++) {\r |
1207 | if (dev_id >= IN_MAX_DEVS)\r |
1208 | dev_id = 0;\r |
1209 | if (in_get_dev_name(dev_id, 1, 0) != NULL)\r |
1210 | break;\r |
1211 | }\r |
1212 | continue;\r |
7090b784 |
1213 | case PBTN_MBACK:\r |
1214 | return;\r |
713c9224 |
1215 | case PBTN_MOK:\r |
1216 | if (sel >= opt_cnt)\r |
1217 | return;\r |
7090b784 |
1218 | while (in_menu_wait_any(30) & PBTN_MOK)\r |
1219 | ;\r |
713c9224 |
1220 | break;\r |
37807164 |
1221 | case PBTN_MA2:\r |
1222 | in_unbind_all(dev_id, opts[sel].mask << mask_shift, bindtype);\r |
1223 | continue;\r |
713c9224 |
1224 | default:continue;\r |
1225 | }\r |
1226 | \r |
1227 | draw_key_config(opts, opt_cnt, player_idx, sel, dev_id, dev_count, 1);\r |
1228 | \r |
1229 | /* wait for some up event */\r |
1230 | for (is_down = 1; is_down; )\r |
1231 | kc = in_update_keycode(&dev_id, &is_down, -1);\r |
1232 | \r |
37807164 |
1233 | i = count_bound_keys(dev_id, opts[sel].mask << mask_shift, bindtype);\r |
9025b931 |
1234 | unbind = (i > 0);\r |
1235 | \r |
1236 | /* allow combos if device supports them */\r |
45285368 |
1237 | in_get_config(dev_id, IN_CFG_DOES_COMBOS, &does_combos);\r |
1238 | if (i == 1 && bindtype == IN_BINDTYPE_EMU && does_combos)\r |
9025b931 |
1239 | unbind = 0;\r |
713c9224 |
1240 | \r |
7090b784 |
1241 | if (unbind)\r |
1242 | in_unbind_all(dev_id, opts[sel].mask << mask_shift, bindtype);\r |
1243 | \r |
1244 | in_bind_key(dev_id, kc, opts[sel].mask << mask_shift, bindtype, 0);\r |
713c9224 |
1245 | }\r |
1246 | }\r |
1247 | \r |