more menu work, scalers, sound
[fceu.git] / drivers / gp2x / input.c
1 /* FCE Ultra - NES/Famicom Emulator
2  *
3  * Copyright notice for this file:
4  *  Copyright (C) 2002 Ben Parnell
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20
21 #define JOY_A   1
22 #define JOY_B   2
23 #define JOY_SELECT      4
24 #define JOY_START       8
25 #define JOY_UP  0x10
26 #define JOY_DOWN        0x20
27 #define JOY_LEFT        0x40
28 #define JOY_RIGHT       0x80
29
30 #include "minimal.h"
31
32 extern uint8 Exit; // exit emu loop
33
34
35 /* UsrInputType[] is user-specified.  InputType[] is current
36         (game loading can override user settings)
37 */
38 static int UsrInputType[2]={SI_GAMEPAD,SI_GAMEPAD};
39 static int InputType[2];
40
41 static int UsrInputTypeFC={SI_NONE};
42 static int InputTypeFC;
43
44 static uint32 JSreturn;
45 int NoWaiting=0;
46
47 static int powerpadsc[2][12];
48 static int powerpadside=0;
49
50
51 static uint32 MouseData[3];
52 static uint8 fkbkeys[0x48];
53 unsigned long lastpad=0;
54
55 char soundvolmeter[21];
56 int soundvolIndex=0;
57
58
59 static void setsoundvol(int soundvolume)
60 {
61     //FCEUI_SetSoundVolume(soundvol);
62     // draw on screen :D
63     gp2x_sound_volume(soundvolume, soundvolume);
64     int meterval=soundvolume/5;
65     for (soundvolIndex=0; soundvolIndex < 20; soundvolIndex++)
66     {
67        if (soundvolIndex < meterval)
68        {
69           soundvolmeter[soundvolIndex]='*';
70        }
71        else
72        {
73           soundvolmeter[soundvolIndex]='_';
74        }
75     }
76     soundvolmeter[20]=0;
77     FCEU_DispMessage("|%s|", soundvolmeter);
78 }
79
80
81
82 void FCEUD_UpdateInput(void)
83 {
84         static int volpushed_frames = 0;
85         static int turbo_rate_cnt_a = 0, turbo_rate_cnt_b = 0;
86         long lastpad2 = lastpad;
87         unsigned long keys = gp2x_joystick_read(0);
88         uint32 JS = 0; // RLDU SEBA
89         int i;
90
91         #define down(b) (keys & GP2X_##b)
92         if ((down(VOL_DOWN) && down(VOL_UP)) || (keys & (GP2X_L|GP2X_L|GP2X_START)) == (GP2X_L|GP2X_L|GP2X_START))
93         {
94                 Exit = 1;
95                 JSreturn = 0;
96                 return;
97         }
98         else if (down(VOL_UP))
99         {
100                 /* wait for at least 10 updates, because user may be just trying to enter menu */
101                 if (volpushed_frames++ > 10) {
102                         soundvol++;
103                         if (soundvol > 100) soundvol=100;
104                         //FCEUI_SetSoundVolume(soundvol);
105                         setsoundvol(soundvol);
106                 }
107         }
108         else if (down(VOL_DOWN))
109         {
110                 if (volpushed_frames++ > 10) {
111                         soundvol-=1;
112                         if (soundvol < 0) soundvol=0;
113                         //FCEUI_SetSoundVolume(soundvol);
114                         setsoundvol(soundvol);
115                 }
116         }
117         else
118         {
119                 volpushed_frames = 0;
120         }
121
122
123         for (i = 0; i < 32; i++)
124         {
125                 if (keys & (1 << i)) {
126                         int acts = Settings.KeyBinds[i];
127                         if (!acts) continue;
128                         JS |= acts & 0xff;
129                         if (acts & 0x100) {             // A turbo
130                                 turbo_rate_cnt_a += Settings.turbo_rate_add;
131                                 JS |= (turbo_rate_cnt_a >> 24) & 1;
132                         }
133                         if (acts & 0x200) {             // B turbo
134                                 turbo_rate_cnt_b += Settings.turbo_rate_add;
135                                 JS |= (turbo_rate_cnt_b >> 23) & 2;
136                         }
137                 }
138         }
139
140
141         JSreturn = JS;
142         lastpad=keys;
143
144         //JSreturn=(JS&0xFF000000)|(JS&0xFF)|((JS&0xFF0000)>>8)|((JS&0xFF00)<<8);
145
146 #define pad keys
147
148   //  JSreturn=(JSreturn&0xFF000000)|(JSreturn&0xFF)|((JSreturn&0xFF0000)>>8)|((JSreturn&0xFF00)<<8);
149   // TODO: make these bindable, use new interface
150   if(gametype==GIT_FDS)
151   {
152     NoWaiting&=~1;
153         if ((pad & GP2X_PUSH) && (!(pad & GP2X_SELECT)) && (!(pad & GP2X_L)) && (!(pad & GP2X_R)) && (!(lastpad2 & GP2X_PUSH)))
154         {
155       DriverInterface(DES_FDSSELECT,0);
156         }
157         else if ((pad & GP2X_L) && (!(pad & GP2X_SELECT)) && (!(pad & GP2X_PUSH)) && (!(pad & GP2X_R))&& (!(lastpad2 & GP2X_L)))
158         {
159       DriverInterface(DES_FDSINSERT,0);
160         }
161         else if ((pad & GP2X_R) && (!(pad & GP2X_SELECT)) && (!(pad & GP2X_L)) && (!(pad & GP2X_PUSH)) && (!(lastpad2 & GP2X_R)))
162         {
163       DriverInterface(DES_FDSEJECT,0);
164         }
165   }
166   return;
167 }
168
169
170 static void InitOtherInput(void)
171 {
172
173    void *InputDPtr;
174
175    int t;
176    int x;
177    int attrib;
178
179    for(t=0,x=0;x<2;x++)
180    {
181     attrib=0;
182     InputDPtr=0;
183     switch(InputType[x])
184     {
185       //case SI_POWERPAD:InputDPtr=&powerpadbuf[x];break;
186      case SI_GAMEPAD:InputDPtr=((uint8 *)&JSreturn)+(x<<1);break;
187      case SI_ARKANOID:InputDPtr=MouseData;t|=1;break;
188      case SI_ZAPPER:InputDPtr=MouseData;
189                                 t|=1;
190                                 attrib=1;
191                                 break;
192     }
193     FCEUI_SetInput(x,InputType[x],InputDPtr,attrib);
194    }
195
196    attrib=0;
197    InputDPtr=0;
198    switch(InputTypeFC)
199    {
200     case SIFC_SHADOW:InputDPtr=MouseData;t|=1;attrib=1;break;
201     case SIFC_ARKANOID:InputDPtr=MouseData;t|=1;break;
202     case SIFC_FKB:InputDPtr=fkbkeys;break;
203    }
204
205    FCEUI_SetInputFC(InputTypeFC,InputDPtr,attrib);
206    FCEUI_DisableFourScore(eoptions&EO_NOFOURSCORE);
207
208    if(t && !(inited&16))
209    {
210     InitMouse();
211     inited|=16;
212    }
213 }