summaryrefslogtreecommitdiff
path: root/contrib/SDL-3.2.8/src/joystick/haiku
diff options
context:
space:
mode:
Diffstat (limited to 'contrib/SDL-3.2.8/src/joystick/haiku')
-rw-r--r--contrib/SDL-3.2.8/src/joystick/haiku/SDL_haikujoystick.cc315
1 files changed, 315 insertions, 0 deletions
diff --git a/contrib/SDL-3.2.8/src/joystick/haiku/SDL_haikujoystick.cc b/contrib/SDL-3.2.8/src/joystick/haiku/SDL_haikujoystick.cc
new file mode 100644
index 0000000..977e4fc
--- /dev/null
+++ b/contrib/SDL-3.2.8/src/joystick/haiku/SDL_haikujoystick.cc
@@ -0,0 +1,315 @@
1/*
2 Simple DirectMedia Layer
3 Copyright (C) 1997-2025 Sam Lantinga <slouken@libsdl.org>
4
5 This software is provided 'as-is', without any express or implied
6 warranty. In no event will the authors be held liable for any damages
7 arising from the use of this software.
8
9 Permission is granted to anyone to use this software for any purpose,
10 including commercial applications, and to alter it and redistribute it
11 freely, subject to the following restrictions:
12
13 1. The origin of this software must not be misrepresented; you must not
14 claim that you wrote the original software. If you use this software
15 in a product, an acknowledgment in the product documentation would be
16 appreciated but is not required.
17 2. Altered source versions must be plainly marked as such, and must not be
18 misrepresented as being the original software.
19 3. This notice may not be removed or altered from any source distribution.
20*/
21#include "SDL_internal.h"
22
23#ifdef SDL_JOYSTICK_HAIKU
24
25// This is the Haiku implementation of the SDL joystick API
26
27#include <support/String.h>
28#include <device/Joystick.h>
29
30extern "C"
31{
32
33#include "../SDL_sysjoystick.h"
34#include "../SDL_joystick_c.h"
35
36
37// The maximum number of joysticks we'll detect
38#define MAX_JOYSTICKS 16
39
40// A list of available joysticks
41 static char *SDL_joyport[MAX_JOYSTICKS];
42 static char *SDL_joyname[MAX_JOYSTICKS];
43
44// The private structure used to keep track of a joystick
45 struct joystick_hwdata
46 {
47 BJoystick *stick;
48 uint8 *new_hats;
49 int16 *new_axes;
50 };
51
52 static int numjoysticks = 0;
53
54 static bool HAIKU_JoystickInit(void)
55 {
56 BJoystick joystick;
57 int i;
58 int32 nports;
59 char name[B_OS_NAME_LENGTH];
60
61 // Search for attached joysticks
62 nports = joystick.CountDevices();
63 numjoysticks = 0;
64 SDL_memset(SDL_joyport, 0, sizeof(SDL_joyport));
65 SDL_memset(SDL_joyname, 0, sizeof(SDL_joyname));
66 for (i = 0; (numjoysticks < MAX_JOYSTICKS) && (i < nports); ++i) {
67 if (joystick.GetDeviceName(i, name) == B_OK) {
68 if (joystick.Open(name) != B_ERROR) {
69 BString stick_name;
70 joystick.GetControllerName(&stick_name);
71 SDL_joyport[numjoysticks] = SDL_strdup(name);
72 SDL_joyname[numjoysticks] = SDL_CreateJoystickName(0, 0, NULL, stick_name.String());
73 numjoysticks++;
74 joystick.Close();
75
76 SDL_PrivateJoystickAdded(numjoysticks);
77 }
78 }
79 }
80 return true;
81 }
82
83 static int HAIKU_JoystickGetCount(void)
84 {
85 return numjoysticks;
86 }
87
88 static void HAIKU_JoystickDetect(void)
89 {
90 }
91
92 static bool HAIKU_JoystickIsDevicePresent(Uint16 vendor_id, Uint16 product_id, Uint16 version, const char *name)
93 {
94 // We don't override any other drivers
95 return false;
96 }
97
98 static const char *HAIKU_JoystickGetDeviceName(int device_index)
99 {
100 return SDL_joyname[device_index];
101 }
102
103 static const char *HAIKU_JoystickGetDevicePath(int device_index)
104 {
105 return SDL_joyport[device_index];
106 }
107
108 static int HAIKU_JoystickGetDeviceSteamVirtualGamepadSlot(int device_index)
109 {
110 return -1;
111 }
112
113 static int HAIKU_JoystickGetDevicePlayerIndex(int device_index)
114 {
115 return -1;
116 }
117
118 static void HAIKU_JoystickSetDevicePlayerIndex(int device_index, int player_index)
119 {
120 }
121
122 static SDL_JoystickID HAIKU_JoystickGetDeviceInstanceID(int device_index)
123 {
124 return device_index + 1;
125 }
126
127 static void HAIKU_JoystickClose(SDL_Joystick *joystick);
128
129 static bool HAIKU_JoystickOpen(SDL_Joystick *joystick, int device_index)
130 {
131 BJoystick *stick;
132
133 // Create the joystick data structure
134 joystick->hwdata = (struct joystick_hwdata *) SDL_calloc(1, sizeof(*joystick->hwdata));
135 if (joystick->hwdata == NULL) {
136 return false;
137 }
138 stick = new BJoystick;
139 joystick->hwdata->stick = stick;
140
141 // Open the requested joystick for use
142 if (stick->Open(SDL_joyport[device_index]) == B_ERROR) {
143 HAIKU_JoystickClose(joystick);
144 return SDL_SetError("Unable to open joystick");
145 }
146
147 // Set the joystick to calibrated mode
148 stick->EnableCalibration();
149
150 // Get the number of buttons, hats, and axes on the joystick
151 joystick->nbuttons = stick->CountButtons();
152 joystick->naxes = stick->CountAxes();
153 joystick->nhats = stick->CountHats();
154
155 joystick->hwdata->new_axes = (int16 *) SDL_calloc(joystick->naxes, sizeof(int16));
156 joystick->hwdata->new_hats = (uint8 *) SDL_calloc(joystick->nhats, sizeof(uint8));
157 if (!joystick->hwdata->new_hats || !joystick->hwdata->new_axes) {
158 HAIKU_JoystickClose(joystick);
159 return false;
160 }
161
162 // We're done!
163 return true;
164 }
165
166/* Function to update the state of a joystick - called as a device poll.
167 * This function shouldn't update the joystick structure directly,
168 * but instead should call SDL_PrivateJoystick*() to deliver events
169 * and update joystick device state.
170 */
171 static void HAIKU_JoystickUpdate(SDL_Joystick *joystick)
172 {
173 static const Uint8 hat_map[9] = {
174 SDL_HAT_CENTERED,
175 SDL_HAT_UP,
176 SDL_HAT_RIGHTUP,
177 SDL_HAT_RIGHT,
178 SDL_HAT_RIGHTDOWN,
179 SDL_HAT_DOWN,
180 SDL_HAT_LEFTDOWN,
181 SDL_HAT_LEFT,
182 SDL_HAT_LEFTUP
183 };
184
185 BJoystick *stick;
186 int i;
187 int16 *axes;
188 uint8 *hats;
189 uint32 buttons;
190 Uint64 timestamp = SDL_GetTicksNS();
191
192 // Set up data pointers
193 stick = joystick->hwdata->stick;
194 axes = joystick->hwdata->new_axes;
195 hats = joystick->hwdata->new_hats;
196
197 // Get the new joystick state
198 stick->Update();
199 stick->GetAxisValues(axes);
200 stick->GetHatValues(hats);
201 buttons = stick->ButtonValues();
202
203 // Generate axis motion events
204 for (i = 0; i < joystick->naxes; ++i) {
205 SDL_SendJoystickAxis(timestamp, joystick, i, axes[i]);
206 }
207
208 // Generate hat change events
209 for (i = 0; i < joystick->nhats; ++i) {
210 SDL_SendJoystickHat(timestamp, joystick, i, hat_map[hats[i]]);
211 }
212
213 // Generate button events
214 for (i = 0; i < joystick->nbuttons; ++i) {
215 bool down = ((buttons & 0x01) != 0);
216 SDL_SendJoystickButton(timestamp, joystick, i, down);
217 buttons >>= 1;
218 }
219 }
220
221// Function to close a joystick after use
222 static void HAIKU_JoystickClose(SDL_Joystick *joystick)
223 {
224 if (joystick->hwdata) {
225 joystick->hwdata->stick->Close();
226 delete joystick->hwdata->stick;
227 SDL_free(joystick->hwdata->new_hats);
228 SDL_free(joystick->hwdata->new_axes);
229 SDL_free(joystick->hwdata);
230 }
231 }
232
233// Function to perform any system-specific joystick related cleanup
234 static void HAIKU_JoystickQuit(void)
235 {
236 int i;
237
238 for (i = 0; i < numjoysticks; ++i) {
239 SDL_free(SDL_joyport[i]);
240 }
241 SDL_joyport[0] = NULL;
242
243 for (i = 0; i < numjoysticks; ++i) {
244 SDL_free(SDL_joyname[i]);
245 }
246 SDL_joyname[0] = NULL;
247 }
248
249 static SDL_GUID HAIKU_JoystickGetDeviceGUID(int device_index)
250 {
251 // the GUID is just the name for now
252 const char *name = HAIKU_JoystickGetDeviceName(device_index);
253 return SDL_CreateJoystickGUIDForName(name);
254 }
255
256 static bool HAIKU_JoystickRumble(SDL_Joystick *joystick, Uint16 low_frequency_rumble, Uint16 high_frequency_rumble)
257 {
258 return SDL_Unsupported();
259 }
260
261
262 static bool HAIKU_JoystickRumbleTriggers(SDL_Joystick *joystick, Uint16 left_rumble, Uint16 right_rumble)
263 {
264 return SDL_Unsupported();
265 }
266
267 static bool HAIKU_JoystickGetGamepadMapping(int device_index, SDL_GamepadMapping *out)
268 {
269 return false;
270 }
271
272 static bool HAIKU_JoystickSetLED(SDL_Joystick *joystick, Uint8 red, Uint8 green, Uint8 blue)
273 {
274 return SDL_Unsupported();
275 }
276
277
278 static bool HAIKU_JoystickSendEffect(SDL_Joystick *joystick, const void *data, int size)
279 {
280 return SDL_Unsupported();
281 }
282
283 static bool HAIKU_JoystickSetSensorsEnabled(SDL_Joystick *joystick, bool enabled)
284 {
285 return SDL_Unsupported();
286 }
287
288 SDL_JoystickDriver SDL_HAIKU_JoystickDriver =
289 {
290 HAIKU_JoystickInit,
291 HAIKU_JoystickGetCount,
292 HAIKU_JoystickDetect,
293 HAIKU_JoystickIsDevicePresent,
294 HAIKU_JoystickGetDeviceName,
295 HAIKU_JoystickGetDevicePath,
296 HAIKU_JoystickGetDeviceSteamVirtualGamepadSlot,
297 HAIKU_JoystickGetDevicePlayerIndex,
298 HAIKU_JoystickSetDevicePlayerIndex,
299 HAIKU_JoystickGetDeviceGUID,
300 HAIKU_JoystickGetDeviceInstanceID,
301 HAIKU_JoystickOpen,
302 HAIKU_JoystickRumble,
303 HAIKU_JoystickRumbleTriggers,
304 HAIKU_JoystickSetLED,
305 HAIKU_JoystickSendEffect,
306 HAIKU_JoystickSetSensorsEnabled,
307 HAIKU_JoystickUpdate,
308 HAIKU_JoystickClose,
309 HAIKU_JoystickQuit,
310 HAIKU_JoystickGetGamepadMapping
311 };
312
313} // extern "C"
314
315#endif // SDL_JOYSTICK_HAIKU