Debian Codegraph @

src/ui/castleinternaljoystickslinux.pas

raw
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 { Linux Joystick support. }
21 unit CastleInternalJoysticksLinux;
22
23 // TODO: drop legacy interface /dev/js#; enable /dev/event#
24
25 interface
26
27 uses BaseUnix,
28 CastleJoysticks;
29
30 type
31 TLinuxJsEvent = record
32 time : UInt32; // event timestamp in milliseconds
33 value : SmallInt; // value
34 EventType : Byte; // event type
35 number : Byte; // axis/button number
36 end;
37
38 const
39 ABS_MAX = $3F;
40
41 JS_EVENT_BUTTON = $01; // button pressed/released
42 JS_EVENT_AXIS = $02; // joystick moved
43 JS_EVENT_INIT = $80; // initial state of device
44
45 JSIOCGNAME = -2142213613;
46 JSIOCGAXMAP = -2143262158;
47 JSIOCGAXES = -2147390959;
48 JSIOCGBUTTONS = -2147390958;
49
50 EAGAIN = 11; //Error: "there is no data available right now, try again later"
51
52 JS_AXIS : array[ 0..17 ] of Byte = ( JOY_AXIS_X, JOY_AXIS_Y, JOY_AXIS_Z, JOY_AXIS_U, JOY_AXIS_V, JOY_AXIS_R, JOY_AXIS_Z, JOY_AXIS_R, 0, 0, 0, 0, 0, 0, 0, 0, JOY_POVX, JOY_POVY );
53
54 type
55 TLinuxJoystickBackendInfo = class
56 DeviceInitialized: Boolean;
57 Device : LongInt;
58 AxesMap : array[ 0..ABS_MAX - 1 ] of Byte;
59 destructor Destroy; override;
60 end;
61
62 TLinuxJoysticksBackend = class(TJoysticksBackend)
63 procedure Initialize(const List: TJoystickList); override;
64 procedure Poll(const List: TJoystickList;
65 const EventContainer: TJoysticks); override;
66 end;
67
68 implementation
69
70 uses
71 SysUtils, CastleLog, Math;
72
73 { TLinuxJoystickBackendInfo -------------------------------------------------- }
74
75 destructor TLinuxJoystickBackendInfo.Destroy;
76 begin
77 { Check for DeviceInitialized, since any Device >= 0 is valid in theory. }
78 if DeviceInitialized then
79 FpClose(Device);
80 inherited;
81 end;
82
83 { TLinuxJoysticksBackend ----------------------------------------------------- }
84
85 procedure TLinuxJoysticksBackend.Initialize(const List: TJoystickList);
86 var
87 i, j : Integer;
88 NewJoystick: TJoystick;
89 NewBackendInfo: TLinuxJoystickBackendInfo;
90 begin
91 for i := 0 to 15 do
92 begin
93 NewJoystick := TJoystick.Create;
94 NewBackendInfo := TLinuxJoystickBackendInfo.Create;
95 NewJoystick.InternalBackendInfo := NewBackendInfo;
96
97 NewBackendInfo.Device := FpOpen( '/dev/input/js' + IntToStr( i ), O_RDONLY or O_NONBLOCK );
98 if NewBackendInfo.Device < 0 then
99 NewBackendInfo.Device := FpOpen( '/dev/js' + IntToStr( i ), O_RDONLY or O_NONBLOCK );
100
101 if NewBackendInfo.Device > -1 then
102 begin
103 NewBackendInfo.DeviceInitialized := true;
104 SetLength( NewJoystick.Info.Name, 256 );
105 { TODO: why is cast to TIOCtlRequest needed (with FPC 3.3.1-r43920),
106 we should probably fix the definition of JSIOCGNAME etc. instead. }
107 FpIOCtl( NewBackendInfo.Device, TIOCtlRequest(JSIOCGNAME), @NewJoystick.Info.Name[ 1 ] );
108 FpIOCtl( NewBackendInfo.Device, TIOCtlRequest(JSIOCGAXMAP), @NewBackendInfo.AxesMap[ 0 ] );
109 FpIOCtl( NewBackendInfo.Device, TIOCtlRequest(JSIOCGAXES), @NewJoystick.Info.Count.Axes );
110 FpIOCtl( NewBackendInfo.Device, TIOCtlRequest(JSIOCGBUTTONS), @NewJoystick.Info.Count.Buttons );
111
112 for j := 0 to NewJoystick.Info.Count.Axes - 1 do
113 with NewJoystick.Info do
114 case NewBackendInfo.AxesMap[ j ] of
115 2, 6: Caps := Caps or JOY_HAS_Z;
116 5, 7: Caps := Caps or JOY_HAS_R;
117 3: Caps := Caps or JOY_HAS_U;
118 4: Caps := Caps or JOY_HAS_V;
119 16, 17: Caps := Caps or JOY_HAS_POV;
120 end;
121
122 for j := 1 to 255 do
123 if NewJoystick.Info.Name[ j ] = #0 then
124 begin
125 SetLength( NewJoystick.Info.Name, j - 1 );
126 break;
127 end;
128
129 // Checking if joystick is a real one, because laptops with accelerometer can be detected as a joystick :)
130 if ( NewJoystick.Info.Count.Axes >= 2 ) and ( NewJoystick.Info.Count.Buttons > 0 ) then
131 begin
132 WritelnLog('CastleJoysticks Init', 'Find joy: %s (ID: %d); Axes: %d; Buttons: %d', [NewJoystick.Info.Name, I, NewJoystick.Info.Count.Axes, NewJoystick.Info.Count.Buttons]);
133 List.Add(NewJoystick);
134 end else
135 FreeAndNil(NewJoystick);
136 end else
137 FreeAndNil(NewJoystick);
138 end;
139 end;
140
141 procedure TLinuxJoysticksBackend.Poll(const List: TJoystickList;
142 const EventContainer: TJoysticks);
143 var
144 i : Integer;
145 Value: Single;
146 axis: Byte;
147 event : TLinuxJsEvent;
148 Joystick: TJoystick;
149 BackendInfo: TLinuxJoystickBackendInfo;
150 BytesRead: TSsize;
151 JoystickHasBeenDisconnected: Boolean;
152 begin
153 JoystickHasBeenDisconnected := false;
154 for I := 0 to List.Count - 1 do
155 begin
156 Joystick := List[I];
157 BackendInfo := Joystick.InternalBackendInfo as TLinuxJoystickBackendInfo;
158 BytesRead := FpRead( BackendInfo.Device, event, 8 );
159 if BytesRead = 8 then
160 repeat
161 case event.EventType of
162 JS_EVENT_AXIS:
163 begin
164 axis := JS_AXIS[ BackendInfo.AxesMap[ event.number ] ];
165 Value := event.value / 32767;
166 { Y axis should be 1 when pointing up, -1 when pointing down.
167 This is consistent with CGE 2D coordinate system
168 (and standard math 2D coordinate system). }
169 if Axis = JOY_AXIS_Y then
170 Value := -Value;
171 Joystick.State.Axis[ axis ] := Value;
172 if Assigned(EventContainer.OnAxisMove) then EventContainer.OnAxisMove(Joystick, axis, Value);
173 end;
174 JS_EVENT_BUTTON:
175 case event.value of
176 0:
177 begin
178 if Joystick.State.BtnDown[ event.number ] then
179 begin
180 Joystick.State.BtnUp[ event.number ] := True;
181 Joystick.State.BtnPress [ event.number ] := False;
182 if Assigned(EventContainer.OnButtonUp) then EventContainer.OnButtonUp(Joystick, event.number);
183 Joystick.State.BtnCanPress[ event.number ] := True;
184 end;
185
186 Joystick.State.BtnDown[ event.number ] := False;
187 end;
188 1:
189 begin
190 Joystick.State.BtnDown[ event.number ] := True;
191 if Assigned(EventContainer.OnButtonDown) then EventContainer.OnButtonDown(Joystick, event.number);
192 Joystick.State.BtnUp [ event.number ] := False;
193 if Joystick.State.BtnCanPress[ event.number ] then
194 begin
195 Joystick.State.BtnPress [ event.number ] := True;
196 if Assigned(EventContainer.OnButtonPress) then EventContainer.OnButtonPress(Joystick, event.number);
197 Joystick.State.BtnCanPress[ event.number ] := False;
198 end;
199 end;
200 end;
201 end;
202 BytesRead := FpRead( BackendInfo.Device, event, 8 );
203 until BytesRead <> 8
204 else
205 if fpgeterrno <> EAGAIN then
206 begin
207 WritelnLog('Joystick error: possibly "%s" was disconnected.', [Joystick.Info.Name]);
208 JoystickHasBeenDisconnected := true;
209 end;
210 end;
211 if JoystickHasBeenDisconnected then
212 if Assigned(Joysticks.OnDisconnect) then
213 Joysticks.OnDisconnect;
214 end;
215
216 end.