menu.c 32 KB

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