cleanups, savestates, input (combos), menu
[fceu.git] / drivers / gp2x / menu.c
1 // (c) Copyright 2006,2007 notaz, All rights reserved.\r
2 // Free for non-commercial use.\r
3 \r
4 // For commercial use, separate licencing terms must be obtained.\r
5 \r
6 #include <stdio.h>\r
7 #include <string.h>\r
8 #include <stdlib.h>\r
9 #include <stdarg.h>\r
10 #include <unistd.h>\r
11 #include <dirent.h>\r
12 \r
13 #include "minimal.h"\r
14 #include "usbjoy.h"\r
15 #include "asmutils.h"\r
16 #include "menu.h"\r
17 #include "main.h"\r
18 #include "fonts.h"\r
19 #include "gp2x.h"\r
20 \r
21 #include "../../input.h"\r
22 #include "../../state.h"\r
23 \r
24 #ifndef _DIRENT_HAVE_D_TYPE\r
25 #error "need d_type for file browser\r
26 #endif\r
27 \r
28 static int GP2X_PORT_REV =\r
29 #include "rev.h"\r
30 ;\r
31 \r
32 extern char lastLoadedGameName[PATH_MAX];\r
33 extern int mmuhack_status;\r
34 extern int soundvol;\r
35 extern uint8 Exit; // exit emu loop flag\r
36 extern int InitSound(void);\r
37 \r
38 #define CONFIGURABLE_KEYS (GP2X_UP|GP2X_LEFT|GP2X_DOWN|GP2X_RIGHT|GP2X_START|GP2X_SELECT|GP2X_L|GP2X_R|GP2X_A|GP2X_B|GP2X_X|GP2X_Y|GP2X_PUSH)\r
39 \r
40 static char *gp2xKeyNames[] = {\r
41         "UP",    "01???",  "LEFT", "03???", "DOWN", "05???", "RIGHT",    "07???",\r
42         "START", "SELECT", "L",    "R",     "A",    "B",     "X",        "Y",\r
43         "10???", "11???",  "12???","13???", "14???","15???", "VOL DOWN", "VOL UP",\r
44         "18???", "19???",  "1a???","PUSH",  "1c???","1d???", "1e???",    "1f???"\r
45 };\r
46 \r
47 char menuErrorMsg[40] = {0, };\r
48 \r
49 // TODO\r
50 void gp2x_fceu_copy_bg(void)\r
51 {\r
52         memset(gp2x_screen, 0, 320*240*2);\r
53 }\r
54 \r
55 // draws white text to current bbp15 screen\r
56 static void gp2x_text_out15_(int x, int y, const char *text)\r
57 {\r
58         int i,l;\r
59         unsigned short *screen = gp2x_screen;\r
60 \r
61         screen = screen + x + y*320;\r
62 \r
63         for (i = 0; i < strlen(text); i++)\r
64         {\r
65                 for (l=0;l<8;l++)\r
66                 {\r
67                         if(fontdata8x8[((text[i])*8)+l]&0x80) screen[l*320+0]=0xffff;\r
68                         if(fontdata8x8[((text[i])*8)+l]&0x40) screen[l*320+1]=0xffff;\r
69                         if(fontdata8x8[((text[i])*8)+l]&0x20) screen[l*320+2]=0xffff;\r
70                         if(fontdata8x8[((text[i])*8)+l]&0x10) screen[l*320+3]=0xffff;\r
71                         if(fontdata8x8[((text[i])*8)+l]&0x08) screen[l*320+4]=0xffff;\r
72                         if(fontdata8x8[((text[i])*8)+l]&0x04) screen[l*320+5]=0xffff;\r
73                         if(fontdata8x8[((text[i])*8)+l]&0x02) screen[l*320+6]=0xffff;\r
74                         if(fontdata8x8[((text[i])*8)+l]&0x01) screen[l*320+7]=0xffff;\r
75                 }\r
76                 screen += 8;\r
77         }\r
78 }\r
79 \r
80 void gp2x_text_out15(int x, int y, const char *texto, ...)\r
81 {\r
82         va_list args;\r
83         char    buffer[512];\r
84 \r
85         va_start(args,texto);\r
86         vsprintf(buffer,texto,args);\r
87         va_end(args);\r
88 \r
89         gp2x_text_out15_(x,y,buffer);\r
90 }\r
91 \r
92 \r
93 void gp2x_text_out15_lim(int x, int y, const char *texto, int max)\r
94 {\r
95         char    buffer[320/8+1];\r
96 \r
97         strncpy(buffer, texto, 320/8);\r
98         if (max > 320/8) max = 320/8;\r
99         if (max < 0) max = 0;\r
100         buffer[max] = 0;\r
101 \r
102         gp2x_text_out15(x,y,buffer);\r
103 }\r
104 \r
105 static void gp2x_smalltext16(int x, int y, const char *texto)\r
106 {\r
107         int i;\r
108         unsigned char  *src;\r
109         unsigned short *dst;\r
110 \r
111         for (i = 0;; i++, x += 6)\r
112         {\r
113                 unsigned char c = (unsigned char) texto[i];\r
114                 int h = 8;\r
115 \r
116                 if (!c) break;\r
117 \r
118                 src = fontdata6x8[c];\r
119                 dst = (unsigned short *)gp2x_screen + x + y*320;\r
120 \r
121                 while (h--)\r
122                 {\r
123                         int w = 0x20;\r
124                         while (w)\r
125                         {\r
126                                 if( *src & w ) *dst = 0xffff;\r
127                                 dst++;\r
128                                 w>>=1;\r
129                         }\r
130                         src++;\r
131 \r
132                         dst += 320-6;\r
133                 }\r
134         }\r
135 }\r
136 \r
137 static void gp2x_smalltext8_lim(int x, int y, const char *texto, int max)\r
138 {\r
139         char    buffer[320/6+1];\r
140 \r
141         strncpy(buffer, texto, 320/6);\r
142         if (max > 320/6) max = 320/6;\r
143         if (max < 0) max = 0;\r
144         buffer[max] = 0;\r
145 \r
146         gp2x_smalltext16(x, y, buffer);\r
147 }\r
148 \r
149 \r
150 static unsigned long inp_prev = 0;\r
151 static int inp_prevjoy = 0;\r
152 \r
153 static unsigned long wait_for_input(unsigned long interesting)\r
154 {\r
155         unsigned long ret;\r
156         static int repeats = 0, wait = 50*1000;\r
157         int release = 0, i;\r
158 \r
159         if (repeats == 2 || repeats == 4) wait /= 2;\r
160         if (repeats == 6) wait = 15 * 1000;\r
161 \r
162         for (i = 0; i < 6 && inp_prev == gp2x_joystick_read(1); i++) {\r
163                 if (i == 0) repeats++;\r
164                 if (wait >= 30*1000) usleep(wait); // usleep sleeps for ~30ms minimum\r
165                 else spend_cycles(wait * Settings.cpuclock);\r
166         }\r
167 \r
168         while ( !((ret = gp2x_joystick_read(1)) & interesting) ) {\r
169                 usleep(50000);\r
170                 release = 1;\r
171         }\r
172 \r
173         if (release || ret != inp_prev) {\r
174                 repeats = 0;\r
175                 wait = 50*1000;\r
176         }\r
177         inp_prev = ret;\r
178         inp_prevjoy = 0;\r
179 \r
180         // we don't need diagonals in menus\r
181         if ((ret&GP2X_UP)   && (ret&GP2X_LEFT))  ret &= ~GP2X_LEFT;\r
182         if ((ret&GP2X_UP)   && (ret&GP2X_RIGHT)) ret &= ~GP2X_RIGHT;\r
183         if ((ret&GP2X_DOWN) && (ret&GP2X_LEFT))  ret &= ~GP2X_LEFT;\r
184         if ((ret&GP2X_DOWN) && (ret&GP2X_RIGHT)) ret &= ~GP2X_RIGHT;\r
185 \r
186         return ret;\r
187 }\r
188 \r
189 static unsigned long input2_read(unsigned long interesting, int *joy)\r
190 {\r
191         unsigned long ret;\r
192         int i;\r
193 \r
194         do\r
195         {\r
196                 *joy = 0;\r
197                 if ((ret = gp2x_joystick_read(0) & interesting)) break;\r
198                 gp2x_usbjoy_update();\r
199                 for (i = 0; i < num_of_joys; i++) {\r
200                         ret = gp2x_usbjoy_check2(i);\r
201                         if (ret) { *joy = i + 1; break; }\r
202                 }\r
203                 if (ret) break;\r
204         }\r
205         while(0);\r
206 \r
207         return ret;\r
208 }\r
209 \r
210 // similar to wait_for_input(), but returns joy num\r
211 static unsigned long wait_for_input_usbjoy(unsigned long interesting, int *joy)\r
212 {\r
213         unsigned long ret;\r
214         const int wait = 300*1000;\r
215         int i;\r
216 \r
217         if (inp_prevjoy == 0) inp_prev &= interesting;\r
218         for (i = 0; i < 6; i++) {\r
219                 ret = input2_read(interesting, joy);\r
220                 if (*joy != inp_prevjoy || ret != inp_prev) break;\r
221                 usleep(wait/6);\r
222         }\r
223 \r
224         while ( !(ret = input2_read(interesting, joy)) ) {\r
225                 usleep(50000);\r
226         }\r
227 \r
228         inp_prev = ret;\r
229         inp_prevjoy = *joy;\r
230 \r
231         return ret;\r
232 }\r
233 \r
234 \r
235 \r
236 // -------------- ROM selector --------------\r
237 \r
238 static void draw_dirlist(char *curdir, struct dirent **namelist, int n, int sel)\r
239 {\r
240         int start, i, pos;\r
241 \r
242         start = 12 - sel;\r
243         n--; // exclude current dir (".")\r
244 \r
245         //memset(gp2x_screen, 0, 320*240);\r
246         gp2x_fceu_copy_bg();\r
247 \r
248         if(start - 2 >= 0)\r
249                 gp2x_smalltext8_lim(14, (start - 2)*10, curdir, 53-2);\r
250         for (i = 0; i < n; i++) {\r
251                 pos = start + i;\r
252                 if (pos < 0)  continue;\r
253                 if (pos > 23) break;\r
254                 if (namelist[i+1]->d_type == DT_DIR) {\r
255                         gp2x_smalltext8_lim(14,   pos*10, "/", 1);\r
256                         gp2x_smalltext8_lim(14+6, pos*10, namelist[i+1]->d_name, 53-3);\r
257                 } else {\r
258                         gp2x_smalltext8_lim(14,   pos*10, namelist[i+1]->d_name, 53-2);\r
259                 }\r
260         }\r
261         gp2x_text_out15(5, 120, ">");\r
262         gp2x_video_flip();\r
263 }\r
264 \r
265 static int scandir_cmp(const void *p1, const void *p2)\r
266 {\r
267         struct dirent **d1 = (struct dirent **)p1, **d2 = (struct dirent **)p2;\r
268         if ((*d1)->d_type == (*d2)->d_type) return alphasort(d1, d2);\r
269         if ((*d1)->d_type == DT_DIR) return -1; // put before\r
270         if ((*d2)->d_type == DT_DIR) return  1;\r
271         return alphasort(d1, d2);\r
272 }\r
273 \r
274 static char *filter_exts[] = {\r
275         ".gpe", ".png", "ck.o", ".txt"\r
276 };\r
277 \r
278 static int scandir_filter(const struct dirent *ent)\r
279 {\r
280         const char *p;\r
281         int i;\r
282 \r
283         if (ent == NULL || ent->d_name == NULL) return 0;\r
284         if (strlen(ent->d_name) < 5) return 1;\r
285 \r
286         p = ent->d_name + strlen(ent->d_name) - 4;\r
287 \r
288         for (i = 0; i < sizeof(filter_exts)/sizeof(filter_exts[0]); i++)\r
289         {\r
290                 if (strcmp(p, filter_exts[i]) == 0) return 0;\r
291         }\r
292 \r
293         return 1;\r
294 }\r
295 \r
296 static char *romsel_loop(char *curr_path)\r
297 {\r
298         struct dirent **namelist;\r
299         DIR *dir;\r
300         int n, sel = 0;\r
301         unsigned long inp = 0;\r
302         char *ret = NULL, *fname = NULL;\r
303 \r
304         // is this a dir or a full path?\r
305         if ((dir = opendir(curr_path))) {\r
306                 closedir(dir);\r
307         } else {\r
308                 char *p;\r
309                 for (p = curr_path + strlen(curr_path) - 1; p > curr_path && *p != '/'; p--);\r
310                 *p = 0;\r
311                 fname = p+1;\r
312         }\r
313 \r
314         n = scandir(curr_path, &namelist, scandir_filter, scandir_cmp);\r
315         if (n < 0) {\r
316                 // try root\r
317                 n = scandir("/", &namelist, scandir_filter, scandir_cmp);\r
318                 if (n < 0) {\r
319                         // oops, we failed\r
320                         printf("dir: %s\n", curr_path);\r
321                         perror("scandir");\r
322                         return NULL;\r
323                 }\r
324         }\r
325 \r
326         // try to find sel\r
327         if (fname != NULL) {\r
328                 int i;\r
329                 for (i = 1; i < n; i++) {\r
330                         if (strcmp(namelist[i]->d_name, fname) == 0) {\r
331                                 sel = i - 1;\r
332                                 break;\r
333                         }\r
334                 }\r
335         }\r
336 \r
337         for (;;)\r
338         {\r
339                 draw_dirlist(curr_path, namelist, n, sel);\r
340                 inp = wait_for_input(GP2X_UP|GP2X_DOWN|GP2X_LEFT|GP2X_RIGHT|GP2X_L|GP2X_R|GP2X_B|GP2X_X);\r
341                 if(inp & GP2X_UP  )  { sel--;   if (sel < 0)   sel = n-2; }\r
342                 if(inp & GP2X_DOWN)  { sel++;   if (sel > n-2) sel = 0; }\r
343                 if(inp & GP2X_LEFT)  { sel-=10; if (sel < 0)   sel = 0; }\r
344                 if(inp & GP2X_L)     { sel-=24; if (sel < 0)   sel = 0; }\r
345                 if(inp & GP2X_RIGHT) { sel+=10; if (sel > n-2) sel = n-2; }\r
346                 if(inp & GP2X_R)     { sel+=24; if (sel > n-2) sel = n-2; }\r
347                 if(inp & GP2X_B)     { // enter dir/select\r
348                         again:\r
349                         if (namelist[sel+1]->d_type == DT_REG) {\r
350                                 strcpy(lastLoadedGameName, curr_path);\r
351                                 strcat(lastLoadedGameName, "/");\r
352                                 strcat(lastLoadedGameName, namelist[sel+1]->d_name);\r
353                                 ret = lastLoadedGameName;\r
354                                 break;\r
355                         } else if (namelist[sel+1]->d_type == DT_DIR) {\r
356                                 int newlen = strlen(curr_path) + strlen(namelist[sel+1]->d_name) + 2;\r
357                                 char *p, *newdir = malloc(newlen);\r
358                                 if (strcmp(namelist[sel+1]->d_name, "..") == 0) {\r
359                                         char *start = curr_path;\r
360                                         p = start + strlen(start) - 1;\r
361                                         while (*p == '/' && p > start) p--;\r
362                                         while (*p != '/' && p > start) p--;\r
363                                         if (p <= start) strcpy(newdir, "/");\r
364                                         else { strncpy(newdir, start, p-start); newdir[p-start] = 0; }\r
365                                 } else {\r
366                                         strcpy(newdir, curr_path);\r
367                                         p = newdir + strlen(newdir) - 1;\r
368                                         while (*p == '/' && p >= newdir) *p-- = 0;\r
369                                         strcat(newdir, "/");\r
370                                         strcat(newdir, namelist[sel+1]->d_name);\r
371                                 }\r
372                                 ret = romsel_loop(newdir);\r
373                                 free(newdir);\r
374                                 break;\r
375                         } else {\r
376                                 // unknown file type, happens on NTFS mounts. Try to guess.\r
377                                 FILE *tstf; int tmp;\r
378                                 strcpy(lastLoadedGameName, curr_path);\r
379                                 strcat(lastLoadedGameName, "/");\r
380                                 strcat(lastLoadedGameName, namelist[sel+1]->d_name);\r
381                                 tstf = fopen(lastLoadedGameName, "rb");\r
382                                 if (tstf != NULL)\r
383                                 {\r
384                                         if (fread(&tmp, 1, 1, tstf) > 0 || ferror(tstf) == 0)\r
385                                                 namelist[sel+1]->d_type = DT_REG;\r
386                                         else    namelist[sel+1]->d_type = DT_DIR;\r
387                                         fclose(tstf);\r
388                                         goto again;\r
389                                 }\r
390                         }\r
391                 }\r
392                 if(inp & GP2X_X) break; // cancel\r
393         }\r
394 \r
395         if (n > 0) {\r
396                 while(n--) free(namelist[n]);\r
397                 free(namelist);\r
398         }\r
399 \r
400         return ret;\r
401 }\r
402 \r
403 // ------------ patch/gg menu ------------\r
404 \r
405 #if 0 // TODO?\r
406 static void draw_patchlist(int sel)\r
407 {\r
408         int start, i, pos;\r
409 \r
410         start = 12 - sel;\r
411 \r
412         gp2x_fceu_copy_bg();\r
413 \r
414         for (i = 0; i < PicoPatchCount; i++) {\r
415                 pos = start + i;\r
416                 if (pos < 0)  continue;\r
417                 if (pos > 23) break;\r
418                 gp2x_smalltext8_lim(14,     pos*10, PicoPatches[i].active ? "ON " : "OFF", 3);\r
419                 gp2x_smalltext8_lim(14+6*4, pos*10, PicoPatches[i].name, 53-6);\r
420         }\r
421         pos = start + i;\r
422         if (pos < 24) gp2x_smalltext8_lim(14, pos*10, "done", 4);\r
423 \r
424         gp2x_text_out15(5, 120, ">");\r
425         gp2x_video_flip();\r
426 }\r
427 \r
428 \r
429 void patches_menu_loop(void)\r
430 {\r
431         int menu_sel = 0;\r
432         unsigned long inp = 0;\r
433 \r
434         for(;;)\r
435         {\r
436                 draw_patchlist(menu_sel);\r
437                 inp = wait_for_input(GP2X_UP|GP2X_DOWN|GP2X_LEFT|GP2X_RIGHT|GP2X_L|GP2X_R|GP2X_B|GP2X_X);\r
438                 if(inp & GP2X_UP  ) { menu_sel--; if (menu_sel < 0) menu_sel = PicoPatchCount; }\r
439                 if(inp & GP2X_DOWN) { menu_sel++; if (menu_sel > PicoPatchCount) menu_sel = 0; }\r
440                 if(inp &(GP2X_LEFT|GP2X_L))  { menu_sel-=10; if (menu_sel < 0) menu_sel = 0; }\r
441                 if(inp &(GP2X_RIGHT|GP2X_R)) { menu_sel+=10; if (menu_sel > PicoPatchCount) menu_sel = PicoPatchCount; }\r
442                 if(inp & GP2X_B) { // action\r
443                         if (menu_sel < PicoPatchCount)\r
444                                 PicoPatches[menu_sel].active = !PicoPatches[menu_sel].active;\r
445                         else    return;\r
446                 }\r
447                 if(inp & GP2X_X) return;\r
448         }\r
449 \r
450 }\r
451 \r
452 // ------------ savestate loader ------------\r
453 \r
454 static void menu_prepare_bg(void);\r
455 \r
456 static int state_slot_flags = 0;\r
457 \r
458 static void state_check_slots(void)\r
459 {\r
460         int slot;\r
461 \r
462         state_slot_flags = 0;\r
463 \r
464         for (slot = 0; slot < 10; slot++)\r
465         {\r
466                 if (emu_check_save_file(slot))\r
467                 {\r
468                         state_slot_flags |= 1 << slot;\r
469                 }\r
470         }\r
471 }\r
472 \r
473 static void draw_savestate_bg(int slot)\r
474 {\r
475         struct PicoVideo tmp_pv;\r
476         unsigned short tmp_cram[0x40];\r
477         unsigned short tmp_vsram[0x40];\r
478         void *tmp_vram, *file;\r
479         char *fname;\r
480 \r
481         fname = emu_GetSaveFName(1, 0, slot);\r
482         if (!fname) return;\r
483 \r
484         tmp_vram = malloc(sizeof(Pico.vram));\r
485         if (tmp_vram == NULL) return;\r
486 \r
487         memcpy(tmp_vram, Pico.vram, sizeof(Pico.vram));\r
488         memcpy(tmp_cram, Pico.cram, sizeof(Pico.cram));\r
489         memcpy(tmp_vsram, Pico.vsram, sizeof(Pico.vsram));\r
490         memcpy(&tmp_pv, &Pico.video, sizeof(Pico.video));\r
491 \r
492         if (strcmp(fname + strlen(fname) - 3, ".gz") == 0) {\r
493                 file = gzopen(fname, "rb");\r
494                 emu_set_save_cbs(1);\r
495         } else {\r
496                 file = fopen(fname, "rb");\r
497                 emu_set_save_cbs(0);\r
498         }\r
499 \r
500         if (file) {\r
501                 if (PicoMCD & 1) {\r
502                         PicoCdLoadStateGfx(file);\r
503                 } else {\r
504                         areaSeek(file, 0x10020, SEEK_SET);  // skip header and RAM in state file\r
505                         areaRead(Pico.vram, 1, sizeof(Pico.vram), file);\r
506                         areaSeek(file, 0x2000, SEEK_CUR);\r
507                         areaRead(Pico.cram, 1, sizeof(Pico.cram), file);\r
508                         areaRead(Pico.vsram, 1, sizeof(Pico.vsram), file);\r
509                         areaSeek(file, 0x221a0, SEEK_SET);\r
510                         areaRead(&Pico.video, 1, sizeof(Pico.video), file);\r
511                 }\r
512                 areaClose(file);\r
513         }\r
514 \r
515         emu_forced_frame();\r
516         gp2x_memcpy_buffers((1<<2), gp2x_screen, 0, 320*240*2);\r
517         menu_prepare_bg();\r
518 \r
519         memcpy(Pico.vram, tmp_vram, sizeof(Pico.vram));\r
520         memcpy(Pico.cram, tmp_cram, sizeof(Pico.cram));\r
521         memcpy(Pico.vsram, tmp_vsram, sizeof(Pico.vsram));\r
522         memcpy(&Pico.video, &tmp_pv,  sizeof(Pico.video));\r
523         free(tmp_vram);\r
524 }\r
525 \r
526 static void draw_savestate_menu(int menu_sel, int is_loading)\r
527 {\r
528         int tl_x = 25, tl_y = 60, y, i;\r
529 \r
530         if (state_slot_flags & (1 << menu_sel))\r
531                 draw_savestate_bg(menu_sel);\r
532         gp2x_fceu_copy_bg();\r
533 \r
534         gp2x_text_out15(tl_x, 30, is_loading ? "Load state" : "Save state");\r
535 \r
536         /* draw all 10 slots */\r
537         y = tl_y;\r
538         for (i = 0; i < 10; i++, y+=10)\r
539         {\r
540                 gp2x_text_out15(tl_x, y, "SLOT %i (%s)", i, (state_slot_flags & (1 << i)) ? "USED" : "free");\r
541         }\r
542         gp2x_text_out15(tl_x, y, "back");\r
543 \r
544         // draw cursor\r
545         gp2x_text_out15(tl_x - 16, tl_y + menu_sel*10, ">");\r
546 \r
547         gp2x_video_flip();\r
548 }\r
549 \r
550 static int savestate_menu_loop(int is_loading)\r
551 {\r
552         int menu_sel = 10, menu_sel_max = 10;\r
553         unsigned long inp = 0;\r
554 \r
555         state_check_slots();\r
556 \r
557         for(;;)\r
558         {\r
559                 draw_savestate_menu(menu_sel, is_loading);\r
560                 inp = wait_for_input(GP2X_UP|GP2X_DOWN|GP2X_B|GP2X_X);\r
561                 if(inp & GP2X_UP  ) {\r
562                         do {\r
563                                 menu_sel--; if (menu_sel < 0) menu_sel = menu_sel_max;\r
564                         } while (!(state_slot_flags & (1 << menu_sel)) && menu_sel != menu_sel_max && is_loading);\r
565                 }\r
566                 if(inp & GP2X_DOWN) {\r
567                         do {\r
568                                 menu_sel++; if (menu_sel > menu_sel_max) menu_sel = 0;\r
569                         } while (!(state_slot_flags & (1 << menu_sel)) && menu_sel != menu_sel_max && is_loading);\r
570                 }\r
571                 if(inp & GP2X_B) { // save/load\r
572                         if (menu_sel < 10) {\r
573                                 state_slot = menu_sel;\r
574                                 if (emu_SaveLoadGame(is_loading, 0)) {\r
575                                         strcpy(menuErrorMsg, is_loading ? "Load failed" : "Save failed");\r
576                                         return 1;\r
577                                 }\r
578                                 return 0;\r
579                         } else  return 1;\r
580                 }\r
581                 if(inp & GP2X_X) return 1;\r
582         }\r
583 }\r
584 #endif\r
585 \r
586 // -------------- key config --------------\r
587 \r
588 static char *usb_joy_key_name(int joy, int num)\r
589 {\r
590         static char name[16];\r
591         switch (num)\r
592         {\r
593                 case 0: sprintf(name, "Joy%i UP", joy); break;\r
594                 case 1: sprintf(name, "Joy%i DOWN", joy); break;\r
595                 case 2: sprintf(name, "Joy%i LEFT", joy); break;\r
596                 case 3: sprintf(name, "Joy%i RIGHT", joy); break;\r
597                 default:sprintf(name, "Joy%i b%i", joy, num-3); break;\r
598         }\r
599         return name;\r
600 }\r
601 \r
602 static char *action_binds(int player_idx, int action_mask)\r
603 {\r
604         static char strkeys[32*5];\r
605         int joy, i;\r
606 \r
607         strkeys[0] = 0;\r
608         for (i = 0; i < 32; i++) // i is key index\r
609         {\r
610                 if (Settings.KeyBinds[i] & action_mask)\r
611                 {\r
612                         if (player_idx >= 0 && ((Settings.KeyBinds[i] >> 16) & 3) != player_idx) continue;\r
613                         if (strkeys[0]) { strcat(strkeys, " + "); strcat(strkeys, gp2xKeyNames[i]); break; }\r
614                         else strcpy(strkeys, gp2xKeyNames[i]);\r
615                 }\r
616         }\r
617         for (joy = 0; joy < num_of_joys; joy++)\r
618         {\r
619                 for (i = 0; i < 32; i++)\r
620                 {\r
621                         if (Settings.JoyBinds[joy][i] & action_mask)\r
622                         {\r
623                                 if (player_idx >= 0 && ((Settings.KeyBinds[i] >> 16) & 3) != player_idx) continue;\r
624                                 if (strkeys[0]) {\r
625                                         strcat(strkeys, ", "); strcat(strkeys, usb_joy_key_name(joy + 1, i));\r
626                                         break;\r
627                                 }\r
628                                 else strcpy(strkeys, usb_joy_key_name(joy + 1, i));\r
629                         }\r
630                 }\r
631         }\r
632 \r
633         return strkeys;\r
634 }\r
635 \r
636 static void unbind_action(int action)\r
637 {\r
638         int i, u;\r
639 \r
640         for (i = 0; i < 32; i++)\r
641                 Settings.KeyBinds[i] &= ~action;\r
642         for (u = 0; u < 4; u++)\r
643                 for (i = 0; i < 32; i++)\r
644                         Settings.JoyBinds[u][i] &= ~action;\r
645 }\r
646 \r
647 static int count_bound_keys(int action, int is_joy)\r
648 {\r
649         int i, u, keys = 0;\r
650 \r
651         if (is_joy)\r
652         {\r
653                 for (u = 0; u < 4; u++)\r
654                         for (i = 0; i < 32; i++)\r
655                                 if (Settings.JoyBinds[u][i] & action) keys++;\r
656         }\r
657         else\r
658         {\r
659                 for (i = 0; i < 32; i++)\r
660                         if (Settings.KeyBinds[i] & action) keys++;\r
661         }\r
662         return keys;\r
663 }\r
664 \r
665 typedef struct { char *name; int mask; } bind_action_t;\r
666 \r
667 static void draw_key_config(const bind_action_t *opts, int opt_cnt, int player_idx, int sel)\r
668 {\r
669         int x, y, tl_y = 40, i;\r
670 \r
671         gp2x_fceu_copy_bg();\r
672         if (player_idx >= 0)\r
673              gp2x_text_out15(80, 20, "Player %i controls", player_idx + 1);\r
674         else gp2x_text_out15(80, 20, "Emulator controls");\r
675 \r
676         x = 40; y = tl_y;\r
677         for (i = 0; i < opt_cnt; i++, y+=10)\r
678                 gp2x_text_out15(x, y, "%s : %s", opts[i].name, action_binds(player_idx, opts[i].mask));\r
679 \r
680         gp2x_text_out15(x, y, "Done");\r
681 \r
682         // draw cursor\r
683         gp2x_text_out15(x - 16, tl_y + sel*10, ">");\r
684 \r
685         if (sel < opt_cnt) {\r
686                 gp2x_text_out15(30, 190, "Press a button to bind/unbind");\r
687                 gp2x_text_out15(30, 200, "Use VOL+ to clear");\r
688                 gp2x_text_out15(30, 210, "To bind UP/DOWN, hold VOL-");\r
689                 gp2x_text_out15(30, 220, "Select \"Done\" to exit");\r
690         } else {\r
691                 gp2x_text_out15(30, 220, "Press B or X to exit");\r
692         }\r
693         gp2x_video_flip();\r
694 }\r
695 \r
696 static void key_config_loop(const bind_action_t *opts, int opt_cnt, int player_idx)\r
697 {\r
698         int joy = 0, sel = 0, menu_sel_max = opt_cnt, i;\r
699         unsigned long inp = 0;\r
700 \r
701         for (;;)\r
702         {\r
703                 draw_key_config(opts, opt_cnt, player_idx, sel);\r
704                 inp = wait_for_input_usbjoy(CONFIGURABLE_KEYS|GP2X_VOL_DOWN|GP2X_VOL_UP, &joy);\r
705                 // printf("got %08lX from joy %i\n", inp, joy);\r
706                 if (joy == 0) {\r
707                         if (!(inp & GP2X_VOL_DOWN)) {\r
708                                 if(inp & GP2X_UP  ) { sel--; if (sel < 0) sel = menu_sel_max; continue; }\r
709                                 if(inp & GP2X_DOWN) { sel++; if (sel > menu_sel_max) sel = 0; continue; }\r
710                         }\r
711                         if (sel >= opt_cnt) {\r
712                                 if (inp & (GP2X_B|GP2X_X)) break;\r
713                                 else continue;\r
714                         }\r
715                         // if we are here, we want to bind/unbind something\r
716                         if (inp & GP2X_VOL_UP)\r
717                                 unbind_action(opts[sel].mask);\r
718                         inp &= CONFIGURABLE_KEYS;\r
719                         for (i = 0; i < 32; i++)\r
720                                 if (inp & (1 << i)) {\r
721                                         if (count_bound_keys(opts[sel].mask, 0) >= 2)\r
722                                              Settings.KeyBinds[i] &= ~opts[sel].mask; // allow to unbind only\r
723                                         else Settings.KeyBinds[i] ^=  opts[sel].mask;\r
724                                         if (player_idx >= 0) {\r
725                                                 Settings.KeyBinds[i] &= ~(3 << 16);\r
726                                                 Settings.KeyBinds[i] |= player_idx << 16;\r
727                                         }\r
728                                 }\r
729                 } else if (sel < opt_cnt) {\r
730                         for (i = 0; i < 32; i++)\r
731                                 if (inp & (1 << i)) {\r
732                                         if (count_bound_keys(opts[sel].mask, 1) >= 1) // disallow combos for usbjoy\r
733                                              Settings.JoyBinds[joy-1][i] &= ~opts[sel].mask;\r
734                                         else Settings.JoyBinds[joy-1][i] ^=  opts[sel].mask;\r
735                                         if (player_idx >= 0) {\r
736                                                 Settings.JoyBinds[joy-1][i] &= ~(3 << 16);\r
737                                                 Settings.JoyBinds[joy-1][i] |= player_idx << 16;\r
738                                         }\r
739                                 }\r
740                 }\r
741         }\r
742 }\r
743 \r
744 static void draw_kc_sel(int menu_sel)\r
745 {\r
746         int tl_x = 25+40, tl_y = 60, y, i;\r
747         char joyname[36];\r
748 \r
749         y = tl_y;\r
750         gp2x_fceu_copy_bg();\r
751         gp2x_text_out15(tl_x, y,       "Player 1");\r
752         gp2x_text_out15(tl_x, (y+=10), "Player 2");\r
753         gp2x_text_out15(tl_x, (y+=10), "Emulator controls");\r
754         gp2x_text_out15(tl_x, (y+=10), "Done");\r
755 \r
756         // draw cursor\r
757         gp2x_text_out15(tl_x - 16, tl_y + menu_sel*10, ">");\r
758 \r
759         tl_x = 25;\r
760         gp2x_text_out15(tl_x, (y=110), "USB joys detected:");\r
761         if (num_of_joys > 0) {\r
762                 for (i = 0; i < num_of_joys; i++) {\r
763                         strncpy(joyname, joy_name(joys[i]), 33); joyname[33] = 0;\r
764                         gp2x_text_out15(tl_x, (y+=10), "%i: %s", i+1, joyname);\r
765                 }\r
766         } else {\r
767                 gp2x_text_out15(tl_x, (y+=10), "none");\r
768         }\r
769 \r
770         gp2x_video_flip();\r
771 }\r
772 \r
773 // b_turbo,a_turbo  RLDU SEBA\r
774 static bind_action_t ctrl_actions[] =\r
775 {\r
776         { "UP     ", 0x010 },\r
777         { "DOWN   ", 0x020 },\r
778         { "LEFT   ", 0x040 },\r
779         { "RIGHT  ", 0x080 },\r
780         { "A      ", 0x001 },\r
781         { "B      ", 0x002 },\r
782         { "A TURBO", 0x100 },\r
783         { "B TURBO", 0x200 },\r
784         { "START  ", 0x008 },\r
785         { "SELECT ", 0x004 },\r
786 };\r
787 \r
788 static bind_action_t emuctrl_actions[] =\r
789 {\r
790         { "Save State     ", 1<<31 },\r
791         { "Load State     ", 1<<30 },\r
792         { "Next State Slot", 1<<29 },\r
793 };\r
794 \r
795 static void kc_sel_loop(void)\r
796 {\r
797         int menu_sel = 3, menu_sel_max = 3;\r
798         unsigned long inp = 0;\r
799 \r
800         for(;;)\r
801         {\r
802                 draw_kc_sel(menu_sel);\r
803                 inp = wait_for_input(GP2X_UP|GP2X_DOWN|GP2X_B|GP2X_X);\r
804                 if(inp & GP2X_UP  ) { menu_sel--; if (menu_sel < 0) menu_sel = menu_sel_max; }\r
805                 if(inp & GP2X_DOWN) { menu_sel++; if (menu_sel > menu_sel_max) menu_sel = 0; }\r
806                 if(inp & GP2X_B) {\r
807                         switch (menu_sel) {\r
808                                 case 0: key_config_loop(ctrl_actions, 10, 0); return;\r
809                                 case 1: key_config_loop(ctrl_actions,  8, 1); return;\r
810                                 case 2: key_config_loop(emuctrl_actions,\r
811                                                 sizeof(emuctrl_actions)/sizeof(emuctrl_actions[0]), -1); return;\r
812                                 default: return;\r
813                         }\r
814                 }\r
815                 if(inp & GP2X_X) return;\r
816         }\r
817 }\r
818 \r
819 \r
820 // --------- FCEU options ----------\r
821 \r
822 static void draw_fcemenu_options(int menu_sel)\r
823 {\r
824         int tl_x = 25, tl_y = 60, y;\r
825 \r
826         y = tl_y;\r
827         gp2x_fceu_copy_bg();\r
828 \r
829         gp2x_text_out15(tl_x,  y,      "                           %s", "OFF"); // 0\r
830         gp2x_text_out15(tl_x, (y+=10), "Done");\r
831 \r
832         // draw cursor\r
833         gp2x_text_out15(tl_x - 16, tl_y + menu_sel*10, ">");\r
834 \r
835         gp2x_video_flip();\r
836 }\r
837 \r
838 static void fcemenu_loop_options(void)\r
839 {\r
840         int menu_sel = 0, menu_sel_max = 1;\r
841         unsigned long inp = 0;\r
842 \r
843         for(;;)\r
844         {\r
845                 draw_fcemenu_options(menu_sel);\r
846                 inp = wait_for_input(GP2X_UP|GP2X_DOWN|GP2X_LEFT|GP2X_RIGHT|GP2X_B|GP2X_X|GP2X_A);\r
847                 if(inp & GP2X_UP  ) { menu_sel--; if (menu_sel < 0) menu_sel = menu_sel_max; }\r
848                 if(inp & GP2X_DOWN) { menu_sel++; if (menu_sel > menu_sel_max) menu_sel = 0; }\r
849                 if((inp& GP2X_B)||(inp&GP2X_LEFT)||(inp&GP2X_RIGHT)) { // toggleable options\r
850                         switch (menu_sel) {\r
851                                 case  0: break;\r
852                                 case  1: return;\r
853                         }\r
854                 }\r
855                 if(inp & (GP2X_X|GP2X_A)) return;\r
856                 if(inp & (GP2X_LEFT|GP2X_RIGHT)) { // multi choise\r
857                         switch (menu_sel) {\r
858                                 case 0:\r
859                                         break;\r
860                         }\r
861                 }\r
862         }\r
863 }\r
864 \r
865 // -------------- options --------------\r
866 \r
867 static void draw_menu_options(int menu_sel)\r
868 {\r
869         int tl_x = 25, tl_y = 32, y;\r
870         char strframeskip[8], *strscaling, *strssconfirm;\r
871         char *mms = mmuhack_status ? "active)  " : "inactive)";\r
872 \r
873         if (Settings.frameskip < 0)\r
874              strcpy(strframeskip, "Auto");\r
875         else sprintf(strframeskip, "%i", Settings.frameskip);\r
876         switch (Settings.scaling) {\r
877                 default: strscaling = "            OFF";   break;\r
878                 case 1:  strscaling = "hw horizontal";     break;\r
879                 case 2:  strscaling = "hw horiz. + vert."; break;\r
880                 case 3:  strscaling = "sw horizontal";     break;\r
881         }\r
882         switch (Settings.sstate_confirm) {\r
883                 default: strssconfirm = "OFF";    break;\r
884                 case 1:  strssconfirm = "writes"; break;\r
885                 case 2:  strssconfirm = "loads";  break;\r
886                 case 3:  strssconfirm = "both";   break;\r
887         }\r
888 \r
889         y = tl_y;\r
890         gp2x_fceu_copy_bg();\r
891 \r
892         gp2x_text_out15(tl_x,  y,      "Scaling:       %s", strscaling);                                // 0\r
893         gp2x_text_out15(tl_x, (y+=10), "Show FPS                   %s", Settings.showfps?"ON":"OFF");   // 1\r
894         gp2x_text_out15(tl_x, (y+=10), "Frameskip                  %s", strframeskip);                  // 2\r
895         gp2x_text_out15(tl_x, (y+=10), "Enable sound               %s", soundvol?"ON":"OFF");\r
896         gp2x_text_out15(tl_x, (y+=10), "Sound Rate:           %5iHz", Settings.sound_rate);             // 4\r
897         gp2x_text_out15(tl_x, (y+=10), "Force Region:              %s",\r
898                 Settings.region_force == 2 ? "PAL" : Settings.region_force == 1 ? "NTSC" : "OFF");      // 5\r
899         gp2x_text_out15(tl_x, (y+=10), "Use SRAM savestates        %s", "OFF");\r
900         gp2x_text_out15(tl_x, (y+=10), "Turbo rate                 %iHz", (Settings.turbo_rate_add*60/2) >> 24);\r
901         gp2x_text_out15(tl_x, (y+=10), "Confirm savestate          %s", strssconfirm);                  // 8\r
902         gp2x_text_out15(tl_x, (y+=10), "Save slot                  %i", CurrentState);\r
903         gp2x_text_out15(tl_x, (y+=10), "Faster RAM timings         %s", Settings.ramtimings?"ON":"OFF");\r
904         gp2x_text_out15(tl_x, (y+=10), "squidgehack (now %s %s",   mms, Settings.mmuhack?"ON":"OFF");   // 11\r
905         gp2x_text_out15(tl_x, (y+=10), "Gamma correction           %i.%02i", Settings.gamma / 100, Settings.gamma%100);\r
906         gp2x_text_out15(tl_x, (y+=10), "GP2X CPU clock             %iMhz", Settings.cpuclock);          // 13\r
907         gp2x_text_out15(tl_x, (y+=10), "[FCE Ultra options]");\r
908         gp2x_text_out15(tl_x, (y+=10), "Save cfg as default");\r
909         if (fceugi)\r
910                 gp2x_text_out15(tl_x, (y+=10), "Save cfg for current game only");\r
911 \r
912         // draw cursor\r
913         gp2x_text_out15(tl_x - 16, tl_y + menu_sel*10, ">");\r
914 \r
915         gp2x_video_flip();\r
916 }\r
917 \r
918 static int sndrate_prevnext(int rate, int dir)\r
919 {\r
920         int i, rates[] = { 8000, 11025, 16000, 22050, 44100 };\r
921 \r
922         for (i = 0; i < 5; i++)\r
923                 if (rates[i] == rate) break;\r
924 \r
925         i += dir ? 1 : -1;\r
926         if (i > 4) return dir ? 44100 : 22050;\r
927         if (i < 0) return dir ? 11025 : 8000;\r
928         return rates[i];\r
929 }\r
930 \r
931 static void int_incdec(int *p, int inc, int min, int max)\r
932 {\r
933         *p += inc;\r
934         if      (*p < min) *p = min;\r
935         else if (*p > max) *p = max;\r
936 }\r
937 \r
938 static void config_commit(void)\r
939 {\r
940         gp2x_cpuclock_gamma_update();\r
941         if (Settings.region_force)\r
942                 FCEUI_SetVidSystem(Settings.region_force - 1);\r
943 }\r
944 \r
945 static int menu_loop_options(void)\r
946 {\r
947         static int menu_sel = 0;\r
948         int menu_sel_max = 15;\r
949         unsigned long inp = 0;\r
950 \r
951         if (fceugi) menu_sel_max++;\r
952 \r
953         for(;;)\r
954         {\r
955                 draw_menu_options(menu_sel);\r
956                 inp = wait_for_input(GP2X_UP|GP2X_DOWN|GP2X_LEFT|GP2X_RIGHT|GP2X_B|GP2X_X|GP2X_A);\r
957                 if(inp & GP2X_UP  ) { menu_sel--; if (menu_sel < 0) menu_sel = menu_sel_max; }\r
958                 if(inp & GP2X_DOWN) { menu_sel++; if (menu_sel > menu_sel_max) menu_sel = 0; }\r
959                 if((inp& GP2X_B)||(inp&GP2X_LEFT)||(inp&GP2X_RIGHT)) { // toggleable options\r
960                         switch (menu_sel) {\r
961                                 case  1: Settings.showfps    = !Settings.showfps; break;\r
962                                 case  3: soundvol = soundvol ? 0 : 100; break;\r
963                                 case 10: Settings.ramtimings = !Settings.ramtimings; break;\r
964                                 case 11: Settings.mmuhack    = !Settings.mmuhack; break;\r
965                                 case 14: fcemenu_loop_options(); break;\r
966                                 case 15: // done (update and write)\r
967                                         config_commit();\r
968                                         SaveConfig(NULL);\r
969                                         return 1;\r
970                                 case 16: // done (update and write for current game)\r
971                                         config_commit();\r
972                                         if (lastLoadedGameName[0])\r
973                                                 SaveConfig(lastLoadedGameName);\r
974                                         return 1;\r
975                         }\r
976                 }\r
977                 if(inp & (GP2X_X|GP2X_A)) {\r
978                         config_commit();\r
979                         return 0;  // done (update, no write)\r
980                 }\r
981                 if(inp & (GP2X_LEFT|GP2X_RIGHT)) { // multi choise\r
982                         switch (menu_sel) {\r
983                                 case  0: int_incdec(&Settings.scaling,   (inp & GP2X_LEFT) ? -1 : 1,  0,  3); break;\r
984                                 case  2: int_incdec(&Settings.frameskip, (inp & GP2X_LEFT) ? -1 : 1, -1, 32); break;\r
985                                 case  4:\r
986                                         Settings.sound_rate = sndrate_prevnext(Settings.sound_rate, inp & GP2X_RIGHT);\r
987                                         InitSound();\r
988                                         break;\r
989                                 case  5: int_incdec(&Settings.region_force,   (inp & GP2X_LEFT) ? -1 : 1, 0, 2); break;\r
990                                 case  7: {\r
991                                         int hz = Settings.turbo_rate_add*60/2 >> 24;\r
992                                         int_incdec(&hz, (inp & GP2X_LEFT) ? -1 : 1, 1, 30);\r
993                                         Settings.turbo_rate_add = (hz*2 << 24) / 60 + 1;\r
994                                         break;\r
995                                 }\r
996                                 case  8: int_incdec(&Settings.sstate_confirm, (inp & GP2X_LEFT) ? -1 : 1, 0, 3); break;\r
997                                 case  9: int_incdec(&CurrentState,            (inp & GP2X_LEFT) ? -1 : 1, 0, 9); break;\r
998                                 case 12: int_incdec(&Settings.gamma,          (inp & GP2X_LEFT) ? -1 : 1, 0, 300); break;\r
999                                 case 13:\r
1000                                         while ((inp = gp2x_joystick_read(1)) & (GP2X_LEFT|GP2X_RIGHT)) {\r
1001                                                 Settings.cpuclock += (inp & GP2X_LEFT) ? -1 : 1;\r
1002                                                 if (Settings.cpuclock < 0) Settings.cpuclock = 0; // 0 ~ do not change\r
1003                                                 draw_menu_options(menu_sel);\r
1004                                                 usleep(50*1000);\r
1005                                         }\r
1006                                         break;\r
1007                         }\r
1008                 }\r
1009         }\r
1010 }\r
1011 \r
1012 // -------------- credits --------------\r
1013 \r
1014 static void draw_menu_credits(void)\r
1015 {\r
1016         char vstr[16];\r
1017 \r
1018         //int tl_x = 15, tl_y = 70;\r
1019         gp2x_fceu_copy_bg();\r
1020 \r
1021         sprintf(vstr, "GPFCE v" GP2X_PORT_VERSION " rev%i", GP2X_PORT_REV);\r
1022         gp2x_text_out15(20,  30, vstr);\r
1023         gp2x_text_out15(20,  40, "(c) notaz, 2007");\r
1024 \r
1025         gp2x_text_out15(20,  70, "Based on FCE Ultra versions");\r
1026         gp2x_text_out15(20,  80, "0.81 and 0.98.15");\r
1027 \r
1028         gp2x_text_out15(20, 110, "         - Credits - ");\r
1029         gp2x_text_out15(20, 130, "Bero: FCE");\r
1030         gp2x_text_out15(20, 140, "Xodnizel: FCE Ultra");\r
1031         gp2x_text_out15(20, 150, "zzhu8192: original port");\r
1032         gp2x_text_out15(20, 160, "rlyeh: minimal lib");\r
1033         gp2x_text_out15(20, 170, "Hermes, theoddbot, god_at_hell:");\r
1034         gp2x_text_out15(20, 180, "  cpuctrl, gamma libs");\r
1035         gp2x_text_out15(20, 190, "Squidge: squidgehack");\r
1036 \r
1037         gp2x_video_flip();\r
1038 }\r
1039 \r
1040 \r
1041 // -------------- root menu --------------\r
1042 \r
1043 static void draw_menu_root(int menu_sel)\r
1044 {\r
1045         int tl_x = 30, tl_y = 128, y;\r
1046         gp2x_fceu_copy_bg();\r
1047 \r
1048         y = tl_y;\r
1049         if (fceugi) {\r
1050                 gp2x_text_out15(tl_x, y,       "Resume game");\r
1051                 gp2x_text_out15(tl_x, (y+=10), "Save State");\r
1052                 gp2x_text_out15(tl_x, (y+=10), "Load State");\r
1053                 gp2x_text_out15(tl_x, (y+=10), "Reset game");\r
1054         } else {\r
1055                 y += 30;\r
1056         }\r
1057         gp2x_text_out15(tl_x, (y+=10), "Load new ROM");\r
1058         gp2x_text_out15(tl_x, (y+=10), "Options");\r
1059         gp2x_text_out15(tl_x, (y+=10), "Controls");\r
1060         gp2x_text_out15(tl_x, (y+=10), "Credits");\r
1061         gp2x_text_out15(tl_x, (y+=10), "Exit");\r
1062 // TODO\r
1063 //      if (PicoPatches)\r
1064 //              gp2x_text_out15(tl_x, (y+=10), "Patches / GameGenie");\r
1065 \r
1066         // draw cursor\r
1067         gp2x_text_out15(tl_x - 16, tl_y + menu_sel*10, ">");\r
1068         // error\r
1069         if (menuErrorMsg[0]) gp2x_text_out15(1, 230, menuErrorMsg);\r
1070         else {\r
1071                 char vstr[16];\r
1072                 sprintf(vstr, "v" GP2X_PORT_VERSION " r%i", GP2X_PORT_REV);\r
1073                 gp2x_text_out15(320-strlen(vstr)*8-1, 230, vstr);\r
1074         }\r
1075         gp2x_video_flip();\r
1076 }\r
1077 \r
1078 \r
1079 static int menu_loop_root(void)\r
1080 {\r
1081         int ret, menu_sel_max = 8, menu_sel_min = 4;\r
1082         static int menu_sel = 4;\r
1083         unsigned long inp = 0;\r
1084         char curr_path[PATH_MAX], *selfname;\r
1085         FILE *tstf;\r
1086 \r
1087         if ( (tstf = fopen(lastLoadedGameName, "rb")) )\r
1088         {\r
1089                 fclose(tstf);\r
1090                 strncpy(curr_path, lastLoadedGameName, sizeof(curr_path));\r
1091                 curr_path[sizeof(curr_path)-1] = 0;\r
1092         }\r
1093         else\r
1094         {\r
1095                 getcwd(curr_path, PATH_MAX);\r
1096         }\r
1097 \r
1098         if (fceugi) menu_sel_min = 0;\r
1099 // TODO if (PicoPatches) menu_sel_max = 9;\r
1100         if (menu_sel < menu_sel_min || menu_sel > menu_sel_max)\r
1101                 menu_sel = menu_sel_min;\r
1102 \r
1103         /* make sure action buttons are not pressed on entering menu */\r
1104         draw_menu_root(menu_sel);\r
1105         while (gp2x_joystick_read(1) & (GP2X_B|GP2X_X|GP2X_SELECT)) usleep(50*1000);\r
1106 \r
1107         for (;;)\r
1108         {\r
1109                 draw_menu_root(menu_sel);\r
1110                 inp = wait_for_input(GP2X_UP|GP2X_DOWN|GP2X_B|GP2X_X);\r
1111                 if(inp & GP2X_UP  )  { menu_sel--; if (menu_sel < menu_sel_min) menu_sel = menu_sel_max; }\r
1112                 if(inp & GP2X_DOWN)  { menu_sel++; if (menu_sel > menu_sel_max) menu_sel = menu_sel_min; }\r
1113                 if(inp &(GP2X_SELECT|GP2X_X)){\r
1114                         if (fceugi) {\r
1115                                 while (gp2x_joystick_read(1) & GP2X_X) usleep(50*1000); // wait until X is released\r
1116                                 Exit = 0;\r
1117                                 return 0;\r
1118                         }\r
1119                 }\r
1120                 if(inp & GP2X_B   )  {\r
1121                         switch (menu_sel) {\r
1122                                 case 0: // resume game\r
1123                                         if (fceugi) {\r
1124                                                 while (gp2x_joystick_read(1) & GP2X_B) usleep(50*1000);\r
1125                                                 Exit = 0;\r
1126                                                 return 0;\r
1127                                         }\r
1128                                         break;\r
1129                                 case 1: // save state\r
1130                                         if (fceugi) {\r
1131                                                 /*if(savestate_menu_loop(0))\r
1132                                                         continue;*/\r
1133                                                 FCEUI_SaveState();\r
1134                                                 Exit = 0;\r
1135                                                 while (gp2x_joystick_read(1) & GP2X_B) usleep(50*1000);\r
1136                                                 return 0;\r
1137                                         }\r
1138                                         break;\r
1139                                 case 2: // load state\r
1140                                         if (fceugi) {\r
1141                                                 /*if(savestate_menu_loop(1))\r
1142                                                         continue;*/\r
1143                                                 FCEUI_LoadState();\r
1144                                                 Exit = 0;\r
1145                                                 while (gp2x_joystick_read(1) & GP2X_B) usleep(50*1000);\r
1146                                                 return 0;\r
1147                                         }\r
1148                                         break;\r
1149                                 case 3: // reset game\r
1150                                         if (fceugi) {\r
1151                                                 FCEU_DoSimpleCommand(FCEUNPCMD_RESET);\r
1152                                                 Exit = 0;\r
1153                                                 return 0;\r
1154                                         }\r
1155                                         break;\r
1156                                 case 4: // select rom\r
1157                                         selfname = romsel_loop(curr_path);\r
1158                                         if (selfname) {\r
1159                                                 printf("selected file: %s\n", selfname);\r
1160                                                 return 2;\r
1161                                         }\r
1162                                         break;\r
1163                                 case 5: // options\r
1164                                         ret = menu_loop_options();\r
1165                                         if (ret == 1) continue; // status update\r
1166                                         break;\r
1167                                 case 6: // controls\r
1168                                         kc_sel_loop();\r
1169                                         break;\r
1170                                 case 7: // credits\r
1171                                         draw_menu_credits();\r
1172                                         usleep(500*1000);\r
1173                                         inp = wait_for_input(GP2X_B|GP2X_X);\r
1174                                         break;\r
1175                                 case 8: // exit\r
1176                                         return 1;\r
1177                                 case 9: // patches/gg\r
1178                                         break;\r
1179                         }\r
1180                 }\r
1181                 menuErrorMsg[0] = 0; // clear error msg\r
1182         }\r
1183 }\r
1184 \r
1185 \r
1186 static void menu_prepare_bg(void)\r
1187 {\r
1188         // TODO...\r
1189 }\r
1190 \r
1191 static void menu_gfx_prepare(void)\r
1192 {\r
1193         menu_prepare_bg();\r
1194 \r
1195         // switch bpp\r
1196         gp2x_video_changemode(16);\r
1197         gp2x_video_set_offs(0);\r
1198         gp2x_video_RGB_setscaling(0, 320, 240);\r
1199         gp2x_video_flip();\r
1200 }\r
1201 \r
1202 \r
1203 int gp2x_menu_do(void)\r
1204 {\r
1205         int ret;\r
1206 \r
1207         menu_gfx_prepare();\r
1208 \r
1209         ret = menu_loop_root();\r
1210 \r
1211         menuErrorMsg[0] = 0;\r
1212 \r
1213         return ret;\r
1214 }\r
1215 \r
1216 \r