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