added files to rel
[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
130 FILE *rom = 0;\r
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
138 rom = fopen(romname, "rb");\r
139 DSoundUnMute();\r
140 if(!rom) return 1;\r
141 PicoCartLoad(rom, &rom_data, &rom_size);\r
142 PicoCartInsert(rom_data, rom_size);\r
143 fclose(rom);\r
144 sblobked = 1;\r
145 }\r
146 else if(!sblobked && GetAsyncKeyState(VK_BACK)) {\r
147 if(frameStep) frameStep=0;\r
148 else fastForward^=1;\r
149 sblobked = 1;\r
150 }\r
151 else if(!sblobked && GetAsyncKeyState(VK_OEM_5)) {\r
152 frameStep=3;\r
153 sblobked = 1;\r
154 }\r
155 else\r
156 sblobked = GetAsyncKeyState(VK_F6) | GetAsyncKeyState(VK_F9) |\r
157 GetAsyncKeyState(VK_TAB) | GetAsyncKeyState(VK_ESCAPE) |\r
158 GetAsyncKeyState(VK_BACK) | GetAsyncKeyState(VK_OEM_5);\r
159 \r
160 return 0;\r
161}\r
162\r
163#endif\r
164\r
165int InputInit()\r
166{\r
167 memset(&Inp,0,sizeof(Inp));\r
168#ifdef _XBOX\r
169 memset(&Pad,0,sizeof(Pad));\r
170 XInitDevices(0,NULL);\r
171#endif\r
172 return 0;\r
173}\r
174\r
175void InputExit()\r
176{\r
177#ifdef _XBOX\r
178 if (GamePad) XInputClose(GamePad);\r
179 GamePad=NULL;\r
180#endif\r
181}\r
182\r
183int InputUpdate()\r
184{\r
185 int i=0;\r
186 int push=0x2000;\r
187\r
188 DeviceRead(); // Read XBox or PC device \r
189\r
190 // Use left analog for left digital too:\r
191 if (Inp.axis[1]>= push) Inp.button[0]|=0xff; // Up\r
192 if (Inp.axis[1]<=-push) Inp.button[1]|=0xff; // Down\r
193 if (Inp.axis[0]<=-push) Inp.button[2]|=0xff; // Left\r
194 if (Inp.axis[0]>= push) Inp.button[3]|=0xff; // Right\r
195\r
196 // Update debounce/time held information:\r
197 for (i=0;i<sizeof(Inp.held);i++)\r
198 {\r
199 if (Inp.held[i]==0)\r
200 {\r
201 if (Inp.button[i]>30) Inp.held[i]=1; // Just pressed\r
202 }\r
203 else\r
204 {\r
205 // Is the button still being held down?\r
206 Inp.held[i]++;\r
207 if (Inp.held[i]>=0x80) Inp.held[i]&=0xbf; // (Keep looping around)\r
208\r
209 if (Inp.button[i]<25) Inp.held[i]=0; // No\r
210 }\r
211 }\r
212\r
213 // Work out some key repeat values:\r
214 for (i=0;i<sizeof(Inp.repeat);i++)\r
215 {\r
216 char rep=0;\r
217 int held=Inp.held[i];\r
218\r
219 if (held==1) rep=1;\r
220 if (held>=0x20 && (held&1)) rep=1;\r
221\r
222 Inp.repeat[i]=rep;\r
223 }\r
224\r
225 return 0;\r
226}\r
227\r
228// Set Lightgun calibration values:\r
229int InputLightCal(int cx,int cy,int ux,int uy)\r
230{\r
231#ifdef _XBOX\r
232 XINPUT_LIGHTGUN_CALIBRATION_OFFSETS cal;\r
233\r
234 memset(&cal,0,sizeof(cal));\r
235\r
236 cal.wCenterX =(WORD)cx;\r
237 cal.wCenterY =(WORD)cy;\r
238 cal.wUpperLeftX=(WORD)ux;\r
239 cal.wUpperLeftY=(WORD)uy;\r
240 XInputSetLightgunCalibration(GamePad,&cal);\r
241\r
242#endif\r
243\r
244 (void)(cx+cy+ux+uy);\r
245\r
246 return 0;\r
247}\r