1 module bindbc.jsl.joyshocklibrary;
2 
3 /**
4  * API difference: I decided to use an enumerator instead of individually defined values.
5  *
6  * Stores the values associated with types of possible controller types.
7  */
8 enum JoyShockTypes {
9 	JoyCon_Left     =   1,
10 	JoyCon_Right    =   2,
11 	ProController   =   3,
12 	DualShock4      =   4
13 }
14 /**
15  * API difference: I decided to use an enumerator instead of individually defined values.
16  *
17  * Stores the values associated with types of potential JoyCon splits.
18  */
19 enum JoyConSplitType {
20 	Left            =   1,
21 	Right           =   2,
22 	Full            =   3
23 }
24 /**
25  * API difference: I decided to use an enumerator instead of individually defined values.
26  *
27  * Stores the values associated with button events. For the right-hand-side buttons, south-east-west-north system 
28  * is used to avoid ambiguity between Microsoft and Nintendo systems. (Offsets)
29  */
30 enum ButtonOffsets {
31 	Up = 0,
32 	Down = 1,
33 	Left = 2,
34 	Right = 3,
35 	Plus = 4,
36 	Options = 4,
37 	Minus = 5,
38 	Share = 5,
39 	LClick = 6,
40 	RClick = 7,
41 	L = 8,
42 	R = 9,
43 	ZL = 10,
44 	ZR = 11,
45 	S = 12,
46 	E = 13,
47 	W = 14,
48 	N = 15,
49 	Home = 16,
50 	PS = 16,
51 	Capture = 17,
52 	TouchpadClick = 17,
53 	SL = 18,
54 	SR = 19,
55 }
56 /**
57  * API difference: I decided to use an enumerator instead of individually defined values.
58  *
59  * Stores the values associated with button events. For the right-hand-side buttons, south-east-west-north system 
60  * is used to avoid ambiguity between Microsoft and Nintendo systems. (Bitmasks)
61  */
62 enum ButtonMasks {
63 	Up = 1 << ButtonOffsets.Up,
64 	Down = 1 << ButtonOffsets.Down,
65 	Left = 1 << ButtonOffsets.Left,
66 	Right = 1 << ButtonOffsets.Right,
67 	Plus = 1 << ButtonOffsets.Plus,
68 	Options = 1 << ButtonOffsets.Options,
69 	Minus = 1 << ButtonOffsets.Minus,
70 	Share = 1 << ButtonOffsets.Share,
71 	LClick = 1 << ButtonOffsets.LClick,
72 	RClick = 1 << ButtonOffsets.RClick,
73 	L = 1 << ButtonOffsets.L,
74 	R = 1 << ButtonOffsets.R,
75 	ZL = 1 << ButtonOffsets.ZL,
76 	ZR = 1 << ButtonOffsets.ZR,
77 	S = 1 << ButtonOffsets.S,
78 	E = 1 << ButtonOffsets.E,
79 	W = 1 << ButtonOffsets.W,
80 	N = 1 << ButtonOffsets.N,
81 	Home = 1 << ButtonOffsets.Home,
82 	PS = 1 << ButtonOffsets.PS,
83 	Capture = 1 << ButtonOffsets.Capture,
84 	TouchpadClick = 1 << ButtonOffsets.TouchpadClick,
85 	SL = 1 << ButtonOffsets.SL,
86 	SR = 1 << ButtonOffsets.SR,
87 }
88 
89 struct JOY_SHOCK_STATE {
90 	int buttons;
91 	float lTrigger;
92 	float rTrigger;
93 	float stickLX;
94 	float stickLY;
95 	float stickRX;
96 	float stickRY;
97 }
98 
99 struct IMU_STATE {
100 	float accelX;
101 	float accelY;
102 	float accelZ;
103 	float gyroX;
104 	float gyroY;
105 	float gyroZ;
106 }
107 
108 version(BindJSL_Static) {
109 extern(C) @nogc nothrow:
110 	JOY_SHOCK_STATE JslGetSimpleState(int deviceId);
111 	IMU_STATE JslGetIMUState(int deviceId);
112 	int JslGetButtons(int deviceId);
113 
114 	// get thumbsticks
115 	float JslGetLeftX(int deviceId);
116 	float JslGetLeftY(int deviceId);
117 	float JslGetRightX(int deviceId);
118 	float JslGetRightY(int deviceId);
119 
120 	// get triggers. Switch controllers don't have analogue triggers, but will report 0.0 or 1.0 so they can be used in the same way as others
121 	float JslGetLeftTrigger(int deviceId);
122 	float JslGetRightTrigger(int deviceId);
123 
124 	// get gyro
125 	float JslGetGyroX(int deviceId);
126 	float JslGetGyroY(int deviceId);
127 	float JslGetGyroZ(int deviceId);
128 
129 	// get accelerometor
130 	float JslGetAccelX(int deviceId);
131 	float JslGetAccelY(int deviceId);
132 	float JslGetAccelZ(int deviceId);
133 
134 	// analog parameters have different resolutions depending on device
135 	float JslGetStickStep(int deviceId);
136 	float JslGetTriggerStep(int deviceId);
137 	float JslGetPollRate(int deviceId);
138 
139 	// calibration
140 	void JslResetContinuousCalibration(int deviceId);
141 	void JslStartContinuousCalibration(int deviceId);
142 	void JslPauseContinuousCalibration(int deviceId);
143 	void JslGetCalibrationOffset(int deviceId, ref float xOffset, ref float yOffset, ref float zOffset);
144 	void JslSetCalibrationOffset(int deviceId, float xOffset, float yOffset, float zOffset);
145 
146 	/// this function will get called for each input event from each controller
147 	void JslSetCallback(void function(int, JOY_SHOCK_STATE, JOY_SHOCK_STATE, IMU_STATE, IMU_STATE, float) callback);
148 
149 	/// what kind of controller is this?
150 	int JslGetControllerType(int deviceId);
151 	/// is this a left, right, or full controller?
152 	int JslGetControllerSplitType(int deviceId);
153 	/// what colour is the controller (not all controllers support this; those that don't will report white)
154 	int JslGetControllerColour(int deviceId);
155 	/// set controller light colour (not all controllers have a light whose colour can be set, but that just means nothing will be done when this is called -- no harm)
156 	void JslSetLightColour(int deviceId, int colour);
157 	/// set controller rumble
158 	void JslSetRumble(int deviceId, int smallRumble, int bigRumble);
159 	/// set controller player number indicator (not all controllers have a number indicator which can be set, but that just means nothing will be done when this is called -- no harm)
160 	void JslSetPlayerNumber(int deviceId, int number);
161 } else {
162 	extern(C) @nogc nothrow {
163 		alias pJslGetSimpleState = JOY_SHOCK_STATE function(int deviceId);
164 		alias pJslGetIMUState = IMU_STATE function(int deviceId);
165 		alias pJslGetButtons = int function(int deviceId);
166 		alias pJslGetLeftX = float function(int deviceId);
167 		alias pJslGetLeftY = float function(int deviceId);
168 		alias pJslGetRightX = float function(int deviceId);
169 		alias pJslGetRightY = float function(int deviceId);
170 		alias pJslGetLeftTrigger = float function(int deviceId);
171 		alias pJslGetRightTrigger = float function(int deviceId);
172 		alias pJslGetGyroX = float function(int deviceId);
173 		alias pJslGetGyroY = float function(int deviceId);
174 		alias pJslGetGyroZ = float function(int deviceId);
175 		alias pJslGetAccelX = float function(int deviceId);
176 		alias pJslGetAccelY = float function(int deviceId);
177 		alias pJslGetAccelZ = float function(int deviceId);
178 		alias pJslGetStickStep = float function(int deviceId);
179 		alias pJslGetTriggerStep = float function(int deviceId);
180 		alias pJslGetPollRate = float function(int deviceId);
181 		alias pJslResetContinuousCalibration = void function(int deviceId);
182 		alias pJslStartContinuousCalibration = void function(int deviceId);
183 		alias pJslPauseContinuousCalibration = void function(int deviceId);
184 		alias pJslGetCalibrationOffset = void function(int deviceId, ref float xOffset, ref float yOffset, ref float zOffset);
185 		alias pJslSetCalibrationOffset = void function(int deviceId, float xOffset, float yOffset, float zOffset);
186 		alias pJslSetCallback = void function(void function(int, JOY_SHOCK_STATE, JOY_SHOCK_STATE, IMU_STATE, IMU_STATE, float) callback);
187 		alias pJslGetControllerType = int function(int deviceId);
188 		alias pJslGetControllerSplitType = int function(int deviceId);
189 		alias pJslGetControllerColour = int function(int deviceId);
190 		alias pJslSetLightColour = void function(int deviceId, int colour);
191 		alias pJslSetRumble = void function(int deviceId, int smallRumble, int bigRumble);
192 		alias pJslSetPlayerNumber = void function(int deviceId, int number);
193 	}
194 	__gshared {
195 		pJslGetSimpleState JslGetSimpleState;
196 		pJslGetIMUState JslGetIMUState;
197 		pJslGetButtons JslGetButtons;
198 		pJslGetLeftX JslGetLeftX;
199 		pJslGetLeftY JslGetLeftY;
200 		pJslGetRightX JslGetRightX;
201 		pJslGetRightY JslGetRightY;
202 		pJslGetLeftTrigger JslGetLeftTrigger;
203 		pJslGetRightTrigger JslGetRightTrigger;
204 		pJslGetGyroX JslGetGyroX;
205 		pJslGetGyroY JslGetGyroY;
206 		pJslGetGyroZ JslGetGyroZ;
207 		pJslGetAccelX JslGetAccelX;
208 		pJslGetAccelY JslGetAccelY;
209 		pJslGetAccelZ JslGetAccelZ;
210 		pJslGetStickStep JslGetStickStep;
211 		pJslGetTriggerStep JslGetTriggerStep;
212 		pJslGetPollRate JslGetPollRate;
213 		pJslResetContinuousCalibration JslResetContinuousCalibration;
214 		pJslStartContinuousCalibration JslStartContinuousCalibration;
215 		pJslPauseContinuousCalibration JslPauseContinuousCalibration;
216 		pJslGetCalibrationOffset JslGetCalibrationOffset;
217 		pJslSetCalibrationOffset JslSetCalibrationOffset;
218 		pJslSetCallback JslSetCallback;
219 		pJslGetControllerType JslGetControllerType;
220 		pJslGetControllerSplitType JslGetControllerSplitType;
221 		pJslGetControllerColour JslGetControllerColour;
222 		pJslSetLightColour JslSetLightColour;
223 		pJslSetRumble JslSetRumble;
224 		pJslSetPlayerNumber JslSetPlayerNumber;
225 	}
226 }