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