PCM sound, refactored code940
[picodrive.git] / platform / 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 // --------- sega/mega cd options ----------\r
566 \r
567 static void draw_cd_menu_options(int menu_sel, char *b_us, char *b_eu, char *b_jp)\r
568 {\r
569         int tl_x = 25, tl_y = 60, y;\r
570 \r
571         y = tl_y;\r
572         memset(gp2x_screen, 0, 320*240);\r
573         gp2x_text_out8(tl_x, y,       "USA BIOS:     %s", b_us); // 0\r
574         gp2x_text_out8(tl_x, (y+=10), "EUR BIOS:     %s", b_eu); // 1\r
575         gp2x_text_out8(tl_x, (y+=10), "JAP BIOS:     %s", b_jp); // 2\r
576         gp2x_text_out8(tl_x, (y+=10), "CD LEDs                    %s", (currentConfig.EmuOpt &0x400)?"ON":"OFF"); // 3\r
577         gp2x_text_out8(tl_x, (y+=10), "CDDA audio (using mp3s)    %s", (currentConfig.PicoOpt&0x800)?"ON":"OFF"); // 4\r
578         gp2x_text_out8(tl_x, (y+=10), "PCM audio                  %s", (currentConfig.PicoOpt&0x400)?"ON":"OFF"); // 5\r
579         gp2x_text_out8(tl_x, (y+=10), "Done");\r
580 \r
581         // draw cursor\r
582         gp2x_text_out8(tl_x - 16, tl_y + menu_sel*10, ">");\r
583 \r
584         if ((menu_sel == 0 && strcmp(b_us, "NOT FOUND")) ||\r
585                 (menu_sel == 1 && strcmp(b_eu, "NOT FOUND")) ||\r
586                 (menu_sel == 2 && strcmp(b_jp, "NOT FOUND")))\r
587                         gp2x_text_out8(tl_x, 220, "Press start to test selected BIOS");\r
588 \r
589         gp2x_video_flip();\r
590 }\r
591 \r
592 static void cd_menu_loop_options(void)\r
593 {\r
594         int menu_sel = 0, menu_sel_max = 6;\r
595         unsigned long inp = 0;\r
596         char bios_us[32], bios_eu[32], bios_jp[32], *bios, *p;\r
597 \r
598         if (find_bios(4, &bios)) { // US\r
599                 for (p = bios+strlen(bios)-1; p > bios && *p != '/'; p--); p++;\r
600                 strncpy(bios_us, p, 31); bios_us[31] = 0;\r
601         } else  strcpy(bios_us, "NOT FOUND");\r
602 \r
603         if (find_bios(8, &bios)) { // EU\r
604                 for (p = bios+strlen(bios)-1; p > bios && *p != '/'; p--); p++;\r
605                 strncpy(bios_eu, p, 31); bios_eu[31] = 0;\r
606         } else  strcpy(bios_eu, "NOT FOUND");\r
607 \r
608         if (find_bios(1, &bios)) { // JP\r
609                 for (p = bios+strlen(bios)-1; p > bios && *p != '/'; p--); p++;\r
610                 strncpy(bios_jp, p, 31); bios_jp[31] = 0;\r
611         } else  strcpy(bios_jp, "NOT FOUND");\r
612 \r
613         for(;;)\r
614         {\r
615                 draw_cd_menu_options(menu_sel, bios_us, bios_eu, bios_jp);\r
616                 inp = wait_for_input(GP2X_UP|GP2X_DOWN|GP2X_LEFT|GP2X_RIGHT|GP2X_B|GP2X_X|GP2X_A|GP2X_START);\r
617                 if(inp & GP2X_UP  ) { menu_sel--; if (menu_sel < 0) menu_sel = menu_sel_max; }\r
618                 if(inp & GP2X_DOWN) { menu_sel++; if (menu_sel > menu_sel_max) menu_sel = 0; }\r
619                 if((inp& GP2X_B)||(inp&GP2X_LEFT)||(inp&GP2X_RIGHT)) { // toggleable options\r
620                         switch (menu_sel) {\r
621                                 case  3: currentConfig.EmuOpt ^=0x400; break;\r
622                                 case  4: currentConfig.PicoOpt^=0x800; break;\r
623                                 case  5: currentConfig.PicoOpt^=0x400; break;\r
624                                 case  6: return;\r
625                         }\r
626                 }\r
627                 if(inp & (GP2X_X|GP2X_A)) return;\r
628                 if(inp &  GP2X_START) { // BIOS testers\r
629                         switch (menu_sel) {\r
630                                 case 0: if (find_bios(4, &bios)) { // test US\r
631                                                 strcpy(romFileName, bios);\r
632                                                 engineState = PGS_ReloadRom;\r
633                                                 return;\r
634                                         }\r
635                                         break;\r
636                                 case 1: if (find_bios(8, &bios)) { // test EU\r
637                                                 strcpy(romFileName, bios);\r
638                                                 engineState = PGS_ReloadRom;\r
639                                                 return;\r
640                                         }\r
641                                         break;\r
642                                 case 2: if (find_bios(1, &bios)) { // test JP\r
643                                                 strcpy(romFileName, bios);\r
644                                                 engineState = PGS_ReloadRom;\r
645                                                 return;\r
646                                         }\r
647                                         break;\r
648                         }\r
649                 }\r
650         }\r
651 }\r
652 \r
653 \r
654 // --------- advanced options ----------\r
655 \r
656 static void draw_amenu_options(int menu_sel)\r
657 {\r
658         int tl_x = 25, tl_y = 60, y;\r
659         char *mms = mmuhack_status ? "active)  " : "inactive)";\r
660 \r
661         y = tl_y;\r
662         memset(gp2x_screen, 0, 320*240);\r
663         gp2x_text_out8(tl_x, y,       "Scale 32 column mode       %s", (currentConfig.PicoOpt&0x100)?"ON":"OFF"); // 0\r
664         gp2x_text_out8(tl_x, (y+=10), "Gamma correction           %i.%02i", currentConfig.gamma / 100, currentConfig.gamma%100); // 1\r
665         gp2x_text_out8(tl_x, (y+=10), "Emulate Z80                %s", (currentConfig.PicoOpt&0x004)?"ON":"OFF"); // 2\r
666         gp2x_text_out8(tl_x, (y+=10), "Emulate YM2612 (FM)        %s", (currentConfig.PicoOpt&0x001)?"ON":"OFF"); // 3\r
667         gp2x_text_out8(tl_x, (y+=10), "Emulate SN76496 (PSG)      %s", (currentConfig.PicoOpt&0x002)?"ON":"OFF"); // 4\r
668         gp2x_text_out8(tl_x, (y+=10), "gzip savestates            %s", (currentConfig.EmuOpt &0x008)?"ON":"OFF"); // 5\r
669         gp2x_text_out8(tl_x, (y+=10), "Don't save config on exit  %s", (currentConfig.EmuOpt &0x020)?"ON":"OFF"); // 6\r
670         gp2x_text_out8(tl_x, (y+=10), "needs restart:");\r
671         gp2x_text_out8(tl_x, (y+=10), "craigix's RAM timings      %s", (currentConfig.EmuOpt &0x100)?"ON":"OFF"); // 8\r
672         gp2x_text_out8(tl_x, (y+=10), "squidgehack (now %s %s",   mms, (currentConfig.EmuOpt &0x010)?"ON":"OFF"); // 9\r
673         gp2x_text_out8(tl_x, (y+=10), "Done");\r
674 \r
675         // draw cursor\r
676         gp2x_text_out8(tl_x - 16, tl_y + menu_sel*10, ">");\r
677 \r
678         gp2x_video_flip();\r
679 }\r
680 \r
681 static void amenu_loop_options(void)\r
682 {\r
683         int menu_sel = 0, menu_sel_max = 10;\r
684         unsigned long inp = 0;\r
685 \r
686         for(;;)\r
687         {\r
688                 draw_amenu_options(menu_sel);\r
689                 inp = wait_for_input(GP2X_UP|GP2X_DOWN|GP2X_LEFT|GP2X_RIGHT|GP2X_B|GP2X_X|GP2X_A);\r
690                 if(inp & GP2X_UP  ) { menu_sel--; if (menu_sel < 0) menu_sel = menu_sel_max; }\r
691                 if(inp & GP2X_DOWN) { menu_sel++; if (menu_sel > menu_sel_max) menu_sel = 0; }\r
692                 if((inp& GP2X_B)||(inp&GP2X_LEFT)||(inp&GP2X_RIGHT)) { // toggleable options\r
693                         switch (menu_sel) {\r
694                                 case  0: currentConfig.PicoOpt^=0x100; break;\r
695                                 case  2: currentConfig.PicoOpt^=0x004; break;\r
696                                 case  3: currentConfig.PicoOpt^=0x001; break;\r
697                                 case  4: currentConfig.PicoOpt^=0x002; break;\r
698                                 case  5: currentConfig.EmuOpt ^=0x008; break;\r
699                                 case  6: currentConfig.EmuOpt ^=0x020; break;\r
700                                 case  8: currentConfig.EmuOpt ^=0x100; break;\r
701                                 case  9: currentConfig.EmuOpt ^=0x010; break;\r
702                                 case 10: return;\r
703                         }\r
704                 }\r
705                 if(inp & (GP2X_X|GP2X_A)) return;\r
706                 if(inp & (GP2X_LEFT|GP2X_RIGHT)) { // multi choise\r
707                         switch (menu_sel) {\r
708                                 case 1:\r
709                                         while ((inp = gp2x_joystick_read(1)) & (GP2X_LEFT|GP2X_RIGHT)) {\r
710                                                 currentConfig.gamma += (inp & GP2X_LEFT) ? -1 : 1;\r
711                                                 if (currentConfig.gamma <   1) currentConfig.gamma =   1;\r
712                                                 if (currentConfig.gamma > 300) currentConfig.gamma = 300;\r
713                                                 draw_amenu_options(menu_sel);\r
714                                                 usleep(18*1000);\r
715                                         }\r
716                                         break;\r
717                         }\r
718                 }\r
719         }\r
720 }\r
721 \r
722 // -------------- options --------------\r
723 \r
724 static const char *region_name(unsigned int code)\r
725 {\r
726         static const char *names[] = { "Auto", "      Japan NTSC", "      Japan PAL", "      USA", "      Europe" };\r
727         static const char *names_short[] = { "", " JP", " JP", " US", " EU" };\r
728         int u, i = 0;\r
729         if (code) {\r
730                 code <<= 1;\r
731                 while((code >>= 1)) i++;\r
732                 if (i > 4) return "unknown";\r
733                 return names[i];\r
734         } else {\r
735                 static char name[24];\r
736                 strcpy(name, "Auto:");\r
737                 for (u = 0; u < 3; u++) {\r
738                         i = 0; code = ((PicoAutoRgnOrder >> u*4) & 0xf) << 1;\r
739                         while((code >>= 1)) i++;\r
740                         strcat(name, names_short[i]);\r
741                 }\r
742                 return name;\r
743         }\r
744 }\r
745 \r
746 static void draw_menu_options(int menu_sel)\r
747 {\r
748         int tl_x = 25, tl_y = 40, y;\r
749         char monostereo[8], strframeskip[8], *strrend;\r
750 \r
751         strcpy(monostereo, (currentConfig.PicoOpt&0x08)?"stereo":"mono");\r
752         if (currentConfig.Frameskip < 0)\r
753                  strcpy(strframeskip, "Auto");\r
754         else sprintf(strframeskip, "%i", currentConfig.Frameskip);\r
755         if (currentConfig.PicoOpt&0x10) {\r
756                 strrend = " 8bit fast";\r
757         } else if (currentConfig.EmuOpt&0x80) {\r
758                 strrend = "16bit accurate";\r
759         } else {\r
760                 strrend = " 8bit accurate";\r
761         }\r
762 \r
763         y = tl_y;\r
764         memset(gp2x_screen, 0, 320*240);\r
765         gp2x_text_out8(tl_x, y,       "Renderer:            %s", strrend); // 0\r
766         gp2x_text_out8(tl_x, (y+=10), "Accurate timing (slower)   %s", (currentConfig.PicoOpt&0x040)?"ON":"OFF"); // 1\r
767         gp2x_text_out8(tl_x, (y+=10), "Accurate sprites (slower)  %s", (currentConfig.PicoOpt&0x080)?"ON":"OFF"); // 2\r
768         gp2x_text_out8(tl_x, (y+=10), "Show FPS                   %s", (currentConfig.EmuOpt &0x002)?"ON":"OFF"); // 3\r
769         gp2x_text_out8(tl_x, (y+=10), "Frameskip                  %s", strframeskip);\r
770         gp2x_text_out8(tl_x, (y+=10), "Enable sound               %s", (currentConfig.EmuOpt &0x004)?"ON":"OFF"); // 5\r
771         gp2x_text_out8(tl_x, (y+=10), "Sound Quality:     %5iHz %s",   currentConfig.PsndRate, monostereo);\r
772         gp2x_text_out8(tl_x, (y+=10), "Use ARM940 core for sound  %s", (currentConfig.PicoOpt&0x200)?"ON":"OFF"); // 7\r
773         gp2x_text_out8(tl_x, (y+=10), "6 button pad               %s", (currentConfig.PicoOpt&0x020)?"ON":"OFF"); // 8\r
774         gp2x_text_out8(tl_x, (y+=10), "Genesis Region:      %s",       region_name(currentConfig.PicoRegion));\r
775         gp2x_text_out8(tl_x, (y+=10), "Use SRAM/BRAM savestates   %s", (currentConfig.EmuOpt &0x001)?"ON":"OFF"); // 10\r
776         gp2x_text_out8(tl_x, (y+=10), "Confirm save overwrites    %s", (currentConfig.EmuOpt &0x200)?"ON":"OFF"); // 11\r
777         gp2x_text_out8(tl_x, (y+=10), "Save slot                  %i", state_slot); // 12\r
778         gp2x_text_out8(tl_x, (y+=10), "GP2X CPU clocks            %iMhz", currentConfig.CPUclock);\r
779         gp2x_text_out8(tl_x, (y+=10), "[Sega/Mega CD options]");\r
780         gp2x_text_out8(tl_x, (y+=10), "[advanced options]");            // 15\r
781         gp2x_text_out8(tl_x, (y+=10), "Save cfg as default");\r
782         if (rom_data)\r
783                 gp2x_text_out8(tl_x, (y+=10), "Save cfg for current game only");\r
784 \r
785         // draw cursor\r
786         gp2x_text_out8(tl_x - 16, tl_y + menu_sel*10, ">");\r
787 \r
788         gp2x_video_flip();\r
789 }\r
790 \r
791 static int sndrate_prevnext(int rate, int dir)\r
792 {\r
793         int i, rates[] = { 8000, 11025, 16000, 22050, 44100 };\r
794 \r
795         for (i = 0; i < 5; i++)\r
796                 if (rates[i] == rate) break;\r
797 \r
798         i += dir ? 1 : -1;\r
799         if (i > 4) return dir ? 44100 : 22050;\r
800         if (i < 0) return dir ? 11025 : 8000;\r
801         return rates[i];\r
802 }\r
803 \r
804 static void region_prevnext(int right)\r
805 {\r
806         // jp_ntsc=1, jp_pal=2, usa=4, eu=8\r
807         static int rgn_orders[] = { 0x148, 0x184, 0x814, 0x418, 0x841, 0x481 };\r
808         int i;\r
809         if (right) {\r
810                 if (!currentConfig.PicoRegion) {\r
811                         for (i = 0; i < 6; i++)\r
812                                 if (rgn_orders[i] == PicoAutoRgnOrder) break;\r
813                         if (i < 5) PicoAutoRgnOrder = rgn_orders[i+1];\r
814                         else currentConfig.PicoRegion=1;\r
815                 }\r
816                 else currentConfig.PicoRegion<<=1;\r
817                 if (currentConfig.PicoRegion > 8) currentConfig.PicoRegion = 8;\r
818         } else {\r
819                 if (!currentConfig.PicoRegion) {\r
820                         for (i = 0; i < 6; i++)\r
821                                 if (rgn_orders[i] == PicoAutoRgnOrder) break;\r
822                         if (i > 0) PicoAutoRgnOrder = rgn_orders[i-1];\r
823                 }\r
824                 else currentConfig.PicoRegion>>=1;\r
825         }\r
826 }\r
827 \r
828 static void menu_options_save(void)\r
829 {\r
830         PicoOpt = currentConfig.PicoOpt;\r
831         PsndRate = currentConfig.PsndRate;\r
832         PicoRegionOverride = currentConfig.PicoRegion;\r
833         if (PicoOpt & 0x20) {\r
834                 actionNames[ 8] = "Z"; actionNames[ 9] = "Y";\r
835                 actionNames[10] = "X"; actionNames[11] = "MODE";\r
836         } else {\r
837                 actionNames[8] = actionNames[9] = actionNames[10] = actionNames[11] = 0;\r
838         }\r
839 }\r
840 \r
841 static int menu_loop_options(void)\r
842 {\r
843         int menu_sel = 0, menu_sel_max = 16;\r
844         unsigned long inp = 0;\r
845 \r
846         if (rom_data) menu_sel_max++;\r
847         currentConfig.PicoOpt = PicoOpt;\r
848         currentConfig.PsndRate = PsndRate;\r
849         currentConfig.PicoRegion = PicoRegionOverride;\r
850 \r
851         for(;;)\r
852         {\r
853                 draw_menu_options(menu_sel);\r
854                 inp = wait_for_input(GP2X_UP|GP2X_DOWN|GP2X_LEFT|GP2X_RIGHT|GP2X_B|GP2X_X|GP2X_A);\r
855                 if(inp & GP2X_UP  ) { menu_sel--; if (menu_sel < 0) menu_sel = menu_sel_max; }\r
856                 if(inp & GP2X_DOWN) { menu_sel++; if (menu_sel > menu_sel_max) menu_sel = 0; }\r
857                 if((inp& GP2X_B)||(inp&GP2X_LEFT)||(inp&GP2X_RIGHT)) { // toggleable options\r
858                         switch (menu_sel) {\r
859                                 case  1: currentConfig.PicoOpt^=0x040; break;\r
860                                 case  2: currentConfig.PicoOpt^=0x080; break;\r
861                                 case  3: currentConfig.EmuOpt ^=0x002; break;\r
862                                 case  5: currentConfig.EmuOpt ^=0x004; break;\r
863                                 case  7: currentConfig.PicoOpt^=0x200; break;\r
864                                 case  8: currentConfig.PicoOpt^=0x020; break;\r
865                                 case 10: currentConfig.EmuOpt ^=0x001; break;\r
866                                 case 11: currentConfig.EmuOpt ^=0x200; break;\r
867                                 case 14: cd_menu_loop_options();\r
868                                         if (engineState == PGS_ReloadRom)\r
869                                                 return 0; // test BIOS\r
870                                         break;\r
871                                 case 15: amenu_loop_options();    break;\r
872                                 case 16: // done (update and write)\r
873                                         menu_options_save();\r
874                                         if (emu_WriteConfig(0)) strcpy(menuErrorMsg, "config saved");\r
875                                         else strcpy(menuErrorMsg, "failed to write config");\r
876                                         return 1;\r
877                                 case 17: // done (update and write for current game)\r
878                                         menu_options_save();\r
879                                         if (emu_WriteConfig(1)) strcpy(menuErrorMsg, "config saved");\r
880                                         else strcpy(menuErrorMsg, "failed to write config");\r
881                                         return 1;\r
882                         }\r
883                 }\r
884                 if(inp & (GP2X_X|GP2X_A)) {\r
885                         menu_options_save();\r
886                         return 0;  // done (update, no write)\r
887                 }\r
888                 if(inp & (GP2X_LEFT|GP2X_RIGHT)) { // multi choise\r
889                         switch (menu_sel) {\r
890                                 case  0:\r
891                                         if (inp & GP2X_LEFT) {\r
892                                                 if      (  currentConfig.PicoOpt&0x10) currentConfig.PicoOpt&= ~0x10;\r
893                                                 else if (!(currentConfig.EmuOpt &0x80))currentConfig.EmuOpt |=  0x80;\r
894                                                 else if (  currentConfig.EmuOpt &0x80) break;\r
895                                         } else {\r
896                                                 if      (  currentConfig.PicoOpt&0x10) break;\r
897                                                 else if (!(currentConfig.EmuOpt &0x80))currentConfig.PicoOpt|=  0x10;\r
898                                                 else if (  currentConfig.EmuOpt &0x80) currentConfig.EmuOpt &= ~0x80;\r
899                                         }\r
900                                         break;\r
901                                 case  4:\r
902                                         currentConfig.Frameskip += (inp & GP2X_LEFT) ? -1 : 1;\r
903                                         if (currentConfig.Frameskip < 0)  currentConfig.Frameskip = -1;\r
904                                         if (currentConfig.Frameskip > 32) currentConfig.Frameskip = 32;\r
905                                         break;\r
906                                 case  6:\r
907                                         if ((inp & GP2X_RIGHT) && currentConfig.PsndRate == 44100 && !(currentConfig.PicoOpt&0x08)) {\r
908                                                 currentConfig.PsndRate = 8000;  currentConfig.PicoOpt|= 0x08;\r
909                                         } else if ((inp & GP2X_LEFT) && currentConfig.PsndRate == 8000 && (currentConfig.PicoOpt&0x08)) {\r
910                                                 currentConfig.PsndRate = 44100; currentConfig.PicoOpt&=~0x08;\r
911                                         } else currentConfig.PsndRate = sndrate_prevnext(currentConfig.PsndRate, inp & GP2X_RIGHT);\r
912                                         break;\r
913                                 case  9:\r
914                                         region_prevnext(inp & GP2X_RIGHT);\r
915                                         break;\r
916                                 case 12:\r
917                                         if (inp & GP2X_RIGHT) {\r
918                                                 state_slot++; if (state_slot > 9) state_slot = 0;\r
919                                         } else {state_slot--; if (state_slot < 0) state_slot = 9;\r
920                                         }\r
921                                         break;\r
922                                 case 13:\r
923                                         while ((inp = gp2x_joystick_read(1)) & (GP2X_LEFT|GP2X_RIGHT)) {\r
924                                                 currentConfig.CPUclock += (inp & GP2X_LEFT) ? -1 : 1;\r
925                                                 if (currentConfig.CPUclock < 1) currentConfig.CPUclock = 1;\r
926                                                 draw_menu_options(menu_sel);\r
927                                                 usleep(50*1000);\r
928                                         }\r
929                                         break;\r
930                         }\r
931                 }\r
932         }\r
933 }\r
934 \r
935 // -------------- credits --------------\r
936 \r
937 static void draw_menu_credits(void)\r
938 {\r
939         int tl_x = 15, tl_y = 70, y;\r
940         memset(gp2x_screen, 0, 320*240);\r
941 \r
942         gp2x_text_out8(tl_x, 20, "PicoDrive v" VERSION " (c) notaz, 2006,2007");\r
943         y = tl_y;\r
944         gp2x_text_out8(tl_x, y, "Credits:");\r
945         gp2x_text_out8(tl_x, (y+=10), "Dave: Cyclone 68000 core,");\r
946         gp2x_text_out8(tl_x, (y+=10), "      base code of PicoDrive");\r
947         gp2x_text_out8(tl_x, (y+=10), "Reesy & FluBBa: DrZ80 core");\r
948         gp2x_text_out8(tl_x, (y+=10), "MAME devs: YM2612 and SN76496 cores");\r
949         gp2x_text_out8(tl_x, (y+=10), "Charles MacDonald: Genesis hw docs");\r
950         gp2x_text_out8(tl_x, (y+=10), "Stephane Dallongeville:");\r
951         gp2x_text_out8(tl_x, (y+=10), "      opensource Gens");\r
952         gp2x_text_out8(tl_x, (y+=10), "Haze: Genesis hw info");\r
953         gp2x_text_out8(tl_x, (y+=10), "rlyeh and others: minimal SDK");\r
954         gp2x_text_out8(tl_x, (y+=10), "Squidge: squidgehack");\r
955         gp2x_text_out8(tl_x, (y+=10), "Dzz: ARM940 sample");\r
956         gp2x_text_out8(tl_x, (y+=10), "GnoStiC / Puck2099: USB joystick");\r
957         gp2x_text_out8(tl_x, (y+=10), "craigix: GP2X hardware");\r
958 \r
959         gp2x_video_flip();\r
960 }\r
961 \r
962 \r
963 // -------------- root menu --------------\r
964 \r
965 static void draw_menu_root(int menu_sel)\r
966 {\r
967         int tl_x = 70, tl_y = 70, y;\r
968         memset(gp2x_screen, 0, 320*240);\r
969 \r
970         gp2x_text_out8(tl_x, 20, "PicoDrive v" VERSION);\r
971 \r
972         y = tl_y;\r
973         if (rom_data) {\r
974                 gp2x_text_out8(tl_x, y,       "Resume game");\r
975                 gp2x_text_out8(tl_x, (y+=10), "Save State");\r
976                 gp2x_text_out8(tl_x, (y+=10), "Load State");\r
977                 gp2x_text_out8(tl_x, (y+=10), "Reset game");\r
978         } else {\r
979                 y += 30;\r
980         }\r
981         gp2x_text_out8(tl_x, (y+=10), "Load new ROM/ISO");\r
982         gp2x_text_out8(tl_x, (y+=10), "Change options");\r
983         gp2x_text_out8(tl_x, (y+=10), "Configure controls");\r
984         gp2x_text_out8(tl_x, (y+=10), "Credits");\r
985         gp2x_text_out8(tl_x, (y+=10), "Exit");\r
986 \r
987         // draw cursor\r
988         gp2x_text_out8(tl_x - 16, tl_y + menu_sel*10, ">");\r
989         // error\r
990         if (menuErrorMsg[0]) gp2x_text_out8(5, 226, menuErrorMsg);\r
991         gp2x_video_flip();\r
992 }\r
993 \r
994 \r
995 static void menu_loop_root(void)\r
996 {\r
997         int ret, menu_sel = 4, menu_sel_max = 8, menu_sel_min = 4;\r
998         unsigned long inp = 0;\r
999         char curr_path[PATH_MAX], *selfname;\r
1000         FILE *tstf;\r
1001 \r
1002         if ( (tstf = fopen(currentConfig.lastRomFile, "rb")) )\r
1003         {\r
1004                 fclose(tstf);\r
1005                 strcpy(curr_path, currentConfig.lastRomFile);\r
1006         }\r
1007         else\r
1008         {\r
1009                 getcwd(curr_path, PATH_MAX);\r
1010         }\r
1011 \r
1012         if (rom_data) menu_sel = menu_sel_min = 0;\r
1013 \r
1014         for(;;)\r
1015         {\r
1016                 draw_menu_root(menu_sel);\r
1017                 inp = wait_for_input(GP2X_UP|GP2X_DOWN|GP2X_B|GP2X_X|GP2X_SELECT);\r
1018                 if(inp & GP2X_UP  )  { menu_sel--; if (menu_sel < menu_sel_min) menu_sel = menu_sel_max; }\r
1019                 if(inp & GP2X_DOWN)  { menu_sel++; if (menu_sel > menu_sel_max) menu_sel = menu_sel_min; }\r
1020                 if(inp &(GP2X_SELECT|GP2X_X)){\r
1021                         if (rom_data) {\r
1022                                 while (gp2x_joystick_read(1) & (GP2X_SELECT|GP2X_X)) usleep(50*1000); // wait until select is released\r
1023                                 engineState = PGS_Running;\r
1024                                 break;\r
1025                         }\r
1026                 }\r
1027                 if(inp & GP2X_B   )  {\r
1028                         switch (menu_sel) {\r
1029                                 case 0: // resume game\r
1030                                         if (rom_data) { engineState = PGS_Running; return; }\r
1031                                         break;\r
1032                                 case 1: // save state\r
1033                                         if (rom_data) {\r
1034                                                 if(emu_SaveLoadGame(0, 0)) {\r
1035                                                         strcpy(menuErrorMsg, "save failed");\r
1036                                                         continue;\r
1037                                                 }\r
1038                                                 engineState = PGS_Running;\r
1039                                                 return;\r
1040                                         }\r
1041                                         break;\r
1042                                 case 2: // load state\r
1043                                         if (rom_data) {\r
1044                                                 if(emu_SaveLoadGame(1, 0)) {\r
1045                                                         strcpy(menuErrorMsg, "load failed");\r
1046                                                         continue;\r
1047                                                 }\r
1048                                                 engineState = PGS_Running;\r
1049                                                 return;\r
1050                                         }\r
1051                                         break;\r
1052                                 case 3: // reset game\r
1053                                         if (rom_data) {\r
1054                                                 emu_ResetGame();\r
1055                                                 engineState = PGS_Running;\r
1056                                                 return;\r
1057                                         }\r
1058                                         break;\r
1059                                 case 4: // select rom\r
1060                                         selfname = romsel_loop(curr_path);\r
1061                                         if (selfname) {\r
1062                                                 printf("selected file: %s\n", selfname);\r
1063                                                 engineState = PGS_ReloadRom;\r
1064                                         }\r
1065                                         return;\r
1066                                 case 5: // options\r
1067                                         ret = menu_loop_options();\r
1068                                         if (ret == 1) continue; // status update\r
1069                                         if (engineState == PGS_ReloadRom)\r
1070                                                 return; // BIOS test\r
1071                                         break;\r
1072                                 case 6: // controls\r
1073                                         kc_sel_loop();\r
1074                                         break;\r
1075                                 case 7: // credits\r
1076                                         draw_menu_credits();\r
1077                                         usleep(500*1000);\r
1078                                         inp = wait_for_input(GP2X_B|GP2X_X);\r
1079                                         break;\r
1080                                 case 8: // exit\r
1081                                         engineState = PGS_Quit;\r
1082                                         return;\r
1083                         }\r
1084                 }\r
1085                 menuErrorMsg[0] = 0; // clear error msg\r
1086         }\r
1087 }\r
1088 \r
1089 \r
1090 void menu_loop(void)\r
1091 {\r
1092         int pal[2];\r
1093 \r
1094         // switch to 8bpp\r
1095         gp2x_video_changemode(8);\r
1096         gp2x_video_RGB_setscaling(320, 240);\r
1097         // set pal\r
1098         pal[0] = 0;\r
1099         pal[1] = 0x00ffffff;\r
1100         gp2x_video_setpalette(pal, 2);\r
1101 \r
1102         menu_loop_root();\r
1103 \r
1104         menuErrorMsg[0] = 0;\r
1105 }\r