Sonic CD runs on GP2X
[libpicofe.git] / gp2x / menu.c
1 // (c) Copyright 2006 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 "gp2x.h"\r
14 #include "emu.h"\r
15 #include "menu.h"\r
16 #include "usbjoy.h"\r
17 #include "version.h"\r
18 \r
19 #include "Pico/PicoInt.h"\r
20 \r
21 #ifndef _DIRENT_HAVE_D_TYPE\r
22 #error "need d_type for file browser\r
23 #endif\r
24 \r
25 extern char *actionNames[];\r
26 extern char romFileName[PATH_MAX];\r
27 extern char *rom_data;\r
28 extern int  mmuhack_status;\r
29 extern int  state_slot;\r
30 \r
31 static char *gp2xKeyNames[] = {\r
32         "UP",    "???",    "LEFT", "???",  "DOWN", "???", "RIGHT",    "???",\r
33         "START", "SELECT", "L",    "R",    "A",    "B",   "X",        "Y",\r
34         "???",   "???",    "???",  "???",  "???",  "???", "VOL DOWN", "VOL UP",\r
35         "???",   "???",    "???",  "PUSH", "???",  "???", "???",      "???"\r
36 };\r
37 \r
38 char menuErrorMsg[40] = {0, };\r
39 \r
40 \r
41 static unsigned char fontdata8x8[] =\r
42 {\r
43         0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,\r
44         0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,\r
45         0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,\r
46         0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,\r
47         0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,\r
48         0x3C,0x42,0x99,0xBD,0xBD,0x99,0x42,0x3C,0x3C,0x42,0x81,0x81,0x81,0x81,0x42,0x3C,\r
49         0xFE,0x82,0x8A,0xD2,0xA2,0x82,0xFE,0x00,0xFE,0x82,0x82,0x82,0x82,0x82,0xFE,0x00,\r
50         0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x38,0x64,0x74,0x7C,0x38,0x00,0x00,\r
51         0x80,0xC0,0xF0,0xFC,0xF0,0xC0,0x80,0x00,0x01,0x03,0x0F,0x3F,0x0F,0x03,0x01,0x00,\r
52         0x18,0x3C,0x7E,0x18,0x7E,0x3C,0x18,0x00,0xEE,0xEE,0xEE,0xCC,0x00,0xCC,0xCC,0x00,\r
53         0x00,0x00,0x30,0x68,0x78,0x30,0x00,0x00,0x00,0x38,0x64,0x74,0x7C,0x38,0x00,0x00,\r
54         0x3C,0x66,0x7A,0x7A,0x7E,0x7E,0x3C,0x00,0x0E,0x3E,0x3A,0x22,0x26,0x6E,0xE4,0x40,\r
55         0x18,0x3C,0x7E,0x3C,0x3C,0x3C,0x3C,0x00,0x3C,0x3C,0x3C,0x3C,0x7E,0x3C,0x18,0x00,\r
56         0x08,0x7C,0x7E,0x7E,0x7C,0x08,0x00,0x00,0x10,0x3E,0x7E,0x7E,0x3E,0x10,0x00,0x00,\r
57         0x58,0x2A,0xDC,0xC8,0xDC,0x2A,0x58,0x00,0x24,0x66,0xFF,0xFF,0x66,0x24,0x00,0x00,\r
58         0x00,0x10,0x10,0x38,0x38,0x7C,0xFE,0x00,0xFE,0x7C,0x38,0x38,0x10,0x10,0x00,0x00,\r
59         0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x1C,0x1C,0x1C,0x18,0x00,0x18,0x18,0x00,\r
60         0x6C,0x6C,0x24,0x00,0x00,0x00,0x00,0x00,0x00,0x28,0x7C,0x28,0x7C,0x28,0x00,0x00,\r
61         0x10,0x38,0x60,0x38,0x0C,0x78,0x10,0x00,0x40,0xA4,0x48,0x10,0x24,0x4A,0x04,0x00,\r
62         0x18,0x34,0x18,0x3A,0x6C,0x66,0x3A,0x00,0x18,0x18,0x20,0x00,0x00,0x00,0x00,0x00,\r
63         0x30,0x60,0x60,0x60,0x60,0x60,0x30,0x00,0x0C,0x06,0x06,0x06,0x06,0x06,0x0C,0x00,\r
64         0x10,0x54,0x38,0x7C,0x38,0x54,0x10,0x00,0x00,0x18,0x18,0x7E,0x18,0x18,0x00,0x00,\r
65         0x00,0x00,0x00,0x00,0x18,0x18,0x30,0x00,0x00,0x00,0x00,0x00,0x3E,0x00,0x00,0x00,\r
66         0x00,0x00,0x00,0x00,0x18,0x18,0x00,0x00,0x00,0x04,0x08,0x10,0x20,0x40,0x00,0x00,\r
67         0x38,0x4C,0xC6,0xC6,0xC6,0x64,0x38,0x00,0x18,0x38,0x18,0x18,0x18,0x18,0x7E,0x00,\r
68         0x7C,0xC6,0x0E,0x3C,0x78,0xE0,0xFE,0x00,0x7E,0x0C,0x18,0x3C,0x06,0xC6,0x7C,0x00,\r
69         0x1C,0x3C,0x6C,0xCC,0xFE,0x0C,0x0C,0x00,0xFC,0xC0,0xFC,0x06,0x06,0xC6,0x7C,0x00,\r
70         0x3C,0x60,0xC0,0xFC,0xC6,0xC6,0x7C,0x00,0xFE,0xC6,0x0C,0x18,0x30,0x30,0x30,0x00,\r
71         0x78,0xC4,0xE4,0x78,0x86,0x86,0x7C,0x00,0x7C,0xC6,0xC6,0x7E,0x06,0x0C,0x78,0x00,\r
72         0x00,0x00,0x18,0x00,0x00,0x18,0x00,0x00,0x00,0x00,0x18,0x00,0x00,0x18,0x18,0x30,\r
73         0x1C,0x38,0x70,0xE0,0x70,0x38,0x1C,0x00,0x00,0x7C,0x00,0x00,0x7C,0x00,0x00,0x00,\r
74         0x70,0x38,0x1C,0x0E,0x1C,0x38,0x70,0x00,0x7C,0xC6,0xC6,0x1C,0x18,0x00,0x18,0x00,\r
75         0x3C,0x42,0x99,0xA1,0xA5,0x99,0x42,0x3C,0x38,0x6C,0xC6,0xC6,0xFE,0xC6,0xC6,0x00,\r
76         0xFC,0xC6,0xC6,0xFC,0xC6,0xC6,0xFC,0x00,0x3C,0x66,0xC0,0xC0,0xC0,0x66,0x3C,0x00,\r
77         0xF8,0xCC,0xC6,0xC6,0xC6,0xCC,0xF8,0x00,0xFE,0xC0,0xC0,0xFC,0xC0,0xC0,0xFE,0x00,\r
78         0xFE,0xC0,0xC0,0xFC,0xC0,0xC0,0xC0,0x00,0x3E,0x60,0xC0,0xCE,0xC6,0x66,0x3E,0x00,\r
79         0xC6,0xC6,0xC6,0xFE,0xC6,0xC6,0xC6,0x00,0x7E,0x18,0x18,0x18,0x18,0x18,0x7E,0x00,\r
80         0x06,0x06,0x06,0x06,0xC6,0xC6,0x7C,0x00,0xC6,0xCC,0xD8,0xF0,0xF8,0xDC,0xCE,0x00,\r
81         0x60,0x60,0x60,0x60,0x60,0x60,0x7E,0x00,0xC6,0xEE,0xFE,0xFE,0xD6,0xC6,0xC6,0x00,\r
82         0xC6,0xE6,0xF6,0xFE,0xDE,0xCE,0xC6,0x00,0x7C,0xC6,0xC6,0xC6,0xC6,0xC6,0x7C,0x00,\r
83         0xFC,0xC6,0xC6,0xC6,0xFC,0xC0,0xC0,0x00,0x7C,0xC6,0xC6,0xC6,0xDE,0xCC,0x7A,0x00,\r
84         0xFC,0xC6,0xC6,0xCE,0xF8,0xDC,0xCE,0x00,0x78,0xCC,0xC0,0x7C,0x06,0xC6,0x7C,0x00,\r
85         0x7E,0x18,0x18,0x18,0x18,0x18,0x18,0x00,0xC6,0xC6,0xC6,0xC6,0xC6,0xC6,0x7C,0x00,\r
86         0xC6,0xC6,0xC6,0xEE,0x7C,0x38,0x10,0x00,0xC6,0xC6,0xD6,0xFE,0xFE,0xEE,0xC6,0x00,\r
87         0xC6,0xEE,0x3C,0x38,0x7C,0xEE,0xC6,0x00,0x66,0x66,0x66,0x3C,0x18,0x18,0x18,0x00,\r
88         0xFE,0x0E,0x1C,0x38,0x70,0xE0,0xFE,0x00,0x3C,0x30,0x30,0x30,0x30,0x30,0x3C,0x00,\r
89         0x60,0x60,0x30,0x18,0x0C,0x06,0x06,0x00,0x3C,0x0C,0x0C,0x0C,0x0C,0x0C,0x3C,0x00,\r
90         0x18,0x3C,0x66,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xFF,\r
91         0x30,0x30,0x18,0x00,0x00,0x00,0x00,0x00,0x00,0x3C,0x06,0x3E,0x66,0x66,0x3C,0x00,\r
92         0x60,0x7C,0x66,0x66,0x66,0x66,0x7C,0x00,0x00,0x3C,0x66,0x60,0x60,0x66,0x3C,0x00,\r
93         0x06,0x3E,0x66,0x66,0x66,0x66,0x3E,0x00,0x00,0x3C,0x66,0x66,0x7E,0x60,0x3C,0x00,\r
94         0x1C,0x30,0x78,0x30,0x30,0x30,0x30,0x00,0x00,0x3E,0x66,0x66,0x66,0x3E,0x06,0x3C,\r
95         0x60,0x7C,0x76,0x66,0x66,0x66,0x66,0x00,0x18,0x00,0x38,0x18,0x18,0x18,0x18,0x00,\r
96         0x0C,0x00,0x1C,0x0C,0x0C,0x0C,0x0C,0x38,0x60,0x60,0x66,0x6C,0x78,0x6C,0x66,0x00,\r
97         0x38,0x18,0x18,0x18,0x18,0x18,0x18,0x00,0x00,0xEC,0xFE,0xFE,0xFE,0xD6,0xC6,0x00,\r
98         0x00,0x7C,0x76,0x66,0x66,0x66,0x66,0x00,0x00,0x3C,0x66,0x66,0x66,0x66,0x3C,0x00,\r
99         0x00,0x7C,0x66,0x66,0x66,0x7C,0x60,0x60,0x00,0x3E,0x66,0x66,0x66,0x3E,0x06,0x06,\r
100         0x00,0x7E,0x70,0x60,0x60,0x60,0x60,0x00,0x00,0x3C,0x60,0x3C,0x06,0x66,0x3C,0x00,\r
101         0x30,0x78,0x30,0x30,0x30,0x30,0x1C,0x00,0x00,0x66,0x66,0x66,0x66,0x6E,0x3E,0x00,\r
102         0x00,0x66,0x66,0x66,0x66,0x3C,0x18,0x00,0x00,0xC6,0xD6,0xFE,0xFE,0x7C,0x6C,0x00,\r
103         0x00,0x66,0x3C,0x18,0x3C,0x66,0x66,0x00,0x00,0x66,0x66,0x66,0x66,0x3E,0x06,0x3C,\r
104         0x00,0x7E,0x0C,0x18,0x30,0x60,0x7E,0x00,0x0E,0x18,0x0C,0x38,0x0C,0x18,0x0E,0x00,\r
105         0x18,0x18,0x18,0x00,0x18,0x18,0x18,0x00,0x70,0x18,0x30,0x1C,0x30,0x18,0x70,0x00,\r
106         0x00,0x00,0x76,0xDC,0x00,0x00,0x00,0x00,0x10,0x28,0x10,0x54,0xAA,0x44,0x00,0x00,\r
107 };\r
108 \r
109 static void gp2x_text(unsigned char *screen, int x, int y, char *text, int color)\r
110 {\r
111         int i,l;\r
112 \r
113         screen = screen + x + y*320;\r
114 \r
115         for (i = 0; i < strlen(text); i++)\r
116         {\r
117                 for (l=0;l<8;l++)\r
118                 {\r
119                         if(fontdata8x8[((text[i])*8)+l]&0x80) screen[l*320+0]=color;\r
120                         if(fontdata8x8[((text[i])*8)+l]&0x40) screen[l*320+1]=color;\r
121                         if(fontdata8x8[((text[i])*8)+l]&0x20) screen[l*320+2]=color;\r
122                         if(fontdata8x8[((text[i])*8)+l]&0x10) screen[l*320+3]=color;\r
123                         if(fontdata8x8[((text[i])*8)+l]&0x08) screen[l*320+4]=color;\r
124                         if(fontdata8x8[((text[i])*8)+l]&0x04) screen[l*320+5]=color;\r
125                         if(fontdata8x8[((text[i])*8)+l]&0x02) screen[l*320+6]=color;\r
126                         if(fontdata8x8[((text[i])*8)+l]&0x01) screen[l*320+7]=color;\r
127                 }\r
128                 screen += 8;\r
129         }\r
130 }\r
131 \r
132 // draws white text to current bbp15 screen\r
133 void gp2x_text_out15(int x, int y, char *text)\r
134 {\r
135         int i,l;\r
136         unsigned short *screen = gp2x_screen;\r
137 \r
138         screen = screen + x + y*320;\r
139 \r
140         for (i = 0; i < strlen(text); i++)\r
141         {\r
142                 for (l=0;l<8;l++)\r
143                 {\r
144                         if(fontdata8x8[((text[i])*8)+l]&0x80) screen[l*320+0]=0xffff;\r
145                         if(fontdata8x8[((text[i])*8)+l]&0x40) screen[l*320+1]=0xffff;\r
146                         if(fontdata8x8[((text[i])*8)+l]&0x20) screen[l*320+2]=0xffff;\r
147                         if(fontdata8x8[((text[i])*8)+l]&0x10) screen[l*320+3]=0xffff;\r
148                         if(fontdata8x8[((text[i])*8)+l]&0x08) screen[l*320+4]=0xffff;\r
149                         if(fontdata8x8[((text[i])*8)+l]&0x04) screen[l*320+5]=0xffff;\r
150                         if(fontdata8x8[((text[i])*8)+l]&0x02) screen[l*320+6]=0xffff;\r
151                         if(fontdata8x8[((text[i])*8)+l]&0x01) screen[l*320+7]=0xffff;\r
152                 }\r
153                 screen += 8;\r
154         }\r
155 }\r
156 \r
157 \r
158 void gp2x_text_out8(int x, int y, char *texto, ...)\r
159 {\r
160         va_list args;\r
161         char    buffer[512];\r
162 \r
163         va_start(args,texto);\r
164         vsprintf(buffer,texto,args);\r
165         va_end(args);\r
166 \r
167         gp2x_text(gp2x_screen,x,y,buffer,1);\r
168 }\r
169 \r
170 \r
171 void gp2x_text_out8_2(int x, int y, char *texto, int color)\r
172 {\r
173         gp2x_text(gp2x_screen, x, y, texto, color);\r
174 }\r
175 \r
176 void gp2x_text_out8_lim(int x, int y, char *texto, int max)\r
177 {\r
178         char    buffer[320/8+1];\r
179 \r
180         strncpy(buffer, texto, 320/8);\r
181         if (max > 320/8) max = 320/8;\r
182         if (max < 0) max = 0;\r
183         buffer[max] = 0;\r
184 \r
185         gp2x_text(gp2x_screen,x,y,buffer,1);\r
186 }\r
187 \r
188 \r
189 static unsigned long inp_prev = 0;\r
190 static int inp_prevjoy = 0;\r
191 \r
192 static unsigned long wait_for_input(unsigned long interesting)\r
193 {\r
194         unsigned long ret;\r
195         static int repeats = 0, wait = 300*1000;\r
196         int release = 0, i;\r
197 \r
198         if (repeats == 5 || repeats == 15 || repeats == 30) wait /= 2;\r
199 \r
200         for (i = 0; i < 6 && inp_prev == gp2x_joystick_read(1); i++) {\r
201                 if(i == 0) repeats++;\r
202                 usleep(wait/6);\r
203         }\r
204 \r
205         while ( !((ret = gp2x_joystick_read(1)) & interesting) ) {\r
206                 usleep(50000);\r
207                 release = 1;\r
208         }\r
209 \r
210         if (release || ret != inp_prev) {\r
211                 repeats = 0;\r
212                 wait = 300*1000;\r
213         }\r
214         inp_prev = ret;\r
215         inp_prevjoy = 0;\r
216 \r
217         // we don't need diagonals in menus\r
218         if ((ret&GP2X_UP)   && (ret&GP2X_LEFT))  ret &= ~GP2X_LEFT;\r
219         if ((ret&GP2X_UP)   && (ret&GP2X_RIGHT)) ret &= ~GP2X_RIGHT;\r
220         if ((ret&GP2X_DOWN) && (ret&GP2X_LEFT))  ret &= ~GP2X_LEFT;\r
221         if ((ret&GP2X_DOWN) && (ret&GP2X_RIGHT)) ret &= ~GP2X_RIGHT;\r
222 \r
223         return ret;\r
224 }\r
225 \r
226 static unsigned long input2_read(unsigned long interesting, int *joy)\r
227 {\r
228         unsigned long ret;\r
229         int i;\r
230 \r
231         do\r
232         {\r
233                 *joy = 0;\r
234                 if ((ret = gp2x_joystick_read(0) & interesting)) break;\r
235                 gp2x_usbjoy_update();\r
236                 for (i = 0; i < num_of_joys; i++) {\r
237                         ret = gp2x_usbjoy_check2(i);\r
238                         if (ret) { *joy = i + 1; break; }\r
239                 }\r
240                 if (ret) break;\r
241         }\r
242         while(0);\r
243 \r
244         return ret;\r
245 }\r
246 \r
247 // similar to wait_for_input(), but returns joy num\r
248 static unsigned long wait_for_input_usbjoy(unsigned long interesting, int *joy)\r
249 {\r
250         unsigned long ret;\r
251         const int wait = 300*1000;\r
252         int i;\r
253 \r
254         if (inp_prevjoy == 0) inp_prev &= interesting;\r
255         for (i = 0; i < 6; i++) {\r
256                 ret = input2_read(interesting, joy);\r
257                 if (*joy != inp_prevjoy || ret != inp_prev) break;\r
258                 usleep(wait/6);\r
259         }\r
260 \r
261         while ( !(ret = input2_read(interesting, joy)) ) {\r
262                 usleep(50000);\r
263         }\r
264 \r
265         inp_prev = ret;\r
266         inp_prevjoy = *joy;\r
267 \r
268         return ret;\r
269 }\r
270 \r
271 \r
272 \r
273 // -------------- ROM selector --------------\r
274 \r
275 static void draw_dirlist(char *curdir, struct dirent **namelist, int n, int sel)\r
276 {\r
277         int start, i, pos;\r
278 \r
279         start = 12 - sel;\r
280         n--; // exclude current dir (".")\r
281 \r
282         memset(gp2x_screen, 0, 320*240);\r
283 \r
284         if(start - 2 >= 0)\r
285                 gp2x_text_out8_lim(14, (start - 2)*10, curdir, 38);\r
286         for (i = 0; i < n; i++) {\r
287                 pos = start + i;\r
288                 if (pos < 0)  continue;\r
289                 if (pos > 23) break;\r
290                 if (namelist[i+1]->d_type == DT_DIR) {\r
291                         gp2x_text_out8_lim(14,   pos*10, "/", 1);\r
292                         gp2x_text_out8_lim(14+8, pos*10, namelist[i+1]->d_name, 37);\r
293                 } else {\r
294                         gp2x_text_out8_lim(14,   pos*10, namelist[i+1]->d_name, 38);\r
295                 }\r
296         }\r
297         gp2x_text_out8(5, 120, ">");\r
298         gp2x_video_flip();\r
299 }\r
300 \r
301 static int scandir_cmp(const void *p1, const void *p2)\r
302 {\r
303         struct dirent **d1 = (struct dirent **)p1, **d2 = (struct dirent **)p2;\r
304         if ((*d1)->d_type == (*d2)->d_type) return alphasort(d1, d2);\r
305         if ((*d1)->d_type == DT_DIR) return -1; // put before\r
306         if ((*d2)->d_type == DT_DIR) return  1;\r
307         return alphasort(d1, d2);\r
308 }\r
309 \r
310 \r
311 static char *romsel_loop(char *curr_path)\r
312 {\r
313         struct dirent **namelist;\r
314         DIR *dir;\r
315         int n, sel = 0;\r
316         unsigned long inp = 0;\r
317         char *ret = NULL, *fname = NULL;\r
318 \r
319         // is this a dir or a full path?\r
320         if ((dir = opendir(curr_path))) {\r
321                 closedir(dir);\r
322         } else {\r
323                 char *p;\r
324                 for (p = curr_path + strlen(curr_path) - 1; p > curr_path && *p != '/'; p--);\r
325                 *p = 0;\r
326                 fname = p+1;\r
327         }\r
328 \r
329         n = scandir(curr_path, &namelist, 0, scandir_cmp);\r
330         if (n < 0) {\r
331                 // try root\r
332                 n = scandir(curr_path, &namelist, 0, scandir_cmp);\r
333                 if (n < 0) {\r
334                         // oops, we failed\r
335                         printf("dir: "); printf(curr_path); printf("\n");\r
336                         perror("scandir");\r
337                         return NULL;\r
338                 }\r
339         }\r
340 \r
341         // try to find sel\r
342         if (fname != NULL) {\r
343                 int i;\r
344                 for (i = 1; i < n; i++) {\r
345                         if (strcmp(namelist[i]->d_name, fname) == 0) {\r
346                                 sel = i - 1;\r
347                                 break;\r
348                         }\r
349                 }\r
350         }\r
351 \r
352         for (;;)\r
353         {\r
354                 draw_dirlist(curr_path, namelist, n, sel);\r
355                 inp = wait_for_input(GP2X_UP|GP2X_DOWN|GP2X_LEFT|GP2X_RIGHT|GP2X_L|GP2X_R|GP2X_B|GP2X_X);\r
356                 if(inp & GP2X_UP  )  { sel--;   if (sel < 0)   sel = n-2; }\r
357                 if(inp & GP2X_DOWN)  { sel++;   if (sel > n-2) sel = 0; }\r
358                 if(inp &(GP2X_LEFT|GP2X_L))  { sel-=10; if (sel < 0)   sel = 0; }\r
359                 if(inp &(GP2X_RIGHT|GP2X_R)) { sel+=10; if (sel > n-2) sel = n-2; }\r
360                 if(inp & GP2X_B)     { // enter dir/select\r
361                         again:\r
362                         if (namelist[sel+1]->d_type == DT_REG) {\r
363                                 strcpy(romFileName, curr_path);\r
364                                 strcat(romFileName, "/");\r
365                                 strcat(romFileName, namelist[sel+1]->d_name);\r
366                                 ret = romFileName;\r
367                                 break;\r
368                         } else if (namelist[sel+1]->d_type == DT_DIR) {\r
369                                 int newlen = strlen(curr_path) + strlen(namelist[sel+1]->d_name) + 2;\r
370                                 char *p, *newdir = malloc(newlen);\r
371                                 if (strcmp(namelist[sel+1]->d_name, "..") == 0) {\r
372                                         char *start = curr_path;\r
373                                         p = start + strlen(start) - 1;\r
374                                         while (*p == '/' && p > start) p--;\r
375                                         while (*p != '/' && p > start) p--;\r
376                                         if (p <= start) strcpy(newdir, "/");\r
377                                         else { strncpy(newdir, start, p-start); newdir[p-start] = 0; }\r
378                                 } else {\r
379                                         strcpy(newdir, curr_path);\r
380                                         p = newdir + strlen(newdir) - 1;\r
381                                         while (*p == '/' && p >= newdir) *p-- = 0;\r
382                                         strcat(newdir, "/");\r
383                                         strcat(newdir, namelist[sel+1]->d_name);\r
384                                 }\r
385                                 ret = romsel_loop(newdir);\r
386                                 free(newdir);\r
387                                 break;\r
388                         } else {\r
389                                 // unknown file type, happens on NTFS mounts. Try to guess.\r
390                                 FILE *tstf; int tmp;\r
391                                 strcpy(romFileName, curr_path);\r
392                                 strcat(romFileName, "/");\r
393                                 strcat(romFileName, namelist[sel+1]->d_name);\r
394                                 tstf = fopen(romFileName, "rb");\r
395                                 if (tstf != NULL)\r
396                                 {\r
397                                         if (fread(&tmp, 1, 1, tstf) > 0 || ferror(tstf) == 0)\r
398                                                 namelist[sel+1]->d_type = DT_REG;\r
399                                         else    namelist[sel+1]->d_type = DT_DIR;\r
400                                         fclose(tstf);\r
401                                         goto again;\r
402                                 }\r
403                         }\r
404                 }\r
405                 if(inp & GP2X_X) break; // cancel\r
406         }\r
407 \r
408         if (n > 0) {\r
409                 while(n--) free(namelist[n]);\r
410                 free(namelist);\r
411         }\r
412 \r
413         return ret;\r
414 }\r
415 \r
416 // -------------- key config --------------\r
417 \r
418 static char *usb_joy_key_name(int joy, int num)\r
419 {\r
420         static char name[16];\r
421         switch (num)\r
422         {\r
423                 case 0: sprintf(name, "Joy%i UP", joy); break;\r
424                 case 1: sprintf(name, "Joy%i DOWN", joy); break;\r
425                 case 2: sprintf(name, "Joy%i LEFT", joy); break;\r
426                 case 3: sprintf(name, "Joy%i RIGHT", joy); break;\r
427                 default:sprintf(name, "Joy%i b%i", joy, num-3); break;\r
428         }\r
429         return name;\r
430 }\r
431 \r
432 static void draw_key_config(int curr_act, int is_p2)\r
433 {\r
434         char strkeys[32*5];\r
435         int joy, i;\r
436 \r
437         strkeys[0] = 0;\r
438         for (i = 0; i < 32; i++)\r
439         {\r
440                 if (currentConfig.KeyBinds[i] & (1 << curr_act))\r
441                 {\r
442                         if (curr_act < 16 && (currentConfig.KeyBinds[i] & (1 << 16)) != (is_p2 << 16)) continue;\r
443                         if (strkeys[0]) { strcat(strkeys, " + "); strcat(strkeys, gp2xKeyNames[i]); break; }\r
444                         else strcpy(strkeys, gp2xKeyNames[i]);\r
445                 }\r
446         }\r
447         for (joy = 0; joy < num_of_joys; joy++)\r
448         {\r
449                 for (i = 0; i < 32; i++)\r
450                 {\r
451                         if (currentConfig.JoyBinds[joy][i] & (1 << curr_act))\r
452                         {\r
453                                 if (curr_act < 16 && (currentConfig.JoyBinds[joy][i] & (1 << 16)) != (is_p2 << 16)) continue;\r
454                                 if (strkeys[0]) {\r
455                                         strcat(strkeys, ", "); strcat(strkeys, usb_joy_key_name(joy + 1, i));\r
456                                         break;\r
457                                 }\r
458                                 else strcpy(strkeys, usb_joy_key_name(joy + 1, i));\r
459                         }\r
460                 }\r
461         }\r
462 \r
463         memset(gp2x_screen, 0, 320*240);\r
464         gp2x_text_out8(60, 40, "Action: %s", actionNames[curr_act]);\r
465         gp2x_text_out8(60, 60, "Keys: %s", strkeys);\r
466 \r
467         gp2x_text_out8(30, 180, "Use SELECT to change action");\r
468         gp2x_text_out8(30, 190, "Press a key to bind/unbind");\r
469         gp2x_text_out8(30, 200, "Select \"Done\" action and");\r
470         gp2x_text_out8(30, 210, "  press any key to finish");\r
471         gp2x_video_flip();\r
472 }\r
473 \r
474 static void key_config_loop(int is_p2)\r
475 {\r
476         int curr_act = 0, joy = 0, i;\r
477         unsigned long inp = 0;\r
478 \r
479         for (;;)\r
480         {\r
481                 draw_key_config(curr_act, is_p2);\r
482                 inp = wait_for_input_usbjoy(CONFIGURABLE_KEYS, &joy);\r
483                 // printf("got %08lX from joy %i\n", inp, joy);\r
484                 if (joy == 0) {\r
485                         if (inp & GP2X_SELECT) {\r
486                                 curr_act++;\r
487                                 while (!actionNames[curr_act] && curr_act < 32) curr_act++;\r
488                                 if (curr_act > 31) curr_act = 0;\r
489                         }\r
490                         inp &= CONFIGURABLE_KEYS;\r
491                         inp &= ~GP2X_SELECT;\r
492                 }\r
493                 if (curr_act == 31 && inp) break;\r
494                 if (joy == 0) {\r
495                         for (i = 0; i < 32; i++)\r
496                                 if (inp & (1 << i)) {\r
497                                         currentConfig.KeyBinds[i] ^= (1 << curr_act);\r
498                                         if (is_p2) currentConfig.KeyBinds[i] |=  (1 << 16); // player 2 flag\r
499                                         else       currentConfig.KeyBinds[i] &= ~(1 << 16);\r
500                                 }\r
501                 } else {\r
502                         for (i = 0; i < 32; i++)\r
503                                 if (inp & (1 << i)) {\r
504                                         currentConfig.JoyBinds[joy-1][i] ^= (1 << curr_act);\r
505                                         if (is_p2) currentConfig.JoyBinds[joy-1][i] |=  (1 << 16);\r
506                                         else       currentConfig.JoyBinds[joy-1][i] &= ~(1 << 16);\r
507                                 }\r
508                 }\r
509         }\r
510 }\r
511 \r
512 static void draw_kc_sel(int menu_sel)\r
513 {\r
514         int tl_x = 25+40, tl_y = 60, y, i;\r
515         char joyname[36];\r
516 \r
517         y = tl_y;\r
518         memset(gp2x_screen, 0, 320*240);\r
519         gp2x_text_out8(tl_x, y,       "Player 1");\r
520         gp2x_text_out8(tl_x, (y+=10), "Player 2");\r
521         gp2x_text_out8(tl_x, (y+=10), "Done");\r
522 \r
523         // draw cursor\r
524         gp2x_text_out8(tl_x - 16, tl_y + menu_sel*10, ">");\r
525 \r
526         tl_x = 25;\r
527         gp2x_text_out8(tl_x, (y=110), "USB joys detected:");\r
528         if (num_of_joys > 0) {\r
529                 for (i = 0; i < num_of_joys; i++) {\r
530                         strncpy(joyname, joy_name(joys[i]), 33); joyname[33] = 0;\r
531                         gp2x_text_out8(tl_x, (y+=10), "%i: %s", i+1, joyname);\r
532                 }\r
533         } else {\r
534                 gp2x_text_out8(tl_x, (y+=10), "none");\r
535         }\r
536 \r
537 \r
538         gp2x_video_flip();\r
539 }\r
540 \r
541 static void kc_sel_loop(void)\r
542 {\r
543         int menu_sel = 2, menu_sel_max = 2;\r
544         unsigned long inp = 0;\r
545 \r
546         for(;;)\r
547         {\r
548                 draw_kc_sel(menu_sel);\r
549                 inp = wait_for_input(GP2X_UP|GP2X_DOWN|GP2X_B|GP2X_X);\r
550                 if(inp & GP2X_UP  ) { menu_sel--; if (menu_sel < 0) menu_sel = menu_sel_max; }\r
551                 if(inp & GP2X_DOWN) { menu_sel++; if (menu_sel > menu_sel_max) menu_sel = 0; }\r
552                 if(inp & GP2X_B) {\r
553                         switch (menu_sel) {\r
554                                 case 0: key_config_loop(0); return;\r
555                                 case 1: key_config_loop(1); return;\r
556                                 default: return;\r
557                         }\r
558                 }\r
559                 if(inp & GP2X_X) return;\r
560         }\r
561 }\r
562 \r
563 \r
564 \r
565 // order must match that of currentConfig_t\r
566 \r
567 struct {\r
568         int EmuOpt;\r
569         int PicoOpt;\r
570         int PsndRate;\r
571         int PicoRegion;\r
572         int Frameskip;\r
573         int CPUclock;\r
574 } tmp_opts;\r
575 int tmp_gamma;\r
576 \r
577 // --------- sega/mega cd options ----------\r
578 \r
579 static void draw_cd_menu_options(int menu_sel, char *b_us, char *b_eu, char *b_jp)\r
580 {\r
581         int tl_x = 25, tl_y = 60, y;\r
582 \r
583         y = tl_y;\r
584         memset(gp2x_screen, 0, 320*240);\r
585         gp2x_text_out8(tl_x, y,       "USA BIOS:     %s", b_us); // 0\r
586         gp2x_text_out8(tl_x, (y+=10), "EUR BIOS:     %s", b_eu); // 1\r
587         gp2x_text_out8(tl_x, (y+=10), "JAP BIOS:     %s", b_jp); // 2\r
588         gp2x_text_out8(tl_x, (y+=10), "CD LEDs                    %s", (tmp_opts.EmuOpt &0x400)?"ON":"OFF"); // 3\r
589         gp2x_text_out8(tl_x, (y+=10), "CDDA audio (using mp3s)    %s", (tmp_opts.EmuOpt &0x800)?"ON":"OFF"); // 4\r
590         gp2x_text_out8(tl_x, (y+=10), "Done");\r
591 \r
592         // draw cursor\r
593         gp2x_text_out8(tl_x - 16, tl_y + menu_sel*10, ">");\r
594 \r
595         if ((menu_sel == 0 && strcmp(b_us, "NOT FOUND")) ||\r
596                 (menu_sel == 1 && strcmp(b_eu, "NOT FOUND")) ||\r
597                 (menu_sel == 2 && strcmp(b_jp, "NOT FOUND")))\r
598                         gp2x_text_out8(tl_x, 220, "Press start to test selected BIOS");\r
599 \r
600         gp2x_video_flip();\r
601 }\r
602 \r
603 static void cd_menu_loop_options(void)\r
604 {\r
605         int menu_sel = 0, menu_sel_max = 5;\r
606         unsigned long inp = 0;\r
607         char bios_us[32], bios_eu[32], bios_jp[32], *bios, *p;\r
608 \r
609         if (find_bios(4, &bios)) { // US\r
610                 for (p = bios+strlen(bios)-1; p > bios && *p != '/'; p--); p++;\r
611                 strncpy(bios_us, p, 31); bios_us[31] = 0;\r
612         } else  strcpy(bios_us, "NOT FOUND");\r
613 \r
614         if (find_bios(8, &bios)) { // EU\r
615                 for (p = bios+strlen(bios)-1; p > bios && *p != '/'; p--); p++;\r
616                 strncpy(bios_eu, p, 31); bios_eu[31] = 0;\r
617         } else  strcpy(bios_eu, "NOT FOUND");\r
618 \r
619         if (find_bios(1, &bios)) { // JP\r
620                 for (p = bios+strlen(bios)-1; p > bios && *p != '/'; p--); p++;\r
621                 strncpy(bios_jp, p, 31); bios_jp[31] = 0;\r
622         } else  strcpy(bios_jp, "NOT FOUND");\r
623 \r
624         for(;;)\r
625         {\r
626                 draw_cd_menu_options(menu_sel, bios_us, bios_eu, bios_jp);\r
627                 inp = wait_for_input(GP2X_UP|GP2X_DOWN|GP2X_LEFT|GP2X_RIGHT|GP2X_B|GP2X_X|GP2X_A|GP2X_START);\r
628                 if(inp & GP2X_UP  ) { menu_sel--; if (menu_sel < 0) menu_sel = menu_sel_max; }\r
629                 if(inp & GP2X_DOWN) { menu_sel++; if (menu_sel > menu_sel_max) menu_sel = 0; }\r
630                 if((inp& GP2X_B)||(inp&GP2X_LEFT)||(inp&GP2X_RIGHT)) { // toggleable options\r
631                         switch (menu_sel) {\r
632                                 case  3: tmp_opts.EmuOpt ^=0x400; break;\r
633                                 case  4: return;\r
634                         }\r
635                 }\r
636                 if(inp & (GP2X_X|GP2X_A)) return;\r
637                 if(inp &  GP2X_START) { // BIOS testers\r
638                         switch (menu_sel) {\r
639                                 case 0: if (find_bios(4, &bios)) { // test US\r
640                                                 strcpy(romFileName, bios);\r
641                                                 engineState = PGS_ReloadRom;\r
642                                                 return;\r
643                                         }\r
644                                         break;\r
645                                 case 1: if (find_bios(8, &bios)) { // test EU\r
646                                                 strcpy(romFileName, bios);\r
647                                                 engineState = PGS_ReloadRom;\r
648                                                 return;\r
649                                         }\r
650                                         break;\r
651                                 case 2: if (find_bios(1, &bios)) { // test JP\r
652                                                 strcpy(romFileName, bios);\r
653                                                 engineState = PGS_ReloadRom;\r
654                                                 return;\r
655                                         }\r
656                                         break;\r
657                         }\r
658                 }\r
659         }\r
660 }\r
661 \r
662 \r
663 // --------- advanced options ----------\r
664 \r
665 static void draw_amenu_options(int menu_sel)\r
666 {\r
667         int tl_x = 25, tl_y = 60, y;\r
668         char *mms = mmuhack_status ? "active)  " : "inactive)";\r
669 \r
670         y = tl_y;\r
671         memset(gp2x_screen, 0, 320*240);\r
672         gp2x_text_out8(tl_x, y,       "Scale 32 column mode       %s", (tmp_opts.PicoOpt&0x100)?"ON":"OFF"); // 0\r
673         gp2x_text_out8(tl_x, (y+=10), "Gamma correction           %i.%02i", tmp_gamma / 100, tmp_gamma%100); // 1\r
674         gp2x_text_out8(tl_x, (y+=10), "Emulate Z80                %s", (tmp_opts.PicoOpt&0x004)?"ON":"OFF"); // 2\r
675         gp2x_text_out8(tl_x, (y+=10), "Emulate YM2612 (FM)        %s", (tmp_opts.PicoOpt&0x001)?"ON":"OFF"); // 3\r
676         gp2x_text_out8(tl_x, (y+=10), "Emulate SN76496 (PSG)      %s", (tmp_opts.PicoOpt&0x002)?"ON":"OFF"); // 4\r
677         gp2x_text_out8(tl_x, (y+=10), "gzip savestates            %s", (tmp_opts.EmuOpt &0x008)?"ON":"OFF"); // 5\r
678         gp2x_text_out8(tl_x, (y+=10), "Don't save config on exit  %s", (tmp_opts.EmuOpt &0x020)?"ON":"OFF"); // 6\r
679         gp2x_text_out8(tl_x, (y+=10), "needs restart:");\r
680         gp2x_text_out8(tl_x, (y+=10), "craigix's RAM timings      %s", (tmp_opts.EmuOpt &0x100)?"ON":"OFF"); // 8\r
681         gp2x_text_out8(tl_x, (y+=10), "squidgehack (now %s %s",   mms, (tmp_opts.EmuOpt &0x010)?"ON":"OFF"); // 9\r
682         gp2x_text_out8(tl_x, (y+=10), "Done");\r
683 \r
684         // draw cursor\r
685         gp2x_text_out8(tl_x - 16, tl_y + menu_sel*10, ">");\r
686 \r
687         gp2x_video_flip();\r
688 }\r
689 \r
690 static void amenu_loop_options(void)\r
691 {\r
692         int menu_sel = 0, menu_sel_max = 10;\r
693         unsigned long inp = 0;\r
694 \r
695         for(;;)\r
696         {\r
697                 draw_amenu_options(menu_sel);\r
698                 inp = wait_for_input(GP2X_UP|GP2X_DOWN|GP2X_LEFT|GP2X_RIGHT|GP2X_B|GP2X_X|GP2X_A);\r
699                 if(inp & GP2X_UP  ) { menu_sel--; if (menu_sel < 0) menu_sel = menu_sel_max; }\r
700                 if(inp & GP2X_DOWN) { menu_sel++; if (menu_sel > menu_sel_max) menu_sel = 0; }\r
701                 if((inp& GP2X_B)||(inp&GP2X_LEFT)||(inp&GP2X_RIGHT)) { // toggleable options\r
702                         switch (menu_sel) {\r
703                                 case  0: tmp_opts.PicoOpt^=0x100; break;\r
704                                 case  2: tmp_opts.PicoOpt^=0x004; break;\r
705                                 case  3: tmp_opts.PicoOpt^=0x001; break;\r
706                                 case  4: tmp_opts.PicoOpt^=0x002; break;\r
707                                 case  5: tmp_opts.EmuOpt ^=0x008; break;\r
708                                 case  6: tmp_opts.EmuOpt ^=0x020; break;\r
709                                 case  8: tmp_opts.EmuOpt ^=0x100; break;\r
710                                 case  9: tmp_opts.EmuOpt ^=0x010; break;\r
711                                 case 10: return;\r
712                         }\r
713                 }\r
714                 if(inp & (GP2X_X|GP2X_A)) return;\r
715                 if(inp & (GP2X_LEFT|GP2X_RIGHT)) { // multi choise\r
716                         switch (menu_sel) {\r
717                                 case 1:\r
718                                         while ((inp = gp2x_joystick_read(1)) & (GP2X_LEFT|GP2X_RIGHT)) {\r
719                                                 tmp_gamma += (inp & GP2X_LEFT) ? -1 : 1;\r
720                                                 if (tmp_gamma <   1) tmp_gamma =   1;\r
721                                                 if (tmp_gamma > 300) tmp_gamma = 300;\r
722                                                 draw_amenu_options(menu_sel);\r
723                                                 usleep(18*1000);\r
724                                         }\r
725                                         break;\r
726                         }\r
727                 }\r
728         }\r
729 }\r
730 \r
731 // -------------- options --------------\r
732 \r
733 static char *region_name(unsigned int code)\r
734 {\r
735         char *names[] = { "Auto", "Japan NTSC", "Japan PAL", "USA", "Europe" };\r
736         int i = 0;\r
737         code <<= 1;\r
738         while((code >>=1)) i++;\r
739         if (i > 4) return "unknown";\r
740         return names[i];\r
741 }\r
742 \r
743 static void draw_menu_options(int menu_sel)\r
744 {\r
745         int tl_x = 25, tl_y = 40, y;\r
746         char monostereo[8], strframeskip[8], *strrend;\r
747 \r
748         strcpy(monostereo, (tmp_opts.PicoOpt&0x08)?"stereo":"mono");\r
749         if (tmp_opts.Frameskip < 0)\r
750                  strcpy(strframeskip, "Auto");\r
751         else sprintf(strframeskip, "%i", tmp_opts.Frameskip);\r
752         if (tmp_opts.PicoOpt&0x10) {\r
753                 strrend = " 8bit fast";\r
754         } else if (tmp_opts.EmuOpt&0x80) {\r
755                 strrend = "16bit accurate";\r
756         } else {\r
757                 strrend = " 8bit accurate";\r
758         }\r
759 \r
760         y = tl_y;\r
761         memset(gp2x_screen, 0, 320*240);\r
762         gp2x_text_out8(tl_x, y,       "Renderer:            %s", strrend); // 0\r
763         gp2x_text_out8(tl_x, (y+=10), "Accurate timing (slower)   %s", (tmp_opts.PicoOpt&0x040)?"ON":"OFF"); // 1\r
764         gp2x_text_out8(tl_x, (y+=10), "Accurate sprites (slower)  %s", (tmp_opts.PicoOpt&0x080)?"ON":"OFF"); // 2\r
765         gp2x_text_out8(tl_x, (y+=10), "Show FPS                   %s", (tmp_opts.EmuOpt &0x002)?"ON":"OFF"); // 3\r
766         gp2x_text_out8(tl_x, (y+=10), "Frameskip                  %s", strframeskip);\r
767         gp2x_text_out8(tl_x, (y+=10), "Enable sound               %s", (tmp_opts.EmuOpt &0x004)?"ON":"OFF"); // 5\r
768         gp2x_text_out8(tl_x, (y+=10), "Sound Quality:     %5iHz %s", tmp_opts.PsndRate, monostereo);\r
769         gp2x_text_out8(tl_x, (y+=10), "Use ARM940 core for sound  %s", (tmp_opts.PicoOpt&0x200)?"ON":"OFF"); // 7\r
770         gp2x_text_out8(tl_x, (y+=10), "6 button pad               %s", (tmp_opts.PicoOpt&0x020)?"ON":"OFF"); // 8\r
771         gp2x_text_out8(tl_x, (y+=10), "Genesis Region:            %s", region_name(tmp_opts.PicoRegion));\r
772         gp2x_text_out8(tl_x, (y+=10), "Use SRAM/BRAM savestates   %s", (tmp_opts.EmuOpt &0x001)?"ON":"OFF"); // 10\r
773         gp2x_text_out8(tl_x, (y+=10), "Confirm save overwrites    %s", (tmp_opts.EmuOpt &0x200)?"ON":"OFF"); // 11\r
774         gp2x_text_out8(tl_x, (y+=10), "Save slot                  %i", state_slot); // 12\r
775         gp2x_text_out8(tl_x, (y+=10), "GP2X CPU clocks            %iMhz", tmp_opts.CPUclock);\r
776         gp2x_text_out8(tl_x, (y+=10), "[Sega/Mega CD options]");\r
777         gp2x_text_out8(tl_x, (y+=10), "[advanced options]");            // 15\r
778         gp2x_text_out8(tl_x, (y+=10), "Save cfg as default");\r
779         if (rom_data)\r
780                 gp2x_text_out8(tl_x, (y+=10), "Save cfg for current game only");\r
781 \r
782         // draw cursor\r
783         gp2x_text_out8(tl_x - 16, tl_y + menu_sel*10, ">");\r
784 \r
785         gp2x_video_flip();\r
786 }\r
787 \r
788 static int sndrate_prevnext(int rate, int dir)\r
789 {\r
790         int i, rates[] = { 8000, 11025, 16000, 22050, 44100 };\r
791 \r
792         for (i = 0; i < 5; i++)\r
793                 if (rates[i] == rate) break;\r
794 \r
795         i += dir ? 1 : -1;\r
796         if (i > 4) return dir ? 44100 : 22050;\r
797         if (i < 0) return dir ? 11025 : 8000;\r
798         return rates[i];\r
799 }\r
800 \r
801 static void menu_options_save(void)\r
802 {\r
803         memcpy(&currentConfig.EmuOpt, &tmp_opts.EmuOpt, sizeof(tmp_opts));\r
804         currentConfig.gamma = tmp_gamma;\r
805         PicoOpt = currentConfig.PicoOpt;\r
806         PsndRate = currentConfig.PsndRate;\r
807         PicoRegionOverride = currentConfig.PicoRegion;\r
808         if (PicoOpt & 0x20) {\r
809                 actionNames[ 8] = "Z"; actionNames[ 9] = "Y";\r
810                 actionNames[10] = "X"; actionNames[11] = "MODE";\r
811         } else {\r
812                 actionNames[8] = actionNames[9] = actionNames[10] = actionNames[11] = 0;\r
813         }\r
814 }\r
815 \r
816 static int menu_loop_options(void)\r
817 {\r
818         int menu_sel = 0, menu_sel_max = 16;\r
819         unsigned long inp = 0;\r
820 \r
821         if (rom_data) menu_sel_max++;\r
822         memcpy(&tmp_opts.EmuOpt, &currentConfig.EmuOpt, sizeof(tmp_opts));\r
823         tmp_gamma = currentConfig.gamma;\r
824         tmp_opts.PicoOpt = PicoOpt;\r
825         tmp_opts.PsndRate = PsndRate;\r
826         tmp_opts.PicoRegion = PicoRegionOverride;\r
827 \r
828         for(;;)\r
829         {\r
830                 draw_menu_options(menu_sel);\r
831                 inp = wait_for_input(GP2X_UP|GP2X_DOWN|GP2X_LEFT|GP2X_RIGHT|GP2X_B|GP2X_X|GP2X_A);\r
832                 if(inp & GP2X_UP  ) { menu_sel--; if (menu_sel < 0) menu_sel = menu_sel_max; }\r
833                 if(inp & GP2X_DOWN) { menu_sel++; if (menu_sel > menu_sel_max) menu_sel = 0; }\r
834                 if((inp& GP2X_B)||(inp&GP2X_LEFT)||(inp&GP2X_RIGHT)) { // toggleable options\r
835                         switch (menu_sel) {\r
836                                 case  1: tmp_opts.PicoOpt^=0x040; break;\r
837                                 case  2: tmp_opts.PicoOpt^=0x080; break;\r
838                                 case  3: tmp_opts.EmuOpt ^=0x002; break;\r
839                                 case  5: tmp_opts.EmuOpt ^=0x004; break;\r
840                                 case  7: tmp_opts.PicoOpt^=0x200; break;\r
841                                 case  8: tmp_opts.PicoOpt^=0x020; break;\r
842                                 case 10: tmp_opts.EmuOpt ^=0x001; break;\r
843                                 case 11: tmp_opts.EmuOpt ^=0x200; break;\r
844                                 case 14: cd_menu_loop_options();\r
845                                         if (engineState == PGS_ReloadRom)\r
846                                                 return 0; // test BIOS\r
847                                         break;\r
848                                 case 15: amenu_loop_options();    break;\r
849                                 case 16: // done (update and write)\r
850                                         menu_options_save();\r
851                                         if (emu_WriteConfig(0)) strcpy(menuErrorMsg, "config saved");\r
852                                         else strcpy(menuErrorMsg, "failed to write config");\r
853                                         return 1;\r
854                                 case 17: // done (update and write for current game)\r
855                                         menu_options_save();\r
856                                         if (emu_WriteConfig(1)) strcpy(menuErrorMsg, "config saved");\r
857                                         else strcpy(menuErrorMsg, "failed to write config");\r
858                                         return 1;\r
859                         }\r
860                 }\r
861                 if(inp & GP2X_X) return 0;  // done (no update or write)\r
862                 if(inp & GP2X_A) {\r
863                         menu_options_save();\r
864                         return 0;  // done (update, no write)\r
865                 }\r
866                 if(inp & (GP2X_LEFT|GP2X_RIGHT)) { // multi choise\r
867                         switch (menu_sel) {\r
868                                 case  0:\r
869                                         if (inp & GP2X_LEFT) {\r
870                                                 if      (  tmp_opts.PicoOpt&0x10) tmp_opts.PicoOpt&= ~0x10;\r
871                                                 else if (!(tmp_opts.EmuOpt &0x80))tmp_opts.EmuOpt |=  0x80;\r
872                                                 else if (  tmp_opts.EmuOpt &0x80) break;\r
873                                         } else {\r
874                                                 if      (  tmp_opts.PicoOpt&0x10) break;\r
875                                                 else if (!(tmp_opts.EmuOpt &0x80))tmp_opts.PicoOpt|=  0x10;\r
876                                                 else if (  tmp_opts.EmuOpt &0x80) tmp_opts.EmuOpt &= ~0x80;\r
877                                         }\r
878                                         break;\r
879                                 case  4:\r
880                                         tmp_opts.Frameskip += (inp & GP2X_LEFT) ? -1 : 1;\r
881                                         if (tmp_opts.Frameskip < 0)  tmp_opts.Frameskip = -1;\r
882                                         if (tmp_opts.Frameskip > 32) tmp_opts.Frameskip = 32;\r
883                                         break;\r
884                                 case  6:\r
885                                         if ((inp & GP2X_RIGHT) && tmp_opts.PsndRate == 44100 && !(tmp_opts.PicoOpt&0x08)) {\r
886                                                 tmp_opts.PsndRate = 8000;  tmp_opts.PicoOpt|= 0x08;\r
887                                         } else if ((inp & GP2X_LEFT) && tmp_opts.PsndRate == 8000 && (tmp_opts.PicoOpt&0x08)) {\r
888                                                 tmp_opts.PsndRate = 44100; tmp_opts.PicoOpt&=~0x08;\r
889                                         } else tmp_opts.PsndRate = sndrate_prevnext(tmp_opts.PsndRate, inp & GP2X_RIGHT);\r
890                                         break;\r
891                                 case  9:\r
892                                         if (inp & GP2X_RIGHT) {\r
893                                                 if (tmp_opts.PicoRegion) tmp_opts.PicoRegion<<=1; else tmp_opts.PicoRegion=1;\r
894                                                 if (tmp_opts.PicoRegion > 8) tmp_opts.PicoRegion = 8;\r
895                                         } else  tmp_opts.PicoRegion>>=1;\r
896                                         break;\r
897                                 case 12:\r
898                                         if (inp & GP2X_RIGHT) {\r
899                                                 state_slot++; if (state_slot > 9) state_slot = 0;\r
900                                         } else {state_slot--; if (state_slot < 0) state_slot = 9;\r
901                                         }\r
902                                         break;\r
903                                 case 13:\r
904                                         while ((inp = gp2x_joystick_read(1)) & (GP2X_LEFT|GP2X_RIGHT)) {\r
905                                                 tmp_opts.CPUclock += (inp & GP2X_LEFT) ? -1 : 1;\r
906                                                 if (tmp_opts.CPUclock < 1) tmp_opts.CPUclock = 1;\r
907                                                 draw_menu_options(menu_sel);\r
908                                                 usleep(50*1000);\r
909                                         }\r
910                                         break;\r
911                         }\r
912                 }\r
913         }\r
914 }\r
915 \r
916 // -------------- credits --------------\r
917 \r
918 static void draw_menu_credits(void)\r
919 {\r
920         int tl_x = 15, tl_y = 70, y;\r
921         memset(gp2x_screen, 0, 320*240);\r
922 \r
923         gp2x_text_out8(tl_x, 20, "PicoDrive v" VERSION " (c) notaz, 2006,2007");\r
924         y = tl_y;\r
925         gp2x_text_out8(tl_x, y, "Credits:");\r
926         gp2x_text_out8(tl_x, (y+=10), "Dave: Cyclone 68000 core,");\r
927         gp2x_text_out8(tl_x, (y+=10), "      base code of PicoDrive");\r
928         gp2x_text_out8(tl_x, (y+=10), "Reesy & FluBBa: DrZ80 core");\r
929         gp2x_text_out8(tl_x, (y+=10), "MAME devs: YM2612 and SN76496 cores");\r
930         gp2x_text_out8(tl_x, (y+=10), "Charles MacDonald: Genesis hw docs");\r
931         gp2x_text_out8(tl_x, (y+=10), "Stephane Dallongeville:");\r
932         gp2x_text_out8(tl_x, (y+=10), "      opensource Gens");\r
933         gp2x_text_out8(tl_x, (y+=10), "Haze: Genesis hw info");\r
934         gp2x_text_out8(tl_x, (y+=10), "rlyeh and others: minimal SDK");\r
935         gp2x_text_out8(tl_x, (y+=10), "Squidge: squidgehack");\r
936         gp2x_text_out8(tl_x, (y+=10), "Dzz: ARM940 sample");\r
937         gp2x_text_out8(tl_x, (y+=10), "GnoStiC / Puck2099: USB joystick");\r
938         gp2x_text_out8(tl_x, (y+=10), "craigix: GP2X hardware");\r
939 \r
940         gp2x_video_flip();\r
941 }\r
942 \r
943 \r
944 // -------------- root menu --------------\r
945 \r
946 static void draw_menu_root(int menu_sel)\r
947 {\r
948         int tl_x = 70, tl_y = 70, y;\r
949         memset(gp2x_screen, 0, 320*240);\r
950 \r
951         gp2x_text_out8(tl_x, 20, "PicoDrive v" VERSION);\r
952 \r
953         y = tl_y;\r
954         if (rom_data) {\r
955                 gp2x_text_out8(tl_x, y,       "Resume game");\r
956                 gp2x_text_out8(tl_x, (y+=10), "Save State");\r
957                 gp2x_text_out8(tl_x, (y+=10), "Load State");\r
958                 gp2x_text_out8(tl_x, (y+=10), "Reset game");\r
959         } else {\r
960                 y += 30;\r
961         }\r
962         gp2x_text_out8(tl_x, (y+=10), "Load new ROM/ISO");\r
963         gp2x_text_out8(tl_x, (y+=10), "Change options");\r
964         gp2x_text_out8(tl_x, (y+=10), "Configure controls");\r
965         gp2x_text_out8(tl_x, (y+=10), "Credits");\r
966         gp2x_text_out8(tl_x, (y+=10), "Exit");\r
967 \r
968         // draw cursor\r
969         gp2x_text_out8(tl_x - 16, tl_y + menu_sel*10, ">");\r
970         // error\r
971         if (menuErrorMsg[0]) gp2x_text_out8(5, 226, menuErrorMsg);\r
972         gp2x_video_flip();\r
973 }\r
974 \r
975 \r
976 static void menu_loop_root(void)\r
977 {\r
978         int ret, menu_sel = 4, menu_sel_max = 8, menu_sel_min = 4;\r
979         unsigned long inp = 0;\r
980         char curr_path[PATH_MAX], *selfname;\r
981         FILE *tstf;\r
982 \r
983         if ( (tstf = fopen(currentConfig.lastRomFile, "rb")) )\r
984         {\r
985                 fclose(tstf);\r
986                 strcpy(curr_path, currentConfig.lastRomFile);\r
987         }\r
988         else\r
989         {\r
990                 getcwd(curr_path, PATH_MAX);\r
991         }\r
992 \r
993         if (rom_data) menu_sel = menu_sel_min = 0;\r
994 \r
995         for(;;)\r
996         {\r
997                 draw_menu_root(menu_sel);\r
998                 inp = wait_for_input(GP2X_UP|GP2X_DOWN|GP2X_B|GP2X_X|GP2X_SELECT);\r
999                 if(inp & GP2X_UP  )  { menu_sel--; if (menu_sel < menu_sel_min) menu_sel = menu_sel_max; }\r
1000                 if(inp & GP2X_DOWN)  { menu_sel++; if (menu_sel > menu_sel_max) menu_sel = menu_sel_min; }\r
1001                 if(inp &(GP2X_SELECT|GP2X_X)){\r
1002                         if (rom_data) {\r
1003                                 while (gp2x_joystick_read(1) & (GP2X_SELECT|GP2X_X)) usleep(50*1000); // wait until select is released\r
1004                                 engineState = PGS_Running;\r
1005                                 break;\r
1006                         }\r
1007                 }\r
1008                 if(inp & GP2X_B   )  {\r
1009                         switch (menu_sel) {\r
1010                                 case 0: // resume game\r
1011                                         if (rom_data) { engineState = PGS_Running; return; }\r
1012                                         break;\r
1013                                 case 1: // save state\r
1014                                         if (rom_data) {\r
1015                                                 if(emu_SaveLoadGame(0, 0)) {\r
1016                                                         strcpy(menuErrorMsg, "save failed");\r
1017                                                         continue;\r
1018                                                 }\r
1019                                                 engineState = PGS_Running;\r
1020                                                 return;\r
1021                                         }\r
1022                                         break;\r
1023                                 case 2: // load state\r
1024                                         if (rom_data) {\r
1025                                                 if(emu_SaveLoadGame(1, 0)) {\r
1026                                                         strcpy(menuErrorMsg, "load failed");\r
1027                                                         continue;\r
1028                                                 }\r
1029                                                 engineState = PGS_Running;\r
1030                                                 return;\r
1031                                         }\r
1032                                         break;\r
1033                                 case 3: // reset game\r
1034                                         if (rom_data) {\r
1035                                                 emu_ResetGame();\r
1036                                                 engineState = PGS_Running;\r
1037                                                 return;\r
1038                                         }\r
1039                                         break;\r
1040                                 case 4: // select rom\r
1041                                         selfname = romsel_loop(curr_path);\r
1042                                         if (selfname) {\r
1043                                                 printf("selected file: %s\n", selfname);\r
1044                                                 engineState = PGS_ReloadRom;\r
1045                                         }\r
1046                                         return;\r
1047                                 case 5: // options\r
1048                                         ret = menu_loop_options();\r
1049                                         if (ret == 1) continue; // status update\r
1050                                         if (engineState == PGS_ReloadRom)\r
1051                                                 return; // BIOS test\r
1052                                         break;\r
1053                                 case 6: // controls\r
1054                                         kc_sel_loop();\r
1055                                         break;\r
1056                                 case 7: // credits\r
1057                                         draw_menu_credits();\r
1058                                         usleep(500*1000);\r
1059                                         inp = wait_for_input(GP2X_B|GP2X_X);\r
1060                                         break;\r
1061                                 case 8: // exit\r
1062                                         engineState = PGS_Quit;\r
1063                                         return;\r
1064                         }\r
1065                 }\r
1066                 menuErrorMsg[0] = 0; // clear error msg\r
1067         }\r
1068 }\r
1069 \r
1070 \r
1071 void menu_loop(void)\r
1072 {\r
1073         int pal[2];\r
1074 \r
1075         // switch to 8bpp\r
1076         gp2x_video_changemode(8);\r
1077         gp2x_video_RGB_setscaling(320, 240);\r
1078         // set pal\r
1079         pal[0] = 0;\r
1080         pal[1] = 0x00ffffff;\r
1081         gp2x_video_setpalette(pal, 2);\r
1082 \r
1083         menu_loop_root();\r
1084 \r
1085         menuErrorMsg[0] = 0;\r
1086 }\r