| 1 |
{ |
| 2 |
Copyright 2015-2019 Tomasz Wojtyś, Michalis Kamburelis. |
| 3 |
Based on zgl_joystick.pas by Andrey Kemka. |
| 4 |
|
| 5 |
This file is part of "Castle Game Engine". |
| 6 |
|
| 7 |
"Castle Game Engine" is free software; see the file COPYING.txt, |
| 8 |
included in this distribution, for details about the copyright. |
| 9 |
This file is based on ZenGL, with "zlib" license |
| 10 |
( http://www.zengl.org/license.html ) which is fully compatible with |
| 11 |
Castle Game Engine "LGPL with static linking exception" / GPL licensing. |
| 12 |
|
| 13 |
"Castle Game Engine" is distributed in the hope that it will be useful, |
| 14 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 16 |
|
| 17 |
---------------------------------------------------------------------------- |
| 18 |
} |
| 19 |
|
| 20 |
{ Windows Joystick support. } |
| 21 |
unit CastleInternalJoysticksWindows; |
| 22 |
|
| 23 |
// TODO: only partially works on Windows (no support of new features); need to be ported to xinput library |
| 24 |
|
| 25 |
interface |
| 26 |
|
| 27 |
{$ifndef MSWINDOWS} |
| 28 |
{ To make things easier for packages, this unit is part of Delphi |
| 29 |
castle_engine.bpl package, and so it has to compile on any platform |
| 30 |
(including Linux), but then it just doesn't do anything. |
| 31 |
From code, just don't use this unit when not MSWINDOWS. } |
| 32 |
implementation |
| 33 |
{$else} |
| 34 |
|
| 35 |
uses CastleJoysticks; |
| 36 |
|
| 37 |
type |
| 38 |
PJOYCAPSW = ^TJOYCAPSW; |
| 39 |
TJOYCAPSW = packed record |
| 40 |
wMid: Word; |
| 41 |
wPid: Word; |
| 42 |
szPname: array[ 0..31 ] of WideChar; |
| 43 |
wXmin: UInt32; |
| 44 |
wXmax: UInt32; |
| 45 |
wYmin: UInt32; |
| 46 |
wYmax: UInt32; |
| 47 |
wZmin: UInt32; |
| 48 |
wZmax: UInt32; |
| 49 |
wNumButtons: UInt32; |
| 50 |
wPeriodMin: UInt32; |
| 51 |
wPeriodMax: UInt32; |
| 52 |
wRmin: UInt32; |
| 53 |
wRmax: UInt32; |
| 54 |
wUmin: UInt32; |
| 55 |
wUmax: UInt32; |
| 56 |
wVmin: UInt32; |
| 57 |
wVmax: UInt32; |
| 58 |
wCaps: UInt32; |
| 59 |
wMaxAxes: UInt32; |
| 60 |
wNumAxes: UInt32; |
| 61 |
wMaxButtons: UInt32; |
| 62 |
szRegKey: array[ 0..31 ] of WideChar; |
| 63 |
szOEMVxD: array[ 0..259 ] of WideChar; |
| 64 |
end; |
| 65 |
|
| 66 |
type |
| 67 |
PJOYINFOEX = ^TJOYINFOEX; |
| 68 |
TJOYINFOEX = packed record |
| 69 |
dwSize: UInt32; |
| 70 |
dwFlags: UInt32; |
| 71 |
wXpos: UInt32; |
| 72 |
wYpos: UInt32; |
| 73 |
wZpos: UInt32; |
| 74 |
dwRpos: UInt32; |
| 75 |
dwUpos: UInt32; |
| 76 |
dwVpos: UInt32; |
| 77 |
wButtons: UInt32; |
| 78 |
dwButtonNumber: UInt32; |
| 79 |
dwPOV: UInt32; |
| 80 |
dwReserved1: UInt32; |
| 81 |
dwReserved2: UInt32; |
| 82 |
end; |
| 83 |
|
| 84 |
const |
| 85 |
JOY_POVCENTERED = -1; |
| 86 |
JOY_POVFORWARD = 0; |
| 87 |
JOY_POVRIGHT = 9000; |
| 88 |
JOY_POVBACKWARD = 18000; |
| 89 |
JOY_POVLEFT = 27000; |
| 90 |
JOY_RETURNX = 1; |
| 91 |
JOY_RETURNY = 2; |
| 92 |
JOY_RETURNZ = 4; |
| 93 |
JOY_RETURNR = 8; |
| 94 |
JOY_RETURNU = 16; |
| 95 |
JOY_RETURNV = 32; |
| 96 |
JOY_RETURNPOV = 64; |
| 97 |
JOY_RETURNBUTTONS = 128; |
| 98 |
JOY_RETURNRAWDATA = 256; |
| 99 |
JOY_RETURNPOVCTS = 512; |
| 100 |
JOY_RETURNCENTERED = $400; |
| 101 |
JOY_USEDEADZONE = $800; |
| 102 |
JOY_RETURNALL = ( JOY_RETURNX or JOY_RETURNY or JOY_RETURNZ or JOY_RETURNR or JOY_RETURNU or JOY_RETURNV or JOY_RETURNPOV or JOY_RETURNBUTTONS ); |
| 103 |
|
| 104 |
JOYCAPS_HASZ = 1; |
| 105 |
JOYCAPS_HASR = 2; |
| 106 |
JOYCAPS_HASU = 4; |
| 107 |
JOYCAPS_HASV = 8; |
| 108 |
JOYCAPS_HASPOV = 16; |
| 109 |
JOYCAPS_POV4DIR = 32; |
| 110 |
JOYCAPS_POVCTS = 64; |
| 111 |
|
| 112 |
JOYERR_NOERROR = 0; |
| 113 |
JOYERR_BASE = 160; |
| 114 |
JOYERR_PARMS = JOYERR_BASE + 5; // The specified joystick identifier is invalid. |
| 115 |
JOYERR_UNPLUGGED = JOYERR_BASE + 7; //The specified joystick is not connected to the system. |
| 116 |
|
| 117 |
WINMMLIB = 'winmm.dll'; |
| 118 |
|
| 119 |
JS_AXIS : array[ 0..5 ] of UInt32 = ( 17 {X}, 19 {Y}, 21 {Z}, 26 {R}, 28 {U}, 30 {V} ); |
| 120 |
|
| 121 |
function joyGetNumDevs : UInt32; stdcall; external WINMMLIB name 'joyGetNumDevs'; |
| 122 |
function joyGetDevCapsW( uJoyID : UInt32; lpCaps : PJOYCAPSW; uSize : UInt32 ) : UInt32; stdcall; external WINMMLIB name 'joyGetDevCapsW'; |
| 123 |
function joyGetPosEx( uJoyID : UInt32; lpInfo : PJOYINFOEX ) : UInt32; stdcall; external WINMMLIB name 'joyGetPosEx'; |
| 124 |
|
| 125 |
type |
| 126 |
TWindowsJoystickBackendInfo = class |
| 127 |
Caps : TJOYCAPSW; |
| 128 |
AxesMap : array[ 0..5 ] of Byte; |
| 129 |
end; |
| 130 |
|
| 131 |
TWindowsJoysticksBackend = class(TJoysticksBackend) |
| 132 |
procedure Initialize(const List: TJoystickList); override; |
| 133 |
procedure Poll(const List: TJoystickList; |
| 134 |
const EventContainer: TJoysticks); override; |
| 135 |
end; |
| 136 |
|
| 137 |
implementation |
| 138 |
|
| 139 |
uses |
| 140 |
SysUtils, Math, |
| 141 |
CastleLog, |
| 142 |
{ Needed to have PUInt32 defined for Delphi 10.2 } |
| 143 |
CastleUtils; |
| 144 |
|
| 145 |
procedure TWindowsJoysticksBackend.Initialize(const List: TJoystickList); |
| 146 |
var |
| 147 |
i, j : Integer; |
| 148 |
axis : Integer; |
| 149 |
caps : PUInt32; |
| 150 |
NewJoystick: TJoystick; |
| 151 |
NewBackendInfo: TWindowsJoystickBackendInfo; |
| 152 |
|
| 153 |
state : TJOYINFOEX; |
| 154 |
JoyError: UInt32; |
| 155 |
|
| 156 |
JoyCapsResult: UInt32; |
| 157 |
begin |
| 158 |
j := joyGetNumDevs(); |
| 159 |
for i := 0 to j - 1 do |
| 160 |
begin |
| 161 |
NewJoystick := TJoystick.Create; |
| 162 |
NewBackendInfo := TWindowsJoystickBackendInfo.Create; |
| 163 |
NewJoystick.InternalBackendInfo := NewBackendInfo; |
| 164 |
|
| 165 |
JoyCapsResult := joyGetDevCapsW( i, @NewBackendInfo.Caps, SizeOf( TJOYCAPSW ) ); |
| 166 |
{ Below we try to counter the WinAPI bug, when in case the application was run |
| 167 |
with no joysticks connected and this is the first call to joyGetDevCapsW |
| 168 |
after the joystick has been connected, the joyGetDevCapsW returns JOYERR_PARAMS = 165 |
| 169 |
which is also returned for disconnected/unavailable joysticks. |
| 170 |
Here we just call joyGetDevCapsW the second time to get the new value. } |
| 171 |
if JoyCapsResult <> 0 then |
| 172 |
JoyCapsResult := joyGetDevCapsW( i, @NewBackendInfo.Caps, SizeOf( TJOYCAPSW ) ); |
| 173 |
|
| 174 |
if JoyCapsResult = 0 then |
| 175 |
begin |
| 176 |
NewJoystick.Info.Name := NewBackendInfo.Caps.szPname; |
| 177 |
NewJoystick.Info.Count.Axes := NewBackendInfo.Caps.wNumAxes; |
| 178 |
NewJoystick.Info.Count.Buttons := NewBackendInfo.Caps.wNumButtons; |
| 179 |
|
| 180 |
caps := @NewJoystick.Info.Caps; |
| 181 |
NewBackendInfo.AxesMap[ 0 ] := JOY_AXIS_X; |
| 182 |
NewBackendInfo.AxesMap[ 1 ] := JOY_AXIS_Y; |
| 183 |
axis := 2; |
| 184 |
if NewBackendInfo.Caps.wCaps and JOYCAPS_HASZ > 0 then |
| 185 |
begin |
| 186 |
caps^ := caps^ or JOY_HAS_Z; |
| 187 |
NewBackendInfo.AxesMap[ axis ] := JOY_AXIS_Z; |
| 188 |
Inc( axis ); |
| 189 |
end; |
| 190 |
if NewBackendInfo.Caps.wCaps and JOYCAPS_HASR > 0 then |
| 191 |
begin |
| 192 |
caps^ := caps^ or JOY_HAS_R; |
| 193 |
NewBackendInfo.AxesMap[ axis ] := JOY_AXIS_R; |
| 194 |
Inc( axis ); |
| 195 |
end; |
| 196 |
if NewBackendInfo.Caps.wCaps and JOYCAPS_HASU > 0 then |
| 197 |
begin |
| 198 |
caps^ := caps^ or JOY_HAS_U; |
| 199 |
NewBackendInfo.AxesMap[ axis ] := JOY_AXIS_U; |
| 200 |
Inc( axis ); |
| 201 |
end; |
| 202 |
if NewBackendInfo.Caps.wCaps and JOYCAPS_HASV > 0 then |
| 203 |
begin |
| 204 |
caps^ := caps^ or JOY_HAS_V; |
| 205 |
NewBackendInfo.AxesMap[ axis ] := JOY_AXIS_V; |
| 206 |
//Inc( axis ); |
| 207 |
end; |
| 208 |
if NewBackendInfo.Caps.wCaps and JOYCAPS_HASPOV > 0 then |
| 209 |
begin |
| 210 |
caps^ := caps^ or JOY_HAS_POV; |
| 211 |
Inc( NewJoystick.Info.Count.Axes, 2 ); |
| 212 |
end; |
| 213 |
|
| 214 |
//workaround Windows reporting recently disconnected joysticks as connected |
| 215 |
state.dwSize := SizeOf( TJOYINFOEX ); |
| 216 |
state.dwFlags := JOY_RETURNALL or JOY_USEDEADZONE; |
| 217 |
if NewBackendInfo.Caps.wCaps and JOYCAPS_POVCTS > 0 then |
| 218 |
state.dwFlags := state.dwFlags or JOY_RETURNPOVCTS; |
| 219 |
JoyError := joyGetPosEx( i, @state ); |
| 220 |
//if no errors, then add this joystick |
| 221 |
if JoyError = JOYERR_NOERROR then |
| 222 |
begin |
| 223 |
WriteLnLog('CastleJoysticks Init', 'Find joy: %s (ID: %d); Axes: %d; Buttons: %d', |
| 224 |
[NewJoystick.Info.Name, i, NewJoystick.Info.Count.Axes, NewJoystick.Info.Count.Buttons]); |
| 225 |
|
| 226 |
List.Add(NewJoystick); |
| 227 |
end else |
| 228 |
begin |
| 229 |
if JoyError = JOYERR_UNPLUGGED then |
| 230 |
WriteLnLog('CastleJoysticks Init', 'Found joy: %s, but it will not be added because it seems to have been disconnected from the system recently (JOYERR_UNPLUGGED).', |
| 231 |
[NewJoystick.Info.Name]) |
| 232 |
else |
| 233 |
WriteLnWarning('CastleJoysticks Init', 'Found joy: %s, but it cannot be added due to an error %d.', |
| 234 |
[NewJoystick.Info.Name, JoyError]); |
| 235 |
FreeAndNil(NewJoystick); |
| 236 |
end; |
| 237 |
end else |
| 238 |
FreeAndNil(NewJoystick); |
| 239 |
end; |
| 240 |
end; |
| 241 |
|
| 242 |
procedure TWindowsJoysticksBackend.Poll(const List: TJoystickList; |
| 243 |
const EventContainer: TJoysticks); |
| 244 |
var |
| 245 |
i : Integer; |
| 246 |
_value: Single; |
| 247 |
j, a : Integer; |
| 248 |
btn : Integer; |
| 249 |
state : TJOYINFOEX; |
| 250 |
pcaps : PUInt32; |
| 251 |
value : PUInt32; |
| 252 |
vMin : UInt32; |
| 253 |
vMax : UInt32; |
| 254 |
Joystick: TJoystick; |
| 255 |
BackendInfo: TWindowsJoystickBackendInfo; |
| 256 |
JoyError: UInt32; |
| 257 |
JoystickHasBeenDisconnected: Boolean; |
| 258 |
begin |
| 259 |
JoystickHasBeenDisconnected := false; |
| 260 |
state.dwSize := SizeOf( TJOYINFOEX ); |
| 261 |
for I := 0 to List.Count - 1 do |
| 262 |
begin |
| 263 |
Joystick := List[I]; |
| 264 |
BackendInfo := Joystick.InternalBackendInfo as TWindowsJoystickBackendInfo; |
| 265 |
|
| 266 |
state.dwFlags := JOY_RETURNALL or JOY_USEDEADZONE; |
| 267 |
if BackendInfo.Caps.wCaps and JOYCAPS_POVCTS > 0 then |
| 268 |
state.dwFlags := state.dwFlags or JOY_RETURNPOVCTS; |
| 269 |
|
| 270 |
JoyError := joyGetPosEx( i, @state ); |
| 271 |
case JoyError of |
| 272 |
JOYERR_NOERROR: |
| 273 |
begin |
| 274 |
for j := 0 to Joystick.Info.Count.Axes - 1 do |
| 275 |
begin |
| 276 |
//stop if joystick reported more axes than the backend can handle |
| 277 |
if j > High(BackendInfo.AxesMap) then |
| 278 |
Break; |
| 279 |
|
| 280 |
// Say "no" to if's, and do everything trciky :) |
| 281 |
a := BackendInfo.AxesMap[ j ]; |
| 282 |
pcaps := @BackendInfo.Caps; |
| 283 |
Inc( pcaps, JS_AXIS[ a ] ); |
| 284 |
vMin := pcaps^; |
| 285 |
Inc( pcaps ); |
| 286 |
vMax := pcaps^; |
| 287 |
value := @state; |
| 288 |
Inc( value, 2 + a ); |
| 289 |
|
| 290 |
_value := value^ / ( vMax - vMin ) * 2 - 1; |
| 291 |
{ Y axis should be 1 when pointing up, -1 when pointing down. |
| 292 |
This is consistent with CGE 2D coordinate system |
| 293 |
(and standard math 2D coordinate system). } |
| 294 |
if J = JOY_AXIS_Y then |
| 295 |
_value := -_value; |
| 296 |
|
| 297 |
if Joystick.State.Axis[ a ] <> _value then |
| 298 |
if Assigned(EventContainer.OnAxisMove) then |
| 299 |
EventContainer.OnAxisMove(Joystick, j, _value); |
| 300 |
Joystick.State.Axis[ a ] := _value; |
| 301 |
end; |
| 302 |
|
| 303 |
FillChar( Joystick.State.Axis[ JOY_POVX ], 8, 0 ); |
| 304 |
if ( Joystick.Info.Caps and JOY_HAS_POV > 0 ) and ( state.dwPOV and $FFFF <> $FFFF ) then |
| 305 |
begin |
| 306 |
_value := Sin( DegToRad(state.dwPOV and $FFFF / 100.0) ); |
| 307 |
if Joystick.State.Axis[ JOY_POVX ] <> _value then |
| 308 |
if Assigned(EventContainer.OnAxisMove) then |
| 309 |
EventContainer.OnAxisMove(Joystick, JOY_POVX, _value); |
| 310 |
Joystick.State.Axis[ JOY_POVX ] := _value; |
| 311 |
|
| 312 |
_value := -Cos( DegToRad(state.dwPOV and $FFFF / 100.0 ) ); |
| 313 |
//_value := -_value; |
| 314 |
if Joystick.State.Axis[ JOY_POVY ] <> _value then |
| 315 |
if Assigned(EventContainer.OnAxisMove) then |
| 316 |
EventContainer.OnAxisMove(Joystick, JOY_POVY, _value); |
| 317 |
Joystick.State.Axis[ JOY_POVY ] := _value; |
| 318 |
end; |
| 319 |
|
| 320 |
for j := 0 to Joystick.Info.Count.Buttons - 1 do |
| 321 |
begin |
| 322 |
btn := state.wButtons and ( 1 shl j ); |
| 323 |
if ( Joystick.State.BtnDown[ j ] ) and ( btn = 0 ) then |
| 324 |
begin |
| 325 |
Joystick.State.BtnPress[ j ] := False; |
| 326 |
if Assigned(EventContainer.OnButtonUp) then EventContainer.OnButtonUp(Joystick, j); |
| 327 |
Joystick.State.BtnCanPress[ j ] := True; |
| 328 |
end; |
| 329 |
|
| 330 |
if ( Joystick.State.BtnCanPress[ j ] ) and ( not Joystick.State.BtnDown[ j ] ) and ( btn <> 0 ) then |
| 331 |
begin |
| 332 |
Joystick.State.BtnPress [ j ] := True; |
| 333 |
if Assigned(EventContainer.OnButtonPress) then EventContainer.OnButtonPress(Joystick, j); |
| 334 |
Joystick.State.BtnCanPress[ j ] := False; |
| 335 |
end; |
| 336 |
Joystick.State.BtnDown[ j ] := btn <> 0; |
| 337 |
Joystick.State.BtnUp[ j ] := btn = 0; |
| 338 |
if Assigned(EventContainer.OnButtonDown) and (btn <> 0) then EventContainer.OnButtonDown(Joystick, j); |
| 339 |
end; |
| 340 |
end; |
| 341 |
JOYERR_UNPLUGGED: |
| 342 |
begin |
| 343 |
WritelnLog('Joystick %s was disconnected', [Joystick.Info.Name]); |
| 344 |
JoystickHasBeenDisconnected := true; |
| 345 |
end; |
| 346 |
JOYERR_PARMS: |
| 347 |
WriteLnWarning('Joystick %s parameters are no longer valid.', [Joystick.Info.Name]); |
| 348 |
else |
| 349 |
WriteLnWarning('Joystick %s error %d', [Joystick.Info.Name, JoyError]); |
| 350 |
end; |
| 351 |
if JoystickHasBeenDisconnected then |
| 352 |
Joysticks.InternalDisconnected; |
| 353 |
end; |
| 354 |
end; |
| 355 |
|
| 356 |
{$endif} |
| 357 |
|
| 358 |
end. |