SDL 2.0
SDL_haikujoystick.cc
Go to the documentation of this file.
1/*
2 Simple DirectMedia Layer
3 Copyright (C) 1997-2019 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_joystick.h"
34#include "../SDL_sysjoystick.h"
35#include "../SDL_joystick_c.h"
36
37
38/* The maximum number of joysticks we'll detect */
39#define MAX_JOYSTICKS 16
40
41/* A list of available joysticks */
42 static char *SDL_joyport[MAX_JOYSTICKS];
43 static char *SDL_joyname[MAX_JOYSTICKS];
44
45/* The private structure used to keep track of a joystick */
46 struct joystick_hwdata
47 {
48 BJoystick *stick;
49 uint8 *new_hats;
50 int16 *new_axes;
51 };
52
53 static int numjoysticks = 0;
54
55/* Function to scan the system for joysticks.
56 * Joystick 0 should be the system default joystick.
57 * It should return 0, or -1 on an unrecoverable fatal error.
58 */
59 static int HAIKU_JoystickInit(void)
60 {
61 BJoystick joystick;
62 int i;
63 int32 nports;
64 char name[B_OS_NAME_LENGTH];
65
66 /* Search for attached joysticks */
67 nports = joystick.CountDevices();
68 numjoysticks = 0;
69 SDL_memset(SDL_joyport, 0, (sizeof SDL_joyport));
70 SDL_memset(SDL_joyname, 0, (sizeof SDL_joyname));
71 for (i = 0; (numjoysticks < MAX_JOYSTICKS) && (i < nports); ++i)
72 {
73 if (joystick.GetDeviceName(i, name) == B_OK) {
74 if (joystick.Open(name) != B_ERROR) {
75 BString stick_name;
76 joystick.GetControllerName(&stick_name);
77 SDL_joyport[numjoysticks] = SDL_strdup(name);
78 SDL_joyname[numjoysticks] = SDL_strdup(stick_name.String());
80 joystick.Close();
81 }
82 }
83 }
84 return (numjoysticks);
85 }
86
87 static int HAIKU_JoystickGetCount(void)
88 {
89 return numjoysticks;
90 }
91
92 static void HAIKU_JoystickDetect(void)
93 {
94 }
95
96/* Function to get the device-dependent name of a joystick */
97 static const char *HAIKU_JoystickGetDeviceName(int device_index)
98 {
99 return SDL_joyname[device_index];
100 }
101
102 static int HAIKU_JoystickGetDevicePlayerIndex(int device_index)
103 {
104 return -1;
105 }
106
107/* Function to perform the mapping from device index to the instance id for this index */
108 static SDL_JoystickID HAIKU_JoystickGetDeviceInstanceID(int device_index)
109 {
110 return device_index;
111 }
112
113 static void HAIKU_JoystickClose(SDL_Joystick * joystick);
114
115/* Function to open a joystick for use.
116 The joystick to open is specified by the device index.
117 This should fill the nbuttons and naxes fields of the joystick structure.
118 It returns 0, or -1 if there is an error.
119 */
120 static int HAIKU_JoystickOpen(SDL_Joystick * joystick, int device_index)
121 {
122 BJoystick *stick;
123
124 /* Create the joystick data structure */
125 joystick->instance_id = device_index;
126 joystick->hwdata = (struct joystick_hwdata *)
127 SDL_malloc(sizeof(*joystick->hwdata));
128 if (joystick->hwdata == NULL) {
129 return SDL_OutOfMemory();
130 }
131 SDL_memset(joystick->hwdata, 0, sizeof(*joystick->hwdata));
132 stick = new BJoystick;
133 joystick->hwdata->stick = stick;
134
135 /* Open the requested joystick for use */
136 if (stick->Open(SDL_joyport[device_index]) == B_ERROR) {
137 HAIKU_JoystickClose(joystick);
138 return SDL_SetError("Unable to open joystick");
139 }
140
141 /* Set the joystick to calibrated mode */
142 stick->EnableCalibration();
143
144 /* Get the number of buttons, hats, and axes on the joystick */
145 joystick->nbuttons = stick->CountButtons();
146 joystick->naxes = stick->CountAxes();
147 joystick->nhats = stick->CountHats();
148
149 joystick->hwdata->new_axes = (int16 *)
150 SDL_malloc(joystick->naxes * sizeof(int16));
151 joystick->hwdata->new_hats = (uint8 *)
152 SDL_malloc(joystick->nhats * sizeof(uint8));
153 if (!joystick->hwdata->new_hats || !joystick->hwdata->new_axes) {
154 HAIKU_JoystickClose(joystick);
155 return SDL_OutOfMemory();
156 }
157
158 /* We're done! */
159 return 0;
160 }
161
162/* Function to update the state of a joystick - called as a device poll.
163 * This function shouldn't update the joystick structure directly,
164 * but instead should call SDL_PrivateJoystick*() to deliver events
165 * and update joystick device state.
166 */
167 static void HAIKU_JoystickUpdate(SDL_Joystick * joystick)
168 {
169 static const Uint8 hat_map[9] = {
179 };
180
181 BJoystick *stick;
182 int i;
183 int16 *axes;
184 uint8 *hats;
185 uint32 buttons;
186
187 /* Set up data pointers */
188 stick = joystick->hwdata->stick;
189 axes = joystick->hwdata->new_axes;
190 hats = joystick->hwdata->new_hats;
191
192 /* Get the new joystick state */
193 stick->Update();
194 stick->GetAxisValues(axes);
195 stick->GetHatValues(hats);
196 buttons = stick->ButtonValues();
197
198 /* Generate axis motion events */
199 for (i = 0; i < joystick->naxes; ++i) {
201 }
202
203 /* Generate hat change events */
204 for (i = 0; i < joystick->nhats; ++i) {
206 }
207
208 /* Generate button events */
209 for (i = 0; i < joystick->nbuttons; ++i) {
211 buttons >>= 1;
212 }
213 }
214
215/* Function to close a joystick after use */
216 static void HAIKU_JoystickClose(SDL_Joystick * joystick)
217 {
218 if (joystick->hwdata) {
219 joystick->hwdata->stick->Close();
220 delete joystick->hwdata->stick;
221 SDL_free(joystick->hwdata->new_hats);
222 SDL_free(joystick->hwdata->new_axes);
223 SDL_free(joystick->hwdata);
224 }
225 }
226
227/* Function to perform any system-specific joystick related cleanup */
228 static void HAIKU_JoystickQuit(void)
229 {
230 int i;
231
232 for (i = 0; i < numjoysticks; ++i) {
233 SDL_free(SDL_joyport[i]);
234 }
235 SDL_joyport[0] = NULL;
236
237 for (i = 0; i < numjoysticks; ++i) {
238 SDL_free(SDL_joyname[i]);
239 }
240 SDL_joyname[0] = NULL;
241 }
242
243 static SDL_JoystickGUID HAIKU_JoystickGetDeviceGUID( int device_index )
244 {
246 /* the GUID is just the first 16 chars of the name for now */
247 const char *name = HAIKU_JoystickGetDeviceName( device_index );
248 SDL_zero( guid );
249 SDL_memcpy( &guid, name, SDL_min( sizeof(guid), SDL_strlen( name ) ) );
250 return guid;
251 }
252
253 static int HAIKU_JoystickRumble(SDL_Joystick * joystick, Uint16 low_frequency_rumble, Uint16 high_frequency_rumble, Uint32 duration_ms)
254 {
255 return SDL_Unsupported();
256 }
257
259 {
260 HAIKU_JoystickInit,
261 HAIKU_JoystickGetCount,
262 HAIKU_JoystickDetect,
263 HAIKU_JoystickGetDeviceName,
264 HAIKU_JoystickGetDevicePlayerIndex,
265 HAIKU_JoystickGetDeviceGUID,
266 HAIKU_JoystickGetDeviceInstanceID,
267 HAIKU_JoystickOpen,
268 HAIKU_JoystickRumble,
269 HAIKU_JoystickUpdate,
270 HAIKU_JoystickClose,
271 HAIKU_JoystickQuit,
272 };
273
274} // extern "C"
275
276#endif /* SDL_JOYSTICK_HAIKU */
277
278/* vi: set ts=4 sw=4 expandtab: */
#define SDL_SetError
#define SDL_memset
#define SDL_malloc
#define SDL_strlen
#define SDL_free
#define SDL_strdup
#define SDL_memcpy
#define SDL_OutOfMemory()
Definition: SDL_error.h:52
#define SDL_Unsupported()
Definition: SDL_error.h:53
int SDL_PrivateJoystickHat(SDL_Joystick *joystick, Uint8 hat, Uint8 value)
Definition: SDL_joystick.c:890
int SDL_PrivateJoystickAxis(SDL_Joystick *joystick, Uint8 axis, Sint16 value)
Definition: SDL_joystick.c:833
int SDL_PrivateJoystickButton(SDL_Joystick *joystick, Uint8 button, Uint8 state)
Definition: SDL_joystick.c:966
#define SDL_HAT_LEFTDOWN
Definition: SDL_joystick.h:337
#define SDL_HAT_LEFT
Definition: SDL_joystick.h:333
#define SDL_HAT_RIGHT
Definition: SDL_joystick.h:331
#define SDL_HAT_RIGHTUP
Definition: SDL_joystick.h:334
#define SDL_HAT_LEFTUP
Definition: SDL_joystick.h:336
Sint32 SDL_JoystickID
Definition: SDL_joystick.h:81
#define SDL_HAT_DOWN
Definition: SDL_joystick.h:332
#define SDL_HAT_RIGHTDOWN
Definition: SDL_joystick.h:335
#define SDL_HAT_UP
Definition: SDL_joystick.h:330
#define SDL_HAT_CENTERED
Definition: SDL_joystick.h:329
GLuint const GLchar * name
#define SDL_zero(x)
Definition: SDL_stdinc.h:416
uint32_t Uint32
Definition: SDL_stdinc.h:203
#define SDL_min(x, y)
Definition: SDL_stdinc.h:406
uint16_t Uint16
Definition: SDL_stdinc.h:191
uint8_t Uint8
Definition: SDL_stdinc.h:179
SDL_JoystickDriver SDL_HAIKU_JoystickDriver
static int numjoysticks
return Display return Display Bool Bool int int int return Display XEvent Bool(*) XPointer return Display return Display Drawable _Xconst char unsigned int unsigned int return Display Pixmap Pixmap XColor XColor unsigned int unsigned int return Display _Xconst char char int char return Display Visual unsigned int int int char unsigned int unsigned int in i)
Definition: SDL_x11sym.h:50
#define NULL
Definition: begin_code.h:167
SDL_JoystickGUID guid
SDL_Joystick * joystick