35868d35 |
1 | /* FCE Ultra - NES/Famicom Emulator |
35868d35 |
2 | * |
3 | * This program is free software; you can redistribute it and/or modify |
4 | * it under the terms of the GNU General Public License as published by |
5 | * the Free Software Foundation; either version 2 of the License, or |
6 | * (at your option) any later version. |
7 | * |
8 | * This program is distributed in the hope that it will be useful, |
9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
11 | * GNU General Public License for more details. |
12 | * |
13 | * You should have received a copy of the GNU General Public License |
14 | * along with this program; if not, write to the Free Software |
15 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
16 | */ |
17 | |
971a1d07 |
18 | #include "../../state.h" |
19 | #include "../../general.h" |
e328100e |
20 | #include "../../input.h" |
6f6bc6fa |
21 | #include "../../svga.h" |
22 | #include "../../video.h" |
23 | #include "usbjoy.h" |
35868d35 |
24 | |
9b451455 |
25 | extern int FSkip; |
26 | |
35868d35 |
27 | /* UsrInputType[] is user-specified. InputType[] is current |
971a1d07 |
28 | (game loading can override user settings) |
35868d35 |
29 | */ |
30 | static int UsrInputType[2]={SI_GAMEPAD,SI_GAMEPAD}; |
35868d35 |
31 | static int UsrInputTypeFC={SI_NONE}; |
971a1d07 |
32 | |
33 | static int InputType[2]; |
35868d35 |
34 | static int InputTypeFC; |
35 | |
36 | static uint32 JSreturn; |
35868d35 |
37 | |
38 | static int powerpadsc[2][12]; |
39 | static int powerpadside=0; |
40 | |
35868d35 |
41 | static uint32 MouseData[3]; |
42 | static uint8 fkbkeys[0x48]; |
35868d35 |
43 | |
971a1d07 |
44 | static uint32 combo_acts = 0, combo_keys = 0; |
45 | static uint32 prev_emu_acts = 0; |
35868d35 |
46 | |
13624c8f |
47 | |
35868d35 |
48 | static void setsoundvol(int soundvolume) |
49 | { |
971a1d07 |
50 | int soundvolIndex; |
51 | static char soundvolmeter[24]; |
52 | |
53 | // draw on screen :D |
54 | gp2x_sound_volume(soundvolume, soundvolume); |
55 | int meterval=soundvolume/5; |
56 | for (soundvolIndex = 0; soundvolIndex < 20; soundvolIndex++) |
57 | { |
58 | if (soundvolIndex < meterval) |
59 | { |
60 | soundvolmeter[soundvolIndex]='*'; |
61 | } |
62 | else |
63 | { |
64 | soundvolmeter[soundvolIndex]='_'; |
65 | } |
66 | } |
67 | soundvolmeter[20]=0; |
68 | FCEU_DispMessage("|%s|", soundvolmeter); |
35868d35 |
69 | } |
13624c8f |
70 | |
71 | |
971a1d07 |
72 | static void do_emu_acts(uint32 acts) |
73 | { |
74 | uint32 actsc = acts; |
75 | acts &= acts ^ prev_emu_acts; |
76 | prev_emu_acts = actsc; |
77 | |
78 | if (acts & (3 << 30)) |
79 | { |
6f6bc6fa |
80 | unsigned long keys; |
81 | int do_it = 1; |
971a1d07 |
82 | if (acts & (1 << 30)) |
83 | { |
6f6bc6fa |
84 | if (Settings.sstate_confirm & 2) |
85 | { |
86 | FCEU_DispMessage("LOAD STATE? (Y=yes, X=no)"); |
87 | FCEU_PutImage(); |
88 | FCEUD_Update(XBuf+8,NULL,0); |
89 | while( !((keys = gp2x_joystick_read(1)) & (GP2X_X|GP2X_Y)) ) usleep(50*1024); |
90 | if (keys & GP2X_X) do_it = 0; |
91 | FCEU_DispMessage(""); |
92 | } |
93 | if (do_it) FCEUI_LoadState(); |
971a1d07 |
94 | } |
95 | else |
96 | { |
6f6bc6fa |
97 | if (Settings.sstate_confirm & 1) |
98 | { |
99 | char *fname = FCEU_MakeFName(FCEUMKF_STATE,CurrentState,0); |
100 | FILE *st=fopen(fname,"rb"); |
101 | free(fname); |
102 | if (st) |
103 | { |
104 | fclose(st); |
105 | FCEU_DispMessage("OVERWRITE SAVE? (Y=yes, X=no)"); |
106 | FCEU_PutImage(); |
107 | FCEUD_Update(XBuf+8,NULL,0); |
108 | while( !((keys = gp2x_joystick_read(1)) & (GP2X_X|GP2X_Y)) ) usleep(50*1024); |
109 | if (keys & GP2X_X) do_it = 0; |
110 | FCEU_DispMessage(""); |
111 | } |
112 | } |
113 | if (do_it) FCEUI_SaveState(); |
971a1d07 |
114 | } |
6f6bc6fa |
115 | RefreshThrottleFPS(); |
971a1d07 |
116 | } |
e328100e |
117 | else if (acts & (3 << 28)) // state slot next/prev |
971a1d07 |
118 | { |
119 | FILE *st; |
120 | char *fname; |
121 | |
e328100e |
122 | CurrentState += (acts & (1 << 29)) ? 1 : -1; |
971a1d07 |
123 | if (CurrentState > 9) CurrentState = 0; |
e328100e |
124 | if (CurrentState < 0) CurrentState = 9; |
971a1d07 |
125 | |
126 | fname = FCEU_MakeFName(FCEUMKF_STATE,CurrentState,0); |
127 | st=fopen(fname,"rb"); |
128 | free(fname); |
129 | FCEU_DispMessage("[%s] State Slot %i", st ? "USED" : "FREE", CurrentState); |
130 | if (st) fclose(st); |
131 | } |
e328100e |
132 | else if (acts & (1 << 27)) // FDS insert/eject |
133 | { |
c4980f9e |
134 | if(FCEUGameInfo.type == GIT_FDS) |
135 | FCEU_DoSimpleCommand(FCEUNPCMD_FDSINSERT); |
e328100e |
136 | } |
137 | else if (acts & (1 << 26)) // FDS select |
138 | { |
c4980f9e |
139 | if(FCEUGameInfo.type == GIT_FDS) |
140 | FCEU_DoSimpleCommand(FCEUNPCMD_FDSSELECT); |
e328100e |
141 | } |
142 | else if (acts & (1 << 25)) // VS Unisystem insert coin |
143 | { |
c4980f9e |
144 | if(FCEUGameInfo.type == GIT_VSUNI) |
145 | FCEU_DoSimpleCommand(FCEUNPCMD_VSUNICOIN); |
e328100e |
146 | } |
147 | } |
148 | |
149 | |
150 | #define down(b) (keys & GP2X_##b) |
151 | static void do_fake_mouse(unsigned long keys) |
152 | { |
21afaa36 |
153 | static int x=256/2, y=240/2; |
e328100e |
154 | int speed = 3; |
155 | |
156 | if (down(A)) speed = 1; |
157 | if (down(Y)) speed = 5; |
158 | |
159 | if (down(LEFT)) |
160 | { |
161 | x -= speed; |
162 | if (x < 0) x = 0; |
163 | } |
164 | else if (down(RIGHT)) |
165 | { |
166 | x += speed; |
167 | if (x > 255) x = 255; |
168 | } |
169 | |
170 | if (down(UP)) |
171 | { |
172 | y -= speed; |
173 | if (y < 0) y = 0; |
174 | } |
175 | else if (down(DOWN)) |
176 | { |
177 | y += speed; |
178 | if (y > 239) y = 239; |
179 | } |
180 | |
181 | MouseData[0] = x; |
182 | MouseData[1] = y; |
183 | MouseData[2] = 0; |
184 | if (down(B)) MouseData[2] |= 1; |
185 | if (down(X)) MouseData[2] |= 2; |
971a1d07 |
186 | } |
187 | |
13624c8f |
188 | |
9b451455 |
189 | static void FCEUD_UpdateInput(void) |
35868d35 |
190 | { |
b547bda7 |
191 | static int volpushed_frames = 0; |
6f6bc6fa |
192 | static int turbo_rate_cnt_a[2] = {0,0}, turbo_rate_cnt_b[2] = {0,0}; |
b547bda7 |
193 | unsigned long keys = gp2x_joystick_read(0); |
6f6bc6fa |
194 | uint32 all_acts[2] = {0,0}; |
b547bda7 |
195 | int i; |
196 | |
b547bda7 |
197 | if ((down(VOL_DOWN) && down(VOL_UP)) || (keys & (GP2X_L|GP2X_L|GP2X_START)) == (GP2X_L|GP2X_L|GP2X_START)) |
35868d35 |
198 | { |
b547bda7 |
199 | Exit = 1; |
9b451455 |
200 | FSkip = 0; /* force rendering the last frame for menu */ |
b547bda7 |
201 | return; |
35868d35 |
202 | } |
b547bda7 |
203 | else if (down(VOL_UP)) |
35868d35 |
204 | { |
b547bda7 |
205 | /* wait for at least 10 updates, because user may be just trying to enter menu */ |
206 | if (volpushed_frames++ > 10) { |
207 | soundvol++; |
208 | if (soundvol > 100) soundvol=100; |
209 | //FCEUI_SetSoundVolume(soundvol); |
210 | setsoundvol(soundvol); |
211 | } |
35868d35 |
212 | } |
b547bda7 |
213 | else if (down(VOL_DOWN)) |
35868d35 |
214 | { |
b547bda7 |
215 | if (volpushed_frames++ > 10) { |
216 | soundvol-=1; |
217 | if (soundvol < 0) soundvol=0; |
218 | //FCEUI_SetSoundVolume(soundvol); |
219 | setsoundvol(soundvol); |
220 | } |
221 | } |
222 | else |
223 | { |
224 | volpushed_frames = 0; |
35868d35 |
225 | } |
226 | |
971a1d07 |
227 | JSreturn = 0; // RLDU SEBA |
228 | |
e328100e |
229 | if (InputType[1] != SI_GAMEPAD) |
230 | { |
231 | /* try to feed fake mouse there */ |
232 | do_fake_mouse(keys); |
233 | } |
234 | |
b547bda7 |
235 | for (i = 0; i < 32; i++) |
236 | { |
971a1d07 |
237 | if (keys & (1 << i)) |
238 | { |
239 | uint32 acts, u = 32; |
240 | acts = Settings.KeyBinds[i]; |
b547bda7 |
241 | if (!acts) continue; |
971a1d07 |
242 | if ((1 << i) & combo_keys) |
243 | { |
244 | // combo key detected, try to find if other is pressed |
245 | for (u = i+1; u < 32; u++) |
246 | { |
247 | if ((keys & (1 << u)) && (Settings.KeyBinds[u] & acts)) |
248 | { |
249 | keys &= ~(1 << u); |
250 | break; |
251 | } |
252 | } |
989672f4 |
253 | } |
971a1d07 |
254 | if (u != 32) acts &= combo_acts; // other combo key pressed |
255 | else acts &= ~combo_acts; |
6f6bc6fa |
256 | all_acts[(acts>>16)&1] |= acts; |
257 | } |
258 | } |
259 | |
260 | // add joy inputs |
261 | if (num_of_joys > 0) |
262 | { |
263 | int joy; |
264 | gp2x_usbjoy_update(); |
265 | for (joy = 0; joy < num_of_joys; joy++) { |
266 | int keys = gp2x_usbjoy_check2(joy); |
267 | for (i = 0; i < 32; i++) { |
268 | if (keys & (1 << i)) { |
269 | int acts = Settings.JoyBinds[joy][i]; |
270 | all_acts[(acts>>16)&1] |= acts; |
271 | } |
272 | } |
b547bda7 |
273 | } |
274 | } |
35868d35 |
275 | |
6f6bc6fa |
276 | // player 1 |
277 | JSreturn |= all_acts[0] & 0xff; |
278 | if (all_acts[0] & 0x100) { // A turbo |
279 | turbo_rate_cnt_a[0] += Settings.turbo_rate_add; |
280 | JSreturn |= (turbo_rate_cnt_a[0] >> 24) & 1; |
971a1d07 |
281 | } |
6f6bc6fa |
282 | if (all_acts[0] & 0x200) { // B turbo |
283 | turbo_rate_cnt_b[0] += Settings.turbo_rate_add; |
284 | JSreturn |= (turbo_rate_cnt_b[0] >> 23) & 2; |
971a1d07 |
285 | } |
35868d35 |
286 | |
6f6bc6fa |
287 | // player 2 |
288 | JSreturn |= (all_acts[1] & 0xff) << 16; |
289 | if (all_acts[1] & 0x100) { // A turbo |
290 | turbo_rate_cnt_a[1] += Settings.turbo_rate_add; |
291 | JSreturn |= (turbo_rate_cnt_a[1] >> 8) & 0x10000; |
292 | } |
293 | if (all_acts[1] & 0x200) { // B turbo |
294 | turbo_rate_cnt_b[1] += Settings.turbo_rate_add; |
295 | JSreturn |= (turbo_rate_cnt_b[1] >> 7) & 0x20000; |
296 | } |
297 | |
298 | do_emu_acts(all_acts[0]|all_acts[1]); |
35868d35 |
299 | } |
35868d35 |
300 | |
301 | |
302 | static void InitOtherInput(void) |
303 | { |
304 | |
305 | void *InputDPtr; |
306 | |
307 | int t; |
308 | int x; |
309 | int attrib; |
310 | |
e328100e |
311 | printf("InitOtherInput: InputType[0]: %i, InputType[1]: %i, InputTypeFC: %i\n", |
312 | InputType[0], InputType[1], InputTypeFC); |
313 | |
35868d35 |
314 | for(t=0,x=0;x<2;x++) |
315 | { |
316 | attrib=0; |
317 | InputDPtr=0; |
318 | switch(InputType[x]) |
319 | { |
320 | //case SI_POWERPAD:InputDPtr=&powerpadbuf[x];break; |
321 | case SI_GAMEPAD:InputDPtr=((uint8 *)&JSreturn)+(x<<1);break; |
322 | case SI_ARKANOID:InputDPtr=MouseData;t|=1;break; |
323 | case SI_ZAPPER:InputDPtr=MouseData; |
324 | t|=1; |
325 | attrib=1; |
326 | break; |
327 | } |
328 | FCEUI_SetInput(x,InputType[x],InputDPtr,attrib); |
329 | } |
330 | |
331 | attrib=0; |
332 | InputDPtr=0; |
333 | switch(InputTypeFC) |
334 | { |
335 | case SIFC_SHADOW:InputDPtr=MouseData;t|=1;attrib=1;break; |
336 | case SIFC_ARKANOID:InputDPtr=MouseData;t|=1;break; |
337 | case SIFC_FKB:InputDPtr=fkbkeys;break; |
338 | } |
339 | |
340 | FCEUI_SetInputFC(InputTypeFC,InputDPtr,attrib); |
341 | FCEUI_DisableFourScore(eoptions&EO_NOFOURSCORE); |
342 | |
971a1d07 |
343 | inited|=16; |
344 | } |
345 | |
346 | |
347 | static void PrepareOtherInput(void) |
348 | { |
349 | uint32 act, key, seen_acts; |
350 | |
351 | combo_acts = combo_keys = prev_emu_acts = seen_acts = 0; |
352 | |
353 | // find combo_acts |
354 | for (act = 1; act; act <<= 1) |
355 | { |
356 | for (key = 1; key < 32; key++) |
357 | { |
358 | if (Settings.KeyBinds[key] & act) |
359 | { |
360 | if (seen_acts & act) combo_acts |= act; |
361 | else seen_acts |= act; |
362 | } |
363 | } |
364 | } |
365 | |
6f6bc6fa |
366 | combo_acts &= ~0x00030000; // don't take player_id bits |
367 | |
971a1d07 |
368 | // find combo_keys |
369 | for (act = 1; act; act <<= 1) |
370 | { |
371 | for (key = 0; key < 32; key++) |
372 | { |
373 | if (Settings.KeyBinds[key] & combo_acts) |
374 | { |
375 | combo_keys |= 1 << key; |
376 | } |
377 | } |
378 | } |
379 | |
03497cf8 |
380 | // printf("generated combo_acts: %08x, combo_keys: %08x\n", combo_acts, combo_keys); |
35868d35 |
381 | } |
971a1d07 |
382 | |