menu bg, pc linux build
[libpicofe.git] / gp2x / menu.c
CommitLineData
720ee7f6 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
25extern char *actionNames[];\r
26extern char romFileName[PATH_MAX];\r
27extern char *rom_data;\r
28extern int mmuhack_status;\r
29extern int state_slot;\r
30\r
31static 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
38char menuErrorMsg[40] = {0, };\r
39\r
40\r
41static 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
109static 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
133void 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
158void 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
e5d315a5 167 gp2x_text(gp2x_screen,x,y,buffer,0xf0);\r
720ee7f6 168}\r
169\r
170\r
171void 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
176void 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
e5d315a5 185 gp2x_text(gp2x_screen,x,y,buffer,0xf0);\r
720ee7f6 186}\r
187\r
188\r
189static unsigned long inp_prev = 0;\r
190static int inp_prevjoy = 0;\r
191\r
192static 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
226static 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
248static 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
275static 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
e5d315a5 282 //memset(gp2x_screen, 0, 320*240);\r
283 gp2x_pd_clone_buffer2();\r
720ee7f6 284\r
285 if(start - 2 >= 0)\r
286 gp2x_text_out8_lim(14, (start - 2)*10, curdir, 38);\r
287 for (i = 0; i < n; i++) {\r
288 pos = start + i;\r
289 if (pos < 0) continue;\r
290 if (pos > 23) break;\r
291 if (namelist[i+1]->d_type == DT_DIR) {\r
292 gp2x_text_out8_lim(14, pos*10, "/", 1);\r
293 gp2x_text_out8_lim(14+8, pos*10, namelist[i+1]->d_name, 37);\r
294 } else {\r
295 gp2x_text_out8_lim(14, pos*10, namelist[i+1]->d_name, 38);\r
296 }\r
297 }\r
298 gp2x_text_out8(5, 120, ">");\r
e5d315a5 299 gp2x_video_flip2();\r
720ee7f6 300}\r
301\r
302static int scandir_cmp(const void *p1, const void *p2)\r
303{\r
304 struct dirent **d1 = (struct dirent **)p1, **d2 = (struct dirent **)p2;\r
305 if ((*d1)->d_type == (*d2)->d_type) return alphasort(d1, d2);\r
306 if ((*d1)->d_type == DT_DIR) return -1; // put before\r
307 if ((*d2)->d_type == DT_DIR) return 1;\r
308 return alphasort(d1, d2);\r
309}\r
310\r
311\r
312static char *romsel_loop(char *curr_path)\r
313{\r
314 struct dirent **namelist;\r
315 DIR *dir;\r
316 int n, sel = 0;\r
317 unsigned long inp = 0;\r
318 char *ret = NULL, *fname = NULL;\r
319\r
320 // is this a dir or a full path?\r
321 if ((dir = opendir(curr_path))) {\r
322 closedir(dir);\r
323 } else {\r
324 char *p;\r
325 for (p = curr_path + strlen(curr_path) - 1; p > curr_path && *p != '/'; p--);\r
326 *p = 0;\r
327 fname = p+1;\r
328 }\r
329\r
330 n = scandir(curr_path, &namelist, 0, scandir_cmp);\r
331 if (n < 0) {\r
332 // try root\r
333 n = scandir(curr_path, &namelist, 0, scandir_cmp);\r
334 if (n < 0) {\r
335 // oops, we failed\r
336 printf("dir: "); printf(curr_path); printf("\n");\r
337 perror("scandir");\r
338 return NULL;\r
339 }\r
340 }\r
341\r
342 // try to find sel\r
343 if (fname != NULL) {\r
344 int i;\r
345 for (i = 1; i < n; i++) {\r
346 if (strcmp(namelist[i]->d_name, fname) == 0) {\r
347 sel = i - 1;\r
348 break;\r
349 }\r
350 }\r
351 }\r
352\r
353 for (;;)\r
354 {\r
355 draw_dirlist(curr_path, namelist, n, sel);\r
5111820c 356 inp = wait_for_input(GP2X_UP|GP2X_DOWN|GP2X_LEFT|GP2X_RIGHT|GP2X_L|GP2X_R|GP2X_B|GP2X_X);\r
720ee7f6 357 if(inp & GP2X_UP ) { sel--; if (sel < 0) sel = n-2; }\r
358 if(inp & GP2X_DOWN) { sel++; if (sel > n-2) sel = 0; }\r
5111820c 359 if(inp &(GP2X_LEFT|GP2X_L)) { sel-=10; if (sel < 0) sel = 0; }\r
360 if(inp &(GP2X_RIGHT|GP2X_R)) { sel+=10; if (sel > n-2) sel = n-2; }\r
720ee7f6 361 if(inp & GP2X_B) { // enter dir/select\r
362 again:\r
363 if (namelist[sel+1]->d_type == DT_REG) {\r
364 strcpy(romFileName, curr_path);\r
365 strcat(romFileName, "/");\r
366 strcat(romFileName, namelist[sel+1]->d_name);\r
367 ret = romFileName;\r
368 break;\r
369 } else if (namelist[sel+1]->d_type == DT_DIR) {\r
370 int newlen = strlen(curr_path) + strlen(namelist[sel+1]->d_name) + 2;\r
371 char *p, *newdir = malloc(newlen);\r
372 if (strcmp(namelist[sel+1]->d_name, "..") == 0) {\r
373 char *start = curr_path;\r
374 p = start + strlen(start) - 1;\r
375 while (*p == '/' && p > start) p--;\r
376 while (*p != '/' && p > start) p--;\r
377 if (p <= start) strcpy(newdir, "/");\r
378 else { strncpy(newdir, start, p-start); newdir[p-start] = 0; }\r
379 } else {\r
380 strcpy(newdir, curr_path);\r
381 p = newdir + strlen(newdir) - 1;\r
382 while (*p == '/' && p >= newdir) *p-- = 0;\r
383 strcat(newdir, "/");\r
384 strcat(newdir, namelist[sel+1]->d_name);\r
385 }\r
386 ret = romsel_loop(newdir);\r
387 free(newdir);\r
388 break;\r
389 } else {\r
390 // unknown file type, happens on NTFS mounts. Try to guess.\r
391 FILE *tstf; int tmp;\r
392 strcpy(romFileName, curr_path);\r
393 strcat(romFileName, "/");\r
394 strcat(romFileName, namelist[sel+1]->d_name);\r
395 tstf = fopen(romFileName, "rb");\r
396 if (tstf != NULL)\r
397 {\r
398 if (fread(&tmp, 1, 1, tstf) > 0 || ferror(tstf) == 0)\r
399 namelist[sel+1]->d_type = DT_REG;\r
400 else namelist[sel+1]->d_type = DT_DIR;\r
401 fclose(tstf);\r
402 goto again;\r
403 }\r
404 }\r
405 }\r
406 if(inp & GP2X_X) break; // cancel\r
407 }\r
408\r
409 if (n > 0) {\r
410 while(n--) free(namelist[n]);\r
411 free(namelist);\r
412 }\r
413\r
414 return ret;\r
415}\r
416\r
417// -------------- key config --------------\r
418\r
419static char *usb_joy_key_name(int joy, int num)\r
420{\r
421 static char name[16];\r
422 switch (num)\r
423 {\r
424 case 0: sprintf(name, "Joy%i UP", joy); break;\r
425 case 1: sprintf(name, "Joy%i DOWN", joy); break;\r
426 case 2: sprintf(name, "Joy%i LEFT", joy); break;\r
427 case 3: sprintf(name, "Joy%i RIGHT", joy); break;\r
428 default:sprintf(name, "Joy%i b%i", joy, num-3); break;\r
429 }\r
430 return name;\r
431}\r
432\r
433static void draw_key_config(int curr_act, int is_p2)\r
434{\r
435 char strkeys[32*5];\r
436 int joy, i;\r
437\r
438 strkeys[0] = 0;\r
439 for (i = 0; i < 32; i++)\r
440 {\r
441 if (currentConfig.KeyBinds[i] & (1 << curr_act))\r
442 {\r
443 if (curr_act < 16 && (currentConfig.KeyBinds[i] & (1 << 16)) != (is_p2 << 16)) continue;\r
444 if (strkeys[0]) { strcat(strkeys, " + "); strcat(strkeys, gp2xKeyNames[i]); break; }\r
445 else strcpy(strkeys, gp2xKeyNames[i]);\r
446 }\r
447 }\r
448 for (joy = 0; joy < num_of_joys; joy++)\r
449 {\r
450 for (i = 0; i < 32; i++)\r
451 {\r
452 if (currentConfig.JoyBinds[joy][i] & (1 << curr_act))\r
453 {\r
454 if (curr_act < 16 && (currentConfig.JoyBinds[joy][i] & (1 << 16)) != (is_p2 << 16)) continue;\r
455 if (strkeys[0]) {\r
456 strcat(strkeys, ", "); strcat(strkeys, usb_joy_key_name(joy + 1, i));\r
457 break;\r
458 }\r
459 else strcpy(strkeys, usb_joy_key_name(joy + 1, i));\r
460 }\r
461 }\r
462 }\r
463\r
e5d315a5 464 //memset(gp2x_screen, 0, 320*240);\r
465 gp2x_pd_clone_buffer2();\r
720ee7f6 466 gp2x_text_out8(60, 40, "Action: %s", actionNames[curr_act]);\r
467 gp2x_text_out8(60, 60, "Keys: %s", strkeys);\r
468\r
469 gp2x_text_out8(30, 180, "Use SELECT to change action");\r
470 gp2x_text_out8(30, 190, "Press a key to bind/unbind");\r
471 gp2x_text_out8(30, 200, "Select \"Done\" action and");\r
472 gp2x_text_out8(30, 210, " press any key to finish");\r
e5d315a5 473 gp2x_video_flip2();\r
720ee7f6 474}\r
475\r
476static void key_config_loop(int is_p2)\r
477{\r
478 int curr_act = 0, joy = 0, i;\r
479 unsigned long inp = 0;\r
480\r
481 for (;;)\r
482 {\r
483 draw_key_config(curr_act, is_p2);\r
484 inp = wait_for_input_usbjoy(CONFIGURABLE_KEYS, &joy);\r
485 // printf("got %08lX from joy %i\n", inp, joy);\r
486 if (joy == 0) {\r
487 if (inp & GP2X_SELECT) {\r
488 curr_act++;\r
489 while (!actionNames[curr_act] && curr_act < 32) curr_act++;\r
490 if (curr_act > 31) curr_act = 0;\r
491 }\r
492 inp &= CONFIGURABLE_KEYS;\r
493 inp &= ~GP2X_SELECT;\r
494 }\r
495 if (curr_act == 31 && inp) break;\r
496 if (joy == 0) {\r
497 for (i = 0; i < 32; i++)\r
498 if (inp & (1 << i)) {\r
499 currentConfig.KeyBinds[i] ^= (1 << curr_act);\r
500 if (is_p2) currentConfig.KeyBinds[i] |= (1 << 16); // player 2 flag\r
501 else currentConfig.KeyBinds[i] &= ~(1 << 16);\r
502 }\r
503 } else {\r
504 for (i = 0; i < 32; i++)\r
505 if (inp & (1 << i)) {\r
506 currentConfig.JoyBinds[joy-1][i] ^= (1 << curr_act);\r
507 if (is_p2) currentConfig.JoyBinds[joy-1][i] |= (1 << 16);\r
508 else currentConfig.JoyBinds[joy-1][i] &= ~(1 << 16);\r
509 }\r
510 }\r
511 }\r
512}\r
513\r
514static void draw_kc_sel(int menu_sel)\r
515{\r
516 int tl_x = 25+40, tl_y = 60, y, i;\r
517 char joyname[36];\r
518\r
519 y = tl_y;\r
e5d315a5 520 //memset(gp2x_screen, 0, 320*240);\r
521 gp2x_pd_clone_buffer2();\r
720ee7f6 522 gp2x_text_out8(tl_x, y, "Player 1");\r
523 gp2x_text_out8(tl_x, (y+=10), "Player 2");\r
524 gp2x_text_out8(tl_x, (y+=10), "Done");\r
525\r
526 // draw cursor\r
527 gp2x_text_out8(tl_x - 16, tl_y + menu_sel*10, ">");\r
528\r
529 tl_x = 25;\r
530 gp2x_text_out8(tl_x, (y=110), "USB joys detected:");\r
531 if (num_of_joys > 0) {\r
532 for (i = 0; i < num_of_joys; i++) {\r
533 strncpy(joyname, joy_name(joys[i]), 33); joyname[33] = 0;\r
534 gp2x_text_out8(tl_x, (y+=10), "%i: %s", i+1, joyname);\r
535 }\r
536 } else {\r
537 gp2x_text_out8(tl_x, (y+=10), "none");\r
538 }\r
539\r
540\r
e5d315a5 541 gp2x_video_flip2();\r
720ee7f6 542}\r
543\r
544static void kc_sel_loop(void)\r
545{\r
546 int menu_sel = 2, menu_sel_max = 2;\r
547 unsigned long inp = 0;\r
548\r
549 for(;;)\r
550 {\r
551 draw_kc_sel(menu_sel);\r
552 inp = wait_for_input(GP2X_UP|GP2X_DOWN|GP2X_B|GP2X_X);\r
553 if(inp & GP2X_UP ) { menu_sel--; if (menu_sel < 0) menu_sel = menu_sel_max; }\r
554 if(inp & GP2X_DOWN) { menu_sel++; if (menu_sel > menu_sel_max) menu_sel = 0; }\r
555 if(inp & GP2X_B) {\r
556 switch (menu_sel) {\r
557 case 0: key_config_loop(0); return;\r
558 case 1: key_config_loop(1); return;\r
559 default: return;\r
560 }\r
561 }\r
562 if(inp & GP2X_X) return;\r
563 }\r
564}\r
565\r
566\r
567\r
daf91588 568// --------- sega/mega cd options ----------\r
569\r
570static void draw_cd_menu_options(int menu_sel, char *b_us, char *b_eu, char *b_jp)\r
571{\r
572 int tl_x = 25, tl_y = 60, y;\r
573\r
574 y = tl_y;\r
e5d315a5 575 //memset(gp2x_screen, 0, 320*240);\r
576 gp2x_pd_clone_buffer2();\r
577\r
daf91588 578 gp2x_text_out8(tl_x, y, "USA BIOS: %s", b_us); // 0\r
579 gp2x_text_out8(tl_x, (y+=10), "EUR BIOS: %s", b_eu); // 1\r
580 gp2x_text_out8(tl_x, (y+=10), "JAP BIOS: %s", b_jp); // 2\r
979ba09f 581 gp2x_text_out8(tl_x, (y+=10), "CD LEDs %s", (currentConfig.EmuOpt &0x400)?"ON":"OFF"); // 3\r
49fe50f0 582 gp2x_text_out8(tl_x, (y+=10), "CDDA audio (using mp3s) %s", (currentConfig.PicoOpt&0x800)?"ON":"OFF"); // 4\r
583 gp2x_text_out8(tl_x, (y+=10), "PCM audio %s", (currentConfig.PicoOpt&0x400)?"ON":"OFF"); // 5\r
daf91588 584 gp2x_text_out8(tl_x, (y+=10), "Done");\r
585\r
586 // draw cursor\r
587 gp2x_text_out8(tl_x - 16, tl_y + menu_sel*10, ">");\r
588\r
589 if ((menu_sel == 0 && strcmp(b_us, "NOT FOUND")) ||\r
590 (menu_sel == 1 && strcmp(b_eu, "NOT FOUND")) ||\r
591 (menu_sel == 2 && strcmp(b_jp, "NOT FOUND")))\r
592 gp2x_text_out8(tl_x, 220, "Press start to test selected BIOS");\r
593\r
e5d315a5 594 gp2x_video_flip2();\r
daf91588 595}\r
596\r
597static void cd_menu_loop_options(void)\r
598{\r
49fe50f0 599 int menu_sel = 0, menu_sel_max = 6;\r
daf91588 600 unsigned long inp = 0;\r
601 char bios_us[32], bios_eu[32], bios_jp[32], *bios, *p;\r
602\r
603 if (find_bios(4, &bios)) { // US\r
604 for (p = bios+strlen(bios)-1; p > bios && *p != '/'; p--); p++;\r
605 strncpy(bios_us, p, 31); bios_us[31] = 0;\r
606 } else strcpy(bios_us, "NOT FOUND");\r
607\r
608 if (find_bios(8, &bios)) { // EU\r
609 for (p = bios+strlen(bios)-1; p > bios && *p != '/'; p--); p++;\r
610 strncpy(bios_eu, p, 31); bios_eu[31] = 0;\r
611 } else strcpy(bios_eu, "NOT FOUND");\r
612\r
613 if (find_bios(1, &bios)) { // JP\r
614 for (p = bios+strlen(bios)-1; p > bios && *p != '/'; p--); p++;\r
615 strncpy(bios_jp, p, 31); bios_jp[31] = 0;\r
616 } else strcpy(bios_jp, "NOT FOUND");\r
617\r
618 for(;;)\r
619 {\r
620 draw_cd_menu_options(menu_sel, bios_us, bios_eu, bios_jp);\r
621 inp = wait_for_input(GP2X_UP|GP2X_DOWN|GP2X_LEFT|GP2X_RIGHT|GP2X_B|GP2X_X|GP2X_A|GP2X_START);\r
622 if(inp & GP2X_UP ) { menu_sel--; if (menu_sel < 0) menu_sel = menu_sel_max; }\r
623 if(inp & GP2X_DOWN) { menu_sel++; if (menu_sel > menu_sel_max) menu_sel = 0; }\r
624 if((inp& GP2X_B)||(inp&GP2X_LEFT)||(inp&GP2X_RIGHT)) { // toggleable options\r
625 switch (menu_sel) {\r
979ba09f 626 case 3: currentConfig.EmuOpt ^=0x400; break;\r
49fe50f0 627 case 4: currentConfig.PicoOpt^=0x800; break;\r
628 case 5: currentConfig.PicoOpt^=0x400; break;\r
629 case 6: return;\r
daf91588 630 }\r
631 }\r
632 if(inp & (GP2X_X|GP2X_A)) return;\r
633 if(inp & GP2X_START) { // BIOS testers\r
634 switch (menu_sel) {\r
635 case 0: if (find_bios(4, &bios)) { // test US\r
636 strcpy(romFileName, bios);\r
637 engineState = PGS_ReloadRom;\r
638 return;\r
639 }\r
640 break;\r
641 case 1: if (find_bios(8, &bios)) { // test EU\r
642 strcpy(romFileName, bios);\r
643 engineState = PGS_ReloadRom;\r
644 return;\r
645 }\r
646 break;\r
647 case 2: if (find_bios(1, &bios)) { // test JP\r
648 strcpy(romFileName, bios);\r
649 engineState = PGS_ReloadRom;\r
650 return;\r
651 }\r
652 break;\r
653 }\r
654 }\r
655 }\r
656}\r
657\r
658\r
659// --------- advanced options ----------\r
660\r
720ee7f6 661static void draw_amenu_options(int menu_sel)\r
662{\r
663 int tl_x = 25, tl_y = 60, y;\r
664 char *mms = mmuhack_status ? "active) " : "inactive)";\r
665\r
666 y = tl_y;\r
e5d315a5 667 //memset(gp2x_screen, 0, 320*240);\r
668 gp2x_pd_clone_buffer2();\r
669\r
979ba09f 670 gp2x_text_out8(tl_x, y, "Scale 32 column mode %s", (currentConfig.PicoOpt&0x100)?"ON":"OFF"); // 0\r
671 gp2x_text_out8(tl_x, (y+=10), "Gamma correction %i.%02i", currentConfig.gamma / 100, currentConfig.gamma%100); // 1\r
672 gp2x_text_out8(tl_x, (y+=10), "Emulate Z80 %s", (currentConfig.PicoOpt&0x004)?"ON":"OFF"); // 2\r
673 gp2x_text_out8(tl_x, (y+=10), "Emulate YM2612 (FM) %s", (currentConfig.PicoOpt&0x001)?"ON":"OFF"); // 3\r
674 gp2x_text_out8(tl_x, (y+=10), "Emulate SN76496 (PSG) %s", (currentConfig.PicoOpt&0x002)?"ON":"OFF"); // 4\r
675 gp2x_text_out8(tl_x, (y+=10), "gzip savestates %s", (currentConfig.EmuOpt &0x008)?"ON":"OFF"); // 5\r
676 gp2x_text_out8(tl_x, (y+=10), "Don't save config on exit %s", (currentConfig.EmuOpt &0x020)?"ON":"OFF"); // 6\r
720ee7f6 677 gp2x_text_out8(tl_x, (y+=10), "needs restart:");\r
979ba09f 678 gp2x_text_out8(tl_x, (y+=10), "craigix's RAM timings %s", (currentConfig.EmuOpt &0x100)?"ON":"OFF"); // 8\r
679 gp2x_text_out8(tl_x, (y+=10), "squidgehack (now %s %s", mms, (currentConfig.EmuOpt &0x010)?"ON":"OFF"); // 9\r
720ee7f6 680 gp2x_text_out8(tl_x, (y+=10), "Done");\r
681\r
682 // draw cursor\r
683 gp2x_text_out8(tl_x - 16, tl_y + menu_sel*10, ">");\r
684\r
e5d315a5 685 gp2x_video_flip2();\r
720ee7f6 686}\r
687\r
688static void amenu_loop_options(void)\r
689{\r
47f22a1f 690 int menu_sel = 0, menu_sel_max = 10;\r
720ee7f6 691 unsigned long inp = 0;\r
692\r
693 for(;;)\r
694 {\r
695 draw_amenu_options(menu_sel);\r
696 inp = wait_for_input(GP2X_UP|GP2X_DOWN|GP2X_LEFT|GP2X_RIGHT|GP2X_B|GP2X_X|GP2X_A);\r
697 if(inp & GP2X_UP ) { menu_sel--; if (menu_sel < 0) menu_sel = menu_sel_max; }\r
698 if(inp & GP2X_DOWN) { menu_sel++; if (menu_sel > menu_sel_max) menu_sel = 0; }\r
699 if((inp& GP2X_B)||(inp&GP2X_LEFT)||(inp&GP2X_RIGHT)) { // toggleable options\r
700 switch (menu_sel) {\r
979ba09f 701 case 0: currentConfig.PicoOpt^=0x100; break;\r
702 case 2: currentConfig.PicoOpt^=0x004; break;\r
703 case 3: currentConfig.PicoOpt^=0x001; break;\r
704 case 4: currentConfig.PicoOpt^=0x002; break;\r
705 case 5: currentConfig.EmuOpt ^=0x008; break;\r
706 case 6: currentConfig.EmuOpt ^=0x020; break;\r
707 case 8: currentConfig.EmuOpt ^=0x100; break;\r
708 case 9: currentConfig.EmuOpt ^=0x010; break;\r
720ee7f6 709 case 10: return;\r
710 }\r
711 }\r
712 if(inp & (GP2X_X|GP2X_A)) return;\r
713 if(inp & (GP2X_LEFT|GP2X_RIGHT)) { // multi choise\r
714 switch (menu_sel) {\r
715 case 1:\r
716 while ((inp = gp2x_joystick_read(1)) & (GP2X_LEFT|GP2X_RIGHT)) {\r
979ba09f 717 currentConfig.gamma += (inp & GP2X_LEFT) ? -1 : 1;\r
718 if (currentConfig.gamma < 1) currentConfig.gamma = 1;\r
719 if (currentConfig.gamma > 300) currentConfig.gamma = 300;\r
720ee7f6 720 draw_amenu_options(menu_sel);\r
721 usleep(18*1000);\r
722 }\r
723 break;\r
724 }\r
725 }\r
726 }\r
727}\r
728\r
729// -------------- options --------------\r
730\r
979ba09f 731static const char *region_name(unsigned int code)\r
720ee7f6 732{\r
979ba09f 733 static const char *names[] = { "Auto", " Japan NTSC", " Japan PAL", " USA", " Europe" };\r
734 static const char *names_short[] = { "", " JP", " JP", " US", " EU" };\r
735 int u, i = 0;\r
736 if (code) {\r
737 code <<= 1;\r
738 while((code >>= 1)) i++;\r
739 if (i > 4) return "unknown";\r
740 return names[i];\r
741 } else {\r
742 static char name[24];\r
743 strcpy(name, "Auto:");\r
744 for (u = 0; u < 3; u++) {\r
745 i = 0; code = ((PicoAutoRgnOrder >> u*4) & 0xf) << 1;\r
746 while((code >>= 1)) i++;\r
747 strcat(name, names_short[i]);\r
748 }\r
749 return name;\r
750 }\r
720ee7f6 751}\r
752\r
753static void draw_menu_options(int menu_sel)\r
754{\r
755 int tl_x = 25, tl_y = 40, y;\r
756 char monostereo[8], strframeskip[8], *strrend;\r
757\r
979ba09f 758 strcpy(monostereo, (currentConfig.PicoOpt&0x08)?"stereo":"mono");\r
759 if (currentConfig.Frameskip < 0)\r
720ee7f6 760 strcpy(strframeskip, "Auto");\r
979ba09f 761 else sprintf(strframeskip, "%i", currentConfig.Frameskip);\r
762 if (currentConfig.PicoOpt&0x10) {\r
720ee7f6 763 strrend = " 8bit fast";\r
979ba09f 764 } else if (currentConfig.EmuOpt&0x80) {\r
720ee7f6 765 strrend = "16bit accurate";\r
766 } else {\r
767 strrend = " 8bit accurate";\r
768 }\r
769\r
770 y = tl_y;\r
e5d315a5 771 //memset(gp2x_screen, 0, 320*240);\r
772 gp2x_pd_clone_buffer2();\r
773\r
720ee7f6 774 gp2x_text_out8(tl_x, y, "Renderer: %s", strrend); // 0\r
979ba09f 775 gp2x_text_out8(tl_x, (y+=10), "Accurate timing (slower) %s", (currentConfig.PicoOpt&0x040)?"ON":"OFF"); // 1\r
776 gp2x_text_out8(tl_x, (y+=10), "Accurate sprites (slower) %s", (currentConfig.PicoOpt&0x080)?"ON":"OFF"); // 2\r
777 gp2x_text_out8(tl_x, (y+=10), "Show FPS %s", (currentConfig.EmuOpt &0x002)?"ON":"OFF"); // 3\r
720ee7f6 778 gp2x_text_out8(tl_x, (y+=10), "Frameskip %s", strframeskip);\r
979ba09f 779 gp2x_text_out8(tl_x, (y+=10), "Enable sound %s", (currentConfig.EmuOpt &0x004)?"ON":"OFF"); // 5\r
780 gp2x_text_out8(tl_x, (y+=10), "Sound Quality: %5iHz %s", currentConfig.PsndRate, monostereo);\r
781 gp2x_text_out8(tl_x, (y+=10), "Use ARM940 core for sound %s", (currentConfig.PicoOpt&0x200)?"ON":"OFF"); // 7\r
782 gp2x_text_out8(tl_x, (y+=10), "6 button pad %s", (currentConfig.PicoOpt&0x020)?"ON":"OFF"); // 8\r
783 gp2x_text_out8(tl_x, (y+=10), "Genesis Region: %s", region_name(currentConfig.PicoRegion));\r
784 gp2x_text_out8(tl_x, (y+=10), "Use SRAM/BRAM savestates %s", (currentConfig.EmuOpt &0x001)?"ON":"OFF"); // 10\r
785 gp2x_text_out8(tl_x, (y+=10), "Confirm save overwrites %s", (currentConfig.EmuOpt &0x200)?"ON":"OFF"); // 11\r
720ee7f6 786 gp2x_text_out8(tl_x, (y+=10), "Save slot %i", state_slot); // 12\r
979ba09f 787 gp2x_text_out8(tl_x, (y+=10), "GP2X CPU clocks %iMhz", currentConfig.CPUclock);\r
daf91588 788 gp2x_text_out8(tl_x, (y+=10), "[Sega/Mega CD options]");\r
789 gp2x_text_out8(tl_x, (y+=10), "[advanced options]"); // 15\r
720ee7f6 790 gp2x_text_out8(tl_x, (y+=10), "Save cfg as default");\r
791 if (rom_data)\r
792 gp2x_text_out8(tl_x, (y+=10), "Save cfg for current game only");\r
793\r
794 // draw cursor\r
795 gp2x_text_out8(tl_x - 16, tl_y + menu_sel*10, ">");\r
796\r
e5d315a5 797 gp2x_video_flip2();\r
720ee7f6 798}\r
799\r
800static int sndrate_prevnext(int rate, int dir)\r
801{\r
802 int i, rates[] = { 8000, 11025, 16000, 22050, 44100 };\r
803\r
804 for (i = 0; i < 5; i++)\r
805 if (rates[i] == rate) break;\r
806\r
807 i += dir ? 1 : -1;\r
808 if (i > 4) return dir ? 44100 : 22050;\r
809 if (i < 0) return dir ? 11025 : 8000;\r
810 return rates[i];\r
811}\r
812\r
979ba09f 813static void region_prevnext(int right)\r
814{\r
815 // jp_ntsc=1, jp_pal=2, usa=4, eu=8\r
816 static int rgn_orders[] = { 0x148, 0x184, 0x814, 0x418, 0x841, 0x481 };\r
817 int i;\r
818 if (right) {\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 < 5) PicoAutoRgnOrder = rgn_orders[i+1];\r
823 else currentConfig.PicoRegion=1;\r
824 }\r
825 else currentConfig.PicoRegion<<=1;\r
826 if (currentConfig.PicoRegion > 8) currentConfig.PicoRegion = 8;\r
827 } else {\r
828 if (!currentConfig.PicoRegion) {\r
829 for (i = 0; i < 6; i++)\r
830 if (rgn_orders[i] == PicoAutoRgnOrder) break;\r
831 if (i > 0) PicoAutoRgnOrder = rgn_orders[i-1];\r
832 }\r
833 else currentConfig.PicoRegion>>=1;\r
834 }\r
835}\r
836\r
720ee7f6 837static void menu_options_save(void)\r
838{\r
720ee7f6 839 PicoOpt = currentConfig.PicoOpt;\r
840 PsndRate = currentConfig.PsndRate;\r
841 PicoRegionOverride = currentConfig.PicoRegion;\r
842 if (PicoOpt & 0x20) {\r
843 actionNames[ 8] = "Z"; actionNames[ 9] = "Y";\r
844 actionNames[10] = "X"; actionNames[11] = "MODE";\r
845 } else {\r
846 actionNames[8] = actionNames[9] = actionNames[10] = actionNames[11] = 0;\r
847 }\r
848}\r
849\r
daf91588 850static int menu_loop_options(void)\r
720ee7f6 851{\r
daf91588 852 int menu_sel = 0, menu_sel_max = 16;\r
720ee7f6 853 unsigned long inp = 0;\r
854\r
855 if (rom_data) menu_sel_max++;\r
979ba09f 856 currentConfig.PicoOpt = PicoOpt;\r
857 currentConfig.PsndRate = PsndRate;\r
858 currentConfig.PicoRegion = PicoRegionOverride;\r
720ee7f6 859\r
860 for(;;)\r
861 {\r
862 draw_menu_options(menu_sel);\r
863 inp = wait_for_input(GP2X_UP|GP2X_DOWN|GP2X_LEFT|GP2X_RIGHT|GP2X_B|GP2X_X|GP2X_A);\r
864 if(inp & GP2X_UP ) { menu_sel--; if (menu_sel < 0) menu_sel = menu_sel_max; }\r
865 if(inp & GP2X_DOWN) { menu_sel++; if (menu_sel > menu_sel_max) menu_sel = 0; }\r
866 if((inp& GP2X_B)||(inp&GP2X_LEFT)||(inp&GP2X_RIGHT)) { // toggleable options\r
867 switch (menu_sel) {\r
979ba09f 868 case 1: currentConfig.PicoOpt^=0x040; break;\r
869 case 2: currentConfig.PicoOpt^=0x080; break;\r
870 case 3: currentConfig.EmuOpt ^=0x002; break;\r
871 case 5: currentConfig.EmuOpt ^=0x004; break;\r
872 case 7: currentConfig.PicoOpt^=0x200; break;\r
873 case 8: currentConfig.PicoOpt^=0x020; break;\r
874 case 10: currentConfig.EmuOpt ^=0x001; break;\r
875 case 11: currentConfig.EmuOpt ^=0x200; break;\r
daf91588 876 case 14: cd_menu_loop_options();\r
877 if (engineState == PGS_ReloadRom)\r
878 return 0; // test BIOS\r
879 break;\r
880 case 15: amenu_loop_options(); break;\r
881 case 16: // done (update and write)\r
720ee7f6 882 menu_options_save();\r
daf91588 883 if (emu_WriteConfig(0)) strcpy(menuErrorMsg, "config saved");\r
884 else strcpy(menuErrorMsg, "failed to write config");\r
885 return 1;\r
886 case 17: // done (update and write for current game)\r
720ee7f6 887 menu_options_save();\r
daf91588 888 if (emu_WriteConfig(1)) strcpy(menuErrorMsg, "config saved");\r
889 else strcpy(menuErrorMsg, "failed to write config");\r
890 return 1;\r
720ee7f6 891 }\r
892 }\r
979ba09f 893 if(inp & (GP2X_X|GP2X_A)) {\r
720ee7f6 894 menu_options_save();\r
daf91588 895 return 0; // done (update, no write)\r
720ee7f6 896 }\r
897 if(inp & (GP2X_LEFT|GP2X_RIGHT)) { // multi choise\r
898 switch (menu_sel) {\r
899 case 0:\r
900 if (inp & GP2X_LEFT) {\r
979ba09f 901 if ( currentConfig.PicoOpt&0x10) currentConfig.PicoOpt&= ~0x10;\r
902 else if (!(currentConfig.EmuOpt &0x80))currentConfig.EmuOpt |= 0x80;\r
903 else if ( currentConfig.EmuOpt &0x80) break;\r
720ee7f6 904 } else {\r
979ba09f 905 if ( currentConfig.PicoOpt&0x10) break;\r
906 else if (!(currentConfig.EmuOpt &0x80))currentConfig.PicoOpt|= 0x10;\r
907 else if ( currentConfig.EmuOpt &0x80) currentConfig.EmuOpt &= ~0x80;\r
720ee7f6 908 }\r
909 break;\r
910 case 4:\r
979ba09f 911 currentConfig.Frameskip += (inp & GP2X_LEFT) ? -1 : 1;\r
912 if (currentConfig.Frameskip < 0) currentConfig.Frameskip = -1;\r
913 if (currentConfig.Frameskip > 32) currentConfig.Frameskip = 32;\r
720ee7f6 914 break;\r
915 case 6:\r
979ba09f 916 if ((inp & GP2X_RIGHT) && currentConfig.PsndRate == 44100 && !(currentConfig.PicoOpt&0x08)) {\r
917 currentConfig.PsndRate = 8000; currentConfig.PicoOpt|= 0x08;\r
918 } else if ((inp & GP2X_LEFT) && currentConfig.PsndRate == 8000 && (currentConfig.PicoOpt&0x08)) {\r
919 currentConfig.PsndRate = 44100; currentConfig.PicoOpt&=~0x08;\r
920 } else currentConfig.PsndRate = sndrate_prevnext(currentConfig.PsndRate, inp & GP2X_RIGHT);\r
720ee7f6 921 break;\r
922 case 9:\r
979ba09f 923 region_prevnext(inp & GP2X_RIGHT);\r
720ee7f6 924 break;\r
925 case 12:\r
926 if (inp & GP2X_RIGHT) {\r
927 state_slot++; if (state_slot > 9) state_slot = 0;\r
928 } else {state_slot--; if (state_slot < 0) state_slot = 9;\r
929 }\r
930 break;\r
931 case 13:\r
932 while ((inp = gp2x_joystick_read(1)) & (GP2X_LEFT|GP2X_RIGHT)) {\r
979ba09f 933 currentConfig.CPUclock += (inp & GP2X_LEFT) ? -1 : 1;\r
934 if (currentConfig.CPUclock < 1) currentConfig.CPUclock = 1;\r
720ee7f6 935 draw_menu_options(menu_sel);\r
936 usleep(50*1000);\r
937 }\r
938 break;\r
939 }\r
940 }\r
941 }\r
942}\r
943\r
944// -------------- credits --------------\r
945\r
946static void draw_menu_credits(void)\r
947{\r
948 int tl_x = 15, tl_y = 70, y;\r
e5d315a5 949 //memset(gp2x_screen, 0, 320*240);\r
950 gp2x_pd_clone_buffer2();\r
720ee7f6 951\r
5111820c 952 gp2x_text_out8(tl_x, 20, "PicoDrive v" VERSION " (c) notaz, 2006,2007");\r
720ee7f6 953 y = tl_y;\r
954 gp2x_text_out8(tl_x, y, "Credits:");\r
955 gp2x_text_out8(tl_x, (y+=10), "Dave: Cyclone 68000 core,");\r
956 gp2x_text_out8(tl_x, (y+=10), " base code of PicoDrive");\r
957 gp2x_text_out8(tl_x, (y+=10), "Reesy & FluBBa: DrZ80 core");\r
958 gp2x_text_out8(tl_x, (y+=10), "MAME devs: YM2612 and SN76496 cores");\r
959 gp2x_text_out8(tl_x, (y+=10), "Charles MacDonald: Genesis hw docs");\r
960 gp2x_text_out8(tl_x, (y+=10), "Stephane Dallongeville:");\r
961 gp2x_text_out8(tl_x, (y+=10), " opensource Gens");\r
962 gp2x_text_out8(tl_x, (y+=10), "Haze: Genesis hw info");\r
963 gp2x_text_out8(tl_x, (y+=10), "rlyeh and others: minimal SDK");\r
964 gp2x_text_out8(tl_x, (y+=10), "Squidge: squidgehack");\r
965 gp2x_text_out8(tl_x, (y+=10), "Dzz: ARM940 sample");\r
966 gp2x_text_out8(tl_x, (y+=10), "GnoStiC / Puck2099: USB joystick");\r
967 gp2x_text_out8(tl_x, (y+=10), "craigix: GP2X hardware");\r
968\r
e5d315a5 969 gp2x_video_flip2();\r
720ee7f6 970}\r
971\r
972\r
973// -------------- root menu --------------\r
974\r
975static void draw_menu_root(int menu_sel)\r
976{\r
977 int tl_x = 70, tl_y = 70, y;\r
e5d315a5 978 //memset(gp2x_screen, 0, 320*240);\r
979 gp2x_pd_clone_buffer2();\r
720ee7f6 980\r
981 gp2x_text_out8(tl_x, 20, "PicoDrive v" VERSION);\r
982\r
983 y = tl_y;\r
984 if (rom_data) {\r
985 gp2x_text_out8(tl_x, y, "Resume game");\r
986 gp2x_text_out8(tl_x, (y+=10), "Save State");\r
987 gp2x_text_out8(tl_x, (y+=10), "Load State");\r
988 gp2x_text_out8(tl_x, (y+=10), "Reset game");\r
989 } else {\r
990 y += 30;\r
991 }\r
8dfb9fd5 992 gp2x_text_out8(tl_x, (y+=10), "Load new ROM/ISO");\r
720ee7f6 993 gp2x_text_out8(tl_x, (y+=10), "Change options");\r
994 gp2x_text_out8(tl_x, (y+=10), "Configure controls");\r
995 gp2x_text_out8(tl_x, (y+=10), "Credits");\r
996 gp2x_text_out8(tl_x, (y+=10), "Exit");\r
997\r
998 // draw cursor\r
999 gp2x_text_out8(tl_x - 16, tl_y + menu_sel*10, ">");\r
1000 // error\r
1001 if (menuErrorMsg[0]) gp2x_text_out8(5, 226, menuErrorMsg);\r
e5d315a5 1002 gp2x_video_flip2();\r
720ee7f6 1003}\r
1004\r
1005\r
1006static void menu_loop_root(void)\r
1007{\r
daf91588 1008 int ret, menu_sel = 4, menu_sel_max = 8, menu_sel_min = 4;\r
720ee7f6 1009 unsigned long inp = 0;\r
1010 char curr_path[PATH_MAX], *selfname;\r
1011 FILE *tstf;\r
1012\r
1013 if ( (tstf = fopen(currentConfig.lastRomFile, "rb")) )\r
1014 {\r
1015 fclose(tstf);\r
1016 strcpy(curr_path, currentConfig.lastRomFile);\r
1017 }\r
1018 else\r
1019 {\r
1020 getcwd(curr_path, PATH_MAX);\r
1021 }\r
1022\r
1023 if (rom_data) menu_sel = menu_sel_min = 0;\r
1024\r
1025 for(;;)\r
1026 {\r
1027 draw_menu_root(menu_sel);\r
1028 inp = wait_for_input(GP2X_UP|GP2X_DOWN|GP2X_B|GP2X_X|GP2X_SELECT);\r
1029 if(inp & GP2X_UP ) { menu_sel--; if (menu_sel < menu_sel_min) menu_sel = menu_sel_max; }\r
1030 if(inp & GP2X_DOWN) { menu_sel++; if (menu_sel > menu_sel_max) menu_sel = menu_sel_min; }\r
1031 if(inp &(GP2X_SELECT|GP2X_X)){\r
1032 if (rom_data) {\r
1033 while (gp2x_joystick_read(1) & (GP2X_SELECT|GP2X_X)) usleep(50*1000); // wait until select is released\r
1034 engineState = PGS_Running;\r
1035 break;\r
1036 }\r
1037 }\r
1038 if(inp & GP2X_B ) {\r
1039 switch (menu_sel) {\r
1040 case 0: // resume game\r
1041 if (rom_data) { engineState = PGS_Running; return; }\r
1042 break;\r
1043 case 1: // save state\r
1044 if (rom_data) {\r
1045 if(emu_SaveLoadGame(0, 0)) {\r
1046 strcpy(menuErrorMsg, "save failed");\r
1047 continue;\r
1048 }\r
1049 engineState = PGS_Running;\r
1050 return;\r
1051 }\r
1052 break;\r
1053 case 2: // load state\r
1054 if (rom_data) {\r
1055 if(emu_SaveLoadGame(1, 0)) {\r
1056 strcpy(menuErrorMsg, "load failed");\r
1057 continue;\r
1058 }\r
1059 engineState = PGS_Running;\r
1060 return;\r
1061 }\r
1062 break;\r
1063 case 3: // reset game\r
1064 if (rom_data) {\r
1065 emu_ResetGame();\r
1066 engineState = PGS_Running;\r
1067 return;\r
1068 }\r
1069 break;\r
1070 case 4: // select rom\r
1071 selfname = romsel_loop(curr_path);\r
1072 if (selfname) {\r
1073 printf("selected file: %s\n", selfname);\r
720ee7f6 1074 engineState = PGS_ReloadRom;\r
1075 }\r
1076 return;\r
1077 case 5: // options\r
daf91588 1078 ret = menu_loop_options();\r
1079 if (ret == 1) continue; // status update\r
1080 if (engineState == PGS_ReloadRom)\r
1081 return; // BIOS test\r
720ee7f6 1082 break;\r
1083 case 6: // controls\r
1084 kc_sel_loop();\r
1085 break;\r
1086 case 7: // credits\r
1087 draw_menu_credits();\r
1088 usleep(500*1000);\r
1089 inp = wait_for_input(GP2X_B|GP2X_X);\r
1090 break;\r
1091 case 8: // exit\r
1092 engineState = PGS_Quit;\r
1093 return;\r
1094 }\r
1095 }\r
1096 menuErrorMsg[0] = 0; // clear error msg\r
1097 }\r
1098}\r
1099\r
1100\r
e5d315a5 1101static void menu_gfx_prepare(void)\r
720ee7f6 1102{\r
e5d315a5 1103 extern int localPal[0x100];\r
1104 int i;\r
1105\r
1106 // don't clear old palette just for fun (but make it dark)\r
1107 for (i = 0x100-1; i >= 0; i--)\r
1108 localPal[i] = (localPal[i] >> 2) & 0x003f3f3f;\r
1109 localPal[0xe0] = 0x00000000; // reserved pixels for OSD\r
1110 localPal[0xf0] = 0x00ffffff;\r
720ee7f6 1111\r
1112 // switch to 8bpp\r
e5d315a5 1113 gp2x_video_changemode2(8);\r
720ee7f6 1114 gp2x_video_RGB_setscaling(320, 240);\r
e5d315a5 1115 gp2x_video_setpalette(localPal, 0x100);\r
1116 gp2x_video_flip2();\r
1117}\r
1118\r
1119\r
1120void menu_loop(void)\r
1121{\r
1122 menu_gfx_prepare();\r
720ee7f6 1123\r
1124 menu_loop_root();\r
1125\r
1126 menuErrorMsg[0] = 0;\r
1127}\r