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