savestate and other bugfixes
[fceu.git] / drivers / gp2x / input.c
1 /* FCE Ultra - NES/Famicom Emulator
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
18 #include "../../state.h"
19 #include "../../general.h"
20 #include "../../input.h"
21 #include "../../svga.h"
22 #include "../../video.h"
23 #include "usbjoy.h"
24
25 extern int FSkip;
26
27 /* UsrInputType[] is user-specified.  InputType[] is current
28        (game loading can override user settings)
29 */
30 static int UsrInputType[2]={SI_GAMEPAD,SI_GAMEPAD};
31 static int UsrInputTypeFC={SI_NONE};
32
33 static int InputType[2];
34 static int InputTypeFC;
35
36 static uint32 JSreturn;
37
38 static int powerpadsc[2][12];
39 static int powerpadside=0;
40
41 static uint32 MouseData[3];
42 static uint8 fkbkeys[0x48];
43
44 static uint32 combo_acts = 0, combo_keys = 0;
45 static uint32 prev_emu_acts = 0;
46
47
48 static void setsoundvol(int soundvolume)
49 {
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);
69 }
70
71
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         {
80                 unsigned long keys;
81                 int do_it = 1;
82                 if (acts & (1 << 30))
83                 {
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_CancelDispMessage();
92                         }
93                         if (do_it) FCEUI_LoadState();
94                 }
95                 else
96                 {
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_CancelDispMessage();
111                                 }
112                         }
113                         if (do_it) FCEUI_SaveState();
114                 }
115                 RefreshThrottleFPS();
116         }
117         else if (acts & (3 << 28)) // state slot next/prev
118         {
119                 FILE *st;
120                 char *fname;
121
122                 CurrentState += (acts & (1 << 29)) ? 1 :  -1;
123                 if (CurrentState > 9) CurrentState = 0;
124                 if (CurrentState < 0) CurrentState = 9;
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         }
132         else if (acts & (1 << 27)) // FDS insert/eject
133         {
134                 if(FCEUGameInfo.type == GIT_FDS)
135                         FCEU_DoSimpleCommand(FCEUNPCMD_FDSINSERT);
136         }
137         else if (acts & (1 << 26)) // FDS select
138         {
139                 if(FCEUGameInfo.type == GIT_FDS)
140                         FCEU_DoSimpleCommand(FCEUNPCMD_FDSSELECT);
141         }
142         else if (acts & (1 << 25)) // VS Unisystem insert coin
143         {
144                 if(FCEUGameInfo.type == GIT_VSUNI)
145                         FCEU_DoSimpleCommand(FCEUNPCMD_VSUNICOIN);
146         }
147 }
148
149
150 #define down(b) (keys & GP2X_##b)
151 static void do_fake_mouse(unsigned long keys)
152 {
153         static int x=256/2, y=240/2;
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;
186 }
187
188
189 static void FCEUD_UpdateInput(void)
190 {
191         static int volpushed_frames = 0;
192         static int turbo_rate_cnt_a[2] = {0,0}, turbo_rate_cnt_b[2] = {0,0};
193         unsigned long keys = gp2x_joystick_read(0);
194         uint32 all_acts[2] = {0,0};
195         int i;
196
197         if ((down(VOL_DOWN) && down(VOL_UP)) || (keys & (GP2X_L|GP2X_L|GP2X_START)) == (GP2X_L|GP2X_L|GP2X_START))
198         {
199                 Exit = 1;
200                 FSkip = 0;      /* force rendering the last frame for menu */
201                 return;
202         }
203         else if (down(VOL_UP))
204         {
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                 }
212         }
213         else if (down(VOL_DOWN))
214         {
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;
225         }
226
227         JSreturn = 0; // RLDU SEBA
228
229         if (InputType[1] != SI_GAMEPAD)
230         {
231                 /* try to feed fake mouse there */
232                 do_fake_mouse(keys);
233         }
234
235         for (i = 0; i < 32; i++)
236         {
237                 if (keys & (1 << i))
238                 {
239                         uint32 acts, u = 32;
240                         acts = Settings.KeyBinds[i];
241                         if (!acts) continue;
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                                 }
253                         }
254                         if (u != 32) acts &=  combo_acts; // other combo key pressed
255                         else         acts &= ~combo_acts;
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                         }
273                 }
274         }
275
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;
281         }
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;
285         }
286
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]);
299 }
300
301
302 static void InitOtherInput(void)
303 {
304
305    void *InputDPtr;
306
307    int t;
308    int x;
309    int attrib;
310
311    printf("InitOtherInput: InputType[0]: %i, InputType[1]: %i, InputTypeFC: %i\n",
312         InputType[0], InputType[1], InputTypeFC);
313
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
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
366         combo_acts &= ~0x00030000; // don't take player_id bits
367
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
380         // printf("generated combo_acts: %08x, combo_keys: %08x\n", combo_acts, combo_keys);
381 }
382