tweaking win32 port
[picodrive.git] / platform / win32 / GenaDrive / Input.cpp
1 \r
2 #include "app.h"\r
3 #include <commdlg.h>\r
4 \r
5 struct Input Inp;\r
6 \r
7 // --------------------- XBox Input -----------------------------\r
8 #ifdef _XBOX\r
9 static HANDLE GamePad=NULL;\r
10 static XINPUT_STATE Pad;\r
11 \r
12 static int DeadZone(short *paxis)\r
13 {\r
14   int zone=0x2000;\r
15   int a=*paxis;\r
16 \r
17        if (a<-zone) a+=zone;\r
18   else if (a> zone) a-=zone; else a=0;\r
19 \r
20   *paxis=(short)a;\r
21   return 0;\r
22 }\r
23 \r
24 static int DeviceRead()\r
25 {\r
26   int but=0,a=0;\r
27 \r
28   memset(Inp.axis,  0,sizeof(Inp.axis));\r
29   memset(Inp.button,0,sizeof(Inp.button));\r
30 \r
31   if (GamePad==NULL) GamePad=XInputOpen(XDEVICE_TYPE_GAMEPAD,0,XDEVICE_NO_SLOT,NULL);\r
32   if (GamePad==NULL) return 1;\r
33 \r
34   // Read XBox joypad:\r
35   XInputGetState(GamePad,&Pad);\r
36 \r
37   // Get analog axes:\r
38   Inp.axis[0]=Pad.Gamepad.sThumbLX;\r
39   Inp.axis[1]=Pad.Gamepad.sThumbLY;\r
40   Inp.axis[2]=Pad.Gamepad.sThumbRX;\r
41   Inp.axis[3]=Pad.Gamepad.sThumbRY;\r
42 \r
43   for (a=0;a<4;a++) DeadZone(Inp.axis+a);\r
44 \r
45   // Get digital buttons:\r
46   but=Pad.Gamepad.wButtons;\r
47   for (a=0;a<8;a++)\r
48   {\r
49     if (but&(1<<a)) Inp.button[a]=0xff;\r
50   }\r
51 \r
52   // Get analog buttons:\r
53   memcpy(Inp.button+8, Pad.Gamepad.bAnalogButtons, 8);\r
54 \r
55   return 0;\r
56 }\r
57 \r
58 #endif\r
59 \r
60 // --------------------- Windows  Input -----------------------------\r
61 \r
62 #ifndef _XBOX\r
63 \r
64 static int DeviceRead()\r
65 {\r
66   int push=0x6000;\r
67   int axis[]={0,0,0,0};\r
68   int i=0;\r
69 \r
70   memset(Inp.axis,  0,sizeof(Inp.axis));\r
71   memset(Inp.button,0,sizeof(Inp.button));\r
72 \r
73   if (GetForegroundWindow()!=FrameWnd) return 1;\r
74 \r
75   if (GetAsyncKeyState(VK_LEFT )) axis[0]-=push;\r
76   if (GetAsyncKeyState(VK_RIGHT)) axis[0]+=push;\r
77   if (GetAsyncKeyState(VK_DOWN )) axis[1]-=push;\r
78   if (GetAsyncKeyState(VK_UP   )) axis[1]+=push;\r
79   for (i=0;i<4;i++) Inp.axis[i]=(short)axis[i];\r
80 \r
81   if (GetAsyncKeyState(VK_RETURN)) Inp.button[4]=0xff; // Start\r
82   //if (GetAsyncKeyState(VK_ESCAPE)) Inp.button[7]=0xff; // Right thumb\r
83 \r
84   if (GetAsyncKeyState('Z')) Inp.button[10]=0xff;\r
85   if (GetAsyncKeyState('X')) Inp.button[ 8]=0xff;\r
86   if (GetAsyncKeyState('C')) Inp.button[ 9]=0xff;\r
87 \r
88   if (GetAsyncKeyState('A')) Inp.button[13]=0xff;\r
89   if (GetAsyncKeyState('S')) Inp.button[12]=0xff;\r
90   if (GetAsyncKeyState('D')) Inp.button[11]=0xff;\r
91   if (GetAsyncKeyState('F')) Inp.button[14]=0xff;\r
92 \r
93   static int sblobked = 0;\r
94   if(!sblobked && GetAsyncKeyState(VK_F6)) {\r
95         FILE *PmovFile;\r
96     romname[strlen(romname)-3] = 0;\r
97     strcat(romname, "mds");\r
98         PmovFile = fopen(romname, "wb");\r
99         if(PmovFile) {\r
100           PmovState(5, PmovFile);\r
101           fclose(PmovFile);\r
102         }\r
103         sblobked = 1;\r
104   }\r
105   else if(!sblobked && GetAsyncKeyState(VK_F9)) {\r
106         FILE *PmovFile;\r
107     romname[strlen(romname)-3] = 0;\r
108     strcat(romname, "mds");\r
109         PmovFile = fopen(romname, "rb");\r
110         if(PmovFile) {\r
111           PmovState(6, PmovFile);\r
112           fclose(PmovFile);\r
113         }\r
114         sblobked = 1;\r
115   }\r
116   else if(!sblobked && GetAsyncKeyState(VK_TAB)) {\r
117         PicoReset(0);\r
118         sblobked = 1;\r
119   }\r
120   else if(!sblobked && GetAsyncKeyState(VK_ESCAPE))\r
121   {\r
122     unsigned char *rom_data;\r
123     unsigned int rom_size;\r
124     DSoundMute();\r
125     pm_file *rom = 0;\r
126     OPENFILENAME of; ZeroMemory(&of, sizeof(OPENFILENAME));\r
127     of.lStructSize = sizeof(OPENFILENAME);\r
128     of.lpstrFilter = "ROMs\0*.smd;*.bin;*.gen\0";\r
129     of.lpstrFile = romname; romname[0] = 0;\r
130     of.nMaxFile = MAX_PATH;\r
131     of.Flags = OFN_FILEMUSTEXIST|OFN_HIDEREADONLY;\r
132     GetOpenFileName(&of);\r
133     rom = pm_open(romname);\r
134     DSoundUnMute();\r
135     if(!rom) return 1;\r
136     PicoCartLoad(rom, &rom_data, &rom_size);\r
137     PicoCartInsert(rom_data, rom_size);\r
138     pm_close(rom);\r
139     sblobked = 1;\r
140   }\r
141   else\r
142     sblobked = GetAsyncKeyState(VK_F6) | GetAsyncKeyState(VK_F9) |\r
143                 GetAsyncKeyState(VK_TAB)  | GetAsyncKeyState(VK_ESCAPE);\r
144   \r
145   return 0;\r
146 }\r
147 \r
148 #endif\r
149 \r
150 int InputInit()\r
151 {\r
152   memset(&Inp,0,sizeof(Inp));\r
153 #ifdef _XBOX\r
154   memset(&Pad,0,sizeof(Pad));\r
155   XInitDevices(0,NULL);\r
156 #endif\r
157   return 0;\r
158 }\r
159 \r
160 void InputExit()\r
161 {\r
162 #ifdef _XBOX\r
163   if (GamePad) XInputClose(GamePad);\r
164   GamePad=NULL;\r
165 #endif\r
166 }\r
167 \r
168 int InputUpdate()\r
169 {\r
170   int i=0;\r
171   int push=0x2000;\r
172 \r
173   DeviceRead(); // Read XBox or PC device \r
174 \r
175   // Use left analog for left digital too:\r
176   if (Inp.axis[1]>= push) Inp.button[0]|=0xff; // Up\r
177   if (Inp.axis[1]<=-push) Inp.button[1]|=0xff; // Down\r
178   if (Inp.axis[0]<=-push) Inp.button[2]|=0xff; // Left\r
179   if (Inp.axis[0]>= push) Inp.button[3]|=0xff; // Right\r
180 \r
181   // Update debounce/time held information:\r
182   for (i=0;i<sizeof(Inp.held);i++)\r
183   {\r
184     if (Inp.held[i]==0)\r
185     {\r
186       if (Inp.button[i]>30) Inp.held[i]=1; // Just pressed\r
187     }\r
188     else\r
189     {\r
190       // Is the button still being held down?\r
191       Inp.held[i]++;\r
192       if (Inp.held[i]>=0x80) Inp.held[i]&=0xbf; // (Keep looping around)\r
193 \r
194       if (Inp.button[i]<25) Inp.held[i]=0; // No\r
195     }\r
196   }\r
197 \r
198   // Work out some key repeat values:\r
199   for (i=0;i<sizeof(Inp.repeat);i++)\r
200   {\r
201     char rep=0;\r
202     int held=Inp.held[i];\r
203 \r
204     if (held==1) rep=1;\r
205     if (held>=0x20 && (held&1)) rep=1;\r
206 \r
207     Inp.repeat[i]=rep;\r
208   }\r
209 \r
210   return 0;\r
211 }\r
212 \r
213 // Set Lightgun calibration values:\r
214 int InputLightCal(int cx,int cy,int ux,int uy)\r
215 {\r
216 #ifdef _XBOX\r
217   XINPUT_LIGHTGUN_CALIBRATION_OFFSETS cal;\r
218 \r
219   memset(&cal,0,sizeof(cal));\r
220 \r
221   cal.wCenterX   =(WORD)cx;\r
222   cal.wCenterY   =(WORD)cy;\r
223   cal.wUpperLeftX=(WORD)ux;\r
224   cal.wUpperLeftY=(WORD)uy;\r
225   XInputSetLightgunCalibration(GamePad,&cal);\r
226 \r
227 #endif\r
228 \r
229   (void)(cx+cy+ux+uy);\r
230 \r
231   return 0;\r
232 }\r