minor adjustments
[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];
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);
35868d35 67}
13624c8f 68
69
971a1d07 70static 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 {
6f6bc6fa 78 unsigned long keys;
79 int do_it = 1;
971a1d07 80 if (acts & (1 << 30))
81 {
6f6bc6fa 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();
971a1d07 92 }
93 else
94 {
6f6bc6fa 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();
971a1d07 112 }
6f6bc6fa 113 RefreshThrottleFPS();
971a1d07 114 }
e328100e 115 else if (acts & (3 << 28)) // state slot next/prev
971a1d07 116 {
117 FILE *st;
118 char *fname;
119
e328100e 120 CurrentState += (acts & (1 << 29)) ? 1 : -1;
971a1d07 121 if (CurrentState > 9) CurrentState = 0;
e328100e 122 if (CurrentState < 0) CurrentState = 9;
971a1d07 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 }
e328100e 130 else if (acts & (1 << 27)) // FDS insert/eject
131 {
c4980f9e 132 if(FCEUGameInfo.type == GIT_FDS)
133 FCEU_DoSimpleCommand(FCEUNPCMD_FDSINSERT);
e328100e 134 }
135 else if (acts & (1 << 26)) // FDS select
136 {
c4980f9e 137 if(FCEUGameInfo.type == GIT_FDS)
138 FCEU_DoSimpleCommand(FCEUNPCMD_FDSSELECT);
e328100e 139 }
140 else if (acts & (1 << 25)) // VS Unisystem insert coin
141 {
c4980f9e 142 if(FCEUGameInfo.type == GIT_VSUNI)
143 FCEU_DoSimpleCommand(FCEUNPCMD_VSUNICOIN);
e328100e 144 }
145}
146
147
148#define down(b) (keys & GP2X_##b)
149static void do_fake_mouse(unsigned long keys)
150{
21afaa36 151 static int x=256/2, y=240/2;
e328100e 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;
971a1d07 184}
185
13624c8f 186
187void FCEUD_UpdateInput(void)
35868d35 188{
b547bda7 189 static int volpushed_frames = 0;
6f6bc6fa 190 static int turbo_rate_cnt_a[2] = {0,0}, turbo_rate_cnt_b[2] = {0,0};
b547bda7 191 unsigned long keys = gp2x_joystick_read(0);
6f6bc6fa 192 uint32 all_acts[2] = {0,0};
b547bda7 193 int i;
194
b547bda7 195 if ((down(VOL_DOWN) && down(VOL_UP)) || (keys & (GP2X_L|GP2X_L|GP2X_START)) == (GP2X_L|GP2X_L|GP2X_START))
35868d35 196 {
b547bda7 197 Exit = 1;
b547bda7 198 return;
35868d35 199 }
b547bda7 200 else if (down(VOL_UP))
35868d35 201 {
b547bda7 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 }
35868d35 209 }
b547bda7 210 else if (down(VOL_DOWN))
35868d35 211 {
b547bda7 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;
35868d35 222 }
223
971a1d07 224 JSreturn = 0; // RLDU SEBA
225
e328100e 226 if (InputType[1] != SI_GAMEPAD)
227 {
228 /* try to feed fake mouse there */
229 do_fake_mouse(keys);
230 }
231
b547bda7 232 for (i = 0; i < 32; i++)
233 {
971a1d07 234 if (keys & (1 << i))
235 {
236 uint32 acts, u = 32;
237 acts = Settings.KeyBinds[i];
b547bda7 238 if (!acts) continue;
971a1d07 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 }
989672f4 250 }
971a1d07 251 if (u != 32) acts &= combo_acts; // other combo key pressed
252 else acts &= ~combo_acts;
6f6bc6fa 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 }
b547bda7 270 }
271 }
35868d35 272
6f6bc6fa 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;
971a1d07 278 }
6f6bc6fa 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;
971a1d07 282 }
35868d35 283
6f6bc6fa 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]);
35868d35 296}
35868d35 297
298
299static void InitOtherInput(void)
300{
301
302 void *InputDPtr;
303
304 int t;
305 int x;
306 int attrib;
307
e328100e 308 printf("InitOtherInput: InputType[0]: %i, InputType[1]: %i, InputTypeFC: %i\n",
309 InputType[0], InputType[1], InputTypeFC);
310
35868d35 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
971a1d07 340 inited|=16;
341}
342
343
344static 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
6f6bc6fa 363 combo_acts &= ~0x00030000; // don't take player_id bits
364
971a1d07 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
03497cf8 377 // printf("generated combo_acts: %08x, combo_keys: %08x\n", combo_acts, combo_keys);
35868d35 378}
971a1d07 379