| 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 |
{ Cross-platform joystick and gamepad handling. } |
| 21 |
unit CastleJoysticks; |
| 22 |
|
| 23 |
{$I castleconf.inc} |
| 24 |
|
| 25 |
interface |
| 26 |
|
| 27 |
uses Generics.Collections, Classes, |
| 28 |
CastleVectors, CastleUtils; |
| 29 |
|
| 30 |
type |
| 31 |
PJoyInfo = ^TJoyInfo; |
| 32 |
{ Joystick information. |
| 33 |
TODO: Deprecate at some point, in favor of simpler joystick API like @link(TJoystick.Axis). } |
| 34 |
TJoyInfo = record |
| 35 |
Name : String; |
| 36 |
Count : record |
| 37 |
Axes : Integer; |
| 38 |
Buttons : Integer; |
| 39 |
end; |
| 40 |
Caps : UInt32; |
| 41 |
end; |
| 42 |
|
| 43 |
PJoyState = ^TJoyState; |
| 44 |
{ Joystick state. |
| 45 |
TODO: Deprecate at some point, in favor of simpler joystick API like @link(TJoystick.Axis). } |
| 46 |
TJoyState = record |
| 47 |
Axis : array[ 0..7 ] of Single; |
| 48 |
BtnUp : array[ 0..31 ] of Boolean; |
| 49 |
BtnDown : array[ 0..31 ] of Boolean; |
| 50 |
BtnPress : array[ 0..31 ] of Boolean; |
| 51 |
BtnCanPress : array[ 0..31 ] of Boolean; |
| 52 |
end; |
| 53 |
|
| 54 |
{ Properties of a given joystick, use by accessing @link(TJoysticks.Items Joysticks[Index]). |
| 55 |
Do not construct instances of this yourself, TJoysticks creates |
| 56 |
this automatically when necessary. } |
| 57 |
TJoystick = class |
| 58 |
public |
| 59 |
{ Implementation-specific information. } |
| 60 |
InternalBackendInfo: TObject; |
| 61 |
{ Information. |
| 62 |
TODO: Deprecate at some point, in favor of simpler joystick API like @link(TJoystick.Axis). } |
| 63 |
Info : TJoyInfo; |
| 64 |
{ State. |
| 65 |
TODO: Deprecate at some point, in favor of simpler joystick API like @link(TJoystick.Axis). } |
| 66 |
State : TJoyState; |
| 67 |
|
| 68 |
function Axis: TVector2; |
| 69 |
destructor Destroy; override; |
| 70 |
end; |
| 71 |
|
| 72 |
PJoy = TJoystick deprecated 'use TJoystick'; |
| 73 |
|
| 74 |
TJoystickList = {$ifdef FPC}specialize{$endif} TObjectList<TJoystick>; |
| 75 |
|
| 76 |
const |
| 77 |
{ TODO: Deprecate these constants at some point, in favor of simpler joystick API like @link(TJoystick.Axis). } |
| 78 |
|
| 79 |
JOY_HAS_Z = $000001; |
| 80 |
JOY_HAS_R = $000002; |
| 81 |
JOY_HAS_U = $000004; |
| 82 |
JOY_HAS_V = $000008; |
| 83 |
JOY_HAS_POV = $000010; |
| 84 |
|
| 85 |
JOY_AXIS_X = 0; |
| 86 |
JOY_AXIS_Y = 1; |
| 87 |
JOY_AXIS_Z = 2; |
| 88 |
JOY_AXIS_R = 3; |
| 89 |
JOY_AXIS_U = 4; |
| 90 |
JOY_AXIS_V = 5; |
| 91 |
JOY_POVX = 6; |
| 92 |
JOY_POVY = 7; |
| 93 |
|
| 94 |
type |
| 95 |
TJoysticks = class; |
| 96 |
|
| 97 |
{ Internal class to provide different implementations of joystick events reading. |
| 98 |
@exclude } |
| 99 |
TJoysticksBackend = class abstract |
| 100 |
{ Detect and add joysticks to given list. } |
| 101 |
procedure Initialize(const List: TJoystickList); virtual; abstract; |
| 102 |
{ Update state of joysticks on given list. } |
| 103 |
procedure Poll(const List: TJoystickList; |
| 104 |
const EventContainer: TJoysticks); virtual; abstract; |
| 105 |
end; |
| 106 |
|
| 107 |
{ Joystick axis move event. } |
| 108 |
TOnJoyAxisMove = procedure(const Joy: TJoystick; const Axis: Byte; const Value: Single) of object; |
| 109 |
{ Joystick button action event. Used on button press/up/down. } |
| 110 |
TOnJoyButtonEvent = procedure(const Joy: TJoystick; const Button: Byte) of object; |
| 111 |
|
| 112 |
{ TJoysticks is a class for joysticks and gamepads management } |
| 113 |
TJoysticks = class |
| 114 |
private |
| 115 |
FOnAxisMove: TOnJoyAxisMove; |
| 116 |
FOnButtonDown: TOnJoyButtonEvent; |
| 117 |
FOnButtonUp: TOnJoyButtonEvent; |
| 118 |
FOnButtonPress: TOnJoyButtonEvent; |
| 119 |
Backend: TJoysticksBackend; |
| 120 |
FList: TJoystickList; |
| 121 |
FInitialized: Boolean; |
| 122 |
FOnChange: TNotifyEvent; |
| 123 |
FOnDisconnect: TSimpleNotifyEvent; |
| 124 |
FOnConnect: TSimpleNotifyEvent; |
| 125 |
function GetItems(const Index: Integer): TJoystick; |
| 126 |
{ Get (creating if necessary) joystick's explicit backend. |
| 127 |
Always returns TExplicitJoystickBackend, but cannot be declared as such. } |
| 128 |
function ExplicitBackend: TJoysticksBackend; |
| 129 |
protected |
| 130 |
{ See OnChange. } |
| 131 |
procedure DoChange; |
| 132 |
public |
| 133 |
constructor Create; |
| 134 |
destructor Destroy; override; |
| 135 |
{ Check state of every connected joystick and run event procedures. |
| 136 |
This is internal, called automatically by CastleUIControls unit, |
| 137 |
user code does not need to call this. |
| 138 |
@exclude } |
| 139 |
procedure InternalPoll; |
| 140 |
{ @exclude } |
| 141 |
procedure Poll; deprecated 'do not call this, it is not necessary'; |
| 142 |
|
| 143 |
function GetInfo( JoyID : Byte ) : PJoyInfo; |
| 144 |
function AxisPos( JoyID, Axis : Byte ): Single; |
| 145 |
function Down( JoyID, Button : Byte ): Boolean; |
| 146 |
function Up( JoyID, Button : Byte ): Boolean; |
| 147 |
function Press( JoyID, Button : Byte ): Boolean; |
| 148 |
procedure ClearState; |
| 149 |
function GetJoy(const JoyID: Integer): TJoystick; deprecated 'use Joysticks[xxx] instead of Joysticks.GetJoy(xxx)'; |
| 150 |
|
| 151 |
property OnAxisMove: TOnJoyAxisMove read FOnAxisMove write FOnAxisMove; |
| 152 |
property OnButtonDown: TOnJoyButtonEvent read FOnButtonDown write FOnButtonDown; |
| 153 |
property OnButtonUp: TOnJoyButtonEvent read FOnButtonUp write FOnButtonUp; |
| 154 |
property OnButtonPress: TOnJoyButtonEvent read FOnButtonPress write FOnButtonPress; |
| 155 |
function JoyCount: Integer; deprecated 'use Count'; |
| 156 |
function Count: Integer; |
| 157 |
|
| 158 |
property Items[const Index: Integer]: TJoystick read GetItems; default; |
| 159 |
|
| 160 |
{ Detect connected joysticks. |
| 161 |
On some platforms, you need to call this to search for connected joysticks, |
| 162 |
otherwise you will always have zero joysticks. |
| 163 |
Calling this again is allowed, it searches for connected joysticks again. } |
| 164 |
procedure Initialize; |
| 165 |
|
| 166 |
property Initialized: Boolean read FInitialized; |
| 167 |
|
| 168 |
{ Used by CASTLE_WINDOW_LIBRARY when |
| 169 |
an external API notifies us about the joysticks state. |
| 170 |
@exclude } |
| 171 |
procedure InternalSetJoystickCount(const JoystickCount: Integer); |
| 172 |
{ @exclude } |
| 173 |
procedure InternalSetJoystickAxis(const JoystickIndex: Integer; const Axis: TVector2); |
| 174 |
|
| 175 |
{ Used by CastleWindow when |
| 176 |
an external API notifies us about connecting/disconnecting devices to the system. |
| 177 |
@exclude } |
| 178 |
procedure InternalConnected; |
| 179 |
{ @exclude } |
| 180 |
procedure InternalDisconnected; |
| 181 |
|
| 182 |
{ Called after TJoystick instances on the list change (some are added, destroyed). |
| 183 |
In case of some backends, this is only called at the end of @link(Initialize), |
| 184 |
but it may be called in other cases (e.g. "explicit" joystick backend, |
| 185 |
used by Nintendo Switch, may call this at any moment). } |
| 186 |
property OnChange: TNotifyEvent read FOnChange write FOnChange; |
| 187 |
|
| 188 |
{ Called in case a previously initalized joystick has been disconnected. } |
| 189 |
property OnDisconnect: TSimpleNotifyEvent read FOnDisconnect write FOnDisconnect; |
| 190 |
{ Called in case a joystick has been connected to the system. } |
| 191 |
property OnConnect: TSimpleNotifyEvent read FOnConnect write FOnConnect; |
| 192 |
end; |
| 193 |
|
| 194 |
{ Detect connected joysticks. } |
| 195 |
procedure EnableJoysticks; deprecated 'use Joysticks.Initialize'; |
| 196 |
|
| 197 |
{ Global joystick manager object. Singleton, automatically created when being accessed. |
| 198 |
Remember to call @link(TJoysticks.Initialize), this is necessary on some platforms |
| 199 |
to detect joysticks. } |
| 200 |
function Joysticks: TJoysticks; |
| 201 |
|
| 202 |
implementation |
| 203 |
|
| 204 |
uses SysUtils, Math, |
| 205 |
CastleLog, |
| 206 |
{$ifdef FPC} {$ifdef LINUX} CastleInternalJoysticksLinux, {$endif} {$endif} |
| 207 |
{$ifdef MSWINDOWS} CastleInternalJoysticksWindows, {$endif} |
| 208 |
CastleInternalJoysticksExplicit; |
| 209 |
|
| 210 |
{ TJoystick ------------------------------------------------------------------ } |
| 211 |
|
| 212 |
destructor TJoystick.Destroy; |
| 213 |
begin |
| 214 |
FreeAndNil(InternalBackendInfo); |
| 215 |
inherited; |
| 216 |
end; |
| 217 |
|
| 218 |
function TJoystick.Axis: TVector2; |
| 219 |
begin |
| 220 |
Result := Vector2( |
| 221 |
State.Axis[JOY_AXIS_X], |
| 222 |
State.Axis[JOY_AXIS_Y] |
| 223 |
); |
| 224 |
end; |
| 225 |
|
| 226 |
{ TJoysticks ----------------------------------------------------------------- } |
| 227 |
|
| 228 |
constructor TJoysticks.Create; |
| 229 |
begin |
| 230 |
inherited; |
| 231 |
FList := TJoystickList.Create(true); |
| 232 |
{$if defined(MSWINDOWS)} |
| 233 |
Backend := TWindowsJoysticksBackend.Create; |
| 234 |
{$elseif defined(LINUX) and defined(FPC)} |
| 235 |
// TODO: Delphi on Linux doesn't support joysticks now |
| 236 |
Backend := TLinuxJoysticksBackend.Create; |
| 237 |
{$else} |
| 238 |
// This way Backend is non-nil always |
| 239 |
Backend := TExplicitJoystickBackend.Create; |
| 240 |
{$endif} |
| 241 |
end; |
| 242 |
|
| 243 |
destructor TJoysticks.Destroy; |
| 244 |
begin |
| 245 |
FreeAndNil(FList); |
| 246 |
FreeAndNil(Backend); |
| 247 |
inherited; |
| 248 |
end; |
| 249 |
|
| 250 |
procedure TJoysticks.Initialize; |
| 251 |
begin |
| 252 |
FInitialized := true; |
| 253 |
|
| 254 |
{ In case of TExplicitJoystickBackend, |
| 255 |
do not clear the list and call Backend.Initialize. |
| 256 |
Instead leave existing joysticks (set by TExplicitJoystickBackend.SetJoystickCount). |
| 257 |
That's because Backend.Initialize doesn't have a way to get new joystick information |
| 258 |
(in case of TExplicitJoystickBackend, it is CGE that is informed by external code |
| 259 |
about joystick existence). } |
| 260 |
if Backend is TExplicitJoystickBackend then |
| 261 |
Exit; |
| 262 |
|
| 263 |
FList.Clear; |
| 264 |
Backend.Initialize(FList); |
| 265 |
ClearState; |
| 266 |
DoChange; |
| 267 |
end; |
| 268 |
|
| 269 |
procedure TJoysticks.Poll; |
| 270 |
begin |
| 271 |
InternalPoll; |
| 272 |
end; |
| 273 |
|
| 274 |
procedure TJoysticks.InternalPoll; |
| 275 |
begin |
| 276 |
if FInitialized then |
| 277 |
Backend.Poll(FList, Self); |
| 278 |
end; |
| 279 |
|
| 280 |
function TJoysticks.GetInfo(JoyID: Byte): PJoyInfo; |
| 281 |
begin |
| 282 |
Result := nil; |
| 283 |
if JoyID >= Count then Exit; |
| 284 |
|
| 285 |
Result := @(FList[JoyID].Info); |
| 286 |
end; |
| 287 |
|
| 288 |
function TJoysticks.AxisPos(JoyID, Axis: Byte): Single; |
| 289 |
begin |
| 290 |
Result := 0; |
| 291 |
if ( JoyID >= Count ) or ( Axis > JOY_POVY ) then Exit; |
| 292 |
|
| 293 |
Result := FList[JoyID].State.Axis[ Axis ]; |
| 294 |
end; |
| 295 |
|
| 296 |
function TJoysticks.Down(JoyID, Button: Byte): Boolean; |
| 297 |
begin |
| 298 |
Result := False; |
| 299 |
if ( JoyID >= Count ) or ( Button >= FList[JoyID].Info.Count.Buttons ) then Exit; |
| 300 |
|
| 301 |
Result := FList[JoyID].State.BtnDown[ Button ]; |
| 302 |
end; |
| 303 |
|
| 304 |
function TJoysticks.Up(JoyID, Button: Byte): Boolean; |
| 305 |
begin |
| 306 |
Result := False; |
| 307 |
if ( JoyID >= Count ) or ( Button >= FList[JoyID].Info.Count.Buttons ) then Exit; |
| 308 |
|
| 309 |
Result := FList[JoyID].State.BtnUp[ Button ]; |
| 310 |
end; |
| 311 |
|
| 312 |
function TJoysticks.Press(JoyID, Button: Byte): Boolean; |
| 313 |
begin |
| 314 |
Result := False; |
| 315 |
if ( JoyID >= Count ) or ( Button >= FList[JoyID].Info.Count.Buttons ) then Exit; |
| 316 |
|
| 317 |
Result := FList[JoyID].State.BtnPress[ Button ]; |
| 318 |
end; |
| 319 |
|
| 320 |
procedure TJoysticks.ClearState; |
| 321 |
var |
| 322 |
i, j : Integer; |
| 323 |
state : PJoyState; |
| 324 |
begin |
| 325 |
for i := 0 to Count - 1 do |
| 326 |
for j := 0 to FList[I].Info.Count.Buttons - 1 do |
| 327 |
begin |
| 328 |
state := @FList[I].State; |
| 329 |
state^.BtnUp[ j ] := False; |
| 330 |
state^.BtnDown[ j ] := False; |
| 331 |
state^.BtnPress[ j ] := False; |
| 332 |
state^.BtnCanPress[ j ] := True; |
| 333 |
end; |
| 334 |
end; |
| 335 |
|
| 336 |
function TJoysticks.GetJoy(const JoyID: Integer): TJoystick; |
| 337 |
begin |
| 338 |
Result := nil; |
| 339 |
if JoyID >= Count then Exit; |
| 340 |
Result := FList[JoyID]; |
| 341 |
end; |
| 342 |
|
| 343 |
function TJoysticks.ExplicitBackend: TJoysticksBackend; |
| 344 |
begin |
| 345 |
Assert(Backend <> nil); |
| 346 |
if not (Backend is TExplicitJoystickBackend) then |
| 347 |
begin |
| 348 |
FreeAndNil(Backend); |
| 349 |
Backend := TExplicitJoystickBackend.Create; |
| 350 |
{ Although TExplicitJoystickBackend.Initialize doesn't do anything for now, |
| 351 |
but call it, to make sure Initialized = true. } |
| 352 |
Initialize; |
| 353 |
end; |
| 354 |
Result := Backend; |
| 355 |
end; |
| 356 |
|
| 357 |
procedure TJoysticks.InternalSetJoystickCount(const JoystickCount: Integer); |
| 358 |
begin |
| 359 |
TExplicitJoystickBackend(ExplicitBackend).SetJoystickCount(FList, JoystickCount); |
| 360 |
DoChange; |
| 361 |
end; |
| 362 |
|
| 363 |
procedure TJoysticks.InternalSetJoystickAxis(const JoystickIndex: Integer; const Axis: TVector2); |
| 364 |
begin |
| 365 |
TExplicitJoystickBackend(ExplicitBackend).SetJoystickAxis(FList, JoystickIndex, Axis); |
| 366 |
end; |
| 367 |
|
| 368 |
procedure TJoysticks.InternalConnected; |
| 369 |
begin |
| 370 |
if Assigned(OnConnect) then |
| 371 |
OnConnect; |
| 372 |
end; |
| 373 |
|
| 374 |
procedure TJoysticks.InternalDisconnected; |
| 375 |
begin |
| 376 |
if Assigned(OnDisconnect) then |
| 377 |
OnDisconnect; |
| 378 |
end; |
| 379 |
|
| 380 |
function TJoysticks.GetItems(const Index: Integer): TJoystick; |
| 381 |
begin |
| 382 |
Result := FList[Index]; |
| 383 |
end; |
| 384 |
|
| 385 |
function TJoysticks.JoyCount: Integer; |
| 386 |
begin |
| 387 |
Result := FList.Count; |
| 388 |
end; |
| 389 |
|
| 390 |
function TJoysticks.Count: Integer; |
| 391 |
begin |
| 392 |
Result := FList.Count; |
| 393 |
end; |
| 394 |
|
| 395 |
procedure TJoysticks.DoChange; |
| 396 |
begin |
| 397 |
if Assigned(OnChange) then |
| 398 |
OnChange(Self); |
| 399 |
end; |
| 400 |
|
| 401 |
{ global --------------------------------------------------------------------- } |
| 402 |
|
| 403 |
procedure EnableJoysticks; |
| 404 |
begin |
| 405 |
Joysticks.Initialize; |
| 406 |
end; |
| 407 |
|
| 408 |
var |
| 409 |
FJoysticks: TJoysticks; |
| 410 |
|
| 411 |
function Joysticks: TJoysticks; |
| 412 |
begin |
| 413 |
if FJoysticks = nil then |
| 414 |
FJoysticks := TJoysticks.Create; |
| 415 |
Result := FJoysticks; |
| 416 |
end; |
| 417 |
|
| 418 |
{$ifndef FPC}initialization{$endif} |
| 419 |
|
| 420 |
finalization |
| 421 |
FreeAndNil(FJoysticks); |
| 422 |
end. |