reset behavior changed, Puggsy detection added
[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_TAB)) {\r
95     PicoReset();\r
96     sblobked = 1;\r
97   }\r
98   else if (!sblobked && GetAsyncKeyState(VK_ESCAPE))\r
99   {\r
100     PostMessage(FrameWnd, WM_COMMAND, 0x20000 | 1000, 0);\r
101     sblobked = 1;\r
102   }\r
103   else\r
104     sblobked = GetAsyncKeyState(VK_TAB)  | GetAsyncKeyState(VK_ESCAPE);\r
105   \r
106   return 0;\r
107 }\r
108 \r
109 #endif\r
110 \r
111 int InputInit()\r
112 {\r
113   memset(&Inp,0,sizeof(Inp));\r
114 #ifdef _XBOX\r
115   memset(&Pad,0,sizeof(Pad));\r
116   XInitDevices(0,NULL);\r
117 #endif\r
118   return 0;\r
119 }\r
120 \r
121 void InputExit()\r
122 {\r
123 #ifdef _XBOX\r
124   if (GamePad) XInputClose(GamePad);\r
125   GamePad=NULL;\r
126 #endif\r
127 }\r
128 \r
129 int InputUpdate()\r
130 {\r
131   int i=0;\r
132   int push=0x2000;\r
133 \r
134   DeviceRead(); // Read XBox or PC device \r
135 \r
136   // Use left analog for left digital too:\r
137   if (Inp.axis[1]>= push) Inp.button[0]|=0xff; // Up\r
138   if (Inp.axis[1]<=-push) Inp.button[1]|=0xff; // Down\r
139   if (Inp.axis[0]<=-push) Inp.button[2]|=0xff; // Left\r
140   if (Inp.axis[0]>= push) Inp.button[3]|=0xff; // Right\r
141 \r
142   // Update debounce/time held information:\r
143   for (i=0;i<sizeof(Inp.held);i++)\r
144   {\r
145     if (Inp.held[i]==0)\r
146     {\r
147       if (Inp.button[i]>30) Inp.held[i]=1; // Just pressed\r
148     }\r
149     else\r
150     {\r
151       // Is the button still being held down?\r
152       Inp.held[i]++;\r
153       if (Inp.held[i]>=0x80) Inp.held[i]&=0xbf; // (Keep looping around)\r
154 \r
155       if (Inp.button[i]<25) Inp.held[i]=0; // No\r
156     }\r
157   }\r
158 \r
159   // Work out some key repeat values:\r
160   for (i=0;i<sizeof(Inp.repeat);i++)\r
161   {\r
162     char rep=0;\r
163     int held=Inp.held[i];\r
164 \r
165     if (held==1) rep=1;\r
166     if (held>=0x20 && (held&1)) rep=1;\r
167 \r
168     Inp.repeat[i]=rep;\r
169   }\r
170 \r
171   return 0;\r
172 }\r
173 \r
174 // Set Lightgun calibration values:\r
175 int InputLightCal(int cx,int cy,int ux,int uy)\r
176 {\r
177 #ifdef _XBOX\r
178   XINPUT_LIGHTGUN_CALIBRATION_OFFSETS cal;\r
179 \r
180   memset(&cal,0,sizeof(cal));\r
181 \r
182   cal.wCenterX   =(WORD)cx;\r
183   cal.wCenterY   =(WORD)cy;\r
184   cal.wUpperLeftX=(WORD)ux;\r
185   cal.wUpperLeftY=(WORD)uy;\r
186   XInputSetLightgunCalibration(GamePad,&cal);\r
187 \r
188 #endif\r
189 \r
190   (void)(cx+cy+ux+uy);\r
191 \r
192   return 0;\r
193 }\r