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