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